diff --git a/Cargo.lock b/Cargo.lock index 6b6104a53..e1c11b17c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -389,6 +389,39 @@ dependencies = [ "vstd", ] +[[package]] +name = "clazel-jit" +version = "0.0.0" +dependencies = [ + "clazel-a64", + "clazel-codecheck", + "clazel-js-vm", + "clazel-lir", + "clazel-x64", + "vstd", +] + +[[package]] +name = "clazel-js" +version = "0.0.0" +dependencies = [ + "clazel-js-builtins", + "clazel-js-heap", + "clazel-js-syntax", + "clazel-js-vm", +] + +[[package]] +name = "clazel-js-builtins" +version = "0.0.0" +dependencies = [ + "clazel-js-heap", + "clazel-js-number", + "clazel-js-syntax", + "clazel-js-vm", + "clazel-json", +] + [[package]] name = "clazel-js-heap" version = "0.0.0" @@ -423,7 +456,9 @@ dependencies = [ name = "clazel-js-vm" version = "0.0.0" dependencies = [ + "clazel-js-heap", "clazel-js-lexer", + "clazel-js-number", "clazel-js-syntax", "vstd", ] @@ -663,6 +698,12 @@ dependencies = [ [[package]] name = "clazel-web" version = "0.0.0" +dependencies = [ + "clazel-css", + "clazel-dom", + "clazel-selectors", + "clazel-treebuilder", +] [[package]] name = "clazel-webidl" diff --git a/crates/clazel-codecheck/Cargo.toml b/crates/clazel-codecheck/Cargo.toml index df5d7a19b..eab44466f 100644 --- a/crates/clazel-codecheck/Cargo.toml +++ b/crates/clazel-codecheck/Cargo.toml @@ -4,7 +4,7 @@ version = "0.0.0" edition.workspace = true license.workspace = true publish.workspace = true -description = "The JIT's code validators (docs/phases/jit.md item J.6), the x86-64 one and the A64 one: what an accepted code blob is, the contract of the validator, and the executable validator, proven sound and complete against the spec. The second x86-64 validator (dataflow) is there, proven sound, and the x86-64 confinement theorem (exit criterion 2) is stated and proven in confine; A64's confinement proof does not exist yet." +description = "The JIT's code validators (docs/phases/jit.md item J.6), the x86-64 one and the A64 one: what an accepted code blob is, the contract of the validator, and the executable validator, proven sound and complete against the spec. The second x86-64 validator (dataflow) is there, proven sound, and the confinement theorem (exit criterion 2) is stated once and proven for x86-64 and for A64 in confine." [dependencies] vstd = "=0.0.0-2026-10-04-0306" diff --git a/crates/clazel-codecheck/src/confine/a64/chain.rs b/crates/clazel-codecheck/src/confine/a64/chain.rs new file mode 100644 index 000000000..9399f5416 --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/chain.rs @@ -0,0 +1,300 @@ +//! **Unit starts and their facts** (A64): what an accepted blob's scan guarantees, +//! phrased for the machine-level invariant. The units the scan takes from +//! instruction 0 ([`chain_facts`]) are the positions the invariant allows; +//! every entry and jump target is one, with no facts; each such unit has a +//! recognized shape and fits in the blob; the unit after one is again one. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The 4 bytes of the canonical encoding of `i` (kept opaque so that the +/// large `encode` is unfolded only where it must be). +#[verifier::opaque] +pub open spec fn word_of(i: Instr) -> Seq { + word_bytes(encode(i)) +} + +/// **What a configuration the sandbox may hand over says**, without the +/// large `encode` unfolded: the blob is the words [`word_of`] of the +/// instructions, which are valid, the policy holds, and the layout is +/// disjoint and below `2^47`. +pub proof fn lemma_cfg(c: Cfg) + requires + cfg_ok(c), + ensures + policy(c.ins, c.entries), + all_valid(c.ins), + c.blob.len() == 4 * c.ins.len(), + word_of_len(), + forall|j: int| + 0 <= j < c.ins.len() ==> c.blob.subrange(4 * j, 4 * j + 4) == #[trigger] word_of( + c.ins[j], + ), + c.bases.len() == abi::num_regions(), + c.bounds.len() == abi::num_regions(), + 0 <= c.code, + c.code % 4 == 0, + c.code + c.blob.len() <= lim(), + 0 <= c.ctx, + c.ctx + abi::ctx_size() <= lim(), + disjoint(c.code, c.blob.len() as int, c.ctx, abi::ctx_size()), + forall|k: int| 0 <= k < abi::num_regions() ==> #[trigger] region_ok(c, k), + 0 <= c.sp < p2(64), + 0 <= c.lr < p2(64), + disjoint(c.lr, 1, c.code, c.blob.len() as int), +{ + reveal(cfg_ok); + reveal(word_of); + assert(decodes_to(c.blob, c.ins)); + assert forall|j: int| 0 <= j < c.ins.len() implies c.blob.subrange(4 * j, 4 * j + 4) + == #[trigger] word_of(c.ins[j]) by { + assert(c.blob.subrange(4 * j, 4 * j + 4) == word_bytes(encode(c.ins[j]))); + } +} + +/// Every [`word_of`] has 4 bytes. +pub open spec fn word_of_len() -> bool { + forall|i: Instr| #[trigger] word_of(i).len() == 4 +} + +/// The proof of [`word_of_len`]. +pub proof fn lemma_word_of_len() + ensures + word_of_len(), +{ + reveal(word_of); +} + +/// Forgetting twice is forgetting once. +pub proof fn lemma_forgotten_idem(c: Cfg, j: int, fs: Facts) + ensures + forgotten(c, j, forgotten(c, j, fs)) == forgotten(c, j, fs), +{ +} + +/// A fact list reached at a unit start is already forgotten there, and the +/// facts reached by continuing from `j` to `t2` are the ones reached by +/// continuing from `t`. +pub proof fn lemma_chain_append(c: Cfg, j: int, fs: Facts, t: int, f: Facts, t2: int) + requires + 0 <= j <= t <= t2, + chain_facts(c, j, fs, t) == Some(f), + ensures + f == forgotten(c, t, f), + chain_facts(c, j, fs, t2) == chain_facts(c, t, f, t2), + decreases t - j, +{ + let fs0 = forgotten(c, j, fs); + if j == t { + assert(f == fs0); + lemma_forgotten_idem(c, j, fs); + } else { + let u = unit_at(c.ins, j, fs0)->Some_0; + assert(unit_at(c.ins, j, fs0) is Some); + assert(u.len >= 1 && j + u.len <= t); + lemma_chain_append(c, j + u.len, u.facts, t, f, t2); + } +} + +/// The facts at the next unit start: the unit's facts, forgotten there. +pub proof fn lemma_next_start(c: Cfg, t: int, f: Facts, u: Unit) + requires + start_facts(c, t) == Some(f), + 0 <= t < c.ins.len(), + unit_at(c.ins, t, f) == Some(u), + u.len >= 1, + ensures + start_facts(c, t + u.len) == Some(forgotten(c, t + u.len, u.facts)), +{ + lemma_chain_append(c, 0, no_facts(), t, f, t + u.len); + lemma_forgotten_idem(c, t, f); + reveal_with_fuel(chain_facts, 2); + assert(forgotten(c, t, f) == f); + assert(chain_facts(c, t, f, t + u.len) == Some(forgotten(c, t + u.len, u.facts))); +} + +/// The scan from a reached unit start is the scan with the facts of that +/// start. +pub proof fn lemma_scan_reach(c: Cfg, j: int, fs: Facts, t: int, f: Facts) + requires + 0 <= j <= t < c.ins.len(), + scan(c.ins, all_targets(c.ins, c.entries), j, fs) is Some, + chain_facts(c, j, fs, t) == Some(f), + ensures + scan(c.ins, all_targets(c.ins, c.entries), t, f) is Some, + decreases t - j, +{ + let tg = all_targets(c.ins, c.entries); + let fs0 = forgotten(c, j, fs); + reveal_with_fuel(scan, 2); + if j == t { + lemma_forgotten_idem(c, j, fs); + assert(forgotten(c, t, f) == f); + } else { + let u = unit_at(c.ins, j, fs0)->Some_0; + lemma_scan_reach(c, j + u.len, u.facts, t, f); + } +} + +/// **Every reached unit start has a unit that fits in the blob.** +pub proof fn lemma_unit_ok(c: Cfg, t: int, f: Facts) + requires + cfg_ok(c), + start_facts(c, t) == Some(f), + 0 <= t < c.ins.len(), + ensures + unit_at(c.ins, t, f) is Some, + unit_at(c.ins, t, f)->Some_0.len >= 1, + t + unit_at(c.ins, t, f)->Some_0.len <= c.ins.len(), +{ + let tg = all_targets(c.ins, c.entries); + lemma_cfg(c); + assert(policy(c.ins, c.entries)); + assert(scan(c.ins, tg, 0, no_facts()) is Some); + lemma_scan_reach(c, 0, no_facts(), t, f); + lemma_chain_append(c, 0, no_facts(), t, f, t); + reveal_with_fuel(scan, 2); + assert(forgotten(c, t, f) == f); +} + +/// Every offset the scan lists as a unit start is a unit start that the +/// chain from `j` reaches. +pub proof fn lemma_scan_chain_starts(c: Cfg, j: int, fs: Facts, x: int) + requires + 0 <= j, + scan(c.ins, all_targets(c.ins, c.entries), j, fs) is Some, + seq_has(scan(c.ins, all_targets(c.ins, c.entries), j, fs)->Some_0, x), + ensures + exists|t: int| + j <= t < c.ins.len() && x == start_of(c.ins, t) && chain_facts(c, j, fs, t) is Some, + decreases c.ins.len() - j, +{ + let tg = all_targets(c.ins, c.entries); + reveal_with_fuel(scan, 2); + if j < c.ins.len() { + let here = start_of(c.ins, j); + let fs0 = forgotten(c, j, fs); + let u = unit_at(c.ins, j, fs0)->Some_0; + let rest = scan(c.ins, tg, j + u.len, u.facts)->Some_0; + crate::a64::acc_lemmas::lemma_seq_has_concat(seq![here], rest, x); + crate::a64::acc_lemmas::lemma_seq_has_single(here, x); + if x != here { + lemma_scan_chain_starts(c, j + u.len, u.facts, x); + let t = choose|t: int| + j + u.len <= t < c.ins.len() && x == start_of(c.ins, t) && chain_facts( + c, + j + u.len, + u.facts, + t, + ) is Some; + assert(chain_facts(c, j, fs, t) is Some); + } else { + assert(chain_facts(c, j, fs, j) is Some); + } + } +} + +/// **Every entry and every jump target is a reached unit start.** +pub proof fn lemma_targets_are_starts(c: Cfg, x: int) + requires + cfg_ok(c), + seq_has(all_targets(c.ins, c.entries), x), + ensures + exists|t: int| + 0 <= t < c.ins.len() && x == start_of(c.ins, t) && start_facts(c, t) is Some, +{ + let tg = all_targets(c.ins, c.entries); + let starts = scan(c.ins, tg, 0, no_facts())->Some_0; + lemma_cfg(c); + assert(policy(c.ins, c.entries)); + crate::a64::acc_lemmas::lemma_subset_elim(tg, starts, x); + lemma_scan_chain_starts(c, 0, no_facts(), x); +} + +/// An entry is a target of the scan. +pub proof fn lemma_entry_in_targets(c: Cfg, e: int) + requires + seq_has(c.entries, e), + ensures + seq_has(all_targets(c.ins, c.entries), e), +{ + crate::a64::acc_lemmas::lemma_seq_has_concat(c.entries, jump_targets(c.ins, 0), e); +} + +/// The jump targets of `ins[j'..]` include the target of the jump at `j`. +pub proof fn lemma_jump_target_from(ins: Seq, j0: int, j: int, x: int) + requires + 0 <= j0 <= j < ins.len(), + jump_rel(ins[j]) is Some, + x == start_of(ins, j) + jump_rel(ins[j])->Some_0, + ensures + seq_has(jump_targets(ins, j0), x), + decreases j - j0, +{ + crate::a64::validate::lemma_jump_targets_step(ins, j0); + let here: Seq = match jump_rel(ins[j0]) { + Some(r) => seq![start_of(ins, j0) + r], + None => Seq::empty(), + }; + if j0 == j { + crate::a64::acc_lemmas::lemma_seq_has_concat(here, jump_targets(ins, j0 + 1), x); + crate::a64::acc_lemmas::lemma_seq_has_single(x, x); + } else { + lemma_jump_target_from(ins, j0 + 1, j, x); + crate::a64::acc_lemmas::lemma_seq_has_concat(here, jump_targets(ins, j0 + 1), x); + } +} + +/// **The target of every direct jump of the blob is a reached unit start** +/// (so it is where a unit begins, and what the scan knows there is nothing). +pub proof fn lemma_jump_target_is_start(c: Cfg, j: int) + requires + cfg_ok(c), + 0 <= j < c.ins.len(), + jump_rel(c.ins[j]) is Some, + ensures + ({ + let x = start_of(c.ins, j) + jump_rel(c.ins[j])->Some_0; + exists|t: int| + 0 <= t < c.ins.len() && x == start_of(c.ins, t) && start_facts(c, t) is Some + && start_facts(c, t)->Some_0 == no_facts() + }), +{ + let x = start_of(c.ins, j) + jump_rel(c.ins[j])->Some_0; + lemma_jump_target_from(c.ins, 0, j, x); + crate::a64::acc_lemmas::lemma_seq_has_concat(c.entries, jump_targets(c.ins, 0), x); + lemma_targets_are_starts(c, x); + let t = choose|t: int| + 0 <= t < c.ins.len() && x == start_of(c.ins, t) && start_facts(c, t) is Some; + // The facts at a target are `no_facts()`: forgotten. + let f = start_facts(c, t)->Some_0; + lemma_chain_append(c, 0, no_facts(), t, f, t); + assert(seq_has(all_targets(c.ins, c.entries), start_of(c.ins, t))); + assert(f == forgotten(c, t, f)); + assert(f == no_facts()); +} + +/// An entry is a unit start with no facts. +pub proof fn lemma_entry_is_start(c: Cfg, e: int) + requires + cfg_ok(c), + seq_has(c.entries, e), + ensures + exists|t: int| + 0 <= t < c.ins.len() && e == start_of(c.ins, t) && start_facts(c, t) is Some + && start_facts(c, t)->Some_0 == no_facts(), +{ + lemma_entry_in_targets(c, e); + lemma_targets_are_starts(c, e); + let t = choose|t: int| + 0 <= t < c.ins.len() && e == start_of(c.ins, t) && start_facts(c, t) is Some; + let f = start_facts(c, t)->Some_0; + lemma_chain_append(c, 0, no_facts(), t, f, t); + assert(f == forgotten(c, t, f)); +} + +} // verus! diff --git a/crates/clazel-codecheck/src/confine/a64/check.rs b/crates/clazel-codecheck/src/confine/a64/check.rs new file mode 100644 index 000000000..efc70dc40 --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/check.rs @@ -0,0 +1,572 @@ +//! **The bounds-check sequence** (A64, seven instructions, [`check_instr`]): +//! each step keeps the invariant [`check_mid`] describes, and after the last +//! the index register is checked ([`Fact::Idx`]) for the region. This is where +//! the carry and zero flags are used: `b.hs` falls through only if the `adds` +//! did not wrap, `b.hi` only if the sum is at most the bound, and the final +//! `csel ..., lo` does not move because the index is below the bound. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The fact of register `r` under [`check_mid_facts`]. +pub proof fn lemma_fact_of_mid(fs0: Facts, ch: Chk, r: u8) + ensures + fact_of(check_mid_facts(fs0, ch), r) == if r == ch.t || r == ch.bnd || r == ch.i { + Fact::Unknown + } else { + fact_of(fs0, r) + }, +{ + let a = set_fact(fs0, ch.t, Fact::Unknown); + let b = set_fact(a, ch.bnd, Fact::Unknown); + crate::a64::unit_exec::lemma_fact_of_set(fs0, ch.t, Fact::Unknown, r); + crate::a64::unit_exec::lemma_fact_of_set(a, ch.bnd, Fact::Unknown, r); + crate::a64::unit_exec::lemma_fact_of_set(b, ch.i, Fact::Unknown, r); +} + +/// The fact of register `r` after the check ([`check_facts`]). +pub proof fn lemma_fact_of_fin(fs0: Facts, ch: Chk, r: u8) + ensures + fact_of( + set_fact( + set_fact(set_fact(fs0, ch.t, Fact::Unknown), ch.bnd, Fact::Unknown), + ch.i, + Fact::Idx(ch.k, ch.n), + ), + r, + ) == if r == ch.i { + Fact::Idx(ch.k, ch.n) + } else if r == ch.t || r == ch.bnd { + Fact::Unknown + } else { + fact_of(fs0, r) + }, +{ + let a = set_fact(fs0, ch.t, Fact::Unknown); + let b = set_fact(a, ch.bnd, Fact::Unknown); + crate::a64::unit_exec::lemma_fact_of_set(fs0, ch.t, Fact::Unknown, r); + crate::a64::unit_exec::lemma_fact_of_set(a, ch.bnd, Fact::Unknown, r); + crate::a64::unit_exec::lemma_fact_of_set(b, ch.i, Fact::Idx(ch.k, ch.n), r); +} + +/// What every step of a bounds check has in hand. +pub proof fn lemma_check_prelude(c: Cfg, s: State, j0: int, m: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, m), + !exit_unit_at(c.ins, j0), + check_unit_at(c.ins, j0), + ensures + start_facts(c, j0) is Some, + ({ + let fs0 = start_facts(c, j0)->Some_0; + let ch = check_params(c.ins, j0)->Some_0; + check_ok(ch) && unit_at(c.ins, j0, fs0) == Some( + Unit { len: 7, facts: check_facts(fs0, c.ins, j0) }, + ) && 0 <= m < 7 && c.ins[j0 + m] == check_instr(m, ch) && (forall|q: int| + 0 <= q < 7 ==> #[trigger] c.ins[j0 + q] == check_instr(q, ch)) && ch.t < 31 + && ch.i < 31 && ch.bnd < 31 && facts_hold(c, s, check_mid_facts(fs0, ch)) + && base_inv(c, s) && mem_read(s.mem, c.ctx + abi::bound_off(ch.k), 8) == Some( + c.bounds[ch.k], + ) && 0 <= ch.k < abi::num_regions() && 1 <= ch.n <= 8 && (m > 0 ==> check_mid( + c, + s, + ch, + fs0, + m, + )) && (m == 0 ==> facts_hold(c, s, fs0)) + }), +{ + lemma_cur_instr(c, s, j0, m); + lemma_base_inv_split(c, s); + lemma_cfg(c); + let fs0 = start_facts(c, j0)->Some_0; + let ch = check_params(c.ins, j0)->Some_0; + let u = unit_at(c.ins, j0, fs0)->Some_0; + assert(u == (Unit { len: 7, facts: check_facts(fs0, c.ins, j0) })); + assert(check_ok(ch)); + assert(c.ins[j0 + 0] == check_instr(0, ch)); + assert(c.ins[j0 + 1] == check_instr(1, ch)); + assert(c.ins[j0 + 2] == check_instr(2, ch)); + assert(c.ins[j0 + 3] == check_instr(3, ch)); + assert(c.ins[j0 + 4] == check_instr(4, ch)); + assert(c.ins[j0 + 5] == check_instr(5, ch)); + assert(c.ins[j0 + 6] == check_instr(6, ch)); + let fsm = check_mid_facts(fs0, ch); + let a = set_fact(fs0, ch.t, Fact::Unknown); + let b = set_fact(a, ch.bnd, Fact::Unknown); + if m == 0 { + lemma_facts_unknown(c, s, fs0, ch.t); + lemma_facts_unknown(c, s, a, ch.bnd); + lemma_facts_unknown(c, s, b, ch.i); + } + assert(mem_read(s.mem, c.ctx + abi::bound_off(ch.k), 8) == Some(c.bounds[ch.k])); +} + +/// The scan's facts after the bounds check hold, if the check's own +/// invariant does and the registers are as at the end. +pub proof fn lemma_check_facts_hold(c: Cfg, s: State, s2: State, ch: Chk, fs0: Facts) + requires + facts_hold(c, s, check_mid_facts(fs0, ch)), + check_ok(ch), + s2.r == s.r, + 0 <= ch.k < abi::num_regions(), + x_read(s, ch.i, 64) + ch.n <= c.bounds[ch.k], + s.r.len() == 31, + ensures + facts_hold( + c, + s2, + set_fact( + set_fact(set_fact(fs0, ch.t, Fact::Unknown), ch.bnd, Fact::Unknown), + ch.i, + Fact::Idx(ch.k, ch.n), + ), + ), +{ + let fin = set_fact( + set_fact(set_fact(fs0, ch.t, Fact::Unknown), ch.bnd, Fact::Unknown), + ch.i, + Fact::Idx(ch.k, ch.n), + ); + let fsm = check_mid_facts(fs0, ch); + assert forall|r: u8| r < 32 implies fact_ok(c, s2, r, #[trigger] fact_of(fin, r)) by { + lemma_fact_of_fin(fs0, ch, r); + lemma_fact_of_mid(fs0, ch, r); + assert(fact_ok(c, s, r, fact_of(fsm, r))); + if r == ch.i { + assert(x_read(s2, r, 64) == x_read(s, r, 64)); + } else if r == ch.t || r == ch.bnd { + } else { + assert(x_read(s2, r, 64) == x_read(s, r, 64)); + } + } +} + +/// Instruction 0 of a bounds check, `ldr bnd, [x28, #(16k + 8)]`. +pub proof fn lemma_check_0(c: Cfg, s: State, j0: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, 0), + !exit_unit_at(c.ins, j0), + check_unit_at(c.ins, j0), + ensures + acc_ok(c, s, c.ins[j0]), + forall|s2: State| step(s, c.ins[j0]) == Some(s2) ==> step_post(c, s, c.ins[j0], s2), +{ + lemma_check_prelude(c, s, j0, 0); + lemma_cfg(c); + lemma_p2(); + let fs0 = start_facts(c, j0)->Some_0; + let ch = check_params(c.ins, j0)->Some_0; + let i = c.ins[j0]; + let fsm = check_mid_facts(fs0, ch); + let imm12 = (abi::bound_off(ch.k) / 8) as u16; + assert(i == (Instr::LoadStoreImm { + sf: Width::W64, + load: true, + t: ch.bnd, + n: abi::ctx_reg(), + imm12, + })); + assert(imm12 as int * 8 == abi::bound_off(ch.k)); + lemma_no_facts_hold(c, s); + assert(access_ok(no_facts(), mem_access(i)->Some_0)); + lemma_acc_ok(c, s, no_facts(), i); + lemma_not_exit(c, s, i); + lemma_region(c, ch.k); + lemma_base_inv_split(c, s); + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_step_straight(s, i, s2); + lemma_pc_succ(c, s, s2, j0, 0); + lemma_xr(s, abi::ctx_reg()); + let addr = c.ctx + abi::bound_off(ch.k); + assert(0 <= addr < p2(64)); + lemma_trunc64_id(addr); + assert(mem_read(s.mem, addr, 8) == Some(c.bounds[ch.k])); + let s1 = execute(s, i)->Some_0; + lemma_trunc64_id(c.bounds[ch.k]); + assert(s1 == x_set(s, ch.bnd, 64, c.bounds[ch.k])); + assert(s2.flag_c == s.flag_c && s2.flag_z == s.flag_z); + assert(written_gpr(i) == Some(ch.bnd)); + lemma_base_inv_after(c, s, s2, Some(ch.bnd)); + lemma_fact_of_mid(fs0, ch, ch.bnd); + lemma_facts_keep(c, s, s2, fsm, Some(ch.bnd)); + lemma_xr(s2, ch.bnd); + assert(x_read(s2, ch.bnd, 64) == c.bounds[ch.k]); + assert(mid_inv(c, s2, j0, fs0, 1)); + lemma_pos_mid(c, s, s2, j0, 0); + lemma_confined_same(c, s, s2); + assert(inv(c, s2)); + } +} + +/// Instruction 1, `adds t, i, #n`. +pub proof fn lemma_check_1(c: Cfg, s: State, j0: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, 1), + !exit_unit_at(c.ins, j0), + check_unit_at(c.ins, j0), + ensures + acc_ok(c, s, c.ins[j0 + 1]), + forall|s2: State| step(s, c.ins[j0 + 1]) == Some(s2) ==> step_post(c, s, c.ins[j0 + 1], s2), +{ + lemma_check_prelude(c, s, j0, 1); + lemma_cfg(c); + lemma_p2(); + let fs0 = start_facts(c, j0)->Some_0; + let ch = check_params(c.ins, j0)->Some_0; + let i = c.ins[j0 + 1]; + let fsm = check_mid_facts(fs0, ch); + assert(i == (Instr::AddSubImm { + sf: Width::W64, + sub_op: false, + setflags: true, + d: ch.t, + n: ch.i, + imm12: ch.n as u16, + sh: false, + })); + assert(mem_access(i) is None); + assert(acc_ok(c, s, i)); + lemma_not_exit(c, s, i); + lemma_xr(s, ch.i); + let iv = x_read(s, ch.i, 64); + let n = ch.n; + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_step_straight(s, i, s2); + lemma_pc_succ(c, s, s2, j0, 1); + assert(written_gpr(i) == Some(ch.t)); + lemma_base_inv_after(c, s, s2, Some(ch.t)); + lemma_fact_of_mid(fs0, ch, ch.t); + lemma_facts_keep(c, s, s2, fsm, Some(ch.t)); + lemma_adds_effect(s, i, ch.t, ch.i, ch.n as u16, s2); + assert(ch.n as u16 as int == ch.n); + lemma_xr(s2, ch.t); + lemma_xr(s2, ch.i); + lemma_xr(s2, ch.bnd); + assert(s2.r[ch.t as int] == (((iv + n) % p2(64)) as u64)); + vstd::arithmetic::div_mod::lemma_mod_bound(iv + n, p2(64)); + assert(x_read(s2, ch.t, 64) == (iv + n) % p2(64)); + assert(x_read(s2, ch.i, 64) == iv); + assert(x_read(s2, ch.bnd, 64) == x_read(s, ch.bnd, 64)); + assert(mid_inv(c, s2, j0, fs0, 2)); + lemma_pos_mid(c, s, s2, j0, 1); + lemma_confined_same(c, s, s2); + assert(inv(c, s2)); + } +} + +/// Instruction 2, `b.hs exit1`. +pub proof fn lemma_check_2(c: Cfg, s: State, j0: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, 2), + !exit_unit_at(c.ins, j0), + check_unit_at(c.ins, j0), + ensures + acc_ok(c, s, c.ins[j0 + 2]), + forall|s2: State| step(s, c.ins[j0 + 2]) == Some(s2) ==> step_post(c, s, c.ins[j0 + 2], s2), +{ + lemma_check_prelude(c, s, j0, 2); + lemma_cfg(c); + lemma_p2(); + let fs0 = start_facts(c, j0)->Some_0; + let ch = check_params(c.ins, j0)->Some_0; + let i = c.ins[j0 + 2]; + let fsm = check_mid_facts(fs0, ch); + assert(i == (Instr::BCond { cond: 2, imm19: ch.r1 })); + assert(mem_access(i) is None); + assert(acc_ok(c, s, i)); + lemma_not_exit(c, s, i); + lemma_base_inv_split(c, s); + lemma_xr(s, ch.i); + lemma_xr(s, ch.t); + let iv = x_read(s, ch.i, 64); + let tv = x_read(s, ch.t, 64); + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + assert(condition_holds(s, 2) == s.flag_c); + lemma_confined_same_branch(c, s, s2, i); + lemma_base_inv_after(c, s, s2, None); + lemma_confined_same(c, s, s2); + if s.flag_c { + assert(s2 == branch_to(s, s.pc as int + ch.r1 as int * 4)); + assert(jump_rel(i) == Some(ch.r1 as int * 4)); + lemma_pos_jump(c, s, s2, j0 + 2); + } else { + assert(s2 == branch_to(s, s.pc as int + 4)); + lemma_pc_succ(c, s, s2, j0, 2); + lemma_facts_keep(c, s, s2, fsm, None); + assert(iv + ch.n < p2(64)); + assert(tv == iv + ch.n) by { + vstd::arithmetic::div_mod::lemma_small_mod((iv + ch.n) as nat, p2(64) as nat); + } + lemma_xr(s2, ch.i); + lemma_xr(s2, ch.t); + lemma_xr(s2, ch.bnd); + assert(mid_inv(c, s2, j0, fs0, 3)); + lemma_pos_mid(c, s, s2, j0, 2); + } + assert(inv(c, s2)); + } +} + +/// Instruction 3, `cmp t, bnd`. +pub proof fn lemma_check_3(c: Cfg, s: State, j0: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, 3), + !exit_unit_at(c.ins, j0), + check_unit_at(c.ins, j0), + ensures + acc_ok(c, s, c.ins[j0 + 3]), + forall|s2: State| step(s, c.ins[j0 + 3]) == Some(s2) ==> step_post(c, s, c.ins[j0 + 3], s2), +{ + lemma_check_prelude(c, s, j0, 3); + lemma_cfg(c); + lemma_p2(); + let fs0 = start_facts(c, j0)->Some_0; + let ch = check_params(c.ins, j0)->Some_0; + let i = c.ins[j0 + 3]; + let fsm = check_mid_facts(fs0, ch); + assert(i == (Instr::AddSubShiftedReg { + sf: Width::W64, + sub_op: true, + setflags: true, + d: abi::sp_reg(), + n: ch.t, + m: ch.bnd, + shift_type: ShiftType::Lsl, + shift_amount: 0, + })); + assert(mem_access(i) is None); + assert(acc_ok(c, s, i)); + lemma_not_exit(c, s, i); + lemma_xr(s, ch.t); + lemma_xr(s, ch.bnd); + lemma_region(c, ch.k); + let tv = x_read(s, ch.t, 64); + let bv = x_read(s, ch.bnd, 64); + let b = c.bounds[ch.k]; + assert(bv == b); + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_step_straight(s, i, s2); + lemma_pc_succ(c, s, s2, j0, 3); + assert(written_gpr(i) == Some(31u8)); + lemma_cmp_effect(s, i, ch.t, ch.bnd, s2); + lemma_base_inv_after(c, s, s2, Some(31u8)); + lemma_facts_keep(c, s, s2, fsm, None); + lemma_xr(s2, ch.i); + lemma_xr(s2, ch.t); + lemma_xr(s2, ch.bnd); + assert(mid_inv(c, s2, j0, fs0, 4)); + lemma_pos_mid(c, s, s2, j0, 3); + lemma_confined_same(c, s, s2); + assert(inv(c, s2)); + } +} + +/// Instruction 4, `b.hi exit2`. +pub proof fn lemma_check_4(c: Cfg, s: State, j0: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, 4), + !exit_unit_at(c.ins, j0), + check_unit_at(c.ins, j0), + ensures + acc_ok(c, s, c.ins[j0 + 4]), + forall|s2: State| step(s, c.ins[j0 + 4]) == Some(s2) ==> step_post(c, s, c.ins[j0 + 4], s2), +{ + lemma_check_prelude(c, s, j0, 4); + lemma_cfg(c); + lemma_p2(); + let fs0 = start_facts(c, j0)->Some_0; + let ch = check_params(c.ins, j0)->Some_0; + let i = c.ins[j0 + 4]; + let fsm = check_mid_facts(fs0, ch); + assert(i == (Instr::BCond { cond: 8, imm19: ch.r2 })); + assert(mem_access(i) is None); + assert(acc_ok(c, s, i)); + lemma_not_exit(c, s, i); + lemma_base_inv_split(c, s); + lemma_xr(s, ch.i); + lemma_xr(s, ch.t); + let iv = x_read(s, ch.i, 64); + let tv = x_read(s, ch.t, 64); + let b = c.bounds[ch.k]; + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + assert(condition_holds(s, 8) == (s.flag_c && !s.flag_z)); + lemma_confined_same_branch(c, s, s2, i); + lemma_base_inv_after(c, s, s2, None); + lemma_confined_same(c, s, s2); + if s.flag_c && !s.flag_z { + assert(s2 == branch_to(s, s.pc as int + ch.r2 as int * 4)); + assert(jump_rel(i) == Some(ch.r2 as int * 4)); + lemma_pos_jump(c, s, s2, j0 + 4); + } else { + assert(s2 == branch_to(s, s.pc as int + 4)); + lemma_pc_succ(c, s, s2, j0, 4); + lemma_facts_keep(c, s, s2, fsm, None); + assert(tv <= b); + assert(iv + ch.n <= b); + lemma_xr(s2, ch.i); + lemma_xr(s2, ch.t); + lemma_xr(s2, ch.bnd); + assert(mid_inv(c, s2, j0, fs0, 5)); + lemma_pos_mid(c, s, s2, j0, 4); + } + assert(inv(c, s2)); + } +} + +/// Instruction 5, `cmp i, bnd`. +pub proof fn lemma_check_5(c: Cfg, s: State, j0: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, 5), + !exit_unit_at(c.ins, j0), + check_unit_at(c.ins, j0), + ensures + acc_ok(c, s, c.ins[j0 + 5]), + forall|s2: State| step(s, c.ins[j0 + 5]) == Some(s2) ==> step_post(c, s, c.ins[j0 + 5], s2), +{ + lemma_check_prelude(c, s, j0, 5); + lemma_cfg(c); + lemma_p2(); + let fs0 = start_facts(c, j0)->Some_0; + let ch = check_params(c.ins, j0)->Some_0; + let i = c.ins[j0 + 5]; + let fsm = check_mid_facts(fs0, ch); + assert(i == (Instr::AddSubShiftedReg { + sf: Width::W64, + sub_op: true, + setflags: true, + d: abi::sp_reg(), + n: ch.i, + m: ch.bnd, + shift_type: ShiftType::Lsl, + shift_amount: 0, + })); + assert(mem_access(i) is None); + assert(acc_ok(c, s, i)); + lemma_not_exit(c, s, i); + lemma_xr(s, ch.i); + lemma_xr(s, ch.bnd); + lemma_region(c, ch.k); + let iv = x_read(s, ch.i, 64); + let bv = x_read(s, ch.bnd, 64); + let b = c.bounds[ch.k]; + assert(bv == b); + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_step_straight(s, i, s2); + lemma_pc_succ(c, s, s2, j0, 5); + assert(written_gpr(i) == Some(31u8)); + lemma_cmp_effect(s, i, ch.i, ch.bnd, s2); + lemma_base_inv_after(c, s, s2, Some(31u8)); + lemma_facts_keep(c, s, s2, fsm, None); + lemma_xr(s2, ch.i); + lemma_xr(s2, ch.t); + lemma_xr(s2, ch.bnd); + assert(mid_inv(c, s2, j0, fs0, 6)); + lemma_pos_mid(c, s, s2, j0, 5); + lemma_confined_same(c, s, s2); + assert(inv(c, s2)); + } +} + +/// Instruction 6, `csel i, i, xzr, lo`, which does not move: the index is +/// below the bound. +pub proof fn lemma_check_6(c: Cfg, s: State, j0: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, 6), + !exit_unit_at(c.ins, j0), + check_unit_at(c.ins, j0), + ensures + acc_ok(c, s, c.ins[j0 + 6]), + forall|s2: State| step(s, c.ins[j0 + 6]) == Some(s2) ==> step_post(c, s, c.ins[j0 + 6], s2), +{ + lemma_check_prelude(c, s, j0, 6); + lemma_cfg(c); + lemma_p2(); + let fs0 = start_facts(c, j0)->Some_0; + let ch = check_params(c.ins, j0)->Some_0; + let i = c.ins[j0 + 6]; + assert(i == (Instr::CondSelect { + sf: Width::W64, + else_inv: false, + else_inc: false, + d: ch.i, + n: ch.i, + m: abi::sp_reg(), + cond: 3, + })); + assert(mem_access(i) is None); + assert(acc_ok(c, s, i)); + lemma_not_exit(c, s, i); + lemma_base_inv_split(c, s); + lemma_xr(s, ch.i); + let iv = x_read(s, ch.i, 64); + let b = c.bounds[ch.k]; + assert(iv + ch.n <= b); + assert(iv < b); + assert(!s.flag_c); + assert(condition_holds(s, 3)); + lemma_not_last(c, j0 + 6); + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_step_straight(s, i, s2); + lemma_pc_succ(c, s, s2, j0, 6); + assert(execute(s, i) == Some(x_set(s, ch.i, 64, iv))); + lemma_trunc64_id(iv); + assert(s2.r =~= s.r); + assert(written_gpr(i) == Some(ch.i)); + lemma_base_inv_after(c, s, s2, Some(ch.i)); + lemma_check_facts_hold(c, s, s2, ch, fs0); + assert(check_facts(fs0, c.ins, j0) == set_fact( + set_fact(set_fact(fs0, ch.t, Fact::Unknown), ch.bnd, Fact::Unknown), + ch.i, + Fact::Idx(ch.k, ch.n), + )); + lemma_pos_fall(c, s, s2, j0, 6); + lemma_confined_same(c, s, s2); + assert(inv(c, s2)); + } +} + +/// **The bounds-check sequence**: at any of its seven positions, the +/// accesses of the next instruction are in bounds and its step keeps the +/// invariant. +pub proof fn lemma_check_unit(c: Cfg, s: State, j0: int, m: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, m), + !exit_unit_at(c.ins, j0), + check_unit_at(c.ins, j0), + ensures + acc_ok(c, s, c.ins[j0 + m]), + forall|s2: State| step(s, c.ins[j0 + m]) == Some(s2) ==> step_post(c, s, c.ins[j0 + m], s2), +{ + lemma_check_prelude(c, s, j0, m); + if m == 0 { + lemma_check_0(c, s, j0); + } else if m == 1 { + lemma_check_1(c, s, j0); + } else if m == 2 { + lemma_check_2(c, s, j0); + } else if m == 3 { + lemma_check_3(c, s, j0); + } else if m == 4 { + lemma_check_4(c, s, j0); + } else if m == 5 { + lemma_check_5(c, s, j0); + } else { + lemma_check_6(c, s, j0); + } +} + +} // verus! diff --git a/crates/clazel-codecheck/src/confine/a64/flags.rs b/crates/clazel-codecheck/src/confine/a64/flags.rs new file mode 100644 index 000000000..ea984f14f --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/flags.rs @@ -0,0 +1,229 @@ +//! **The flags of `adds` and `cmp`** (A64): what `add_with_carry` leaves in +//! `PSTATE.N Z C V` after the two instructions the bounds check relies on. +//! `adds t, i, #n` sets the carry exactly when `i + n` wrapped; `cmp t, b` +//! (`subs xzr, t, b`) sets the carry exactly when `t >= b` and the zero flag +//! exactly when `t == b`. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The bits of an `nzcv` value assembled as `add_with_carry` does. +pub proof fn lemma_nzcv_bits(n: int, z: int, c: int, v: int) + requires + 0 <= n <= 1, + 0 <= z <= 1, + 0 <= c <= 1, + 0 <= v <= 1, + ensures + bit(n * 8 + z * 4 + c * 2 + v, 1) == c, + bit(n * 8 + z * 4 + c * 2 + v, 2) == z, +{ + lemma_p2(); + let x = n * 8 + z * 4 + c * 2 + v; + assert(bit(x, 1) == (x / 2) % 2); + assert(bit(x, 2) == (x / 4) % 2); + assert(x / 2 == n * 4 + z * 2 + c) by (nonlinear_arith) + requires + 0 <= v <= 1, + x == n * 8 + z * 4 + c * 2 + v, + ; + assert(x / 4 == n * 2 + z) by (nonlinear_arith) + requires + 0 <= v <= 1, + 0 <= c <= 1, + x == n * 8 + z * 4 + c * 2 + v, + ; + assert((n * 4 + z * 2 + c) % 2 == c) by (nonlinear_arith) + requires + 0 <= c <= 1, + ; + assert((n * 2 + z) % 2 == z) by (nonlinear_arith) + requires + 0 <= z <= 1, + ; +} + +/// **`add_with_carry` on 64 bits**: the result, and the carry and zero +/// flags, from a sum in `[0, 2^65)`. +pub proof fn lemma_awc64(x: int, y: int, cin: int) + requires + 0 <= x < p2(64), + 0 <= y < p2(64), + cin == 0 || cin == 1, + ensures + add_with_carry(64, x, y, cin).0 == (x + y + cin) % p2(64), + (bit(add_with_carry(64, x, y, cin).1, 1) == 1) == (x + y + cin >= p2(64)), + (bit(add_with_carry(64, x, y, cin).1, 2) == 1) == ((x + y + cin) % p2(64) == 0), +{ + lemma_p2(); + lemma_trunc64_id(x); + lemma_trunc64_id(y); + let us = x + y + cin; + let m = p2(64); + let result = trunc(64, us); + assert(result == us % m); + vstd::arithmetic::div_mod::lemma_mod_bound(us, m); + if us < m { + vstd::arithmetic::div_mod::lemma_small_mod(us as nat, m as nat); + assert(result == us); + } else { + assert(us - m < m); + vstd::arithmetic::div_mod::lemma_fundamental_div_mod_converse(us, m, 1, us - m); + assert(result == us - m); + } + let fn_ = bit(result, 63); + assert(0 <= fn_ <= 1) by { + vstd::arithmetic::div_mod::lemma_mod_bound(result / p2(63), 2); + } + let fz = if result == 0 { + 1int + } else { + 0 + }; + let fc = if result == us { + 0int + } else { + 1 + }; + let signed_sum = sint(64, x) + sint(64, y) + cin; + let fv = if sint(64, result) == signed_sum { + 0int + } else { + 1 + }; + let r = add_with_carry(64, x, y, cin); + assert(r.1 == fn_ * 8 + fz * 4 + fc * 2 + fv); + lemma_nzcv_bits(fn_, fz, fc, fv); +} + +/// **`adds t, i, #n`** with `i` and `n` below `2^64`: the result is the sum +/// modulo `2^64` and the carry is set exactly when the sum wrapped. +pub proof fn lemma_adds_flags(i: int, n: int) + requires + 0 <= i < p2(64), + 0 <= n < p2(64), + ensures + add_with_carry(64, i, n, 0).0 == (i + n) % p2(64), + (bit(add_with_carry(64, i, n, 0).1, 1) == 1) == (i + n >= p2(64)), +{ + lemma_awc64(i, n, 0); +} + +/// **`cmp t, b`** (`subs xzr, t, b`) with `t` and `b` below `2^64`: the carry +/// is set exactly when `t >= b` and the zero flag exactly when `t == b`. +pub proof fn lemma_cmp_flags(t: int, b: int) + requires + 0 <= t < p2(64), + 0 <= b < p2(64), + ensures + (bit(add_with_carry(64, t, not_vec(64, b), 1).1, 1) == 1) == (t >= b), + (bit(add_with_carry(64, t, not_vec(64, b), 1).1, 2) == 1) == (t == b), +{ + lemma_p2(); + lemma_trunc64_id(b); + let nb = not_vec(64, b); + assert(nb == p2(64) - 1 - b); + assert(0 <= nb < p2(64)); + lemma_awc64(t, nb, 1); + let us = t + nb + 1; + assert(us == t - b + p2(64)); + let m = p2(64); + if t >= b { + assert(us >= m); + assert(us - m < m); + vstd::arithmetic::div_mod::lemma_fundamental_div_mod_converse(us, m, 1, us - m); + assert(us % m == t - b); + } else { + assert(0 < us < m); + vstd::arithmetic::div_mod::lemma_small_mod(us as nat, m as nat); + assert(us % m == us); + } +} + +/// **The effect of `adds xt, xa, #imm`** (64-bit): the destination gets the sum +/// modulo `2^64` and the carry is set exactly when the sum wrapped; nothing +/// else but the program counter changes. +pub proof fn lemma_adds_effect(s: State, i: Instr, t: u8, a: u8, imm: u16, s2: State) + requires + s.r.len() == 31, + t < 31, + a < 31, + imm < 4096, + i == (Instr::AddSubImm { + sf: Width::W64, + sub_op: false, + setflags: true, + d: t, + n: a, + imm12: imm, + sh: false, + }), + step(s, i) == Some(s2), + ensures + s2.r == s.r.update(t as int, (((x_read(s, a, 64) + imm as int) % p2(64)) as u64)), + s2.flag_c == (x_read(s, a, 64) + imm as int >= p2(64)), + s2.sp == s.sp, + s2.mem == s.mem, + s2.vreg == s.vreg, + s2.pc as int == trunc(64, s.pc as int + 4), +{ + lemma_p2(); + lemma_xr(s, a); + let iv = x_read(s, a, 64); + lemma_adds_flags(iv, imm as int); + let r = add_with_carry(64, iv, imm as int, 0); + assert(execute(s, i) == Some(x_set(set_nzcv(s, r.1), t, 64, r.0))); + lemma_trunc64_range(r.0); + lemma_trunc64_id(r.0); + assert(s2.flag_c == (bit(r.1, 1) == 1)); +} + +/// **The effect of `cmp xa, xb`** (`subs xzr, xa, xb`, 64-bit): only the +/// flags and the program counter change; the carry is set exactly when +/// `a >= b` and the zero flag exactly when `a == b`. +pub proof fn lemma_cmp_effect(s: State, i: Instr, a: u8, bb: u8, s2: State) + requires + s.r.len() == 31, + a < 31, + bb < 31, + i == (Instr::AddSubShiftedReg { + sf: Width::W64, + sub_op: true, + setflags: true, + d: abi::sp_reg(), + n: a, + m: bb, + shift_type: ShiftType::Lsl, + shift_amount: 0, + }), + step(s, i) == Some(s2), + ensures + s2.r == s.r, + s2.flag_c == (x_read(s, a, 64) >= x_read(s, bb, 64)), + s2.flag_z == (x_read(s, a, 64) == x_read(s, bb, 64)), + s2.sp == s.sp, + s2.mem == s.mem, + s2.vreg == s.vreg, + s2.pc as int == trunc(64, s.pc as int + 4), +{ + lemma_p2(); + lemma_xr(s, a); + lemma_xr(s, bb); + let x = x_read(s, a, 64); + let y = x_read(s, bb, 64); + lemma_cmp_flags(x, y); + lemma_trunc64_id(y); + assert(shift_reg(s, bb, ShiftType::Lsl, 0nat, 64nat) == y) by { + assert(lsl(64, y, 0nat) == trunc(64, y * p2(0))); + } + let r = add_with_carry(64, x, not_vec(64, y), 1); + assert(execute(s, i) == Some(x_set(set_nzcv(s, r.1), abi::sp_reg(), 64, r.0))); + assert(s2.flag_c == (bit(r.1, 1) == 1)); + assert(s2.flag_z == (bit(r.1, 2) == 1)); +} + +} // verus! diff --git a/crates/clazel-codecheck/src/confine/a64/frame.rs b/crates/clazel-codecheck/src/confine/a64/frame.rs new file mode 100644 index 000000000..536ea75ef --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/frame.rs @@ -0,0 +1,287 @@ +//! **What a step of `clazel-a64` leaves alone**: the frame lemmas of the A64 +//! confinement proof. `execute` writes the destination register of +//! [`written_gpr`], `SP` only for [`writes_sp`], and memory only in a store's +//! bytes ([`mem_set`]); a step then moves the program counter. Also the facts +//! about [`mem_read`] and [`mem_set`] the proof uses: a store changes only the +//! bytes it names, and a load or a store's fault depends only on the bytes it +//! touches. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The values of `2^k` the proof uses. +pub proof fn lemma_p2() + ensures + p2(0) == 1, + p2(1) == 2, + p2(2) == 4, + p2(3) == 8, + p2(64) == 18446744073709551616, + p2(32) == 4294967296, +{ + vstd::arithmetic::power2::lemma2_to64(); + vstd::arithmetic::power2::lemma2_to64_rest(); +} + +/// Truncating to 64 bits is the identity on a value in range. +pub proof fn lemma_trunc64_id(x: int) + requires + 0 <= x < p2(64), + ensures + trunc(64, x) == x, +{ + lemma_p2(); + vstd::arithmetic::div_mod::lemma_small_mod(x as nat, p2(64) as nat); +} + +/// A truncation to 64 bits is in range. +pub proof fn lemma_trunc64_range(x: int) + ensures + 0 <= trunc(64, x) < p2(64), +{ + lemma_p2(); + vstd::arithmetic::div_mod::lemma_mod_bound(x, p2(64)); +} + +/// Reading a general register of a well-formed state in 64 bits. +pub proof fn lemma_xr(s: State, r: u8) + requires + s.r.len() == 31, + r < 31, + ensures + x_read(s, r, 64) == s.r[r as int] as int, + 0 <= x_read(s, r, 64) < p2(64), +{ + lemma_p2(); + let v = s.r[r as int] as int; + assert(0 <= v < 18446744073709551616); + lemma_trunc64_id(v); +} + +/// Reading register 31 gives zero. +pub proof fn lemma_xr31(s: State) + ensures + x_read(s, 31, 64) == 0, +{ +} + +/// The memory part of a store: the domain is unchanged and every byte outside +/// `[addr, addr + size)` is unchanged. +pub open spec fn store_frame(s: State, s2: State, addr: int, size: int) -> bool { + s2.mem.dom() =~= s.mem.dom() && forall|x: int| + (x < addr || x >= addr + size) && s.mem.dom().contains(x) ==> #[trigger] s2.mem[x] + == s.mem[x] +} + +/// An instruction is a store. +pub open spec fn is_store(i: Instr) -> bool { + match mem_access(i) { + Some(a) => a.store, + None => false, + } +} + +/// A successful [`mem_set`] changes only the bytes it names, and keeps the +/// domain, if the bytes do not wrap around `2^64`. +pub proof fn lemma_mem_set_frame(m: Map, a: int, n: nat, v: int, m2: Map) + requires + mem_set(m, a, n, v) == Some(m2), + 0 <= a, + a + n <= p2(64), + ensures + m2.dom() =~= m.dom(), + forall|x: int| (x < a || x >= a + n) && m.dom().contains(x) ==> #[trigger] m2[x] == m[x], + decreases n, +{ + lemma_p2(); + if n == 0 { + } else { + lemma_trunc64_id(a); + assert(m.dom().contains(a)); + let m3 = mem_set(m, a + 1, (n - 1) as nat, v / 256)->Some_0; + assert(mem_set(m, a + 1, (n - 1) as nat, v / 256) == Some(m3)); + lemma_mem_set_frame(m, a + 1, (n - 1) as nat, v / 256, m3); + assert(m2 == m3.insert(a, (v % 256) as u8)); + assert(m2.dom() =~= m.dom()); + assert forall|x: int| (x < a || x >= a + n) && m.dom().contains(x) implies #[trigger] m2[x] + == m[x] by { + assert(x != a); + assert(x < a + 1 || x >= a + 1 + (n - 1)); + } + } +} + +/// A store faults in `m2` if and only if in `m`, if they agree on which of +/// its bytes are mapped. +pub proof fn lemma_mem_set_some_same(m: Map, m2: Map, a: int, n: nat, v: int) + requires + 0 <= a, + a + n <= p2(64), + forall|x: int| a <= x < a + n ==> #[trigger] m2.dom().contains(x) == m.dom().contains(x), + ensures + mem_set(m2, a, n, v) is Some == mem_set(m, a, n, v) is Some, + decreases n, +{ + lemma_p2(); + if n > 0 { + lemma_trunc64_id(a); + assert(m2.dom().contains(a) == m.dom().contains(a)); + lemma_mem_set_some_same(m, m2, a + 1, (n - 1) as nat, v / 256); + } +} + +/// A load gives the same in `m2` as in `m`, if they agree on its bytes. +pub proof fn lemma_mem_read_same(m: Map, m2: Map, a: int, n: nat) + requires + 0 <= a, + a + n <= p2(64), + forall|x: int| + a <= x < a + n ==> (#[trigger] m2.dom().contains(x) == m.dom().contains(x) && ( + m.dom().contains(x) ==> m2[x] == m[x])), + ensures + mem_read(m2, a, n) == mem_read(m, a, n), + decreases n, +{ + lemma_p2(); + if n > 0 { + lemma_trunc64_id(a); + lemma_mem_read_same(m, m2, a + 1, (n - 1) as nat); + } +} + +/// The frame of one instruction's `execute`: the program counter, the lengths, +/// the registers other than the destination, `SP` unless written, memory +/// unless a store. +pub open spec fn exec_frame(s: State, s1: State, i: Instr) -> bool { + s1.r.len() == 31 && s1.vreg.len() == 32 && s1.pc == s.pc && (forall|r: int| + 0 <= r < 31 && written_gpr(i) != Some(r as u8) ==> #[trigger] s1.r[r] == s.r[r]) && ( + !writes_sp(i) ==> s1.sp == s.sp) && (!is_store(i) ==> s1.mem == s.mem) +} + +/// **The frame of `execute`**, for every instruction of the subset: it +/// writes the destination register (and `SP` for `ADD`/`SUB` into 31), the +/// flags and the vector registers, and, for a store, exactly the bytes +/// [`mem_set`] writes at the address [`eff_addr`] computes. +pub proof fn lemma_execute_frame(s: State, i: Instr, s1: State) + requires + s.r.len() == 31, + s.vreg.len() == 32, + valid_instr(i), + execute(s, i) == Some(s1), + ensures + exec_frame(s, s1, i), + is_store(i) ==> exists|data: int| + #[trigger] mem_set( + s.mem, + eff_addr(s, mem_access(i)->Some_0), + mem_access(i)->Some_0.size as nat, + data, + ) == Some(s1.mem), +{ + lemma_p2(); + match i { + Instr::AddSubShiftedReg { .. } => {}, + Instr::AddSubImm { .. } => {}, + Instr::MulAddSub { .. } => {}, + Instr::Div { .. } => {}, + Instr::LogicalShiftedReg { .. } => {}, + Instr::ShiftVariable { .. } => {}, + Instr::Bitfield { .. } => {}, + Instr::MoveWide { .. } => {}, + Instr::CondSelect { .. } => {}, + Instr::LoadStoreImm { sf, load, t, n, imm12 } => { + let datasize = datasize(sf); + let a = mem_access(i)->Some_0; + assert(eff_addr(s, a) == trunc(64, x_read(s, n, 64) + imm12 as int * nbytes(sf))); + if !load { + let data = x_read(s, t, datasize); + assert(mem_set(s.mem, eff_addr(s, a), a.size as nat, data) == Some(s1.mem)); + } + }, + Instr::LoadStoreReg { sf, load, t, n, m, s: sbit } => { + let datasize = datasize(sf); + let a = mem_access(i)->Some_0; + if !load { + let data = x_read(s, t, datasize); + assert(mem_set(s.mem, eff_addr(s, a), a.size as nat, data) == Some(s1.mem)); + } + }, + Instr::FpLoadStoreImm { load, t, n, imm12 } => { + let a = mem_access(i)->Some_0; + if !load { + let data = v_read(s, t, 64); + assert(mem_set(s.mem, eff_addr(s, a), a.size as nat, data) == Some(s1.mem)); + } + }, + Instr::FpLoadStoreReg { load, t, n, m, s: sbit } => { + let a = mem_access(i)->Some_0; + if !load { + let data = v_read(s, t, 64); + assert(mem_set(s.mem, eff_addr(s, a), a.size as nat, data) == Some(s1.mem)); + } + }, + Instr::B { .. } => {}, + Instr::BCond { .. } => {}, + Instr::CompareBranch { .. } => {}, + Instr::Ret => {}, + Instr::FAddSub { .. } => {}, + Instr::FMul { .. } => {}, + Instr::FDiv { .. } => {}, + Instr::FUnary { .. } => {}, + Instr::FCmp { .. } => {}, + Instr::FMovGen { .. } => {}, + Instr::Scvtf { .. } => {}, + Instr::Fcvtzs { .. } => {}, + } +} + +/// The frame of a successful `step` of an instruction that is not a branch: +/// as [`exec_frame`], and the program counter has moved to the next word. +pub proof fn lemma_step_straight(s: State, i: Instr, s2: State) + requires + s.r.len() == 31, + s.vreg.len() == 32, + valid_instr(i), + !(i is B), + !(i is BCond), + !(i is CompareBranch), + !(i is Ret), + step(s, i) == Some(s2), + ensures + execute(s, i) is Some, + ({ + let s1 = execute(s, i)->Some_0; + exec_frame(s, s1, i) && s2 == State { pc: trunc(64, s.pc as int + 4) as u64, ..s1 } + }), + s2.r.len() == 31, + s2.vreg.len() == 32, + s2.pc as int == trunc(64, s.pc as int + 4), + forall|r: int| + 0 <= r < 31 && written_gpr(i) != Some(r as u8) ==> #[trigger] s2.r[r] == s.r[r], + !writes_sp(i) ==> s2.sp == s.sp, + !is_store(i) ==> s2.mem == s.mem, + is_store(i) ==> exists|data: int| + #[trigger] mem_set( + s.mem, + eff_addr(s, mem_access(i)->Some_0), + mem_access(i)->Some_0.size as nat, + data, + ) == Some(s2.mem), +{ + let s1 = execute(s, i)->Some_0; + lemma_execute_frame(s, i, s1); + lemma_trunc64_range(s.pc as int + 4); + assert(s2 == branch_to(s1, s.pc as int + 4)); + let v = trunc(64, s.pc as int + 4); + assert(0 <= v < 18446744073709551616) by { + lemma_p2(); + } + assert(s2.pc == v as u64); + assert(v as u64 as int == v); +} + +} // verus! diff --git a/crates/clazel-codecheck/src/confine/a64/infra.rs b/crates/clazel-codecheck/src/confine/a64/infra.rs new file mode 100644 index 000000000..810f81109 --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/infra.rs @@ -0,0 +1,574 @@ +//! **Infrastructure of the A64 confinement proof**: the memory part of the +//! invariant and how stores and register writes keep it, the scan's facts +//! under register writes, the safety of a permitted access, the instruction +//! the invariant puts at the program counter, and the construction of the +//! invariant at the next position of a step. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// ---------------------------------------------------------------------------- +// Section: the memory part of the invariant +// ---------------------------------------------------------------------------- + +/// The memory part of [`base_inv`]: the region table and the code are as +/// configured. +pub open spec fn mem_inv(c: Cfg, m: Map) -> bool { + (forall|k: int| + 0 <= k < abi::num_regions() ==> mem_read(m, c.ctx + abi::base_off(k), 8) == Some( + c.bases[k], + ) && #[trigger] mem_read(m, c.ctx + abi::bound_off(k), 8) == Some(c.bounds[k])) + && (forall|q: int| + 0 <= q < c.blob.len() ==> #[trigger] m.dom().contains(c.code + q) && m[c.code + q] + == c.blob[q]) +} + +/// The register part of [`base_inv`]. +pub open spec fn regs_inv(c: Cfg, s: State) -> bool { + wf(s) && x_read(s, abi::ctx_reg(), 64) == c.ctx && x_read(s, abi::link_reg(), 64) == c.lr + && s.sp as int == c.sp +} + +/// [`base_inv`] is the register part and the memory part. +pub proof fn lemma_base_inv_split(c: Cfg, s: State) + ensures + base_inv(c, s) == (regs_inv(c, s) && mem_inv(c, s.mem)), +{ +} + +/// Region `k` of a configuration is well placed. +pub proof fn lemma_region(c: Cfg, k: int) + requires + cfg_ok(c), + 0 <= k < abi::num_regions(), + ensures + region_ok(c, k), + 0 <= c.bases[k], + 0 <= c.bounds[k], + c.bases[k] + c.bounds[k] <= lim(), + disjoint(c.bases[k], c.bounds[k], c.code, c.blob.len() as int), + disjoint(c.bases[k], c.bounds[k], c.ctx, 256), +{ + lemma_cfg(c); +} + +/// A write that is `writable` is disjoint from the table and the code. +pub proof fn lemma_writable_disjoint(c: Cfg, addr: int, size: int) + requires + cfg_ok(c), + size >= 0, + writable(c, addr, size), + ensures + disjoint(addr, size, c.ctx, 256), + disjoint(addr, size, c.code, c.blob.len() as int), +{ + lemma_cfg(c); + if c.ctx + 256 <= addr && addr + size <= c.ctx + 512 { + assert(disjoint(c.code, c.blob.len() as int, c.ctx, abi::ctx_size())); + } else { + let k = choose|k: int| + 0 <= k < abi::num_regions() && c.bases[k] <= addr && addr + size <= c.bases[k] + + c.bounds[k]; + lemma_region(c, k); + } +} + +/// The 8 bytes at `p` are the same in `m` and `m2`, if neither the store of +/// `[addr, addr + size)` nor anything else changed them. +pub proof fn lemma_read8_same(m: Map, m2: Map, p: int, addr: int, size: int) + requires + 0 <= p, + p + 8 <= p2(64), + m2.dom() =~= m.dom(), + forall|x: int| + (x < addr || x >= addr + size) && m.dom().contains(x) ==> #[trigger] m2[x] == m[x], + p + 8 <= addr || addr + size <= p, + ensures + mem_read(m2, p, 8) == mem_read(m, p, 8), +{ + assert forall|x: int| p <= x < p + 8 implies (#[trigger] m2.dom().contains(x) + == m.dom().contains(x) && (m.dom().contains(x) ==> m2[x] == m[x])) by { + assert(x < addr || x >= addr + size); + } + lemma_mem_read_same(m, m2, p, 8); +} + +/// A store to writable bytes keeps the memory invariant. +pub proof fn lemma_mem_inv_store(c: Cfg, m: Map, m2: Map, addr: int, size: int) + requires + cfg_ok(c), + mem_inv(c, m), + size >= 0, + writable(c, addr, size), + m2.dom() =~= m.dom(), + forall|x: int| + (x < addr || x >= addr + size) && m.dom().contains(x) ==> #[trigger] m2[x] == m[x], + ensures + mem_inv(c, m2), +{ + lemma_p2(); + lemma_cfg(c); + lemma_writable_disjoint(c, addr, size); + assert forall|k: int| 0 <= k < abi::num_regions() implies mem_read( + m2, + c.ctx + abi::base_off(k), + 8, + ) == Some(c.bases[k]) && #[trigger] mem_read(m2, c.ctx + abi::bound_off(k), 8) == Some( + c.bounds[k], + ) by { + lemma_read8_same(m, m2, c.ctx + abi::base_off(k), addr, size); + lemma_read8_same(m, m2, c.ctx + abi::bound_off(k), addr, size); + } + assert forall|q: int| 0 <= q < c.blob.len() implies #[trigger] m2.dom().contains(c.code + q) + && m2[c.code + q] == c.blob[q] by { + assert(m.dom().contains(c.code + q)); + assert(c.code + q < addr || c.code + q >= addr + size); + } +} + +/// **A register write that is not to a reserved register, with the program +/// counter and `SP` kept and memory kept**, keeps [`base_inv`]. +pub proof fn lemma_base_inv_after(c: Cfg, s: State, s2: State, d: Option) + requires + cfg_ok(c), + base_inv(c, s), + s2.r.len() == 31, + s2.vreg.len() == 32, + forall|r: int| 0 <= r < 31 && d != Some(r as u8) ==> #[trigger] s2.r[r] == s.r[r], + d is None || !abi::reserved(d->Some_0), + s2.sp == s.sp, + mem_inv(c, s2.mem), + s2.mem.dom() =~= s.mem.dom(), + ensures + base_inv(c, s2), +{ + lemma_base_inv_split(c, s); + lemma_base_inv_split(c, s2); + assert(wf(s2)) by { + assert forall|a: int| s2.mem.dom().contains(a) implies 0 <= a < 0x100_0000_0000_0000 by { + assert(s.mem.dom().contains(a)); + } + } + lemma_xr(s, abi::ctx_reg()); + lemma_xr(s2, abi::ctx_reg()); + lemma_xr(s, abi::link_reg()); + lemma_xr(s2, abi::link_reg()); + assert(d != Some(28u8)); + assert(d != Some(30u8)); + assert(s2.r[28int] == s.r[28int]); + assert(s2.r[30int] == s.r[30int]); +} + +// ---------------------------------------------------------------------------- +// Section: the scan's facts under register writes +// ---------------------------------------------------------------------------- + +/// What [`facts_hold`] says of one register `r`, as a predicate. +pub open spec fn fact_ok(c: Cfg, s: State, r: u8, f: Fact) -> bool { + match f { + Fact::Unknown => true, + Fact::Base(k) => r < 31 && 0 <= k < abi::num_regions() && x_read(s, r, 64) == c.bases[k], + Fact::Idx(k, n) => r < 31 && 0 <= k < abi::num_regions() && 0 <= n && x_read(s, r, 64) + n + <= c.bounds[k], + } +} + +/// [`facts_hold`] by [`fact_ok`]. +pub proof fn lemma_facts_hold_iff(c: Cfg, s: State, fs: Facts) + ensures + facts_hold(c, s, fs) == (forall|r: u8| r < 32 ==> fact_ok(c, s, r, #[trigger] fact_of(fs, r))), +{ +} + +/// A write to a register whose fact is `Unknown` (or to none) keeps the facts. +pub proof fn lemma_facts_keep(c: Cfg, s: State, s2: State, fs: Facts, d: Option) + requires + facts_hold(c, s, fs), + s.r.len() == 31, + s2.r.len() == 31, + forall|r: int| 0 <= r < 31 && d != Some(r as u8) ==> #[trigger] s2.r[r] == s.r[r], + d is None || fact_of(fs, d->Some_0) is Unknown, + ensures + facts_hold(c, s2, fs), +{ + assert forall|r: u8| r < 32 implies fact_ok(c, s2, r, #[trigger] fact_of(fs, r)) by { + assert(fact_ok(c, s, r, fact_of(fs, r))); + if fact_of(fs, r) is Unknown { + } else { + assert(d != Some(r)); + assert(r < 31); + lemma_xr(s, r); + lemma_xr(s2, r); + assert(s2.r[r as int] == s.r[r as int]); + } + } +} + +/// A write to register `d` that makes its fact `f` (which holds in `s2`) +/// gives the facts with `d` bound to `f`. +pub proof fn lemma_facts_set(c: Cfg, s: State, s2: State, fs: Facts, d: u8, f: Fact) + requires + facts_hold(c, s, fs), + s.r.len() == 31, + s2.r.len() == 31, + d < 32, + forall|r: int| 0 <= r < 31 && r != d ==> #[trigger] s2.r[r] == s.r[r], + fact_ok(c, s2, d, f), + ensures + facts_hold(c, s2, set_fact(fs, d, f)), +{ + assert forall|r: u8| r < 32 implies fact_ok(c, s2, r, #[trigger] fact_of(set_fact(fs, d, f), r)) by { + crate::a64::unit_exec::lemma_fact_of_set(fs, d, f, r); + assert(fact_ok(c, s, r, fact_of(fs, r))); + if r != d { + if !(fact_of(fs, r) is Unknown) { + assert(r < 31); + lemma_xr(s, r); + lemma_xr(s2, r); + assert(s2.r[r as int] == s.r[r as int]); + } + } + } +} + +/// Facts that hold still hold with a register's fact made `Unknown` (with the +/// state as it was). +pub proof fn lemma_facts_unknown(c: Cfg, s: State, fs: Facts, r0: u8) + requires + facts_hold(c, s, fs), + ensures + facts_hold(c, s, set_fact(fs, r0, Fact::Unknown)), +{ + assert forall|r: u8| r < 32 implies fact_ok( + c, + s, + r, + #[trigger] fact_of(set_fact(fs, r0, Fact::Unknown), r), + ) by { + crate::a64::unit_exec::lemma_fact_of_set(fs, r0, Fact::Unknown, r); + assert(fact_ok(c, s, r, fact_of(fs, r))); + } +} + +/// No facts hold trivially. +pub proof fn lemma_no_facts_hold(c: Cfg, s: State) + ensures + facts_hold(c, s, no_facts()), +{ + assert forall|r: u8| r < 32 implies fact_ok(c, s, r, #[trigger] fact_of(no_facts(), r)) by { + reveal_with_fuel(fact_of, 1); + } +} + +/// Forgetting keeps facts that hold. +pub proof fn lemma_forgotten_hold(c: Cfg, s: State, j: int, fs: Facts) + requires + facts_hold(c, s, fs), + ensures + facts_hold(c, s, forgotten(c, j, fs)), +{ + lemma_no_facts_hold(c, s); +} + +// ---------------------------------------------------------------------------- +// Section: the safety of a permitted access +// ---------------------------------------------------------------------------- + +/// **A permitted access is in bounds**: if the scan permits an access given +/// facts that hold, the bytes it touches are in the context (a load), in the +/// context's exit area (a store), or in a handed region. +pub proof fn lemma_access_safe(c: Cfg, s: State, fs: Facts, a: Access) + requires + cfg_ok(c), + regs_inv(c, s), + facts_hold(c, s, fs), + access_ok(fs, a), + a.base < 32, + match a.index { + Some(m) => m < 32, + None => true, + }, + 0 < a.size <= abi::max_access(), + ensures + readable(c, eff_addr(s, a), a.size), + a.store ==> writable(c, eff_addr(s, a), a.size), +{ + lemma_p2(); + lemma_cfg(c); + if a.base == abi::ctx_reg() { + assert(a.index is None); + lemma_xr(s, abi::ctx_reg()); + let addr = c.ctx + a.off; + assert(0 <= addr < p2(64)); + lemma_trunc64_id(addr); + assert(eff_addr(s, a) == addr); + } else { + let m = a.index->Some_0; + let r1 = a.base; + assert(fact_ok(c, s, r1, fact_of(fs, r1))); + assert(fact_ok(c, s, m, fact_of(fs, m))); + match (fact_of(fs, r1), fact_of(fs, m)) { + (Fact::Base(k), Fact::Idx(k2, n)) => { + lemma_region(c, k); + assert(r1 < 31 && m < 31); + lemma_xr(s, r1); + lemma_xr(s, m); + let b = x_read(s, r1, 64); + let i = x_read(s, m, 64); + assert(b == c.bases[k]); + assert(i + n <= c.bounds[k]); + assert(!a.scaled); + assert(extend_reg_uxtx(s, m, 0nat) == trunc(64, i) * p2(0)); + lemma_trunc64_id(i); + assert(extend_reg_uxtx(s, m, 0nat) == i); + assert(0 <= b + i < p2(64)); + lemma_trunc64_id(b + i); + assert(eff_addr(s, a) == b + i); + assert(in_region(c, eff_addr(s, a), a.size)); + }, + _ => {}, + } + } +} + +// ---------------------------------------------------------------------------- +// Section: the instruction at the program counter +// ---------------------------------------------------------------------------- + +/// The instruction at offset `4 j` is the next instruction. +pub proof fn lemma_next_at(c: Cfg, s: State, j: int) + requires + 0 <= j < c.ins.len(), + s.pc as int - c.code == start_of(c.ins, j), + ensures + next_instr(c, s) == Some(c.ins[j]), +{ + assert(exists|jj: int| + 0 <= jj < c.ins.len() && start_of(c.ins, jj) == s.pc as int - c.code); + let jj = choose|jj: int| 0 <= jj < c.ins.len() && start_of(c.ins, jj) == s.pc as int - c.code; + assert(jj == j); + assert(index_at(c, s) == Some(j)); +} + +/// `fetch_ok` from the bytes of [`word_of`]. +pub proof fn lemma_fetch_from(s: State, i: Instr) + requires + valid_instr(i), + (s.pc as int) % 4 == 0, + word_of(i).len() == 4, + forall|k: int| + 0 <= k < 4 ==> #[trigger] s.mem.dom().contains(s.pc as int + k) && s.mem[s.pc as int + + k] == word_of(i)[k], + ensures + fetch_ok(s, i), +{ + reveal(word_of); +} + +/// The bytes of the word at the program counter are the encoding of the +/// instruction, if the blob is in memory there. +pub proof fn lemma_fetch_blob( + s: State, + code: int, + blob: Seq, + i: Instr, + j: int, +) + requires + valid_instr(i), + code % 4 == 0, + 0 <= j, + 4 * j + 4 <= blob.len(), + blob.subrange(4 * j, 4 * j + 4) == word_of(i), + word_of(i).len() == 4, + forall|q: int| + 0 <= q < blob.len() ==> #[trigger] s.mem.dom().contains(code + q) && s.mem[code + q] + == blob[q], + s.pc as int == code + 4 * j, + ensures + fetch_ok(s, i), +{ + let w = word_of(i); + assert((s.pc as int) % 4 == 0); + assert forall|k: int| 0 <= k < 4 implies #[trigger] s.mem.dom().contains(s.pc as int + k) + && s.mem[s.pc as int + k] == w[k] by { + assert(blob[4 * j + k] == blob.subrange(4 * j, 4 * j + 4)[k]); + assert(0 <= 4 * j + k < blob.len()); + assert(s.mem.dom().contains(code + (4 * j + k))); + } + lemma_fetch_from(s, i); +} + +/// `fetch_ok` of the instruction at the program counter. +pub proof fn lemma_fetch(c: Cfg, s: State, j: int) + requires + cfg_ok(c), + base_inv(c, s), + 0 <= j < c.ins.len(), + s.pc as int == c.code + start_of(c.ins, j), + ensures + fetch_ok(s, c.ins[j]), +{ + lemma_cfg(c); + lemma_word_of_len(); + assert(valid_instr(c.ins[j])); + lemma_fetch_blob(s, c.code, c.blob, c.ins[j], j); +} + +/// **The invariant puts the instruction `ins[j0 + m]` at the program +/// counter**: it is the next instruction, it is valid, and its bytes are in +/// memory. +pub proof fn lemma_cur_instr(c: Cfg, s: State, j0: int, m: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, m), + ensures + 0 <= j0 + m < c.ins.len(), + next_instr(c, s) == Some(c.ins[j0 + m]), + fetch_ok(s, c.ins[j0 + m]), + valid_instr(c.ins[j0 + m]), +{ + let fs0 = start_facts(c, j0)->Some_0; + let u = unit_at(c.ins, j0, fs0)->Some_0; + let j = j0 + m; + assert(j < c.ins.len()); + lemma_next_at(c, s, j); + lemma_fetch(c, s, j); + lemma_cfg(c); +} + +// ---------------------------------------------------------------------------- +// Section: positions +// ---------------------------------------------------------------------------- + +/// A step that leaves memory as it was confines writes. +pub proof fn lemma_confined_same(c: Cfg, s: State, s2: State) + requires + s2.mem == s.mem, + ensures + writes_confined(c, s, s2), +{ +} + +/// A store to writable bytes confines writes. +pub proof fn lemma_confined_store(c: Cfg, s: State, s2: State, addr: int, size: int) + requires + size >= 0, + writable(c, addr, size), + store_frame(s, s2, addr, size), + ensures + writes_confined(c, s, s2), +{ + assert forall|a: int| !writable_byte(c, a) implies (#[trigger] s2.mem.dom().contains(a) + == s.mem.dom().contains(a) && (s.mem.dom().contains(a) ==> s2.mem[a] == s.mem[a])) by { + if addr <= a < addr + size { + if c.ctx + abi::exit_code_off() <= addr && addr + size <= c.ctx + abi::ctx_size() { + assert(writable(c, a, 1)); + } else { + let k = choose|k: int| + 0 <= k < abi::num_regions() && c.bases[k] <= addr && addr + size <= c.bases[k] + + c.bounds[k]; + assert(in_region(c, a, 1)); + assert(writable(c, a, 1)); + } + assert(false); + } + assert(s2.mem.dom() =~= s.mem.dom()); + } +} + +/// **The last instruction of a blob is a `ret` or `b`**, so an instruction +/// that is neither is followed by another. +pub proof fn lemma_not_last(c: Cfg, k: int) + requires + cfg_ok(c), + 0 <= k < c.ins.len(), + !(c.ins[k] is Ret), + !(c.ins[k] is B), + ensures + k + 1 < c.ins.len(), +{ + lemma_cfg(c); + assert(c.ins.last() is Ret || c.ins.last() is B); + if k + 1 == c.ins.len() { + assert(c.ins[k] == c.ins.last()); + } +} + +/// **The position inside a unit**: after an instruction that is not the last of +/// its unit, the invariant at the next instruction of the unit holds if +/// the base invariant, the program counter and the registers' invariant do. +pub proof fn lemma_pos_mid(c: Cfg, s: State, s2: State, j0: int, m: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, m), + unit_at(c.ins, j0, start_facts(c, j0)->Some_0)->Some_0.len > m + 1, + base_inv(c, s2), + s2.pc as int == s.pc as int + 4, + mid_inv(c, s2, j0, start_facts(c, j0)->Some_0, m + 1), + ensures + pos_inv(c, s2, j0, m + 1), +{ + lemma_cur_instr(c, s, j0, m); +} + +/// **The position after the last instruction of a unit** that falls through: +/// the next unit start, with the unit's facts (forgotten there). +pub proof fn lemma_pos_fall(c: Cfg, s: State, s2: State, j0: int, m: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, m), + unit_at(c.ins, j0, start_facts(c, j0)->Some_0)->Some_0.len == m + 1, + j0 + m + 1 < c.ins.len(), + base_inv(c, s2), + s2.pc as int == s.pc as int + 4, + facts_hold(c, s2, unit_at(c.ins, j0, start_facts(c, j0)->Some_0)->Some_0.facts), + ensures + pos_inv(c, s2, j0 + m + 1, 0), +{ + lemma_cur_instr(c, s, j0, m); + let fs0 = start_facts(c, j0)->Some_0; + let u = unit_at(c.ins, j0, fs0)->Some_0; + lemma_next_start(c, j0, fs0, u); + let t = j0 + u.len; + let f = forgotten(c, t, u.facts); + assert(start_facts(c, t) == Some(f)); + lemma_unit_ok(c, t, f); + lemma_forgotten_hold(c, s2, t, u.facts); +} + +/// **The position after a jump**: the target is a unit start with no facts. +pub proof fn lemma_pos_jump(c: Cfg, s: State, s2: State, j: int) + requires + cfg_ok(c), + 0 <= j < c.ins.len(), + jump_rel(c.ins[j]) is Some, + base_inv(c, s2), + s.pc as int == c.code + start_of(c.ins, j), + s2.pc as int == trunc(64, s.pc as int + jump_rel(c.ins[j])->Some_0), + ensures + inv(c, s2), +{ + lemma_cfg(c); + lemma_p2(); + lemma_jump_target_is_start(c, j); + let x = start_of(c.ins, j) + jump_rel(c.ins[j])->Some_0; + let t = choose|t: int| + 0 <= t < c.ins.len() && x == start_of(c.ins, t) && start_facts(c, t) is Some && start_facts( + c, + t, + )->Some_0 == no_facts(); + assert(0 <= c.code + x < p2(64)); + lemma_trunc64_id(c.code + x); + assert(s2.pc as int == c.code + start_of(c.ins, t)); + let f = start_facts(c, t)->Some_0; + lemma_unit_ok(c, t, f); + lemma_no_facts_hold(c, s2); + assert(pos_inv(c, s2, t, 0)); +} + +} // verus! diff --git a/crates/clazel-codecheck/src/confine/a64/instance.rs b/crates/clazel-codecheck/src/confine/a64/instance.rs new file mode 100644 index 000000000..66baed453 --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/instance.rs @@ -0,0 +1,591 @@ +//! **The A64 machine** of the confinement theorem +//! ([`crate::confine::machine::confinement`]): the configuration, the +//! machine-level invariant, and the observations, over `clazel_a64::spec`'s +//! `State` and `step` and `crate::a64::spec`'s acceptance spec. This file is +//! *statement only*: definitions and the [`Machine`] instance. The proof is in +//! the other modules of this directory (CLAUDE.md rule 3: the statement is +//! committed before it). +//! +//! It is the A64 counterpart of [`crate::confine::x64`] and the statement is +//! the same one; this header says where the two machines differ. +//! +//! # The configuration ([`Cfg`]) +//! +//! What the sandbox hands over, as numbers: the blob and the instruction list +//! it decodes to, the entries; the address of the blob's first byte (`code`, +//! a multiple of 4: the program counter is aligned), of the context (`ctx`, +//! 512 bytes, jit.md J.0 and [`crate::a64::spec::abi`]); the 16 regions' bases +//! and bounds (as in the context's table); the value of `SP` on entry (`sp`), +//! and the return address the enter stub left in `x30` (`lr`). +//! [`cfg_ok`] says it is a configuration the sandbox may hand over: the blob +//! is accepted, everything lies in the positive canonical addresses +//! (`0 <= a < 2^47`, so that no address computation wraps modulo `2^64`), and +//! **the code, the context's table and each region are pairwise disjoint** (a +//! region may overlap another region and the exit area, which is harmless). +//! These are obligations of the enter stub and the sandbox (J.7, J.8), not of +//! the validator. +//! +//! # The machine-level invariant ([`inv`]) +//! +//! Before an instruction ([`pos_inv`], at unit `j0`, `m` instructions into +//! it): +//! +//! - **the state is well formed** (`clazel_a64::spec::wf`); +//! - **the context register `x28` holds the context**, **the link register +//! `x30` holds the stub's return address** and **`SP` holds the stub's +//! stack pointer**; +//! - **the region table is intact**: the 16 base/bound words in memory are +//! the configuration's; +//! - **the code is intact**: the blob's bytes are in memory at `code`; +//! - **the program counter is at the start of instruction `j0 + m`**, where +//! `j0` is a *unit start* of the blob: a position the validator's scan +//! reaches by taking units from 0 ([`start_facts`]); +//! - **the facts the scan tracks hold** ([`facts_hold`]): at a unit start +//! (`m == 0`) those the scan has there, after forgetting at targets; inside +//! an exit or bounds-check sequence (`m > 0`), the corresponding +//! intermediate facts ([`mid_inv`]), including the carry and zero flags +//! after `adds` and `cmp`. +//! +//! # Modelling choices +//! +//! - **The accesses are [`mem_access`]'s**, the validator's own account of +//! what an instruction touches ([`loads_ok`], [`stores_ok`], at the address +//! [`eff_addr`] computes from the registers); the proof ties it to the +//! semantics: a write changes memory only inside the access it names +//! ([`writes_confined`]), and a step's outcome depends only on the bytes of +//! the read area ([`reads_confined`]). +//! - **`ret` reads no memory.** It branches to `x30`; the exit sequence's +//! `ret` therefore returns to `lr` ([`exited`]). There is no stack slot +//! and so no disjointness condition for one: the blob cannot touch the +//! stack at all (`SP` is written by nothing the validator accepts, and no +//! load or store has base `SP`). +//! - **No undefined state.** `clazel-a64`'s `step` is a function of the state +//! and the instruction, so the choice `Env` the theorem quantifies over is +//! the unit type. +//! - **The next instruction is found by offset**: [`next_instr`] looks the +//! program counter's offset up among the instruction starts; [`inv`] has it +//! at one. +//! - **No system instruction**: [`is_system`] is false for every [`Instr`]; +//! the match is spelled out so that a new variant forces a decision. +//! - **Addresses wrap modulo `2^64`** in the semantics; [`eff_addr`] is the +//! wrapped address, and [`cfg_ok`] keeps every region and the context below +//! `2^47` so no permitted access can wrap. +//! - **Facts about registers 0 to 30.** Register 31 is the zero register in +//! every operand where facts matter, so no fact is ever about it +//! ([`facts_hold`]). + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The A64 machine: the marker the [`Machine`] instance is for. +pub struct A64; + +/// What the sandbox hands over (see the module documentation). +pub struct Cfg { + /// The blob. + pub blob: Seq, + /// The instructions it decodes to. + pub ins: Seq, + /// The entries, as byte offsets. + pub entries: Seq, + /// The address of the blob's first byte. + pub code: int, + /// The address of the 512-byte context. + pub ctx: int, + /// The base address of each of the 16 regions. + pub bases: Seq, + /// The bound (size in bytes) of each of the 16 regions. + pub bounds: Seq, + /// The value of `SP` on entry. + pub sp: int, + /// The value of `x30` on entry: where the exit sequence's `ret` returns. + pub lr: int, +} + +/// The first address that is not in the positive canonical range: `2^47`. +pub open spec fn lim() -> int { + 140737488355328 +} + +/// The byte ranges `[a, a + la)` and `[b, b + lb)` do not overlap. +pub open spec fn disjoint(a: int, la: int, b: int, lb: int) -> bool { + a + la <= b || b + lb <= a +} + +/// Region `k` is well placed: positive canonical addresses, and disjoint from +/// the code and the context's table. +pub open spec fn region_ok(c: Cfg, k: int) -> bool { + 0 <= c.bases[k] && 0 <= c.bounds[k] && c.bases[k] + c.bounds[k] <= lim() && disjoint( + c.bases[k], + c.bounds[k], + c.code, + c.blob.len() as int, + ) && disjoint(c.bases[k], c.bounds[k], c.ctx, abi::table_end()) +} + +/// **A configuration the sandbox may hand over**: the blob is accepted (the +/// instruction list `ins` is its decoding and passes the policy at the +/// entries), and the layout is as the module documentation says. +/// +/// `opaque` only for the prover's sake (`decodes_to` quantifies over the +/// large `encode`, and a proof that unfolded it at every step would not +/// finish): `super::chain::lemma_cfg` reveals it, and nothing else changes. +#[verifier::opaque] +pub open spec fn cfg_ok(c: Cfg) -> bool { + decodes_to(c.blob, c.ins) && policy(c.ins, c.entries) && c.bases.len() == abi::num_regions() + && c.bounds.len() == abi::num_regions() && 0 <= c.code && c.code % 4 == 0 && c.code + + c.blob.len() <= lim() && 0 <= c.ctx && c.ctx + abi::ctx_size() <= lim() && disjoint( + c.code, + c.blob.len() as int, + c.ctx, + abi::ctx_size(), + ) && (forall|k: int| 0 <= k < abi::num_regions() ==> #[trigger] region_ok(c, k)) && 0 <= c.sp + && c.sp < p2(64) && 0 <= c.lr && c.lr < p2(64) && disjoint( + c.lr, + 1, + c.code, + c.blob.len() as int, + ) +} + +// ---------------------------------------------------------------------------- +// Section: what holds in the registers +// ---------------------------------------------------------------------------- + +/// The scan's facts `fs` hold in `s`: a register known to hold region `k`'s +/// base does, and a register known to be checked for `n` bytes of region `k` +/// holds an `i` with `i + n <= bound(k)`. Neither is ever a fact about +/// register 31, the zero register. +pub open spec fn facts_hold(c: Cfg, s: State, fs: Facts) -> bool { + forall|r: u8| + r < 32 ==> match #[trigger] fact_of(fs, r) { + Fact::Unknown => true, + Fact::Base(k) => r < 31 && 0 <= k < abi::num_regions() && x_read(s, r, 64) + == c.bases[k], + Fact::Idx(k, n) => r < 31 && 0 <= k < abi::num_regions() && 0 <= n && x_read(s, r, 64) + + n <= c.bounds[k], + } +} + +/// What the scan forgets at instruction `j`: everything, if its offset is a +/// target or an entry, else nothing. +pub open spec fn forgotten(c: Cfg, j: int, fs: Facts) -> Facts { + if seq_has(all_targets(c.ins, c.entries), start_of(c.ins, j)) { + no_facts() + } else { + fs + } +} + +/// The facts the scan has on reaching the unit that starts at instruction +/// `t`, after forgetting there, if the scan starting at unit `j` with `fs` +/// reaches `t` as a unit start; `None` if it does not. +pub open spec fn chain_facts(c: Cfg, j: int, fs: Facts, t: int) -> Option + decreases t - j, +{ + let fs0 = forgotten(c, j, fs); + if j == t { + Some(fs0) + } else if j > t || j < 0 || j >= c.ins.len() { + None + } else { + match unit_at(c.ins, j, fs0) { + None => None, + Some(u) => if u.len >= 1 && j + u.len <= t { + chain_facts(c, j + u.len, u.facts, t) + } else { + None + }, + } + } +} + +/// The facts at unit start `t`: the scan from the first instruction. +pub open spec fn start_facts(c: Cfg, t: int) -> Option { + chain_facts(c, 0, no_facts(), t) +} + +// ---------------------------------------------------------------------------- +// Section: the invariant inside the validator's sequences +// ---------------------------------------------------------------------------- + +/// What is known after `movz t, #code` of an exit sequence starting at +/// instruction `j0`: `t` is overwritten. +pub open spec fn exit_mid_facts(ins: Seq, j0: int, fs0: Facts) -> Facts { + match ins[j0] { + Instr::MoveWide { d, .. } => set_fact(fs0, d, Fact::Unknown), + _ => fs0, + } +} + +/// The facts that hold throughout a bounds check `ch`: all of `fs0` except +/// for the three registers it writes. +pub open spec fn check_mid_facts(fs0: Facts, ch: Chk) -> Facts { + set_fact( + set_fact(set_fact(fs0, ch.t, Fact::Unknown), ch.bnd, Fact::Unknown), + ch.i, + Fact::Unknown, + ) +} + +/// The invariant before instruction `m` (1 to 6) of the bounds check `ch`, +/// whose region's bound is `b` and which began with `fs0` (the instructions +/// are numbered as in [`check_instr`]). With `I` the index register's value, +/// `n` the size, `T` the scratch's and `B` the bound register's, `C` and `Z` +/// the carry and zero flags: +/// +/// - 1: `B = b`; +/// - 2: `T = (I + n) mod 2^64` and `C` is set exactly when `I + n >= 2^64`; +/// - 3: `b.hs` fell through, so `I + n < 2^64` and `T = I + n`; +/// - 4: `cmp t, b` has set `C` for `T >= b` and `Z` for `T = b`; +/// - 5: `b.hi` fell through, so `I + n <= b`; +/// - 6: the same, and `C` is set exactly when `I >= b`. +/// +/// (and `B = b` throughout). +pub open spec fn check_mid(c: Cfg, s: State, ch: Chk, fs0: Facts, m: int) -> bool { + 0 <= ch.k < abi::num_regions() && facts_hold(c, s, check_mid_facts(fs0, ch)) && { + let i = x_read(s, ch.i, 64); + let t = x_read(s, ch.t, 64); + let bn = x_read(s, ch.bnd, 64); + let n = ch.n; + let b = c.bounds[ch.k]; + bn == b && if m == 1 { + true + } else if m == 2 { + t == (i + n) % p2(64) && s.flag_c == (i + n >= p2(64)) + } else if m == 3 { + i + n < p2(64) && t == i + n + } else if m == 4 { + t == i + n && s.flag_c == (t >= b) && s.flag_z == (t == b) + } else if m == 5 { + i + n <= b + } else if m == 6 { + i + n <= b && s.flag_c == (i >= b) + } else { + false + } + } +} + +/// The invariant on the registers, `m` instructions into the unit that starts +/// at instruction `j0` with facts `fs0`. +pub open spec fn mid_inv(c: Cfg, s: State, j0: int, fs0: Facts, m: int) -> bool { + if m == 0 { + facts_hold(c, s, fs0) + } else if exit_unit_at(c.ins, j0) { + facts_hold(c, s, exit_mid_facts(c.ins, j0, fs0)) + } else { + check_unit_at(c.ins, j0) && check_mid(c, s, check_params(c.ins, j0)->Some_0, fs0, m) + } +} + +// ---------------------------------------------------------------------------- +// Section: the machine-level invariant +// ---------------------------------------------------------------------------- + +/// The part of the invariant that does not depend on where in the blob the +/// state is: the state is well formed, `x28` is the context, `x30` the stub's +/// return address, `SP` the stub's stack pointer, and the region table and the +/// code are intact in memory. +pub open spec fn base_inv(c: Cfg, s: State) -> bool { + wf(s) && x_read(s, abi::ctx_reg(), 64) == c.ctx && x_read(s, abi::link_reg(), 64) == c.lr + && s.sp as int == c.sp && (forall|k: int| + 0 <= k < abi::num_regions() ==> mem_read(s.mem, c.ctx + abi::base_off(k), 8) == Some( + c.bases[k], + ) && #[trigger] mem_read(s.mem, c.ctx + abi::bound_off(k), 8) == Some(c.bounds[k])) + && (forall|q: int| + 0 <= q < c.blob.len() ==> #[trigger] s.mem.dom().contains(c.code + q) && s.mem[c.code + q] + == c.blob[q]) +} + +/// **The invariant at a position**: the base invariant, and the program +/// counter is at instruction `j0 + m`, where `j0` is a unit start that the +/// scan reaches and `m` is within the unit; the facts hold as [`mid_inv`] +/// says. +pub open spec fn pos_inv(c: Cfg, s: State, j0: int, m: int) -> bool { + base_inv(c, s) && match start_facts(c, j0) { + None => false, + Some(fs0) => match unit_at(c.ins, j0, fs0) { + None => false, + Some(u) => 0 <= j0 && j0 + u.len <= c.ins.len() && 0 <= m && m < u.len && s.pc as int + == c.code + start_of(c.ins, j0 + m) && mid_inv(c, s, j0, fs0, m), + }, + } +} + +/// **The machine-level invariant**: [`pos_inv`] at some position. +pub open spec fn inv(c: Cfg, s: State) -> bool { + exists|j0: int, m: int| pos_inv(c, s, j0, m) +} + +// ---------------------------------------------------------------------------- +// Section: the observations +// ---------------------------------------------------------------------------- + +/// The index of the instruction whose first byte is at the program counter, +/// if there is one. +pub open spec fn index_at(c: Cfg, s: State) -> Option { + if exists|j: int| 0 <= j < c.ins.len() && start_of(c.ins, j) == s.pc as int - c.code { + Some(choose|j: int| 0 <= j < c.ins.len() && start_of(c.ins, j) == s.pc as int - c.code) + } else { + None + } +} + +/// The instruction executed next from `s`. +pub open spec fn next_instr(c: Cfg, s: State) -> Option { + match index_at(c, s) { + Some(j) => Some(c.ins[j]), + None => None, + } +} + +/// `i` is a system or privileged instruction: none of the subset is. (Spelled +/// out over every variant, so that adding one forces a decision here.) +pub open spec fn is_system(i: Instr) -> bool { + match i { + Instr::AddSubShiftedReg { .. } => false, + Instr::AddSubImm { .. } => false, + Instr::MulAddSub { .. } => false, + Instr::Div { .. } => false, + Instr::LogicalShiftedReg { .. } => false, + Instr::ShiftVariable { .. } => false, + Instr::Bitfield { .. } => false, + Instr::MoveWide { .. } => false, + Instr::CondSelect { .. } => false, + Instr::LoadStoreImm { .. } => false, + Instr::LoadStoreReg { .. } => false, + Instr::FpLoadStoreImm { .. } => false, + Instr::FpLoadStoreReg { .. } => false, + Instr::B { .. } => false, + Instr::BCond { .. } => false, + Instr::CompareBranch { .. } => false, + Instr::Ret => false, + Instr::FAddSub { .. } => false, + Instr::FMul { .. } => false, + Instr::FDiv { .. } => false, + Instr::FUnary { .. } => false, + Instr::FCmp { .. } => false, + Instr::FMovGen { .. } => false, + Instr::Scvtf { .. } => false, + Instr::Fcvtzs { .. } => false, + } +} + +/// The `n` bytes at `addr` lie in a handed region. +pub open spec fn in_region(c: Cfg, addr: int, n: int) -> bool { + exists|k: int| + 0 <= k < abi::num_regions() && c.bases[k] <= addr && addr + n <= c.bases[k] + c.bounds[k] +} + +/// The `n` bytes at `addr` may be read: they lie in the context or a region. +pub open spec fn readable(c: Cfg, addr: int, n: int) -> bool { + (c.ctx <= addr && addr + n <= c.ctx + abi::ctx_size()) || in_region(c, addr, n) +} + +/// The `n` bytes at `addr` may be written: they lie in the context's exit +/// area or a region. +pub open spec fn writable(c: Cfg, addr: int, n: int) -> bool { + (c.ctx + abi::exit_code_off() <= addr && addr + n <= c.ctx + abi::ctx_size()) || in_region( + c, + addr, + n, + ) +} + +/// The byte at `a` is writable. +pub open spec fn writable_byte(c: Cfg, a: int) -> bool { + writable(c, a, 1) +} + +/// The address an access of `a` touches from `s`, as the semantics computes +/// it: the base register plus the immediate, or plus the (shifted) index +/// register (`ExtendReg` with `UXTX`), modulo `2^64`. +pub open spec fn eff_addr(s: State, a: Access) -> int { + match a.index { + None => trunc(64, x_read(s, a.base, 64) + a.off), + Some(m) => trunc( + 64, + x_read(s, a.base, 64) + extend_reg_uxtx( + s, + m, + if a.scaled { + if a.size == 8 { + 3nat + } else { + 2nat + } + } else { + 0nat + }, + ), + ), + } +} + +/// Every load of `i` from `s` is to readable bytes (by [`mem_access`]). +pub open spec fn loads_ok(c: Cfg, s: State, i: Instr) -> bool { + match mem_access(i) { + None => true, + Some(a) => a.store || readable(c, eff_addr(s, a), a.size), + } +} + +/// Every store of `i` from `s` is to writable bytes (by [`mem_access`]). +pub open spec fn stores_ok(c: Cfg, s: State, i: Instr) -> bool { + match mem_access(i) { + None => true, + Some(a) => !a.store || writable(c, eff_addr(s, a), a.size), + } +} + +/// The byte at `a` may be observed by a step: it is readable. +pub open spec fn read_area(c: Cfg, a: int) -> bool { + readable(c, a, 1) +} + +/// The memories `m` and `m2` agree (on which bytes are mapped, and on their +/// values) at every byte of the read area. +pub open spec fn agree_on_read_area(c: Cfg, m: Map, m2: Map) -> bool { + forall|a: int| + read_area(c, a) ==> (#[trigger] m2.dom().contains(a) == m.dom().contains(a) && ( + m.dom().contains(a) ==> m2[a] == m[a])) +} + +/// The states agree on everything but memory. +pub open spec fn eq_mod_mem(t: State, t2: State) -> bool { + t.r == t2.r && t.sp == t2.sp && t.pc == t2.pc && t.flag_n == t2.flag_n && t.flag_z + == t2.flag_z && t.flag_c == t2.flag_c && t.flag_v == t2.flag_v && t.vreg == t2.vreg +} + +/// Two outcomes of a step agree but for memory: both fault, or neither does +/// and the states agree apart from memory. +pub open spec fn same_outcome(o: Option, o2: Option) -> bool { + (o is Some == o2 is Some) && (o is Some ==> eq_mod_mem(o->Some_0, o2->Some_0)) +} + +/// **Reads are confined**: from every state that differs from `s` only in +/// memory outside the read area, the step of `i` has the same outcome (apart +/// from memory). +pub open spec fn reads_confined(c: Cfg, s: State, i: Instr) -> bool { + forall|m2: Map| + agree_on_read_area(c, s.mem, m2) ==> #[trigger] same_outcome( + step(s, i), + step(State { mem: m2, ..s }, i), + ) +} + +/// Memory is the same in `s` and `s2` at every byte that is not writable +/// (mapped or not). +pub open spec fn writes_confined(c: Cfg, s: State, s2: State) -> bool { + forall|a: int| + !writable_byte(c, a) ==> (#[trigger] s2.mem.dom().contains(a) == s.mem.dom().contains(a) + && (s.mem.dom().contains(a) ==> s2.mem[a] == s.mem[a])) +} + +/// `i`, executed from `s`, is the `ret` that ends an exit sequence of the +/// blob: the instruction at the third position of an [`exit_unit_at`]. +pub open spec fn is_exit(c: Cfg, s: State, i: Instr) -> bool { + exists|j: int| + exit_unit_at(c.ins, j) && i == c.ins[j + 2] && s.pc as int == c.code + start_of( + c.ins, + j + 2, + ) +} + +/// `s` is the state after the exit sequence returned to the stub: the program +/// counter is the stub's return address, `x28` is still the context, `x30` the +/// return address, and `SP` the stub's. +pub open spec fn exited(c: Cfg, s: State) -> bool { + s.pc as int == c.lr && x_read(s, abi::ctx_reg(), 64) == c.ctx && x_read( + s, + abi::link_reg(), + 64, + ) == c.lr && s.sp as int == c.sp +} + +/// The context register, the link register and the stack pointer hold the +/// same in `s` and `s2`. +pub open spec fn reserved_same(s: State, s2: State) -> bool { + x_read(s, abi::ctx_reg(), 64) == x_read(s2, abi::ctx_reg(), 64) && x_read( + s, + abi::link_reg(), + 64, + ) == x_read(s2, abi::link_reg(), 64) && s.sp == s2.sp +} + +impl Machine for A64 { + type Cfg = Cfg; + type St = State; + type Ins = Instr; + type Env = (); + + open spec fn cfg_ok(c: Cfg) -> bool { + cfg_ok(c) + } + + open spec fn inv(c: Cfg, s: State) -> bool { + inv(c, s) + } + + open spec fn next_instr(c: Cfg, s: State) -> Option { + next_instr(c, s) + } + + open spec fn fetched(c: Cfg, s: State, i: Instr) -> bool { + fetch_ok(s, i) + } + + open spec fn step(e: (), s: State, i: Instr) -> Option { + step(s, i) + } + + open spec fn is_system(i: Instr) -> bool { + is_system(i) + } + + open spec fn loads_ok(c: Cfg, s: State, i: Instr) -> bool { + loads_ok(c, s, i) + } + + open spec fn stores_ok(c: Cfg, s: State, i: Instr) -> bool { + stores_ok(c, s, i) + } + + open spec fn reads_confined(c: Cfg, e: (), s: State, i: Instr) -> bool { + reads_confined(c, s, i) + } + + open spec fn writes_confined(c: Cfg, s: State, s2: State) -> bool { + writes_confined(c, s, s2) + } + + open spec fn is_exit(c: Cfg, s: State, i: Instr) -> bool { + is_exit(c, s, i) + } + + open spec fn exited(c: Cfg, s: State) -> bool { + exited(c, s) + } + + open spec fn reserved_same(s: State, s2: State) -> bool { + reserved_same(s, s2) + } +} + +/// **The A64 confinement theorem, as a statement** (to be proven by +/// `super::proof`): [`confinement`] for [`A64`]. +pub open spec fn a64_confinement() -> bool { + confinement::() +} + +/// **The corollary over every finite A64 execution**, as a statement. +pub open spec fn a64_run_confinement() -> bool { + run_confinement::() +} + +} // verus! diff --git a/crates/clazel-codecheck/src/confine/a64/mod.rs b/crates/clazel-codecheck/src/confine/a64/mod.rs new file mode 100644 index 000000000..697d91d4c --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/mod.rs @@ -0,0 +1,56 @@ +//! **The A64 instance of the confinement theorem** (jit.md exit criterion 2, +//! item J.6, for A64): the same statement as `super::machine::confinement`, +//! over `clazel-a64`'s semantics and `crate::a64`'s acceptance spec. +//! +//! - [`instance`]: the A64 machine: configuration, machine-level invariant, +//! observations, and the [`super::machine::Machine`] instance. Statement +//! only (CLAUDE.md rule 3: it is committed before the proof). +//! +//! The A64 proof, complete for every case, is in the other modules: [`frame`] +//! (what `execute` leaves alone, and `mem_set`/`mem_read`), [`reads`] (a +//! step's outcome depends only on the read area), [`flags`] (carry and zero +//! after `adds` and `cmp`), [`chain`] (the units the scan reaches), [`infra`] +//! (the invariant under a step), [`steps`] (the exit sequence and single +//! instructions) and [`check`] (the bounds check); [`proof`] puts them +//! together ([`proof::theorem_a64`], [`proof::theorem_a64_runs`]), and +//! [`sanity`] shows the hypotheses are satisfiable. See `instance`'s header +//! for the A64 modelling choices. No axiom is used. + +/// The A64 acceptance spec, `clazel-a64`'s semantics and every module of this +/// directory, re-exported under the name `spec` so that the modules import all +/// of it as `use super::spec::*` (the one ghost import CLAUDE.md allows next +/// to `vstd::prelude::*`). The modules that hold only ghost code are +/// re-exported under `cfg(verus_only)`, the cfg the workspace lets guard ghost +/// imports. +pub mod spec { + pub use crate::a64::spec::*; + #[cfg(verus_only)] + pub use crate::confine::a64::chain::*; + #[cfg(verus_only)] + pub use crate::confine::a64::check::*; + #[cfg(verus_only)] + pub use crate::confine::a64::flags::*; + #[cfg(verus_only)] + pub use crate::confine::a64::frame::*; + #[cfg(verus_only)] + pub use crate::confine::a64::infra::*; + pub use crate::confine::a64::instance::*; + #[cfg(verus_only)] + pub use crate::confine::a64::proof::*; + #[cfg(verus_only)] + pub use crate::confine::a64::reads::*; + #[cfg(verus_only)] + pub use crate::confine::a64::steps::*; + pub use crate::confine::machine::*; +} + +pub mod chain; +pub mod check; +pub mod flags; +pub mod frame; +pub mod infra; +pub mod instance; +pub mod proof; +pub mod reads; +pub mod sanity; +pub mod steps; diff --git a/crates/clazel-codecheck/src/confine/a64/proof.rs b/crates/clazel-codecheck/src/confine/a64/proof.rs new file mode 100644 index 000000000..b4626fdf2 --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/proof.rs @@ -0,0 +1,139 @@ +//! **The proof of the A64 confinement theorem** ([`a64_confinement`]) and of +//! its corollary over finite executions ([`a64_run_confinement`]). +//! +//! [`lemma_step_conf`] is the theorem at one position: it dispatches on the +//! unit that starts at `j0` (the exit sequence, `super::steps`; the bounds +//! check, `super::check`; or a single instruction, `super::steps`). +//! [`theorem_a64`] quantifies it away; [`lemma_entry_inv`] says the invariant +//! holds at every accepted entry given the context invariant; +//! [`theorem_a64_runs`] is the induction over an execution. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// **The theorem at one position**: from the invariant at `(j0, m)`, the step +/// is confined. +pub proof fn lemma_step_conf(c: Cfg, s: State, j0: int, m: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, m), + ensures + confined_step::(c, (), s), +{ + lemma_cur_instr(c, s, j0, m); + let i = c.ins[j0 + m]; + lemma_not_system(i); + if exit_unit_at(c.ins, j0) { + lemma_exit_unit(c, s, j0, m); + } else if check_unit_at(c.ins, j0) { + lemma_check_unit(c, s, j0, m); + } else { + lemma_plain_unit(c, s, j0, m); + } + assert(inv(c, s)); + lemma_base_inv_split(c, s); + lemma_reads_confined(c, s, i); + match step(s, i) { + None => {}, + Some(s2) => { + assert(step_post(c, s, i, s2)); + }, + } +} + +/// **The A64 confinement theorem** ([`a64_confinement`]). +pub proof fn theorem_a64() + ensures + a64_confinement(), +{ + assert forall|c: Cfg, e: (), s: State| cfg_ok(c) implies #[trigger] confined_step::( + c, + e, + s, + ) by { + if inv(c, s) { + let (j0, m) = choose|j0: int, m: int| pos_inv(c, s, j0, m); + lemma_step_conf(c, s, j0, m); + } + } +} + +/// **The invariant holds at every accepted entry**, given the context +/// invariant: a state whose registers, memory and program counter are as the +/// configuration says, with the program counter at an entry the validator +/// accepted, satisfies [`inv`]. (This is the hypothesis "started at an +/// accepted entry with the context invariant" of jit.md exit criterion 2.) +pub proof fn lemma_entry_inv(c: Cfg, s: State, e: int) + requires + cfg_ok(c), + seq_has(c.entries, e), + base_inv(c, s), + s.pc as int == c.code + e, + ensures + inv(c, s), +{ + lemma_entry_is_start(c, e); + let t = choose|t: int| + 0 <= t < c.ins.len() && e == start_of(c.ins, t) && start_facts(c, t) is Some + && start_facts(c, t)->Some_0 == no_facts(); + lemma_unit_ok(c, t, no_facts()); + lemma_no_facts_hold(c, s); + assert(pos_inv(c, s, t, 0)); +} + +/// The induction over an execution: every state but the last satisfies the +/// invariant, and the last does or was entered by the exit. +pub proof fn lemma_run_inv(c: Cfg, e: (), ex: Seq, k: int) + requires + cfg_ok(c), + ex.len() > 0, + is_run::(c, e, ex), + inv(c, ex[0]), + 0 <= k < ex.len(), + ensures + inv(c, ex[k]) || (k >= 1 && exited(c, ex[k]) && exists|i: Instr| + is_exit(c, ex[k - 1], i) && next_instr(c, ex[k - 1]) == Some(i)), + k + 1 < ex.len() ==> inv(c, ex[k]), + decreases k, +{ + if k > 0 { + lemma_run_inv(c, e, ex, k - 1); + assert(inv(c, ex[k - 1])); + assert(steps_to::(c, e, ex[k - 1], ex[k])); + let i = choose|i: Instr| + next_instr(c, ex[k - 1]) == Some(i) && #[trigger] step(ex[k - 1], i) == Some(ex[k]); + let sp = ex[k - 1]; + let (j0, m) = choose|j0: int, m: int| pos_inv(c, sp, j0, m); + lemma_step_conf(c, sp, j0, m); + assert(confined_step::(c, e, ex[k - 1])); + if is_exit(c, ex[k - 1], i) { + assert(exited(c, ex[k]) && next_instr(c, ex[k]) is None); + if k + 1 < ex.len() { + assert(steps_to::(c, e, ex[k], ex[k + 1])); + assert(false); + } + } + } +} + +/// **The corollary over every finite A64 execution** ([`a64_run_confinement`]). +pub proof fn theorem_a64_runs() + ensures + a64_run_confinement(), +{ + assert forall|c: Cfg, e: (), ex: Seq| + cfg_ok(c) && ex.len() > 0 && #[trigger] is_run::(c, e, ex) && inv(c, ex[0]) implies run_confined::< + A64, + >(c, ex) by { + assert forall|k: int| 0 <= k && k + 1 < ex.len() implies #[trigger] inv(c, ex[k]) by { + lemma_run_inv(c, e, ex, k); + } + lemma_run_inv(c, e, ex, ex.len() - 1); + } +} + +} // verus! diff --git a/crates/clazel-codecheck/src/confine/a64/reads.rs b/crates/clazel-codecheck/src/confine/a64/reads.rs new file mode 100644 index 000000000..244dd83c1 --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/reads.rs @@ -0,0 +1,388 @@ +//! **Reads are confined** (A64): the part of the A64 confinement proof that +//! says a step's outcome depends only on the bytes of the read area. +//! `execute` reads memory only through [`mem_read`] at the access's address +//! (or decides a fault by whether the bytes of a store are mapped); the +//! lemmas below show that two memories that agree on those bytes give the same +//! outcome. [`lemma_reads_confined`] puts them together with the access facts +//! of the invariant. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The memories `m` and `m2` agree on the `n` bytes at `a`. +pub open spec fn same_bytes(m: Map, m2: Map, a: int, n: int) -> bool { + forall|x: int| + a <= x < a + n ==> (#[trigger] m2.dom().contains(x) == m.dom().contains(x) && ( + m.dom().contains(x) ==> m2[x] == m[x])) +} + +/// The bytes of a readable range lie in the positive canonical addresses and +/// are in the read area. +pub proof fn lemma_readable_range(c: Cfg, addr: int, n: int) + requires + cfg_ok(c), + readable(c, addr, n), + ensures + 0 <= addr, + addr + n <= lim(), + lim() < p2(64), + forall|x: int| addr <= x < addr + n ==> #[trigger] read_area(c, x), +{ + lemma_cfg(c); + lemma_p2(); + if c.ctx <= addr && addr + n <= c.ctx + abi::ctx_size() { + } else { + let k = choose|k: int| + 0 <= k < abi::num_regions() && c.bases[k] <= addr && addr + n <= c.bases[k] + + c.bounds[k]; + lemma_region(c, k); + assert forall|x: int| addr <= x < addr + n implies #[trigger] read_area(c, x) by { + assert(in_region(c, x, 1)); + } + } +} + +/// Agreement on the read area, at one byte of it. +pub proof fn lemma_agree_at(c: Cfg, m: Map, m2: Map, x: int) + requires + agree_on_read_area(c, m, m2), + read_area(c, x), + ensures + m2.dom().contains(x) == m.dom().contains(x), + m.dom().contains(x) ==> m2[x] == m[x], +{ + assert(read_area(c, x) ==> (m2.dom().contains(x) == m.dom().contains(x) && (m.dom().contains( + x, + ) ==> m2[x] == m[x]))); +} + +/// Memories that agree on the read area agree on every range of it. +pub proof fn lemma_agree_bytes(c: Cfg, m: Map, m2: Map, addr: int, n: int) + requires + agree_on_read_area(c, m, m2), + forall|x: int| addr <= x < addr + n ==> #[trigger] read_area(c, x), + ensures + same_bytes(m, m2, addr, n), +{ + assert forall|x: int| addr <= x < addr + n implies (#[trigger] m2.dom().contains(x) + == m.dom().contains(x) && (m.dom().contains(x) ==> m2[x] == m[x])) by { + assert(read_area(c, x)); + lemma_agree_at(c, m, m2, x); + } +} + +/// What is writable is readable. +pub proof fn lemma_writable_readable(c: Cfg, addr: int, n: int) + requires + writable(c, addr, n), + ensures + readable(c, addr, n), +{ +} + +/// The outcome of a general-register load or store is the same from two +/// memories that agree on its bytes. +pub proof fn lemma_lsg_same( + s: State, + m2: Map, + load: bool, + t: u8, + ds: nat, + address: int, +) + requires + 0 <= address, + address + ds / 8 <= p2(64), + same_bytes(s.mem, m2, address, (ds / 8) as int), + ensures + same_outcome( + load_store_gpr(s, load, t, ds, address), + load_store_gpr(State { mem: m2, ..s }, load, t, ds, address), + ), +{ + let s2 = State { mem: m2, ..s }; + if load { + lemma_mem_read_same(s.mem, m2, address, ds / 8); + } else { + lemma_mem_set_some_same( + s.mem, + m2, + address, + ds / 8, + x_read(s, t, ds), + ); + assert(x_read(s2, t, ds) == x_read(s, t, ds)); + } +} + +/// The outcome of a vector-register load or store is the same from two +/// memories that agree on its bytes. +pub proof fn lemma_lsf_same(s: State, m2: Map, load: bool, t: u8, address: int) + requires + 0 <= address, + address + 8 <= p2(64), + same_bytes(s.mem, m2, address, 8), + ensures + same_outcome( + load_store_fp(s, load, t, address), + load_store_fp(State { mem: m2, ..s }, load, t, address), + ), +{ + let s2 = State { mem: m2, ..s }; + if load { + lemma_mem_read_same(s.mem, m2, address, 8); + } else { + lemma_mem_set_some_same(s.mem, m2, address, 8, v_read(s, t, 64)); + assert(v_read(s2, t, 64) == v_read(s, t, 64)); + } +} + +/// `execute` of an instruction that makes no memory access does not depend on +/// memory, one lemma for each integer instruction (so that each is small). +pub proof fn lemma_exec_nomem_add_sub_reg(s: State, m2: Map, sf: Width, sub_op: bool, setflags: bool, d: u8, n: u8, m: u8, shift_type: ShiftType, shift_amount: u8) + ensures + same_outcome( + execute(s, Instr::AddSubShiftedReg { sf, sub_op, setflags, d, n, m, shift_type, shift_amount }), + execute(State { mem: m2, ..s }, Instr::AddSubShiftedReg { sf, sub_op, setflags, d, n, m, shift_type, shift_amount }), + ), +{ +} + +pub proof fn lemma_exec_nomem_add_sub_imm(s: State, m2: Map, sf: Width, sub_op: bool, setflags: bool, d: u8, n: u8, imm12: u16, sh: bool) + ensures + same_outcome( + execute(s, Instr::AddSubImm { sf, sub_op, setflags, d, n, imm12, sh }), + execute(State { mem: m2, ..s }, Instr::AddSubImm { sf, sub_op, setflags, d, n, imm12, sh }), + ), +{ +} + +pub proof fn lemma_exec_nomem_mul_add_sub(s: State, m2: Map, sf: Width, sub_op: bool, d: u8, n: u8, m: u8, a: u8) + ensures + same_outcome( + execute(s, Instr::MulAddSub { sf, sub_op, d, n, m, a }), + execute(State { mem: m2, ..s }, Instr::MulAddSub { sf, sub_op, d, n, m, a }), + ), +{ +} + +pub proof fn lemma_exec_nomem_div(s: State, m2: Map, sf: Width, is_unsigned: bool, d: u8, n: u8, m: u8) + ensures + same_outcome( + execute(s, Instr::Div { sf, is_unsigned, d, n, m }), + execute(State { mem: m2, ..s }, Instr::Div { sf, is_unsigned, d, n, m }), + ), +{ +} + +pub proof fn lemma_exec_nomem_logical(s: State, m2: Map, sf: Width, opc: LogicalOpc, d: u8, n: u8, m: u8, shift_type: ShiftType, shift_amount: u8) + ensures + same_outcome( + execute(s, Instr::LogicalShiftedReg { sf, opc, d, n, m, shift_type, shift_amount }), + execute(State { mem: m2, ..s }, Instr::LogicalShiftedReg { sf, opc, d, n, m, shift_type, shift_amount }), + ), +{ +} + +pub proof fn lemma_exec_nomem_shift_var(s: State, m2: Map, sf: Width, shift_type: ShiftType, d: u8, n: u8, m: u8) + ensures + same_outcome( + execute(s, Instr::ShiftVariable { sf, shift_type, d, n, m }), + execute(State { mem: m2, ..s }, Instr::ShiftVariable { sf, shift_type, d, n, m }), + ), +{ +} + +pub proof fn lemma_exec_nomem_move_wide(s: State, m2: Map, sf: Width, opcode: MoveWideOp, d: u8, imm16: u16, hw: u8) + ensures + same_outcome( + execute(s, Instr::MoveWide { sf, opcode, d, imm16, hw }), + execute(State { mem: m2, ..s }, Instr::MoveWide { sf, opcode, d, imm16, hw }), + ), +{ +} + +pub proof fn lemma_exec_nomem_cond_select(s: State, m2: Map, sf: Width, else_inv: bool, else_inc: bool, d: u8, n: u8, m: u8, cond: u8) + ensures + same_outcome( + execute(s, Instr::CondSelect { sf, else_inv, else_inc, d, n, m, cond }), + execute(State { mem: m2, ..s }, Instr::CondSelect { sf, else_inv, else_inc, d, n, m, cond }), + ), +{ +} + +/// [`lemma_exec_nomem_add_sub_reg`] and the others, for the integer instructions. +pub proof fn lemma_exec_nomem_int(s: State, m2: Map, i: Instr) + requires + i is AddSubShiftedReg || i is AddSubImm || i is MulAddSub || i is Div || i is + LogicalShiftedReg || i is ShiftVariable || i is MoveWide || i is CondSelect, + ensures + same_outcome(execute(s, i), execute(State { mem: m2, ..s }, i)), +{ + match i { + Instr::AddSubShiftedReg { sf, sub_op, setflags, d, n, m, shift_type, shift_amount } => lemma_exec_nomem_add_sub_reg(s, m2, sf, sub_op, setflags, d, n, m, shift_type, shift_amount), + Instr::AddSubImm { sf, sub_op, setflags, d, n, imm12, sh } => lemma_exec_nomem_add_sub_imm(s, m2, sf, sub_op, setflags, d, n, imm12, sh), + Instr::MulAddSub { sf, sub_op, d, n, m, a } => lemma_exec_nomem_mul_add_sub(s, m2, sf, sub_op, d, n, m, a), + Instr::Div { sf, is_unsigned, d, n, m } => lemma_exec_nomem_div(s, m2, sf, is_unsigned, d, n, m), + Instr::LogicalShiftedReg { sf, opc, d, n, m, shift_type, shift_amount } => lemma_exec_nomem_logical(s, m2, sf, opc, d, n, m, shift_type, shift_amount), + Instr::ShiftVariable { sf, shift_type, d, n, m } => lemma_exec_nomem_shift_var(s, m2, sf, shift_type, d, n, m), + Instr::MoveWide { sf, opcode, d, imm16, hw } => lemma_exec_nomem_move_wide(s, m2, sf, opcode, d, imm16, hw), + Instr::CondSelect { sf, else_inv, else_inc, d, n, m, cond } => lemma_exec_nomem_cond_select(s, m2, sf, else_inv, else_inc, d, n, m, cond), + _ => {}, + } +} + +/// [`lemma_exec_nomem_int`] for `SBFM`/`UBFM`. +pub proof fn lemma_exec_nomem_bitfield(s: State, m2: Map, i: Instr) + requires + i is Bitfield, + ensures + same_outcome(execute(s, i), execute(State { mem: m2, ..s }, i)), +{ +} + +/// [`lemma_exec_nomem_int`] for the floating-point instructions. +pub proof fn lemma_exec_nomem_fp(s: State, m2: Map, i: Instr) + requires + i is FAddSub || i is FMul || i is FDiv || i is FUnary || i is FCmp || i is FMovGen || i is + Scvtf || i is Fcvtzs, + ensures + same_outcome(execute(s, i), execute(State { mem: m2, ..s }, i)), +{ + match i { + Instr::FAddSub { .. } => {}, + Instr::FMul { .. } => {}, + Instr::FDiv { .. } => {}, + Instr::FUnary { .. } => {}, + Instr::FCmp { .. } => {}, + Instr::FMovGen { .. } => {}, + Instr::Scvtf { .. } => {}, + Instr::Fcvtzs { .. } => {}, + _ => {}, + } +} + +/// `execute` of an instruction that makes no memory access does not depend on +/// memory. +pub proof fn lemma_exec_nomem(s: State, m2: Map, i: Instr) + requires + mem_access(i) is None, + ensures + same_outcome(execute(s, i), execute(State { mem: m2, ..s }, i)), +{ + match i { + Instr::Bitfield { .. } => lemma_exec_nomem_bitfield(s, m2, i), + Instr::LoadStoreImm { .. } => { + assert(false); + }, + Instr::LoadStoreReg { .. } => { + assert(false); + }, + Instr::FpLoadStoreImm { .. } => { + assert(false); + }, + Instr::FpLoadStoreReg { .. } => { + assert(false); + }, + Instr::B { .. } => {}, + Instr::BCond { .. } => {}, + Instr::CompareBranch { .. } => {}, + Instr::Ret => {}, + Instr::FAddSub { .. } | Instr::FMul { .. } | Instr::FDiv { .. } | Instr::FUnary { .. } + | Instr::FCmp { .. } | Instr::FMovGen { .. } | Instr::Scvtf { .. } | Instr::Fcvtzs { .. } => + lemma_exec_nomem_fp(s, m2, i), + _ => lemma_exec_nomem_int(s, m2, i), + } +} + +/// `branch_to` keeps agreement apart from memory. +pub proof fn lemma_branch_same(o: Option, o2: Option, target: int) + requires + same_outcome(o, o2), + ensures + same_outcome( + match o { + Some(s1) => Some(branch_to(s1, target)), + None => None, + }, + match o2 { + Some(s1) => Some(branch_to(s1, target)), + None => None, + }, + ), +{ +} + +/// **Reads are confined**: for a valid instruction whose accesses are +/// permitted, the step's outcome (apart from memory) is the same from every +/// memory that agrees with the state's on the read area. +pub proof fn lemma_reads_confined(c: Cfg, s: State, i: Instr) + requires + cfg_ok(c), + valid_instr(i), + loads_ok(c, s, i), + stores_ok(c, s, i), + ensures + reads_confined(c, s, i), +{ + assert forall|m2: Map| agree_on_read_area(c, s.mem, m2) implies #[trigger] same_outcome( + step(s, i), + step(State { mem: m2, ..s }, i), + ) by { + let s2 = State { mem: m2, ..s }; + match mem_access(i) { + None => { + lemma_exec_nomem(s, m2, i); + match i { + Instr::B { .. } => {}, + Instr::BCond { .. } => {}, + Instr::CompareBranch { .. } => {}, + Instr::Ret => {}, + _ => { + lemma_branch_same( + execute(s, i), + execute(s2, i), + s.pc as int + 4, + ); + }, + } + }, + Some(a) => { + let addr = eff_addr(s, a); + assert(eff_addr(s2, a) == addr); + if a.store { + lemma_writable_readable(c, addr, a.size); + } + lemma_readable_range(c, addr, a.size); + lemma_agree_bytes(c, s.mem, m2, addr, a.size); + match i { + Instr::LoadStoreImm { sf, load, t, n, imm12 } => { + lemma_lsg_same(s, m2, load, t, datasize(sf), addr); + lemma_branch_same(execute(s, i), execute(s2, i), s.pc as int + 4); + }, + Instr::LoadStoreReg { sf, load, t, n, m, s: sbit } => { + lemma_lsg_same(s, m2, load, t, datasize(sf), addr); + lemma_branch_same(execute(s, i), execute(s2, i), s.pc as int + 4); + }, + Instr::FpLoadStoreImm { load, t, n, imm12 } => { + lemma_lsf_same(s, m2, load, t, addr); + lemma_branch_same(execute(s, i), execute(s2, i), s.pc as int + 4); + }, + Instr::FpLoadStoreReg { load, t, n, m, s: sbit } => { + lemma_lsf_same(s, m2, load, t, addr); + lemma_branch_same(execute(s, i), execute(s2, i), s.pc as int + 4); + }, + _ => { + assert(false); + }, + } + }, + } + } +} + +} // verus! diff --git a/crates/clazel-codecheck/src/confine/a64/sanity.rs b/crates/clazel-codecheck/src/confine/a64/sanity.rs new file mode 100644 index 000000000..e31e14b4f --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/sanity.rs @@ -0,0 +1,194 @@ +//! **The hypotheses of the A64 theorem are satisfiable**: a concrete +//! configuration ([`sa_cfg`], the exit sequence `movz w0, #7; str x0, [x28, +//! #256]; ret`, entered at 0, with 16 empty regions) that passes [`cfg_ok`], +//! and a concrete state ([`sa_state`]) that satisfies [`inv`] at its entry. A +//! theorem whose hypotheses nothing satisfies would say nothing; this is the +//! check that it does. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The exit sequence's instructions. +pub open spec fn sa_ins() -> Seq { + crate::a64::examples::exit_seq(0, 7) +} + +/// Its bytes. +pub open spec fn sa_blob() -> Seq { + crate::a64::examples::exit_blob() +} + +/// The configuration: code at 4096, the context at 65536, sixteen empty +/// regions at address 0, `SP` 131072, the stub's return address 8. +pub open spec fn sa_cfg() -> Cfg { + Cfg { + blob: sa_blob(), + ins: sa_ins(), + entries: seq![0int], + code: 4096, + ctx: 65536, + bases: Seq::new(16, |k: int| 0int), + bounds: Seq::new(16, |k: int| 0int), + sp: 131072, + lr: 8, + } +} + +/// The mapped bytes: the code and the context. +pub open spec fn sa_mapped(a: int) -> bool { + (4096 <= a < 4108) || (65536 <= a < 66048) +} + +/// The byte at `a`. +pub open spec fn sa_byte(a: int) -> u8 { + if 4096 <= a < 4108 { + sa_blob()[a - 4096] + } else { + 0u8 + } +} + +/// The domain of the memory: the mapped bytes, as a finite set. +pub open spec fn sa_dom() -> Set { + Set::::range(4096, 4108) + Set::::range(65536, 66048) +} + +/// The memory. +pub open spec fn sa_mem() -> Map { + Map::new(sa_dom(), |a: int| sa_byte(a)) +} + +/// The domain is the mapped bytes. +pub proof fn lemma_sa_dom(a: int) + ensures + sa_dom().contains(a) == sa_mapped(a), +{ + vstd::set_lib::lemma_int_range(4096, 4108); + vstd::set_lib::lemma_int_range(65536, 66048); + assert(Set::::range(4096, 4108) == vstd::set_lib::set_int_range(4096, 4108)); + assert(Set::::range(65536, 66048) == vstd::set_lib::set_int_range(65536, 66048)); +} + +/// The state at entry: `x28` the context, `x30` the return address, `SP`, +/// the program counter at the entry. +pub open spec fn sa_state() -> State { + State { + r: Seq::new(31, |r: int| if r == 28 { + 65536u64 + } else if r == 30 { + 8u64 + } else { + 0u64 + }), + sp: 131072, + pc: 4096, + flag_n: false, + flag_z: false, + flag_c: false, + flag_v: false, + vreg: Seq::new(32, |r: int| 0u128), + mem: sa_mem(), + } +} + +/// Reading `n` bytes that are mapped and zero gives 0. +pub proof fn lemma_read_zero(m: Map, p: int, n: nat) + requires + 0 <= p, + p + n < p2(64), + forall|x: int| p <= x < p + n ==> #[trigger] m.dom().contains(x), + forall|x: int| p <= x < p + n ==> #[trigger] m[x] == 0u8, + ensures + mem_read(m, p, n) == Some(0int), + decreases n, +{ + if n > 0 { + lemma_trunc64_id(p); + assert(m.dom().contains(p)); + assert(m[p] == 0u8); + lemma_read_zero(m, p + 1, (n - 1) as nat); + } +} + +/// **The configuration is one the sandbox may hand over.** +pub proof fn example_cfg_ok() + ensures + cfg_ok(sa_cfg()), +{ + lemma_p2(); + crate::a64::examples::example_exit_accepted(); + crate::a64::examples::example_exit_valid(); + crate::a64::examples::example_bytes_decode(); + reveal(cfg_ok); + let c = sa_cfg(); + assert(decodes_to(sa_blob(), sa_ins())); + assert(policy(sa_ins(), seq![0int])); + assert(sa_blob().len() == 12) by (compute_only); + assert forall|k: int| 0 <= k < abi::num_regions() implies #[trigger] region_ok(c, k) by { + assert(c.bases[k] == 0 && c.bounds[k] == 0); + } + assert(cfg_ok(c)); +} + +/// **The state at the entry satisfies the invariant.** +pub proof fn example_inv() + ensures + inv(sa_cfg(), sa_state()), +{ + lemma_p2(); + example_cfg_ok(); + let c = sa_cfg(); + let s = sa_state(); + let m = sa_mem(); + lemma_cfg(c); + assert(wf(s)) by { + assert forall|a: int| s.mem.dom().contains(a) implies 0 <= a < 0x100_0000_0000_0000 by { + lemma_sa_dom(a); + assert(sa_mapped(a)); + } + } + assert(s.r[28int] == 65536u64 && s.r[30int] == 8u64); + lemma_xr(s, 28); + lemma_xr(s, 30); + assert forall|k: int| 0 <= k < abi::num_regions() implies mem_read( + m, + c.ctx + abi::base_off(k), + 8, + ) == Some(c.bases[k]) && #[trigger] mem_read(m, c.ctx + abi::bound_off(k), 8) == Some( + c.bounds[k], + ) by { + assert(c.bases[k] == 0 && c.bounds[k] == 0); + let p1 = c.ctx + abi::base_off(k); + let p2_ = c.ctx + abi::bound_off(k); + assert forall|x: int| p1 <= x < p1 + 8 implies #[trigger] m.dom().contains(x) by { + lemma_sa_dom(x); + } + assert forall|x: int| p1 <= x < p1 + 8 implies #[trigger] m[x] == 0u8 by {} + assert forall|x: int| p2_ <= x < p2_ + 8 implies #[trigger] m.dom().contains(x) by { + lemma_sa_dom(x); + } + assert forall|x: int| p2_ <= x < p2_ + 8 implies #[trigger] m[x] == 0u8 by {} + lemma_read_zero(m, p1, 8); + lemma_read_zero(m, p2_, 8); + } + assert forall|q: int| 0 <= q < c.blob.len() implies #[trigger] m.dom().contains(c.code + q) + && m[c.code + q] == c.blob[q] by { + lemma_sa_dom(c.code + q); + assert(sa_mapped(c.code + q)); + } + assert(base_inv(c, s)); + // The position: the first instruction of the exit sequence. + reveal_with_fuel(chain_facts, 2); + assert(start_facts(c, 0) == Some(no_facts())); + assert(exit_unit_at(sa_ins(), 0)) by (compute_only); + assert(unit_at(sa_ins(), 0, no_facts()) == Some(Unit { len: 3, facts: no_facts() })); + assert(start_of(sa_ins(), 0) == 0); + lemma_no_facts_hold(c, s); + assert(pos_inv(c, s, 0, 0)); +} + +} // verus! diff --git a/crates/clazel-codecheck/src/confine/a64/steps.rs b/crates/clazel-codecheck/src/confine/a64/steps.rs new file mode 100644 index 000000000..6c3e4e7cc --- /dev/null +++ b/crates/clazel-codecheck/src/confine/a64/steps.rs @@ -0,0 +1,471 @@ +//! **One step, unit by unit** (A64): from the invariant at a position, the +//! next instruction's accesses are confined and the step either keeps the +//! invariant or leaves through the exit. This file does the exit sequence and +//! the single instructions; `super::check` does the bounds-check sequence. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Both of the accesses of `i` from `s` are in bounds. +pub open spec fn acc_ok(c: Cfg, s: State, i: Instr) -> bool { + loads_ok(c, s, i) && stores_ok(c, s, i) +} + +/// What the theorem says of a step from `s`, executing `i`, to `s2`. +pub open spec fn step_post(c: Cfg, s: State, i: Instr, s2: State) -> bool { + writes_confined(c, s, s2) && if is_exit(c, s, i) { + exited(c, s2) && next_instr(c, s2) is None + } else { + inv(c, s2) && reserved_same(s, s2) + } +} + +/// The instruction `ret` is the exit only in an exit sequence. +pub proof fn lemma_is_exit_ret(c: Cfg, s: State, i: Instr) + requires + is_exit(c, s, i), + ensures + i is Ret, +{ + let j = choose|j: int| + exit_unit_at(c.ins, j) && i == c.ins[j + 2] && s.pc as int == c.code + start_of( + c.ins, + j + 2, + ); +} + +/// An instruction that is not `ret` is not an exit. +pub proof fn lemma_not_exit(c: Cfg, s: State, i: Instr) + requires + !(i is Ret), + ensures + !is_exit(c, s, i), +{ + if is_exit(c, s, i) { + lemma_is_exit_ret(c, s, i); + } +} + +/// An access of an instruction has a positive size of at most 8, and names +/// registers below 32. +pub proof fn lemma_access_regs(i: Instr) + requires + valid_instr(i), + mem_access(i) is Some, + ensures + 0 < mem_access(i)->Some_0.size <= abi::max_access(), + mem_access(i)->Some_0.base < 32, + match mem_access(i)->Some_0.index { + Some(m) => m < 32, + None => true, + }, +{ +} + +/// A permitted access (by the scan, given facts that hold) of the valid +/// instruction `i` is in bounds. +pub proof fn lemma_acc_ok(c: Cfg, s: State, fs: Facts, i: Instr) + requires + cfg_ok(c), + base_inv(c, s), + facts_hold(c, s, fs), + valid_instr(i), + match mem_access(i) { + Some(a) => access_ok(fs, a), + None => true, + }, + ensures + acc_ok(c, s, i), +{ + lemma_base_inv_split(c, s); + match mem_access(i) { + Some(a) => { + lemma_access_regs(i); + lemma_access_safe(c, s, fs, a); + }, + None => {}, + } +} + +/// `is_system` is false. +pub proof fn lemma_not_system(i: Instr) + ensures + !is_system(i), +{ +} + +/// A step that moves the program counter on by one word does not wrap. +pub proof fn lemma_pc_succ(c: Cfg, s: State, s2: State, j0: int, m: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, m), + s2.pc as int == trunc(64, s.pc as int + 4), + ensures + s2.pc as int == s.pc as int + 4, +{ + lemma_cfg(c); + lemma_cur_instr(c, s, j0, m); + lemma_p2(); + let v = s.pc as int + 4; + assert(0 <= v < p2(64)); + lemma_trunc64_id(v); +} + +// ---------------------------------------------------------------------------- +// Section: the exit sequence +// ---------------------------------------------------------------------------- + +/// The step of the exit sequence's `ret`. +pub proof fn lemma_ret_step(c: Cfg, s: State, s2: State) + requires + cfg_ok(c), + base_inv(c, s), + step(s, Instr::Ret) == Some(s2), + ensures + s2.pc as int == c.lr, + x_read(s2, abi::ctx_reg(), 64) == c.ctx, + x_read(s2, abi::link_reg(), 64) == c.lr, + s2.sp as int == c.sp, + s2.mem == s.mem, +{ + lemma_cfg(c); + lemma_p2(); + lemma_trunc64_id(c.lr); + assert(x_read(s, 30, 64) == c.lr); + assert(s2 == branch_to(s, x_read(s, 30, 64))); + assert(trunc(64, c.lr) == c.lr); + assert(s2.pc == c.lr as u64); +} + +/// **After the exit `ret`, no instruction of the blob is next.** +pub proof fn lemma_exit_no_next(c: Cfg, s2: State) + requires + cfg_ok(c), + s2.pc as int == c.lr, + ensures + next_instr(c, s2) is None, +{ + lemma_cfg(c); + if index_at(c, s2) is Some { + let j = choose|j: int| 0 <= j < c.ins.len() && start_of(c.ins, j) == s2.pc as int - c.code; + assert(disjoint(c.lr, 1, c.code, c.blob.len() as int)); + assert(false); + } +} + +/// **The exit sequence** (three instructions at `j0`): its accesses are in +/// bounds, and each step either stays in the sequence or, at the `ret`, +/// leaves. +pub proof fn lemma_exit_unit(c: Cfg, s: State, j0: int, m: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, m), + exit_unit_at(c.ins, j0), + ensures + acc_ok(c, s, c.ins[j0 + m]), + forall|s2: State| step(s, c.ins[j0 + m]) == Some(s2) ==> step_post(c, s, c.ins[j0 + m], s2), +{ + lemma_cur_instr(c, s, j0, m); + lemma_base_inv_split(c, s); + let fs0 = start_facts(c, j0)->Some_0; + assert(unit_at(c.ins, j0, fs0) == Some(Unit { len: 3, facts: no_facts() })); + let i = c.ins[j0 + m]; + assert(wf(s)); + match (c.ins[j0], c.ins[j0 + 1], c.ins[j0 + 2]) { + ( + Instr::MoveWide { sf: Width::W32, opcode: MoveWideOp::Z, d: t, imm16, hw }, + Instr::LoadStoreImm { sf: Width::W64, load: false, t: src, n, imm12 }, + Instr::Ret, + ) => { + if m == 0 { + assert(i == c.ins[j0]); + assert(mem_access(i) is None); + assert(acc_ok(c, s, i)); + lemma_not_exit(c, s, i); + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_step_straight(s, i, s2); + lemma_pc_succ(c, s, s2, j0, m); + assert(written_gpr(i) == Some(t)); + lemma_base_inv_after(c, s, s2, Some(t)); + lemma_facts_set(c, s, s2, fs0, t, Fact::Unknown); + assert(exit_mid_facts(c.ins, j0, fs0) == set_fact(fs0, t, Fact::Unknown)); + lemma_pos_mid(c, s, s2, j0, 0); + lemma_confined_same(c, s, s2); + assert(inv(c, s2)); + } + } else if m == 1 { + let fsm = exit_mid_facts(c.ins, j0, fs0); + assert(i == c.ins[j0 + 1]); + let a = mem_access(i)->Some_0; + assert(access_ok(fsm, a)); + lemma_acc_ok(c, s, fsm, i); + lemma_not_exit(c, s, i); + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_step_straight(s, i, s2); + lemma_pc_succ(c, s, s2, j0, m); + let addr = eff_addr(s, a); + assert(writable(c, addr, a.size)); + let data = choose|data: int| + #[trigger] mem_set(s.mem, addr, a.size as nat, data) == Some(s2.mem); + lemma_p2(); + lemma_cfg(c); + assert(0 <= addr && addr + a.size <= p2(64)) by { + lemma_writable_in_range(c, addr, a.size); + } + lemma_mem_set_frame(s.mem, addr, a.size as nat, data, s2.mem); + lemma_mem_inv_store(c, s.mem, s2.mem, addr, a.size); + assert(written_gpr(i) is None); + lemma_base_inv_after(c, s, s2, None); + lemma_facts_keep(c, s, s2, fsm, None); + lemma_pos_mid(c, s, s2, j0, 1); + assert(store_frame(s, s2, addr, a.size)); + lemma_confined_store(c, s, s2, addr, a.size); + assert(inv(c, s2)); + } + } else { + assert(m == 2); + assert(i == c.ins[j0 + 2]); + assert(mem_access(i) is None); + assert(acc_ok(c, s, i)); + assert(is_exit(c, s, i)) by { + assert(exit_unit_at(c.ins, j0) && i == c.ins[j0 + 2] && s.pc as int == c.code + + start_of(c.ins, j0 + 2)); + } + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_ret_step(c, s, s2); + lemma_confined_same(c, s, s2); + lemma_exit_no_next(c, s2); + } + } + }, + _ => { + assert(false); + }, + } +} + +/// Writable bytes lie within the positive canonical addresses. +pub proof fn lemma_writable_in_range(c: Cfg, addr: int, size: int) + requires + cfg_ok(c), + writable(c, addr, size), + ensures + 0 <= addr, + addr + size <= lim(), + lim() < p2(64), +{ + lemma_cfg(c); + lemma_p2(); + if c.ctx + abi::exit_code_off() <= addr && addr + size <= c.ctx + abi::ctx_size() { + } else { + let k = choose|k: int| + 0 <= k < abi::num_regions() && c.bases[k] <= addr && addr + size <= c.bases[k] + + c.bounds[k]; + lemma_region(c, k); + } +} + +// ---------------------------------------------------------------------------- +// Section: single instructions +// ---------------------------------------------------------------------------- + +/// A 64-bit load of `[x28, #16k]` loads region `k`'s base. +pub proof fn lemma_base_load_value(c: Cfg, s: State, i: Instr, s2: State, k: int) + requires + cfg_ok(c), + base_inv(c, s), + valid_instr(i), + base_load_region(i) == Some(k), + step(s, i) == Some(s2), + ensures + 0 <= k < abi::num_regions(), + written_gpr(i) is Some, + written_gpr(i)->Some_0 < 31, + x_read(s2, written_gpr(i)->Some_0, 64) == c.bases[k], +{ + lemma_cfg(c); + lemma_p2(); + lemma_base_inv_split(c, s); + match i { + Instr::LoadStoreImm { sf, load, t, n, imm12 } => { + assert(imm12 as int * 8 == 16 * k); + assert(0 <= k < abi::num_regions()); + lemma_xr(s, abi::ctx_reg()); + let addr = c.ctx + 16 * k; + lemma_region(c, k); + assert(0 <= addr < p2(64)); + lemma_trunc64_id(addr); + assert(mem_read(s.mem, c.ctx + abi::bound_off(k), 8) == Some(c.bounds[k])); + assert(mem_read(s.mem, c.ctx + abi::base_off(k), 8) == Some(c.bases[k])); + lemma_step_straight(s, i, s2); + let s1 = execute(s, i)->Some_0; + assert(sf is W64 && load); + lemma_trunc64_id(c.bases[k]); + assert(s1 == x_set(s, t, 64, c.bases[k])); + assert(t < 31); + assert(s2.r == s1.r); + lemma_xr(s2, t); + }, + _ => { + assert(false); + }, + } +} + +/// **A single instruction**: if the scan permits it (it is not part of a +/// recognized sequence), its accesses are in bounds and its step keeps the +/// invariant. +pub proof fn lemma_plain_unit(c: Cfg, s: State, j0: int, m: int) + requires + cfg_ok(c), + pos_inv(c, s, j0, m), + !exit_unit_at(c.ins, j0), + !check_unit_at(c.ins, j0), + ensures + acc_ok(c, s, c.ins[j0 + m]), + forall|s2: State| step(s, c.ins[j0 + m]) == Some(s2) ==> step_post(c, s, c.ins[j0 + m], s2), +{ + lemma_cur_instr(c, s, j0, m); + lemma_base_inv_split(c, s); + lemma_cfg(c); + lemma_p2(); + let fs0 = start_facts(c, j0)->Some_0; + let i = c.ins[j0]; + assert(m == 0); + assert(facts_hold(c, s, fs0)); + let pk = plain_ok(fs0, i); + assert(pk is Some); + let fs1 = pk->Some_0; + assert(unit_at(c.ins, j0, fs0) == Some(Unit { len: 1, facts: fs1 })); + assert(!(i is Ret)); + assert(!writes_reserved(i)); + assert(wf(s)); + lemma_acc_ok(c, s, fs0, i); + lemma_not_exit(c, s, i); + match i { + Instr::B { imm26 } => { + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + assert(s2 == branch_to(s, s.pc as int + imm26 as int * 4)); + lemma_base_inv_after(c, s, s2, None); + lemma_confined_same(c, s, s2); + assert(jump_rel(i) == Some(imm26 as int * 4)); + lemma_pos_jump(c, s, s2, j0); + assert(inv(c, s2)); + } + }, + Instr::BCond { cond, imm19 } => { + assert(fs1 == fs0); + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_confined_same_branch(c, s, s2, i); + lemma_base_inv_after(c, s, s2, None); + lemma_confined_same(c, s, s2); + if condition_holds(s, cond) { + assert(s2 == branch_to(s, s.pc as int + imm19 as int * 4)); + assert(jump_rel(i) == Some(imm19 as int * 4)); + lemma_pos_jump(c, s, s2, j0); + } else { + assert(s2 == branch_to(s, s.pc as int + 4)); + lemma_pc_succ(c, s, s2, j0, m); + lemma_not_last(c, j0); + lemma_pos_fall(c, s, s2, j0, 0); + } + assert(inv(c, s2)); + } + }, + Instr::CompareBranch { sf, iszero, t, imm19 } => { + assert(fs1 == fs0); + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_confined_same_branch(c, s, s2, i); + lemma_base_inv_after(c, s, s2, None); + lemma_confined_same(c, s, s2); + let operand1 = x_read(s, t, datasize(sf)); + if (operand1 == 0) == iszero { + assert(s2 == branch_to(s, s.pc as int + imm19 as int * 4)); + assert(jump_rel(i) == Some(imm19 as int * 4)); + lemma_pos_jump(c, s, s2, j0); + } else { + assert(s2 == branch_to(s, s.pc as int + 4)); + lemma_pc_succ(c, s, s2, j0, m); + lemma_not_last(c, j0); + lemma_pos_fall(c, s, s2, j0, 0); + } + assert(inv(c, s2)); + } + }, + Instr::Ret => { + assert(false); + }, + _ => { + lemma_not_last(c, j0); + assert forall|s2: State| step(s, i) == Some(s2) implies step_post(c, s, i, s2) by { + lemma_step_straight(s, i, s2); + lemma_pc_succ(c, s, s2, j0, m); + let d = written_gpr(i); + // The memory invariant. + match mem_access(i) { + Some(a) => { + if a.store { + let addr = eff_addr(s, a); + assert(writable(c, addr, a.size)); + let data = choose|data: int| + #[trigger] mem_set(s.mem, addr, a.size as nat, data) == Some(s2.mem); + lemma_writable_in_range(c, addr, a.size); + lemma_mem_set_frame(s.mem, addr, a.size as nat, data, s2.mem); + lemma_mem_inv_store(c, s.mem, s2.mem, addr, a.size); + assert(store_frame(s, s2, addr, a.size)); + lemma_confined_store(c, s, s2, addr, a.size); + } else { + lemma_confined_same(c, s, s2); + } + }, + None => { + lemma_confined_same(c, s, s2); + }, + } + lemma_base_inv_after(c, s, s2, d); + // The facts. + match d { + None => { + assert(fs1 == fs0); + lemma_facts_keep(c, s, s2, fs0, None); + }, + Some(dd) => { + assert(dd < 32); + match base_load_region(i) { + Some(k) => { + lemma_base_load_value(c, s, i, s2, k); + lemma_facts_set(c, s, s2, fs0, dd, Fact::Base(k)); + }, + None => { + lemma_facts_set(c, s, s2, fs0, dd, Fact::Unknown); + }, + } + }, + } + lemma_pos_fall(c, s, s2, j0, 0); + assert(inv(c, s2)); + } + }, + } +} + +/// The step of a conditional branch changes only the program counter. +pub proof fn lemma_confined_same_branch(c: Cfg, s: State, s2: State, i: Instr) + requires + i is BCond || i is CompareBranch, + step(s, i) == Some(s2), + ensures + s2.mem == s.mem, + s2.r == s.r, + s2.vreg == s.vreg, + s2.sp == s.sp, + s2.r.len() == s.r.len(), + s2.vreg.len() == s.vreg.len(), +{ +} + +} // verus! diff --git a/crates/clazel-codecheck/src/confine/machine.rs b/crates/clazel-codecheck/src/confine/machine.rs index f345422bb..11d9e707c 100644 --- a/crates/clazel-codecheck/src/confine/machine.rs +++ b/crates/clazel-codecheck/src/confine/machine.rs @@ -7,7 +7,7 @@ //! theorem needs (which accesses an instruction makes, what the machine-level //! invariant is, what leaving through the exit sequence looks like). Each //! instruction set implements it from its own semantics -//! ([`super::x64::X64`] is the only implementation so far; A64's is later work). +//! ([`super::x64::X64`] and [`super::a64::instance::A64`]). //! //! The statement is [`confinement`], a closed spec function. A proof of it for //! one machine is a proof function whose `ensures` is `confinement::()`. diff --git a/crates/clazel-codecheck/src/confine/mod.rs b/crates/clazel-codecheck/src/confine/mod.rs index a15a3f263..721c080dc 100644 --- a/crates/clazel-codecheck/src/confine/mod.rs +++ b/crates/clazel-codecheck/src/confine/mod.rs @@ -5,6 +5,7 @@ //! does not write the reserved registers. //! //! - [`machine`]: the statement, once, over an abstract [`machine::Machine`]. +//! - [`a64`]: the A64 instance of the same statement (its own module tree). //! - [`x64`]: the x86-64 instance: configuration, machine-level invariant, //! observations. Statement only. //! - The x86-64 proof, [`x64proof`], from: [`frame`] (what `effect` leaves @@ -18,7 +19,9 @@ //! [`machine`]'s header for exactly what it says and does not say, and //! [`x64`]'s for the x86-64 modelling choices. The proof is complete for //! x86-64: [`x64proof::theorem_x64`] and [`x64proof::theorem_x64_runs`]. It -//! uses no axiom. A64's is later work. The crate's tier is unchanged: nothing +//! uses no axiom. A64's, over the same statement, is in [`a64`] +//! ([`a64::proof::theorem_a64`] and [`a64::proof::theorem_a64_runs`]), also +//! with no axiom. The crate's tier is unchanged: nothing //! here counts toward a tier until CI's full verification is green //! (CLAUDE.md rule 7). @@ -50,6 +53,7 @@ pub mod spec { pub use crate::spec::*; } +pub mod a64; pub mod chain; pub mod check; pub mod flags; diff --git a/crates/clazel-codecheck/src/lib.rs b/crates/clazel-codecheck/src/lib.rs index d905fa992..98f490ca6 100644 --- a/crates/clazel-codecheck/src/lib.rs +++ b/crates/clazel-codecheck/src/lib.rs @@ -1,8 +1,7 @@ //! The JIT's code validators (docs/phases/jit.md item J.6): the x86-64 one //! in this module's root, the A64 one in [`a64`], and the second, independent //! x86-64 validator (a dataflow analysis, proven to accept only what the -//! first accepts) in [`dataflow`]. The A64 dataflow validator and the -//! confinement theorem are later work. +//! first accepts) in [`dataflow`]. The A64 dataflow validator is later work. //! //! This is the first part of item J.6: the specification of what the //! validator accepts and the contract of its entry point, written before any @@ -35,9 +34,10 @@ //! (each unit check equal to its spec function) and [`scan_exec`] (the scan //! equal to the spec's recursive `scan`). The tier stays 0 until CI's full //! verification runs (CLAUDE.md rule 7). The theorem of jit.md exit criterion 2 -//! (an accepted blob is confined) is stated and proven for x86-64 in -//! [`confine`], not in [`spec`] or [`validate`]: it is about the semantics of -//! `clazel-x64`, over the acceptance spec defined here. +//! (an accepted blob is confined) is stated once and proven for x86-64 and for A64 +//! in [`confine`] (A64's in [`confine::a64`]), not in [`spec`] or [`validate`]: +//! it is about the semantics of `clazel-x64` and `clazel-a64`, over the +//! acceptance specs defined here. //! //! The A64 validator ([`a64`]) has the same shape and the same status: a spec //! and contract committed first, then [`a64::validate::validate`], proven sound diff --git a/crates/clazel-css/tests/bombs.rs b/crates/clazel-css/tests/bombs.rs index 7d9ed13ff..21276361e 100644 --- a/crates/clazel-css/tests/bombs.rs +++ b/crates/clazel-css/tests/bombs.rs @@ -21,18 +21,32 @@ fn time(input: &[u32]) -> Duration { spent } -/// Parses at two sizes and fails if the larger is out of proportion. Times are noisy, so the -/// limit is far above 2 and far below the 16 that quadratic work would give for 4 times the -/// input, and a time too short to measure is passed. +/// How many times each size is parsed; the least time counts. +const RUNS: usize = 7; + +/// The least of [`RUNS`] parses of `input`. Preemption, a page fault storm or a busy +/// neighbour on a loaded runner only ever adds time to a run (macOS full run 44824551100 +/// measured `"[" "]"` at 8.6 ms then 96 ms, 11 times for 4 times the input, in a test binary +/// sharing the machine with others), so the minimum is the estimate least disturbed by it. +/// It does not hide worse-than-linear work: that is slow in every run, and its minimum +/// grows 16-fold for 4 times the input, as the limit below is meant to catch. +fn fastest(input: &[u32]) -> Duration { + (0..RUNS).map(|_| time(input)).min().unwrap_or_default() +} + +/// Parses at two sizes and fails if the larger is out of proportion. Each time is the +/// minimum of several runs ([`fastest`]), so a pause in one run does not decide the test. +/// The limit is still far above 4 (4 times the input) and far below the 16 that quadratic +/// work would give, and a time too short to measure is passed. fn linear(open: &str, close: &str) { let small = nested(open, close, 50_000); let large = nested(open, close, 200_000); - let (a, b) = (time(&small), time(&large)); + let (a, b) = (fastest(&small), fastest(&large)); println!("{open:?} {close:?}: {a:?} then {b:?}"); let floor = Duration::from_millis(5); assert!( b < a.max(floor).saturating_mul(10), - "{open:?} {close:?} took {a:?} then {b:?}" + "{open:?} {close:?} took {a:?} then {b:?} (least of {RUNS} runs each)" ); } diff --git a/crates/clazel-dom/Cargo.toml b/crates/clazel-dom/Cargo.toml index 8acf9f2a3..6ff767c3e 100644 --- a/crates/clazel-dom/Cargo.toml +++ b/crates/clazel-dom/Cargo.toml @@ -46,6 +46,55 @@ theorems = [ "theorem_remove_keeps_order", "theorem_append_attribute_frame", "theorem_set_mode_frame", + # Phase 7 item 7.13's theorems (src/ops_lemmas.rs). + "theorem_change_attribute_wf", + "theorem_remove_attribute_wf", + "theorem_replace_attribute_wf", + "theorem_adopt_wf", + "theorem_clone_node_wf", + "theorem_insert_any_wf", + "theorem_pre_insert_wf", + "theorem_replace_wf", + "theorem_replace_all_wf", + # The children rules of steps 5 and 6 (src/validity_lemmas.rs). + "theorem_pre_insert_children_rules", + "theorem_replace_children_rules", + # Deep "clone a node" (src/deep_clone_lemmas.rs). + "theorem_deep_copy_kind_fields", + "theorem_deep_copy_children_facts", + "theorem_deep_clone_leaf", + # The size of a deep clone and when it may be refused + # (src/deep_clone_size_lemmas.rs). + "theorem_deep_clone_size", + "theorem_deep_clone_grew", + "theorem_clone_deep_refuses_exactly_when_full", + "theorem_clone_deep_refused_only_if_full", + "theorem_clone_deep_grew_means", + "theorem_ranked_at_facts", + # Every operation keeps `host_acyclic` (src/host_acyclic_lemmas.rs). + "theorem_new_document_host_acyclic", + "theorem_create_node_host_acyclic", + "theorem_create_template_contents_host_acyclic", + "theorem_append_data_host_acyclic", + "theorem_append_attribute_host_acyclic", + "theorem_set_mode_host_acyclic", + "theorem_change_attribute_host_acyclic", + "theorem_remove_attribute_host_acyclic", + "theorem_replace_attribute_host_acyclic", + "theorem_remove_host_acyclic", + "theorem_adopt_host_acyclic", + "theorem_insert_any_host_acyclic", + "theorem_insert_host_acyclic", + "theorem_append_host_acyclic", + "theorem_insert_before_host_acyclic", + "theorem_pre_insert_host_acyclic", + "theorem_replace_host_acyclic", + "theorem_replace_all_host_acyclic", + "theorem_reparent_children_host_acyclic", + "theorem_clone_node_host_acyclic", + "theorem_deep_clone_src_host_acyclic", + "theorem_deep_clone_host_acyclic", + "theorem_clone_deep_host_acyclic", ] [package.metadata.verus] diff --git a/crates/clazel-dom/README.md b/crates/clazel-dom/README.md index 6ef3996ca..67deb4b8d 100644 --- a/crates/clazel-dom/README.md +++ b/crates/clazel-dom/README.md @@ -21,6 +21,34 @@ carry machine-checked proofs. "replace data" at the end of a `Text`, §4.9 "append an attribute", and setting the document's mode. Readers: parent, children, previous sibling, last child, "has a child", inclusive ancestor, kind. +- **Operations scripts call (Phase 7 item 7.13)**, specified and proven + in the same way: DOM §4.2.3† "ensure pre-insertion validity" + (`ensure_pre_insertion_validity`, returning the `HierarchyRequestError` + or `NotFoundError` the standard throws), "pre-insert" (`pre_insert`, with + a node or a `DocumentFragment`, before a child or at the end), "replace" + (`replace`), "replace all" (`replace_all`), shallow "clone a node" + (`clone_node`), "adopt" (`adopt`), and §4.9† "change", "remove" and + "replace an attribute" (`change_attribute`, `remove_attribute`, + `replace_attribute`). The named hooks "insertion steps" and "removing + steps" are called from "insert" and "remove" and change nothing here. + Section and step numbers marked † were written from memory, not read at + the pinned DOM commit. Deep "clone a node" (`clone_deep`, the clone + children flag) is specified as a relation (the copy is an isomorphic + fresh subtree of the host-including subtree, template contents included) + and implemented as a worklist, not a recursion. A clone that does not fit + the node limit leaves the arena as it was, and is refused only if it does + not fit: a successful clone adds exactly `subtree_size` nodes (a count of + the host-including subtree, proven), and a refusal needs the arena to be + unable to hold that many more. It requires a host-acyclic arena + (`host_acyclic`): `wf` forbids a cycle of parents but not one through a + template's host, whose unfolding is infinite. Every operation keeps + `host_acyclic` (`src/host_acyclic_lemmas.rs`), so it is an invariant of + the tree and a caller that starts from `new_document` has it for free; + `wf` itself is not strengthened: the tree builder's raw inserts would + then each need the host half of the check proven. Document-children + rules (step 6 of validity) and the Text/doctype rule (step 5) are covered + by `tests/operations.rs` only: no theorem says them, so no mutation of + them is certified. - **A node limit**, 4,000,000 nodes: creation is refused beyond it, and the tree builder treats the refusal as "stop parsing". This is the one deliberate deviation from the standards, which have no limit. @@ -59,8 +87,22 @@ not the tier. The conformance evidence is the crate's own contents, "append an attribute" and setting the mode leave alone, and that "remove" keeps the siblings' order, are theorems of their own (`theorem_*_frame`, `theorem_remove_keeps_order`), so that proofs needing - only well-formedness do not carry them. The seventeen theorems are listed - in `Cargo.toml`. + only well-formedness do not carry them. The twenty-six theorems are listed + in `Cargo.toml` (nine of them, in `src/ops_lemmas.rs`, are Phase 7 + item 7.13's). +- **Proven (`src/host_acyclic_lemmas.rs`):** every operation keeps + `host_acyclic`, the one thing `wf` does not say (no cycle through a + template's host): creating a node or template contents, attribute and + mode changes, `remove`, `adopt`, pre-insert, replace, replace all, shallow + and deep clone. Pre-insert, replace and replace all need nothing but + `host_acyclic` of their input: DOM's "host-including inclusive ancestor" + check (step 2 of validity) is what refuses a cycle. The raw `insert`, + `append`, `insert_before` and `reparent_children`, which the parser calls + after its own checks, carry only the inclusive-ancestor half of that + check, which is not enough (a template inserted below a node of its own + contents passes it), so they additionally need `parent` not to be below + `node` through hosts (`host_acyclic_contract::insert_acyclic_requires`, + `host_acyclic_contract::reparent_children_acyclic_requires`). - **Proven (`src/exec.rs`):** the executable arena equals the spec, for every operation and reader (the contracts are `src/contract.rs`). It is safe Rust with no recursion; a node's children are edited in place, and @@ -70,7 +112,11 @@ not the tier. The conformance evidence is the crate's own `independent_equivalence.rs` proves them equal to the spec on the parser's calls. DOM §4.2.3 has no second source, so this is a same-text transcription: it catches slips, not a shared misreading. -- **Mutations:** fourteen deliberately broken specs (a parent pointer not +- **Mutations:** twenty-six deliberately broken specs (twelve of them for + item 7.13's operations, `src/ops_mutation_lemmas.rs`: validity steps 1 to + 4 skipped, replace not removing, replace all keeping the old children, a + fragment's first child dropped, a clone without attributes or with the + wrong count, and the three attribute changes), the first fourteen being a parent pointer not set, a child inserted at the wrong index, `remove` leaving the child listed or rotating the siblings, `reparent_children` reversing the order, the limit off by one, `set_mode` swapping the URLs, `append_attribute` @@ -118,7 +164,7 @@ not the tier. The conformance evidence is the crate's own cast inside `tendril` (the oracle's string type), not in this crate. - **Not proven:** that the spec transcribes the DOM Standard correctly beyond the above; the two suites and the independent transcription are - the evidence. `tests/arena.rs` holds ordinary unit tests. One restriction + the evidence. `tests/arena.rs` and `tests/operations.rs` hold ordinary unit tests. One restriction the tests respect and the spec leaves to the parser: inserting a template into its own contents is not excluded by `insert_requires` (DOM's host-including ancestor check), so the differential never makes that call. diff --git a/crates/clazel-dom/mutations.json b/crates/clazel-dom/mutations.json index f384d97b5..2655bb9b5 100644 --- a/crates/clazel-dom/mutations.json +++ b/crates/clazel-dom/mutations.json @@ -194,7 +194,959 @@ "lemma": "ml_fragment_takes_children()", "witness": "append(ml_doc_template(), 4, 1).nodes[4].children.len() == 1 && append(ml_doc_template(), 4, 1).nodes[4].kind is DocumentFragment" }, - "module": "lemmas" + "module": "mutation_lemmas" + }, + { + "id": "validity-skips-host-check", + "file": "src/spec.rs", + "find": " else if is_host_including_inclusive_ancestor(doc, node, parent) {\n Some(DomError::HierarchyRequest)\n }\n // 3.\n else if child is Some &&", + "replace": " else if false {\n Some(DomError::HierarchyRequest)\n }\n // 3.\n else if child is Some &&", + "expect": "killed", + "reason": "\"Ensure pre-insertion validity\" step 2 never fires, so a node can be inserted into its own descendant: lemma_pre_insert_valid (no error implies the node is not an inclusive ancestor of the parent) is false, and so is theorem_pre_insert_wf's well-formedness.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_validity_ancestor()", + "witness": "pre_insert_error(ol_base(), 1, 2, None) is None" + }, + "module": "ops_lemmas" + }, + { + "id": "validity-skips-not-found", + "file": "src/spec.rs", + "find": " else if child is Some && doc.nodes[child->Some_0 as int].parent != Some(parent) {", + "replace": " else if false {", + "expect": "killed", + "reason": "Step 3 never fires, so a reference child that is not a child of the parent is accepted: lemma_pre_insert_valid (no error implies the reference child is the parent's child) is false.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_validity_not_found()", + "witness": "pre_insert_error(ol_base(), 5, 2, Some(1)) is None" + }, + "module": "ops_lemmas" + }, + { + "id": "validity-allows-leaf-parent", + "file": "src/spec.rs", + "find": " if !may_have_children(doc.nodes[parent as int].kind) {\n Some(DomError::HierarchyRequest)\n }\n // 2.\n else if is_host_including_inclusive_ancestor(doc, node, parent) {\n Some(DomError::HierarchyRequest)\n }\n // 3.\n else if child is Some &&", + "replace": " if false {\n Some(DomError::HierarchyRequest)\n }\n // 2.\n else if is_host_including_inclusive_ancestor(doc, node, parent) {\n Some(DomError::HierarchyRequest)\n }\n // 3.\n else if child is Some &&", + "expect": "killed", + "reason": "Step 1 never fires, so a node whose kind has no children (a Text node) can be a parent: lemma_pre_insert_valid (no error implies the parent may have children) is false, and with it wf's rule that only documents, elements and fragments have children.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_validity_leaf_parent()", + "witness": "pre_insert_error(ol_base(), 5, 3, None) is None" + }, + "module": "ops_lemmas" + }, + { + "id": "validity-allows-document-node", + "file": "src/spec.rs", + "find": " // 4. (Every kind but a Document is one of the listed.)\n else if doc.nodes[node as int].kind is Document {", + "replace": " // 4. (Every kind but a Document is one of the listed.)\n else if false {", + "expect": "killed", + "reason": "Step 4 never fires, so the document node can be inserted: lemma_pre_insert_valid (no error implies the node is not a document) is false, and wf says node 0 is the only document and has no parent.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_validity_document_node()", + "witness": "pre_insert_error(ol_base(), 0, 5, None) is None" + }, + "module": "ops_lemmas" + }, + { + "id": "replace-skips-remove", + "file": "src/spec.rs", + "find": " remove(doc, child)\n } else {\n doc\n };", + "replace": " doc\n } else {\n doc\n };", + "expect": "killed", + "reason": "Step 11 does not remove child, so the replaced node stays a child of the parent: theorem_replace_wf (child has no parent afterwards) and lemma_replace_valid are false.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_replace_detaches()", + "witness": "replace(ol_base(), 3, 4, 2).nodes[3].parent is Some" + }, + "module": "ops_lemmas" + }, + { + "id": "replace-all-keeps-children", + "file": "src/spec.rs", + "find": " Some(n) => insert_any(d1, n, parent, None),", + "replace": " Some(n) => insert_any(doc, n, parent, None),", + "expect": "killed", + "reason": "Step 6 inserts into the arena from before step 5, so the old children stay: theorem_replace_all_wf (the parent's children are exactly the node) is false.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_replace_all_clears()", + "witness": "children(replace_all(ol_base(), Some(4), 2), 2).len() == 2" + }, + "module": "ops_lemmas" + }, + { + "id": "fragment-drops-first-node", + "file": "src/spec.rs", + "find": " insert_list(d1, nodes, parent, child)", + "replace": " insert_list(d1, nodes.drop_first(), parent, child)", + "expect": "killed", + "reason": "Inserting a fragment skips its first child: theorem_insert_any_wf (the parent's children are the old ones with the fragment's children, in order, at the reference child) is false.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_fragment_order()", + "witness": "pre_insert(ol_base(), 6, 2, None).nodes[2].children.len() == 2" + }, + "module": "ops_lemmas" + }, + { + "id": "clone-drops-attributes", + "file": "src/spec.rs", + "find": " attributes,\n template_contents: None,\n form_owner: None,\n },\n NodeKind::DocumentFragment { .. } => NodeKind::DocumentFragment { host: None },", + "replace": " attributes: seq![],\n template_contents: None,\n form_owner: None,\n },\n NodeKind::DocumentFragment { .. } => NodeKind::DocumentFragment { host: None },", + "expect": "killed", + "reason": "The copy of an element has no attributes: theorem_clone_node_wf (the copy's attribute list is the element's) is false.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_clone_attributes()", + "witness": "clone_node(ol_attrs(), 1).0.nodes[2].kind->Element_attributes.len() == 0" + }, + "module": "ops_lemmas" + }, + { + "id": "change-attribute-keeps-value", + "file": "src/spec.rs", + "find": "attrs.update(i as int, Attr { value, ..attrs[i as int] })", + "replace": "attrs.update(i as int, attrs[i as int])", + "expect": "killed", + "reason": "\"Change an attribute\" does not set the value: theorem_change_attribute_wf (the attribute is the old one with the new value) is false.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_change_value()", + "witness": "change_attribute(ol_attrs(), 1, 0, seq![9u32]).nodes[1].kind->Element_attributes[0].value[0] == 1u32" + }, + "module": "ops_lemmas" + }, + { + "id": "remove-attribute-wrong-index", + "file": "src/spec.rs", + "find": "doc.nodes[e as int].kind->Element_attributes.remove(i as int)", + "replace": "doc.nodes[e as int].kind->Element_attributes.remove(0)", + "expect": "killed", + "reason": "\"Remove an attribute\" removes the first attribute whichever is named: theorem_remove_attribute_wf (the list is the old one without the i-th) is false.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_remove_index()", + "witness": "remove_attribute(ol_attrs(), 1, 1).nodes[1].kind->Element_attributes[0].local_name[0] == 2u32" + }, + "module": "ops_lemmas" + }, + { + "id": "replace-attribute-appends", + "file": "src/spec.rs", + "find": "doc.nodes[e as int].kind->Element_attributes.update(i as int, new_attr)", + "replace": "doc.nodes[e as int].kind->Element_attributes.push(new_attr)", + "expect": "killed", + "reason": "\"Replace an attribute\" appends the new one and keeps the old: theorem_replace_attribute_wf (the list keeps its length, with the new attribute at the old one's place) is false.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_replace_attribute_length()", + "witness": "replace_attribute(ol_attrs(), 1, 0, ol_attr(3)).nodes[1].kind->Element_attributes.len() == 3" + }, + "module": "ops_lemmas" + }, + { + "id": "template-clone-count-one", + "file": "src/spec.rs", + "find": " if is_template(doc.nodes[node as int].kind) {\n 2\n } else {\n 1\n }", + "replace": " if is_template(doc.nodes[node as int].kind) {\n 1\n } else {\n 1\n }", + "expect": "killed", + "reason": "A template's clone is counted as one new node, though it adds two (the copy and its contents): theorem_clone_node_wf (the arena grows by clone_count) is false.", + "certificate": { + "file": "src/ops_mutation_lemmas.rs", + "lemma": "ol_clone_count()", + "witness": "clone_count(ol_template(), 1) == 1" + }, + "module": "ops_lemmas" + }, + { + "id": "pi-allows-text-in-document", + "file": "src/spec.rs", + "find": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && pre_insert_document_violation", + "replace": " else if (false) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && pre_insert_document_violation", + "expect": "killed", + "reason": "Step 5 no longer refuses a `Text` node as a document's child: theorem_pre_insert_children_rules (a call without an error leaves the rules true) is false, for a text inserted into the empty document.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_text_in_document()", + "witness": "pre_insert_error(vm_pre(), 1, 0, None) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-allows-doctype-in-element", + "file": "src/spec.rs", + "find": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && pre_insert_document_violation", + "replace": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (false) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && pre_insert_document_violation", + "expect": "killed", + "reason": "Step 5 no longer refuses a doctype under an element: theorem_pre_insert_children_rules is false, for a doctype inserted into an element.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_doctype_in_element()", + "witness": "pre_insert_error(vm_pre(), 5, 15, None) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-fragment-skips-two-elements", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype\n || after > 0))", + "replace": "false || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype\n || after > 0))", + "expect": "killed", + "reason": "A fragment of two elements is no longer refused as a document's child: theorem_pre_insert_children_rules is false (the document would get two elements).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_fragment_two_elements()", + "witness": "pre_insert_error(vm_pre(), 8, 0, None) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-fragment-skips-text", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype\n || after > 0))", + "replace": "elements > 1 || false || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype\n || after > 0))", + "expect": "killed", + "reason": "A fragment with a `Text` child is no longer refused as a document's child: theorem_pre_insert_children_rules is false (the document would get a text).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_fragment_text()", + "witness": "pre_insert_error(vm_pre(), 11, 0, None) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-fragment-skips-parent-element", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype\n || after > 0))", + "replace": "elements > 1 || texts > 0 || (elements == 1 && (false || child_is_doctype\n || after > 0))", + "expect": "killed", + "reason": "A fragment of one element is no longer refused when the document has an element: theorem_pre_insert_children_rules is false (two elements).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_fragment_element_parent_element()", + "witness": "pre_insert_error(vm_s1(), 13, 0, None) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-fragment-skips-child-doctype", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype\n || after > 0))", + "replace": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || false\n || after > 0))", + "expect": "killed", + "reason": "A fragment of one element is no longer refused before a doctype: theorem_pre_insert_children_rules is false (the element would precede the doctype).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_fragment_element_before_doctype()", + "witness": "pre_insert_error(vm_s2(), 13, 0, Some(4usize)) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-fragment-skips-doctype-after", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype\n || after > 0))", + "replace": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype\n || false))", + "expect": "killed", + "reason": "A fragment of one element is no longer refused when a doctype follows the child: theorem_pre_insert_children_rules is false (the element would precede the doctype).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_fragment_element_doctype_after()", + "witness": "pre_insert_error(vm_s3(), 13, 0, Some(6usize)) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-element-skips-parent-element", + "file": "src/spec.rs", + "find": "NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype || after > 0,", + "replace": "NodeKind::Element { .. } => false || child_is_doctype || after > 0,", + "expect": "killed", + "reason": "An element is no longer refused when the document has an element: theorem_pre_insert_children_rules is false (two elements).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_element_parent_element()", + "witness": "pre_insert_error(vm_s1(), 3, 0, None) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-element-skips-child-doctype", + "file": "src/spec.rs", + "find": "NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype || after > 0,", + "replace": "NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, None) > 0 || false || after > 0,", + "expect": "killed", + "reason": "An element is no longer refused before a doctype: theorem_pre_insert_children_rules is false (the element would precede the doctype).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_element_before_doctype()", + "witness": "pre_insert_error(vm_s2(), 3, 0, Some(4usize)) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-element-skips-doctype-after", + "file": "src/spec.rs", + "find": "NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype || after > 0,", + "replace": "NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype || false,", + "expect": "killed", + "reason": "An element is no longer refused when a doctype follows the child: theorem_pre_insert_children_rules is false (the element would precede the doctype).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_element_doctype_after()", + "witness": "pre_insert_error(vm_s3(), 3, 0, Some(6usize)) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-doctype-skips-parent-doctype", + "file": "src/spec.rs", + "find": "NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, None) > 0 || before > 0 || (child is None\n && count_tag(doc, kids, NodeTag::Element, None) > 0),", + "replace": "NodeKind::DocumentType { .. } => false || before > 0 || (child is None\n && count_tag(doc, kids, NodeTag::Element, None) > 0),", + "expect": "killed", + "reason": "A doctype is no longer refused when the document has one: theorem_pre_insert_children_rules is false (two doctypes).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_doctype_parent_doctype()", + "witness": "pre_insert_error(vm_s2(), 5, 0, None) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-doctype-skips-element-preceding", + "file": "src/spec.rs", + "find": "NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, None) > 0 || before > 0 || (child is None\n && count_tag(doc, kids, NodeTag::Element, None) > 0),", + "replace": "NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, None) > 0 || false || (child is None\n && count_tag(doc, kids, NodeTag::Element, None) > 0),", + "expect": "killed", + "reason": "A doctype is no longer refused after the element: theorem_pre_insert_children_rules is false (the doctype would follow the element).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_doctype_element_preceding()", + "witness": "pre_insert_error(vm_s4(), 5, 0, Some(6usize)) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-doctype-skips-append-after-element", + "file": "src/spec.rs", + "find": "NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, None) > 0 || before > 0 || (child is None\n && count_tag(doc, kids, NodeTag::Element, None) > 0),", + "replace": "NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, None) > 0 || before > 0 || (child is None\n && false),", + "expect": "killed", + "reason": "A doctype appended after the document's element is no longer refused: theorem_pre_insert_children_rules is false (the doctype would follow the element).", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_doctype_after_element()", + "witness": "pre_insert_error(vm_s1(), 5, 0, None) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-fragment-refuses-one-element", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype\n || after > 0))", + "replace": "elements > 0 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype\n || after > 0))", + "expect": "killed", + "reason": "A fragment of one element is refused as a child of the empty document: theorem_pre_insert_children_rules (a call that would leave the rules true has no error) is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_fragment_one_element()", + "witness": "pre_insert_error(vm_pre(), 13, 0, None) is Some" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-refuses-text-in-element", + "file": "src/spec.rs", + "find": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && pre_insert_document_violation", + "replace": " else if (doc.nodes[node as int].kind is Text) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && pre_insert_document_violation", + "expect": "killed", + "reason": "A `Text` node is refused as an element's child: theorem_pre_insert_children_rules (a call that would leave the rules true has no error) is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_text_in_element()", + "witness": "pre_insert_error(vm_pre(), 1, 15, None) is Some" + }, + "module": "validity_lemmas" + }, + { + "id": "pi-refuses-doctype-in-document", + "file": "src/spec.rs", + "find": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && pre_insert_document_violation", + "replace": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && pre_insert_document_violation", + "expect": "killed", + "reason": "A doctype is refused as the child of the empty document: theorem_pre_insert_children_rules (a call that would leave the rules true has no error) is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_pi_doctype_in_document()", + "witness": "pre_insert_error(vm_pre(), 4, 0, None) is Some" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-allows-text-in-document", + "file": "src/spec.rs", + "find": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && replace_document_violation", + "replace": " else if (false) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && replace_document_violation", + "expect": "killed", + "reason": "Replace no longer refuses a `Text` node as a document's child: theorem_replace_children_rules (a call without an error leaves the rules true) is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_text_in_document()", + "witness": "replace_error(vm_s1(), 2, 1, 0) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-allows-doctype-in-element", + "file": "src/spec.rs", + "find": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && replace_document_violation", + "replace": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (false) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && replace_document_violation", + "expect": "killed", + "reason": "Replace no longer refuses a doctype under an element: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_doctype_in_element()", + "witness": "replace_error(vm_s9(), 7, 5, 15) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-fragment-skips-two-elements", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0))", + "replace": "false || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0))", + "expect": "killed", + "reason": "Replace no longer refuses a fragment of two elements in a document: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_fragment_two_elements()", + "witness": "replace_error(vm_s6(), 6, 8, 0) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-fragment-skips-text", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0))", + "replace": "elements > 1 || false || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0))", + "expect": "killed", + "reason": "Replace no longer refuses a fragment with a `Text` child in a document: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_fragment_text()", + "witness": "replace_error(vm_s6(), 6, 11, 0) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-fragment-skips-parent-element", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0))", + "replace": "elements > 1 || texts > 0 || (elements == 1 && (false || after > 0))", + "expect": "killed", + "reason": "Replace no longer refuses a fragment of one element when the document has another element: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_fragment_element_parent_element()", + "witness": "replace_error(vm_s4(), 6, 13, 0) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-fragment-skips-doctype-after", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0))", + "replace": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || false))", + "expect": "killed", + "reason": "Replace no longer refuses a fragment of one element when a doctype follows the child: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_fragment_element_doctype_after()", + "witness": "replace_error(vm_s3(), 6, 13, 0) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-element-skips-parent-element", + "file": "src/spec.rs", + "find": "NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0,", + "replace": "NodeKind::Element { .. } => false || after > 0,", + "expect": "killed", + "reason": "Replace no longer refuses an element when the document has another element: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_element_parent_element()", + "witness": "replace_error(vm_s4(), 6, 3, 0) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-element-skips-doctype-after", + "file": "src/spec.rs", + "find": "NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0,", + "replace": "NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || false,", + "expect": "killed", + "reason": "Replace no longer refuses an element when a doctype follows the child: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_element_doctype_after()", + "witness": "replace_error(vm_s3(), 6, 3, 0) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-doctype-skips-parent-doctype", + "file": "src/spec.rs", + "find": "NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, Some(child)) > 0 || before > 0,", + "replace": "NodeKind::DocumentType { .. } => false || before > 0,", + "expect": "killed", + "reason": "Replace no longer refuses a doctype when the document has another doctype: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_doctype_parent_doctype()", + "witness": "replace_error(vm_s8(), 6, 5, 0) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-doctype-skips-element-preceding", + "file": "src/spec.rs", + "find": "NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, Some(child)) > 0 || before > 0,", + "replace": "NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, Some(child)) > 0 || false,", + "expect": "killed", + "reason": "Replace no longer refuses a doctype after the document's element: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_doctype_element_preceding()", + "witness": "replace_error(vm_s4(), 6, 5, 0) is None" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-element-counts-child", + "file": "src/spec.rs", + "find": "NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0,", + "replace": "NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, None) > 0 || after > 0,", + "expect": "killed", + "reason": "Replace counts the replaced child against the new element: theorem_replace_children_rules (a call that would leave the rules true has no error) is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_element_replaces_element()", + "witness": "replace_error(vm_s1(), 2, 3, 0) is Some" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-doctype-counts-child", + "file": "src/spec.rs", + "find": "NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, Some(child)) > 0 || before > 0,", + "replace": "NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, None) > 0 || before > 0,", + "expect": "killed", + "reason": "Replace counts the replaced child against the new doctype: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_doctype_replaces_doctype()", + "witness": "replace_error(vm_s2(), 4, 5, 0) is Some" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-fragment-counts-child", + "file": "src/spec.rs", + "find": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0))", + "replace": "elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || after > 0))", + "expect": "killed", + "reason": "Replace counts the replaced child against a fragment's element: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_fragment_replaces_element()", + "witness": "replace_error(vm_s1(), 2, 13, 0) is Some" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-refuses-text-in-element", + "file": "src/spec.rs", + "find": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && replace_document_violation", + "replace": " else if (doc.nodes[node as int].kind is Text) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && replace_document_violation", + "expect": "killed", + "reason": "Replace refuses a `Text` node as an element's child: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_text_in_element()", + "witness": "replace_error(vm_s9(), 7, 1, 15) is Some" + }, + "module": "validity_lemmas" + }, + { + "id": "rp-refuses-doctype-in-document", + "file": "src/spec.rs", + "find": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType\n && !(doc.nodes[parent as int].kind is Document)) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && replace_document_violation", + "replace": " else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType) {\n Some(DomError::HierarchyRequest)\n }\n // 6.\n else if doc.nodes[parent as int].kind is Document && replace_document_violation", + "expect": "killed", + "reason": "Replace refuses a doctype as a document's child: theorem_replace_children_rules is false.", + "certificate": { + "file": "src/validity_mutation_lemmas.rs", + "lemma": "vm_rp_doctype_in_document()", + "witness": "replace_error(vm_s6(), 6, 4, 0) is Some" + }, + "module": "validity_lemmas" + }, + { + "id": "deep-copy-keeps-form-owner", + "file": "src/spec.rs", + "find": "&&& n.kind->Element_form_owner is None", + "replace": "&&& true", + "expect": "killed", + "reason": "A deep clone copies the form owner: theorem_deep_copy_kind_fields (the copy has none) is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_form_owner()", + "witness": "deep_copy_kind(dm_doc(), dm_out(dm_doc(), dm_el(seq![], seq![], Some(0), None)), seq![1usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "deep-copy-attributes-free", + "file": "src/spec.rs", + "find": "&&& n.kind->Element_attributes == attributes", + "replace": "&&& true", + "expect": "killed", + "reason": "A copy may have other attributes than its source: theorem_deep_copy_kind_fields is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_attributes()", + "witness": "deep_copy_kind(dm_doc(), dm_out(dm_doc(), dm_el(seq![], seq![dm_attr()], None, None)), seq![1usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "deep-copy-local-name-free", + "file": "src/spec.rs", + "find": "&&& n.kind->Element_local_name == local_name", + "replace": "&&& true", + "expect": "killed", + "reason": "A copy may have another local name than its source: theorem_deep_copy_kind_fields is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_local_name()", + "witness": "deep_copy_kind(dm_doc(), dm_out(dm_doc(), dm_el(seq![1u32], seq![], None, None)), seq![1usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "deep-copy-namespace-free", + "file": "src/spec.rs", + "find": "&&& n.kind->Element_namespace == namespace", + "replace": "&&& true", + "expect": "killed", + "reason": "A copy may be in another namespace than its source: theorem_deep_copy_kind_fields is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_namespace()", + "witness": "deep_copy_kind(dm_doc(), dm_out(dm_doc(), NodeKind::Element { namespace: Some(Namespace::Svg), prefix: None, local_name: seq![], attributes: seq![], template_contents: None, form_owner: None }), seq![1usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "deep-copy-prefix-free", + "file": "src/spec.rs", + "find": "&&& n.kind->Element_prefix == prefix", + "replace": "&&& true", + "expect": "killed", + "reason": "A copy may have another prefix than its source: theorem_deep_copy_kind_fields is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_prefix()", + "witness": "deep_copy_kind(dm_doc(), dm_out(dm_doc(), NodeKind::Element { namespace: Some(Namespace::Html), prefix: Some(seq![]), local_name: seq![], attributes: seq![], template_contents: None, form_owner: None }), seq![1usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "deep-copy-other-kind-free", + "file": "src/spec.rs", + "find": "other => n.kind == other,", + "replace": "other => true,", + "expect": "killed", + "reason": "A copy of a text, comment, doctype or processing instruction may be anything: theorem_deep_copy_kind_fields is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_other_kind()", + "witness": "deep_copy_kind(dm_doc(), dm_out(dm_doc(), NodeKind::Comment { data: seq![] }), seq![2usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "deep-copy-contents-free", + "file": "src/spec.rs", + "find": "&&& (template_contents is None) == (n.kind->Element_template_contents is None)", + "replace": "&&& true", + "expect": "killed", + "reason": "A copy of a plain element may gain template contents: theorem_deep_copy_kind_fields is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_gains_contents()", + "witness": "deep_copy_kind(dm_doc(), dm_out(dm_doc(), dm_el(seq![], seq![], None, Some(0))), seq![1usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "deep-copy-contents-source-free", + "file": "src/spec.rs", + "find": "&&& src[c - b] == template_contents->Some_0", + "replace": "&&& true", + "expect": "killed", + "reason": "A template's copy may have contents that copy another node than its source's contents: theorem_deep_copy_kind_fields is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_wrong_contents()", + "witness": "deep_copy_kind(ol_template(), dm_out(dm_out(ol_template(), dm_el(seq![], seq![], None, Some(4))), NodeKind::DocumentFragment { host: Some(3) }), seq![1usize, 1usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "deep-copy-root-fragment-host", + "file": "src/spec.rs", + "find": "&&& t == 0 ==> n.kind->DocumentFragment_host is None", + "replace": "&&& true", + "expect": "killed", + "reason": "A cloned root fragment may keep a host: theorem_deep_copy_kind_fields is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_root_fragment_host()", + "witness": "deep_copy_kind(ol_template(), dm_out(ol_template(), NodeKind::DocumentFragment { host: Some(1) }), seq![2usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "deep-copy-root-parent", + "file": "src/spec.rs", + "find": "&&& t == 0 ==> n.parent is None", + "replace": "&&& true", + "expect": "killed", + "reason": "The root of a deep clone may have a parent: theorem_deep_copy_kind_fields is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_root_parent()", + "witness": "deep_copy_kind(dm_doc(), Document { nodes: dm_out(dm_doc(), dm_el(seq![], seq![], None, None)).nodes.update(3, Node { parent: Some(0usize), ..dm_out(dm_doc(), dm_el(seq![], seq![], None, None)).nodes[3] }) }, seq![1usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "deep-copy-wrapper", + "file": "src/spec.rs", + "find": "&&& n.wrapper is None", + "replace": "&&& true", + "expect": "killed", + "reason": "A copy may have a wrapper: theorem_deep_copy_kind_fields is false.", + "certificate": { + "file": "src/deep_clone_mutation_lemmas.rs", + "lemma": "dm_wrapper()", + "witness": "deep_copy_kind(dm_doc(), Document { nodes: dm_out(dm_doc(), dm_el(seq![], seq![], None, None)).nodes.update(3, Node { wrapper: Some(0nat), ..dm_out(dm_doc(), dm_el(seq![], seq![], None, None)).nodes[3] }) }, seq![1usize], 0)" + }, + "module": "deep_clone_lemmas" + }, + { + "id": "subtree-count-counts-nothing", + "file": "src/spec.rs", + "find": "(k - 1) as NodeId) {\n 1nat\n } else {\n 0nat", + "replace": "(k - 1) as NodeId) {\n 0nat\n } else {\n 0nat", + "expect": "killed", + "reason": "The subtree count counts no node, so a deep clone of a leaf would add none: theorem_deep_clone_size is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_leaf_exact()", + "witness": "subtree_size(ds_leaf(), 1) == 0" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "subtree-count-counts-every-node", + "file": "src/spec.rs", + "find": "} else {\n 0nat\n }\n }\n}\n\n/// The size of", + "replace": "} else {\n 1nat\n }\n }\n}\n\n/// The size of", + "expect": "killed", + "reason": "The subtree count counts every node of the arena, not only the subtree: theorem_deep_clone_size is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_leaf_exact()", + "witness": "subtree_size(ds_leaf(), 1) == 2" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "subtree-count-ancestors-only", + "file": "src/spec.rs", + "find": "if is_host_including_inclusive_ancestor(doc, node, (k - 1) as NodeId) {\n 1nat", + "replace": "if is_inclusive_ancestor(doc, node, (k - 1) as NodeId) {\n 1nat", + "expect": "killed", + "reason": "The subtree leaves out a template's contents (ordinary descendants only): a deep clone of a template adds its contents too, so theorem_deep_clone_size is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_tmpl_exact()", + "witness": "subtree_size(ds_tmpl(), 1) == 1" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "subtree-count-extra-node", + "file": "src/spec.rs", + "find": "subtree_count(doc, node, (k - 1) as nat) + if", + "replace": "subtree_count(doc, node, (k - 1) as nat) + 1 + if", + "expect": "killed", + "reason": "The subtree count adds one node per arena node: theorem_deep_clone_size is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_leaf_exact()", + "witness": "subtree_size(ds_leaf(), 1) == 3" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "subtree-size-misses-last-node", + "file": "src/spec.rs", + "find": " subtree_count(doc, node, doc.nodes.len())\n}", + "replace": " subtree_count(doc, node, (doc.nodes.len() - 1) as nat)\n}", + "expect": "killed", + "reason": "The subtree size does not look at the last node of the arena (off by one): theorem_deep_clone_size is false when that node is in the subtree.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_tmpl_exact()", + "witness": "subtree_size(ds_tmpl(), 1) == 1" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "deep-clone-refusal-always", + "file": "src/contract.rs", + "find": " &&& !can_create(doc, subtree_size(doc, node))", + "replace": " &&& true", + "expect": "killed", + "reason": "A deep clone may be refused although the copy fits: theorem_clone_deep_refuses_exactly_when_full is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_leaf_cannot_refuse()", + "witness": "clone_deep_refused(ds_leaf(), 1, ds_leaf())" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "deep-clone-refusal-whole-limit", + "file": "src/contract.rs", + "find": "!can_create(doc, subtree_size(doc, node))", + "replace": "!can_create(doc, max_nodes())", + "expect": "killed", + "reason": "A deep clone is refused unless the arena is empty (it needs the whole node limit, not the subtree size): theorem_clone_deep_refuses_exactly_when_full is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_leaf_cannot_refuse()", + "witness": "clone_deep_refused(ds_leaf(), 1, ds_leaf())" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "deep-clone-grew-at-most", + "file": "src/contract.rs", + "find": "out.nodes.len() == doc.nodes.len() + subtree_size(doc, node)", + "replace": "out.nodes.len() <= doc.nodes.len() + subtree_size(doc, node)", + "expect": "killed", + "reason": "A deep clone may add fewer nodes than the subtree has: theorem_clone_deep_grew_means is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_leaf_unchanged_is_not_grown()", + "witness": "clone_deep_grew(ds_leaf(), 1, ds_leaf())" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "deep-clone-grew-one-more", + "file": "src/contract.rs", + "find": "out.nodes.len() == doc.nodes.len() + subtree_size(doc, node)", + "replace": "out.nodes.len() == doc.nodes.len() + 1 + subtree_size(doc, node)", + "expect": "killed", + "reason": "A deep clone must add one node more than the subtree has: a valid clone does not meet it, so theorem_deep_clone_grew is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_leaf_grew()", + "witness": "!clone_deep_grew(ds_leaf(), 1, ds_leaf_copy())" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "ranked-parent-not-strict", + "file": "src/spec.rs", + "find": "rank[node.parent->Some_0 as int] < rank[i]", + "replace": "rank[node.parent->Some_0 as int] <= rank[i]", + "expect": "killed", + "reason": "A node may rank like its parent (a cycle of parents would be ranked): theorem_ranked_at_facts is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_rank_parent_strict()", + "witness": "ranked_at(ml_doc_child(), seq![0nat, 0nat, 0nat], 1)" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "ranked-host-not-strict", + "file": "src/spec.rs", + "find": "h < n && rank[h as int] < rank[i]", + "replace": "h < n && rank[h as int] <= rank[i]", + "expect": "killed", + "reason": "A fragment may rank like its host (a template inside its own contents would be ranked): theorem_ranked_at_facts is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_rank_host_strict()", + "witness": "ranked_at(ds_tmpl(), seq![0nat, 0nat, 0nat], 2)" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "ranked-unbounded", + "file": "src/spec.rs", + "find": " &&& rank[i] < n\n", + "replace": " &&& true\n", + "expect": "killed", + "reason": "A rank may exceed the node count: theorem_ranked_at_facts is false.", + "certificate": { + "file": "src/deep_clone_size_mutation_lemmas.rs", + "lemma": "ds_rank_bounded()", + "witness": "ranked_at(ds_leaf(), seq![5nat, 0nat], 0)" + }, + "module": "deep_clone_size_lemmas" + }, + { + "id": "validity-inclusive-ancestor-only", + "file": "src/spec.rs", + "find": " else if is_host_including_inclusive_ancestor(doc, node, parent) {\n Some(DomError::HierarchyRequest)\n }\n // 3.\n else if child is Some &&", + "replace": " else if is_inclusive_ancestor(doc, node, parent) {\n Some(DomError::HierarchyRequest)\n }\n // 3.\n else if child is Some &&", + "expect": "killed", + "reason": "Step 2 of \"ensure pre-insertion validity\" checks only inclusive ancestors, not host-including ones, so a template can be inserted into its own contents and make a cycle through the host: theorem_pre_insert_host_acyclic (pre-insert keeps host_acyclic) is false.", + "certificate": { + "file": "src/host_acyclic_mutation_lemmas.rs", + "lemma": "ml_host_cycle_refused()", + "witness": "pre_insert_error(ds_tmpl(), 1, 2, None) is None" + }, + "module": "host_acyclic_lemmas" + }, + { + "id": "host-including-ignores-hosts", + "file": "src/spec.rs", + "find": "NodeKind::DocumentFragment { host: Some(h) } => host_including_within(doc, a, h, (fuel - 1) as nat),", + "replace": "NodeKind::DocumentFragment { host: Some(h) } => false,", + "expect": "killed", + "reason": "A host-including ancestor follows parents only, so step 2 of pre-insertion validity cannot see a template's contents' host: theorem_pre_insert_host_acyclic is false (the same arena as for validity-inclusive-ancestor-only), and lemma_hi_unfold, relating it to the step up, no longer holds.", + "certificate": { + "file": "src/host_acyclic_mutation_lemmas.rs", + "lemma": "ml_host_cycle_refused()", + "witness": "pre_insert_error(ds_tmpl(), 1, 2, None) is None" + }, + "module": "deep_clone_size_lemmas" } ] } diff --git a/crates/clazel-dom/src/contract.rs b/crates/clazel-dom/src/contract.rs index cfe1605e0..a1000c2ce 100644 --- a/crates/clazel-dom/src/contract.rs +++ b/crates/clazel-dom/src/contract.rs @@ -10,6 +10,10 @@ //! Every mutation keeps [`wf`]; that is proven once about the spec //! (`lemmas`: `theorem_insert_wf`, …), so the exec functions need only be //! equal to the spec functions. +//! +//! Every mutation also keeps `host_acyclic`, the one thing [`wf`] does not +//! say (no cycle through a template's host): see `host_acyclic_contract` and +//! `host_acyclic_lemmas`. #[allow(unused_imports)] use super::spec::*; @@ -113,4 +117,141 @@ pub open spec fn set_mode_ensures(doc: Document, mode: DocumentMode, out: Docume out == set_mode(doc, mode) } +// --------------------------------------------------------------------- +// Phase 7 item 7.13 (see `spec.rs`, "the mutation algorithms scripts +// call"). Their section and step numbers carry the mark † there. + +/// Precondition of `Arena::ensure_pre_insertion_validity(node, parent, +/// child)` and `Arena::pre_insert(node, parent, child)`: ids of a +/// well-formed arena. Any node, parent and child are allowed: the +/// algorithm's job is to refuse the invalid ones. +pub open spec fn pre_insert_requires(doc: Document, node: NodeId, parent: NodeId, child: Option) -> bool { + ids_requires(doc, node, parent, child) +} + +/// Contract for `Arena::ensure_pre_insertion_validity(node, parent, +/// child) -> Option`: the error DOM §4.2.3† throws, or none. +pub open spec fn ensure_pre_insertion_validity_ensures( + doc: Document, + node: NodeId, + parent: NodeId, + child: Option, + err: Option, +) -> bool { + err == pre_insert_error(doc, node, parent, child) +} + +/// Contract for `Arena::pre_insert(node, parent, child) -> +/// Option`: the error is [`pre_insert_error`], and the arena is +/// [`pre_insert`] (unchanged if there is an error). +pub open spec fn pre_insert_ensures( + doc: Document, + node: NodeId, + parent: NodeId, + child: Option, + out: Document, + err: Option, +) -> bool { + &&& err == pre_insert_error(doc, node, parent, child) + &&& out == pre_insert(doc, node, parent, child) +} + +/// Precondition of `Arena::replace(child, node, parent)`: ids of a +/// well-formed arena. +pub open spec fn replace_requires(doc: Document, child: NodeId, node: NodeId, parent: NodeId) -> bool { + &&& ids_requires(doc, node, parent, Some(child)) +} + +/// Contract for `Arena::replace(child, node, parent) -> Option`. +pub open spec fn replace_ensures( + doc: Document, + child: NodeId, + node: NodeId, + parent: NodeId, + out: Document, + err: Option, +) -> bool { + &&& err == replace_error(doc, child, node, parent) + &&& out == replace(doc, child, node, parent) +} + +/// Contract for `Arena::replace_all(node, parent)`, under +/// [`replace_all_requires`]. +pub open spec fn replace_all_ensures(doc: Document, node: Option, parent: NodeId, out: Document) -> bool { + out == replace_all(doc, node, parent) +} + +/// Precondition of `Arena::adopt(node)`: a node of a well-formed arena. +pub open spec fn adopt_requires(doc: Document, node: NodeId) -> bool { + wf(doc) && valid(doc, node) +} + +/// Contract for `Arena::adopt(node)`. +pub open spec fn adopt_ensures(doc: Document, node: NodeId, out: Document) -> bool { + out == adopt(doc, node) +} + +/// Contract for `Arena::clone_node(node) -> Option`, under +/// [`clone_requires`]: refused (`None`, nothing changed) unless the arena +/// can hold the copy (and a template's contents), and otherwise +/// [`clone_node`]. +pub open spec fn clone_node_ensures(doc: Document, node: NodeId, out: Document, id: Option) -> bool { + if can_create(doc, clone_count(doc, node)) { + id == Some(clone_node(doc, node).1) && out == clone_node(doc, node).0 + } else { + id is None && out == doc + } +} + +/// Precondition of `Arena::clone_deep(node)`: [`clone_requires`], and an +/// arena with no cycle through template hosts ([`host_acyclic`]; [`wf`] +/// does not give it, and the unfolding of such a cycle is infinite). +pub open spec fn clone_deep_requires(doc: Document, node: NodeId) -> bool { + &&& clone_requires(doc, node) + &&& host_acyclic(doc) +} + +/// The arena `out` grew by exactly [`subtree_size`] nodes: the copy is the +/// whole host-including subtree of `node`, and nothing else. +pub open spec fn clone_deep_grew(doc: Document, node: NodeId, out: Document) -> bool { + out.nodes.len() == doc.nodes.len() + subtree_size(doc, node) +} + +/// A refusal of `Arena::clone_deep(node)`: the arena is exactly as it was, +/// and it could not have held the copy ([`subtree_size`] more nodes). +pub open spec fn clone_deep_refused(doc: Document, node: NodeId, out: Document) -> bool { + &&& out == doc + &&& !can_create(doc, subtree_size(doc, node)) +} + +/// Contract for `Arena::clone_deep(node) -> Option`, under +/// [`clone_deep_requires`]: refused (`None`) only if the copy does not fit +/// ([`clone_deep_refused`]), and then the arena is exactly as it was; +/// otherwise the arena is well formed, holds [`deep_clone`] of `node`, whose +/// copy is the id returned, and grew by [`subtree_size`] nodes. +pub open spec fn clone_deep_ensures(doc: Document, node: NodeId, out: Document, id: Option) -> bool { + match id { + Some(copy) => wf(out) && deep_clone(doc, node, out, copy) && clone_deep_grew(doc, node, out), + None => clone_deep_refused(doc, node, out), + } +} + +/// Contract for `Arena::change_attribute(e, i, value)`, under +/// [`attribute_requires`]. +pub open spec fn change_attribute_ensures(doc: Document, e: NodeId, i: nat, value: Seq, out: Document) -> bool { + out == change_attribute(doc, e, i, value) +} + +/// Contract for `Arena::remove_attribute(e, i)`, under +/// [`attribute_requires`]. +pub open spec fn remove_attribute_ensures(doc: Document, e: NodeId, i: nat, out: Document) -> bool { + out == remove_attribute(doc, e, i) +} + +/// Contract for `Arena::replace_attribute(e, i, new_attr)`, under +/// [`attribute_requires`]. +pub open spec fn replace_attribute_ensures(doc: Document, e: NodeId, i: nat, new_attr: Attr, out: Document) -> bool { + out == replace_attribute(doc, e, i, new_attr) +} + } // verus! diff --git a/crates/clazel-dom/src/deep_clone_lemmas.rs b/crates/clazel-dom/src/deep_clone_lemmas.rs new file mode 100644 index 000000000..4b57d6762 --- /dev/null +++ b/crates/clazel-dom/src/deep_clone_lemmas.rs @@ -0,0 +1,693 @@ +//! The proof that deep "clone a node" (`exec::Arena::clone_deep`, a +//! worklist over the source subtree) meets [`clone_deep_ensures`]. The loop +//! keeps [`deep_inv`]: a well-formed partial copy whose nodes `base ..` are +//! the copies of the nodes listed so far in `order` (breadth first), the +//! ones before the cursor `i` complete, the one at it holding the first `j` +//! of its children, the rest childless. Each lemma here is one step of it. +//! +//! `theorem_deep_clone_leaf` ties the relational spec to the shallow +//! [`clone_node`], the one that was proven first. + +#[cfg(verus_only)] +use super::contract::*; +#[cfg(verus_only)] +use super::lemmas::*; +#[cfg(verus_only)] +use super::ops_lemmas::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// --------------------------------------------------------------------- +// The worklist + +/// The worklist after cloning `x` (a source node) off it: `x`, and, if it +/// is a template, its contents, whose copy `clone_node` made as well. +pub open spec fn order_after(d0: Document, order: Seq, x: NodeId) -> Seq { + if is_template(d0.nodes[x as int].kind) { + order.push(x).push(d0.nodes[x as int].kind->Element_template_contents->Some_0) + } else { + order.push(x) + } +} + +/// The loop invariant of `clone_deep`: `a` is the partial copy of `d0`'s +/// subtree at `node`, `order` the source of each new node, and the cursor is +/// at child `j` of the source of new node `i`. +#[verifier::opaque] +pub open spec fn deep_inv(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int) -> bool { + let b = d0.nodes.len(); + let n = order.len(); + &&& wf(d0) + &&& wf(a) + &&& n >= 1 + &&& a.nodes.len() == b + n + &&& order[0] == node + &&& forall|k: int| 0 <= k < b ==> #[trigger] a.nodes[k] == d0.nodes[k] + &&& forall|t: int| 0 <= t < n ==> valid(d0, #[trigger] order[t]) + &&& forall|t: int| 0 <= t < n ==> #[trigger] deep_copy_kind(d0, a, order, t) + &&& 0 <= i <= n + &&& forall|t: int| 0 <= t < i ==> #[trigger] deep_copy_children(d0, a, order, t) + &&& forall|t: int| i < t < n ==> #[trigger] a.nodes[b + t].children.len() == 0 + &&& i < n ==> { + let s = d0.nodes[order[i] as int]; + &&& 0 <= j <= s.children.len() + &&& a.nodes[b + i].children.len() == j + &&& forall|q: int| 0 <= q < j ==> { + let c = #[trigger] a.nodes[b + i].children[q] as int; + &&& b <= c < b + n + &&& order[c - b] == s.children[q] + } + } +} + +/// What [`deep_inv`] gives the executable loop. +pub proof fn lemma_inv_facts(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int) + requires + deep_inv(d0, node, a, order, i, j), + ensures + wf(a), + wf(d0), + order.len() >= 1, + a.nodes.len() == d0.nodes.len() + order.len(), + 0 <= i <= order.len(), + forall|k: int| 0 <= k < d0.nodes.len() ==> #[trigger] a.nodes[k] == d0.nodes[k], + forall|t: int| 0 <= t < order.len() ==> valid(d0, #[trigger] order[t]), + i < order.len() ==> 0 <= j <= d0.nodes[order[i] as int].children.len(), +{ + reveal(deep_inv); +} + +/// A well-formed arena is within the node limit. +pub proof fn lemma_wf_bound(a: Document) + requires + wf(a), + ensures + a.nodes.len() <= max_nodes(), +{ + let _ = lemma_wf_witness(a); +} + +// --------------------------------------------------------------------- +// Facts about the shallow clone, and leaves + +/// The copy and the contents `clone_node` makes are new, empty nodes. +pub proof fn lemma_clone_records(doc: Document, node: NodeId) + requires + clone_requires(doc, node), + can_create(doc, clone_count(doc, node)), + ensures + ({ + let (d, copy) = clone_node(doc, node); + &&& d.nodes[copy as int].children.len() == 0 + &&& d.nodes[copy as int].parent is None + &&& d.nodes[copy as int].wrapper is None + &&& is_template(doc.nodes[node as int].kind) ==> { + &&& d.nodes[(copy + 1) as int].children.len() == 0 + &&& d.nodes[(copy + 1) as int].parent is None + &&& d.nodes[(copy + 1) as int].wrapper is None + } + }), +{ + theorem_clone_node_wf(doc, node); + let k = doc.nodes[node as int].kind; + let (d1, copy) = create_node(doc, clone_kind(k)); + if is_template(k) { + let (d2, f) = create_template_contents(d1, copy); + assert(f == copy + 1); + } +} + +/// A node with no children is an inclusive ancestor only of itself. +pub proof fn lemma_childless_not_ancestor(doc: Document, a: NodeId, x: NodeId) + requires + wf(doc), + valid(doc, a), + valid(doc, x), + a != x, + doc.nodes[a as int].children.len() == 0, + ensures + !is_inclusive_ancestor(doc, a, x), +{ + let paths = lemma_wf_witness(doc); + lemma_ancestor_iff_path(doc, paths, a, x); + if paths[x as int].contains(a) { + lemma_path_prefix(doc, paths, a, x); + lemma_path(doc, paths, x); + let k = choose|k: int| 0 <= k < paths[x as int].len() && paths[x as int][k] == a && paths[a as int] == paths[x as int].subrange(0, k + 1); + // `a` is not the last of x's path (that is x), so the path goes on. + assert(paths[x as int].last() == x); + assert(k < paths[x as int].len() - 1); + let z = paths[x as int][k + 1]; + assert(paths[z as int] == paths[x as int].subrange(0, k + 2)); + assert(z < doc.nodes.len()); + lemma_path(doc, paths, z); + assert(paths[z as int].len() == k + 2); + match doc.nodes[z as int].parent { + None => { + assert(paths[z as int] == seq![z]); + }, + Some(p) => { + assert(paths[z as int] == paths[p as int].push(z)); + lemma_path(doc, paths, p); + assert(paths[p as int] =~= paths[x as int].subrange(0, k + 1)); + assert(paths[p as int].last() == p); + assert(paths[a as int].last() == a); + assert(p == a); + assert(doc.nodes[a as int].children.contains(z)); + }, + } + } +} + +/// Appending a parentless node: the arena is the two records updated. +pub proof fn lemma_append_leaf(doc: Document, p: NodeId, c: NodeId) + requires + insert_requires(doc, c, p, None), + doc.nodes[c as int].parent is None, + p != c, + ensures + append(doc, p, c).nodes == doc.nodes.update(p as int, Node { children: doc.nodes[p as int].children.push(c), ..doc.nodes[p as int] }).update( + c as int, + Node { parent: Some(p), ..doc.nodes[c as int] }, + ), +{ + assert(adopt(doc, c) == doc); +} + +// --------------------------------------------------------------------- +// The theorems about the relation itself + +/// What [`deep_copy_kind`] says of a copy's own fields, stated by cases on +/// its source's kind: an element's copy has the source's name, namespace, +/// prefix and attributes and no form owner, and has template contents +/// exactly when the source has, copying the source's (HTML §4.12.3); a +/// fragment's copy is a fragment (hostless if it is the root); any other +/// node's copy has the same kind. No copy has a wrapper, and the root has +/// no parent. +pub proof fn theorem_deep_copy_kind_fields(doc: Document, out: Document, src: Seq, t: int) + requires + 0 <= t < src.len(), + valid(doc, src[t]), + out.nodes.len() == doc.nodes.len() + src.len(), + deep_copy_kind(doc, out, src, t), + ensures + ({ + let b = doc.nodes.len(); + let s = doc.nodes[src[t] as int]; + let n = out.nodes[b + t]; + &&& n.wrapper is None + &&& t == 0 ==> n.parent is None + &&& !(s.kind is Document) + &&& s.kind is Element ==> { + &&& n.kind is Element + &&& n.kind->Element_namespace == s.kind->Element_namespace + &&& n.kind->Element_prefix == s.kind->Element_prefix + &&& n.kind->Element_local_name == s.kind->Element_local_name + &&& n.kind->Element_attributes == s.kind->Element_attributes + &&& n.kind->Element_form_owner is None + &&& (s.kind->Element_template_contents is None) == (n.kind->Element_template_contents is None) + &&& s.kind->Element_template_contents is Some ==> { + let c = n.kind->Element_template_contents->Some_0 as int; + &&& b <= c < b + src.len() + &&& src[c - b] == s.kind->Element_template_contents->Some_0 + } + } + &&& s.kind is DocumentFragment ==> { + &&& n.kind is DocumentFragment + &&& t == 0 ==> n.kind->DocumentFragment_host is None + } + &&& !(s.kind is Element) && !(s.kind is DocumentFragment) ==> n.kind == s.kind + }), +{ +} + +/// The children clause says what step 6 does: a copy has as many children +/// as its source, and the `j`th is a new node copying the source's `j`th. +pub proof fn theorem_deep_copy_children_facts(doc: Document, out: Document, src: Seq, t: int, j: int) + requires + 0 <= t < src.len(), + valid(doc, src[t]), + out.nodes.len() == doc.nodes.len() + src.len(), + deep_copy_children(doc, out, src, t), + 0 <= j < doc.nodes[src[t] as int].children.len(), + ensures + ({ + let b = doc.nodes.len(); + let c = out.nodes[b + t].children[j] as int; + &&& out.nodes[b + t].children.len() == doc.nodes[src[t] as int].children.len() + &&& b <= c < b + src.len() + &&& src[c - b] == doc.nodes[src[t] as int].children[j] + }), +{ +} + +/// The relation agrees with the shallow clone on a childless, non-template +/// source: the copy (the first new node) is exactly what [`clone_node`] +/// makes, and no existing node changes. (That nothing else is made needs +/// `wf`: a new node must have a parent or a host.) +pub proof fn theorem_deep_clone_leaf(doc: Document, node: NodeId, out: Document, src: Seq) + requires + clone_requires(doc, node), + deep_clone_src(doc, node, out, src), + doc.nodes[node as int].children.len() == 0, + !is_template(doc.nodes[node as int].kind), + ensures + out.nodes[doc.nodes.len() as int] == clone_node(doc, node).0.nodes[doc.nodes.len() as int], + forall|i: int| 0 <= i < doc.nodes.len() ==> #[trigger] out.nodes[i] == doc.nodes[i], +{ + let b = doc.nodes.len(); + assert(deep_copy_node(doc, out, src, 0)); + assert(deep_copy_kind(doc, out, src, 0)); + assert(deep_copy_children(doc, out, src, 0)); + theorem_deep_copy_kind_fields(doc, out, src, 0); + let k = doc.nodes[node as int].kind; + let n = out.nodes[b as int]; + let c = clone_node(doc, node).0.nodes[b as int]; + assert(src[0] == node); + assert(n.kind == clone_kind(k)) by { + match k { + NodeKind::Element { .. } => { + assert(k->Element_template_contents is None); + assert(n.kind->Element_template_contents is None); + }, + _ => {}, + } + } + assert(n.children =~= Seq::::empty()); + assert(c.children =~= Seq::::empty()); + assert(n == c); +} + +// --------------------------------------------------------------------- +// The kinds of the new copies + +/// The copy `clone_node(a, x)` makes of a source node `x` (and a template's +/// contents) meets [`deep_copy_kind`] as the nodes `a.len() ..` of any +/// arena `e` that has the clone's kinds and wrappers and gives the copy a +/// parent unless it is the root (the first, when `order` is empty). +pub proof fn lemma_new_copy_kind(d0: Document, a: Document, x: NodeId, order: Seq, e: Document) + requires + wf(d0), + a.nodes.len() == d0.nodes.len() + order.len(), + x < d0.nodes.len(), + a.nodes[x as int] == d0.nodes[x as int], + clone_requires(a, x), + can_create(a, clone_count(a, x)), + e.nodes.len() == clone_node(a, x).0.nodes.len(), + forall|k: int| 0 <= k < e.nodes.len() ==> { + &&& (#[trigger] e.nodes[k]).kind == clone_node(a, x).0.nodes[k].kind + &&& e.nodes[k].wrapper == clone_node(a, x).0.nodes[k].wrapper + }, + order.len() == 0 ==> e.nodes[a.nodes.len() as int].parent is None, + order.len() > 0 ==> e.nodes[a.nodes.len() as int].parent is Some, + ensures + ({ + let o2 = order_after(d0, order, x); + let t = order.len() as int; + &&& deep_copy_kind(d0, e, o2, t) + &&& is_template(d0.nodes[x as int].kind) ==> deep_copy_kind(d0, e, o2, t + 1) + }), +{ + theorem_clone_node_wf(a, x); + lemma_clone_records(a, x); + let k = d0.nodes[x as int].kind; + let o2 = order_after(d0, order, x); + let t = order.len() as int; + let b = d0.nodes.len(); + assert(o2[t] == x); + if is_template(k) { + // wf(d0): the contents of a template is a fragment, hosted by it. + let f = k->Element_template_contents->Some_0; + assert(d0.nodes[x as int].kind is Element); + assert(f < b); + assert(d0.nodes[f as int].kind is DocumentFragment); + assert(o2[t + 1] == f); + assert(o2.len() == t + 2); + } else { + assert(o2.len() == t + 1); + } +} + +// --------------------------------------------------------------------- +// One step of the loop + +/// `ch`, the `j`th child of the source of node `i`, is a node that can be +/// cloned: neither the document nor a fragment. +pub proof fn lemma_child_requires(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int, ch: NodeId) + requires + deep_inv(d0, node, a, order, i, j), + i < order.len(), + j < d0.nodes[order[i] as int].children.len(), + ch == d0.nodes[order[i] as int].children[j], + ensures + clone_requires(a, ch), + a.nodes[ch as int] == d0.nodes[ch as int], + !(d0.nodes[ch as int].kind is DocumentFragment), + ch < d0.nodes.len(), +{ + reveal(deep_inv); + let b = d0.nodes.len(); + let s = order[i]; + // wf(d0): `ch` is a node whose parent is `s`; so it is neither the + // document nor a fragment. + assert(d0.nodes[ch as int].parent == Some(s)); + assert(ch < b); + assert(ch != 0); + assert(!(d0.nodes[ch as int].kind is Document)); + assert(!(d0.nodes[ch as int].kind is DocumentFragment)); + assert(a.nodes[ch as int] == d0.nodes[ch as int]); +} + +/// With room, `ch` is cloned and the copy appended to node `i`. +pub proof fn lemma_step_requires(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int, ch: NodeId) + requires + deep_inv(d0, node, a, order, i, j), + i < order.len(), + j < d0.nodes[order[i] as int].children.len(), + ch == d0.nodes[order[i] as int].children[j], + can_create(a, clone_count(a, ch)), + ensures + clone_requires(a, ch), + ({ + let (a1, cc) = clone_node(a, ch); + insert_requires(a1, cc, (d0.nodes.len() + i) as NodeId, None) + }), +{ + reveal(deep_inv); + let b = d0.nodes.len(); + let s = order[i]; + lemma_child_requires(d0, node, a, order, i, j, ch); + theorem_clone_node_wf(a, ch); + lemma_clone_records(a, ch); + let (a1, cc) = clone_node(a, ch); + let p = (b + i) as NodeId; + // The source has children, so it may have them, so does its copy. + assert(d0.nodes[s as int].children.len() > 0); + assert(may_have_children(d0.nodes[s as int].kind)); + assert(deep_copy_kind(d0, a, order, i)); + assert(a1.nodes[p as int] == a.nodes[p as int]); + assert(may_have_children(a1.nodes[p as int].kind)); + assert(cc != p); + lemma_childless_not_ancestor(a1, cc, p); +} + +/// What the step does to the arena: only node `p` (the new copy's parent) +/// and the new nodes differ, the copy is appended to `p`, and the new nodes +/// are copies by kind. +#[verifier::rlimit(100)] +pub proof fn lemma_step_frame(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int, ch: NodeId) + requires + deep_inv(d0, node, a, order, i, j), + i < order.len(), + j < d0.nodes[order[i] as int].children.len(), + ch == d0.nodes[order[i] as int].children[j], + can_create(a, clone_count(a, ch)), + ensures + ({ + let (a1, cc) = clone_node(a, ch); + let p = (d0.nodes.len() + i) as NodeId; + let a2 = append(a1, p, cc); + let n = order.len() as int; + let o2 = order_after(d0, order, ch); + &&& cc == a.nodes.len() + &&& wf(a2) + &&& a2.nodes.len() == a.nodes.len() + clone_count(a, ch) + &&& forall|k: int| 0 <= k < a.nodes.len() && k != p ==> #[trigger] a2.nodes[k] == a.nodes[k] + &&& a2.nodes[p as int].kind == a.nodes[p as int].kind + &&& a2.nodes[p as int].parent == a.nodes[p as int].parent + &&& a2.nodes[p as int].wrapper == a.nodes[p as int].wrapper + &&& a2.nodes[p as int].children == a.nodes[p as int].children.push(cc) + &&& a2.nodes[cc as int].children.len() == 0 + &&& is_template(d0.nodes[ch as int].kind) ==> a2.nodes[(cc + 1) as int].children.len() == 0 + &&& deep_copy_kind(d0, a2, o2, n) + &&& is_template(d0.nodes[ch as int].kind) ==> deep_copy_kind(d0, a2, o2, n + 1) + }), +{ + reveal(deep_inv); + let b = d0.nodes.len(); + let n = order.len() as int; + lemma_step_requires(d0, node, a, order, i, j, ch); + assert(a.nodes[ch as int] == d0.nodes[ch as int]); + theorem_clone_node_wf(a, ch); + lemma_clone_records(a, ch); + let (a1, cc) = clone_node(a, ch); + let p = (b + i) as NodeId; + let a2 = append(a1, p, cc); + assert(cc == a.nodes.len()); + assert(cc == b + n); + assert(a1.nodes[cc as int].parent is None); + assert(p != cc); + theorem_append_wf(a1, p, cc); + lemma_append_leaf(a1, p, cc); + lemma_new_copy_kind(d0, a, ch, order, a2); + assert(a2.nodes.len() == a1.nodes.len()); + assert forall|k: int| 0 <= k < a.nodes.len() && k != p implies #[trigger] a2.nodes[k] == a.nodes[k] by { + assert(a1.nodes[k] == a.nodes[k]); + } +} + +/// The step keeps the invariant: clone `ch`, append the copy to node `i`. +#[verifier::rlimit(100)] +pub proof fn lemma_step_inv(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int, ch: NodeId) + requires + deep_inv(d0, node, a, order, i, j), + i < order.len(), + j < d0.nodes[order[i] as int].children.len(), + ch == d0.nodes[order[i] as int].children[j], + can_create(a, clone_count(a, ch)), + ensures + ({ + let (a1, cc) = clone_node(a, ch); + let p = (d0.nodes.len() + i) as NodeId; + let a2 = append(a1, p, cc); + deep_inv(d0, node, a2, order_after(d0, order, ch), i, j + 1) + }), +{ + let b = d0.nodes.len(); + let n = order.len() as int; + reveal(deep_inv); + let s = order[i]; + lemma_step_frame(d0, node, a, order, i, j, ch); + assert(a.nodes[ch as int] == d0.nodes[ch as int]); + let (a1, cc) = clone_node(a, ch); + let p = (b + i) as NodeId; + let a2 = append(a1, p, cc); + let o2 = order_after(d0, order, ch); + let n2 = o2.len() as int; + assert(cc == b + n); + assert(n2 == n + clone_count(a, ch)); + // o2 extends order. + assert forall|x: int| 0 <= x < n implies o2[x] == order[x] by {} + assert(o2[n] == ch); + // The invariant, piece by piece. + assert(a2.nodes.len() == b + n2); + assert(o2[0] == node); + assert forall|k: int| 0 <= k < b implies #[trigger] a2.nodes[k] == d0.nodes[k] by { + assert(a2.nodes[k] == a.nodes[k]); + } + assert forall|t: int| 0 <= t < n2 implies valid(d0, #[trigger] o2[t]) by { + if t < n { + assert(o2[t] == order[t]); + } else if t == n { + assert(o2[t] == ch); + } else { + assert(is_template(d0.nodes[ch as int].kind)); + let f = d0.nodes[ch as int].kind->Element_template_contents->Some_0; + assert(o2[t] == f); + } + } + assert forall|t: int| 0 <= t < n2 implies #[trigger] deep_copy_kind(d0, a2, o2, t) by { + if t < n { + // An old node: only node p's children and cc's parent change. + assert(deep_copy_kind(d0, a, order, t)); + if (b + t) as NodeId == p { + assert(a2.nodes[p as int].kind == a.nodes[p as int].kind); + } else { + assert(a2.nodes[(b + t) as int] == a.nodes[(b + t) as int]); + } + assert(a2.nodes[(b + t) as int].kind == a.nodes[(b + t) as int].kind); + assert(a2.nodes[(b + t) as int].wrapper == a.nodes[(b + t) as int].wrapper); + assert(a2.nodes[(b + t) as int].parent == a.nodes[(b + t) as int].parent); + assert(o2[t] == order[t]); + } else if t == n { + assert(deep_copy_kind(d0, a2, o2, n)); + } else { + assert(t == n + 1); + assert(deep_copy_kind(d0, a2, o2, n + 1)); + } + } + assert forall|t: int| 0 <= t < i implies #[trigger] deep_copy_children(d0, a2, o2, t) by { + assert(deep_copy_children(d0, a, order, t)); + assert(a2.nodes[(b + t) as int] == a.nodes[(b + t) as int]); + assert(o2[t] == order[t]); + let sn = d0.nodes[order[t] as int]; + let cn = a.nodes[(b + t) as int]; + assert forall|q: int| 0 <= q < sn.children.len() implies { + let c = #[trigger] cn.children[q] as int; + &&& b <= c < b + n2 + &&& o2[c - b] == sn.children[q] + } by { + let c = cn.children[q] as int; + assert(b <= c < b + n); + assert(o2[c - b] == order[c - b]); + } + } + assert forall|t: int| i < t < n2 implies #[trigger] a2.nodes[b + t].children.len() == 0 by { + if t < n { + assert((b + t) as NodeId != p); + assert(a2.nodes[(b + t) as int] == a.nodes[(b + t) as int]); + } else if t == n { + assert(a2.nodes[cc as int].children.len() == 0); + } else { + assert(t == n + 1); + assert(a2.nodes[(cc + 1) as int].children.len() == 0); + } + } + let sn = d0.nodes[s as int]; + let kids2 = a2.nodes[p as int].children; + assert(kids2 == a.nodes[p as int].children.push(cc)); + assert forall|q: int| 0 <= q < j + 1 implies { + let c = #[trigger] kids2[q] as int; + &&& b <= c < b + n2 + &&& o2[c - b] == sn.children[q] + } by { + if q < j { + let c = a.nodes[p as int].children[q] as int; + assert(kids2[q] == a.nodes[p as int].children[q]); + assert(b <= c < b + n); + assert(o2[c - b] == order[c - b]); + } else { + assert(kids2[q] == cc); + assert(o2[cc - b] == ch); + } + } +} + +/// The cursor passes the end of node `i`'s source's children: node `i` is +/// complete. +pub proof fn lemma_advance(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int) + requires + deep_inv(d0, node, a, order, i, j), + i < order.len(), + j == d0.nodes[order[i] as int].children.len(), + ensures + deep_inv(d0, node, a, order, i + 1, 0), +{ + reveal(deep_inv); + let b = d0.nodes.len(); + let n = order.len() as int; + lemma_complete(d0, a, order, i); + assert forall|t: int| 0 <= t < i + 1 implies #[trigger] deep_copy_children(d0, a, order, t) by { + if t < i { + assert(deep_copy_children(d0, a, order, t)); + } + } + assert forall|t: int| i + 1 < t < n implies #[trigger] a.nodes[b + t].children.len() == 0 by { + assert(i < t); + } + if i + 1 < n { + let t1 = i + 1; + assert(a.nodes[b + t1].children.len() == 0); + assert(0 <= 0 <= d0.nodes[order[t1] as int].children.len()); + } +} + +/// With the cursor at the end of node `i`'s source's children, node `i` is +/// complete ([`deep_copy_children`]). +pub proof fn lemma_complete(d0: Document, a: Document, order: Seq, i: int) + requires + 0 <= i < order.len(), + a.nodes.len() == d0.nodes.len() + order.len(), + ({ + let b = d0.nodes.len(); + let s = d0.nodes[order[i] as int]; + let cn = a.nodes[b + i]; + &&& cn.children.len() == s.children.len() + &&& forall|q: int| 0 <= q < s.children.len() ==> { + let c = #[trigger] cn.children[q] as int; + &&& b <= c < b + order.len() + &&& order[c - b] == s.children[q] + } + }), + ensures + deep_copy_children(d0, a, order, i), +{ +} + +/// The loop is over: the invariant at the end of the worklist is the +/// relational spec. +pub proof fn lemma_deep_done(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int) + requires + deep_inv(d0, node, a, order, i, j), + i == order.len(), + ensures + wf(a), + deep_clone_src(d0, node, a, order), + deep_clone(d0, node, a, d0.nodes.len() as NodeId), +{ + reveal(deep_inv); + assert forall|t: int| 0 <= t < order.len() implies #[trigger] deep_copy_node(d0, a, order, t) by { + assert(deep_copy_kind(d0, a, order, t)); + assert(deep_copy_children(d0, a, order, t)); + } + assert(deep_clone_src(d0, node, a, order)); +} + +/// The first copy: the loop starts with `node`'s copy (and a template's +/// contents) and the cursor at its first child. +pub proof fn lemma_deep_init(d0: Document, node: NodeId) + requires + clone_requires(d0, node), + can_create(d0, clone_count(d0, node)), + ensures + deep_inv(d0, node, clone_node(d0, node).0, order_after(d0, Seq::empty(), node), 0, 0), +{ + reveal(deep_inv); + let b = d0.nodes.len(); + theorem_clone_node_wf(d0, node); + lemma_clone_records(d0, node); + let (a, copy) = clone_node(d0, node); + let order = order_after(d0, Seq::empty(), node); + assert(a.nodes[copy as int].parent is None); + lemma_new_copy_kind(d0, d0, node, Seq::empty(), a); + assert(copy == b); + let n = order.len() as int; + assert(order[0] == node); + assert(n == clone_count(d0, node)); + assert(a.nodes.len() == b + n); + if is_template(d0.nodes[node as int].kind) { + let f = d0.nodes[node as int].kind->Element_template_contents->Some_0; + assert(f < b); + assert(order[1] == f); + assert(a.nodes[(b + 1) as int].children.len() == 0); + } + assert forall|t: int| 0 <= t < n implies valid(d0, #[trigger] order[t]) by {} + assert forall|t: int| 0 <= t < n implies #[trigger] deep_copy_kind(d0, a, order, t) by { + if t == 1 { + assert(is_template(d0.nodes[node as int].kind)); + } + } + assert forall|t: int| 0 < t < n implies #[trigger] a.nodes[b + t].children.len() == 0 by { + assert(t == 1); + } + assert(a.nodes[b as int].children.len() == 0); + assert forall|k: int| 0 <= k < b implies #[trigger] a.nodes[k] == d0.nodes[k] by {} +} + +/// A failed clone's partial copy is the new nodes after the old ones: the +/// arena cut back to the old length is the old arena. +pub proof fn lemma_deep_rollback(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int) + requires + deep_inv(d0, node, a, order, i, j), + ensures + (Document { nodes: a.nodes.take(d0.nodes.len() as int) }) == d0, +{ + reveal(deep_inv); + assert(a.nodes.take(d0.nodes.len() as int) =~= d0.nodes); +} + +} // verus! diff --git a/crates/clazel-dom/src/deep_clone_mutation_lemmas.rs b/crates/clazel-dom/src/deep_clone_mutation_lemmas.rs new file mode 100644 index 000000000..dad50f181 --- /dev/null +++ b/crates/clazel-dom/src/deep_clone_mutation_lemmas.rs @@ -0,0 +1,233 @@ +//! Concrete instances of `theorem_deep_copy_kind_fields`, the kill +//! certificates of the mutations of `deep_copy_kind` in `mutations.json` +//! (`xtask/src/mutate.rs`). Each lemma says that one concrete, wrong copy is +//! not a copy ([`deep_copy_kind`] is false for it): true of the real spec +//! by the theorem, so a mutant that makes [`deep_copy_kind`] accept it (the +//! certificate's witness) contradicts the theorem. +//! +//! The arenas: [`dm_doc`] is a document (0), an element (1) and a text node +//! (2), and [`ol_template`] a document, a template element (1) and its +//! contents, the fragment (2). A copy is the next node, 3. + +#[cfg(verus_only)] +use super::deep_clone_lemmas::*; +#[cfg(verus_only)] +use super::mutation_lemmas::*; +#[cfg(verus_only)] +use super::ops_mutation_lemmas::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A document, an element (1) and a text node (2). +pub open spec fn dm_doc() -> Document { + create_node(create_node(new_document(seq![]), ml_element()).0, ml_text()).0 +} + +/// An element kind with the given name, attributes, form owner and +/// template contents, in the HTML namespace. +pub open spec fn dm_el(local_name: Seq, attributes: Seq, form_owner: Option, template_contents: Option) -> NodeKind { + NodeKind::Element { namespace: Some(Namespace::Html), prefix: None, local_name, attributes, template_contents, form_owner } +} + +/// `doc` with one more node, of kind `k`: the candidate copy. +pub open spec fn dm_out(doc: Document, k: NodeKind) -> Document { + create_node(doc, k).0 +} + +/// An attribute. +pub open spec fn dm_attr() -> Attr { + Attr { namespace: None, prefix: None, local_name: seq![], value: seq![] } +} + +/// The witnesses below are evaluated by `compute_only`: it does evaluate +/// [`deep_copy_kind`] (sequence equalities and all) on a correct copy. +pub proof fn dm_sanity() + ensures + deep_copy_kind(dm_doc(), dm_out(dm_doc(), dm_el(seq![], seq![], None, None)), seq![1usize], 0), +{ + assert(deep_copy_kind(dm_doc(), dm_out(dm_doc(), dm_el(seq![], seq![], None, None)), seq![1usize], 0)) by (compute_only); +} + +/// A copy with a form owner is not a copy. +pub proof fn dm_form_owner() + ensures + !deep_copy_kind(dm_doc(), dm_out(dm_doc(), dm_el(seq![], seq![], Some(0), None)), seq![1usize], 0), +{ + let out = dm_out(dm_doc(), dm_el(seq![], seq![], Some(0), None)); + if deep_copy_kind(dm_doc(), out, seq![1usize], 0) { + theorem_deep_copy_kind_fields(dm_doc(), out, seq![1usize], 0); + assert(out.nodes[3].kind->Element_form_owner is None); + assert(false); + } +} + +/// A copy with other attributes is not a copy. +pub proof fn dm_attributes() + ensures + !deep_copy_kind(dm_doc(), dm_out(dm_doc(), dm_el(seq![], seq![dm_attr()], None, None)), seq![1usize], 0), +{ + let out = dm_out(dm_doc(), dm_el(seq![], seq![dm_attr()], None, None)); + if deep_copy_kind(dm_doc(), out, seq![1usize], 0) { + theorem_deep_copy_kind_fields(dm_doc(), out, seq![1usize], 0); + assert(out.nodes[3].kind->Element_attributes.len() == 1); + assert(false); + } +} + +/// A copy with another local name is not a copy. +pub proof fn dm_local_name() + ensures + !deep_copy_kind(dm_doc(), dm_out(dm_doc(), dm_el(seq![1u32], seq![], None, None)), seq![1usize], 0), +{ + let out = dm_out(dm_doc(), dm_el(seq![1u32], seq![], None, None)); + if deep_copy_kind(dm_doc(), out, seq![1usize], 0) { + theorem_deep_copy_kind_fields(dm_doc(), out, seq![1usize], 0); + assert(out.nodes[3].kind->Element_local_name.len() == 1); + assert(false); + } +} + +/// A copy in another namespace is not a copy. +pub proof fn dm_namespace() + ensures + !deep_copy_kind( + dm_doc(), + dm_out(dm_doc(), NodeKind::Element { namespace: Some(Namespace::Svg), prefix: None, local_name: seq![], attributes: seq![], template_contents: None, form_owner: None }), + seq![1usize], + 0, + ), +{ + let out = dm_out( + dm_doc(), + NodeKind::Element { namespace: Some(Namespace::Svg), prefix: None, local_name: seq![], attributes: seq![], template_contents: None, form_owner: None }, + ); + if deep_copy_kind(dm_doc(), out, seq![1usize], 0) { + theorem_deep_copy_kind_fields(dm_doc(), out, seq![1usize], 0); + assert(out.nodes[3].kind->Element_namespace == Some(Namespace::Svg)); + assert(false); + } +} + +/// A copy with a prefix the source lacks is not a copy. +pub proof fn dm_prefix() + ensures + !deep_copy_kind( + dm_doc(), + dm_out(dm_doc(), NodeKind::Element { namespace: Some(Namespace::Html), prefix: Some(seq![]), local_name: seq![], attributes: seq![], template_contents: None, form_owner: None }), + seq![1usize], + 0, + ), +{ + let out = dm_out( + dm_doc(), + NodeKind::Element { namespace: Some(Namespace::Html), prefix: Some(seq![]), local_name: seq![], attributes: seq![], template_contents: None, form_owner: None }, + ); + if deep_copy_kind(dm_doc(), out, seq![1usize], 0) { + theorem_deep_copy_kind_fields(dm_doc(), out, seq![1usize], 0); + assert(out.nodes[3].kind->Element_prefix is Some); + assert(false); + } +} + +/// A copy of a text node that is a comment is not a copy. +pub proof fn dm_other_kind() + ensures + !deep_copy_kind(dm_doc(), dm_out(dm_doc(), NodeKind::Comment { data: seq![] }), seq![2usize], 0), +{ + let out = dm_out(dm_doc(), NodeKind::Comment { data: seq![] }); + if deep_copy_kind(dm_doc(), out, seq![2usize], 0) { + theorem_deep_copy_kind_fields(dm_doc(), out, seq![2usize], 0); + assert(out.nodes[3].kind is Comment); + assert(false); + } +} + +/// A copy of a plain element that has template contents is not a copy. +pub proof fn dm_gains_contents() + ensures + !deep_copy_kind(dm_doc(), dm_out(dm_doc(), dm_el(seq![], seq![], None, Some(0))), seq![1usize], 0), +{ + let out = dm_out(dm_doc(), dm_el(seq![], seq![], None, Some(0))); + if deep_copy_kind(dm_doc(), out, seq![1usize], 0) { + theorem_deep_copy_kind_fields(dm_doc(), out, seq![1usize], 0); + assert(out.nodes[3].kind->Element_template_contents is Some); + assert(false); + } +} + +/// A copy of a template whose contents copies the wrong node is not a copy +/// (the source of node 4, the contents, is 1, not the source's contents 2). +pub proof fn dm_wrong_contents() + ensures + !deep_copy_kind( + ol_template(), + dm_out(dm_out(ol_template(), dm_el(seq![], seq![], None, Some(4))), NodeKind::DocumentFragment { host: Some(3) }), + seq![1usize, 1usize], + 0, + ), +{ + let out = dm_out(dm_out(ol_template(), dm_el(seq![], seq![], None, Some(4))), NodeKind::DocumentFragment { host: Some(3) }); + if deep_copy_kind(ol_template(), out, seq![1usize, 1usize], 0) { + theorem_deep_copy_kind_fields(ol_template(), out, seq![1usize, 1usize], 0); + assert(ol_template().nodes[1].kind->Element_template_contents == Some(2usize)); + assert(out.nodes[3].kind->Element_template_contents == Some(4usize)); + assert(false); + } +} + +/// A copy of a fragment, the root, that has a host is not a copy. +pub proof fn dm_root_fragment_host() + ensures + !deep_copy_kind(ol_template(), dm_out(ol_template(), NodeKind::DocumentFragment { host: Some(1) }), seq![2usize], 0), +{ + let out = dm_out(ol_template(), NodeKind::DocumentFragment { host: Some(1) }); + if deep_copy_kind(ol_template(), out, seq![2usize], 0) { + theorem_deep_copy_kind_fields(ol_template(), out, seq![2usize], 0); + assert(out.nodes[3].kind->DocumentFragment_host == Some(1usize)); + assert(false); + } +} + +/// The root's copy with a parent is not a copy. +pub proof fn dm_root_parent() + ensures + !deep_copy_kind( + dm_doc(), + Document { nodes: dm_out(dm_doc(), dm_el(seq![], seq![], None, None)).nodes.update(3, Node { parent: Some(0usize), ..dm_out(dm_doc(), dm_el(seq![], seq![], None, None)).nodes[3] }) }, + seq![1usize], + 0, + ), +{ + let o = dm_out(dm_doc(), dm_el(seq![], seq![], None, None)); + let out = Document { nodes: o.nodes.update(3, Node { parent: Some(0usize), ..o.nodes[3] }) }; + if deep_copy_kind(dm_doc(), out, seq![1usize], 0) { + theorem_deep_copy_kind_fields(dm_doc(), out, seq![1usize], 0); + assert(out.nodes[3].parent is Some); + assert(false); + } +} + +/// A copy with a wrapper is not a copy. +pub proof fn dm_wrapper() + ensures + !deep_copy_kind( + dm_doc(), + Document { nodes: dm_out(dm_doc(), dm_el(seq![], seq![], None, None)).nodes.update(3, Node { wrapper: Some(0nat), ..dm_out(dm_doc(), dm_el(seq![], seq![], None, None)).nodes[3] }) }, + seq![1usize], + 0, + ), +{ + let o = dm_out(dm_doc(), dm_el(seq![], seq![], None, None)); + let out = Document { nodes: o.nodes.update(3, Node { wrapper: Some(0nat), ..o.nodes[3] }) }; + if deep_copy_kind(dm_doc(), out, seq![1usize], 0) { + theorem_deep_copy_kind_fields(dm_doc(), out, seq![1usize], 0); + assert(out.nodes[3].wrapper is Some); + assert(false); + } +} + +} // verus! diff --git a/crates/clazel-dom/src/deep_clone_size_lemmas.rs b/crates/clazel-dom/src/deep_clone_size_lemmas.rs new file mode 100644 index 000000000..f43f0cadb --- /dev/null +++ b/crates/clazel-dom/src/deep_clone_size_lemmas.rs @@ -0,0 +1,961 @@ +//! The size of a deep clone: a valid deep clone adds exactly +//! [`subtree_size`] nodes (`theorem_deep_clone_size`), and the worklist of +//! `exec::Arena::clone_deep` is never longer than [`subtree_size`], not +//! even with the next node it would clone (`lemma_step_fits`, +//! `lemma_root_fits`), so it refuses only if the copy cannot fit. +//! +//! All of it rests on a ranking of the arena that grows along every parent +//! edge and every host edge ([`host_ranked`], the precondition +//! [`host_acyclic`] of `clone_deep`). The source of every new node is a +//! host-including descendant of the cloned node, and distinct new nodes have +//! distinct sources (`lemma_image`, `lemma_inj`); with every descendant +//! copied (`lemma_surj`), the new nodes are counted by the descendants +//! (`lemma_count_eq`). +//! +//! The facts about a copy are stated for [`copy_map`], a predicate that +//! holds of every partial copy of the loop (`lemma_inv_copy_map`, from +//! [`deep_inv`]) and of every complete one (`lemma_src_copy_map`, from +//! [`deep_clone_src`] and `wf`): the new nodes are copies by kind, and the +//! children of each new node are a prefix of the copies of its source's. + +#[cfg(verus_only)] +use super::contract::*; +#[cfg(verus_only)] +use super::deep_clone_lemmas::*; +#[cfg(verus_only)] +use super::lemmas::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// --------------------------------------------------------------------- +// What `wf` says, as facts + +/// The links of a well-formed arena, as facts. +pub proof fn lemma_wf_facts(doc: Document) + requires + wf(doc), + ensures + forall|i: int, j: int| 0 <= i < doc.nodes.len() && 0 <= j < doc.nodes[i].children.len() ==> { + let c = (#[trigger] doc.nodes[i].children[j]); + &&& c < doc.nodes.len() + &&& doc.nodes[c as int].parent == Some(i as NodeId) + }, + forall|i: int| 0 <= i < doc.nodes.len() && (#[trigger] doc.nodes[i]).parent is Some ==> { + let p = doc.nodes[i].parent->Some_0; + &&& p < doc.nodes.len() + &&& doc.nodes[p as int].children.contains(i as NodeId) + }, + forall|i: int| 0 <= i < doc.nodes.len() ==> (#[trigger] doc.nodes[i]).children.no_duplicates(), + forall|i: int| 0 <= i < doc.nodes.len() && (#[trigger] doc.nodes[i]).kind is DocumentFragment ==> doc.nodes[i].parent is None, + forall|i: int| 0 <= i < doc.nodes.len() && (#[trigger] doc.nodes[i]).kind is Element && doc.nodes[i].kind->Element_template_contents is Some ==> { + let f = doc.nodes[i].kind->Element_template_contents->Some_0; + &&& f < doc.nodes.len() + &&& doc.nodes[f as int].kind is DocumentFragment + &&& doc.nodes[f as int].kind->DocumentFragment_host == Some(i as NodeId) + }, + forall|i: int| 0 <= i < doc.nodes.len() && (#[trigger] doc.nodes[i]).kind is DocumentFragment && doc.nodes[i].kind->DocumentFragment_host is Some ==> { + let h = doc.nodes[i].kind->DocumentFragment_host->Some_0; + &&& h < doc.nodes.len() + &&& doc.nodes[h as int].kind is Element + &&& doc.nodes[h as int].kind->Element_template_contents == Some(i as NodeId) + }, +{ + let paths = lemma_wf_witness(doc); + assert(wf_with(doc, paths)); +} + +// --------------------------------------------------------------------- +// The ranking + +/// A ranking of a host-acyclic arena. +pub proof fn lemma_rank_witness(doc: Document) -> (rank: Seq) + requires + host_acyclic(doc), + ensures + host_ranked(doc, rank), +{ + choose|rank: Seq| host_ranked(doc, rank) +} + +/// What a ranking says of node `i`, as facts. +pub proof fn theorem_ranked_at_facts(doc: Document, rank: Seq, i: int) + requires + 0 <= i < doc.nodes.len(), + rank.len() == doc.nodes.len(), + ranked_at(doc, rank, i), + ensures + rank[i] < doc.nodes.len(), + doc.nodes[i].parent is Some ==> { + let p = doc.nodes[i].parent->Some_0; + &&& p < doc.nodes.len() + &&& rank[p as int] < rank[i] + }, + (doc.nodes[i].kind is DocumentFragment && doc.nodes[i].kind->DocumentFragment_host is Some) ==> { + let h = doc.nodes[i].kind->DocumentFragment_host->Some_0; + &&& h < doc.nodes.len() + &&& rank[h as int] < rank[i] + }, +{ +} + +/// The step up from `x`: its parent, or, for a parentless fragment, its host. +/// This is the step of [`host_including_within`]. +pub open spec fn up(doc: Document, x: NodeId) -> Option { + match parent(doc, x) { + Some(p) => Some(p), + None => match doc.nodes[x as int].kind { + NodeKind::DocumentFragment { host: Some(h) } => Some(h), + _ => None, + }, + } +} + +/// A step up lands on a node of the arena with a lower rank. +pub proof fn lemma_up_rank(doc: Document, rank: Seq, x: NodeId) + requires + host_ranked(doc, rank), + valid(doc, x), + up(doc, x) is Some, + ensures + valid(doc, up(doc, x)->Some_0), + rank[up(doc, x)->Some_0 as int] < rank[x as int], +{ + assert(ranked_at(doc, rank, x as int)); + theorem_ranked_at_facts(doc, rank, x as int); +} + +/// Fuel only helps. (No node is read but through `up`, so no validity is +/// needed: the statement is about the function `host_including_within`.) +pub proof fn lemma_hw_mono(doc: Document, a: NodeId, x: NodeId, f: nat, g: nat) + requires + f <= g, + host_including_within(doc, a, x, f), + ensures + host_including_within(doc, a, x, g), + decreases f, +{ + if x != a { + assert(f > 0); + match up(doc, x) { + Some(y) => { + lemma_hw_mono(doc, a, y, (f - 1) as nat, (g - 1) as nat); + }, + None => {}, + } + } +} + +/// With a ranking, fuel beyond the rank of `x` is not needed. +pub proof fn lemma_hw_sat(doc: Document, rank: Seq, a: NodeId, x: NodeId, f: nat) + requires + host_ranked(doc, rank), + valid(doc, x), + host_including_within(doc, a, x, f), + ensures + host_including_within(doc, a, x, rank[x as int]), + decreases f, +{ + if x != a { + assert(f > 0); + match up(doc, x) { + Some(y) => { + lemma_up_rank(doc, rank, x); + lemma_hw_sat(doc, rank, a, y, (f - 1) as nat); + lemma_hw_mono(doc, a, y, rank[y as int], (rank[x as int] - 1) as nat); + }, + None => {}, + } + } +} + +/// [`is_host_including_inclusive_ancestor`] of a non-trivial pair is the +/// same as of the step up: `a` is a host-including inclusive ancestor of +/// `x` (not `a`) iff `x` has a step up and `a` is one of that node. +pub proof fn lemma_hi_unfold(doc: Document, rank: Seq, a: NodeId, x: NodeId) + requires + host_ranked(doc, rank), + valid(doc, x), + x != a, + ensures + is_host_including_inclusive_ancestor(doc, a, x) <==> (up(doc, x) is Some && is_host_including_inclusive_ancestor(doc, a, up(doc, x)->Some_0)), +{ + let n = doc.nodes.len(); + match up(doc, x) { + Some(y) => { + lemma_up_rank(doc, rank, x); + if is_host_including_inclusive_ancestor(doc, a, x) { + lemma_hw_mono(doc, a, y, (n - 1) as nat, n); + } + if is_host_including_inclusive_ancestor(doc, a, y) { + lemma_hw_sat(doc, rank, a, y, n); + assert(ranked_at(doc, rank, x as int)); + theorem_ranked_at_facts(doc, rank, x as int); + lemma_hw_mono(doc, a, y, rank[y as int], (n - 1) as nat); + } + }, + None => {}, + } +} + +/// An ancestor ranks no higher than its descendants. +pub proof fn lemma_hi_rank(doc: Document, rank: Seq, a: NodeId, x: NodeId) + requires + host_ranked(doc, rank), + valid(doc, x), + is_host_including_inclusive_ancestor(doc, a, x), + ensures + valid(doc, a), + rank[a as int] <= rank[x as int], + decreases rank[x as int], +{ + if x != a { + lemma_hi_unfold(doc, rank, a, x); + lemma_up_rank(doc, rank, x); + let y = up(doc, x)->Some_0; + lemma_hi_rank(doc, rank, a, y); + } +} + +/// `c`, a child of `p`, is in the subtree of `a` if `p` is. +pub proof fn lemma_hi_child(doc: Document, rank: Seq, a: NodeId, p: NodeId, c: NodeId) + requires + host_ranked(doc, rank), + valid(doc, c), + doc.nodes[c as int].parent == Some(p), + is_host_including_inclusive_ancestor(doc, a, p), + ensures + is_host_including_inclusive_ancestor(doc, a, c), +{ + if c != a { + lemma_hi_unfold(doc, rank, a, c); + } +} + +/// `f`, the contents of `h`, is in the subtree of `a` if `h` is. +pub proof fn lemma_hi_contents(doc: Document, rank: Seq, a: NodeId, h: NodeId, f: NodeId) + requires + host_ranked(doc, rank), + valid(doc, f), + doc.nodes[f as int].parent is None, + doc.nodes[f as int].kind is DocumentFragment, + doc.nodes[f as int].kind->DocumentFragment_host == Some(h), + is_host_including_inclusive_ancestor(doc, a, h), + ensures + is_host_including_inclusive_ancestor(doc, a, f), +{ + if f != a { + lemma_hi_unfold(doc, rank, a, f); + } +} + +/// A node is in its own subtree. +pub proof fn lemma_hi_self(doc: Document, a: NodeId) + requires + valid(doc, a), + ensures + is_host_including_inclusive_ancestor(doc, a, a), +{ +} + +// --------------------------------------------------------------------- +// Counting + +/// A list without duplicates of nodes of the subtree, all below `k`, is no +/// longer than [`subtree_count`] of `k`. +pub proof fn lemma_count_le(doc: Document, node: NodeId, s: Seq, k: nat) + requires + k <= max_nodes(), + s.no_duplicates(), + forall|i: int| 0 <= i < s.len() ==> (s[i] as nat) < k && is_host_including_inclusive_ancestor(doc, node, #[trigger] s[i]), + ensures + s.len() <= subtree_count(doc, node, k), + decreases k, +{ + if k == 0 { + if s.len() > 0 { + assert((s[0] as nat) < k); + } + } else if exists|j: int| 0 <= j < s.len() && s[j] as int == k - 1 { + let j = choose|j: int| 0 <= j < s.len() && s[j] as int == k - 1; + lemma_seq_remove(s, j); + let t = s.remove(j); + assert(is_host_including_inclusive_ancestor(doc, node, (k - 1) as NodeId)); + assert forall|i: int| 0 <= i < t.len() implies (t[i] as nat) < (k - 1) as nat && is_host_including_inclusive_ancestor(doc, node, #[trigger] t[i]) by { + let si = if i < j { i } else { i + 1 }; + assert(t[i] == s[si]); + assert(si != j); + } + lemma_count_le(doc, node, t, (k - 1) as nat); + } else { + assert forall|i: int| 0 <= i < s.len() implies (s[i] as nat) < (k - 1) as nat && is_host_including_inclusive_ancestor(doc, node, #[trigger] s[i]) by { + assert((s[i] as nat) < k); + } + lemma_count_le(doc, node, s, (k - 1) as nat); + } +} + +/// If the list holds every node of the subtree below `k`, it is as long as +/// [`subtree_count`] of `k`. +pub proof fn lemma_count_eq(doc: Document, node: NodeId, s: Seq, k: nat) + requires + k <= max_nodes(), + s.no_duplicates(), + forall|i: int| 0 <= i < s.len() ==> (s[i] as nat) < k && is_host_including_inclusive_ancestor(doc, node, #[trigger] s[i]), + forall|x: NodeId| (x as nat) < k && is_host_including_inclusive_ancestor(doc, node, x) ==> s.contains(x), + ensures + s.len() == subtree_count(doc, node, k), + decreases k, +{ + if k == 0 { + if s.len() > 0 { + assert((s[0] as nat) < k); + } + } else if exists|j: int| 0 <= j < s.len() && s[j] as int == k - 1 { + let j = choose|j: int| 0 <= j < s.len() && s[j] as int == k - 1; + lemma_seq_remove(s, j); + let t = s.remove(j); + assert(is_host_including_inclusive_ancestor(doc, node, (k - 1) as NodeId)); + assert forall|i: int| 0 <= i < t.len() implies (t[i] as nat) < (k - 1) as nat && is_host_including_inclusive_ancestor(doc, node, #[trigger] t[i]) by { + let si = if i < j { i } else { i + 1 }; + assert(t[i] == s[si]); + assert(si != j); + } + assert forall|x: NodeId| (x as nat) < (k - 1) as nat && is_host_including_inclusive_ancestor(doc, node, x) implies t.contains(x) by { + assert(s.contains(x)); + assert(x != s[j]); + } + lemma_count_eq(doc, node, t, (k - 1) as nat); + } else { + assert forall|i: int| 0 <= i < s.len() implies (s[i] as nat) < (k - 1) as nat && is_host_including_inclusive_ancestor(doc, node, #[trigger] s[i]) by { + assert((s[i] as nat) < k); + } + assert(!is_host_including_inclusive_ancestor(doc, node, (k - 1) as NodeId)) by { + if is_host_including_inclusive_ancestor(doc, node, (k - 1) as NodeId) { + assert(s.contains((k - 1) as NodeId)); + let j = choose|j: int| 0 <= j < s.len() && s[j] == (k - 1) as NodeId; + assert(s[j] as int == k - 1); + assert(false); + } + } + assert forall|x: NodeId| (x as nat) < (k - 1) as nat && is_host_including_inclusive_ancestor(doc, node, x) implies s.contains(x) by {} + lemma_count_eq(doc, node, s, (k - 1) as nat); + } +} + +/// A list without duplicates of nodes of the subtree is no longer than +/// [`subtree_size`]. +pub proof fn lemma_size_ge(doc: Document, node: NodeId, s: Seq) + requires + doc.nodes.len() <= max_nodes(), + s.no_duplicates(), + forall|i: int| 0 <= i < s.len() ==> valid(doc, #[trigger] s[i]) && is_host_including_inclusive_ancestor(doc, node, s[i]), + ensures + s.len() <= subtree_size(doc, node), +{ + lemma_count_le(doc, node, s, doc.nodes.len()); +} + +// --------------------------------------------------------------------- +// The copy map + +/// The children of new node `k` are a prefix of the copies of its source's +/// children, in order. +pub open spec fn children_prefix(d0: Document, a: Document, order: Seq, k: int) -> bool { + let b = d0.nodes.len(); + let s = d0.nodes[order[k] as int]; + let cn = a.nodes[b + k]; + &&& cn.children.len() <= s.children.len() + &&& forall|q: int| 0 <= q < cn.children.len() ==> { + let c = #[trigger] cn.children[q] as int; + &&& b <= c < b + order.len() + &&& order[c - b] == s.children[q] + } +} + +/// `a` is `d0` with the nodes `d0.nodes.len() ..` added, each a copy by +/// kind of the node of `order` at its offset, whose children are a prefix of +/// the copies of its source's: what a partial copy and a complete one have +/// in common. +pub open spec fn copy_map(d0: Document, node: NodeId, a: Document, order: Seq) -> bool { + let b = d0.nodes.len(); + let n = order.len(); + &&& wf(d0) + &&& wf(a) + &&& n >= 1 + &&& a.nodes.len() == b + n + &&& order[0] == node + &&& forall|k: int| 0 <= k < b ==> #[trigger] a.nodes[k] == d0.nodes[k] + &&& forall|t: int| 0 <= t < n ==> valid(d0, #[trigger] order[t]) + &&& forall|t: int| 0 <= t < n ==> #[trigger] deep_copy_kind(d0, a, order, t) + &&& forall|k: int| 0 <= k < n ==> #[trigger] children_prefix(d0, a, order, k) +} + +/// A partial copy of the loop is a [`copy_map`]. +pub proof fn lemma_inv_copy_map(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int) + requires + deep_inv(d0, node, a, order, i, j), + ensures + copy_map(d0, node, a, order), +{ + reveal(deep_inv); + let b = d0.nodes.len(); + let n = order.len() as int; + assert forall|k: int| 0 <= k < n implies #[trigger] children_prefix(d0, a, order, k) by { + if k < i { + assert(deep_copy_children(d0, a, order, k)); + } else if k == i { + } else { + assert(a.nodes[b + k].children.len() == 0); + } + } +} + +/// A complete copy is a [`copy_map`], with every child copied. +pub proof fn lemma_src_copy_map(doc: Document, node: NodeId, out: Document, src: Seq) + requires + wf(doc), + wf(out), + deep_clone_src(doc, node, out, src), + ensures + copy_map(doc, node, out, src), + forall|k: int| 0 <= k < src.len() ==> #[trigger] deep_copy_children(doc, out, src, k), +{ + let b = doc.nodes.len(); + assert forall|t: int| 0 <= t < src.len() implies #[trigger] deep_copy_kind(doc, out, src, t) by { + assert(deep_copy_node(doc, out, src, t)); + } + assert forall|k: int| 0 <= k < src.len() implies #[trigger] deep_copy_children(doc, out, src, k) by { + assert(deep_copy_node(doc, out, src, k)); + } + assert forall|k: int| 0 <= k < src.len() implies #[trigger] children_prefix(doc, out, src, k) by { + assert(deep_copy_children(doc, out, src, k)); + } +} + +/// Every new node but the first hangs under a new node, which is the copy +/// of its source's parent (or host): node `t` of the copy is the `q`th child +/// of node `u` (the source of `t` is the `q`th child of the source of `u`), +/// or the template contents of node `u` (the source of `t` is the contents +/// of the source of `u`). +pub proof fn lemma_up_copy(d0: Document, node: NodeId, a: Document, order: Seq, t: int) -> (u: int) + requires + copy_map(d0, node, a, order), + 1 <= t < order.len(), + ensures + 0 <= u < order.len(), + up(d0, order[t]) == Some(order[u]), + d0.nodes[order[t] as int].parent is Some ==> exists|q: int| 0 <= q < a.nodes[d0.nodes.len() + u].children.len() + && a.nodes[d0.nodes.len() + u].children[q] == (d0.nodes.len() + t) as NodeId && d0.nodes[order[u] as int].children[q] == order[t], + d0.nodes[order[t] as int].parent is None ==> { + &&& a.nodes[d0.nodes.len() + u].kind is Element + &&& a.nodes[d0.nodes.len() + u].kind->Element_template_contents == Some((d0.nodes.len() + t) as NodeId) + &&& d0.nodes[order[t] as int].kind is DocumentFragment + &&& d0.nodes[order[t] as int].kind->DocumentFragment_host == Some(order[u]) + }, +{ + let b = d0.nodes.len(); + let n = order.len() as int; + lemma_wf_facts(d0); + lemma_wf_facts(a); + assert(deep_copy_kind(d0, a, order, t)); + let nt = a.nodes[b + t]; + if nt.parent is Some { + let p = nt.parent->Some_0; + assert(a.nodes[p as int].children.contains((b + t) as NodeId)); + if p < b { + assert(a.nodes[p as int] == d0.nodes[p as int]); + let j = choose|j: int| 0 <= j < d0.nodes[p as int].children.len() && d0.nodes[p as int].children[j] == (b + t) as NodeId; + assert(d0.nodes[p as int].children[j] < b); + } + let u = p as int - b; + assert(0 <= u < n); + assert(b + u == p); + let q = choose|q: int| 0 <= q < a.nodes[p as int].children.len() && a.nodes[p as int].children[q] == (b + t) as NodeId; + assert(children_prefix(d0, a, order, u)); + let s = d0.nodes[order[u] as int]; + assert(order[t] == s.children[q]); + let x = order[t]; + assert(d0.nodes[x as int].parent == Some(order[u])); + assert(up(d0, x) == Some(order[u])); + u + } else { + assert(nt.kind is DocumentFragment && nt.kind->DocumentFragment_host is Some); + let h = nt.kind->DocumentFragment_host->Some_0; + assert(a.nodes[h as int].kind is Element); + assert(a.nodes[h as int].kind->Element_template_contents == Some((b + t) as NodeId)); + if h < b { + assert(a.nodes[h as int] == d0.nodes[h as int]); + assert(d0.nodes[h as int].kind->Element_template_contents->Some_0 < b); + } + let u = h as int - b; + assert(0 <= u < n); + assert(b + u == h); + assert(deep_copy_kind(d0, a, order, u)); + theorem_deep_copy_kind_fields(d0, a, order, u); + let s = d0.nodes[order[u] as int]; + assert(s.kind is Element); + assert(s.kind->Element_template_contents is Some); + let x = order[t]; + assert(x == s.kind->Element_template_contents->Some_0); + assert(d0.nodes[x as int].kind is DocumentFragment); + assert(d0.nodes[x as int].kind->DocumentFragment_host == Some(order[u])); + assert(d0.nodes[x as int].parent is None); + assert(up(d0, x) == Some(order[u])); + u + } +} + +/// Every new node copies a node of the subtree of `node`. +pub proof fn lemma_image(d0: Document, node: NodeId, a: Document, order: Seq, rank: Seq, t: int) + requires + copy_map(d0, node, a, order), + host_ranked(d0, rank), + 0 <= t < order.len(), + ensures + is_host_including_inclusive_ancestor(d0, node, order[t]), + decreases rank[order[t] as int], +{ + let x = order[t]; + if t == 0 { + lemma_hi_self(d0, node); + } else { + let u = lemma_up_copy(d0, node, a, order, t); + let y = order[u]; + assert(valid(d0, x) && valid(d0, y)); + lemma_up_rank(d0, rank, x); + lemma_image(d0, node, a, order, rank, u); + lemma_wf_facts(d0); + if d0.nodes[x as int].parent is Some { + assert(d0.nodes[x as int].parent == Some(y)); + lemma_hi_child(d0, rank, node, y, x); + } else { + lemma_hi_contents(d0, rank, node, y, x); + } + } +} + +/// No new node but the first copies `node` itself. +pub proof fn lemma_only_first_copies_node(d0: Document, node: NodeId, a: Document, order: Seq, rank: Seq, t: int) + requires + copy_map(d0, node, a, order), + host_ranked(d0, rank), + 1 <= t < order.len(), + ensures + order[t] != node, +{ + let u = lemma_up_copy(d0, node, a, order, t); + lemma_image(d0, node, a, order, rank, u); + let x = order[t]; + assert(valid(d0, x) && valid(d0, order[u])); + if x == node { + lemma_up_rank(d0, rank, x); + lemma_hi_rank(d0, rank, node, order[u]); + } +} + +/// Distinct new nodes copy distinct nodes. +pub proof fn lemma_inj(d0: Document, node: NodeId, a: Document, order: Seq, rank: Seq, t1: int, t2: int) + requires + copy_map(d0, node, a, order), + host_ranked(d0, rank), + 0 <= t1 < order.len(), + 0 <= t2 < order.len(), + order[t1] == order[t2], + ensures + t1 == t2, + decreases rank[order[t1] as int], +{ + let b = d0.nodes.len(); + if t1 == 0 && t2 != 0 { + lemma_only_first_copies_node(d0, node, a, order, rank, t2); + } else if t2 == 0 && t1 != 0 { + lemma_only_first_copies_node(d0, node, a, order, rank, t1); + } else if t1 != 0 && t2 != 0 { + let x = order[t1]; + let u1 = lemma_up_copy(d0, node, a, order, t1); + let u2 = lemma_up_copy(d0, node, a, order, t2); + assert(valid(d0, x)); + lemma_up_rank(d0, rank, x); + assert(order[u1] == order[u2]); + lemma_inj(d0, node, a, order, rank, u1, u2); + lemma_wf_facts(d0); + if d0.nodes[x as int].parent is Some { + let q1 = choose|q: int| 0 <= q < a.nodes[b + u1].children.len() && a.nodes[b + u1].children[q] == (b + t1) as NodeId + && d0.nodes[order[u1] as int].children[q] == order[t1]; + let q2 = choose|q: int| 0 <= q < a.nodes[b + u2].children.len() && a.nodes[b + u2].children[q] == (b + t2) as NodeId + && d0.nodes[order[u2] as int].children[q] == order[t2]; + let kids = d0.nodes[order[u1] as int].children; + assert(kids.no_duplicates()); + assert(children_prefix(d0, a, order, u1)); + assert(q1 < kids.len() && q2 < kids.len()); + assert(kids[q1] == kids[q2]); + assert(q1 == q2); + } + } +} + +/// The copies' sources are distinct. +pub proof fn lemma_order_nodups(d0: Document, node: NodeId, a: Document, order: Seq, rank: Seq) + requires + copy_map(d0, node, a, order), + host_ranked(d0, rank), + ensures + order.no_duplicates(), +{ + assert forall|i: int, j: int| 0 <= i < order.len() && 0 <= j < order.len() && i != j implies order[i] != order[j] by { + if order[i] == order[j] { + lemma_inj(d0, node, a, order, rank, i, j); + } + } +} + +/// In a complete copy, every node of the subtree of `node` has a copy. +pub proof fn lemma_surj(d0: Document, node: NodeId, a: Document, order: Seq, rank: Seq, x: NodeId) -> (t: int) + requires + copy_map(d0, node, a, order), + forall|k: int| 0 <= k < order.len() ==> #[trigger] deep_copy_children(d0, a, order, k), + host_ranked(d0, rank), + valid(d0, x), + is_host_including_inclusive_ancestor(d0, node, x), + ensures + 0 <= t < order.len(), + order[t] == x, + decreases rank[x as int], +{ + let b = d0.nodes.len(); + if x == node { + 0 + } else { + lemma_hi_unfold(d0, rank, node, x); + lemma_up_rank(d0, rank, x); + let y = up(d0, x)->Some_0; + let ty = lemma_surj(d0, node, a, order, rank, y); + lemma_wf_facts(d0); + assert(order[ty] == y); + assert(deep_copy_kind(d0, a, order, ty)); + assert(deep_copy_children(d0, a, order, ty)); + if d0.nodes[x as int].parent is Some { + assert(d0.nodes[x as int].parent == Some(y)); + assert(d0.nodes[y as int].children.contains(x)); + let q = choose|q: int| 0 <= q < d0.nodes[y as int].children.len() && d0.nodes[y as int].children[q] == x; + let c = a.nodes[b + ty].children[q] as int; + assert(order[c - b] == x); + c - b + } else { + assert(d0.nodes[x as int].kind is DocumentFragment); + assert(d0.nodes[x as int].kind->DocumentFragment_host == Some(y)); + assert(d0.nodes[y as int].kind->Element_template_contents == Some(x)); + theorem_deep_copy_kind_fields(d0, a, order, ty); + let c = a.nodes[b + ty].kind->Element_template_contents->Some_0 as int; + assert(order[c - b] == x); + c - b + } + } +} + +/// A well-formed arena that grew by [`subtree_size`] nodes is within the node +/// limit, so a refusal of [`clone_deep_ensures`] is exactly the case where +/// the copy does not fit: `clone_deep` refuses only if the arena cannot hold +/// [`subtree_size`] more nodes, and, since it then cannot be well formed, +/// refuses whenever it cannot. +pub proof fn theorem_clone_deep_refuses_exactly_when_full(doc: Document, node: NodeId, out: Document, id: Option) + requires + clone_deep_requires(doc, node), + clone_deep_ensures(doc, node, out, id), + ensures + id is None <==> !can_create(doc, subtree_size(doc, node)), +{ + match id { + Some(copy) => { + lemma_wf_bound_dom(out); + }, + None => {}, + } +} + +/// A refusal needs the arena to be too full for the copy. +pub proof fn theorem_clone_deep_refused_only_if_full(doc: Document, node: NodeId, out: Document) + requires + clone_deep_refused(doc, node, out), + ensures + doc.nodes.len() + subtree_size(doc, node) > max_nodes(), +{ +} + +/// A valid deep clone meets the size clause of the contract. +pub proof fn theorem_deep_clone_grew(doc: Document, node: NodeId, out: Document, src: Seq) + requires + wf(doc), + wf(out), + host_acyclic(doc), + deep_clone_src(doc, node, out, src), + ensures + clone_deep_grew(doc, node, out), +{ + theorem_deep_clone_size(doc, node, out, src); +} + +/// The size clause says the arena grew by exactly [`subtree_size`] nodes. +pub proof fn theorem_clone_deep_grew_means(doc: Document, node: NodeId, out: Document) + requires + clone_deep_grew(doc, node, out), + ensures + out.nodes.len() == doc.nodes.len() + subtree_size(doc, node), +{ +} + +/// A well-formed arena is within the node limit. +pub proof fn lemma_wf_bound_dom(a: Document) + requires + wf(a), + ensures + a.nodes.len() <= max_nodes(), +{ + let _ = lemma_wf_witness(a); +} + +// --------------------------------------------------------------------- +// The worklist never outgrows the subtree + +/// Pushing a node not in the list keeps it free of duplicates. +pub proof fn lemma_push_nodups(s: Seq, x: NodeId) + requires + s.no_duplicates(), + forall|k: int| 0 <= k < s.len() ==> s[k] != x, + ensures + s.push(x).no_duplicates(), +{ + let t = s.push(x); + assert forall|i: int, j: int| 0 <= i < t.len() && 0 <= j < t.len() && i != j implies t[i] != t[j] by { + if i < s.len() && j < s.len() { + assert(t[i] == s[i] && t[j] == s[j]); + } else if i < s.len() { + assert(t[i] == s[i] && t[j] == x); + } else { + assert(t[j] == s[j] && t[i] == x); + } + } +} + +/// The child `ch` the loop is about to clone (the `j`th of the source of node +/// `i`) is in the subtree, and no node listed so far copies it: it is the +/// `j`th child of its parent, whose copy has only `j` children so far. +pub proof fn lemma_unlisted_child( + d0: Document, + node: NodeId, + a: Document, + order: Seq, + rank: Seq, + i: int, + j: int, + ch: NodeId, +) + requires + copy_map(d0, node, a, order), + host_ranked(d0, rank), + order.no_duplicates(), + 0 <= i < order.len(), + j < d0.nodes[order[i] as int].children.len(), + ch == d0.nodes[order[i] as int].children[j], + a.nodes[d0.nodes.len() + i].children.len() == j, + ensures + valid(d0, ch), + d0.nodes[ch as int].parent == Some(order[i]), + is_host_including_inclusive_ancestor(d0, node, ch), + forall|k: int| 0 <= k < order.len() ==> order[k] != ch, +{ + lemma_wf_facts(d0); + let b = d0.nodes.len(); + let n = order.len() as int; + let s = order[i]; + assert(valid(d0, s)); + assert(d0.nodes[s as int].children[j] == ch); + assert(up(d0, ch) == Some(s)); + lemma_image(d0, node, a, order, rank, i); + lemma_hi_child(d0, rank, node, s, ch); + assert forall|k: int| 0 <= k < n implies order[k] != ch by { + if order[k] == ch { + if k == 0 { + lemma_up_rank(d0, rank, ch); + lemma_hi_rank(d0, rank, node, s); + } else { + let u = lemma_up_copy(d0, node, a, order, k); + assert(order[u] == s); + assert(u == i); + let q = choose|q: int| 0 <= q < a.nodes[b + u].children.len() && a.nodes[b + u].children[q] == (b + k) as NodeId + && d0.nodes[order[u] as int].children[q] == order[k]; + let kids = d0.nodes[s as int].children; + assert(kids.no_duplicates()); + assert(q < j); + assert(kids[q] == kids[j]); + assert(false); + } + } + } +} + +/// The contents `f` of the template `ch` that the loop is about to clone is +/// in the subtree, and no node listed so far copies it, nor is it `ch`. +pub proof fn lemma_unlisted_contents( + d0: Document, + node: NodeId, + a: Document, + order: Seq, + rank: Seq, + ch: NodeId, +) + requires + copy_map(d0, node, a, order), + host_ranked(d0, rank), + valid(d0, ch), + d0.nodes[ch as int].parent is Some, + is_host_including_inclusive_ancestor(d0, node, ch), + forall|k: int| 0 <= k < order.len() ==> order[k] != ch, + is_template(d0.nodes[ch as int].kind), + ensures + ({ + let f = d0.nodes[ch as int].kind->Element_template_contents->Some_0; + &&& valid(d0, f) + &&& is_host_including_inclusive_ancestor(d0, node, f) + &&& f != ch + &&& forall|k: int| 0 <= k < order.len() ==> order[k] != f + }), +{ + lemma_wf_facts(d0); + let f = d0.nodes[ch as int].kind->Element_template_contents->Some_0; + assert(valid(d0, f)); + assert(d0.nodes[f as int].kind is DocumentFragment); + assert(d0.nodes[f as int].kind->DocumentFragment_host == Some(ch)); + assert(d0.nodes[f as int].parent is None); + lemma_hi_contents(d0, rank, node, ch, f); + assert(f != ch); + assert forall|k: int| 0 <= k < order.len() implies order[k] != f by { + if order[k] == f { + if k == 0 { + lemma_up_rank(d0, rank, f); + lemma_hi_rank(d0, rank, node, ch); + } else { + let u = lemma_up_copy(d0, node, a, order, k); + assert(up(d0, f) == Some(ch)); + assert(order[u] == ch); + assert(order[u] != ch); + } + } + } +} + +/// The worklist after cloning `ch`, the `j`th child of the source of node +/// `i`, off it ([`order_after`]) still lists distinct nodes of the subtree: +/// so the nodes `clone_deep` has made plus the ones it is about to make are +/// no more than [`subtree_size`]. This is why a clone that does not fit +/// cannot fit at all. +pub proof fn lemma_step_fits(d0: Document, node: NodeId, a: Document, order: Seq, i: int, j: int, ch: NodeId) + requires + deep_inv(d0, node, a, order, i, j), + host_acyclic(d0), + i < order.len(), + j < d0.nodes[order[i] as int].children.len(), + ch == d0.nodes[order[i] as int].children[j], + ensures + order.len() + clone_count(d0, ch) <= subtree_size(d0, node), +{ + lemma_inv_copy_map(d0, node, a, order, i, j); + reveal(deep_inv); + let rank = lemma_rank_witness(d0); + lemma_order_nodups(d0, node, a, order, rank); + lemma_wf_bound_dom(d0); + assert(a.nodes[d0.nodes.len() + i].children.len() == j); + lemma_unlisted_child(d0, node, a, order, rank, i, j, ch); + let n = order.len() as int; + let o2 = order_after(d0, order, ch); + assert(o2.len() == n + clone_count(d0, ch)); + assert forall|x: int| 0 <= x < n implies o2[x] == order[x] by {} + lemma_push_nodups(order, ch); + assert forall|x: int| 0 <= x < n implies valid(d0, #[trigger] order[x]) && is_host_including_inclusive_ancestor(d0, node, order[x]) by { + lemma_image(d0, node, a, order, rank, x); + } + if is_template(d0.nodes[ch as int].kind) { + lemma_unlisted_contents(d0, node, a, order, rank, ch); + let f = d0.nodes[ch as int].kind->Element_template_contents->Some_0; + assert(o2 == order.push(ch).push(f)); + assert forall|k: int| 0 <= k < n + 1 implies order.push(ch)[k] != f by { + if k < n { + assert(order.push(ch)[k] == order[k]); + } + } + lemma_push_nodups(order.push(ch), f); + } + assert forall|x: int| 0 <= x < o2.len() implies valid(d0, #[trigger] o2[x]) && is_host_including_inclusive_ancestor(d0, node, o2[x]) by { + if x >= n { + let f = d0.nodes[ch as int].kind->Element_template_contents->Some_0; + if x == n { + assert(o2[x] == ch); + } else { + assert(o2[x] == f); + } + } + } + lemma_size_ge(d0, node, o2); +} + +/// The first copy (a template and its contents) is no more than +/// [`subtree_size`]. +pub proof fn lemma_root_fits(d0: Document, node: NodeId) + requires + clone_deep_requires(d0, node), + ensures + clone_count(d0, node) <= subtree_size(d0, node), +{ + let rank = lemma_rank_witness(d0); + lemma_wf_bound_dom(d0); + lemma_wf_facts(d0); + let o = order_after(d0, Seq::empty(), node); + lemma_hi_self(d0, node); + if is_template(d0.nodes[node as int].kind) { + let f = d0.nodes[node as int].kind->Element_template_contents->Some_0; + assert(o[1] == f); + assert(valid(d0, f)); + assert(d0.nodes[f as int].kind is DocumentFragment); + assert(d0.nodes[f as int].kind->DocumentFragment_host == Some(node)); + assert(d0.nodes[f as int].parent is None); + lemma_hi_contents(d0, rank, node, node, f); + assert(f != node); + } + assert(o.no_duplicates()); + assert forall|x: int| 0 <= x < o.len() implies valid(d0, #[trigger] o[x]) && is_host_including_inclusive_ancestor(d0, node, o[x]) by {} + lemma_size_ge(d0, node, o); +} + +/// A valid deep clone adds exactly [`subtree_size`] nodes: the copy is the +/// unfolding of the host-including subtree of `node`, and nothing else. (The +/// relation alone, without `wf(out)`, would leave room for unreachable new +/// nodes; it is `wf(out)` that rules them out.) +pub proof fn theorem_deep_clone_size(doc: Document, node: NodeId, out: Document, src: Seq) + requires + wf(doc), + wf(out), + host_acyclic(doc), + deep_clone_src(doc, node, out, src), + ensures + src.len() == subtree_size(doc, node), + out.nodes.len() == doc.nodes.len() + subtree_size(doc, node), +{ + lemma_src_copy_map(doc, node, out, src); + let rank = lemma_rank_witness(doc); + lemma_order_nodups(doc, node, out, src, rank); + lemma_wf_bound_dom(doc); + assert forall|i: int| 0 <= i < src.len() implies (src[i] as nat) < doc.nodes.len() && is_host_including_inclusive_ancestor(doc, node, #[trigger] src[i]) by { + lemma_image(doc, node, out, src, rank, i); + } + assert forall|x: NodeId| (x as nat) < doc.nodes.len() && is_host_including_inclusive_ancestor(doc, node, x) implies src.contains(x) by { + let t = lemma_surj(doc, node, out, src, rank, x); + assert(src[t] == x); + } + lemma_count_eq(doc, node, src, doc.nodes.len()); +} + +} // verus! diff --git a/crates/clazel-dom/src/deep_clone_size_mutation_lemmas.rs b/crates/clazel-dom/src/deep_clone_size_mutation_lemmas.rs new file mode 100644 index 000000000..bb56d8d94 --- /dev/null +++ b/crates/clazel-dom/src/deep_clone_size_mutation_lemmas.rs @@ -0,0 +1,278 @@ +//! Concrete instances of the size theorems of deep clone, the kill +//! certificates of the mutations of [`subtree_count`], [`subtree_size`], +//! [`clone_deep_refused`], [`clone_deep_grew`] and [`ranked_at`] in +//! `mutations.json` (`xtask/src/mutate.rs`). Each lemma states a fact about +//! a concrete arena that a proven theorem implies for the real spec, and +//! that a mutant makes false (the certificate's witness). +//! +//! The arenas: [`ds_leaf`] is a document (0) and an element (1), and +//! [`ds_tmpl`] a document, a template element (1) and its contents, the +//! fragment (2). A deep clone of node 1 is [`ds_leaf_copy`], resp. +//! [`ds_tmpl_copy`]: the shallow clone, which is a valid deep clone of a node +//! with no children. + +#[cfg(verus_only)] +use super::contract::*; +#[cfg(verus_only)] +use super::deep_clone_size_lemmas::*; +#[cfg(verus_only)] +use super::lemmas::*; +#[cfg(verus_only)] +use super::mutation_lemmas::*; +#[cfg(verus_only)] +use super::ops_lemmas::*; +#[cfg(verus_only)] +use super::ops_mutation_lemmas::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A document and an element (1). +pub open spec fn ds_leaf() -> Document { + ol_d1() +} + +/// The shallow clone of node 1 of [`ds_leaf`]: node 2. +pub open spec fn ds_leaf_copy() -> Document { + clone_node(ds_leaf(), 1).0 +} + +/// The source of the copy of [`ds_leaf`]. +pub open spec fn ds_leaf_src() -> Seq { + seq![1usize] +} + +/// The sources of the copies of [`ds_tmpl`]. +pub open spec fn ds_tmpl_src() -> Seq { + seq![1usize, 2usize] +} + +/// A document, a template element (1) and its contents (2). +pub open spec fn ds_tmpl() -> Document { + ol_template() +} + +/// The shallow clone of node 1 of [`ds_tmpl`]: a template (3) and its +/// contents (4). +pub open spec fn ds_tmpl_copy() -> Document { + clone_node(ds_tmpl(), 1).0 +} + +/// A ranking of [`ds_leaf`]. +pub open spec fn ds_rank_leaf() -> Seq { + seq![0nat, 0nat] +} + +/// A ranking of [`ds_tmpl`]: the contents ranks above the template. +pub open spec fn ds_rank_tmpl() -> Seq { + seq![0nat, 0nat, 1nat] +} + +/// [`ds_leaf`] is well formed and host-acyclic, and node 1 may be cloned. +pub proof fn ds_leaf_facts() + ensures + wf(ds_leaf()), + ds_leaf().nodes.len() == 2, + host_acyclic(ds_leaf()), + clone_deep_requires(ds_leaf(), 1), + can_create(ds_leaf(), clone_count(ds_leaf(), 1)), + wf(ds_leaf_copy()), + ds_leaf_copy().nodes.len() == 3, +{ + theorem_new_document_wf(seq![]); + assert(creatable(ol_d0(), ml_element())) by (compute_only); + assert(can_create(ol_d0(), 1)) by (compute_only); + theorem_create_node_wf(ol_d0(), ml_element()); + assert(ds_leaf().nodes.len() == 2) by (compute_only); + let rank = ds_rank_leaf(); + assert(ranked_at(ds_leaf(), ds_rank_leaf(), 0)) by (compute_only); + assert(ranked_at(ds_leaf(), ds_rank_leaf(), 1)) by (compute_only); + assert(host_ranked(ds_leaf(), rank)) by { + assert forall|i: int| 0 <= i < ds_leaf().nodes.len() implies #[trigger] ranked_at(ds_leaf(), rank, i) by { + if i == 0 { + } else { + assert(i == 1); + } + } + } + assert(valid(ds_leaf(), 1)); + assert(!(ds_leaf().nodes[1].kind is Document)) by (compute_only); + assert(clone_requires(ds_leaf(), 1)); + assert(can_create(ds_leaf(), clone_count(ds_leaf(), 1))) by (compute_only); + theorem_clone_node_wf(ds_leaf(), 1); + assert(ds_leaf_copy().nodes.len() == 3) by (compute_only); +} + +/// The deep clone of node 1 of [`ds_leaf`] is its shallow clone, and its +/// subtree is one node. +pub proof fn ds_leaf_exact() + ensures + subtree_size(ds_leaf(), 1) == 1, +{ + ds_leaf_facts(); + theorem_clone_node_wf(ds_leaf(), 1); + assert(ds_leaf().nodes[1].children.len() == 0) by (compute_only); + assert(ds_leaf_copy().nodes[2].children.len() == 0) by (compute_only); + assert(deep_copy_kind(ds_leaf(), ds_leaf_copy(), ds_leaf_src(), 0)) by (compute_only); + assert(deep_copy_children(ds_leaf(), ds_leaf_copy(), ds_leaf_src(), 0)); + assert(deep_copy_node(ds_leaf(), ds_leaf_copy(), ds_leaf_src(), 0)); + assert(deep_clone_src(ds_leaf(), 1, ds_leaf_copy(), ds_leaf_src())) by { + assert forall|t: int| 0 <= t < ds_leaf_src().len() implies valid(ds_leaf(), #[trigger] ds_leaf_src()[t]) by {} + assert forall|t: int| 0 <= t < ds_leaf_src().len() implies #[trigger] deep_copy_node(ds_leaf(), ds_leaf_copy(), ds_leaf_src(), t) by {} + } + theorem_deep_clone_size(ds_leaf(), 1, ds_leaf_copy(), ds_leaf_src()); +} + +/// [`ds_tmpl`] is well formed and host-acyclic, and node 1 may be cloned. +pub proof fn ds_tmpl_facts() + ensures + wf(ds_tmpl()), + ds_tmpl().nodes.len() == 3, + host_acyclic(ds_tmpl()), + clone_deep_requires(ds_tmpl(), 1), + can_create(ds_tmpl(), clone_count(ds_tmpl(), 1)), + wf(ds_tmpl_copy()), + ds_tmpl_copy().nodes.len() == 5, +{ + theorem_new_document_wf(seq![]); + assert(creatable(ol_d0(), ml_element())) by (compute_only); + assert(can_create(ol_d0(), 1)) by (compute_only); + theorem_create_node_wf(ol_d0(), ml_element()); + assert(ol_d1().nodes[1].kind is Element) by (compute_only); + assert(ol_d1().nodes[1].kind->Element_template_contents is None) by (compute_only); + theorem_create_template_contents_wf(ol_d1(), 1); + assert(ds_tmpl().nodes.len() == 3) by (compute_only); + let rank = ds_rank_tmpl(); + assert(ranked_at(ds_tmpl(), ds_rank_tmpl(), 0)) by (compute_only); + assert(ranked_at(ds_tmpl(), ds_rank_tmpl(), 1)) by (compute_only); + assert(ranked_at(ds_tmpl(), ds_rank_tmpl(), 2)) by (compute_only); + assert(host_ranked(ds_tmpl(), rank)) by { + assert forall|i: int| 0 <= i < ds_tmpl().nodes.len() implies #[trigger] ranked_at(ds_tmpl(), rank, i) by { + if i == 0 { + } else if i == 1 { + } else { + assert(i == 2); + } + } + } + assert(valid(ds_tmpl(), 1)); + assert(!(ds_tmpl().nodes[1].kind is Document)) by (compute_only); + assert(clone_requires(ds_tmpl(), 1)); + assert(ds_tmpl().nodes[1].kind is Element) by (compute_only); + assert(ds_tmpl().nodes[1].kind->Element_template_contents is Some) by (compute_only); + assert(can_create(ds_tmpl(), clone_count(ds_tmpl(), 1))) by (compute_only); + theorem_clone_node_wf(ds_tmpl(), 1); + assert(ds_tmpl_copy().nodes.len() == 5) by (compute_only); +} + +/// The deep clone of the template, node 1 of [`ds_tmpl`], is its shallow +/// clone (the template and its contents), and its subtree is two nodes: +/// the template and its contents. +pub proof fn ds_tmpl_exact() + ensures + subtree_size(ds_tmpl(), 1) == 2, +{ + ds_tmpl_facts(); + theorem_clone_node_wf(ds_tmpl(), 1); + assert(ds_tmpl().nodes[1].children.len() == 0) by (compute_only); + assert(ds_tmpl().nodes[2].children.len() == 0) by (compute_only); + assert(ds_tmpl_copy().nodes[3].children.len() == 0) by (compute_only); + assert(ds_tmpl_copy().nodes[4].children.len() == 0) by (compute_only); + assert(deep_copy_kind(ds_tmpl(), ds_tmpl_copy(), ds_tmpl_src(), 0)) by (compute_only); + assert(deep_copy_kind(ds_tmpl(), ds_tmpl_copy(), ds_tmpl_src(), 1)) by (compute_only); + assert(deep_copy_children(ds_tmpl(), ds_tmpl_copy(), ds_tmpl_src(), 0)); + assert(deep_copy_children(ds_tmpl(), ds_tmpl_copy(), ds_tmpl_src(), 1)); + assert(deep_clone_src(ds_tmpl(), 1, ds_tmpl_copy(), ds_tmpl_src())) by { + assert forall|t: int| 0 <= t < ds_tmpl_src().len() implies valid(ds_tmpl(), #[trigger] ds_tmpl_src()[t]) by {} + assert forall|t: int| 0 <= t < ds_tmpl_src().len() implies #[trigger] deep_copy_node(ds_tmpl(), ds_tmpl_copy(), ds_tmpl_src(), t) by { + assert(deep_copy_kind(ds_tmpl(), ds_tmpl_copy(), ds_tmpl_src(), t)); + assert(deep_copy_children(ds_tmpl(), ds_tmpl_copy(), ds_tmpl_src(), t)); + } + } + theorem_deep_clone_size(ds_tmpl(), 1, ds_tmpl_copy(), ds_tmpl_src()); +} + +/// The deep clone of the leaf meets the size clause of the contract. +pub proof fn ds_leaf_grew() + ensures + clone_deep_grew(ds_leaf(), 1, ds_leaf_copy()), +{ + ds_leaf_exact(); + ds_leaf_facts(); + assert(ds_leaf_copy().nodes.len() == 3) by (compute_only); +} + +/// Refusing to clone the leaf, leaving the arena as it was, is not allowed: +/// the copy fits. +pub proof fn ds_leaf_cannot_refuse() + ensures + !clone_deep_refused(ds_leaf(), 1, ds_leaf()), +{ + ds_leaf_exact(); + ds_leaf_facts(); + if clone_deep_refused(ds_leaf(), 1, ds_leaf()) { + theorem_clone_deep_refuses_exactly_when_full(ds_leaf(), 1, ds_leaf(), None); + assert(can_create(ds_leaf(), 1)) by (compute_only); + assert(false); + } +} + +/// An arena that does not grow is not a clone of the leaf. +pub proof fn ds_leaf_unchanged_is_not_grown() + ensures + !clone_deep_grew(ds_leaf(), 1, ds_leaf()), +{ + ds_leaf_exact(); + if clone_deep_grew(ds_leaf(), 1, ds_leaf()) { + theorem_clone_deep_grew_means(ds_leaf(), 1, ds_leaf()); + assert(false); + } +} + +/// The ranking of a node and its parent is strict: nodes 1 and 0 of a +/// document with node 1 appended to it, ranked alike, are not ranked. +pub proof fn ds_rank_parent_strict() + ensures + !ranked_at(ml_doc_child(), seq![0nat, 0nat, 0nat], 1), +{ + ml_doc_child_wf(); + ml_doc3_wf(); + assert(ml_doc_child().nodes.len() == 3) by (compute_only); + if ranked_at(ml_doc_child(), seq![0nat, 0nat, 0nat], 1) { + theorem_ranked_at_facts(ml_doc_child(), seq![0nat, 0nat, 0nat], 1); + assert(ml_doc_child().nodes[1].parent == Some(0usize)); + assert(false); + } +} + +/// The ranking of a fragment and its host is strict: a template's contents +/// ranked like the template is not ranked. +pub proof fn ds_rank_host_strict() + ensures + !ranked_at(ds_tmpl(), seq![0nat, 0nat, 0nat], 2), +{ + ds_tmpl_facts(); + assert(ds_tmpl().nodes[2].kind is DocumentFragment) by (compute_only); + assert(ds_tmpl().nodes[2].kind->DocumentFragment_host == Some(1usize)) by (compute_only); + if ranked_at(ds_tmpl(), seq![0nat, 0nat, 0nat], 2) { + theorem_ranked_at_facts(ds_tmpl(), seq![0nat, 0nat, 0nat], 2); + assert(false); + } +} + +/// A rank is below the node count. +pub proof fn ds_rank_bounded() + ensures + !ranked_at(ds_leaf(), seq![5nat, 0nat], 0), +{ + ds_leaf_facts(); + if ranked_at(ds_leaf(), seq![5nat, 0nat], 0) { + theorem_ranked_at_facts(ds_leaf(), seq![5nat, 0nat], 0); + assert(false); + } +} + +} // verus! diff --git a/crates/clazel-dom/src/exec.rs b/crates/clazel-dom/src/exec.rs index a67b7337b..0bf18711f 100644 --- a/crates/clazel-dom/src/exec.rs +++ b/crates/clazel-dom/src/exec.rs @@ -16,7 +16,13 @@ #[cfg(verus_only)] use super::contract::*; #[cfg(verus_only)] +use super::deep_clone_lemmas::*; +#[cfg(verus_only)] +use super::deep_clone_size_lemmas::*; +#[cfg(verus_only)] use super::lemmas::*; +#[cfg(verus_only)] +use super::ops_lemmas::*; #[allow(unused_imports)] use super::spec::*; #[allow(unused_imports)] @@ -491,14 +497,13 @@ impl Arena { } } - /// Steps 7.1 of "insert": adopt `node` (remove it from its parent, if - /// any). - fn adopt(&mut self, node: usize) + /// DOM §4.5 "adopt" (step 7.1 of "insert" calls it): remove `node` from + /// its parent, if any ([`adopt_ensures`]). + pub fn adopt(&mut self, node: usize) requires - wf(old(self)@), - valid(old(self)@, node), + adopt_requires(old(self)@, node), ensures - final(self)@ == adopt(old(self)@, node), + adopt_ensures(old(self)@, node, final(self)@), { if self.node(node).parent.is_some() { self.remove(node); @@ -1026,4 +1031,1242 @@ proof fn lemma_exec_state_done(d0: Document, from: NodeId, to: NodeId) assert(s.nodes.update(to as int, Node { children: s.nodes[to as int].children + f, ..s.nodes[to as int] }) =~= r.nodes); } +// --------------------------------------------------------------------- +// Phase 7 item 7.13: the operations scripts call (see `spec.rs`) + +/// The records after setting the parent of each of `ids` to `p`. +pub open spec fn set_parents_spec(s: Seq, ids: Seq, p: Option) -> Seq { + Seq::new(s.len(), |x: int| if ids.contains(x as NodeId) { Node { parent: p, ..s[x] } } else { s[x] }) +} + +/// One iteration of [`Arena::set_parents`]'s loop. +proof fn lemma_set_parents_step(s: Seq, ids: Seq, p: Option, i: int) + requires + s.len() <= max_nodes(), + 0 <= i < ids.len(), + ids[i] < s.len(), + ensures + ({ + let c = ids[i]; + let a = set_parents_spec(s, ids.subrange(0, i), p); + a.update(c as int, Node { parent: p, ..a[c as int] }) == set_parents_spec(s, ids.subrange(0, i + 1), p) + }), +{ + let c = ids[i]; + let a = set_parents_spec(s, ids.subrange(0, i), p); + let b = set_parents_spec(s, ids.subrange(0, i + 1), p); + let u = a.update(c as int, Node { parent: p, ..a[c as int] }); + assert(ids.subrange(0, i + 1)[i] == c); + assert forall|x: int| 0 <= x < s.len() implies #[trigger] u[x] == b[x] by { + let xn = x as NodeId; + if x == c { + assert(ids.subrange(0, i + 1).contains(c)); + } else { + assert(xn != c); + if ids.subrange(0, i + 1).contains(xn) { + let j = choose|j: int| 0 <= j < i + 1 && #[trigger] ids.subrange(0, i + 1)[j] == xn; + if j == i { + assert(ids.subrange(0, i + 1)[i] == c); + } + assert(j < i); + assert(ids.subrange(0, i)[j] == xn); + } + if ids.subrange(0, i).contains(xn) { + let j = choose|j: int| 0 <= j < i && #[trigger] ids.subrange(0, i)[j] == xn; + assert(ids.subrange(0, i + 1)[j] == xn); + } + } + } + assert(u =~= b); +} + +/// A node's children are nodes of the arena that have it as their parent, +/// without duplicates, and it is not one of them. +proof fn lemma_children_facts(doc: Document, p: NodeId) + requires + wf(doc), + valid(doc, p), + ensures + doc.nodes[p as int].children.no_duplicates(), + !doc.nodes[p as int].children.contains(p), + forall|j: int| + 0 <= j < doc.nodes[p as int].children.len() ==> (#[trigger] doc.nodes[p as int].children[j]) < doc.nodes.len() + && doc.nodes[doc.nodes[p as int].children[j] as int].parent == Some(p), +{ + let paths = lemma_wf_witness(doc); + lemma_wf_split(doc, paths); + let f = doc.nodes[p as int].children; + assert forall|j: int| 0 <= j < f.len() implies (#[trigger] f[j]) < doc.nodes.len() && doc.nodes[f[j] as int].parent == Some(p) by { + assert(doc.nodes[p as int].children[j] == f[j]); + } + if f.contains(p) { + let j = choose|j: int| 0 <= j < f.len() && f[j] == p; + assert(f[j] == p); + lemma_parent_not_self(doc, paths, p); + } +} + +/// The arena after taking all of `p`'s children and setting their parents +/// to none is [`removed_prefix`] with everything removed. +proof fn lemma_remove_children_state(d0: Document, p: NodeId) + requires + wf(d0), + valid(d0, p), + ensures + set_parents_spec( + d0.nodes.update(p as int, Node { children: seq![], ..d0.nodes[p as int] }), + d0.nodes[p as int].children, + None, + ) == removed_prefix(d0, p, d0.nodes[p as int].children.len() as int), +{ + lemma_children_facts(d0, p); + let kids = d0.nodes[p as int].children; + let len = kids.len(); + let s1 = d0.nodes.update(p as int, Node { children: seq![], ..d0.nodes[p as int] }); + let a = set_parents_spec(s1, kids, None); + let r = removed_prefix(d0, p, len as int); + assert(kids.subrange(0, len as int) =~= kids); + assert(kids.subrange(len as int, len as int) =~= Seq::::empty()); + assert forall|x: int| 0 <= x < d0.nodes.len() implies #[trigger] a[x] == r[x] by { + if x == p { + assert(!kids.contains(p)); + } + } + assert(a =~= r); +} + +/// An empty fragment inserts nothing: the arena is [`fragment_inserted`]. +proof fn lemma_fragment_state_empty(d0: Document, frag: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(d0, frag, parent, child), + d0.nodes[frag as int].kind is DocumentFragment, + d0.nodes[frag as int].children.len() == 0, + ensures + insert_fragment(d0, frag, parent, child) == d0, +{ + assert(d0.nodes[frag as int].children =~= Seq::::empty()); +} + +/// The arena after steps 4.1 and 7 done in bulk is [`fragment_inserted`]. +proof fn lemma_fragment_state(d0: Document, frag: NodeId, parent: NodeId, child: Option, newkids: Seq) + requires + insert_any_requires(d0, frag, parent, child), + d0.nodes[frag as int].kind is DocumentFragment, + d0.nodes[frag as int].children.len() > 0, + newkids == list_kids(d0.nodes[parent as int].children, d0.nodes[frag as int].children, child), + ensures + ({ + let nodes0 = d0.nodes[frag as int].children; + let s1 = d0.nodes.update(frag as int, Node { children: seq![], ..d0.nodes[frag as int] }); + let s2 = set_parents_spec(s1, nodes0, Some(parent)); + s2.update(parent as int, Node { children: newkids, ..s2[parent as int] }) == fragment_inserted(d0, frag, parent, child) + }), +{ + let paths = lemma_wf_witness(d0); + lemma_wf_split(d0, paths); + lemma_frag_facts(d0, paths, frag, parent, child); + let nodes0 = d0.nodes[frag as int].children; + let s1 = d0.nodes.update(frag as int, Node { children: seq![], ..d0.nodes[frag as int] }); + let s2 = set_parents_spec(s1, nodes0, Some(parent)); + let u = s2.update(parent as int, Node { children: newkids, ..s2[parent as int] }); + let b = fragment_inserted(d0, frag, parent, child); + assert(d0.nodes.len() <= max_nodes()); + assert(parent != frag); + assert(!nodes0.contains(parent)); + assert(!nodes0.contains(frag)); + assert forall|x: int| 0 <= x < d0.nodes.len() implies #[trigger] u[x] == b[x] by { + if x == frag { + assert(s2[x] == s1[x]); + } else if x == parent { + assert(s2[x] == s1[x]); + assert(s1[x] == d0.nodes[x]); + } else if nodes0.contains(x as NodeId) { + assert(s1[x] == d0.nodes[x]); + } else { + assert(s1[x] == d0.nodes[x]); + } + } + assert(u =~= b); +} + +/// Whether a kind is an element, as a boolean. +fn kind_is(k: &ExecKind, t: &NodeTag) -> (r: bool) + ensures + r == has_tag(k@, *t), +{ + match t { + NodeTag::Element => matches!(k, ExecKind::Element { .. }), + NodeTag::Text => matches!(k, ExecKind::Text { .. }), + NodeTag::DocumentType => matches!(k, ExecKind::DocumentType { .. }), + } +} + +/// A kind of a node that may have children. +fn kind_may_have_children(k: &ExecKind) -> (r: bool) + ensures + r == may_have_children(k@), +{ + matches!(k, ExecKind::Document { .. } | ExecKind::Element { .. } | ExecKind::DocumentFragment { .. }) +} + +impl Arena { + /// Set the parent of each of `ids` to `p`, in one pass. + fn set_parents(&mut self, ids: &[usize], p: Option) + requires + old(self)@.nodes.len() <= max_nodes(), + forall|j: int| 0 <= j < ids@.len() ==> (#[trigger] ids@[j]) < old(self)@.nodes.len(), + ensures + final(self)@.nodes == set_parents_spec(old(self)@.nodes, ids@, p), + { + let ghost s0 = self@.nodes; + proof { + assert(ids@.subrange(0, 0) =~= Seq::::empty()); + assert(set_parents_spec(s0, ids@.subrange(0, 0), p) =~= s0) by { + assert forall|x: int| 0 <= x < s0.len() implies #[trigger] set_parents_spec(s0, ids@.subrange(0, 0), p)[x] == s0[x] by {} + } + } + let mut i: usize = 0; + while i < ids.len() + invariant + i <= ids.len(), + s0.len() <= max_nodes(), + s0.len() == self@.nodes.len(), + forall|j: int| 0 <= j < ids@.len() ==> (#[trigger] ids@[j]) < s0.len(), + self@.nodes == set_parents_spec(s0, ids@.subrange(0, i as int), p), + decreases ids.len() - i, + { + if let Some(c) = ids.get(i) { + proof { + lemma_set_parents_step(s0, ids@, p, i as int); + } + self.set_parent(*c, p); + proof { + assert(self@.nodes =~= set_parents_spec(s0, ids@.subrange(0, i as int + 1), p)); + } + } + // Never saturates: i < ids.len(). + i = i.saturating_add(1); + } + proof { + assert(ids@.subrange(0, ids.len() as int) =~= ids@); + } + } + + /// DOM §4.2.3 "remove all of `p`'s children, in tree order", in one pass. + fn remove_children(&mut self, p: usize) + requires + wf(old(self)@), + valid(old(self)@, p), + ensures + final(self)@ == remove_children(old(self)@, p), + { + let ghost d0 = self@; + let ghost kids0 = d0.nodes[p as int].children; + proof { + lemma_children_facts(d0, p); + lemma_remove_children_n(d0, p, kids0.len()); + let paths = lemma_wf_witness(d0); + lemma_wf_split(d0, paths); + } + let kids = self.take_children(p); + self.set_parents(kids.as_slice(), None); + proof { + lemma_remove_children_state(d0, p); + assert(children(d0, p) == kids0); + assert(self@.nodes == removed_prefix(d0, p, kids0.len() as int)); + assert(self@ == remove_children(d0, p)); + } + } + + /// DOM §4.2 "next sibling". + pub fn next_sibling(&self, n: usize) -> (r: Option) + requires + wf(self@), + valid(self@, n), + ensures + r == next_sibling(self@, n), + { + let ghost paths = lemma_wf_witness(self@); + proof { + lemma_wf_split(self@, paths); + } + let p = self.node(n).parent?; + let kids = self.node(p).children.as_slice(); + match find(kids, n) { + Some(i) => { + // Never saturates: i < kids.len(). + let j = i.saturating_add(1); + if j < kids.len() { + let x = kids.get(j)?; + Some(*x) + } else { + None + } + }, + None => None, + } + } + + /// DOM §4.2 "host-including inclusive ancestor": `a` is `x` or one of + /// its ancestors, or of its root's host's (and so on). Walks from `x`, + /// at most `len` steps. + #[verifier::rlimit(50)] + pub fn is_host_including_inclusive_ancestor(&self, a: usize, x: usize) -> (r: bool) + requires + wf(self@), + valid(self@, x), + ensures + r == is_host_including_inclusive_ancestor(self@, a, x), + { + let ghost paths = lemma_wf_witness(self@); + proof { + lemma_wf_split(self@, paths); + } + let fuel = self.nodes.len(); + let mut cur = x; + let mut steps: usize = 0; + while steps <= fuel + invariant + wf_with(self@, paths), + fuel == self@.nodes.len(), + valid(self@, cur), + steps <= fuel, + host_including_within(self@, a, x, fuel as nat) == host_including_within(self@, a, cur, (fuel - steps) as nat), + decreases fuel - steps, + { + if cur == a { + return true; + } + if steps == fuel { + return false; + } + match self.node(cur).parent { + Some(p) => { + cur = p; + }, + None => match &self.node(cur).kind { + ExecKind::DocumentFragment { host: Some(h) } => { + proof { + assert(self@.nodes[cur as int].kind is DocumentFragment); + } + cur = *h; + }, + _ => return false, + }, + } + // Never saturates: steps < fuel. + steps = steps.saturating_add(1); + } + false + } + + /// How many of `kids[lo..hi]` have the tag `t`, other than `skip` + /// ([`count_tag`]). + fn count_tag(&self, kids: &[usize], lo: usize, hi: usize, t: NodeTag, skip: Option) -> (r: usize) + requires + lo <= hi <= kids@.len(), + forall|j: int| 0 <= j < kids@.len() ==> (#[trigger] kids@[j]) < self@.nodes.len(), + ensures + r as nat == count_tag(self@, kids@.subrange(lo as int, hi as int), t, skip), + { + let mut count: usize = 0; + let mut i: usize = lo; + proof { + assert(kids@.subrange(lo as int, lo as int) =~= Seq::::empty()); + } + while i < hi + invariant + lo <= i <= hi, + hi <= kids@.len(), + forall|j: int| 0 <= j < kids@.len() ==> (#[trigger] kids@[j]) < self@.nodes.len(), + count <= i - lo, + count as nat == count_tag(self@, kids@.subrange(lo as int, i as int), t, skip), + decreases hi - i, + { + if let Some(c) = kids.get(i) { + proof { + let sub = kids@.subrange(lo as int, i as int + 1); + assert(sub.drop_last() =~= kids@.subrange(lo as int, i as int)); + assert(sub.last() == kids@[i as int]); + } + if kind_is(self.kind(*c), &t) && skip != Some(*c) { + // Never saturates: count <= i - lo < hi. + count = count.saturating_add(1); + } + } + // Never saturates: i < hi. + i = i.saturating_add(1); + } + count + } +} + +impl Arena { + /// The statements of step 6 of "ensure pre-insertion validity" about a + /// document `parent` ([`pre_insert_document_violation`]). + fn pre_insert_document_violation(&self, node: usize, parent: usize, child: Option) -> (r: bool) + requires + wf(self@), + valid(self@, node), + valid(self@, parent), + match child { + None => true, + Some(c) => valid(self@, c) && self@.nodes[c as int].parent == Some(parent), + }, + ensures + r == pre_insert_document_violation(self@, node, parent, child), + { + let ghost d = self@; + proof { + lemma_children_facts(d, parent); + lemma_children_facts(d, node); + } + let kids = self.children(parent); + let len = kids.len(); + let mut after: usize = 0; + let mut before: usize = 0; + let mut child_is_doctype = false; + if let Some(c) = child { + proof { + let paths = lemma_wf_witness(d); + lemma_wf_split(d, paths); + assert(d.nodes[parent as int].children.contains(c)); + lemma_index_of(kids@, c); + } + let i = match find(kids, c) { + Some(i) => i, + None => { + proof { + assert(false); + } + return false; + }, + }; + // Never overflows: i < len. + let next = i.saturating_add(1); + after = self.count_tag(kids, next, len, NodeTag::DocumentType, None); + before = self.count_tag(kids, 0, i, NodeTag::Element, None); + child_is_doctype = kind_is(self.kind(c), &NodeTag::DocumentType); + } + proof { + assert(kids@.subrange(0, len as int) =~= kids@); + } + match self.kind(node) { + ExecKind::DocumentFragment { .. } => { + let nk = self.children(node); + let nlen = nk.len(); + proof { + assert(nk@.subrange(0, nlen as int) =~= nk@); + } + let elements = self.count_tag(nk, 0, nlen, NodeTag::Element, None); + let texts = self.count_tag(nk, 0, nlen, NodeTag::Text, None); + if elements > 1 || texts > 0 { + return true; + } + if elements == 1 { + let pe = self.count_tag(kids, 0, len, NodeTag::Element, None); + return pe > 0 || child_is_doctype || after > 0; + } + false + }, + ExecKind::Element { .. } => { + let pe = self.count_tag(kids, 0, len, NodeTag::Element, None); + pe > 0 || child_is_doctype || after > 0 + }, + ExecKind::DocumentType { .. } => { + let pd = self.count_tag(kids, 0, len, NodeTag::DocumentType, None); + if pd > 0 || before > 0 { + return true; + } + if child.is_none() { + let pe = self.count_tag(kids, 0, len, NodeTag::Element, None); + return pe > 0; + } + false + }, + _ => false, + } + } + + /// DOM §4.2.3† "ensure pre-insertion validity" + /// ([`ensure_pre_insertion_validity_ensures`]). + pub fn ensure_pre_insertion_validity(&self, node: usize, parent: usize, child: Option) -> (r: Option) + requires + pre_insert_requires(self@, node, parent, child), + ensures + ensure_pre_insertion_validity_ensures(self@, node, parent, child, r), + { + let ghost d = self@; + let pk = self.kind(parent); + // 1. + if !kind_may_have_children(pk) { + return Some(DomError::HierarchyRequest); + } + // 2. + if self.is_host_including_inclusive_ancestor(node, parent) { + return Some(DomError::HierarchyRequest); + } + // 3. + let not_found = match child { + Some(c) => self.node(c).parent != Some(parent), + None => false, + }; + if not_found { + return Some(DomError::NotFound); + } + let nk = self.kind(node); + // 4. + if let ExecKind::Document { .. } = nk { + return Some(DomError::HierarchyRequest); + } + // 5. + let parent_is_document = matches!(pk, ExecKind::Document { .. }); + let node_is_text = kind_is(nk, &NodeTag::Text); + let node_is_doctype = kind_is(nk, &NodeTag::DocumentType); + if (node_is_text && parent_is_document) || (node_is_doctype && !parent_is_document) { + return Some(DomError::HierarchyRequest); + } + // 6. + if parent_is_document && self.pre_insert_document_violation(node, parent, child) { + return Some(DomError::HierarchyRequest); + } + None + } + + /// The statements of step 6 of "replace" about a document `parent` + /// ([`replace_document_violation`]). + fn replace_document_violation(&self, child: usize, node: usize, parent: usize) -> (r: bool) + requires + wf(self@), + valid(self@, node), + valid(self@, parent), + valid(self@, child), + self@.nodes[child as int].parent == Some(parent), + ensures + r == replace_document_violation(self@, child, node, parent), + { + let ghost d = self@; + proof { + lemma_children_facts(d, parent); + lemma_children_facts(d, node); + let paths = lemma_wf_witness(d); + lemma_wf_split(d, paths); + assert(d.nodes[parent as int].children.contains(child)); + lemma_index_of(d.nodes[parent as int].children, child); + } + let kids = self.children(parent); + let len = kids.len(); + let i = match find(kids, child) { + Some(i) => i, + None => { + proof { + assert(false); + } + return false; + }, + }; + // Never overflows: i < len. + let next = i.saturating_add(1); + let after = self.count_tag(kids, next, len, NodeTag::DocumentType, None); + let before = self.count_tag(kids, 0, i, NodeTag::Element, None); + proof { + assert(kids@.subrange(0, len as int) =~= kids@); + } + match self.kind(node) { + ExecKind::DocumentFragment { .. } => { + let nk = self.children(node); + let nlen = nk.len(); + proof { + assert(nk@.subrange(0, nlen as int) =~= nk@); + } + let elements = self.count_tag(nk, 0, nlen, NodeTag::Element, None); + let texts = self.count_tag(nk, 0, nlen, NodeTag::Text, None); + if elements > 1 || texts > 0 { + return true; + } + if elements == 1 { + let pe = self.count_tag(kids, 0, len, NodeTag::Element, Some(child)); + return pe > 0 || after > 0; + } + false + }, + ExecKind::Element { .. } => { + let pe = self.count_tag(kids, 0, len, NodeTag::Element, Some(child)); + pe > 0 || after > 0 + }, + ExecKind::DocumentType { .. } => { + let pd = self.count_tag(kids, 0, len, NodeTag::DocumentType, Some(child)); + pd > 0 || before > 0 + }, + _ => false, + } + } + + /// Steps 1 to 6 of DOM §4.2.3† "replace" ([`replace_error`]). + fn replace_validity(&self, child: usize, node: usize, parent: usize) -> (r: Option) + requires + replace_requires(self@, child, node, parent), + ensures + r == replace_error(self@, child, node, parent), + { + let pk = self.kind(parent); + // 1. + if !kind_may_have_children(pk) { + return Some(DomError::HierarchyRequest); + } + // 2. + if self.is_host_including_inclusive_ancestor(node, parent) { + return Some(DomError::HierarchyRequest); + } + // 3. + if self.node(child).parent != Some(parent) { + return Some(DomError::NotFound); + } + let nk = self.kind(node); + // 4. + if let ExecKind::Document { .. } = nk { + return Some(DomError::HierarchyRequest); + } + // 5. + let parent_is_document = matches!(pk, ExecKind::Document { .. }); + let node_is_text = kind_is(nk, &NodeTag::Text); + let node_is_doctype = kind_is(nk, &NodeTag::DocumentType); + if (node_is_text && parent_is_document) || (node_is_doctype && !parent_is_document) { + return Some(DomError::HierarchyRequest); + } + // 6. + if parent_is_document && self.replace_document_violation(child, node, parent) { + return Some(DomError::HierarchyRequest); + } + None + } + + /// DOM §4.2.3 "insert" a `DocumentFragment` ([`insert_fragment`]). + /// Linear in the number of moved children. + fn insert_fragment(&mut self, frag: usize, parent: usize, child: Option) + requires + insert_any_requires(old(self)@, frag, parent, child), + old(self)@.nodes[frag as int].kind is DocumentFragment, + ensures + final(self)@ == insert_fragment(old(self)@, frag, parent, child), + { + let ghost d0 = self@; + let ghost nodes0 = d0.nodes[frag as int].children; + let ghost kids0 = d0.nodes[parent as int].children; + proof { + lemma_insert_fragment(d0, frag, parent, child); + lemma_children_facts(d0, frag); + let paths = lemma_wf_witness(d0); + lemma_wf_split(d0, paths); + } + // 3. + if self.node(frag).children.is_empty() { + proof { + lemma_fragment_state_empty(d0, frag, parent, child); + } + return ; + } + // 4.1 and 7., in bulk. + let mut moved = self.take_children(frag); + self.set_parents(moved.as_slice(), Some(parent)); + let mut kids = self.take_children(parent); + match child { + None => { + kids.append(&mut moved); + }, + Some(c) => { + proof { + lemma_index_of(kids0, c); + } + let i = match find(kids.as_slice(), c) { + Some(i) => i, + None => { + proof { + assert(false); + } + return ; + }, + }; + let mut tail = kids.split_off(i); + kids.append(&mut moved); + kids.append(&mut tail); + }, + } + proof { + match child { + None => { + assert(kids@ == kids0 + nodes0); + }, + Some(c) => { + let i = index_of(kids0, c); + assert(kids@ == kids0.subrange(0, i) + nodes0 + kids0.subrange(i, kids0.len() as int)); + }, + } + assert(kids@ == list_kids(kids0, nodes0, child)); + } + self.set_children(parent, kids); + proof { + lemma_fragment_state(d0, frag, parent, child, kids@); + assert(self@.nodes == fragment_inserted(d0, frag, parent, child)); + assert(self@ == insert_fragment(d0, frag, parent, child)); + } + } + + /// DOM §4.2.3 "insert" for any node ([`insert_any`]). + fn insert_any(&mut self, node: usize, parent: usize, child: Option) + requires + insert_any_requires(old(self)@, node, parent, child), + ensures + final(self)@ == insert_any(old(self)@, node, parent, child), + { + let is_fragment = matches!(self.kind(node), ExecKind::DocumentFragment { .. }); + if is_fragment { + self.insert_fragment(node, parent, child); + } else { + match child { + None => self.append(parent, node), + Some(c) => self.insert_before(parent, node, c), + } + } + } + + /// DOM §4.2.3† "pre-insert" ([`pre_insert_ensures`]). + pub fn pre_insert(&mut self, node: usize, parent: usize, child: Option) -> (err: Option) + requires + pre_insert_requires(old(self)@, node, parent, child), + ensures + pre_insert_ensures(old(self)@, node, parent, child, final(self)@, err), + { + let ghost d0 = self@; + // 1. + let err = self.ensure_pre_insertion_validity(node, parent, child); + if err.is_some() { + return err; + } + proof { + lemma_pre_insert_valid(d0, node, parent, child); + } + // 2. and 3. + let reference_child = if child == Some(node) { + self.next_sibling(node) + } else { + child + }; + // 4. + self.insert_any(node, parent, reference_child); + None + } + + /// DOM §4.2.3† "replace" ([`replace_ensures`]). + pub fn replace(&mut self, child: usize, node: usize, parent: usize) -> (err: Option) + requires + replace_requires(old(self)@, child, node, parent), + ensures + replace_ensures(old(self)@, child, node, parent, final(self)@, err), + { + let ghost d0 = self@; + // 1. to 6. + let err = self.replace_validity(child, node, parent); + if err.is_some() { + return err; + } + proof { + lemma_replace_valid(d0, child, node, parent); + assert(d0.nodes[child as int].parent == Some(parent)); + } + // 7. + let reference0 = self.next_sibling(child); + // 8. + let reference_child = if reference0 == Some(node) { + self.next_sibling(node) + } else { + reference0 + }; + // 11. + self.remove(child); + // 13. + self.insert_any(node, parent, reference_child); + None + } + + /// DOM §4.2.3† "replace all" ([`replace_all_ensures`]). + pub fn replace_all(&mut self, node: Option, parent: usize) + requires + replace_all_requires(old(self)@, node, parent), + ensures + replace_all_ensures(old(self)@, node, parent, final(self)@), + { + let ghost d0 = self@; + proof { + lemma_replace_all_valid(d0, node, parent); + } + // 5. + self.remove_children(parent); + // 6. + if let Some(n) = node { + self.insert_any(n, parent, None); + } + } +} + +// --------------------------------------------------------------------- +// Copies of kinds (for "clone a node") + +/// A copy of a string. +fn copy_str(s: &Vec) -> (r: Vec) + ensures + r@ == s@, +{ + vstd::slice::slice_to_vec(s.as_slice()) +} + +/// A copy of an optional string. +fn copy_opt_str(s: &Option>) -> (r: Option>) + ensures + opt_str_view(r) == opt_str_view(*s), +{ + match s { + None => None, + Some(v) => { + let c = copy_str(v); + Some(c) + }, + } +} + +/// A copy of an optional namespace. +fn copy_ns(n: &Option) -> (r: Option) + ensures + r == *n, +{ + match n { + None => None, + Some(Namespace::Html) => Some(Namespace::Html), + Some(Namespace::MathMl) => Some(Namespace::MathMl), + Some(Namespace::Svg) => Some(Namespace::Svg), + Some(Namespace::XLink) => Some(Namespace::XLink), + Some(Namespace::Xml) => Some(Namespace::Xml), + Some(Namespace::Xmlns) => Some(Namespace::Xmlns), + } +} + +/// A copy of a document mode. +fn copy_mode(m: &DocumentMode) -> (r: DocumentMode) + ensures + r == *m, +{ + match m { + DocumentMode::NoQuirks => DocumentMode::NoQuirks, + DocumentMode::Quirks => DocumentMode::Quirks, + DocumentMode::LimitedQuirks => DocumentMode::LimitedQuirks, + } +} + +/// A copy of an attribute ("clone an attribute"). +fn copy_attr(a: &ExecAttr) -> (r: ExecAttr) + ensures + r@ == a@, +{ + ExecAttr { namespace: copy_ns(&a.namespace), prefix: copy_opt_str(&a.prefix), local_name: copy_str(&a.local_name), value: copy_str(&a.value) } +} + +/// A copy of an attribute list. +fn copy_attrs(v: &[ExecAttr]) -> (r: Vec) + ensures + attrs_view(r@) == attrs_view(v@), +{ + let mut out: Vec = Vec::new(); + let mut i: usize = 0; + while i < v.len() + invariant + i <= v.len(), + out@.len() == i, + forall|j: int| 0 <= j < i ==> (#[trigger] out@[j])@ == v@[j]@, + decreases v.len() - i, + { + if let Some(a) = v.get(i) { + out.push(copy_attr(a)); + } + // Never saturates: i < v.len(). + i = i.saturating_add(1); + } + proof { + assert(attrs_view(out@) =~= attrs_view(v@)); + } + out +} + +/// The kind of the copy "clone a node" makes ([`clone_kind`]). +fn clone_kind_exec(k: &ExecKind) -> (r: ExecKind) + ensures + r@ == clone_kind(k@), +{ + match k { + ExecKind::Document { mode, url, base_url } => ExecKind::Document { mode: copy_mode(mode), url: copy_str(url), base_url: copy_str(base_url) }, + ExecKind::DocumentType { name, public_id, system_id } => ExecKind::DocumentType { + name: copy_str(name), + public_id: copy_str(public_id), + system_id: copy_str(system_id), + }, + ExecKind::Element { namespace, prefix, local_name, attributes, .. } => ExecKind::Element { + namespace: copy_ns(namespace), + prefix: copy_opt_str(prefix), + local_name: copy_str(local_name), + attributes: copy_attrs(attributes), + template_contents: None, + form_owner: None, + }, + ExecKind::Text { data } => ExecKind::Text { data: copy_str(data) }, + ExecKind::Comment { data } => ExecKind::Comment { data: copy_str(data) }, + ExecKind::ProcessingInstruction { target, data } => ExecKind::ProcessingInstruction { target: copy_str(target), data: copy_str(data) }, + ExecKind::DocumentFragment { .. } => ExecKind::DocumentFragment { host: None }, + } +} + +/// A template element ([`is_template`]). +fn is_template_exec(k: &ExecKind) -> (r: bool) + ensures + r == is_template(k@), +{ + matches!(k, ExecKind::Element { template_contents: Some(_), .. }) +} + +/// A template's contents, if `k` is a template ([`is_template`]). +fn template_contents_exec(k: &ExecKind) -> (r: Option) + ensures + is_template(k@) ==> r == Some(k@->Element_template_contents->Some_0), + !is_template(k@) ==> r is None, +{ + match k { + ExecKind::Element { template_contents: Some(f), .. } => Some(*f), + _ => None, + } +} + +impl Arena { + /// Cut the arena back to its first `len` nodes (a clone that does not + /// fit is rolled back with it). + fn truncate(&mut self, len: usize) + requires + len <= old(self)@.nodes.len(), + ensures + final(self)@ == (Document { nodes: old(self)@.nodes.take(len as int) }), + { + let ghost s0 = self.nodes@; + self.nodes.truncate(len); + proof { + assert(self.nodes@ =~= s0.take(len as int)); + assert(nodes_view(self.nodes@) =~= nodes_view(s0).take(len as int)); + } + } + + /// One step of [`Arena::clone_deep`]: clone the `j`th child of + /// `order[i]`, append the copy to node `base + i`, and list the source + /// (and a template's contents). If the copy does not fit, the arena is + /// cut back to its old length and the result is false. + #[verifier::rlimit(100)] + fn deep_step(&mut self, order: &mut Vec, base: usize, i: usize, j: usize, Ghost(d0): Ghost, Ghost(node): Ghost) -> (ok: bool) + requires + base == d0.nodes.len(), + deep_inv(d0, node, old(self)@, old(order)@, i as int, j as int), + i < old(order).len(), + j < d0.nodes[old(order)@[i as int] as int].children.len(), + host_acyclic(d0), + ensures + ok ==> deep_inv(d0, node, final(self)@, final(order)@, i as int, j as int + 1), + ok ==> final(self)@.nodes.len() == base + final(order).len(), + ok ==> forall|k: int| 0 <= k < old(order).len() ==> #[trigger] final(order)@[k] == old(order)@[k], + !ok ==> final(self)@ == d0, + !ok ==> !can_create(d0, subtree_size(d0, node)), + { + let ghost a = self@; + let ghost order0 = order@; + proof { + lemma_inv_facts(d0, node, a, order0, i as int, j as int); + assert(a.nodes[order0[i as int] as int] == d0.nodes[order0[i as int] as int]); + } + // In bounds by the precondition; `get` keeps the code free of panics. + let s = match order.get(i) { + Some(s) => *s, + None => { + proof { + assert(false); + } + return false; + }, + }; + let ch = match self.children(s).get(j) { + Some(c) => *c, + None => { + proof { + assert(false); + } + return false; + }, + }; + proof { + lemma_child_requires(d0, node, a, order0, i as int, j as int, ch); + } + match self.clone_node(ch) { + None => { + proof { + assert(!can_create(a, clone_count(a, ch))); + lemma_step_fits(d0, node, a, order0, i as int, j as int, ch); + assert(a.nodes[ch as int] == d0.nodes[ch as int]); + lemma_deep_rollback(d0, node, self@, order0, i as int, j as int); + } + self.truncate(base); + false + }, + Some(cc) => { + proof { + assert(can_create(a, clone_count(a, ch))); + lemma_step_requires(d0, node, a, order0, i as int, j as int, ch); + lemma_step_inv(d0, node, a, order0, i as int, j as int, ch); + } + // base + i < self.len() <= MAX_NODES: never saturates. + self.append(base.saturating_add(i), cc); + proof { + assert(self@.nodes[ch as int] == a.nodes[ch as int]); + } + order.push(ch); + if let Some(f) = template_contents_exec(self.kind(ch)) { + order.push(f); + } + proof { + assert(order@ =~= order_after(d0, order0, ch)); + } + true + }, + } + } + + /// DOM §4.4† "clone a node" with the clone children flag set + /// ([`clone_deep_ensures`]): a worklist over the source's host-including + /// subtree. Node `base + i` is the copy of `order[i]`; the cursor `(i, + /// j)` is at the `j`th child of `order[i]`, which is cloned (shallowly, + /// with its template contents) and appended to node `base + i`, and + /// listed. Nothing recurses. If a clone does not fit, the arena is cut + /// back to its old length. + #[verifier::rlimit(20)] + pub fn clone_deep(&mut self, node: usize) -> (r: Option) + requires + clone_deep_requires(old(self)@, node), + ensures + clone_deep_ensures(old(self)@, node, final(self)@, r), + { + let ghost d0 = self@; + let base = self.nodes.len(); + let root = match self.clone_node(node) { + Some(root) => root, + None => { + proof { + lemma_root_fits(d0, node); + } + return None; + }, + }; + proof { + assert(can_create(d0, clone_count(d0, node))); + assert(root == d0.nodes.len()); + lemma_deep_init(d0, node); + lemma_inv_facts(d0, node, self@, order_after(d0, Seq::empty(), node), 0, 0); + lemma_wf_bound(self@); + } + let mut order: Vec = Vec::new(); + order.push(node); + if let Some(f) = template_contents_exec(self.kind(node)) { + order.push(f); + } + proof { + assert(order@ =~= order_after(d0, Seq::empty(), node)); + lemma_inv_facts(d0, node, self@, order@, 0, 0); + assert(self@.nodes[node as int] == d0.nodes[node as int]); + } + let mut i: usize = 0; + let mut j: usize = 0; + let mut nch: usize = self.children(node).len(); + while i < order.len() + invariant + d0 == old(self)@, + base == d0.nodes.len(), + host_acyclic(d0), + deep_inv(d0, node, self@, order@, i as int, j as int), + i < order.len() ==> nch == d0.nodes[order@[i as int] as int].children.len(), + i < order.len() ==> j <= nch, + i <= order.len(), + self@.nodes.len() == base + order.len(), + self@.nodes.len() <= MAX_NODES, + decreases MAX_NODES - i, nch - j, + { + if j < nch { + if !self.deep_step(&mut order, base, i, j, Ghost(d0), Ghost(node)) { + proof { + assert(self@ == d0); + assert(d0 == old(self)@); + } + return None; + } + // j < nch <= the node count: never saturates. + j = j.saturating_add(1); + } else { + proof { + lemma_advance(d0, node, self@, order@, i as int, j as int); + } + // i < order.len(): never saturates. + i = i.saturating_add(1); + j = 0; + match order.get(i) { + Some(s) => { + proof { + lemma_inv_facts(d0, node, self@, order@, i as int, 0); + assert(self@.nodes[*s as int] == d0.nodes[*s as int]); + } + nch = self.children(*s).len(); + }, + None => { + nch = 0; + }, + } + } + proof { + lemma_inv_facts(d0, node, self@, order@, i as int, j as int); + lemma_wf_bound(self@); + } + } + proof { + lemma_deep_done(d0, node, self@, order@, i as int, j as int); + theorem_deep_clone_size(d0, node, self@, order@); + } + Some(root) + } + + /// DOM §4.4† "clone a node", shallow ([`clone_node_ensures`]). + pub fn clone_node(&mut self, node: usize) -> (r: Option) + requires + clone_requires(old(self)@, node), + ensures + clone_node_ensures(old(self)@, node, final(self)@, r), + { + let ghost d0 = self@; + // 2. and 3. + let kind = clone_kind_exec(self.kind(node)); + let template = is_template_exec(self.kind(node)); + let count: usize = if template { + 2 + } else { + 1 + }; + if self.nodes.len().saturating_add(count) > MAX_NODES { + return None; + } + proof { + let k = d0.nodes[node as int].kind; + assert(creatable(d0, clone_kind(k))); + } + let id = self.create(kind)?; + // 5. + if template { + proof { + let k = d0.nodes[node as int].kind; + theorem_create_node_wf(d0, clone_kind(k)); + } + let _contents = self.create_template_contents(id); + } + Some(id) + } + + /// DOM §4.9† "change an attribute" ([`change_attribute_ensures`]). + pub fn change_attribute(&mut self, e: usize, i: usize, value: Vec) + requires + attribute_requires(old(self)@, e, i as nat), + ensures + change_attribute_ensures(old(self)@, e, i as nat, value@, final(self)@), + { + let ghost s0 = self.nodes@; + let ghost v = value@; + let mut y = self.take(e); + let mut kind = ExecKind::DocumentFragment { host: None }; + core::mem::swap(&mut y.kind, &mut kind); + y.kind = match kind { + ExecKind::Element { namespace, prefix, local_name, mut attributes, template_contents, form_owner } => { + let ghost before = attributes@; + let mut old_attr = ExecAttr { namespace: None, prefix: None, local_name: Vec::new(), value: Vec::new() }; + attributes.set_and_swap(i, &mut old_attr); + old_attr.value = value; + attributes.set(i, old_attr); + proof { + assert(attrs_view(attributes@) =~= attrs_view(before).update(i as int, Attr { value: v, ..attrs_view(before)[i as int] })); + } + ExecKind::Element { namespace, prefix, local_name, attributes, template_contents, form_owner } + }, + other => other, + }; + self.put(e, y, Ghost(s0)); + proof { + lemma_view_update(s0, e as int, y); + assert(self@.nodes =~= change_attribute(old(self)@, e, i as nat, v).nodes); + } + } + + /// DOM §4.9† "remove an attribute" ([`remove_attribute_ensures`]). + pub fn remove_attribute(&mut self, e: usize, i: usize) + requires + attribute_requires(old(self)@, e, i as nat), + ensures + remove_attribute_ensures(old(self)@, e, i as nat, final(self)@), + { + let ghost s0 = self.nodes@; + let mut y = self.take(e); + let mut kind = ExecKind::DocumentFragment { host: None }; + core::mem::swap(&mut y.kind, &mut kind); + y.kind = match kind { + ExecKind::Element { namespace, prefix, local_name, mut attributes, template_contents, form_owner } => { + let ghost before = attributes@; + let _removed = attributes.remove(i); + proof { + assert(attrs_view(attributes@) =~= attrs_view(before).remove(i as int)); + } + ExecKind::Element { namespace, prefix, local_name, attributes, template_contents, form_owner } + }, + other => other, + }; + self.put(e, y, Ghost(s0)); + proof { + lemma_view_update(s0, e as int, y); + assert(self@.nodes =~= remove_attribute(old(self)@, e, i as nat).nodes); + } + } + + /// DOM §4.9† "replace an attribute" ([`replace_attribute_ensures`]). + pub fn replace_attribute(&mut self, e: usize, i: usize, new_attr: ExecAttr) + requires + attribute_requires(old(self)@, e, i as nat), + ensures + replace_attribute_ensures(old(self)@, e, i as nat, new_attr@, final(self)@), + { + let ghost s0 = self.nodes@; + let ghost a = new_attr@; + let mut y = self.take(e); + let mut kind = ExecKind::DocumentFragment { host: None }; + core::mem::swap(&mut y.kind, &mut kind); + y.kind = match kind { + ExecKind::Element { namespace, prefix, local_name, mut attributes, template_contents, form_owner } => { + let ghost before = attributes@; + attributes.set(i, new_attr); + proof { + assert(attrs_view(attributes@) =~= attrs_view(before).update(i as int, a)); + } + ExecKind::Element { namespace, prefix, local_name, attributes, template_contents, form_owner } + }, + other => other, + }; + self.put(e, y, Ghost(s0)); + proof { + lemma_view_update(s0, e as int, y); + assert(self@.nodes =~= replace_attribute(old(self)@, e, i as nat, a).nodes); + } + } +} + } // verus! diff --git a/crates/clazel-dom/src/host_acyclic_contract.rs b/crates/clazel-dom/src/host_acyclic_contract.rs new file mode 100644 index 000000000..2d04fd276 --- /dev/null +++ b/crates/clazel-dom/src/host_acyclic_contract.rs @@ -0,0 +1,45 @@ +//! What the raw operations need to keep [`host_acyclic`]. Every mutation of +//! the tree keeps [`wf`] (`contract`), and also keeps [`host_acyclic`], the +//! one thing [`wf`] does not say (no cycle through a template's host), +//! proven about the spec in `host_acyclic_lemmas`. Two raw operations need a +//! precondition beyond their `wf` one for that: [`insert_acyclic_requires`] +//! and [`reparent_children_acyclic_requires`], DOM's "host-including +//! inclusive ancestor" check (§4.2.3† "ensure pre-insertion validity", step +//! 2) in place of the inclusive-ancestor check the parser's calls carry. +//! Every operation that makes that check itself (pre-insert, replace) needs +//! nothing more than [`host_acyclic`] of its input. +//! +//! (This is a module of its own, and not part of `contract`, which other +//! proofs import: an unrelated spec function there changes what the solver +//! does with the failing proofs of their mutations.) + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// What `Arena::append`, `Arena::insert_before` and the spec's [`insert`] need +/// to keep [`host_acyclic`] (`host_acyclic_lemmas::theorem_insert_host_acyclic`): +/// [`insert_requires`] on a [`host_acyclic`] arena, where `node` is not a +/// host-including inclusive ancestor of `parent` (DOM §4.2.3† "ensure +/// pre-insertion validity" step 2; [`insert_requires`] only has the +/// inclusive-ancestor half of it). Without the host half, a template +/// inserted below a node of its own contents makes a cycle through the host. +pub open spec fn insert_acyclic_requires(doc: Document, node: NodeId, parent: NodeId, child: Option) -> bool { + &&& insert_requires(doc, node, parent, child) + &&& host_acyclic(doc) + &&& !is_host_including_inclusive_ancestor(doc, node, parent) +} + +/// What `Arena::reparent_children(from, to)` needs to keep [`host_acyclic`]: +/// [`reparent_children_requires`] on a [`host_acyclic`] arena, where `from` +/// is not a host-including inclusive ancestor of `to`. +pub open spec fn reparent_children_acyclic_requires(doc: Document, from: NodeId, to: NodeId) -> bool { + &&& reparent_children_requires(doc, from, to) + &&& host_acyclic(doc) + &&& !is_host_including_inclusive_ancestor(doc, from, to) +} + +} // verus! diff --git a/crates/clazel-dom/src/host_acyclic_lemmas.rs b/crates/clazel-dom/src/host_acyclic_lemmas.rs new file mode 100644 index 000000000..2d8b486f9 --- /dev/null +++ b/crates/clazel-dom/src/host_acyclic_lemmas.rs @@ -0,0 +1,1437 @@ +//! Every operation keeps [`host_acyclic`]: no cycle through parents and a +//! template's host. [`wf`] forbids a cycle of parents only (its `paths` +//! follow parents), so a template that is a child of its own contents is +//! well formed but not host-acyclic, and the unfolding of its subtree is +//! infinite (`clone_deep`'s precondition, `contract::clone_deep_requires`). +//! The theorems here say that DOM's algorithms never build one, so +//! [`host_acyclic`] is an invariant of the tree: a caller that starts from +//! [`new_document`] and uses only the operations below has it for free. +//! +//! The key fact is the "host-including inclusive ancestor" check of DOM +//! §4.2.3† "ensure pre-insertion validity", step 2 (and "replace", step 2), +//! which exists to forbid such cycles. The raw [`insert`] and +//! [`reparent_children`], which the parser calls after its own checks, carry +//! only [`insert_requires`]'s inclusive-ancestor half of it, which is not +//! enough (a template inserted below a node of its own contents has +//! `!is_inclusive_ancestor(node, parent)`); they need the host half as well +//! (`host_acyclic_contract::insert_acyclic_requires`, +//! `host_acyclic_contract::reparent_children_acyclic_requires`). Pre-insert, replace and +//! replace all make the check themselves. +//! +//! Everything rests on one lemma, [`lemma_graft`]: if some nodes are given +//! the new parent `p` (and no other link is added), none of them a +//! host-including inclusive ancestor of `p`, a ranking of the old arena +//! (parents and hosts rank below their children) is changed into one of the +//! new arena by raising the rank of everything below a grafted node above +//! `p`'s rank. That ranking is not below the node count any more; +//! [`lemma_ranked_u_acyclic`] compresses it (a node's compressed rank is the +//! number of nodes ranked below it). + +#[cfg(verus_only)] +use super::contract::*; +#[cfg(verus_only)] +use super::deep_clone_lemmas::*; +#[cfg(verus_only)] +use super::deep_clone_size_lemmas::*; +#[cfg(verus_only)] +use super::host_acyclic_contract::*; +#[cfg(verus_only)] +use super::lemmas::*; +#[cfg(verus_only)] +use super::ops_lemmas::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// --------------------------------------------------------------------- +// Rankings that need not be below the node count + +/// A ranking of the arena's nodes that grows along every parent edge and +/// every host edge, at node `i`: like [`ranked_at`], but its ranks are not +/// bounded by the node count. +pub open spec fn ranked_u_at(doc: Document, rank: Seq, i: int) -> bool { + let n = doc.nodes.len(); + let node = doc.nodes[i]; + &&& node.parent is Some ==> node.parent->Some_0 < n && rank[node.parent->Some_0 as int] < rank[i] + &&& (node.kind is DocumentFragment && node.kind->DocumentFragment_host is Some) ==> { + let h = node.kind->DocumentFragment_host->Some_0; + h < n && rank[h as int] < rank[i] + } +} + +/// `rank` ranks every node of the arena ([`ranked_u_at`]). +pub open spec fn ranked_u(doc: Document, rank: Seq) -> bool { + &&& rank.len() == doc.nodes.len() + &&& forall|i: int| 0 <= i < doc.nodes.len() ==> #[trigger] ranked_u_at(doc, rank, i) +} + +/// How many of the first `k` entries of `rank` are below `b`. +pub open spec fn rank_below(rank: Seq, k: nat, b: nat) -> nat + decreases k, +{ + if k == 0 { + 0 + } else { + rank_below(rank, (k - 1) as nat, b) + (if rank[(k - 1) as int] < b { + 1nat + } else { + 0nat + }) + } +} + +/// At most `k`. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_rank_below_le(rank: Seq, k: nat, b: nat) + ensures + rank_below(rank, k, b) <= k, + decreases k, +{ + if k > 0 { + lemma_rank_below_le(rank, (k - 1) as nat, b); + } +} + +/// A higher bound counts at least as many. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_rank_below_mono(rank: Seq, k: nat, b1: nat, b2: nat) + requires + b1 <= b2, + ensures + rank_below(rank, k, b1) <= rank_below(rank, k, b2), + decreases k, +{ + if k > 0 { + lemma_rank_below_mono(rank, (k - 1) as nat, b1, b2); + } +} + +/// An entry between the bounds makes the higher bound count more. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_rank_below_strict(rank: Seq, k: nat, b1: nat, b2: nat, j: int) + requires + k <= rank.len(), + 0 <= j < k, + b1 <= rank[j] < b2, + ensures + rank_below(rank, k, b1) < rank_below(rank, k, b2), + decreases k, +{ + if j == k - 1 { + lemma_rank_below_mono(rank, (k - 1) as nat, b1, b2); + } else { + lemma_rank_below_strict(rank, (k - 1) as nat, b1, b2, j); + } +} + +/// An entry at least `b` keeps the count below `k`. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_rank_below_lt(rank: Seq, k: nat, b: nat, j: int) + requires + k <= rank.len(), + 0 <= j < k, + b <= rank[j], + ensures + rank_below(rank, k, b) < k, + decreases k, +{ + if j == k - 1 { + lemma_rank_below_le(rank, (k - 1) as nat, b); + } else { + lemma_rank_below_lt(rank, (k - 1) as nat, b, j); + } +} + +/// A ranking with ranks that need not be below the node count gives one +/// that is: a node's new rank is how many nodes are ranked below it. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn lemma_ranked_u_acyclic(doc: Document, rank: Seq) + requires + ranked_u(doc, rank), + ensures + host_acyclic(doc), +{ + let n = doc.nodes.len(); + let cr = Seq::new(n, |i: int| rank_below(rank, n as nat, rank[i])); + assert forall|i: int| 0 <= i < n implies #[trigger] ranked_at(doc, cr, i) by { + assert(ranked_u_at(doc, rank, i)); + lemma_rank_below_lt(rank, n as nat, rank[i], i); + let node = doc.nodes[i]; + if node.parent is Some { + let q = node.parent->Some_0 as int; + lemma_rank_below_strict(rank, n as nat, rank[q], rank[i], q); + } + if node.kind is DocumentFragment && node.kind->DocumentFragment_host is Some { + let h = node.kind->DocumentFragment_host->Some_0 as int; + lemma_rank_below_strict(rank, n as nat, rank[h], rank[i], h); + } + } + assert(host_ranked(doc, cr)); +} + +/// What [`wf`] gives of a fragment, which is all the lemmas below need of +/// it: a fragment has no parent. +pub open spec fn frag_parentless(doc: Document) -> bool { + forall|i: int| 0 <= i < doc.nodes.len() && (#[trigger] doc.nodes[i]).kind is DocumentFragment ==> doc.nodes[i].parent is None +} + +/// A well-formed arena is [`frag_parentless`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn lemma_wf_frag_parentless(doc: Document) + requires + wf(doc), + ensures + frag_parentless(doc), +{ + lemma_wf_facts(doc); +} + +// --------------------------------------------------------------------- +// Facts about host-including ancestors under a ranking + +/// A host-including inclusive ancestor of a host-including inclusive +/// ancestor of `x` is one of `x`. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn lemma_hi_trans(doc: Document, rank: Seq, a: NodeId, b: NodeId, x: NodeId) + requires + host_ranked(doc, rank), + valid(doc, x), + is_host_including_inclusive_ancestor(doc, a, b), + is_host_including_inclusive_ancestor(doc, b, x), + ensures + is_host_including_inclusive_ancestor(doc, a, x), + decreases rank[x as int], +{ + if x != b { + lemma_hi_unfold(doc, rank, b, x); + lemma_up_rank(doc, rank, x); + let y = up(doc, x)->Some_0; + lemma_hi_trans(doc, rank, a, b, y); + if x != a { + lemma_hi_unfold(doc, rank, a, x); + } + } +} + +/// Chains read in an arena that has only lost links (a parent that was +/// there is kept or gone, kinds are kept) are chains of the old arena. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn lemma_hw_sub(d: Document, d2: Document, a: NodeId, x: NodeId, f: nat) + requires + wf(d), + d2.nodes.len() == d.nodes.len(), + valid(d, x), + forall|y: int| 0 <= y < d.nodes.len() ==> { + &&& (#[trigger] d2.nodes[y]).kind == d.nodes[y].kind + &&& (d2.nodes[y].parent == d.nodes[y].parent || d2.nodes[y].parent is None) + }, + host_including_within(d2, a, x, f), + ensures + host_including_within(d, a, x, f), + decreases f, +{ + lemma_wf_facts(d); + if x != a { + assert(f > 0); + match up(d2, x) { + Some(y) => { + if d2.nodes[x as int].parent is Some { + assert(d.nodes[x as int].parent == d2.nodes[x as int].parent); + } else { + assert(d.nodes[x as int].kind is DocumentFragment); + assert(d.nodes[x as int].parent is None); + } + assert(up(d, x) == Some(y)); + lemma_hw_sub(d, d2, a, y, (f - 1) as nat); + }, + None => {}, + } + } +} + +/// Chains that do not pass through the node `n` do not see its new parent. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn lemma_hw_unchanged(d: Document, d2: Document, n: NodeId, a: NodeId, x: NodeId, f: nat) + requires + wf(d), + d2.nodes.len() == d.nodes.len(), + valid(d, x), + forall|y: int| 0 <= y < d.nodes.len() ==> (#[trigger] d2.nodes[y]).kind == d.nodes[y].kind, + forall|y: int| 0 <= y < d.nodes.len() && y != n ==> (#[trigger] d2.nodes[y]).parent == d.nodes[y].parent, + !host_including_within(d, n, x, f), + ensures + host_including_within(d2, a, x, f) == host_including_within(d, a, x, f), + decreases f, +{ + lemma_wf_facts(d); + if x != a { + assert(x != n); + if f > 0 { + assert(up(d2, x) == up(d, x)); + match up(d, x) { + Some(y) => { + lemma_hw_unchanged(d, d2, n, a, y, (f - 1) as nat); + }, + None => {}, + } + } + } +} + +// --------------------------------------------------------------------- +// Grafting + +/// `x` is below one of `nodes`: a host-including descendant of one of them +/// (or one of them). +pub open spec fn in_graft(d: Document, nodes: Seq, x: NodeId) -> bool { + exists|i: int| 0 <= i < nodes.len() && #[trigger] is_host_including_inclusive_ancestor(d, nodes[i], x) +} + +/// A node below a grafted node, and not itself grafted, has its step up +/// below one too. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_graft_up(d: Document, rank: Seq, nodes: Seq, x: NodeId, y: NodeId) + requires + host_ranked(d, rank), + valid(d, x), + up(d, x) == Some(y), + !nodes.contains(x), + in_graft(d, nodes, x), + ensures + in_graft(d, nodes, y), +{ + let i = choose|i: int| 0 <= i < nodes.len() && #[trigger] is_host_including_inclusive_ancestor(d, nodes[i], x); + assert(nodes[i] != x); + lemma_hi_unfold(d, rank, nodes[i], x); + assert(is_host_including_inclusive_ancestor(d, nodes[i], y)); +} + +/// A node whose parent is below a grafted node is below it. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_graft_child(d: Document, rank: Seq, nodes: Seq, c: NodeId, y: NodeId) + requires + host_ranked(d, rank), + valid(d, c), + d.nodes[c as int].parent == Some(y), + in_graft(d, nodes, y), + ensures + in_graft(d, nodes, c), +{ + let i = choose|i: int| 0 <= i < nodes.len() && #[trigger] is_host_including_inclusive_ancestor(d, nodes[i], y); + lemma_hi_child(d, rank, nodes[i], y, c); + assert(is_host_including_inclusive_ancestor(d, nodes[i], c)); +} + +/// A fragment whose host is below a grafted node is below it. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_graft_contents(d: Document, rank: Seq, nodes: Seq, f: NodeId, h: NodeId) + requires + host_ranked(d, rank), + valid(d, f), + d.nodes[f as int].parent is None, + d.nodes[f as int].kind is DocumentFragment, + d.nodes[f as int].kind->DocumentFragment_host == Some(h), + in_graft(d, nodes, h), + ensures + in_graft(d, nodes, f), +{ + let i = choose|i: int| 0 <= i < nodes.len() && #[trigger] is_host_including_inclusive_ancestor(d, nodes[i], h); + lemma_hi_contents(d, rank, nodes[i], h, f); + assert(is_host_including_inclusive_ancestor(d, nodes[i], f)); +} + +/// Grafting: `d2` is `d` with each of `nodes` given the parent `p`, and no +/// other node given a parent it did not have (it may lose its parent), the +/// kinds unchanged. If no grafted node is a host-including inclusive +/// ancestor of `p` and none is a fragment, `d2` is [`host_acyclic`] if `d` +/// is. With no nodes this is losing links, which never makes a cycle. +/// +/// The ranking of `d2`: raise everything below a grafted node by `rank[p] + +/// 1`. A grafted node then ranks above `p` (which is not below one), every +/// edge below a grafted node is between nodes raised alike, and an edge +/// from outside into the nodes below a grafted node is only the new one. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn lemma_graft(d: Document, d2: Document, nodes: Seq, p: NodeId) + requires + host_acyclic(d), + frag_parentless(d), + d.nodes.len() <= max_nodes(), + valid(d, p), + d2.nodes.len() == d.nodes.len(), + forall|x: int| 0 <= x < d.nodes.len() ==> (#[trigger] d2.nodes[x]).kind == d.nodes[x].kind, + forall|i: int| 0 <= i < nodes.len() ==> { + let c = #[trigger] nodes[i]; + &&& valid(d, c) + &&& !(d.nodes[c as int].kind is DocumentFragment) + &&& !is_host_including_inclusive_ancestor(d, c, p) + &&& d2.nodes[c as int].parent == Some(p) + }, + forall|x: int| 0 <= x < d.nodes.len() && !nodes.contains(x as NodeId) ==> { + (#[trigger] d2.nodes[x]).parent == d.nodes[x].parent || d2.nodes[x].parent is None + }, + ensures + host_acyclic(d2), +{ + let n = d.nodes.len(); + let r = lemma_rank_witness(d); + let k = r[p as int] + 1; + let r2 = Seq::new(n, |x: int| if in_graft(d, nodes, x as NodeId) { + r[x] + k + } else { + r[x] + }); + // `p` is not below a grafted node. + assert(!in_graft(d, nodes, p)) by { + if in_graft(d, nodes, p) { + let i = choose|i: int| 0 <= i < nodes.len() && #[trigger] is_host_including_inclusive_ancestor(d, nodes[i], p); + assert(!is_host_including_inclusive_ancestor(d, nodes[i], p)); + } + } + assert forall|i: int| 0 <= i < n implies ranked_u_at(d2, r2, i) by { + assert(ranked_at(d, r, i)); + theorem_ranked_at_facts(d, r, i); + let node2 = d2.nodes[i]; + let ig = in_graft(d, nodes, i as NodeId); + if node2.parent is Some { + let q = node2.parent->Some_0; + if nodes.contains(i as NodeId) { + let j = choose|j: int| 0 <= j < nodes.len() && nodes[j] == i as NodeId; + assert(nodes[j] as int == i); + assert(d2.nodes[nodes[j] as int].parent == Some(p)); + assert(q == p); + lemma_hi_self(d, i as NodeId); + assert(is_host_including_inclusive_ancestor(d, nodes[j], i as NodeId)); + assert(ig); + } else { + assert(d.nodes[i].parent == Some(q)); + assert((i as NodeId) as int == i); + assert(parent(d, i as NodeId) == Some(q)); + assert(up(d, i as NodeId) == Some(q)); + if ig { + lemma_graft_up(d, r, nodes, i as NodeId, q); + } else if in_graft(d, nodes, q) { + lemma_graft_child(d, r, nodes, i as NodeId, q); + } + } + } + if node2.kind is DocumentFragment && node2.kind->DocumentFragment_host is Some { + let h = node2.kind->DocumentFragment_host->Some_0; + assert(d.nodes[i].kind is DocumentFragment); + assert(!nodes.contains(i as NodeId)) by { + if nodes.contains(i as NodeId) { + let j = choose|j: int| 0 <= j < nodes.len() && nodes[j] == i as NodeId; + assert(!(d.nodes[nodes[j] as int].kind is DocumentFragment)); + } + } + assert(d.nodes[i].parent is None); + assert((i as NodeId) as int == i); + assert(parent(d, i as NodeId) is None); + assert(up(d, i as NodeId) == Some(h)); + if ig { + lemma_graft_up(d, r, nodes, i as NodeId, h); + } else if in_graft(d, nodes, h) { + lemma_graft_contents(d, r, nodes, i as NodeId, h); + } + } + } + assert(ranked_u(d2, r2)); + lemma_ranked_u_acyclic(d2, r2); +} +// --------------------------------------------------------------------- +// What `wf` gives + +/// A well-formed arena is within the node limit. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_wf_limit(doc: Document) + requires + wf(doc), + ensures + doc.nodes.len() <= max_nodes(), +{ + let paths = lemma_wf_witness(doc); + assert(wf_with(doc, paths)); +} + +/// A node with a parent is not the document node. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_child_not_document(doc: Document, c: NodeId) + requires + wf(doc), + valid(doc, c), + doc.nodes[c as int].parent is Some, + ensures + !(doc.nodes[c as int].kind is Document), +{ + let paths = lemma_wf_witness(doc); + assert(wf_with(doc, paths)); +} + +// --------------------------------------------------------------------- +// New document, creating nodes, kinds + +/// A new document is [`host_acyclic`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_new_document_host_acyclic(url: Seq) + ensures + host_acyclic(new_document(url)), +{ + let d = new_document(url); + let rank = seq![0nat]; + assert forall|i: int| 0 <= i < d.nodes.len() implies #[trigger] ranked_at(d, rank, i) by {} + assert(host_ranked(d, rank)); +} + +/// Creating a node keeps [`host_acyclic`]: the new node has no parent and, +/// being [`creatable`], no host. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_create_node_host_acyclic(doc: Document, k: NodeKind) + requires + host_acyclic(doc), + creatable(doc, k), + ensures + host_acyclic(create_node(doc, k).0), +{ + let r = lemma_rank_witness(doc); + let n = doc.nodes.len(); + let d2 = create_node(doc, k).0; + let r2 = r.push(0nat); + assert forall|i: int| 0 <= i < n + 1 implies #[trigger] ranked_at(d2, r2, i) by { + if i < n { + assert(ranked_at(doc, r, i)); + theorem_ranked_at_facts(doc, r, i); + assert(d2.nodes[i] == doc.nodes[i]); + } else { + assert(d2.nodes[i].parent is None); + } + } + assert(host_ranked(d2, r2)); +} + +/// Creating a template's contents keeps [`host_acyclic`]: the contents is +/// ranked just above its host, and the host stays an element. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_create_template_contents_host_acyclic(doc: Document, t: NodeId) + requires + host_acyclic(doc), + valid(doc, t), + doc.nodes[t as int].kind is Element, + ensures + host_acyclic(create_template_contents(doc, t).0), +{ + let r = lemma_rank_witness(doc); + let n = doc.nodes.len(); + let d2 = create_template_contents(doc, t).0; + let r2 = r.push(r[t as int] + 1); + assert(ranked_at(doc, r, t as int)); + theorem_ranked_at_facts(doc, r, t as int); + assert forall|i: int| 0 <= i < n + 1 implies #[trigger] ranked_at(d2, r2, i) by { + if i < n { + assert(ranked_at(doc, r, i)); + theorem_ranked_at_facts(doc, r, i); + if i == t { + assert(d2.nodes[i].parent == doc.nodes[i].parent); + assert(d2.nodes[i].kind is Element); + } else { + assert(d2.nodes[i] == doc.nodes[i]); + } + } else { + assert(d2.nodes[i].parent is None); + assert(d2.nodes[i].kind == (NodeKind::DocumentFragment { host: Some(t) })); + } + } + assert(host_ranked(d2, r2)); +} + +/// Replacing a node's kind by one of the same shape keeps [`host_acyclic`]: +/// no parent or host changes. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn lemma_set_kind_acyclic(doc: Document, x: NodeId, k: NodeKind) + requires + host_acyclic(doc), + valid(doc, x), + same_shape(doc.nodes[x as int].kind, k), + ensures + host_acyclic(set_kind(doc, x, k)), +{ + let r = lemma_rank_witness(doc); + let d2 = set_kind(doc, x, k); + assert forall|i: int| 0 <= i < doc.nodes.len() implies #[trigger] ranked_at(d2, r, i) by { + assert(ranked_at(doc, r, i)); + theorem_ranked_at_facts(doc, r, i); + assert(d2.nodes[i].parent == doc.nodes[i].parent); + } + assert(host_ranked(d2, r)); +} + +/// "Replace data" at the end of a text keeps [`host_acyclic`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_append_data_host_acyclic(doc: Document, n: NodeId, data: Seq) + requires + host_acyclic(doc), + append_data_requires(doc, n), + ensures + host_acyclic(append_data(doc, n, data)), +{ + let k = append_data(doc, n, data).nodes[n as int].kind; + assert(append_data(doc, n, data) == set_kind(doc, n, k)); + lemma_set_kind_acyclic(doc, n, k); +} + +/// "Append an attribute" keeps [`host_acyclic`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_append_attribute_host_acyclic(doc: Document, e: NodeId, attr: Attr) + requires + host_acyclic(doc), + append_attribute_requires(doc, e), + ensures + host_acyclic(append_attribute(doc, e, attr)), +{ + let k = append_attribute(doc, e, attr).nodes[e as int].kind; + assert(append_attribute(doc, e, attr) == set_kind(doc, e, k)); + lemma_set_kind_acyclic(doc, e, k); +} + +/// Setting the document's mode keeps [`host_acyclic`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_set_mode_host_acyclic(doc: Document, mode: DocumentMode) + requires + host_acyclic(doc), + wf(doc), + ensures + host_acyclic(set_mode(doc, mode)), +{ + let k = set_mode(doc, mode).nodes[0].kind; + assert(set_mode(doc, mode) == set_kind(doc, 0, k)); + lemma_set_kind_acyclic(doc, 0, k); +} + +/// Setting an element's attribute list keeps [`host_acyclic`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn lemma_set_attributes_acyclic(doc: Document, e: NodeId, attrs: Seq) + requires + host_acyclic(doc), + valid(doc, e), + doc.nodes[e as int].kind is Element, + ensures + host_acyclic(set_attributes(doc, e, attrs)), +{ + let k = set_attributes(doc, e, attrs).nodes[e as int].kind; + assert(set_attributes(doc, e, attrs) == set_kind(doc, e, k)); + lemma_set_kind_acyclic(doc, e, k); +} + +/// "Change an attribute" keeps [`host_acyclic`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_change_attribute_host_acyclic(doc: Document, e: NodeId, i: nat, value: Seq) + requires + host_acyclic(doc), + attribute_requires(doc, e, i), + ensures + host_acyclic(change_attribute(doc, e, i, value)), +{ + let attrs = doc.nodes[e as int].kind->Element_attributes; + lemma_set_attributes_acyclic(doc, e, attrs.update(i as int, Attr { value, ..attrs[i as int] })); +} + +/// "Remove an attribute" keeps [`host_acyclic`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_remove_attribute_host_acyclic(doc: Document, e: NodeId, i: nat) + requires + host_acyclic(doc), + attribute_requires(doc, e, i), + ensures + host_acyclic(remove_attribute(doc, e, i)), +{ + lemma_set_attributes_acyclic(doc, e, doc.nodes[e as int].kind->Element_attributes.remove(i as int)); +} + +/// "Replace an attribute" keeps [`host_acyclic`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_replace_attribute_host_acyclic(doc: Document, e: NodeId, i: nat, new_attr: Attr) + requires + host_acyclic(doc), + attribute_requires(doc, e, i), + ensures + host_acyclic(replace_attribute(doc, e, i, new_attr)), +{ + lemma_set_attributes_acyclic(doc, e, doc.nodes[e as int].kind->Element_attributes.update(i as int, new_attr)); +} + +// --------------------------------------------------------------------- +// remove, adopt + +/// "Remove" keeps [`host_acyclic`]: it only takes a parent link away. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_remove_host_acyclic(doc: Document, node: NodeId) + requires + host_acyclic(doc), + remove_requires(doc, node), + ensures + host_acyclic(remove(doc, node)), +{ + theorem_remove_keeps_record(doc, node); + lemma_wf_frag_parentless(doc); + lemma_wf_limit(doc); + let d2 = remove(doc, node); + assert forall|x: int| 0 <= x < doc.nodes.len() && !(Seq::::empty()).contains(x as NodeId) implies { + (#[trigger] d2.nodes[x]).parent == doc.nodes[x].parent || d2.nodes[x].parent is None + } by { + if x == node { + assert(d2.nodes[x].parent is None); + } + } + lemma_graft(doc, d2, Seq::::empty(), node); +} + +/// "Adopt" keeps [`host_acyclic`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_adopt_host_acyclic(doc: Document, node: NodeId) + requires + host_acyclic(doc), + adopt_requires(doc, node), + ensures + host_acyclic(adopt(doc, node)), +{ + if doc.nodes[node as int].parent is Some { + theorem_remove_host_acyclic(doc, node); + } +} + +// --------------------------------------------------------------------- +// insert + +/// A child of a fragment that is not a host-including inclusive ancestor of +/// `parent` is a node that is not a fragment and is not one either. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_frag_child_graftable(doc: Document, r: Seq, frag: NodeId, parent: NodeId, i: int) + requires + wf(doc), + host_ranked(doc, r), + valid(doc, frag), + valid(doc, parent), + 0 <= i < doc.nodes[frag as int].children.len(), + !is_host_including_inclusive_ancestor(doc, frag, parent), + ensures + valid(doc, doc.nodes[frag as int].children[i]), + !(doc.nodes[doc.nodes[frag as int].children[i] as int].kind is DocumentFragment), + !is_host_including_inclusive_ancestor(doc, doc.nodes[frag as int].children[i], parent), +{ + lemma_wf_facts(doc); + lemma_wf_frag_parentless(doc); + let c = doc.nodes[frag as int].children[i]; + assert(doc.nodes[c as int].parent == Some(frag)); + lemma_hi_self(doc, frag); + lemma_hi_child(doc, r, frag, frag, c); + if is_host_including_inclusive_ancestor(doc, c, parent) { + lemma_hi_trans(doc, r, frag, c, parent); + } +} + +/// A fragment of a well-formed arena has no parent. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_frag_parentless_at(doc: Document, x: int) + requires + wf(doc), + 0 <= x < doc.nodes.len(), + doc.nodes[x].kind is DocumentFragment, + ensures + doc.nodes[x].parent is None, +{ + lemma_wf_frag_parentless(doc); +} + +/// A node that is neither `node` nor (if `node` is a fragment) one of its +/// children keeps its parent, or loses it (a fragment is parentless in the +/// arena after "insert" too). +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_insert_any_parents(doc: Document, node: NodeId, parent: NodeId, child: Option, x: int) + requires + insert_any_requires(doc, node, parent, child), + 0 <= x < doc.nodes.len(), + doc.nodes[node as int].kind is DocumentFragment || x != node, + !(doc.nodes[node as int].kind is DocumentFragment && doc.nodes[node as int].children.contains(x as NodeId)), + ensures + ({ + let d2 = insert_any(doc, node, parent, child); + d2.nodes[x].parent == doc.nodes[x].parent || d2.nodes[x].parent is None + }), +{ + theorem_insert_any_wf(doc, node, parent, child); + let d2 = insert_any(doc, node, parent, child); + if x == node { + assert(d2.nodes[x].kind == doc.nodes[x].kind); + lemma_frag_parentless_at(d2, x); + } +} + +/// What "insert" of any node grafts: `node`, or a fragment's children. +pub open spec fn graft_nodes(doc: Document, node: NodeId) -> Seq { + if doc.nodes[node as int].kind is DocumentFragment { + doc.nodes[node as int].children + } else { + seq![node] + } +} + +/// The premises of [`lemma_graft`] about the arena `d2` after the graft. +pub open spec fn graft_premises(doc: Document, d2: Document, nodes: Seq, parent: NodeId) -> bool { + &&& d2.nodes.len() == doc.nodes.len() + &&& forall|x: int| 0 <= x < doc.nodes.len() ==> (#[trigger] d2.nodes[x]).kind == doc.nodes[x].kind + &&& forall|i: int| 0 <= i < nodes.len() ==> { + let c = #[trigger] nodes[i]; + &&& valid(doc, c) + &&& !(doc.nodes[c as int].kind is DocumentFragment) + &&& !is_host_including_inclusive_ancestor(doc, c, parent) + &&& d2.nodes[c as int].parent == Some(parent) + } + &&& forall|x: int| 0 <= x < doc.nodes.len() && !nodes.contains(x as NodeId) ==> { + (#[trigger] d2.nodes[x]).parent == doc.nodes[x].parent || d2.nodes[x].parent is None + } +} + +/// "Insert" of a node that is not a fragment is a graft of that node. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_graftable_node(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + !(doc.nodes[node as int].kind is DocumentFragment), + !is_host_including_inclusive_ancestor(doc, node, parent), + ensures + graft_premises(doc, insert_any(doc, node, parent, child), seq![node], parent), +{ + assert(insert_requires(doc, node, parent, child)); + theorem_insert_wf(doc, node, parent, child); + lemma_wf_limit(doc); + assert(insert_any(doc, node, parent, child) == insert(doc, node, parent, child)); + let d2 = insert(doc, node, parent, child); + assert(seq![node][0] == node); + assert forall|x: int| 0 <= x < doc.nodes.len() && !(seq![node]).contains(x as NodeId) implies { + (#[trigger] d2.nodes[x]).parent == doc.nodes[x].parent || d2.nodes[x].parent is None + } by { + assert(x != node); + } +} + +/// "Insert" of a fragment is a graft of its children. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_graftable_frag(doc: Document, node: NodeId, parent: NodeId, child: Option, r: Seq) + requires + insert_any_requires(doc, node, parent, child), + doc.nodes[node as int].kind is DocumentFragment, + host_ranked(doc, r), + !is_host_including_inclusive_ancestor(doc, node, parent), + ensures + graft_premises(doc, insert_any(doc, node, parent, child), doc.nodes[node as int].children, parent), +{ + theorem_insert_any_wf(doc, node, parent, child); + lemma_wf_limit(doc); + let d2 = insert_any(doc, node, parent, child); + let nodes = doc.nodes[node as int].children; + assert forall|i: int| 0 <= i < nodes.len() implies { + let c = #[trigger] nodes[i]; + &&& valid(doc, c) + &&& !(doc.nodes[c as int].kind is DocumentFragment) + &&& !is_host_including_inclusive_ancestor(doc, c, parent) + &&& d2.nodes[c as int].parent == Some(parent) + } by { + lemma_frag_child_graftable(doc, r, node, parent, i); + } + assert forall|x: int| 0 <= x < doc.nodes.len() && !nodes.contains(x as NodeId) implies { + (#[trigger] d2.nodes[x]).parent == doc.nodes[x].parent || d2.nodes[x].parent is None + } by { + lemma_insert_any_parents(doc, node, parent, child, x); + } +} + +/// "Insert" of any node (a fragment inserts its children) keeps +/// [`host_acyclic`] if `node` is not a host-including inclusive ancestor of +/// `parent`: the grafted nodes are `node`, or a fragment's children (below +/// a fragment that is not an ancestor of `parent`, so none of them is). +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_insert_any_host_acyclic(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + host_acyclic(doc), + !is_host_including_inclusive_ancestor(doc, node, parent), + ensures + host_acyclic(insert_any(doc, node, parent, child)), +{ + lemma_wf_frag_parentless(doc); + lemma_wf_limit(doc); + let d2 = insert_any(doc, node, parent, child); + if doc.nodes[node as int].kind is DocumentFragment { + let r = lemma_rank_witness(doc); + lemma_graftable_frag(doc, node, parent, child, r); + lemma_graft(doc, d2, doc.nodes[node as int].children, parent); + } else { + lemma_graftable_node(doc, node, parent, child); + lemma_graft(doc, d2, seq![node], parent); + } +} + +/// The raw "insert" (the parser's) keeps [`host_acyclic`] when `node` is not +/// a host-including inclusive ancestor of `parent`. [`insert_requires`]'s +/// inclusive-ancestor check is not enough: a template inserted below a node +/// of its own contents passes it, and makes a cycle through the host. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_insert_host_acyclic(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_acyclic_requires(doc, node, parent, child), + ensures + host_acyclic(insert(doc, node, parent, child)), +{ + assert(insert_any_requires(doc, node, parent, child)); + theorem_insert_any_host_acyclic(doc, node, parent, child); + assert(insert_any(doc, node, parent, child) == insert(doc, node, parent, child)); +} + +/// "Append" keeps [`host_acyclic`], under [`insert_acyclic_requires`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_append_host_acyclic(doc: Document, parent: NodeId, node: NodeId) + requires + insert_acyclic_requires(doc, node, parent, None), + ensures + host_acyclic(append(doc, parent, node)), +{ + theorem_insert_host_acyclic(doc, node, parent, None); +} + +/// "Insert before" keeps [`host_acyclic`], under [`insert_acyclic_requires`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_insert_before_host_acyclic(doc: Document, parent: NodeId, node: NodeId, child: NodeId) + requires + insert_acyclic_requires(doc, node, parent, Some(child)), + ensures + host_acyclic(insert_before(doc, parent, node, child)), +{ + theorem_insert_host_acyclic(doc, node, parent, Some(child)); +} + +/// DOM §4.2.3† "pre-insert" keeps [`host_acyclic`], with no precondition +/// beyond [`host_acyclic`] of the input: its step 2 refuses a host-including +/// inclusive ancestor of `parent`, and an error leaves the tree as it is. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_pre_insert_host_acyclic(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + pre_insert_requires(doc, node, parent, child), + host_acyclic(doc), + ensures + host_acyclic(pre_insert(doc, node, parent, child)), +{ + if pre_insert_error(doc, node, parent, child) is None { + lemma_pre_insert_valid(doc, node, parent, child); + assert(!is_host_including_inclusive_ancestor(doc, node, parent)); + theorem_insert_any_host_acyclic(doc, node, parent, pre_insert_reference(doc, node, child)); + } +} + +/// A node that is not a host-including inclusive ancestor of `x` before +/// links are taken away (`d2` has them only as `d` has them, or has none) +/// is not one after it. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_not_hi_after_removal(d: Document, d2: Document, a: NodeId, x: NodeId) + requires + wf(d), + d2.nodes.len() == d.nodes.len(), + valid(d, x), + forall|y: int| 0 <= y < d.nodes.len() ==> { + &&& (#[trigger] d2.nodes[y]).kind == d.nodes[y].kind + &&& (d2.nodes[y].parent == d.nodes[y].parent || d2.nodes[y].parent is None) + }, + !is_host_including_inclusive_ancestor(d, a, x), + ensures + !is_host_including_inclusive_ancestor(d2, a, x), +{ + if is_host_including_inclusive_ancestor(d2, a, x) { + lemma_hw_sub(d, d2, a, x, d.nodes.len()); + } +} + +/// DOM §4.2.3† "replace" keeps [`host_acyclic`], with no precondition +/// beyond [`host_acyclic`] of the input: its step 2 refuses a host-including +/// inclusive ancestor of `parent`, and removing `child` first only takes a +/// link away. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_replace_host_acyclic(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + replace_requires(doc, child, node, parent), + host_acyclic(doc), + ensures + host_acyclic(replace(doc, child, node, parent)), +{ + if replace_error(doc, child, node, parent) is None { + lemma_replace_valid(doc, child, node, parent); + let d1 = remove(doc, child); + theorem_remove_host_acyclic(doc, child); + theorem_remove_keeps_record(doc, child); + assert(!is_host_including_inclusive_ancestor(doc, node, parent)); + assert forall|y: int| 0 <= y < doc.nodes.len() implies { + &&& (#[trigger] d1.nodes[y]).kind == doc.nodes[y].kind + &&& (d1.nodes[y].parent == doc.nodes[y].parent || d1.nodes[y].parent is None) + } by { + if y == child { + assert(d1.nodes[y].parent is None); + } + } + lemma_not_hi_after_removal(doc, d1, node, parent); + theorem_insert_any_host_acyclic(d1, node, parent, replace_reference(doc, child, node)); + } +} + +// --------------------------------------------------------------------- +// replace all + +/// A record after removing a prefix of a node's children keeps its kind, and +/// its parent or loses it. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_removed_prefix_links(d: Document, p: NodeId, k: int, y: int) + requires + valid(d, p), + 0 <= y < d.nodes.len(), + ensures + removed_prefix(d, p, k)[y].kind == d.nodes[y].kind, + removed_prefix(d, p, k)[y].parent == d.nodes[y].parent || removed_prefix(d, p, k)[y].parent is None, +{ +} + +/// Removing all of a node's children only takes parent links away. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_remove_children_links(doc: Document, parent: NodeId) + requires + wf(doc), + valid(doc, parent), + ensures + remove_children(doc, parent).nodes.len() == doc.nodes.len(), + forall|y: int| 0 <= y < doc.nodes.len() ==> { + &&& (#[trigger] remove_children(doc, parent).nodes[y]).kind == doc.nodes[y].kind + &&& (remove_children(doc, parent).nodes[y].parent == doc.nodes[y].parent || remove_children(doc, parent).nodes[y].parent is None) + }, +{ + let d1 = remove_children(doc, parent); + let len = doc.nodes[parent as int].children.len(); + lemma_remove_children_n(doc, parent, len); + assert(children(doc, parent) == doc.nodes[parent as int].children); + assert(d1.nodes == removed_prefix(doc, parent, len as int)); + assert forall|y: int| 0 <= y < doc.nodes.len() implies { + &&& (#[trigger] d1.nodes[y]).kind == doc.nodes[y].kind + &&& (d1.nodes[y].parent == doc.nodes[y].parent || d1.nodes[y].parent is None) + } by { + lemma_removed_prefix_links(doc, parent, len as int, y); + } +} + +/// DOM §4.2.3† "replace all" keeps [`host_acyclic`], under what it asks of +/// its caller ([`replace_all_requires`]: `node` is not a host-including +/// inclusive ancestor of `parent`) and [`host_acyclic`] of the input. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_replace_all_host_acyclic(doc: Document, node: Option, parent: NodeId) + requires + replace_all_requires(doc, node, parent), + host_acyclic(doc), + ensures + host_acyclic(replace_all(doc, node, parent)), +{ + lemma_replace_all_valid(doc, node, parent); + lemma_wf_frag_parentless(doc); + lemma_wf_limit(doc); + lemma_remove_children_links(doc, parent); + let d1 = remove_children(doc, parent); + lemma_graft(doc, d1, Seq::::empty(), parent); + if let Some(n) = node { + lemma_not_hi_after_removal(doc, d1, n, parent); + theorem_insert_any_host_acyclic(d1, n, parent, None); + } +} + +// --------------------------------------------------------------------- +// reparent children + +/// Moving `k` children of `from` to the end of `to`, one by one, keeps +/// [`host_acyclic`] if `from` is not a host-including inclusive ancestor of +/// `to`. (Nor is a child of `from`, and moving a child under `to` does not +/// change the chain above `to`.) +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn lemma_reparent_n_host_acyclic(d: Document, from: NodeId, to: NodeId, k: nat) + requires + wf(d), + host_acyclic(d), + valid(d, from), + valid(d, to), + may_have_children(d.nodes[to as int].kind), + !is_host_including_inclusive_ancestor(d, from, to), + ensures + host_acyclic(reparent_children_n(d, from, to, k)), + decreases k, +{ + if k == 0 || d.nodes[from as int].children.len() == 0 { + } else { + let c = d.nodes[from as int].children[0]; + lemma_wf_facts(d); + lemma_wf_frag_parentless(d); + let r = lemma_rank_witness(d); + assert(valid(d, c)); + assert(d.nodes[c as int].parent == Some(from)); + lemma_child_not_document(d, c); + lemma_hi_self(d, from); + lemma_hi_child(d, r, from, from, c); + assert(!is_host_including_inclusive_ancestor(d, c, to)) by { + if is_host_including_inclusive_ancestor(d, c, to) { + lemma_hi_trans(d, r, from, c, to); + } + } + lemma_not_host_not_incl(d, c, to); + assert(insert_requires(d, c, to, None)); + assert(insert_acyclic_requires(d, c, to, None)); + theorem_insert_wf(d, c, to, None); + theorem_insert_host_acyclic(d, c, to, None); + let d1 = append(d, to, c); + assert(d1 == insert(d, c, to, None)); + lemma_wf_limit(d); + lemma_hw_unchanged(d, d1, c, from, to, d.nodes.len()); + assert(!is_host_including_inclusive_ancestor(d1, from, to)); + lemma_reparent_n_host_acyclic(d1, from, to, (k - 1) as nat); + } +} + +/// The adoption agency's "take all of the child nodes of furthest block and +/// append them to the element" keeps [`host_acyclic`], under +/// [`reparent_children_acyclic_requires`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_reparent_children_host_acyclic(doc: Document, from: NodeId, to: NodeId) + requires + reparent_children_acyclic_requires(doc, from, to), + ensures + host_acyclic(reparent_children(doc, from, to)), +{ + lemma_reparent_n_host_acyclic(doc, from, to, doc.nodes[from as int].children.len()); +} + +// --------------------------------------------------------------------- +// clone + +/// Shallow "clone a node" keeps [`host_acyclic`]: a new parentless node, and +/// for a template its contents, ranked just above it. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_clone_node_host_acyclic(doc: Document, node: NodeId) + requires + clone_requires(doc, node), + host_acyclic(doc), + ensures + host_acyclic(clone_node(doc, node).0), +{ + let k = doc.nodes[node as int].kind; + let ck = clone_kind(k); + assert(creatable(doc, ck)); + theorem_create_node_host_acyclic(doc, ck); + let (d1, copy) = create_node(doc, ck); + if is_template(k) { + assert(d1.nodes[copy as int].kind is Element); + theorem_create_template_contents_host_acyclic(d1, copy); + } +} + +/// A new node of a deep clone with a parent has a new node as its parent, +/// which copies the parent of what the node copies. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_deep_parent(doc: Document, out: Document, src: Seq, t: int) -> (u: int) + requires + wf(doc), + wf(out), + out.nodes.len() == doc.nodes.len() + src.len(), + forall|i: int| 0 <= i < doc.nodes.len() ==> #[trigger] out.nodes[i] == doc.nodes[i], + forall|t: int| 0 <= t < src.len() ==> valid(doc, #[trigger] src[t]), + forall|t: int| 0 <= t < src.len() ==> #[trigger] deep_copy_children(doc, out, src, t), + 0 <= t < src.len(), + out.nodes[doc.nodes.len() + t].parent is Some, + ensures + 0 <= u < src.len(), + out.nodes[doc.nodes.len() + t].parent == Some((doc.nodes.len() + u) as NodeId), + doc.nodes[src[t] as int].parent == Some(src[u]), +{ + let b = doc.nodes.len(); + let i = b + t; + let q = out.nodes[i].parent->Some_0; + lemma_wf_facts(out); + lemma_wf_facts(doc); + lemma_wf_limit(out); + assert((i as NodeId) as int == i); + assert(out.nodes[q as int].children.contains(i as NodeId)); + if (q as int) < b { + assert(out.nodes[q as int] == doc.nodes[q as int]); + let j = choose|j: int| 0 <= j < doc.nodes[q as int].children.len() && doc.nodes[q as int].children[j] == i as NodeId; + assert(doc.nodes[q as int].children[j] < b); + } + let u = q as int - b; + assert(deep_copy_children(doc, out, src, u)); + let j = choose|j: int| 0 <= j < out.nodes[q as int].children.len() && out.nodes[q as int].children[j] == i as NodeId; + theorem_deep_copy_children_facts(doc, out, src, u, j); + assert(src[t] == doc.nodes[src[u] as int].children[j]); + u +} + +/// A new fragment of a deep clone with a host has a new node as its host, +/// which copies the host of what the fragment copies. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_deep_host(doc: Document, out: Document, src: Seq, t: int) -> (u: int) + requires + wf(doc), + wf(out), + out.nodes.len() == doc.nodes.len() + src.len(), + forall|i: int| 0 <= i < doc.nodes.len() ==> #[trigger] out.nodes[i] == doc.nodes[i], + forall|t: int| 0 <= t < src.len() ==> valid(doc, #[trigger] src[t]), + forall|t: int| 0 <= t < src.len() ==> #[trigger] deep_copy_kind(doc, out, src, t), + 0 <= t < src.len(), + out.nodes[doc.nodes.len() + t].kind is DocumentFragment, + out.nodes[doc.nodes.len() + t].kind->DocumentFragment_host is Some, + ensures + 0 <= u < src.len(), + out.nodes[doc.nodes.len() + t].kind->DocumentFragment_host == Some((doc.nodes.len() + u) as NodeId), + doc.nodes[src[t] as int].kind is DocumentFragment, + doc.nodes[src[t] as int].kind->DocumentFragment_host == Some(src[u]), +{ + let b = doc.nodes.len(); + let i = b + t; + let h = out.nodes[i].kind->DocumentFragment_host->Some_0; + lemma_wf_facts(out); + lemma_wf_facts(doc); + lemma_wf_limit(out); + assert((i as NodeId) as int == i); + assert(out.nodes[h as int].kind is Element); + assert(out.nodes[h as int].kind->Element_template_contents == Some(i as NodeId)); + if (h as int) < b { + assert(out.nodes[h as int] == doc.nodes[h as int]); + assert(doc.nodes[h as int].kind->Element_template_contents->Some_0 < b); + } + let u = h as int - b; + assert(deep_copy_kind(doc, out, src, u)); + theorem_deep_copy_kind_fields(doc, out, src, u); + assert(doc.nodes[src[u] as int].kind is Element); + u +} + +/// The ranking of a deep clone: a new node is ranked as the node it copies. +pub open spec fn deep_rank(doc: Document, src: Seq, r: Seq) -> Seq { + Seq::new(doc.nodes.len() + src.len(), |i: int| if i < doc.nodes.len() { + r[i] + } else { + r[src[i - doc.nodes.len()] as int] + }) +} + +/// A new node of a deep clone with a parent ranks above it. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_deep_parent_ranked(doc: Document, out: Document, src: Seq, r: Seq, t: int) + requires + wf(doc), + wf(out), + host_ranked(doc, r), + out.nodes.len() == doc.nodes.len() + src.len(), + forall|i: int| 0 <= i < doc.nodes.len() ==> #[trigger] out.nodes[i] == doc.nodes[i], + forall|t: int| 0 <= t < src.len() ==> valid(doc, #[trigger] src[t]), + forall|t: int| 0 <= t < src.len() ==> #[trigger] deep_copy_children(doc, out, src, t), + 0 <= t < src.len(), + out.nodes[doc.nodes.len() + t].parent is Some, + ensures + ({ + let q = out.nodes[doc.nodes.len() + t].parent->Some_0; + deep_rank(doc, src, r)[q as int] < deep_rank(doc, src, r)[doc.nodes.len() + t] + }), +{ + let b = doc.nodes.len(); + let r2 = deep_rank(doc, src, r); + let s = src[t]; + let u = lemma_deep_parent(doc, out, src, t); + assert(valid(doc, src[u])); + assert(ranked_at(doc, r, s as int)); + theorem_ranked_at_facts(doc, r, s as int); + assert(r2[b + t] == r[s as int]); + assert(r2[b + u] == r[src[u] as int]); +} + +/// A new fragment of a deep clone with a host ranks above it. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_deep_host_ranked(doc: Document, out: Document, src: Seq, r: Seq, t: int) + requires + wf(doc), + wf(out), + host_ranked(doc, r), + out.nodes.len() == doc.nodes.len() + src.len(), + forall|i: int| 0 <= i < doc.nodes.len() ==> #[trigger] out.nodes[i] == doc.nodes[i], + forall|t: int| 0 <= t < src.len() ==> valid(doc, #[trigger] src[t]), + forall|t: int| 0 <= t < src.len() ==> #[trigger] deep_copy_kind(doc, out, src, t), + 0 <= t < src.len(), + out.nodes[doc.nodes.len() + t].kind is DocumentFragment, + out.nodes[doc.nodes.len() + t].kind->DocumentFragment_host is Some, + ensures + ({ + let h = out.nodes[doc.nodes.len() + t].kind->DocumentFragment_host->Some_0; + deep_rank(doc, src, r)[h as int] < deep_rank(doc, src, r)[doc.nodes.len() + t] + }), +{ + let b = doc.nodes.len(); + let r2 = deep_rank(doc, src, r); + let s = src[t]; + let u = lemma_deep_host(doc, out, src, t); + assert(valid(doc, src[u])); + assert(ranked_at(doc, r, s as int)); + theorem_ranked_at_facts(doc, r, s as int); + assert(r2[b + t] == r[s as int]); + assert(r2[b + u] == r[src[u] as int]); +} + +/// A new node of a deep clone is ranked above its parent and its host. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +proof fn lemma_deep_new_ranked(doc: Document, out: Document, src: Seq, r: Seq, t: int) + requires + wf(doc), + wf(out), + host_ranked(doc, r), + out.nodes.len() == doc.nodes.len() + src.len(), + forall|i: int| 0 <= i < doc.nodes.len() ==> #[trigger] out.nodes[i] == doc.nodes[i], + forall|t: int| 0 <= t < src.len() ==> valid(doc, #[trigger] src[t]), + forall|t: int| 0 <= t < src.len() ==> #[trigger] deep_copy_children(doc, out, src, t), + forall|t: int| 0 <= t < src.len() ==> #[trigger] deep_copy_kind(doc, out, src, t), + 0 <= t < src.len(), + ensures + ranked_at(out, deep_rank(doc, src, r), doc.nodes.len() + t), +{ + let b = doc.nodes.len(); + let s = src[t]; + assert(ranked_at(doc, r, s as int)); + theorem_ranked_at_facts(doc, r, s as int); + if out.nodes[b + t].parent is Some { + lemma_deep_parent_ranked(doc, out, src, r, t); + } + if out.nodes[b + t].kind is DocumentFragment && out.nodes[b + t].kind->DocumentFragment_host is Some { + lemma_deep_host_ranked(doc, out, src, r, t); + } +} + +/// A deep clone of a [`host_acyclic`] arena is [`host_acyclic`]: each new node +/// is ranked as the node it copies, and an edge between new nodes copies an +/// edge of the source arena. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_deep_clone_src_host_acyclic(doc: Document, node: NodeId, out: Document, src: Seq) + requires + wf(doc), + host_acyclic(doc), + wf(out), + deep_clone_src(doc, node, out, src), + ensures + host_acyclic(out), +{ + let b = doc.nodes.len(); + let m = src.len(); + let r = lemma_rank_witness(doc); + let r2 = deep_rank(doc, src, r); + assert forall|t: int| 0 <= t < m implies #[trigger] deep_copy_children(doc, out, src, t) by { + assert(deep_copy_node(doc, out, src, t)); + } + assert forall|t: int| 0 <= t < m implies #[trigger] deep_copy_kind(doc, out, src, t) by { + assert(deep_copy_node(doc, out, src, t)); + } + assert forall|i: int| 0 <= i < b + m implies #[trigger] ranked_at(out, r2, i) by { + if i < b { + assert(ranked_at(doc, r, i)); + theorem_ranked_at_facts(doc, r, i); + assert(out.nodes[i] == doc.nodes[i]); + } else { + lemma_deep_new_ranked(doc, out, src, r, i - b); + let s = src[i - b]; + assert(valid(doc, s)); + assert(ranked_at(doc, r, s as int)); + theorem_ranked_at_facts(doc, r, s as int); + } + } + assert(host_ranked(out, r2)); +} + +/// A deep clone of a [`host_acyclic`] arena is [`host_acyclic`], as +/// [`deep_clone`] (the relation) says it. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_deep_clone_host_acyclic(doc: Document, node: NodeId, out: Document, copy: NodeId) + requires + wf(doc), + host_acyclic(doc), + wf(out), + deep_clone(doc, node, out, copy), + ensures + host_acyclic(out), +{ + let src = choose|src: Seq| deep_clone_src(doc, node, out, src); + theorem_deep_clone_src_host_acyclic(doc, node, out, src); +} + +/// `Arena::clone_deep` keeps [`host_acyclic`]: on a copy the arena is the +/// copy of a [`host_acyclic`] one, and on a refusal it is as it was. +#[verifier::spinoff_prover] +#[verifier::rlimit(50)] +pub proof fn theorem_clone_deep_host_acyclic(doc: Document, node: NodeId, out: Document, id: Option) + requires + clone_deep_requires(doc, node), + clone_deep_ensures(doc, node, out, id), + ensures + host_acyclic(out), +{ + if let Some(copy) = id { + theorem_deep_clone_host_acyclic(doc, node, out, copy); + } +} + +} // verus! diff --git a/crates/clazel-dom/src/host_acyclic_mutation_lemmas.rs b/crates/clazel-dom/src/host_acyclic_mutation_lemmas.rs new file mode 100644 index 000000000..0d6067492 --- /dev/null +++ b/crates/clazel-dom/src/host_acyclic_mutation_lemmas.rs @@ -0,0 +1,59 @@ +//! Concrete instances of the host-acyclicity theorems, the kill certificates +//! of the mutations of [`pre_insert_error`] and [`host_including_within`] in +//! `mutations.json` (`xtask/src/mutate.rs`). Each lemma states a fact about +//! a concrete arena that a proven theorem implies for the real spec, and +//! that a mutant makes false (the certificate's witness). +//! +//! The arena is [`ds_tmpl`]: a document (0), a template element (1) and its +//! contents, the fragment (2) whose host is 1. Inserting the template into its +//! own contents is not refused by an inclusive-ancestor check (the fragment +//! has no parent), only by the host-including one, and would make a cycle +//! through the host. + +#[cfg(verus_only)] +use super::contract::*; +#[cfg(verus_only)] +use super::deep_clone_size_lemmas::*; +#[cfg(verus_only)] +use super::deep_clone_size_mutation_lemmas::*; +#[cfg(verus_only)] +use super::host_acyclic_lemmas::*; +#[cfg(verus_only)] +use super::ops_lemmas::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// "Ensure pre-insertion validity" refuses to insert the template into its +/// own contents: were it allowed, the arena after it would be +/// host-acyclic (`theorem_pre_insert_host_acyclic`), but the template +/// would be a child of its contents, which are its contents. +pub proof fn ml_host_cycle_refused() + ensures + !(pre_insert_error(ds_tmpl(), 1, 2, None) is None), +{ + ds_tmpl_facts(); + if pre_insert_error(ds_tmpl(), 1, 2, None) is None { + assert(valid(ds_tmpl(), 1)); + assert(valid(ds_tmpl(), 2)); + assert(pre_insert_requires(ds_tmpl(), 1, 2, None)); + theorem_pre_insert_host_acyclic(ds_tmpl(), 1, 2, None); + theorem_pre_insert_wf(ds_tmpl(), 1, 2, None); + let d2 = pre_insert(ds_tmpl(), 1, 2, None); + assert(!(ds_tmpl().nodes[1].kind is DocumentFragment)) by (compute_only); + assert(ds_tmpl().nodes[2].kind == (NodeKind::DocumentFragment { host: Some(1usize) })) by (compute_only); + assert(d2.nodes[1].parent == Some(2usize)); + assert(d2.nodes[2].kind == ds_tmpl().nodes[2].kind); + let rank = lemma_rank_witness(d2); + assert(ranked_at(d2, rank, 1)); + assert(ranked_at(d2, rank, 2)); + theorem_ranked_at_facts(d2, rank, 1); + theorem_ranked_at_facts(d2, rank, 2); + assert(false); + } +} + +} // verus! diff --git a/crates/clazel-dom/src/independent.rs b/crates/clazel-dom/src/independent.rs index 489f548b9..dba3f503d 100644 --- a/crates/clazel-dom/src/independent.rs +++ b/crates/clazel-dom/src/independent.rs @@ -24,6 +24,18 @@ //! node between the two parts; //! - the new arena is built record by record (`Seq::new`), not by updates. //! +//! **Phase 7 item 7.13's operations are outside this file.** "Pre-insert", +//! "replace", "replace all" and the insertion of a fragment are +//! compositions of the three algorithms transcribed here ("remove" and +//! "insert", through "adopt") with the checks of "ensure pre-insertion +//! validity" and the reference-child steps; a shallow "clone a node" and +//! the attribute changes are single record updates. This file does not +//! transcribe the validity algorithm or those compositions a second time: +//! the validity checks are covered by `tests/operations.rs`, and the +//! compositions by the theorems of `ops_lemmas.rs` (each states what the +//! composed tree is). The "insertion steps" and "removing steps" hooks of +//! `spec.rs` change nothing and are not transcribed. +//! //! The same restrictions as `spec.rs` apply (the parser's calls): `node` //! is not a `DocumentFragment` (insert's step 1 gives « node », steps 3 //! and 4 do nothing), and steps that only touch live ranges, slots, diff --git a/crates/clazel-dom/src/independent_equivalence.rs b/crates/clazel-dom/src/independent_equivalence.rs index c814a814e..b9f19d3ae 100644 --- a/crates/clazel-dom/src/independent_equivalence.rs +++ b/crates/clazel-dom/src/independent_equivalence.rs @@ -124,6 +124,7 @@ pub proof fn lemma_independent_adopt(doc: Document, node: NodeId) /// The independent "insert" is the spec's, on every call the parser makes /// ([`insert_requires`]); so are "append" and "pre-insert", which call it. +#[verifier::rlimit(50)] pub proof fn lemma_independent_insert(doc: Document, node: NodeId, parent: NodeId, child: Option) requires insert_requires(doc, node, parent, child), @@ -160,8 +161,10 @@ pub proof fn lemma_independent_insert(doc: Document, node: NodeId, parent: NodeI } let a = ind_insert(doc, node, parent, child); let b = insert(doc, node, parent, child); - assert forall|i: int| 0 <= i < doc.nodes.len() implies #[trigger] a.nodes[i] == b.nodes[i] by {} - assert(a.nodes =~= b.nodes); + let c = attach(d1, node, parent, kids); + assert(b == c); + assert forall|i: int| 0 <= i < doc.nodes.len() implies #[trigger] a.nodes[i] == c.nodes[i] by {} + assert(a.nodes =~= c.nodes); } } // verus! diff --git a/crates/clazel-dom/src/lemmas.rs b/crates/clazel-dom/src/lemmas.rs index b823ef26e..1bd3b10b5 100644 --- a/crates/clazel-dom/src/lemmas.rs +++ b/crates/clazel-dom/src/lemmas.rs @@ -1242,6 +1242,7 @@ pub proof fn lemma_insert_attach_requires(doc: Document, paths: Seq> /// DOM §4.2.3 "insert" keeps the arena well formed, with [`insert_paths`] /// as the witness; paths that do not pass through `node` are unchanged. +#[verifier::rlimit(50)] pub proof fn lemma_insert_wf_with(doc: Document, paths: Seq>, node: NodeId, parent: NodeId, child: Option) requires insert_requires(doc, node, parent, child), @@ -2260,6 +2261,7 @@ pub open spec fn reparent_one(d: Document, from: NodeId, to: NodeId) -> Seq) + requires + wf(doc), + valid(doc, e), + doc.nodes[e as int].kind is Element, + ensures + wf(set_attributes(doc, e, attrs)), + set_attributes(doc, e, attrs).nodes.len() == doc.nodes.len(), + ({ + let old = doc.nodes[e as int]; + let new = set_attributes(doc, e, attrs).nodes[e as int]; + &&& new.kind is Element + &&& new.kind->Element_attributes == attrs + &&& new.kind->Element_namespace == old.kind->Element_namespace + &&& new.kind->Element_prefix == old.kind->Element_prefix + &&& new.kind->Element_local_name == old.kind->Element_local_name + &&& new.kind->Element_template_contents == old.kind->Element_template_contents + &&& new.kind->Element_form_owner == old.kind->Element_form_owner + &&& new.parent == old.parent + &&& new.children == old.children + &&& new.wrapper == old.wrapper + }), + forall|i: int| 0 <= i < doc.nodes.len() && i != e ==> #[trigger] set_attributes(doc, e, attrs).nodes[i] == doc.nodes[i], +{ + let paths = lemma_wf_witness(doc); + let k = set_attributes(doc, e, attrs).nodes[e as int].kind; + assert(set_attributes(doc, e, attrs) == set_kind(doc, e, k)); + lemma_set_kind_wf(doc, paths, e, k); +} + +/// DOM §4.9† "change an attribute" keeps the arena well formed; the +/// attribute is the same but for its value, which is `value`, and every +/// other attribute is as it was. +pub proof fn theorem_change_attribute_wf(doc: Document, e: NodeId, i: nat, value: Seq) + requires + attribute_requires(doc, e, i), + ensures + wf(change_attribute(doc, e, i, value)), + change_attribute(doc, e, i, value).nodes.len() == doc.nodes.len(), + ({ + let old = doc.nodes[e as int].kind->Element_attributes; + let new = change_attribute(doc, e, i, value).nodes[e as int].kind->Element_attributes; + &&& new.len() == old.len() + &&& new[i as int] == (Attr { value, ..old[i as int] }) + &&& forall|j: int| 0 <= j < old.len() && j != i ==> #[trigger] new[j] == old[j] + }), + change_attribute(doc, e, i, value).nodes[e as int].kind is Element, + forall|j: int| 0 <= j < doc.nodes.len() && j != e ==> #[trigger] change_attribute(doc, e, i, value).nodes[j] == doc.nodes[j], +{ + let attrs = doc.nodes[e as int].kind->Element_attributes; + lemma_set_attributes_wf(doc, e, attrs.update(i as int, Attr { value, ..attrs[i as int] })); +} + +/// DOM §4.9† "remove an attribute" keeps the arena well formed; the +/// element's attribute list is its old one without the `i`th. +pub proof fn theorem_remove_attribute_wf(doc: Document, e: NodeId, i: nat) + requires + attribute_requires(doc, e, i), + ensures + wf(remove_attribute(doc, e, i)), + remove_attribute(doc, e, i).nodes.len() == doc.nodes.len(), + remove_attribute(doc, e, i).nodes[e as int].kind is Element, + remove_attribute(doc, e, i).nodes[e as int].kind->Element_attributes == doc.nodes[e as int].kind->Element_attributes.remove(i as int), + remove_attribute(doc, e, i).nodes[e as int].kind->Element_attributes.len() + 1 == doc.nodes[e as int].kind->Element_attributes.len(), + forall|j: int| 0 <= j < doc.nodes.len() && j != e ==> #[trigger] remove_attribute(doc, e, i).nodes[j] == doc.nodes[j], +{ + lemma_set_attributes_wf(doc, e, doc.nodes[e as int].kind->Element_attributes.remove(i as int)); +} + +/// DOM §4.9† "replace an attribute" keeps the arena well formed; the +/// `i`th attribute of the element's list is `new_attr` and the others are +/// as they were. +pub proof fn theorem_replace_attribute_wf(doc: Document, e: NodeId, i: nat, new_attr: Attr) + requires + attribute_requires(doc, e, i), + ensures + wf(replace_attribute(doc, e, i, new_attr)), + replace_attribute(doc, e, i, new_attr).nodes.len() == doc.nodes.len(), + replace_attribute(doc, e, i, new_attr).nodes[e as int].kind is Element, + ({ + let old = doc.nodes[e as int].kind->Element_attributes; + let new = replace_attribute(doc, e, i, new_attr).nodes[e as int].kind->Element_attributes; + &&& new.len() == old.len() + &&& new[i as int] == new_attr + &&& forall|j: int| 0 <= j < old.len() && j != i ==> #[trigger] new[j] == old[j] + }), + forall|j: int| 0 <= j < doc.nodes.len() && j != e ==> #[trigger] replace_attribute(doc, e, i, new_attr).nodes[j] == doc.nodes[j], +{ + lemma_set_attributes_wf(doc, e, doc.nodes[e as int].kind->Element_attributes.update(i as int, new_attr)); +} + +// --------------------------------------------------------------------- +// adopt + +/// DOM §4.5 "adopt" keeps the arena well formed and leaves the node +/// without a parent; no kind changes, no other node's parent changes. +pub proof fn theorem_adopt_wf(doc: Document, node: NodeId) + requires + adopt_requires(doc, node), + ensures + wf(adopt(doc, node)), + adopt(doc, node).nodes.len() == doc.nodes.len(), + adopt(doc, node).nodes[node as int].parent is None, + forall|x: int| #![trigger adopt(doc, node).nodes[x]] 0 <= x < doc.nodes.len() ==> { + &&& adopt(doc, node).nodes[x].kind == doc.nodes[x].kind + &&& adopt(doc, node).nodes[x].wrapper == doc.nodes[x].wrapper + &&& (x != node ==> adopt(doc, node).nodes[x].parent == doc.nodes[x].parent) + }, +{ + if doc.nodes[node as int].parent is Some { + theorem_remove_wf(doc, node); + theorem_remove_keeps_record(doc, node); + } +} + +// --------------------------------------------------------------------- +// clone a node (shallow) + +/// DOM §4.4† "clone a node" (shallow) keeps the arena well formed and adds +/// the copy (and a template's contents): the copy is the next id, has the +/// kind of [`clone_kind`] and no parent or children, a template's +/// contents is the id after it, and no existing node changes. +pub proof fn theorem_clone_node_wf(doc: Document, node: NodeId) + requires + clone_requires(doc, node), + can_create(doc, clone_count(doc, node)), + ensures + wf(clone_node(doc, node).0), + clone_node(doc, node).1 == doc.nodes.len(), + clone_node(doc, node).0.nodes.len() == doc.nodes.len() + clone_count(doc, node), + forall|i: int| 0 <= i < doc.nodes.len() ==> #[trigger] clone_node(doc, node).0.nodes[i] == doc.nodes[i], + ({ + let (d, copy) = clone_node(doc, node); + let k = doc.nodes[node as int].kind; + &&& d.nodes[copy as int].parent is None + &&& d.nodes[copy as int].children.len() == 0 + &&& d.nodes[copy as int].wrapper is None + &&& is_template(k) ==> { + &&& d.nodes[copy as int].kind is Element + &&& d.nodes[copy as int].kind->Element_template_contents == Some((copy + 1) as NodeId) + &&& d.nodes[copy as int].kind->Element_attributes == k->Element_attributes + &&& d.nodes[copy as int].kind->Element_local_name == k->Element_local_name + &&& d.nodes[copy as int].kind->Element_namespace == k->Element_namespace + &&& d.nodes[copy as int].kind->Element_prefix == k->Element_prefix + &&& d.nodes[copy as int].kind->Element_form_owner is None + &&& d.nodes[(copy + 1) as int].kind == (NodeKind::DocumentFragment { host: Some(copy) }) + } + &&& !is_template(k) ==> d.nodes[copy as int].kind == clone_kind(k) + &&& k is Element ==> { + &&& d.nodes[copy as int].kind is Element + &&& d.nodes[copy as int].kind->Element_attributes == k->Element_attributes + &&& d.nodes[copy as int].kind->Element_local_name == k->Element_local_name + &&& d.nodes[copy as int].kind->Element_namespace == k->Element_namespace + &&& d.nodes[copy as int].kind->Element_prefix == k->Element_prefix + &&& d.nodes[copy as int].kind->Element_form_owner is None + } + &&& !(k is Element) && !(k is DocumentFragment) ==> d.nodes[copy as int].kind == k + &&& k is DocumentFragment ==> d.nodes[copy as int].kind == (NodeKind::DocumentFragment { host: None }) + }), +{ + let k = doc.nodes[node as int].kind; + let ck = clone_kind(k); + assert(creatable(doc, ck)); + theorem_create_node_wf(doc, ck); + let (d1, copy) = create_node(doc, ck); + if is_template(k) { + assert(d1.nodes[copy as int].kind is Element); + assert(d1.nodes[copy as int].kind->Element_template_contents is None); + theorem_create_template_contents_wf(d1, copy); + theorem_create_template_contents_frame(d1, copy); + } +} + + +// --------------------------------------------------------------------- +// Helpers: ancestors, next sibling + +/// An inclusive ancestor is a host-including inclusive ancestor, with the +/// same fuel. +pub proof fn lemma_incl_implies_host(doc: Document, a: NodeId, x: NodeId, fuel: nat) + ensures + inclusive_ancestor_within(doc, a, x, fuel) ==> host_including_within(doc, a, x, fuel), + decreases fuel, +{ + if x != a && fuel > 0 { + if let Some(p) = parent(doc, x) { + lemma_incl_implies_host(doc, a, p, (fuel - 1) as nat); + } + } +} + +/// A node that is not a host-including inclusive ancestor of `x` is not an +/// inclusive ancestor of it. +pub proof fn lemma_not_host_not_incl(doc: Document, a: NodeId, x: NodeId) + requires + !is_host_including_inclusive_ancestor(doc, a, x), + ensures + !is_inclusive_ancestor(doc, a, x), +{ + lemma_incl_implies_host(doc, a, x, doc.nodes.len()); +} + +/// What "next sibling" is, for a node with a parent in a well-formed +/// arena: the entry after it in its parent's children, which is a child of +/// the same parent, another node, and at the next index. +pub proof fn lemma_next_sibling(doc: Document, n: NodeId) + requires + wf(doc), + valid(doc, n), + parent(doc, n) is Some, + ensures + ({ + let p = parent(doc, n)->Some_0; + let kids = children(doc, p); + let i = index_of(kids, n); + &&& valid(doc, p) + &&& 0 <= i < kids.len() + &&& kids[i] == n + &&& match next_sibling(doc, n) { + Some(x) => { + &&& i + 1 < kids.len() + &&& x == kids[i + 1] + &&& valid(doc, x) + &&& parent(doc, x) == Some(p) + &&& x != n + &&& index_of(kids, x) == i + 1 + }, + None => i + 1 == kids.len(), + } + }), +{ + let paths = lemma_wf_witness(doc); + lemma_wf_split(doc, paths); + let p = parent(doc, n)->Some_0; + let kids = children(doc, p); + assert(kids.contains(n)); + lemma_index_of(kids, n); + let i = index_of(kids, n); + assert(kids.no_duplicates()); + if i + 1 < kids.len() { + lemma_index_of_unique(kids, i + 1); + let x = kids[i + 1]; + assert(doc.nodes[p as int].children[i + 1] == x); + assert(x != n); + } +} + +proof fn lemma_remove_paths_contains(paths: Seq>, node: NodeId, x: int, a: NodeId) + requires + 0 <= x < paths.len(), + node < paths.len(), + paths[node as int].len() >= 1, + paths[x].contains(node) ==> paths[node as int].len() <= paths[x].len(), + ensures + remove_paths(paths, node)[x].contains(a) ==> paths[x].contains(a), +{ + let q = remove_paths(paths, node); + if q[x].contains(a) { + if paths[x].contains(node) { + let m = paths[node as int].len() - 1; + let s = paths[x].subrange(m, paths[x].len() as int); + assert(q[x] == s); + let k = choose|k: int| 0 <= k < s.len() && #[trigger] s[k] == a; + assert(0 <= m); + assert(s[k] == paths[x][m + k]); + assert(paths[x][m + k] == a); + assert(paths[x].contains(a)); + } else { + assert(q[x] == paths[x]); + } + } +} + +/// Removing a node never makes something an ancestor that was not: an +/// inclusive ancestor in the arena after is one before. +pub proof fn lemma_remove_ancestor_mono(doc: Document, node: NodeId, a: NodeId, x: NodeId) + requires + super::contract::remove_requires(doc, node), + valid(doc, x), + ensures + is_inclusive_ancestor(remove(doc, node), a, x) ==> is_inclusive_ancestor(doc, a, x), +{ + let paths = lemma_wf_witness(doc); + lemma_remove_wf_with(doc, paths, node); + lemma_remove_nodes(doc, paths, node); + let d = remove(doc, node); + let q = remove_paths(paths, node); + assert(valid(d, x)); + lemma_ancestor_iff_path(doc, paths, a, x); + lemma_ancestor_iff_path(d, q, a, x); + lemma_path(doc, paths, node); + if paths[x as int].contains(node) { + lemma_path_prefix(doc, paths, node, x); + } + lemma_remove_paths_contains(paths, node, x as int, a); +} + +// --------------------------------------------------------------------- +// Removing all of a node's children + +/// The records after the first `k` children of `p` have been removed: `p` +/// has lost them from its children and each has no parent. +pub open spec fn removed_prefix(d: Document, p: NodeId, k: int) -> Seq { + let kids = d.nodes[p as int].children; + Seq::new( + d.nodes.len(), + |x: int| + if x == p { + Node { children: kids.subrange(k, kids.len() as int), ..d.nodes[x] } + } else if kids.subrange(0, k).contains(x as NodeId) { + Node { parent: None, ..d.nodes[x] } + } else { + d.nodes[x] + }, + ) +} + +/// Dropping the first element moves the prefix by one. +proof fn lemma_contains_shift(kids: Seq, k: int, x: NodeId) + requires + 1 <= k <= kids.len(), + ensures + kids.subrange(0, k).contains(kids[0]), + x != kids[0] ==> (kids.subrange(0, k).contains(x) <==> kids.remove(0).subrange(0, k - 1).contains(x)), +{ + let a = kids.subrange(0, k); + let b = kids.remove(0).subrange(0, k - 1); + assert(a[0] == kids[0]); + if x != kids[0] { + if a.contains(x) { + let j = choose|j: int| 0 <= j < a.len() && a[j] == x; + assert(j >= 1); + assert(b[j - 1] == x); + } + if b.contains(x) { + let j = choose|j: int| 0 <= j < b.len() && b[j] == x; + assert(a[j + 1] == x); + } + } +} + +/// Removing the first child of `p` and then `k - 1` more is removing `k`: +/// the records after the one removal and `k - 1` more prefix are +/// [`removed_prefix`] with `k`. +#[verifier::rlimit(50)] +proof fn lemma_removed_prefix_step(d: Document, p: NodeId, k: int) + requires + wf(d), + valid(d, p), + 1 <= k <= d.nodes[p as int].children.len(), + ensures + ({ + let c0 = d.nodes[p as int].children[0]; + &&& super::contract::remove_requires(d, c0) + &&& wf(remove(d, c0)) + &&& valid(remove(d, c0), p) + &&& children(remove(d, c0), p).len() + 1 == d.nodes[p as int].children.len() + &&& removed_prefix(remove(d, c0), p, k - 1) == removed_prefix(d, p, k) + }), +{ + let kids = d.nodes[p as int].children; + let n = d.nodes.len(); + let paths = lemma_wf_witness(d); + lemma_wf_split(d, paths); + let c0 = kids[0]; + assert(d.nodes[p as int].children[0] == c0); + assert(c0 < n); + assert(d.nodes[c0 as int].parent == Some(p)); + assert(super::contract::remove_requires(d, c0)); + let d1 = remove(d, c0); + theorem_remove_wf(d, c0); + theorem_remove_keeps_order(d, c0); + theorem_remove_keeps_record(d, c0); + assert(kids.no_duplicates()); + lemma_index_of_unique(kids, 0); + let kids1 = kids.remove(0); + assert(children(d1, p) == kids1); + assert(kids1 =~= kids.subrange(1, kids.len() as int)); + lemma_seq_remove(kids, 0); + let r = removed_prefix(d, p, k); + let r1 = removed_prefix(d1, p, k - 1); + assert(c0 != p); + assert(d1.nodes.len() == n); + assert forall|x: int| 0 <= x < n implies #[trigger] r1[x] == r[x] by { + let xn = x as NodeId; + if x == p { + assert(kids1.subrange(k - 1, kids1.len() as int) =~= kids.subrange(k, kids.len() as int)); + assert(d1.nodes[x].kind == d.nodes[x].kind); + assert(d1.nodes[x].parent == d.nodes[x].parent); + assert(d1.nodes[x].wrapper == d.nodes[x].wrapper); + } else { + lemma_contains_shift(kids, k, xn); + if x == c0 { + assert(!kids1.contains(c0)); + assert(!kids1.subrange(0, k - 1).contains(c0)) by { + if kids1.subrange(0, k - 1).contains(c0) { + let j = choose|j: int| 0 <= j < k - 1 && #[trigger] kids1.subrange(0, k - 1)[j] == c0; + assert(kids1[j] == c0); + } + } + assert(d1.nodes[x].kind == d.nodes[x].kind); + assert(d1.nodes[x].children == d.nodes[x].children); + assert(d1.nodes[x].wrapper == d.nodes[x].wrapper); + } else { + assert(d1.nodes[x] == d.nodes[x]); + } + } + } + assert(r1 =~= r); +} + +/// Nothing removed is the arena itself. +proof fn lemma_removed_prefix_zero(d: Document, p: NodeId) + requires + valid(d, p), + ensures + removed_prefix(d, p, 0) == d.nodes, +{ + let kids = d.nodes[p as int].children; + let r = removed_prefix(d, p, 0); + assert(kids.subrange(0, kids.len() as int) =~= kids); + assert forall|x: int| 0 <= x < d.nodes.len() implies #[trigger] r[x] == d.nodes[x] by { + if x != p { + assert(!kids.subrange(0, 0).contains(x as NodeId)); + } + } + assert(r =~= d.nodes); +} + +/// Removing the first `k` children of `p` one by one keeps the arena well +/// formed and gives [`removed_prefix`]. +pub proof fn lemma_remove_children_n(d: Document, p: NodeId, k: nat) + requires + wf(d), + valid(d, p), + k <= d.nodes[p as int].children.len(), + ensures + wf(remove_children_n(d, p, k)), + remove_children_n(d, p, k).nodes == removed_prefix(d, p, k as int), + decreases k, +{ + let kids = d.nodes[p as int].children; + if k == 0 { + lemma_removed_prefix_zero(d, p); + } else { + lemma_removed_prefix_step(d, p, k as int); + let c0 = kids[0]; + let d1 = remove(d, c0); + assert(remove_children_n(d, p, k) == remove_children_n(d1, p, (k - 1) as nat)); + lemma_remove_children_n(d1, p, (k - 1) as nat); + assert((k - 1) as nat as int == k as int - 1); + } +} + +/// Removing the first `k` children of `p` never makes a node an ancestor +/// that was not. +pub proof fn lemma_remove_children_n_ancestors(d: Document, p: NodeId, k: nat, a: NodeId, x: NodeId) + requires + wf(d), + valid(d, p), + k <= d.nodes[p as int].children.len(), + valid(d, x), + ensures + is_inclusive_ancestor(remove_children_n(d, p, k), a, x) ==> is_inclusive_ancestor(d, a, x), + decreases k, +{ + let kids = d.nodes[p as int].children; + if k > 0 { + lemma_removed_prefix_step(d, p, k as int); + let c0 = kids[0]; + let d1 = remove(d, c0); + assert(remove_children_n(d, p, k) == remove_children_n(d1, p, (k - 1) as nat)); + lemma_remove_children_n_ancestors(d1, p, (k - 1) as nat, a, x); + lemma_remove_ancestor_mono(d, c0, a, x); + } +} + +// --------------------------------------------------------------------- +// Inserting a list of nodes + +/// What `parent`'s children `kids` become when `nodes` are inserted before +/// `child`, in order, each before it: the list goes where `child` was. +pub open spec fn list_kids(kids: Seq, nodes: Seq, child: Option) -> Seq { + match child { + None => kids + nodes, + Some(c) => { + let i = index_of(kids, c); + kids.subrange(0, i) + nodes + kids.subrange(i, kids.len() as int) + }, + } +} + +/// The records after [`insert_list`]: `parent`'s children are +/// [`list_kids`], each inserted node has `parent` as its parent. +pub open spec fn list_inserted(d: Document, nodes: Seq, parent: NodeId, child: Option) -> Seq { + Seq::new( + d.nodes.len(), + |x: int| + if x == parent { + Node { children: list_kids(d.nodes[parent as int].children, nodes, child), ..d.nodes[x] } + } else if nodes.contains(x as NodeId) { + Node { parent: Some(parent), ..d.nodes[x] } + } else { + d.nodes[x] + }, + ) +} + +/// Inserting `n0` and then the rest is inserting the list. +proof fn lemma_list_kids_step(kids: Seq, n0: NodeId, rest: Seq, child: Option) + requires + kids.no_duplicates(), + !kids.contains(n0), + child is Some ==> kids.contains(child->Some_0), + ensures + list_kids(insert_into(kids, n0, child), rest, child) == list_kids(kids, seq![n0] + rest, child), +{ + match child { + None => { + assert(kids.push(n0) + rest =~= kids + (seq![n0] + rest)); + }, + Some(c) => { + lemma_index_of(kids, c); + let i = index_of(kids, c); + lemma_seq_insert(kids, i, n0); + let kids1 = kids.insert(i, n0); + assert(kids1[i + 1] == kids[i]); + assert(kids1[i + 1] == c); + lemma_index_of_unique(kids1, i + 1); + assert(index_of(kids1, c) == i + 1); + assert(kids1.subrange(0, i + 1) =~= kids.subrange(0, i).push(n0)); + assert(kids1.subrange(i + 1, kids1.len() as int) =~= kids.subrange(i, kids.len() as int)); + assert(kids.subrange(0, i).push(n0) + rest + kids.subrange(i, kids.len() as int) =~= kids.subrange(0, i) + (seq![n0] + rest) + + kids.subrange(i, kids.len() as int)); + }, + } +} + +/// What [`insert_list`] asks of its arguments. +pub open spec fn insert_list_requires( + d: Document, + paths: Seq>, + nodes: Seq, + parent: NodeId, + child: Option, +) -> bool { + &&& wf_with(d, paths) + &&& valid(d, parent) + &&& may_have_children(d.nodes[parent as int].kind) + &&& nodes.no_duplicates() + &&& forall|j: int| 0 <= j < nodes.len() ==> { + &&& (#[trigger] nodes[j]) < d.nodes.len() + &&& d.nodes[nodes[j] as int].parent is None + &&& !(d.nodes[nodes[j] as int].kind is Document) + &&& !(d.nodes[nodes[j] as int].kind is DocumentFragment) + &&& !paths[parent as int].contains(nodes[j]) + } + &&& match child { + None => true, + Some(c) => valid(d, c) && d.nodes[c as int].parent == Some(parent), + } +} + +/// The first step of [`insert_list`] is a valid "insert" that keeps the +/// requirements of the rest. +proof fn lemma_insert_list_step_requires(d: Document, paths: Seq>, nodes: Seq, parent: NodeId, child: Option) + requires + insert_list_requires(d, paths, nodes, parent, child), + nodes.len() > 0, + ensures + ({ + let n0 = nodes[0]; + let rest = nodes.drop_first(); + let d1 = insert(d, n0, parent, child); + let q = insert_paths(d, paths, n0, parent); + &&& insert_requires(d, n0, parent, child) + &&& wf_with(d1, q) + &&& d1.nodes.len() == d.nodes.len() + &&& insert_list_requires(d1, q, rest, parent, child) + }), +{ + let n = d.nodes.len(); + lemma_wf_split(d, paths); + lemma_path(d, paths, parent); + let n0 = nodes[0]; + let rest = nodes.drop_first(); + assert(nodes[0] < n); + assert(!paths[parent as int].contains(n0)); + assert(n0 != parent); + lemma_ancestor_iff_path(d, paths, n0, parent); + assert(insert_requires(d, n0, parent, child)) by { + if let Some(c) = child { + assert(d.nodes[c as int].parent is Some); + assert(c != n0); + } + } + let d1 = insert(d, n0, parent, child); + lemma_insert_is_attach(d, n0, parent, child); + assert(adopt(d, n0) == d); + lemma_insert_attach_requires(d, paths, n0, parent, child); + let kids = d.nodes[parent as int].children; + let kids1 = insert_kids(d, n0, parent, child); + lemma_attach_nodes(d, paths, n0, parent, kids1); + lemma_insert_wf_with(d, paths, n0, parent, child); + let q = insert_paths(d, paths, n0, parent); + assert(q[parent as int] == paths[parent as int]); + assert(d1.nodes.len() == n); + assert forall|j: int| 0 <= j < rest.len() implies { + &&& (#[trigger] rest[j]) < d1.nodes.len() + &&& d1.nodes[rest[j] as int].parent is None + &&& !(d1.nodes[rest[j] as int].kind is Document) + &&& !(d1.nodes[rest[j] as int].kind is DocumentFragment) + &&& !q[parent as int].contains(rest[j]) + } by { + assert(rest[j] == nodes[j + 1]); + assert(rest[j] != n0); + assert(rest[j] != parent); + } + assert(rest.no_duplicates()); + if let Some(c) = child { + assert(c != n0); + lemma_parent_not_self(d, paths, c); + assert(c != parent); + } +} + +/// Dropping the first element keeps the others. +proof fn lemma_drop_first_contains(nodes: Seq, x: NodeId) + requires + nodes.len() > 0, + x != nodes[0], + ensures + nodes.contains(x) <==> nodes.drop_first().contains(x), +{ + if nodes.contains(x) { + let j = choose|j: int| 0 <= j < nodes.len() && nodes[j] == x; + assert(j != 0); + assert(nodes.drop_first()[j - 1] == nodes[j]); + } + if nodes.drop_first().contains(x) { + let j = choose|j: int| 0 <= j < nodes.drop_first().len() && nodes.drop_first()[j] == x; + assert(nodes[j + 1] == x); + } +} + +/// What the first step of [`insert_list`] does to the records: `parent` +/// gets `n0` among its children, `n0` gets `parent` as its parent, and +/// nothing else changes. +proof fn lemma_insert_list_step_nodes(d: Document, paths: Seq>, nodes: Seq, parent: NodeId, child: Option) + requires + insert_list_requires(d, paths, nodes, parent, child), + nodes.len() > 0, + ensures + ({ + let n0 = nodes[0]; + let d1 = insert(d, n0, parent, child); + let kids = d.nodes[parent as int].children; + &&& n0 != parent + &&& d1.nodes.len() == d.nodes.len() + &&& !kids.contains(n0) + &&& d1.nodes[parent as int] == (Node { children: insert_into(kids, n0, child), ..d.nodes[parent as int] }) + &&& d1.nodes[n0 as int] == (Node { parent: Some(parent), ..d.nodes[n0 as int] }) + &&& forall|x: int| 0 <= x < d.nodes.len() && x != parent && x != n0 ==> #[trigger] d1.nodes[x] == d.nodes[x] + }), +{ + lemma_wf_split(d, paths); + lemma_path(d, paths, parent); + let n0 = nodes[0]; + assert(nodes[0] < d.nodes.len()); + assert(!paths[parent as int].contains(n0)); + assert(n0 != parent); + lemma_ancestor_iff_path(d, paths, n0, parent); + assert(insert_requires(d, n0, parent, child)) by { + if let Some(c) = child { + assert(d.nodes[c as int].parent is Some); + assert(c != n0); + } + } + lemma_insert_is_attach(d, n0, parent, child); + assert(adopt(d, n0) == d); + lemma_insert_attach_requires(d, paths, n0, parent, child); + let kids1 = insert_kids(d, n0, parent, child); + lemma_attach_nodes(d, paths, n0, parent, kids1); + assert(kids1 == insert_into(d.nodes[parent as int].children, n0, child)); +} + +/// The records after the first step of [`insert_list`] and then the rest +/// are those of the whole list. +#[verifier::rlimit(50)] +proof fn lemma_insert_list_step_records(d: Document, paths: Seq>, nodes: Seq, parent: NodeId, child: Option) + requires + insert_list_requires(d, paths, nodes, parent, child), + nodes.len() > 0, + ensures + list_inserted(insert(d, nodes[0], parent, child), nodes.drop_first(), parent, child) == list_inserted(d, nodes, parent, child), +{ + let n = d.nodes.len(); + lemma_wf_split(d, paths); + let n0 = nodes[0]; + let rest = nodes.drop_first(); + let kids = d.nodes[parent as int].children; + lemma_insert_list_step_nodes(d, paths, nodes, parent, child); + let d1 = insert(d, n0, parent, child); + lemma_listed(d, paths, n0, parent, child); + if let Some(c) = child { + assert(d.nodes[c as int].parent == Some(parent)); + assert(kids.contains(c)); + } + lemma_list_kids_step(kids, n0, rest, child); + assert(seq![n0] + rest =~= nodes); + assert(nodes.no_duplicates()); + assert(!rest.contains(n0)) by { + if rest.contains(n0) { + let j = choose|j: int| 0 <= j < rest.len() && rest[j] == n0; + assert(nodes[j + 1] == n0); + } + } + assert(nodes.contains(n0)); + let a = list_inserted(d1, rest, parent, child); + let b = list_inserted(d, nodes, parent, child); + assert forall|x: int| 0 <= x < n implies #[trigger] a[x] == b[x] by { + if x != parent && x != n0 { + lemma_drop_first_contains(nodes, x as NodeId); + } + } + assert(a =~= b); +} + +/// No nodes inserted is the arena itself. +proof fn lemma_list_inserted_empty(d: Document, parent: NodeId, child: Option) + requires + wf(d), + valid(d, parent), + match child { + None => true, + Some(c) => valid(d, c) && d.nodes[c as int].parent == Some(parent), + }, + ensures + list_inserted(d, seq![], parent, child) == d.nodes, +{ + let paths = lemma_wf_witness(d); + lemma_wf_split(d, paths); + let nodes = Seq::::empty(); + let kids = d.nodes[parent as int].children; + if let Some(c) = child { + assert(kids.contains(c)); + lemma_index_of(kids, c); + let i = index_of(kids, c); + assert(kids.subrange(0, i) + nodes + kids.subrange(i, kids.len() as int) =~= kids); + } else { + assert(kids + nodes =~= kids); + } + assert(list_kids(kids, nodes, child) =~= kids); + let r = list_inserted(d, nodes, parent, child); + assert forall|x: int| 0 <= x < d.nodes.len() implies #[trigger] r[x] == d.nodes[x] by { + if x != parent { + assert(!nodes.contains(x as NodeId)); + } + } + assert(r =~= d.nodes); +} + +/// Inserting parentless nodes, one after the other, before `child`, keeps +/// the arena well formed and gives [`list_inserted`]. +pub proof fn lemma_insert_list(d: Document, paths: Seq>, nodes: Seq, parent: NodeId, child: Option) + requires + insert_list_requires(d, paths, nodes, parent, child), + ensures + wf(insert_list(d, nodes, parent, child)), + insert_list(d, nodes, parent, child).nodes == list_inserted(d, nodes, parent, child), + decreases nodes.len(), +{ + if nodes.len() == 0 { + assert(nodes =~= seq![]); + lemma_list_inserted_empty(d, parent, child); + } else { + lemma_insert_list_step_requires(d, paths, nodes, parent, child); + lemma_insert_list_step_records(d, paths, nodes, parent, child); + let d1 = insert(d, nodes[0], parent, child); + let q = insert_paths(d, paths, nodes[0], parent); + lemma_insert_list(d1, q, nodes.drop_first(), parent, child); + } +} + +// --------------------------------------------------------------------- +// insert, for any node + +/// What [`insert_any`] asks of its arguments: [`insert_requires`] without +/// the restriction to non-fragments (a fragment must not be an inclusive +/// ancestor of `parent`, which excludes `parent` itself). +pub open spec fn insert_any_requires(doc: Document, node: NodeId, parent: NodeId, child: Option) -> bool { + &&& wf(doc) + &&& valid(doc, node) + &&& valid(doc, parent) + &&& !(doc.nodes[node as int].kind is Document) + &&& may_have_children(doc.nodes[parent as int].kind) + &&& !is_inclusive_ancestor(doc, node, parent) + &&& match child { + None => true, + Some(c) => valid(doc, c) && c != node && doc.nodes[c as int].parent == Some(parent), + } +} + +/// The records after inserting the fragment `frag`: its children have +/// moved, in order, to `parent`'s children (before `child`, or at the +/// end) and have `parent` as their parent; `frag` has no children. +pub open spec fn fragment_inserted(d: Document, frag: NodeId, parent: NodeId, child: Option) -> Seq { + let nodes = d.nodes[frag as int].children; + Seq::new( + d.nodes.len(), + |x: int| + if x == frag { + Node { children: seq![], ..d.nodes[x] } + } else if x == parent { + Node { children: list_kids(d.nodes[parent as int].children, nodes, child), ..d.nodes[x] } + } else if nodes.contains(x as NodeId) { + Node { parent: Some(parent), ..d.nodes[x] } + } else { + d.nodes[x] + }, + ) +} + +/// A child of `frag` is not an inclusive ancestor of `parent` if `frag` +/// is not. +proof fn lemma_frag_child_not_ancestor(d: Document, paths: Seq>, frag: NodeId, parent: NodeId, c: NodeId) + requires + wf_with(d, paths), + valid(d, frag), + valid(d, parent), + valid(d, c), + d.nodes[c as int].parent == Some(frag), + !is_inclusive_ancestor(d, frag, parent), + ensures + !is_inclusive_ancestor(d, c, parent), +{ + lemma_ancestor_iff_path(d, paths, c, parent); + lemma_ancestor_iff_path(d, paths, frag, parent); + if paths[parent as int].contains(c) { + lemma_path_prefix(d, paths, c, parent); + lemma_path(d, paths, frag); + assert(paths[c as int] == paths[frag as int].push(c)); + let m = paths[frag as int].len() - 1; + assert(paths[c as int][m] == paths[frag as int][m]); + assert(paths[c as int].contains(frag)); + } +} + +/// What [`insert_any_requires`] gives about the children of a fragment +/// being inserted. +pub proof fn lemma_frag_facts(d: Document, paths: Seq>, frag: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(d, frag, parent, child), + wf_with(d, paths), + d.nodes[frag as int].kind is DocumentFragment, + ensures + ({ + let nodes = d.nodes[frag as int].children; + let kids = d.nodes[parent as int].children; + &&& parent != frag + &&& nodes.no_duplicates() + &&& forall|j: int| 0 <= j < nodes.len() ==> { + &&& (#[trigger] nodes[j]) < d.nodes.len() + &&& d.nodes[nodes[j] as int].parent == Some(frag) + &&& nodes[j] != frag + &&& !is_inclusive_ancestor(d, nodes[j], parent) + &&& nodes[j] != parent + &&& !(d.nodes[nodes[j] as int].kind is Document) + &&& !(d.nodes[nodes[j] as int].kind is DocumentFragment) + } + &&& !nodes.contains(parent) + &&& !nodes.contains(frag) + &&& match child { + None => true, + Some(c) => !nodes.contains(c) && c != frag && kids.contains(c), + } + }), +{ + let n = d.nodes.len(); + lemma_wf_split(d, paths); + let nodes = d.nodes[frag as int].children; + let kids = d.nodes[parent as int].children; + assert(parent != frag); + assert(nodes.no_duplicates()); + assert forall|j: int| 0 <= j < nodes.len() implies { + &&& (#[trigger] nodes[j]) < n + &&& d.nodes[nodes[j] as int].parent == Some(frag) + &&& nodes[j] != frag + &&& !is_inclusive_ancestor(d, nodes[j], parent) + &&& nodes[j] != parent + &&& !(d.nodes[nodes[j] as int].kind is Document) + &&& !(d.nodes[nodes[j] as int].kind is DocumentFragment) + } by { + let c = nodes[j]; + assert(d.nodes[frag as int].children[j] == c); + lemma_parent_not_self(d, paths, c); + lemma_frag_child_not_ancestor(d, paths, frag, parent, c); + assert(d.nodes[c as int].parent is Some); + assert(c != 0); + } + if let Some(c) = child { + assert(d.nodes[c as int].parent == Some(parent)); + assert(!nodes.contains(c)) by { + if nodes.contains(c) { + let j = choose|j: int| 0 <= j < nodes.len() && nodes[j] == c; + assert(d.nodes[nodes[j] as int].parent == Some(frag)); + } + } + assert(kids.contains(c)); + } + assert(!nodes.contains(parent)) by { + if nodes.contains(parent) { + let j = choose|j: int| 0 <= j < nodes.len() && nodes[j] == parent; + assert(nodes[j] != parent); + } + } + assert(!nodes.contains(frag)) by { + if nodes.contains(frag) { + let j = choose|j: int| 0 <= j < nodes.len() && nodes[j] == frag; + assert(nodes[j] != frag); + } + } +} + +/// An empty fragment inserts nothing. +#[verifier::rlimit(50)] +proof fn lemma_insert_fragment_empty(d: Document, frag: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(d, frag, parent, child), + d.nodes[frag as int].kind is DocumentFragment, + d.nodes[frag as int].children.len() == 0, + ensures + wf(insert_fragment(d, frag, parent, child)), + insert_fragment(d, frag, parent, child).nodes == fragment_inserted(d, frag, parent, child), +{ + let n = d.nodes.len(); + let paths = lemma_wf_witness(d); + lemma_frag_facts(d, paths, frag, parent, child); + let nodes = d.nodes[frag as int].children; + let kids = d.nodes[parent as int].children; + assert(nodes =~= seq![]); + let r = fragment_inserted(d, frag, parent, child); + if let Some(c) = child { + lemma_index_of(kids, c); + } + assert(list_kids(kids, nodes, child) =~= kids) by { + if let Some(c) = child { + let i = index_of(kids, c); + assert(kids.subrange(0, i) + nodes + kids.subrange(i, kids.len() as int) =~= kids); + } else { + assert(kids + nodes =~= kids); + } + } + assert forall|x: int| 0 <= x < n implies #[trigger] r[x] == d.nodes[x] by { + if x == frag { + assert(d.nodes[x].children =~= seq![]); + } else if x != parent { + assert(!nodes.contains(x as NodeId)); + } + } + assert(r =~= d.nodes); +} + +/// A fragment with children: the arena after step 4.1. +proof fn lemma_insert_fragment_removed(d: Document, paths: Seq>, frag: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(d, frag, parent, child), + wf_with(d, paths), + d.nodes[frag as int].kind is DocumentFragment, + d.nodes[frag as int].children.len() > 0, + ensures + ({ + let nodes = d.nodes[frag as int].children; + let d1 = remove_children(d, frag); + &&& wf(d1) + &&& d1.nodes == removed_prefix(d, frag, nodes.len() as int) + &&& d1.nodes.len() == d.nodes.len() + &&& d1.nodes[parent as int] == d.nodes[parent as int] + }), +{ + let n = d.nodes.len(); + lemma_frag_facts(d, paths, frag, parent, child); + let nodes = d.nodes[frag as int].children; + let len = nodes.len(); + lemma_remove_children_n(d, frag, len); + let d1 = remove_children(d, frag); + assert(children(d, frag) == nodes); + assert(d1.nodes == removed_prefix(d, frag, len as int)); + assert(nodes.subrange(0, len as int) =~= nodes); + assert(d1.nodes.len() == n); + assert(d1.nodes[parent as int] == d.nodes[parent as int]); +} + +/// The arena after step 4.1 meets what [`insert_list`] asks. +proof fn lemma_insert_fragment_requires( + d: Document, + paths: Seq>, + paths1: Seq>, + frag: NodeId, + parent: NodeId, + child: Option, +) + requires + insert_any_requires(d, frag, parent, child), + wf_with(d, paths), + d.nodes[frag as int].kind is DocumentFragment, + d.nodes[frag as int].children.len() > 0, + wf_with(remove_children(d, frag), paths1), + ensures + insert_list_requires(remove_children(d, frag), paths1, d.nodes[frag as int].children, parent, child), +{ + let nodes = d.nodes[frag as int].children; + let len = nodes.len(); + lemma_frag_facts(d, paths, frag, parent, child); + lemma_insert_fragment_removed(d, paths, frag, parent, child); + let d1 = remove_children(d, frag); + assert forall|j: int| 0 <= j < nodes.len() implies { + &&& (#[trigger] nodes[j]) < d1.nodes.len() + &&& d1.nodes[nodes[j] as int].parent is None + &&& !(d1.nodes[nodes[j] as int].kind is Document) + &&& !(d1.nodes[nodes[j] as int].kind is DocumentFragment) + &&& !paths1[parent as int].contains(nodes[j]) + } by { + let c = nodes[j]; + assert(nodes.subrange(0, len as int)[j] == c); + assert(nodes.subrange(0, len as int).contains(c)); + lemma_remove_children_n_ancestors(d, frag, len, c, parent); + lemma_ancestor_iff_path(d1, paths1, c, parent); + } + if let Some(c) = child { + assert(!nodes.subrange(0, len as int).contains(c)); + assert(d1.nodes[c as int] == d.nodes[c as int]); + } +} + +/// The records after step 4.1 and the list of step 7 are +/// [`fragment_inserted`]. +proof fn lemma_fragment_records(d: Document, frag: NodeId, parent: NodeId, child: Option) + requires + valid(d, frag), + valid(d, parent), + parent != frag, + d.nodes[frag as int].children.len() > 0, + !d.nodes[frag as int].children.contains(frag), + !d.nodes[frag as int].children.contains(parent), + d.nodes[parent as int] == removed_prefix(d, frag, d.nodes[frag as int].children.len() as int)[parent as int], + ensures + list_inserted( + Document { nodes: removed_prefix(d, frag, d.nodes[frag as int].children.len() as int) }, + d.nodes[frag as int].children, + parent, + child, + ) == fragment_inserted(d, frag, parent, child), +{ + let n = d.nodes.len(); + let nodes = d.nodes[frag as int].children; + let len = nodes.len(); + let d1 = Document { nodes: removed_prefix(d, frag, len as int) }; + let a = list_inserted(d1, nodes, parent, child); + let b = fragment_inserted(d, frag, parent, child); + assert(nodes.subrange(0, len as int) =~= nodes); + assert(nodes.subrange(len as int, len as int) =~= seq![]); + assert forall|x: int| 0 <= x < n implies #[trigger] a[x] == b[x] by { + if x == frag { + } else if x == parent { + } else if nodes.contains(x as NodeId) { + } else { + } + } + assert(a =~= b); +} + +/// Inserting a fragment keeps the arena well formed and gives +/// [`fragment_inserted`]. +pub proof fn lemma_insert_fragment(d: Document, frag: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(d, frag, parent, child), + d.nodes[frag as int].kind is DocumentFragment, + ensures + wf(insert_fragment(d, frag, parent, child)), + insert_fragment(d, frag, parent, child).nodes == fragment_inserted(d, frag, parent, child), +{ + let nodes = d.nodes[frag as int].children; + if nodes.len() == 0 { + lemma_insert_fragment_empty(d, frag, parent, child); + } else { + let paths = lemma_wf_witness(d); + lemma_insert_fragment_removed(d, paths, frag, parent, child); + lemma_frag_facts(d, paths, frag, parent, child); + let d1 = remove_children(d, frag); + let paths1 = lemma_wf_witness(d1); + lemma_insert_fragment_requires(d, paths, paths1, frag, parent, child); + lemma_insert_list(d1, paths1, nodes, parent, child); + assert(insert_list(d1, nodes, parent, child).nodes == list_inserted(d1, nodes, parent, child)); + lemma_fragment_records(d, frag, parent, child); + assert(d1 == Document { nodes: removed_prefix(d, frag, nodes.len() as int) }); + } +} + +/// What [`fragment_inserted`] keeps of each record. +proof fn lemma_fragment_frame(d: Document, frag: NodeId, parent: NodeId, child: Option) + requires + valid(d, frag), + valid(d, parent), + parent != frag, + d.nodes.len() <= max_nodes(), + ensures + forall|x: int| 0 <= x < d.nodes.len() ==> { + &&& (#[trigger] fragment_inserted(d, frag, parent, child)[x]).kind == d.nodes[x].kind + &&& fragment_inserted(d, frag, parent, child)[x].wrapper == d.nodes[x].wrapper + &&& (x != frag && !d.nodes[frag as int].children.contains(x as NodeId) ==> fragment_inserted(d, frag, parent, child)[x].parent == d.nodes[x].parent) + }, +{ + let f = fragment_inserted(d, frag, parent, child); + assert forall|x: int| 0 <= x < d.nodes.len() implies { + &&& (#[trigger] f[x]).kind == d.nodes[x].kind + &&& f[x].wrapper == d.nodes[x].wrapper + &&& (x != frag && !d.nodes[frag as int].children.contains(x as NodeId) ==> f[x].parent == d.nodes[x].parent) + } by { + if x == frag { + } else if x == parent { + } else if d.nodes[frag as int].children.contains(x as NodeId) { + } else { + } + } +} + +/// [`theorem_insert_any_wf`] for a fragment. +#[verifier::rlimit(100)] +proof fn lemma_insert_any_frag(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + doc.nodes[node as int].kind is DocumentFragment, + ensures + wf(insert_any(doc, node, parent, child)), + insert_any(doc, node, parent, child).nodes.len() == doc.nodes.len(), + forall|x: int| #![trigger insert_any(doc, node, parent, child).nodes[x]] 0 <= x < doc.nodes.len() ==> { + &&& insert_any(doc, node, parent, child).nodes[x].kind == doc.nodes[x].kind + &&& insert_any(doc, node, parent, child).nodes[x].wrapper == doc.nodes[x].wrapper + &&& (x != node && !(doc.nodes[node as int].kind is DocumentFragment && doc.nodes[node as int].children.contains(x as NodeId)) + ==> insert_any(doc, node, parent, child).nodes[x].parent == doc.nodes[x].parent) + }, + insert_any(doc, node, parent, child).nodes[parent as int].children == list_kids( + doc.nodes[parent as int].children, + doc.nodes[node as int].children, + child, + ), + insert_any(doc, node, parent, child).nodes[node as int].children.len() == 0, + forall|j: int| 0 <= j < doc.nodes[node as int].children.len() ==> + insert_any(doc, node, parent, child).nodes[#[trigger] doc.nodes[node as int].children[j] as int].parent == Some(parent), +{ + lemma_insert_fragment(doc, node, parent, child); + let r = insert_fragment(doc, node, parent, child); + let f = fragment_inserted(doc, node, parent, child); + assert(r.nodes == f); + lemma_fragment_inserted_parent(doc, node, parent, child); + let nodes = doc.nodes[node as int].children; + let paths = lemma_wf_witness(doc); + lemma_frag_facts(doc, paths, node, parent, child); + lemma_wf_split(doc, paths); + lemma_fragment_frame(doc, node, parent, child); + assert forall|j: int| 0 <= j < nodes.len() implies r.nodes[#[trigger] nodes[j] as int].parent == Some(parent) by { + let c = nodes[j]; + assert(nodes.contains(c)); + } +} + +/// [`theorem_insert_any_wf`] for a node that is not a fragment. +#[verifier::rlimit(100)] +proof fn lemma_insert_any_node(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + !(doc.nodes[node as int].kind is DocumentFragment), + ensures + wf(insert_any(doc, node, parent, child)), + insert_any(doc, node, parent, child).nodes.len() == doc.nodes.len(), + forall|x: int| #![trigger insert_any(doc, node, parent, child).nodes[x]] 0 <= x < doc.nodes.len() ==> { + &&& insert_any(doc, node, parent, child).nodes[x].kind == doc.nodes[x].kind + &&& insert_any(doc, node, parent, child).nodes[x].wrapper == doc.nodes[x].wrapper + &&& (x != node && !(doc.nodes[node as int].kind is DocumentFragment && doc.nodes[node as int].children.contains(x as NodeId)) + ==> insert_any(doc, node, parent, child).nodes[x].parent == doc.nodes[x].parent) + }, + insert_any(doc, node, parent, child).nodes[node as int].parent == Some(parent), + insert_any(doc, node, parent, child).nodes[parent as int].children == insert_kids(doc, node, parent, child), +{ + assert(insert_requires(doc, node, parent, child)); + theorem_insert_wf(doc, node, parent, child); +} + +/// DOM §4.2.3 "insert", for any node, keeps the arena well formed and does +/// what its steps say: a node that is not a fragment is `parent`'s new +/// child (the parser's [`insert`]); a fragment is emptied, and each of its +/// children has `parent` as its parent. No kind or wrapper changes, and no +/// other node's parent does. +#[verifier::rlimit(100)] +pub proof fn theorem_insert_any_wf(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + ensures + wf(insert_any(doc, node, parent, child)), + insert_any(doc, node, parent, child).nodes.len() == doc.nodes.len(), + forall|x: int| #![trigger insert_any(doc, node, parent, child).nodes[x]] 0 <= x < doc.nodes.len() ==> { + &&& insert_any(doc, node, parent, child).nodes[x].kind == doc.nodes[x].kind + &&& insert_any(doc, node, parent, child).nodes[x].wrapper == doc.nodes[x].wrapper + &&& (x != node && !(doc.nodes[node as int].kind is DocumentFragment && doc.nodes[node as int].children.contains(x as NodeId)) + ==> insert_any(doc, node, parent, child).nodes[x].parent == doc.nodes[x].parent) + }, + !(doc.nodes[node as int].kind is DocumentFragment) ==> insert_any(doc, node, parent, child).nodes[node as int].parent == Some(parent), + !(doc.nodes[node as int].kind is DocumentFragment) ==> insert_any(doc, node, parent, child).nodes[parent as int].children == insert_kids( + doc, + node, + parent, + child, + ), + doc.nodes[node as int].kind is DocumentFragment ==> insert_any(doc, node, parent, child).nodes[parent as int].children == list_kids( + doc.nodes[parent as int].children, + doc.nodes[node as int].children, + child, + ), + doc.nodes[node as int].kind is DocumentFragment ==> { + &&& insert_any(doc, node, parent, child).nodes[node as int].children.len() == 0 + &&& forall|j: int| 0 <= j < doc.nodes[node as int].children.len() ==> + insert_any(doc, node, parent, child).nodes[#[trigger] doc.nodes[node as int].children[j] as int].parent == Some(parent) + }, +{ + if doc.nodes[node as int].kind is DocumentFragment { + lemma_insert_any_frag(doc, node, parent, child); + } else { + lemma_insert_any_node(doc, node, parent, child); + } +} + +/// The children of `parent` after inserting a fragment: the old ones with +/// the fragment's, in order, at the reference child. +pub proof fn lemma_insert_fragment_children(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + doc.nodes[node as int].kind is DocumentFragment, + ensures + insert_any(doc, node, parent, child).nodes[parent as int].children == list_kids( + doc.nodes[parent as int].children, + doc.nodes[node as int].children, + child, + ), +{ + lemma_insert_any_frag(doc, node, parent, child); +} + +// --------------------------------------------------------------------- +// pre-insert + +/// "referenceChild" after steps 2 and 3 of "pre-insert". +pub open spec fn pre_insert_reference(doc: Document, node: NodeId, child: Option) -> Option { + if child == Some(node) { + next_sibling(doc, node) + } else { + child + } +} + +/// A call without an error from "ensure pre-insertion validity" meets +/// what [`insert_any`] asks, with the reference child of step 3. +pub proof fn lemma_pre_insert_valid(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + pre_insert_requires(doc, node, parent, child), + pre_insert_error(doc, node, parent, child) is None, + ensures + insert_any_requires(doc, node, parent, pre_insert_reference(doc, node, child)), + pre_insert(doc, node, parent, child) == insert_any(doc, node, parent, pre_insert_reference(doc, node, child)), +{ + lemma_not_host_not_incl(doc, node, parent); + if child == Some(node) { + assert(doc.nodes[node as int].parent == Some(parent)); + lemma_next_sibling(doc, node); + } +} + +/// DOM §4.2.3† "pre-insert" keeps the arena well formed. An error leaves +/// it as it is; without one it is "insert" ([`insert_any`]) at the +/// reference child, so the node (or a fragment's children) is a child of +/// `parent`. No kind or wrapper changes. +pub proof fn theorem_pre_insert_wf(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + pre_insert_requires(doc, node, parent, child), + ensures + wf(pre_insert(doc, node, parent, child)), + pre_insert(doc, node, parent, child).nodes.len() == doc.nodes.len(), + pre_insert_error(doc, node, parent, child) is Some ==> pre_insert(doc, node, parent, child) == doc, + forall|x: int| #![trigger pre_insert(doc, node, parent, child).nodes[x]] 0 <= x < doc.nodes.len() ==> { + &&& pre_insert(doc, node, parent, child).nodes[x].kind == doc.nodes[x].kind + &&& pre_insert(doc, node, parent, child).nodes[x].wrapper == doc.nodes[x].wrapper + }, + pre_insert_error(doc, node, parent, child) is None && !(doc.nodes[node as int].kind is DocumentFragment) ==> + pre_insert(doc, node, parent, child).nodes[node as int].parent == Some(parent), + pre_insert_error(doc, node, parent, child) is None && doc.nodes[node as int].kind is DocumentFragment ==> { + &&& pre_insert(doc, node, parent, child).nodes[node as int].children.len() == 0 + &&& forall|j: int| 0 <= j < doc.nodes[node as int].children.len() ==> + pre_insert(doc, node, parent, child).nodes[#[trigger] doc.nodes[node as int].children[j] as int].parent == Some(parent) + }, +{ + if pre_insert_error(doc, node, parent, child) is None { + lemma_pre_insert_valid(doc, node, parent, child); + let r = pre_insert_reference(doc, node, child); + theorem_insert_any_wf(doc, node, parent, r); + } +} + + +// --------------------------------------------------------------------- +// replace + +/// "referenceChild" after steps 7 and 8 of "replace". +pub open spec fn replace_reference(doc: Document, child: NodeId, node: NodeId) -> Option { + let r0 = next_sibling(doc, child); + if r0 == Some(node) { + next_sibling(doc, node) + } else { + r0 + } +} + +/// The reference child of "replace" is a child of `parent` that is neither +/// `node` nor `child`, so it is still one after `child` is removed. +pub proof fn lemma_replace_reference(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + wf(doc), + valid(doc, child), + valid(doc, node), + doc.nodes[child as int].parent == Some(parent), + ensures + match replace_reference(doc, child, node) { + Some(r) => valid(doc, r) && r != node && r != child && doc.nodes[r as int].parent == Some(parent), + None => true, + }, +{ + lemma_next_sibling(doc, child); + let kids = children(doc, parent); + let i = index_of(kids, child); + if let Some(x) = next_sibling(doc, child) { + if x == node { + lemma_next_sibling(doc, node); + assert(index_of(kids, node) == i + 1); + if let Some(y) = next_sibling(doc, node) { + assert(index_of(kids, y) == i + 2); + assert(y != child); + } + } + } +} + +/// A call without an error from steps 1 to 6 of "replace" meets what +/// [`insert_any`] asks, after `child` is removed, with the reference child +/// of steps 7 and 8. +pub proof fn lemma_replace_valid(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + replace_requires(doc, child, node, parent), + replace_error(doc, child, node, parent) is None, + ensures + super::contract::remove_requires(doc, child), + insert_any_requires(remove(doc, child), node, parent, replace_reference(doc, child, node)), + replace(doc, child, node, parent) == insert_any(remove(doc, child), node, parent, replace_reference(doc, child, node)), +{ + assert(doc.nodes[child as int].parent == Some(parent)); + let d1 = remove(doc, child); + theorem_remove_wf(doc, child); + theorem_remove_keeps_record(doc, child); + lemma_not_host_not_incl(doc, node, parent); + lemma_remove_ancestor_mono(doc, child, node, parent); + lemma_replace_reference(doc, child, node, parent); + if let Some(r) = replace_reference(doc, child, node) { + assert(d1.nodes[r as int].parent == doc.nodes[r as int].parent); + } +} + +/// DOM §4.2.3† "replace" keeps the arena well formed. An error leaves it as +/// it is. Without one `child` has no parent afterwards (unless it is +/// `node`), and `node` (or a fragment's children) is a child of `parent`. +/// No kind or wrapper changes. +#[verifier::rlimit(50)] +pub proof fn theorem_replace_wf(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + replace_requires(doc, child, node, parent), + ensures + wf(replace(doc, child, node, parent)), + replace(doc, child, node, parent).nodes.len() == doc.nodes.len(), + replace_error(doc, child, node, parent) is Some ==> replace(doc, child, node, parent) == doc, + forall|x: int| #![trigger replace(doc, child, node, parent).nodes[x]] 0 <= x < doc.nodes.len() ==> { + &&& replace(doc, child, node, parent).nodes[x].kind == doc.nodes[x].kind + &&& replace(doc, child, node, parent).nodes[x].wrapper == doc.nodes[x].wrapper + }, + replace_error(doc, child, node, parent) is None && child != node ==> replace(doc, child, node, parent).nodes[child as int].parent is None, + replace_error(doc, child, node, parent) is None && !(doc.nodes[node as int].kind is DocumentFragment) ==> + replace(doc, child, node, parent).nodes[node as int].parent == Some(parent), + replace_error(doc, child, node, parent) is None && doc.nodes[node as int].kind is DocumentFragment ==> { + &&& replace(doc, child, node, parent).nodes[node as int].children.len() == 0 + &&& forall|j: int| 0 <= j < doc.nodes[node as int].children.len() ==> + replace(doc, child, node, parent).nodes[#[trigger] doc.nodes[node as int].children[j] as int].parent == Some(parent) + }, +{ + if replace_error(doc, child, node, parent) is None { + lemma_replace_valid(doc, child, node, parent); + let d1 = remove(doc, child); + let r = replace_reference(doc, child, node); + theorem_remove_wf(doc, child); + theorem_remove_keeps_record(doc, child); + theorem_insert_any_wf(d1, node, parent, r); + let out = replace(doc, child, node, parent); + assert(out == insert_any(d1, node, parent, r)); + let paths = lemma_wf_witness(d1); + lemma_wf_split(d1, paths); + if child != node && doc.nodes[node as int].kind is DocumentFragment { + if d1.nodes[node as int].children.contains(child) { + let j = choose|j: int| 0 <= j < d1.nodes[node as int].children.len() && d1.nodes[node as int].children[j] == child; + assert(d1.nodes[node as int].children[j] == child); + assert(d1.nodes[child as int].parent == Some(node)); + } + } + if doc.nodes[node as int].kind is DocumentFragment { + // `node` is not `parent`, the only node whose children changed. + assert(node != parent); + assert(d1.nodes[node as int].children == doc.nodes[node as int].children); + } + } +} + +// --------------------------------------------------------------------- +// replace all + +/// A call meeting [`replace_all_requires`]: after step 5, the arena is well +/// formed, `parent` has no children, and `node` may be inserted. +pub proof fn lemma_replace_all_valid(doc: Document, node: Option, parent: NodeId) + requires + replace_all_requires(doc, node, parent), + ensures + ({ + let d1 = remove_children(doc, parent); + &&& wf(d1) + &&& d1.nodes.len() == doc.nodes.len() + &&& d1.nodes[parent as int].children.len() == 0 + &&& forall|x: int| 0 <= x < doc.nodes.len() ==> (#[trigger] d1.nodes[x]).kind == doc.nodes[x].kind && d1.nodes[x].wrapper + == doc.nodes[x].wrapper + &&& match node { + None => true, + Some(n) => insert_any_requires(d1, n, parent, None), + } + }), +{ + let kids = doc.nodes[parent as int].children; + let len = kids.len(); + lemma_remove_children_n(doc, parent, len); + let d1 = remove_children(doc, parent); + assert(children(doc, parent) == kids); + assert(d1.nodes == removed_prefix(doc, parent, len as int)); + assert(kids.subrange(len as int, len as int) =~= seq![]); + assert(d1.nodes.len() == doc.nodes.len()); + assert forall|x: int| 0 <= x < doc.nodes.len() implies (#[trigger] d1.nodes[x]).kind == doc.nodes[x].kind && d1.nodes[x].wrapper + == doc.nodes[x].wrapper by {} + if let Some(n) = node { + lemma_not_host_not_incl(doc, n, parent); + lemma_remove_children_n_ancestors(doc, parent, len, n, parent); + } +} + +/// The records of [`removed_prefix`] for a node that is neither the parent +/// nor among the removed. +proof fn lemma_removed_prefix_other(d: Document, p: NodeId, k: int, x: NodeId) + requires + valid(d, p), + valid(d, x), + x != p, + !d.nodes[p as int].children.subrange(0, k).contains(x), + ensures + removed_prefix(d, p, k)[x as int] == d.nodes[x as int], +{ +} + +/// The children of `parent` in [`fragment_inserted`]. +proof fn lemma_fragment_inserted_parent(d: Document, frag: NodeId, parent: NodeId, child: Option) + requires + valid(d, frag), + valid(d, parent), + parent != frag, + ensures + fragment_inserted(d, frag, parent, child)[parent as int].children == list_kids( + d.nodes[parent as int].children, + d.nodes[frag as int].children, + child, + ), +{ +} + +/// "Replace all" with a fragment: `parent`'s children are the fragment's. +proof fn lemma_replace_all_fragment(doc: Document, n: NodeId, parent: NodeId) + requires + replace_all_requires(doc, Some(n), parent), + doc.nodes[n as int].kind is DocumentFragment, + ensures + children(replace_all(doc, Some(n), parent), parent) == children(doc, n), +{ + lemma_replace_all_valid(doc, Some(n), parent); + let d1 = remove_children(doc, parent); + lemma_insert_fragment(d1, n, parent, None); + let kids = d1.nodes[parent as int].children; + let len = doc.nodes[parent as int].children.len(); + assert(kids =~= seq![]); + assert(n != parent); + let paths = lemma_wf_witness(doc); + lemma_wf_split(doc, paths); + lemma_remove_children_n(doc, parent, len); + assert(children(doc, parent) == doc.nodes[parent as int].children); + assert(!doc.nodes[parent as int].children.contains(n)) by { + if doc.nodes[parent as int].children.contains(n) { + let j = choose|j: int| 0 <= j < len && doc.nodes[parent as int].children[j] == n; + assert(doc.nodes[n as int].parent == Some(parent)); + } + } + assert(doc.nodes[parent as int].children.subrange(0, len as int) =~= doc.nodes[parent as int].children); + lemma_removed_prefix_other(doc, parent, len as int, n); + assert(d1.nodes[n as int] == doc.nodes[n as int]); + lemma_fragment_inserted_parent(d1, n, parent, None); + assert(list_kids(kids, doc.nodes[n as int].children, None) =~= doc.nodes[n as int].children); +} + +/// "Replace all" with a node that is not a fragment: `parent`'s children are +/// that node. +proof fn lemma_replace_all_node(doc: Document, n: NodeId, parent: NodeId) + requires + replace_all_requires(doc, Some(n), parent), + !(doc.nodes[n as int].kind is DocumentFragment), + ensures + children(replace_all(doc, Some(n), parent), parent) == seq![n], +{ + lemma_replace_all_valid(doc, Some(n), parent); + let d1 = remove_children(doc, parent); + let paths = lemma_wf_witness(d1); + assert(d1.nodes[n as int].kind == doc.nodes[n as int].kind); + assert(insert_requires(d1, n, parent, None)); + theorem_insert_wf(d1, n, parent, None); + let p0 = d1.nodes[n as int].parent; + if p0 is Some { + let q = p0->Some_0; + assert(super::contract::remove_requires(d1, n)); + theorem_remove_keeps_record(d1, n); + lemma_wf_split(d1, paths); + assert(d1.nodes[q as int].children.contains(n)); + assert(q != parent); + assert(adopt(d1, n).nodes[parent as int].children == d1.nodes[parent as int].children); + } + assert(adopt(d1, n).nodes[parent as int].children =~= seq![]); + assert(insert_kids(d1, n, parent, None) =~= seq![n]); +} + +/// DOM §4.2.3† "replace all" keeps the arena well formed. `parent`'s +/// children afterwards are none (no node), `node` alone, or a fragment's +/// children. No kind or wrapper changes. +pub proof fn theorem_replace_all_wf(doc: Document, node: Option, parent: NodeId) + requires + replace_all_requires(doc, node, parent), + ensures + wf(replace_all(doc, node, parent)), + replace_all(doc, node, parent).nodes.len() == doc.nodes.len(), + forall|x: int| #![trigger replace_all(doc, node, parent).nodes[x]] 0 <= x < doc.nodes.len() ==> { + &&& replace_all(doc, node, parent).nodes[x].kind == doc.nodes[x].kind + &&& replace_all(doc, node, parent).nodes[x].wrapper == doc.nodes[x].wrapper + }, + node is None ==> children(replace_all(doc, node, parent), parent).len() == 0, + node is Some && !(doc.nodes[node->Some_0 as int].kind is DocumentFragment) ==> children(replace_all(doc, node, parent), parent) == seq![node->Some_0], + node is Some && doc.nodes[node->Some_0 as int].kind is DocumentFragment ==> children(replace_all(doc, node, parent), parent) == children( + doc, + node->Some_0, + ), +{ + lemma_replace_all_valid(doc, node, parent); + let d1 = remove_children(doc, parent); + if let Some(n) = node { + theorem_insert_any_wf(d1, n, parent, None); + assert(d1.nodes[n as int].kind == doc.nodes[n as int].kind); + if doc.nodes[n as int].kind is DocumentFragment { + lemma_replace_all_fragment(doc, n, parent); + } else { + lemma_replace_all_node(doc, n, parent); + } + } +} + +} // verus! diff --git a/crates/clazel-dom/src/ops_mutation_lemmas.rs b/crates/clazel-dom/src/ops_mutation_lemmas.rs new file mode 100644 index 000000000..0841c1eb2 --- /dev/null +++ b/crates/clazel-dom/src/ops_mutation_lemmas.rs @@ -0,0 +1,364 @@ +//! Concrete instances of the theorems of [`super::ops_lemmas`], the kill +//! certificates of Phase 7 item 7.13's mutations in `mutations.json` +//! (`xtask/src/mutate.rs`). Each lemma states what a theorem says about a +//! small concrete arena; a certificate evaluates the mutated spec on that +//! arena (`compute_only`) and shows it contradicts the lemma. +//! +//! The arenas: [`ol_base`] is a document (0) with an element `A` (1) child, +//! an element `B` (2) child of `A`, a text node (3) child of `B`, a +//! parentless text node (4), a parentless element (5), and a fragment (6) +//! with two text children (7 and 8). [`ol_attrs`] is a document and a +//! parentless element (1) with two attributes. [`ol_template`] is a +//! document and a parentless template element (1) whose contents is the +//! fragment 2. +//! +//! Every `by (compute_only)` here is evaluated whenever this module is +//! built, mutants included, so each evaluates only spec parts that no +//! mutation changes; facts that a mutation would change come from the +//! theorems instead. + +#[cfg(verus_only)] +use super::lemmas::*; +#[cfg(verus_only)] +use super::mutation_lemmas::*; +#[cfg(verus_only)] +use super::ops_lemmas::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The base arena, step by step: the document. +pub open spec fn ol_d0() -> Document { + new_document(seq![]) +} + +/// Node 1, an element. +pub open spec fn ol_d1() -> Document { + create_node(ol_d0(), ml_element()).0 +} + +/// Node 2, an element. +pub open spec fn ol_d2() -> Document { + create_node(ol_d1(), ml_element()).0 +} + +/// Node 3, a text node. +pub open spec fn ol_d3() -> Document { + create_node(ol_d2(), ml_text()).0 +} + +/// Node 4, a text node. +pub open spec fn ol_d4() -> Document { + create_node(ol_d3(), ml_text()).0 +} + +/// Node 5, an element. +pub open spec fn ol_d5() -> Document { + create_node(ol_d4(), ml_element()).0 +} + +/// Node 6, a fragment. +pub open spec fn ol_d6() -> Document { + create_node(ol_d5(), NodeKind::DocumentFragment { host: None }).0 +} + +/// Node 7, a text node. +pub open spec fn ol_d7() -> Document { + create_node(ol_d6(), ml_text()).0 +} + +/// Node 8, a text node. +pub open spec fn ol_d8() -> Document { + create_node(ol_d7(), ml_text()).0 +} + +/// Node 1 appended to the document. +pub open spec fn ol_d9() -> Document { + append(ol_d8(), 0, 1) +} + +/// Node 2 appended to node 1. +pub open spec fn ol_d10() -> Document { + append(ol_d9(), 1, 2) +} + +/// Node 3 appended to node 2. +pub open spec fn ol_d11() -> Document { + append(ol_d10(), 2, 3) +} + +/// Node 7 appended to the fragment. +pub open spec fn ol_d12() -> Document { + append(ol_d11(), 6, 7) +} + +/// The base arena: node 8 appended to the fragment. +pub open spec fn ol_base() -> Document { + append(ol_d12(), 6, 8) +} + +/// Creating a node of kind `k` in a well-formed arena that holds `n` nodes +/// is allowed. +proof fn ol_create(d: Document, k: NodeKind, n: nat) + requires + wf(d), + creatable(d, k), + d.nodes.len() == n, + n + 1 <= max_nodes(), + ensures + wf(create_node(d, k).0), + create_node(d, k).0.nodes.len() == n + 1, +{ + theorem_create_node_wf(d, k); +} + +/// The base arena is well formed and has the parents and kinds the +/// description says. +pub proof fn ol_base_wf() + ensures + wf(ol_base()), + ol_base().nodes.len() == 9, +{ + theorem_new_document_wf(seq![]); + ol_create(ol_d0(), ml_element(), 1); + ol_create(ol_d1(), ml_element(), 2); + ol_create(ol_d2(), ml_text(), 3); + ol_create(ol_d3(), ml_text(), 4); + ol_create(ol_d4(), ml_element(), 5); + ol_create(ol_d5(), NodeKind::DocumentFragment { host: None }, 6); + ol_create(ol_d6(), ml_text(), 7); + ol_create(ol_d7(), ml_text(), 8); + assert(ol_d8().nodes.len() == 9) by (compute_only); + assert(ol_d8().nodes[0].kind is Document) by (compute_only); + assert(ol_d8().nodes[1].kind is Element) by (compute_only); + assert(!is_inclusive_ancestor(ol_d8(), 1, 0)) by (compute_only); + theorem_append_wf(ol_d8(), 0, 1); + assert(ol_d9().nodes.len() == 9) by (compute_only); + assert(ol_d9().nodes[2].kind is Element) by (compute_only); + assert(ol_d9().nodes[1].kind is Element) by (compute_only); + assert(!is_inclusive_ancestor(ol_d9(), 2, 1)) by (compute_only); + theorem_append_wf(ol_d9(), 1, 2); + assert(ol_d10().nodes.len() == 9) by (compute_only); + assert(ol_d10().nodes[3].kind is Text) by (compute_only); + assert(ol_d10().nodes[2].kind is Element) by (compute_only); + assert(!is_inclusive_ancestor(ol_d10(), 3, 2)) by (compute_only); + theorem_append_wf(ol_d10(), 2, 3); + assert(ol_d11().nodes.len() == 9) by (compute_only); + assert(ol_d11().nodes[7].kind is Text) by (compute_only); + assert(ol_d11().nodes[6].kind is DocumentFragment) by (compute_only); + assert(!is_inclusive_ancestor(ol_d11(), 7, 6)) by (compute_only); + theorem_append_wf(ol_d11(), 6, 7); + assert(ol_d12().nodes.len() == 9) by (compute_only); + assert(ol_d12().nodes[8].kind is Text) by (compute_only); + assert(ol_d12().nodes[6].kind is DocumentFragment) by (compute_only); + assert(!is_inclusive_ancestor(ol_d12(), 8, 6)) by (compute_only); + theorem_append_wf(ol_d12(), 6, 8); +} + +/// "Ensure pre-insertion validity" (step 2): inserting `A` into its +/// descendant `B` is an error. +pub proof fn ol_validity_ancestor() + ensures + pre_insert_error(ol_base(), 1, 2, None) is Some, +{ + ol_base_wf(); + if pre_insert_error(ol_base(), 1, 2, None) is None { + lemma_pre_insert_valid(ol_base(), 1, 2, None); + assert(is_inclusive_ancestor(ol_base(), 1, 2)) by (compute_only); + } +} + +/// "Ensure pre-insertion validity" (step 3): a reference child that is not +/// a child of the parent is an error. +pub proof fn ol_validity_not_found() + ensures + pre_insert_error(ol_base(), 5, 2, Some(1)) is Some, +{ + ol_base_wf(); + if pre_insert_error(ol_base(), 5, 2, Some(1)) is None { + lemma_pre_insert_valid(ol_base(), 5, 2, Some(1)); + assert(ol_base().nodes[1].parent == Some(0usize)) by (compute_only); + } +} + +/// "Ensure pre-insertion validity" (step 1): a text node cannot be a +/// parent. +pub proof fn ol_validity_leaf_parent() + ensures + pre_insert_error(ol_base(), 5, 3, None) is Some, +{ + ol_base_wf(); + if pre_insert_error(ol_base(), 5, 3, None) is None { + lemma_pre_insert_valid(ol_base(), 5, 3, None); + assert(ol_base().nodes[3].kind is Text) by (compute_only); + } +} + +/// "Ensure pre-insertion validity" (step 4): a document cannot be +/// inserted. +pub proof fn ol_validity_document_node() + ensures + pre_insert_error(ol_base(), 0, 5, None) is Some, +{ + ol_base_wf(); + if pre_insert_error(ol_base(), 0, 5, None) is None { + lemma_pre_insert_valid(ol_base(), 0, 5, None); + assert(ol_base().nodes[0].kind is Document) by (compute_only); + } +} + +/// `theorem_replace_wf`: replacing the text node 3 of `B` by the +/// parentless text node 4 leaves 3 without a parent. +pub proof fn ol_replace_detaches() + ensures + replace(ol_base(), 3, 4, 2).nodes[3].parent is None, +{ + ol_base_wf(); + assert(replace_error(ol_base(), 3, 4, 2) is None) by (compute_only); + theorem_replace_wf(ol_base(), 3, 4, 2); +} + +/// `theorem_replace_all_wf`: replacing all of `B`'s children by the text +/// node 4 leaves it that one child. +pub proof fn ol_replace_all_clears() + ensures + children(replace_all(ol_base(), Some(4), 2), 2).len() == 1, +{ + ol_base_wf(); + assert(ol_base().nodes[2].kind is Element) by (compute_only); + assert(ol_base().nodes[4].kind is Text) by (compute_only); + assert(!is_host_including_inclusive_ancestor(ol_base(), 4, 2)) by (compute_only); + theorem_replace_all_wf(ol_base(), Some(4), 2); +} + +/// `theorem_insert_any_wf` through `lemma_pre_insert_valid`: inserting the +/// fragment 6 into `B` gives `B` the children 3, 7, 8, in that order. +#[verifier::rlimit(400)] +pub proof fn ol_fragment_order() + ensures + pre_insert(ol_base(), 6, 2, None).nodes[2].children == seq![3usize, 7usize, 8usize], +{ + ol_base_wf(); + assert(pre_insert_error(ol_base(), 6, 2, None) is None) by (compute_only); + lemma_pre_insert_valid(ol_base(), 6, 2, None); + assert(pre_insert_reference(ol_base(), 6, None) == None::); + assert(ol_base().nodes[6].kind is DocumentFragment) by (compute_only); + lemma_insert_fragment_children(ol_base(), 6, 2, None); + assert(ol_base().nodes[2].children == seq![3usize]) by (compute_only); + assert(ol_base().nodes[6].children == seq![7usize, 8usize]) by (compute_only); + assert(seq![3usize] + seq![7usize, 8usize] =~= seq![3usize, 7usize, 8usize]); +} + +/// The element kind of [`ol_attrs`]: attributes `a1` and `a2`. +pub open spec fn ol_attr(n: u32) -> Attr { + Attr { namespace: None, prefix: None, local_name: seq![n], value: seq![n] } +} + +/// An element with two attributes. +pub open spec fn ol_attr_kind() -> NodeKind { + NodeKind::Element { + namespace: Some(Namespace::Html), + prefix: None, + local_name: seq![], + attributes: seq![ol_attr(1), ol_attr(2)], + template_contents: None, + form_owner: None, + } +} + +/// A document and a parentless element (1) with two attributes. +pub open spec fn ol_attrs() -> Document { + create_node(ol_d0(), ol_attr_kind()).0 +} + +/// [`ol_attrs`] is well formed. +pub proof fn ol_attrs_wf() + ensures + wf(ol_attrs()), + ol_attrs().nodes.len() == 2, + attribute_requires(ol_attrs(), 1, 0), + attribute_requires(ol_attrs(), 1, 1), + clone_requires(ol_attrs(), 1), + can_create(ol_attrs(), 1), +{ + theorem_new_document_wf(seq![]); + assert(creatable(ol_d0(), ol_attr_kind())); + theorem_create_node_wf(ol_d0(), ol_attr_kind()); + assert(ol_attrs().nodes.len() == 2) by (compute_only); + assert(ol_attrs().nodes[1].kind is Element) by (compute_only); + assert(ol_attrs().nodes[1].kind->Element_attributes.len() == 2) by (compute_only); + assert(clone_count(ol_attrs(), 1) == 1) by (compute_only); +} + +/// `theorem_change_attribute_wf`: the new value is the attribute's value. +pub proof fn ol_change_value() + ensures + change_attribute(ol_attrs(), 1, 0, seq![9u32]).nodes[1].kind->Element_attributes[0].value[0] == 9u32, +{ + ol_attrs_wf(); + theorem_change_attribute_wf(ol_attrs(), 1, 0, seq![9u32]); + assert(ol_attrs().nodes[1].kind->Element_attributes[0] == ol_attr(1)) by (compute_only); +} + +/// `theorem_remove_attribute_wf`: removing the second attribute leaves the +/// first. +pub proof fn ol_remove_index() + ensures + remove_attribute(ol_attrs(), 1, 1).nodes[1].kind->Element_attributes[0].local_name[0] == 1u32, +{ + ol_attrs_wf(); + theorem_remove_attribute_wf(ol_attrs(), 1, 1); + assert(ol_attrs().nodes[1].kind->Element_attributes.remove(1)[0].local_name[0] == 1u32) by (compute_only); +} + +/// `theorem_replace_attribute_wf`: replacing an attribute keeps the length +/// of the list. +pub proof fn ol_replace_attribute_length() + ensures + replace_attribute(ol_attrs(), 1, 0, ol_attr(3)).nodes[1].kind->Element_attributes.len() == 2, +{ + ol_attrs_wf(); + theorem_replace_attribute_wf(ol_attrs(), 1, 0, ol_attr(3)); + assert(ol_attrs().nodes[1].kind->Element_attributes.len() == 2) by (compute_only); +} + +/// `theorem_clone_node_wf`: the copy of an element has the element's two +/// attributes. +pub proof fn ol_clone_attributes() + ensures + clone_node(ol_attrs(), 1).0.nodes[2].kind->Element_attributes.len() == 2, +{ + ol_attrs_wf(); + assert(clone_count(ol_attrs(), 1) == 1) by (compute_only); + theorem_clone_node_wf(ol_attrs(), 1); + assert(clone_node(ol_attrs(), 1).1 == 2usize) by (compute_only); + assert(ol_attrs().nodes[1].kind->Element_attributes.len() == 2) by (compute_only); +} + +/// A document and a parentless template element (1) with its contents (2). +pub open spec fn ol_template() -> Document { + create_template_contents(create_node(ol_d0(), ml_element()).0, 1).0 +} + +/// `theorem_clone_node_wf`: cloning a template creates two nodes. +pub proof fn ol_clone_count() + ensures + clone_count(ol_template(), 1) == 2, +{ + theorem_new_document_wf(seq![]); + ol_create(ol_d0(), ml_element(), 1); + assert(ol_d1().nodes[1].kind is Element) by (compute_only); + assert(ol_d1().nodes[1].kind->Element_template_contents is None) by (compute_only); + theorem_create_template_contents_wf(ol_d1(), 1); + assert(ol_template().nodes.len() == 3) by (compute_only); + assert(ol_template().nodes[1].kind is Element) by (compute_only); + assert(ol_template().nodes[1].kind->Element_template_contents is Some) by (compute_only); + assert(can_create(ol_template(), clone_count(ol_template(), 1))) by (compute_only); + theorem_clone_node_wf(ol_template(), 1); + assert(clone_node(ol_template(), 1).0.nodes.len() == 5) by (compute_only); +} + +} // verus! diff --git a/crates/clazel-dom/src/spec.rs b/crates/clazel-dom/src/spec.rs index ee11f9a48..fc2d6919b 100644 --- a/crates/clazel-dom/src/spec.rs +++ b/crates/clazel-dom/src/spec.rs @@ -466,6 +466,28 @@ pub open spec fn create_template_contents(doc: Document, t: NodeId) -> (Document // --------------------------------------------------------------------- // Mutation algorithms (DOM §4.2.3) +/// The "insertion steps" hook (DOM §4.2.3 "insert", step 7.7.1†: "Run the +/// insertion steps with inclusiveDescendant."). Other standards define +/// insertion steps for particular elements (HTML §4.12.1 `script`, §4.8.5 +/// `iframe`, §4.12.3 `template`, and others). A named hook point: here the +/// hook changes nothing, and is called once, with the root of the inserted +/// subtree, from [`insert`] only. A later phase that gives a hook an effect +/// replaces this one definition, and must then re-prove the theorems of +/// `lemmas.rs` that unfold [`insert`] (each states, in its `ensures`, what +/// the hook may not change). +pub open spec fn insertion_steps(doc: Document, inserted: NodeId) -> Document { + doc +} + +/// The "removing steps" hook (DOM §4.2.3 "remove", step 12†: "Run the +/// removing steps with node and parent."). Defined by other standards for +/// particular elements, as [`insertion_steps`] is. A named hook point +/// that changes nothing here, called once from [`remove`] only, with the +/// removed node and its former parent. +pub open spec fn removing_steps(doc: Document, removed: NodeId, old_parent: NodeId) -> Document { + doc +} + /// DOM §4.2.3 "remove" a node, the steps that change the tree: "1. Let /// parent be node's parent. 2. Assert: parent is non-null." ... "7. Remove /// node from its parent's children." Steps 3 to 6 and 8 onward update live @@ -479,12 +501,14 @@ pub open spec fn remove(doc: Document, node: NodeId) -> Document { let pn = doc.nodes[p as int]; // 7. let i = index_of(pn.children, node); - Document { + let removed = Document { nodes: doc.nodes.update(p as int, Node { children: pn.children.remove(i), ..pn }).update( node as int, Node { parent: None, ..doc.nodes[node as int] }, ), - } + }; + // 12.† "Run the removing steps with node and parent." + removing_steps(removed, node, p) } /// DOM §4.5 "adopt" a node into its own node document: "1. Let oldDocument @@ -540,14 +564,17 @@ pub open spec fn insert(doc: Document, node: NodeId, parent: NodeId, child: Opti // from the same parent.) Some(c) => pn.children.insert(index_of(pn.children, c), node), }; - // 7.4 to 7.7, 8 onward: slots, insertion steps, custom elements, - // observers, children changed and post-connection steps; not modelled. - Document { + let inserted = Document { nodes: d1.nodes.update(parent as int, Node { children: kids, ..pn }).update( node as int, Node { parent: Some(parent), ..d1.nodes[node as int] }, ), - } + }; + // 7.4 to 7.6: slots; 7.7.1† "Run the insertion steps with + // inclusiveDescendant."; 7.7.2 onward, 8 onward: custom elements, + // observers, children changed and post-connection steps; not modelled + // beyond the hook. + insertion_steps(inserted, node) } /// DOM §4.2.3 "append" a node to a parent: "pre-insert node into parent @@ -646,4 +673,839 @@ pub open spec fn set_mode(doc: Document, mode: DocumentMode) -> Document { Document { nodes: doc.nodes.update(0, Node { kind: k, ..node }) } } +// --------------------------------------------------------------------- +// Phase 7 item 7.13: the mutation algorithms scripts call +// +// Everything below was transcribed from memory of the DOM Standard, not read +// at the pinned commit: its section and step numbers carry the mark † +// (docs/phases/phase-7.md's convention), and must be checked against the +// pinned snapshot before a review relies on them. The algorithms are +// §4.2.3 "ensure pre-insertion validity", "pre-insert", "insert" (for a +// DocumentFragment too), "replace", "replace all", §4.4 "clone a node" and +// §4.9 "change", "remove" and "replace an attribute". +// +// Modelling choices of this part, in addition to those at the top of the +// file: +// - **Errors are values.** "Throw a HierarchyRequestError / NotFoundError" +// becomes returning `Some(DomError)` from an `_error` function; an +// algorithm that throws leaves the tree unchanged, because every check +// runs before the first change. A caller (Phase 7's bindings) turns the +// value into a `DOMException`. +// - **A DocumentFragment may be inserted.** The restriction of +// [`insert_requires`] (the node is not a fragment) belongs to the +// parser's calls; [`insert_any`] below handles a fragment, as step 4 of +// "insert" does. A fragment is still never a child (`wf`), so inserting +// one moves its children and leaves it empty and parentless. +// - **The only `CharacterData` interfaces are `Text`, `Comment` and +// `ProcessingInstruction`**, as the arena has no `CDATASection` (a +// `Text` subclass, so "node is a Text node" would cover it). +// - **"Host-including inclusive ancestor"** (§4.2) is +// [`is_host_including_inclusive_ancestor`]: walk parents to the root, and +// from a fragment root that has a host continue at the host. The walk is +// deterministic, so a node it reaches it reaches within `nodes.len()` +// steps (it visits each node at most once before repeating), which is +// the fuel. +// - **Index arguments.** An attribute is named by its position in the +// element's attribute list (DOM names the `Attr` object); the position +// is a precondition, as the attribute's presence in the list is. +// - **Attributes' "handle attribute changes"** (queues records, runs +// attribute change steps) is not modelled, as in [`append_attribute`]. +// - **Steps 5 and 6 are checked against a second statement.** The checks +// of "ensure pre-insertion validity" and "replace" (their steps 5 and 6) +// are written as DOM writes them. [`children_rules_hold`] says the same +// thing as a property of the tree: only a document has a doctype child, +// and a document's children have no `Text`, at most one element, at most +// one doctype and no doctype after the element +// ([`document_children_ok`]). `ops_lemmas`'s theorems prove, for a node +// with no parent (a fragment never has one), that a call passes steps 5 +// and 6 exactly when the tree after the insertion obeys the rules. +// (A node that has a parent is first removed from it by "adopt"; the +// theorems do not cover that case, and DOM's checks count such a node +// among its new parent's children, so they would refuse some calls the +// rules allow.) +// - **"Clone a node" is shallow in [`clone_node`]**, with the clone children +// flag unset. The flag set is [`deep_clone`], a relation (see "Deep +// cloning" below). +// - **A template** is an element whose template contents is non-null (the +// parser creates the contents of every `template` it creates). Its clone +// gets contents of its own (HTML §4.12.3 "when a template element is +// created"); the copied element's form owner is null (a created element +// has none). +// - **The node limit applies to clones** like to creation: a clone that +// needs `k` new nodes is refused (the arena unchanged) unless the arena +// can hold `k` more. +// - **The hooks** [`insertion_steps`] and [`removing_steps`] are called +// from [`insert`] and [`remove`], so every algorithm below reaches them +// through those two. +// --------------------------------------------------------------------- + +/// The `DOMException` names the algorithms below throw. +pub enum DomError { + /// "HierarchyRequestError" + HierarchyRequest, + /// "NotFoundError" + NotFound, +} + +/// The kinds of node the validity checks count among a parent's children. +pub enum NodeTag { + /// An `Element`. + Element, + /// A `Text` node. + Text, + /// A `DocumentType` (a "doctype"). + DocumentType, +} + +/// A node of kind `k` is a node of the tag `t`. +pub open spec fn has_tag(k: NodeKind, t: NodeTag) -> bool { + match t { + NodeTag::Element => k is Element, + NodeTag::Text => k is Text, + NodeTag::DocumentType => k is DocumentType, + } +} + +/// How many of `kids` have the tag `t`, other than `skip` (a node, or none). +/// Defined from the end, so that appending a child adds its share. +pub open spec fn count_tag(doc: Document, kids: Seq, t: NodeTag, skip: Option) -> nat + decreases kids.len(), +{ + if kids.len() == 0 { + 0 + } else { + let last = kids.last(); + count_tag(doc, kids.drop_last(), t, skip) + if has_tag(doc.nodes[last as int].kind, t) && skip != Some(last) { + 1nat + } else { + 0nat + } + } +} + +/// DOM §4.2 "next sibling": "the first following sibling", null if there is +/// none (or no parent). +pub open spec fn next_sibling(doc: Document, n: NodeId) -> Option { + match parent(doc, n) { + None => None, + Some(p) => { + let i = index_of(children(doc, p), n); + if 0 <= i && i + 1 < children(doc, p).len() { + Some(children(doc, p)[i + 1]) + } else { + None + } + }, + } +} + +/// `a` is `x` or one of its first `fuel` host-including ancestors (§4.2). +pub open spec fn host_including_within(doc: Document, a: NodeId, x: NodeId, fuel: nat) -> bool + decreases fuel, +{ + if x == a { + true + } else if fuel == 0 { + false + } else { + match parent(doc, x) { + Some(p) => host_including_within(doc, a, p, (fuel - 1) as nat), + None => match doc.nodes[x as int].kind { + // x is a root: "if B's root has a non-null host and A is a + // host-including inclusive ancestor of B's root's host". + NodeKind::DocumentFragment { host: Some(h) } => host_including_within(doc, a, h, (fuel - 1) as nat), + _ => false, + }, + } + } +} + +/// DOM §4.2 "host-including inclusive ancestor": "An object A is a +/// host-including inclusive ancestor of an object B, if either A is an +/// inclusive ancestor of B, or if B's root has a non-null host and A is a +/// host-including inclusive ancestor of B's root's host." +pub open spec fn is_host_including_inclusive_ancestor(doc: Document, a: NodeId, x: NodeId) -> bool { + host_including_within(doc, a, x, doc.nodes.len()) +} + +/// The statements of step 6 of "ensure pre-insertion validity"† about a +/// `parent` that is a document, switched on the interface `node` +/// implements. True means "throw a HierarchyRequestError". +pub open spec fn pre_insert_document_violation(doc: Document, node: NodeId, parent: NodeId, child: Option) -> bool { + let kids = children(doc, parent); + // "child is non-null and a doctype is following child" and "an element + // is preceding child": the siblings after / before child. + let after = match child { + Some(c) => count_tag(doc, kids.subrange(index_of(kids, c) + 1, kids.len() as int), NodeTag::DocumentType, None), + None => 0, + }; + let before = match child { + Some(c) => count_tag(doc, kids.subrange(0, index_of(kids, c)), NodeTag::Element, None), + None => 0, + }; + let child_is_doctype = match child { + Some(c) => doc.nodes[c as int].kind is DocumentType, + None => false, + }; + match doc.nodes[node as int].kind { + // "DocumentFragment: If node has more than one element child or has + // a Text node child. Otherwise, if node has one element child and + // either parent has an element child, child is a doctype, or child + // is non-null and a doctype is following child." + NodeKind::DocumentFragment { .. } => { + let elements = count_tag(doc, children(doc, node), NodeTag::Element, None); + let texts = count_tag(doc, children(doc, node), NodeTag::Text, None); + elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype + || after > 0)) + }, + // "Element: parent has an element child, child is a doctype, or + // child is non-null and a doctype is following child." + NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, None) > 0 || child_is_doctype || after > 0, + // "DocumentType: parent has a doctype child, child is non-null and + // an element is preceding child, or child is null and parent has an + // element child." + NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, None) > 0 || before > 0 || (child is None + && count_tag(doc, kids, NodeTag::Element, None) > 0), + _ => false, + } +} + +/// DOM §4.2.3† "ensure pre-insertion validity" of `node` into `parent` +/// before `child`: the error the algorithm throws, or none. +/// "1. If parent is not a Document, DocumentFragment, or Element node, +/// then throw a "HierarchyRequestError" DOMException. 2. If node is a +/// host-including inclusive ancestor of parent, then throw a +/// "HierarchyRequestError". 3. If child is non-null and its parent is not +/// parent, then throw a "NotFoundError". 4. If node is not a +/// DocumentFragment, DocumentType, Element, or CharacterData node, then +/// throw a "HierarchyRequestError". 5. If either node is a Text node and +/// parent is a document, or node is a doctype and parent is not a +/// document, then throw a "HierarchyRequestError". 6. If parent is a +/// document, and any of the statements below, switched on the interface +/// node implements, are true, then throw a "HierarchyRequestError"." +pub open spec fn pre_insert_error(doc: Document, node: NodeId, parent: NodeId, child: Option) -> Option { + // 1. + if !may_have_children(doc.nodes[parent as int].kind) { + Some(DomError::HierarchyRequest) + } + // 2. + else if is_host_including_inclusive_ancestor(doc, node, parent) { + Some(DomError::HierarchyRequest) + } + // 3. + else if child is Some && doc.nodes[child->Some_0 as int].parent != Some(parent) { + Some(DomError::NotFound) + } + // 4. (Every kind but a Document is one of the listed.) + else if doc.nodes[node as int].kind is Document { + Some(DomError::HierarchyRequest) + } + // 5. + else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType + && !(doc.nodes[parent as int].kind is Document)) { + Some(DomError::HierarchyRequest) + } + // 6. + else if doc.nodes[parent as int].kind is Document && pre_insert_document_violation(doc, node, parent, child) { + Some(DomError::HierarchyRequest) + } else { + None + } +} + +/// What the callers of the algorithms below guarantee of their ids: they +/// name nodes of a well-formed arena. +pub open spec fn ids_requires(doc: Document, node: NodeId, parent: NodeId, child: Option) -> bool { + &&& wf(doc) + &&& valid(doc, node) + &&& valid(doc, parent) + &&& match child { + None => true, + Some(c) => valid(doc, c), + } +} + +/// DOM §4.2.3 "remove" every child of `parent`, "in tree order": the +/// first `n` remaining children are removed, one after the other. +pub open spec fn remove_children_n(doc: Document, parent: NodeId, n: nat) -> Document + decreases n, +{ + if n == 0 || children(doc, parent).len() == 0 { + doc + } else { + remove_children_n(remove(doc, children(doc, parent)[0]), parent, (n - 1) as nat) + } +} + +/// "Remove all parent's children, in tree order" (DOM §4.2.3† "replace +/// all" step 5, "insert" step 4.1). +pub open spec fn remove_children(doc: Document, parent: NodeId) -> Document { + remove_children_n(doc, parent, children(doc, parent).len()) +} + +/// Insert each of `nodes`, in order, into `parent` before `child` (DOM +/// §4.2.3 "insert" step 7, "For each node in nodes, in tree order"), each +/// a node that is not a `DocumentFragment`. +pub open spec fn insert_list(doc: Document, nodes: Seq, parent: NodeId, child: Option) -> Document + decreases nodes.len(), +{ + if nodes.len() == 0 { + doc + } else { + insert_list(insert(doc, nodes[0], parent, child), nodes.drop_first(), parent, child) + } +} + +/// DOM §4.2.3 "insert" a `DocumentFragment` `node` into `parent` before +/// `child` (null: `None`): "1. Let nodes be node's children, if node is a +/// DocumentFragment node; otherwise « node ». 2. Let count be nodes's size. +/// 3. If count is 0, then return. 4. If node is a DocumentFragment node: +/// 1. Remove its children with the suppress observers flag set. 2. Queue a +/// tree mutation record for node with « », nodes, null, and null." (not +/// modelled) "5. If child is non-null: …" (live ranges, not modelled) "6. +/// Let previousSibling be …" (observers, not modelled) "7. For each node +/// in nodes, in tree order: …" ([`insert_list`]; each node has no parent, +/// so step 7.1's adopt changes nothing). +pub open spec fn insert_fragment(doc: Document, node: NodeId, parent: NodeId, child: Option) -> Document { + // 1. and 2. + let nodes = children(doc, node); + // 3. + if nodes.len() == 0 { + doc + } else { + // 4.1 + let d1 = remove_children(doc, node); + // 7. + insert_list(d1, nodes, parent, child) + } +} + +/// DOM §4.2.3 "insert" for any `node`: a `DocumentFragment` by +/// [`insert_fragment`], any other node by [`insert`] (the parser's +/// operation, whose steps 1 to 4 do nothing for « node »). +pub open spec fn insert_any(doc: Document, node: NodeId, parent: NodeId, child: Option) -> Document { + if doc.nodes[node as int].kind is DocumentFragment { + insert_fragment(doc, node, parent, child) + } else { + insert(doc, node, parent, child) + } +} + +/// DOM §4.2.3† "pre-insert" `node` into `parent` before `child`, on a +/// valid call: "1. Ensure pre-insertion validity of node, parent, and +/// child." (An error leaves the tree as it is: [`pre_insert_error`].) "2. +/// Let referenceChild be child. 3. If referenceChild is node, then set +/// referenceChild to node's next sibling. 4. Insert node into parent before +/// referenceChild. 5. Return node." +pub open spec fn pre_insert(doc: Document, node: NodeId, parent: NodeId, child: Option) -> Document { + // 1. + if pre_insert_error(doc, node, parent, child) is Some { + doc + } else { + // 2. and 3. + let reference_child = if child == Some(node) { + next_sibling(doc, node) + } else { + child + }; + // 4. + insert_any(doc, node, parent, reference_child) + } +} + +/// The statements of step 6 of "replace"† about a `parent` that is a +/// document; true means "throw a HierarchyRequestError". They differ from +/// pre-insert's: "an element child that is not child", and a doctype +/// following (or an element preceding) `child`. +pub open spec fn replace_document_violation(doc: Document, child: NodeId, node: NodeId, parent: NodeId) -> bool { + let kids = children(doc, parent); + let i = index_of(kids, child); + let after = count_tag(doc, kids.subrange(i + 1, kids.len() as int), NodeTag::DocumentType, None); + let before = count_tag(doc, kids.subrange(0, i), NodeTag::Element, None); + match doc.nodes[node as int].kind { + // "DocumentFragment: If node has more than one element child or has + // a Text node child. Otherwise, if node has one element child and + // either parent has an element child that is not child or a doctype + // is following child." + NodeKind::DocumentFragment { .. } => { + let elements = count_tag(doc, children(doc, node), NodeTag::Element, None); + let texts = count_tag(doc, children(doc, node), NodeTag::Text, None); + elements > 1 || texts > 0 || (elements == 1 && (count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0)) + }, + // "Element: parent has an element child that is not child or a + // doctype is following child." + NodeKind::Element { .. } => count_tag(doc, kids, NodeTag::Element, Some(child)) > 0 || after > 0, + // "DocumentType: parent has a doctype child that is not child, or + // an element is preceding child." + NodeKind::DocumentType { .. } => count_tag(doc, kids, NodeTag::DocumentType, Some(child)) > 0 || before > 0, + _ => false, + } +} + +/// Steps 1 to 6 of DOM §4.2.3† "replace" `child` with `node` within +/// `parent`: the error it throws, or none. "1. If parent is not a +/// Document, DocumentFragment, or Element node, then throw a +/// "HierarchyRequestError". 2. If node is a host-including inclusive +/// ancestor of parent, then throw a "HierarchyRequestError". 3. If child's +/// parent is not parent, then throw a "NotFoundError". 4. If node is not a +/// DocumentFragment, DocumentType, Element, or CharacterData node, then +/// throw a "HierarchyRequestError". 5. If either node is a Text node and +/// parent is a document, or node is a doctype and parent is not a +/// document, then throw a "HierarchyRequestError". 6. If parent is a +/// document, and any of the statements below, switched on the interface +/// node implements, are true, then throw a "HierarchyRequestError"." +pub open spec fn replace_error(doc: Document, child: NodeId, node: NodeId, parent: NodeId) -> Option { + // 1. + if !may_have_children(doc.nodes[parent as int].kind) { + Some(DomError::HierarchyRequest) + } + // 2. + else if is_host_including_inclusive_ancestor(doc, node, parent) { + Some(DomError::HierarchyRequest) + } + // 3. + else if doc.nodes[child as int].parent != Some(parent) { + Some(DomError::NotFound) + } + // 4. + else if doc.nodes[node as int].kind is Document { + Some(DomError::HierarchyRequest) + } + // 5. + else if (doc.nodes[node as int].kind is Text && doc.nodes[parent as int].kind is Document) || (doc.nodes[node as int].kind is DocumentType + && !(doc.nodes[parent as int].kind is Document)) { + Some(DomError::HierarchyRequest) + } + // 6. + else if doc.nodes[parent as int].kind is Document && replace_document_violation(doc, child, node, parent) { + Some(DomError::HierarchyRequest) + } else { + None + } +} + +/// DOM §4.2.3† "replace" `child` with `node` within `parent`, on a valid +/// call. "7. Let referenceChild be child's next sibling. 8. If +/// referenceChild is node, then set referenceChild to node's next sibling. +/// 9. Let previousSibling be child's previous sibling. 10. Let +/// removedNodes be the empty set. 11. If child's parent is non-null, then: +/// 1. Set removedNodes to « child ». 2. Remove child with the suppress +/// observers flag set. 12. Let nodes be node's children if node is a +/// DocumentFragment node; otherwise « node ». 13. Insert node into parent +/// before referenceChild with the suppress observers flag set. 14. Queue a +/// tree mutation record … 15. Return child." Steps 9, 10, 12 (only +/// `nodes`' size matters to "insert") and 14 touch observers only. An +/// error (steps 1 to 6) leaves the tree as it is. +pub open spec fn replace(doc: Document, child: NodeId, node: NodeId, parent: NodeId) -> Document { + if replace_error(doc, child, node, parent) is Some { + doc + } else { + // 7. + let reference0 = next_sibling(doc, child); + // 8. + let reference_child = if reference0 == Some(node) { + next_sibling(doc, node) + } else { + reference0 + }; + // 11. + let d1 = if doc.nodes[child as int].parent is Some { + remove(doc, child) + } else { + doc + }; + // 13. + insert_any(d1, node, parent, reference_child) + } +} + +/// What a caller of [`replace_all`] guarantees: `parent` may have children, +/// `node` (if any) is not a document and not a host-including inclusive +/// ancestor of `parent`. (The standard's callers, `textContent` and +/// `replaceChildren`, have established what "insert" needs; DOM's "replace +/// all" checks nothing itself.) +pub open spec fn replace_all_requires(doc: Document, node: Option, parent: NodeId) -> bool { + &&& wf(doc) + &&& valid(doc, parent) + &&& may_have_children(doc.nodes[parent as int].kind) + &&& match node { + None => true, + Some(n) => valid(doc, n) && !(doc.nodes[n as int].kind is Document) && !is_host_including_inclusive_ancestor(doc, n, parent), + } +} + +/// DOM §4.2.3† "replace all with a node `node` within a node `parent`": +/// "1. Let removedNodes be parent's children. 2. Let addedNodes be the +/// empty set. 3. If node is a DocumentFragment node, then set addedNodes to +/// node's children. 4. Otherwise, if node is non-null, set addedNodes to « +/// node ». 5. Remove all parent's children, in tree order, with the +/// suppress observers flag set. 6. If node is non-null, then insert node +/// into parent before null with the suppress observers flag set. 7. If +/// either addedNodes or removedNodes is not empty, then queue a tree +/// mutation record for parent with addedNodes, removedNodes, null, and +/// null." Steps 1 to 4 and 7 only feed observers. +pub open spec fn replace_all(doc: Document, node: Option, parent: NodeId) -> Document { + // 5. + let d1 = remove_children(doc, parent); + // 6. + match node { + Some(n) => insert_any(d1, n, parent, None), + None => d1, + } +} + +// --------------------------------------------------------------------- +// Cloning (DOM §4.4† "clone a node") + +/// The kind of the copy made by steps 2 and 3 of "clone a node"†: "2. If +/// node is an element, then: create an element with … node's local name, +/// node's namespace, node's namespace prefix …. Let copy be the result. +/// Then, for each attribute in node's attribute list: Let copyAttribute be +/// a clone of attribute. Append copyAttribute to copy. 3. Otherwise, let +/// copy be a node that implements the same interfaces as node, and +/// fulfills these additional requirements, switching on the interface node +/// implements: … DocumentType: Set copy's name, public ID, and system ID to +/// those of node. … Text, Comment: Set copy's data to that of node. +/// ProcessingInstruction: Set copy's target and data to those of node. Any +/// other node: Do nothing." A created element has no template contents +/// (yet) and no form owner. A document is not cloned here (one per arena). +pub open spec fn clone_kind(k: NodeKind) -> NodeKind { + match k { + NodeKind::Element { namespace, prefix, local_name, attributes, .. } => NodeKind::Element { + namespace, + prefix, + local_name, + attributes, + template_contents: None, + form_owner: None, + }, + NodeKind::DocumentFragment { .. } => NodeKind::DocumentFragment { host: None }, + other => other, + } +} + +/// A template element (see the modelling choices). +pub open spec fn is_template(k: NodeKind) -> bool { + k is Element && k->Element_template_contents is Some +} + +/// How many nodes cloning `node` creates: the copy, and for a template its +/// contents too. +pub open spec fn clone_count(doc: Document, node: NodeId) -> nat { + if is_template(doc.nodes[node as int].kind) { + 2 + } else { + 1 + } +} + +/// DOM §4.4† "clone a node" `node`, with the clone children flag unset: +/// "1. If document is not given, let document be node's node document." +/// (One document.) "2.", "3." ([`clone_kind`]). "4. Set copy's node +/// document and document to document." (Implicit.) "5. Run any cloning +/// steps defined for node in other applicable specifications and pass copy, +/// node, document, and the clone children flag if set, as parameters." +/// (HTML §4.12.3: a template's cloning steps return when the flag is +/// unset; its copy was created with template contents, step 2.) "6. If the +/// clone children flag is set, …" (never.) "7. Return copy." The copy's id +/// is the next one; a template's contents' is the one after. +pub open spec fn clone_node(doc: Document, node: NodeId) -> (Document, NodeId) { + // 2. and 3. + let (d1, copy) = create_node(doc, clone_kind(doc.nodes[node as int].kind)); + // 5. + if is_template(doc.nodes[node as int].kind) { + (create_template_contents(d1, copy).0, copy) + } else { + (d1, copy) + } +} + +/// What the caller of [`clone_node`] guarantees: `node` is a node of a +/// well-formed arena, and not its document. +pub open spec fn clone_requires(doc: Document, node: NodeId) -> bool { + &&& wf(doc) + &&& valid(doc, node) + &&& !(doc.nodes[node as int].kind is Document) +} + +// --------------------------------------------------------------------- +// Deep cloning (DOM §4.4† "clone a node" with the clone children flag) +// +// Modelling choices of this part: +// - **The spec is a relation, not a function.** DOM's step 6 recurses, and +// the order of its recursion does not fix which node id each copy gets. +// What it fixes is the shape: the copy is a fresh tree that is the +// "unfolding" of the host-including subtree of `node` (children, and a +// template's contents), each copied node carrying its source's kind. So +// the spec says exactly that: the new nodes are `base .. base + m`, and a +// map `src` from each new node (by offset) to the node it copies is +// consistent at every edge ([`deep_clone_src`]). A new node other than the +// copy has a parent or a host, so no node outside the copy is made. Any +// assignment of ids that satisfies it is a correct clone. +// - **Template contents are cloned by HTML §4.12.3's cloning steps:** "1. If +// the clone children flag is not set in the calling clone algorithm, +// return. 2. Let copied contents be the result of cloning all the children +// of node's template contents, with document set to copy's template +// contents's node document, and the clone children flag set. 3. Append +// copied contents to copy's template contents." The copy's contents is the +// fragment "create an element" made ([`clone_node`], shallow); its source +// is the source's contents, and its children are copies of that +// fragment's children, which is how every node is related to its source. +// - **A refusal changes nothing, and happens only when the copy cannot +// fit** (`contract::clone_deep_ensures`): the arena is rolled back to what +// it was, and a refusal is allowed only if the arena cannot hold +// [`subtree_size`] more nodes. A clone that succeeds is [`wf`], so within +// the node limit, and adds exactly [`subtree_size`] nodes: the copy is the +// unfolding of the source's host-including subtree, nothing more. +// - **The source arena must be host-acyclic** ([`host_acyclic`], required by +// `contract::clone_deep_requires`). [`wf`] forbids a cycle of parents but +// not one through a template's host (a template that is a descendant of +// its own contents); the unfolding of such a subtree is infinite, so a +// clone of it can only be refused although [`subtree_size`] (a count of +// nodes) is small. The arenas the DOM algorithms build never have one (the +// pre-insertion check refuses to insert a host-including inclusive +// ancestor of the parent), and `host_acyclic_lemmas` proves it: every +// operation keeps [`host_acyclic`], so it is an invariant that a caller +// gets from [`new_document`] and the operations. It is not part of [`wf`] +// because the parser's raw [`insert`] and [`reparent_children`] check only +// the inclusive-ancestor half of step 2, and `clazel-treebuilder` has not +// been proven to exclude the host half. +// - **A fragment that is cloned directly** (a template's contents is the +// only kind) gets no host: "any other node: do nothing" (§4.4† step 3). + +/// Node `b + t` of `out` is the copy of `src[t]`, where `b` is the arena's +/// length before the clone and `m` the number of new nodes. This is the +/// local meaning of §4.4† steps 2 and 3 (the copy's kind: [`clone_kind`] +/// with its template contents linked as HTML §4.12.3's cloning steps say), +/// of step 6 ("for each child of node's children, in tree order: let +/// childCopy be the result of cloning child …, and append childCopy to +/// copy": the copy's children are copies of the source's, in order), and +/// of HTML's steps 2 and 3 (the contents' children are copies). +pub open spec fn deep_copy_kind(doc: Document, out: Document, src: Seq, t: int) -> bool { + let b = doc.nodes.len(); + let m = src.len(); + let s = doc.nodes[src[t] as int]; + let n = out.nodes[b + t]; + // The copy is a new node: no wrapper, and but for the root a parent or + // a host (the root has no parent). + &&& n.wrapper is None + &&& t == 0 ==> n.parent is None + &&& t > 0 ==> (n.parent is Some || (n.kind is DocumentFragment && n.kind->DocumentFragment_host is Some)) + // 2. and 3.: the copy's kind. + &&& match s.kind { + NodeKind::Document { .. } => false, + NodeKind::Element { namespace, prefix, local_name, attributes, template_contents, .. } => { + &&& n.kind is Element + &&& n.kind->Element_namespace == namespace + &&& n.kind->Element_prefix == prefix + &&& n.kind->Element_local_name == local_name + &&& n.kind->Element_attributes == attributes + &&& n.kind->Element_form_owner is None + // HTML §4.12.3: a template's copy has contents, which copy its + // source's. + &&& (template_contents is None) == (n.kind->Element_template_contents is None) + &&& template_contents is Some ==> { + let c = n.kind->Element_template_contents->Some_0 as int; + &&& b <= c < b + m + &&& src[c - b] == template_contents->Some_0 + } + }, + NodeKind::DocumentFragment { .. } => { + &&& n.kind is DocumentFragment + &&& t == 0 ==> n.kind->DocumentFragment_host is None + }, + other => n.kind == other, + } +} + +/// The children of node `b + t` of `out` are copies of those of `src[t]`, in +/// order (§4.4† step 6, and HTML §4.12.3's step 2 for a template's +/// contents): "for each child of node's children, in tree order: let +/// childCopy be the result of cloning child …, and append childCopy to +/// copy". +pub open spec fn deep_copy_children(doc: Document, out: Document, src: Seq, t: int) -> bool { + let b = doc.nodes.len(); + let m = src.len(); + let s = doc.nodes[src[t] as int]; + let n = out.nodes[b + t]; + &&& n.children.len() == s.children.len() + &&& forall|j: int| 0 <= j < s.children.len() ==> { + let c = #[trigger] n.children[j] as int; + &&& b <= c < b + m + &&& src[c - b] == s.children[j] + } +} + +/// Node `b + t` of `out` is the copy of `src[t]`: its own fields +/// ([`deep_copy_kind`]) and its children ([`deep_copy_children`]). +pub open spec fn deep_copy_node(doc: Document, out: Document, src: Seq, t: int) -> bool { + &&& deep_copy_kind(doc, out, src, t) + &&& deep_copy_children(doc, out, src, t) +} + +/// `out` is `doc` with a copy of `node`'s host-including subtree added, as +/// the nodes `doc.nodes.len() ..`, the first being the copy of `node`, and +/// `src[t]` the node that the new node `doc.nodes.len() + t` copies. +pub open spec fn deep_clone_src(doc: Document, node: NodeId, out: Document, src: Seq) -> bool { + let b = doc.nodes.len(); + &&& src.len() >= 1 + &&& out.nodes.len() == b + src.len() + // No existing node changes: the copy is attached to nothing. + &&& forall|i: int| 0 <= i < b ==> #[trigger] out.nodes[i] == doc.nodes[i] + &&& src[0] == node + &&& forall|t: int| 0 <= t < src.len() ==> valid(doc, #[trigger] src[t]) + &&& forall|t: int| 0 <= t < src.len() ==> #[trigger] deep_copy_node(doc, out, src, t) +} + +/// DOM §4.4† "clone a node" `node` with the clone children flag set: +/// `out` is the arena after it, and `copy` the copy (the first new node). +/// ("7. Return copy.") The copy is the id `doc.nodes.len()`. +pub open spec fn deep_clone(doc: Document, node: NodeId, out: Document, copy: NodeId) -> bool { + &&& copy == doc.nodes.len() + &&& exists|src: Seq| deep_clone_src(doc, node, out, src) +} + +/// A ranking of the arena's nodes that grows along every parent edge and +/// every host edge, at node `i`: its rank is below the node count, and above +/// the rank of its parent and, if it is a fragment with a host, of that host. +pub open spec fn ranked_at(doc: Document, rank: Seq, i: int) -> bool { + let n = doc.nodes.len(); + let node = doc.nodes[i]; + &&& rank[i] < n + &&& node.parent is Some ==> node.parent->Some_0 < n && rank[node.parent->Some_0 as int] < rank[i] + &&& (node.kind is DocumentFragment && node.kind->DocumentFragment_host is Some) ==> { + let h = node.kind->DocumentFragment_host->Some_0; + h < n && rank[h as int] < rank[i] + } +} + +/// `rank` ranks every node of the arena ([`ranked_at`]). +pub open spec fn host_ranked(doc: Document, rank: Seq) -> bool { + &&& rank.len() == doc.nodes.len() + &&& forall|i: int| 0 <= i < doc.nodes.len() ==> #[trigger] ranked_at(doc, rank, i) +} + +/// The arena has no cycle through parents and template hosts: following a +/// node's parent, or a parentless fragment's host, never returns to it +/// (witnessed by a ranking that grows along every such step, with ranks +/// below the node count). [`wf`] gives the parent half +/// (its `paths`); the host half is not part of it (see the modelling +/// choices of deep cloning). Every operation keeps it +/// (`host_acyclic_lemmas`). +pub open spec fn host_acyclic(doc: Document) -> bool { + exists|rank: Seq| host_ranked(doc, rank) +} + +/// How many of the first `k` nodes of the arena are host-including +/// descendants of `node`, counting `node` itself: `node` is a +/// host-including inclusive ancestor of them (DOM §4.2). +pub open spec fn subtree_count(doc: Document, node: NodeId, k: nat) -> nat + decreases k, +{ + if k == 0 { + 0 + } else { + subtree_count(doc, node, (k - 1) as nat) + if is_host_including_inclusive_ancestor(doc, node, (k - 1) as NodeId) { + 1nat + } else { + 0nat + } + } +} + +/// The size of `node`'s host-including subtree: the nodes a deep clone of +/// `node` copies (its descendants, and those of a template's contents, +/// which HTML §4.12.3's cloning steps copy too, and the contents). +pub open spec fn subtree_size(doc: Document, node: NodeId) -> nat { + subtree_count(doc, node, doc.nodes.len()) +} + +// --------------------------------------------------------------------- +// Attribute changes (DOM §4.9†) + +/// The element `e` with its attribute list replaced by `attrs`. +pub open spec fn set_attributes(doc: Document, e: NodeId, attrs: Seq) -> Document { + let node = doc.nodes[e as int]; + let k = match node.kind { + NodeKind::Element { namespace, prefix, local_name, template_contents, form_owner, .. } => NodeKind::Element { + namespace, + prefix, + local_name, + attributes: attrs, + template_contents, + form_owner, + }, + other => other, + }; + Document { nodes: doc.nodes.update(e as int, Node { kind: k, ..node }) } +} + +/// DOM §4.9† "change an attribute" (the `i`th of `e`'s attribute list) "to +/// value": "1. Let oldValue be attribute's value. 2. Set attribute's value +/// to value. 3. Handle attribute changes for attribute with attribute's +/// element, oldValue, and value." (Not modelled.) +pub open spec fn change_attribute(doc: Document, e: NodeId, i: nat, value: Seq) -> Document { + let attrs = doc.nodes[e as int].kind->Element_attributes; + set_attributes(doc, e, attrs.update(i as int, Attr { value, ..attrs[i as int] })) +} + +/// DOM §4.9† "remove an attribute" (the `i`th of `e`'s attribute list): +/// "1. Let element be attribute's element. 2. Remove attribute from +/// element's attribute list. 3. Set attribute's element to null. 4. Handle +/// attribute changes for attribute with element, attribute's value, and +/// null." (Step 3 is implicit, step 4 not modelled.) +pub open spec fn remove_attribute(doc: Document, e: NodeId, i: nat) -> Document { + set_attributes(doc, e, doc.nodes[e as int].kind->Element_attributes.remove(i as int)) +} + +/// DOM §4.9† "replace an attribute" (the `i`th of `e`'s attribute list, +/// oldAttr) "with newAttr": "1. Replace oldAttr by newAttr in oldAttr's +/// element's attribute list. 2. Set newAttr's element to oldAttr's +/// element. 3. Set newAttr's node document to element's node document. 4. +/// Set oldAttr's element to null. 5. Handle attribute changes for oldAttr +/// with oldAttr's element, oldAttr's value, and newAttr's value." (Steps 2 +/// to 4 are implicit, step 5 not modelled.) +pub open spec fn replace_attribute(doc: Document, e: NodeId, i: nat, new_attr: Attr) -> Document { + set_attributes(doc, e, doc.nodes[e as int].kind->Element_attributes.update(i as int, new_attr)) +} + +/// What the callers of the attribute changes above guarantee: `e` is an +/// element of a well-formed arena and `i` names one of its attributes. +pub open spec fn attribute_requires(doc: Document, e: NodeId, i: nat) -> bool { + &&& wf(doc) + &&& valid(doc, e) + &&& doc.nodes[e as int].kind is Element + &&& i < doc.nodes[e as int].kind->Element_attributes.len() +} + +// --------------------------------------------------------------------- +// The children rules, stated on their own (DOM §4.2.3† "ensure +// pre-insertion validity" steps 5 and 6, "replace" steps 5 and 6) + +/// The rules a document's children obey (DOM §4.2.3† steps 5 and 6 of +/// "ensure pre-insertion validity" keep them true), stated about the +/// sequence itself, not as checks on an insertion: no `Text` node, at +/// most one element, at most one doctype, and the doctype is not after the +/// element. (Comments and processing instructions may be anywhere.) +pub open spec fn document_children_ok(doc: Document, kids: Seq) -> bool { + &&& forall|j: int| 0 <= j < kids.len() ==> !(doc.nodes[#[trigger] kids[j] as int].kind is Text) + &&& forall|j: int, k: int| 0 <= j < k < kids.len() ==> !(doc.nodes[#[trigger] kids[j] as int].kind is Element + && doc.nodes[#[trigger] kids[k] as int].kind is Element) + &&& forall|j: int, k: int| 0 <= j < k < kids.len() ==> !(doc.nodes[#[trigger] kids[j] as int].kind is DocumentType + && doc.nodes[#[trigger] kids[k] as int].kind is DocumentType) + &&& forall|j: int, k: int| 0 <= j < k < kids.len() ==> !(doc.nodes[#[trigger] kids[j] as int].kind is Element + && doc.nodes[#[trigger] kids[k] as int].kind is DocumentType) +} + +/// The children rules hold in the whole arena: only a document has a +/// doctype child (step 5: "node is a doctype and parent is not a +/// document"), and the document node's children obey +/// [`document_children_ok`] (step 6). The document node, `nodes[0]`, is the +/// only document ([`wf`]). +pub open spec fn children_rules_hold(doc: Document) -> bool { + &&& forall|p: int, j: int| 0 <= p < doc.nodes.len() && !(doc.nodes[p].kind is Document) && 0 <= j < doc.nodes[p].children.len() + ==> !(doc.nodes[#[trigger] doc.nodes[p].children[j] as int].kind is DocumentType) + &&& 1 <= doc.nodes.len() && document_children_ok(doc, doc.nodes[0].children) +} + } // verus! diff --git a/crates/clazel-dom/src/validity_lemmas.rs b/crates/clazel-dom/src/validity_lemmas.rs new file mode 100644 index 000000000..bc9e08cd5 --- /dev/null +++ b/crates/clazel-dom/src/validity_lemmas.rs @@ -0,0 +1,1466 @@ +//! Phase 7 item 7.13: the checks of DOM §4.2.3† "ensure pre-insertion +//! validity" and "replace", steps 5 and 6, enforce the children rules +//! ([`children_rules_hold`]) exactly. +//! +//! For a call whose node has no parent, and which passes steps 1 to 4: +//! it passes steps 5 and 6 if and only if the tree after the insertion +//! obeys the rules. [`theorem_pre_insert_children_rules`] and +//! [`theorem_replace_children_rules`] are the two theorems (listed in +//! `Cargo.toml`); everything else here is their machinery. +//! +//! The counts of the checks ([`count_tag`]) are bridged to the positions +//! the rules talk about ([`document_children_ok`]) by the `lemma_count_*` +//! lemmas; [`lemma_ok_insert_nodes`] and [`lemma_ok_insert_doctype`] say +//! when inserting a list of nodes in the middle of a document's children +//! keeps the rules. + +#[cfg(verus_only)] +use super::contract::*; +#[cfg(verus_only)] +use super::lemmas::*; +#[cfg(verus_only)] +use super::ops_lemmas::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// --------------------------------------------------------------------- +// Counts and positions + +/// A count is positive exactly when some entry has the tag (and is not the +/// skipped node). +pub proof fn lemma_count_pos(doc: Document, s: Seq, t: NodeTag, skip: Option) + ensures + count_tag(doc, s, t, skip) > 0 <==> exists|j: int| + #![trigger s[j]] + 0 <= j < s.len() && has_tag(doc.nodes[s[j] as int].kind, t) && skip != Some(s[j]), + decreases s.len(), +{ + if s.len() > 0 { + let d = s.drop_last(); + lemma_count_pos(doc, d, t, skip); + let last = s.last(); + if has_tag(doc.nodes[last as int].kind, t) && skip != Some(last) { + assert(s[s.len() - 1] == last); + } else { + if exists|j: int| #![trigger s[j]] 0 <= j < s.len() && has_tag(doc.nodes[s[j] as int].kind, t) && skip != Some(s[j]) { + let j = choose|j: int| #![trigger s[j]] 0 <= j < s.len() && has_tag(doc.nodes[s[j] as int].kind, t) && skip != Some(s[j]); + assert(j < s.len() - 1); + assert(d[j] == s[j]); + } + } + if exists|j: int| #![trigger d[j]] 0 <= j < d.len() && has_tag(doc.nodes[d[j] as int].kind, t) && skip != Some(d[j]) { + let j = choose|j: int| #![trigger d[j]] 0 <= j < d.len() && has_tag(doc.nodes[d[j] as int].kind, t) && skip != Some(d[j]); + assert(s[j] == d[j]); + } + } +} + +/// A count is at least 2 exactly when two entries have the tag (and are not +/// the skipped node). +pub proof fn lemma_count_two(doc: Document, s: Seq, t: NodeTag, skip: Option) + ensures + count_tag(doc, s, t, skip) >= 2 <==> exists|j: int, k: int| + #![trigger s[j], s[k]] + 0 <= j < k < s.len() && has_tag(doc.nodes[s[j] as int].kind, t) && skip != Some(s[j]) && has_tag(doc.nodes[s[k] as int].kind, t) + && skip != Some(s[k]), + decreases s.len(), +{ + if s.len() > 0 { + let d = s.drop_last(); + lemma_count_two(doc, d, t, skip); + lemma_count_pos(doc, d, t, skip); + let last = s.last(); + let lt = has_tag(doc.nodes[last as int].kind, t) && skip != Some(last); + if lt { + if count_tag(doc, d, t, skip) >= 1 { + let j = choose|j: int| #![trigger d[j]] 0 <= j < d.len() && has_tag(doc.nodes[d[j] as int].kind, t) && skip != Some(d[j]); + assert(s[j] == d[j]); + assert(s[s.len() - 1] == last); + } + } + if exists|j: int, k: int| + #![trigger s[j], s[k]] + 0 <= j < k < s.len() && has_tag(doc.nodes[s[j] as int].kind, t) && skip != Some(s[j]) && has_tag(doc.nodes[s[k] as int].kind, t) + && skip != Some(s[k]) { + let (j, k) = choose|j: int, k: int| + #![trigger s[j], s[k]] + 0 <= j < k < s.len() && has_tag(doc.nodes[s[j] as int].kind, t) && skip != Some(s[j]) && has_tag(doc.nodes[s[k] as int].kind, t) + && skip != Some(s[k]); + if k == s.len() - 1 { + assert(d[j] == s[j]); + assert(0 <= j < d.len() && has_tag(doc.nodes[d[j] as int].kind, t) && skip != Some(d[j])); + } else { + assert(d[j] == s[j]); + assert(d[k] == s[k]); + } + } + if exists|j: int, k: int| + #![trigger d[j], d[k]] + 0 <= j < k < d.len() && has_tag(doc.nodes[d[j] as int].kind, t) && skip != Some(d[j]) && has_tag(doc.nodes[d[k] as int].kind, t) + && skip != Some(d[k]) { + let (j, k) = choose|j: int, k: int| + #![trigger d[j], d[k]] + 0 <= j < k < d.len() && has_tag(doc.nodes[d[j] as int].kind, t) && skip != Some(d[j]) && has_tag(doc.nodes[d[k] as int].kind, t) + && skip != Some(d[k]); + assert(s[j] == d[j]); + assert(s[k] == d[k]); + } + } +} + +/// The count of a concatenation is the sum of the counts. +pub proof fn lemma_count_add(doc: Document, a: Seq, b: Seq, t: NodeTag, skip: Option) + ensures + count_tag(doc, a + b, t, skip) == count_tag(doc, a, t, skip) + count_tag(doc, b, t, skip), + decreases b.len(), +{ + if b.len() == 0 { + assert(a + b =~= a); + } else { + lemma_count_add(doc, a, b.drop_last(), t, skip); + assert((a + b).drop_last() =~= a + b.drop_last()); + assert((a + b).last() == b.last()); + } +} + +// --------------------------------------------------------------------- +// The rules and sub-sequences + +/// A sub-sequence of a document's children (entries kept in order) obeys +/// the rules if the whole does. +pub proof fn lemma_ok_subseq(doc: Document, a: Seq, b: Seq, f: spec_fn(int) -> int) + requires + document_children_ok(doc, b), + forall|j: int| 0 <= j < a.len() ==> 0 <= #[trigger] f(j) < b.len() && b[f(j)] == a[j], + forall|j: int, k: int| 0 <= j < k < a.len() ==> #[trigger] f(j) < #[trigger] f(k), + ensures + document_children_ok(doc, a), +{ + assert forall|j: int| 0 <= j < a.len() implies !(doc.nodes[#[trigger] a[j] as int].kind is Text) by { + assert(b[f(j)] == a[j]); + } + assert forall|j: int, k: int| 0 <= j < k < a.len() implies !(doc.nodes[#[trigger] a[j] as int].kind is Element + && doc.nodes[#[trigger] a[k] as int].kind is Element) by { + assert(b[f(j)] == a[j]); + assert(b[f(k)] == a[k]); + assert(f(j) < f(k)); + } + assert forall|j: int, k: int| 0 <= j < k < a.len() implies !(doc.nodes[#[trigger] a[j] as int].kind is DocumentType + && doc.nodes[#[trigger] a[k] as int].kind is DocumentType) by { + assert(b[f(j)] == a[j]); + assert(b[f(k)] == a[k]); + assert(f(j) < f(k)); + } + assert forall|j: int, k: int| 0 <= j < k < a.len() implies !(doc.nodes[#[trigger] a[j] as int].kind is Element + && doc.nodes[#[trigger] a[k] as int].kind is DocumentType) by { + assert(b[f(j)] == a[j]); + assert(b[f(k)] == a[k]); + assert(f(j) < f(k)); + } +} + +/// Dropping the middle of `p + mid + s` keeps the rules. +pub proof fn lemma_ok_drop_mid(doc: Document, p: Seq, mid: Seq, s: Seq) + requires + document_children_ok(doc, p + mid + s), + ensures + document_children_ok(doc, p + s), +{ + let m = mid.len() as int; + let pl = p.len() as int; + lemma_ok_subseq(doc, p + s, p + mid + s, |j: int| if j < pl { j } else { j + m }); +} + +/// The entries of `p + ins + s` and of `p + s`, by position. +proof fn lemma_concat_index(p: Seq, ins: Seq, s: Seq) + ensures + (p + ins + s).len() == p.len() + ins.len() + s.len(), + (p + s).len() == p.len() + s.len(), + forall|a: int| 0 <= a < p.len() ==> (#[trigger] (p + ins + s)[a]) == p[a], + forall|a: int| p.len() <= a < p.len() + ins.len() ==> (#[trigger] (p + ins + s)[a]) == ins[a - p.len()], + forall|a: int| p.len() + ins.len() <= a < p.len() + ins.len() + s.len() ==> (#[trigger] (p + ins + s)[a]) == s[a - p.len() - ins.len()], + forall|a: int| 0 <= a < p.len() ==> (#[trigger] (p + s)[a]) == p[a], + forall|a: int| p.len() <= a < p.len() + s.len() ==> (#[trigger] (p + s)[a]) == s[a - p.len()], +{ +} + +/// Inserting `ins`, none of which is a doctype, between `p` and `s`, in a +/// document whose children `p + s` obey the rules: the result obeys them +/// exactly when `ins` has no `Text` and at most one element, and, if it has +/// an element, neither `p` nor `s` has one and `s` has no doctype. +#[verifier::rlimit(100)] +pub proof fn lemma_ok_insert_nodes(doc: Document, p: Seq, ins: Seq, s: Seq) + requires + document_children_ok(doc, p + s), + forall|j: int| 0 <= j < ins.len() ==> !(doc.nodes[#[trigger] ins[j] as int].kind is DocumentType), + ensures + document_children_ok(doc, p + ins + s) <==> (count_tag(doc, ins, NodeTag::Text, None) == 0 && count_tag(doc, ins, NodeTag::Element, None) <= 1 + && (count_tag(doc, ins, NodeTag::Element, None) == 0 || (count_tag(doc, p + s, NodeTag::Element, None) == 0 && count_tag( + doc, + s, + NodeTag::DocumentType, + None, + ) == 0))), +{ + let q = p + ins + s; + let ps = p + s; + let pl = p.len() as int; + let il = ins.len() as int; + lemma_concat_index(p, ins, s); + lemma_count_pos(doc, ins, NodeTag::Text, None); + lemma_count_pos(doc, ins, NodeTag::Element, None); + lemma_count_two(doc, ins, NodeTag::Element, None); + lemma_count_pos(doc, ps, NodeTag::Element, None); + lemma_count_pos(doc, s, NodeTag::DocumentType, None); + let tx = count_tag(doc, ins, NodeTag::Text, None); + let el = count_tag(doc, ins, NodeTag::Element, None); + let el_ps = count_tag(doc, ps, NodeTag::Element, None); + let dt_s = count_tag(doc, s, NodeTag::DocumentType, None); + if document_children_ok(doc, q) { + if tx > 0 { + let j = choose|j: int| #![trigger ins[j]] 0 <= j < ins.len() && doc.nodes[ins[j] as int].kind is Text; + assert(q[pl + j] == ins[j]); + } + if el >= 2 { + let (j, k) = choose|j: int, k: int| + #![trigger ins[j], ins[k]] + 0 <= j < k < ins.len() && doc.nodes[ins[j] as int].kind is Element && doc.nodes[ins[k] as int].kind is Element; + assert(q[pl + j] == ins[j]); + assert(q[pl + k] == ins[k]); + } + if el > 0 { + let j = choose|j: int| #![trigger ins[j]] 0 <= j < ins.len() && doc.nodes[ins[j] as int].kind is Element; + assert(q[pl + j] == ins[j]); + if el_ps > 0 { + let m = choose|m: int| #![trigger ps[m]] 0 <= m < ps.len() && doc.nodes[ps[m] as int].kind is Element; + if m < pl { + assert(q[m] == p[m]); + assert(ps[m] == p[m]); + } else { + assert(q[m + il] == s[m - pl]); + assert(ps[m] == s[m - pl]); + } + } + if dt_s > 0 { + let m = choose|m: int| #![trigger s[m]] 0 <= m < s.len() && doc.nodes[s[m] as int].kind is DocumentType; + assert(q[pl + il + m] == s[m]); + } + } + } + if tx == 0 && el <= 1 && (el == 0 || (el_ps == 0 && dt_s == 0)) { + // No Text, and any element of ps would show up in the count. + assert forall|a: int| 0 <= a < q.len() implies !(doc.nodes[#[trigger] q[a] as int].kind is Text) by { + if a < pl { + assert(ps[a] == p[a]); + } else if a < pl + il { + assert(q[a] == ins[a - pl]); + } else { + assert(ps[a - il] == s[a - pl - il]); + } + } + assert forall|a: int, b: int| 0 <= a < b < q.len() implies !(doc.nodes[#[trigger] q[a] as int].kind is Element + && doc.nodes[#[trigger] q[b] as int].kind is Element) by { + if b < pl { + assert(ps[a] == p[a]); + assert(ps[b] == p[b]); + } else if b < pl + il { + assert(q[b] == ins[b - pl]); + if a < pl { + assert(ps[a] == p[a]); + } else { + assert(q[a] == ins[a - pl]); + } + } else { + assert(q[b] == s[b - pl - il]); + assert(ps[b - il] == s[b - pl - il]); + if a < pl { + assert(ps[a] == p[a]); + } else if a < pl + il { + assert(q[a] == ins[a - pl]); + } else { + assert(ps[a - il] == s[a - pl - il]); + } + } + } + assert forall|a: int, b: int| 0 <= a < b < q.len() implies !(doc.nodes[#[trigger] q[a] as int].kind is DocumentType + && doc.nodes[#[trigger] q[b] as int].kind is DocumentType) by { + if b < pl { + assert(ps[a] == p[a]); + assert(ps[b] == p[b]); + } else if b < pl + il { + assert(q[b] == ins[b - pl]); + } else { + assert(q[b] == s[b - pl - il]); + assert(ps[b - il] == s[b - pl - il]); + if a < pl { + assert(ps[a] == p[a]); + } else if a < pl + il { + assert(q[a] == ins[a - pl]); + } else { + assert(ps[a - il] == s[a - pl - il]); + } + } + } + assert forall|a: int, b: int| 0 <= a < b < q.len() implies !(doc.nodes[#[trigger] q[a] as int].kind is Element + && doc.nodes[#[trigger] q[b] as int].kind is DocumentType) by { + if b < pl { + assert(ps[a] == p[a]); + assert(ps[b] == p[b]); + } else if b < pl + il { + assert(q[b] == ins[b - pl]); + } else { + assert(q[b] == s[b - pl - il]); + assert(ps[b - il] == s[b - pl - il]); + if a < pl { + assert(ps[a] == p[a]); + } else if a < pl + il { + assert(q[a] == ins[a - pl]); + } else { + assert(ps[a - il] == s[a - pl - il]); + } + } + } + } +} + +/// Inserting the doctype `d` between `p` and `s`, in a document whose +/// children `p + s` obey the rules: the result obeys them exactly when +/// `p + s` has no doctype and `p` has no element. +#[verifier::rlimit(100)] +pub proof fn lemma_ok_insert_doctype(doc: Document, p: Seq, d: NodeId, s: Seq) + requires + document_children_ok(doc, p + s), + doc.nodes[d as int].kind is DocumentType, + ensures + document_children_ok(doc, p + seq![d] + s) <==> (count_tag(doc, p + s, NodeTag::DocumentType, None) == 0 && count_tag( + doc, + p, + NodeTag::Element, + None, + ) == 0), +{ + let q = p + seq![d] + s; + let ps = p + s; + let pl = p.len() as int; + lemma_concat_index(p, seq![d], s); + lemma_count_pos(doc, ps, NodeTag::DocumentType, None); + lemma_count_pos(doc, p, NodeTag::Element, None); + let dt_ps = count_tag(doc, ps, NodeTag::DocumentType, None); + let el_p = count_tag(doc, p, NodeTag::Element, None); + assert(q[pl] == d); + if document_children_ok(doc, q) { + if dt_ps > 0 { + let m = choose|m: int| #![trigger ps[m]] 0 <= m < ps.len() && doc.nodes[ps[m] as int].kind is DocumentType; + if m < pl { + assert(q[m] == p[m]); + assert(ps[m] == p[m]); + } else { + assert(q[m + 1] == s[m - pl]); + assert(ps[m] == s[m - pl]); + } + } + if el_p > 0 { + let m = choose|m: int| #![trigger p[m]] 0 <= m < p.len() && doc.nodes[p[m] as int].kind is Element; + assert(q[m] == p[m]); + } + } + if dt_ps == 0 && el_p == 0 { + assert forall|a: int| 0 <= a < q.len() implies !(doc.nodes[#[trigger] q[a] as int].kind is Text) by { + if a < pl { + assert(ps[a] == p[a]); + } else if a == pl { + } else { + assert(ps[a - 1] == s[a - pl - 1]); + } + } + assert forall|a: int, b: int| 0 <= a < b < q.len() implies !(doc.nodes[#[trigger] q[a] as int].kind is Element + && doc.nodes[#[trigger] q[b] as int].kind is Element) by { + if b < pl { + assert(ps[a] == p[a]); + assert(ps[b] == p[b]); + } else if b == pl { + } else { + assert(q[b] == s[b - pl - 1]); + assert(ps[b - 1] == s[b - pl - 1]); + if a < pl { + assert(ps[a] == p[a]); + } else if a == pl { + } else { + assert(ps[a - 1] == s[a - pl - 1]); + } + } + } + assert forall|a: int, b: int| 0 <= a < b < q.len() implies !(doc.nodes[#[trigger] q[a] as int].kind is DocumentType + && doc.nodes[#[trigger] q[b] as int].kind is DocumentType) by { + if b < pl { + assert(ps[a] == p[a]); + assert(ps[b] == p[b]); + } else if b == pl { + assert(ps[a] == p[a]); + } else { + assert(q[b] == s[b - pl - 1]); + assert(ps[b - 1] == s[b - pl - 1]); + if a < pl { + assert(ps[a] == p[a]); + } else if a == pl { + } else { + assert(ps[a - 1] == s[a - pl - 1]); + } + } + } + assert forall|a: int, b: int| 0 <= a < b < q.len() implies !(doc.nodes[#[trigger] q[a] as int].kind is Element + && doc.nodes[#[trigger] q[b] as int].kind is DocumentType) by { + if b < pl { + assert(ps[a] == p[a]); + assert(ps[b] == p[b]); + } else if b == pl { + assert(ps[a] == p[a]); + } else { + assert(q[b] == s[b - pl - 1]); + assert(ps[b - 1] == s[b - pl - 1]); + if a < pl { + assert(ps[a] == p[a]); + } else if a == pl { + } else { + assert(ps[a - 1] == s[a - pl - 1]); + } + } + } + } +} + +// --------------------------------------------------------------------- +// The checks of step 6, by the split they are about + +/// Skipping a node that is not in the sequence changes no count. +pub proof fn lemma_count_skip_absent(doc: Document, s: Seq, t: NodeTag, c: NodeId) + requires + !s.contains(c), + ensures + count_tag(doc, s, t, Some(c)) == count_tag(doc, s, t, None), + decreases s.len(), +{ + if s.len() > 0 { + let d = s.drop_last(); + assert(s[s.len() - 1] == s.last()); + assert forall|j: int| 0 <= j < d.len() implies d[j] != c by { + assert(d[j] == s[j]); + } + assert(!d.contains(c)); + lemma_count_skip_absent(doc, d, t, c); + assert(s.last() != c); + } +} + +/// The count of a one-element sequence. +pub proof fn lemma_count_single(doc: Document, c: NodeId, t: NodeTag, skip: Option) + ensures + count_tag(doc, seq![c], t, skip) == if has_tag(doc.nodes[c as int].kind, t) && skip != Some(c) { + 1nat + } else { + 0nat + }, +{ + assert(seq![c].drop_last() =~= Seq::::empty()); + assert(seq![c].last() == c); + assert(count_tag(doc, Seq::::empty(), t, skip) == 0); +} + +/// Skipping `c` in `p + [c] + s`, which has `c` only in the middle, counts +/// what `p` and `s` have. +pub proof fn lemma_count_skip_split(doc: Document, p: Seq, c: NodeId, s: Seq, t: NodeTag) + requires + !p.contains(c), + !s.contains(c), + ensures + count_tag(doc, p + seq![c] + s, t, Some(c)) == count_tag(doc, p, t, None) + count_tag(doc, s, t, None), +{ + lemma_count_add(doc, p + seq![c], s, t, Some(c)); + lemma_count_add(doc, p, seq![c], t, Some(c)); + lemma_count_single(doc, c, t, Some(c)); + lemma_count_skip_absent(doc, p, t, c); + lemma_count_skip_absent(doc, s, t, c); +} + +/// The nodes `node` contributes to its new parent's children: a +/// fragment's children, or the node itself. +pub open spec fn inserted_nodes(doc: Document, node: NodeId) -> Seq { + if doc.nodes[node as int].kind is DocumentFragment { + doc.nodes[node as int].children + } else { + seq![node] + } +} + +/// The statements of step 6, switched on the interface `node` implements +/// (both "ensure pre-insertion validity" and "replace"), in terms of the +/// children `p` before and `s` after the place the nodes go in: "parent has +/// an element child" is a count over `p + s`, "a doctype is following +/// child" a count over `s`, "an element is preceding child" one over `p`. +pub open spec fn rules_violation(doc: Document, node: NodeId, p: Seq, s: Seq) -> bool { + match doc.nodes[node as int].kind { + NodeKind::DocumentFragment { .. } => { + let el = count_tag(doc, doc.nodes[node as int].children, NodeTag::Element, None); + let tx = count_tag(doc, doc.nodes[node as int].children, NodeTag::Text, None); + el > 1 || tx > 0 || (el == 1 && (count_tag(doc, p + s, NodeTag::Element, None) > 0 || count_tag(doc, s, NodeTag::DocumentType, None) > 0)) + }, + NodeKind::Element { .. } => count_tag(doc, p + s, NodeTag::Element, None) > 0 || count_tag(doc, s, NodeTag::DocumentType, None) > 0, + NodeKind::DocumentType { .. } => count_tag(doc, p + s, NodeTag::DocumentType, None) > 0 || count_tag(doc, p, NodeTag::Element, None) > 0, + _ => false, + } +} + +/// Where "ensure pre-insertion validity" splits `parent`'s children: before +/// `child`, or at the end. +pub open spec fn split_index(kids: Seq, child: Option) -> int { + match child { + Some(c) => index_of(kids, c), + None => kids.len() as int, + } +} + +/// The doctypes of `parent`'s children from `child` on, as step 6 counts +/// them: `child` itself and the siblings after it. +proof fn lemma_pre_insert_doctypes_from(doc: Document, parent: NodeId, child: Option) + requires + wf(doc), + valid(doc, parent), + match child { + None => true, + Some(c) => valid(doc, c) && doc.nodes[c as int].parent == Some(parent), + }, + ensures + ({ + let kids = children(doc, parent); + let k = split_index(kids, child); + &&& 0 <= k <= kids.len() + &&& kids.subrange(0, k) + kids.subrange(k, kids.len() as int) == kids + &&& (count_tag(doc, kids.subrange(k, kids.len() as int), NodeTag::DocumentType, None) > 0) == (match child { + Some(c) => doc.nodes[c as int].kind is DocumentType || count_tag( + doc, + kids.subrange(index_of(kids, c) + 1, kids.len() as int), + NodeTag::DocumentType, + None, + ) > 0, + None => false, + }) + }), +{ + let kids = children(doc, parent); + let k = split_index(kids, child); + if let Some(c) = child { + assert(kids.contains(c)); + lemma_index_of(kids, c); + } + let p = kids.subrange(0, k); + let s = kids.subrange(k, kids.len() as int); + assert(p + s =~= kids); + if let Some(c) = child { + let after_s = kids.subrange(k + 1, kids.len() as int); + assert(s =~= seq![c] + after_s); + lemma_count_add(doc, seq![c], after_s, NodeTag::DocumentType, None); + lemma_count_single(doc, c, NodeTag::DocumentType, None); + } else { + assert(s =~= Seq::::empty()); + assert(count_tag(doc, Seq::::empty(), NodeTag::DocumentType, None) == 0); + } +} + +/// Step 6 of "ensure pre-insertion validity" is [`rules_violation`] with +/// the children split at `child`. +#[verifier::spinoff_prover] +#[verifier::rlimit(40)] +pub proof fn lemma_pre_insert_violation(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + wf(doc), + valid(doc, node), + valid(doc, parent), + match child { + None => true, + Some(c) => valid(doc, c) && doc.nodes[c as int].parent == Some(parent), + }, + ensures + ({ + let kids = children(doc, parent); + let k = split_index(kids, child); + &&& 0 <= k <= kids.len() + &&& kids.subrange(0, k) + kids.subrange(k, kids.len() as int) == kids + &&& pre_insert_document_violation(doc, node, parent, child) == rules_violation( + doc, + node, + kids.subrange(0, k), + kids.subrange(k, kids.len() as int), + ) + }), +{ + lemma_pre_insert_doctypes_from(doc, parent, child); + let kids = children(doc, parent); + let k = split_index(kids, child); + let p = kids.subrange(0, k); + let s = kids.subrange(k, kids.len() as int); + lemma_count_add(doc, p, s, NodeTag::Element, None); + lemma_count_add(doc, p, s, NodeTag::DocumentType, None); +} + +/// Step 6 of "replace" is [`rules_violation`] with the children split at +/// `child`, which is left out. +pub proof fn lemma_replace_violation(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + wf(doc), + valid(doc, node), + valid(doc, parent), + valid(doc, child), + doc.nodes[child as int].parent == Some(parent), + ensures + ({ + let kids = children(doc, parent); + let i = index_of(kids, child); + &&& 0 <= i < kids.len() + &&& kids.subrange(0, i) + seq![child] + kids.subrange(i + 1, kids.len() as int) == kids + &&& replace_document_violation(doc, child, node, parent) == rules_violation( + doc, + node, + kids.subrange(0, i), + kids.subrange(i + 1, kids.len() as int), + ) + }), +{ + let kids = children(doc, parent); + assert(kids.contains(child)); + lemma_index_of(kids, child); + let i = index_of(kids, child); + let p = kids.subrange(0, i); + let s = kids.subrange(i + 1, kids.len() as int); + assert(p + seq![child] + s =~= kids); + assert(kids.no_duplicates()); + // `child` is neither in `p` nor in `s`. + assert forall|j: int| 0 <= j < p.len() implies p[j] != child by { + assert(p[j] == kids[j]); + assert(kids[i] == child); + } + assert forall|j: int| 0 <= j < s.len() implies s[j] != child by { + assert(s[j] == kids[i + 1 + j]); + assert(kids[i] == child); + } + assert(!p.contains(child)); + assert(!s.contains(child)); + lemma_count_skip_split(doc, p, child, s, NodeTag::Element); + lemma_count_skip_split(doc, p, child, s, NodeTag::DocumentType); + lemma_count_add(doc, p, s, NodeTag::Element, None); + lemma_count_add(doc, p, s, NodeTag::DocumentType, None); +} + +/// A node `node` (not a document) inserted between `p` and `s`, in a +/// document whose children `p + s` obey the rules, and whose fragments' +/// children are not doctypes: the children afterwards obey the rules +/// exactly when `node` is not a `Text` node and [`rules_violation`] does +/// not hold. +pub proof fn lemma_ok_insert_node(doc: Document, node: NodeId, p: Seq, s: Seq) + requires + document_children_ok(doc, p + s), + !(doc.nodes[node as int].kind is Document), + doc.nodes[node as int].kind is DocumentFragment ==> forall|j: int| + 0 <= j < doc.nodes[node as int].children.len() ==> !(doc.nodes[#[trigger] doc.nodes[node as int].children[j] as int].kind is DocumentType), + ensures + document_children_ok(doc, p + inserted_nodes(doc, node) + s) <==> (!(doc.nodes[node as int].kind is Text) && !rules_violation( + doc, + node, + p, + s, + )), +{ + let k = doc.nodes[node as int].kind; + let ins = inserted_nodes(doc, node); + if k is DocumentType { + lemma_ok_insert_doctype(doc, p, node, s); + } else { + if !(k is DocumentFragment) { + assert forall|j: int| 0 <= j < ins.len() implies !(doc.nodes[#[trigger] ins[j] as int].kind is DocumentType) by { + assert(ins[j] == node); + } + } + lemma_ok_insert_nodes(doc, p, ins, s); + lemma_count_add(doc, p, s, NodeTag::Element, None); + if !(k is DocumentFragment) { + lemma_count_single(doc, node, NodeTag::Element, None); + lemma_count_single(doc, node, NodeTag::Text, None); + } + } +} + +// --------------------------------------------------------------------- +// The rules on the whole tree + +/// Only kinds matter to the rules on a sequence. +pub proof fn lemma_ok_kinds(doc: Document, d2: Document, kids: Seq) + requires + d2.nodes.len() >= doc.nodes.len(), + forall|x: int| 0 <= x < doc.nodes.len() ==> (#[trigger] d2.nodes[x]).kind == doc.nodes[x].kind, + forall|j: int| 0 <= j < kids.len() ==> (#[trigger] kids[j]) < doc.nodes.len(), + document_children_ok(doc, kids), + ensures + document_children_ok(d2, kids), +{ + assert forall|j: int| 0 <= j < kids.len() implies !(d2.nodes[#[trigger] kids[j] as int].kind is Text) by { + assert(d2.nodes[kids[j] as int].kind == doc.nodes[kids[j] as int].kind); + } + assert forall|j: int, k: int| 0 <= j < k < kids.len() implies !(d2.nodes[#[trigger] kids[j] as int].kind is Element + && d2.nodes[#[trigger] kids[k] as int].kind is Element) by { + assert(d2.nodes[kids[j] as int].kind == doc.nodes[kids[j] as int].kind); + assert(d2.nodes[kids[k] as int].kind == doc.nodes[kids[k] as int].kind); + } + assert forall|j: int, k: int| 0 <= j < k < kids.len() implies !(d2.nodes[#[trigger] kids[j] as int].kind is DocumentType + && d2.nodes[#[trigger] kids[k] as int].kind is DocumentType) by { + assert(d2.nodes[kids[j] as int].kind == doc.nodes[kids[j] as int].kind); + assert(d2.nodes[kids[k] as int].kind == doc.nodes[kids[k] as int].kind); + } + assert forall|j: int, k: int| 0 <= j < k < kids.len() implies !(d2.nodes[#[trigger] kids[j] as int].kind is Element + && d2.nodes[#[trigger] kids[k] as int].kind is DocumentType) by { + assert(d2.nodes[kids[j] as int].kind == doc.nodes[kids[j] as int].kind); + assert(d2.nodes[kids[k] as int].kind == doc.nodes[kids[k] as int].kind); + } +} + +/// The rules hold in `d2`, an arena of the same nodes and kinds as `doc` +/// (where they hold) in which only `parent`'s children, now `q`, changed, +/// except that some node may have lost all its children, exactly when +/// `q` obeys them: the document rules if `parent` is the document, and no +/// doctype otherwise. +pub proof fn lemma_rules_shape(doc: Document, d2: Document, parent: NodeId, q: Seq) + requires + wf(doc), + wf(d2), + children_rules_hold(doc), + valid(doc, parent), + d2.nodes.len() == doc.nodes.len(), + forall|x: int| 0 <= x < doc.nodes.len() ==> (#[trigger] d2.nodes[x]).kind == doc.nodes[x].kind, + d2.nodes[parent as int].children == q, + forall|x: int| + 0 <= x < doc.nodes.len() && x != parent ==> (#[trigger] d2.nodes[x]).children == doc.nodes[x].children || d2.nodes[x].children.len() == 0, + ensures + children_rules_hold(d2) <==> (if doc.nodes[parent as int].kind is Document { + document_children_ok(doc, q) + } else { + forall|j: int| 0 <= j < q.len() ==> !(doc.nodes[#[trigger] q[j] as int].kind is DocumentType) + }), +{ + let n = doc.nodes.len(); + let paths = lemma_wf_witness(doc); + let paths2 = lemma_wf_witness(d2); + lemma_wf_split(doc, paths); + lemma_wf_split(d2, paths2); + assert(doc.nodes[0].kind is Document); + assert forall|i: int| 0 < i < n implies !((#[trigger] doc.nodes[i]).kind is Document) by {} + // Every child in either arena is a node of the arena. + assert forall|i: int, j: int| 0 <= i < n && 0 <= j < d2.nodes[i].children.len() implies (#[trigger] d2.nodes[i].children[j]) < n by {} + assert forall|i: int, j: int| 0 <= i < n && 0 <= j < doc.nodes[i].children.len() implies (#[trigger] doc.nodes[i].children[j]) < n by {} + assert forall|j: int| 0 <= j < q.len() implies (#[trigger] q[j]) < n by { + assert(d2.nodes[parent as int].children[j] == q[j]); + } + if children_rules_hold(d2) { + if doc.nodes[parent as int].kind is Document { + assert(parent == 0); + assert(document_children_ok(d2, q)); + lemma_ok_kinds(d2, doc, q); + } else { + assert forall|j: int| 0 <= j < q.len() implies !(doc.nodes[#[trigger] q[j] as int].kind is DocumentType) by { + assert(!(d2.nodes[parent as int].kind is Document)); + assert(d2.nodes[parent as int].children[j] == q[j]); + assert(d2.nodes[q[j] as int].kind == doc.nodes[q[j] as int].kind); + } + } + } + let rule = if doc.nodes[parent as int].kind is Document { + document_children_ok(doc, q) + } else { + forall|j: int| 0 <= j < q.len() ==> !(doc.nodes[#[trigger] q[j] as int].kind is DocumentType) + }; + if rule { + assert forall|p2: int, j: int| + 0 <= p2 < d2.nodes.len() && !(d2.nodes[p2].kind is Document) && 0 <= j < d2.nodes[p2].children.len() implies !(d2.nodes[#[trigger] d2.nodes[p2].children[j] as int].kind is DocumentType) by { + let c = d2.nodes[p2].children[j]; + assert(d2.nodes[c as int].kind == doc.nodes[c as int].kind); + assert(!(doc.nodes[p2].kind is Document)); + if p2 == parent as int { + assert(q[j] == c); + } else { + assert(d2.nodes[p2].children == doc.nodes[p2].children); + assert(doc.nodes[p2].children[j] == c); + } + } + if parent == 0 { + lemma_ok_kinds(doc, d2, q); + } else { + assert(!(doc.nodes[parent as int].kind is Document)); + if d2.nodes[0].children == doc.nodes[0].children { + lemma_ok_kinds(doc, d2, doc.nodes[0].children); + } else { + assert(d2.nodes[0].children.len() == 0); + assert(document_children_ok(d2, d2.nodes[0].children)); + } + } + } +} + +// --------------------------------------------------------------------- +// What the insertion does to the children + +/// What [`lemma_insert_any_shape`] says, for a call with a split index. +pub open spec fn insert_shape(doc: Document, node: NodeId, parent: NodeId, child: Option) -> bool { + let d2 = insert_any(doc, node, parent, child); + let kids = children(doc, parent); + let k = split_index(kids, child); + &&& wf(d2) + &&& d2.nodes.len() == doc.nodes.len() + &&& forall|x: int| 0 <= x < doc.nodes.len() ==> (#[trigger] d2.nodes[x]).kind == doc.nodes[x].kind + &&& 0 <= k <= kids.len() + &&& d2.nodes[parent as int].children == kids.subrange(0, k) + inserted_nodes(doc, node) + kids.subrange(k, kids.len() as int) + &&& forall|x: int| + 0 <= x < doc.nodes.len() && x != parent ==> (#[trigger] d2.nodes[x]).children == doc.nodes[x].children || d2.nodes[x].children.len() == 0 +} + +/// Inserting a fragment changes the children of no node but `parent` and +/// the fragment, which is emptied. +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +proof fn lemma_frag_other_children(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + doc.nodes[node as int].kind is DocumentFragment, + ensures + forall|x: int| + 0 <= x < doc.nodes.len() && x != parent ==> (#[trigger] insert_any(doc, node, parent, child).nodes[x]).children == doc.nodes[x].children + || insert_any(doc, node, parent, child).nodes[x].children.len() == 0, +{ + lemma_insert_fragment(doc, node, parent, child); + let d2 = insert_any(doc, node, parent, child); + assert(d2.nodes == fragment_inserted(doc, node, parent, child)); + assert forall|x: int| 0 <= x < doc.nodes.len() && x != parent implies (#[trigger] d2.nodes[x]).children == doc.nodes[x].children + || d2.nodes[x].children.len() == 0 by { + assert(d2.nodes[x] == fragment_inserted(doc, node, parent, child)[x]); + } +} + +/// [`lemma_insert_any_shape`] for a fragment. +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +proof fn lemma_insert_any_shape_frag(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + doc.nodes[node as int].kind is DocumentFragment, + ensures + insert_shape(doc, node, parent, child), +{ + theorem_insert_any_wf(doc, node, parent, child); + lemma_frag_other_children(doc, node, parent, child); + let kids = children(doc, parent); + let k = split_index(kids, child); + if let Some(c) = child { + let paths = lemma_wf_witness(doc); + lemma_wf_split(doc, paths); + assert(kids.contains(c)); + lemma_index_of(kids, c); + } + match child { + None => { + assert(kids.subrange(0, k) =~= kids); + assert(kids.subrange(k, kids.len() as int) =~= Seq::::empty()); + assert(kids + doc.nodes[node as int].children + Seq::::empty() =~= kids + doc.nodes[node as int].children); + }, + Some(c) => {}, + } +} + +/// [`lemma_insert_any_shape`] for a node that is not a fragment. +#[verifier::rlimit(400)] +proof fn lemma_insert_any_shape_node(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + !(doc.nodes[node as int].kind is DocumentFragment), + doc.nodes[node as int].parent is None, + ensures + insert_shape(doc, node, parent, child), +{ + theorem_insert_any_wf(doc, node, parent, child); + let d2 = insert_any(doc, node, parent, child); + let kids = children(doc, parent); + let k = split_index(kids, child); + if let Some(c) = child { + let paths = lemma_wf_witness(doc); + lemma_wf_split(doc, paths); + assert(kids.contains(c)); + lemma_index_of(kids, c); + } + assert(node != parent); + assert(adopt(doc, node) == doc); + assert(d2 == insert(doc, node, parent, child)); + match child { + None => { + assert(kids.push(node) =~= kids + seq![node]); + assert(kids.subrange(0, k) =~= kids); + assert(kids.subrange(k, kids.len() as int) =~= Seq::::empty()); + assert(kids + seq![node] + Seq::::empty() =~= kids + seq![node]); + }, + Some(c) => { + assert(kids.insert(k, node) =~= kids.subrange(0, k) + seq![node] + kids.subrange(k, kids.len() as int)); + }, + } + assert forall|x: int| 0 <= x < doc.nodes.len() && x != parent implies (#[trigger] d2.nodes[x]).children == doc.nodes[x].children + || d2.nodes[x].children.len() == 0 by { + assert(d2.nodes[x].children == doc.nodes[x].children); + } +} + +/// Inserting a node that has no parent (or a fragment) changes only +/// `parent`'s children, to `p + nodes + s`, and, for a fragment, empties +/// the fragment. +pub proof fn lemma_insert_any_shape(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + doc.nodes[node as int].parent is None, + ensures + insert_shape(doc, node, parent, child), +{ + if doc.nodes[node as int].kind is DocumentFragment { + lemma_insert_any_shape_frag(doc, node, parent, child); + } else { + lemma_insert_any_shape_node(doc, node, parent, child); + } +} + +// --------------------------------------------------------------------- +// Pre-insert + +/// A call that passes steps 1 to 4 of "ensure pre-insertion validity": +/// `parent` may have children, `node` is not a host-including inclusive +/// ancestor of it, `child` (if any) is one of its children, and `node` is +/// not a document. (And, a restriction of this theorem and not a step, +/// `child` is not `node`.) +pub open spec fn pre_insert_steps_1_to_4(doc: Document, node: NodeId, parent: NodeId, child: Option) -> bool { + &&& insert_any_requires(doc, node, parent, child) + &&& !is_host_including_inclusive_ancestor(doc, node, parent) +} + +/// A fragment's children are not doctypes, where the rules hold. +proof fn lemma_fragment_children_not_doctype(doc: Document, node: NodeId) + requires + children_rules_hold(doc), + valid(doc, node), + doc.nodes[node as int].kind is DocumentFragment, + ensures + forall|j: int| 0 <= j < doc.nodes[node as int].children.len() ==> !(doc.nodes[#[trigger] doc.nodes[node as int].children[j] as int].kind is DocumentType), +{ +} + +/// What inserting `node` between `p` and `s` needs, for the rules to stay +/// true: a document takes no `Text` node and no [`rules_violation`]; any +/// other parent takes no doctype. +pub open spec fn insert_rules_condition(doc: Document, node: NodeId, parent: NodeId, p: Seq, s: Seq) -> bool { + if doc.nodes[parent as int].kind is Document { + !(doc.nodes[node as int].kind is Text) && !rules_violation(doc, node, p, s) + } else { + !(doc.nodes[node as int].kind is DocumentType) + } +} + +/// [`insert_rules_condition`] for an insertion of `node` into `parent` +/// before `child`. +pub open spec fn pre_insert_condition(doc: Document, node: NodeId, parent: NodeId, child: Option) -> bool { + let kids = children(doc, parent); + let k = split_index(kids, child); + insert_rules_condition(doc, node, parent, kids.subrange(0, k), kids.subrange(k, kids.len() as int)) +} + +/// Inserting a node that has no parent (or a fragment), in an arena where +/// the rules hold, leaves the rules true exactly when +/// [`pre_insert_condition`] holds. (This says nothing of the checks of +/// "ensure pre-insertion validity".) +#[verifier::rlimit(400)] +pub proof fn lemma_insert_rules_iff(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + insert_any_requires(doc, node, parent, child), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + ensures + children_rules_hold(insert_any(doc, node, parent, child)) <==> pre_insert_condition(doc, node, parent, child), +{ + lemma_insert_any_shape(doc, node, parent, child); + let d2 = insert_any(doc, node, parent, child); + let kids = children(doc, parent); + let k = split_index(kids, child); + let p = kids.subrange(0, k); + let s = kids.subrange(k, kids.len() as int); + let ins = inserted_nodes(doc, node); + let q = p + ins + s; + lemma_rules_shape(doc, d2, parent, q); + assert(p + s =~= kids); + if doc.nodes[node as int].kind is DocumentFragment { + lemma_fragment_children_not_doctype(doc, node); + } + if doc.nodes[parent as int].kind is Document { + let paths = lemma_wf_witness(doc); + lemma_wf_split(doc, paths); + assert(parent == 0); + assert(document_children_ok(doc, p + s)); + lemma_ok_insert_node(doc, node, p, s); + } else { + assert(forall|j: int| 0 <= j < kids.len() ==> !(doc.nodes[#[trigger] kids[j] as int].kind is DocumentType)); + if doc.nodes[node as int].kind is DocumentType { + assert(ins == seq![node]); + assert(q[p.len() as int] == node); + } else { + assert forall|j: int| 0 <= j < q.len() implies !(doc.nodes[#[trigger] q[j] as int].kind is DocumentType) by { + if j < p.len() { + assert(q[j] == p[j]); + assert(p[j] == kids[j]); + } else if j < p.len() + ins.len() { + assert(q[j] == ins[j - p.len()]); + } else { + assert(q[j] == s[j - p.len() - ins.len()]); + assert(s[j - p.len() - ins.len()] == kids[k + j - p.len() - ins.len()]); + } + } + } + } +} + +/// For a call that passes steps 1 to 4, with a node that has no parent, +/// in an arena where the rules hold: it passes steps 5 and 6 exactly when +/// inserting leaves the rules true. +pub proof fn lemma_pre_insert_exact(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + pre_insert_requires(doc, node, parent, child), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + pre_insert_steps_1_to_4(doc, node, parent, child), + ensures + pre_insert_error(doc, node, parent, child) is None <==> children_rules_hold(insert_any(doc, node, parent, child)), +{ + lemma_insert_rules_iff(doc, node, parent, child); + lemma_pre_insert_violation(doc, node, parent, child); + assert(pre_insert_error(doc, node, parent, child) is None <==> pre_insert_condition(doc, node, parent, child)); +} + +/// DOM §4.2.3† "ensure pre-insertion validity", steps 5 and 6: "pre-insert" +/// a node that has no parent (a fragment never has one), in an arena where +/// [`children_rules_hold`], leaves the rules true. And the checks are +/// exact: for a call that passes steps 1 to 4, the call passes them (no +/// error) exactly when inserting without them would leave the rules true. +/// So a `Text` node is refused as a document's child, a doctype as any +/// other node's child, and the document keeps at most one element and one +/// doctype, the doctype first. +pub proof fn theorem_pre_insert_children_rules(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + pre_insert_requires(doc, node, parent, child), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + ensures + children_rules_hold(pre_insert(doc, node, parent, child)), + pre_insert_steps_1_to_4(doc, node, parent, child) ==> (pre_insert_error(doc, node, parent, child) is None <==> children_rules_hold( + insert_any(doc, node, parent, child), + )), +{ + if pre_insert_steps_1_to_4(doc, node, parent, child) { + lemma_pre_insert_exact(doc, node, parent, child); + } + if pre_insert_error(doc, node, parent, child) is None { + lemma_pre_insert_valid(doc, node, parent, child); + // `child` is not `node`: step 3 passed, and `node` has no parent. + if child == Some(node) { + assert(doc.nodes[node as int].parent == Some(parent)); + } + assert(pre_insert_reference(doc, node, child) == child); + lemma_not_host_not_incl(doc, node, parent); + assert(pre_insert_steps_1_to_4(doc, node, parent, child)); + lemma_pre_insert_exact(doc, node, parent, child); + } +} + +// --------------------------------------------------------------------- +// Replace + +/// A call that passes steps 1 to 4 of "replace": `parent` may have +/// children, `node` is not a host-including inclusive ancestor of it, +/// `child`'s parent is `parent`, and `node` is not a document. +pub open spec fn replace_steps_1_to_4(doc: Document, child: NodeId, node: NodeId, parent: NodeId) -> bool { + &&& may_have_children(doc.nodes[parent as int].kind) + &&& !is_host_including_inclusive_ancestor(doc, node, parent) + &&& doc.nodes[child as int].parent == Some(parent) + &&& !(doc.nodes[node as int].kind is Document) +} + +/// "Replace" without its checks: steps 7 to 13, as [`replace`] runs them +/// on a call without an error. +pub open spec fn replace_unchecked(doc: Document, child: NodeId, node: NodeId, parent: NodeId) -> Document { + insert_any(remove(doc, child), node, parent, replace_reference(doc, child, node)) +} + +/// A call that passes steps 1 to 4 of "replace" meets what [`insert_any`] +/// asks, after `child` is removed, with the reference child of steps 7 and +/// 8 ([`lemma_replace_valid`], which assumes steps 5 and 6 pass too). +pub proof fn lemma_replace_valid_steps_1_to_4(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + replace_requires(doc, child, node, parent), + replace_steps_1_to_4(doc, child, node, parent), + ensures + remove_requires(doc, child), + insert_any_requires(remove(doc, child), node, parent, replace_reference(doc, child, node)), +{ + let d1 = remove(doc, child); + theorem_remove_wf(doc, child); + theorem_remove_keeps_record(doc, child); + lemma_not_host_not_incl(doc, node, parent); + lemma_remove_ancestor_mono(doc, child, node, parent); + lemma_replace_reference(doc, child, node, parent); + if let Some(r) = replace_reference(doc, child, node) { + assert(d1.nodes[r as int].parent == doc.nodes[r as int].parent); + } +} + +/// What [`lemma_replace_shape`] says. +pub open spec fn replace_shape(doc: Document, child: NodeId, node: NodeId, parent: NodeId) -> bool { + let d2 = replace_unchecked(doc, child, node, parent); + let kids = children(doc, parent); + let i = index_of(kids, child); + &&& wf(d2) + &&& d2.nodes.len() == doc.nodes.len() + &&& forall|x: int| 0 <= x < doc.nodes.len() ==> (#[trigger] d2.nodes[x]).kind == doc.nodes[x].kind + &&& 0 <= i < kids.len() + &&& kids.subrange(0, i) + seq![child] + kids.subrange(i + 1, kids.len() as int) == kids + &&& d2.nodes[parent as int].children == kids.subrange(0, i) + inserted_nodes(doc, node) + kids.subrange(i + 1, kids.len() as int) + &&& forall|x: int| + 0 <= x < doc.nodes.len() && x != parent ==> (#[trigger] d2.nodes[x]).children == doc.nodes[x].children || d2.nodes[x].children.len() == 0 +} + +/// "Replace" of `child` by a node that has no parent changes only +/// `parent`'s children, to `child`'s place taken by the node's, and, for a +/// fragment, empties the fragment. +#[verifier::rlimit(400)] +pub proof fn lemma_replace_shape(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + replace_requires(doc, child, node, parent), + replace_steps_1_to_4(doc, child, node, parent), + doc.nodes[node as int].parent is None, + ensures + replace_shape(doc, child, node, parent), +{ + lemma_replace_valid_steps_1_to_4(doc, child, node, parent); + let d1 = remove(doc, child); + theorem_remove_wf(doc, child); + theorem_remove_keeps_record(doc, child); + let paths = lemma_wf_witness(doc); + lemma_wf_split(doc, paths); + let r = replace_reference(doc, child, node); + let kids = children(doc, parent); + assert(kids.contains(child)); + lemma_index_of(kids, child); + let i = index_of(kids, child); + let pre = kids.subrange(0, i); + let suf = kids.subrange(i + 1, kids.len() as int); + assert(pre + seq![child] + suf =~= kids); + assert(node != child); + assert(d1.nodes[node as int].parent is None); + // The children of `parent` after the removal. + let kids1 = children(d1, parent); + assert(d1.nodes == doc.nodes.update(parent as int, Node { children: kids.remove(i), ..doc.nodes[parent as int] }).update( + child as int, + Node { parent: None, ..doc.nodes[child as int] }, + )); + assert(child != parent); + assert(kids1 == kids.remove(i)); + assert(kids.remove(i) =~= pre + suf); + lemma_insert_any_shape(d1, node, parent, r); + let d2 = replace_unchecked(doc, child, node, parent); + assert(d2 == insert_any(d1, node, parent, r)); + assert(node != parent); + assert(inserted_nodes(d1, node) == inserted_nodes(doc, node)); + lemma_next_sibling(doc, child); + // `node` is not `child`'s next sibling (that has a parent, `node` has none). + assert(r == next_sibling(doc, child)); + let paths1 = lemma_wf_witness(d1); + lemma_wf_split(d1, paths1); + assert(kids1.no_duplicates()); + match r { + None => { + assert(i + 1 == kids.len()); + assert(suf =~= Seq::::empty()); + assert(kids1.subrange(0, kids1.len() as int) =~= kids1); + assert(kids1.subrange(kids1.len() as int, kids1.len() as int) =~= Seq::::empty()); + assert(pre =~= kids1); + }, + Some(x) => { + assert(i + 1 < kids.len()); + assert(x == kids[i + 1]); + assert(kids1[i] == x); + lemma_index_of_unique(kids1, i); + assert(kids1.subrange(0, i) =~= pre); + assert(kids1.subrange(i, kids1.len() as int) =~= suf); + }, + } + assert forall|x: int| 0 <= x < doc.nodes.len() && x != parent implies (#[trigger] d2.nodes[x]).children == doc.nodes[x].children + || d2.nodes[x].children.len() == 0 by { + assert(d1.nodes[x].children == doc.nodes[x].children); + } +} + +/// [`insert_rules_condition`] for the replacement of `child` by `node`. +pub open spec fn replace_condition(doc: Document, child: NodeId, node: NodeId, parent: NodeId) -> bool { + let kids = children(doc, parent); + let i = index_of(kids, child); + insert_rules_condition(doc, node, parent, kids.subrange(0, i), kids.subrange(i + 1, kids.len() as int)) +} + +/// Replacing `child` by a node that has no parent (or a fragment), in an +/// arena where the rules hold, leaves the rules true exactly when +/// [`replace_condition`] holds. (This says nothing of the checks of +/// "replace".) +#[verifier::rlimit(400)] +pub proof fn lemma_replace_rules_iff(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + replace_requires(doc, child, node, parent), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + replace_steps_1_to_4(doc, child, node, parent), + ensures + children_rules_hold(replace_unchecked(doc, child, node, parent)) <==> replace_condition(doc, child, node, parent), +{ + lemma_replace_shape(doc, child, node, parent); + let d2 = replace_unchecked(doc, child, node, parent); + let kids = children(doc, parent); + let i = index_of(kids, child); + let p = kids.subrange(0, i); + let s = kids.subrange(i + 1, kids.len() as int); + let ins = inserted_nodes(doc, node); + let q = p + ins + s; + lemma_rules_shape(doc, d2, parent, q); + if doc.nodes[node as int].kind is DocumentFragment { + lemma_fragment_children_not_doctype(doc, node); + } + if doc.nodes[parent as int].kind is Document { + let paths = lemma_wf_witness(doc); + lemma_wf_split(doc, paths); + assert(parent == 0); + assert(document_children_ok(doc, p + seq![child] + s)); + lemma_ok_drop_mid(doc, p, seq![child], s); + lemma_ok_insert_node(doc, node, p, s); + } else { + assert(forall|j: int| 0 <= j < kids.len() ==> !(doc.nodes[#[trigger] kids[j] as int].kind is DocumentType)); + if doc.nodes[node as int].kind is DocumentType { + assert(ins == seq![node]); + assert(q[p.len() as int] == node); + } else { + assert forall|j: int| 0 <= j < q.len() implies !(doc.nodes[#[trigger] q[j] as int].kind is DocumentType) by { + if j < p.len() { + assert(q[j] == p[j]); + assert(p[j] == kids[j]); + } else if j < p.len() + ins.len() { + assert(q[j] == ins[j - p.len()]); + } else { + assert(q[j] == s[j - p.len() - ins.len()]); + assert(s[j - p.len() - ins.len()] == kids[i + 1 + j - p.len() - ins.len()]); + } + } + } + } +} + +/// For a call that passes steps 1 to 4, with a node that has no parent, +/// in an arena where the rules hold: it passes steps 5 and 6 of "replace" +/// exactly when replacing leaves the rules true. +pub proof fn lemma_replace_exact(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + replace_requires(doc, child, node, parent), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + replace_steps_1_to_4(doc, child, node, parent), + ensures + replace_error(doc, child, node, parent) is None <==> children_rules_hold(replace_unchecked(doc, child, node, parent)), +{ + lemma_replace_rules_iff(doc, child, node, parent); + lemma_replace_violation(doc, child, node, parent); + assert(replace_error(doc, child, node, parent) is None <==> replace_condition(doc, child, node, parent)); +} + +/// DOM §4.2.3† "replace", steps 5 and 6: replacing a child with a node +/// that has no parent (a fragment never has one), in an arena where +/// [`children_rules_hold`], leaves the rules true. And the checks are +/// exact: for a call that passes steps 1 to 4, the call passes them +/// exactly when replacing without them would leave the rules true. The +/// replaced child itself does not count against the new node, so a +/// document's element may be replaced by another element, and its doctype +/// by another doctype. +pub proof fn theorem_replace_children_rules(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + replace_requires(doc, child, node, parent), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + ensures + children_rules_hold(replace(doc, child, node, parent)), + replace_steps_1_to_4(doc, child, node, parent) ==> (replace_error(doc, child, node, parent) is None <==> children_rules_hold( + replace_unchecked(doc, child, node, parent), + )), +{ + if replace_steps_1_to_4(doc, child, node, parent) { + lemma_replace_exact(doc, child, node, parent); + } + if replace_error(doc, child, node, parent) is None { + lemma_not_host_not_incl(doc, node, parent); + assert(replace_steps_1_to_4(doc, child, node, parent)); + lemma_replace_exact(doc, child, node, parent); + assert(replace(doc, child, node, parent) == replace_unchecked(doc, child, node, parent)); + } +} + +// --------------------------------------------------------------------- +// For the kill certificates of `mutations.json`: building arenas where the +// rules hold, and what the theorems say of a call, in terms of the +// condition that does not read the checks. + +/// A new document obeys the rules. +pub proof fn lemma_new_document_rules(url: Seq) + ensures + children_rules_hold(new_document(url)), +{ + let d = new_document(url); + assert(d.nodes.len() == 1); + assert(d.nodes[0].kind is Document); + assert(d.nodes[0].children =~= Seq::::empty()); + assert(document_children_ok(d, d.nodes[0].children)); +} + +/// In a well-formed arena every child is a node of it. +pub proof fn lemma_children_valid(doc: Document) + requires + wf(doc), + ensures + forall|i: int, j: int| 0 <= i < doc.nodes.len() && 0 <= j < doc.nodes[i].children.len() ==> (#[trigger] doc.nodes[i].children[j]) < doc.nodes.len(), +{ + let paths = lemma_wf_witness(doc); + lemma_wf_split(doc, paths); +} + +/// Creating a node keeps the rules true: it has no children and no +/// existing node changes. +#[verifier::rlimit(400)] +pub proof fn lemma_create_keeps_rules(doc: Document, k: NodeKind) + requires + wf(doc), + children_rules_hold(doc), + ensures + children_rules_hold(create_node(doc, k).0), +{ + let d2 = create_node(doc, k).0; + let n = doc.nodes.len(); + lemma_children_valid(doc); + assert(d2.nodes.len() == n + 1); + assert(1 <= n); + assert forall|p: int, j: int| + 0 <= p < d2.nodes.len() && !(d2.nodes[p].kind is Document) && 0 <= j < d2.nodes[p].children.len() implies !(d2.nodes[#[trigger] d2.nodes[p].children[j] as int].kind is DocumentType) by { + assert(p < n); + assert(d2.nodes[p] == doc.nodes[p]); + let c = doc.nodes[p].children[j]; + assert(c < n); + assert(d2.nodes[c as int] == doc.nodes[c as int]); + } + assert(d2.nodes[0] == doc.nodes[0]); + assert forall|x: int| 0 <= x < n implies (#[trigger] d2.nodes[x]).kind == doc.nodes[x].kind by {} + lemma_ok_kinds(doc, d2, doc.nodes[0].children); +} + +/// Appending to a parent a node that has no parent keeps the rules true +/// when [`pre_insert_condition`] holds. +pub proof fn lemma_append_keeps_rules(doc: Document, parent: NodeId, node: NodeId) + requires + insert_requires(doc, node, parent, None), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + pre_insert_condition(doc, node, parent, None), + ensures + children_rules_hold(append(doc, parent, node)), +{ + assert(insert_any_requires(doc, node, parent, None)); + assert(append(doc, parent, node) == insert_any(doc, node, parent, None)); + lemma_insert_rules_iff(doc, node, parent, None); +} + +/// A call that [`pre_insert_condition`] rejects gets an error from "ensure +/// pre-insertion validity" (`theorem_pre_insert_children_rules`). +pub proof fn lemma_pre_insert_refused(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + pre_insert_requires(doc, node, parent, child), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + !pre_insert_condition(doc, node, parent, child), + ensures + pre_insert_error(doc, node, parent, child) is Some, +{ + theorem_pre_insert_children_rules(doc, node, parent, child); + if pre_insert_error(doc, node, parent, child) is None { + lemma_pre_insert_valid(doc, node, parent, child); + if child == Some(node) { + assert(doc.nodes[node as int].parent == Some(parent)); + } + assert(pre_insert_reference(doc, node, child) == child); + lemma_not_host_not_incl(doc, node, parent); + assert(pre_insert_steps_1_to_4(doc, node, parent, child)); + lemma_insert_rules_iff(doc, node, parent, child); + } +} + +/// A call that passes steps 1 to 4 and that [`pre_insert_condition`] +/// accepts gets no error from "ensure pre-insertion validity" +/// (`theorem_pre_insert_children_rules`). +pub proof fn lemma_pre_insert_accepted(doc: Document, node: NodeId, parent: NodeId, child: Option) + requires + pre_insert_requires(doc, node, parent, child), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + pre_insert_steps_1_to_4(doc, node, parent, child), + pre_insert_condition(doc, node, parent, child), + ensures + pre_insert_error(doc, node, parent, child) is None, +{ + theorem_pre_insert_children_rules(doc, node, parent, child); + lemma_insert_rules_iff(doc, node, parent, child); +} + +/// A call that [`replace_condition`] rejects gets an error from "replace" +/// (`theorem_replace_children_rules`). +pub proof fn lemma_replace_refused(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + replace_requires(doc, child, node, parent), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + !replace_condition(doc, child, node, parent), + ensures + replace_error(doc, child, node, parent) is Some, +{ + theorem_replace_children_rules(doc, child, node, parent); + if replace_error(doc, child, node, parent) is None { + lemma_not_host_not_incl(doc, node, parent); + assert(replace_steps_1_to_4(doc, child, node, parent)); + lemma_replace_rules_iff(doc, child, node, parent); + } +} + +/// A call that passes steps 1 to 4 and that [`replace_condition`] accepts +/// gets no error from "replace" (`theorem_replace_children_rules`). +pub proof fn lemma_replace_accepted(doc: Document, child: NodeId, node: NodeId, parent: NodeId) + requires + replace_requires(doc, child, node, parent), + children_rules_hold(doc), + doc.nodes[node as int].parent is None, + replace_steps_1_to_4(doc, child, node, parent), + replace_condition(doc, child, node, parent), + ensures + replace_error(doc, child, node, parent) is None, +{ + theorem_replace_children_rules(doc, child, node, parent); + lemma_replace_rules_iff(doc, child, node, parent); +} + +} // verus! diff --git a/crates/clazel-dom/src/validity_mutation_lemmas.rs b/crates/clazel-dom/src/validity_mutation_lemmas.rs new file mode 100644 index 000000000..04db053c1 --- /dev/null +++ b/crates/clazel-dom/src/validity_mutation_lemmas.rs @@ -0,0 +1,763 @@ +//! Concrete instances of the theorems of [`super::validity_lemmas`], the +//! kill certificates of the mutations of "ensure pre-insertion validity" and +//! "replace" (steps 5 and 6) in `mutations.json` (`xtask/src/mutate.rs`). +//! Each lemma says what a theorem implies about a call on a small concrete +//! arena; a certificate evaluates the mutated check on that call +//! (`compute_only`) and shows it contradicts the lemma. +//! +//! The arena [`vm_pre`]: a document (0) with nothing in it, and loose nodes: +//! a text (1), elements (2, 3), doctypes (4, 5), comments (6, 7), a fragment +//! (8) of two elements (9, 10), a fragment (11) of a text (12), a fragment +//! (13) of an element (14) and an element (15). The scenarios `vm_s*` put +//! some of them in the document, or comment 7 in element 15. +//! +//! The lemmas prove each call's outcome from the theorems and from +//! [`pre_insert_condition`] and [`replace_condition`], which do not read the +//! checks. The `compute_only` assertions evaluate only what no mutation +//! changes: the arenas, and those conditions. + +#[cfg(verus_only)] +use super::contract::*; +#[cfg(verus_only)] +use super::lemmas::*; +#[cfg(verus_only)] +use super::mutation_lemmas::*; +#[cfg(verus_only)] +use super::ops_lemmas::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::validity_lemmas::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A doctype with empty strings. +pub open spec fn vm_doctype() -> NodeKind { + NodeKind::DocumentType { name: seq![], public_id: seq![], system_id: seq![] } +} + +/// A comment with no data. +pub open spec fn vm_comment() -> NodeKind { + NodeKind::Comment { data: seq![] } +} + +/// What a call of "append" needs, apart from the arena being well formed: +/// [`insert_requires`], the node has no parent, and the rules stay true. +pub open spec fn vm_append_ok(d: Document, parent: NodeId, node: NodeId) -> bool { + &&& valid(d, node) + &&& valid(d, parent) + &&& !(d.nodes[node as int].kind is Document) + &&& !(d.nodes[node as int].kind is DocumentFragment) + &&& may_have_children(d.nodes[parent as int].kind) + &&& !is_inclusive_ancestor(d, node, parent) + &&& d.nodes[node as int].parent is None + &&& pre_insert_condition(d, node, parent, None) +} + +/// What a call of "ensure pre-insertion validity" needs to pass steps 1 to +/// 4, apart from the arena being well formed. +pub open spec fn vm_steps(d: Document, node: NodeId, parent: NodeId, child: Option) -> bool { + &&& valid(d, node) + &&& valid(d, parent) + &&& !(d.nodes[node as int].kind is Document) + &&& may_have_children(d.nodes[parent as int].kind) + &&& !is_inclusive_ancestor(d, node, parent) + &&& match child { + None => true, + Some(c) => valid(d, c) && c != node && d.nodes[c as int].parent == Some(parent), + } + &&& !is_host_including_inclusive_ancestor(d, node, parent) +} + +/// Creating a node in an arena where the rules hold keeps both. +proof fn vm_create(d: Document, k: NodeKind) + requires + wf(d), + children_rules_hold(d), + creatable(d, k), + d.nodes.len() + 1 <= max_nodes(), + ensures + wf(create_node(d, k).0), + children_rules_hold(create_node(d, k).0), + create_node(d, k).0.nodes.len() == d.nodes.len() + 1, +{ + theorem_create_node_wf(d, k); + lemma_create_keeps_rules(d, k); +} + +/// Appending in an arena where the rules hold keeps both. +proof fn vm_append(d: Document, parent: NodeId, node: NodeId) + requires + wf(d), + children_rules_hold(d), + vm_append_ok(d, parent, node), + ensures + wf(append(d, parent, node)), + children_rules_hold(append(d, parent, node)), + append(d, parent, node).nodes.len() == d.nodes.len(), +{ + assert(insert_requires(d, node, parent, None)); + theorem_append_wf(d, parent, node); + lemma_append_keeps_rules(d, parent, node); +} + +/// The document, then each loose node created in turn. +pub open spec fn vm_d0() -> Document { + new_document(seq![]) +} + +/// Node 1 created. +pub open spec fn vm_d1() -> Document { + create_node(vm_d0(), ml_text()).0 +} + +/// Node 2 created. +pub open spec fn vm_d2() -> Document { + create_node(vm_d1(), ml_element()).0 +} + +/// Node 3 created. +pub open spec fn vm_d3() -> Document { + create_node(vm_d2(), ml_element()).0 +} + +/// Node 4 created. +pub open spec fn vm_d4() -> Document { + create_node(vm_d3(), vm_doctype()).0 +} + +/// Node 5 created. +pub open spec fn vm_d5() -> Document { + create_node(vm_d4(), vm_doctype()).0 +} + +/// Node 6 created. +pub open spec fn vm_d6() -> Document { + create_node(vm_d5(), vm_comment()).0 +} + +/// Node 7 created. +pub open spec fn vm_d7() -> Document { + create_node(vm_d6(), vm_comment()).0 +} + +/// Node 8 created. +pub open spec fn vm_d8() -> Document { + create_node(vm_d7(), NodeKind::DocumentFragment { host: None }).0 +} + +/// Node 9 created. +pub open spec fn vm_d9() -> Document { + create_node(vm_d8(), ml_element()).0 +} + +/// Node 10 created. +pub open spec fn vm_d10() -> Document { + create_node(vm_d9(), ml_element()).0 +} + +/// Node 11 created. +pub open spec fn vm_d11() -> Document { + create_node(vm_d10(), NodeKind::DocumentFragment { host: None }).0 +} + +/// Node 12 created. +pub open spec fn vm_d12() -> Document { + create_node(vm_d11(), ml_text()).0 +} + +/// Node 13 created. +pub open spec fn vm_d13() -> Document { + create_node(vm_d12(), NodeKind::DocumentFragment { host: None }).0 +} + +/// Node 14 created. +pub open spec fn vm_d14() -> Document { + create_node(vm_d13(), ml_element()).0 +} + +/// Node 15 created. +pub open spec fn vm_d15() -> Document { + create_node(vm_d14(), ml_element()).0 +} + +/// Node 9 appended to the fragment or element 8. +pub open spec fn vm_a1() -> Document { + append(vm_d15(), 8, 9) +} + +/// Node 10 appended to the fragment or element 8. +pub open spec fn vm_a2() -> Document { + append(vm_a1(), 8, 10) +} + +/// Node 12 appended to the fragment or element 11. +pub open spec fn vm_a3() -> Document { + append(vm_a2(), 11, 12) +} + +/// Node 14 appended to the fragment or element 13. +pub open spec fn vm_a4() -> Document { + append(vm_a3(), 13, 14) +} + +/// The base arena, [`vm_pre`]: the loose nodes, the fragments filled. +pub open spec fn vm_pre() -> Document { + vm_a4() +} + +/// The base arena is well formed, obeys the rules, and has 16 nodes. +#[verifier::rlimit(400)] +pub proof fn vm_pre_ok() + ensures + wf(vm_pre()), + children_rules_hold(vm_pre()), + vm_pre().nodes.len() == 16, +{ + theorem_new_document_wf(seq![]); + lemma_new_document_rules(seq![]); + assert(creatable(vm_d0(), ml_text())) by (compute_only); + vm_create(vm_d0(), ml_text()); + assert(creatable(vm_d1(), ml_element())) by (compute_only); + vm_create(vm_d1(), ml_element()); + assert(creatable(vm_d2(), ml_element())) by (compute_only); + vm_create(vm_d2(), ml_element()); + assert(creatable(vm_d3(), vm_doctype())) by (compute_only); + vm_create(vm_d3(), vm_doctype()); + assert(creatable(vm_d4(), vm_doctype())) by (compute_only); + vm_create(vm_d4(), vm_doctype()); + assert(creatable(vm_d5(), vm_comment())) by (compute_only); + vm_create(vm_d5(), vm_comment()); + assert(creatable(vm_d6(), vm_comment())) by (compute_only); + vm_create(vm_d6(), vm_comment()); + assert(creatable(vm_d7(), NodeKind::DocumentFragment { host: None })) by (compute_only); + vm_create(vm_d7(), NodeKind::DocumentFragment { host: None }); + assert(creatable(vm_d8(), ml_element())) by (compute_only); + vm_create(vm_d8(), ml_element()); + assert(creatable(vm_d9(), ml_element())) by (compute_only); + vm_create(vm_d9(), ml_element()); + assert(creatable(vm_d10(), NodeKind::DocumentFragment { host: None })) by (compute_only); + vm_create(vm_d10(), NodeKind::DocumentFragment { host: None }); + assert(creatable(vm_d11(), ml_text())) by (compute_only); + vm_create(vm_d11(), ml_text()); + assert(creatable(vm_d12(), NodeKind::DocumentFragment { host: None })) by (compute_only); + vm_create(vm_d12(), NodeKind::DocumentFragment { host: None }); + assert(creatable(vm_d13(), ml_element())) by (compute_only); + vm_create(vm_d13(), ml_element()); + assert(creatable(vm_d14(), ml_element())) by (compute_only); + vm_create(vm_d14(), ml_element()); + assert(vm_append_ok(vm_d15(), 8, 9)) by (compute_only); + vm_append(vm_d15(), 8, 9); + assert(vm_append_ok(vm_a1(), 8, 10)) by (compute_only); + vm_append(vm_a1(), 8, 10); + assert(vm_append_ok(vm_a2(), 11, 12)) by (compute_only); + vm_append(vm_a2(), 11, 12); + assert(vm_append_ok(vm_a3(), 13, 14)) by (compute_only); + vm_append(vm_a3(), 13, 14); +} + +/// The scenario `s1`: node 2 appended to node 0 of [`vm_pre`]. +pub open spec fn vm_s1() -> Document { + append(vm_pre(), 0, 2) +} + +/// The scenario `s1` is well formed, obeys the rules, and has 16 nodes. +pub proof fn vm_s1_ok() + ensures + wf(vm_s1()), + children_rules_hold(vm_s1()), + vm_s1().nodes.len() == 16, +{ + vm_pre_ok(); + assert(vm_append_ok(vm_pre(), 0, 2)) by (compute_only); + vm_append(vm_pre(), 0, 2); +} + +/// The scenario `s2`: node 4 appended to node 0 of [`vm_pre`]. +pub open spec fn vm_s2() -> Document { + append(vm_pre(), 0, 4) +} + +/// The scenario `s2` is well formed, obeys the rules, and has 16 nodes. +pub proof fn vm_s2_ok() + ensures + wf(vm_s2()), + children_rules_hold(vm_s2()), + vm_s2().nodes.len() == 16, +{ + vm_pre_ok(); + assert(vm_append_ok(vm_pre(), 0, 4)) by (compute_only); + vm_append(vm_pre(), 0, 4); +} + +/// The scenario `s6`: node 6 appended to node 0 of [`vm_pre`]. +pub open spec fn vm_s6() -> Document { + append(vm_pre(), 0, 6) +} + +/// The scenario `s6` is well formed, obeys the rules, and has 16 nodes. +pub proof fn vm_s6_ok() + ensures + wf(vm_s6()), + children_rules_hold(vm_s6()), + vm_s6().nodes.len() == 16, +{ + vm_pre_ok(); + assert(vm_append_ok(vm_pre(), 0, 6)) by (compute_only); + vm_append(vm_pre(), 0, 6); +} + +/// The scenario `s9`: node 7 appended to node 15 of [`vm_pre`]. +pub open spec fn vm_s9() -> Document { + append(vm_pre(), 15, 7) +} + +/// The scenario `s9` is well formed, obeys the rules, and has 16 nodes. +pub proof fn vm_s9_ok() + ensures + wf(vm_s9()), + children_rules_hold(vm_s9()), + vm_s9().nodes.len() == 16, +{ + vm_pre_ok(); + assert(vm_append_ok(vm_pre(), 15, 7)) by (compute_only); + vm_append(vm_pre(), 15, 7); +} + +/// The scenario `s3`: node 4 appended to node 0 of [`vm_s6`]. +pub open spec fn vm_s3() -> Document { + append(vm_s6(), 0, 4) +} + +/// The scenario `s3` is well formed, obeys the rules, and has 16 nodes. +pub proof fn vm_s3_ok() + ensures + wf(vm_s3()), + children_rules_hold(vm_s3()), + vm_s3().nodes.len() == 16, +{ + vm_s6_ok(); + assert(vm_append_ok(vm_s6(), 0, 4)) by (compute_only); + vm_append(vm_s6(), 0, 4); +} + +/// The scenario `s4`: node 6 appended to node 0 of [`vm_s1`]. +pub open spec fn vm_s4() -> Document { + append(vm_s1(), 0, 6) +} + +/// The scenario `s4` is well formed, obeys the rules, and has 16 nodes. +pub proof fn vm_s4_ok() + ensures + wf(vm_s4()), + children_rules_hold(vm_s4()), + vm_s4().nodes.len() == 16, +{ + vm_s1_ok(); + assert(vm_append_ok(vm_s1(), 0, 6)) by (compute_only); + vm_append(vm_s1(), 0, 6); +} + +/// The scenario `s8`: node 6 appended to node 0 of [`vm_s2`]. +pub open spec fn vm_s8() -> Document { + append(vm_s2(), 0, 6) +} + +/// The scenario `s8` is well formed, obeys the rules, and has 16 nodes. +pub proof fn vm_s8_ok() + ensures + wf(vm_s8()), + children_rules_hold(vm_s8()), + vm_s8().nodes.len() == 16, +{ + vm_s2_ok(); + assert(vm_append_ok(vm_s2(), 0, 6)) by (compute_only); + vm_append(vm_s2(), 0, 6); +} + +/// Step 5: a `Text` node is refused as a document's child. +pub proof fn vm_pi_text_in_document() + ensures + pre_insert_error(vm_pre(), 1, 0, None) is Some, +{ + vm_pre_ok(); + let d = vm_pre(); + assert(vm_pre().nodes[1].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_pre(), 1, 0, None)) by (compute_only); + lemma_pre_insert_refused(d, 1, 0, None); +} + +/// Step 5: a doctype is refused as a non-document's child. +pub proof fn vm_pi_doctype_in_element() + ensures + pre_insert_error(vm_pre(), 5, 15, None) is Some, +{ + vm_pre_ok(); + let d = vm_pre(); + assert(vm_pre().nodes[5].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_pre(), 5, 15, None)) by (compute_only); + lemma_pre_insert_refused(d, 5, 15, None); +} + +/// Step 6, fragment: more than one element child is refused. +pub proof fn vm_pi_fragment_two_elements() + ensures + pre_insert_error(vm_pre(), 8, 0, None) is Some, +{ + vm_pre_ok(); + let d = vm_pre(); + assert(vm_pre().nodes[8].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_pre(), 8, 0, None)) by (compute_only); + lemma_pre_insert_refused(d, 8, 0, None); +} + +/// Step 6, fragment: a `Text` child is refused. +pub proof fn vm_pi_fragment_text() + ensures + pre_insert_error(vm_pre(), 11, 0, None) is Some, +{ + vm_pre_ok(); + let d = vm_pre(); + assert(vm_pre().nodes[11].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_pre(), 11, 0, None)) by (compute_only); + lemma_pre_insert_refused(d, 11, 0, None); +} + +/// Step 6, fragment of one element: refused if the parent has an element child. +pub proof fn vm_pi_fragment_element_parent_element() + ensures + pre_insert_error(vm_s1(), 13, 0, None) is Some, +{ + vm_s1_ok(); + let d = vm_s1(); + assert(vm_s1().nodes[13].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_s1(), 13, 0, None)) by (compute_only); + lemma_pre_insert_refused(d, 13, 0, None); +} + +/// Step 6, fragment of one element: refused if the child is a doctype. +pub proof fn vm_pi_fragment_element_before_doctype() + ensures + pre_insert_error(vm_s2(), 13, 0, Some(4usize)) is Some, +{ + vm_s2_ok(); + let d = vm_s2(); + assert(vm_s2().nodes[13].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_s2(), 13, 0, Some(4usize))) by (compute_only); + lemma_pre_insert_refused(d, 13, 0, Some(4usize)); +} + +/// Step 6, fragment of one element: refused if a doctype is following the child. +pub proof fn vm_pi_fragment_element_doctype_after() + ensures + pre_insert_error(vm_s3(), 13, 0, Some(6usize)) is Some, +{ + vm_s3_ok(); + let d = vm_s3(); + assert(vm_s3().nodes[13].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_s3(), 13, 0, Some(6usize))) by (compute_only); + lemma_pre_insert_refused(d, 13, 0, Some(6usize)); +} + +/// Step 6, element: refused if the parent has an element child. +pub proof fn vm_pi_element_parent_element() + ensures + pre_insert_error(vm_s1(), 3, 0, None) is Some, +{ + vm_s1_ok(); + let d = vm_s1(); + assert(vm_s1().nodes[3].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_s1(), 3, 0, None)) by (compute_only); + lemma_pre_insert_refused(d, 3, 0, None); +} + +/// Step 6, element: refused if the child is a doctype. +pub proof fn vm_pi_element_before_doctype() + ensures + pre_insert_error(vm_s2(), 3, 0, Some(4usize)) is Some, +{ + vm_s2_ok(); + let d = vm_s2(); + assert(vm_s2().nodes[3].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_s2(), 3, 0, Some(4usize))) by (compute_only); + lemma_pre_insert_refused(d, 3, 0, Some(4usize)); +} + +/// Step 6, element: refused if a doctype is following the child. +pub proof fn vm_pi_element_doctype_after() + ensures + pre_insert_error(vm_s3(), 3, 0, Some(6usize)) is Some, +{ + vm_s3_ok(); + let d = vm_s3(); + assert(vm_s3().nodes[3].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_s3(), 3, 0, Some(6usize))) by (compute_only); + lemma_pre_insert_refused(d, 3, 0, Some(6usize)); +} + +/// Step 6, doctype: refused if the parent has a doctype child. +pub proof fn vm_pi_doctype_parent_doctype() + ensures + pre_insert_error(vm_s2(), 5, 0, None) is Some, +{ + vm_s2_ok(); + let d = vm_s2(); + assert(vm_s2().nodes[5].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_s2(), 5, 0, None)) by (compute_only); + lemma_pre_insert_refused(d, 5, 0, None); +} + +/// Step 6, doctype: refused if an element is preceding the child. +pub proof fn vm_pi_doctype_element_preceding() + ensures + pre_insert_error(vm_s4(), 5, 0, Some(6usize)) is Some, +{ + vm_s4_ok(); + let d = vm_s4(); + assert(vm_s4().nodes[5].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_s4(), 5, 0, Some(6usize))) by (compute_only); + lemma_pre_insert_refused(d, 5, 0, Some(6usize)); +} + +/// Step 6, doctype: appended after an element child is refused. +pub proof fn vm_pi_doctype_after_element() + ensures + pre_insert_error(vm_s1(), 5, 0, None) is Some, +{ + vm_s1_ok(); + let d = vm_s1(); + assert(vm_s1().nodes[5].parent is None) by (compute_only); + assert(!pre_insert_condition(vm_s1(), 5, 0, None)) by (compute_only); + lemma_pre_insert_refused(d, 5, 0, None); +} + +/// A fragment of one element is accepted into an empty document. +pub proof fn vm_pi_fragment_one_element() + ensures + pre_insert_error(vm_pre(), 13, 0, None) is None, +{ + vm_pre_ok(); + let d = vm_pre(); + assert(vm_pre().nodes[13].parent is None) by (compute_only); + assert(vm_steps(vm_pre(), 13, 0, None)) by (compute_only); + assert(pre_insert_steps_1_to_4(d, 13, 0, None)); + assert(pre_insert_condition(vm_pre(), 13, 0, None)) by (compute_only); + lemma_pre_insert_accepted(d, 13, 0, None); +} + +/// A `Text` node is accepted as an element's child. +pub proof fn vm_pi_text_in_element() + ensures + pre_insert_error(vm_pre(), 1, 15, None) is None, +{ + vm_pre_ok(); + let d = vm_pre(); + assert(vm_pre().nodes[1].parent is None) by (compute_only); + assert(vm_steps(vm_pre(), 1, 15, None)) by (compute_only); + assert(pre_insert_steps_1_to_4(d, 1, 15, None)); + assert(pre_insert_condition(vm_pre(), 1, 15, None)) by (compute_only); + lemma_pre_insert_accepted(d, 1, 15, None); +} + +/// A doctype is accepted as the child of an empty document. +pub proof fn vm_pi_doctype_in_document() + ensures + pre_insert_error(vm_pre(), 4, 0, None) is None, +{ + vm_pre_ok(); + let d = vm_pre(); + assert(vm_pre().nodes[4].parent is None) by (compute_only); + assert(vm_steps(vm_pre(), 4, 0, None)) by (compute_only); + assert(pre_insert_steps_1_to_4(d, 4, 0, None)); + assert(pre_insert_condition(vm_pre(), 4, 0, None)) by (compute_only); + lemma_pre_insert_accepted(d, 4, 0, None); +} + +/// Step 5 of "replace": a `Text` node is refused as a document's child. +pub proof fn vm_rp_text_in_document() + ensures + replace_error(vm_s1(), 2, 1, 0) is Some, +{ + vm_s1_ok(); + let d = vm_s1(); + assert(vm_s1().nodes[1].parent is None) by (compute_only); + assert(!replace_condition(vm_s1(), 2, 1, 0)) by (compute_only); + lemma_replace_refused(d, 2, 1, 0); +} + +/// Step 5 of "replace": a doctype is refused as a non-document's child. +pub proof fn vm_rp_doctype_in_element() + ensures + replace_error(vm_s9(), 7, 5, 15) is Some, +{ + vm_s9_ok(); + let d = vm_s9(); + assert(vm_s9().nodes[5].parent is None) by (compute_only); + assert(!replace_condition(vm_s9(), 7, 5, 15)) by (compute_only); + lemma_replace_refused(d, 7, 5, 15); +} + +/// Step 6 of "replace", fragment: more than one element child is refused. +pub proof fn vm_rp_fragment_two_elements() + ensures + replace_error(vm_s6(), 6, 8, 0) is Some, +{ + vm_s6_ok(); + let d = vm_s6(); + assert(vm_s6().nodes[8].parent is None) by (compute_only); + assert(!replace_condition(vm_s6(), 6, 8, 0)) by (compute_only); + lemma_replace_refused(d, 6, 8, 0); +} + +/// Step 6 of "replace", fragment: a `Text` child is refused. +pub proof fn vm_rp_fragment_text() + ensures + replace_error(vm_s6(), 6, 11, 0) is Some, +{ + vm_s6_ok(); + let d = vm_s6(); + assert(vm_s6().nodes[11].parent is None) by (compute_only); + assert(!replace_condition(vm_s6(), 6, 11, 0)) by (compute_only); + lemma_replace_refused(d, 6, 11, 0); +} + +/// Step 6 of "replace", fragment of one element: refused if the parent has another element child. +pub proof fn vm_rp_fragment_element_parent_element() + ensures + replace_error(vm_s4(), 6, 13, 0) is Some, +{ + vm_s4_ok(); + let d = vm_s4(); + assert(vm_s4().nodes[13].parent is None) by (compute_only); + assert(!replace_condition(vm_s4(), 6, 13, 0)) by (compute_only); + lemma_replace_refused(d, 6, 13, 0); +} + +/// Step 6 of "replace", fragment of one element: refused if a doctype is following the child. +pub proof fn vm_rp_fragment_element_doctype_after() + ensures + replace_error(vm_s3(), 6, 13, 0) is Some, +{ + vm_s3_ok(); + let d = vm_s3(); + assert(vm_s3().nodes[13].parent is None) by (compute_only); + assert(!replace_condition(vm_s3(), 6, 13, 0)) by (compute_only); + lemma_replace_refused(d, 6, 13, 0); +} + +/// Step 6 of "replace", element: refused if the parent has another element child. +pub proof fn vm_rp_element_parent_element() + ensures + replace_error(vm_s4(), 6, 3, 0) is Some, +{ + vm_s4_ok(); + let d = vm_s4(); + assert(vm_s4().nodes[3].parent is None) by (compute_only); + assert(!replace_condition(vm_s4(), 6, 3, 0)) by (compute_only); + lemma_replace_refused(d, 6, 3, 0); +} + +/// Step 6 of "replace", element: refused if a doctype is following the child. +pub proof fn vm_rp_element_doctype_after() + ensures + replace_error(vm_s3(), 6, 3, 0) is Some, +{ + vm_s3_ok(); + let d = vm_s3(); + assert(vm_s3().nodes[3].parent is None) by (compute_only); + assert(!replace_condition(vm_s3(), 6, 3, 0)) by (compute_only); + lemma_replace_refused(d, 6, 3, 0); +} + +/// Step 6 of "replace", doctype: refused if the parent has another doctype child. +pub proof fn vm_rp_doctype_parent_doctype() + ensures + replace_error(vm_s8(), 6, 5, 0) is Some, +{ + vm_s8_ok(); + let d = vm_s8(); + assert(vm_s8().nodes[5].parent is None) by (compute_only); + assert(!replace_condition(vm_s8(), 6, 5, 0)) by (compute_only); + lemma_replace_refused(d, 6, 5, 0); +} + +/// Step 6 of "replace", doctype: refused if an element is preceding the child. +pub proof fn vm_rp_doctype_element_preceding() + ensures + replace_error(vm_s4(), 6, 5, 0) is Some, +{ + vm_s4_ok(); + let d = vm_s4(); + assert(vm_s4().nodes[5].parent is None) by (compute_only); + assert(!replace_condition(vm_s4(), 6, 5, 0)) by (compute_only); + lemma_replace_refused(d, 6, 5, 0); +} + +/// An element may replace the document's element. +pub proof fn vm_rp_element_replaces_element() + ensures + replace_error(vm_s1(), 2, 3, 0) is None, +{ + vm_s1_ok(); + let d = vm_s1(); + assert(vm_s1().nodes[3].parent is None) by (compute_only); + assert(replace_steps_1_to_4(vm_s1(), 2, 3, 0)) by (compute_only); + assert(replace_condition(vm_s1(), 2, 3, 0)) by (compute_only); + lemma_replace_accepted(d, 2, 3, 0); +} + +/// A doctype may replace the document's doctype. +pub proof fn vm_rp_doctype_replaces_doctype() + ensures + replace_error(vm_s2(), 4, 5, 0) is None, +{ + vm_s2_ok(); + let d = vm_s2(); + assert(vm_s2().nodes[5].parent is None) by (compute_only); + assert(replace_steps_1_to_4(vm_s2(), 4, 5, 0)) by (compute_only); + assert(replace_condition(vm_s2(), 4, 5, 0)) by (compute_only); + lemma_replace_accepted(d, 4, 5, 0); +} + +/// A fragment of one element may replace the document's element. +pub proof fn vm_rp_fragment_replaces_element() + ensures + replace_error(vm_s1(), 2, 13, 0) is None, +{ + vm_s1_ok(); + let d = vm_s1(); + assert(vm_s1().nodes[13].parent is None) by (compute_only); + assert(replace_steps_1_to_4(vm_s1(), 2, 13, 0)) by (compute_only); + assert(replace_condition(vm_s1(), 2, 13, 0)) by (compute_only); + lemma_replace_accepted(d, 2, 13, 0); +} + +/// A `Text` node may replace an element's child. +pub proof fn vm_rp_text_in_element() + ensures + replace_error(vm_s9(), 7, 1, 15) is None, +{ + vm_s9_ok(); + let d = vm_s9(); + assert(vm_s9().nodes[1].parent is None) by (compute_only); + assert(replace_steps_1_to_4(vm_s9(), 7, 1, 15)) by (compute_only); + assert(replace_condition(vm_s9(), 7, 1, 15)) by (compute_only); + lemma_replace_accepted(d, 7, 1, 15); +} + +/// A doctype may replace a comment of an empty-of-elements document. +pub proof fn vm_rp_doctype_in_document() + ensures + replace_error(vm_s6(), 6, 4, 0) is None, +{ + vm_s6_ok(); + let d = vm_s6(); + assert(vm_s6().nodes[4].parent is None) by (compute_only); + assert(replace_steps_1_to_4(vm_s6(), 6, 4, 0)) by (compute_only); + assert(replace_condition(vm_s6(), 6, 4, 0)) by (compute_only); + lemma_replace_accepted(d, 6, 4, 0); +} + +} // verus! diff --git a/crates/clazel-dom/tests/operations.rs b/crates/clazel-dom/tests/operations.rs new file mode 100644 index 000000000..db6526aba --- /dev/null +++ b/crates/clazel-dom/tests/operations.rs @@ -0,0 +1,736 @@ +//! Unit tests of Phase 7 item 7.13's operations on the executable arena: +//! "ensure pre-insertion validity", "pre-insert", "insert" of a fragment, +//! "replace", "replace all", "adopt", shallow "clone a node" and the +//! attribute changes. +//! +//! The proofs show each operation equals its spec function and keeps the +//! tree well formed; these tests check the compiled code on the cases the +//! DOM Standard's algorithms (and the WPT `dom/nodes` tests `Node-insertBefore`, +//! `Node-replaceChild`, `Node-cloneNode`, `ParentNode-replaceChildren` and +//! `Document-adoptNode`, from which the cases are taken from memory) are +//! about. They matter most for step 6 of validity (a document's children), +//! which no theorem covers. + +use clazel_dom::exec::{Arena, ExecAttr, ExecKind}; +use clazel_dom::spec::{DomError, Namespace}; + +fn s(text: &str) -> Vec { + text.chars().map(u32::from).collect() +} + +fn element(name: &str) -> ExecKind { + ExecKind::Element { + namespace: Some(Namespace::Html), + prefix: None, + local_name: s(name), + attributes: Vec::new(), + template_contents: None, + form_owner: None, + } +} + +fn text(data: &str) -> ExecKind { + ExecKind::Text { data: s(data) } +} + +fn doctype(name: &str) -> ExecKind { + ExecKind::DocumentType { + name: s(name), + public_id: Vec::new(), + system_id: Vec::new(), + } +} + +fn fragment() -> ExecKind { + ExecKind::DocumentFragment { host: None } +} + +fn create(a: &mut Arena, kind: ExecKind) -> usize { + let id = a.create(kind); + assert!(id.is_some(), "the node limit is far away"); + id.unwrap_or_default() +} + +fn attr(name: &str, value: &str) -> ExecAttr { + ExecAttr { + namespace: None, + prefix: None, + local_name: s(name), + value: s(value), + } +} + +fn is_hierarchy(e: &Option) -> bool { + matches!(e, Some(DomError::HierarchyRequest)) +} + +fn is_not_found(e: &Option) -> bool { + matches!(e, Some(DomError::NotFound)) +} + +/// The parent/children links agree, there are no duplicates, node 0 has no +/// parent, and every chain of parents ends. +fn check(a: &Arena) { + for n in 0..a.len() { + let kids = a.children(n); + for c in kids { + assert_eq!(a.parent(*c), Some(n), "child {c} of {n}"); + assert_eq!( + kids.iter().filter(|x| *x == c).count(), + 1, + "duplicate child {c} of {n}" + ); + } + if let Some(p) = a.parent(n) { + assert!(a.children(p).contains(&n), "{n} is not in its parent {p}"); + } + let mut cur = n; + for _ in 0..=a.len() { + match a.parent(cur) { + Some(p) => cur = p, + None => break, + } + } + assert_eq!(a.parent(cur), None, "cycle above {n}"); + } + assert_eq!(a.parent(0), None); +} + +/// Three elements `a`, `b` and `c`, appended in order to `parent`. +fn kids3(a: &mut Arena, parent: usize) -> (usize, usize, usize) { + let x = create(a, element("a")); + let y = create(a, element("b")); + let z = create(a, element("c")); + a.append(parent, x); + a.append(parent, y); + a.append(parent, z); + (x, y, z) +} + +/// A new document. +fn new_doc() -> Arena { + Arena::new(s("about:blank")) +} + +#[test] +fn document_children_rules() { + let mut a = new_doc(); + let t = create(&mut a, text("x")); + // 5. A Text node cannot be a child of a document. + assert!(is_hierarchy(&a.ensure_pre_insertion_validity(t, 0, None))); + + let html = create(&mut a, element("html")); + let dt = create(&mut a, doctype("html")); + let dt2 = create(&mut a, doctype("html")); + let e2 = create(&mut a, element("other")); + // A doctype after the element is invalid; before it, valid. + assert!(a.pre_insert(html, 0, None).is_none()); + assert!(is_hierarchy(&a.ensure_pre_insertion_validity(dt, 0, None))); + assert!(a.ensure_pre_insertion_validity(dt, 0, Some(html)).is_none()); + assert!(a.pre_insert(dt, 0, Some(html)).is_none()); + assert_eq!(a.children(0), &[dt, html]); + // A second doctype, or a second element, is invalid. + assert!(is_hierarchy(&a.pre_insert(dt2, 0, Some(dt)))); + assert!(is_hierarchy(&a.pre_insert(dt2, 0, Some(html)))); + assert!(is_hierarchy(&a.pre_insert(e2, 0, None))); + // An element before a doctype is invalid. + assert!(is_hierarchy(&a.pre_insert(e2, 0, Some(dt)))); + assert_eq!(a.children(0), &[dt, html]); + // Moving the element within the document is fine: the check sees the + // element where it is, as the standard does (a parent with an element + // child, here `html` itself, is invalid). + assert!(is_hierarchy(&a.pre_insert(html, 0, None))); + // A doctype is not allowed in an element. + assert!(is_hierarchy( + &a.ensure_pre_insertion_validity(dt2, html, None) + )); + check(&a); +} + +#[test] +fn document_children_rules_for_fragments() { + let mut a = new_doc(); + let f = create(&mut a, fragment()); + let e1 = create(&mut a, element("a")); + let e2 = create(&mut a, element("b")); + let t = create(&mut a, text("x")); + a.append(f, e1); + a.append(f, e2); + // More than one element child. + assert!(is_hierarchy(&a.ensure_pre_insertion_validity(f, 0, None))); + // A Text child. + let g = create(&mut a, fragment()); + let e3 = create(&mut a, element("c")); + a.append(g, e3); + a.append(g, t); + assert!(is_hierarchy(&a.ensure_pre_insertion_validity(g, 0, None))); + // One element child is fine in an empty document, not in one with an + // element. + let h = create(&mut a, fragment()); + let e4 = create(&mut a, element("d")); + a.append(h, e4); + assert!(a.pre_insert(h, 0, None).is_none()); + assert_eq!(a.children(0), &[e4]); + assert!(a.children(h).is_empty()); + let h2 = create(&mut a, fragment()); + let e5 = create(&mut a, element("e")); + a.append(h2, e5); + assert!(is_hierarchy(&a.ensure_pre_insertion_validity(h2, 0, None))); + check(&a); +} + +#[test] +fn hierarchy_checks() { + let mut a = new_doc(); + let html = create(&mut a, element("html")); + let body = create(&mut a, element("body")); + let p = create(&mut a, element("p")); + let t = create(&mut a, text("x")); + a.append(0, html); + a.append(html, body); + a.append(body, p); + a.append(p, t); + // 1. A text node cannot have children. + assert!(is_hierarchy(&a.ensure_pre_insertion_validity(p, t, None))); + // 2. A node cannot go into itself or a descendant. + assert!(is_hierarchy( + &a.ensure_pre_insertion_validity(html, p, None) + )); + assert!(is_hierarchy( + &a.ensure_pre_insertion_validity(body, body, None) + )); + // 3. The reference child must be a child of the parent. + assert!(is_not_found(&a.ensure_pre_insertion_validity( + p, + body, + Some(t) + ))); + // 4. The document node cannot be inserted (into a parentless element). + let loose = create(&mut a, element("div")); + assert!(is_hierarchy( + &a.ensure_pre_insertion_validity(0, loose, None) + )); + // An error changes nothing. + let before: Vec> = (0..a.len()).map(|n| a.children(n).to_vec()).collect(); + assert!(is_hierarchy(&a.pre_insert(html, p, None))); + assert!(is_not_found(&a.pre_insert(p, body, Some(t)))); + let after: Vec> = (0..a.len()).map(|n| a.children(n).to_vec()).collect(); + assert_eq!(before, after); + check(&a); +} + +#[test] +fn a_template_cannot_go_into_its_own_contents() { + let mut a = new_doc(); + let tpl = create(&mut a, element("template")); + let contents = a.create_template_contents(tpl); + assert!(contents.is_some()); + let contents = contents.unwrap_or_default(); + // The template is a host-including inclusive ancestor of its contents' + // children and of the contents themselves. + let p = create(&mut a, element("p")); + a.append(contents, p); + assert!(is_hierarchy(&a.ensure_pre_insertion_validity(tpl, p, None))); + assert!(is_hierarchy( + &a.ensure_pre_insertion_validity(tpl, contents, None) + )); + // A fragment that is the contents of a template can itself be inserted, + // and then moves the template's children out. + let host = create(&mut a, element("div")); + assert!(a.pre_insert(contents, host, None).is_none()); + assert_eq!(a.children(host), &[p]); + assert!(a.children(contents).is_empty()); + assert_eq!(a.parent(contents), None); + check(&a); +} + +#[test] +fn pre_insert_places_children() { + let mut a = new_doc(); + let div = create(&mut a, element("div")); + let (k0, k1, k2) = kids3(&mut a, div); + let x = create(&mut a, element("x")); + assert!(a.pre_insert(x, div, Some(k1)).is_none()); + assert_eq!(a.children(div), &[k0, x, k1, k2]); + assert_eq!(a.next_sibling(x), Some(k1)); + assert_eq!(a.next_sibling(k2), None); + assert_eq!(a.next_sibling(0), None); + // Before itself: the reference child is its next sibling, so it stays. + assert!(a.pre_insert(x, div, Some(x)).is_none()); + assert_eq!(a.children(div), &[k0, x, k1, k2]); + // Before itself, as the last child: it stays last. + assert!(a.pre_insert(k2, div, Some(k2)).is_none()); + assert_eq!(a.children(div), &[k0, x, k1, k2]); + // Moving the first child to the end. + assert!(a.pre_insert(k0, div, None).is_none()); + assert_eq!(a.children(div), &[x, k1, k2, k0]); + // Moving a node before a later sibling. + assert!(a.pre_insert(x, div, Some(k0)).is_none()); + assert_eq!(a.children(div), &[k1, k2, x, k0]); + check(&a); +} + +#[test] +fn inserting_a_fragment_moves_its_children_in_order() { + let mut a = new_doc(); + let div = create(&mut a, element("div")); + let (k0, k1, k2) = kids3(&mut a, div); + let f = create(&mut a, fragment()); + let x = create(&mut a, element("x")); + let y = create(&mut a, text("y")); + a.append(f, x); + a.append(f, y); + assert!(a.pre_insert(f, div, Some(k1)).is_none()); + assert_eq!(a.children(div), &[k0, x, y, k1, k2]); + assert!(a.children(f).is_empty()); + assert_eq!(a.parent(f), None); + assert_eq!(a.parent(x), Some(div)); + assert_eq!(a.parent(y), Some(div)); + // An empty fragment changes nothing. + assert!(a.pre_insert(f, div, Some(k0)).is_none()); + assert_eq!(a.children(div), &[k0, x, y, k1, k2]); + // At the end. + let g = create(&mut a, fragment()); + let z = create(&mut a, element("z")); + a.append(g, z); + assert!(a.pre_insert(g, div, None).is_none()); + assert_eq!(a.children(div), &[k0, x, y, k1, k2, z]); + // A fragment cannot go into one of its own children. + let h = create(&mut a, fragment()); + let w = create(&mut a, element("w")); + a.append(h, w); + assert!(is_hierarchy(&a.pre_insert(h, w, None))); + assert_eq!(a.children(h), &[w]); + check(&a); +} + +#[test] +fn replace_swaps_in_place() { + let mut a = new_doc(); + let div = create(&mut a, element("div")); + let (k0, k1, k2) = kids3(&mut a, div); + let x = create(&mut a, element("x")); + assert!(a.replace(k1, x, div).is_none()); + assert_eq!(a.children(div), &[k0, x, k2]); + assert_eq!(a.parent(k1), None); + // Replacing a child with itself changes nothing. + assert!(a.replace(x, x, div).is_none()); + assert_eq!(a.children(div), &[k0, x, k2]); + // With its next sibling: the next sibling takes its place. + assert!(a.replace(k0, x, div).is_none()); + assert_eq!(a.children(div), &[x, k2]); + assert_eq!(a.parent(k0), None); + // With a node that comes before it. + assert!(a.replace(k2, x, div).is_none()); + assert_eq!(a.children(div), &[x]); + // A node that is not a child. + let other = create(&mut a, element("o")); + assert!(is_not_found(&a.replace(other, x, div))); + // A fragment. + let f = create(&mut a, fragment()); + let p = create(&mut a, element("p")); + let q = create(&mut a, element("q")); + a.append(f, p); + a.append(f, q); + assert!(a.replace(x, f, div).is_none()); + assert_eq!(a.children(div), &[p, q]); + assert!(a.children(f).is_empty()); + assert_eq!(a.parent(x), None); + // The parent cannot be put into itself. + assert!(is_hierarchy(&a.replace(p, div, div))); + check(&a); +} + +#[test] +fn replace_in_a_document() { + let mut a = new_doc(); + let dt = create(&mut a, doctype("html")); + let html = create(&mut a, element("html")); + a.append(0, dt); + a.append(0, html); + // An element for the element is fine; for the doctype it is not (an + // element child that is not the child). + let e = create(&mut a, element("other")); + assert!(is_hierarchy(&a.replace(dt, e, 0))); + assert!(a.replace(html, e, 0).is_none()); + assert_eq!(a.children(0), &[dt, e]); + // A doctype for the doctype is fine; for the element it is not (the + // doctype after... an element is preceding). + let dt2 = create(&mut a, doctype("html")); + assert!(is_hierarchy(&a.replace(e, dt2, 0))); + assert!(a.replace(dt, dt2, 0).is_none()); + assert_eq!(a.children(0), &[dt2, e]); + // A text node cannot replace anything in a document. + let t = create(&mut a, text("x")); + assert!(is_hierarchy(&a.replace(e, t, 0))); + // An element cannot replace the doctype when a doctype follows... + let e2 = create(&mut a, element("e2")); + assert!(is_hierarchy(&a.replace(dt2, e2, 0))); + check(&a); +} + +#[test] +fn replace_all_replaces_children() { + let mut a = new_doc(); + let div = create(&mut a, element("div")); + let (k0, k1, k2) = kids3(&mut a, div); + let t = create(&mut a, text("hello")); + a.replace_all(Some(t), div); + assert_eq!(a.children(div), &[t]); + for n in [k0, k1, k2] { + assert_eq!(a.parent(n), None); + } + // A child of the parent itself. + a.replace_all(Some(t), div); + assert_eq!(a.children(div), &[t]); + // No node. + a.replace_all(None, div); + assert!(a.children(div).is_empty()); + assert_eq!(a.parent(t), None); + // A fragment. + let f = create(&mut a, fragment()); + let x = create(&mut a, element("x")); + let y = create(&mut a, element("y")); + a.append(f, x); + a.append(f, y); + a.append(div, k0); + a.replace_all(Some(f), div); + assert_eq!(a.children(div), &[x, y]); + assert!(a.children(f).is_empty()); + assert_eq!(a.parent(k0), None); + // A node from elsewhere in the tree moves. + let other = create(&mut a, element("other")); + let z = create(&mut a, element("z")); + a.append(other, z); + a.replace_all(Some(z), div); + assert_eq!(a.children(div), &[z]); + assert!(a.children(other).is_empty()); + check(&a); +} + +#[test] +fn adopt_detaches() { + let mut a = new_doc(); + let div = create(&mut a, element("div")); + let p = create(&mut a, element("p")); + a.append(div, p); + a.adopt(p); + assert_eq!(a.parent(p), None); + assert!(a.children(div).is_empty()); + // A node without a parent is left alone. + a.adopt(p); + assert_eq!(a.parent(p), None); + check(&a); +} + +fn attrs(a: &Arena, n: usize) -> Vec<(String, String)> { + match a.kind(n) { + ExecKind::Element { attributes, .. } => attributes + .iter() + .map(|x| { + ( + x.local_name + .iter() + .filter_map(|c| char::from_u32(*c)) + .collect(), + x.value.iter().filter_map(|c| char::from_u32(*c)).collect(), + ) + }) + .collect(), + _ => Vec::new(), + } +} + +#[test] +fn attribute_changes() { + let mut a = new_doc(); + let e = create(&mut a, element("e")); + a.append_attribute(e, attr("a", "1")); + a.append_attribute(e, attr("b", "2")); + a.append_attribute(e, attr("c", "3")); + a.change_attribute(e, 1, s("two")); + assert_eq!( + attrs(&a, e), + [ + ("a".into(), "1".into()), + ("b".into(), "two".into()), + ("c".into(), "3".into()) + ] + ); + a.replace_attribute(e, 0, attr("z", "26")); + assert_eq!( + attrs(&a, e), + [ + ("z".into(), "26".into()), + ("b".into(), "two".into()), + ("c".into(), "3".into()) + ] + ); + a.remove_attribute(e, 1); + assert_eq!( + attrs(&a, e), + [("z".into(), "26".into()), ("c".into(), "3".into())] + ); + a.remove_attribute(e, 1); + a.remove_attribute(e, 0); + assert!(attrs(&a, e).is_empty()); + check(&a); +} + +#[test] +fn shallow_clone() { + let mut a = new_doc(); + let e = create( + &mut a, + ExecKind::Element { + namespace: Some(Namespace::Svg), + prefix: Some(s("p")), + local_name: s("circle"), + attributes: vec![attr("r", "5"), attr("cx", "1")], + template_contents: None, + form_owner: None, + }, + ); + let kid = create(&mut a, text("kid")); + a.append(0, e); + a.append(e, kid); + let n = a.len(); + let copy = a.clone_node(e); + assert_eq!(copy, Some(n)); + let c = copy.unwrap_or_default(); + // A shallow copy: same fields, no parent, no children. + assert_eq!(a.parent(c), None); + assert!(a.children(c).is_empty()); + assert_eq!(attrs(&a, c), attrs(&a, e)); + match a.kind(c) { + ExecKind::Element { + namespace, + prefix, + local_name, + template_contents, + form_owner, + .. + } => { + assert!(matches!(namespace, Some(Namespace::Svg))); + assert_eq!(prefix.as_deref(), Some(s("p").as_slice())); + assert_eq!(*local_name, s("circle")); + assert!(template_contents.is_none()); + assert!(form_owner.is_none()); + } + other => assert!(matches!(other, ExecKind::Element { .. }), "an element"), + } + assert_eq!(a.len(), n + 1); + // The original is untouched, and the copy is independent of it. + assert_eq!(a.children(e), &[kid]); + a.change_attribute(c, 0, s("9")); + assert_eq!(attrs(&a, e).first().map(|x| x.1.as_str()), Some("5")); + // Other kinds. + let t = a.clone_node(kid).unwrap_or_default(); + assert!(matches!(a.kind(t), ExecKind::Text { data } if *data == s("kid"))); + let dt = create(&mut a, doctype("html")); + let d2 = a.clone_node(dt).unwrap_or_default(); + assert!(matches!(a.kind(d2), ExecKind::DocumentType { name, .. } if *name == s("html"))); + let f = create(&mut a, fragment()); + let f2 = a.clone_node(f).unwrap_or_default(); + assert!(matches!( + a.kind(f2), + ExecKind::DocumentFragment { host: None } + )); + check(&a); +} + +#[test] +fn cloning_a_template_gives_the_copy_contents_of_its_own() { + let mut a = new_doc(); + let tpl = create(&mut a, element("template")); + let contents = a.create_template_contents(tpl).unwrap_or_default(); + let p = create(&mut a, element("p")); + a.append(contents, p); + let n = a.len(); + let copy = a.clone_node(tpl).unwrap_or_default(); + assert_eq!(copy, n); + assert_eq!(a.len(), n + 2); + match a.kind(copy) { + ExecKind::Element { + template_contents: Some(c), + .. + } => { + assert_eq!(*c, n + 1); + // Shallow: the copy's contents are empty, and the original's + // are as they were. + assert!(a.children(*c).is_empty()); + assert!( + matches!(a.kind(*c), ExecKind::DocumentFragment { host: Some(h) } if *h == copy) + ); + } + other => assert!( + matches!( + other, + ExecKind::Element { + template_contents: Some(_), + .. + } + ), + "a template" + ), + } + assert_eq!(a.children(contents), &[p]); + check(&a); +} + +/// `copy` is a fresh copy of the host-including subtree at `src`: the same +/// shape, kinds and data, template contents included, and every node of it +/// is a new node (id >= `old_len`). +fn same_shape(a: &Arena, src: usize, copy: usize, old_len: usize) { + assert!(copy >= old_len, "{copy} is not a new node"); + match (a.kind(src), a.kind(copy)) { + (ExecKind::Text { data: x }, ExecKind::Text { data: y }) => assert_eq!(x, y), + (ExecKind::Comment { data: x }, ExecKind::Comment { data: y }) => assert_eq!(x, y), + ( + ExecKind::Element { + local_name: x, + template_contents: tx, + .. + }, + ExecKind::Element { + local_name: y, + template_contents: ty, + form_owner, + .. + }, + ) => { + assert_eq!(x, y); + assert!(form_owner.is_none()); + assert_eq!(tx.is_some(), ty.is_some()); + if let (Some(sx), Some(sy)) = (tx, ty) { + same_shape(a, *sx, *sy, old_len); + } + assert_eq!(attrs(a, src), attrs(a, copy)); + } + (x, y) => assert!( + std::mem::discriminant(x) == std::mem::discriminant(y), + "kinds differ" + ), + } + let (ks, kc) = (a.children(src), a.children(copy)); + assert_eq!(ks.len(), kc.len()); + for (s, c) in ks.iter().zip(kc) { + assert_eq!(a.parent(*c), Some(copy)); + same_shape(a, *s, *c, old_len); + } +} + +#[test] +fn deep_clone_copies_the_whole_subtree() { + let mut a = new_doc(); + let root = create(&mut a, element("div")); + a.append(0, root); + let p = create(&mut a, element("p")); + let t = create(&mut a, text("hello")); + let c = create(&mut a, ExecKind::Comment { data: s("note") }); + let q = create(&mut a, element("q")); + let t2 = create(&mut a, text("deep")); + a.append(root, p); + a.append(p, t); + a.append(root, c); + a.append(root, q); + a.append(q, t2); + let before: Vec> = (0..a.len()).map(|n| a.children(n).to_vec()).collect(); + let n = a.len(); + let copy = a.clone_deep(root); + assert_eq!(copy, Some(n)); + let copy = copy.unwrap_or_default(); + assert_eq!(a.len(), n + 6); + assert_eq!(a.parent(copy), None); + same_shape(&a, root, copy, n); + // The source is untouched. + for (i, kids) in before.iter().enumerate() { + assert_eq!(a.children(i), kids.as_slice()); + } + check(&a); + // A leaf's deep clone is its shallow clone. + let leaf = a.clone_deep(t2).unwrap_or_default(); + assert!(matches!(a.kind(leaf), ExecKind::Text { data } if *data == s("deep"))); + assert!(a.children(leaf).is_empty()); + check(&a); +} + +#[test] +fn deep_clone_clones_template_contents_and_nests() { + let mut a = new_doc(); + let outer = create(&mut a, element("template")); + let oc = a.create_template_contents(outer).unwrap_or_default(); + let inner = create(&mut a, element("template")); + let ic = a.create_template_contents(inner).unwrap_or_default(); + let span = create(&mut a, element("span")); + let tx = create(&mut a, text("x")); + a.append(oc, inner); + a.append(ic, span); + a.append(span, tx); + // A template with ordinary children too. + let kid = create(&mut a, element("b")); + a.append(outer, kid); + let n = a.len(); + let copy = a.clone_deep(outer).unwrap_or_default(); + assert_eq!(a.len(), n + 7); + same_shape(&a, outer, copy, n); + // The copy's contents is hosted by the copy; the source's is as it was. + match a.kind(copy) { + ExecKind::Element { + template_contents: Some(c), + .. + } => { + assert!( + matches!(a.kind(*c), ExecKind::DocumentFragment { host: Some(h) } if *h == copy) + ); + assert_ne!(*c, oc); + } + other => assert!( + matches!(other, ExecKind::Element { .. }), + "a template element" + ), + } + assert_eq!(a.children(oc), &[inner]); + check(&a); + // Cloning a template's contents directly gives a fragment with no host. + let f = a.clone_deep(oc).unwrap_or_default(); + assert!(matches!( + a.kind(f), + ExecKind::DocumentFragment { host: None } + )); + assert_eq!(a.children(f).len(), 1); + same_shape(&a, oc, f, n); + check(&a); +} + +/// A deep clone that does not fit leaves the arena exactly as it was. The +/// arena is filled to the node limit, which Miri would take minutes to do. +#[test] +#[cfg_attr(miri, ignore = "creating four million nodes takes minutes under Miri")] +fn deep_clone_that_does_not_fit_changes_nothing() { + let mut a = new_doc(); + let root = create(&mut a, element("div")); + a.append(0, root); + for _ in 0..4 { + let k = create(&mut a, element("i")); + a.append(root, k); + } + // Leave room for the copy of the root and two of its four children. + while a.len() < clazel_dom::exec::MAX_NODES - 3 { + assert!(a.create(text("filler")).is_some()); + } + let n = a.len(); + let kids = a.children(root).to_vec(); + assert_eq!(a.clone_deep(root), None); + assert_eq!(a.len(), n); + assert_eq!(a.children(root), kids.as_slice()); + assert_eq!(a.parent(root), Some(0)); + for c in &kids { + assert!(a.children(*c).is_empty()); + assert_eq!(a.parent(*c), Some(root)); + } + // One that does fit still works. + let first = kids.first().copied().unwrap_or_default(); + assert!(a.clone_deep(first).is_some()); + assert_eq!(a.len(), n + 1); +} diff --git a/crates/clazel-gpu/README.md b/crates/clazel-gpu/README.md index 540b049c4..332607520 100644 --- a/crates/clazel-gpu/README.md +++ b/crates/clazel-gpu/README.md @@ -390,7 +390,12 @@ CI; under Miri it does nothing. The order is in `src/sandbox.rs`: a check that the host turned off Mesa's on-disk shader cache (`MESA_SHADER_CACHE_DISABLE=true`, `sandbox::LINUX_ENVIRONMENT`; with a cold cache its worker thread wrote cache files after the filter and was -killed for it, in 3 runs of `browse_gpu.rs` in 10); `no_new_privs` and the namespaces (user, mount, +killed for it, in 3 runs of `browse_gpu.rs` in 10) and fixed glibc's arena limit +(`MALLOC_ARENA_MAX=64`; without it, the first thread to need a new arena once the +process held more than eight worked the limit out by opening +`/sys/devices/system/cpu/online`, which on a CI runner with four processors could +come after the filter: `SIGSYS`, audit line `syscall=257`, on x86-64); +`no_new_privs` and the namespaces (user, mount, network, IPC; no PID namespace, after which the kernel refuses new threads) while there is one thread; the Vulkan backend; a warm-up frame of our own, drawn, read back and checked; Landlock with no rule; then the seccomp list on every thread at diff --git a/crates/clazel-gpu/src/sandbox.rs b/crates/clazel-gpu/src/sandbox.rs index 1bbc4a795..26bf37e43 100644 --- a/crates/clazel-gpu/src/sandbox.rs +++ b/crates/clazel-gpu/src/sandbox.rs @@ -19,7 +19,9 @@ //! when it starts the process (`clazel-renderer`'s `launch::spawn_gpu`): it //! turns off Mesa's on-disk shader cache, whose worker thread otherwise //! opens, locks and writes cache files at any time, the filter below -//! included. A process started without it is refused here, every time, +//! included, and it fixes the C library's arena limit, whose lazy +//! computation otherwise opens a file at the first new thread after the +//! filter. A process started without it is refused here, every time, //! rather than killed by its filter some of the time; //! 1. `no_new_privs`, then the namespaces (`namespaces::GPU`: user, mount, //! network, IPC), while the process still has one thread, which a user @@ -89,9 +91,11 @@ impl fmt::Display for Failure { impl std::error::Error for Failure {} /// What the host adds to the Linux GPU process's environment, and what -/// [`start_vulkan`] checks is there before it makes the backend. +/// [`start_vulkan`] checks is there before it makes the backend. Two things, each +/// of which was a way for the process to be killed by its own filter some of the +/// time. /// -/// `MESA_SHADER_CACHE_DISABLE=true` turns off Mesa's on-disk shader cache +/// **`MESA_SHADER_CACHE_DISABLE=true`** turns off Mesa's on-disk shader cache /// (`util/disk_cache.c`, read when the driver makes its device). With the cache /// on, lavapipe hands each compiled shader to a worker thread (named `disk$0`) /// that writes it under `~/.cache/mesa_shader_cache` later, with `openat`, @@ -105,7 +109,34 @@ impl std::error::Error for Failure {} /// forbid, and reading compiled machine code back from a file any program of /// the user can write is a way in. The warm-up frame compiles every shader /// before the filter, so nothing needs the cache after it. -pub const LINUX_ENVIRONMENT: &[(&str, &str)] = &[("MESA_SHADER_CACHE_DISABLE", "true")]; +/// +/// **`MALLOC_ARENA_MAX=64`** fixes glibc malloc's arena limit. glibc 2.39's +/// `arena_get2` (`malloc/arena.c`), when a thread needs an arena of its own and +/// none is free, makes a new one; once the process holds more than +/// `arena_test` (8 on 64-bit) arenas it first works out the limit, once, from +/// the number of processors, with `get_nprocs`, which **opens +/// `/sys/devices/system/cpu/online`** (`openat`). Under the filter that is a +/// kill. Whether it comes before or after the filter depends on how many +/// threads have made their first allocation by then, which is timing: the +/// driver's workers (llvmpipe's, LLVM's pool) start as the device is made, the +/// warm-up does not force each to allocate, and the GPU process's own two reader +/// threads, started after the filter, each take an arena. On a machine with many +/// processors the workers alone pass 8 long before the filter; on a CI runner +/// with four, a run can reach the filter with the limit not yet worked out. +/// **Observed on x86-64** (CI's Linux base job: a hosted Ubuntu 24.04 runner, +/// lavapipe 25.2.8, LLVM 20.1.2): a GPU process lost to `SIGSYS` in +/// `tests/browse_gpu.rs`, whose kernel audit line is `comm="clazel-gpu" +/// arch=c000003e syscall=257` (`openat`). **Observed on aarch64** under +/// `strace -f`: every thread that makes an arena before the filter opens +/// `/sys/devices/system/cpu/online` just before its heap's `mmap`, and no file is +/// opened after the filter. With `arena_max` set, `arena_get2` takes it as the +/// limit and never calls `get_nprocs`, so no thread opens anything when it +/// starts; past the limit threads share arenas, which takes futexes. The filter +/// is not widened for `openat`: a file open is what it exists to forbid. +pub const LINUX_ENVIRONMENT: &[(&str, &str)] = &[ + ("MESA_SHADER_CACHE_DISABLE", "true"), + ("MALLOC_ARENA_MAX", "64"), +]; /// Whether `get` (the environment, by name) holds [`LINUX_ENVIRONMENT`]. pub fn has_environment(get: impl Fn(&str) -> Option) -> bool { @@ -326,25 +357,34 @@ mod tests { use crate::soft::SoftBackend; use std::ffi::OsString; - /// The cache is off only when the variable says so exactly. + /// The environment holds only when every variable says so exactly: the + /// shader cache off and the arena limit fixed. #[test] - fn the_environment_must_turn_the_shader_cache_off() { + fn the_environment_must_turn_the_shader_cache_off_and_fix_the_arena_limit() { assert_eq!( LINUX_ENVIRONMENT, - &[("MESA_SHADER_CACHE_DISABLE", "true")][..] + &[ + ("MESA_SHADER_CACHE_DISABLE", "true"), + ("MALLOC_ARENA_MAX", "64") + ][..] ); - let with = |value: Option<&str>| { + let with = |cache: Option<&str>, arenas: Option<&str>| { has_environment(|name| { - (name == "MESA_SHADER_CACHE_DISABLE") - .then_some(value) - .flatten() - .map(OsString::from) + match name { + "MESA_SHADER_CACHE_DISABLE" => cache, + "MALLOC_ARENA_MAX" => arenas, + _ => None, + } + .map(OsString::from) }) }; - assert!(with(Some("true"))); - assert!(!with(None)); - assert!(!with(Some("false"))); - assert!(!with(Some(""))); + assert!(with(Some("true"), Some("64"))); + assert!(!with(None, Some("64"))); + assert!(!with(Some("false"), Some("64"))); + assert!(!with(Some(""), Some("64"))); + assert!(!with(Some("true"), None)); + assert!(!with(Some("true"), Some("0"))); + assert!(!with(Some("true"), Some(""))); } /// The software reference draws the warm-up frame to the pixels it names, so diff --git a/crates/clazel-ipc/mutations.json b/crates/clazel-ipc/mutations.json index 5d7da39ce..ffdf59e23 100644 --- a/crates/clazel-ipc/mutations.json +++ b/crates/clazel-ipc/mutations.json @@ -731,8 +731,8 @@ { "id": "dir-navigate-not-renderer", "file": "src/message/spec.rs", - "find": "| Message::StorageLength\n | Message::Navigate { .. }\n | Message::Display { .. }\n | Message::Title { .. }\n | Message::FetchStyle { .. } => true,", - "replace": "| Message::StorageLength\n | Message::Display { .. }\n | Message::Title { .. }\n | Message::FetchStyle { .. } => true,", + "find": "| Message::StorageLength\n | Message::Navigate { .. }\n | Message::FetchScript { .. }\n | Message::UpdateUrl { .. }\n | Message::Display { .. }\n | Message::Title { .. }\n | Message::FetchStyle { .. } => true,", + "replace": "| Message::StorageLength\n | Message::FetchScript { .. }\n | Message::UpdateUrl { .. }\n | Message::Display { .. }\n | Message::Title { .. }\n | Message::FetchStyle { .. } => true,", "expect": "killed", "reason": "Navigate is not counted as a renderer message: the kernel rejects it, so lemma_example_renderer_navigate is false.", "certificate": { @@ -978,8 +978,8 @@ { "id": "msg-display-direction", "file": "src/message/spec.rs", - "find": " | Message::Navigate { .. }\n | Message::Display { .. }\n | Message::Title { .. }\n | Message::FetchStyle { .. } => true,", - "replace": " | Message::Navigate { .. }\n | Message::Title { .. }\n | Message::FetchStyle { .. } => true,", + "find": " | Message::Navigate { .. }\n | Message::FetchScript { .. }\n | Message::UpdateUrl { .. }\n | Message::Display { .. }\n | Message::Title { .. }\n | Message::FetchStyle { .. } => true,", + "replace": " | Message::Navigate { .. }\n | Message::FetchScript { .. }\n | Message::UpdateUrl { .. }\n | Message::Title { .. }\n | Message::FetchStyle { .. } => true,", "expect": "killed", "reason": "Display is not counted as a renderer message: the kernel rejects it, so lemma_example_display is false.", "certificate": { @@ -1273,6 +1273,110 @@ "lemma": "lemma_max_style_chunk()", "witness": "max_style_chunk() != 1048559nat" } + }, + { + "id": "msg-fetch-script-tag", + "file": "src/message/spec.rs", + "find": "Message::FetchScript { request, url } => seq![Token::Array(3), Token::Uint(16),", + "replace": "Message::FetchScript { request, url } => seq![Token::Array(3), Token::Uint(20),", + "expect": "killed", + "reason": "FetchScript is written with tag 20, which nothing reads: lemma_decode_encode_message is false.", + "certificate": { + "file": "src/message/lemmas.rs", + "lemma": "lemma_decode_encode_message(Message::FetchScript { request: 1, url: seq![0x61u8] })", + "witness": "message_ok(Message::FetchScript { request: 1, url: seq![0x61u8] }) && decode_message(encode_message(Message::FetchScript { request: 1, url: seq![0x61u8] })) != Some(Message::FetchScript { request: 1, url: seq![0x61u8] })" + } + }, + { + "id": "msg-script-body-request-dropped", + "file": "src/message/spec.rs", + "find": "Token::Uint(116), Token::Uint(request), Token::Bytes(bytes)]", + "replace": "Token::Uint(116), Token::Uint(0), Token::Bytes(bytes)]", + "expect": "killed", + "reason": "A ScriptBody loses its request id: the renderer would add the bytes to another fetch's script. lemma_decode_encode_message is false.", + "certificate": { + "file": "src/message/lemmas.rs", + "lemma": "lemma_decode_encode_message(Message::ScriptBody { request: 2, bytes: seq![0x61u8] })", + "witness": "message_ok(Message::ScriptBody { request: 2, bytes: seq![0x61u8] }) && decode_message(encode_message(Message::ScriptBody { request: 2, bytes: seq![0x61u8] })) != Some(Message::ScriptBody { request: 2, bytes: seq![0x61u8] })" + } + }, + { + "id": "msg-script-failed-parse-tag", + "file": "src/message/spec.rs", + "find": "} else if tag == 118 {", + "replace": "} else if tag == 119 {", + "expect": "killed", + "reason": "The parser reads ScriptFailed under tag 119: lemma_decode_encode_message is false.", + "certificate": { + "file": "src/message/lemmas.rs", + "lemma": "lemma_decode_encode_message(Message::ScriptFailed { request: 3 })", + "witness": "message_ok(Message::ScriptFailed { request: 3 }) && decode_message(encode_message(Message::ScriptFailed { request: 3 })) != Some(Message::ScriptFailed { request: 3 })" + } + }, + { + "id": "msg-update-url-replace-flipped", + "file": "src/message/spec.rs", + "find": "Token::Uint(17), Token::Bool(replace), Token::Text(url)]", + "replace": "Token::Uint(17), Token::Bool(!replace), Token::Text(url)]", + "expect": "killed", + "reason": "The replace flag is flipped on the wire: a pushState would be read as replaceState (and the reverse). lemma_decode_encode_message is false.", + "certificate": { + "file": "src/message/lemmas.rs", + "lemma": "lemma_decode_encode_message(Message::UpdateUrl { replace: true, url: seq![0x61u8] })", + "witness": "message_ok(Message::UpdateUrl { replace: true, url: seq![0x61u8] }) && decode_message(encode_message(Message::UpdateUrl { replace: true, url: seq![0x61u8] })) != Some(Message::UpdateUrl { replace: true, url: seq![0x61u8] })" + } + }, + { + "id": "dir-update-url-not-renderer", + "file": "src/message/spec.rs", + "find": " | Message::UpdateUrl { .. }\n | Message::Display { .. }", + "replace": " | Message::Display { .. }", + "expect": "killed", + "reason": "UpdateUrl is not counted as a renderer message: the kernel would refuse it, so lemma_example_script is false.", + "certificate": { + "file": "src/message/lemmas.rs", + "lemma": "super::examples::lemma_example_script()", + "witness": "decode_renderer_message(seq![0x83u8, 0x11, 0xF5, 0x62, 0x2F, 0x61]) is None" + } + }, + { + "id": "dir-fetch-script-not-renderer", + "file": "src/message/spec.rs", + "find": " | Message::FetchScript { .. }\n | Message::UpdateUrl { .. }", + "replace": " | Message::UpdateUrl { .. }", + "expect": "killed", + "reason": "FetchScript is not counted as a renderer message: the kernel would refuse it, so lemma_example_script is false.", + "certificate": { + "file": "src/message/lemmas.rs", + "lemma": "super::examples::lemma_example_script()", + "witness": "decode_renderer_message(seq![0x83u8, 0x10, 0x01, 0x64, 0x73, 0x2E, 0x6A, 0x73]) is None" + } + }, + { + "id": "dir-script-body-from-renderer", + "file": "src/message/spec.rs", + "find": " | Message::FetchStyle { .. } => true,\n _ => false,", + "replace": " | Message::FetchStyle { .. }\n | Message::ScriptBody { .. } => true,\n _ => false,", + "expect": "killed", + "reason": "A renderer may send ScriptBody, a kernel-to-renderer message: lemma_example_script (the kernel's decoder refuses it) is false.", + "certificate": { + "file": "src/message/lemmas.rs", + "lemma": "super::examples::lemma_example_script()", + "witness": "decode_renderer_message(seq![0x83u8, 0x18, 0x74, 0x01, 0x41, 0x61]) is Some" + } + }, + { + "id": "msg-max-script-chunk", + "file": "src/message/spec.rs", + "find": "pub open spec fn max_script_chunk() -> nat {\n max_style_chunk()\n}", + "replace": "pub open spec fn max_script_chunk() -> nat {\n max_style_chunk() + 1\n}", + "expect": "killed", + "reason": "The largest ScriptBody (with a 9-octet request id) is one octet too long for a frame. lemma_script_body_fits_frame is false for it, but a certificate on a 1 MiB input is too large to evaluate; the certificate is lemma_max_script_chunk, which ties the codec's MAX_SCRIPT_CHUNK (1_048_559, the size a_largest_script_body_fits_one_frame checks on the real code) to the spec.", + "certificate": { + "file": "src/message/codec.rs", + "lemma": "lemma_max_script_chunk()", + "witness": "max_script_chunk() != 1048559nat" + } } ] } diff --git a/crates/clazel-ipc/src/message/codec.rs b/crates/clazel-ipc/src/message/codec.rs index e01023964..661905510 100644 --- a/crates/clazel-ipc/src/message/codec.rs +++ b/crates/clazel-ipc/src/message/codec.rs @@ -111,6 +111,44 @@ pub enum Msg { /// The renderer's id for the fetch. request: u64, }, + /// Renderer to kernel: fetch a script (item 7.16). + FetchScript { + /// The renderer's id for this fetch. + request: u64, + /// The script's URL, as the document wrote it. + url: Vec, + }, + /// Renderer to kernel: `pushState` or `replaceState` (item 7.16). + UpdateUrl { + /// Whether it is `replaceState`. + replace: bool, + /// The URL, as the script wrote it. + url: Vec, + }, + /// Kernel to renderer: the script's response arrived. + ScriptResponse { + /// The renderer's id for the fetch. + request: u64, + /// The response's combined `Content-Type` field value, or empty. + content_type: Vec, + }, + /// Kernel to renderer: the next part of the script's body. + ScriptBody { + /// The renderer's id for the fetch. + request: u64, + /// The octets. + bytes: Vec, + }, + /// Kernel to renderer: the script's body is complete. + ScriptEnd { + /// The renderer's id for the fetch. + request: u64, + }, + /// Kernel to renderer: the script fetch failed (or was refused). + ScriptFailed { + /// The renderer's id for the fetch. + request: u64, + }, /// Kernel to renderer: a document is committed; its body follows in /// `Body` frames, then `BodyEnd`. Commit { @@ -205,6 +243,17 @@ pub proof fn lemma_max_style_chunk() { } +/// [`max_script_chunk`]: the most octets one `ScriptBody` carries, +/// `MAX_FRAME` minus 17 (item 7.16). +pub const MAX_SCRIPT_CHUNK: usize = 1_048_559; + +/// [`MAX_SCRIPT_CHUNK`] is the spec's [`max_script_chunk`]. +pub proof fn lemma_max_script_chunk() + ensures + MAX_SCRIPT_CHUNK as nat == max_script_chunk(), +{ +} + /// The view of a list of strings. pub open spec fn strings_view(v: Seq>) -> Seq> { v.map_values(|s: Vec| s@) @@ -259,6 +308,12 @@ impl View for Msg { Msg::StyleBody { request, bytes } => Message::StyleBody { request: *request, bytes: bytes@ }, Msg::StyleEnd { request } => Message::StyleEnd { request: *request }, Msg::StyleFailed { request } => Message::StyleFailed { request: *request }, + Msg::FetchScript { request, url } => Message::FetchScript { request: *request, url: url@ }, + Msg::UpdateUrl { replace, url } => Message::UpdateUrl { replace: *replace, url: url@ }, + Msg::ScriptResponse { request, content_type } => Message::ScriptResponse { request: *request, content_type: content_type@ }, + Msg::ScriptBody { request, bytes } => Message::ScriptBody { request: *request, bytes: bytes@ }, + Msg::ScriptEnd { request } => Message::ScriptEnd { request: *request }, + Msg::ScriptFailed { request } => Message::ScriptFailed { request: *request }, } } } @@ -405,6 +460,20 @@ fn parse_fields(tag: u64, toks: &[Tok]) -> (r: Option) _ => None, } } else { None } + } else if tag == 16 { + if k == 2 { + match (uint_at(toks, 2), text_at(toks, 3)) { + (Some(a), Some(b)) => Some(Msg::FetchScript { request: a, url: b }), + _ => None, + } + } else { None } + } else if tag == 17 { + if k == 2 { + match (bool_at(toks, 2), text_at(toks, 3)) { + (Some(a), Some(b)) => Some(Msg::UpdateUrl { replace: a, url: b }), + _ => None, + } + } else { None } } else if tag == 15 { if k == 3 { match (uint_at(toks, 2), uint_at(toks, 3), uint_at(toks, 4)) { @@ -498,6 +567,30 @@ fn parse_fields(tag: u64, toks: &[Tok]) -> (r: Option) let a = uint_at(toks, 2)?; Some(Msg::StyleFailed { request: a }) } else { None } + } else if tag == 115 { + if k == 2 { + match (uint_at(toks, 2), bytes_at(toks, 3)) { + (Some(a), Some(b)) => Some(Msg::ScriptResponse { request: a, content_type: b }), + _ => None, + } + } else { None } + } else if tag == 116 { + if k == 2 { + match (uint_at(toks, 2), bytes_at(toks, 3)) { + (Some(a), Some(b)) => Some(Msg::ScriptBody { request: a, bytes: b }), + _ => None, + } + } else { None } + } else if tag == 117 { + if k == 1 { + let a = uint_at(toks, 2)?; + Some(Msg::ScriptEnd { request: a }) + } else { None } + } else if tag == 118 { + if k == 1 { + let a = uint_at(toks, 2)?; + Some(Msg::ScriptFailed { request: a }) + } else { None } } else if tag == 114 { if k == 3 { match (uint_at(toks, 2), uint_at(toks, 3), uint_at(toks, 4)) { @@ -652,6 +745,8 @@ pub fn is_from_renderer(m: &Msg) -> (r: bool) | Msg::Display { .. } | Msg::Title { .. } | Msg::FetchStyle { .. } + | Msg::FetchScript { .. } + | Msg::UpdateUrl { .. } ) } @@ -847,6 +942,12 @@ fn message_tokens(m: &Msg) -> (t: Vec) Msg::StyleBody { request, bytes } => toks4(Tok::Array(3), Tok::Uint(111), Tok::Uint(*request), Tok::Bytes(copy(bytes.as_slice()))), Msg::StyleEnd { request } => toks3(Tok::Array(2), Tok::Uint(112), Tok::Uint(*request)), Msg::StyleFailed { request } => toks3(Tok::Array(2), Tok::Uint(113), Tok::Uint(*request)), + Msg::FetchScript { request, url } => toks4(Tok::Array(3), Tok::Uint(16), Tok::Uint(*request), Tok::Text(copy(url.as_slice()))), + Msg::UpdateUrl { replace, url } => toks4(Tok::Array(3), Tok::Uint(17), Tok::Bool(*replace), Tok::Text(copy(url.as_slice()))), + Msg::ScriptResponse { request, content_type } => toks4(Tok::Array(3), Tok::Uint(115), Tok::Uint(*request), Tok::Bytes(copy(content_type.as_slice()))), + Msg::ScriptBody { request, bytes } => toks4(Tok::Array(3), Tok::Uint(116), Tok::Uint(*request), Tok::Bytes(copy(bytes.as_slice()))), + Msg::ScriptEnd { request } => toks3(Tok::Array(2), Tok::Uint(117), Tok::Uint(*request)), + Msg::ScriptFailed { request } => toks3(Tok::Array(2), Tok::Uint(118), Tok::Uint(*request)), } } diff --git a/crates/clazel-ipc/src/message/examples.rs b/crates/clazel-ipc/src/message/examples.rs index efb9f03f0..e2c0f3668 100644 --- a/crates/clazel-ipc/src/message/examples.rs +++ b/crates/clazel-ipc/src/message/examples.rs @@ -358,4 +358,33 @@ pub proof fn lemma_example_style() assert(decode_renderer_message(seq![0x82u8, 0x18, 0x70, 0x01]) is None) by (compute_only); } +/// `FetchScript(1, "s.js")` is `[16, 1, "s.js"]` and `UpdateUrl(true, "/a")` +/// is `[17, true, "/a"]`, both from a renderer only; `ScriptResponse(1, +/// h'74')` is `[115, 1, h'74']`, `ScriptBody(1, h'61')` `[116, 1, h'61']`, +/// `ScriptEnd(1)` `[117, 1]` and `ScriptFailed(1)` `[118, 1]` (item 7.16). +pub proof fn lemma_example_script() + ensures + decode_renderer_message(seq![0x83u8, 0x10, 0x01, 0x64, 0x73, 0x2E, 0x6A, 0x73]) == Some(Message::FetchScript { request: 1, url: seq![0x73u8, 0x2E, 0x6A, 0x73] }), + encode_message(Message::FetchScript { request: 1, url: seq![0x73u8, 0x2E, 0x6A, 0x73] }) == seq![0x83u8, 0x10, 0x01, 0x64, 0x73, 0x2E, 0x6A, 0x73], + decode_renderer_message(seq![0x83u8, 0x11, 0xF5, 0x62, 0x2F, 0x61]) == Some(Message::UpdateUrl { replace: true, url: seq![0x2Fu8, 0x61] }), + encode_message(Message::UpdateUrl { replace: false, url: seq![0x2Fu8, 0x61] }) == seq![0x83u8, 0x11, 0xF4, 0x62, 0x2F, 0x61], + decode_message(seq![0x83u8, 0x18, 0x73, 0x01, 0x41, 0x74]) == Some(Message::ScriptResponse { request: 1, content_type: seq![0x74u8] }), + decode_message(seq![0x83u8, 0x18, 0x74, 0x01, 0x41, 0x61]) == Some(Message::ScriptBody { request: 1, bytes: seq![0x61u8] }), + decode_message(seq![0x82u8, 0x18, 0x75, 0x01]) == Some(Message::ScriptEnd { request: 1 }), + decode_message(seq![0x82u8, 0x18, 0x76, 0x01]) == Some(Message::ScriptFailed { request: 1 }), + decode_renderer_message(seq![0x82u8, 0x18, 0x75, 0x01]) is None, + decode_renderer_message(seq![0x83u8, 0x18, 0x74, 0x01, 0x41, 0x61]) is None, +{ + assert(decode_renderer_message(seq![0x83u8, 0x10, 0x01, 0x64, 0x73, 0x2E, 0x6A, 0x73]) == Some(Message::FetchScript { request: 1, url: seq![0x73u8, 0x2E, 0x6A, 0x73] })) by (compute_only); + assert(encode_message(Message::FetchScript { request: 1, url: seq![0x73u8, 0x2E, 0x6A, 0x73] }) == seq![0x83u8, 0x10, 0x01, 0x64, 0x73, 0x2E, 0x6A, 0x73]) by (compute_only); + assert(decode_renderer_message(seq![0x83u8, 0x11, 0xF5, 0x62, 0x2F, 0x61]) == Some(Message::UpdateUrl { replace: true, url: seq![0x2Fu8, 0x61] })) by (compute_only); + assert(encode_message(Message::UpdateUrl { replace: false, url: seq![0x2Fu8, 0x61] }) == seq![0x83u8, 0x11, 0xF4, 0x62, 0x2F, 0x61]) by (compute_only); + assert(decode_message(seq![0x83u8, 0x18, 0x73, 0x01, 0x41, 0x74]) == Some(Message::ScriptResponse { request: 1, content_type: seq![0x74u8] })) by (compute_only); + assert(decode_message(seq![0x83u8, 0x18, 0x74, 0x01, 0x41, 0x61]) == Some(Message::ScriptBody { request: 1, bytes: seq![0x61u8] })) by (compute_only); + assert(decode_message(seq![0x82u8, 0x18, 0x75, 0x01]) == Some(Message::ScriptEnd { request: 1 })) by (compute_only); + assert(decode_message(seq![0x82u8, 0x18, 0x76, 0x01]) == Some(Message::ScriptFailed { request: 1 })) by (compute_only); + assert(decode_renderer_message(seq![0x82u8, 0x18, 0x75, 0x01]) is None) by (compute_only); + assert(decode_renderer_message(seq![0x83u8, 0x18, 0x74, 0x01, 0x41, 0x61]) is None) by (compute_only); +} + } // verus! diff --git a/crates/clazel-ipc/src/message/lemmas.rs b/crates/clazel-ipc/src/message/lemmas.rs index 698f3e7b5..a055c4c9b 100644 --- a/crates/clazel-ipc/src/message/lemmas.rs +++ b/crates/clazel-ipc/src/message/lemmas.rs @@ -234,6 +234,46 @@ pub proof fn lemma_style_body_fits_frame(request: u64, bytes: Seq) assert(crate::cbor::spec::encode_head(2, bytes.len()).len() <= 5); } +/// A `ScriptBody` of at most [`max_script_chunk`] octets can be sent, and its +/// bytes fit one frame (item 7.16), whatever its request id. +pub proof fn lemma_script_body_fits_frame(request: u64, bytes: Seq) + requires + bytes.len() <= max_script_chunk(), + ensures + message_ok(Message::ScriptBody { request, bytes }), + crate::frame::spec::payload_ok(encode_message(Message::ScriptBody { request, bytes })), +{ + let t = seq![Token::Array(3), Token::Uint(116), Token::Uint(request), Token::Bytes(bytes)]; + let rest = seq![Token::Uint(request), Token::Bytes(bytes)]; + assert(t =~= seq![Token::Array(3), Token::Uint(116)] + rest); + lemma_items_head(3, 116, rest); + lemma_items_step(rest, 2); + assert(rest[0] == Token::Uint(request)); + assert(rest.drop_first() =~= seq![Token::Bytes(bytes)]); + lemma_items_step(seq![Token::Bytes(bytes)], 1); + assert(seq![Token::Bytes(bytes)][0] == Token::Bytes(bytes)); + assert(seq![Token::Bytes(bytes)].drop_first() =~= Seq::::empty()); + assert(crate::cbor::spec::token_ok(Token::Bytes(bytes))); + assert(crate::cbor::spec::token_ok(Token::Uint(request))); + assert(crate::cbor::spec::is_items(Seq::::empty(), 0)); + reveal_with_fuel(crate::cbor::spec::encode, 5); + assert(t.drop_first() =~= seq![Token::Uint(116), Token::Uint(request), Token::Bytes(bytes)]); + assert(t.drop_first().drop_first() =~= rest); + assert(t.drop_first().drop_first().drop_first() =~= seq![Token::Bytes(bytes)]); + assert(t.drop_first().drop_first().drop_first().drop_first() =~= Seq::::empty()); + crate::cbor::lemmas::lemma_be_bytes_len(116, 1); + crate::cbor::lemmas::lemma_be_bytes_len(request as nat, 1); + crate::cbor::lemmas::lemma_be_bytes_len(request as nat, 2); + crate::cbor::lemmas::lemma_be_bytes_len(request as nat, 4); + crate::cbor::lemmas::lemma_be_bytes_len(request as nat, 8); + crate::cbor::lemmas::lemma_be_bytes_len(bytes.len(), 1); + crate::cbor::lemmas::lemma_be_bytes_len(bytes.len(), 2); + crate::cbor::lemmas::lemma_be_bytes_len(bytes.len(), 4); + assert(crate::cbor::spec::encode(Seq::::empty()) == Seq::::empty()); + assert(crate::cbor::spec::encode_head(0, request as nat).len() <= 9); + assert(crate::cbor::spec::encode_head(2, bytes.len()).len() <= 5); +} + /// The tokens of a sendable message parse back to it. pub proof fn lemma_parse_message_tokens(m: Message) requires @@ -290,6 +330,12 @@ pub proof fn lemma_parse_message_tokens(m: Message) Message::StyleBody { request, bytes } => { assert(t.subrange(2, 4) =~= seq![Token::Uint(request), Token::Bytes(bytes)]); }, Message::StyleEnd { request } => { assert(t.subrange(2, 3) =~= seq![Token::Uint(request)]); }, Message::StyleFailed { request } => { assert(t.subrange(2, 3) =~= seq![Token::Uint(request)]); }, + Message::FetchScript { request, url } => { assert(t.subrange(2, 4) =~= seq![Token::Uint(request), Token::Text(url)]); }, + Message::UpdateUrl { replace, url } => { assert(t.subrange(2, 4) =~= seq![Token::Bool(replace), Token::Text(url)]); }, + Message::ScriptResponse { request, content_type } => { assert(t.subrange(2, 4) =~= seq![Token::Uint(request), Token::Bytes(content_type)]); }, + Message::ScriptBody { request, bytes } => { assert(t.subrange(2, 4) =~= seq![Token::Uint(request), Token::Bytes(bytes)]); }, + Message::ScriptEnd { request } => { assert(t.subrange(2, 3) =~= seq![Token::Uint(request)]); }, + Message::ScriptFailed { request } => { assert(t.subrange(2, 3) =~= seq![Token::Uint(request)]); }, } } diff --git a/crates/clazel-ipc/src/message/spec.rs b/crates/clazel-ipc/src/message/spec.rs index 559a0eeb6..c8ff1f32f 100644 --- a/crates/clazel-ipc/src/message/spec.rs +++ b/crates/clazel-ipc/src/message/spec.rs @@ -22,6 +22,8 @@ //! | 10 | renderer to kernel | `Display` | title (text), text (text), links (array of text) | //! | 11 | renderer to kernel | `Title` | title (text) | //! | 14 | renderer to kernel | `FetchStyle` | request (unsigned), url (text) | +//! | 16 | renderer to kernel | `FetchScript` | request (unsigned), url (text) | +//! | 17 | renderer to kernel | `UpdateUrl` | replace (bool), url (text) | //! | 15 | GPU process to kernel | `FrameReady` | buffer (unsigned), width (unsigned), height (unsigned) | //! | 18 | GPU process to kernel | `GpuReady` | software (bool) | //! | 19 | GPU process to kernel | `GpuRefused` | | @@ -39,6 +41,10 @@ //! | 112 | kernel to renderer | `StyleEnd` | request (unsigned) | //! | 113 | kernel to renderer | `StyleFailed` | request (unsigned) | //! | 114 | kernel to GPU process | `GpuViewport` | width (unsigned), height (unsigned), scale (unsigned) | +//! | 115 | kernel to renderer | `ScriptResponse` | request (unsigned), content type (bytes) | +//! | 116 | kernel to renderer | `ScriptBody` | request (unsigned), bytes (bytes, at most [`max_script_chunk`]) | +//! | 117 | kernel to renderer | `ScriptEnd` | request (unsigned) | +//! | 118 | kernel to renderer | `ScriptFailed` | request (unsigned) | //! //! `StorageLengthValue` is not in the plan's list, which has no reply for //! `StorageLength`; the schema needs one. @@ -78,6 +84,20 @@ //! `StyleFailed`, each naming that `request`. The request id is the //! renderer's own number for its fetch, not an identity: it names nobody. //! +//! Phase 7 item 7.16 adds the script fetch and the URL update. +//! `FetchScript` has 5.19's request shape: a committed renderer asks the +//! kernel to fetch `url` (resolved against its committed URL) under +//! `request`, an id the renderer chose, and the kernel answers with +//! `ScriptResponse`, zero or more `ScriptBody` and `ScriptEnd`, or with +//! `ScriptFailed`, each naming that `request`, exactly as it does for +//! `FetchStyle` (the two differ only in their tags, so that a renderer never +//! reads a script's bytes as a sheet's). `UpdateUrl` is `pushState` (`replace` +//! false) and `replaceState` (`replace` true): it names the URL the renderer +//! wants its document to have, as written (resolved by the kernel against +//! the committed URL, as `Navigate`'s is), and the kernel decides whether the +//! address bar changes. The tags 16 and 17 are the first free ones below 100; +//! 115 to 118 follow 114. +//! //! Two rules are part of the schema's spec ([`message_fields`], and //! `lemmas::lemma_no_identity_fields`): //! - **No message carries a claimed identity.** No origin, renderer id, tab @@ -242,6 +262,46 @@ pub enum Message { /// The renderer's id for the fetch. request: u64, }, + /// Renderer to kernel: fetch a script (item 7.16), with 5.19's request + /// shape. + FetchScript { + /// The renderer's id for this fetch. + request: u64, + /// The script's URL, as the document wrote it. + url: Seq, + }, + /// Renderer to kernel: `history.pushState` or `history.replaceState` + /// (item 7.16). + UpdateUrl { + /// Whether it is `replaceState` (`pushState` otherwise). + replace: bool, + /// The URL the document asks to have, as the script wrote it. + url: Seq, + }, + /// Kernel to renderer: the script's response arrived (item 7.16). + ScriptResponse { + /// The renderer's id for the fetch. + request: u64, + /// The response's combined `Content-Type` field value, or empty. + content_type: Seq, + }, + /// Kernel to renderer: the next part of the script's body. + ScriptBody { + /// The renderer's id for the fetch. + request: u64, + /// The octets. + bytes: Seq, + }, + /// Kernel to renderer: the script's body is complete. + ScriptEnd { + /// The renderer's id for the fetch. + request: u64, + }, + /// Kernel to renderer: the script fetch failed (or was refused). + ScriptFailed { + /// The renderer's id for the fetch. + request: u64, + }, /// Kernel to renderer: a document is committed; its body follows in /// `Body` frames, then `BodyEnd`. Commit { @@ -332,6 +392,16 @@ pub open spec fn max_style_chunk() -> nat { (crate::frame::spec::MAX_FRAME - 17) as nat } +/// The most octets one `ScriptBody` carries: `MAX_FRAME` minus the rest of +/// its payload, which is the array's head (1 octet), the tag 116 (2 octets), +/// the request id (at most 9 octets) and the byte string's head (at most 5 +/// octets below 2^32), so that every `ScriptBody` of at most this many octets +/// fits one frame (item 7.16). That is [`max_style_chunk`] (a `StyleBody`'s +/// payload has the same shape, with the tag 111 in place of 116). +pub open spec fn max_script_chunk() -> nat { + max_style_chunk() +} + /// Whether `m` is sent by a renderer to the kernel. pub open spec fn is_from_renderer(m: Message) -> bool { match m { @@ -344,6 +414,8 @@ pub open spec fn is_from_renderer(m: Message) -> bool { | Message::StorageKey { .. } | Message::StorageLength | Message::Navigate { .. } + | Message::FetchScript { .. } + | Message::UpdateUrl { .. } | Message::Display { .. } | Message::Title { .. } | Message::FetchStyle { .. } => true, @@ -411,8 +483,11 @@ pub enum Field { Delta, /// A key code. KeyCode, - /// The renderer's own id for a style sheet fetch (item 5.19). + /// The renderer's own id for a style sheet or script fetch (items 5.19 + /// and 7.16). Request, + /// Whether a URL update replaces the current history entry (item 7.16). + Replace, /// A claimed origin. No message has one. Origin, /// A renderer id. No message has one. @@ -459,6 +534,12 @@ pub open spec fn message_fields(m: Message) -> Seq { Message::StyleBody { .. } => seq![Field::Request, Field::BodyBytes], Message::StyleEnd { .. } => seq![Field::Request], Message::StyleFailed { .. } => seq![Field::Request], + Message::FetchScript { .. } => seq![Field::Request, Field::Url], + Message::UpdateUrl { .. } => seq![Field::Replace, Field::Url], + Message::ScriptResponse { .. } => seq![Field::Request, Field::ContentType], + Message::ScriptBody { .. } => seq![Field::Request, Field::BodyBytes], + Message::ScriptEnd { .. } => seq![Field::Request], + Message::ScriptFailed { .. } => seq![Field::Request], } } @@ -534,6 +615,12 @@ pub open spec fn message_tokens(m: Message) -> Seq { Message::StyleBody { request, bytes } => seq![Token::Array(3), Token::Uint(111), Token::Uint(request), Token::Bytes(bytes)], Message::StyleEnd { request } => seq![Token::Array(2), Token::Uint(112), Token::Uint(request)], Message::StyleFailed { request } => seq![Token::Array(2), Token::Uint(113), Token::Uint(request)], + Message::FetchScript { request, url } => seq![Token::Array(3), Token::Uint(16), Token::Uint(request), Token::Text(url)], + Message::UpdateUrl { replace, url } => seq![Token::Array(3), Token::Uint(17), Token::Bool(replace), Token::Text(url)], + Message::ScriptResponse { request, content_type } => seq![Token::Array(3), Token::Uint(115), Token::Uint(request), Token::Bytes(content_type)], + Message::ScriptBody { request, bytes } => seq![Token::Array(3), Token::Uint(116), Token::Uint(request), Token::Bytes(bytes)], + Message::ScriptEnd { request } => seq![Token::Array(2), Token::Uint(117), Token::Uint(request)], + Message::ScriptFailed { request } => seq![Token::Array(2), Token::Uint(118), Token::Uint(request)], } } @@ -583,6 +670,20 @@ pub open spec fn parse_fields(tag: u64, f: Seq) -> Option { _ => None, } } else { None } + } else if tag == 16 { + if f.len() == 2 { + match (f[0], f[1]) { + (Token::Uint(r), Token::Text(u)) => Some(Message::FetchScript { request: r, url: u }), + _ => None, + } + } else { None } + } else if tag == 17 { + if f.len() == 2 { + match (f[0], f[1]) { + (Token::Bool(b), Token::Text(u)) => Some(Message::UpdateUrl { replace: b, url: u }), + _ => None, + } + } else { None } } else if tag == 18 { if f.len() == 1 { match f[0] { Token::Bool(b) => Some(Message::GpuReady { software: b }), _ => None } } else { None } } else if tag == 19 { @@ -647,6 +748,24 @@ pub open spec fn parse_fields(tag: u64, f: Seq) -> Option { if f.len() == 1 { match f[0] { Token::Uint(r) => Some(Message::StyleEnd { request: r }), _ => None } } else { None } } else if tag == 113 { if f.len() == 1 { match f[0] { Token::Uint(r) => Some(Message::StyleFailed { request: r }), _ => None } } else { None } + } else if tag == 115 { + if f.len() == 2 { + match (f[0], f[1]) { + (Token::Uint(r), Token::Bytes(c)) => Some(Message::ScriptResponse { request: r, content_type: c }), + _ => None, + } + } else { None } + } else if tag == 116 { + if f.len() == 2 { + match (f[0], f[1]) { + (Token::Uint(r), Token::Bytes(b)) => Some(Message::ScriptBody { request: r, bytes: b }), + _ => None, + } + } else { None } + } else if tag == 117 { + if f.len() == 1 { match f[0] { Token::Uint(r) => Some(Message::ScriptEnd { request: r }), _ => None } } else { None } + } else if tag == 118 { + if f.len() == 1 { match f[0] { Token::Uint(r) => Some(Message::ScriptFailed { request: r }), _ => None } } else { None } } else if tag == 114 { if f.len() == 3 { match (f[0], f[1], f[2]) { diff --git a/crates/clazel-ipc/tests/messages.rs b/crates/clazel-ipc/tests/messages.rs index 9b19eb1cd..4b7086c28 100644 --- a/crates/clazel-ipc/tests/messages.rs +++ b/crates/clazel-ipc/tests/messages.rs @@ -127,6 +127,28 @@ fn all() -> Vec { }, Msg::StyleEnd { request: 7 }, Msg::StyleFailed { request: 0 }, + Msg::FetchScript { + request: 9, + url: t("app.js?v=2"), + }, + Msg::UpdateUrl { + replace: false, + url: t("/a/b?c#d"), + }, + Msg::UpdateUrl { + replace: true, + url: t(""), + }, + Msg::ScriptResponse { + request: u64::MAX, + content_type: t("text/javascript"), + }, + Msg::ScriptBody { + request: 9, + bytes: vec![0x00, 0xFF], + }, + Msg::ScriptEnd { request: 9 }, + Msg::ScriptFailed { request: 0 }, ] } @@ -147,6 +169,23 @@ fn a_largest_style_body_fits_one_frame() { assert!(encode_framed(&over).is_none()); } +#[test] +fn a_largest_script_body_fits_one_frame() { + use clazel_ipc::message::codec::MAX_SCRIPT_CHUNK; + let m = Msg::ScriptBody { + request: u64::MAX, + bytes: vec![0x5A; MAX_SCRIPT_CHUNK], + }; + let frame = encode_framed(&m).unwrap_or_default(); + assert_eq!(frame.len(), MAX_FRAME + 4); + assert_eq!(decode_framed(&frame), Some(m_clone(&m))); + let over = Msg::ScriptBody { + request: u64::MAX, + bytes: vec![0x5A; MAX_SCRIPT_CHUNK + 1], + }; + assert!(encode_framed(&over).is_none()); +} + #[test] fn every_message_round_trips_through_a_frame() { for m in all() { @@ -181,6 +220,8 @@ fn the_kernel_decoder_takes_only_renderer_messages() { | Msg::Display { .. } | Msg::Title { .. } | Msg::FetchStyle { .. } + | Msg::FetchScript { .. } + | Msg::UpdateUrl { .. } ); assert_eq!( decode_renderer_message(&bytes).is_some(), diff --git a/crates/clazel-jit/Cargo.toml b/crates/clazel-jit/Cargo.toml new file mode 100644 index 000000000..5d0ed3c33 --- /dev/null +++ b/crates/clazel-jit/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "clazel-jit" +version = "0.0.0" +edition.workspace = true +license.workspace = true +publish.workspace = true +description = "The JIT's back ends (docs/phases/jit.md item J.9): LIR to x86-64 with no register allocation, each LIR instruction a fixed template over scratch registers, with a Verus refinement proof per template. First slice: straight-line integer code, for x86-64 (crate root) and A64 (`a64`). A JS front end (bytecode to LIR, `front`), first ops only. Also the code cache, its budget and the tiering policy (J.11, first slice; nothing mapped or entered yet)." + +[dependencies] +vstd = "=0.0.0-2026-10-04-0306" +# The source language, the target instruction subset, and the validator whose +# ABI (context register, region table, exit sequence) the templates follow. +clazel-lir = { path = "../clazel-lir" } +# The source of the front end: the bytecode and its semantics (`step`). +clazel-js-vm = { path = "../clazel-js-vm" } +clazel-x64 = { path = "../clazel-x64" } +clazel-a64 = { path = "../clazel-a64" } +clazel-codecheck = { path = "../clazel-codecheck" } + +# Verification tier (docs/VERIFICATION.md). `tier` is what is proven about the +# shipped code today: nothing is claimed until CI's full verification of this +# crate is green (CLAUDE.md rule 7), and the crate has no independent +# transcription or mutation file yet. `target-tier` is what jit.md's "Crates" +# table sets. +[package.metadata.clazel] +tier = 0 +target-tier = 2 + +[package.metadata.verus] +verify = true + +[lints] +workspace = true diff --git a/crates/clazel-jit/mutations.json b/crates/clazel-jit/mutations.json new file mode 100644 index 000000000..c2be2637d --- /dev/null +++ b/crates/clazel-jit/mutations.json @@ -0,0 +1,629 @@ +{ + "mutations": [ + { + "id": "setcc-less-than-uses-wrong-condition", + "file": "src/templates.rs", + "find": "lir::ICmp::LtS => 12,", + "replace": "lir::ICmp::LtS => 13,", + "expect": "killed-incidentally", + "reason": "The signed less-than comparison would read condition code 13 (`ge`) instead of 12 (`l`). The refinement of the comparison template fails (`flags::lemma_cc_icmp`, then `mid::lemma_mid_cmp`), because the code would compute the opposite of LIR's `lt_s`. It is not labelled `killed`: that needs a certificate, a concrete witness evaluated with `compute_only`, and the refinement statement is quantified over states, which `compute_only` cannot evaluate. So this claims no coverage, though the proof does catch the bug." + }, + { + "id": "select-takes-the-wrong-arm", + "file": "src/templates.rs", + "find": "x64::Instr::Cmovcc { cc: 4, sz: x64::Size::W64, dst: rax(), src: rcx() },", + "replace": "x64::Instr::Cmovcc { cc: 5, sz: x64::Size::W64, dst: rax(), src: rcx() },", + "expect": "killed-incidentally", + "reason": "`select` would take its second value when the condition is nonzero (`cmovne` for `cmove`). `mid::lemma_mid_select` fails, and with it the refinement of `select`. No certificate, for the reason of the first mutation, so no coverage is claimed." + }, + { + "id": "frame-overlaps-the-exit-code-word", + "file": "src/spec.rs", + "find": " abi::exit_code_off() + 8\n}", + "replace": " abi::exit_code_off()\n}", + "expect": "killed-incidentally", + "reason": "Frame slot 0 would be the exit-code word, so an exit sequence would overwrite a local. `steps::lemma_abi_consts` (which pins the frame to bytes 264 and up, and 31 slots) fails, and so do the proofs that rest on it. No certificate: the theorem that fails is about the constants, and `compute_only` would need a witness that is itself the mutated constant; no coverage is claimed." + }, + { + "id": "regions-may-overlap-the-frame", + "file": "src/spec.rs", + "find": " forall|a: nat| #[trigger] in_regions(lay, a) ==> !in_exit_area(ctxb, a as int)", + "replace": " true", + "expect": "killed-incidentally", + "reason": "A region may then overlap the context's exit area, where the frame is, so a store to a slot could change a byte LIR's memory holds. `rel::lemma_store_finish` fails (it needs the region bytes outside the exit area to be unchanged by the store). No certificate (the statement is quantified), so no coverage is claimed." + }, + { + "id": "preserved-forgets-the-stack-pointer", + "file": "src/spec.rs", + "find": " preserved_mem(m, m2) && m2.rgf[abi::sp_reg() as int] == m.rgf[abi::sp_reg() as int]\n}", + "replace": " preserved_mem(m, m2)\n}", + "expect": "killed-incidentally", + "reason": "Drops the claim that a template leaves `rsp` as it was. Every template still leaves `rsp` unchanged, so no theorem becomes false for the mutant; but the control-flow refinement (`cfspec::cf_refines`) states `preserved_r`, which includes `rsp`, and `cfop::lemma_preserved_to_r` derives it from `preserved`, so that derivation no longer goes through. Only a proof step breaks: not coverage. A certified kill needs a theorem the mutant falsifies, which `rsp` preservation by the code between exits (the confinement theorem, jit.md exit criterion 2) will give once the back end is tied to it." + }, + { + "id": "a64-cset-signed-less-than-uses-wrong-condition", + "module": "a64::flags", + "file": "src/a64/templates.rs", + "find": " lir::ICmp::LtS => 10,", + "replace": " lir::ICmp::LtS => 11,", + "expect": "killed", + "reason": "The A64 template for a signed less-than comparison would give `cset` the condition `lt` (11) instead of its inverse `ge` (10) (a `cset` is `csinc` on the inverse of the condition that sets the result), so it would return 0 where LIR's `lt_s` is 1: after `cmp 1, 2` the `N` and `V` flags differ, so `lt` holds. `flags::lemma_cc_icmp_blank32` is false for the mutant at `lt_s`, 32 bits, 1 and 2.", + "certificate": { + "file": "src/a64/flags.rs", + "lemma": "lemma_cc_icmp_blank32(lir::ICmp::LtS, 1, 2)", + "witness": "isa::condition_holds(isa::set_nzcv(blank(), cmp_nzcv(32, 1, 2)), super::templates::cset_cond(lir::ICmp::LtS)) == true && lir::icmp(lir::ICmp::LtS, 32, 1, 2) == true", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "a64-cset-equal-uses-wrong-condition", + "module": "a64::flags", + "file": "src/a64/templates.rs", + "find": " lir::ICmp::Eq => 1,", + "replace": " lir::ICmp::Eq => 0,", + "expect": "killed", + "reason": "The A64 template for `eq` would give `cset` the condition `eq` (0) instead of its inverse `ne` (1), so it would return 0 where LIR's `eq` is 1: after `cmp 1, 1` the `Z` flag is set. `flags::lemma_cc_icmp_blank32` is false for the mutant at `eq`, 32 bits, 1 and 1.", + "certificate": { + "file": "src/a64/flags.rs", + "lemma": "lemma_cc_icmp_blank32(lir::ICmp::Eq, 1, 1)", + "witness": "isa::condition_holds(isa::set_nzcv(blank(), cmp_nzcv(32, 1, 1)), super::templates::cset_cond(lir::ICmp::Eq)) == true && lir::icmp(lir::ICmp::Eq, 32, 1, 1) == true", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "a64-cset-unsigned-less-than-uses-wrong-condition", + "module": "a64::flags", + "file": "src/a64/templates.rs", + "find": " lir::ICmp::LtU => 2,", + "replace": " lir::ICmp::LtU => 3,", + "expect": "killed", + "reason": "The A64 template for an unsigned less-than comparison would give `cset` the condition `lo` (3) instead of its inverse `hs` (2), so it would return 0 where LIR's `lt_u` is 1: after `cmp 1, 2` the carry is clear, so `lo` holds. `flags::lemma_cc_icmp_blank32` is false for the mutant at `lt_u`, 32 bits, 1 and 2.", + "certificate": { + "file": "src/a64/flags.rs", + "lemma": "lemma_cc_icmp_blank32(lir::ICmp::LtU, 1, 2)", + "witness": "isa::condition_holds(isa::set_nzcv(blank(), cmp_nzcv(32, 1, 2)), super::templates::cset_cond(lir::ICmp::LtU)) == true && lir::icmp(lir::ICmp::LtU, 32, 1, 2) == true", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "a64-cset-unsigned-greater-than-uses-wrong-condition", + "module": "a64::flags", + "file": "src/a64/templates.rs", + "find": " lir::ICmp::GtU => 9,", + "replace": " lir::ICmp::GtU => 8,", + "expect": "killed", + "reason": "The A64 template for an unsigned greater-than comparison would give `cset` the condition `hi` (8) instead of its inverse `ls` (9), so it would return 0 where LIR's `gt_u` is 1: after `cmp 2, 1` the carry is set and `Z` clear, so `hi` holds. `flags::lemma_cc_icmp_blank32` is false for the mutant at `gt_u`, 32 bits, 2 and 1.", + "certificate": { + "file": "src/a64/flags.rs", + "lemma": "lemma_cc_icmp_blank32(lir::ICmp::GtU, 2, 1)", + "witness": "isa::condition_holds(isa::set_nzcv(blank(), cmp_nzcv(32, 2, 1)), super::templates::cset_cond(lir::ICmp::GtU)) == true && lir::icmp(lir::ICmp::GtU, 32, 2, 1) == true", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "a64-chunk-one-uses-the-wrong-divisor", + "module": "a64::bitmid", + "file": "src/a64/templates.rs", + "find": " ((v / 0x1_0000) % 0x1_0000) as u16", + "replace": " ((v / 0x2_0000) % 0x1_0000) as u16", + "expect": "killed", + "reason": "The second 16-bit chunk of a constant (the `movk ..., lsl #16` immediate) would be taken from bit 17 instead of bit 16, so the four chunks no longer add up to the word: for 0x10000 the chunks would all be 0. `bitmid::lemma_chunks` is false for the mutant at 0x10000.", + "certificate": { + "file": "src/a64/bitmid.rs", + "lemma": "lemma_chunks(0x1_0000u64)", + "witness": "super::templates::chunk(0x1_0000u64, 1) == 0u16" + } + }, + { + "id": "a64-neg-loads-the-wrong-mask", + "module": "a64::bitmid", + "file": "src/a64/templates.rs", + "find": " imm16: 0x8000,", + "replace": " imm16: 0x4000,", + "expect": "killed", + "reason": "The `movz` of the `f64.neg` template would load 2^62 instead of the sign bit 2^63 into `x1`, so `eor` would flip the wrong bit. `bitmid::lemma_neg_template_mask` (which `lemma_mid_neg` rests on) is false for the mutant.", + "certificate": { + "file": "src/a64/bitmid.rs", + "lemma": "lemma_neg_template_mask()", + "witness": "(match super::templates::neg_mid()[0] { isa::Instr::MoveWide { sf, opcode, d, imm16, hw } => chunk_val(imm16, hw) == 0x4000_0000_0000_0000int, _ => false }) == true" + } + }, + { + "id": "a64-slot-immediate-scaled-by-four", + "module": "a64::steps", + "file": "src/a64/spec.rs", + "find": " (slot_disp(j) / 8) as u16", + "replace": " (slot_disp(j) / 4) as u16", + "expect": "killed", + "reason": "The `imm12` of a frame slot's `ldr`/`str` is the displacement divided by the access size, 8; divided by 4 every slot access would land at twice the offset (slot 0 at 528, outside the context). `steps::lemma_slot_imm` is false for the mutant at slot 0.", + "certificate": { + "file": "src/a64/steps.rs", + "lemma": "lemma_slot_imm(0)", + "witness": "slot_imm(0) == 66u16" + } + }, + { + "id": "mem-access-index-is-scaled", + "module": "cfmemvalid", + "file": "src/cftemplates.rs", + "find": "index: Some(x64::Index { reg: rcx(), scale: 0 })", + "replace": "index: Some(x64::Index { reg: rcx(), scale: 1 })", + "expect": "killed", + "reason": "The access `[rsi + 2 rcx]` would scale the checked index, which reaches past the region; the validator refuses any scale but 1. `cfmemvalid::lemma_access_accepted` (the validator's `access_ok` admits `[rsi + rcx]` after the check) is false for the mutant at region 0, 8 bytes, a load.", + "certificate": { + "file": "src/cfmemvalid.rs", + "lemma": "lemma_access_accepted(0, 8, 8, false)", + "witness": "!clazel_codecheck::spec::access_ok(seq![clazel_codecheck::spec::Binding { reg: 6, fact: clazel_codecheck::spec::Fact::Base(0) }, clazel_codecheck::spec::Binding { reg: 2, fact: clazel_codecheck::spec::Fact::Unknown }, clazel_codecheck::spec::Binding { reg: 0, fact: clazel_codecheck::spec::Fact::Unknown }, clazel_codecheck::spec::Binding { reg: 1, fact: clazel_codecheck::spec::Fact::Idx(0, 8) }], clazel_codecheck::spec::Access { mem: ix_mem(), size: 8, store: false })" + } + }, + { + "id": "mem-access-base-register", + "module": "cfmemvalid", + "file": "src/cftemplates.rs", + "find": "x64::Mem { base: rsi(), index: Some(", + "replace": "x64::Mem { base: rdi(), index: Some(", + "expect": "killed", + "reason": "The access would use `rdi` as the base, which is not known to hold the region's base. `cfmemvalid::lemma_access_accepted` (the validator's `access_ok` admits `[rsi + rcx]` after the check) is false for the mutant at region 0, 8 bytes, a load.", + "certificate": { + "file": "src/cfmemvalid.rs", + "lemma": "lemma_access_accepted(0, 8, 8, false)", + "witness": "!clazel_codecheck::spec::access_ok(seq![clazel_codecheck::spec::Binding { reg: 6, fact: clazel_codecheck::spec::Fact::Base(0) }, clazel_codecheck::spec::Binding { reg: 2, fact: clazel_codecheck::spec::Fact::Unknown }, clazel_codecheck::spec::Binding { reg: 0, fact: clazel_codecheck::spec::Fact::Unknown }, clazel_codecheck::spec::Binding { reg: 1, fact: clazel_codecheck::spec::Fact::Idx(0, 8) }], clazel_codecheck::spec::Access { mem: ix_mem(), size: 8, store: false })" + } + }, + { + "id": "mem-access-index-register", + "module": "cfmemvalid", + "file": "src/cftemplates.rs", + "find": "Index { reg: rcx(), scale: 0 }", + "replace": "Index { reg: rdx(), scale: 0 }", + "expect": "killed", + "reason": "The access would use `rdx`, the check's scratch register, not the checked and masked index. `cfmemvalid::lemma_access_accepted` (the validator's `access_ok` admits `[rsi + rcx]` after the check) is false for the mutant at region 0, 8 bytes, a load.", + "certificate": { + "file": "src/cfmemvalid.rs", + "lemma": "lemma_access_accepted(0, 8, 8, false)", + "witness": "!clazel_codecheck::spec::access_ok(seq![clazel_codecheck::spec::Binding { reg: 6, fact: clazel_codecheck::spec::Fact::Base(0) }, clazel_codecheck::spec::Binding { reg: 2, fact: clazel_codecheck::spec::Fact::Unknown }, clazel_codecheck::spec::Binding { reg: 0, fact: clazel_codecheck::spec::Fact::Unknown }, clazel_codecheck::spec::Binding { reg: 1, fact: clazel_codecheck::spec::Fact::Idx(0, 8) }], clazel_codecheck::spec::Access { mem: ix_mem(), size: 8, store: false })" + } + }, + { + "id": "mem-access-displacement", + "module": "cfmemvalid", + "file": "src/cftemplates.rs", + "find": "scale: 0 }), disp: 0 }", + "replace": "scale: 0 }), disp: 8 }", + "expect": "killed", + "reason": "The access would have a displacement of 8, which the validator refuses (it checked `i + n`, not `i + 8 + n`). `cfmemvalid::lemma_access_accepted` (the validator's `access_ok` admits `[rsi + rcx]` after the check) is false for the mutant at region 0, 8 bytes, a load.", + "certificate": { + "file": "src/cfmemvalid.rs", + "lemma": "lemma_access_accepted(0, 8, 8, false)", + "witness": "!clazel_codecheck::spec::access_ok(seq![clazel_codecheck::spec::Binding { reg: 6, fact: clazel_codecheck::spec::Fact::Base(0) }, clazel_codecheck::spec::Binding { reg: 2, fact: clazel_codecheck::spec::Fact::Unknown }, clazel_codecheck::spec::Binding { reg: 0, fact: clazel_codecheck::spec::Fact::Unknown }, clazel_codecheck::spec::Binding { reg: 1, fact: clazel_codecheck::spec::Fact::Idx(0, 8) }], clazel_codecheck::spec::Access { mem: ix_mem(), size: 8, store: false })" + } + }, + { + "id": "i32-access-reads-eight-bytes", + "module": "cfmemvalid", + "file": "src/cftemplates.rs", + "find": "MTy::I32 => x64::Size::W32,", + "replace": "MTy::I32 => x64::Size::W64,", + "expect": "killed", + "reason": "An `i32` load or store would move 8 bytes after a check of 4, reading or overwriting 4 bytes of the next element. `cfmemvalid::lemma_mty_sz_bytes` (the operand size moves as many bytes as the type has) is false for the mutant at `i32`.", + "certificate": { + "file": "src/cfmemvalid.rs", + "lemma": "lemma_mty_sz_bytes(MTy::I32)", + "witness": "x64::nbytes(mty_sz(MTy::I32)) != MTy::I32.size()" + } + }, + { + "id": "f64-access-is-four-bytes", + "module": "cfmemvalid", + "file": "src/cfspec.rs", + "find": "MTy::F64 => 8,", + "replace": "MTy::F64 => 4,", + "expect": "killed", + "reason": "An `f64` access would be checked for 4 bytes and move 8 (in the statement's `MTy::size`, which the check's size is taken from). `cfmemvalid::lemma_mty_size_is_lir` (the access size is LIR's `ty_size`) is false for the mutant at `f64`.", + "certificate": { + "file": "src/cfmemvalid.rs", + "lemma": "lemma_mty_size_is_lir(MTy::F64)", + "witness": "lir::ty_size(MTy::F64.lir_ty()) != MTy::F64.size()" + } + }, + { + "id": "bounds-check-exits-with-zero", + "module": "cfmemvalid", + "file": "src/cftemplates.rs", + "find": "MemOp::BoundsCheck { code, .. } => code,", + "replace": "MemOp::BoundsCheck { code, .. } => 0,", + "expect": "killed", + "reason": "A failing `bounds_check` would exit with code 0, not its own code, which LIR's `Exit` carries. `cfmemvalid::lemma_bc_exit_code` is false for the mutant at code 9.", + "certificate": { + "file": "src/cfmemvalid.rs", + "lemma": "lemma_bc_exit_code(0, 1, 9)", + "witness": "mem_exit_code(MemOp::BoundsCheck { region: 0, size: 1, code: 9 }) != 9u32" + } + }, + { + "id": "br-table-chain-jumps-on-not-equal", + "module": "cfmemvalid", + "file": "src/cftemplates.rs", + "find": "jcc_i(4, rel),", + "replace": "jcc_i(5, rel),", + "expect": "killed", + "reason": "A `br_table` entry would jump when the index differs from the entry's number (`jne` for `je`), so it would take the first target for every index but 0. `cfmemvalid::lemma_brt_cc` (the entry's condition code is `eq`'s) is false for the mutant.", + "certificate": { + "file": "src/cfmemvalid.rs", + "lemma": "lemma_brt_cc(0, 0)", + "witness": "brt_entry(0, 0)[2]->Jcc_cc != crate::templates::cc_of(lir::ICmp::Eq)" + } + }, + { + "id": "emitter-max-size-differs-from-spec", + "module": "cfemit", + "file": "src/cfemit.rs", + "find": "pub const MAX_SIZE: usize = 0x2000_0000;", + "replace": "pub const MAX_SIZE: usize = 0x4000_0000;", + "expect": "killed", + "reason": "The emitter would translate bodies up to 2^30 bytes where the spec's `cf_prog_ok` stops at `max_size` (2^29), so it would return a blob for a program the contract says has no translation, and a displacement could leave the range `labels_fit` covers. `cfemit::lemma_emitter_limits` (the emitter's limit is the spec's) is false for the mutant.", + "certificate": { + "file": "src/cfemit.rs", + "lemma": "lemma_emitter_limits()", + "witness": "MAX_SIZE as nat != max_size()" + } + }, + { + "id": "emitter-max-targets-differs-from-spec", + "module": "cfemit", + "file": "src/cfemit.rs", + "find": "pub const MAX_TARGETS: usize = 0x10000;", + "replace": "pub const MAX_TARGETS: usize = 0x20000;", + "expect": "killed", + "reason": "The emitter would accept a `br_table` of up to 2^17 targets where `cf_flow` refuses more than `max_targets` (2^16). `cfemit::lemma_emitter_limits` is false for the mutant.", + "certificate": { + "file": "src/cfemit.rs", + "lemma": "lemma_emitter_limits()", + "witness": "MAX_TARGETS as nat != max_targets()" + } + }, + { + "id": "a64-load-access-index-register", + "module": "a64::cfmemvalid", + "file": "src/a64/cftemplates.rs", + "find": "load: true,\n t: x0(),\n n: x3(),\n m: x1(),", + "replace": "load: true,\n t: x0(),\n n: x3(),\n m: x2(),", + "expect": "killed", + "reason": "A load's access would index with `x2`, the check's scratch register for the bound, not the checked and masked index in `x1`. `a64::cfmemvalid::lemma_load_access_accepted` (the validator's `access_ok` admits the template's access after the check) is false for the mutant at region 0, 8 bytes.", + "certificate": { + "file": "src/a64/cfmemvalid.rs", + "lemma": "lemma_load_access_accepted()", + "witness": "!clazel_codecheck::a64::spec::access_ok(after_check(0, 8), clazel_codecheck::a64::spec::mem_access(mem_tail(MemOp::Load { region: 0, ty: MTy::I64 }, 0, 1)[0])->Some_0)" + } + }, + { + "id": "a64-load-access-is-scaled", + "module": "a64::cfmemvalid", + "file": "src/a64/cftemplates.rs", + "find": "m: x1(),\n s: false,\n },\n store_slot(", + "replace": "m: x1(),\n s: true,\n },\n store_slot(", + "expect": "killed", + "reason": "A load's index would be shifted left by the access size (`S = 1`), so it would read at `base + 8 * index`, past the checked range. `a64::cfmemvalid::lemma_load_access_accepted` is false for the mutant (the validator's `access_ok` refuses a shifted index).", + "certificate": { + "file": "src/a64/cfmemvalid.rs", + "lemma": "lemma_load_access_accepted()", + "witness": "!clazel_codecheck::a64::spec::access_ok(after_check(0, 8), clazel_codecheck::a64::spec::mem_access(mem_tail(MemOp::Load { region: 0, ty: MTy::I64 }, 0, 1)[0])->Some_0)" + } + }, + { + "id": "a64-store-access-index-register", + "module": "a64::cfmemvalid", + "file": "src/a64/cftemplates.rs", + "find": "t: x4(),\n n: x3(),\n m: x1(),", + "replace": "t: x4(),\n n: x3(),\n m: x2(),", + "expect": "killed", + "reason": "A store's access would index with `x2`, the check's scratch register for the bound, not the checked index. `a64::cfmemvalid::lemma_store_access_accepted` is false for the mutant at region 0, 8 bytes.", + "certificate": { + "file": "src/a64/cfmemvalid.rs", + "lemma": "lemma_store_access_accepted()", + "witness": "!clazel_codecheck::a64::spec::access_ok(after_check(0, 8), clazel_codecheck::a64::spec::mem_access(mem_tail(MemOp::Store { region: 0, ty: MTy::I64 }, 0, 2)[0])->Some_0)" + } + }, + { + "id": "a64-i32-access-is-64-bit", + "module": "a64::cfmemvalid", + "file": "src/a64/cftemplates.rs", + "find": "MTy::I32 => isa::Width::W32,", + "replace": "MTy::I32 => isa::Width::W64,", + "expect": "killed", + "reason": "An `i32` access would be a 64-bit `ldr`/`str` (8 bytes) while its check admits 4: it would read or write past the checked range. `a64::cfmemvalid::lemma_mty_sf_bytes` (an access moves as many bytes as its type has) is false for the mutant at `i32`.", + "certificate": { + "file": "src/a64/cfmemvalid.rs", + "lemma": "lemma_mty_sf_bytes(MTy::I32)", + "witness": "sf_bytes(mty_sf(MTy::I32)) != MTy::I32.size()" + } + }, + { + "id": "a64-bounds-check-exits-with-zero", + "module": "a64::cfmemvalid", + "file": "src/a64/cftemplates.rs", + "find": "MemOp::BoundsCheck { code, .. } => code as u16,", + "replace": "MemOp::BoundsCheck { code, .. } => 0u16,", + "expect": "killed", + "reason": "A failing `bounds_check` would exit with code 0, not its own code, which LIR's `Exit` carries. `a64::cfmemvalid::lemma_bc_exit_code` is false for the mutant at code 9.", + "certificate": { + "file": "src/a64/cfmemvalid.rs", + "lemma": "lemma_bc_exit_code(0, 1, 9)", + "witness": "mem_exit_code(MemOp::BoundsCheck { region: 0, size: 1, code: 9 }) as u32 != 9u32" + } + }, + { + "id": "a64-br-table-chain-branches-on-not-equal", + "module": "a64::cfmemvalid", + "file": "src/a64/cftemplates.rs", + "find": "bcond_i(0, rel),", + "replace": "bcond_i(1, rel),", + "expect": "killed", + "reason": "A `br_table` entry would branch when the index differs from the entry's number (`b.ne` for `b.eq`), so it would take the first target for every index but 0. `a64::cfmemvalid::lemma_brt_cond` (the entry's condition is `eq`, 0) is false for the mutant.", + "certificate": { + "file": "src/a64/cfmemvalid.rs", + "lemma": "lemma_brt_cond(0, 0)", + "witness": "brt_entry(0, 0)[2]->BCond_cond != 0u8" + } + }, + { + "id": "a64-branch-displacement-is-in-bytes", + "module": "a64::cfmemvalid", + "file": "src/a64/cftemplates.rs", + "find": "isa::Instr::B { imm26: (rel / 4) as i32 }", + "replace": "isa::Instr::B { imm26: (rel / 2) as i32 }", + "expect": "killed", + "reason": "A `b` would carry half the byte displacement where the machine scales its field by 4, so every branch would go twice as far. `a64::cfmemvalid::lemma_b_words` (a `b` 8 bytes on has `imm26` 2) is false for the mutant.", + "certificate": { + "file": "src/a64/cfmemvalid.rs", + "lemma": "lemma_b_words()", + "witness": "b_i(8)->B_imm26 != 2i32" + } + }, + { + "id": "a64-br-if-branches-on-zero", + "module": "a64::cfmemvalid", + "file": "src/a64/cftemplates.rs", + "find": "Node::BrIf(l) => cond_pre(nl, h, false, if l < offs.len() {", + "replace": "Node::BrIf(l) => cond_pre(nl, h, true, if l < offs.len() {", + "expect": "killed", + "reason": "`br_if` would branch on a zero condition (`cbz` for `cbnz`). `a64::cfmemvalid::lemma_brif_branches_on_nonzero` is false for the mutant.", + "certificate": { + "file": "src/a64/cfmemvalid.rs", + "lemma": "lemma_brif_branches_on_nonzero()", + "witness": "ctmpl(Node::BrIf(0), 0, 1, seq![8int])[1]->CompareBranch_iszero != false" + } + }, + { + "id": "a64-emitter-max-words-differs-from-spec", + "module": "a64::cfemit", + "file": "src/a64/cfemit.rs", + "find": "pub const MAX_WORDS: usize = 0x2_0000;", + "replace": "pub const MAX_WORDS: usize = 0x4_0000;", + "expect": "killed", + "reason": "The emitter would translate bodies up to 2^20 bytes where the spec's `cf_prog_ok` stops at `max_size` (2^19), so it would return a blob for a program the contract says has no translation, and a `cbz` could be asked to reach farther than its 19 bits. `a64::cfemit::lemma_emitter_limits` (the emitter's limit is the spec's) is false for the mutant.", + "certificate": { + "file": "src/a64/cfemit.rs", + "lemma": "lemma_emitter_limits()", + "witness": "4 * MAX_WORDS as nat != max_size()" + } + }, + { + "id": "a64-emitter-max-targets-differs-from-spec", + "module": "a64::cfemit", + "file": "src/a64/cfemit.rs", + "find": "pub const MAX_TARGETS: usize = 0x10000;", + "replace": "pub const MAX_TARGETS: usize = 0x20000;", + "expect": "killed", + "reason": "The emitter would accept a `br_table` of up to 2^17 targets where the spec stops at `max_targets` (2^16, the most a 16-bit `movz` immediate can number), so a target's index would be truncated. `a64::cfemit::lemma_emitter_limits` is false for the mutant.", + "certificate": { + "file": "src/a64/cfemit.rs", + "lemma": "lemma_emitter_limits()", + "witness": "MAX_TARGETS as nat != max_targets()" + } + }, + { + "id": "front-int32-tag-test-uses-the-wrong-tag", + "module": "front::trace", + "file": "src/front/arith.rs", + "find": "pub open spec fn tag_hi() -> u64 {\n 0xFFF9_0000\n}", + "replace": "pub open spec fn tag_hi() -> u64 {\n 0xFFFA_0000\n}", + "expect": "killed", + "reason": "The int32 test of the small-integer lowerings would compare the high half of a word with the tag of the specials (0xFFFA) instead of int32's (0xFFF9), so it would reject every int32 and accept `undefined`, `null` and the booleans. `trace::lemma_tag_test` (a word's high half is the tag exactly when it is an int32) is false for the mutant at the int32 5.", + "certificate": { + "file": "src/front/trace.rs", + "lemma": "lemma_tag_test(0xFFF9_0000_0000_0005u64)", + "witness": "((0xFFF9_0000_0000_0005u64 >> 32u64) == super::arith::tag_hi()) == false && js::is_int32(0xFFF9_0000_0000_0005u64) == true" + } + }, + { + "id": "front-result-is-tagged-as-a-special", + "module": "front::arith", + "file": "src/front/arith.rs", + "find": "pub open spec fn tag_bits() -> u64 {\n 0xFFF9_0000_0000_0000\n}", + "replace": "pub open spec fn tag_bits() -> u64 {\n 0xFFFA_0000_0000_0000\n}", + "expect": "killed", + "reason": "The word the lowering stores for an int32 result would carry the specials' tag (0xFFFA) instead of int32's (0xFFF9), so a sum would be stored as a non-value. `arith::lemma_fits_bv` (the tagged low half of a fitting result is `enc_int32` of it) is false for the mutant at 5.", + "certificate": { + "file": "src/front/arith.rs", + "lemma": "lemma_fits_bv(5i64)", + "witness": "tagged(5u64) != js::enc_int32(5i32)" + } + }, + { + "id": "front-subtract-lowers-to-add", + "module": "front::arith", + "file": "src/front/arith.rs", + "find": "lir::IBin::Add\n } else if k == 1 {\n lir::IBin::Sub", + "replace": "lir::IBin::Add\n } else if k == 1 {\n lir::IBin::Add", + "expect": "killed", + "reason": "The lowering of `Sub` would emit a 64-bit `add`. `arith::lemma_ibin64` (the 64-bit operation of `k` is the machine's add, subtract or multiply) is false for the mutant at `k = 1`, 5 and 3: it gives 8, not 2.", + "certificate": { + "file": "src/front/arith.rs", + "lemma": "lemma_ibin64(1nat, 5u64, 3u64)", + "witness": "lir::ibin64(ibin_of(1nat), 5u64, 3u64) == 8u64", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "front-sign-extension-zero-extends", + "module": "front::arith", + "file": "src/front/arith.rs", + "find": " ((x as i32) as i64) as u64\n}", + "replace": " (x as i64) as u64\n}", + "expect": "killed", + "reason": "The sign extension of a payload would zero-extend, so `-1 + 1` in the lowering would be computed as `4294967295 + 1`. `arith::lemma_sx` (`i64.extend_i32_s` computes `sext`) is false for the mutant at 0xFFFF_FFFF.", + "certificate": { + "file": "src/front/arith.rs", + "lemma": "lemma_sx(0xFFFF_FFFFu32)", + "witness": "lir::wrap(64, lir::signed_of(32, 0xFFFF_FFFFu32 as int)) as u64 == 0xFFFF_FFFF_FFFF_FFFFu64", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "front-handler-codes-collide", + "module": "front::dispatch", + "file": "src/front/spec.rs", + "find": " js::HandlerId::Sub => 1,", + "replace": " js::HandlerId::Sub => 0,", + "expect": "killed", + "reason": "`Sub` would exit with `Add`'s code, so the host would run the wrong handler. `dispatch::lemma_handler_index_inverse` (decoding an exit code gives back the handler) is false for the mutant at `Sub`.", + "certificate": { + "file": "src/front/dispatch.rs", + "lemma": "lemma_handler_index_inverse(js::HandlerId::Sub)", + "witness": "handler_index(js::HandlerId::Sub) == 0nat && handler_of_index(0nat) == Some(js::HandlerId::Add)" + } + }, + { + "id": "front-less-than-lowers-to-less-or-equal", + "module": "front::intcmp", + "file": "src/front/intcmp.rs", + "find": " if k == 0 {\n lir::ICmp::LtS\n", + "replace": " if k == 0 {\n lir::ICmp::LeS\n", + "expect": "killed", + "reason": "`Lt` would be lowered to a signed `<=` on the sign-extended payloads, so `5 < 5` would store `true`. `intcmp::lemma_cmp_word` (the comparison's word is the encoding of the integers' comparison) is false for the mutant at `Lt` of 5 and 5: the word is `true`'s, the integers' comparison is false.", + "certificate": { + "file": "src/front/intcmp.rs", + "lemma": "lemma_cmp_word(0nat, 0xFFF9_0000_0000_0005u64, 0xFFF9_0000_0000_0005u64)", + "witness": "js::is_int32(0xFFF9_0000_0000_0005u64) == true && cmp_word(0nat, super::arith::sext(super::arith::lo32(0xFFF9_0000_0000_0005u64)), super::arith::sext(super::arith::lo32(0xFFF9_0000_0000_0005u64))) == js::TRUE && js::enc_bool(cmp_truth(0nat, js::int32_of(0xFFF9_0000_0000_0005u64) as int, js::int32_of(0xFFF9_0000_0000_0005u64) as int)) == js::FALSE", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "front-strict-equal-lowers-to-not-equal-truth", + "module": "front::intcmp", + "file": "src/front/intcmp.rs", + "find": " } else if k == 4 {\n ia == ib\n", + "replace": " } else if k == 4 {\n ia != ib\n", + "expect": "killed", + "reason": "The truth value `StrictEq` is compared against would be `!=`, so the lowering's `eq` word (true for `5 === 5`) would disagree with it. `intcmp::lemma_cmp_word` is false for the mutant at `StrictEq` of 5 and 5.", + "certificate": { + "file": "src/front/intcmp.rs", + "lemma": "lemma_cmp_word(4nat, 0xFFF9_0000_0000_0005u64, 0xFFF9_0000_0000_0005u64)", + "witness": "js::is_int32(0xFFF9_0000_0000_0005u64) == true && cmp_word(4nat, super::arith::sext(super::arith::lo32(0xFFF9_0000_0000_0005u64)), super::arith::sext(super::arith::lo32(0xFFF9_0000_0000_0005u64))) == js::TRUE && js::enc_bool(cmp_truth(4nat, js::int32_of(0xFFF9_0000_0000_0005u64) as int, js::int32_of(0xFFF9_0000_0000_0005u64) as int)) == js::FALSE", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "front-shift-count-reduced-modulo-64", + "module": "front::bitops", + "file": "src/front/bitops.rs", + "find": " ((ib as u32) as nat) % 32\n", + "replace": " ((ib as u32) as nat) % 64\n", + "expect": "killed", + "reason": "The shift count the semantics uses would be reduced modulo 64 instead of 32, so `1 << 32` would be `2^32` (wrapped to 0) while the lowering masks the count to five bits and gives 1. `bitops::lemma_bit_word` (the lowering's word is the encoding of the int32 result) is false for the mutant at `Shl` of 1 and 32.", + "certificate": { + "file": "src/front/bitops.rs", + "lemma": "lemma_bit_word(3nat, 0xFFF9_0000_0000_0001u64, 0xFFF9_0000_0000_0020u64)", + "witness": "js::is_int32(0xFFF9_0000_0000_0001u64) == true && js::is_int32(0xFFF9_0000_0000_0020u64) == true && bit_word(3nat, super::arith::sext(super::arith::lo32(0xFFF9_0000_0000_0001u64)), super::arith::sext(super::arith::lo32(0xFFF9_0000_0000_0020u64))) != js::enc_int32(bit_res(3nat, js::int32_of(0xFFF9_0000_0000_0001u64), js::int32_of(0xFFF9_0000_0000_0020u64)))", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "front-truth-flag-tests-the-payload-against-one", + "module": "front::truth", + "file": "src/front/truth.rs", + "find": "lir::bool_i32(lir::bool_i32(super::arith::lo32(va) == 0) == 0),", + "replace": "lir::bool_i32(lir::bool_i32(super::arith::lo32(va) == 1) == 0),", + "expect": "killed", + "reason": "The truth flag of an int32 would test its payload against 1 instead of 0, so the int32 `0` would be truthy. `truth::lemma_tr_t` (the truth flag is non-zero exactly when `to_boolean_fast` is `Some(true)`) is false for the mutant at the int32 0.", + "certificate": { + "file": "src/front/truth.rs", + "lemma": "lemma_tr_t(0xFFF9_0000_0000_0000u64)", + "witness": "tr_g(0xFFF9_0000_0000_0000u64) != 0u32 && tr_t(0xFFF9_0000_0000_0000u64) != 0u32 && js::to_boolean_fast(0xFFF9_0000_0000_0000u64) == Some(false)", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "front-inc-guard-excludes-the-wrong-value", + "module": "front::unops", + "file": "src/front/unops.rs", + "find": " ia != 2147483647\n", + "replace": " ia != 2147483646\n", + "expect": "killed", + "reason": "The set of int32 operands `Inc` is lowered for would exclude `2^31 - 2` instead of `2^31 - 1`, so the guard (which tests against `2^31 - 1`) would let through a value the semantics says is not covered. `unops::lemma_un_g` (the flag is non-zero exactly for an int32 the fast result covers) is false for the mutant at `Inc` of `2^31 - 2`.", + "certificate": { + "file": "src/front/unops.rs", + "lemma": "lemma_un_g(0nat, 0xFFF9_0000_7FFF_FFFEu64)", + "witness": "js::is_int32(0xFFF9_0000_7FFF_FFFEu64) == true && un_g(0nat, 0xFFF9_0000_7FFF_FFFEu64) != 0u32 && un_cond(0nat, js::int32_of(0xFFF9_0000_7FFF_FFFEu64)) == false", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "front-decrement-result-is-an-increment", + "module": "front::unops", + "file": "src/front/unops.rs", + "find": " } else if k == 1 {\n ((ia as int) - 1) as i32\n", + "replace": " } else if k == 1 {\n ((ia as int) + 1) as i32\n", + "expect": "killed", + "reason": "The int32 `Dec` is specified to give would be `a + 1`, so the lowering's word (`a - 1`, tagged) would disagree. `unops::lemma_un_word` (the value block's word is the encoding of the int32 result) is false for the mutant at `Dec` of 5: the word is 4, the result 6.", + "certificate": { + "file": "src/front/unops.rs", + "lemma": "lemma_un_word(1nat, 0xFFF9_0000_0000_0005u64)", + "witness": "js::is_int32(0xFFF9_0000_0000_0005u64) == true && un_word(1nat, super::arith::sext(super::arith::lo32(0xFFF9_0000_0000_0005u64))) != js::enc_int32(un_res(1nat, js::int32_of(0xFFF9_0000_0000_0005u64)))", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + }, + { + "id": "front-negative-key-has-the-wrong-sign", + "module": "front::numord", + "file": "src/front/numord.rs", + "find": " -pos_bits((-n) as nat)\n", + "replace": " pos_bits((-n) as nat)\n", + "expect": "killed", + "reason": "The sign-magnitude key of the double a negative integer stands for would be positive, so `-1` and `1` would have the same key. `numord::lemma_korder_mono` (the key is strictly increasing) is false for the mutant at -1 and 1.", + "certificate": { + "file": "src/front/numord.rs", + "lemma": "lemma_korder_mono(-1int, 1int)", + "witness": "korder(-1int) == korder(1int)", + "reveal": [ + "vstd::arithmetic::power::pow" + ] + } + } + ] +} diff --git a/crates/clazel-jit/src/a64/alu.rs b/crates/clazel-jit/src/a64/alu.rs new file mode 100644 index 000000000..2795c56b9 --- /dev/null +++ b/crates/clazel-jit/src/a64/alu.rs @@ -0,0 +1,266 @@ +//! What one [`isa::step`] does for the arithmetic, logic, shift and multiply +//! forms the templates use (`x0 := x0 op x1`), as numbers: the result word in +//! `x0` is LIR's result. The A64 counterpart of [`crate::alu`], which has the +//! same job for x86-64. Each lemma takes the operands as the values of `x0` +//! and `x1` below `2^n` for the instruction's width `n`, and says what the +//! state after the step is. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The bit width of an operand size, as the `nat` the spec's arithmetic takes. +pub open spec fn bits(sf: isa::Width) -> nat { + isa::datasize(sf) +} + +/// The step of a register-writing instruction that does not touch the flags, +/// memory or the stack pointer: `x0` is set to `v`, `pc` advances by 4. +pub open spec fn wrote_x0(s: isa::State, v: int, n: nat) -> isa::State { + isa::State { r: s.r.update(0, isa::trunc(n, v) as u64), pc: (s.pc + 4) as u64, ..s } +} + +/// `pc + 4` does not wrap, so the program counter after a step is the address +/// after the instruction. +pub proof fn lemma_pc4(pc: u64) + requires + pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::trunc(64, pc as int + 4) == pc as int + 4, +{ + super::memlemmas::lemma_trunc64(pc as int + 4); +} + +/// A number below `2^n` is its own low `n` bits. +pub proof fn lemma_trunc_small(n: nat, x: int) + requires + n == 32 || n == 64, + 0 <= x < isa::p2(n), + ensures + isa::trunc(n, x) == x, +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::div_mod::lemma_small_mod(x as nat, isa::p2(n) as nat); +} + +/// A number's low `n` bits are below `2^n`. +pub proof fn lemma_trunc_bound(n: nat, x: int) + requires + n == 32 || n == 64, + ensures + 0 <= isa::trunc(n, x) < isa::p2(n), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::div_mod::lemma_mod_bound(x, isa::p2(n)); +} + +/// `add x0, x0, x1` and `sub x0, x0, x1` (no flags) at width `sf`: `x0` gets +/// `x + y` or `x - y` modulo `2^n`. +#[verifier::rlimit(200)] +pub proof fn lemma_step_addsub(s: isa::State, sf: isa::Width, sub: bool, x: u64, y: u64) + requires + isa::wf(s), + s.r[0] == x, + s.r[1] == y, + x < isa::p2(bits(sf)), + y < isa::p2(bits(sf)), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::int_op_instr(if sub { + lir::IBin::Sub + } else { + lir::IBin::Add + }, sf)) == Some( + wrote_x0( + s, + if sub { + (x as int - y as int) % isa::p2(bits(sf)) + } else { + (x as int + y as int) % isa::p2(bits(sf)) + }, + bits(sf), + ), + ), +{ + let n = bits(sf); + let ni = n as int; + assert(n == 32 || n == 64); + super::memlemmas::lemma_p2_consts(); + lemma_pc4(s.pc); + let p = isa::p2(n); + lemma_trunc_small(n, x as int); + lemma_trunc_small(n, y as int); + // operand2 after the shift by 0 + assert(isa::lsl(n, y as int, 0) == y as int) by { + assert(isa::p2(0) == 1); + } + if sub { + // not_vec = ones - y = p - 1 - y + assert(isa::not_vec(n, y as int) == p - 1 - y); + assert(isa::trunc(n, p - 1 - y) == p - 1 - y) by { + lemma_trunc_small(n, p - 1 - y); + } + assert(isa::trunc(n, x as int + (p - 1 - y) + 1) == (x as int - y as int) % p) by { + vstd::arithmetic::div_mod::lemma_mod_add_multiples_vanish(x as int - y as int, p); + } + } else { + assert(isa::trunc(n, x as int + y as int + 0) == (x as int + y as int) % p); + } +} + +/// The width of `sf` is 32 or 64. +pub proof fn lemma_bits(sf: isa::Width) + ensures + bits(sf) == 32 || bits(sf) == 64, + (sf is W32) <==> bits(sf) == 32, +{ +} + +/// The bitwise operation of `and`, `or`, `xor` on two words. +pub open spec fn logic_val(o: lir::IBin, x: u64, y: u64) -> u64 { + match o { + lir::IBin::And => x & y, + lir::IBin::Or => x | y, + _ => x ^ y, + } +} + +/// `and`, `orr`, `eor` (shifted register, no shift) on `x0` and `x1` at width +/// `sf`: the bitwise operation on the operands. +#[verifier::rlimit(200)] +pub proof fn lemma_step_logic(s: isa::State, sf: isa::Width, o: lir::IBin, x: u64, y: u64) + requires + isa::wf(s), + super::templates::logic_of(o) is Some, + s.r[0] == x, + s.r[1] == y, + x < isa::p2(bits(sf)), + y < isa::p2(bits(sf)), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::int_op_instr(o, sf)) == Some( + wrote_x0(s, logic_val(o, x, y) as int, bits(sf)), + ), + logic_val(o, x, y) < isa::p2(bits(sf)), +{ + let n = bits(sf); + lemma_bits(sf); + super::memlemmas::lemma_p2_consts(); + lemma_pc4(s.pc); + lemma_trunc_small(n, x as int); + lemma_trunc_small(n, y as int); + assert(isa::lsl(n, y as int, 0) == y as int) by { + assert(isa::p2(0) == 1); + } + assert(isa::lsl(n, x as int, 0) == x as int); + // the result is below 2^n + if n == 32 { + assert((x & y) < 0x1_0000_0000u64) by (bit_vector) + requires + x < 0x1_0000_0000u64, + y < 0x1_0000_0000u64, + ; + assert((x | y) < 0x1_0000_0000u64) by (bit_vector) + requires + x < 0x1_0000_0000u64, + y < 0x1_0000_0000u64, + ; + assert((x ^ y) < 0x1_0000_0000u64) by (bit_vector) + requires + x < 0x1_0000_0000u64, + y < 0x1_0000_0000u64, + ; + } + assert(isa::and_vec(x as int, y as int) == (x & y) as int); + assert(isa::or_vec(x as int, y as int) == (x | y) as int); + assert(isa::eor_vec(x as int, y as int) == (x ^ y) as int); + lemma_trunc_small(n, logic_val(o, x, y) as int); +} + +/// `mul x0, x0, x1` (`madd x0, x0, x1, xzr`) at width `sf`: the low `n` bits +/// of the product. +#[verifier::rlimit(200)] +pub proof fn lemma_step_mul(s: isa::State, sf: isa::Width, x: u64, y: u64) + requires + isa::wf(s), + s.r[0] == x, + s.r[1] == y, + x < isa::p2(bits(sf)), + y < isa::p2(bits(sf)), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::int_op_instr(lir::IBin::Mul, sf)) == Some( + wrote_x0(s, (x as int * y as int) % isa::p2(bits(sf)), bits(sf)), + ), +{ + let n = bits(sf); + lemma_bits(sf); + super::memlemmas::lemma_p2_consts(); + lemma_pc4(s.pc); + lemma_trunc_small(n, x as int); + lemma_trunc_small(n, y as int); + let p = isa::p2(n); + // x_set truncates the already truncated product + assert(isa::trunc(n, 0 + x as int * y as int) == (x as int * y as int) % p); + vstd::arithmetic::div_mod::lemma_mod_bound(x as int * y as int, p); + lemma_trunc_small(n, (x as int * y as int) % p); +} + +/// `lslv`, `lsrv`, `asrv` of `x0` by `x1` at width `sf`: the count is `x1` +/// modulo the width, and the result is the shift of `x0`. +#[verifier::rlimit(300)] +pub proof fn lemma_step_shift(s: isa::State, sf: isa::Width, o: lir::IBin, x: u64, y: u64) + requires + isa::wf(s), + super::templates::shift_of(o) is Some, + s.r[0] == x, + s.r[1] == y, + x < isa::p2(bits(sf)), + y < isa::p2(bits(sf)), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + ({ + let k = (y as int % bits(sf) as int) as nat; + isa::step(s, super::templates::int_op_instr(o, sf)) == Some( + wrote_x0( + s, + match o { + lir::IBin::Shl => (x as int * isa::p2(k)) % isa::p2(bits(sf)), + lir::IBin::ShrU => x as int / isa::p2(k), + _ => isa::trunc(bits(sf), isa::sint(bits(sf), x as int) / isa::p2(k)), + }, + bits(sf), + ), + ) + }), +{ + let n = bits(sf); + lemma_bits(sf); + super::memlemmas::lemma_p2_consts(); + lemma_pc4(s.pc); + lemma_trunc_small(n, x as int); + lemma_trunc_small(n, y as int); + let k = (y as int % n as int) as nat; + let p = isa::p2(n); + assert(isa::trunc(n, x as int * isa::p2(k)) == (x as int * isa::p2(k)) % p); + // the shifted value of a logical shift right is at most x, so below 2^n + if o is ShrU { + vstd::arithmetic::power2::lemma_pow2_pos(k); + assert(isa::p2(k) >= 1); + assert(x as int / isa::p2(k) <= x as int) by (nonlinear_arith) + requires + isa::p2(k) >= 1, + x as int >= 0, + ; + assert(0 <= x as int / isa::p2(k)) by (nonlinear_arith) + requires + isa::p2(k) >= 1, + x as int >= 0, + ; + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/api.rs b/crates/clazel-jit/src/a64/api.rs new file mode 100644 index 000000000..4caa47840 --- /dev/null +++ b/crates/clazel-jit/src/a64/api.rs @@ -0,0 +1,44 @@ +//! The contract-only API of the A64 back end: a trait with no implementation, +//! so Verus accepts it with no escape hatch (CLAUDE.md rule 4), the pattern of +//! [`crate::api`]. The implementation (the next commit) implements it. +//! +//! The input is [`Op`]s, as for x86-64: LIR's own `Instr` holds proof-only +//! numbers and sequences, and the JS front end (J.10) will have its own +//! executable form. [`Op`]'s `view` says which LIR instruction each stands for. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The LIR → A64 back end, with its contracts. +pub trait Backend { + /// The A64 instructions for the straight-line program `ops`, in a + /// function with `nl` locals, entered with an empty operand stack (see + /// [`crate::a64::contract::emit_body_ensures`]). + fn emit_body(ops: &[Op], nl: usize) -> (r: Option>) + requires + crate::a64::contract::emit_requires(ops@, nl), + ensures + crate::a64::contract::emit_body_ensures(ops@, nl, match r { + Some(v) => Some(v@), + None => None, + }), + ; + + /// The bytes of a blob: the canonical encoding of the body, then the exit + /// sequence for `exit_code` (see [`crate::a64::contract::emit_blob_ensures`]). + fn emit_blob(ops: &[Op], nl: usize, exit_code: u16) -> (r: Option>) + requires + crate::a64::contract::emit_requires(ops@, nl), + ensures + crate::a64::contract::emit_blob_ensures(ops@, nl, exit_code, match r { + Some(v) => Some(v@), + None => None, + }), + ; +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/bitfield.rs b/crates/clazel-jit/src/a64/bitfield.rs new file mode 100644 index 000000000..879416a15 --- /dev/null +++ b/crates/clazel-jit/src/a64/bitfield.rs @@ -0,0 +1,171 @@ +//! The bit-field instructions the templates use: `ubfm x0, x0, #0, #62` +//! (`f64.abs`) and `sbfm x0, x0, #0, #31` (`i64.extend_i32_s`), as numbers. The +//! bit masks come from `DecodeBitMasks`, evaluated here for these two +//! encodings. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `DecodeBitMasks(1, 62, 0, 64)` (the masks of `ubfm x, x, #0, #62`) are both +/// the low 63 bits. +pub proof fn lemma_masks_62() + ensures + isa::decode_bit_masks(1, 62, 0, 64) == (0x7FFF_FFFF_FFFF_FFFFint, 0x7FFF_FFFF_FFFF_FFFFint), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + assert(isa::p2(6) == 64); + assert(isa::not_vec(6, 62) == 1) by { + assert(isa::trunc(6, 62) == 62); + assert(isa::ones(6) == 63); + } + assert(isa::highest_set_bit(7, 65int) == 6) by { + assert(isa::bit(65, 6) == 1); + } + assert(isa::and_vec(62, 63) == 62) by (bit_vector); + assert(isa::and_vec(0, 63) == 0) by (bit_vector); + assert(isa::ones(63) == 0x7FFF_FFFF_FFFF_FFFF); + assert(isa::ror(64, 0x7FFF_FFFF_FFFF_FFFF, 0) == 0x7FFF_FFFF_FFFF_FFFFint); + assert(isa::trunc(6, 62 - 0) == 62); +} + +/// `DecodeBitMasks(1, 31, 0, 64)` (the masks of `sbfm x, x, #0, #31`) are both +/// the low 32 bits. +pub proof fn lemma_masks_31() + ensures + isa::decode_bit_masks(1, 31, 0, 64) == (0xFFFF_FFFFint, 0xFFFF_FFFFint), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + assert(isa::p2(6) == 64); + assert(isa::not_vec(6, 31) == 32) by { + assert(isa::trunc(6, 31) == 31); + assert(isa::ones(6) == 63); + } + assert(isa::highest_set_bit(7, 96int) == 6) by { + assert(isa::bit(96, 6) == 1); + } + assert(isa::and_vec(31, 63) == 31) by (bit_vector); + assert(isa::and_vec(0, 63) == 0) by (bit_vector); + assert(isa::ones(32) == 0xFFFF_FFFF); + assert(isa::ror(64, 0xFFFF_FFFF, 0) == 0xFFFF_FFFFint); + assert(isa::trunc(6, 31 - 0) == 31); +} + +/// `ubfm x0, x0, #0, #62` clears the sign bit: `x0` becomes `x & (2^63 - 1)`. +#[verifier::rlimit(300)] +pub proof fn lemma_step_abs(s: isa::State, x: u64) + requires + isa::wf(s), + s.r[0] == x, + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::abs_mid()[0]) == Some( + super::alu::wrote_x0(s, (x & 0x7FFF_FFFF_FFFF_FFFFu64) as int, 64), + ), + (x & 0x7FFF_FFFF_FFFF_FFFFu64) as int <= 0x7FFF_FFFF_FFFF_FFFF, +{ + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + lemma_masks_62(); + super::alu::lemma_trunc_small(64, x as int); + let m: u64 = 0x7FFF_FFFF_FFFF_FFFF; + let nm = isa::not_vec(64, m as int); + assert(nm == 0x8000_0000_0000_0000int) by { + assert(isa::trunc(64, m as int) == m as int); + assert(isa::ones(64) == 0xFFFF_FFFF_FFFF_FFFF); + } + assert(isa::and_vec(0, nm) == 0) by { + assert(0u64 & (nm as u64) == 0u64) by (bit_vector); + } + assert(isa::ror(64, x as int, 0) == x as int); + assert(isa::and_vec(x as int, m as int) == (x & m) as int); + assert(isa::or_vec(0, (x & m) as int) == (x & m) as int) by (bit_vector); + assert(isa::and_vec((x & m) as int, m as int) == (x & m) as int) by { + assert(((x & m) & m) == (x & m)) by (bit_vector); + } + assert(isa::or_vec(0, (x & m) as int) == (x & m) as int) by { + assert((0u64 | (x & m)) == (x & m)) by (bit_vector); + } + super::alu::lemma_trunc_small(64, (x & m) as int); + assert((x & 0x7FFF_FFFF_FFFF_FFFFu64) <= 0x7FFF_FFFF_FFFF_FFFFu64) by (bit_vector); +} + +/// `sbfm x0, x0, #0, #31` sign-extends the low 32 bits: for `x < 2^32`, `x0` +/// becomes `x` or `x + (2^64 - 2^32)` by the top bit. +#[verifier::rlimit(300)] +pub proof fn lemma_step_exts(s: isa::State, x: u64) + requires + isa::wf(s), + s.r[0] == x, + x < 0x1_0000_0000, + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::exts_mid()[0]) == Some( + super::alu::wrote_x0( + s, + if x >= 0x8000_0000 { + x as int + 0xFFFF_FFFF_0000_0000int + } else { + x as int + }, + 64, + ), + ), +{ + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + lemma_masks_31(); + super::alu::lemma_trunc_small(64, x as int); + let m: u64 = 0xFFFF_FFFF; + let nm = isa::not_vec(64, m as int); + assert(nm == 0xFFFF_FFFF_0000_0000int) by { + assert(isa::trunc(64, m as int) == m as int); + assert(isa::ones(64) == 0xFFFF_FFFF_FFFF_FFFF); + } + assert(isa::and_vec(0, nm) == 0) by { + assert(0u64 & (nm as u64) == 0u64) by (bit_vector); + } + assert(isa::ror(64, x as int, 0) == x as int); + assert((x & 0xFFFF_FFFFu64) == x) by (bit_vector) + requires + x < 0x1_0000_0000u64, + ; + assert(isa::and_vec(x as int, m as int) == x as int); + assert(isa::or_vec(0, x as int) == x as int) by { + assert((0u64 | x) == x) by (bit_vector); + } + // the sign bit and the top + assert(isa::bit(x as int, 31) == (x as int / 0x8000_0000) % 2); + let top = isa::replicate_bit(64, isa::bit(x as int, 31)); + if x >= 0x8000_0000 { + assert(isa::bit(x as int, 31) == 1); + assert(top == 0xFFFF_FFFF_FFFF_FFFFint); + assert(isa::and_vec(top, nm) == 0xFFFF_FFFF_0000_0000int) by { + assert((0xFFFF_FFFF_FFFF_FFFFu64 & 0xFFFF_FFFF_0000_0000u64) == 0xFFFF_FFFF_0000_0000u64) + by (bit_vector); + } + assert(isa::or_vec(0xFFFF_FFFF_0000_0000int, x as int) == x as int + 0xFFFF_FFFF_0000_0000int) by { + assert((0xFFFF_FFFF_0000_0000u64 | x) == 0xFFFF_FFFF_0000_0000u64 + x) by (bit_vector) + requires + x < 0x1_0000_0000u64, + ; + } + super::alu::lemma_trunc_small(64, x as int + 0xFFFF_FFFF_0000_0000int); + } else { + assert(isa::bit(x as int, 31) == 0); + assert(top == 0); + assert(isa::and_vec(top, nm) == 0) by { + assert(0u64 & (nm as u64) == 0u64) by (bit_vector); + } + assert(isa::or_vec(0, x as int) == x as int) by { + assert((0u64 | x) == x) by (bit_vector); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/bitmid.rs b/crates/clazel-jit/src/a64/bitmid.rs new file mode 100644 index 000000000..ef75f4915 --- /dev/null +++ b/crates/clazel-jit/src/a64/bitmid.rs @@ -0,0 +1,473 @@ +//! The middles that are constants and bit operations: `movz`/`movk` chains +//! (`t.const`), `movz; eor` (`f64.neg`), `ubfm` (`f64.abs`), `sbfm` +//! (`i64.extend_i32_s`) and `orr w0, wzr, w0` (`i32.wrap_i64`). With +//! [`super::mid`], these are every register-only middle of the first slice. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `imm16 << (16 hw)` for `hw` at most 3, with literal multipliers. +pub open spec fn chunk_val(imm16: u16, hw: u8) -> int { + if hw == 0 { + imm16 as int + } else if hw == 1 { + imm16 as int * 0x1_0000 + } else if hw == 2 { + imm16 as int * 0x1_0000_0000 + } else { + imm16 as int * 0x1_0000_0000_0000 + } +} + +/// The power of two `movz` and `movk` scale by. +pub proof fn lemma_p2_hw(hw: u8) + requires + hw <= 3, + ensures + isa::p2((hw as nat) * 16) == (if hw == 0 { + 1int + } else if hw == 1 { + 0x1_0000int + } else if hw == 2 { + 0x1_0000_0000int + } else { + 0x1_0000_0000_0000int + }), +{ + super::memlemmas::lemma_p2_consts(); +} + +/// `movz x0, #imm16, lsl #(16 hw)` at width `sf` sets `x0` to the shifted +/// immediate. +#[verifier::rlimit(200)] +pub proof fn lemma_step_movz(s: isa::State, sf: isa::Width, d: u8, imm16: u16, hw: u8) + requires + isa::wf(s), + d < 31, + hw <= 3, + (hw as nat) * 16 < super::alu::bits(sf), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + 0 <= chunk_val(imm16, hw) < isa::p2(super::alu::bits(sf)), + isa::step( + s, + isa::Instr::MoveWide { sf, opcode: isa::MoveWideOp::Z, d, imm16, hw }, + ) == Some( + isa::State { + r: s.r.update(d as int, chunk_val(imm16, hw) as u64), + pc: (s.pc + 4) as u64, + ..s + }, + ), +{ + let n = super::alu::bits(sf); + super::alu::lemma_bits(sf); + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + lemma_p2_hw(hw); + let pos = (hw as nat) * 16; + let pp = isa::p2(pos); + vstd::arithmetic::power2::lemma_pow2_pos(pos); + vstd::arithmetic::div_mod::lemma_basic_div(0, pp); + assert(0int / pp == 0); + assert(0 <= chunk_val(imm16, hw) < isa::p2(n)); + super::alu::lemma_trunc_small(n, chunk_val(imm16, hw)); + assert(chunk_val(imm16, hw) == imm16 as int * pp); + // the instruction's result: the shifted immediate (the old word is zeros) + let i = isa::Instr::MoveWide { sf, opcode: isa::MoveWideOp::Z, d, imm16, hw }; + let v = chunk_val(imm16, hw); + let q = (0int / pp) % 0x1_0000; + assert(q == 0); + vstd::arithmetic::mul::lemma_mul_by_zero_is_zero(pp); + assert(q * pp == 0); + assert(0int - q * pp + imm16 as int * pp == v); + assert(isa::execute(s, i) == Some(isa::x_set(s, d, n, v))); + assert(isa::x_set(s, d, n, v) == isa::State { r: s.r.update(d as int, v as u64), ..s }); +} + +/// `movk x0, #imm16, lsl #(16 hw)` (`hw` from 1) at width `sf`, when `x0` is +/// below `2^(16 hw)`, adds the shifted immediate. +#[verifier::rlimit(200)] +pub proof fn lemma_step_movk(s: isa::State, sf: isa::Width, d: u8, imm16: u16, hw: u8) + requires + isa::wf(s), + d < 31, + 1 <= hw <= 3, + (hw as nat) * 16 < super::alu::bits(sf), + s.r[d as int] < isa::p2((hw as nat) * 16), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + 0 <= s.r[d as int] as int + chunk_val(imm16, hw) < isa::p2(super::alu::bits(sf)), + isa::step( + s, + isa::Instr::MoveWide { sf, opcode: isa::MoveWideOp::K, d, imm16, hw }, + ) == Some( + isa::State { + r: s.r.update(d as int, (s.r[d as int] as int + chunk_val(imm16, hw)) as u64), + pc: (s.pc + 4) as u64, + ..s + }, + ), +{ + let n = super::alu::bits(sf); + super::alu::lemma_bits(sf); + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + lemma_p2_hw(hw); + let pos = (hw as nat) * 16; + let pp = isa::p2(pos); + let old = s.r[d as int] as int; + vstd::arithmetic::power2::lemma_pow2_pos(pos); + // x_read of the old value (below 2^pos, so below 2^n) + assert(pp <= isa::p2(n)) by { + if hw == 1 { + assert(pp == 0x1_0000); + } else if hw == 2 { + assert(pp == 0x1_0000_0000); + } else { + assert(pp == 0x1_0000_0000_0000); + } + } + super::alu::lemma_trunc_small(n, old); + vstd::arithmetic::div_mod::lemma_basic_div(old, pp); + assert(old / pp == 0); + assert(chunk_val(imm16, hw) == imm16 as int * pp); + // the new value is below 2^n + assert(old + chunk_val(imm16, hw) < isa::p2(n)) by { + if hw == 1 { + assert(old + imm16 as int * 0x1_0000 < 0x1_0000_0000); + } else if hw == 2 { + assert(old + imm16 as int * 0x1_0000_0000 < 0x1_0000_0000_0000); + } else { + assert(old + imm16 as int * 0x1_0000_0000_0000 < 0x1_0000_0000_0000_0000); + } + } + super::alu::lemma_trunc_small(n, old + chunk_val(imm16, hw)); + // the instruction's result: the old word with chunk `hw` replaced, which is + // `old + imm16 * 2^pos` because that chunk of `old` is 0 + let i = isa::Instr::MoveWide { sf, opcode: isa::MoveWideOp::K, d, imm16, hw }; + let v = old + chunk_val(imm16, hw); + assert(isa::x_read(s, d, n) == old); + let q = (old / pp) % 0x1_0000; + assert(q == 0); + vstd::arithmetic::mul::lemma_mul_by_zero_is_zero(pp); + assert(q * pp == 0); + assert(old - q * pp + imm16 as int * pp == v); + assert(isa::execute(s, i) == Some(isa::x_set(s, d, n, v))); + assert(isa::x_set(s, d, n, v) == isa::State { r: s.r.update(d as int, v as u64), ..s }); +} + +/// The four chunks of a 64-bit word add up to it. +pub proof fn lemma_chunks(c: u64) + ensures + c as int == super::templates::chunk(c, 0) as int + 0x1_0000 * super::templates::chunk( + c, + 1, + ) as int + 0x1_0000_0000 * super::templates::chunk(c, 2) as int + 0x1_0000_0000_0000 + * super::templates::chunk(c, 3) as int, +{ + let c0 = c as int % 0x1_0000; + let c1 = (c as int / 0x1_0000) % 0x1_0000; + let c2 = (c as int / 0x1_0000_0000) % 0x1_0000; + let c3 = c as int / 0x1_0000_0000_0000; + assert(c3 < 0x1_0000); + assert(super::templates::chunk(c, 0) as int == c0); + assert(super::templates::chunk(c, 1) as int == c1); + assert(super::templates::chunk(c, 2) as int == c2); + assert(super::templates::chunk(c, 3) as int == c3); +} + +/// **A constant** is loaded into `x0`: the middle of `t.const`. +#[verifier::rlimit(500)] +pub proof fn lemma_mid_const(s: isa::State, v: lir::Val) + requires + isa::wf(s), + s.pc as int + 4 * super::templates::const_mid(v).len() < 0x1_0000_0000_0000_0000, + ensures + run_seq(s, super::templates::const_mid(v)) is Some, + super::mid::mid_post( + s, + run_seq(s, super::templates::const_mid(v)).unwrap(), + 0, + lir::val_bits(v), + 4 * super::templates::const_mid(v).len(), + ), +{ + super::memlemmas::lemma_p2_consts(); + match v { + lir::Val::I32(c) => { + let lo = super::templates::chunk(c as u64, 0); + let hi = super::templates::chunk(c as u64, 1); + let i1 = super::templates::movz0(isa::Width::W32, lo, 0); + let i2 = super::templates::movk0(isa::Width::W32, hi, 1); + assert(super::templates::const_mid(v) == seq![i1, i2]); + assert(super::alu::bits(isa::Width::W32) == 32); + lemma_step_movz(s, isa::Width::W32, 0, lo, 0); + let s1 = isa::State { + r: s.r.update(0, chunk_val(lo, 0) as u64), + pc: (s.pc + 4) as u64, + ..s + }; + assert(isa::step(s, i1) == Some(s1)); + super::alu::lemma_pc4(s.pc); + assert(isa::wf(s1)); + assert(s1.r[0] == lo as u64); + lemma_step_movk(s1, isa::Width::W32, 0, hi, 1); + let s2 = isa::State { + r: s1.r.update(0, (s1.r[0] as int + chunk_val(hi, 1)) as u64), + pc: (s1.pc + 4) as u64, + ..s1 + }; + assert(isa::step(s1, i2) == Some(s2)); + super::runseq::run2(s, i1, i2, s1, s2); + // lo + 65536 hi == c + assert(lo as int == c as int % 0x1_0000); + assert(hi as int == (c as int / 0x1_0000) % 0x1_0000); + assert(lo as int + 0x1_0000 * hi as int == c as int); + assert(s2.r[0] as nat == c as nat); + }, + lir::Val::I64(c) => { + lemma_const64(s, c); + }, + lir::Val::F64(c) => { + lemma_const64(s, c); + }, + } +} + +/// The four-instruction load of a 64-bit word. +#[verifier::rlimit(500)] +pub proof fn lemma_const64(s: isa::State, c: u64) + requires + isa::wf(s), + s.pc as int + 16 < 0x1_0000_0000_0000_0000, + ensures + ({ + let code = seq![ + super::templates::movz0(isa::Width::W64, super::templates::chunk(c, 0), 0), + super::templates::movk0(isa::Width::W64, super::templates::chunk(c, 1), 1), + super::templates::movk0(isa::Width::W64, super::templates::chunk(c, 2), 2), + super::templates::movk0(isa::Width::W64, super::templates::chunk(c, 3), 3), + ]; + &&& run_seq(s, code) is Some + &&& super::mid::mid_post(s, run_seq(s, code).unwrap(), 0, c as nat, 16) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + lemma_chunks(c); + let c0 = super::templates::chunk(c, 0); + let c1 = super::templates::chunk(c, 1); + let c2 = super::templates::chunk(c, 2); + let c3 = super::templates::chunk(c, 3); + let w = isa::Width::W64; + assert(super::alu::bits(w) == 64); + let i1 = super::templates::movz0(w, c0, 0); + let i2 = super::templates::movk0(w, c1, 1); + let i3 = super::templates::movk0(w, c2, 2); + let i4 = super::templates::movk0(w, c3, 3); + lemma_step_movz(s, w, 0, c0, 0); + let s1 = isa::State { r: s.r.update(0, chunk_val(c0, 0) as u64), pc: (s.pc + 4) as u64, ..s }; + assert(isa::step(s, i1) == Some(s1)); + assert(isa::wf(s1)); + assert(s1.r[0] == c0 as u64); + lemma_step_movk(s1, w, 0, c1, 1); + let v2 = s1.r[0] as int + chunk_val(c1, 1); + let s2 = isa::State { r: s1.r.update(0, v2 as u64), pc: (s1.pc + 4) as u64, ..s1 }; + assert(isa::step(s1, i2) == Some(s2)); + assert(isa::wf(s2)); + assert(v2 < 0x1_0000_0000); + assert(s2.r[0] == v2 as u64); + lemma_step_movk(s2, w, 0, c2, 2); + let v3 = s2.r[0] as int + chunk_val(c2, 2); + let s3 = isa::State { r: s2.r.update(0, v3 as u64), pc: (s2.pc + 4) as u64, ..s2 }; + assert(isa::step(s2, i3) == Some(s3)); + assert(isa::wf(s3)); + assert(v3 < 0x1_0000_0000_0000); + assert(s3.r[0] == v3 as u64); + lemma_step_movk(s3, w, 0, c3, 3); + let v4 = s3.r[0] as int + chunk_val(c3, 3); + let s4 = isa::State { r: s3.r.update(0, v4 as u64), pc: (s3.pc + 4) as u64, ..s3 }; + assert(isa::step(s3, i4) == Some(s4)); + assert(v4 == c as int); + super::runseq::run4(s, i1, i2, i3, i4, s1, s2, s3, s4); + assert(s4.r[0] as nat == c as nat); +} + +/// The `movz` of the `f64.neg` template loads the sign bit into `x1`: its +/// immediate shifted by its `hw` is `2^63`, and its destination is `x1`. +pub proof fn lemma_neg_template_mask() + ensures + ({ + match super::templates::neg_mid()[0] { + isa::Instr::MoveWide { sf, opcode, d, imm16, hw } => hw <= 3 && d == 1 && chunk_val( + imm16, + hw, + ) == 0x8000_0000_0000_0000int, + _ => false, + } + }), +{ +} + +/// **`f64.neg`**: `movz x1, #0x8000, lsl #48; eor x0, x0, x1` flips the sign bit. +#[verifier::rlimit(300)] +pub proof fn lemma_mid_neg(s: isa::State, x: u64) + requires + isa::wf(s), + s.r[0] == x, + s.pc as int + 8 < 0x1_0000_0000_0000_0000, + ensures + run_seq(s, super::templates::neg_mid()) is Some, + super::mid::mid_post( + s, + run_seq(s, super::templates::neg_mid()).unwrap(), + 0, + (x ^ 0x8000_0000_0000_0000u64) as nat, + 8, + ), +{ + super::memlemmas::lemma_p2_consts(); + let i1 = super::templates::neg_mid()[0]; + let i2 = super::templates::neg_mid()[1]; + assert(super::templates::neg_mid() == seq![i1, i2]); + assert(super::alu::bits(isa::Width::W64) == 64); + // the template's `movz` immediate and shift (not literals of this proof) + lemma_neg_template_mask(); + let (imm16, hw) = match i1 { + isa::Instr::MoveWide { imm16, hw, .. } => (imm16, hw), + _ => (0, 0), + }; + assert(i1 == isa::Instr::MoveWide { + sf: isa::Width::W64, + opcode: isa::MoveWideOp::Z, + d: 1, + imm16, + hw, + }); + lemma_step_movz(s, isa::Width::W64, 1, imm16, hw); + assert(chunk_val(imm16, hw) == 0x8000_0000_0000_0000int); + let s1 = isa::State { r: s.r.update(1, 0x8000_0000_0000_0000u64), pc: (s.pc + 4) as u64, ..s }; + assert(isa::step(s, i1) == Some(s1)); + super::alu::lemma_pc4(s.pc); + assert(isa::wf(s1)); + assert(s1.r[0] == x && s1.r[1] == 0x8000_0000_0000_0000u64); + super::alu::lemma_step_logic(s1, isa::Width::W64, lir::IBin::Xor, x, 0x8000_0000_0000_0000u64); + let s2 = super::alu::wrote_x0(s1, (x ^ 0x8000_0000_0000_0000u64) as int, 64); + assert(isa::step(s1, i2) == Some(s2)); + super::runseq::run2(s, i1, i2, s1, s2); + super::mid::lemma_wrote_post(s1, (x ^ 0x8000_0000_0000_0000u64) as int, 64); + assert(s2.mem == s.mem && s2.sp == s.sp && s2.vreg == s.vreg); +} + +/// **`f64.abs`**: `ubfm x0, x0, #0, #62` clears the sign bit. +pub proof fn lemma_mid_abs(s: isa::State, x: u64) + requires + isa::wf(s), + s.r[0] == x, + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + run_seq(s, super::templates::abs_mid()) is Some, + super::mid::mid_post( + s, + run_seq(s, super::templates::abs_mid()).unwrap(), + 0, + (x & 0x7FFF_FFFF_FFFF_FFFFu64) as nat, + 4, + ), +{ + super::memlemmas::lemma_p2_consts(); + super::bitfield::lemma_step_abs(s, x); + let i = super::templates::abs_mid()[0]; + assert(super::templates::abs_mid() == seq![i]); + super::runseq::lemma_run_seq_one(s, i); + let v = (x & 0x7FFF_FFFF_FFFF_FFFFu64) as int; + assert(0 <= v < isa::p2(64)); + super::mid::lemma_wrote_post(s, v, 64); +} + +/// **`i64.extend_i32_s`**: `sbfm x0, x0, #0, #31` sign-extends. +pub proof fn lemma_mid_exts(s: isa::State, x: u32) + requires + isa::wf(s), + s.r[0] == x as u64, + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + run_seq(s, super::templates::exts_mid()) is Some, + super::mid::mid_post( + s, + run_seq(s, super::templates::exts_mid()).unwrap(), + 0, + lir::wrap(64, lir::signed_of(32, x as int)) as u64 as nat, + 4, + ), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + vstd::arithmetic::power2::lemma2_to64_rest(); + super::bitfield::lemma_step_exts(s, x as u64); + let i = super::templates::exts_mid()[0]; + assert(super::templates::exts_mid() == seq![i]); + super::runseq::lemma_run_seq_one(s, i); + let v = if x as u64 >= 0x8000_0000 { + x as int + 0xFFFF_FFFF_0000_0000int + } else { + x as int + }; + assert(0 <= v < isa::p2(64)); + super::mid::lemma_wrote_post(s, v, 64); + // LIR: wrap(64, signed_of(32, x)) = (x - 2^32 if x >= 2^31) mod 2^64 + assert(lir::pow2(32) == 0x1_0000_0000); + assert(lir::pow2(64) == 0x1_0000_0000_0000_0000); + assert(lir::wrap(64, lir::signed_of(32, x as int)) == v); + assert(v as u64 as nat == v as nat); +} + +/// **`i32.wrap_i64`**: `orr w0, wzr, w0` keeps the low 32 bits. +pub proof fn lemma_mid_wrap(s: isa::State, x: u64) + requires + isa::wf(s), + s.r[0] == x, + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + run_seq(s, super::templates::wrap_mid()) is Some, + super::mid::mid_post( + s, + run_seq(s, super::templates::wrap_mid()).unwrap(), + 0, + ((x & 0xFFFF_FFFFu64) as u32) as nat, + 4, + ), +{ + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + let i = super::templates::wrap_mid()[0]; + assert(super::templates::wrap_mid() == seq![i]); + // x_read(0, 32) is the low half, or_vec(0, h) = h + let h = isa::trunc(32, x as int); + super::alu::lemma_trunc_bound(32, x as int); + assert(isa::lsl(32, x as int, 0) == h) by { + assert(isa::p2(0) == 1); + assert(isa::trunc(32, h) == h) by { + super::alu::lemma_trunc_small(32, h); + } + } + assert(isa::or_vec(0, h) == h) by { + assert((0u64 | (h as u64)) == (h as u64)) by (bit_vector); + } + assert(isa::trunc(32, h) == h) by { + super::alu::lemma_trunc_small(32, h); + } + assert(isa::step(s, i) == Some(super::alu::wrote_x0(s, h, 32))); + assert(h == ((x & 0xFFFF_FFFFu64) as u32) as int) by { + assert(((x & 0xFFFF_FFFFu64) as u32) as u64 == x % 0x1_0000_0000u64) by (bit_vector); + } + super::runseq::lemma_run_seq_one(s, i); + super::mid::lemma_wrote_post(s, h, 32); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfaccept.rs b/crates/clazel-jit/src/a64/cfaccept.rs new file mode 100644 index 000000000..9b572686c --- /dev/null +++ b/crates/clazel-jit/src/a64/cfaccept.rs @@ -0,0 +1,271 @@ +//! The templates as lists of the validator's units, and where a jump may land +//! (jit.md item J.9, "every emitted blob is accepted", x86-64). +//! +//! [`utmpl`] is [`ctmpl`] cut into the units the validator's scan sees: every +//! instruction is a unit of its own, except the bounds-check sequence of a +//! memory instruction (8 instructions) and every exit sequence (3). +//! [`lemma_utmpl_flat`]: the instructions of the units are `ctmpl`. +//! +//! [`bs`] says where a jump inside a node may land, as a byte position from +//! the node's start, other than at one of the labels it was given: the +//! node's own start and end, the starts and ends of its parts, and the stub +//! after a memory instruction (where the check's exits go). [`prot`] is where +//! no jump lands: in a memory instruction, the start of the bounds-check +//! sequence and the start of the access, because the scan forgets what it +//! knows at a jump target and the access needs what the load of the region's +//! base and the check told it. They are disjoint ([`lemma_bs_prot`]), and +//! every position of [`bs`] is a unit start or the end ([`lemma_bs_ubi`]). + +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::cfunits::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The units of a memory instruction: the operands and the base one +/// instruction at a time, the bounds-check sequence, the access, the `jmp` +/// over the stub, and the stub (an exit sequence). +pub open spec fn mem_units(m: MemOp, nl: nat, h: nat) -> Us { + ones(mem_pre(m, nl, h)) + seq![mem_check(m, nl, h)] + ones(mem_tail(m, nl, h)) + seq![ + seq![b_i(code_len(exit_seq(mem_exit_code(m))) as int + 4)], + exit_seq(mem_exit_code(m)), + ] +} + +/// **[`ctmpl`] as units.** +pub open spec fn utmpl(n: Node, nl: nat, h: nat, offs: Seq) -> Us + decreases n, +{ + match n { + Node::Op(o) => ones(tmpl(o, nl, h)), + Node::Mem(m) => mem_units(m, nl, h), + Node::Then(a, b) => utmpl(*a, nl, h, offs) + utmpl( + *b, + nl, + flow_h(*a, nl, h, offs.len()), + shift(offs, csize(*a, nl, h, offs.len()) as int), + ), + Node::Block(b) => utmpl(*b, nl, h, seq![csize(*b, nl, h, offs.len() + 1) as int] + offs), + Node::Loop(b) => utmpl(*b, nl, h, seq![0int] + offs), + Node::If(t, e) => { + let st = csize(*t, nl, (h - 1) as nat, offs.len() + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, offs.len() + 1) as int; + ones(cond_pre(nl, h, true, st + 8)) + utmpl( + *t, + nl, + (h - 1) as nat, + seq![st + 4 + se] + shift(offs, 8), + ) + seq![seq![b_i(se + 4)]] + utmpl( + *e, + nl, + (h - 1) as nat, + seq![se] + shift(offs, 12 + st), + ) + }, + Node::Br(_) => ones(ctmpl(n, nl, h, offs)), + Node::BrIf(_) => ones(ctmpl(n, nl, h, offs)), + Node::BrTable(_, _) => ones(ctmpl(n, nl, h, offs)), + Node::Exit(c, _) => seq![exit_seq(c as u16)], + } +} + +/// The byte offset of the stub of a memory instruction: after the operands, +/// the check, the access and the `jmp`. +pub open spec fn stub_off(m: MemOp, nl: nat, h: nat) -> int { + (code_len(mem_pre(m, nl, h)) + code_len(mem_check(m, nl, h)) + code_len(mem_tail(m, nl, h)) + + 4) as int +} + +/// **Where a jump of the node may land**, other than at a label it was +/// given, as a byte position from the node's first byte. +pub open spec fn bs(n: Node, nl: nat, h: nat, d: nat, x: int) -> bool + decreases n, +{ + match n { + Node::Mem(m) => x == 0 || x == stub_off(m, nl, h) || x == csize(n, nl, h, d) as int, + Node::Then(a, b) => bs(*a, nl, h, d, x) || bs( + *b, + nl, + flow_h(*a, nl, h, d), + d, + x - csize(*a, nl, h, d) as int, + ), + Node::Block(b) => bs(*b, nl, h, d + 1, x), + Node::Loop(b) => bs(*b, nl, h, d + 1, x), + Node::If(t, e) => { + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + x == 0 || bs(*t, nl, (h - 1) as nat, d + 1, x - 8) || bs( + *e, + nl, + (h - 1) as nat, + d + 1, + x - (12 + st), + ) + }, + _ => x == 0 || x == csize(n, nl, h, d) as int, + } +} + +/// **Where no jump lands**: inside a memory instruction, the start of the +/// check and the start of the access. +pub open spec fn prot(n: Node, nl: nat, h: nat, d: nat, x: int) -> bool + decreases n, +{ + match n { + Node::Mem(m) => x == code_len(mem_pre(m, nl, h)) as int || x == (code_len( + mem_pre(m, nl, h), + ) + code_len(mem_check(m, nl, h))) as int, + Node::Then(a, b) => prot(*a, nl, h, d, x) || prot( + *b, + nl, + flow_h(*a, nl, h, d), + d, + x - csize(*a, nl, h, d) as int, + ), + Node::Block(b) => prot(*b, nl, h, d + 1, x), + Node::Loop(b) => prot(*b, nl, h, d + 1, x), + Node::If(t, e) => { + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + prot(*t, nl, (h - 1) as nat, d + 1, x - 8) || prot( + *e, + nl, + (h - 1) as nat, + d + 1, + x - (12 + st), + ) + }, + _ => false, + } +} + +// ---------------------------------------------------------------------------- +// The units are the template +// ---------------------------------------------------------------------------- + +/// The instructions of a one-unit list. +pub proof fn lemma_uflat_one(u: Seq) + ensures + uflat(seq![u]) =~= u, +{ + let s = seq![u]; + assert(s.skip(1) =~= Seq::>::empty()); + assert(uflat(s) =~= u + uflat(s.skip(1))); + assert(u + Seq::::empty() =~= u); +} + +/// The instructions of a two-unit list. +pub proof fn lemma_uflat_two(u: Seq, v: Seq) + ensures + uflat(seq![u, v]) =~= u + v, +{ + let s = seq![u, v]; + assert(s.skip(1) =~= seq![v]); + lemma_uflat_one(v); + assert(uflat(s) =~= u + uflat(s.skip(1))); +} + +/// The units of a memory instruction are its code. +pub proof fn lemma_mem_units_flat(m: MemOp, nl: nat, h: nat) + ensures + uflat(mem_units(m, nl, h)) =~= mem_code(m, nl, h), +{ + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![b_i(code_len(exit_seq(mem_exit_code(m))) as int + 4)]; + let stub = exit_seq(mem_exit_code(m)); + lemma_uflat_ones(pre); + lemma_uflat_ones(tail); + lemma_uflat_one(chk); + lemma_uflat_two(jm, stub); + lemma_uflat_cat(ones(pre), seq![chk]); + lemma_uflat_cat(ones(pre) + seq![chk], ones(tail)); + lemma_uflat_cat(ones(pre) + seq![chk] + ones(tail), seq![jm, stub]); + assert(uflat(mem_units(m, nl, h)) =~= pre + chk + tail + (jm + stub)); + assert(mem_code(m, nl, h) =~= pre + chk + tail + jm + stub); +} + +/// **The units of [`utmpl`] are the instructions of [`ctmpl`].** +pub proof fn lemma_utmpl_flat(n: Node, nl: nat, h: nat, offs: Seq) + ensures + uflat(utmpl(n, nl, h, offs)) =~= ctmpl(n, nl, h, offs), + decreases n, +{ + let d = offs.len(); + match n { + Node::Op(o) => { + lemma_uflat_ones(tmpl(o, nl, h)); + }, + Node::Mem(m) => { + lemma_mem_units_flat(m, nl, h); + }, + Node::Then(a, b) => { + let ob = shift(offs, csize(*a, nl, h, d) as int); + lemma_utmpl_flat(*a, nl, h, offs); + lemma_utmpl_flat(*b, nl, flow_h(*a, nl, h, d), ob); + lemma_uflat_cat( + utmpl(*a, nl, h, offs), + utmpl(*b, nl, flow_h(*a, nl, h, d), ob), + ); + }, + Node::Block(b) => { + lemma_utmpl_flat(*b, nl, h, seq![csize(*b, nl, h, d + 1) as int] + offs); + }, + Node::Loop(b) => { + lemma_utmpl_flat(*b, nl, h, seq![0int] + offs); + }, + Node::If(t, e) => { + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 4 + se] + shift(offs, 8); + let oe = seq![se] + shift(offs, 12 + st); + let cp = cond_pre(nl, h, true, st + 8); + lemma_utmpl_flat(*t, nl, (h - 1) as nat, ot); + lemma_utmpl_flat(*e, nl, (h - 1) as nat, oe); + lemma_uflat_ones(cp); + lemma_uflat_one(seq![b_i(se + 4)]); + lemma_uflat_cat(ones(cp), utmpl(*t, nl, (h - 1) as nat, ot)); + lemma_uflat_cat(ones(cp) + utmpl(*t, nl, (h - 1) as nat, ot), seq![seq![b_i(se + 4)]]); + lemma_uflat_cat( + ones(cp) + utmpl(*t, nl, (h - 1) as nat, ot) + seq![seq![b_i(se + 4)]], + utmpl(*e, nl, (h - 1) as nat, oe), + ); + assert(uflat(utmpl(n, nl, h, offs)) =~= cp + ctmpl(*t, nl, (h - 1) as nat, ot) + seq![ + b_i(se + 4), + ] + ctmpl(*e, nl, (h - 1) as nat, oe)); + }, + Node::Br(_) => { + lemma_uflat_ones(ctmpl(n, nl, h, offs)); + }, + Node::BrIf(_) => { + lemma_uflat_ones(ctmpl(n, nl, h, offs)); + }, + Node::BrTable(_, _) => { + lemma_uflat_ones(ctmpl(n, nl, h, offs)); + }, + Node::Exit(c, _) => { + lemma_uflat_one(exit_seq(c as u16)); + }, + } +} + +/// The byte length of the units is `csize`. +pub proof fn lemma_utmpl_blen(n: Node, nl: nat, h: nat, offs: Seq) + ensures + blen(utmpl(n, nl, h, offs)) == csize(n, nl, h, offs.len()), +{ + lemma_utmpl_flat(n, nl, h, offs); + lemma_blen_flat(utmpl(n, nl, h, offs)); + super::cflens::lemma_ctmpl_len(n, nl, h, offs); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfalg.rs b/crates/clazel-jit/src/a64/cfalg.rs new file mode 100644 index 000000000..85c5c7c9f --- /dev/null +++ b/crates/clazel-jit/src/a64/cfalg.rs @@ -0,0 +1,231 @@ +//! The algebra of the control-flow statements: [`preserved_mem_r`] is +//! transitive, it carries the region table, the relation survives truncating +//! the stack to a prefix, and a [`cf_refines`] statement can be used at one +//! state. The A64 counterpart of [`crate::cfalg`] (without the native stack +//! word, which the exit sequence does not read). + +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// [`preserved_r`] is [`preserved_mem_r`] with the link register and `SP`. +pub proof fn lemma_pr_mem(lay: lir::Layout, m: isa::State, m2: isa::State) + requires + preserved_r(lay, m, m2), + ensures + preserved_mem_r(lay, m, m2), +{ +} + +/// [`preserved_mem_r`] is transitive. +pub proof fn lemma_pmr_trans(lay: lir::Layout, m: isa::State, m1: isa::State, m2: isa::State) + requires + preserved_mem_r(lay, m, m1), + preserved_mem_r(lay, m1, m2), + ensures + preserved_mem_r(lay, m, m2), +{ + super::steps::lemma_abi_consts(); + assert(ctx_of(m1) == ctx_of(m)); + assert forall|b: int| + #[trigger] m.mem.dom().contains(b) && !in_exit_area(ctx_of(m), b) && !in_regions_i( + lay, + b, + ) implies m2.mem[b] == m.mem[b] by { + assert(m1.mem.dom().contains(b)); + assert(m1.mem[b] == m.mem[b]); + assert(m2.mem[b] == m1.mem[b]); + } +} + +/// [`preserved_r`] is transitive. +pub proof fn lemma_pr_trans(lay: lir::Layout, m: isa::State, m1: isa::State, m2: isa::State) + requires + preserved_r(lay, m, m1), + preserved_r(lay, m1, m2), + ensures + preserved_r(lay, m, m2), +{ + lemma_pmr_trans(lay, m, m1, m2); +} + +/// [`preserved_r`] then [`preserved_mem_r`] is [`preserved_mem_r`]. +pub proof fn lemma_pr_pmr_trans(lay: lir::Layout, m: isa::State, m1: isa::State, m2: isa::State) + requires + preserved_r(lay, m, m1), + preserved_mem_r(lay, m1, m2), + ensures + preserved_mem_r(lay, m, m2), +{ + lemma_pmr_trans(lay, m, m1, m2); +} + +/// A successful read of `n` bytes means each byte is mapped. +pub proof fn read_mem_some_dom(mm: Map, a: int, n: nat, i: int) + requires + isa::mem_read(mm, a, n) is Some, + 0 <= a, + a + n < 0x1_0000_0000_0000_0000, + 0 <= i < n, + ensures + mm.dom().contains(a + i), + decreases n, +{ + super::memlemmas::lemma_trunc64(a); + if i > 0 { + read_mem_some_dom(mm, a + 1, (n - 1) as nat, i - 1); + assert(a + 1 + (i - 1) == a + i); + } +} + +/// A read of mapped bytes that are outside the exit area and every region +/// gives the same result after a run that preserves everything else. +pub proof fn lemma_read_pres( + lay: lir::Layout, + m: isa::State, + m1: isa::State, + a: int, + n: nat, +) + requires + preserved_mem_r(lay, m, m1), + 0 <= a, + a + n < 0x1_0000_0000_0000_0000, + isa::mem_read(m.mem, a, n) is Some, + forall|i: int| + 0 <= i < n ==> !in_exit_area(ctx_of(m), #[trigger] (a + i)) && !in_regions_i( + lay, + a + i, + ), + ensures + isa::mem_read(m1.mem, a, n) == isa::mem_read(m.mem, a, n), +{ + assert forall|i: int| 0 <= i < n implies (m.mem.dom().contains(#[trigger] (a + i)) + == m1.mem.dom().contains(a + i)) && (m.mem.dom().contains(a + i) ==> m.mem[a + i] + == m1.mem[a + i]) by { + read_mem_some_dom(m.mem, a, n, i); + } + super::memlemmas::lemma_mem_read_congr(m.mem, m1.mem, a, n); +} + +/// **The region table survives.** What is outside the exit area and every +/// region is unchanged, and the table is both. +pub proof fn lemma_table_pres(lay: lir::Layout, m: isa::State, m1: isa::State) + requires + table_ok(lay, m), + ctx_ok(m), + preserved_mem_r(lay, m, m1), + ensures + table_ok(lay, m1), +{ + super::steps::lemma_abi_consts(); + super::memlemmas::lemma_p2_consts(); + assert(ctx_of(m1) == ctx_of(m)); + assert forall|k: int| 0 <= k < lay.len() implies { + &&& isa::mem_read(m1.mem, ctx_of(m1) + abi::base_off(k), 8) == Some( + (#[trigger] lay[k]).base as int, + ) + &&& isa::mem_read(m1.mem, ctx_of(m1) + abi::bound_off(k), 8) == Some(lay[k].bound as int) + } by { + let a1 = ctx_of(m) + abi::base_off(k); + let a2 = ctx_of(m) + abi::bound_off(k); + assert(isa::mem_read(m.mem, a1, 8) == Some(lay[k].base as int)); + assert(isa::mem_read(m.mem, a2, 8) == Some(lay[k].bound as int)); + assert forall|i: int| 0 <= i < 8 implies !in_exit_area(ctx_of(m), #[trigger] (a1 + i)) + && !in_regions_i(lay, a1 + i) by { + assert(ctx_of(m) <= a1 + i < ctx_of(m) + abi::table_end()); + } + assert forall|i: int| 0 <= i < 8 implies !in_exit_area(ctx_of(m), #[trigger] (a2 + i)) + && !in_regions_i(lay, a2 + i) by { + assert(ctx_of(m) <= a2 + i < ctx_of(m) + abi::table_end()); + } + lemma_read_pres(lay, m, m1, a1, 8); + lemma_read_pres(lay, m, m1, a2, 8); + } + assert forall|b: int| ctx_of(m1) <= b < ctx_of(m1) + abi::table_end() implies !#[trigger] in_regions_i( + lay, + b, + ) by { + assert(ctx_of(m) <= b < ctx_of(m) + abi::table_end()); + } +} + +/// The relation survives truncating the stack to a prefix. +pub proof fn lemma_rel_prefix( + lay: lir::Layout, + a: lir::St, + full: Seq, + pre: Seq, + m: isa::State, + nl: nat, +) + requires + rel(lay, a.with_stack(full), m, nl), + pre.len() <= full.len(), + pre =~= full.take(pre.len() as int), + ensures + rel(lay, a.with_stack(pre), m, nl), +{ + let a1 = a.with_stack(full); + let a2 = a.with_stack(pre); + assert(crate::rel::slots_agree(a1, a2, nl)) by { + assert forall|j: nat| j < nl + a2.stack.len() implies j < nl + a1.stack.len() + && #[trigger] lir::val_bits(slot_val(a2, nl, j)) == lir::val_bits(slot_val(a1, nl, j)) by { + if j >= nl { + assert(a2.stack[(j - nl) as int] == a1.stack[(j - nl) as int]); + } + } + } + super::rel::lemma_rel_agree(lay, a1, a2, m, nl); +} + +/// What [`rel_base`] says, unfolded (it is opaque elsewhere). +pub proof fn lemma_rel_base_unfold( + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + nl: nat, +) + requires + rel_base(lay, base, a, m, nl), + ensures + rel(lay, a.with_stack(base + a.stack), m, nl), + table_ok(lay, m), + isa::wf(m), + ctx_ok(m), +{ + reveal(rel_base); +} + +/// **A [`cf_refines`] statement, used at one state.** +pub proof fn lemma_use_cf( + i: lir::Instr, + nl: nat, + h: nat, + offs: Seq, + code: Seq, + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, +) + requires + cf_refines(i, nl, h, offs, code), + base.len() + a.stack.len() == h, + rel_base(lay, base, a, m, nl), + cf_canon(m.pc as int, offs, code), + ensures + cf_post(lay, base, nl, offs, code, m, lir::eval(lay, i, a, fuel)), +{ +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfblobacc.rs b/crates/clazel-jit/src/a64/cfblobacc.rs new file mode 100644 index 000000000..80f9c562e --- /dev/null +++ b/crates/clazel-jit/src/a64/cfblobacc.rs @@ -0,0 +1,275 @@ +//! **J.9: the back end never produces a blob the validator refuses**, for A64 +//! (jit.md item J.9): for every program with a translation, the blob the +//! control-flow back end emits ([`super::cfemit::emit_cf_blob`]: the +//! canonical encodings of [`ctmpl`] and the exit sequence) is accepted by the +//! validator's spec ([`clazel_codecheck::a64::spec::accepted`]), from the +//! entry the back end uses (byte 0): [`theorem_blob_accepted`]. The +//! counterpart of [`crate::cfblobacc`], whose header says how it is proven; +//! here the units are an exit sequence (3 instructions), a bounds-check +//! sequence (7) and single instructions, and a branch is relative to its own +//! address. + +#[cfg(verus_only)] +use super::cfaccept::*; +#[cfg(verus_only)] +use super::cfbs::*; +#[cfg(verus_only)] +use super::cffit::labels_fit; +#[cfg(verus_only)] +use super::cfjt::*; +#[cfg(verus_only)] +use super::cflens::*; +#[cfg(verus_only)] +use super::cfproofs::lemma_csize_mod4; +#[cfg(verus_only)] +use super::cfsimple::nojump; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::cfuacc2::*; +#[cfg(verus_only)] +use super::cfunits::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The instructions the back end emits for the program `n` with `nl` locals +/// and the exit code `c` (the function's return label is at the end of the +/// body, where the exit sequence starts). +pub open spec fn emitted_ins(n: Node, nl: nat, c: u16) -> Seq { + ctmpl(n, nl, 0, seq![csize(n, nl, 0, 1) as int]) + exit_seq(c) +} + +/// A list of numbers is a subset of another if each is in it. +proof fn lemma_subset_intro(a: Seq, b: Seq) + requires + forall|x: int| clazel_codecheck::a64::spec::seq_has(a, x) + ==> clazel_codecheck::a64::spec::seq_has(b, x), + ensures + clazel_codecheck::a64::spec::subset_of(a, b), + decreases a.len(), +{ + if a.len() > 0 { + let d = a.drop_last(); + assert(clazel_codecheck::a64::spec::seq_has(a, a.last())); + assert forall|x: int| clazel_codecheck::a64::spec::seq_has(d, x) + implies clazel_codecheck::a64::spec::seq_has(b, x) by { + assert(clazel_codecheck::a64::spec::seq_has(a, x)); + } + lemma_subset_intro(d, b); + } +} + +/// The scan's branch targets are the targets `jt` names. +proof fn lemma_jump_targets_has(ins: Seq, j: int, x: int) + requires + 0 <= j, + ensures + clazel_codecheck::a64::spec::seq_has(clazel_codecheck::a64::spec::jump_targets(ins, j), x) + == (exists|j2: int| + j <= j2 < ins.len() && #[trigger] clazel_codecheck::a64::spec::jump_rel(ins[j2]) is Some + && x == clazel_codecheck::a64::spec::start_of(ins, j2) + + clazel_codecheck::a64::spec::jump_rel(ins[j2])->Some_0), + decreases ins.len() - j, +{ + if j >= ins.len() { + reveal_with_fuel(clazel_codecheck::a64::spec::seq_has, 1); + } else { + lemma_jump_targets_has(ins, j + 1, x); + let rest = clazel_codecheck::a64::spec::jump_targets(ins, j + 1); + match clazel_codecheck::a64::spec::jump_rel(ins[j]) { + Some(r) => { + let here = seq![clazel_codecheck::a64::spec::start_of(ins, j) + r]; + clazel_codecheck::a64::acc_lemmas::lemma_seq_has_concat(here, rest, x); + clazel_codecheck::a64::acc_lemmas::lemma_seq_has_single( + clazel_codecheck::a64::spec::start_of(ins, j) + r, + x, + ); + assert(clazel_codecheck::a64::spec::jump_targets(ins, j) =~= here + rest); + }, + None => { + assert(clazel_codecheck::a64::spec::jump_targets(ins, j) =~= rest); + }, + } + } +} + +/// The targets of the whole blob. +proof fn lemma_targets_has(ins: Seq, x: int) + ensures + clazel_codecheck::a64::spec::seq_has( + clazel_codecheck::a64::spec::all_targets(ins, seq![0int]), + x, + ) == (x == 0 || jt(ins, x)), +{ + lemma_jump_targets_has(ins, 0, x); + clazel_codecheck::a64::acc_lemmas::lemma_seq_has_concat( + seq![0int], + clazel_codecheck::a64::spec::jump_targets(ins, 0), + x, + ); + clazel_codecheck::a64::acc_lemmas::lemma_seq_has_single(0, x); +} + +/// **The validator accepts the instructions** the back end emits for a program +/// with a translation, from entry 0: the policy holds of them. +#[verifier::rlimit(1500)] +pub proof fn theorem_policy(n: Node, nl: nat, c: u16) + requires + cf_prog_ok(n, nl), + ensures + clazel_codecheck::a64::spec::policy(emitted_ins(n, nl, c), seq![0int]), +{ + let size = csize(n, nl, 0, 1); + let offs = seq![size as int]; + assert(offs.len() == 1); + let body = ctmpl(n, nl, 0, offs); + let ins = body + exit_seq(c); + lemma_csize_mod4(n, nl, 0, 1); + assert(labels_fit(offs, size)) by { + assert forall|l: int| 0 <= l < offs.len() implies #[trigger] offs[l] % 4 == 0 && -0x10_0000 + + size + 8 <= offs[l] && offs[l] <= 0xF_FFF0 by { + assert(size <= max_size()); + } + } + // units + let ut = utmpl(n, nl, 0, offs); + let us = ut + seq![exit_seq(c)]; + lemma_utmpl_flat(n, nl, 0, offs); + lemma_uflat_one(exit_seq(c)); + lemma_uflat_cat(ut, seq![exit_seq(c)]); + assert(uflat(us) =~= ins); + lemma_utmpl_uok(n, nl, 0, offs); + lemma_exit_uok(c); + assert(uok_all(seq![exit_seq(c)])) by { + assert forall|k: int| 0 <= k < seq![exit_seq(c)].len() implies uok( + #[trigger] seq![exit_seq(c)][k], + ) by { + assert(k == 0); + } + } + lemma_uok_cat(ut, seq![exit_seq(c)]); + // the branch targets + let p = |x: int| x == 0 || jt(ins, x); + assert forall|x: int| clazel_codecheck::a64::spec::seq_has( + clazel_codecheck::a64::spec::all_targets(ins, seq![0int]), + x, + ) == p(x) by { + lemma_targets_has(ins, x); + } + lemma_bs_facts(n, nl, 0, 1); + lemma_bs_ubi(n, nl, 0, offs); + lemma_jt_bs(n, nl, 0, offs); + lemma_utmpl_blen(n, nl, 0, offs); + // every branch target is a boundary of the body, and not protected + assert forall|x: int| jt(ins, x) implies (bs(n, nl, 0, 1, x) && 0 <= x <= size) by { + lemma_jt_cat(body, exit_seq(c), x); + if jt(exit_seq(c), x - code_len(body)) { + let e = exit_seq(c); + assert(forall|j: int| 0 <= j < e.len() ==> nojump(#[trigger] e[j])) by { + assert forall|j: int| 0 <= j < e.len() implies nojump(e[j]) by { + assert(j == 0 || j == 1 || j == 2); + } + } + lemma_jt_none(e, x - code_len(body)); + } + assert(jt(body, x)); + if inoffs(offs, x) { + assert(x == size as int); + } + } + // the scan + let fs = clazel_codecheck::a64::spec::no_facts(); + assert(pf(n, nl, 0, 1, 0, p)) by { + assert forall|x: int| #[trigger] prot(n, nl, 0, 1, x) implies !p(0 + x) by { + assert(0 < x < size as int); + if jt(ins, x) { + assert(bs(n, nl, 0, 1, x)); + } + } + } + lemma_utmpl_ucheck(n, nl, 0, offs, p); + assert(ucheck(ut, 0, fs, p) is Some); + let f = ucheck(ut, 0, fs, p)->Some_0; + lemma_ucheck_cat(ut, seq![exit_seq(c)], 0, fs, p); + super::cfuacc::lemma_ucheck_one(exit_seq(c), blen(ut) as int, f, p); + assert(exit_seq(c).len() == 3); + assert(ucheck(us, 0, fs, p) is Some); + let tg = clazel_codecheck::a64::spec::all_targets(ins, seq![0int]); + lemma_scan_units(ins, tg, 0, us, 0, fs, p); + assert(clazel_codecheck::a64::spec::start_of(ins, 0) == 0); + let starts = ustarts(us, 0); + assert(clazel_codecheck::a64::spec::scan(ins, tg, 0, fs) == Some(starts)); + // the targets are unit starts + lemma_blen_cat(ut, seq![exit_seq(c)]); + lemma_blen_one(exit_seq(c)); + assert forall|x: int| clazel_codecheck::a64::spec::seq_has(tg, x) + implies clazel_codecheck::a64::spec::seq_has(starts, x) by { + lemma_seq_has_ustarts(us, 0, x); + assert(p(x)); + if x == 0 { + assert(us.len() > 0); + assert(ub(us, 0, 0)); + } else { + assert(jt(ins, x)); + assert(bs(n, nl, 0, 1, x)); + assert(ubi(ut, 0, 0 + x)); + lemma_ubi_cat(ut, seq![exit_seq(c)], 0, x); + assert(x <= size as int); + assert(blen(ut) == size); + assert(x < blen(us)); + assert(ub(us, 0, x)); + } + } + lemma_subset_intro(tg, starts); + assert(ins.last() is Ret) by { + assert(ins[ins.len() - 1] == exit_seq(c)[2]); + } + assert(ins.len() > 0); + assert(clazel_codecheck::a64::spec::policy(ins, seq![0int])); +} + +/// **The validator accepts the blob**: any bytes that are the canonical +/// encodings of the instructions the back end emits are accepted, from entry +/// 0. +pub proof fn theorem_blob_accepted(n: Node, nl: nat, c: u16, bytes: Seq) + requires + cf_prog_ok(n, nl), + clazel_codecheck::a64::spec::decodes_to(bytes, emitted_ins(n, nl, c)), + ensures + clazel_codecheck::a64::spec::accepted(bytes, seq![0int]), +{ + theorem_policy(n, nl, c); +} + +/// **The emitter's blob is accepted** (`emit_cf_blob` carries this as its +/// own postcondition). +pub open spec fn emit_cf_blob_accepted_ensures( + n: Node, + nl: usize, + exit_code: u16, + r: Option>, +) -> bool { + match r { + Some(b) => clazel_codecheck::a64::spec::accepted(b, seq![0int]), + None => true, + } +} + +/// From the emitter's two postconditions to the validator's acceptance. +pub proof fn lemma_emitter_accepted(n: Node, nl: usize, exit_code: u16, bytes: Seq) + requires + super::cfcontract::emit_cf_blob_ensures(n, nl, exit_code, Some(bytes)), + clazel_codecheck::a64::spec::decodes_to(bytes, emitted_ins(n, nl as nat, exit_code)), + ensures + emit_cf_blob_accepted_ensures(n, nl, exit_code, Some(bytes)), +{ + theorem_blob_accepted(n, nl as nat, exit_code, bytes); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfblock.rs b/crates/clazel-jit/src/a64/cfblock.rs new file mode 100644 index 000000000..123bb7d91 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfblock.rs @@ -0,0 +1,498 @@ +//! `block` and `loop`: a body's [`cf_post`], on top of the stack the block was +//! entered with, becomes the block's. Four transports (falling out of the +//! body, a branch to the block's own label, a branch past it, an exit), then +//! the two lemmas that use them. + +#[cfg(verus_only)] +use super::cfalg::*; +#[cfg(verus_only)] +use super::cfcompose::*; +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::contract::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfheight::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The body of a block, entered with the outer stack `ast` as its `base`, is +/// related to the same machine state as the block: `rel_base` with the +/// stacks concatenated. +pub proof fn lemma_rel_base_cat( + lay: lir::Layout, + base: Seq, + ast: Seq, + s: lir::St, + m: isa::State, + nl: nat, +) + requires + rel_base(lay, base + ast, s, m, nl), + ensures + rel_base(lay, base, s.with_stack(ast + s.stack), m, nl), +{ + reveal(rel_base); + assert((base + ast) + s.stack =~= base + (ast + s.stack)); + assert(s.with_stack(ast + s.stack).with_stack(base + (ast + s.stack)) == s.with_stack( + base + (ast + s.stack), + )); +} + +/// The block's start state, as the body's: the outer stack `a.stack` becomes +/// the body's `base`, and the body's own stack is empty. +pub proof fn lemma_rel_base_enter( + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + nl: nat, +) + requires + rel_base(lay, base, a, m, nl), + ensures + rel_base(lay, base + a.stack, fresh(a), m, nl), +{ + reveal(rel_base); + assert(base + a.stack + Seq::::empty() =~= base + a.stack); + assert(fresh(a).with_stack(base + a.stack + fresh(a).stack) == a.with_stack(base + a.stack)); +} + +/// Concatenation of stacks is associative inside [`rel_base`]. +pub proof fn lemma_rel_base_assoc( + lay: lir::Layout, + base: Seq, + x: Seq, + y: Seq, + s: lir::St, + m: isa::State, + nl: nat, +) + ensures + rel_base(lay, base + x + y, s, m, nl) == rel_base(lay, base + (x + y), s, m, nl), +{ + reveal(rel_base); + assert(base + x + y =~= base + (x + y)); +} + +/// **Falling out of the body.** The body's `Normal(s)` is the block's +/// `Normal(s)` on top of the outer stack. +pub proof fn lemma_blk_normal( + lay: lir::Layout, + base: Seq, + ast: Seq, + nl: nat, + offs: Seq, + c: Seq, + m: isa::State, + s: lir::St, + offs_b: Seq, +) + requires + cf_post(lay, base + ast, nl, offs_b, c, m, lir::Outcome::Normal(s)), + ensures + cf_post(lay, base, nl, offs, c, m, lir::Outcome::Normal(s.with_stack(ast + s.stack))), +{ + let m2 = choose|m2: isa::State| + reaches(c, m.pc as int, m, m2) && m2.pc as int == m.pc as int + code_len(c) && rel_base( + lay, + base + ast, + s, + m2, + nl, + ) && preserved_r(lay, m, m2); + lemma_rel_base_cat(lay, base, ast, s, m2, nl); +} + +/// Seen from outside the block, the body's extra values include the block's +/// outer stack. +pub proof fn lemma_rel_ext_cat( + lay: lir::Layout, + base: Seq, + ast: Seq, + s: lir::St, + m2: isa::State, + nl: nat, +) + requires + rel_ext(lay, base + ast, s, m2, nl), + ensures + rel_ext(lay, base, s, m2, nl), +{ + let extra = choose|extra: Seq| rel_base(lay, base + ast + extra, s, m2, nl); + lemma_rel_base_assoc(lay, base, ast, extra, s, m2, nl); + let extra2 = ast + extra; + assert(rel_base(lay, base + extra2, s, m2, nl)); +} + +/// As [`lemma_rel_ext_cat`], for an exit's operands. +pub proof fn lemma_exit_rel_cat( + lay: lir::Layout, + base: Seq, + ast: Seq, + s: lir::St, + m2: isa::State, + nl: nat, + ctxb: int, + vals: Seq, +) + requires + exit_rel(lay, base + ast, s, m2, nl, ctxb, vals), + ensures + exit_rel(lay, base, s, m2, nl, ctxb, vals), +{ + let extra = choose|extra: Seq| + rel_base(lay, base + ast + extra, s, m2, nl) && ops_in_frame( + m2.mem, + ctxb, + nl, + (base + ast + extra).len() + s.stack.len(), + vals, + ); + lemma_rel_base_assoc(lay, base, ast, extra, s, m2, nl); + let extra2 = ast + extra; + assert(rel_base(lay, base + extra2, s, m2, nl)); + assert((base + extra2).len() == (base + ast + extra).len()); +} + +/// The relation part of a branch to the block's own label: with the body's +/// base extended by the outer stack and some extra values, the outer stack +/// alone is related. +pub proof fn lemma_br0_rel( + lay: lir::Layout, + base: Seq, + ast: Seq, + s: lir::St, + m2: isa::State, + nl: nat, +) + requires + rel_ext(lay, base + ast, s, m2, nl), + ensures + rel_base(lay, base, s.with_stack(ast), m2, nl), +{ + reveal(rel_base); + let extra = choose|extra: Seq| rel_base(lay, base + ast + extra, s, m2, nl); + let full = base + ast + extra + s.stack; + assert(rel(lay, s.with_stack(full), m2, nl)); + assert(full.take((base + ast).len() as int) =~= base + ast); + lemma_rel_prefix(lay, s, full, base + ast, m2, nl); + assert(s.with_stack(ast).with_stack(base + ast) == s.with_stack(base + ast)); +} + +/// **A branch to the block's own label** (label 0 of the body) is the block +/// ending: the state is related to the outer stack alone, at the end of the +/// block's code (`offs_b[0]` is its length). +pub proof fn lemma_blk_br0( + lay: lir::Layout, + base: Seq, + ast: Seq, + nl: nat, + offs: Seq, + c: Seq, + m: isa::State, + s: lir::St, + offs_b: Seq, +) + requires + offs_b.len() > 0, + offs_b[0] == code_len(c), + cf_post(lay, base + ast, nl, offs_b, c, m, lir::Outcome::Branch(0, s)), + ensures + cf_post(lay, base, nl, offs, c, m, lir::Outcome::Normal(s.with_stack(ast))), +{ + let m2 = choose|m2: isa::State| + reaches(c, m.pc as int, m, m2) && m2.pc as int == m.pc as int + offs_b[0] && rel_ext( + lay, + base + ast, + s, + m2, + nl, + ) && preserved_r(lay, m, m2); + lemma_br0_rel(lay, base, ast, s, m2, nl); +} + +/// **A branch past the block** (label `n + 1` of the body is the block's +/// label `n`): still a branch, on top of more values. +pub proof fn lemma_blk_brn( + lay: lir::Layout, + base: Seq, + ast: Seq, + nl: nat, + offs: Seq, + c: Seq, + m: isa::State, + n: nat, + s: lir::St, + offs_b: Seq, +) + requires + n > 0, + n < offs_b.len(), + ((n - 1) as nat) < offs.len(), + offs_b[n as int] == offs[((n - 1) as nat) as int], + cf_post(lay, base + ast, nl, offs_b, c, m, lir::Outcome::Branch(n, s)), + ensures + cf_post(lay, base, nl, offs, c, m, lir::Outcome::Branch((n - 1) as nat, s)), +{ + let m2 = choose|m2: isa::State| + reaches(c, m.pc as int, m, m2) && m2.pc as int == m.pc as int + offs_b[n as int] && rel_ext( + lay, + base + ast, + s, + m2, + nl, + ) && preserved_r(lay, m, m2); + lemma_rel_ext_cat(lay, base, ast, s, m2, nl); +} + +/// **An exit inside the body** is the block's exit, on top of more values. +pub proof fn lemma_blk_exit( + lay: lir::Layout, + base: Seq, + ast: Seq, + nl: nat, + offs: Seq, + c: Seq, + m: isa::State, + code: nat, + vals: Seq, + s: lir::St, + offs_b: Seq, +) + requires + cf_post(lay, base + ast, nl, offs_b, c, m, lir::Outcome::Exit(code, vals, s)), + ensures + cf_post(lay, base, nl, offs, c, m, lir::Outcome::Exit(code, vals, s)), +{ + let m2 = choose|m2: isa::State| + reaches(c, m.pc as int, m, m2) && exit_done(m, m2, code) && exit_rel( + lay, + base + ast, + s, + m2, + nl, + ctx_of(m), + vals, + ) && preserved_mem_r(lay, m, m2); + lemma_exit_rel_cat(lay, base, ast, s, m2, nl, ctx_of(m), vals); +} + +/// **`block`.** If the body's code refines the body with the block's label +/// added at the end of the code, the code refines the `block`. +#[verifier::rlimit(400)] +pub proof fn lemma_block(b: Node, nl: nat, h: nat, offs: Seq, cb: Seq) + requires + cf_refines(b@, nl, h, seq![code_len(cb) as int] + offs, cb), + ensures + cf_refines(Node::Block(Box::new(b))@, nl, h, offs, cb), +{ + super::memlemmas::lemma_p2_consts(); + let t = Node::Block(Box::new(b)); + let offs_b = seq![code_len(cb) as int] + offs; + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, cb, m, lir::eval(lay, t@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) + && cf_canon(m.pc as int, offs, cb) implies cf_post( + lay, + base, + nl, + offs, + cb, + m, + lir::eval(lay, t@, a, fuel), + ) by { + if fuel > 0 { + let f = (fuel - 1) as nat; + lemma_eval_block(lay, b, a, fuel); + lemma_rel_base_enter(lay, base, a, m, nl); + assert(cf_canon(m.pc as int, offs_b, cb)) by { + assert forall|l: int| 0 <= l < offs_b.len() implies 0 <= m.pc as int + + #[trigger] offs_b[l] && m.pc as int + offs_b[l] < isa::p2(64) && offs_b[l] + % 4 == 0 by { + if l > 0 { + assert(offs_b[l] == offs[l - 1]); + } else { + assert(offs_b[0] == code_len(cb) as int); + } + } + } + assert((base + a.stack).len() + fresh(a).stack.len() == h); + lemma_use_cf(b@, nl, h, offs_b, cb, lay, base + a.stack, fresh(a), m, f); + let o = lir::eval(lay, b@, fresh(a), f); + match o { + lir::Outcome::Normal(s) => { + lemma_blk_normal(lay, base, a.stack, nl, offs, cb, m, s, offs_b); + }, + lir::Outcome::Branch(n, s) => { + if n == 0 { + assert(lir::top(s.stack, 0) =~= Seq::::empty()); + assert(a.stack + lir::top(s.stack, 0) =~= a.stack); + lemma_blk_br0(lay, base, a.stack, nl, offs, cb, m, s, offs_b); + } else { + assert(n < offs_b.len()); + assert(((n - 1) as nat) < offs.len()); + assert(offs_b[n as int] == offs[((n - 1) as nat) as int]); + lemma_blk_brn(lay, base, a.stack, nl, offs, cb, m, n, s, offs_b); + } + }, + lir::Outcome::Exit(c, vals, s) => { + lemma_blk_exit(lay, base, a.stack, nl, offs, cb, m, c, vals, s, offs_b); + }, + _ => {}, + } + } + } +} + +/// **`loop`, at one fuel.** By induction on the fuel: a branch to the loop's +/// label restarts it, from the state after the body, which is related to the +/// loop's own entry state with the outer stack alone. +#[verifier::rlimit(400)] +pub proof fn lemma_loop_fuel( + b: Node, + nl: nat, + h: nat, + offs: Seq, + cb: Seq, + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, +) + requires + cf_refines(b@, nl, h, seq![0int] + offs, cb), + base.len() + a.stack.len() == h, + rel_base(lay, base, a, m, nl), + cf_canon(m.pc as int, offs, cb), + ensures + cf_post(lay, base, nl, offs, cb, m, lir::eval(lay, Node::Loop(Box::new(b))@, a, fuel)), + decreases fuel, +{ + super::memlemmas::lemma_p2_consts(); + let t = Node::Loop(Box::new(b)); + let offs_b = seq![0int] + offs; + if fuel > 0 { + let f = (fuel - 1) as nat; + lemma_eval_loop(lay, b, a, fuel); + lemma_rel_base_enter(lay, base, a, m, nl); + lemma_rel_base_unfold(lay, base, a, m, nl); + assert(cf_canon(m.pc as int, offs_b, cb)) by { + assert forall|l: int| 0 <= l < offs_b.len() implies 0 <= m.pc as int + + #[trigger] offs_b[l] && m.pc as int + offs_b[l] < isa::p2(64) && offs_b[l] + % 4 == 0 by { + if l > 0 { + assert(offs_b[l] == offs[l - 1]); + } else { + assert(offs_b[0] == 0); + } + } + } + assert((base + a.stack).len() + fresh(a).stack.len() == h); + lemma_use_cf(b@, nl, h, offs_b, cb, lay, base + a.stack, fresh(a), m, f); + let restart = Some(lir::Instr::Loop(void_bt(), Box::new(b@))); + let o = lir::eval(lay, b@, fresh(a), f); + match o { + lir::Outcome::Normal(s) => { + lemma_blk_normal(lay, base, a.stack, nl, offs, cb, m, s, offs_b); + }, + lir::Outcome::Branch(n, s) => { + if n > 0 { + assert(n < offs_b.len()); + assert(((n - 1) as nat) < offs.len()); + assert(offs_b[n as int] == offs[((n - 1) as nat) as int]); + lemma_blk_brn(lay, base, a.stack, nl, offs, cb, m, n, s, offs_b); + } else { + assert(lir::top(s.stack, 0) =~= Seq::::empty()); + assert(a.stack + lir::top(s.stack, 0) =~= a.stack); + if f > 0 { + // the machine is back at the start of the loop + let m1 = choose|m1: isa::State| + reaches(cb, m.pc as int, m, m1) && m1.pc as int == m.pc as int + offs_b[0] + && rel_ext(lay, base + a.stack, s, m1, nl) + && preserved_r(lay, m, m1); + let extra = choose|extra: Seq| + rel_base(lay, base + a.stack + extra, s, m1, nl); + reveal(rel_base); + let full = base + a.stack + extra + s.stack; + assert(rel(lay, s.with_stack(full), m1, nl)); + assert(full.take((base + a.stack).len() as int) =~= base + a.stack); + lemma_rel_prefix(lay, s, full, base + a.stack, m1, nl); + let a1 = s.with_stack(a.stack); + assert(a1.with_stack(base + a1.stack) == s.with_stack(base + a.stack)); + lemma_table_pres(lay, m, m1); + assert(rel_base(lay, base, a1, m1, nl)); + assert(m1.pc as int == m.pc as int); + lemma_loop_fuel(b, nl, h, offs, cb, lay, base, a1, m1, (f - 1) as nat); + assert(Seq::::empty() + cb =~= cb); + assert(code_len(Seq::::empty()) == 0); + assert forall|l: int| 0 <= l < offs.len() implies #[trigger] offs[l] + + code_len(Seq::::empty()) == offs[l] by {} + lemma_lift_pre( + lay, + base, + nl, + offs, + offs, + Seq::empty(), + cb, + m, + m1, + lir::eval(lay, t@, a1, (f - 1) as nat), + ); + } + } + }, + lir::Outcome::Exit(c, vals, s) => { + lemma_blk_exit(lay, base, a.stack, nl, offs, cb, m, c, vals, s, offs_b); + }, + _ => {}, + } + } +} + +/// **`loop`.** If the body's code refines the body with the loop's label +/// added at the start of the code, the code refines the `loop`. +pub proof fn lemma_loop(b: Node, nl: nat, h: nat, offs: Seq, cb: Seq) + requires + cf_refines(b@, nl, h, seq![0int] + offs, cb), + ensures + cf_refines(Node::Loop(Box::new(b))@, nl, h, offs, cb), +{ + let t = Node::Loop(Box::new(b)); + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, cb, m, lir::eval(lay, t@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) + && cf_canon(m.pc as int, offs, cb) implies cf_post( + lay, + base, + nl, + offs, + cb, + m, + lir::eval(lay, t@, a, fuel), + ) by { + lemma_loop_fuel(b, nl, h, offs, cb, lay, base, a, m, fuel); + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfbrt.rs b/crates/clazel-jit/src/a64/cfbrt.rs new file mode 100644 index 000000000..9792f2ca5 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfbrt.rs @@ -0,0 +1,442 @@ +//! **`br_table` refines its LIR instruction** ([`lemma_brt_cf`]): the code is +//! [`super::cftemplates::brt_code`], `ldr x0, [index]`, a `movz`, `cmp` and +//! `b.eq` per target, and a `b` to the default (the subset has no indirect +//! branch). The proof runs the chain by induction on the entry. The A64 +//! counterpart of [`crate::cfbrt`]; each entry is 3 words, so entry `i` is at +//! word `1 + 3 i` and the final `b` at word `1 + 3 n`. + +#[cfg(verus_only)] +use super::cfalg::*; +#[cfg(verus_only)] +use super::cfchk::*; +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cffit::*; +#[cfg(verus_only)] +use super::cfleaf::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cfsteps::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The chain from entry `k` is 12 bytes per entry left, and entry `i`'s words +/// are at `3 (i - k)` in it. +pub proof fn lemma_entries_at(ls: Seq, offs: Seq, p0: int, k: nat) + ensures + brt_entries(ls, offs, p0, k).len() == if k >= ls.len() { + 0nat + } else { + (3 * (ls.len() - k)) as nat + }, + forall|i: nat, t: int| + k <= i < ls.len() && 0 <= t < 3 ==> #[trigger] brt_entries(ls, offs, p0, k)[3 * (i + - k) + t] == brt_entry( + i, + brt_rel(offs, ls[i as int], p0 + i * brt_e() + 8), + )[t], + decreases ls.len() - k, +{ + if k < ls.len() { + let e = brt_entry(k, brt_rel(offs, ls[k as int], p0 + k * brt_e() + 8)); + lemma_entries_at(ls, offs, p0, k + 1); + let rest = brt_entries(ls, offs, p0, k + 1); + let ch = brt_entries(ls, offs, p0, k); + assert(ch == e + rest); + assert(e.len() == 3); + assert forall|i: nat, t: int| + k <= i < ls.len() && 0 <= t < 3 implies #[trigger] ch[3 * (i - k) + t] == brt_entry( + i, + brt_rel(offs, ls[i as int], p0 + i * brt_e() + 8), + )[t] by { + if i == k { + } else { + assert(ch[3 * (i - k) + t] == rest[3 * (i - (k + 1)) + t]); + } + } + } +} + +/// **The shape of the whole code**: the load, entry `i`'s words at `1 + 3 i`, +/// and the final `b` at `1 + 3 n`. +pub proof fn lemma_brt_shape(ls: Seq, d: usize, nl: nat, h: nat, offs: Seq) + ensures + ({ + let w = brt_code(ls, d, nl, h, offs); + let n = ls.len(); + &&& w.len() == 2 + 3 * n + &&& w[0] == load_slot(x0(), (nl + h - 1) as nat) + &&& forall|i: nat, t: int| + i < n && 0 <= t < 3 ==> #[trigger] w[(1 + 3 * i) as int + t] == brt_entry( + i, + brt_rel(offs, ls[i as int], (4 + i * 12 + 8) as int), + )[t] + &&& w[(1 + 3 * n) as int] == b_i(brt_rel(offs, d, (4 + n * 12) as int)) + }), +{ + assert(brt_p0(nl, h) == 4); + assert(brt_e() == 12); + let p0 = brt_p0(nl, h); + lemma_entries_at(ls, offs, p0, 0); + let a = seq![load_slot(x0(), (nl + h - 1) as nat)]; + let ch = brt_entries(ls, offs, p0, 0); + let jm = seq![b_i(brt_rel(offs, d, p0 + ls.len() * brt_e()))]; + let w = brt_code(ls, d, nl, h, offs); + assert(w == a + ch + jm); + let n = ls.len(); + assert forall|i: nat, t: int| + i < n && 0 <= t < 3 implies #[trigger] w[(1 + 3 * i) as int + t] == brt_entry( + i, + brt_rel(offs, ls[i as int], (4 + i * 12 + 8) as int), + )[t] by { + assert(3 * (i - 0) + t == 3 * i + t); + assert(ch[3 * (i - 0) + t] == brt_entry( + i, + brt_rel(offs, ls[i as int], p0 + i * brt_e() + 8), + )[t]); + assert(p0 + i * brt_e() + 8 == (4 + i * 12 + 8) as int); + } +} + +/// What the chain needs of the labels: in scope, within a conditional +/// branch's reach of every place in the code, aligned and addresses. +pub open spec fn brt_labels_ok(ls: Seq, d: usize, offs: Seq, b0: int) -> bool { + d < offs.len() && (forall|q: int| 0 <= q < ls.len() ==> #[trigger] ls[q] < offs.len()) + && labels_fit(offs, 8 + 12 * ls.len()) && (forall|l: int| + 0 <= l < offs.len() ==> 0 <= b0 + #[trigger] offs[l] && b0 + offs[l] + < 0x1_0000_0000_0000_0000) && 0 <= b0 && b0 + 8 + 12 * ls.len() + < 0x1_0000_0000_0000_0000 +} + +/// What the chain leaves alone: everything but `x1` (and the flags, `pc`). +pub open spec fn brt_fr(s: isa::State, s2: isa::State) -> bool { + s2.r.len() == 31 && s.r.len() == 31 && s2.vreg == s.vreg && s2.mem == s.mem && s2.sp == s.sp + && forall|k: int| 0 <= k < 31 && k != 1 ==> #[trigger] s2.r[k] == s.r[k] +} + +/// [`brt_fr`] is transitive. +pub proof fn lemma_brt_fr_trans(s: isa::State, s1: isa::State, s2: isa::State) + requires + brt_fr(s, s1), + brt_fr(s1, s2), + ensures + brt_fr(s, s2), +{ +} + +/// **The final `b`** of the chain, to the default label. +pub proof fn lemma_brt_last( + ls: Seq, + d: usize, + nl: nat, + h: nat, + offs: Seq, + s: isa::State, + b0: int, +) + requires + ls.len() <= max_targets(), + brt_labels_ok(ls, d, offs, b0), + isa::wf(s), + s.pc as int == b0 + 4 + 12 * ls.len(), + ensures + ({ + let whole = brt_code(ls, d, nl, h, offs); + exists|me: isa::State| + #![trigger reaches(whole, b0, s, me)] + reaches(whole, b0, s, me) && isa::wf(me) && brt_fr(s, me) && me.pc as int == b0 + + offs[d as int] + }), +{ + super::memlemmas::lemma_p2_consts(); + let whole = brt_code(ls, d, nl, h, offs); + let n = ls.len(); + lemma_brt_shape(ls, d, nl, h, offs); + let rel = brt_rel(offs, d, (4 + n * 12) as int); + assert(rel == offs[d as int] - (4 + n * 12)); + assert(rel % 4 == 0); + assert(0 <= b0 + offs[d as int] < 0x1_0000_0000_0000_0000); + lemma_step_b(s, rel); + let me = isa::State { pc: (s.pc as int + rel) as u64, ..s }; + assert(whole[(1 + 3 * n) as int] == b_i(rel)); + lemma_reach_step(whole, b0, s, (1 + 3 * n) as int, me); +} + +/// **One entry of the chain**: `movz w1, #i; cmp w0, w1; b.eq`. The run reaches +/// the label of target `i` when `x0 == i`, else the next entry. +#[verifier::rlimit(800)] +pub proof fn lemma_brt_entry( + ls: Seq, + d: usize, + nl: nat, + h: nat, + offs: Seq, + i: nat, + c: u32, + s: isa::State, + b0: int, +) + requires + i < ls.len(), + ls.len() <= max_targets(), + brt_labels_ok(ls, d, offs, b0), + isa::wf(s), + s.pc as int == b0 + 4 + 12 * i, + s.r[0] == c as u64, + ensures + ({ + let whole = brt_code(ls, d, nl, h, offs); + exists|s3: isa::State| + #![trigger reaches(whole, b0, s, s3)] + reaches(whole, b0, s, s3) && isa::wf(s3) && brt_fr(s, s3) && s3.pc as int == ( + if c as nat == i { + b0 + offs[ls[i as int] as int] + } else { + b0 + 4 + 12 * (i + 1) + }) + }), +{ + super::memlemmas::lemma_p2_consts(); + let whole = brt_code(ls, d, nl, h, offs); + let n = ls.len(); + lemma_brt_shape(ls, d, nl, h, offs); + let li = ls[i as int]; + assert(li < offs.len()); + let pos: int = (4 + i * 12 + 8) as int; + let rel = brt_rel(offs, li, pos); + assert(rel == offs[li as int] - pos); + assert(rel % 4 == 0); + assert(-0x10_0000 <= rel < 0x10_0000); + let e = brt_entry(i, rel); + assert(whole[(1 + 3 * i) as int + 0] == brt_entry(i, brt_rel(offs, ls[i as int], pos))[0]); + assert(whole[(1 + 3 * i) as int] == e[0]); + assert(whole[(1 + 3 * i) as int + 1] == e[1]); + assert(whole[(1 + 3 * i) as int + 2] == e[2]); + assert(i < 0x1_0000); + // movz w1, #i + super::bitmid::lemma_step_movz(s, isa::Width::W32, 1, i as u16, 0); + assert(super::bitmid::chunk_val(i as u16, 0) == i as int); + let s1 = isa::step(s, e[0]).unwrap(); + assert(s1.r == s.r.update(1, i as u64)); + lemma_reach_step(whole, b0, s, (1 + 3 * i) as int, s1); + // cmp w0, w1 + assert(s1.r[0] == c as u64 && s1.r[1] == i as u64); + assert(e[1] == cmp_rr(isa::Width::W32, 0, 1)); + super::flags::lemma_step_cmp(s1, isa::Width::W32, c as u64, i as u64); + let s2 = isa::step(s1, e[1]).unwrap(); + lemma_reach_step(whole, b0, s1, (1 + 3 * i) as int + 1, s2); + lemma_reaches_trans(whole, b0, s, s1, s2); + super::flags::lemma_cc_icmp(s1, lir::ICmp::Eq, 32, c as int, i as int); + assert(cset_cond(lir::ICmp::Eq) == 1); + assert(isa::condition_holds(s2, 0) == (c as nat == i)) by { + assert(s2 == isa::State { + pc: (s1.pc + 4) as u64, + ..isa::set_nzcv(s1, super::flags::cmp_nzcv(32, c as int, i as int)) + }); + } + // b.eq + assert(e[2] == bcond_i(0, rel)); + assert(0 <= s2.pc as int + rel < 0x1_0000_0000_0000_0000); + lemma_step_bcond(s2, 0, rel); + let s3 = isa::step(s2, e[2]).unwrap(); + lemma_reach_step(whole, b0, s2, (1 + 3 * i) as int + 2, s3); + lemma_reaches_trans(whole, b0, s, s2, s3); +} + +/// **The chain, run from entry `i`**, with `x0` holding `c >= i`: it reaches +/// the label the `br_table` selects, with memory and everything but `x1` as +/// they were. +#[verifier::rlimit(400)] +pub proof fn lemma_brt_chain( + ls: Seq, + d: usize, + nl: nat, + h: nat, + offs: Seq, + i: nat, + c: u32, + s: isa::State, + b0: int, +) + requires + i <= ls.len(), + ls.len() <= max_targets(), + c as nat >= i, + brt_labels_ok(ls, d, offs, b0), + isa::wf(s), + s.pc as int == b0 + 4 + 12 * i, + s.r[0] == c as u64, + ensures + ({ + let whole = brt_code(ls, d, nl, h, offs); + exists|me: isa::State| + #![trigger reaches(whole, b0, s, me)] + reaches(whole, b0, s, me) && isa::wf(me) && brt_fr(s, me) && me.pc as int == b0 + ( + if (c as nat) < ls.len() { + offs[ls[c as int] as int] + } else { + offs[d as int] + }) + }), + decreases ls.len() - i, +{ + let whole = brt_code(ls, d, nl, h, offs); + let n = ls.len(); + if i == n { + lemma_brt_last(ls, d, nl, h, offs, s, b0); + assert(c as nat >= n); + } else { + lemma_brt_entry(ls, d, nl, h, offs, i, c, s, b0); + let s3 = choose|s3: isa::State| + reaches(whole, b0, s, s3) && isa::wf(s3) && brt_fr(s, s3) && s3.pc as int == (if c as nat + == i { + b0 + offs[ls[i as int] as int] + } else { + b0 + 4 + 12 * (i + 1) + }); + if c as nat == i { + assert((c as nat) < n); + } else { + assert(s3.r[0] == c as u64); + lemma_brt_chain(ls, d, nl, h, offs, i + 1, c, s3, b0); + let me = choose|me: isa::State| + reaches(whole, b0, s3, me) && isa::wf(me) && brt_fr(s3, me) && me.pc as int == b0 + ( + if (c as nat) < n { + offs[ls[c as int] as int] + } else { + offs[d as int] + }); + lemma_reaches_trans(whole, b0, s, s3, me); + lemma_brt_fr_trans(s, s3, me); + } + } +} + +/// **`br_table ls d`.** +#[verifier::rlimit(1500)] +pub proof fn lemma_brt_cf(ls: Vec, d: usize, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + ls@.len() <= max_targets(), + d < offs.len(), + forall|q: int| 0 <= q < ls@.len() ==> #[trigger] ls@[q] < offs.len(), + labels_fit(offs, 8 + 12 * ls@.len()), + ensures + cf_refines(Node::BrTable(ls, d)@, nl, h, offs, brt_code(ls@, d, nl, h, offs)), +{ + super::memlemmas::lemma_p2_consts(); + let lsq = ls@; + let code = brt_code(lsq, d, nl, h, offs); + lemma_brt_shape(lsq, d, nl, h, offs); + let j = (nl + h - 1) as nat; + let i0 = load_slot(x0(), j); + assert(code[0] == i0); + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, m, lir::eval(lay, Node::BrTable(ls, d)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && cf_canon( + m.pc as int, + offs, + code, + ) implies cf_post( + lay, + base, + nl, + offs, + code, + m, + lir::eval(lay, Node::BrTable(ls, d)@, a, fuel), + ) by { + let n0 = a.stack.len(); + if fuel > 0 && n0 >= 1 { + match a.stack[(n0 - 1) as int] { + lir::Val::I32(c) => { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + assert(ac.stack[(h - 1) as int] == lir::Val::I32(c)); + assert(slot_val(ac, nl, j) == lir::Val::I32(c)); + assert(j < nl + ac.stack.len()); + let b0 = m.pc as int; + assert(code_len(code) == 8 + 12 * lsq.len()); + assert(brt_labels_ok(lsq, d, offs, b0)) by { + assert forall|l: int| 0 <= l < offs.len() implies 0 <= b0 + #[trigger] offs[l] + && b0 + offs[l] < 0x1_0000_0000_0000_0000 by {} + } + super::rel::lemma_load_rel(lay, ac, m, 0, j, nl); + let m1 = isa::step(m, i0).unwrap(); + assert(m1.r[0] == c as u64); + lemma_reach_step(code, b0, m, 0, m1); + assert(m1.pc as int == b0 + 4); + lemma_brt_chain(lsq, d, nl, h, offs, 0, c, m1, b0); + let me = choose|q: isa::State| + reaches(code, b0, m1, q) && isa::wf(q) && brt_fr(m1, q) && q.pc as int == b0 + + (if (c as nat) < lsq.len() { + offs[lsq[c as int] as int] + } else { + offs[d as int] + }); + lemma_reaches_trans(code, b0, m, m1, me); + let l: nat = if (c as nat) < lsq.len() { + lsq[c as int] as nat + } else { + d as nat + }; + let mapped = lsq.map_values(|x: usize| x as nat); + assert(mapped.len() == lsq.len()); + assert(lir::eval(lay, Node::BrTable(ls, d)@, a, fuel) == lir::Outcome::Branch( + l, + a.with_stack(lir::below(a.stack, 1)), + )) by { + if (c as nat) < lsq.len() { + assert(mapped[c as int] == lsq[c as int] as nat); + } + } + assert(l < offs.len()); + assert(me.pc as int == b0 + offs[l as int]); + assert(me.r[28] == m1.r[28] && me.r[30] == m1.r[30] && m1.r[28] == m.r[28]); + assert(me.vreg.len() == 32); + super::rel::lemma_rel_regs(lay, ac, m1, me, nl); + lemma_preserved_same(lay, m, me); + lemma_table_pres(lay, m, me); + let s = a.with_stack(lir::below(a.stack, 1)); + assert((base + a.stack).take((base + lir::below(a.stack, 1)).len() as int) + =~= base + lir::below(a.stack, 1)); + lemma_rel_prefix( + lay, + a, + base + a.stack, + base + lir::below(a.stack, 1), + me, + nl, + ); + assert(s.with_stack(base + s.stack) == a.with_stack( + base + lir::below(a.stack, 1), + )); + reveal(rel_base); + assert(rel_base(lay, base, s, me, nl)); + assert(base + Seq::::empty() =~= base); + assert(rel_ext(lay, base, s, me, nl)); + }, + _ => {}, + } + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfbs.rs b/crates/clazel-jit/src/a64/cfbs.rs new file mode 100644 index 000000000..a2d91f333 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfbs.rs @@ -0,0 +1,604 @@ +//! The positions a branch may and may not land on ([`bs`] and [`prot`] of +//! [`super::cfaccept`]): they are inside the node, disjoint, and the first +//! are unit starts (jit.md item J.9, "every emitted blob is accepted", A64). +//! The counterpart of [`crate::cfbs`]. + +#[cfg(verus_only)] +use super::cfaccept::*; +#[cfg(verus_only)] +use super::cflens::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::cfunits::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A memory instruction's layout: operands (4 to 12 bytes), the check (28), +/// the access, the `b` (4) and the stub (12). +pub proof fn lemma_mem_csize(m: MemOp, nl: nat, h: nat, d: nat) + ensures + csize(Node::Mem(m), nl, h, d) == stub_off(m, nl, h) + 12, + code_len(mem_pre(m, nl, h)) >= 4, + code_len(mem_check(m, nl, h)) == 28, + code_len(mem_tail(m, nl, h)) >= 0, + stub_off(m, nl, h) == code_len(mem_pre(m, nl, h)) + 28 + code_len(mem_tail(m, nl, h)) + 4, +{ + assert(mem_check(m, nl, h).len() == 7); + assert(mem_pre(m, nl, h).len() >= 1) by { + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { .. } => {}, + MemOp::Store { .. } => {}, + } + } + assert(csize(Node::Mem(m), nl, h, d) == code_len(mem_code(m, nl, h))); +} + +/// A `nop` has no code. +pub proof fn lemma_nop_size(nl: nat, h: nat, d: nat) + ensures + csize(Node::Op(Op::Nop), nl, h, d) == 0, +{ + assert(tmpl(Op::Nop, nl, h) =~= Seq::::empty()); +} + +/// What [`lemma_bs_facts`] says about a node. +pub open spec fn bs_facts(n: Node, nl: nat, h: nat, d: nat) -> bool { + bs(n, nl, h, d, 0) && bs(n, nl, h, d, csize(n, nl, h, d) as int) && (forall|x: int| + #[trigger] bs(n, nl, h, d, x) ==> 0 <= x <= csize(n, nl, h, d) as int) && (forall|x: int| + #[trigger] prot(n, nl, h, d, x) ==> 0 < x < csize(n, nl, h, d) as int) && (forall|x: int| + !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x))) +} + +/// `then`, from the facts of its parts: the second part starts `sa` bytes in. +proof fn lemma_bs_then(a: Node, b: Node, nl: nat, h: nat, d: nat, h1: nat, sb: nat) + requires + h1 == flow_h(a, nl, h, d), + csize(Node::Then(Box::new(a), Box::new(b)), nl, h, d) == csize(a, nl, h, d) + sb, + bs_facts(a, nl, h, d), + bs(b, nl, h1, d, 0), + bs(b, nl, h1, d, sb as int), + forall|x: int| #[trigger] bs(b, nl, h1, d, x) ==> 0 <= x <= sb as int, + forall|x: int| #[trigger] prot(b, nl, h1, d, x) ==> 0 < x < sb as int, + forall|x: int| !(#[trigger] bs(b, nl, h1, d, x) && prot(b, nl, h1, d, x)), + ensures + bs_facts(Node::Then(Box::new(a), Box::new(b)), nl, h, d), +{ + let t = Node::Then(Box::new(a), Box::new(b)); + let sa = csize(a, nl, h, d) as int; + assert(bs(t, nl, h, d, 0)); + assert(bs(b, nl, h1, d, sb as int)); + assert(bs(t, nl, h, d, sa + sb as int)); + assert forall|x: int| #[trigger] bs(t, nl, h, d, x) implies 0 <= x <= sa + sb by { + if bs(a, nl, h, d, x) { + } else { + assert(bs(b, nl, h1, d, x - sa)); + } + } + assert forall|x: int| #[trigger] prot(t, nl, h, d, x) implies 0 < x < sa + sb by { + if prot(a, nl, h, d, x) { + } else { + assert(prot(b, nl, h1, d, x - sa)); + } + } + assert forall|x: int| !(#[trigger] bs(t, nl, h, d, x) && prot(t, nl, h, d, x)) by { + if bs(t, nl, h, d, x) && prot(t, nl, h, d, x) { + if bs(a, nl, h, d, x) { + if prot(a, nl, h, d, x) { + } else { + assert(prot(b, nl, h1, d, x - sa)); + } + } else { + assert(bs(b, nl, h1, d, x - sa)); + if prot(a, nl, h, d, x) { + } else { + assert(prot(b, nl, h1, d, x - sa)); + } + } + } + } +} + +/// `if`, from the facts of its arms. +proof fn lemma_bs_if(t: Node, e: Node, nl: nat, h: nat, d: nat) + requires + 1 <= h, + bs_facts(t, nl, (h - 1) as nat, d + 1), + bs_facts(e, nl, (h - 1) as nat, d + 1), + ensures + bs_facts(Node::If(Box::new(t), Box::new(e)), nl, h, d), +{ + lemma_csize_if(t, e, nl, h, d); + let n = Node::If(Box::new(t), Box::new(e)); + let h0 = (h - 1) as nat; + let st = csize(t, nl, h0, d + 1) as int; + let se = csize(e, nl, h0, d + 1) as int; + assert(bs(n, nl, h, d, 0)); + assert(bs(e, nl, h0, d + 1, se)); + assert(bs(n, nl, h, d, 12 + st + se)); + assert forall|x: int| #[trigger] bs(n, nl, h, d, x) implies 0 <= x <= 12 + st + se by { + if x == 0 { + } else if bs(t, nl, h0, d + 1, x - 8) { + } else { + assert(bs(e, nl, h0, d + 1, x - (12 + st))); + } + } + assert forall|x: int| #[trigger] prot(n, nl, h, d, x) implies 0 < x < 12 + st + se by { + if prot(t, nl, h0, d + 1, x - 8) { + } else { + assert(prot(e, nl, h0, d + 1, x - (12 + st))); + } + } + assert forall|x: int| !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x)) by { + if bs(n, nl, h, d, x) && prot(n, nl, h, d, x) { + if x == 0 { + if prot(t, nl, h0, d + 1, x - 8) { + } else { + assert(prot(e, nl, h0, d + 1, x - (12 + st))); + } + } else if bs(t, nl, h0, d + 1, x - 8) { + if prot(t, nl, h0, d + 1, x - 8) { + } else { + assert(prot(e, nl, h0, d + 1, x - (12 + st))); + } + } else { + assert(bs(e, nl, h0, d + 1, x - (12 + st))); + if prot(t, nl, h0, d + 1, x - 8) { + } else { + assert(prot(e, nl, h0, d + 1, x - (12 + st))); + } + } + } + } +} + +/// The nodes with no inner structure. +proof fn lemma_bs_leaf(n: Node, nl: nat, h: nat, d: nat) + requires + n is Br || n is BrIf || n is BrTable || n is Op || n is Exit, + ensures + bs_facts(n, nl, h, d), +{ + assert(bs(n, nl, h, d, 0)); + assert(bs(n, nl, h, d, csize(n, nl, h, d) as int)); + assert forall|x: int| #[trigger] bs(n, nl, h, d, x) implies 0 <= x <= csize(n, nl, h, d) as int + by {} + assert forall|x: int| !prot(n, nl, h, d, x) by {} + assert forall|x: int| !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x)) by {} +} + +/// `block`. +proof fn lemma_bs_block(b: Node, nl: nat, h: nat, d: nat) + requires + bs_facts(b, nl, h, d + 1), + ensures + bs_facts(Node::Block(Box::new(b)), nl, h, d), +{ + let n = Node::Block(Box::new(b)); + lemma_csize_block(b, nl, h, d); + let sz = csize(b, nl, h, d + 1) as int; + assert(bs(n, nl, h, d, 0)); + assert(bs(n, nl, h, d, sz)); + assert forall|x: int| #[trigger] bs(n, nl, h, d, x) implies 0 <= x <= sz by { + assert(bs(b, nl, h, d + 1, x)); + } + assert forall|x: int| #[trigger] prot(n, nl, h, d, x) implies 0 < x < sz by { + assert(prot(b, nl, h, d + 1, x)); + } + assert forall|x: int| !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x)) by { + assert(bs(n, nl, h, d, x) == bs(b, nl, h, d + 1, x)); + assert(prot(n, nl, h, d, x) == prot(b, nl, h, d + 1, x)); + } +} + +/// `loop`. +proof fn lemma_bs_loop(b: Node, nl: nat, h: nat, d: nat) + requires + bs_facts(b, nl, h, d + 1), + ensures + bs_facts(Node::Loop(Box::new(b)), nl, h, d), +{ + let n = Node::Loop(Box::new(b)); + lemma_csize_loop(b, nl, h, d); + let sz = csize(b, nl, h, d + 1) as int; + assert(bs(n, nl, h, d, 0)); + assert(bs(n, nl, h, d, sz)); + assert forall|x: int| #[trigger] bs(n, nl, h, d, x) implies 0 <= x <= sz by { + assert(bs(b, nl, h, d + 1, x)); + } + assert forall|x: int| #[trigger] prot(n, nl, h, d, x) implies 0 < x < sz by { + assert(prot(b, nl, h, d + 1, x)); + } + assert forall|x: int| !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x)) by { + assert(bs(n, nl, h, d, x) == bs(b, nl, h, d + 1, x)); + assert(prot(n, nl, h, d, x) == prot(b, nl, h, d + 1, x)); + } +} + +/// **Where a branch may land is inside the node, where none may is strictly +/// inside it, and the two are disjoint.** The node's start and end are +/// among the first. +pub proof fn lemma_bs_facts(n: Node, nl: nat, h: nat, d: nat) + requires + cf_flow(n, nl, h, d) is Some, + ensures + bs_facts(n, nl, h, d), + decreases n, +{ + match n { + Node::Mem(m) => { + lemma_mem_csize(m, nl, h, d); + assert forall|x: int| !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x)) by {} + }, + Node::Then(a, b) => { + lemma_bs_facts(*a, nl, h, d); + lemma_csize_then(*a, *b, nl, h, d); + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + lemma_bs_facts(*b, nl, h1, d); + assert(flow_h(*a, nl, h, d) == h1); + lemma_bs_then(*a, *b, nl, h, d, h1, csize(*b, nl, h1, d)); + }, + Some(Flow::Dead) => { + assert(*b == Node::Op(Op::Nop)); + lemma_nop_size(nl, h, d); + assert(flow_h(*a, nl, h, d) == h); + assert forall|y: int| #[trigger] bs(*b, nl, h, d, y) == (y == 0) by {} + assert forall|y: int| !prot(*b, nl, h, d, y) by {} + lemma_bs_then(*a, *b, nl, h, d, h, 0); + }, + None => {}, + } + }, + Node::Block(b) => { + lemma_bs_facts(*b, nl, h, d + 1); + lemma_bs_block(*b, nl, h, d); + }, + Node::Loop(b) => { + lemma_bs_facts(*b, nl, h, d + 1); + lemma_bs_loop(*b, nl, h, d); + }, + Node::If(t, e) => { + lemma_bs_facts(*t, nl, (h - 1) as nat, d + 1); + lemma_bs_facts(*e, nl, (h - 1) as nat, d + 1); + lemma_bs_if(*t, *e, nl, h, d); + }, + Node::Op(_) => { + lemma_bs_leaf(n, nl, h, d); + }, + Node::Br(_) => { + lemma_bs_leaf(n, nl, h, d); + }, + Node::BrIf(_) => { + lemma_bs_leaf(n, nl, h, d); + }, + Node::BrTable(_, _) => { + lemma_bs_leaf(n, nl, h, d); + }, + Node::Exit(_, _) => { + lemma_bs_leaf(n, nl, h, d); + }, + } +} + +/// What [`lemma_bs_ubi`] says about a node. +pub open spec fn ubi_facts(n: Node, nl: nat, h: nat, offs: Seq) -> bool { + forall|pos: int, x: int| + bs(n, nl, h, offs.len(), x) ==> #[trigger] ubi(utmpl(n, nl, h, offs), pos, pos + x) +} + +/// A memory instruction. +proof fn lemma_ubif_mem(m: MemOp, nl: nat, h: nat, offs: Seq) + ensures + ubi_facts(Node::Mem(m), nl, h, offs), +{ + let n = Node::Mem(m); + let d = offs.len(); + lemma_mem_csize(m, nl, h, d); + lemma_utmpl_blen(n, nl, h, offs); + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![b_i(code_len(exit_seq(mem_exit_code(m))) as int + 4)]; + let stub = exit_seq(mem_exit_code(m)); + let u1 = ones(pre) + seq![chk] + ones(tail); + let u2 = seq![jm, stub]; + lemma_blen_ones(pre); + lemma_blen_ones(tail); + lemma_blen_cat(ones(pre), seq![chk]); + lemma_blen_cat(ones(pre) + seq![chk], ones(tail)); + lemma_blen_one(chk); + assert(blen(u1) == code_len(pre) + code_len(chk) + code_len(tail)); + lemma_blen_two(jm, stub); + assert(code_len(jm) == 4); + assert(mem_units(m, nl, h) =~= u1 + u2); + assert forall|pos: int, x: int| bs(n, nl, h, d, x) implies #[trigger] ubi( + utmpl(n, nl, h, offs), + pos, + pos + x, + ) by { + if x == 0 { + lemma_uflat_ones(pre); + assert(ones(pre).len() > 0); + assert(ub(ones(pre), pos, pos)); + assert(mem_units(m, nl, h) =~= ones(pre) + (seq![chk] + ones(tail) + u2)); + lemma_ub_cat(ones(pre), seq![chk] + ones(tail) + u2, pos, pos + x); + } else if x == stub_off(m, nl, h) { + assert(u2.len() == 2); + assert(u2.skip(1)[0] == stub); + assert(u2[0] == jm); + assert(code_len(u2[0]) == 4); + assert(ub(u2.skip(1), pos + blen(u1) + 4, pos + x)); + assert(ub(u2, pos + blen(u1), pos + x)); + lemma_ub_cat(u1, u2, pos, pos + x); + } else { + assert(x == csize(n, nl, h, d) as int); + lemma_blen_cat(u1, u2); + } + } +} + +/// `if`. +proof fn lemma_ubif_if(t: Node, e: Node, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + ubi_facts(t, nl, (h - 1) as nat, seq![ + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 4 + csize( + e, + nl, + (h - 1) as nat, + offs.len() + 1, + ) as int, + ] + shift(offs, 8)), + ubi_facts(e, nl, (h - 1) as nat, seq![csize(e, nl, (h - 1) as nat, offs.len() + 1) as int] + + shift(offs, 12 + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int)), + ensures + ubi_facts(Node::If(Box::new(t), Box::new(e)), nl, h, offs), +{ + let n = Node::If(Box::new(t), Box::new(e)); + let d = offs.len(); + let st = csize(t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 4 + se] + shift(offs, 8); + let oe = seq![se] + shift(offs, 12 + st); + assert(ot.len() == d + 1 && oe.len() == d + 1); + let cp = cond_pre(nl, h, true, st + 8); + let ut = utmpl(t, nl, (h - 1) as nat, ot); + let ue = utmpl(e, nl, (h - 1) as nat, oe); + let jmu = seq![seq![b_i(se + 4)]]; + lemma_blen_ones(cp); + lemma_utmpl_blen(t, nl, (h - 1) as nat, ot); + lemma_utmpl_blen(e, nl, (h - 1) as nat, oe); + lemma_blen_one(seq![b_i(se + 4)]); + assert(code_len(cp) == 8); + let a1 = ones(cp); + let a2 = a1 + ut; + let a3 = a2 + jmu; + lemma_blen_cat(a1, ut); + lemma_blen_cat(a2, jmu); + lemma_blen_cat(a3, ue); + assert(utmpl(n, nl, h, offs) =~= a3 + ue); + assert forall|pos: int, x: int| bs(n, nl, h, d, x) implies #[trigger] ubi( + utmpl(n, nl, h, offs), + pos, + pos + x, + ) by { + if x == 0 { + lemma_uflat_ones(cp); + assert(ub(a1, pos, pos)); + assert(utmpl(n, nl, h, offs) =~= a1 + (ut + jmu + ue)); + lemma_ubi_cat(a1, ut + jmu + ue, pos, pos + x); + } else if bs(t, nl, (h - 1) as nat, d + 1, x - 8) { + assert(ubi(ut, pos + 8, pos + 8 + (x - 8))); + lemma_ubi_cat(a1, ut, pos, pos + x); + lemma_ubi_cat(a2, jmu, pos, pos + x); + lemma_ubi_cat(a3, ue, pos, pos + x); + } else { + assert(bs(e, nl, (h - 1) as nat, d + 1, x - (12 + st))); + assert(ubi(ue, pos + (12 + st), pos + (12 + st) + (x - (12 + st)))); + lemma_ubi_cat(a3, ue, pos, pos + x); + } + } +} + +/// `then`. +proof fn lemma_ubif_then(a: Node, b: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(Node::Then(Box::new(a), Box::new(b)), nl, h, offs.len()) is Some, + ubi_facts(a, nl, h, offs), + cf_flow(a, nl, h, offs.len()) is Some, + match cf_flow(a, nl, h, offs.len()) { + Some(Flow::Falls(h1)) => ubi_facts( + b, + nl, + h1, + shift(offs, csize(a, nl, h, offs.len()) as int), + ), + _ => true, + }, + ensures + ubi_facts(Node::Then(Box::new(a), Box::new(b)), nl, h, offs), +{ + let n = Node::Then(Box::new(a), Box::new(b)); + let d = offs.len(); + let sa = csize(a, nl, h, d); + let ob = shift(offs, sa as int); + let h1 = flow_h(a, nl, h, d); + lemma_utmpl_blen(a, nl, h, offs); + assert forall|pos: int, x: int| bs(n, nl, h, d, x) implies #[trigger] ubi( + utmpl(n, nl, h, offs), + pos, + pos + x, + ) by { + if bs(a, nl, h, d, x) { + assert(ubi(utmpl(a, nl, h, offs), pos, pos + x)); + lemma_ubi_cat(utmpl(a, nl, h, offs), utmpl(b, nl, h1, ob), pos, pos + x); + } else { + assert(bs(b, nl, h1, d, x - sa as int)); + match cf_flow(a, nl, h, d) { + Some(Flow::Falls(_)) => { + assert(ob.len() == d); + assert(ubi( + utmpl(b, nl, h1, ob), + pos + sa as int, + pos + sa as int + (x - sa as int), + )); + lemma_ubi_cat(utmpl(a, nl, h, offs), utmpl(b, nl, h1, ob), pos, pos + x); + }, + Some(Flow::Dead) => { + assert(b == Node::Op(Op::Nop)); + lemma_nop_size(nl, h, d); + assert(utmpl(b, nl, h1, ob) =~= Seq::>::empty()); + assert(x == sa as int); + lemma_blen_cat(utmpl(a, nl, h, offs), utmpl(b, nl, h1, ob)); + }, + None => {}, + } + } + } +} + +/// A node whose units are one instruction each, or an exit sequence. +proof fn lemma_ubif_leaf(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + n is Br || n is BrIf || n is BrTable || n is Op || n is Exit, + ensures + ubi_facts(n, nl, h, offs), +{ + let d = offs.len(); + lemma_utmpl_blen(n, nl, h, offs); + let us = utmpl(n, nl, h, offs); + assert forall|pos: int, x: int| bs(n, nl, h, d, x) implies #[trigger] ubi(us, pos, pos + x) by { + if x == 0 { + match n { + Node::Exit(c, _) => { + assert(us.len() == 1); + assert(ub(us, pos, pos)); + }, + _ => { + let c = match n { + Node::Op(o) => tmpl(o, nl, h), + _ => ctmpl(n, nl, h, offs), + }; + assert(us == ones(c)); + if c.len() > 0 { + assert(us.len() > 0); + assert(ub(us, pos, pos)); + } else { + assert(us.len() == 0); + lemma_blen_ones(c); + } + }, + } + } else { + assert(x == csize(n, nl, h, d) as int); + } + } +} + +/// `block`. +proof fn lemma_ubif_block(b: Node, nl: nat, h: nat, offs: Seq) + requires + ubi_facts(b, nl, h, seq![csize(b, nl, h, offs.len() + 1) as int] + offs), + ensures + ubi_facts(Node::Block(Box::new(b)), nl, h, offs), +{ + let n = Node::Block(Box::new(b)); + let ob = seq![csize(b, nl, h, offs.len() + 1) as int] + offs; + assert(ob.len() == offs.len() + 1); + assert forall|pos: int, x: int| bs(n, nl, h, offs.len(), x) implies #[trigger] ubi( + utmpl(n, nl, h, offs), + pos, + pos + x, + ) by { + assert(bs(b, nl, h, offs.len() + 1, x)); + assert(ubi(utmpl(b, nl, h, ob), pos, pos + x)); + } +} + +/// `loop`. +proof fn lemma_ubif_loop(b: Node, nl: nat, h: nat, offs: Seq) + requires + ubi_facts(b, nl, h, seq![0int] + offs), + ensures + ubi_facts(Node::Loop(Box::new(b)), nl, h, offs), +{ + let n = Node::Loop(Box::new(b)); + let ob = seq![0int] + offs; + assert(ob.len() == offs.len() + 1); + assert forall|pos: int, x: int| bs(n, nl, h, offs.len(), x) implies #[trigger] ubi( + utmpl(n, nl, h, offs), + pos, + pos + x, + ) by { + assert(bs(b, nl, h, offs.len() + 1, x)); + assert(ubi(utmpl(b, nl, h, ob), pos, pos + x)); + } +} + +/// **Every position a branch may land on is a unit start or the end of the +/// units.** +pub proof fn lemma_bs_ubi(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + ensures + ubi_facts(n, nl, h, offs), + decreases n, +{ + let d = offs.len(); + match n { + Node::Mem(m) => { + lemma_ubif_mem(m, nl, h, offs); + }, + Node::Then(a, b) => { + let sa = csize(*a, nl, h, d); + let ob = shift(offs, sa as int); + lemma_bs_ubi(*a, nl, h, offs); + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + assert(ob.len() == d); + lemma_bs_ubi(*b, nl, h1, ob); + }, + _ => {}, + } + lemma_ubif_then(*a, *b, nl, h, offs); + }, + Node::Block(b) => { + let ob = seq![csize(*b, nl, h, d + 1) as int] + offs; + assert(ob.len() == d + 1); + lemma_bs_ubi(*b, nl, h, ob); + lemma_ubif_block(*b, nl, h, offs); + }, + Node::Loop(b) => { + let ob = seq![0int] + offs; + assert(ob.len() == d + 1); + lemma_bs_ubi(*b, nl, h, ob); + lemma_ubif_loop(*b, nl, h, offs); + }, + Node::If(t, e) => { + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 4 + se] + shift(offs, 8); + let oe = seq![se] + shift(offs, 12 + st); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_bs_ubi(*t, nl, (h - 1) as nat, ot); + lemma_bs_ubi(*e, nl, (h - 1) as nat, oe); + lemma_ubif_if(*t, *e, nl, h, offs); + }, + _ => { + lemma_ubif_leaf(n, nl, h, offs); + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfchk.rs b/crates/clazel-jit/src/a64/cfchk.rs new file mode 100644 index 000000000..3735ee195 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfchk.rs @@ -0,0 +1,592 @@ +//! **The run of the validator's bounds-check sequence** (jit.md J.6; the +//! sequence is `clazel_codecheck::a64::spec::check_seq`): from the state at +//! its first instruction, in a list of code that contains it, the machine +//! reaches the stub both its exits go to when `i + n > bound`, and falls +//! through to the instruction after it, with the index unchanged (`csel i, i, +//! xzr, lo` is the identity after the check), when `i + n <= bound`. The A64 +//! counterpart of [`crate::cfcheckrun`]. Only registers `x0` to `x2` and the +//! flags change. + +#[cfg(verus_only)] +use super::cfleaf::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cfsteps::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use clazel_codecheck::a64::spec::{Chk, check_instr}; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// What the check leaves alone: the memory, `SP`, the vector registers, and +/// every general register but `x0`, `x1` and `x2` (the flags are not +/// mentioned). +pub open spec fn chk_frame(s: isa::State, s2: isa::State) -> bool { + s2.r.len() == 31 && s.r.len() == 31 && s2.vreg == s.vreg && s2.mem == s.mem && s2.sp == s.sp + && forall|k: int| + 0 <= k < 31 && k != 0 && k != 1 && k != 2 ==> #[trigger] s2.r[k] == s.r[k] +} + +/// [`chk_frame`] is transitive. +pub proof fn lemma_chk_frame_trans(s: isa::State, s1: isa::State, s2: isa::State) + requires + chk_frame(s, s1), + chk_frame(s1, s2), + ensures + chk_frame(s, s2), +{ +} + +/// One step at index `idx` of `code`, placed at `b0`, is a run of the code. +pub proof fn lemma_reach_step( + code: Seq, + b0: int, + s: isa::State, + idx: int, + s1: isa::State, +) + requires + 0 <= idx < code.len(), + s.pc as int == b0 + 4 * idx, + isa::step(s, code[idx]) == Some(s1), + ensures + reaches(code, b0, s, s1), +{ + assert(fetch(code, s.pc as int - b0) == Some(code[idx])); + lemma_steps_one(code, b0, s, code[idx], s1); +} + +/// `ldr xr, [x28, #(8 imm12)]` loads the word at that offset of the context +/// into `xr`. +pub proof fn lemma_step_ctx_ld(m: isa::State, r: u8, imm12: u16, v: nat) + requires + isa::wf(m), + ctx_ok(m), + r < 31, + imm12 as int * 8 + 8 <= abi::ctx_size(), + isa::mem_read(m.mem, ctx_of(m) + imm12 as int * 8, 8) == Some(v as int), + v < 0x1_0000_0000_0000_0000, + m.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step( + m, + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: true, + t: r, + n: abi::ctx_reg(), + imm12, + }, + ) == Some(isa::State { r: m.r.update(r as int, v as u64), pc: (m.pc + 4) as u64, ..m }), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + super::steps::lemma_x_read64(m, 28); + super::memlemmas::lemma_trunc64(v as int); + super::memlemmas::lemma_trunc64(m.pc as int + 4); + super::memlemmas::lemma_trunc64(ctx_of(m) + imm12 as int * 8); + let i = isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: true, + t: r, + n: abi::ctx_reg(), + imm12, + }; + assert(isa::x_read(m, 28, 64) == ctx_of(m)); + assert(isa::datasize(isa::Width::W64) == 64); + assert(isa::execute(m, i) == Some(isa::x_set(m, r, 64, v as int))); + assert(isa::x_set(m, r, 64, v as int) == isa::State { r: m.r.update(r as int, v as u64), ..m }); +} + +#[verifier::rlimit(2000)] +/// `adds xd, xn, #imm` (64 bits): `xd` gets the sum modulo `2^64`, and the +/// carry flag says it wrapped. +pub proof fn lemma_step_adds_imm(s: isa::State, d: u8, n: u8, imm: u16, x: u64) + requires + isa::wf(s), + d < 31, + n < 31, + s.r[n as int] == x, + imm < 4096, + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + ({ + let i = isa::Instr::AddSubImm { + sf: isa::Width::W64, + sub_op: false, + setflags: true, + d, + n, + imm12: imm, + sh: false, + }; + &&& isa::step(s, i) is Some + &&& ({ + let s1 = isa::step(s, i).unwrap(); + &&& s1.pc as int == s.pc as int + 4 + &&& s1.r == s.r.update( + d as int, + ((x as int + imm as int) % 0x1_0000_0000_0000_0000) as u64, + ) + &&& s1.mem == s.mem + &&& s1.sp == s.sp + &&& s1.vreg == s.vreg + &&& s1.flag_c == (x as int + imm as int >= 0x1_0000_0000_0000_0000) + }) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + let p: int = 0x1_0000_0000_0000_0000; + let h: int = 0x8000_0000_0000_0000; + let xi = x as int; + let yi = imm as int; + super::alu::lemma_trunc_small(64, xi); + super::alu::lemma_trunc_small(64, yi); + let us = xi + yi; + let res = us % p; + assert(isa::trunc(64, us) == res); + let sx = if xi >= h { xi - p } else { xi }; + let sy = yi; + assert(isa::sint(64, xi) == sx && isa::sint(64, yi) == sy); + let ss = sx + sy; + let fn_ = (res / h) % 2; + let fz = if res == 0 { + 1int + } else { + 0 + }; + let fc = if res == us { + 0int + } else { + 1 + }; + let sres = if res >= h { + res - p + } else { + res + }; + assert(isa::sint(64, res) == sres); + let fv = if sres == ss { + 0int + } else { + 1 + }; + assert(isa::bit(res, 63) == fn_); + assert(isa::add_with_carry(64, xi, yi, 0) == (res, fn_ * 8 + fz * 4 + fc * 2 + fv)); + assert(0 <= fn_ <= 1 && 0 <= res < p); + super::flags::lemma_nzcv_bits(fn_, fz, fc, fv); + let i = isa::Instr::AddSubImm { + sf: isa::Width::W64, + sub_op: false, + setflags: true, + d, + n, + imm12: imm, + sh: false, + }; + assert(isa::x_read(s, n, 64) == xi); + let s0 = isa::set_nzcv(s, fn_ * 8 + fz * 4 + fc * 2 + fv); + assert(s0.flag_c == (fc == 1)); + assert(isa::execute(s, i) == Some(isa::x_set(s0, d, 64, res))); +} + +/// `cmp xn, xm` (64 bits, `subs xzr, xn, xm`) sets the flags of `x - y`. +pub proof fn lemma_step_cmp_rr(s: isa::State, n: u8, m: u8, x: u64, y: u64) + requires + isa::wf(s), + n < 31, + m < 31, + s.r[n as int] == x, + s.r[m as int] == y, + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::cmp_rr(isa::Width::W64, n, m)) == Some( + isa::State { + pc: (s.pc + 4) as u64, + ..isa::set_nzcv(s, super::flags::cmp_nzcv(64, x as int, y as int)) + }, + ), +{ + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + super::alu::lemma_trunc_small(64, x as int); + super::alu::lemma_trunc_small(64, y as int); + assert(isa::lsl(64, y as int, 0) == y as int) by { + assert(isa::p2(0) == 1); + } +} + +/// The flags of `cmp x, y`: `hi` (condition 8) holds exactly when `x > y`, and +/// `lo` (3) exactly when `x < y`. +pub proof fn lemma_cmp_hi_lo(s: isa::State, x: int, y: int) + requires + 0 <= x < 0x1_0000_0000_0000_0000, + 0 <= y < 0x1_0000_0000_0000_0000, + ensures + ({ + let t = isa::set_nzcv(s, super::flags::cmp_nzcv(64, x, y)); + &&& isa::condition_holds(t, 8) == (x > y) + &&& isa::condition_holds(t, 3) == (x < y) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::flags::lemma_cmp_arith64(x, y); + let nzcv = super::flags::cmp_nzcv(64, x, y); + let t = isa::set_nzcv(s, nzcv); + assert(0 <= isa::bit(nzcv, 1) <= 1 && 0 <= isa::bit(nzcv, 2) <= 1); + assert(t.flag_c == (isa::bit(nzcv, 1) == 1)); + assert(t.flag_z == (isa::bit(nzcv, 2) == 1)); +} + +/// `csel xd, xn, xzr, lo` (64 bits): `xd` is `xn` when the carry is clear and +/// zero when it is set. +pub proof fn lemma_step_csel_lo(s: isa::State, d: u8, n: u8, x: u64) + requires + isa::wf(s), + d < 31, + n < 31, + s.r[n as int] == x, + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + ({ + let i = isa::Instr::CondSelect { + sf: isa::Width::W64, + else_inv: false, + else_inc: false, + d, + n, + m: 31, + cond: 3, + }; + &&& isa::step(s, i) == Some( + isa::State { + r: s.r.update(d as int, if isa::condition_holds(s, 3) { + x + } else { + 0u64 + }), + pc: (s.pc + 4) as u64, + ..s + }, + ) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + super::alu::lemma_trunc_small(64, x as int); + let i = isa::Instr::CondSelect { + sf: isa::Width::W64, + else_inv: false, + else_inc: false, + d, + n, + m: 31, + cond: 3, + }; + assert(isa::x_read(s, 31, 64) == 0int); + assert(isa::trunc(64, 0int) == 0int) by { + super::alu::lemma_trunc_small(64, 0int); + } +} + +/// Word `j` of the check in `code` (starting at word `st`, both exits to word +/// `sx`) is the validator's instruction `j` for region `k`, `n` bytes. +pub open spec fn chk_at(code: Seq, st: int, sx: int, k: nat, n: nat, j: int) -> bool { + 0 <= st + j < code.len() && code[st + j] == check_instr( + j, + mem_chk(k, n, sx - (st + 2), sx - (st + 4)), + ) +} + +/// **The first three instructions of the check**: the bound into `x2`, `adds`, +/// and the branch on the carry. If the sum wrapped the machine is at the stub; +/// if not, at the fourth instruction, with `x0` the sum. +#[verifier::rlimit(400)] +pub proof fn lemma_chk_head( + code: Seq, + b0: int, + st: int, + sx: int, + k: nat, + n: nat, + s: isa::State, + ix: u64, + bnd: u64, +) + requires + 0 <= st, + st + 7 <= sx, + sx < st + 0x1_0000, + 0 <= b0, + b0 + 4 * sx + 4 < 0x1_0000_0000_0000_0000, + chk_at(code, st, sx, k, n, 0), + chk_at(code, st, sx, k, n, 1), + chk_at(code, st, sx, k, n, 2), + s.pc as int == b0 + 4 * st, + isa::wf(s), + ctx_ok(s), + k < 16, + 1 <= n <= 8, + s.r[1] == ix, + isa::mem_read(s.mem, ctx_of(s) + abi::bound_off(k as int), 8) == Some(bnd as int), + ensures + exists|s3: isa::State| + #![trigger reaches(code, b0, s, s3)] + reaches(code, b0, s, s3) && chk_frame(s, s3) && isa::wf(s3) && ctx_ok(s3) + && s3.r[1] == ix && s3.r[2] == bnd && (ix as int + n >= 0x1_0000_0000_0000_0000 + ==> s3.pc as int == b0 + 4 * sx) && (ix as int + n < 0x1_0000_0000_0000_0000 + ==> s3.pc as int == b0 + 4 * (st + 3) && s3.r[0] as int == ix as int + n), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + let p: int = 0x1_0000_0000_0000_0000; + let c = mem_chk(k, n, sx - (st + 2), sx - (st + 4)); + let r1 = sx - (st + 2); + let xi = ix as int; + let ni = n as int; + let i0 = check_instr(0, c); + assert(i0 == isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: true, + t: 2, + n: abi::ctx_reg(), + imm12: (abi::bound_off(k as int) / 8) as u16, + }); + assert((abi::bound_off(k as int) / 8) as u16 as int * 8 == abi::bound_off(k as int)); + lemma_step_ctx_ld(s, 2, (abi::bound_off(k as int) / 8) as u16, bnd as nat); + let s1 = isa::step(s, i0).unwrap(); + lemma_reach_step(code, b0, s, st, s1); + assert(s1.r[1] == ix && s1.r[2] == bnd); + let i1 = check_instr(1, c); + lemma_step_adds_imm(s1, 0, 1, n as u16, ix); + let s2 = isa::step(s1, i1).unwrap(); + lemma_reach_step(code, b0, s1, st + 1, s2); + assert(s2.flag_c == (xi + ni >= p)); + assert(s2.r[1] == ix && s2.r[2] == bnd); + let i2 = check_instr(2, c); + assert(i2 == bcond_i(2, 4 * r1)) by { + assert((4 * r1) / 4 == r1); + assert(c.r1 == r1 as i32); + } + assert(isa::condition_holds(s2, 2) == s2.flag_c); + assert(0 <= s2.pc as int + 4 * r1 < p); + lemma_step_bcond(s2, 2, 4 * r1); + let s3 = isa::step(s2, i2).unwrap(); + lemma_reach_step(code, b0, s2, st + 2, s3); + lemma_chk_frame_trans(s, s1, s2); + lemma_chk_frame_trans(s, s2, s3); + lemma_reaches_trans(code, b0, s, s1, s2); + lemma_reaches_trans(code, b0, s, s2, s3); + assert(s3.r[1] == ix && s3.r[2] == bnd); + assert(isa::wf(s3) && ctx_ok(s3)); + if xi + ni < p { + assert((xi + ni) % p == xi + ni) by { + vstd::arithmetic::div_mod::lemma_small_mod((xi + ni) as nat, p as nat); + } + } +} + +/// **The compare and `b.hi`**: from the fourth instruction, with `x0` the sum +/// `sum` (no wrap) and `x2` the bound, the machine reaches the stub if `sum > +/// bnd` and the sixth instruction if not. +#[verifier::rlimit(400)] +pub proof fn lemma_chk_mid( + code: Seq, + b0: int, + st: int, + sx: int, + k: nat, + n: nat, + s: isa::State, + sum: int, + bnd: u64, +) + requires + 0 <= st, + st + 7 <= sx, + sx < st + 0x1_0000, + 0 <= b0, + b0 + 4 * sx + 4 < 0x1_0000_0000_0000_0000, + chk_at(code, st, sx, k, n, 3), + chk_at(code, st, sx, k, n, 4), + s.pc as int == b0 + 4 * (st + 3), + isa::wf(s), + 0 <= sum < 0x1_0000_0000_0000_0000, + s.r[0] as int == sum, + s.r[2] == bnd, + ensures + exists|s5: isa::State| + #![trigger reaches(code, b0, s, s5)] + reaches(code, b0, s, s5) && chk_frame(s, s5) && s5.r[1] == s.r[1] && s5.r[2] == bnd + && (sum > bnd as int ==> s5.pc as int == b0 + 4 * sx) && (sum <= bnd as int + ==> s5.pc as int == b0 + 4 * (st + 5)), +{ + super::memlemmas::lemma_p2_consts(); + let p: int = 0x1_0000_0000_0000_0000; + let c = mem_chk(k, n, sx - (st + 2), sx - (st + 4)); + let r2 = sx - (st + 4); + let bi = bnd as int; + let i3 = check_instr(3, c); + assert(i3 == super::templates::cmp_rr(isa::Width::W64, 0, 2)); + lemma_step_cmp_rr(s, 0, 2, s.r[0], bnd); + let s4 = isa::step(s, i3).unwrap(); + lemma_reach_step(code, b0, s, st + 3, s4); + lemma_cmp_hi_lo(s, sum, bi); + assert(isa::condition_holds(s4, 8) == (sum > bi)) by { + assert(s4 == isa::State { + pc: (s.pc + 4) as u64, + ..isa::set_nzcv(s, super::flags::cmp_nzcv(64, sum, bi)) + }); + } + let i4 = check_instr(4, c); + assert(i4 == bcond_i(8, 4 * r2)) by { + assert((4 * r2) / 4 == r2); + assert(c.r2 == r2 as i32); + } + assert(0 <= s4.pc as int + 4 * r2 < p); + lemma_step_bcond(s4, 8, 4 * r2); + let s5 = isa::step(s4, i4).unwrap(); + lemma_reach_step(code, b0, s4, st + 4, s5); + lemma_chk_frame_trans(s, s4, s5); + lemma_reaches_trans(code, b0, s, s4, s5); +} + +/// **The last compare and the mask**: from the sixth instruction, with `x1` the +/// index `ix` below the bound `bnd` in `x2`, the machine reaches the word after +/// the check with `x1` still `ix`. +#[verifier::rlimit(400)] +pub proof fn lemma_chk_tail( + code: Seq, + b0: int, + st: int, + sx: int, + k: nat, + n: nat, + s: isa::State, + ix: u64, + bnd: u64, +) + requires + 0 <= st, + 0 <= b0, + b0 + 4 * (st + 7) < 0x1_0000_0000_0000_0000, + chk_at(code, st, sx, k, n, 5), + chk_at(code, st, sx, k, n, 6), + s.pc as int == b0 + 4 * (st + 5), + isa::wf(s), + s.r[1] == ix, + s.r[2] == bnd, + ix < bnd, + ensures + exists|s7: isa::State| + #![trigger reaches(code, b0, s, s7)] + reaches(code, b0, s, s7) && chk_frame(s, s7) && s7.r[1] == ix && s7.pc as int == b0 + 4 + * (st + 7), +{ + super::memlemmas::lemma_p2_consts(); + let c = mem_chk(k, n, sx - (st + 2), sx - (st + 4)); + let xi = ix as int; + let bi = bnd as int; + let i5 = check_instr(5, c); + assert(i5 == super::templates::cmp_rr(isa::Width::W64, 1, 2)); + lemma_step_cmp_rr(s, 1, 2, ix, bnd); + let s6 = isa::step(s, i5).unwrap(); + lemma_reach_step(code, b0, s, st + 5, s6); + lemma_cmp_hi_lo(s, xi, bi); + assert(isa::condition_holds(s6, 3)) by { + assert(s6 == isa::State { + pc: (s.pc + 4) as u64, + ..isa::set_nzcv(s, super::flags::cmp_nzcv(64, xi, bi)) + }); + } + let i6 = check_instr(6, c); + lemma_step_csel_lo(s6, 1, 1, ix); + let s7 = isa::step(s6, i6).unwrap(); + lemma_reach_step(code, b0, s6, st + 6, s7); + lemma_chk_frame_trans(s, s6, s7); + lemma_reaches_trans(code, b0, s, s6, s7); +} + +/// **The run of the bounds check.** `code` is placed at `b0`; its words `st` to +/// `st + 6` are the check for region `k`, `n` bytes, with both exits going to +/// word `sx`; the machine is at its first word with the index `ix` in `x1` and +/// the region's bound `bnd` in the context table. If `ix + n > bnd` the check +/// reaches `sx`; if not, the word after the check, with `x1` still `ix`. +pub proof fn lemma_check_run( + code: Seq, + b0: int, + st: int, + sx: int, + k: nat, + n: nat, + s: isa::State, + ix: u64, + bnd: u64, +) + requires + 0 <= st, + st + 7 <= sx, + sx < st + 0x1_0000, + 0 <= b0, + b0 + 4 * sx + 4 < 0x1_0000_0000_0000_0000, + b0 + 4 * (st + 7) < 0x1_0000_0000_0000_0000, + forall|j: int| 0 <= j < 7 ==> #[trigger] chk_at(code, st, sx, k, n, j), + s.pc as int == b0 + 4 * st, + isa::wf(s), + ctx_ok(s), + k < 16, + 1 <= n <= 8, + s.r[1] == ix, + isa::mem_read(s.mem, ctx_of(s) + abi::bound_off(k as int), 8) == Some(bnd as int), + ensures + ix as int + n > bnd as int ==> exists|s2: isa::State| + #![trigger reaches(code, b0, s, s2)] + reaches(code, b0, s, s2) && s2.pc as int == b0 + 4 * sx && chk_frame(s, s2), + ix as int + n <= bnd as int ==> exists|s2: isa::State| + #![trigger reaches(code, b0, s, s2)] + reaches(code, b0, s, s2) && s2.pc as int == b0 + 4 * (st + 7) && chk_frame(s, s2) + && s2.r[1] == ix, +{ + super::memlemmas::lemma_p2_consts(); + let p: int = 0x1_0000_0000_0000_0000; + assert(chk_at(code, st, sx, k, n, 0)); + assert(chk_at(code, st, sx, k, n, 1)); + assert(chk_at(code, st, sx, k, n, 2)); + assert(chk_at(code, st, sx, k, n, 3)); + assert(chk_at(code, st, sx, k, n, 4)); + assert(chk_at(code, st, sx, k, n, 5)); + assert(chk_at(code, st, sx, k, n, 6)); + lemma_chk_head(code, b0, st, sx, k, n, s, ix, bnd); + let s3 = choose|s3: isa::State| + reaches(code, b0, s, s3) && chk_frame(s, s3) && isa::wf(s3) && ctx_ok(s3) && s3.r[1] == ix + && s3.r[2] == bnd && (ix as int + n >= p ==> s3.pc as int == b0 + 4 * sx) && (ix as int + + n < p ==> s3.pc as int == b0 + 4 * (st + 3) && s3.r[0] as int == ix as int + n); + if ix as int + n < p { + let sum = ix as int + n as int; + lemma_chk_mid(code, b0, st, sx, k, n, s3, sum, bnd); + let s5 = choose|s5: isa::State| + reaches(code, b0, s3, s5) && chk_frame(s3, s5) && s5.r[1] == s3.r[1] && s5.r[2] == bnd + && (sum > bnd as int ==> s5.pc as int == b0 + 4 * sx) && (sum <= bnd as int + ==> s5.pc as int == b0 + 4 * (st + 5)); + lemma_reaches_trans(code, b0, s, s3, s5); + lemma_chk_frame_trans(s, s3, s5); + if sum <= bnd as int { + assert(isa::wf(s5)); + lemma_chk_tail(code, b0, st, sx, k, n, s5, ix, bnd); + let s7 = choose|s7: isa::State| + reaches(code, b0, s5, s7) && chk_frame(s5, s7) && s7.r[1] == ix && s7.pc as int == b0 + + 4 * (st + 7); + lemma_reaches_trans(code, b0, s, s5, s7); + lemma_chk_frame_trans(s, s5, s7); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfcompose.rs b/crates/clazel-jit/src/a64/cfcompose.rs new file mode 100644 index 000000000..a10c62ba9 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfcompose.rs @@ -0,0 +1,332 @@ +//! **Composition over structured control flow**: how the [`cf_refines`] +//! statement of a node follows from those of its parts. One lemma per way of +//! combining: `then` ([`lemma_then`]), `block` ([`lemma_block`]), `loop` +//! ([`lemma_loop`]), `if` ([`super::cfif::lemma_if`]). The leaves +//! (`br`, `br_if`, `exit`, straight-line instructions) are in +//! [`super::cfleaf`]; [`super::cfproofs`] puts them together by induction on +//! the node. +//! +//! Two lifting lemmas do the bookkeeping the cases share: a statement about a +//! part, from the state after the code before it ([`lemma_lift_pre`]), and one +//! about the code followed by more ([`lemma_lift_post`]). + +#[cfg(verus_only)] +use super::cfalg::*; +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cfsteps::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::contract::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfheight::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `shift(offs, d)` at `l` is `offs[l] - d`. +pub proof fn lemma_shift_index(offs: Seq, d: int, l: int) + requires + 0 <= l < offs.len(), + ensures + shift(offs, d).len() == offs.len(), + shift(offs, d)[l] == offs[l] - d, +{ +} + +/// **Lifting a part's statement to the code that starts with something +/// else.** `c`'s outcome `o`, seen from `m1` (the state after the code +/// `pre`, which ran from `m`), is the outcome of `pre + c` from `m`, with the +/// labels moved by the length of `pre`. +pub proof fn lemma_lift_pre( + lay: lir::Layout, + base: Seq, + nl: nat, + offs: Seq, + offs_c: Seq, + pre: Seq, + c: Seq, + m: isa::State, + m1: isa::State, + o: lir::Outcome, +) + requires + offs_c.len() == offs.len(), + forall|l: int| 0 <= l < offs.len() ==> #[trigger] offs_c[l] + code_len(pre) == offs[l], + m1.pc as int == m.pc as int + code_len(pre), + reaches(pre + c, m.pc as int, m, m1), + preserved_r(lay, m, m1), + cf_post(lay, base, nl, offs_c, c, m1, o), + ensures + cf_post(lay, base, nl, offs, pre + c, m, o), +{ + super::steps::lemma_abi_consts(); + assert(ctx_of(m1) == ctx_of(m)); + let d = code_len(pre); + match o { + lir::Outcome::Normal(a2) => { + let m2 = choose|m2: isa::State| + reaches(c, m1.pc as int, m1, m2) && m2.pc as int == m1.pc as int + code_len(c) && rel_base( + lay, + base, + a2, + m2, + nl, + ) && preserved_r(lay, m1, m2); + assert(pre + c + Seq::::empty() =~= pre + c); + lemma_reaches_embed(pre, c, Seq::empty(), m1.pc as int, m1, m2); + assert(m1.pc as int - d == m.pc as int); + lemma_reaches_trans(pre + c, m.pc as int, m, m1, m2); + super::runseq::lemma_code_len_append(pre, c); + lemma_pr_trans(lay, m, m1, m2); + assert(reaches(pre + c, m.pc as int, m, m2) && m2.pc as int == m.pc as int + code_len(pre + c) + && rel_base(lay, base, a2, m2, nl) && preserved_r(lay, m, m2)); + }, + lir::Outcome::Branch(l, s) => { + let m2 = choose|m2: isa::State| + reaches(c, m1.pc as int, m1, m2) && m2.pc as int == m1.pc as int + offs_c[l as int] && rel_ext( + lay, + base, + s, + m2, + nl, + ) && preserved_r(lay, m1, m2); + assert(pre + c + Seq::::empty() =~= pre + c); + lemma_reaches_embed(pre, c, Seq::empty(), m1.pc as int, m1, m2); + assert(m1.pc as int - d == m.pc as int); + lemma_reaches_trans(pre + c, m.pc as int, m, m1, m2); + lemma_pr_trans(lay, m, m1, m2); + assert(l < offs.len()); + assert(offs_c[l as int] + d == offs[l as int]); + }, + lir::Outcome::Exit(code, vals, s) => { + let m2 = choose|m2: isa::State| + reaches(c, m1.pc as int, m1, m2) && exit_done(m1, m2, code) && exit_rel( + lay, + base, + s, + m2, + nl, + ctx_of(m1), + vals, + ) && preserved_mem_r(lay, m1, m2); + assert(pre + c + Seq::::empty() =~= pre + c); + lemma_reaches_embed(pre, c, Seq::empty(), m1.pc as int, m1, m2); + assert(m1.pc as int - d == m.pc as int); + lemma_reaches_trans(pre + c, m.pc as int, m, m1, m2); + lemma_pr_pmr_trans(lay, m, m1, m2); + assert(exit_done(m, m2, code)); + }, + _ => {}, + } +} + +/// **Lifting a statement to the code followed by more**, for an outcome other +/// than falling through (which would end earlier). +pub proof fn lemma_lift_post( + lay: lir::Layout, + base: Seq, + nl: nat, + offs: Seq, + c: Seq, + post: Seq, + m: isa::State, + o: lir::Outcome, +) + requires + !(o is Normal), + cf_post(lay, base, nl, offs, c, m, o), + ensures + cf_post(lay, base, nl, offs, c + post, m, o), +{ + match o { + lir::Outcome::Branch(l, s) => { + let m2 = choose|m2: isa::State| + reaches(c, m.pc as int, m, m2) && m2.pc as int == m.pc as int + offs[l as int] && rel_ext( + lay, + base, + s, + m2, + nl, + ) && preserved_r(lay, m, m2); + assert(Seq::::empty() + c + post =~= c + post); + lemma_reaches_embed(Seq::empty(), c, post, m.pc as int, m, m2); + assert(code_len(Seq::::empty()) == 0); + }, + lir::Outcome::Exit(code, vals, s) => { + let m2 = choose|m2: isa::State| + reaches(c, m.pc as int, m, m2) && exit_done(m, m2, code) && exit_rel( + lay, + base, + s, + m2, + nl, + ctx_of(m), + vals, + ) && preserved_mem_r(lay, m, m2); + assert(Seq::::empty() + c + post =~= c + post); + lemma_reaches_embed(Seq::empty(), c, post, m.pc as int, m, m2); + assert(code_len(Seq::::empty()) == 0); + }, + _ => {}, + } +} + +/// `cf_canon` for the first part of a sequence. +pub proof fn lemma_canon_prefix(pc: int, offs: Seq, a: Seq, b: Seq) + requires + cf_canon(pc, offs, a + b), + ensures + cf_canon(pc, offs, a), +{ + super::runseq::lemma_code_len_append(a, b); +} + +/// **`then`.** If the first instruction falls through at height `h1` and its +/// code and the second's (whose labels are the first's moved by its length) +/// refine them, their concatenation refines the pair. +#[verifier::rlimit(400)] +pub proof fn lemma_then( + x: Node, + y: Node, + nl: nat, + h: nat, + h1: nat, + offs: Seq, + cx: Seq, + cy: Seq, +) + requires + cf_flow(x, nl, h, offs.len()) == Some(Flow::Falls(h1)), + cf_refines(x@, nl, h, offs, cx), + cf_refines(y@, nl, h1, shift(offs, code_len(cx) as int), cy), + ensures + cf_refines(Node::Then(Box::new(x), Box::new(y))@, nl, h, offs, cx + cy), +{ + let t = Node::Then(Box::new(x), Box::new(y)); + let big = cx + cy; + super::runseq::lemma_code_len_append(cx, cy); + let ll = code_len(cx) as int; + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, big, m, lir::eval(lay, t@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) + && cf_canon(m.pc as int, offs, big) implies cf_post( + lay, + base, + nl, + offs, + big, + m, + lir::eval(lay, t@, a, fuel), + ) by { + if fuel > 0 { + let f = (fuel - 1) as nat; + lemma_rel_base_unfold(lay, base, a, m, nl); + lemma_canon_prefix(m.pc as int, offs, cx, cy); + lemma_use_cf(x@, nl, h, offs, cx, lay, base, a, m, f); + let o1 = lir::eval(lay, x@, a, f); + match o1 { + lir::Outcome::Normal(a1) => { + let m1 = choose|m1: isa::State| + reaches(cx, m.pc as int, m, m1) && m1.pc as int == m.pc as int + code_len(cx) + && rel_base(lay, base, a1, m1, nl) && preserved_r(lay, m, m1); + lemma_cf_height(x, nl, h, offs.len(), h1, base.len(), lay, a, a1, f); + assert forall|l: int| 0 <= l < shift(offs, ll).len() implies 0 <= m1.pc as int + + #[trigger] shift(offs, ll)[l] && m1.pc as int + shift(offs, ll)[l] + < isa::p2(64) && shift(offs, ll)[l] % 4 == 0 by { + lemma_shift_index(offs, ll, l); + assert(cf_canon(m.pc as int, offs, big)); + assert(ll == 4 * cx.len()); + } + assert(cf_canon(m1.pc as int, shift(offs, ll), cy)); + lemma_use_cf(y@, nl, h1, shift(offs, ll), cy, lay, base, a1, m1, f); + assert(Seq::::empty() + cx + cy =~= cx + cy); + lemma_reaches_embed(Seq::empty(), cx, cy, m.pc as int, m, m1); + assert(code_len(Seq::::empty()) == 0); + assert forall|l: int| 0 <= l < offs.len() implies #[trigger] shift(offs, ll)[l] + + code_len(cx) == offs[l] by { + lemma_shift_index(offs, ll, l); + } + lemma_lift_pre( + lay, + base, + nl, + offs, + shift(offs, ll), + cx, + cy, + m, + m1, + lir::eval(lay, y@, a1, f), + ); + }, + _ => { + lemma_lift_post(lay, base, nl, offs, cx, cy, m, o1); + }, + } + } + } +} + +/// **`then` whose first part is dead** (a `br`, `br_table` or `exit`): only +/// `nop` follows, and it has no code, so the pair's code is the first's. +pub proof fn lemma_then_dead( + x: Node, + nl: nat, + h: nat, + offs: Seq, + cx: Seq, +) + requires + cf_flow(x, nl, h, offs.len()) == Some(Flow::Dead), + cf_refines(x@, nl, h, offs, cx), + ensures + cf_refines( + Node::Then(Box::new(x), Box::new(Node::Op(Op::Nop)))@, + nl, + h, + offs, + cx, + ), +{ + let t = Node::Then(Box::new(x), Box::new(Node::Op(Op::Nop))); + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, cx, m, lir::eval(lay, t@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) + && cf_canon(m.pc as int, offs, cx) implies cf_post( + lay, + base, + nl, + offs, + cx, + m, + lir::eval(lay, t@, a, fuel), + ) by { + if fuel > 0 { + let f = (fuel - 1) as nat; + lemma_use_cf(x@, nl, h, offs, cx, lay, base, a, m, f); + lemma_dead_not_normal(x, nl, h, offs.len(), lay, a, f); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfcontract.rs b/crates/clazel-jit/src/a64/cfcontract.rs new file mode 100644 index 000000000..c7ce0788a --- /dev/null +++ b/crates/clazel-jit/src/a64/cfcontract.rs @@ -0,0 +1,243 @@ +//! What the A64 back end must be proven to satisfy for **structured control +//! flow, memory instructions and `exit` with operands** (CLAUDE.md rule 3, +//! docs/SPEC-STYLE.md): the refinement statement [`cf_refines`] and the +//! emitter's contract, in terms of [`crate::a64::cfspec`] and the two +//! semantics it relates. It is [`crate::cfcontract`]'s A64 counterpart; read +//! that header, which says the same in x86-64's terms. Reviewing this file and +//! `cfspec` is enough to know what is claimed. +//! +//! # The refinement statement +//! +//! [`cf_refines`]`(i, nl, h, offs, code)` says the A64 list `code` is a +//! correct translation of the LIR instruction `i`, for a function with `nl` +//! locals, when the operand stack is `h` values high (the `base` below `i`'s +//! fresh stack, and its own stack), with the labels in scope at the byte +//! offsets `offs` from the first byte of `code` (label 0 first). For every +//! layout, `base`, LIR state `a` and machine state `m` related by +//! [`rel_base`], with every address the code can end at or branch to a +//! 4-byte-aligned address below `2^64` ([`cf_canon`]): +//! +//! - **`Normal(a2)`** (falls through): `code` [`reaches`] a state `m2` at the +//! byte after `code`, related to `a2` on the same `base`, differing from +//! `m` only in the exit area and the regions ([`preserved_r`]). +//! - **`Branch(l, s)`** (a `br` that no label inside `i` catches): `l` is a +//! label in scope, and `code` reaches a state at the label's address, +//! related to `s` on `base` and some `extra` values (choice 13 of +//! [`crate::cfspec`]), with the exit area and regions the only change. +//! - **`Exit(c, vals, s)`**: `code` reaches the state the exit sequence +//! leaves: the exit code `c` in the context's exit-code word, `pc` at the +//! return address the enter stub left in `x30`, the frame and regions +//! related to `s`, and the operands `vals` still in the frame, in the slots +//! above `s`'s stack. +//! - **`Stuck`, `OutOfFuel`**: nothing is claimed. A bounds-check failure +//! in a `load` or `store` is `Stuck` in LIR (choice 11). +//! +//! That is a forward simulation of one LIR evaluation by one run of the +//! translation. LIR's evaluation is fuelled, so a divergent program is +//! `OutOfFuel` at every fuel and says nothing, and a terminating one is +//! matched by a terminating run. +//! +//! # What is not stated +//! +//! - That the output is **accepted by the A64 validator** (checked by +//! `tests/validator_a64_cf.rs`, as for the first slice). +//! - Typed blocks, the `f64` operations, a frame beyond 31 slots (see +//! [`crate::cfspec`]). +//! - Anything about `pc`, flags or scratch registers beyond what is said. + +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::contract::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every address the code can end at or branch to is a 4-byte-aligned address +/// below `2^64`: the end of the code, and each label. +pub open spec fn cf_canon(pc: int, offs: Seq, code: Seq) -> bool { + pc + code_len(code) < isa::p2(64) && forall|l: int| + 0 <= l < offs.len() ==> 0 <= pc + #[trigger] offs[l] && pc + offs[l] < isa::p2(64) + && offs[l] % 4 == 0 +} + +/// What the machine is at after an exit sequence from a state: the exit-code +/// word holds `c`, and `pc` is the return address in the link register (see +/// [`exit_refines`]). +pub open spec fn exit_done(m: isa::State, m2: isa::State, c: nat) -> bool { + isa::mem_read(m2.mem, ctx_of(m) + abi::exit_code_off(), 8) == Some(c as int) && m2.pc + == m.r[abi::link_reg() as int] +} + +/// The operands `vals` of an exit are in the frame, in the slots after the +/// first `n0` stack slots (`nl` locals, then the stack): value `i` in slot +/// `nl + n0 + i`. +pub open spec fn ops_in_frame( + mm: Map, + ctxb: int, + nl: nat, + n0: nat, + vals: Seq, +) -> bool { + forall|i: int| + 0 <= i < vals.len() ==> #[trigger] slot_holds( + mm, + ctxb, + (nl + n0 + i) as nat, + lir::val_bits(vals[i]), + ) +} + +/// The machine represents `s` on top of `base` and **some extra values** +/// (the bases that nested bodies added: see [`crate::cfspec`], choice 13). +pub open spec fn rel_ext( + lay: lir::Layout, + base: Seq, + s: lir::St, + m2: isa::State, + nl: nat, +) -> bool { + exists|extra: Seq| + #![trigger rel_base(lay, base + extra, s, m2, nl)] + rel_base(lay, base + extra, s, m2, nl) +} + +/// [`rel_ext`], and the operands `vals` of an exit are in the frame above the +/// stack (`ctxb` is the context's address). +pub open spec fn exit_rel( + lay: lir::Layout, + base: Seq, + s: lir::St, + m2: isa::State, + nl: nat, + ctxb: int, + vals: Seq, +) -> bool { + exists|extra: Seq| + #![trigger rel_base(lay, base + extra, s, m2, nl)] + rel_base(lay, base + extra, s, m2, nl) && ops_in_frame( + m2.mem, + ctxb, + nl, + (base + extra).len() + s.stack.len(), + vals, + ) +} + +/// What must hold after `code` ran from `m`, for LIR's outcome `o` (see the +/// module doc). +pub open spec fn cf_post( + lay: lir::Layout, + base: Seq, + nl: nat, + offs: Seq, + code: Seq, + m: isa::State, + o: lir::Outcome, +) -> bool { + match o { + lir::Outcome::Normal(a2) => exists|m2: isa::State| + #![trigger reaches(code, m.pc as int, m, m2)] + reaches(code, m.pc as int, m, m2) && m2.pc as int == m.pc as int + code_len(code) + && rel_base(lay, base, a2, m2, nl) && preserved_r(lay, m, m2), + lir::Outcome::Branch(l, s) => l < offs.len() && exists|m2: isa::State| + #![trigger reaches(code, m.pc as int, m, m2)] + reaches(code, m.pc as int, m, m2) && m2.pc as int == m.pc as int + offs[l as int] + && rel_ext(lay, base, s, m2, nl) && preserved_r(lay, m, m2), + lir::Outcome::Exit(c, vals, s) => exists|m2: isa::State| + #![trigger reaches(code, m.pc as int, m, m2)] + reaches(code, m.pc as int, m, m2) && exit_done(m, m2, c) && exit_rel( + lay, + base, + s, + m2, + nl, + ctx_of(m), + vals, + ) && preserved_mem_r(lay, m, m2), + _ => true, + } +} + +/// **`code` refines the LIR instruction `i`** at stack height `h` with `nl` +/// locals, the labels in scope at the byte offsets `offs` (see the module +/// doc). +pub open spec fn cf_refines( + i: lir::Instr, + nl: nat, + h: nat, + offs: Seq, + code: Seq, +) -> bool { + forall|lay: lir::Layout, base: Seq, a: lir::St, m: isa::State, fuel: nat| + #![trigger cf_post(lay, base, nl, offs, code, m, lir::eval(lay, i, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && cf_canon( + m.pc as int, + offs, + code, + ) ==> cf_post(lay, base, nl, offs, code, m, lir::eval(lay, i, a, fuel)) +} + +/// The exit sequence for `c` refines LIR's `exit c` **with `k` operands**: +/// [`exit_refines`], and the operands are still in the frame afterwards, in +/// the slots above the stack the exit leaves (the exit sequence writes only +/// the exit-code word, so they are never overwritten; the host reads them +/// from there). +pub open spec fn exit_ops_refines(c: u16, k: nat, nl: nat) -> bool { + forall|lay: lir::Layout, a: lir::St, m: isa::State, fuel: nat| + #![trigger lir::eval(lay, exit_instr(c as nat, k), a, fuel), run_seq(m, exit_seq(c))] + rel(lay, a, m, nl) && (m.pc as int) + code_len(exit_seq(c)) < isa::p2(64) + ==> match lir::eval(lay, exit_instr(c as nat, k), a, fuel) { + lir::Outcome::Exit(code, vals, a2) => match run_seq(m, exit_seq(c)) { + Some(m2) => rel(lay, a2, m2, nl) && preserved_mem(m, m2) && exit_done(m, m2, code) + && m2.r[abi::link_reg() as int] == m.r[abi::link_reg() as int] && m2.sp + == m.sp && forall|i: int| + 0 <= i < vals.len() ==> #[trigger] slot_holds( + m2.mem, + ctx_of(m), + (nl + a2.stack.len() + i) as nat, + lir::val_bits(vals[i]), + ), + None => false, + }, + _ => true, + } +} + +// ---------------------------------------------------------------------------- +// The emitter's contract for control flow (the executable back end) +// ---------------------------------------------------------------------------- + +/// Postcondition of the control-flow emitter's `emit_cf_blob` (the code it +/// must return is [`crate::a64::cftemplates::ctmpl`]): the bytes are the +/// canonical encodings ([`clazel_codecheck::a64::spec::decodes_to`], which is +/// what the validator reads back) of a body that refines the program `n` +/// with the function's return label at the end of the body (a `br` to it, and +/// falling off the end, reach the exit sequence that follows), followed by the +/// exit sequence for `exit_code`. `None` exactly when [`cf_prog_ok`] refuses. +pub open spec fn emit_cf_blob_ensures( + n: Node, + nl: usize, + exit_code: u16, + r: Option>, +) -> bool { + match r { + Some(bytes) => cf_prog_ok(n, nl as nat) && exists|body: Seq| + #![trigger all_valid_instrs(body)] + all_valid_instrs(body) && cf_refines( + n@, + nl as nat, + 0, + seq![code_len(body) as int], + body, + ) && clazel_codecheck::a64::spec::decodes_to(bytes, body + exit_seq(exit_code)), + None => !cf_prog_ok(n, nl as nat), + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfemit.rs b/crates/clazel-jit/src/a64/cfemit.rs new file mode 100644 index 000000000..22afa3350 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfemit.rs @@ -0,0 +1,1551 @@ +//! The executable back end for **structured control flow, memory +//! instructions and `br_table`** (A64): [`emit_cf_blob`], **proven equal to the +//! templates**. The A64 counterpart of [`crate::cfemit`]; read that header for +//! the design, which is the same (an analysis [`analyze`] that equals `cf_flow` +//! and `csize`, then an emission [`emit_node`] that appends exactly +//! [`ctmpl`], leaf by leaf). +//! +//! [`emit_cf_blob`]'s postcondition is +//! [`super::cfcontract::emit_cf_blob_ensures`]: the bytes it returns are the +//! canonical encodings of [`super::cftemplates::ctmpl`] (the body, with the +//! return label at its end) followed by the exit sequence, and it answers +//! `None` exactly when [`super::cftemplates::cf_prog_ok`] refuses. Together +//! with [`super::cfproofs::lemma_ctmpl_refines`] that makes every emitted blob +//! a refinement of its program ([`lemma_emitted_refines`]). +//! +//! # Where it differs from x86-64's +//! +//! - **Positions are words.** Every instruction is 4 bytes, so a position is +//! a `usize` count of words below [`MAX_WORDS`] (the spec's `max_size` is +//! `4 * MAX_WORDS` bytes), and the spec's byte offsets of the labels in scope +//! are `4 * (position - start)` ([`offs_of`]). +//! - **Displacements are in words** from the branch itself, so a displacement +//! is `target - position of the branch` ([`relw`]). They fit `imm26` and +//! `imm19` because every position is below [`MAX_WORDS`], which is why the +//! size bound (`2^19` bytes) is what it is. Negative ones are formed, as in +//! x86-64's emitter, without signed arithmetic: `!(pos - target - 1)` on a +//! `u32` ([`lemma_not_neg`]). +//! - **Exit codes** are checked to be 16 bits first ([`codes_ok_exec`]), then +//! assumed by [`analyze`] and the emission. + +#[cfg(verus_only)] +use super::cfblobacc::{emit_cf_blob_accepted_ensures, emitted_ins, lemma_emitter_accepted}; +#[cfg(verus_only)] +use super::cfbrt::*; +#[cfg(verus_only)] +use super::cfcompose::lemma_shift_index; +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cffit::*; +#[cfg(verus_only)] +use super::cflens::*; +#[cfg(verus_only)] +use super::cfproofs::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::contract::*; +use super::emit::{emit_exit, emit_op, ex_load, ex_store, op_ok_exec, pops_exec, pushes_exec}; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +use crate::cfspec::{MTy, MemOp, Node}; +use clazel_a64::encoder::encode_instr; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The largest body this emitter translates, in words (the spec's +/// `cftemplates::max_size`, `2^19` bytes). +pub const MAX_WORDS: usize = 0x2_0000; + +/// The most targets of a `br_table` (the spec's `max_targets`). +pub const MAX_TARGETS: usize = 0x10000; + +/// The emitter's limits are the spec's. +pub proof fn lemma_emitter_limits() + ensures + 4 * MAX_WORDS as nat == max_size(), + MAX_TARGETS as nat == max_targets(), +{ +} + +// ---------------------------------------------------------------------------- +// Arithmetic (the workspace denies the unchecked operators) +// ---------------------------------------------------------------------------- + +/// `a + b`, when it does not overflow. +fn add_u(a: usize, b: usize) -> (r: usize) + requires + a + b <= usize::MAX, + ensures + r == a + b, +{ + a.saturating_add(b) +} + +/// `a - b`, when it does not underflow. +fn sub_u(a: usize, b: usize) -> (r: usize) + requires + b <= a, + ensures + r == a - b, +{ + a.saturating_sub(b) +} + +/// `a * b`, when it does not overflow. +fn mul_u(a: usize, b: usize) -> (r: usize) + requires + a * b <= usize::MAX, + ensures + r == a * b, +{ + a.saturating_mul(b) +} + +/// The bit-complement of a small `u32`, read as an `i32`, is `-x - 1`. +proof fn lemma_not_neg(x: u32) + requires + x < 0x4000_0000, + ensures + (!x) as i32 as int == -(x as int) - 1, +{ + assert((!x) as i32 == -(x as i32) - 1) by (bit_vector) + requires + x < 0x4000_0000, + ; + assert((x as i32) as int == x as int); +} + +/// The displacement in words from a branch at word `pos` to word `target`. +/// Both are at most [`MAX_WORDS`], so it fits an `i32`; a negative one is the +/// two's complement `!(pos - target - 1)`. +fn relw(target: usize, pos: usize) -> (r: i32) + requires + target <= MAX_WORDS, + pos <= MAX_WORDS, + ensures + r as int == target as int - pos as int, +{ + if target >= pos { + let d = sub_u(target, pos); + d as i32 + } else { + let d1 = sub_u(sub_u(pos, target), 1); + let x = d1 as u32; + let y = !x; + proof { + lemma_not_neg(x); + } + y as i32 + } +} + +/// Every exit code in the node is 16 bits (the spec's [`codes_ok`]). +fn codes_ok_exec(n: &Node) -> (r: bool) + ensures + r == codes_ok(*n), + decreases *n, +{ + match n { + Node::Op(_) => true, + Node::Mem(m) => match m { + MemOp::BoundsCheck { code, .. } => *code < 0x1_0000, + _ => true, + }, + Node::Then(a, b) => codes_ok_exec(a) && codes_ok_exec(b), + Node::Block(b) => codes_ok_exec(b), + Node::Loop(b) => codes_ok_exec(b), + Node::If(t, e) => codes_ok_exec(t) && codes_ok_exec(e), + Node::Br(_) => true, + Node::BrIf(_) => true, + Node::BrTable(_, _) => true, + Node::Exit(c, _) => *c < 0x1_0000, + } +} + +// ---------------------------------------------------------------------------- +// Labels +// ---------------------------------------------------------------------------- + +/// The spec's label offsets (bytes) for a node that starts at word `start`, +/// when the labels are at the absolute word positions `labs`. +pub open spec fn offs_of(labs: Seq, start: int) -> Seq { + Seq::new(labs.len(), |i: int| 4 * (labs[i] as int - start)) +} + +/// Moving the start on by `d` words is [`shift`] by `4 d` bytes. +proof fn lemma_offs_shift(labs: Seq, start: int, d: int) + ensures + offs_of(labs, start + d) =~= shift(offs_of(labs, start), 4 * d), +{ + let o = offs_of(labs, start); + assert forall|i: int| 0 <= i < labs.len() implies #[trigger] offs_of(labs, start + d)[i] + == shift(o, 4 * d)[i] by { + lemma_shift_index(o, 4 * d, i); + } +} + +/// A new label in front, at the absolute word `new`. +proof fn lemma_offs_push(labs: Seq, new: usize, start: int) + ensures + offs_of(seq![new] + labs, start) =~= seq![4 * (new as int - start)] + offs_of(labs, start), +{ + let l = seq![new] + labs; + let r = seq![4 * (new as int - start)] + offs_of(labs, start); + assert(l.len() == r.len()); + assert forall|i: int| 0 <= i < l.len() implies #[trigger] offs_of(l, start)[i] == r[i] by { + if i > 0 { + assert(l[i] == labs[i - 1]); + } + } +} + +/// `new` followed by `labs`. +fn push_label(new: usize, labs: &[usize]) -> (v: Vec) + ensures + v@ == seq![new] + labs@, +{ + let mut v: Vec = Vec::new(); + v.push(new); + v.extend_from_slice(labs); + proof { + assert(v@ =~= seq![new] + labs@); + } + v +} + +// ---------------------------------------------------------------------------- +// Leaves +// ---------------------------------------------------------------------------- + +/// The flow of an analysis, as the spec's [`Flow`]: `Some(h)` falls through at +/// height `h`, `None` is dead. +pub open spec fn fl_spec(f: Option) -> Flow { + match f { + Some(x) => Flow::Falls(x as nat), + None => Flow::Dead, + } +} + +/// `ldr x0, [top of stack]; cbz/cbnz w0, rel` with `rel` words from the +/// branch. +fn cond_pre_exec(nl: usize, h: usize, iszero: bool, rel: i32, v: &mut Vec) + requires + 1 <= h, + nl + h <= 31, + ensures + final(v)@ == old(v)@ + cond_pre(nl as nat, h as nat, iszero, 4 * (rel as int)), +{ + let ghost ov = v@; + let top = sub_u(add_u(nl, h), 1); + v.push(ex_load(0, top)); + v.push(isa::Instr::CompareBranch { sf: isa::Width::W32, iszero, t: 0, imm19: rel }); + proof { + assert((4 * (rel as int)) / 4 == rel as int); + assert(v@ =~= ov + cond_pre(nl as nat, h as nat, iszero, 4 * (rel as int))); + } +} + +/// The access width of a type. +fn mty_sf_exec(ty: &MTy) -> (s: isa::Width) + ensures + s == mty_sf(*ty), +{ + match ty { + MTy::I32 => isa::Width::W32, + MTy::I64 => isa::Width::W64, + MTy::F64 => isa::Width::W64, + } +} + +/// The number of bytes of an access. +fn mty_size_exec(ty: &MTy) -> (n: usize) + ensures + n as nat == ty.size(), +{ + match ty { + MTy::I32 => 4, + MTy::I64 => 8, + MTy::F64 => 8, + } +} + +/// `ldr x3, [x28, #16 k]`: region `k`'s base. +fn base_ld_exec(k: usize) -> (i: isa::Instr) + requires + k < 16, + ensures + i == base_ld(k as nat), +{ + proof { + super::steps::lemma_abi_consts(); + } + let d = mul_u(k, 2); + isa::Instr::LoadStoreImm { sf: isa::Width::W64, load: true, t: 3, n: 28, imm12: d as u16 } +} + +/// The lengths of a memory instruction's parts. +proof fn lemma_mem_lens(m: MemOp, nl: nat, h: nat) + ensures + match m { + MemOp::BoundsCheck { .. } => mem_pre(m, nl, h).len() == 1 && mem_tail(m, nl, h).len() + == 0, + MemOp::Load { .. } => mem_pre(m, nl, h).len() == 2 && mem_tail(m, nl, h).len() == 2, + MemOp::Store { .. } => mem_pre(m, nl, h).len() == 3 && mem_tail(m, nl, h).len() == 1, + }, + mem_check(m, nl, h).len() == 7, + mem_code(m, nl, h).len() == mem_pre(m, nl, h).len() + 7 + mem_tail(m, nl, h).len() + 4, +{ +} + +/// The facts a memory instruction's parts need. +proof fn lemma_mem_facts(m: MemOp, nl: nat, h: nat) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + ensures + nl + h <= 31, + 1 <= h, + mem_region(m) < 16, + m is Store ==> 2 <= h, +{ + super::tlemmas::lemma_abi_consts_t(); +} + +/// What a memory instruction does before its check. +fn mem_pre_exec(m: &MemOp, nl: usize, h: usize, v: &mut Vec) + requires + cf_flow(Node::Mem(*m), nl as nat, h as nat, 0) is Some, + ensures + final(v)@ == old(v)@ + mem_pre(*m, nl as nat, h as nat), +{ + let ghost ov = v@; + proof { + lemma_mem_facts(*m, nl as nat, h as nat); + } + let top = sub_u(add_u(nl, h), 1); + match m { + MemOp::BoundsCheck { .. } => { + v.push(ex_load(1, top)); + }, + MemOp::Load { region, .. } => { + v.push(ex_load(1, top)); + v.push(base_ld_exec(*region)); + }, + MemOp::Store { region, .. } => { + v.push(ex_load(4, top)); + v.push(ex_load(1, sub_u(top, 1))); + v.push(base_ld_exec(*region)); + }, + } + proof { + assert(v@ =~= ov + mem_pre(*m, nl as nat, h as nat)); + } +} + +/// What a memory instruction does after its check. +fn mem_tail_exec(m: &MemOp, nl: usize, h: usize, v: &mut Vec) + requires + cf_flow(Node::Mem(*m), nl as nat, h as nat, 0) is Some, + ensures + final(v)@ == old(v)@ + mem_tail(*m, nl as nat, h as nat), +{ + let ghost ov = v@; + proof { + lemma_mem_facts(*m, nl as nat, h as nat); + } + let top = sub_u(add_u(nl, h), 1); + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { ty, .. } => { + v.push( + isa::Instr::LoadStoreReg { + sf: mty_sf_exec(ty), + load: true, + t: 0, + n: 3, + m: 1, + s: false, + }, + ); + v.push(ex_store(top, 0)); + }, + MemOp::Store { ty, .. } => { + v.push( + isa::Instr::LoadStoreReg { + sf: mty_sf_exec(ty), + load: false, + t: 4, + n: 3, + m: 1, + s: false, + }, + ); + }, + } + proof { + assert(v@ =~= ov + mem_tail(*m, nl as nat, h as nat)); + } +} + +/// Append the code of a memory instruction (`mem_code`): the operands, the +/// validator's bounds check, the access, a `b` over the stub, the stub. +fn mem_code_exec(m: &MemOp, nl: usize, h: usize, out: &mut Vec) + requires + cf_flow(Node::Mem(*m), nl as nat, h as nat, 0) is Some, + codes_ok(Node::Mem(*m)), + ensures + final(out)@ == old(out)@ + mem_code(*m, nl as nat, h as nat), +{ + proof { + lemma_mem_facts(*m, nl as nat, h as nat); + lemma_mem_lens(*m, nl as nat, h as nat); + super::steps::lemma_abi_consts(); + } + let ghost old_out = out@; + let (region, n, exit_code, tl): (usize, usize, u16, usize) = match m { + MemOp::BoundsCheck { region, size, code } => (*region, *size, *code as u16, 0), + MemOp::Load { region, ty } => (*region, mty_size_exec(ty), 0xFFFF, 2), + MemOp::Store { region, ty } => (*region, mty_size_exec(ty), 0xFFFF, 1), + }; + proof { + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { ty, .. } => { + assert(mem_n(*m) == ty.size()); + }, + MemOp::Store { ty, .. } => { + assert(mem_n(*m) == ty.size()); + }, + } + assert(n as nat == mem_n(*m)); + assert(region as nat == mem_region(*m)); + assert(exit_code == mem_exit_code(*m)); + assert(tl as nat == mem_tail(*m, nl as nat, h as nat).len()); + assert(n <= 8); + } + mem_pre_exec(m, nl, h, out); + let r1 = add_u(6, tl) as i32; + let r2 = add_u(4, tl) as i32; + let bd = add_u(mul_u(region, 2), 1) as u16; + out.push( + isa::Instr::LoadStoreImm { sf: isa::Width::W64, load: true, t: 2, n: 28, imm12: bd }, + ); + out.push( + isa::Instr::AddSubImm { + sf: isa::Width::W64, + sub_op: false, + setflags: true, + d: 0, + n: 1, + imm12: n as u16, + sh: false, + }, + ); + out.push(isa::Instr::BCond { cond: 2, imm19: r1 }); + out.push( + isa::Instr::AddSubShiftedReg { + sf: isa::Width::W64, + sub_op: true, + setflags: true, + d: 31, + n: 0, + m: 2, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + ); + out.push(isa::Instr::BCond { cond: 8, imm19: r2 }); + out.push( + isa::Instr::AddSubShiftedReg { + sf: isa::Width::W64, + sub_op: true, + setflags: true, + d: 31, + n: 1, + m: 2, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + ); + out.push( + isa::Instr::CondSelect { + sf: isa::Width::W64, + else_inv: false, + else_inc: false, + d: 1, + n: 1, + m: 31, + cond: 3, + }, + ); + mem_tail_exec(m, nl, h, out); + out.push(isa::Instr::B { imm26: 4 }); + emit_exit(out, exit_code); + proof { + let c = mem_chk( + mem_region(*m), + mem_n(*m), + mem_r1(*m, nl as nat, h as nat), + mem_r2(*m, nl as nat, h as nat), + ); + let cs = clazel_codecheck::a64::spec::check_seq(c); + assert(r1 as int == mem_r1(*m, nl as nat, h as nat)); + assert(r2 as int == mem_r2(*m, nl as nat, h as nat)); + assert(abi::bound_off(mem_region(*m) as int) / 8 == bd as int); + assert(code_len(exit_seq(mem_exit_code(*m))) == 12); + assert(cs =~= seq![ + isa::Instr::LoadStoreImm { sf: isa::Width::W64, load: true, t: 2, n: 28, imm12: bd }, + isa::Instr::AddSubImm { + sf: isa::Width::W64, + sub_op: false, + setflags: true, + d: 0, + n: 1, + imm12: n as u16, + sh: false, + }, + isa::Instr::BCond { cond: 2, imm19: r1 }, + isa::Instr::AddSubShiftedReg { + sf: isa::Width::W64, + sub_op: true, + setflags: true, + d: 31, + n: 0, + m: 2, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + isa::Instr::BCond { cond: 8, imm19: r2 }, + isa::Instr::AddSubShiftedReg { + sf: isa::Width::W64, + sub_op: true, + setflags: true, + d: 31, + n: 1, + m: 2, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + isa::Instr::CondSelect { + sf: isa::Width::W64, + else_inv: false, + else_inc: false, + d: 1, + n: 1, + m: 31, + cond: 3, + }, + ]); + assert(b_i(code_len(exit_seq(mem_exit_code(*m))) as int + 4) == isa::Instr::B { imm26: 4 }); + assert(out@ =~= old_out + mem_code(*m, nl as nat, h as nat)); + } +} + +/// Spec of the chain's entries `0..i`. +pub open spec fn brt_upto(ls: Seq, offs: Seq, i: nat) -> Seq + decreases i, +{ + if i == 0 { + seq![] + } else { + brt_upto(ls, offs, (i - 1) as nat) + brt_entry( + (i - 1) as nat, + brt_rel(offs, ls[i - 1], (4 + (i - 1) * 12 + 8) as int), + ) + } +} + +/// The entries `0..i` then the entries from `i` are all of them. +proof fn lemma_brt_upto(ls: Seq, offs: Seq, i: nat) + requires + i <= ls.len(), + ensures + brt_upto(ls, offs, i) + brt_entries(ls, offs, 4, i) =~= brt_entries(ls, offs, 4, 0), + decreases i, +{ + assert(brt_e() == 12); + if i > 0 { + lemma_brt_upto(ls, offs, (i - 1) as nat); + let k = (i - 1) as nat; + assert(brt_entries(ls, offs, 4, k) == brt_entry( + k, + brt_rel(offs, ls[k as int], 4 + k * brt_e() + 8), + ) + brt_entries(ls, offs, 4, i)); + assert((brt_upto(ls, offs, k) + brt_entry( + k, + brt_rel(offs, ls[k as int], (4 + k * 12 + 8) as int), + )) + brt_entries(ls, offs, 4, i) =~= brt_upto(ls, offs, k) + brt_entries( + ls, + offs, + 4, + k, + )); + } +} + +/// Append the code of `br_table ls d` at the absolute word `start`. +fn brt_exec( + ls: &[usize], + d: usize, + nl: usize, + h: usize, + start: usize, + labs: &[usize], + out: &mut Vec, +) + requires + 1 <= h, + nl + h <= 31, + ls@.len() <= 0x10000, + d < labs@.len(), + forall|i: int| 0 <= i < ls@.len() ==> #[trigger] ls@[i] < labs@.len(), + forall|i: int| 0 <= i < labs@.len() ==> #[trigger] labs@[i] <= MAX_WORDS, + start + 2 + 3 * ls@.len() <= MAX_WORDS, + ensures + final(out)@ == old(out)@ + brt_code( + ls@, + d, + nl as nat, + h as nat, + offs_of(labs@, start as int), + ), +{ + let ghost old_out = out@; + let ghost offs = offs_of(labs@, start as int); + proof { + assert(brt_p0(nl as nat, h as nat) == 4); + assert(brt_e() == 12); + } + let top = sub_u(add_u(nl, h), 1); + out.push(ex_load(0, top)); + let mut i: usize = 0; + while i < ls.len() + invariant + i <= ls.len(), + 1 <= h, + nl + h <= 31, + ls@.len() <= 0x10000, + forall|j: int| 0 <= j < ls@.len() ==> #[trigger] ls@[j] < labs@.len(), + forall|j: int| 0 <= j < labs@.len() ==> #[trigger] labs@[j] <= MAX_WORDS, + start + 2 + 3 * ls@.len() <= MAX_WORDS, + offs == offs_of(labs@, start as int), + out@ == old_out + seq![load_slot(x0(), (nl + h - 1) as nat)] + brt_upto( + ls@, + offs, + i as nat, + ), + decreases ls.len() - i, + { + let l = match ls.get(i) { + Some(l) => *l, + None => { + proof { + assert(false); + } + return ; + }, + }; + let tgt = match labs.get(l) { + Some(t) => *t, + None => { + proof { + assert(false); + } + return ; + }, + }; + // the `b.eq` is word `start + 3 + 3 i` + let pos = add_u(add_u(start, 3), mul_u(3, i)); + let rel = relw(tgt, pos); + out.push( + isa::Instr::MoveWide { + sf: isa::Width::W32, + opcode: isa::MoveWideOp::Z, + d: 1, + imm16: i as u16, + hw: 0, + }, + ); + out.push( + isa::Instr::AddSubShiftedReg { + sf: isa::Width::W32, + sub_op: true, + setflags: true, + d: 31, + n: 0, + m: 1, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + ); + out.push(isa::Instr::BCond { cond: 0, imm19: rel }); + proof { + let ii = (i + 1) as nat; + let r = brt_rel(offs, ls@[i as int], (4 + i * 12 + 8) as int); + assert(r == 4 * (tgt as int - pos as int)); + assert(r / 4 == rel as int); + assert(brt_upto(ls@, offs, ii) == brt_upto(ls@, offs, i as nat) + brt_entry( + i as nat, + r, + )); + assert(brt_entry(i as nat, r) =~= seq![ + isa::Instr::MoveWide { + sf: isa::Width::W32, + opcode: isa::MoveWideOp::Z, + d: 1, + imm16: i as u16, + hw: 0, + }, + isa::Instr::AddSubShiftedReg { + sf: isa::Width::W32, + sub_op: true, + setflags: true, + d: 31, + n: 0, + m: 1, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + isa::Instr::BCond { cond: 0, imm19: rel }, + ]); + assert(out@ =~= old_out + seq![load_slot(x0(), (nl + h - 1) as nat)] + brt_upto( + ls@, + offs, + ii, + )); + } + i = add_u(i, 1); + } + let tgt = match labs.get(d) { + Some(t) => *t, + None => { + proof { + assert(false); + } + return ; + }, + }; + let pos = add_u(add_u(start, 1), mul_u(3, ls.len())); + let rl = relw(tgt, pos); + out.push(isa::Instr::B { imm26: rl }); + proof { + lemma_brt_upto(ls@, offs, ls@.len()); + assert(brt_entries(ls@, offs, 4, ls@.len() as nat) =~= Seq::::empty()); + assert(brt_p0(nl as nat, h as nat) == 4); + assert(brt_e() == 12); + assert(brt_upto(ls@, offs, ls@.len()) =~= brt_entries(ls@, offs, 4, 0)); + let r = brt_rel(offs, d, (4 + ls@.len() * 12) as int); + assert(r == 4 * (tgt as int - pos as int)); + assert(r / 4 == rl as int); + assert(out@ =~= old_out + brt_code(ls@, d, nl as nat, h as nat, offs)); + } +} + +// ---------------------------------------------------------------------------- +// The analysis +// ---------------------------------------------------------------------------- + +/// Every node is templated in this slice. +proof fn lemma_templated_all(n: Node) + ensures + templated(n), + decreases n, +{ + match n { + Node::Then(a, b) => { + lemma_templated_all(*a); + lemma_templated_all(*b); + }, + Node::Block(b) => { + lemma_templated_all(*b); + }, + Node::Loop(b) => { + lemma_templated_all(*b); + }, + Node::If(t, e) => { + lemma_templated_all(*t); + lemma_templated_all(*e); + }, + _ => {}, + } +} + +/// The analysis of `n` (the spec's `cf_flow`), entered at height `h` with `nl` +/// locals and `labs.len()` labels in scope, and the size of its code in words +/// (a quarter of the spec's `csize`). `None` only if it has no translation, or +/// its code is larger than `max_size`. +#[verifier::spinoff_prover] +#[verifier::rlimit(1500)] +fn analyze(n: &Node, nl: usize, h: usize, labs: &[usize]) -> (r: Option<(Option, usize)>) + requires + nl <= 31, + h <= 31, + codes_ok(*n), + ensures + match r { + Some((f, s)) => { + &&& cf_flow(*n, nl as nat, h as nat, labs@.len()) == Some(fl_spec(f)) + &&& 4 * s == csize(*n, nl as nat, h as nat, labs@.len()) + &&& s <= MAX_WORDS + &&& match f { + Some(x) => x <= 31, + None => true, + } + }, + None => cf_flow(*n, nl as nat, h as nat, labs@.len()) is None || csize( + *n, + nl as nat, + h as nat, + labs@.len(), + ) > max_size(), + }, + decreases *n, +{ + proof { + super::tlemmas::lemma_abi_consts_t(); + } + let depth = labs.len(); + let room = add_u(nl, h) <= 31; + match n { + Node::Op(o) => { + let ok = op_ok_exec(o, nl, h); + if !ok { + return None; + } + let mut v: Vec = Vec::new(); + emit_op(o, nl, h, &mut v); + proof { + assert(v@ =~= tmpl(*o, nl as nat, h as nat)); + assert(csize(*n, nl as nat, h as nat, depth as nat) == code_len(v@)); + } + let sz = v.len(); + if sz > MAX_WORDS { + return None; + } + let h2 = add_u(sub_u(h, pops_exec(o)), pushes_exec(o)); + Some((Some(h2), sz)) + }, + Node::Mem(m) => { + let ok = room && match m { + MemOp::BoundsCheck { region, size, .. } => h >= 1 && *region < 16 && 1 <= *size + && *size <= 8, + MemOp::Load { region, .. } => h >= 1 && *region < 16, + MemOp::Store { region, .. } => h >= 2 && *region < 16, + }; + if !ok { + return None; + } + let h2 = match m { + MemOp::Store { .. } => sub_u(h, 2), + _ => h, + }; + proof { + assert(cf_flow(*n, nl as nat, h as nat, depth as nat) is Some); + } + let mut v: Vec = Vec::new(); + mem_code_exec(m, nl, h, &mut v); + proof { + assert(v@ =~= mem_code(*m, nl as nat, h as nat)); + assert(csize(*n, nl as nat, h as nat, depth as nat) == code_len(v@)); + } + let sz = v.len(); + if sz > MAX_WORDS { + return None; + } + Some((Some(h2), sz)) + }, + Node::Then(a, b) => { + proof { + lemma_csize_then(**a, **b, nl as nat, h as nat, depth as nat); + } + let (fa, sa) = match analyze(a, nl, h, labs) { + Some(x) => x, + None => { + return None; + }, + }; + match fa { + Some(h1) => { + let (fb, sb) = match analyze(b, nl, h1, labs) { + Some(x) => x, + None => { + return None; + }, + }; + let s = add_u(sa, sb); + if s > MAX_WORDS { + return None; + } + Some((fb, s)) + }, + None => { + if matches!(&**b, Node::Op(crate::spec::Op::Nop)) { + proof { + assert(csize(**b, nl as nat, h as nat, depth as nat) == 0) by { + assert(ctmpl(**b, nl as nat, h as nat, zeros(depth as nat)) + =~= Seq::::empty()); + } + } + Some((None, sa)) + } else { + None + } + }, + } + }, + Node::Block(b) | Node::Loop(b) => { + proof { + if n is Block { + lemma_csize_block(**b, nl as nat, h as nat, depth as nat); + } else { + lemma_csize_loop(**b, nl as nat, h as nat, depth as nat); + } + } + let l0 = push_label(0, labs); + let (fb, sb) = match analyze(b, nl, h, l0.as_slice()) { + Some(x) => x, + None => { + return None; + }, + }; + let bad = match fb { + Some(h1) => h1 != h, + None => false, + }; + if bad { + return None; + } + Some((Some(h), sb)) + }, + Node::If(t, e) => { + if h < 1 || !room { + return None; + } + let h1 = sub_u(h, 1); + proof { + lemma_csize_if(**t, **e, nl as nat, h as nat, depth as nat); + } + let l0 = push_label(0, labs); + let (ft, st) = match analyze(t, nl, h1, l0.as_slice()) { + Some(x) => x, + None => { + return None; + }, + }; + let (fe, se) = match analyze(e, nl, h1, l0.as_slice()) { + Some(x) => x, + None => { + return None; + }, + }; + let fits_t = match ft { + Some(x) => x == h1, + None => true, + }; + let fits_e = match fe { + Some(x) => x == h1, + None => true, + }; + if !fits_t || !fits_e { + return None; + } + let s = add_u(add_u(add_u(2, st), 1), se); + if s > MAX_WORDS { + return None; + } + Some((Some(h1), s)) + }, + Node::Br(l) => { + proof { + lemma_csize_br(*l, nl as nat, h as nat, depth as nat); + } + if *l < depth { + Some((None, 1)) + } else { + None + } + }, + Node::BrIf(l) => { + proof { + lemma_csize_brif(*l, nl as nat, h as nat, depth as nat); + } + if *l < depth && h >= 1 && room { + Some((Some(sub_u(h, 1)), 2)) + } else { + None + } + }, + Node::BrTable(ls, d) => { + if !(*d < depth && h >= 1 && room && ls.len() <= MAX_TARGETS) { + return None; + } + let mut i: usize = 0; + while i < ls.len() + invariant + i <= ls.len(), + depth == labs@.len(), + *n == Node::BrTable(*ls, *d), + *d < depth, + 1 <= h, + nl + h <= 31, + ls@.len() <= 0x10000, + forall|j: int| 0 <= j < i ==> #[trigger] ls@[j] < depth, + decreases ls.len() - i, + { + let l = match ls.get(i) { + Some(l) => *l, + None => { + proof { + assert(false); + } + return None; + }, + }; + if l >= depth { + proof { + assert(ls@[i as int] >= depth); + } + return None; + } + i = add_u(i, 1); + } + proof { + lemma_brt_len(ls@, *d, nl as nat, h as nat, zeros(depth as nat)); + assert(csize(*n, nl as nat, h as nat, depth as nat) == 8 + 12 * ls@.len()); + } + let s = add_u(2, mul_u(3, ls.len())); + if s > MAX_WORDS { + return None; + } + Some((None, s)) + }, + Node::Exit(_, k) => { + proof { + assert(csize(*n, nl as nat, h as nat, depth as nat) == 12); + } + if *k <= h && room { + Some((None, 3)) + } else { + None + } + }, + } +} + +// ---------------------------------------------------------------------------- +// The emission +// ---------------------------------------------------------------------------- + +/// The facts every emission function needs of its node. +pub open spec fn emit_pre(n: Node, nl: usize, h: usize, start: usize, labs: Seq) -> bool { + nl <= 31 && h <= 31 && cf_flow(n, nl as nat, h as nat, labs.len()) is Some && codes_ok(n) + && start + csize(n, nl as nat, h as nat, labs.len()) / 4 <= MAX_WORDS && forall|i: int| + 0 <= i < labs.len() ==> #[trigger] labs[i] <= MAX_WORDS +} + +/// `then a b`. +fn emit_then(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is Then, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), + decreases *n, 0nat, +{ + let ghost old_out = out@; + let ghost offs = offs_of(labs@, start as int); + if let Node::Then(a, b) = n { + { + proof { + super::tlemmas::lemma_abi_consts_t(); + lemma_csize_then(**a, **b, nl as nat, h as nat, labs@.len()); + } + let (fa, sa) = emit_node(a, nl, h, start, labs, out); + if let Some(h1) = fa { + proof { + lemma_offs_shift(labs@, start as int, sa as int); + } + emit_node(b, nl, h1, add_u(start, sa), labs, out); + proof { + assert(flow_h(**a, nl as nat, h as nat, labs@.len()) == h1 as nat); + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + } else { + proof { + lemma_offs_shift(labs@, start as int, sa as int); + assert(ctmpl(**b, nl as nat, h as nat, shift(offs, 4 * sa as int)) + =~= Seq::::empty()); + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + } + } + } +} + +/// `block b`. +fn emit_block(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is Block, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), + decreases *n, 0nat, +{ + let ghost offs = offs_of(labs@, start as int); + if let Node::Block(b) = n { + { + proof { + super::tlemmas::lemma_abi_consts_t(); + lemma_csize_block(**b, nl as nat, h as nat, labs@.len()); + } + let l0 = push_label(0, labs); + let sb = match analyze(b, nl, h, l0.as_slice()) { + Some(x) => x.1, + None => { + proof { + assert(false); + } + return ; + }, + }; + let l = push_label(add_u(start, sb), labs); + proof { + lemma_offs_push(labs@, (start + sb) as usize, start as int); + assert(offs_of(l@, start as int) =~= seq![4 * sb as int] + offs); + } + emit_node(b, nl, h, start, l.as_slice(), out); + } + } +} + +/// `loop b`. +fn emit_loop(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is Loop, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), + decreases *n, 0nat, +{ + let ghost offs = offs_of(labs@, start as int); + if let Node::Loop(b) = n { + { + proof { + super::tlemmas::lemma_abi_consts_t(); + lemma_csize_loop(**b, nl as nat, h as nat, labs@.len()); + } + let l = push_label(start, labs); + proof { + lemma_offs_push(labs@, start, start as int); + assert(offs_of(l@, start as int) =~= seq![0int] + offs); + } + emit_node(b, nl, h, start, l.as_slice(), out); + } + } +} + +/// `if t e`. +fn emit_if(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is If, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), + decreases *n, 0nat, +{ + let ghost old_out = out@; + let ghost offs = offs_of(labs@, start as int); + if let Node::If(t, e) = n { + { + proof { + super::tlemmas::lemma_abi_consts_t(); + } + let h1 = sub_u(h, 1); + proof { + lemma_csize_if(**t, **e, nl as nat, h as nat, labs@.len()); + } + let l0 = push_label(0, labs); + let st = match analyze(t, nl, h1, l0.as_slice()) { + Some(x) => x.1, + None => { + proof { + assert(false); + } + return ; + }, + }; + let se = match analyze(e, nl, h1, l0.as_slice()) { + Some(x) => x.1, + None => { + proof { + assert(false); + } + return ; + }, + }; + let end = add_u(add_u(add_u(add_u(start, 2), st), 1), se); + let l = push_label(end, labs); + let skip = add_u(st, 2) as i32; + cond_pre_exec(nl, h, true, skip, out); + let tstart = add_u(start, 2); + proof { + lemma_offs_push(labs@, end, tstart as int); + lemma_offs_shift(labs@, start as int, 2); + assert(offs_of(l@, tstart as int) =~= seq![4 * st as int + 4 + 4 * se as int] + + shift(offs, 8)); + } + emit_node(t, nl, h1, tstart, l.as_slice(), out); + out.push(isa::Instr::B { imm26: add_u(se, 1) as i32 }); + let estart = add_u(tstart, add_u(st, 1)); + proof { + lemma_offs_push(labs@, end, estart as int); + lemma_offs_shift(labs@, start as int, 2 + st as int + 1); + assert(offs_of(l@, estart as int) =~= seq![4 * se as int] + shift( + offs, + 12 + 4 * st as int, + )); + assert(b_i(4 * se as int + 4) == isa::Instr::B { imm26: (se + 1) as i32 }); + } + emit_node(e, nl, h1, estart, l.as_slice(), out); + proof { + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + } + } +} + +/// `br l` and `br_if l`. +fn emit_br(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is Br || n is BrIf, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), +{ + let ghost old_out = out@; + let ghost offs = offs_of(labs@, start as int); + proof { + super::tlemmas::lemma_abi_consts_t(); + } + match n { + Node::Br(l) => { + proof { + lemma_csize_br(*l, nl as nat, h as nat, labs@.len()); + } + let tgt = match labs.get(*l) { + Some(t) => *t, + None => { + proof { + assert(false); + } + return ; + }, + }; + let r = relw(tgt, start); + out.push(isa::Instr::B { imm26: r }); + proof { + assert(offs[*l as int] == 4 * (tgt as int - start as int)); + assert((4 * (tgt as int - start as int)) / 4 == r as int); + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + }, + Node::BrIf(l) => { + proof { + lemma_csize_brif(*l, nl as nat, h as nat, labs@.len()); + } + let tgt = match labs.get(*l) { + Some(t) => *t, + None => { + proof { + assert(false); + } + return ; + }, + }; + let rel = relw(tgt, add_u(start, 1)); + cond_pre_exec(nl, h, false, rel, out); + proof { + assert(offs[*l as int] == 4 * (tgt as int - start as int)); + assert(offs[*l as int] - 4 == 4 * (rel as int)); + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + }, + _ => {}, + } +} + +/// `br_table`, `exit`, memory and straight-line instructions. +fn emit_leaf(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is BrTable || n is Exit || n is Mem || n is Op, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), +{ + let ghost old_out = out@; + let ghost offs = offs_of(labs@, start as int); + proof { + super::tlemmas::lemma_abi_consts_t(); + } + match n { + Node::Op(o) => { + emit_op(o, nl, h, out); + proof { + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + }, + Node::Mem(m) => { + mem_code_exec(m, nl, h, out); + }, + Node::BrTable(ls, d) => { + proof { + lemma_brt_len(ls@, *d, nl as nat, h as nat, zeros(labs@.len())); + } + brt_exec(ls.as_slice(), *d, nl, h, start, labs, out); + }, + Node::Exit(c, _) => { + emit_exit(out, *c as u16); + }, + _ => {}, + } +} + +/// Append the code of `n` at the absolute word `start`, with the labels in +/// scope at the absolute words `labs`: exactly [`ctmpl`]. Returns the flow and +/// the size in words, as [`analyze`] does. +fn emit_node( + n: &Node, + nl: usize, + h: usize, + start: usize, + labs: &[usize], + out: &mut Vec, +) -> (r: (Option, usize)) + requires + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl( + *n, + nl as nat, + h as nat, + offs_of(labs@, start as int), + ), + cf_flow(*n, nl as nat, h as nat, labs@.len()) == Some(fl_spec(r.0)), + 4 * r.1 == csize(*n, nl as nat, h as nat, labs@.len()), + match r.0 { + Some(x) => x <= 31, + None => true, + }, + decreases *n, 1nat, +{ + let (flow, size) = match analyze(n, nl, h, labs) { + Some(x) => x, + None => { + proof { + assert(false); + } + return (None, 0); + }, + }; + match n { + Node::Then(..) => emit_then(n, nl, h, start, labs, out), + Node::Block(..) => emit_block(n, nl, h, start, labs, out), + Node::Loop(..) => emit_loop(n, nl, h, start, labs, out), + Node::If(..) => emit_if(n, nl, h, start, labs, out), + Node::Br(..) => emit_br(n, nl, h, start, labs, out), + Node::BrIf(..) => emit_br(n, nl, h, start, labs, out), + _ => emit_leaf(n, nl, h, start, labs, out), + } + (flow, size) +} + +/// Encode `code` to bytes: the canonical word of each instruction. +fn encode_code(code: &[isa::Instr]) -> (bytes: Vec) + requires + all_valid_instrs(code@), + ensures + clazel_codecheck::a64::spec::decodes_to(bytes@, code@), +{ + let mut bytes: Vec = Vec::new(); + let mut k: usize = 0; + while k < code.len() + invariant + k <= code.len(), + bytes@.len() == 4 * k, + forall|j: int| + 0 <= j < k ==> #[trigger] bytes@.subrange(4 * j, 4 * j + 4) == isa::word_bytes( + isa::encode(code@[j]), + ), + all_valid_instrs(code@), + decreases code.len() - k, + { + let ins = match code.get(k) { + Some(ins) => ins, + None => { + proof { + assert(false); + } + return bytes; + }, + }; + let w = encode_instr(ins); + let ghost old_bytes = bytes@; + bytes.push((w % 256) as u8); + bytes.push(((w / 0x100) % 256) as u8); + bytes.push(((w / 0x1_0000) % 256) as u8); + bytes.push((w / 0x100_0000) as u8); + proof { + assert(ins == &code@[k as int]); + assert(w as int == isa::encode(code@[k as int])); + assert(w as int / 0x100_0000 < 0x100); + let b = isa::word_bytes(isa::encode(code@[k as int])); + assert(bytes@ =~= old_bytes + b); + assert forall|j: int| 0 <= j < k + 1 implies #[trigger] bytes@.subrange(4 * j, 4 * j + 4) + == isa::word_bytes(isa::encode(code@[j])) by { + if j < k { + assert(bytes@.subrange(4 * j, 4 * j + 4) =~= old_bytes.subrange( + 4 * j, + 4 * j + 4, + )); + } else { + assert(bytes@.subrange(4 * j, 4 * j + 4) =~= b); + } + } + } + k = add_u(k, 1); + } + proof { + assert(clazel_codecheck::a64::spec::decodes_to(bytes@, code@)) by { + assert(bytes@.len() == 4 * code@.len()); + } + } + bytes +} + +/// **Every emitted blob refines its program.** If the emitter answers with +/// `bytes`, the encoded body is [`ctmpl`] of the program with the return label +/// at its end, which refines it. +pub proof fn lemma_emitted_refines(n: Node, nl: usize, exit_code: u16, bytes: Seq) + requires + emit_cf_blob_ensures(n, nl, exit_code, Some(bytes)), + ensures + cf_prog_ok(n, nl as nat), + exists|body: Seq| + #![trigger all_valid_instrs(body)] + all_valid_instrs(body) && cf_refines( + n@, + nl as nat, + 0, + seq![code_len(body) as int], + body, + ) && clazel_codecheck::a64::spec::decodes_to(bytes, body + exit_seq(exit_code)), +{ +} + +/// Emit the body of the function `n` with `nl` locals as encoded bytes, +/// followed by the exit sequence for `exit_code`. The function's return label +/// (a `br` to it, and falling off the end) is the exit sequence. `None` if the +/// program has no translation. +/// +/// **Proven**: [`super::cfcontract::emit_cf_blob_ensures`]. +#[verifier::rlimit(500)] +pub fn emit_cf_blob(n: &Node, nl: usize, exit_code: u16) -> (r: Option>) + ensures + emit_cf_blob_ensures(*n, nl, exit_code, match r { + Some(v) => Some(v@), + None => None, + }), + // **Exactly** the template instructions (`super::cfblobacc::emitted_ins`): + // an addition to the contract, which says only that some body + // refines; the validator-acceptance theorem is about these. + match r { + Some(v) => clazel_codecheck::a64::spec::decodes_to( + v@, + emitted_ins(*n, nl as nat, exit_code), + ), + None => true, + }, + // **The validator accepts it** (J.9): `super::cfblobacc::theorem_blob_accepted`. + emit_cf_blob_accepted_ensures(*n, nl, exit_code, match r { + Some(v) => Some(v@), + None => None, + }), +{ + proof { + super::tlemmas::lemma_abi_consts_t(); + lemma_templated_all(*n); + } + if nl > 31 { + return None; + } + if !codes_ok_exec(n) { + return None; + } + let l1: Vec = push_label(0, &[]); + let size = match analyze(n, nl, 0, l1.as_slice()) { + Some((_, s)) => s, + None => { + return None; + }, + }; + let labs: Vec = push_label(size, &[]); + let mut body: Vec = Vec::new(); + proof { + assert(4 * size == csize(*n, nl as nat, 0, 1)); + } + emit_node(n, nl, 0, 0, labs.as_slice(), &mut body); + let ghost body_seq = body@; + emit_exit(&mut body, exit_code); + proof { + let offs = offs_of(labs@, 0); + assert(offs =~= seq![4 * size as int]); + lemma_ctmpl_len(*n, nl as nat, 0, offs); + assert(labels_fit(offs, 4 * size as nat)); + lemma_ctmpl_refines(*n, nl as nat, 0, offs); + super::tlemmas::lemma_exit_valid(exit_code); + assert(Seq::::empty() + body_seq =~= body_seq); + assert(all_valid_instrs(body@)) by { + assert forall|k: int| 0 <= k < body@.len() implies isa::valid_instr( + #[trigger] body@[k], + ) by { + if k < body_seq.len() { + assert(body@[k] == body_seq[k]); + } else { + assert(body@[k] == exit_seq(exit_code)[k - body_seq.len()]); + } + } + } + } + let bytes = encode_code(body.as_slice()); + proof { + let offs = offs_of(labs@, 0); + assert(offs =~= seq![4 * size as int]); + assert(csize(*n, nl as nat, 0, 1) == 4 * size); + assert(body_seq =~= ctmpl(*n, nl as nat, 0, offs)) by { + assert(Seq::::empty() + ctmpl(*n, nl as nat, 0, offs) =~= ctmpl( + *n, + nl as nat, + 0, + offs, + )); + } + assert(body@ =~= body_seq + exit_seq(exit_code)); + assert(emitted_ins(*n, nl as nat, exit_code) =~= body@); + assert(clazel_codecheck::a64::spec::decodes_to( + bytes@, + emitted_ins(*n, nl as nat, exit_code), + )); + assert(emit_cf_blob_ensures(*n, nl, exit_code, Some(bytes@))); + lemma_emitter_accepted(*n, nl, exit_code, bytes@); + } + Some(bytes) +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfexit.rs b/crates/clazel-jit/src/a64/cfexit.rs new file mode 100644 index 000000000..71a9c69e3 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfexit.rs @@ -0,0 +1,235 @@ +//! `exit` with operands, in the control-flow statement. The code is the exit +//! sequence the first slice proved ([`super::exit::lemma_exit_refines`]); the +//! operands are the top `k` values of the stack, and the exit sequence writes +//! nothing but the exit-code word, so they are still in their frame slots +//! afterwards ([`exit_ops_refines`], [`cf_refines`]). The A64 counterpart of +//! [`crate::cfexit`]. + +#[cfg(verus_only)] +use super::cfalg::*; +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cfleaf::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cfsteps::*; +#[cfg(verus_only)] +use super::contract::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The exit sequence is in two parts: two plain instructions, then `ret`. +pub proof fn lemma_exit_parts(c: u16) + ensures + exit_seq(c) == seq![ + isa::Instr::MoveWide { + sf: isa::Width::W32, + opcode: isa::MoveWideOp::Z, + d: x0(), + imm16: c, + hw: 0, + }, + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: false, + t: x0(), + n: abi::ctx_reg(), + imm12: (abi::exit_code_off() / 8) as u16, + }, + ] + seq![isa::Instr::Ret], + no_jumps( + seq![ + isa::Instr::MoveWide { + sf: isa::Width::W32, + opcode: isa::MoveWideOp::Z, + d: x0(), + imm16: c, + hw: 0, + }, + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: false, + t: x0(), + n: abi::ctx_reg(), + imm12: (abi::exit_code_off() / 8) as u16, + }, + ], + ), +{ + let pre = seq![ + isa::Instr::MoveWide { + sf: isa::Width::W32, + opcode: isa::MoveWideOp::Z, + d: x0(), + imm16: c, + hw: 0, + }, + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: false, + t: x0(), + n: abi::ctx_reg(), + imm12: (abi::exit_code_off() / 8) as u16, + }, + ]; + assert(exit_seq(c) =~= pre + seq![isa::Instr::Ret]); +} + +/// **The run of the exit sequence** from a state related to `a`: it reaches a +/// state that is done ([`exit_done`]), still related to `a`, with nothing +/// outside the exit area changed and the link register and `SP` as they were. +#[verifier::rlimit(200)] +pub proof fn lemma_exit_reach(c: u16, nl: nat, lay: lir::Layout, a: lir::St, m: isa::State) + requires + rel(lay, a, m, nl), + m.pc as int + code_len(exit_seq(c)) < isa::p2(64), + ensures + exists|m2: isa::State| + #![trigger reaches(exit_seq(c), m.pc as int, m, m2)] + reaches(exit_seq(c), m.pc as int, m, m2) && exit_done(m, m2, c as nat) && rel( + lay, + a, + m2, + nl, + ) && preserved(m, m2), +{ + let code = exit_seq(c); + let pre = seq![ + isa::Instr::MoveWide { + sf: isa::Width::W32, + opcode: isa::MoveWideOp::Z, + d: x0(), + imm16: c, + hw: 0, + }, + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: false, + t: x0(), + n: abi::ctx_reg(), + imm12: (abi::exit_code_off() / 8) as u16, + }, + ]; + lemma_exit_parts(c); + super::exit::lemma_exit_refines(c, nl); + assert(a.with_stack(lir::below(a.stack, 0)) == a) by { + assert(lir::below(a.stack, 0) =~= a.stack); + } + assert(lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, 1) == lir::Outcome::Exit( + c as nat, + lir::top(a.stack, 0), + a.with_stack(lir::below(a.stack, 0)), + )); + let m2 = run_seq(m, code).unwrap(); + super::runseq::lemma_run_seq_append(m, pre, seq![isa::Instr::Ret]); + let m1 = run_seq(m, pre).unwrap(); + super::runseq::lemma_run_seq_one(m1, isa::Instr::Ret); + assert(code_len(pre) == 8); + lemma_reaches_plain_then(pre, isa::Instr::Ret, m, m1, m2); + assert(pre + seq![isa::Instr::Ret] =~= code); +} + +/// **`exit c` with `k` operands.** The code is the exit sequence for `c`. +#[verifier::rlimit(400)] +pub proof fn lemma_exit_cf(c: u32, k: usize, nl: nat, h: nat, offs: Seq) + requires + c < 0x1_0000, + k <= h, + nl + h <= max_slots(), + ensures + cf_refines(Node::Exit(c, k)@, nl, h, offs, exit_seq(c as u16)), +{ + super::memlemmas::lemma_p2_consts(); + let c16 = c as u16; + assert(c16 as nat == c as nat); + let code = exit_seq(c16); + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, m, lir::eval(lay, Node::Exit(c, k)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && cf_canon( + m.pc as int, + offs, + code, + ) implies cf_post( + lay, + base, + nl, + offs, + code, + m, + lir::eval(lay, Node::Exit(c, k)@, a, fuel), + ) by { + if fuel > 0 && a.stack.len() >= k { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + let n = a.stack.len(); + assert(code_len(code) == 12); + lemma_exit_reach(c16, nl, lay, ac, m); + let m2 = choose|m2: isa::State| + reaches(code, m.pc as int, m, m2) && exit_done(m, m2, c16 as nat) && rel( + lay, + ac, + m2, + nl, + ) && preserved(m, m2); + // the outcome + let vals = lir::top(a.stack, k as nat); + let s = a.with_stack(lir::below(a.stack, k as nat)); + assert(lir::eval(lay, Node::Exit(c, k)@, a, fuel) == lir::Outcome::Exit( + c as nat, + vals, + s, + )); + // the relation, below the operands + assert((base + a.stack).take((base + s.stack).len() as int) =~= base + s.stack); + lemma_rel_prefix(lay, a, base + a.stack, base + s.stack, m2, nl); + assert(preserved_mem_r(lay, m, m2)) by { + assert(ctx_of(m2) == ctx_of(m)) by { + super::steps::lemma_abi_consts(); + } + } + lemma_table_pres(lay, m, m2); + assert(s.with_stack(base + s.stack) == a.with_stack(base + s.stack)); + reveal(rel_base); + assert(rel_base(lay, base + Seq::::empty(), s, m2, nl)) by { + assert(base + Seq::::empty() =~= base); + } + // the operands are still in their slots + assert forall|i: int| 0 <= i < vals.len() implies #[trigger] slot_holds( + m2.mem, + ctx_of(m), + (nl + (base + Seq::::empty()).len() + s.stack.len() + i) as nat, + lir::val_bits(vals[i]), + ) by { + let jj = (nl + base.len() + (n - k) + i) as nat; + assert(vals[i] == a.stack[(n - k) as int + i]); + assert((base + a.stack)[(base.len() + n - k) as int + i] == a.stack[(n - k) as int + + i]); + assert(ctx_of(m2) == ctx_of(m)); + assert(slot_val(ac, nl, jj) == vals[i]); + } + assert(ops_in_frame( + m2.mem, + ctx_of(m), + nl, + (base + Seq::::empty()).len() + s.stack.len(), + vals, + )); + assert(exit_rel(lay, base, s, m2, nl, ctx_of(m), vals)); + assert(reaches(code, m.pc as int, m, m2) && exit_done(m, m2, c as nat)); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cffit.rs b/crates/clazel-jit/src/a64/cffit.rs new file mode 100644 index 000000000..4f7b178d2 --- /dev/null +++ b/crates/clazel-jit/src/a64/cffit.rs @@ -0,0 +1,78 @@ +//! **The size invariant** every displacement fits. A conditional branch +//! (`b.cond`, `cbz`, `cbnz`) has a 19-bit signed displacement in words, so it +//! reaches at most `2^20` bytes either way; `b` reaches `2^27`. [`labels_fit`] +//! says every label is far enough from every place in code of `size` bytes +//! that a conditional branch there reaches it; and a multiple of 4. The +//! invariant is closed under taking a piece of the code ([`lemma_fit_shift`]), +//! adding a label in front ([`lemma_fit_cons`]) and shrinking the size. + +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every label offset is a multiple of 4 and far enough from every byte of code +/// of `size` bytes that a 19-bit-word displacement reaches it: from a branch at +/// byte `p` with `0 <= p < size`, `-2^20 <= o - p < 2^20`. +pub open spec fn labels_fit(offs: Seq, size: nat) -> bool { + forall|l: int| + 0 <= l < offs.len() ==> #[trigger] offs[l] % 4 == 0 && -0x10_0000 + size + 8 <= offs[l] + && offs[l] <= 0xF_FFF0 +} + +/// Labels seen from code that starts `d` bytes in (`d` a multiple of 4), for a +/// smaller piece. +pub proof fn lemma_fit_shift(offs: Seq, d: nat, size: nat, size2: nat) + requires + labels_fit(offs, size), + d % 4 == 0, + d + size2 <= size, + ensures + labels_fit(shift(offs, d as int), size2), +{ + assert forall|l: int| 0 <= l < shift(offs, d as int).len() implies #[trigger] shift( + offs, + d as int, + )[l] % 4 == 0 && -0x10_0000 + size2 + 8 <= shift(offs, d as int)[l] && shift( + offs, + d as int, + )[l] <= 0xF_FFF0 by { + super::cfcompose::lemma_shift_index(offs, d as int, l); + } +} + +/// A new label in front. +pub proof fn lemma_fit_cons(x: int, offs: Seq, size: nat) + requires + labels_fit(offs, size), + x % 4 == 0, + -0x10_0000 + size + 8 <= x <= 0xF_FFF0, + ensures + labels_fit(seq![x] + offs, size), +{ + let all = seq![x] + offs; + assert forall|l: int| 0 <= l < all.len() implies #[trigger] all[l] % 4 == 0 && -0x10_0000 + + size + 8 <= all[l] && all[l] <= 0xF_FFF0 by { + if l > 0 { + assert(all[l] == offs[l - 1]); + } + } +} + +/// `labels_fit` for a smaller size. +pub proof fn lemma_fit_mono(offs: Seq, size: nat, size2: nat) + requires + labels_fit(offs, size), + size2 <= size, + ensures + labels_fit(offs, size2), +{ +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfif.rs b/crates/clazel-jit/src/a64/cfif.rs new file mode 100644 index 000000000..8ded2e84c --- /dev/null +++ b/crates/clazel-jit/src/a64/cfif.rs @@ -0,0 +1,863 @@ +//! `if`: the condition test, a `je` over the first arm, the arms, and a `jmp` +//! over the second. LIR evaluates `if` as a void `block` around the arm the +//! condition picks, run with the condition popped, so the arm's label 0 is the +//! end of the whole `if`. + +#[cfg(verus_only)] +use super::cfalg::*; +#[cfg(verus_only)] +use super::cfblock::*; +#[cfg(verus_only)] +use super::cfcompose::*; +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cfleaf::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cfsteps::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::contract::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[cfg(verus_only)] +use crate::cfheight::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The length of the condition test. +pub open spec fn if_p(nl: nat, h: nat) -> int { + cond_len(nl, h) as int +} + +/// The code of an `if` whose arms have the code `ct` and `ce`. +pub open spec fn if_code(nl: nat, h: nat, ct: Seq, ce: Seq) -> Seq< + isa::Instr, +> { + cond_pre(nl, h, true, code_len(ct) as int + 8) + ct + seq![b_i(code_len(ce) as int + 4)] + ce +} + +/// The labels in scope in the first arm: its own (the end of the `if`), then +/// the outer ones. +pub open spec fn if_offs_t( + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, +) -> Seq { + seq![code_len(ct) as int + 4 + code_len(ce) as int] + shift(offs, if_p(nl, h)) +} + +/// The labels in scope in the second arm. +pub open spec fn if_offs_e( + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, +) -> Seq { + seq![code_len(ce) as int] + shift(offs, if_p(nl, h) + code_len(ct) as int + 4) +} + +/// A `b` in the middle of `pre + [b] + post`, reached at the end of `pre`, is +/// one step. +pub proof fn lemma_reach_b( + pre: Seq, + post: Seq, + m: isa::State, + m4: isa::State, + rel: int, +) + requires + m4.pc as int == m.pc as int + code_len(pre), + rel % 4 == 0, + -0x800_0000 <= rel < 0x800_0000, + 0 <= m4.pc as int + rel < isa::p2(64), + ensures + reaches( + pre + seq![b_i(rel)] + post, + m.pc as int, + m4, + isa::State { pc: (m4.pc as int + rel) as u64, ..m4 }, + ), +{ + super::memlemmas::lemma_p2_consts(); + let whole = pre + seq![b_i(rel)] + post; + let m5 = isa::State { pc: (m4.pc as int + rel) as u64, ..m4 }; + assert(whole =~= pre + (seq![b_i(rel)] + post)); + lemma_fetch_append_r(pre, seq![b_i(rel)] + post, code_len(pre) as int); + assert(code_len(pre) as int - code_len(pre) == 0); + assert(fetch(seq![b_i(rel)] + post, 0) == Some(b_i(rel))); + assert(m4.pc as int - m.pc as int == code_len(pre)); + lemma_step_b(m4, rel); + lemma_steps_one(whole, m.pc as int, m4, b_i(rel), m5); +} + +/// The length of the condition test. +pub proof fn lemma_cond_pre_len(nl: nat, h: nat, iszero: bool, rel: int) + ensures + code_len(cond_pre(nl, h, iszero, rel)) == 8, +{ +} + +/// Where the arm starts, from the `if`'s first byte. +pub open spec fn if_q(is_then: bool, nl: nat, h: nat, ct: Seq) -> int { + if is_then { + if_p(nl, h) + } else { + if_p(nl, h) + code_len(ct) as int + 4 + } +} + +/// The arm's code. +pub open spec fn if_cx(is_then: bool, ct: Seq, ce: Seq) -> Seq { + if is_then { + ct + } else { + ce + } +} + +/// The labels in scope in the arm. +pub open spec fn if_offs_x( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, +) -> Seq { + if is_then { + if_offs_t(nl, h, offs, ct, ce) + } else { + if_offs_e(nl, h, offs, ct, ce) + } +} + +/// The code before the arm. +pub open spec fn if_pre( + is_then: bool, + nl: nat, + h: nat, + ct: Seq, + ce: Seq, +) -> Seq { + let cp = cond_pre(nl, h, true, code_len(ct) as int + 8); + if is_then { + cp + } else { + cp + ct + seq![b_i(code_len(ce) as int + 4)] + } +} + +/// The code after the arm. +pub open spec fn if_post( + is_then: bool, + nl: nat, + h: nat, + ct: Seq, + ce: Seq, +) -> Seq { + if is_then { + seq![b_i(code_len(ce) as int + 4)] + ce + } else { + Seq::empty() + } +} + +/// The whole code is the code before the arm, the arm, and the code after. +pub proof fn lemma_if_split(is_then: bool, nl: nat, h: nat, ct: Seq, ce: Seq) + ensures + if_pre(is_then, nl, h, ct, ce) + if_cx(is_then, ct, ce) + if_post(is_then, nl, h, ct, ce) + == if_code(nl, h, ct, ce), + code_len(if_pre(is_then, nl, h, ct, ce)) == if_q(is_then, nl, h, ct), + code_len(if_code(nl, h, ct, ce)) == if_p(nl, h) + code_len(ct) + 4 + code_len(ce), + if_code(nl, h, ct, ce) == cond_pre(nl, h, true, code_len(ct) as int + 8) + (ct + seq![b_i( + code_len(ce) as int + 4, + )] + ce), + is_then ==> if_pre(is_then, nl, h, ct, ce) + if_cx(is_then, ct, ce) == cond_pre( + nl, + h, + true, + code_len(ct) as int + 8, + ) + ct, + !is_then ==> if_pre(is_then, nl, h, ct, ce) + if_cx(is_then, ct, ce) == if_code( + nl, + h, + ct, + ce, + ), +{ + let cp = cond_pre(nl, h, true, code_len(ct) as int + 8); + let jm = b_i(code_len(ce) as int + 4); + lemma_cond_pre_len(nl, h, true, code_len(ct) as int + 8); + super::runseq::lemma_code_len_append(cp, ct); + super::runseq::lemma_code_len_append(cp + ct, seq![jm]); + super::runseq::lemma_code_len_append(cp + ct + seq![jm], ce); + assert(Seq::::empty() + ce =~= ce); + assert(cp + ct + seq![jm] + ce =~= cp + (ct + seq![jm] + ce)); + if is_then { + assert(cp + ct + (seq![jm] + ce) =~= cp + ct + seq![jm] + ce); + } else { + assert(cp + ct + seq![jm] + ce + Seq::::empty() =~= cp + ct + seq![jm] + ce); + } +} + +/// **What running the condition test and the `je` does**, for `if`: the machine +/// is at the start of the arm the condition picks, related to the `if`'s state +/// with the condition popped, and the arm's statement can be used there. +pub open spec fn if_head( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + m3: isa::State, +) -> bool { + let a0 = a.with_stack(lir::below(a.stack, 1)); + let cx = if_cx(is_then, ct, ce); + let q = if_q(is_then, nl, h, ct); + &&& m3.pc as int == m.pc as int + q + &&& reaches(cond_pre(nl, h, true, code_len(ct) as int + 8), m.pc as int, m, m3) + &&& rel_base(lay, base + a0.stack, fresh(a0), m3, nl) + &&& preserved_r(lay, m, m3) + &&& cf_canon(m3.pc as int, if_offs_x(is_then, nl, h, offs, ct, ce), cx) +} + +/// **The head of an `if`**: the condition test and the `cbz` take the machine +/// to the start of the arm ([`if_head`]). +#[verifier::rlimit(400)] +pub proof fn lemma_if_head( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + c: u32, +) + requires + 1 <= h, + nl + h <= max_slots(), + code_len(ct) < 0x8_0000, + code_len(ce) < 0x8_0000, + base.len() + a.stack.len() == h, + a.stack.len() >= 1, + a.stack[(a.stack.len() - 1) as int] == lir::Val::I32(c), + (c != 0) == is_then, + rel_base(lay, base, a, m, nl), + cf_canon(m.pc as int, offs, if_code(nl, h, ct, ce)), + ensures + exists|m3: isa::State| if_head(is_then, nl, h, offs, ct, ce, lay, base, a, m, m3), +{ + super::steps::lemma_abi_consts(); + super::memlemmas::lemma_p2_consts(); + lemma_if_split(is_then, nl, h, ct, ce); + let p = if_p(nl, h); + let st = code_len(ct) as int; + let se = code_len(ce) as int; + let cp = cond_pre(nl, h, true, st + 8); + let whole = if_code(nl, h, ct, ce); + let ee = p + st + 4 + se; + let j = (nl + h - 1) as nat; + let i1 = load_slot(x0(), j); + let i3 = cb_i(true, st + 8); + assert(p == 8); + assert(cp == seq![i1, i3]); + assert(code_len(whole) == ee); + assert(cf_canon(m.pc as int, offs, whole)); + assert(m.pc as int + ee < isa::p2(64)); + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + assert(ac.stack[(h - 1) as int] == lir::Val::I32(c)); + assert(slot_val(ac, nl, j) == lir::Val::I32(c)); + assert(j < nl + ac.stack.len()); + super::rel::lemma_load_rel(lay, ac, m, x0(), j, nl); + let m1 = isa::step(m, i1).unwrap(); + assert(m1.r[0] == c as u64); + assert(m1.pc as int == m.pc as int + 4); + assert(isa::trunc(32, c as int) == c) by { + vstd::arithmetic::div_mod::lemma_small_mod(c as nat, 0x1_0000_0000nat); + } + lemma_step_cb(m1, true, st + 8); + let m3 = isa::step(m1, i3).unwrap(); + super::runseq::run1(m, i1, m1); + assert(no_jumps(seq![i1])); + lemma_reaches_plain_then(seq![i1], i3, m, m1, m3); + assert(cp == seq![i1] + seq![i3]); + let q = if_q(is_then, nl, h, ct); + assert(m3.pc as int == m.pc as int + q); + assert(reaches(cp, m.pc as int, m, m3)); + // relation at m3 + assert(m3.r == m1.r && m3.mem == m1.mem && m3.vreg == m1.vreg); + super::rel::lemma_rel_regs(lay, ac, m1, m3, nl); + let a0 = a.with_stack(lir::below(a.stack, 1)); + assert((base + a.stack).take((base + a0.stack).len() as int) =~= base + a0.stack); + lemma_rel_prefix(lay, a, base + a.stack, base + a0.stack, m3, nl); + lemma_preserved_same(lay, m, m3); + lemma_table_pres(lay, m, m3); + assert(a0.with_stack(base + a0.stack) == a.with_stack(base + a0.stack)); + reveal(rel_base); + assert(rel_base(lay, base, a0, m3, nl)); + lemma_rel_base_enter(lay, base, a0, m3, nl); + // the arm's labels are canonical + let cx = if_cx(is_then, ct, ce); + let offs_x = if_offs_x(is_then, nl, h, offs, ct, ce); + assert(code_len(cx) <= (if is_then { st } else { se })); + assert forall|l: int| 0 <= l < offs_x.len() implies 0 <= m3.pc as int + #[trigger] offs_x[l] + && m3.pc as int + offs_x[l] < isa::p2(64) && offs_x[l] % 4 == 0 by { + if l == 0 { + assert(offs_x[0] == ee - q); + } else { + lemma_shift_index(offs, q, l - 1); + assert(offs_x[l] == offs[l - 1] - q); + assert(0 <= m.pc as int + offs[l - 1]); + } + } + assert(cf_canon(m3.pc as int, offs_x, cx)); + assert(if_head(is_then, nl, h, offs, ct, ce, lay, base, a, m, m3)); +} + + +/// The condition test's run, in the whole `if`'s code. +pub proof fn lemma_head_reach_whole( + nl: nat, + h: nat, + ct: Seq, + ce: Seq, + m: isa::State, + m3: isa::State, +) + requires + reaches(cond_pre(nl, h, true, code_len(ct) as int + 8), m.pc as int, m, m3), + ensures + reaches(if_code(nl, h, ct, ce), m.pc as int, m, m3), +{ + lemma_if_split(true, nl, h, ct, ce); + lemma_reaches_post( + cond_pre(nl, h, true, code_len(ct) as int + 8), + ct + seq![b_i(code_len(ce) as int + 4)] + ce, + m.pc as int, + m, + m3, + ); +} + +/// The arm's reach, embedded in the whole `if`'s code, from the `if`'s start. +pub proof fn lemma_if_arm_reach( + is_then: bool, + nl: nat, + h: nat, + ct: Seq, + ce: Seq, + m: isa::State, + m3: isa::State, + m4: isa::State, +) + requires + m3.pc as int == m.pc as int + if_q(is_then, nl, h, ct), + reaches(if_cx(is_then, ct, ce), m3.pc as int, m3, m4), + ensures + reaches(if_code(nl, h, ct, ce), m.pc as int, m3, m4), +{ + lemma_if_split(is_then, nl, h, ct, ce); + let pre = if_pre(is_then, nl, h, ct, ce); + let post = if_post(is_then, nl, h, ct, ce); + let cx = if_cx(is_then, ct, ce); + lemma_reaches_embed(pre, cx, post, m3.pc as int, m3, m4); + assert(m3.pc as int - code_len(pre) == m.pc as int); +} + +/// **The arm's run to its end**: from the start of the arm, its code reaches +/// the end of the arm, related to the arm's final state on top of the `if`'s +/// own stack. +#[verifier::rlimit(100)] +pub proof fn lemma_if_arm_end( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + m3: isa::State, + s: lir::St, +) + requires + if_head(is_then, nl, h, offs, ct, ce, lay, base, a, m, m3), + cf_post( + lay, + base + a.with_stack(lir::below(a.stack, 1)).stack, + nl, + if_offs_x(is_then, nl, h, offs, ct, ce), + if_cx(is_then, ct, ce), + m3, + lir::Outcome::Normal(s), + ), + ensures + exists|m4: isa::State| + #![trigger reaches(if_code(nl, h, ct, ce), m.pc as int, m, m4)] + reaches(if_code(nl, h, ct, ce), m.pc as int, m, m4) && m4.pc as int == m.pc as int + if_q( + is_then, + nl, + h, + ct, + ) + code_len(if_cx(is_then, ct, ce)) && rel_base( + lay, + base, + s.with_stack(a.with_stack(lir::below(a.stack, 1)).stack + s.stack), + m4, + nl, + ) && preserved_r(lay, m, m4), +{ + lemma_if_split(is_then, nl, h, ct, ce); + let a0 = a.with_stack(lir::below(a.stack, 1)); + let cx = if_cx(is_then, ct, ce); + let m4 = choose|m4: isa::State| + reaches(cx, m3.pc as int, m3, m4) && m4.pc as int == m3.pc as int + code_len(cx) && rel_base( + lay, + base + a0.stack, + s, + m4, + nl, + ) && preserved_r(lay, m3, m4); + lemma_head_reach_whole(nl, h, ct, ce, m, m3); + lemma_if_arm_reach(is_then, nl, h, ct, ce, m, m3, m4); + lemma_reaches_trans(if_code(nl, h, ct, ce), m.pc as int, m, m3, m4); + lemma_pr_trans(lay, m, m3, m4); + lemma_rel_base_cat(lay, base, a0.stack, s, m4, nl); +} + +/// **Running a `b` that follows code already run**: the machine at the end of +/// `pre`, in `pre + [b] + post`, takes the `b`, and stays related. +#[verifier::rlimit(100)] +pub proof fn lemma_b_finish( + lay: lir::Layout, + base: Seq, + nl: nat, + pre: Seq, + post: Seq, + m: isa::State, + m4: isa::State, + sn: lir::St, + se: int, +) + requires + reaches(pre + seq![b_i(se + 4)] + post, m.pc as int, m, m4), + m4.pc as int == m.pc as int + code_len(pre), + se % 4 == 0, + 0 <= se < 0x8_0000, + 0 <= m4.pc as int + se + 4 < isa::p2(64), + rel_base(lay, base, sn, m4, nl), + preserved_r(lay, m, m4), + ensures + exists|m5: isa::State| + #![trigger reaches(pre + seq![b_i(se + 4)] + post, m.pc as int, m, m5)] + reaches(pre + seq![b_i(se + 4)] + post, m.pc as int, m, m5) && m5.pc as int + == m4.pc as int + se + 4 && rel_base(lay, base, sn, m5, nl) && preserved_r( + lay, + m, + m5, + ), +{ + super::memlemmas::lemma_p2_consts(); + lemma_rel_base_unfold(lay, base, sn, m4, nl); + lemma_reach_b(pre, post, m, m4, se + 4); + let m5 = isa::State { pc: (m4.pc as int + se + 4) as u64, ..m4 }; + assert(m5.pc as int == m4.pc as int + se + 4); + lemma_reaches_trans(pre + seq![b_i(se + 4)] + post, m.pc as int, m, m4, m5); + lemma_rel_base_same(lay, base, sn, m4, m5, nl); + lemma_preserved_same(lay, m4, m5); + lemma_pr_trans(lay, m, m4, m5); +} + +/// **The arm falls through.** For the first arm there is the `jmp` over the +/// second still to run. +#[verifier::rlimit(100)] +pub proof fn lemma_if_normal( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + m3: isa::State, + s: lir::St, +) + requires + code_len(ct) < 0x8_0000, + code_len(ce) < 0x8_0000, + cf_canon(m.pc as int, offs, if_code(nl, h, ct, ce)), + if_head(is_then, nl, h, offs, ct, ce, lay, base, a, m, m3), + cf_post( + lay, + base + a.with_stack(lir::below(a.stack, 1)).stack, + nl, + if_offs_x(is_then, nl, h, offs, ct, ce), + if_cx(is_then, ct, ce), + m3, + lir::Outcome::Normal(s), + ), + ensures + cf_post( + lay, + base, + nl, + offs, + if_code(nl, h, ct, ce), + m, + lir::Outcome::Normal( + s.with_stack(a.with_stack(lir::below(a.stack, 1)).stack + s.stack), + ), + ), +{ + super::memlemmas::lemma_p2_consts(); + lemma_if_split(is_then, nl, h, ct, ce); + lemma_if_arm_end(is_then, nl, h, offs, ct, ce, lay, base, a, m, m3, s); + let p = if_p(nl, h); + let st = code_len(ct) as int; + let se = code_len(ce) as int; + let whole = if_code(nl, h, ct, ce); + let ee = p + st + 4 + se; + let a0 = a.with_stack(lir::below(a.stack, 1)); + let sn = s.with_stack(a0.stack + s.stack); + let m4 = choose|m4: isa::State| + reaches(whole, m.pc as int, m, m4) && m4.pc as int == m.pc as int + if_q(is_then, nl, h, ct) + code_len( + if_cx(is_then, ct, ce), + ) && rel_base(lay, base, sn, m4, nl) && preserved_r(lay, m, m4); + if is_then { + let cp = cond_pre(nl, h, true, st + 8); + assert(m4.pc as int == m.pc as int + p + st); + assert(cf_canon(m.pc as int, offs, whole)); + assert(code_len(whole) == ee); + assert(m.pc as int + ee < isa::p2(64)); + assert(st % 4 == 0 && se % 4 == 0); + assert(code_len(cp + ct) == p + st) by { + lemma_cond_pre_len(nl, h, true, st + 8); + super::runseq::lemma_code_len_append(cp, ct); + } + assert(whole == (cp + ct) + seq![b_i(se + 4)] + ce); + lemma_b_finish(lay, base, nl, cp + ct, ce, m, m4, sn, se); + } else { + assert(m4.pc as int == m.pc as int + ee); + } +} + +/// **A branch to the arm's own label** is the `if` ending. +pub proof fn lemma_if_br0( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + m3: isa::State, + s: lir::St, +) + requires + code_len(ct) < 0x8_0000, + code_len(ce) < 0x8_0000, + if_head(is_then, nl, h, offs, ct, ce, lay, base, a, m, m3), + cf_post( + lay, + base + a.with_stack(lir::below(a.stack, 1)).stack, + nl, + if_offs_x(is_then, nl, h, offs, ct, ce), + if_cx(is_then, ct, ce), + m3, + lir::Outcome::Branch(0, s), + ), + ensures + cf_post( + lay, + base, + nl, + offs, + if_code(nl, h, ct, ce), + m, + lir::Outcome::Normal(s.with_stack(a.with_stack(lir::below(a.stack, 1)).stack)), + ), +{ + lemma_if_split(is_then, nl, h, ct, ce); + let a0 = a.with_stack(lir::below(a.stack, 1)); + let cx = if_cx(is_then, ct, ce); + let offs_x = if_offs_x(is_then, nl, h, offs, ct, ce); + let q = if_q(is_then, nl, h, ct); + let ee = if_p(nl, h) + code_len(ct) as int + 4 + code_len(ce) as int; + let m4 = choose|m4: isa::State| + reaches(cx, m3.pc as int, m3, m4) && m4.pc as int == m3.pc as int + offs_x[0] && rel_ext( + lay, + base + a0.stack, + s, + m4, + nl, + ) && preserved_r(lay, m3, m4); + lemma_head_reach_whole(nl, h, ct, ce, m, m3); + lemma_if_arm_reach(is_then, nl, h, ct, ce, m, m3, m4); + lemma_reaches_trans(if_code(nl, h, ct, ce), m.pc as int, m, m3, m4); + lemma_pr_trans(lay, m, m3, m4); + lemma_br0_rel(lay, base, a0.stack, s, m4, nl); + assert(offs_x[0] == ee - q); + assert(m4.pc as int == m.pc as int + ee); +} + +/// **The arm branches past the `if`, or exits**: the outcome is the `if`'s, +/// seen from the start of the code. +pub proof fn lemma_if_out( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + m3: isa::State, + o: lir::Outcome, + o2: lir::Outcome, +) + requires + code_len(ct) < 0x8_0000, + code_len(ce) < 0x8_0000, + !(o2 is Normal), + if_head(is_then, nl, h, offs, ct, ce, lay, base, a, m, m3), + cf_post( + lay, + base, + nl, + shift(offs, if_q(is_then, nl, h, ct)), + if_cx(is_then, ct, ce), + m3, + o2, + ), + ensures + cf_post(lay, base, nl, offs, if_code(nl, h, ct, ce), m, o2), +{ + lemma_if_split(is_then, nl, h, ct, ce); + let cx = if_cx(is_then, ct, ce); + let q = if_q(is_then, nl, h, ct); + let pre = if_pre(is_then, nl, h, ct, ce); + let post = if_post(is_then, nl, h, ct, ce); + let whole = if_code(nl, h, ct, ce); + assert forall|l: int| 0 <= l < offs.len() implies #[trigger] shift(offs, q)[l] + code_len(pre) + == offs[l] by { + lemma_shift_index(offs, q, l); + } + // the run from m to m3, inside pre + cx + let cp = cond_pre(nl, h, true, code_len(ct) as int + 8); + assert(reaches(cp, m.pc as int, m, m3)); + if is_then { + lemma_reaches_post(cp, ct, m.pc as int, m, m3); + assert(pre + cx == cp + ct); + } else { + lemma_reaches_post(cp, ct + seq![b_i(code_len(ce) as int + 4)] + ce, m.pc as int, m, m3); + assert(pre + cx == whole); + assert(whole == cp + (ct + seq![b_i(code_len(ce) as int + 4)] + ce)); + } + assert(reaches(pre + cx, m.pc as int, m, m3)); + lemma_lift_pre(lay, base, nl, offs, shift(offs, q), pre, cx, m, m3, o2); + lemma_lift_post(lay, base, nl, offs, pre + cx, post, m, o2); +} + +/// **`if`.** If the arms' code refines the arms, with the labels as the `if` +/// puts them (the `if`'s own label, at its end, in front), the `if`'s code +/// refines the `if`. +#[verifier::rlimit(150)] +pub proof fn lemma_if( + t: Node, + e: Node, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, +) + requires + 1 <= h, + nl + h <= max_slots(), + code_len(ct) < 0x8_0000, + code_len(ce) < 0x8_0000, + cf_refines(t@, nl, (h - 1) as nat, if_offs_t(nl, h, offs, ct, ce), ct), + cf_refines(e@, nl, (h - 1) as nat, if_offs_e(nl, h, offs, ct, ce), ce), + ensures + cf_refines( + Node::If(Box::new(t), Box::new(e))@, + nl, + h, + offs, + if_code(nl, h, ct, ce), + ), +{ + let node = Node::If(Box::new(t), Box::new(e)); + let whole = if_code(nl, h, ct, ce); + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, whole, m, lir::eval(lay, node@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) + && cf_canon(m.pc as int, offs, whole) implies cf_post( + lay, + base, + nl, + offs, + whole, + m, + lir::eval(lay, node@, a, fuel), + ) by { + if fuel > 0 { + let f = (fuel - 1) as nat; + let n = a.stack.len(); + if n >= 1 { + match a.stack[(n - 1) as int] { + lir::Val::I32(c) => { + let is_then = c != 0; + let arm = if is_then { + t + } else { + e + }; + let a0 = a.with_stack(lir::below(a.stack, 1)); + assert(f > 0 || lir::eval(lay, lir::Instr::Block(void_bt(), Box::new(arm@)), a0, f) + == lir::Outcome::OutOfFuel); + if f > 0 { + lemma_rel_base_unfold(lay, base, a, m, nl); + lemma_if_head(is_then, nl, h, offs, ct, ce, lay, base, a, m, c); + let m3 = choose|m3: isa::State| + if_head(is_then, nl, h, offs, ct, ce, lay, base, a, m, m3); + lemma_eval_block(lay, arm, a0, f); + let g = (f - 1) as nat; + let cx = if_cx(is_then, ct, ce); + let offs_x = if_offs_x(is_then, nl, h, offs, ct, ce); + let q = if_q(is_then, nl, h, ct); + assert((base + a0.stack).len() + fresh(a0).stack.len() == (h - 1) as nat); + lemma_use_cf( + arm@, + nl, + (h - 1) as nat, + offs_x, + cx, + lay, + base + a0.stack, + fresh(a0), + m3, + g, + ); + let o = lir::eval(lay, arm@, fresh(a0), g); + match o { + lir::Outcome::Normal(s) => { + lemma_if_normal(is_then, nl, h, offs, ct, ce, lay, base, a, m, m3, s); + }, + lir::Outcome::Branch(k, s) => { + if k == 0 { + assert(lir::top(s.stack, 0) =~= Seq::::empty()); + assert(a0.stack + lir::top(s.stack, 0) =~= a0.stack); + lemma_if_br0(is_then, nl, h, offs, ct, ce, lay, base, a, m, m3, s); + } else { + assert(k < offs_x.len()); + assert(((k - 1) as nat) < shift(offs, q).len()); + assert(offs_x[k as int] == shift(offs, q)[((k - 1) as nat) as int]); + lemma_blk_brn( + lay, + base, + a0.stack, + nl, + shift(offs, q), + cx, + m3, + k, + s, + offs_x, + ); + lemma_if_out( + is_then, + nl, + h, + offs, + ct, + ce, + lay, + base, + a, + m, + m3, + o, + lir::Outcome::Branch((k - 1) as nat, s), + ); + } + }, + lir::Outcome::Exit(code, vals, s) => { + lemma_blk_exit( + lay, + base, + a0.stack, + nl, + shift(offs, q), + cx, + m3, + code, + vals, + s, + offs_x, + ); + lemma_if_out( + is_then, + nl, + h, + offs, + ct, + ce, + lay, + base, + a, + m, + m3, + o, + lir::Outcome::Exit(code, vals, s), + ); + }, + _ => {}, + } + } + }, + _ => {}, + } + } + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfjt.rs b/crates/clazel-jit/src/a64/cfjt.rs new file mode 100644 index 000000000..e54124e43 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfjt.rs @@ -0,0 +1,844 @@ +//! Where the branches of the A64 templates land (jit.md item J.9, "every +//! emitted blob is accepted"): every branch target of a node's code is one of +//! the labels it was given or one of [`bs`]. The counterpart of +//! [`crate::cfjt`]; a branch is relative to its own address. +//! +//! `jt c x` says that some branch of `c` lands on byte `x` of `c`, computed +//! as the validator does ([`clazel_codecheck::a64::spec::jump_targets`]: the +//! address of the branch plus its displacement). + +#[cfg(verus_only)] +use super::cfaccept::*; +#[cfg(verus_only)] +use super::cfbs::*; +#[cfg(verus_only)] +use super::cfcompose::lemma_shift_index; +#[cfg(verus_only)] +use super::cffit::{labels_fit, lemma_fit_cons, lemma_fit_mono, lemma_fit_shift}; +#[cfg(verus_only)] +use super::cflens::*; +#[cfg(verus_only)] +use super::cfsimple::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::cfunits::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Some branch of `c` lands on byte `x` of `c`. +pub open spec fn jt(c: Seq, x: int) -> bool { + exists|j: int| + 0 <= j < c.len() && #[trigger] clazel_codecheck::a64::spec::jump_rel(c[j]) is Some && x + == clazel_codecheck::a64::spec::start_of(c, j) + + clazel_codecheck::a64::spec::jump_rel(c[j])->Some_0 +} + +/// `x` is one of the labels. +pub open spec fn inoffs(offs: Seq, x: int) -> bool { + exists|l: int| 0 <= l < offs.len() && #[trigger] offs[l] == x +} + +// ---------------------------------------------------------------------------- +// Positions in a list of instructions +// ---------------------------------------------------------------------------- + +/// A branch of a concatenation is a branch of a part. +pub proof fn lemma_jt_cat(a: Seq, b: Seq, x: int) + requires + jt(a + b, x), + ensures + jt(a, x) || jt(b, x - code_len(a)), +{ + let j = choose|j: int| + 0 <= j < (a + b).len() && #[trigger] clazel_codecheck::a64::spec::jump_rel((a + b)[j]) is Some + && x == clazel_codecheck::a64::spec::start_of(a + b, j) + + clazel_codecheck::a64::spec::jump_rel((a + b)[j])->Some_0; + if j < a.len() { + assert((a + b)[j] == a[j]); + assert(jt(a, x)); + } else { + let k = j - a.len(); + assert((a + b)[j] == b[k]); + assert(clazel_codecheck::a64::spec::jump_rel(b[k]) is Some); + assert(jt(b, x - code_len(a))); + } +} + +/// Code with no branch has no branch target. +pub proof fn lemma_jt_none(c: Seq, x: int) + requires + forall|k: int| 0 <= k < c.len() ==> nojump(#[trigger] c[k]), + ensures + !jt(c, x), +{ +} + +/// The only branch is the last instruction. +pub proof fn lemma_jt_last(c: Seq, x: int) + requires + c.len() > 0, + forall|k: int| 0 <= k < c.len() - 1 ==> nojump(#[trigger] c[k]), + clazel_codecheck::a64::spec::jump_rel(c[c.len() - 1]) is Some, + jt(c, x), + ensures + x == code_len(c) - 4 + clazel_codecheck::a64::spec::jump_rel(c[c.len() - 1])->Some_0, +{ + let j = choose|j: int| + 0 <= j < c.len() && #[trigger] clazel_codecheck::a64::spec::jump_rel(c[j]) is Some && x + == clazel_codecheck::a64::spec::start_of(c, j) + + clazel_codecheck::a64::spec::jump_rel(c[j])->Some_0; + assert(j == c.len() - 1); +} + +/// The displacement of a `b`, `b.cond` or `cbz`/`cbnz` made with a byte +/// displacement that is a multiple of 4 is that displacement. +pub proof fn lemma_branch_rel(rel: int, cc: u8, iszero: bool) + requires + rel % 4 == 0, + -0x4000_0000 <= rel <= 0x4000_0000, + ensures + clazel_codecheck::a64::spec::jump_rel(b_i(rel)) == Some(rel), + clazel_codecheck::a64::spec::jump_rel(bcond_i(cc, rel)) == Some(rel), + clazel_codecheck::a64::spec::jump_rel(cb_i(iszero, rel)) == Some(rel), +{ + assert(rel / 4 * 4 == rel); + assert(((rel / 4) as i32) as int == rel / 4); +} + +// ---------------------------------------------------------------------------- +// The leaves +// ---------------------------------------------------------------------------- + +/// The statement of [`lemma_jt_bs`] for a node. +pub open spec fn jt_facts(n: Node, nl: nat, h: nat, offs: Seq) -> bool { + forall|x: int| + jt(ctmpl(n, nl, h, offs), x) ==> #[trigger] inoffs(offs, x) || bs( + n, + nl, + h, + offs.len(), + x, + ) +} + +/// `br l`: the branch lands on the label. +proof fn lemma_jtf_br(l: usize, nl: nat, h: nat, offs: Seq) + requires + l < offs.len(), + labels_fit(offs, 4), + ensures + jt_facts(Node::Br(l), nl, h, offs), +{ + let rel = offs[l as int]; + assert(offs[l as int] % 4 == 0); + lemma_branch_rel(rel, 0, false); + assert forall|x: int| jt(ctmpl(Node::Br(l), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::Br(l), nl, h, offs.len(), x) by { + let c = ctmpl(Node::Br(l), nl, h, offs); + assert(c == seq![b_i(rel)]); + lemma_jt_last(c, x); + assert(offs[l as int] == x); + } +} + +/// `br_if l`. +proof fn lemma_jtf_brif(l: usize, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + l < offs.len(), + labels_fit(offs, 8), + ensures + jt_facts(Node::BrIf(l), nl, h, offs), +{ + let rel = offs[l as int] - 4; + assert(offs[l as int] % 4 == 0); + lemma_branch_rel(rel, 0, false); + assert(max_slots() == 31) by { + super::tlemmas::lemma_abi_consts_t(); + } + assert forall|x: int| jt(ctmpl(Node::BrIf(l), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::BrIf(l), nl, h, offs.len(), x) by { + let c = ctmpl(Node::BrIf(l), nl, h, offs); + assert(c == cond_pre(nl, h, false, rel)); + assert(c.len() == 2); + assert(forall|k: int| 0 <= k < 1 ==> nojump(#[trigger] c[k])) by { + assert forall|k: int| 0 <= k < 1 implies nojump(c[k]) by { + assert(k == 0); + } + } + lemma_jt_last(c, x); + assert(offs[l as int] == x); + } +} + +/// `exit`: no branch. +proof fn lemma_jtf_exit(c: u32, k: usize, nl: nat, h: nat, offs: Seq) + ensures + jt_facts(Node::Exit(c, k), nl, h, offs), +{ + assert forall|x: int| jt(ctmpl(Node::Exit(c, k), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::Exit(c, k), nl, h, offs.len(), x) by { + let e = exit_seq(c as u16); + assert(forall|j: int| 0 <= j < e.len() ==> nojump(#[trigger] e[j])) by { + assert forall|j: int| 0 <= j < e.len() implies nojump(e[j]) by { + assert(j == 0 || j == 1 || j == 2); + } + } + lemma_jt_none(e, x); + } +} + +/// A straight-line instruction: no branch. +proof fn lemma_jtf_op(o: Op, nl: nat, h: nat, offs: Seq) + requires + op_ok(o, nl, h), + ensures + jt_facts(Node::Op(o), nl, h, offs), +{ + lemma_tmpl_straight(o, nl, h); + assert forall|x: int| jt(ctmpl(Node::Op(o), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::Op(o), nl, h, offs.len(), x) by { + let c = tmpl(o, nl, h); + assert(forall|k: int| 0 <= k < c.len() ==> nojump(#[trigger] c[k])) by { + assert forall|k: int| 0 <= k < c.len() implies nojump(c[k]) by { + assert(straight(c[k])); + } + } + lemma_jt_none(c, x); + } +} + +// ---------------------------------------------------------------------------- +// br_table +// ---------------------------------------------------------------------------- + +/// The chain from entry `i` on: a branch lands on a target label. +proof fn lemma_jt_entries(ls: Seq, offs: Seq, p0: int, i: nat, x: int) + requires + i <= ls.len(), + p0 == 4, + forall|k: int| 0 <= k < ls.len() ==> #[trigger] ls[k] < offs.len(), + forall|l: int| + 0 <= l < offs.len() ==> #[trigger] offs[l] % 4 == 0 && -0x10_0000 + 8 + 12 * ls.len() + <= offs[l] && offs[l] <= 0xF_FFF0, + jt(brt_entries(ls, offs, p0, i), x), + ensures + exists|k: int| i <= k < ls.len() && x + p0 + i * 12 == #[trigger] offs[ls[k] as int], + decreases ls.len() - i, +{ + assert(brt_e() == 12); + if i >= ls.len() { + assert(brt_entries(ls, offs, p0, i) =~= Seq::::empty()); + assert(false); + } else { + let rel = brt_rel(offs, ls[i as int], p0 + i * brt_e() + 8); + let e = brt_entry(i, rel); + let rest = brt_entries(ls, offs, p0, i + 1); + assert(brt_entries(ls, offs, p0, i) == e + rest); + lemma_jt_cat(e, rest, x); + if jt(e, x) { + assert(ls[i as int] < offs.len()); + let t = ls[i as int] as int; + assert(rel == offs[t] - (p0 + i * 12 + 8)); + assert((i + 1) * 12 <= ls.len() * 12) by (nonlinear_arith) + requires + i < ls.len(), + ; + assert(rel % 4 == 0) by { + assert(offs[t] % 4 == 0); + assert((p0 + i * 12 + 8) % 4 == 0) by (nonlinear_arith) + requires + p0 == 4, + ; + } + lemma_branch_rel(rel, 0, false); + assert(e.len() == 3); + assert(forall|k: int| 0 <= k < 2 ==> nojump(#[trigger] e[k])) by { + assert forall|k: int| 0 <= k < 2 implies nojump(e[k]) by { + assert(k == 0 || k == 1); + } + } + assert(clazel_codecheck::a64::spec::jump_rel(e[2]) == Some(rel)); + lemma_jt_last(e, x); + assert(x + p0 + i * 12 == offs[t]); + assert(offs[ls[i as int] as int] == x + p0 + i * 12); + assert(i <= i && i < ls.len()); + } else { + assert(jt(rest, x - 12)); + lemma_jt_entries(ls, offs, p0, i + 1, x - 12); + let k = choose|k: int| + i + 1 <= k < ls.len() && (x - 12) + p0 + (i + 1) * 12 == #[trigger] offs[ls[k] as int]; + assert(x + p0 + i * 12 == offs[ls[k] as int]); + } + } +} + +/// `br_table`. +proof fn lemma_jtf_brt(ls: Vec, d: usize, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + d < offs.len(), + forall|k: int| 0 <= k < ls@.len() ==> #[trigger] ls@[k] < offs.len(), + labels_fit(offs, 8 + 12 * ls@.len()), + ensures + jt_facts(Node::BrTable(ls, d), nl, h, offs), +{ + let p0 = brt_p0(nl, h); + assert(p0 == 4); + let n = ls@.len(); + let a = seq![load_slot(x0(), (nl + h - 1) as nat)]; + let ch = brt_entries(ls@, offs, p0, 0); + let jm = seq![b_i(brt_rel(offs, d, p0 + n * brt_e()))]; + let whole = brt_code(ls@, d, nl, h, offs); + assert(brt_e() == 12); + assert(whole =~= a + (ch + jm)); + assert(code_len(ch) == 12 * n) by { + lemma_entries_code_len(ls@, offs, p0, 0); + } + assert(max_slots() == 31) by { + super::tlemmas::lemma_abi_consts_t(); + } + assert forall|x: int| jt(ctmpl(Node::BrTable(ls, d), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::BrTable(ls, d), nl, h, offs.len(), x) by { + assert(ctmpl(Node::BrTable(ls, d), nl, h, offs) == whole); + lemma_jt_cat(a, ch + jm, x); + assert(!jt(a, x)) by { + assert(nojump(a[0])); + lemma_jt_none(a, x); + } + lemma_jt_cat(ch, jm, x - code_len(a)); + assert(code_len(a) == 4); + if jt(ch, x - code_len(a)) { + assert forall|l: int| + 0 <= l < offs.len() implies #[trigger] offs[l] % 4 == 0 && -0x10_0000 + 8 + 12 * n + <= offs[l] && offs[l] <= 0xF_FFF0 by {} + lemma_jt_entries(ls@, offs, p0, 0, x - code_len(a)); + let k = choose|k: int| + 0 <= k < n && (x - code_len(a)) + p0 + 0 * 12 == #[trigger] offs[ls@[k] as int]; + assert(offs[ls@[k] as int] == x); + assert(inoffs(offs, x)); + } else { + let z = x - code_len(a) - code_len(ch); + assert(jt(jm, z)); + let rel = brt_rel(offs, d, p0 + n * brt_e()); + assert(rel == offs[d as int] - (4 + 12 * n)); + assert(rel % 4 == 0) by { + assert(offs[d as int] % 4 == 0); + } + lemma_branch_rel(rel, 0, false); + assert(clazel_codecheck::a64::spec::jump_rel(jm[0]) == Some(rel)); + lemma_jt_last(jm, z); + assert(x == offs[d as int]); + assert(inoffs(offs, x)); + } + } +} + +/// The chain's entries are 12 bytes each. +proof fn lemma_entries_code_len(ls: Seq, offs: Seq, p0: int, i: nat) + ensures + code_len(brt_entries(ls, offs, p0, i)) == if i >= ls.len() { + 0nat + } else { + (12 * (ls.len() - i)) as nat + }, + decreases ls.len() - i, +{ + if i < ls.len() { + let e = brt_entry(i, brt_rel(offs, ls[i as int], p0 + i * brt_e() + 8)); + lemma_entries_code_len(ls, offs, p0, i + 1); + assert(brt_entries(ls, offs, p0, i) == e + brt_entries(ls, offs, p0, i + 1)); + } else { + assert(brt_entries(ls, offs, p0, i) =~= Seq::::empty()); + } +} + +// ---------------------------------------------------------------------------- +// Memory instructions +// ---------------------------------------------------------------------------- + +/// The check's two exits: the validator's own sequence, whose `b.cond`s are the +/// third and fifth instructions. +proof fn lemma_jt_chk(c: clazel_codecheck::a64::spec::Chk, x: int) + requires + jt(clazel_codecheck::a64::spec::check_seq(c), x), + ensures + x == 8 + 4 * (c.r1 as int) || x == 16 + 4 * (c.r2 as int), +{ + let cs = clazel_codecheck::a64::spec::check_seq(c); + assert(cs[0] == clazel_codecheck::a64::spec::check_instr(0, c)); + assert(cs[1] == clazel_codecheck::a64::spec::check_instr(1, c)); + assert(cs[2] == clazel_codecheck::a64::spec::check_instr(2, c)); + assert(cs[3] == clazel_codecheck::a64::spec::check_instr(3, c)); + assert(cs[4] == clazel_codecheck::a64::spec::check_instr(4, c)); + assert(cs[5] == clazel_codecheck::a64::spec::check_instr(5, c)); + assert(cs[6] == clazel_codecheck::a64::spec::check_instr(6, c)); + let j = choose|j: int| + 0 <= j < cs.len() && #[trigger] clazel_codecheck::a64::spec::jump_rel(cs[j]) is Some && x + == clazel_codecheck::a64::spec::start_of(cs, j) + + clazel_codecheck::a64::spec::jump_rel(cs[j])->Some_0; + assert(j == 2 || j == 4); +} + +/// A memory instruction: its branches land on the stub, or at its end. +proof fn lemma_jtf_mem(m: MemOp, nl: nat, h: nat, offs: Seq) + ensures + jt_facts(Node::Mem(m), nl, h, offs), +{ + lemma_mem_csize(m, nl, h, offs.len()); + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![b_i(code_len(exit_seq(mem_exit_code(m))) as int + 4)]; + let stub = exit_seq(mem_exit_code(m)); + let c0 = mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)); + assert(code_len(stub) == 12); + lemma_branch_rel(16, 0, false); + assert forall|x: int| jt(ctmpl(Node::Mem(m), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::Mem(m), nl, h, offs.len(), x) by { + let a1 = pre + chk; + let a2 = a1 + tail; + let a3 = a2 + jm; + assert(ctmpl(Node::Mem(m), nl, h, offs) == mem_code(m, nl, h)); + assert(mem_code(m, nl, h) == a3 + stub); + lemma_jt_cat(a3, stub, x); + assert(!jt(stub, x - code_len(a3))) by { + assert(forall|j: int| 0 <= j < stub.len() ==> nojump(#[trigger] stub[j])) by { + assert forall|j: int| 0 <= j < stub.len() implies nojump(stub[j]) by { + assert(j == 0 || j == 1 || j == 2); + } + } + lemma_jt_none(stub, x - code_len(a3)); + } + lemma_jt_cat(a2, jm, x); + if jt(jm, x - code_len(a2)) { + lemma_jt_last(jm, x - code_len(a2)); + assert(clazel_codecheck::a64::spec::jump_rel(jm[0]) == Some(16int)); + assert(x == csize(Node::Mem(m), nl, h, offs.len())); + assert(bs(Node::Mem(m), nl, h, offs.len(), x)); + } else { + lemma_jt_cat(a1, tail, x); + assert(jt(a1, x)) by { + if jt(tail, x - code_len(a1)) { + assert(forall|j: int| 0 <= j < tail.len() ==> nojump(#[trigger] tail[j])) by { + assert forall|j: int| 0 <= j < tail.len() implies nojump(tail[j]) by { + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { .. } => { + assert(j == 0 || j == 1); + }, + MemOp::Store { .. } => { + assert(j == 0); + }, + } + } + } + lemma_jt_none(tail, x - code_len(a1)); + } + } + lemma_jt_cat(pre, chk, x); + assert(jt(chk, x - code_len(pre))) by { + if jt(pre, x) { + assert(forall|j: int| 0 <= j < pre.len() ==> nojump(#[trigger] pre[j])) by { + assert forall|j: int| 0 <= j < pre.len() implies nojump(pre[j]) by { + match m { + MemOp::BoundsCheck { .. } => { + assert(j == 0); + }, + MemOp::Load { .. } => { + assert(j == 0 || j == 1); + }, + MemOp::Store { .. } => { + assert(j == 0 || j == 1 || j == 2); + }, + } + } + } + lemma_jt_none(pre, x); + } + } + assert(chk == clazel_codecheck::a64::spec::check_seq(c0)); + lemma_jt_chk(c0, x - code_len(pre)); + assert(c0.r1 == mem_r1(m, nl, h) as i32); + assert(c0.r2 == mem_r2(m, nl, h) as i32); + assert(x == stub_off(m, nl, h)); + assert(bs(Node::Mem(m), nl, h, offs.len(), x)); + } + } +} + +// ---------------------------------------------------------------------------- +// Composition +// ---------------------------------------------------------------------------- + +/// `then`. +proof fn lemma_jtf_then(a: Node, b: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(Node::Then(Box::new(a), Box::new(b)), nl, h, offs.len()) is Some, + cf_flow(a, nl, h, offs.len()) is Some, + jt_facts(a, nl, h, offs), + match cf_flow(a, nl, h, offs.len()) { + Some(Flow::Falls(h1)) => jt_facts( + b, + nl, + flow_h(a, nl, h, offs.len()), + shift(offs, csize(a, nl, h, offs.len()) as int), + ), + _ => true, + }, + ensures + jt_facts(Node::Then(Box::new(a), Box::new(b)), nl, h, offs), +{ + let n = Node::Then(Box::new(a), Box::new(b)); + let d = offs.len(); + let sa = csize(a, nl, h, d) as int; + let h1 = flow_h(a, nl, h, d); + let ob = shift(offs, sa); + lemma_ctmpl_len(a, nl, h, offs); + assert forall|x: int| jt(ctmpl(n, nl, h, offs), x) implies #[trigger] inoffs(offs, x) || bs( + n, + nl, + h, + d, + x, + ) by { + lemma_jt_cat(ctmpl(a, nl, h, offs), ctmpl(b, nl, h1, ob), x); + if jt(ctmpl(a, nl, h, offs), x) { + if inoffs(offs, x) { + } else { + assert(bs(a, nl, h, d, x)); + } + } else { + let y = x - sa; + assert(jt(ctmpl(b, nl, h1, ob), y)); + match cf_flow(a, nl, h, d) { + Some(Flow::Falls(_)) => { + assert(ob.len() == d); + if inoffs(ob, y) { + let l = choose|l: int| 0 <= l < ob.len() && #[trigger] ob[l] == y; + lemma_shift_index(offs, sa, l); + assert(offs[l] == x); + assert(inoffs(offs, x)); + } else { + assert(bs(b, nl, h1, d, y)); + } + }, + Some(Flow::Dead) => { + assert(b == Node::Op(Op::Nop)); + assert(ctmpl(b, nl, h1, ob) =~= Seq::::empty()); + assert(false); + }, + None => {}, + } + } + } +} + +/// `block`. +proof fn lemma_jtf_block(b: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(Node::Block(Box::new(b)), nl, h, offs.len()) is Some, + jt_facts(b, nl, h, seq![csize(b, nl, h, offs.len() + 1) as int] + offs), + ensures + jt_facts(Node::Block(Box::new(b)), nl, h, offs), +{ + let n = Node::Block(Box::new(b)); + let d = offs.len(); + let sb = csize(b, nl, h, d + 1) as int; + let ob = seq![sb] + offs; + assert(ob.len() == d + 1); + lemma_bs_facts(b, nl, h, d + 1); + assert forall|x: int| jt(ctmpl(n, nl, h, offs), x) implies #[trigger] inoffs(offs, x) || bs( + n, + nl, + h, + d, + x, + ) by { + assert(jt(ctmpl(b, nl, h, ob), x)); + if inoffs(ob, x) { + let l = choose|l: int| 0 <= l < ob.len() && #[trigger] ob[l] == x; + if l == 0 { + assert(x == sb); + assert(bs(b, nl, h, d + 1, sb)); + } else { + assert(ob[l] == offs[l - 1]); + assert(inoffs(offs, x)); + } + } else { + assert(bs(b, nl, h, d + 1, x)); + } + } +} + +/// `loop`. +proof fn lemma_jtf_loop(b: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(Node::Loop(Box::new(b)), nl, h, offs.len()) is Some, + jt_facts(b, nl, h, seq![0int] + offs), + ensures + jt_facts(Node::Loop(Box::new(b)), nl, h, offs), +{ + let n = Node::Loop(Box::new(b)); + let d = offs.len(); + let ob = seq![0int] + offs; + assert(ob.len() == d + 1); + lemma_bs_facts(b, nl, h, d + 1); + assert forall|x: int| jt(ctmpl(n, nl, h, offs), x) implies #[trigger] inoffs(offs, x) || bs( + n, + nl, + h, + d, + x, + ) by { + assert(jt(ctmpl(b, nl, h, ob), x)); + if inoffs(ob, x) { + let l = choose|l: int| 0 <= l < ob.len() && #[trigger] ob[l] == x; + if l == 0 { + assert(x == 0); + assert(bs(b, nl, h, d + 1, 0)); + } else { + assert(ob[l] == offs[l - 1]); + assert(inoffs(offs, x)); + } + } else { + assert(bs(b, nl, h, d + 1, x)); + } + } +} + +/// `if`. +#[verifier::rlimit(1500)] +proof fn lemma_jtf_if(t: Node, e: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(Node::If(Box::new(t), Box::new(e)), nl, h, offs.len()) is Some, + csize(Node::If(Box::new(t), Box::new(e)), nl, h, offs.len()) <= max_size(), + jt_facts(t, nl, (h - 1) as nat, seq![ + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 4 + csize( + e, + nl, + (h - 1) as nat, + offs.len() + 1, + ) as int, + ] + shift(offs, 8)), + jt_facts(e, nl, (h - 1) as nat, seq![csize(e, nl, (h - 1) as nat, offs.len() + 1) as int] + + shift(offs, 12 + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int)), + ensures + jt_facts(Node::If(Box::new(t), Box::new(e)), nl, h, offs), +{ + let n = Node::If(Box::new(t), Box::new(e)); + let d = offs.len(); + let h0 = (h - 1) as nat; + let st = csize(t, nl, h0, d + 1) as int; + let se = csize(e, nl, h0, d + 1) as int; + let ot = seq![st + 4 + se] + shift(offs, 8); + let oe = seq![se] + shift(offs, 12 + st); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_csize_if(t, e, nl, h, d); + lemma_bs_facts(t, nl, h0, d + 1); + lemma_bs_facts(e, nl, h0, d + 1); + let cp = cond_pre(nl, h, true, st + 8); + let ct = ctmpl(t, nl, h0, ot); + let ce = ctmpl(e, nl, h0, oe); + let jm = seq![b_i(se + 4)]; + lemma_ctmpl_len(t, nl, h0, ot); + lemma_ctmpl_len(e, nl, h0, oe); + assert(st % 4 == 0 && se % 4 == 0) by { + super::cfproofs::lemma_csize_mod4(t, nl, h0, d + 1); + super::cfproofs::lemma_csize_mod4(e, nl, h0, d + 1); + } + lemma_branch_rel(st + 8, 0, true); + lemma_branch_rel(se + 4, 0, true); + assert(code_len(cp) == 8); + assert forall|x: int| jt(ctmpl(n, nl, h, offs), x) implies #[trigger] inoffs(offs, x) || bs( + n, + nl, + h, + d, + x, + ) by { + assert(ctmpl(n, nl, h, offs) == cp + ct + jm + ce); + lemma_jt_cat(cp + ct + jm, ce, x); + assert(code_len(cp + ct) == 8 + st); + assert(code_len(cp + ct + jm) == 12 + st); + if jt(cp + ct + jm, x) { + lemma_jt_cat(cp + ct, jm, x); + if jt(cp + ct, x) { + lemma_jt_cat(cp, ct, x); + if jt(cp, x) { + assert(cp.len() == 2); + assert(forall|k: int| 0 <= k < 1 ==> nojump(#[trigger] cp[k])) by { + assert forall|k: int| 0 <= k < 1 implies nojump(cp[k]) by { + assert(k == 0); + } + } + lemma_jt_last(cp, x); + assert(x == 4 + st + 8); + assert(bs(e, nl, h0, d + 1, 0)); + assert(x - (12 + st) == 0); + } else { + let y = x - 8; + assert(jt(ct, y)); + if inoffs(ot, y) { + let l = choose|l: int| 0 <= l < ot.len() && #[trigger] ot[l] == y; + if l == 0 { + assert(x == 12 + st + se); + assert(bs(e, nl, h0, d + 1, se)); + } else { + assert(ot[l] == shift(offs, 8)[l - 1]); + lemma_shift_index(offs, 8, l - 1); + assert(offs[l - 1] == x); + assert(inoffs(offs, x)); + } + } else { + assert(bs(t, nl, h0, d + 1, y)); + } + } + } else { + lemma_jt_last(jm, x - code_len(cp + ct)); + assert(clazel_codecheck::a64::spec::jump_rel(jm[0]) == Some(se + 4)); + assert(x == 8 + st + se + 4); + assert(bs(e, nl, h0, d + 1, se)); + } + } else { + let y = x - (12 + st); + assert(jt(ce, y)); + if inoffs(oe, y) { + let l = choose|l: int| 0 <= l < oe.len() && #[trigger] oe[l] == y; + if l == 0 { + assert(bs(e, nl, h0, d + 1, se)); + } else { + assert(oe[l] == shift(offs, 12 + st)[l - 1]); + lemma_shift_index(offs, 12 + st, l - 1); + assert(offs[l - 1] == x); + assert(inoffs(offs, x)); + } + } else { + assert(bs(e, nl, h0, d + 1, y)); + } + } + } +} + +/// **Every branch of a node's code lands on a label it was given, or on a +/// position of [`bs`].** +#[verifier::rlimit(1500)] +pub proof fn lemma_jt_bs(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + csize(n, nl, h, offs.len()) <= max_size(), + labels_fit(offs, csize(n, nl, h, offs.len())), + ensures + jt_facts(n, nl, h, offs), + decreases n, +{ + let d = offs.len(); + let size = csize(n, nl, h, d); + match n { + Node::Op(o) => { + lemma_jtf_op(o, nl, h, offs); + }, + Node::Mem(m) => { + lemma_jtf_mem(m, nl, h, offs); + }, + Node::Then(a, b) => { + lemma_csize_then(*a, *b, nl, h, d); + let sa = csize(*a, nl, h, d); + lemma_fit_mono(offs, size, sa); + lemma_jt_bs(*a, nl, h, offs); + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + assert(flow_h(*a, nl, h, d) == h1); + let sb = csize(*b, nl, h1, d); + let ob = shift(offs, sa as int); + super::cfproofs::lemma_csize_mod4(*a, nl, h, d); + lemma_fit_shift(offs, sa, size, sb); + assert(ob.len() == d); + lemma_jt_bs(*b, nl, h1, ob); + }, + _ => {}, + } + lemma_jtf_then(*a, *b, nl, h, offs); + }, + Node::Block(b) => { + lemma_csize_block(*b, nl, h, d); + let sb = csize(*b, nl, h, d + 1); + let ob = seq![sb as int] + offs; + super::cfproofs::lemma_csize_mod4(*b, nl, h, d + 1); + lemma_fit_mono(offs, size, sb); + lemma_fit_cons(sb as int, offs, sb); + assert(ob.len() == d + 1); + lemma_jt_bs(*b, nl, h, ob); + lemma_jtf_block(*b, nl, h, offs); + }, + Node::Loop(b) => { + lemma_csize_loop(*b, nl, h, d); + let sb = csize(*b, nl, h, d + 1); + let ob = seq![0int] + offs; + lemma_fit_mono(offs, size, sb); + lemma_fit_cons(0, offs, sb); + assert(ob.len() == d + 1); + lemma_jt_bs(*b, nl, h, ob); + lemma_jtf_loop(*b, nl, h, offs); + }, + Node::If(t, e) => { + lemma_csize_if(*t, *e, nl, h, d); + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 4 + se] + shift(offs, 8); + let oe = seq![se] + shift(offs, 12 + st); + super::cfproofs::lemma_csize_mod4(*t, nl, (h - 1) as nat, d + 1); + super::cfproofs::lemma_csize_mod4(*e, nl, (h - 1) as nat, d + 1); + lemma_fit_shift(offs, 8, size, st as nat); + lemma_fit_cons(st + 4 + se, shift(offs, 8), st as nat); + lemma_fit_shift(offs, (12 + st) as nat, size, se as nat); + lemma_fit_cons(se, shift(offs, 12 + st), se as nat); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_jt_bs(*t, nl, (h - 1) as nat, ot); + lemma_jt_bs(*e, nl, (h - 1) as nat, oe); + lemma_jtf_if(*t, *e, nl, h, offs); + }, + Node::Br(l) => { + lemma_csize_br(l, nl, h, d); + lemma_jtf_br(l, nl, h, offs); + }, + Node::BrIf(l) => { + lemma_csize_brif(l, nl, h, d); + lemma_jtf_brif(l, nl, h, offs); + }, + Node::BrTable(ls, dd) => { + lemma_brt_len(ls@, dd, nl, h, offs); + lemma_ctmpl_len(n, nl, h, offs); + assert(size == 8 + 12 * ls@.len()); + lemma_jtf_brt(ls, dd, nl, h, offs); + }, + Node::Exit(c, k) => { + lemma_jtf_exit(c, k, nl, h, offs); + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfleaf.rs b/crates/clazel-jit/src/a64/cfleaf.rs new file mode 100644 index 000000000..f64629b45 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfleaf.rs @@ -0,0 +1,386 @@ +//! The leaves of the control-flow composition: `br` (a `b`), `br_if` (a load +//! of the condition and a `cbnz`) and the pieces they need: one step of a +//! `b`, of a `b.cond` and of a `cbz`/`cbnz`, a run of plain instructions +//! followed by one more, and the relation surviving a change of `pc`. The A64 +//! counterpart of [`crate::cfleaf`]. A branch is relative to its own address +//! and in words (choice 16 of [`super::cfspec`]). + +#[cfg(verus_only)] +use super::cfalg::*; +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cfsteps::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `b rel` goes to its own address plus `rel`. +pub proof fn lemma_step_b(m: isa::State, rel: int) + requires + rel % 4 == 0, + -0x800_0000 <= rel < 0x800_0000, + 0 <= m.pc as int + rel < 0x1_0000_0000_0000_0000, + ensures + isa::step(m, b_i(rel)) == Some(isa::State { pc: (m.pc as int + rel) as u64, ..m }), +{ + super::memlemmas::lemma_p2_consts(); + super::memlemmas::lemma_trunc64(m.pc as int + rel); + assert(((rel / 4) as i32) as int == rel / 4); + assert((rel / 4) * 4 == rel); +} + +/// `b.cc rel` goes to its own address plus `rel` when the condition holds, and +/// to the next instruction when not. +pub proof fn lemma_step_bcond(m: isa::State, cc: u8, rel: int) + requires + cc < 14, + rel % 4 == 0, + -0x10_0000 <= rel < 0x10_0000, + 0 <= m.pc as int + rel < 0x1_0000_0000_0000_0000, + m.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(m, bcond_i(cc, rel)) == Some( + isa::State { + pc: (if isa::condition_holds(m, cc) { + m.pc as int + rel + } else { + m.pc as int + 4 + }) as u64, + ..m + }, + ), +{ + super::memlemmas::lemma_p2_consts(); + super::memlemmas::lemma_trunc64(m.pc as int + rel); + super::memlemmas::lemma_trunc64(m.pc as int + 4); + assert(((rel / 4) as i32) as int == rel / 4); + assert((rel / 4) * 4 == rel); +} + +/// `cbz w0, rel` (`iszero`) or `cbnz w0, rel` goes to its own address plus +/// `rel` when the low 32 bits of `x0` are zero (are not zero), and to the next +/// instruction when not. +pub proof fn lemma_step_cb(m: isa::State, iszero: bool, rel: int) + requires + m.r.len() == 31, + rel % 4 == 0, + -0x10_0000 <= rel < 0x10_0000, + 0 <= m.pc as int + rel < 0x1_0000_0000_0000_0000, + m.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(m, cb_i(iszero, rel)) == Some( + isa::State { + pc: (if (isa::trunc(32, m.r[0] as int) == 0) == iszero { + m.pc as int + rel + } else { + m.pc as int + 4 + }) as u64, + ..m + }, + ), +{ + super::memlemmas::lemma_p2_consts(); + super::memlemmas::lemma_trunc64(m.pc as int + rel); + super::memlemmas::lemma_trunc64(m.pc as int + 4); + assert(((rel / 4) as i32) as int == rel / 4); + assert((rel / 4) * 4 == rel); + assert(isa::x_read(m, 0, 32) == isa::trunc(32, m.r[0] as int)); +} + +/// `m2` differs from `m` in registers other than `x28` (and `pc`, flags): the +/// relation, and the region table, carry over. +pub proof fn lemma_rel_base_same( + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + m2: isa::State, + nl: nat, +) + requires + rel_base(lay, base, a, m, nl), + m2.mem == m.mem, + m2.r.len() == 31, + m2.vreg.len() == 32, + m2.r[abi::ctx_reg() as int] == m.r[abi::ctx_reg() as int], + ensures + rel_base(lay, base, a, m2, nl), +{ + reveal(rel_base); + super::steps::lemma_abi_consts(); + super::rel::lemma_rel_regs(lay, a.with_stack(base + a.stack), m, m2, nl); + assert(ctx_of(m2) == ctx_of(m)); +} + +/// A state with the same memory, the same `x28`, `x30` and `SP` preserves +/// everything. +pub proof fn lemma_preserved_same(lay: lir::Layout, m: isa::State, m2: isa::State) + requires + m.r.len() == 31, + m2.r.len() == 31, + m2.mem == m.mem, + m2.r[abi::ctx_reg() as int] == m.r[abi::ctx_reg() as int], + m2.r[abi::link_reg() as int] == m.r[abi::link_reg() as int], + m2.sp == m.sp, + ensures + preserved_r(lay, m, m2), +{ + assert(ctx_of(m2) == ctx_of(m)); +} + +/// One step at the start of `code`, placed at `base`, is a run of one step. +pub proof fn lemma_steps_one( + code: Seq, + base: int, + s: isa::State, + i: isa::Instr, + s1: isa::State, +) + requires + fetch(code, s.pc as int - base) == Some(i), + isa::step(s, i) == Some(s1), + ensures + steps(code, base, s, 1) == Some(s1), +{ + reveal_with_fuel(steps, 2); +} + +/// **Plain instructions, then one more**, for an abstract step function `f` +/// (so the solver does not unfold `isa::step` on the unknown instruction +/// `last`; see [`steps_g`]). If `pre` has no jumps and runs from `m` to `m1`, +/// and `last` steps from `m1` to `m2`, then `pre + [last]` placed at `m.pc` +/// reaches `m2` by `steps_g`, with `m1` at the end of `pre`. +pub proof fn lemma_steps_plain_then_g( + f: spec_fn(isa::State, isa::Instr) -> Option, + pre: Seq, + last: isa::Instr, + m: isa::State, + m1: isa::State, + m2: isa::State, +) + requires + pc_plain(f), + no_jumps(pre), + m.pc as int + code_len(pre) + 4 < isa::p2(64), + run_gen(f, m, pre) == Some(m1), + f(m1, last) == Some(m2), + ensures + m1.pc as int == m.pc as int + code_len(pre), + steps_g(f, pre + seq![last], m.pc as int, m, pre.len() + 1) == Some(m2), +{ + let whole = pre + seq![last]; + assert(Seq::::empty() + pre + seq![last] =~= whole); + assert(code_len(Seq::::empty()) == 0); + lemma_run_gen_steps(f, Seq::empty(), pre, seq![last], m.pc as int, m, m1); + super::runseq::lemma_code_len_append(pre, seq![last]); + lemma_fetch_append_r(pre, seq![last], code_len(pre) as int); + assert(fetch(seq![last], 0) == Some(last)); + assert(m1.pc as int - m.pc as int == code_len(pre)); + assert(code_len(pre) as int - code_len(pre) == 0); + assert(fetch(whole, m1.pc as int - m.pc as int) == Some(last)); + reveal_with_fuel(steps_g, 2); + assert(steps_g(f, whole, m.pc as int, m1, 1) == Some(m2)); + lemma_steps_g_trans(f, whole, m.pc as int, m, m1, m2, pre.len(), 1); +} + +/// [`lemma_steps_plain_then_g`] for the machine's step: the `reaches` form. +pub proof fn lemma_reaches_plain_then( + pre: Seq, + last: isa::Instr, + m: isa::State, + m1: isa::State, + m2: isa::State, +) + requires + no_jumps(pre), + m.pc as int + code_len(pre) + 4 < isa::p2(64), + run_seq(m, pre) == Some(m1), + isa::step(m1, last) == Some(m2), + ensures + m1.pc as int == m.pc as int + code_len(pre), + reaches(pre + seq![last], m.pc as int, m, m2), +{ + let f = stepper(); + lemma_stepper_pc_plain(); + lemma_run_seq_is_gen(m, pre); + assert(f(m1, last) == Some(m2)); + lemma_steps_plain_then_g(f, pre, last, m, m1, m2); + lemma_steps_is_gen(pre + seq![last], m.pc as int, m, pre.len() + 1); +} + +/// **`br l` is a `b`.** The code is `b` to label `l`; the machine is then at +/// the label, related to the same LIR state. +pub proof fn lemma_br(nl: nat, h: nat, offs: Seq, l: usize) + requires + l < offs.len(), + offs[l as int] % 4 == 0, + -0x800_0000 <= offs[l as int] < 0x800_0000, + ensures + cf_refines(Node::Br(l)@, nl, h, offs, seq![b_i(offs[l as int])]), +{ + let rel = offs[l as int]; + let code = seq![b_i(rel)]; + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, m, lir::eval(lay, Node::Br(l)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && cf_canon( + m.pc as int, + offs, + code, + ) implies cf_post( + lay, + base, + nl, + offs, + code, + m, + lir::eval(lay, Node::Br(l)@, a, fuel), + ) by { + if fuel > 0 { + lemma_rel_base_unfold(lay, base, a, m, nl); + super::memlemmas::lemma_p2_consts(); + assert(code_len(code) == 4); + assert(cf_canon(m.pc as int, offs, code)); + assert(0 <= m.pc as int + offs[l as int]); + assert(0 <= m.pc as int + rel < 0x1_0000_0000_0000_0000); + lemma_step_b(m, rel); + let m2 = isa::State { pc: (m.pc as int + rel) as u64, ..m }; + assert(m2.pc as int == m.pc as int + offs[l as int]); + assert(fetch(code, m.pc as int - m.pc as int) == Some(b_i(rel))); + lemma_steps_one(code, m.pc as int, m, b_i(rel), m2); + assert(reaches(code, m.pc as int, m, m2)); + assert(base + Seq::::empty() =~= base); + lemma_rel_base_same(lay, base, a, m, m2, nl); + lemma_preserved_same(lay, m, m2); + assert(rel_base(lay, base + Seq::::empty(), a, m2, nl)); + assert(rel_ext(lay, base, a, m2, nl)); + } + } +} + +/// **`br_if l`**: `ldr x0, [cond]; cbnz w0` to label `l`. +#[verifier::rlimit(400)] +pub proof fn lemma_brif(nl: nat, h: nat, offs: Seq, l: usize) + requires + l < offs.len(), + 1 <= h, + nl + h <= max_slots(), + offs[l as int] % 4 == 0, + -0x10_0000 <= offs[l as int] - 4 < 0x10_0000, + ensures + cf_refines( + Node::BrIf(l)@, + nl, + h, + offs, + cond_pre(nl, h, false, offs[l as int] - 4), + ), +{ + super::steps::lemma_abi_consts(); + super::memlemmas::lemma_p2_consts(); + let rel = offs[l as int] - 4; + let code = cond_pre(nl, h, false, rel); + let j = (nl + h - 1) as nat; + let i1 = load_slot(x0(), j); + let i2 = cb_i(false, rel); + assert(code == seq![i1, i2]); + assert(code_len(code) == 8); + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, m, lir::eval(lay, Node::BrIf(l)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && cf_canon( + m.pc as int, + offs, + code, + ) implies cf_post( + lay, + base, + nl, + offs, + code, + m, + lir::eval(lay, Node::BrIf(l)@, a, fuel), + ) by { + if fuel > 0 { + let n = a.stack.len(); + if n >= 1 { + match a.stack[(n - 1) as int] { + lir::Val::I32(c) => { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + assert(ac.stack[(h - 1) as int] == lir::Val::I32(c)); + assert(slot_val(ac, nl, j) == lir::Val::I32(c)); + assert(j < nl + ac.stack.len()); + assert(m.pc as int + 4 < 0x1_0000_0000_0000_0000); + super::rel::lemma_load_rel(lay, ac, m, x0(), j, nl); + let m1 = isa::step(m, i1).unwrap(); + assert(m1.r[0] == c as u64); + assert(m1.pc as int == m.pc as int + 4); + assert(0 <= m1.pc as int + rel < 0x1_0000_0000_0000_0000); + assert(m1.pc as int + 4 < 0x1_0000_0000_0000_0000); + assert(isa::trunc(32, c as int) == c) by { + vstd::arithmetic::div_mod::lemma_small_mod(c as nat, 0x1_0000_0000nat); + } + lemma_step_cb(m1, false, rel); + let m2 = isa::step(m1, i2).unwrap(); + super::runseq::run1(m, i1, m1); + assert(no_jumps(seq![i1])); + lemma_reaches_plain_then(seq![i1], i2, m, m1, m2); + assert(code == seq![i1] + seq![i2]); + assert(m2.r == m1.r && m2.mem == m1.mem && m2.vreg == m1.vreg); + super::rel::lemma_rel_regs(lay, ac, m1, m2, nl); + let s1 = a.with_stack(lir::below(a.stack, 1)); + assert((base + a.stack).take((base + lir::below(a.stack, 1)).len() as int) + =~= base + lir::below(a.stack, 1)); + lemma_rel_prefix( + lay, + a, + base + a.stack, + base + lir::below(a.stack, 1), + m2, + nl, + ); + lemma_preserved_same(lay, m, m2); + lemma_table_pres(lay, m, m2); + assert(s1.with_stack(base + s1.stack) == a.with_stack( + base + lir::below(a.stack, 1), + )); + reveal(rel_base); + assert(rel_base(lay, base, s1, m2, nl)); + assert(base + Seq::::empty() =~= base); + assert(rel_ext(lay, base, s1, m2, nl)); + assert(reaches(code, m.pc as int, m, m2)); + if c != 0 { + assert(m2.pc as int == m.pc as int + offs[l as int]); + } else { + assert(m2.pc as int == m.pc as int + code_len(code)); + } + }, + _ => {}, + } + } + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cflens.rs b/crates/clazel-jit/src/a64/cflens.rs new file mode 100644 index 000000000..75a3ff479 --- /dev/null +++ b/crates/clazel-jit/src/a64/cflens.rs @@ -0,0 +1,168 @@ +//! The lengths of the control-flow templates: every A64 instruction is 4 +//! bytes, so [`csize`] is the length of [`ctmpl`] whatever the labels' offsets +//! are. The A64 counterpart of [`crate::cflens`]. + +#[cfg(verus_only)] +use super::cfbrt::*; +#[cfg(verus_only)] +use super::cfif::lemma_cond_pre_len; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The length of `br_table`'s code does not depend on the labels. +pub proof fn lemma_brt_len(ls: Seq, d: usize, nl: nat, h: nat, offs: Seq) + ensures + code_len(brt_code(ls, d, nl, h, offs)) == 8 + 12 * ls.len(), +{ + lemma_brt_shape(ls, d, nl, h, offs); +} + +/// The length of a node's code does not depend on the values of the label +/// offsets, only on their number. +pub proof fn lemma_ctmpl_len_eq(n: Node, nl: nat, h: nat, o1: Seq, o2: Seq) + requires + o1.len() == o2.len(), + ensures + code_len(ctmpl(n, nl, h, o1)) == code_len(ctmpl(n, nl, h, o2)), + decreases n, +{ + match n { + Node::Op(o) => {}, + Node::Mem(_) => {}, + Node::Then(a, b) => { + lemma_ctmpl_len_eq(*a, nl, h, o1, o2); + let d = csize(*a, nl, h, o1.len()) as int; + let h1 = flow_h(*a, nl, h, o1.len()); + assert(shift(o1, d).len() == shift(o2, d).len()); + lemma_ctmpl_len_eq(*b, nl, h1, shift(o1, d), shift(o2, d)); + }, + Node::Block(b) => { + let s1 = seq![csize(*b, nl, h, o1.len() + 1) as int] + o1; + let s2 = seq![csize(*b, nl, h, o2.len() + 1) as int] + o2; + lemma_ctmpl_len_eq(*b, nl, h, s1, s2); + }, + Node::Loop(b) => { + lemma_ctmpl_len_eq(*b, nl, h, seq![0int] + o1, seq![0int] + o2); + }, + Node::If(t, e) => { + let st = csize(*t, nl, (h - 1) as nat, o1.len() + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, o1.len() + 1) as int; + let t1 = seq![st + 4 + se] + shift(o1, 8); + let t2 = seq![st + 4 + se] + shift(o2, 8); + let e1 = seq![se] + shift(o1, 12 + st); + let e2 = seq![se] + shift(o2, 12 + st); + lemma_ctmpl_len_eq(*t, nl, (h - 1) as nat, t1, t2); + lemma_ctmpl_len_eq(*e, nl, (h - 1) as nat, e1, e2); + }, + Node::Br(l) => {}, + Node::BrIf(l) => {}, + Node::BrTable(ls, d) => { + lemma_brt_len(ls@, d, nl, h, o1); + lemma_brt_len(ls@, d, nl, h, o2); + }, + Node::Exit(c, _) => {}, + } +} + +/// [`csize`] is the length of the code, for any labels. +pub proof fn lemma_ctmpl_len(n: Node, nl: nat, h: nat, offs: Seq) + ensures + code_len(ctmpl(n, nl, h, offs)) == csize(n, nl, h, offs.len()), +{ + lemma_ctmpl_len_eq(n, nl, h, offs, zeros(offs.len())); +} + +/// `csize` of `then`: the first's, and the second's at the height the first +/// leaves. +pub proof fn lemma_csize_then(a: Node, b: Node, nl: nat, h: nat, depth: nat) + ensures + csize(Node::Then(Box::new(a), Box::new(b)), nl, h, depth) == csize(a, nl, h, depth) + + csize(b, nl, flow_h(a, nl, h, depth), depth), +{ + let z = zeros(depth); + let d = csize(a, nl, h, depth) as int; + let h1 = flow_h(a, nl, h, depth); + assert(z.len() == depth); + lemma_ctmpl_len(b, nl, h1, shift(z, d)); + assert(shift(z, d).len() == depth); +} + +/// `csize` of `block`: the body's, one label deeper. +pub proof fn lemma_csize_block(b: Node, nl: nat, h: nat, depth: nat) + ensures + csize(Node::Block(Box::new(b)), nl, h, depth) == csize(b, nl, h, depth + 1), +{ + let z = zeros(depth); + lemma_ctmpl_len(b, nl, h, seq![csize(b, nl, h, depth + 1) as int] + z); + assert((seq![csize(b, nl, h, depth + 1) as int] + z).len() == depth + 1); +} + +/// `csize` of `loop`: the body's, one label deeper. +pub proof fn lemma_csize_loop(b: Node, nl: nat, h: nat, depth: nat) + ensures + csize(Node::Loop(Box::new(b)), nl, h, depth) == csize(b, nl, h, depth + 1), +{ + let z = zeros(depth); + lemma_ctmpl_len(b, nl, h, seq![0int] + z); + assert((seq![0int] + z).len() == depth + 1); +} + +/// `csize` of `br`: one `b`. +pub proof fn lemma_csize_br(l: usize, nl: nat, h: nat, depth: nat) + ensures + csize(Node::Br(l), nl, h, depth) == 4, +{ +} + +/// `csize` of `br_if`: the condition test. +pub proof fn lemma_csize_brif(l: usize, nl: nat, h: nat, depth: nat) + ensures + csize(Node::BrIf(l), nl, h, depth) == 8, +{ + let z = zeros(depth); + lemma_cond_pre_len( + nl, + h, + false, + if l < z.len() { + z[l as int] - 4 + } else { + 0 + }, + ); +} + +/// `csize` of `if`: the test, the first arm, a `b`, the second arm. +pub proof fn lemma_csize_if(t: Node, e: Node, nl: nat, h: nat, depth: nat) + requires + 1 <= h, + ensures + csize(Node::If(Box::new(t), Box::new(e)), nl, h, depth) == 8 + csize( + t, + nl, + (h - 1) as nat, + depth + 1, + ) + 4 + csize(e, nl, (h - 1) as nat, depth + 1), +{ + let z = zeros(depth); + let st = csize(t, nl, (h - 1) as nat, depth + 1) as int; + let se = csize(e, nl, (h - 1) as nat, depth + 1) as int; + let ot = seq![st + 4 + se] + shift(z, 8); + let oe = seq![se] + shift(z, 12 + st); + lemma_ctmpl_len(t, nl, (h - 1) as nat, ot); + lemma_ctmpl_len(e, nl, (h - 1) as nat, oe); + assert(ot.len() == depth + 1); + assert(oe.len() == depth + 1); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfmemops.rs b/crates/clazel-jit/src/a64/cfmemops.rs new file mode 100644 index 000000000..d42498c42 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfmemops.rs @@ -0,0 +1,1050 @@ +//! **The memory instructions refine LIR** (A64): `bounds_check`, `load` and +//! `store`. Each template ([`mem_code`]) is the operands into registers, the +//! validator's own bounds-check sequence ([`crate::a64::cfchk`]), the access, +//! a `b` over the stub, and the stub (an exit sequence) both of the check's +//! exits go to. The A64 counterpart of [`crate::cfmemops`]. + +#[cfg(verus_only)] +use super::cfalg::*; +#[cfg(verus_only)] +use super::cfchk::*; +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cfexit::*; +#[cfg(verus_only)] +use super::cfleaf::*; +#[cfg(verus_only)] +use super::cfmemrel::*; +#[cfg(verus_only)] +use super::cfmemsteps::*; +#[cfg(verus_only)] +use super::cfop::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cfsteps::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[cfg(verus_only)] +use crate::cfmemacc::{lemma_access_in_regions, load_result, store_result}; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Indexing a five-part concatenation. +pub proof fn lemma_cat5_idx( + pre: Seq, + chk: Seq, + tail: Seq, + bb: isa::Instr, + stub: Seq, +) + ensures + ({ + let w = pre + chk + tail + seq![bb] + stub; + &&& w.len() == pre.len() + chk.len() + tail.len() + 1 + stub.len() + &&& forall|i: int| 0 <= i < pre.len() ==> #[trigger] w[i] == pre[i] + &&& forall|j: int| 0 <= j < chk.len() ==> #[trigger] w[pre.len() + j] == chk[j] + &&& forall|i: int| + 0 <= i < tail.len() ==> #[trigger] w[pre.len() + chk.len() + i] == tail[i] + &&& w[(pre.len() + chk.len() + tail.len()) as int] == bb + &&& forall|i: int| + 0 <= i < stub.len() ==> #[trigger] w[pre.len() + chk.len() + tail.len() + 1 + i] + == stub[i] + }), +{ + let w = pre + chk + tail + seq![bb] + stub; + assert(w.len() == pre.len() + chk.len() + tail.len() + 1 + stub.len()); + assert forall|i: int| 0 <= i < pre.len() implies #[trigger] w[i] == pre[i] by {} + assert forall|j: int| 0 <= j < chk.len() implies #[trigger] w[pre.len() + j] == chk[j] by {} + assert forall|i: int| 0 <= i < tail.len() implies #[trigger] w[pre.len() + chk.len() + i] + == tail[i] by {} + assert forall|i: int| 0 <= i < stub.len() implies #[trigger] w[pre.len() + chk.len() + + tail.len() + 1 + i] == stub[i] by {} +} + +/// The start of the check in a memory instruction's code. +pub open spec fn mem_st(m: MemOp, nl: nat, h: nat) -> int { + mem_pre(m, nl, h).len() as int +} + +/// The word of the stub (and the target of both exits of the check). +pub open spec fn mem_sx(m: MemOp, nl: nat, h: nat) -> int { + mem_st(m, nl, h) + 7 + mem_tail(m, nl, h).len() + 1 +} + +/// **The shape of a memory instruction's code**: where the pieces are. +pub proof fn lemma_mem_shape(m: MemOp, nl: nat, h: nat) + ensures + ({ + let code = mem_code(m, nl, h); + let st = mem_st(m, nl, h); + let sx = mem_sx(m, nl, h); + let tl = mem_tail(m, nl, h).len() as int; + &&& code.len() == sx + 3 + &&& forall|j: int| + 0 <= j < 7 ==> #[trigger] chk_at(code, st, sx, mem_region(m), mem_n(m), j) + &&& forall|i: int| 0 <= i < st ==> #[trigger] code[i] == mem_pre(m, nl, h)[i] + &&& forall|i: int| 0 <= i < tl ==> #[trigger] code[st + 7 + i] == mem_tail(m, nl, h)[i] + &&& code[sx - 1] == b_i(16) + &&& forall|i: int| + 0 <= i < 3 ==> #[trigger] code[sx + i] == exit_seq(mem_exit_code(m))[i] + }), +{ + let pre = mem_pre(m, nl, h); + let chk = mem_check(m, nl, h); + let tail = mem_tail(m, nl, h); + let stub = exit_seq(mem_exit_code(m)); + let bb = b_i(code_len(stub) as int + 4); + assert(chk.len() == 7); + assert(stub.len() == 3); + assert(code_len(stub) == 12); + lemma_cat5_idx(pre, chk, tail, bb, stub); + let code = mem_code(m, nl, h); + assert(code == pre + chk + tail + seq![bb] + stub); + let st = mem_st(m, nl, h); + let sx = mem_sx(m, nl, h); + assert forall|j: int| 0 <= j < 7 implies #[trigger] chk_at( + code, + st, + sx, + mem_region(m), + mem_n(m), + j, + ) by { + assert(code[st + j] == chk[j]); + assert(sx - (st + 2) == mem_r1(m, nl, h)); + assert(sx - (st + 4) == mem_r2(m, nl, h)); + assert(chk[j] == clazel_codecheck::a64::spec::check_instr( + j, + mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)), + )); + } +} + +/// The relation survives a run that changes registers other than `x28`, `x30` +/// and `SP`, and keeps the memory: it, the region table and `preserved_r` all +/// carry over. +pub open spec fn fr5(s: isa::State, s2: isa::State) -> bool { + s2.r.len() == 31 && s.r.len() == 31 && s2.vreg == s.vreg && s2.mem == s.mem && s2.sp == s.sp + && forall|k: int| 5 <= k < 31 ==> #[trigger] s2.r[k] == s.r[k] +} + +/// [`chk_frame`] is [`fr5`]. +pub proof fn lemma_chk_fr5(s: isa::State, s2: isa::State) + requires + chk_frame(s, s2), + ensures + fr5(s, s2), +{ +} + +/// [`fr5`] is transitive. +pub proof fn lemma_fr5_trans(s: isa::State, s1: isa::State, s2: isa::State) + requires + fr5(s, s1), + fr5(s1, s2), + ensures + fr5(s, s2), +{ +} + +/// A [`fr5`] step keeps the relation, the region table and [`preserved_r`]. +pub proof fn lemma_fr5_keep( + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + m2: isa::State, + nl: nat, +) + requires + rel_base(lay, base, a, m, nl), + fr5(m, m2), + ensures + rel_base(lay, base, a, m2, nl), + preserved_r(lay, m, m2), +{ + super::steps::lemma_abi_consts(); + lemma_rel_base_unfold(lay, base, a, m, nl); + assert(m2.vreg.len() == 32); + lemma_rel_base_same(lay, base, a, m, m2, nl); + lemma_preserved_same(lay, m, m2); +} + +/// Words 0 to 8 of a bounds check are the slot load, the check, and the `b`; +/// the stub is the exit sequence. +#[verifier::rlimit(1000)] +pub proof fn lemma_bc_cf(region: usize, size: usize, code: u32, nl: nat, h: nat, offs: Seq) + requires + nl + h <= max_slots(), + 1 <= h, + region < 16, + 1 <= size <= 8, + code < 0x1_0000, + ensures + cf_refines( + Node::Mem(MemOp::BoundsCheck { region, size, code })@, + nl, + h, + offs, + mem_code(MemOp::BoundsCheck { region, size, code }, nl, h), + ), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + let mo = MemOp::BoundsCheck { region, size, code }; + let cd = mem_code(mo, nl, h); + lemma_mem_shape(mo, nl, h); + let c16 = code as u16; + assert(mem_st(mo, nl, h) == 1 && mem_sx(mo, nl, h) == 9); + assert(cd.len() == 12); + assert(mem_exit_code(mo) == c16); + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, cd, m, lir::eval(lay, Node::Mem(mo)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && cf_canon( + m.pc as int, + offs, + cd, + ) implies cf_post( + lay, + base, + nl, + offs, + cd, + m, + lir::eval(lay, Node::Mem(mo)@, a, fuel), + ) by { + let n = a.stack.len(); + if fuel > 0 && n >= 1 && (region as nat) < lay.len() { + match a.stack[(n - 1) as int] { + lir::Val::I64(ix) => { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + let j = (nl + h - 1) as nat; + let b0 = m.pc as int; + assert(ac.stack[(h - 1) as int] == lir::Val::I64(ix)); + assert(slot_val(ac, nl, j) == lir::Val::I64(ix)); + assert(j < nl + ac.stack.len()); + assert(b0 + 48 < 0x1_0000_0000_0000_0000); + // word 0: ldr x1, [slot] + super::rel::lemma_load_rel(lay, ac, m, 1, j, nl); + let m1 = isa::step(m, load_slot(1, j)).unwrap(); + assert(cd[0] == load_slot(1, j)); + lemma_reach_step(cd, b0, m, 0, m1); + assert(m1.r[1] as nat == ix as nat); + // the bound + let k = region as nat; + let bound = lay[k as int].bound; + assert(table_ok(lay, m)); + assert(isa::mem_read(m.mem, ctx_of(m) + abi::bound_off(k as int), 8) == Some( + bound as int, + )); + lemma_read_mem_range(m.mem, ctx_of(m) + abi::bound_off(k as int), 8, bound as int); + assert(vstd::arithmetic::power2::pow2(64) == 0x1_0000_0000_0000_0000nat); + let bnd = bound as u64; + assert(ctx_of(m1) == ctx_of(m)); + assert(m1.mem == m.mem); + lemma_check_run(cd, b0, 1, 9, k, size as nat, m1, ix, bnd); + if ix as int + size as int > bound as int { + let m3 = choose|m3: isa::State| + reaches(cd, b0, m1, m3) && m3.pc as int == b0 + 4 * 9 && chk_frame( + m1, + m3, + ); + lemma_reaches_trans(cd, b0, m, m1, m3); + assert(m3.r.len() == 31 && m3.vreg == m1.vreg && m3.mem == m.mem); + assert(m3.r[28] == m.r[28] && m3.r[30] == m.r[30]); + super::rel::lemma_rel_regs(lay, ac, m1, m3, nl); + let s = a.with_stack(lir::below(a.stack, 1)); + assert((base + a.stack).take((base + s.stack).len() as int) =~= base + + s.stack); + lemma_rel_prefix(lay, a, base + a.stack, base + s.stack, m3, nl); + assert(s.with_stack(base + s.stack) == a.with_stack(base + s.stack)); + let acs = a.with_stack(base + s.stack); + assert(m3.pc as int + code_len(exit_seq(c16)) < isa::p2(64)); + lemma_exit_reach(c16, nl, lay, acs, m3); + let m4 = choose|m4: isa::State| + reaches(exit_seq(c16), m3.pc as int, m3, m4) && exit_done( + m3, + m4, + c16 as nat, + ) && rel(lay, acs, m4, nl) && preserved(m3, m4); + let pre9 = cd.take(9); + assert(cd =~= pre9 + exit_seq(c16) + Seq::::empty()) by { + assert forall|i: int| 0 <= i < 3 implies cd[9 + i] == exit_seq(c16)[i] + by {} + } + assert(code_len(pre9) == 36); + lemma_reaches_embed( + pre9, + exit_seq(c16), + Seq::empty(), + m3.pc as int, + m3, + m4, + ); + assert(m3.pc as int - 36 == b0); + lemma_reaches_trans(cd, b0, m, m3, m4); + // the outcome + assert(lir::eval(lay, Node::Mem(mo)@, a, fuel) == lir::Outcome::Exit( + code as nat, + seq![], + s, + )); + lemma_pr_trans_mem(lay, m, m3, m4); + assert(ctx_of(m3) == ctx_of(m)); + assert(exit_done(m, m4, code as nat)); + lemma_table_pres(lay, m3, m4); + reveal(rel_base); + assert(rel_base(lay, base + Seq::::empty(), s, m4, nl)) by { + assert(base + Seq::::empty() =~= base); + } + assert(ops_in_frame(m4.mem, ctx_of(m), nl, (base + Seq::::empty()).len() + + s.stack.len(), seq![])); + assert(exit_rel(lay, base, s, m4, nl, ctx_of(m), seq![])); + } else { + let m3 = choose|m3: isa::State| + reaches(cd, b0, m1, m3) && m3.pc as int == b0 + 4 * 8 && chk_frame(m1, m3) + && m3.r[1] == ix; + lemma_reaches_trans(cd, b0, m, m1, m3); + assert(cd[8] == b_i(16)); + lemma_step_b(m3, 16); + let m5 = isa::State { pc: (m3.pc as int + 16) as u64, ..m3 }; + lemma_reach_step(cd, b0, m3, 8, m5); + lemma_reaches_trans(cd, b0, m, m3, m5); + assert(m5.pc as int == b0 + 48); + assert(m5.r[28] == m.r[28] && m5.r[30] == m.r[30] && m5.r.len() == 31); + assert(fr5(m, m5)); + lemma_fr5_keep(lay, base, a, m, m5, nl); + assert(lir::eval(lay, Node::Mem(mo)@, a, fuel) == lir::Outcome::Normal(a)); + } + }, + _ => {}, + } + } + } +} + +/// [`preserved`] twice in the weaker, memory form. +pub proof fn lemma_pr_trans_mem(lay: lir::Layout, m: isa::State, m3: isa::State, m4: isa::State) + requires + m.r.len() == 31, + m3.r.len() == 31, + m3.mem == m.mem, + m3.r[28] == m.r[28], + preserved(m3, m4), + ensures + preserved_mem_r(lay, m, m4), +{ + super::steps::lemma_abi_consts(); + assert(ctx_of(m3) == ctx_of(m)); +} + + +/// **The region access facts**: with the index `ix` within the bound, every byte +/// the access touches is a region byte, mapped, and below `2^56`; the effective +/// address is `base + ix`. +pub proof fn lemma_acc_facts( + lay: lir::Layout, + lm: lir::Mem, + ms: isa::State, + k: nat, + ix: u64, + sz: nat, +) + requires + isa::wf(ms), + lir::layout_wf(lay), + mem_rel(lay, lm, ms.mem), + k < lay.len(), + 1 <= sz, + ix as nat + sz <= lay[k as int].bound, + ensures + lay[k as int].base + ix as nat + sz < 0x1_0000_0000_0000_0000, + forall|i: nat| i < sz ==> in_regions(lay, #[trigger] (lay[k as int].base + ix as nat + i)), + forall|i: int| + 0 <= i < sz ==> ms.mem.dom().contains( + #[trigger] ((lay[k as int].base + ix as nat) as int + i), + ), +{ + super::memlemmas::lemma_p2_consts(); + lemma_access_in_regions(lay, k, ix as nat, sz); + let a_ = lay[k as int].base + ix as nat; + assert forall|i: int| 0 <= i < sz implies ms.mem.dom().contains(#[trigger] (a_ as int + i)) by { + assert(in_regions(lay, a_ + i as nat)); + } + assert(ms.mem.dom().contains(a_ as int + (sz - 1) as int)); +} + +/// **The loads before a `load`'s check**: the index into `x1` and the region's +/// base into `x3`. +#[verifier::spinoff_prover] +#[verifier::rlimit(800)] +pub proof fn lemma_load_prefix( + lay: lir::Layout, + ac: lir::St, + m: isa::State, + nl: nat, + h: nat, + ty: MTy, + region: usize, + ix: u64, +) + requires + nl + h <= max_slots(), + 1 <= h, + region < 16, + (region as nat) < lay.len(), + rel(lay, ac, m, nl), + table_ok(lay, m), + ac.stack.len() == h, + ac.stack[(h - 1) as int] == lir::Val::I64(ix), + m.pc as int + 60 < 0x1_0000_0000_0000_0000, + ensures + ({ + let cd = mem_code(MemOp::Load { region, ty }, nl, h); + exists|m2: isa::State| + #![trigger reaches(cd, m.pc as int, m, m2)] + reaches(cd, m.pc as int, m, m2) && m2.pc as int == m.pc as int + 8 && fr5(m, m2) + && m2.r[1] == ix && m2.r[3] == lay[region as int].base as u64 && isa::wf(m2) + && ctx_ok(m2) && rel(lay, ac, m2, nl) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + let mo = MemOp::Load { region, ty }; + let cd = mem_code(mo, nl, h); + lemma_mem_shape(mo, nl, h); + assert(mem_st(mo, nl, h) == 2); + let j = (nl + h - 1) as nat; + let k = region as nat; + let b0 = m.pc as int; + let base_r = lay[k as int].base; + assert(slot_val(ac, nl, j) == lir::Val::I64(ix)); + assert(j < nl + ac.stack.len()); + super::rel::lemma_load_rel(lay, ac, m, 1, j, nl); + let m1 = isa::step(m, load_slot(1, j)).unwrap(); + assert(cd[0] == load_slot(1, j)); + lemma_reach_step(cd, b0, m, 0, m1); + assert(ctx_of(m1) == ctx_of(m) && m1.mem == m.mem); + assert(isa::mem_read(m.mem, ctx_of(m) + abi::base_off(k as int), 8) == Some(base_r as int)); + lemma_read_mem_range(m.mem, ctx_of(m) + abi::base_off(k as int), 8, base_r as int); + assert(vstd::arithmetic::power2::pow2(64) == 0x1_0000_0000_0000_0000nat); + assert(cd[1] == base_ld(k)); + assert((abi::base_off(k as int) / 8) as u16 as int * 8 == abi::base_off(k as int)); + assert(isa::wf(m1) && ctx_ok(m1)); + lemma_step_ctx_ld(m1, 3, (abi::base_off(k as int) / 8) as u16, base_r); + let m2 = isa::step(m1, base_ld(k)).unwrap(); + lemma_reach_step(cd, b0, m1, 1, m2); + lemma_reaches_trans(cd, b0, m, m1, m2); + assert(m2.r[1] == ix && m2.r[3] == base_r as u64 && m2.pc as int == b0 + 8); + assert(isa::wf(m2) && ctx_ok(m2)); + assert(fr5(m, m2)); + super::rel::lemma_rel_regs(lay, ac, m, m2, nl); +} + +/// **The access of a `load` and what follows**: from the state after the check +/// (at the `ldr x0, [x3, x1]`), the access, the store of the result to the slot, +/// and the `b` over the stub reach the end of the code, related to LIR's +/// result. +#[verifier::spinoff_prover] +#[verifier::rlimit(1500)] +pub proof fn lemma_load_finish( + lay: lir::Layout, + ac: lir::St, + m: isa::State, + m3: isa::State, + nl: nat, + h: nat, + ty: MTy, + region: usize, + ix: u64, +) + requires + nl + h <= max_slots(), + 1 <= h, + region < 16, + (region as nat) < lay.len(), + rel(lay, ac, m, nl), + ac.stack.len() == h, + ac.stack[(h - 1) as int] == lir::Val::I64(ix), + ix as nat + ty.size() <= lay[region as int].bound, + m.pc as int + 60 < 0x1_0000_0000_0000_0000, + m3.pc as int == m.pc as int + 36, + fr5(m, m3), + m3.r[1] == ix, + m3.r[3] == lay[region as int].base as u64, + isa::wf(m3), + ensures + ({ + let cd = mem_code(MemOp::Load { region, ty }, nl, h); + exists|m6: isa::State| + #![trigger reaches(cd, m.pc as int, m3, m6)] + reaches(cd, m.pc as int, m3, m6) && m6.pc as int == m.pc as int + 60 && rel( + lay, + load_result(lay, ac, region as nat, ty, ix), + m6, + nl, + ) && preserved_r(lay, m, m6) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + let mo = MemOp::Load { region, ty }; + let cd = mem_code(mo, nl, h); + lemma_mem_shape(mo, nl, h); + let sf = mty_sf(ty); + let sz = ty.size(); + let j = (nl + h - 1) as nat; + let k = region as nat; + let b0 = m.pc as int; + let r = lay[k as int]; + let bound = r.bound; + assert(mem_tail(mo, nl, h).len() == 2); + assert(m3.mem == m.mem && m3.r.len() == 31); + lemma_acc_facts(lay, ac.mem, m3, k, ix, sz); + let a_ = r.base + ix as nat; + assert(m3.r[3] as int + m3.r[1] as int == a_ as int); + assert(mem_rel(lay, ac.mem, m3.mem)); + lemma_read_rel(lay, ac.mem, m3.mem, a_, sz); + let vv = lir::read_le(ac.mem, a_, sz); + crate::cfmemrel::lemma_read_le_range(ac.mem, a_, sz); + assert(cd[9] == isa::Instr::LoadStoreReg { sf, load: true, t: 0, n: 3, m: 1, s: false }); + assert(sf_bytes(sf) == sz); + lemma_step_ld_reg(m3, sf, 0, 3, 1, vv); + let m4 = isa::step(m3, cd[9]).unwrap(); + lemma_reach_step(cd, b0, m3, 9, m4); + let res = load_result(lay, ac, k, ty, ix); + let val = lir::val_of_bits(ty.lir_ty(), vv); + assert(lir::masked(bound, ix as nat) == ix as nat); + assert(vv < vstd::arithmetic::power2::pow2(8 * sz)); + assert(lir::val_bits(val) == vv) by { + match ty { + MTy::I32 => { + assert(vstd::arithmetic::power2::pow2(32) == 0x1_0000_0000nat); + assert((vv as u32) as nat == vv); + }, + MTy::I64 => { + assert((vv as u64) as nat == vv); + }, + MTy::F64 => { + assert((vv as u64) as nat == vv); + }, + } + } + assert(res.stack =~= lir::below(ac.stack, 1).push(val)); + assert(res.stack.len() == h); + assert(slot_val(res, nl, j) == val); + assert(crate::rel::slots_agree_except(ac, res, nl, j)) by { + assert forall|jj: nat| + #![trigger lir::val_bits(slot_val(res, nl, jj))] + jj < nl + res.stack.len() && jj != j implies jj < nl + ac.stack.len() + && lir::val_bits(slot_val(res, nl, jj)) == lir::val_bits(slot_val(ac, nl, jj)) by { + if jj >= nl { + assert(res.stack[(jj - nl) as int] == ac.stack[(jj - nl) as int]); + } + } + } + assert(m4.r.len() == 31 && m4.vreg == m3.vreg && m4.mem == m.mem); + assert(m4.r[28] == m.r[28] && m4.r[30] == m.r[30] && m4.sp == m.sp); + super::rel::lemma_rel_regs(lay, ac, m, m4, nl); + assert(m4.r[0] as nat == lir::val_bits(slot_val(res, nl, j))); + assert(cd[10] == store_slot(j, 0)); + assert(m4.pc as int == b0 + 40); + super::rel::lemma_store_finish(lay, ac, res, m, m4, nl, j, 0, vv); + let m5 = isa::step(m4, store_slot(j, 0)).unwrap(); + lemma_reach_step(cd, b0, m4, 10, m5); + assert(cd[11] == b_i(16)); + lemma_step_b(m5, 16); + let m6 = isa::State { pc: (m5.pc as int + 16) as u64, ..m5 }; + lemma_reach_step(cd, b0, m5, 11, m6); + lemma_reaches_trans(cd, b0, m3, m4, m5); + lemma_reaches_trans(cd, b0, m3, m5, m6); + assert(m6.pc as int == b0 + 60); + super::rel::lemma_rel_regs(lay, res, m5, m6, nl); + lemma_preserved_to_r(lay, m, m5); + lemma_preserved_same(lay, m5, m6); + lemma_pr_trans(lay, m, m5, m6); +} + +/// **`t.load k`**: the template refines it. +#[verifier::rlimit(1000)] +pub proof fn lemma_load_cf(region: usize, ty: MTy, nl: nat, h: nat, offs: Seq) + requires + nl + h <= max_slots(), + 1 <= h, + region < 16, + ensures + cf_refines( + Node::Mem(MemOp::Load { region, ty })@, + nl, + h, + offs, + mem_code(MemOp::Load { region, ty }, nl, h), + ), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + let mo = MemOp::Load { region, ty }; + let cd = mem_code(mo, nl, h); + lemma_mem_shape(mo, nl, h); + assert(mem_st(mo, nl, h) == 2 && mem_sx(mo, nl, h) == 12); + assert(cd.len() == 15); + let sz = ty.size(); + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, cd, m, lir::eval(lay, Node::Mem(mo)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && cf_canon( + m.pc as int, + offs, + cd, + ) implies cf_post( + lay, + base, + nl, + offs, + cd, + m, + lir::eval(lay, Node::Mem(mo)@, a, fuel), + ) by { + let n = a.stack.len(); + if fuel > 0 && n >= 1 && (region as nat) < lay.len() { + match a.stack[(n - 1) as int] { + lir::Val::I64(ix) => { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + let k = region as nat; + let b0 = m.pc as int; + let r = lay[k as int]; + let bound = r.bound; + assert(ac.stack[(h - 1) as int] == lir::Val::I64(ix)); + assert(b0 + 60 < 0x1_0000_0000_0000_0000); + if ix as nat + sz <= bound { + lemma_load_prefix(lay, ac, m, nl, h, ty, region, ix); + let m2 = choose|m2: isa::State| + reaches(cd, b0, m, m2) && m2.pc as int == b0 + 8 && fr5(m, m2) + && m2.r[1] == ix && m2.r[3] == r.base as u64 && isa::wf(m2) + && ctx_ok(m2) && rel(lay, ac, m2, nl); + lemma_read_mem_range( + m.mem, + ctx_of(m) + abi::bound_off(k as int), + 8, + bound as int, + ); + assert(vstd::arithmetic::power2::pow2(64) == 0x1_0000_0000_0000_0000nat); + assert(ctx_of(m2) == ctx_of(m) && m2.mem == m.mem); + lemma_check_run(cd, b0, 2, 12, k, sz, m2, ix, bound as u64); + let m3 = choose|m3: isa::State| + reaches(cd, b0, m2, m3) && m3.pc as int == b0 + 4 * 9 && chk_frame( + m2, + m3, + ) && m3.r[1] == ix; + lemma_reaches_trans(cd, b0, m, m2, m3); + lemma_chk_fr5(m2, m3); + lemma_fr5_trans(m, m2, m3); + assert(m3.r[3] == m2.r[3]); + assert(isa::wf(m3)); + lemma_load_finish(lay, ac, m, m3, nl, h, ty, region, ix); + let m6 = choose|m6: isa::State| + reaches(cd, b0, m3, m6) && m6.pc as int == b0 + 60 && rel( + lay, + load_result(lay, ac, k, ty, ix), + m6, + nl, + ) && preserved_r(lay, m, m6); + lemma_reaches_trans(cd, b0, m, m3, m6); + lemma_table_pres(lay, m, m6); + let val = lir::val_of_bits( + ty.lir_ty(), + lir::read_le(ac.mem, r.base + ix as nat, sz), + ); + let a2 = a.with_stack(lir::below(a.stack, 1).push(val)); + assert(lir::masked(bound, ix as nat) == ix as nat); + assert(lir::eval(lay, Node::Mem(mo)@, a, fuel) == lir::Outcome::Normal(a2)); + assert((base + lir::below(a.stack, 1).push(val)) =~= lir::below( + base + a.stack, + 1, + ).push(val)); + assert(a2.with_stack(base + a2.stack) == load_result(lay, ac, k, ty, ix)); + reveal(rel_base); + assert(rel_base(lay, base, a2, m6, nl)); + } + }, + _ => {}, + } + } + } +} + +/// **The loads before a `store`'s check**: the value into `x4`, the index into +/// `x1`, the region's base into `x3`. +#[verifier::spinoff_prover] +#[verifier::rlimit(800)] +pub proof fn lemma_store_prefix( + lay: lir::Layout, + ac: lir::St, + m: isa::State, + nl: nat, + h: nat, + ty: MTy, + region: usize, + ix: u64, + v: lir::Val, +) + requires + nl + h <= max_slots(), + 2 <= h, + region < 16, + (region as nat) < lay.len(), + rel(lay, ac, m, nl), + table_ok(lay, m), + ac.stack.len() == h, + ac.stack[(h - 1) as int] == v, + ac.stack[(h - 2) as int] == lir::Val::I64(ix), + m.pc as int + 60 < 0x1_0000_0000_0000_0000, + ensures + ({ + let cd = mem_code(MemOp::Store { region, ty }, nl, h); + exists|m2: isa::State| + #![trigger reaches(cd, m.pc as int, m, m2)] + reaches(cd, m.pc as int, m, m2) && m2.pc as int == m.pc as int + 12 && fr5(m, m2) + && m2.r[1] == ix && m2.r[3] == lay[region as int].base as u64 && m2.r[4] as nat + == lir::val_bits(v) && isa::wf(m2) && ctx_ok(m2) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + let mo = MemOp::Store { region, ty }; + let cd = mem_code(mo, nl, h); + lemma_mem_shape(mo, nl, h); + assert(mem_st(mo, nl, h) == 3); + let jv = (nl + h - 1) as nat; + let ji = (nl + h - 2) as nat; + let k = region as nat; + let b0 = m.pc as int; + let base_r = lay[k as int].base; + assert(slot_val(ac, nl, jv) == v); + assert(slot_val(ac, nl, ji) == lir::Val::I64(ix)); + super::rel::lemma_load_rel(lay, ac, m, 4, jv, nl); + let m1 = isa::step(m, load_slot(4, jv)).unwrap(); + assert(cd[0] == load_slot(4, jv)); + lemma_reach_step(cd, b0, m, 0, m1); + super::rel::lemma_load_rel(lay, ac, m1, 1, ji, nl); + let m2 = isa::step(m1, load_slot(1, ji)).unwrap(); + assert(cd[1] == load_slot(1, ji)); + lemma_reach_step(cd, b0, m1, 1, m2); + lemma_reaches_trans(cd, b0, m, m1, m2); + assert(ctx_of(m2) == ctx_of(m) && m2.mem == m.mem); + assert(isa::mem_read(m.mem, ctx_of(m) + abi::base_off(k as int), 8) == Some(base_r as int)); + lemma_read_mem_range(m.mem, ctx_of(m) + abi::base_off(k as int), 8, base_r as int); + assert(vstd::arithmetic::power2::pow2(64) == 0x1_0000_0000_0000_0000nat); + assert(cd[2] == base_ld(k)); + assert((abi::base_off(k as int) / 8) as u16 as int * 8 == abi::base_off(k as int)); + assert(isa::wf(m2) && ctx_ok(m2)); + lemma_step_ctx_ld(m2, 3, (abi::base_off(k as int) / 8) as u16, base_r); + let m2b = isa::step(m2, base_ld(k)).unwrap(); + lemma_reach_step(cd, b0, m2, 2, m2b); + lemma_reaches_trans(cd, b0, m, m2, m2b); + assert(m2b.r[1] == ix && m2b.r[3] == base_r as u64 && m2b.pc as int == b0 + 12); + assert(m2b.r[4] as nat == lir::val_bits(v)); + assert(isa::wf(m2b) && ctx_ok(m2b)); + assert(fr5(m, m2b)); +} + +/// **The access of a `store` and what follows**: the store through `[x3, x1]` +/// and the `b` over the stub reach the end of the code, related to LIR's +/// result; only region bytes changed. +#[verifier::spinoff_prover] +#[verifier::rlimit(2000)] +pub proof fn lemma_store_finish_ls( + lay: lir::Layout, + ac: lir::St, + m: isa::State, + m3: isa::State, + nl: nat, + h: nat, + ty: MTy, + region: usize, + ix: u64, + v: lir::Val, +) + requires + nl + h <= max_slots(), + 2 <= h, + region < 16, + (region as nat) < lay.len(), + rel(lay, ac, m, nl), + ac.stack.len() == h, + ac.stack[(h - 1) as int] == v, + ac.stack[(h - 2) as int] == lir::Val::I64(ix), + lir::val_has_base_ty(v, ty.lir_ty()), + ix as nat + ty.size() <= lay[region as int].bound, + m.pc as int + 60 < 0x1_0000_0000_0000_0000, + m3.pc as int == m.pc as int + 40, + fr5(m, m3), + m3.r[1] == ix, + m3.r[3] == lay[region as int].base as u64, + m3.r[4] as nat == lir::val_bits(v), + isa::wf(m3), + ensures + ({ + let cd = mem_code(MemOp::Store { region, ty }, nl, h); + exists|m5: isa::State| + #![trigger reaches(cd, m.pc as int, m3, m5)] + reaches(cd, m.pc as int, m3, m5) && m5.pc as int == m.pc as int + 60 && rel( + lay, + store_result(lay, ac, region as nat, ty, ix, v), + m5, + nl, + ) && preserved_r(lay, m, m5) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + let mo = MemOp::Store { region, ty }; + let cd = mem_code(mo, nl, h); + lemma_mem_shape(mo, nl, h); + let sf = mty_sf(ty); + let sz = ty.size(); + let k = region as nat; + let b0 = m.pc as int; + let r = lay[k as int]; + let bound = r.bound; + assert(mem_tail(mo, nl, h).len() == 1); + assert(m3.mem == m.mem && m3.r.len() == 31); + lemma_acc_facts(lay, ac.mem, m3, k, ix, sz); + let a_ = r.base + ix as nat; + assert(m3.r[3] as int + m3.r[1] as int == a_ as int); + assert(cd[10] == isa::Instr::LoadStoreReg { sf, load: false, t: 4, n: 3, m: 1, s: false }); + assert(sf_bytes(sf) == sz); + lemma_step_st_reg(m3, sf, 4, 3, 1); + let vb = lir::val_bits(v); + let w = isa::trunc(isa::datasize(sf), m3.r[4] as int); + assert(vb < vstd::arithmetic::power2::pow2(8 * sz)) by { + match v { + lir::Val::I32(x) => { + assert(ty is I32); + assert(vstd::arithmetic::power2::pow2(32) == 0x1_0000_0000nat); + }, + lir::Val::I64(x) => { + assert(!(ty is I32)); + }, + lir::Val::F64(x) => { + assert(!(ty is I32)); + }, + } + } + assert(w == vb as int) by { + assert(isa::p2(isa::datasize(sf)) == vstd::arithmetic::power2::pow2(8 * sz) as int); + vstd::arithmetic::div_mod::lemma_small_mod(vb, isa::p2(isa::datasize(sf)) as nat); + } + let mem2 = isa::mem_set(m3.mem, a_ as int, sz, w).unwrap(); + let m4 = isa::step(m3, cd[10]).unwrap(); + assert(m4 == isa::State { mem: mem2, pc: (m3.pc + 4) as u64, ..m3 }); + lemma_reach_step(cd, b0, m3, 10, m4); + super::memlemmas::lemma_mem_set_frame(m3.mem, a_ as int, sz, w, mem2); + lemma_write_rel(lay, ac.mem, m3.mem, mem2, a_, sz, vb); + let res = store_result(lay, ac, k, ty, ix, v); + assert(lir::masked(bound, ix as nat) == ix as nat); + let ctxb = ctx_of(m); + assert(ctx_of(m4) == ctxb); + assert forall|b: int| a_ as int <= b < a_ as int + sz implies !in_exit_area(ctxb, b) + && in_regions_i(lay, b) by { + assert(in_regions(lay, a_ + (b - a_ as int) as nat)); + } + assert(preserved_mem_r(lay, m, m4)) by { + assert(m4.r.len() == 31 && m4.r[28] == m.r[28]); + assert forall|b: int| + #[trigger] m.mem.dom().contains(b) && !in_exit_area(ctxb, b) && !in_regions_i( + lay, + b, + ) implies m4.mem[b] == m.mem[b] by { + assert(!(a_ as int <= b && b < a_ as int + sz)); + } + } + assert(rel(lay, res, m4, nl)) by { + assert(isa::wf(m4)) by { + assert(m4.mem.dom() =~= m3.mem.dom()); + } + assert(ctx_ok(m4)); + assert(exit_area_mapped(m4.mem, ctxb)); + assert(res.locals.len() == nl); + assert(res.stack.len() == h - 2); + assert(res.mem == lir::write_le(ac.mem, a_, vb, sz)); + assert forall|jj: nat| jj < nl + res.stack.len() implies #[trigger] slot_holds( + m4.mem, + ctxb, + jj, + lir::val_bits(slot_val(res, nl, jj)), + ) by { + assert(slot_val(res, nl, jj) == slot_val(ac, nl, jj)) by { + if jj >= nl { + assert(res.stack[(jj - nl) as int] == ac.stack[(jj - nl) as int]); + } + } + assert(slot_holds(m.mem, ctxb, jj, lir::val_bits(slot_val(ac, nl, jj)))); + let sa = ctxb + slot_disp(jj); + assert(jj < max_slots()); + super::steps::lemma_slot_addr(m, jj); + assert forall|i: int| 0 <= i < 8 implies (m3.mem.dom().contains(#[trigger] (sa + i)) + == m4.mem.dom().contains(sa + i)) && (m3.mem.dom().contains(sa + i) + ==> m3.mem[sa + i] == m4.mem[sa + i]) by { + super::rel::lemma_slot_in_exit_area(ctxb, jj, sa + i); + assert(!(a_ as int <= sa + i && sa + i < a_ as int + sz)); + } + super::memlemmas::lemma_mem_read_congr(m3.mem, m4.mem, sa, 8); + } + } + assert(m4.pc as int == b0 + 44); + assert(cd[11] == b_i(16)); + lemma_step_b(m4, 16); + let m5 = isa::State { pc: (m4.pc as int + 16) as u64, ..m4 }; + lemma_reach_step(cd, b0, m4, 11, m5); + lemma_reaches_trans(cd, b0, m3, m4, m5); + assert(m5.pc as int == b0 + 60); + assert(m5.r == m4.r && m5.mem == m4.mem && m5.vreg == m4.vreg); + super::rel::lemma_rel_regs(lay, res, m4, m5, nl); + assert(preserved_mem_r(lay, m, m5)); + assert(m5.r[30] == m.r[30] && m5.sp == m.sp); +} + +/// **`t.store k`**: the template refines it. +#[verifier::rlimit(1000)] +pub proof fn lemma_store_cf(region: usize, ty: MTy, nl: nat, h: nat, offs: Seq) + requires + nl + h <= max_slots(), + 2 <= h, + region < 16, + ensures + cf_refines( + Node::Mem(MemOp::Store { region, ty })@, + nl, + h, + offs, + mem_code(MemOp::Store { region, ty }, nl, h), + ), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + let mo = MemOp::Store { region, ty }; + let cd = mem_code(mo, nl, h); + lemma_mem_shape(mo, nl, h); + assert(mem_st(mo, nl, h) == 3 && mem_sx(mo, nl, h) == 12); + assert(cd.len() == 15); + let sz = ty.size(); + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, cd, m, lir::eval(lay, Node::Mem(mo)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && cf_canon( + m.pc as int, + offs, + cd, + ) implies cf_post( + lay, + base, + nl, + offs, + cd, + m, + lir::eval(lay, Node::Mem(mo)@, a, fuel), + ) by { + let n = a.stack.len(); + if fuel > 0 && n >= 2 && (region as nat) < lay.len() && lir::val_has_base_ty( + a.stack[(n - 1) as int], + ty.lir_ty(), + ) { + match a.stack[(n - 2) as int] { + lir::Val::I64(ix) => { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + let k = region as nat; + let b0 = m.pc as int; + let r = lay[k as int]; + let bound = r.bound; + let v = a.stack[(n - 1) as int]; + assert(ac.stack[(h - 1) as int] == v); + assert(ac.stack[(h - 2) as int] == lir::Val::I64(ix)); + assert(b0 + 60 < 0x1_0000_0000_0000_0000); + if ix as nat + sz <= bound { + lemma_store_prefix(lay, ac, m, nl, h, ty, region, ix, v); + let m2 = choose|m2: isa::State| + reaches(cd, b0, m, m2) && m2.pc as int == b0 + 12 && fr5(m, m2) + && m2.r[1] == ix && m2.r[3] == r.base as u64 && m2.r[4] as nat + == lir::val_bits(v) && isa::wf(m2) && ctx_ok(m2); + lemma_read_mem_range( + m.mem, + ctx_of(m) + abi::bound_off(k as int), + 8, + bound as int, + ); + assert(vstd::arithmetic::power2::pow2(64) == 0x1_0000_0000_0000_0000nat); + assert(ctx_of(m2) == ctx_of(m) && m2.mem == m.mem); + lemma_check_run(cd, b0, 3, 12, k, sz, m2, ix, bound as u64); + let m3 = choose|m3: isa::State| + reaches(cd, b0, m2, m3) && m3.pc as int == b0 + 4 * 10 && chk_frame( + m2, + m3, + ) && m3.r[1] == ix; + lemma_reaches_trans(cd, b0, m, m2, m3); + lemma_chk_fr5(m2, m3); + lemma_fr5_trans(m, m2, m3); + assert(m3.r[3] == m2.r[3] && m3.r[4] == m2.r[4]); + assert(isa::wf(m3)); + lemma_store_finish_ls(lay, ac, m, m3, nl, h, ty, region, ix, v); + let m5 = choose|m5: isa::State| + reaches(cd, b0, m3, m5) && m5.pc as int == b0 + 60 && rel( + lay, + store_result(lay, ac, k, ty, ix, v), + m5, + nl, + ) && preserved_r(lay, m, m5); + lemma_reaches_trans(cd, b0, m, m3, m5); + lemma_table_pres(lay, m, m5); + let a_ = r.base + ix as nat; + let a2 = a.with_stack(lir::below(a.stack, 2)); + let a2m = a2.with_mem(lir::write_le(a.mem, a_, lir::val_bits(v), sz)); + assert(lir::masked(bound, ix as nat) == ix as nat); + assert(lir::eval(lay, Node::Mem(mo)@, a, fuel) == lir::Outcome::Normal(a2m)); + assert((base + lir::below(a.stack, 2)) =~= lir::below(base + a.stack, 2)); + assert(a2m.with_stack(base + a2m.stack) == store_result( + lay, + ac, + k, + ty, + ix, + v, + )); + reveal(rel_base); + assert(rel_base(lay, base, a2m, m5, nl)); + } + }, + _ => {}, + } + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfmemrel.rs b/crates/clazel-jit/src/a64/cfmemrel.rs new file mode 100644 index 000000000..6f574ea35 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfmemrel.rs @@ -0,0 +1,141 @@ +//! `mem_rel` used: a machine read of bytes inside regions is LIR's `read_le` of +//! its memory, and a machine write of them gives a memory related to LIR's +//! `write_le`. The A64 counterpart of [`crate::cfmemrel`] (whose LIR-only +//! lemmas, [`crate::cfmemrel::lemma_write_le_byte`] and the rest, are shared). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfmemrel::{bytek, lemma_write_le_byte}; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A byte of a region of a well-formed layout is a machine address. +pub proof fn lemma_region_lt(lay: lir::Layout, a: nat) + requires + lir::layout_wf(lay), + in_regions(lay, a), + ensures + a < 0x1_0000_0000_0000_0000, +{ + let r = choose|r: int| 0 <= r < lay.len() && lay[r].base <= a && a < lay[r].base + lay[r].bound; + assert(lay[r].base + lay[r].bound <= 0x1_0000_0000_0000_0000); +} + +/// `mem_read` is below `2^(8n)`. +pub proof fn lemma_read_mem_range(mm: Map, a: int, n: nat, v: int) + requires + isa::mem_read(mm, a, n) == Some(v), + ensures + 0 <= v < vstd::arithmetic::power2::pow2(8 * n) as int, + decreases n, +{ + vstd::arithmetic::power2::lemma2_to64(); + if n > 0 { + let n1 = (n - 1) as nat; + match isa::mem_read(mm, a + 1, n1) { + Some(rest) => { + lemma_read_mem_range(mm, a + 1, n1, rest); + vstd::arithmetic::power2::lemma_pow2_adds(8, 8 * n1); + assert(8 * n == 8 + 8 * n1); + }, + None => {}, + } + } +} + +/// **A read of bytes inside regions is LIR's read.** +pub proof fn lemma_read_rel(lay: lir::Layout, lm: lir::Mem, mm: Map, a: nat, n: nat) + requires + lir::layout_wf(lay), + mem_rel(lay, lm, mm), + forall|i: nat| i < n ==> in_regions(lay, #[trigger] (a + i)), + ensures + isa::mem_read(mm, a as int, n) == Some(lir::read_le(lm, a, n) as int), + decreases n, +{ + if n > 0 { + let n1 = (n - 1) as nat; + assert(in_regions(lay, a + 0)); + lemma_region_lt(lay, a); + super::memlemmas::lemma_trunc64(a as int); + assert forall|i: nat| i < n1 implies in_regions(lay, #[trigger] (a + 1 + i)) by { + assert(in_regions(lay, a + (i + 1))); + } + lemma_read_rel(lay, lm, mm, a + 1, n1); + assert((a + 1) as int == a as int + 1); + assert(mm.dom().contains(a as int) && mm[a as int] == lir::byte_at(lm, a)); + } +} + +/// `mem_set` puts byte `b - a` of `v` at `b` inside the range. +pub proof fn lemma_write_mem_byte( + mm: Map, + a: int, + n: nat, + v: int, + mm2: Map, + b: int, +) + requires + 0 <= v, + 0 <= a, + a + n < 0x1_0000_0000_0000_0000, + isa::mem_set(mm, a, n, v) == Some(mm2), + a <= b < a + n, + ensures + mm2[b] == bytek(v as nat, (b - a) as nat) as u8, + decreases n, +{ + super::memlemmas::lemma_trunc64(a); + let n1 = (n - 1) as nat; + match isa::mem_set(mm, a + 1, n1, v / 256) { + Some(m3) => { + assert(mm2 == m3.insert(a, (v % 256) as u8)); + if b == a { + } else { + lemma_write_mem_byte(mm, a + 1, n1, v / 256, m3, b); + assert(bytek(v as nat, (b - a) as nat) == bytek( + (v / 256) as nat, + (b - (a + 1)) as nat, + )); + } + }, + None => {}, + } +} + +/// **A write of bytes inside regions keeps the memories related.** The machine +/// writes the low `n` bytes of `v`, LIR the same. +pub proof fn lemma_write_rel( + lay: lir::Layout, + lm: lir::Mem, + mm: Map, + mm2: Map, + a: nat, + n: nat, + v: nat, +) + requires + lir::layout_wf(lay), + mem_rel(lay, lm, mm), + forall|i: nat| i < n ==> in_regions(lay, #[trigger] (a + i)), + isa::mem_set(mm, a as int, n, v as int) == Some(mm2), + a + n < 0x1_0000_0000_0000_0000, + ensures + mem_rel(lay, lir::write_le(lm, a, v, n), mm2), +{ + super::memlemmas::lemma_mem_set_frame(mm, a as int, n, v as int, mm2); + assert forall|b: nat| #[trigger] in_regions(lay, b) implies mm2.dom().contains(b as int) + && mm2[b as int] == lir::byte_at(lir::write_le(lm, a, v, n), b) by { + assert(mm.dom().contains(b as int) && mm[b as int] == lir::byte_at(lm, b)); + lemma_write_le_byte(lm, a, v, n, b); + if a <= b && b < a + n { + lemma_write_mem_byte(mm, a as int, n, v as int, mm2, b as int); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfmemsteps.rs b/crates/clazel-jit/src/a64/cfmemsteps.rs new file mode 100644 index 000000000..686dc3bd1 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfmemsteps.rs @@ -0,0 +1,116 @@ +//! One step of the two region-access instruction forms the memory templates +//! use: `ldr`/`str` (register offset, `UXTX`, no shift) of a W or X register, +//! `[xn, xm]`. The A64 counterpart of [`crate::cfmemsteps`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The bytes an access of width `sf` touches. +pub open spec fn sf_bytes(sf: isa::Width) -> nat { + match sf { + isa::Width::W32 => 4, + isa::Width::W64 => 8, + } +} + +/// `ldr wt/xt, [xn, xm]` loads the `sf_bytes` bytes at `xn + xm` (zero-extended) +/// into `xt`, advances `pc` by 4, and changes nothing else. +pub proof fn lemma_step_ld_reg(m: isa::State, sf: isa::Width, t: u8, n: u8, mr: u8, v: nat) + requires + isa::wf(m), + t < 31, + n < 31, + mr < 31, + 0 <= m.r[n as int] as int + m.r[mr as int] as int, + m.r[n as int] as int + m.r[mr as int] as int + sf_bytes(sf) < 0x1_0000_0000_0000_0000, + isa::mem_read( + m.mem, + m.r[n as int] as int + m.r[mr as int] as int, + sf_bytes(sf), + ) == Some(v as int), + m.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step( + m, + isa::Instr::LoadStoreReg { sf, load: true, t, n, m: mr, s: false }, + ) == Some(isa::State { r: m.r.update(t as int, v as u64), pc: (m.pc + 4) as u64, ..m }), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_x_read64(m, n); + super::steps::lemma_x_read64(m, mr); + let addr = m.r[n as int] as int + m.r[mr as int] as int; + super::memlemmas::lemma_trunc64(addr); + super::memlemmas::lemma_trunc64(m.pc as int + 4); + super::memlemmas::lemma_trunc64(m.r[mr as int] as int); + let i = isa::Instr::LoadStoreReg { sf, load: true, t, n, m: mr, s: false }; + let ds = isa::datasize(sf); + assert(ds / 8 == sf_bytes(sf)); + assert(isa::extend_reg_uxtx(m, mr, 0) == m.r[mr as int] as int) by { + assert(isa::p2(0) == 1); + assert(isa::trunc(64, m.r[mr as int] as int) == m.r[mr as int] as int); + } + super::cfmemrel::lemma_read_mem_range(m.mem, addr, sf_bytes(sf), v as int); + assert(isa::trunc(ds, v as int) == v as int) by { + vstd::arithmetic::power2::lemma2_to64(); + assert(isa::p2(ds) == vstd::arithmetic::power2::pow2(8 * sf_bytes(sf)) as int); + vstd::arithmetic::div_mod::lemma_small_mod(v, isa::p2(ds) as nat); + } + assert(isa::execute(m, i) == Some(isa::x_set(m, t, ds, v as int))); + assert(isa::x_set(m, t, ds, v as int) == isa::State { r: m.r.update(t as int, v as u64), ..m }); +} + +/// `str wt/xt, [xn, xm]` writes the low `sf_bytes` bytes of `xt` at `xn + xm` +/// (the bytes are mapped), advances `pc` by 4, and changes nothing but that +/// memory. +pub proof fn lemma_step_st_reg(m: isa::State, sf: isa::Width, t: u8, n: u8, mr: u8) + requires + isa::wf(m), + t < 31, + n < 31, + mr < 31, + 0 <= m.r[n as int] as int + m.r[mr as int] as int, + m.r[n as int] as int + m.r[mr as int] as int + sf_bytes(sf) < 0x1_0000_0000_0000_0000, + forall|i: int| + 0 <= i < sf_bytes(sf) ==> m.mem.dom().contains( + #[trigger] (m.r[n as int] as int + m.r[mr as int] as int + i), + ), + m.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + ({ + let addr = m.r[n as int] as int + m.r[mr as int] as int; + let w = isa::trunc(isa::datasize(sf), m.r[t as int] as int); + &&& isa::mem_set(m.mem, addr, sf_bytes(sf), w) is Some + &&& isa::step(m, isa::Instr::LoadStoreReg { sf, load: false, t, n, m: mr, s: false }) + == Some( + isa::State { + mem: isa::mem_set(m.mem, addr, sf_bytes(sf), w).unwrap(), + pc: (m.pc + 4) as u64, + ..m + }, + ) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_x_read64(m, n); + super::steps::lemma_x_read64(m, mr); + let addr = m.r[n as int] as int + m.r[mr as int] as int; + super::memlemmas::lemma_trunc64(addr); + super::memlemmas::lemma_trunc64(m.pc as int + 4); + super::memlemmas::lemma_trunc64(m.r[mr as int] as int); + let i = isa::Instr::LoadStoreReg { sf, load: false, t, n, m: mr, s: false }; + let ds = isa::datasize(sf); + assert(ds / 8 == sf_bytes(sf)); + assert(isa::extend_reg_uxtx(m, mr, 0) == m.r[mr as int] as int) by { + assert(isa::p2(0) == 1); + assert(isa::trunc(64, m.r[mr as int] as int) == m.r[mr as int] as int); + } + let w = isa::trunc(ds, m.r[t as int] as int); + assert(isa::x_read(m, t, ds) == w); + super::memlemmas::lemma_mem_set_some(m.mem, addr, sf_bytes(sf), w); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfmemvalid.rs b/crates/clazel-jit/src/a64/cfmemvalid.rs new file mode 100644 index 000000000..a3de45daa --- /dev/null +++ b/crates/clazel-jit/src/a64/cfmemvalid.rs @@ -0,0 +1,120 @@ +//! Small facts about the memory and branch templates that the validator and +//! LIR's semantics need, each a proven theorem about a concrete input, so that +//! a mutation of the template that breaks it has a **certificate** +//! (`mutations.json`, `xtask/src/mutate.rs`): the validator's access rule +//! admits the access a memory instruction makes after its check, an access +//! moves as many bytes as its type has, a failing `bounds_check` exits with its +//! own code, a `br_table` entry branches on equal, a branch's displacement is in +//! words, and `br_if` branches on a nonzero condition. The A64 counterpart of +//! [`crate::cfmemvalid`]. + +#[cfg(verus_only)] +use super::cfmemsteps::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// What the validator knows after `ldr x3, [x28, #16 k]` and the bounds check +/// of `n` bytes on `x1` (`clazel_codecheck::a64::spec::check_facts`): `x3` is +/// region `k`'s base, `x0` and `x2` are unknown, `x1` is checked for `n` bytes. +pub open spec fn after_check(k: int, n: int) -> Seq { + seq![ + clazel_codecheck::a64::spec::Binding { + reg: 3, + fact: clazel_codecheck::a64::spec::Fact::Base(k), + }, + clazel_codecheck::a64::spec::Binding { + reg: 2, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }, + clazel_codecheck::a64::spec::Binding { + reg: 0, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }, + clazel_codecheck::a64::spec::Binding { + reg: 1, + fact: clazel_codecheck::a64::spec::Fact::Idx(k, n), + }, + ] +} + +/// **The validator accepts a load's access**: the facts after the check admit the +/// template's `ldr x0, [x3, x1]` of 8 bytes in region 0. +pub proof fn lemma_load_access_accepted() + ensures + clazel_codecheck::a64::spec::access_ok( + after_check(0, 8), + clazel_codecheck::a64::spec::mem_access( + mem_tail(MemOp::Load { region: 0, ty: MTy::I64 }, 0, 1)[0], + )->Some_0, + ), +{ + reveal_with_fuel(clazel_codecheck::a64::spec::fact_of, 6); +} + +/// **The validator accepts a store's access**: the same for `str x4, [x3, x1]`. +pub proof fn lemma_store_access_accepted() + ensures + clazel_codecheck::a64::spec::access_ok( + after_check(0, 8), + clazel_codecheck::a64::spec::mem_access( + mem_tail(MemOp::Store { region: 0, ty: MTy::I64 }, 0, 2)[0], + )->Some_0, + ), +{ + reveal_with_fuel(clazel_codecheck::a64::spec::fact_of, 6); +} + +/// An access of a type's width moves as many bytes as the type has (so it is +/// within the `n` bytes the check admitted). +pub proof fn lemma_mty_sf_bytes(ty: MTy) + ensures + sf_bytes(mty_sf(ty)) == ty.size(), +{ +} + +/// The access size of a type is LIR's. +pub proof fn lemma_mty_size_is_lir(ty: MTy) + ensures + lir::ty_size(ty.lir_ty()) == ty.size(), +{ +} + +/// A `bounds_check`'s stub exits with the instruction's own code. +pub proof fn lemma_bc_exit_code(region: usize, size: usize, code: u32) + requires + code < 0x1_0000, + ensures + mem_exit_code(MemOp::BoundsCheck { region, size, code }) as u32 == code, +{ +} + +/// The chain's entry branches on equal (`b.eq`: condition 0). +pub proof fn lemma_brt_cond(i: nat, rel: int) + ensures + brt_entry(i, rel)[2]->BCond_cond == 0, +{ +} + +/// A branch's displacement is in words: `b` 8 bytes on has `imm26` 2. +pub proof fn lemma_b_words() + ensures + b_i(8)->B_imm26 == 2, +{ +} + +/// `br_if` branches on a nonzero condition (`cbnz`). +pub proof fn lemma_brif_branches_on_nonzero() + ensures + ctmpl(Node::BrIf(0), 0, 1, seq![8int])[1]->CompareBranch_iszero == false, +{ +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfop.rs b/crates/clazel-jit/src/a64/cfop.rs new file mode 100644 index 000000000..04999e549 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfop.rs @@ -0,0 +1,352 @@ +//! The straight-line templates have no jumps, and run by `steps` as they do by +//! `run_seq`: the bridge from the first slice's [`refines`] to the +//! control-flow statement [`cf_refines`] for a straight-line instruction. The +//! A64 counterpart of [`crate::cfop`]; the facts about LIR's evaluation of a +//! straight-line instruction ([`crate::cfop::lemma_op_frame`]) are shared. + +#[cfg(verus_only)] +use super::cfalg::*; +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cfsteps::*; +#[cfg(verus_only)] +use super::contract::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[cfg(verus_only)] +use super::tlemmas::*; +#[cfg(verus_only)] +use crate::cfop::{lemma_op_frame, lemma_op_shape}; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The two `op_ok`s agree (the x86-64 one, which [`cf_flow`] is written with, +/// and this back end's): the frames are the same. +pub proof fn lemma_op_ok_agrees(op: Op, nl: nat, h: nat) + ensures + crate::spec::op_ok(op, nl, h) == op_ok(op, nl, h), +{ + super::steps::lemma_frame_agrees(); +} + +/// The concatenation of jump-free lists is jump-free. +pub proof fn lemma_no_jumps_concat(a: Seq, b: Seq) + requires + no_jumps(a), + no_jumps(b), + ensures + no_jumps(a + b), +{ + assert forall|k: int| 0 <= k < (a + b).len() implies !is_jump(#[trigger] (a + b)[k]) by { + if k < a.len() { + assert((a + b)[k] == a[k]); + } else { + assert((a + b)[k] == b[k - a.len()]); + } + } +} + +/// Loads of slots are jump-free. +pub proof fn lemma_loads_no_jumps(ls: Seq<(u8, nat)>) + ensures + no_jumps(loads_seq(ls)), + decreases ls.len(), +{ + if ls.len() > 0 { + lemma_loads_no_jumps(ls.drop_last()); + let one = seq![load_slot(ls.last().0, ls.last().1)]; + assert(no_jumps(one)) by { + assert forall|k: int| 0 <= k < one.len() implies !is_jump(#[trigger] one[k]) by { + assert(k == 0); + } + } + lemma_no_jumps_concat(loads_seq(ls.drop_last()), one); + } +} + +/// A template with a jump-free middle is jump-free. +pub proof fn lemma_shape_no_jumps(ls: Seq<(u8, nat)>, mid: Seq, ts: nat) + requires + no_jumps(mid), + ensures + no_jumps(shape(ls, mid, ts)), +{ + lemma_loads_no_jumps(ls); + let st = seq![store_slot(ts, x0())]; + assert(no_jumps(st)) by { + assert forall|k: int| 0 <= k < st.len() implies !is_jump(#[trigger] st[k]) by { + assert(k == 0); + } + } + lemma_no_jumps_concat(loads_seq(ls), mid); + lemma_no_jumps_concat(loads_seq(ls) + mid, st); +} + +/// The middle of a supported integer operator is one jump-free instruction. +pub proof fn lemma_int_op_no_jump(o: lir::IBin, sf: isa::Width) + requires + crate::spec::ibin_supported(o), + ensures + no_jumps(seq![int_op_instr(o, sf)]), +{ + assert forall|k: int| 0 <= k < 1 implies !is_jump(#[trigger] seq![int_op_instr(o, sf)][k]) by { + match o { + lir::IBin::Add => {}, + lir::IBin::Sub => {}, + lir::IBin::And => {}, + lir::IBin::Or => {}, + lir::IBin::Xor => {}, + lir::IBin::Shl => {}, + lir::IBin::ShrU => {}, + lir::IBin::ShrS => {}, + lir::IBin::Mul => {}, + lir::IBin::Rotl => { + assert(false); + }, + lir::IBin::Rotr => { + assert(false); + }, + } + } +} + +/// Every middle of the first slice is jump-free. +pub proof fn lemma_mids_no_jumps() + ensures + forall|c: lir::ICmp, sf: isa::Width| #[trigger] no_jumps(cmp_mid(c, sf)), + forall|sf: isa::Width| #[trigger] no_jumps(eqz_mid(sf)), + no_jumps(neg_mid()), + no_jumps(abs_mid()), + no_jumps(exts_mid()), + no_jumps(wrap_mid()), + no_jumps(select_mid()), + forall|v: lir::Val| #[trigger] no_jumps(const_mid(v)), +{ + assert forall|c: lir::ICmp, sf: isa::Width| #[trigger] no_jumps(cmp_mid(c, sf)) by { + let mid = cmp_mid(c, sf); + assert forall|k: int| 0 <= k < mid.len() implies !is_jump(#[trigger] mid[k]) by { + assert(k == 0 || k == 1); + } + } + assert forall|sf: isa::Width| #[trigger] no_jumps(eqz_mid(sf)) by { + let mid = eqz_mid(sf); + assert forall|k: int| 0 <= k < mid.len() implies !is_jump(#[trigger] mid[k]) by { + assert(k == 0 || k == 1); + } + } + let nm = neg_mid(); + assert forall|k: int| 0 <= k < nm.len() implies !is_jump(#[trigger] nm[k]) by { + assert(k == 0 || k == 1); + } + let am = abs_mid(); + assert forall|k: int| 0 <= k < am.len() implies !is_jump(#[trigger] am[k]) by { + assert(k == 0); + } + let em = exts_mid(); + assert forall|k: int| 0 <= k < em.len() implies !is_jump(#[trigger] em[k]) by { + assert(k == 0); + } + let wm = wrap_mid(); + assert forall|k: int| 0 <= k < wm.len() implies !is_jump(#[trigger] wm[k]) by { + assert(k == 0); + } + let sm = select_mid(); + assert forall|k: int| 0 <= k < sm.len() implies !is_jump(#[trigger] sm[k]) by { + assert(k == 0 || k == 1); + } + assert forall|v: lir::Val| #[trigger] no_jumps(const_mid(v)) by { + let cm = const_mid(v); + assert forall|k: int| 0 <= k < cm.len() implies !is_jump(#[trigger] cm[k]) by { + match v { + lir::Val::I32(c) => { + assert(k == 0 || k == 1); + }, + lir::Val::I64(c) => { + assert(k == 0 || k == 1 || k == 2 || k == 3); + }, + lir::Val::F64(c) => { + assert(k == 0 || k == 1 || k == 2 || k == 3); + }, + } + } + } +} + +/// No instruction of a template is a branch or `ret`. +pub proof fn lemma_tmpl_no_jumps(op: Op, nl: nat, h: nat) + requires + op_ok(op, nl, h), + ensures + no_jumps(tmpl(op, nl, h)), +{ + lemma_mids_no_jumps(); + let code = tmpl(op, nl, h); + match op { + Op::Nop => {}, + Op::Drop => {}, + Op::Const(v) => { + lemma_shape_no_jumps(seq![], const_mid(v), nl + h); + }, + Op::LocalGet(x) => { + lemma_shape_no_jumps(seq![(x0(), x as nat)], seq![], nl + h); + }, + Op::LocalSet(x) => { + lemma_shape_no_jumps(seq![(x0(), (nl + h - 1) as nat)], seq![], x as nat); + }, + Op::LocalTee(x) => { + lemma_shape_no_jumps(seq![(x0(), (nl + h - 1) as nat)], seq![], x as nat); + }, + Op::Select => { + let t = (nl + h - 3) as nat; + lemma_shape_no_jumps( + seq![(x0(), t), (x1(), t + 1), (x2(), t + 2)], + select_mid(), + t, + ); + }, + Op::Bin(b) => { + let t = (nl + h - 2) as nat; + let ls = seq![(x0(), t), (x1(), t + 1)]; + match b { + lir::BinOp::I32(o) => { + lemma_int_op_no_jump(o, isa::Width::W32); + lemma_shape_no_jumps(ls, seq![int_op_instr(o, isa::Width::W32)], t); + }, + lir::BinOp::I64(o) => { + lemma_int_op_no_jump(o, isa::Width::W64); + lemma_shape_no_jumps(ls, seq![int_op_instr(o, isa::Width::W64)], t); + }, + lir::BinOp::I32Cmp(c) => { + lemma_shape_no_jumps(ls, cmp_mid(c, isa::Width::W32), t); + }, + lir::BinOp::I64Cmp(c) => { + lemma_shape_no_jumps(ls, cmp_mid(c, isa::Width::W64), t); + }, + lir::BinOp::F64(_) => { + assert(false); + }, + lir::BinOp::F64Cmp(_) => { + assert(false); + }, + } + }, + Op::Un(u) => { + let t = (nl + h - 1) as nat; + let ls = seq![(x0(), t)]; + match u { + lir::UnOp::I32Eqz => { + lemma_shape_no_jumps(ls, eqz_mid(isa::Width::W32), t); + }, + lir::UnOp::I64Eqz => { + lemma_shape_no_jumps(ls, eqz_mid(isa::Width::W64), t); + }, + lir::UnOp::F64Neg => { + lemma_shape_no_jumps(ls, neg_mid(), t); + }, + lir::UnOp::F64Abs => { + lemma_shape_no_jumps(ls, abs_mid(), t); + }, + lir::UnOp::I64ExtendI32S => { + lemma_shape_no_jumps(ls, exts_mid(), t); + }, + lir::UnOp::I32WrapI64 => { + lemma_shape_no_jumps(ls, wrap_mid(), t); + }, + lir::UnOp::I64ExtendI32U => {}, + lir::UnOp::F64ReinterpretI64 => {}, + lir::UnOp::I64ReinterpretF64 => {}, + lir::UnOp::F64Sqrt => { + assert(false); + }, + lir::UnOp::F64ConvertI32S => { + assert(false); + }, + lir::UnOp::F64ConvertI32U => { + assert(false); + }, + lir::UnOp::F64ConvertI64S => { + assert(false); + }, + lir::UnOp::F64ConvertI64U => { + assert(false); + }, + } + }, + } +} + +/// `preserved` (nothing outside the exit area changed) is [`preserved_r`]. +pub proof fn lemma_preserved_to_r(lay: lir::Layout, m: isa::State, m2: isa::State) + requires + preserved(m, m2), + ensures + preserved_r(lay, m, m2), +{ + assert(ctx_of(m2) == ctx_of(m)) by { + super::steps::lemma_abi_consts(); + } +} + +/// **A straight-line instruction's template refines it, in the control-flow +/// statement.** The first slice's [`refines`] (for the template, as one block +/// of code) becomes [`cf_refines`] for any labels: the base under the stack is +/// untouched (the frame lemma), and a run by [`run_seq`] of jump-free code is +/// a run by [`steps`]. +#[verifier::rlimit(400)] +pub proof fn lemma_op_cf(op: Op, nl: nat, h: nat, offs: Seq) + requires + op_ok(op, nl, h), + ensures + cf_refines(op@, nl, h, offs, tmpl(op, nl, h)), +{ + let code = tmpl(op, nl, h); + super::proofs::lemma_op_refines(op, nl, h); + lemma_tmpl_no_jumps(op, nl, h); + assert forall| + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, m, lir::eval(lay, op@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && cf_canon( + m.pc as int, + offs, + code, + ) implies cf_post(lay, base, nl, offs, code, m, lir::eval(lay, op@, a, fuel)) by { + lemma_op_shape(op, lay, a, fuel); + match lir::eval(lay, op@, a, fuel) { + lir::Outcome::Normal(a2) => { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + lemma_op_frame(op, lay, a, a2, base, fuel); + let a2c = a2.with_stack(base + a2.stack); + super::proofs::lemma_use_refines(op@, nl, h, code, lay, ac, m, fuel); + assert(ac.stack.len() == h); + assert(m.pc as int + code_len(code) < isa::p2(64)); + let m2 = run_seq(m, code).unwrap(); + assert(rel(lay, a2c, m2, nl) && m2.pc as int == m.pc as int + code_len(code) + && preserved(m, m2)); + assert(Seq::::empty() + code + Seq::::empty() =~= code); + assert(code_len(Seq::::empty()) == 0); + lemma_run_seq_steps(Seq::empty(), code, Seq::empty(), m.pc as int, m, m2); + assert(reaches(code, m.pc as int, m, m2)); + lemma_preserved_to_r(lay, m, m2); + lemma_table_pres(lay, m, m2); + reveal(rel_base); + assert(rel_base(lay, base, a2, m2, nl)); + }, + _ => {}, + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfproofs.rs b/crates/clazel-jit/src/a64/cfproofs.rs new file mode 100644 index 000000000..fa2faf3e4 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfproofs.rs @@ -0,0 +1,238 @@ +//! **The refinement of every templated node** ([`lemma_ctmpl_refines`]): by +//! induction on the node, from the leaves ([`super::cfop`], [`super::cfleaf`], +//! [`super::cfexit`], [`super::cfmemops`], [`super::cfbrt`]) and the +//! compositions ([`super::cfcompose`], [`super::cfblock`], [`super::cfif`]). +//! The induction also carries the size invariant ([`super::cffit::labels_fit`]) +//! that every `b`, `b.cond`, `cbz` and `cbnz` displacement fits its field, and +//! with it that the code is in the subset. The A64 counterpart of +//! [`crate::cfproofs`]. + +#[cfg(verus_only)] +use super::cfblock::*; +#[cfg(verus_only)] +use super::cfbrt::*; +#[cfg(verus_only)] +use super::cfcompose::*; +#[cfg(verus_only)] +use super::cfcontract::*; +#[cfg(verus_only)] +use super::cfexit::*; +#[cfg(verus_only)] +use super::cffit::*; +#[cfg(verus_only)] +use super::cfif::*; +#[cfg(verus_only)] +use super::cfleaf::*; +#[cfg(verus_only)] +use super::cflens::*; +#[cfg(verus_only)] +use super::cfmemops::*; +#[cfg(verus_only)] +use super::cfop::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::cfvalid::*; +#[cfg(verus_only)] +use super::contract::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The size of a node's code is a multiple of 4. +pub proof fn lemma_csize_mod4(n: Node, nl: nat, h: nat, depth: nat) + ensures + csize(n, nl, h, depth) % 4 == 0, +{ +} + +/// **Every templated node's code refines it, and is in the subset.** For a node +/// the analysis accepts and that has a template, entered at height `h` with +/// `nl` locals and the labels at the byte offsets `offs`, [`ctmpl`] refines it +/// ([`cf_refines`]), provided the code is small enough for its displacements +/// ([`labels_fit`]). +#[verifier::rlimit(1000)] +pub proof fn lemma_ctmpl_refines(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + templated(n), + codes_ok(n), + csize(n, nl, h, offs.len()) <= max_size(), + labels_fit(offs, csize(n, nl, h, offs.len())), + ensures + cf_refines(n@, nl, h, offs, ctmpl(n, nl, h, offs)), + all_valid_instrs(ctmpl(n, nl, h, offs)), + decreases n, +{ + let d = offs.len(); + let size = csize(n, nl, h, d); + lemma_ctmpl_len(n, nl, h, offs); + super::steps::lemma_frame_agrees(); + match n { + Node::Op(o) => { + lemma_op_ok_agrees(o, nl, h); + lemma_op_cf(o, nl, h, offs); + super::tlemmas::lemma_tmpl_valid(o, nl, h); + }, + Node::Mem(m) => { + match m { + MemOp::BoundsCheck { region, size, code } => { + lemma_bc_cf(region, size, code, nl, h, offs); + }, + MemOp::Load { region, ty } => { + lemma_load_cf(region, ty, nl, h, offs); + }, + MemOp::Store { region, ty } => { + lemma_store_cf(region, ty, nl, h, offs); + }, + } + lemma_mem_valid(m, nl, h); + }, + Node::Then(a, b) => { + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + lemma_csize_then(*a, *b, nl, h, d); + assert(flow_h(*a, nl, h, d) == h1); + let sa = csize(*a, nl, h, d); + let sb = csize(*b, nl, h1, d); + lemma_csize_mod4(*a, nl, h, d); + lemma_fit_mono(offs, size, sa); + lemma_ctmpl_refines(*a, nl, h, offs); + let ob = shift(offs, sa as int); + lemma_fit_shift(offs, sa, size, sb); + assert(ob.len() == d); + lemma_ctmpl_refines(*b, nl, h1, ob); + lemma_ctmpl_len(*a, nl, h, offs); + lemma_then( + *a, + *b, + nl, + h, + h1, + offs, + ctmpl(*a, nl, h, offs), + ctmpl(*b, nl, h1, ob), + ); + lemma_valid_cat(ctmpl(*a, nl, h, offs), ctmpl(*b, nl, h1, ob)); + }, + Some(Flow::Dead) => { + // the second is `nop`: no code + lemma_csize_then(*a, *b, nl, h, d); + lemma_fit_mono(offs, size, csize(*a, nl, h, d)); + lemma_ctmpl_refines(*a, nl, h, offs); + lemma_then_dead(*a, nl, h, offs, ctmpl(*a, nl, h, offs)); + assert(ctmpl(n, nl, h, offs) == ctmpl(*a, nl, h, offs) + ctmpl( + *b, + nl, + h, + shift(offs, csize(*a, nl, h, d) as int), + )); + assert(ctmpl(*b, nl, h, shift(offs, csize(*a, nl, h, d) as int)) =~= Seq::< + isa::Instr, + >::empty()); + assert(ctmpl(*a, nl, h, offs) + Seq::::empty() =~= ctmpl( + *a, + nl, + h, + offs, + )); + }, + None => {}, + } + }, + Node::Block(b) => { + lemma_csize_block(*b, nl, h, d); + let sb = csize(*b, nl, h, d + 1); + let ob = seq![sb as int] + offs; + lemma_csize_mod4(*b, nl, h, d + 1); + lemma_fit_mono(offs, size, sb); + lemma_fit_cons(sb as int, offs, sb); + lemma_ctmpl_refines(*b, nl, h, ob); + lemma_ctmpl_len(*b, nl, h, ob); + lemma_block(*b, nl, h, offs, ctmpl(*b, nl, h, ob)); + }, + Node::Loop(b) => { + lemma_csize_loop(*b, nl, h, d); + let sb = csize(*b, nl, h, d + 1); + let ob = seq![0int] + offs; + lemma_fit_mono(offs, size, sb); + lemma_fit_cons(0, offs, sb); + lemma_ctmpl_refines(*b, nl, h, ob); + lemma_loop(*b, nl, h, offs, ctmpl(*b, nl, h, ob)); + }, + Node::If(t, e) => { + lemma_csize_if(*t, *e, nl, h, d); + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + lemma_csize_mod4(*t, nl, (h - 1) as nat, d + 1); + lemma_csize_mod4(*e, nl, (h - 1) as nat, d + 1); + let ot = seq![st + 4 + se] + shift(offs, 8); + let oe = seq![se] + shift(offs, 12 + st); + lemma_fit_shift(offs, 8, size, st as nat); + lemma_fit_cons(st + 4 + se, shift(offs, 8), st as nat); + lemma_fit_shift(offs, (12 + st) as nat, size, se as nat); + lemma_fit_cons(se, shift(offs, 12 + st), se as nat); + assert(cf_flow(*t, nl, (h - 1) as nat, d + 1) is Some); + assert(cf_flow(*e, nl, (h - 1) as nat, d + 1) is Some); + lemma_ctmpl_refines(*t, nl, (h - 1) as nat, ot); + lemma_ctmpl_refines(*e, nl, (h - 1) as nat, oe); + let ct = ctmpl(*t, nl, (h - 1) as nat, ot); + let ce = ctmpl(*e, nl, (h - 1) as nat, oe); + lemma_ctmpl_len(*t, nl, (h - 1) as nat, ot); + lemma_ctmpl_len(*e, nl, (h - 1) as nat, oe); + assert(code_len(ct) == st && code_len(ce) == se); + assert(cond_len(nl, h) == 8) by { + lemma_cond_pre_len(nl, h, true, 0); + } + assert(if_offs_t(nl, h, offs, ct, ce) == ot); + assert(if_offs_e(nl, h, offs, ct, ce) == oe); + lemma_if(*t, *e, nl, h, offs, ct, ce); + assert(ctmpl(n, nl, h, offs) == if_code(nl, h, ct, ce)); + lemma_cond_pre_valid(nl, h, true, st + 8); + lemma_b_valid(se + 4); + let cp = cond_pre(nl, h, true, st + 8); + let jm = seq![b_i(se + 4)]; + assert(all_valid_instrs(jm)) by { + assert forall|j: int| 0 <= j < jm.len() implies isa::valid_instr(#[trigger] jm[j]) by { + assert(j == 0); + } + } + lemma_valid_cat(cp, ct); + lemma_valid_cat(cp + ct, jm); + lemma_valid_cat(cp + ct + jm, ce); + }, + Node::Br(l) => { + lemma_csize_br(l, nl, h, d); + assert(l < offs.len()); + lemma_br(nl, h, offs, l); + lemma_b_valid(offs[l as int]); + let c = ctmpl(n, nl, h, offs); + assert(all_valid_instrs(c)) by { + assert forall|j: int| 0 <= j < c.len() implies isa::valid_instr(#[trigger] c[j]) by { + assert(j == 0); + } + } + }, + Node::BrIf(l) => { + lemma_csize_brif(l, nl, h, d); + lemma_brif(nl, h, offs, l); + lemma_cond_pre_valid(nl, h, false, offs[l as int] - 4); + }, + Node::BrTable(ls, d2) => { + lemma_brt_len(ls@, d2, nl, h, offs); + lemma_brt_cf(ls, d2, nl, h, offs); + lemma_brt_valid(ls@, d2, nl, h, offs); + }, + Node::Exit(c, k) => { + lemma_exit_cf(c, k, nl, h, offs); + super::tlemmas::lemma_exit_valid(c as u16); + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfsimple.rs b/crates/clazel-jit/src/a64/cfsimple.rs new file mode 100644 index 000000000..758b5d25f --- /dev/null +++ b/crates/clazel-jit/src/a64/cfsimple.rs @@ -0,0 +1,346 @@ +//! The A64 straight-line templates are single instructions the validator +//! accepts whatever it knows (jit.md item J.9, "every emitted blob is +//! accepted"): none writes the context register, the link register or `sp`, +//! none touches memory except the context (the frame), none is a branch, and +//! none is a `ret` or a `b.hs`. The counterpart of [`crate::cfsimple`]. + +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::cfunits::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The instruction's memory access, if any, is to the context at a fixed +/// offset, and a store is to the exit area: the validator admits it whatever +/// it knows. +pub open spec fn ctx_only(i: isa::Instr) -> bool { + match clazel_codecheck::a64::spec::mem_access(i) { + None => true, + Some(a) => a.base == abi::ctx_reg() && a.index is None && 0 <= a.off && a.off + a.size + <= abi::ctx_size() && (!a.store || abi::exit_code_off() <= a.off), + } +} + +/// **A simple instruction**: not a `ret` or a `b.hs`, writes no reserved +/// register, and touches memory only as [`ctx_only`] says. +pub open spec fn simp(i: isa::Instr) -> bool { + ok2(i) && !clazel_codecheck::a64::spec::writes_reserved(i) && ctx_only(i) +} + +/// A simple instruction is accepted by the validator's single-instruction +/// rule, whatever it knows. +pub proof fn lemma_simp_plain(fs: clazel_codecheck::a64::spec::Facts, i: isa::Instr) + requires + simp(i), + ensures + clazel_codecheck::a64::spec::plain_ok(fs, i) is Some, +{ + if clazel_codecheck::a64::spec::mem_access(i) is Some { + let a = clazel_codecheck::a64::spec::mem_access(i)->Some_0; + assert(clazel_codecheck::a64::spec::access_ok(fs, a)); + } +} + +/// An instruction that is not a branch. +pub open spec fn nojump(i: isa::Instr) -> bool { + clazel_codecheck::a64::spec::jump_rel(i) is None +} + +/// A simple instruction that is not a branch. +pub open spec fn straight(i: isa::Instr) -> bool { + simp(i) && nojump(i) +} + +/// Every instruction of the list is [`straight`]. +pub open spec fn all_straight(c: Seq) -> bool { + forall|k: int| 0 <= k < c.len() ==> straight(#[trigger] c[k]) +} + +/// Straight code followed by straight code is straight. +pub proof fn lemma_straight_cat(a: Seq, b: Seq) + requires + all_straight(a), + all_straight(b), + ensures + all_straight(a + b), +{ + assert forall|k: int| 0 <= k < (a + b).len() implies straight(#[trigger] (a + b)[k]) by { + if k < a.len() { + assert((a + b)[k] == a[k]); + } else { + assert((a + b)[k] == b[k - a.len()]); + } + } +} + +/// A load or store of a frame slot is straight. +pub proof fn lemma_load_slot_straight(r: u8, j: nat) + requires + r < 8, + j < 31, + ensures + straight(load_slot(r, j)), + straight(store_slot(j, r)), +{ + assert(max_slots() == 31) by { + super::tlemmas::lemma_abi_consts_t(); + } +} + +/// The loads of a template are straight. +pub proof fn lemma_loads_straight(ls: Seq<(u8, nat)>) + requires + forall|q: int| 0 <= q < ls.len() ==> #[trigger] ls[q].0 < 8 && ls[q].1 < 31, + ensures + all_straight(loads_seq(ls)), + decreases ls.len(), +{ + if ls.len() > 0 { + let d = ls.drop_last(); + assert forall|q: int| 0 <= q < d.len() implies #[trigger] d[q].0 < 8 && d[q].1 < 31 by { + assert(d[q] == ls[q]); + } + lemma_loads_straight(d); + let l = ls.last(); + assert(l == ls[ls.len() - 1]); + lemma_load_slot_straight(l.0, l.1); + let one = seq![load_slot(l.0, l.1)]; + assert(all_straight(one)); + lemma_straight_cat(loads_seq(d), one); + } +} + +/// A template of the shape loads, middle, store is straight. +pub proof fn lemma_shape_straight(ls: Seq<(u8, nat)>, mid: Seq, ts: nat) + requires + forall|q: int| 0 <= q < ls.len() ==> #[trigger] ls[q].0 < 8 && ls[q].1 < 31, + all_straight(mid), + ts < 31, + ensures + all_straight(shape(ls, mid, ts)), +{ + lemma_loads_straight(ls); + lemma_load_slot_straight(0, ts); + let st = seq![store_slot(ts, x0())]; + assert(all_straight(st)); + lemma_straight_cat(loads_seq(ls), mid); + lemma_straight_cat(loads_seq(ls) + mid, st); +} + +/// The integer operation is straight. +pub proof fn lemma_int_op_straight(o: lir::IBin, sf: isa::Width) + ensures + all_straight(seq![int_op_instr(o, sf)]), +{ + let c = seq![int_op_instr(o, sf)]; + assert forall|k: int| 0 <= k < c.len() implies straight(#[trigger] c[k]) by { + assert(k == 0); + match o { + lir::IBin::Add => {}, + lir::IBin::Sub => {}, + lir::IBin::And => {}, + lir::IBin::Or => {}, + lir::IBin::Xor => {}, + lir::IBin::Shl => {}, + lir::IBin::ShrU => {}, + lir::IBin::ShrS => {}, + lir::IBin::Mul => {}, + lir::IBin::Rotl => {}, + lir::IBin::Rotr => {}, + } + } +} + +/// Every instruction of every constant's code is straight. +pub proof fn lemma_const_straight(v: lir::Val) + ensures + all_straight(const_mid(v)), +{ + let c = const_mid(v); + assert forall|k: int| 0 <= k < c.len() implies straight(#[trigger] c[k]) by { + match v { + lir::Val::I32(_) => { + assert(k == 0 || k == 1); + }, + lir::Val::I64(_) => { + assert(k == 0 || k == 1 || k == 2 || k == 3); + }, + lir::Val::F64(_) => { + assert(k == 0 || k == 1 || k == 2 || k == 3); + }, + } + } +} + +/// Every instruction of the other middles is straight. +pub proof fn lemma_mids_straight(c: lir::ICmp, sf: isa::Width) + ensures + all_straight(cmp_mid(c, sf)), + all_straight(eqz_mid(sf)), + all_straight(neg_mid()), + all_straight(abs_mid()), + all_straight(exts_mid()), + all_straight(wrap_mid()), + all_straight(select_mid()), +{ + assert forall|k: int| 0 <= k < cmp_mid(c, sf).len() implies straight( + #[trigger] cmp_mid(c, sf)[k], + ) by { + assert(k == 0 || k == 1); + } + assert forall|k: int| 0 <= k < eqz_mid(sf).len() implies straight(#[trigger] eqz_mid(sf)[k]) by { + assert(k == 0 || k == 1); + } + assert forall|k: int| 0 <= k < neg_mid().len() implies straight(#[trigger] neg_mid()[k]) by { + assert(k == 0 || k == 1); + } + assert forall|k: int| 0 <= k < abs_mid().len() implies straight(#[trigger] abs_mid()[k]) by { + assert(k == 0); + } + assert forall|k: int| 0 <= k < exts_mid().len() implies straight(#[trigger] exts_mid()[k]) by { + assert(k == 0); + } + assert forall|k: int| 0 <= k < wrap_mid().len() implies straight(#[trigger] wrap_mid()[k]) by { + assert(k == 0); + } + assert forall|k: int| 0 <= k < select_mid().len() implies straight( + #[trigger] select_mid()[k], + ) by { + assert(k == 0 || k == 1); + } +} + +/// **A template of a straight-line instruction is straight.** +pub proof fn lemma_tmpl_straight(op: Op, nl: nat, h: nat) + requires + op_ok(op, nl, h), + ensures + all_straight(tmpl(op, nl, h)), +{ + assert(max_slots() == 31) by { + super::tlemmas::lemma_abi_consts_t(); + } + lemma_mids_straight(lir::ICmp::Eq, isa::Width::W32); + match op { + Op::Nop => {}, + Op::Drop => {}, + Op::Const(v) => { + lemma_const_straight(v); + lemma_shape_straight(seq![], const_mid(v), nl + h); + }, + Op::LocalGet(x) => { + lemma_shape_straight(seq![(x0(), x as nat)], seq![], nl + h); + }, + Op::LocalSet(x) => { + lemma_shape_straight(seq![(x0(), (nl + h - 1) as nat)], seq![], x as nat); + }, + Op::LocalTee(x) => { + lemma_shape_straight(seq![(x0(), (nl + h - 1) as nat)], seq![], x as nat); + }, + Op::Select => { + let t = (nl + h - 3) as nat; + lemma_shape_straight( + seq![(x0(), t), (x1(), t + 1), (x2(), t + 2)], + select_mid(), + t, + ); + }, + Op::Bin(b) => { + let t = (nl + h - 2) as nat; + match b { + lir::BinOp::I32(o) => { + lemma_int_op_straight(o, isa::Width::W32); + lemma_shape_straight( + seq![(x0(), t), (x1(), t + 1)], + seq![int_op_instr(o, isa::Width::W32)], + t, + ); + }, + lir::BinOp::I64(o) => { + lemma_int_op_straight(o, isa::Width::W64); + lemma_shape_straight( + seq![(x0(), t), (x1(), t + 1)], + seq![int_op_instr(o, isa::Width::W64)], + t, + ); + }, + lir::BinOp::I32Cmp(c) => { + lemma_mids_straight(c, isa::Width::W32); + lemma_shape_straight( + seq![(x0(), t), (x1(), t + 1)], + cmp_mid(c, isa::Width::W32), + t, + ); + }, + lir::BinOp::I64Cmp(c) => { + lemma_mids_straight(c, isa::Width::W64); + lemma_shape_straight( + seq![(x0(), t), (x1(), t + 1)], + cmp_mid(c, isa::Width::W64), + t, + ); + }, + lir::BinOp::F64(_) => { + assert(false); + }, + lir::BinOp::F64Cmp(_) => { + assert(false); + }, + } + }, + Op::Un(u) => { + let t = (nl + h - 1) as nat; + match u { + lir::UnOp::I32Eqz => { + lemma_mids_straight(lir::ICmp::Eq, isa::Width::W32); + lemma_shape_straight(seq![(x0(), t)], eqz_mid(isa::Width::W32), t); + }, + lir::UnOp::I64Eqz => { + lemma_mids_straight(lir::ICmp::Eq, isa::Width::W64); + lemma_shape_straight(seq![(x0(), t)], eqz_mid(isa::Width::W64), t); + }, + lir::UnOp::F64Neg => { + lemma_shape_straight(seq![(x0(), t)], neg_mid(), t); + }, + lir::UnOp::F64Abs => { + lemma_shape_straight(seq![(x0(), t)], abs_mid(), t); + }, + lir::UnOp::I64ExtendI32S => { + lemma_shape_straight(seq![(x0(), t)], exts_mid(), t); + }, + lir::UnOp::I32WrapI64 => { + lemma_shape_straight(seq![(x0(), t)], wrap_mid(), t); + }, + lir::UnOp::I64ExtendI32U => {}, + lir::UnOp::F64ReinterpretI64 => {}, + lir::UnOp::I64ReinterpretF64 => {}, + lir::UnOp::F64Sqrt => { + assert(false); + }, + lir::UnOp::F64ConvertI32S => { + assert(false); + }, + lir::UnOp::F64ConvertI32U => { + assert(false); + }, + lir::UnOp::F64ConvertI64S => { + assert(false); + }, + lir::UnOp::F64ConvertI64U => { + assert(false); + }, + } + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfspec.rs b/crates/clazel-jit/src/a64/cfspec.rs new file mode 100644 index 000000000..8a3fbf607 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfspec.rs @@ -0,0 +1,203 @@ +//! What the A64 back end's **structured control flow and memory +//! instructions** are about (jit.md item J.9, second slice, A64): the state +//! relation that includes the region table ([`rel_base`]), what a run of +//! code with branches preserves ([`preserved_r`]), and the machine's running of +//! code with jumps ([`steps`], [`reaches`]). It is [`crate::cfspec`]'s A64 +//! counterpart and follows its design; the source language ([`Node`], whose +//! blocks are all void) and its static analysis ([`cf_flow`]) are shared with +//! the x86-64 back end, which this crate proves sound for LIR's `eval` +//! ([`crate::cfheight`]). Read [`crate::cfspec`]'s header for the choices +//! the two share (8: void blocks, 9: static heights, 10: labels are byte +//! offsets, 11: a `load` or `store` is its own bounds-check sequence and the +//! access, with its two exits going to a stub after the access, 12: the +//! region table is related, 13: the fresh stack of a block is a `base`, 14: +//! running with jumps, 15: regions are not code). This header says where A64 +//! differs. +//! +//! # Where A64 differs from [`crate::cfspec`] +//! +//! 16. **A branch is relative to its own address, in words.** `b` has a +//! 26-bit and `b.cond`, `cbz` and `cbnz` a 19-bit *signed* displacement +//! in 4-byte words, from the address of the branch itself (`clazel-a64`'s +//! [`isa::step`]: `BranchTo(PC + 4 * imm)`). So a label's byte offset must +//! be a multiple of 4 ([`cf_canon`] says it is) and a conditional branch +//! reaches only 1 MiB each way: the translation refuses a program whose +//! code is more than [`crate::a64::cftemplates::max_size`] bytes +//! ([`crate::a64::cftemplates::cf_prog_ok`]), which is what makes every +//! displacement fit. +//! 17. **Exit codes are 16 bits.** The validator's exit sequence is `movz` of +//! a 16-bit code ([`exit_seq`]), so an `exit` or a `bounds_check` whose +//! code is 65536 or more has no translation ([`codes_ok`]); the exit +//! code of the stub after a `load` or `store` is [`trap_code`], `0xFFFF` +//! (choice 11: nothing is claimed about it). +//! 18. **No native stack, no `create_undef`.** The exit sequence returns +//! through `x30` and reads no memory, so there is no [`crate::spec::stack_ok_r`] +//! analogue, and [`isa::step`] takes no source of undefined values, so no +//! statement has a `cu`. The link register and `SP` are preserved +//! ([`preserved_r`]), which the exit sequence needs. +//! 19. **Registers.** The scratch registers are `x0` to `x4`: `x0` and `x2` +//! are the bounds check's scratch registers (`t`, `bnd`), `x1` holds the +//! index, `x3` the region's base and `x4` the value a `store` writes +//! ([`x3`], [`x4`]). +//! +//! # What is not here +//! +//! Typed blocks (choice 8), `f64` arithmetic, rotates, frames beyond 31 +//! slots, a `br_table` of more than [`crate::cfspec::max_targets`] targets, +//! and a program of more than `max_size` bytes. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +pub use crate::cfspec::{ + Flow, MTy, MemOp, Node, cf_flow, exit_instr, flow_fits, max_targets, templated, void_bt, +}; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// ============================================================================ +// Section: registers and codes +// ============================================================================ + +/// `x3`: a region's base, in an access. +pub open spec fn x3() -> u8 { + 3 +} + +/// `x4`: the value a `store` writes. +pub open spec fn x4() -> u8 { + 4 +} + +/// The exit code of a bounds check inside a `load` or `store` (choice 11 of +/// [`crate::cfspec`]). +pub open spec fn trap_code() -> u16 { + 0xFFFF +} + +/// Every exit code in `n` (of an `exit`, or of a `bounds_check`) is 16 bits +/// (choice 17), so the validator's exit sequence can say it. +pub open spec fn codes_ok(n: Node) -> bool + decreases n, +{ + match n { + Node::Op(_) => true, + Node::Mem(m) => match m { + MemOp::BoundsCheck { code, .. } => code < 0x1_0000, + _ => true, + }, + Node::Then(a, b) => codes_ok(*a) && codes_ok(*b), + Node::Block(b) => codes_ok(*b), + Node::Loop(b) => codes_ok(*b), + Node::If(t, e) => codes_ok(*t) && codes_ok(*e), + Node::Br(_) => true, + Node::BrIf(_) => true, + Node::BrTable(_, _) => true, + Node::Exit(c, _) => c < 0x1_0000, + } +} + +// ============================================================================ +// Section: the state relation with a base, and the region table +// ============================================================================ + +/// `a` is a byte of some region, `a` an `int`. +pub open spec fn in_regions_i(lay: lir::Layout, a: int) -> bool { + 0 <= a && in_regions(lay, a as nat) +} + +/// The context's region table holds LIR's layout: at most 16 regions, and for +/// each, `base` at `x28 + 16 k` and `bound` at `x28 + 16 k + 8`, as 64-bit +/// words (choice 12 of [`crate::cfspec`]). No region overlaps the table, so a +/// store to a region never changes it (the exit area is already clear: +/// [`rel`]). +pub open spec fn table_ok(lay: lir::Layout, m: isa::State) -> bool { + lay.len() <= abi::num_regions() && (forall|k: int| + 0 <= k < lay.len() ==> { + &&& isa::mem_read(m.mem, ctx_of(m) + abi::base_off(k), 8) == Some( + (#[trigger] lay[k]).base as int, + ) + &&& isa::mem_read(m.mem, ctx_of(m) + abi::bound_off(k), 8) == Some( + lay[k].bound as int, + ) + }) && forall|b: int| + ctx_of(m) <= b < ctx_of(m) + abi::table_end() ==> !#[trigger] in_regions_i(lay, b) +} + +/// The machine `m` represents the LIR state `a` on top of the values `base` +/// (choice 13 of [`crate::cfspec`]), and the region table is as LIR's layout. +#[verifier::opaque] +pub open spec fn rel_base( + lay: lir::Layout, + base: Seq, + a: lir::St, + m: isa::State, + nl: nat, +) -> bool { + rel(lay, a.with_stack(base + a.stack), m, nl) && table_ok(lay, m) +} + +/// What a run of code leaves alone in memory, now that a store may write a +/// region: the context register keeps its value, the mapped addresses are the +/// same, and every byte outside the exit area **and outside every region** +/// keeps its value (choice 15 of [`crate::cfspec`]). +pub open spec fn preserved_mem_r(lay: lir::Layout, m: isa::State, m2: isa::State) -> bool { + m.r.len() == 31 && m2.r.len() == 31 && m2.r[abi::ctx_reg() as int] == m.r[abi::ctx_reg() as int] + && m2.mem.dom() == m.mem.dom() && (forall|a: int| + #[trigger] m.mem.dom().contains(a) && !in_exit_area(ctx_of(m), a) && !in_regions_i(lay, a) + ==> m2.mem[a] == m.mem[a]) +} + +/// [`preserved_mem_r`], and the link register and the stack pointer keep their +/// values too (choice 18). +pub open spec fn preserved_r(lay: lir::Layout, m: isa::State, m2: isa::State) -> bool { + preserved_mem_r(lay, m, m2) && m2.r[abi::link_reg() as int] == m.r[abi::link_reg() as int] + && m2.sp == m.sp +} + +// ============================================================================ +// Section: running code with jumps +// ============================================================================ + +/// The instruction of `code` that begins `off` bytes after the first byte: +/// `None` if no instruction begins there (past the end, before the start, or +/// not a multiple of 4). +pub open spec fn fetch(code: Seq, off: int) -> Option { + if 0 <= off && off % 4 == 0 && off / 4 < code.len() { + Some(code[off / 4]) + } else { + None + } +} + +/// `n` steps of the machine from `s`, the code `code` being placed at address +/// `base` (choice 14 of [`crate::cfspec`]): each fetches the instruction at +/// `pc` and steps it. A fault, or a `pc` with no instruction of `code`, is +/// `None`. +pub open spec fn steps(code: Seq, base: int, s: isa::State, n: nat) -> Option< + isa::State, +> + decreases n, +{ + if n == 0 { + Some(s) + } else { + match fetch(code, s.pc as int - base) { + Some(i) => match isa::step(s, i) { + Some(s1) => steps(code, base, s1, (n - 1) as nat), + None => None, + }, + None => None, + } + } +} + +/// Running `code` (placed at `base`) from `s` reaches `s2` after some number +/// of steps, none of which faults. +pub open spec fn reaches(code: Seq, base: int, s: isa::State, s2: isa::State) -> bool { + exists|n: nat| steps(code, base, s, n) == Some(s2) +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfsteps.rs b/crates/clazel-jit/src/a64/cfsteps.rs new file mode 100644 index 000000000..fba967d37 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfsteps.rs @@ -0,0 +1,448 @@ +//! Facts about [`fetch`], [`steps`] and [`reaches`]: a run in a piece of code +//! is a run in any list that contains it, runs compose, and a run of +//! jump-free code by [`run_seq`] is a run by [`steps`]. The A64 counterpart of +//! [`crate::cfsteps`]; every instruction is 4 bytes, so `fetch` is an index. + +#[cfg(verus_only)] +use super::cfspec::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `code` has no instruction that changes `pc` other than by 4: no branch, no +/// `ret`. +pub open spec fn no_jumps(code: Seq) -> bool { + forall|j: int| 0 <= j < code.len() ==> !is_jump(#[trigger] code[j]) +} + +/// A branch or `ret`. +pub open spec fn is_jump(i: isa::Instr) -> bool { + i is B || i is BCond || i is CompareBranch || i is Ret +} + +/// A step of an instruction that is not a jump advances `pc` by 4 (when that +/// does not wrap). +pub proof fn lemma_step_pc_plain(s: isa::State, i: isa::Instr, s1: isa::State) + requires + isa::step(s, i) == Some(s1), + !is_jump(i), + s.pc as int + 4 < isa::p2(64), + ensures + s1.pc as int == s.pc as int + 4, +{ + super::memlemmas::lemma_p2_consts(); + super::memlemmas::lemma_trunc64(s.pc as int + 4); +} + +/// `fetch` in a list sees the same instruction in the list followed by more. +pub proof fn lemma_fetch_append_l(a: Seq, b: Seq, off: int) + requires + fetch(a, off) is Some, + ensures + fetch(a + b, off) == fetch(a, off), +{ +} + +/// `fetch` at or past the end of `a` is `fetch` in `b`. +pub proof fn lemma_fetch_append_r(a: Seq, b: Seq, off: int) + requires + off >= code_len(a), + ensures + fetch(a + b, off) == fetch(b, off - code_len(a)), +{ + assert(code_len(a) == 4 * a.len()); + let off2 = off - 4 * a.len(); + assert(off % 4 == off2 % 4); + assert(off / 4 == off2 / 4 + a.len()); + if 0 <= off2 && off2 % 4 == 0 && off2 / 4 < b.len() { + assert((a + b)[off / 4] == b[off2 / 4]); + } +} + +/// [`steps`] with the machine's step function abstracted, so that the lemmas +/// below, which are about fetching and counting, do not make the solver unfold +/// `isa::step` on an instruction it knows nothing about. +pub open spec fn steps_g( + f: spec_fn(isa::State, isa::Instr) -> Option, + code: Seq, + base: int, + s: isa::State, + n: nat, +) -> Option + decreases n, +{ + if n == 0 { + Some(s) + } else { + match fetch(code, s.pc as int - base) { + Some(i) => match f(s, i) { + Some(s1) => steps_g(f, code, base, s1, (n - 1) as nat), + None => None, + }, + None => None, + } + } +} + +/// The machine's step as a function value. +pub open spec fn stepper() -> spec_fn(isa::State, isa::Instr) -> Option { + |s: isa::State, i: isa::Instr| isa::step(s, i) +} + +/// Running a list by an abstract step function (as [`run_seq`]). +pub open spec fn run_gen( + f: spec_fn(isa::State, isa::Instr) -> Option, + s: isa::State, + code: Seq, +) -> Option + decreases code.len(), +{ + if code.len() == 0 { + Some(s) + } else { + match f(s, code[0]) { + Some(s1) => run_gen(f, s1, code.skip(1)), + None => None, + } + } +} + +/// [`run_seq`] is [`run_gen`] of the machine's step. +pub proof fn lemma_run_seq_is_gen(s: isa::State, code: Seq) + ensures + run_seq(s, code) == run_gen(stepper(), s, code), + decreases code.len(), +{ + if code.len() > 0 { + match isa::step(s, code[0]) { + Some(s1) => { + lemma_run_seq_is_gen(s1, code.skip(1)); + }, + None => {}, + } + } +} + +/// [`steps`] is [`steps_g`] of the machine's step. +pub proof fn lemma_steps_is_gen(code: Seq, base: int, s: isa::State, n: nat) + ensures + steps(code, base, s, n) == steps_g(stepper(), code, base, s, n), + decreases n, +{ + if n > 0 { + match fetch(code, s.pc as int - base) { + Some(i) => match isa::step(s, i) { + Some(s1) => { + lemma_steps_is_gen(code, base, s1, (n - 1) as nat); + }, + None => {}, + }, + None => {}, + } + } +} + +/// Generic [`lemma_steps_trans`]. +pub proof fn lemma_steps_g_trans( + f: spec_fn(isa::State, isa::Instr) -> Option, + code: Seq, + base: int, + s: isa::State, + s1: isa::State, + s2: isa::State, + n1: nat, + n2: nat, +) + requires + steps_g(f, code, base, s, n1) == Some(s1), + steps_g(f, code, base, s1, n2) == Some(s2), + ensures + steps_g(f, code, base, s, n1 + n2) == Some(s2), + decreases n1, +{ + if n1 > 0 { + match fetch(code, s.pc as int - base) { + Some(i) => match f(s, i) { + Some(t) => { + lemma_steps_g_trans(f, code, base, t, s1, s2, (n1 - 1) as nat, n2); + }, + None => {}, + }, + None => {}, + } + } +} + +/// Runs compose: `n1` steps, then `n2` more. +pub proof fn lemma_steps_trans( + code: Seq, + base: int, + s: isa::State, + s1: isa::State, + s2: isa::State, + n1: nat, + n2: nat, +) + requires + steps(code, base, s, n1) == Some(s1), + steps(code, base, s1, n2) == Some(s2), + ensures + steps(code, base, s, n1 + n2) == Some(s2), +{ + let f = stepper(); + lemma_steps_is_gen(code, base, s, n1); + lemma_steps_is_gen(code, base, s1, n2); + lemma_steps_is_gen(code, base, s, n1 + n2); + lemma_steps_g_trans(f, code, base, s, s1, s2, n1, n2); +} + +/// [`reaches`] is transitive. +pub proof fn lemma_reaches_trans( + code: Seq, + base: int, + s: isa::State, + s1: isa::State, + s2: isa::State, +) + requires + reaches(code, base, s, s1), + reaches(code, base, s1, s2), + ensures + reaches(code, base, s, s2), +{ + let n1 = choose|n: nat| steps(code, base, s, n) == Some(s1); + let n2 = choose|n: nat| steps(code, base, s1, n) == Some(s2); + lemma_steps_trans(code, base, s, s1, s2, n1, n2); +} + +/// Generic [`lemma_steps_embed`]. +pub proof fn lemma_steps_g_embed( + f: spec_fn(isa::State, isa::Instr) -> Option, + pre: Seq, + c: Seq, + post: Seq, + base: int, + s: isa::State, + s2: isa::State, + n: nat, +) + requires + steps_g(f, c, base, s, n) == Some(s2), + ensures + steps_g(f, pre + c + post, base - code_len(pre), s, n) == Some(s2), + decreases n, +{ + if n > 0 { + let off = s.pc as int - base; + match fetch(c, off) { + Some(i) => { + lemma_fetch_append_l(c, post, off); + assert(off >= 0); + lemma_fetch_append_r(pre, c + post, off + code_len(pre)); + assert(pre + c + post =~= pre + (c + post)); + assert(off + code_len(pre) - code_len(pre) == off); + assert(s.pc as int - (base - code_len(pre)) == off + code_len(pre)); + match f(s, i) { + Some(t) => { + lemma_steps_g_embed(f, pre, c, post, base, t, s2, (n - 1) as nat); + }, + None => {}, + } + }, + None => {}, + } + } +} + +/// **A run in a piece is a run in the whole.** If `c` placed at `base` runs +/// from `s` to `s2` in `n` steps, then `pre + c + post`, placed so that `c` +/// is where it was, does too. +pub proof fn lemma_steps_embed( + pre: Seq, + c: Seq, + post: Seq, + base: int, + s: isa::State, + s2: isa::State, + n: nat, +) + requires + steps(c, base, s, n) == Some(s2), + ensures + steps(pre + c + post, base - code_len(pre), s, n) == Some(s2), +{ + let f = stepper(); + lemma_steps_is_gen(c, base, s, n); + lemma_steps_is_gen(pre + c + post, base - code_len(pre), s, n); + lemma_steps_g_embed(f, pre, c, post, base, s, s2, n); +} + +/// [`reaches`] in a piece is [`reaches`] in the whole. +pub proof fn lemma_reaches_embed( + pre: Seq, + c: Seq, + post: Seq, + base: int, + s: isa::State, + s2: isa::State, +) + requires + reaches(c, base, s, s2), + ensures + reaches(pre + c + post, base - code_len(pre), s, s2), +{ + let n = choose|n: nat| steps(c, base, s, n) == Some(s2); + lemma_steps_embed(pre, c, post, base, s, s2, n); +} + +/// The step function advances `pc` by 4 for every instruction that is not a +/// jump, from a `pc` at which that does not wrap. +pub open spec fn pc_plain(f: spec_fn(isa::State, isa::Instr) -> Option) -> bool { + forall|s: isa::State, i: isa::Instr| + !is_jump(i) && s.pc as int + 4 < isa::p2(64) ==> match #[trigger] f(s, i) { + Some(s1) => s1.pc as int == s.pc as int + 4, + None => true, + } +} + +/// The machine's step is [`pc_plain`]. +pub proof fn lemma_stepper_pc_plain() + ensures + pc_plain(stepper()), +{ + let f = stepper(); + assert forall|s: isa::State, i: isa::Instr| + !is_jump(i) && s.pc as int + 4 < isa::p2(64) implies match #[trigger] f(s, i) { + Some(s1) => s1.pc as int == s.pc as int + 4, + None => true, + } by { + match isa::step(s, i) { + Some(s1) => { + lemma_step_pc_plain(s, i, s1); + }, + None => {}, + } + } +} + +/// Generic [`lemma_run_seq_steps`]. +pub proof fn lemma_run_gen_steps( + f: spec_fn(isa::State, isa::Instr) -> Option, + pre: Seq, + code: Seq, + post: Seq, + b: int, + s: isa::State, + s2: isa::State, +) + requires + pc_plain(f), + no_jumps(code), + s.pc as int == b + code_len(pre), + s.pc as int + code_len(code) < isa::p2(64), + run_gen(f, s, code) == Some(s2), + ensures + steps_g(f, pre + code + post, b, s, code.len()) == Some(s2), + s2.pc as int == s.pc as int + code_len(code), + decreases code.len(), +{ + super::memlemmas::lemma_p2_consts(); + if code.len() == 0 { + } else { + let whole = pre + code + post; + let i = code[0]; + assert(!is_jump(i)); + let off: int = code_len(pre) as int; + assert(whole =~= pre + (code + post)); + lemma_fetch_append_r(pre, code + post, off); + assert(fetch(code + post, 0) == Some(i)); + assert(s.pc as int - b == off); + assert(s.pc as int + 4 < isa::p2(64)); + match f(s, i) { + Some(s1) => { + assert(s1.pc as int == s.pc as int + 4); + let pre1 = pre + seq![i]; + let rest = code.skip(1); + assert(code_len(pre1) == code_len(pre) + 4); + assert(pre1 + rest + post =~= whole); + assert(no_jumps(rest)) by { + assert forall|j: int| 0 <= j < rest.len() implies !is_jump( + #[trigger] rest[j], + ) by { + assert(rest[j] == code[j + 1]); + } + } + assert(code_len(code) == 4 + code_len(rest)); + lemma_run_gen_steps(f, pre1, rest, post, b, s1, s2); + }, + None => {}, + } + } +} + +/// **Jump-free code run by [`run_seq`] is run by [`steps`].** `whole` is +/// `pre + code + post` placed at `b`; the state is at the first byte of +/// `code`; the run of `code` by [`run_seq`] reaches `s2`. +pub proof fn lemma_run_seq_steps( + pre: Seq, + code: Seq, + post: Seq, + b: int, + s: isa::State, + s2: isa::State, +) + requires + no_jumps(code), + s.pc as int == b + code_len(pre), + s.pc as int + code_len(code) < isa::p2(64), + run_seq(s, code) == Some(s2), + ensures + steps(pre + code + post, b, s, code.len()) == Some(s2), + s2.pc as int == s.pc as int + code_len(code), +{ + let f = stepper(); + lemma_stepper_pc_plain(); + lemma_run_seq_is_gen(s, code); + lemma_run_gen_steps(f, pre, code, post, b, s, s2); + lemma_steps_is_gen(pre + code + post, b, s, code.len()); +} + +/// [`reaches`] in code followed by more. +pub proof fn lemma_reaches_post( + c: Seq, + post: Seq, + base: int, + s: isa::State, + s2: isa::State, +) + requires + reaches(c, base, s, s2), + ensures + reaches(c + post, base, s, s2), +{ + assert(Seq::::empty() + c + post =~= c + post); + assert(code_len(Seq::::empty()) == 0); + lemma_reaches_embed(Seq::empty(), c, post, base, s, s2); +} + +/// [`reaches`] in code preceded by more (the base moves back). +pub proof fn lemma_reaches_pre( + pre: Seq, + c: Seq, + base: int, + s: isa::State, + s2: isa::State, +) + requires + reaches(c, base, s, s2), + ensures + reaches(pre + c, base - code_len(pre), s, s2), +{ + assert(pre + c + Seq::::empty() =~= pre + c); + lemma_reaches_embed(pre, c, Seq::empty(), base, s, s2); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cftemplates.rs b/crates/clazel-jit/src/a64/cftemplates.rs new file mode 100644 index 000000000..acf58cdd3 --- /dev/null +++ b/crates/clazel-jit/src/a64/cftemplates.rs @@ -0,0 +1,361 @@ +//! The templates for structured control flow and memory instructions (jit.md +//! J.9, second slice, A64), as spec functions: [`ctmpl`] is the A64 code of a +//! [`Node`], given the offsets of the labels in scope; [`csize`] is its length +//! in bytes, which is what a `block`'s label offset is made from. The +//! refinement proofs are in [`crate::a64::cfproofs`]; the statement is +//! [`crate::a64::cfcontract::cf_refines`]. It is [`crate::cftemplates`]'s A64 +//! counterpart. +//! +//! The layout (jit.md J.9: "composition over structured control flow", with +//! the labels resolved to code offsets): +//! +//! - `then a b`: the code of `a`, then the code of `b`; `b`'s labels are `a`'s +//! shifted by the length of `a`'s code. +//! - `block b`: the code of `b`, with a new label 0 at the end of it. +//! - `loop b`: the code of `b`, with a new label 0 at its start. +//! - `if t e`: `ldr x0, [cond]; cbz w0, Lelse; t; b Lend; Lelse: e; Lend:`, +//! with a new label 0 at `Lend` in each arm. +//! - `br l`: `b` to label `l`. `br_if l`: `ldr x0, [cond]; cbnz w0` to label `l`. +//! - `exit c k`: the exit sequence; the operands stay in their frame slots. +//! - `br_table ls d`: `ldr x0, [index]`, then for each target `i` `movz w1, +//! #i; cmp w0, w1; b.eq`, then `b` to the default. +//! - a memory instruction: see [`mem_code`]. +//! +//! A `b`, `b.cond`, `cbz` or `cbnz` takes a displacement in words from the +//! address of the branch itself, so a branch at byte `pos` to the label at +//! `offs[l]` has displacement `(offs[l] - pos) / 4` (choice 16 of +//! [`crate::a64::cfspec`]). + +#[cfg(verus_only)] +use super::cfspec::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[cfg(verus_only)] +pub use crate::cftemplates::{flow_h, shift, zeros}; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// ---------------------------------------------------------------------------- +// Branch instructions +// ---------------------------------------------------------------------------- + +/// `b` to the byte `rel` after the branch itself (`rel` a multiple of 4). +pub open spec fn b_i(rel: int) -> isa::Instr { + isa::Instr::B { imm26: (rel / 4) as i32 } +} + +/// `b.cond cc` to the byte `rel` after the branch itself. +pub open spec fn bcond_i(cc: u8, rel: int) -> isa::Instr { + isa::Instr::BCond { cond: cc, imm19: (rel / 4) as i32 } +} + +/// `cbz w0` (`iszero`) or `cbnz w0` to the byte `rel` after the branch itself. +pub open spec fn cb_i(iszero: bool, rel: int) -> isa::Instr { + isa::Instr::CompareBranch { sf: isa::Width::W32, iszero, t: x0(), imm19: (rel / 4) as i32 } +} + +/// The condition test: `ldr x0, [top of stack]; cbz/cbnz w0, rel`, at height +/// `h` with `nl` locals. `rel` is from the branch (the second instruction). +pub open spec fn cond_pre(nl: nat, h: nat, iszero: bool, rel: int) -> Seq { + seq![load_slot(x0(), (nl + h - 1) as nat), cb_i(iszero, rel)] +} + +/// The length of [`cond_pre`] (it does not depend on `rel`). +pub open spec fn cond_len(nl: nat, h: nat) -> nat { + code_len(cond_pre(nl, h, true, 0)) +} + +// ---------------------------------------------------------------------------- +// The code of a node +// ---------------------------------------------------------------------------- + +/// **The code of a node** at height `h` with `nl` locals, the labels in scope +/// at the byte offsets `offs` from its first byte. +pub open spec fn ctmpl(n: Node, nl: nat, h: nat, offs: Seq) -> Seq + decreases n, 0nat, +{ + match n { + Node::Op(o) => tmpl(o, nl, h), + Node::Mem(m) => mem_code(m, nl, h), + Node::Then(a, b) => ctmpl(*a, nl, h, offs) + ctmpl( + *b, + nl, + flow_h(*a, nl, h, offs.len()), + shift(offs, csize(*a, nl, h, offs.len()) as int), + ), + Node::Block(b) => ctmpl(*b, nl, h, seq![csize(*b, nl, h, offs.len() + 1) as int] + offs), + Node::Loop(b) => ctmpl(*b, nl, h, seq![0int] + offs), + Node::If(t, e) => { + let st = csize(*t, nl, (h - 1) as nat, offs.len() + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, offs.len() + 1) as int; + cond_pre(nl, h, true, st + 8) + ctmpl( + *t, + nl, + (h - 1) as nat, + seq![st + 4 + se] + shift(offs, 8), + ) + seq![b_i(se + 4)] + ctmpl( + *e, + nl, + (h - 1) as nat, + seq![se] + shift(offs, 12 + st), + ) + }, + Node::Br(l) => seq![b_i(if l < offs.len() { + offs[l as int] + } else { + 0 + })], + Node::BrIf(l) => cond_pre(nl, h, false, if l < offs.len() { + offs[l as int] - 4 + } else { + 0 + }), + Node::BrTable(ls, d) => brt_code(ls@, d, nl, h, offs), + Node::Exit(c, _) => exit_seq(c as u16), + } +} + +// ---------------------------------------------------------------------------- +// Memory instructions +// ---------------------------------------------------------------------------- + +/// The access width of a value of type `ty`: `i32` 4 bytes, the others 8. +pub open spec fn mty_sf(ty: MTy) -> isa::Width { + match ty { + MTy::I32 => isa::Width::W32, + _ => isa::Width::W64, + } +} + +/// `ldr x3, [x28, #16 k]`: region `k`'s base, from the context's table. +pub open spec fn base_ld(k: nat) -> isa::Instr { + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: true, + t: x3(), + n: abi::ctx_reg(), + imm12: (abi::base_off(k as int) / 8) as u16, + } +} + +/// The bounds check of the validator +/// (`clazel_codecheck::a64::spec::check_seq`) on `x1`, with `x0` and `x2` as +/// its scratch registers, region `k`, `n` bytes, and the displacements `r1`, +/// `r2` (in words, from the branch itself) of its two exits. +pub open spec fn mem_chk(k: nat, n: nat, r1: int, r2: int) -> clazel_codecheck::a64::spec::Chk { + clazel_codecheck::a64::spec::Chk { + k: k as int, + n: n as int, + i: x1(), + t: x0(), + bnd: x2(), + r1: r1 as i32, + r2: r2 as i32, + } +} + +/// The region of a memory instruction. +pub open spec fn mem_region(m: MemOp) -> nat { + match m { + MemOp::BoundsCheck { region, .. } => region as nat, + MemOp::Load { region, .. } => region as nat, + MemOp::Store { region, .. } => region as nat, + } +} + +/// The number of bytes the check of a memory instruction admits. +pub open spec fn mem_n(m: MemOp) -> nat { + match m { + MemOp::BoundsCheck { size, .. } => size as nat, + MemOp::Load { ty, .. } => ty.size(), + MemOp::Store { ty, .. } => ty.size(), + } +} + +/// The exit code of the stub after a memory instruction. +pub open spec fn mem_exit_code(m: MemOp) -> u16 { + match m { + MemOp::BoundsCheck { code, .. } => code as u16, + _ => trap_code(), + } +} + +/// What a memory instruction does before its check: the operands into +/// registers (the index in `x1`, the value of a store in `x4`), and, for an +/// access, the region's base into `x3`. +pub open spec fn mem_pre(m: MemOp, nl: nat, h: nat) -> Seq { + match m { + MemOp::BoundsCheck { .. } => seq![load_slot(x1(), (nl + h - 1) as nat)], + MemOp::Load { region, .. } => seq![ + load_slot(x1(), (nl + h - 1) as nat), + base_ld(region as nat), + ], + MemOp::Store { region, .. } => seq![ + load_slot(x4(), (nl + h - 1) as nat), + load_slot(x1(), (nl + h - 2) as nat), + base_ld(region as nat), + ], + } +} + +/// What a memory instruction does after its check passed: the access, and for +/// a load the store of the result to the slot of the index. +pub open spec fn mem_tail(m: MemOp, nl: nat, h: nat) -> Seq { + match m { + MemOp::BoundsCheck { .. } => seq![], + MemOp::Load { ty, .. } => seq![ + isa::Instr::LoadStoreReg { + sf: mty_sf(ty), + load: true, + t: x0(), + n: x3(), + m: x1(), + s: false, + }, + store_slot((nl + h - 1) as nat, x0()), + ], + MemOp::Store { ty, .. } => seq![ + isa::Instr::LoadStoreReg { + sf: mty_sf(ty), + load: false, + t: x4(), + n: x3(), + m: x1(), + s: false, + }, + ], + } +} + +/// The displacement (in words, from the branch itself) of the check's first +/// exit, the `b.hs` at the third instruction of the check: over the rest of +/// the check (5 instructions), the tail and the `b` over the stub. +pub open spec fn mem_r1(m: MemOp, nl: nat, h: nat) -> int { + (5 + mem_tail(m, nl, h).len() + 1) as int +} + +/// The displacement of the check's second exit, the `b.hi` at the fifth +/// instruction. +pub open spec fn mem_r2(m: MemOp, nl: nat, h: nat) -> int { + (3 + mem_tail(m, nl, h).len() + 1) as int +} + +/// The bounds-check sequence of a memory instruction. +pub open spec fn mem_check(m: MemOp, nl: nat, h: nat) -> Seq { + clazel_codecheck::a64::spec::check_seq( + mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)), + ) +} + +/// **The code of a memory instruction** (choice 11 of [`crate::cfspec`]): the +/// operands and the region's base in registers; the validator's bounds check, +/// whose two exits go to the stub; the access; a `b` over the stub; the stub +/// (an exit sequence), so that check and access are one basic block. +pub open spec fn mem_code(m: MemOp, nl: nat, h: nat) -> Seq { + mem_pre(m, nl, h) + mem_check(m, nl, h) + mem_tail(m, nl, h) + seq![ + b_i(code_len(exit_seq(mem_exit_code(m))) as int + 4), + ] + exit_seq(mem_exit_code(m)) +} + +// ---------------------------------------------------------------------------- +// br_table +// ---------------------------------------------------------------------------- + +/// The chain's entry for target `i`: `movz w1, #i; cmp w0, w1; b.eq rel`, with +/// `rel` from the `b.eq`. +pub open spec fn brt_entry(i: nat, rel: int) -> Seq { + seq![ + isa::Instr::MoveWide { + sf: isa::Width::W32, + opcode: isa::MoveWideOp::Z, + d: x1(), + imm16: i as u16, + hw: 0, + }, + cmp_rr(isa::Width::W32, x0(), x1()), + bcond_i(0, rel), + ] +} + +/// The length of a chain entry (it does not depend on `i` or `rel`). +pub open spec fn brt_e() -> int { + code_len(brt_entry(0, 0)) as int +} + +/// The length of what comes before the chain: `ldr x0, [index]`. +pub open spec fn brt_p0(nl: nat, h: nat) -> int { + code_len(seq![load_slot(x0(), (nl + h - 1) as nat)]) as int +} + +/// The displacement to label `l` from a jump at byte `pos`. +pub open spec fn brt_rel(offs: Seq, l: usize, pos: int) -> int { + if l < offs.len() { + offs[l as int] - pos + } else { + 0 + } +} + +/// The chain from entry `i` on: entry `j` begins `p0 + j e` bytes in, and its +/// `b.eq` is its third instruction. +pub open spec fn brt_entries(ls: Seq, offs: Seq, p0: int, i: nat) -> Seq + decreases ls.len() - i, +{ + if i >= ls.len() { + seq![] + } else { + brt_entry(i, brt_rel(offs, ls[i as int], p0 + i * brt_e() + 8)) + brt_entries( + ls, + offs, + p0, + i + 1, + ) + } +} + +/// **The code of `br_table ls d`** (choice 10 of [`crate::cfspec`]): `ldr x0, +/// [index]`, a compare and `b.eq` per target, then a `b` to the default. +pub open spec fn brt_code(ls: Seq, d: usize, nl: nat, h: nat, offs: Seq) -> Seq< + isa::Instr, +> { + let p0 = brt_p0(nl, h); + seq![load_slot(x0(), (nl + h - 1) as nat)] + brt_entries(ls, offs, p0, 0) + seq![ + b_i(brt_rel(offs, d, p0 + ls.len() * brt_e())), + ] +} + +/// The length in bytes of a node's code (it does not depend on the values of +/// the label offsets, only on how many there are: +/// [`crate::a64::cflens::lemma_ctmpl_len`]). +pub open spec fn csize(n: Node, nl: nat, h: nat, depth: nat) -> nat + decreases n, 1nat, +{ + code_len(ctmpl(n, nl, h, zeros(depth))) +} + +/// The largest code this slice translates, in bytes: `2^19`, well below the +/// reach of a `b.cond` or `cbz` (1 MiB each way, 19-bit signed words), so +/// every displacement fits ([`crate::a64::cfproofs::labels_fit`]). +pub open spec fn max_size() -> nat { + 0x8_0000 +} + +/// The program `n` of a function with `nl` locals has a translation: its +/// frame holds the locals, its analysis, entered with an empty stack and the +/// return label in scope, is defined, every node in it has a template +/// ([`templated`]), every exit code is 16 bits ([`codes_ok`]), and the code is +/// at most [`max_size`] bytes. +pub open spec fn cf_prog_ok(n: Node, nl: nat) -> bool { + nl <= max_slots() && cf_flow(n, nl, 0, 1) is Some && templated(n) && codes_ok(n) && csize( + n, + nl, + 0, + 1, + ) <= max_size() +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfuacc.rs b/crates/clazel-jit/src/a64/cfuacc.rs new file mode 100644 index 000000000..415116a6a --- /dev/null +++ b/crates/clazel-jit/src/a64/cfuacc.rs @@ -0,0 +1,717 @@ +//! The validator accepts the units of every A64 template (jit.md item J.9, +//! "every emitted blob is accepted"): every unit of [`utmpl`] is well formed +//! ([`super::cfunits::uok`]) and the scan ([`ucheck`]) accepts them, whatever +//! it knew before, provided no branch lands on the start of a memory +//! instruction's check or access ([`prot`]). The counterpart of +//! [`crate::cfuacc`]. +//! +//! The one place the scan needs what it learnt earlier is the access of a +//! `load` or `store`: it needs `x3` to hold region `k`'s base (the load before +//! the check) and `x1` to be checked for the access's size (the check +//! itself), and nothing may have made it forget in between, which is what +//! [`prot`] rules out. + +#[cfg(verus_only)] +use super::cfaccept::*; +#[cfg(verus_only)] +use super::cfbs::*; +#[cfg(verus_only)] +use super::cflens::*; +#[cfg(verus_only)] +use super::cfsimple::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::cfunits::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every instruction of the list is simple. +pub open spec fn all_simp(c: Seq) -> bool { + forall|k: int| 0 <= k < c.len() ==> simp(#[trigger] c[k]) +} + +/// Single-instruction units of simple instructions are well formed. +pub proof fn lemma_ones_uok(c: Seq) + requires + forall|k: int| 0 <= k < c.len() ==> ok2(#[trigger] c[k]), + ensures + forall|k: int| 0 <= k < ones(c).len() ==> uok(#[trigger] ones(c)[k]), +{ + lemma_uflat_ones(c); + assert forall|k: int| 0 <= k < ones(c).len() implies uok(#[trigger] ones(c)[k]) by { + assert(ones(c)[k] == seq![c[k]]); + assert(ok2(c[k])); + } +} + +/// The scan accepts single-instruction units of simple instructions, whatever +/// it knew. +pub proof fn lemma_ones_ucheck( + c: Seq, + pos: int, + fs: clazel_codecheck::a64::spec::Facts, + p: spec_fn(int) -> bool, +) + requires + all_simp(c), + ensures + ucheck(ones(c), pos, fs, p) is Some, + decreases c.len(), +{ + if c.len() > 0 { + lemma_uflat_ones(c); + assert(ones(c)[0] == seq![c[0]]); + let fs0 = if p(pos) { + clazel_codecheck::a64::spec::no_facts() + } else { + fs + }; + lemma_simp_plain(fs0, c[0]); + let f = clazel_codecheck::a64::spec::plain_ok(fs0, c[0])->Some_0; + assert(ufacts(seq![c[0]], fs0) == Some(f)); + assert(ones(c).skip(1) =~= ones(c.skip(1))); + assert(all_simp(c.skip(1))) by { + assert forall|k: int| 0 <= k < c.skip(1).len() implies simp(#[trigger] c.skip(1)[k]) by { + assert(c.skip(1)[k] == c[k + 1]); + } + } + lemma_ones_ucheck(c.skip(1), pos + code_len(seq![c[0]]) as int, f, p); + } +} + +// ---------------------------------------------------------------------------- +// The simple instructions of the templates +// ---------------------------------------------------------------------------- + +/// The condition test is simple. +pub proof fn lemma_cond_pre_simp(nl: nat, h: nat, iszero: bool, rel: int) + requires + 1 <= h, + nl + h <= max_slots(), + ensures + all_simp(cond_pre(nl, h, iszero, rel)), +{ + assert(max_slots() == 31) by { + super::tlemmas::lemma_abi_consts_t(); + } + let c = cond_pre(nl, h, iszero, rel); + assert forall|k: int| 0 <= k < c.len() implies simp(#[trigger] c[k]) by { + assert(k == 0 || k == 1); + } +} + +/// A `b` is simple. +pub proof fn lemma_b_simp(rel: int) + ensures + all_simp(seq![b_i(rel)]), +{ + let c = seq![b_i(rel)]; + assert forall|k: int| 0 <= k < c.len() implies simp(#[trigger] c[k]) by { + assert(k == 0); + } +} + +/// The chain of `br_table` is simple. +pub proof fn lemma_entries_simp(ls: Seq, offs: Seq, p0: int, i: nat) + ensures + all_simp(brt_entries(ls, offs, p0, i)), + decreases ls.len() - i, +{ + if i < ls.len() { + let e = brt_entry(i, brt_rel(offs, ls[i as int], p0 + i * brt_e() + 8)); + assert forall|k: int| 0 <= k < e.len() implies simp(#[trigger] e[k]) by { + assert(k == 0 || k == 1 || k == 2); + } + lemma_entries_simp(ls, offs, p0, i + 1); + let r = brt_entries(ls, offs, p0, i + 1); + assert(brt_entries(ls, offs, p0, i) == e + r); + assert forall|k: int| 0 <= k < (e + r).len() implies simp(#[trigger] (e + r)[k]) by { + if k < e.len() { + assert((e + r)[k] == e[k]); + } else { + assert((e + r)[k] == r[k - e.len()]); + } + } + } else { + assert(brt_entries(ls, offs, p0, i) =~= Seq::::empty()); + } +} + +/// The whole `br_table` is simple. +pub proof fn lemma_brt_simp(ls: Seq, d: usize, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + ensures + all_simp(brt_code(ls, d, nl, h, offs)), +{ + assert(max_slots() == 31) by { + super::tlemmas::lemma_abi_consts_t(); + } + let p0 = brt_p0(nl, h); + let a = seq![load_slot(x0(), (nl + h - 1) as nat)]; + let ch = brt_entries(ls, offs, p0, 0); + let jm = seq![b_i(brt_rel(offs, d, p0 + ls.len() * brt_e()))]; + lemma_entries_simp(ls, offs, p0, 0); + assert(all_simp(a)) by { + assert forall|k: int| 0 <= k < a.len() implies simp(#[trigger] a[k]) by { + assert(k == 0); + } + } + lemma_b_simp(brt_rel(offs, d, p0 + ls.len() * brt_e())); + let whole = brt_code(ls, d, nl, h, offs); + assert(whole == a + ch + jm); + assert forall|k: int| 0 <= k < whole.len() implies simp(#[trigger] whole[k]) by { + if k < a.len() { + assert(whole[k] == a[k]); + } else if k < a.len() + ch.len() { + assert(whole[k] == ch[k - a.len()]); + } else { + assert(whole[k] == jm[k - a.len() - ch.len()]); + } + } +} + +// ---------------------------------------------------------------------------- +// Memory instructions +// ---------------------------------------------------------------------------- + +/// The validator reads the bounds-check sequence of the template as a unit. +pub proof fn lemma_check_unit(c: clazel_codecheck::a64::spec::Chk) + requires + clazel_codecheck::a64::spec::check_ok(c), + 0 <= c.n <= 8, + ensures + clazel_codecheck::a64::spec::check_unit_at(clazel_codecheck::a64::spec::check_seq(c), 0), + clazel_codecheck::a64::spec::check_params(clazel_codecheck::a64::spec::check_seq(c), 0) + == Some(c), +{ + let cs = clazel_codecheck::a64::spec::check_seq(c); + assert(cs[0] == clazel_codecheck::a64::spec::check_instr(0, c)); + assert(cs[1] == clazel_codecheck::a64::spec::check_instr(1, c)); + assert(cs[2] == clazel_codecheck::a64::spec::check_instr(2, c)); + assert(cs[3] == clazel_codecheck::a64::spec::check_instr(3, c)); + assert(cs[4] == clazel_codecheck::a64::spec::check_instr(4, c)); + assert(cs[5] == clazel_codecheck::a64::spec::check_instr(5, c)); + assert(cs[6] == clazel_codecheck::a64::spec::check_instr(6, c)); + let bo = abi::bound_off(c.k); + assert(bo == 16 * c.k + 8); + assert((bo / 8) as u16 as int == 2 * c.k + 1) by (nonlinear_arith) + requires + bo == 16 * c.k + 8, + 0 <= c.k < 16, + ; + assert((c.n as u16) as int == c.n); + let o = (bo / 8) as u16; + assert(((o as int) * 8 - 8) / 16 == c.k) by (nonlinear_arith) + requires + o as int == 2 * c.k + 1, + 0 <= c.k < 16, + ; + let c2 = clazel_codecheck::a64::spec::check_params(cs, 0)->Some_0; + assert(c2 == c); +} + +/// `fact_of` after one more binding. +pub proof fn lemma_fact_of_push( + fs: clazel_codecheck::a64::spec::Facts, + b: clazel_codecheck::a64::spec::Binding, + r: u8, +) + ensures + clazel_codecheck::a64::spec::fact_of(fs.push(b), r) == if b.reg == r { + b.fact + } else { + clazel_codecheck::a64::spec::fact_of(fs, r) + }, +{ + assert(fs.push(b).drop_last() =~= fs); + assert(fs.push(b).last() == b); +} + +/// The load of a region's base gives `x3` that region, whatever the scan +/// knew. +pub proof fn lemma_base_load(fs: clazel_codecheck::a64::spec::Facts, k: nat) + requires + k < 16, + ensures + clazel_codecheck::a64::spec::plain_ok(fs, base_ld(k)) == Some( + fs.push( + clazel_codecheck::a64::spec::Binding { + reg: 3, + fact: clazel_codecheck::a64::spec::Fact::Base(k as int), + }, + ), + ), +{ + let i = base_ld(k); + assert(abi::base_off(k as int) == 16 * k); + assert(((16 * k) / 8) as u16 as int == 2 * k) by (nonlinear_arith) + requires + k < 16, + ; + assert(clazel_codecheck::a64::spec::base_load_region(i) == Some(k as int)) by { + assert((2 * k * 8) / 16 == k) by (nonlinear_arith); + } + assert(clazel_codecheck::a64::spec::access_ok( + fs, + clazel_codecheck::a64::spec::mem_access(i)->Some_0, + )); +} + +/// What the scan knows after the check: `x3` is region `k`'s base, and `x1` +/// is checked for `n` bytes. Then an access of at most `n` bytes through +/// `[x3, x1]` is admitted. +pub proof fn lemma_access_after( + fs: clazel_codecheck::a64::spec::Facts, + k: int, + n: int, + size: int, + store: bool, + sf: isa::Width, + t: u8, +) + requires + size <= n, + clazel_codecheck::a64::spec::fact_of(fs, 3) == clazel_codecheck::a64::spec::Fact::Base(k), + ensures + clazel_codecheck::a64::spec::access_ok( + fs.push(clazel_codecheck::a64::spec::Binding { + reg: 0, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }).push( + clazel_codecheck::a64::spec::Binding { + reg: 2, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }, + ).push( + clazel_codecheck::a64::spec::Binding { + reg: 1, + fact: clazel_codecheck::a64::spec::Fact::Idx(k, n), + }, + ), + clazel_codecheck::a64::spec::Access { + base: 3, + index: Some(1u8), + scaled: false, + off: 0, + size: size, + store: store, + }, + ), +{ + let b0 = clazel_codecheck::a64::spec::Binding { + reg: 0, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }; + let b2 = clazel_codecheck::a64::spec::Binding { + reg: 2, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }; + let b1 = clazel_codecheck::a64::spec::Binding { + reg: 1, + fact: clazel_codecheck::a64::spec::Fact::Idx(k, n), + }; + let f1 = fs.push(b0); + let f2 = f1.push(b2); + let f3 = f2.push(b1); + lemma_fact_of_push(fs, b0, 3); + lemma_fact_of_push(f1, b2, 3); + lemma_fact_of_push(f2, b1, 3); + lemma_fact_of_push(f2, b1, 1); +} + +/// What the scan knows after the check, as the validator computes it from the +/// check's instructions. +proof fn lemma_check_facts_of( + c: clazel_codecheck::a64::spec::Chk, + fs: clazel_codecheck::a64::spec::Facts, +) + requires + clazel_codecheck::a64::spec::check_ok(c), + 0 <= c.n <= 8, + ensures + clazel_codecheck::a64::spec::check_facts( + fs, + clazel_codecheck::a64::spec::check_seq(c), + 0, + ) == fs.push( + clazel_codecheck::a64::spec::Binding { + reg: c.t, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }, + ).push( + clazel_codecheck::a64::spec::Binding { + reg: c.bnd, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }, + ).push( + clazel_codecheck::a64::spec::Binding { + reg: c.i, + fact: clazel_codecheck::a64::spec::Fact::Idx(c.k, c.n), + }, + ), +{ + lemma_check_unit(c); +} + +/// The check of a memory instruction is well formed for the validator. +pub proof fn lemma_mem_check_ok(m: MemOp, nl: nat, h: nat) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + ensures + clazel_codecheck::a64::spec::check_ok( + mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)), + ), + 0 <= mem_n(m) <= 8, + mem_region(m) < 16, +{ + assert(abi::reserved(0) == false && abi::reserved(1) == false && abi::reserved(2) == false); + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { ty, .. } => { + assert(ty.size() == 4 || ty.size() == 8); + }, + MemOp::Store { ty, .. } => { + assert(ty.size() == 4 || ty.size() == 8); + }, + } +} + +/// The scan over one unit. +pub proof fn lemma_ucheck_one( + u: Seq, + pos: int, + fs: clazel_codecheck::a64::spec::Facts, + p: spec_fn(int) -> bool, +) + ensures + ucheck(seq![u], pos, fs, p) == ufacts(u, if p(pos) { + clazel_codecheck::a64::spec::no_facts() + } else { + fs + }), +{ + assert(seq![u].skip(1) =~= Seq::>::empty()); + let fs0 = if p(pos) { + clazel_codecheck::a64::spec::no_facts() + } else { + fs + }; + match ufacts(u, fs0) { + None => {}, + Some(f) => { + assert(ucheck(Seq::>::empty(), pos + code_len(u) as int, f, p) == Some( + f, + )); + }, + } +} + +/// One instruction as a one-unit list. +pub proof fn lemma_ones_one(i: isa::Instr) + ensures + ones(seq![i]) =~= seq![seq![i]], +{ + lemma_uflat_ones(seq![i]); + assert(ones(seq![i]).len() == 1); + assert(ones(seq![i])[0] == seq![i]); +} + +/// `ones` of a concatenation. +proof fn lemma_ones_cat(a: Seq, b: Seq) + ensures + ones(a + b) =~= ones(a) + ones(b), +{ + lemma_uflat_ones(a); + lemma_uflat_ones(b); + lemma_uflat_ones(a + b); + assert forall|q: int| 0 <= q < ones(a + b).len() implies ones(a + b)[q] == (ones(a) + + ones(b))[q] by { + if q < a.len() { + assert((a + b)[q] == a[q]); + } else { + assert((a + b)[q] == b[q - a.len()]); + } + } +} + +/// The scan over the first part of a memory instruction: the operands and the +/// base. For a load or a store it ends knowing `x3` is region `k`'s base. +proof fn lemma_pre_ucheck( + m: MemOp, + nl: nat, + h: nat, + pos: int, + fs: clazel_codecheck::a64::spec::Facts, + p: spec_fn(int) -> bool, +) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + ensures + ucheck(ones(mem_pre(m, nl, h)), pos, fs, p) is Some, + !(m is BoundsCheck) ==> clazel_codecheck::a64::spec::fact_of( + ucheck(ones(mem_pre(m, nl, h)), pos, fs, p)->Some_0, + 3, + ) == clazel_codecheck::a64::spec::Fact::Base(mem_region(m) as int), +{ + assert(max_slots() == 31) by { + super::tlemmas::lemma_abi_consts_t(); + } + let pre = mem_pre(m, nl, h); + let k = mem_region(m); + match m { + MemOp::BoundsCheck { .. } => { + assert(all_simp(pre)) by { + assert forall|q: int| 0 <= q < pre.len() implies simp(#[trigger] pre[q]) by { + assert(q == 0); + } + } + lemma_ones_ucheck(pre, pos, fs, p); + }, + MemOp::Load { .. } | MemOp::Store { .. } => { + let last = base_ld(k); + let init = pre.drop_last(); + assert(pre.last() == last); + lemma_load_slot_straight(x1(), (nl + h - 1) as nat); + lemma_load_slot_straight(x4(), (nl + h - 1) as nat); + if m is Store { + lemma_load_slot_straight(x1(), (nl + h - 2) as nat); + } + assert(all_simp(init)) by { + assert forall|q: int| 0 <= q < init.len() implies simp(#[trigger] init[q]) by { + assert(init[q] == pre[q]); + assert(q == 0 || q == 1); + } + } + assert(pre =~= init + seq![last]); + lemma_ones_cat(init, seq![last]); + lemma_ones_one(last); + lemma_ucheck_cat(ones(init), ones(seq![last]), pos, fs, p); + lemma_ones_ucheck(init, pos, fs, p); + let f0 = ucheck(ones(init), pos, fs, p)->Some_0; + lemma_blen_ones(init); + let q = pos + code_len(init); + let fs0 = if p(q) { + clazel_codecheck::a64::spec::no_facts() + } else { + f0 + }; + lemma_base_load(fs0, k); + lemma_ucheck_one(seq![last], q, f0, p); + let b3 = clazel_codecheck::a64::spec::Binding { + reg: 3, + fact: clazel_codecheck::a64::spec::Fact::Base(k as int), + }; + assert(ufacts(seq![last], fs0) == Some(fs0.push(b3))); + lemma_fact_of_push(fs0, b3, 3); + }, + } +} + +/// The access of a load or a store is admitted after the check. +proof fn lemma_tail_ucheck( + m: MemOp, + nl: nat, + h: nat, + pos: int, + f1: clazel_codecheck::a64::spec::Facts, + p: spec_fn(int) -> bool, +) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + !(m is BoundsCheck) ==> clazel_codecheck::a64::spec::fact_of(f1, 3) + == clazel_codecheck::a64::spec::Fact::Base(mem_region(m) as int), + !p(pos), + ensures + ucheck( + ones(mem_tail(m, nl, h)), + pos, + f1.push(clazel_codecheck::a64::spec::Binding { + reg: 0, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }).push( + clazel_codecheck::a64::spec::Binding { + reg: 2, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }, + ).push( + clazel_codecheck::a64::spec::Binding { + reg: 1, + fact: clazel_codecheck::a64::spec::Fact::Idx( + mem_region(m) as int, + mem_n(m) as int, + ), + }, + ), + p, + ) is Some, +{ + assert(max_slots() == 31) by { + super::tlemmas::lemma_abi_consts_t(); + } + let tail = mem_tail(m, nl, h); + let k = mem_region(m) as int; + let n = mem_n(m) as int; + let f3 = f1.push( + clazel_codecheck::a64::spec::Binding { + reg: 0, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }, + ).push( + clazel_codecheck::a64::spec::Binding { + reg: 2, + fact: clazel_codecheck::a64::spec::Fact::Unknown, + }, + ).push( + clazel_codecheck::a64::spec::Binding { + reg: 1, + fact: clazel_codecheck::a64::spec::Fact::Idx(k, n), + }, + ); + match m { + MemOp::BoundsCheck { .. } => { + assert(tail =~= Seq::::empty()); + assert(ones(tail) =~= Seq::>::empty()); + assert(ucheck(ones(tail), pos, f3, p) == Some(f3)); + }, + MemOp::Load { ty, .. } => { + let acc = isa::Instr::LoadStoreReg { + sf: mty_sf(ty), + load: true, + t: x0(), + n: x3(), + m: x1(), + s: false, + }; + let st = store_slot((nl + h - 1) as nat, x0()); + assert(tail == seq![acc, st]); + lemma_access_after(f1, k, n, clazel_codecheck::a64::spec::nbytes(mty_sf(ty)), false, mty_sf(ty), x0()); + let a = clazel_codecheck::a64::spec::mem_access(acc)->Some_0; + assert(clazel_codecheck::a64::spec::access_ok(f3, a)); + assert(!clazel_codecheck::a64::spec::writes_reserved(acc)); + assert(clazel_codecheck::a64::spec::plain_ok(f3, acc) is Some); + let f4 = clazel_codecheck::a64::spec::plain_ok(f3, acc)->Some_0; + assert(tail =~= seq![acc] + seq![st]); + lemma_ones_cat(seq![acc], seq![st]); + lemma_ones_one(acc); + lemma_ucheck_cat(ones(seq![acc]), ones(seq![st]), pos, f3, p); + lemma_ucheck_one(seq![acc], pos, f3, p); + assert(ufacts(seq![acc], f3) == Some(f4)); + lemma_blen_ones(seq![acc]); + lemma_load_slot_straight(0, (nl + h - 1) as nat); + assert(all_simp(seq![st])) by { + assert forall|q: int| 0 <= q < 1 implies simp(#[trigger] seq![st][q]) by { + assert(q == 0); + } + } + lemma_ones_ucheck(seq![st], pos + code_len(seq![acc]) as int, f4, p); + }, + MemOp::Store { ty, .. } => { + let acc = isa::Instr::LoadStoreReg { + sf: mty_sf(ty), + load: false, + t: x4(), + n: x3(), + m: x1(), + s: false, + }; + assert(tail == seq![acc]); + lemma_access_after(f1, k, n, clazel_codecheck::a64::spec::nbytes(mty_sf(ty)), true, mty_sf(ty), x4()); + let a = clazel_codecheck::a64::spec::mem_access(acc)->Some_0; + assert(clazel_codecheck::a64::spec::access_ok(f3, a)); + assert(!clazel_codecheck::a64::spec::writes_reserved(acc)); + assert(clazel_codecheck::a64::spec::plain_ok(f3, acc) is Some); + lemma_ones_one(acc); + lemma_ucheck_one(seq![acc], pos, f3, p); + }, + } +} + +/// **A memory instruction is accepted**, whatever the scan knew, if no branch +/// lands on the start of its check or of its access. +pub proof fn lemma_uc_mem( + m: MemOp, + nl: nat, + h: nat, + pos: int, + fs: clazel_codecheck::a64::spec::Facts, + p: spec_fn(int) -> bool, +) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + !p(pos + code_len(mem_pre(m, nl, h))), + !p(pos + code_len(mem_pre(m, nl, h)) + code_len(mem_check(m, nl, h))), + ensures + ucheck(mem_units(m, nl, h), pos, fs, p) is Some, +{ + lemma_mem_check_ok(m, nl, h); + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![b_i(code_len(exit_seq(mem_exit_code(m))) as int + 4)]; + let stub = exit_seq(mem_exit_code(m)); + let c0 = mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)); + let k = mem_region(m) as int; + let n = mem_n(m) as int; + let b1 = code_len(pre) as int; + let b2 = b1 + code_len(chk) as int; + lemma_pre_ucheck(m, nl, h, pos, fs, p); + let f1 = ucheck(ones(pre), pos, fs, p)->Some_0; + lemma_blen_ones(pre); + // the check + lemma_check_unit(c0); + assert(chk == clazel_codecheck::a64::spec::check_seq(c0)); + lemma_check_facts_of(c0, f1); + let f2 = clazel_codecheck::a64::spec::check_facts(f1, chk, 0); + lemma_ucheck_one(chk, pos + b1, f1, p); + assert(ufacts(chk, f1) == Some(f2)); + lemma_blen_one(chk); + // the access + lemma_tail_ucheck(m, nl, h, pos + b2, f1, p); + let f3 = ucheck(ones(tail), pos + b2, f2, p)->Some_0; + lemma_blen_ones(tail); + // the b and the stub + lemma_b_simp(code_len(stub) as int + 4); + let q = pos + b2 + code_len(tail) as int; + let fj = if p(q) { + clazel_codecheck::a64::spec::no_facts() + } else { + f3 + }; + lemma_simp_plain(fj, jm[0]); + assert(ufacts(jm, fj) is Some); + assert(code_len(stub) == 12); + assert(seq![jm, stub] =~= seq![jm] + seq![stub]); + lemma_ucheck_cat(seq![jm], seq![stub], q, f3, p); + lemma_ucheck_one(jm, q, f3, p); + lemma_blen_one(jm); + let fj1 = ufacts(jm, fj)->Some_0; + lemma_ucheck_one(stub, q + code_len(jm) as int, fj1, p); + assert(stub.len() == 3); + // put the parts together + let u1 = ones(pre); + let u2 = seq![chk]; + let u3 = ones(tail); + let u4 = seq![jm, stub]; + assert(mem_units(m, nl, h) =~= u1 + u2 + u3 + u4); + lemma_ucheck_cat(u1, u2, pos, fs, p); + lemma_ucheck_cat(u1 + u2, u3, pos, fs, p); + lemma_ucheck_cat(u1 + u2 + u3, u4, pos, fs, p); + lemma_blen_cat(u1, u2); + lemma_blen_cat(u1 + u2, u3); + assert(blen(u1) == b1); + assert(blen(u1 + u2) == b2); + assert(blen(u1 + u2 + u3) == b2 + code_len(tail)); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfuacc2.rs b/crates/clazel-jit/src/a64/cfuacc2.rs new file mode 100644 index 000000000..09a88a2dc --- /dev/null +++ b/crates/clazel-jit/src/a64/cfuacc2.rs @@ -0,0 +1,680 @@ +//! The units of every A64 template are well formed and accepted by the +//! validator's scan, by induction over the program (jit.md item J.9, "every +//! emitted blob is accepted"): [`lemma_utmpl_uok`] and +//! [`lemma_utmpl_ucheck`]. The leaves are in [`super::cfuacc`]. The +//! counterpart of [`crate::cfuacc2`]. + +#[cfg(verus_only)] +use super::cfaccept::*; +#[cfg(verus_only)] +use super::cfbs::*; +#[cfg(verus_only)] +use super::cflens::*; +#[cfg(verus_only)] +use super::cfsimple::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::cfuacc::*; +#[cfg(verus_only)] +use super::cfunits::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every unit of the list is well formed. +pub open spec fn uok_all(us: Us) -> bool { + forall|k: int| 0 <= k < us.len() ==> uok(#[trigger] us[k]) +} + +/// Well-formed units followed by well-formed units. +pub proof fn lemma_uok_cat(a: Us, b: Us) + requires + uok_all(a), + uok_all(b), + ensures + uok_all(a + b), +{ + assert forall|k: int| 0 <= k < (a + b).len() implies uok(#[trigger] (a + b)[k]) by { + if k < a.len() { + assert((a + b)[k] == a[k]); + } else { + assert((a + b)[k] == b[k - a.len()]); + } + } +} + +/// A list of simple instructions is a list of well-formed units. +proof fn lemma_simp_uok(c: Seq) + requires + all_simp(c), + ensures + uok_all(ones(c)), +{ + assert forall|k: int| 0 <= k < c.len() implies ok2(#[trigger] c[k]) by { + assert(simp(c[k])); + } + lemma_ones_uok(c); +} + +/// An exit sequence is a well-formed unit. +pub proof fn lemma_exit_uok(c: u16) + ensures + uok(exit_seq(c)), +{ + let e = exit_seq(c); + assert(e.len() == 3); + assert(clazel_codecheck::a64::spec::exit_unit_at(e, 0)); +} + +/// A memory instruction's units are well formed. +proof fn lemma_mem_uok(m: MemOp, nl: nat, h: nat) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + ensures + uok_all(mem_units(m, nl, h)), +{ + assert(max_slots() == 31) by { + super::tlemmas::lemma_abi_consts_t(); + } + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![b_i(code_len(exit_seq(mem_exit_code(m))) as int + 4)]; + let stub = exit_seq(mem_exit_code(m)); + let c0 = mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)); + lemma_mem_check_ok(m, nl, h); + lemma_check_unit(c0); + assert(chk == clazel_codecheck::a64::spec::check_seq(c0)); + // operands + assert(forall|q: int| 0 <= q < pre.len() ==> ok2(#[trigger] pre[q])) by { + assert forall|q: int| 0 <= q < pre.len() implies ok2(pre[q]) by { + match m { + MemOp::BoundsCheck { .. } => { + assert(q == 0); + }, + MemOp::Load { .. } => { + assert(q == 0 || q == 1); + }, + MemOp::Store { .. } => { + assert(q == 0 || q == 1 || q == 2); + }, + } + } + } + lemma_ones_uok(pre); + assert(forall|q: int| 0 <= q < tail.len() ==> ok2(#[trigger] tail[q])) by { + assert forall|q: int| 0 <= q < tail.len() implies ok2(tail[q]) by { + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { .. } => { + assert(q == 0 || q == 1); + }, + MemOp::Store { .. } => { + assert(q == 0); + }, + } + } + } + lemma_ones_uok(tail); + lemma_exit_uok(mem_exit_code(m)); + let u2 = seq![chk]; + let u4 = seq![jm, stub]; + assert(uok_all(u2)) by { + assert forall|k: int| 0 <= k < u2.len() implies uok(#[trigger] u2[k]) by { + assert(k == 0); + assert(u2[0] == chk); + } + } + assert(uok_all(u4)) by { + assert forall|k: int| 0 <= k < u4.len() implies uok(#[trigger] u4[k]) by { + assert(k == 0 || k == 1); + } + } + lemma_uok_cat(ones(pre), u2); + lemma_uok_cat(ones(pre) + u2, ones(tail)); + lemma_uok_cat(ones(pre) + u2 + ones(tail), u4); +} + +/// **Every unit of a template is well formed.** +pub proof fn lemma_utmpl_uok(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + ensures + uok_all(utmpl(n, nl, h, offs)), + decreases n, +{ + let d = offs.len(); + match n { + Node::Op(o) => { + lemma_tmpl_straight(o, nl, h); + assert(all_simp(tmpl(o, nl, h))) by { + assert forall|k: int| 0 <= k < tmpl(o, nl, h).len() implies simp( + #[trigger] tmpl(o, nl, h)[k], + ) by { + assert(straight(tmpl(o, nl, h)[k])); + } + } + lemma_simp_uok(tmpl(o, nl, h)); + }, + Node::Mem(m) => { + assert(cf_flow(n, nl, h, 0) is Some); + lemma_mem_uok(m, nl, h); + }, + Node::Then(a, b) => { + let ob = shift(offs, csize(*a, nl, h, d) as int); + lemma_utmpl_uok(*a, nl, h, offs); + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + assert(ob.len() == d); + lemma_utmpl_uok(*b, nl, flow_h(*a, nl, h, d), ob); + lemma_uok_cat(utmpl(*a, nl, h, offs), utmpl(*b, nl, flow_h(*a, nl, h, d), ob)); + }, + Some(Flow::Dead) => { + assert(*b == Node::Op(Op::Nop)); + assert(utmpl(*b, nl, flow_h(*a, nl, h, d), ob) =~= Seq::< + Seq, + >::empty()); + assert(utmpl(*a, nl, h, offs) + utmpl(*b, nl, flow_h(*a, nl, h, d), ob) + =~= utmpl(*a, nl, h, offs)); + }, + None => {}, + } + }, + Node::Block(b) => { + let ob = seq![csize(*b, nl, h, d + 1) as int] + offs; + assert(ob.len() == d + 1); + lemma_utmpl_uok(*b, nl, h, ob); + }, + Node::Loop(b) => { + let ob = seq![0int] + offs; + assert(ob.len() == d + 1); + lemma_utmpl_uok(*b, nl, h, ob); + }, + Node::If(t, e) => { + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 4 + se] + shift(offs, 8); + let oe = seq![se] + shift(offs, 12 + st); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_utmpl_uok(*t, nl, (h - 1) as nat, ot); + lemma_utmpl_uok(*e, nl, (h - 1) as nat, oe); + let cp = cond_pre(nl, h, true, st + 8); + lemma_cond_pre_simp(nl, h, true, st + 8); + lemma_simp_uok(cp); + lemma_b_simp(se + 4); + lemma_simp_uok(seq![b_i(se + 4)]); + let jmu = seq![seq![b_i(se + 4)]]; + assert(uok_all(jmu)) by { + assert forall|k: int| 0 <= k < jmu.len() implies uok(#[trigger] jmu[k]) by { + assert(k == 0); + assert(ones(seq![b_i(se + 4)])[0] == seq![b_i(se + 4)]); + } + } + lemma_uok_cat(ones(cp), utmpl(*t, nl, (h - 1) as nat, ot)); + lemma_uok_cat(ones(cp) + utmpl(*t, nl, (h - 1) as nat, ot), jmu); + lemma_uok_cat( + ones(cp) + utmpl(*t, nl, (h - 1) as nat, ot) + jmu, + utmpl(*e, nl, (h - 1) as nat, oe), + ); + }, + Node::Br(l) => { + assert(ctmpl(n, nl, h, offs) == seq![b_i(if l < offs.len() { + offs[l as int] + } else { + 0 + })]); + lemma_b_simp(if l < offs.len() { + offs[l as int] + } else { + 0 + }); + lemma_simp_uok(ctmpl(n, nl, h, offs)); + }, + Node::BrIf(l) => { + let rel = if l < offs.len() { + offs[l as int] - 4 + } else { + 0 + }; + lemma_cond_pre_simp(nl, h, false, rel); + assert(ctmpl(n, nl, h, offs) == cond_pre(nl, h, false, rel)); + lemma_simp_uok(ctmpl(n, nl, h, offs)); + }, + Node::BrTable(ls, dd) => { + lemma_brt_simp(ls@, dd, nl, h, offs); + lemma_simp_uok(ctmpl(n, nl, h, offs)); + }, + Node::Exit(c, _) => { + lemma_exit_uok(c as u16); + let us = seq![exit_seq(c as u16)]; + assert(uok_all(us)) by { + assert forall|k: int| 0 <= k < us.len() implies uok(#[trigger] us[k]) by { + assert(k == 0); + } + } + }, + } +} + +// ---------------------------------------------------------------------------- +// The scan accepts them +// ---------------------------------------------------------------------------- + +/// No protected position of the node (starting at `pos`) is a branch target. +pub open spec fn pf(n: Node, nl: nat, h: nat, d: nat, pos: int, p: spec_fn(int) -> bool) -> bool { + forall|x: int| #[trigger] prot(n, nl, h, d, x) ==> !p(pos + x) +} + +/// The statement of [`lemma_utmpl_ucheck`] for a node: from any position, with +/// whatever the scan knew, and any set of branch targets that avoids +/// [`prot`], the scan accepts the units. +pub open spec fn uc_all(n: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) -> bool { + forall|pos: int, fs: clazel_codecheck::a64::spec::Facts| + pf(n, nl, h, offs.len(), pos, p) ==> #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some +} + +/// `then`. +proof fn lemma_uc_then( + a: Node, + b: Node, + nl: nat, + h: nat, + offs: Seq, + p: spec_fn(int) -> bool, +) + requires + cf_flow(Node::Then(Box::new(a), Box::new(b)), nl, h, offs.len()) is Some, + cf_flow(a, nl, h, offs.len()) is Some, + uc_all(a, nl, h, offs, p), + match cf_flow(a, nl, h, offs.len()) { + Some(Flow::Falls(h1)) => uc_all( + b, + nl, + flow_h(a, nl, h, offs.len()), + shift(offs, csize(a, nl, h, offs.len()) as int), + p, + ), + _ => true, + }, + ensures + uc_all(Node::Then(Box::new(a), Box::new(b)), nl, h, offs, p), +{ + let n = Node::Then(Box::new(a), Box::new(b)); + let d = offs.len(); + let sa = csize(a, nl, h, d); + let h1 = flow_h(a, nl, h, d); + let ob = shift(offs, sa as int); + lemma_utmpl_blen(a, nl, h, offs); + assert forall|pos: int, fs: clazel_codecheck::a64::spec::Facts| + pf(n, nl, h, d, pos, p) implies #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some by { + let ua = utmpl(a, nl, h, offs); + let ub = utmpl(b, nl, h1, ob); + assert(pf(a, nl, h, d, pos, p)) by { + assert forall|x: int| #[trigger] prot(a, nl, h, d, x) implies !p(pos + x) by { + assert(prot(n, nl, h, d, x)); + } + } + assert(ucheck(ua, pos, fs, p) is Some); + let f = ucheck(ua, pos, fs, p)->Some_0; + lemma_ucheck_cat(ua, ub, pos, fs, p); + match cf_flow(a, nl, h, d) { + Some(Flow::Falls(_)) => { + assert(ob.len() == d); + assert(pf(b, nl, h1, d, pos + sa as int, p)) by { + assert forall|y: int| #[trigger] prot(b, nl, h1, d, y) implies !p( + pos + sa as int + y, + ) by { + assert(prot(n, nl, h, d, y + sa as int)); + } + } + assert(ucheck(ub, pos + sa as int, f, p) is Some); + }, + Some(Flow::Dead) => { + assert(b == Node::Op(Op::Nop)); + assert(ub =~= Seq::>::empty()); + }, + None => {}, + } + } +} + +/// The part of `if` after the condition, from the scan's facts after it. +proof fn lemma_uc_if_rest( + t: Node, + e: Node, + nl: nat, + h: nat, + offs: Seq, + p: spec_fn(int) -> bool, + pos: int, + f1: clazel_codecheck::a64::spec::Facts, +) + requires + cf_flow(Node::If(Box::new(t), Box::new(e)), nl, h, offs.len()) is Some, + uc_all(t, nl, (h - 1) as nat, seq![ + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 4 + csize( + e, + nl, + (h - 1) as nat, + offs.len() + 1, + ) as int, + ] + shift(offs, 8), p), + uc_all(e, nl, (h - 1) as nat, seq![csize(e, nl, (h - 1) as nat, offs.len() + 1) as int] + + shift(offs, 12 + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int), p), + pf(Node::If(Box::new(t), Box::new(e)), nl, h, offs.len(), pos, p), + ensures + ucheck( + utmpl(t, nl, (h - 1) as nat, seq![ + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 4 + csize( + e, + nl, + (h - 1) as nat, + offs.len() + 1, + ) as int, + ] + shift(offs, 8)) + + seq![seq![b_i(csize(e, nl, (h - 1) as nat, offs.len() + 1) as int + 4)]] + + utmpl(e, nl, (h - 1) as nat, seq![ + csize(e, nl, (h - 1) as nat, offs.len() + 1) as int, + ] + shift(offs, 12 + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int)), + pos + 8, + f1, + p, + ) is Some, +{ + let n = Node::If(Box::new(t), Box::new(e)); + let d = offs.len(); + let h0 = (h - 1) as nat; + let st = csize(t, nl, h0, d + 1) as int; + let se = csize(e, nl, h0, d + 1) as int; + let ot = seq![st + 4 + se] + shift(offs, 8); + let oe = seq![se] + shift(offs, 12 + st); + assert(ot.len() == d + 1 && oe.len() == d + 1); + let ut = utmpl(t, nl, h0, ot); + let ue = utmpl(e, nl, h0, oe); + let jmu = seq![seq![b_i(se + 4)]]; + lemma_utmpl_blen(t, nl, h0, ot); + lemma_utmpl_blen(e, nl, h0, oe); + lemma_blen_one(seq![b_i(se + 4)]); + lemma_b_simp(se + 4); + assert(pf(t, nl, h0, d + 1, pos + 8, p)) by { + assert forall|x: int| #[trigger] prot(t, nl, h0, d + 1, x) implies !p(pos + 8 + x) by { + assert(prot(n, nl, h, d, x + 8)); + } + } + assert(ucheck(ut, pos + 8, f1, p) is Some); + let f2 = ucheck(ut, pos + 8, f1, p)->Some_0; + let q = pos + 8 + st; + let fj = if p(q) { + clazel_codecheck::a64::spec::no_facts() + } else { + f2 + }; + lemma_simp_plain(fj, b_i(se + 4)); + lemma_ucheck_one(seq![b_i(se + 4)], q, f2, p); + let f3 = ufacts(seq![b_i(se + 4)], fj)->Some_0; + assert(pf(e, nl, h0, d + 1, pos + (12 + st), p)) by { + assert forall|x: int| #[trigger] prot(e, nl, h0, d + 1, x) implies !p(pos + (12 + st) + x) by { + assert(prot(n, nl, h, d, x + (12 + st))); + } + } + assert(ucheck(ue, pos + (12 + st), f3, p) is Some); + lemma_ucheck_cat(ut, jmu + ue, pos + 8, f1, p); + lemma_ucheck_cat(jmu, ue, pos + 8 + st, f2, p); + assert(ucheck(jmu, q, f2, p) == Some(f3)) by { + lemma_ucheck_one(seq![b_i(se + 4)], q, f2, p); + } + assert(blen(jmu) == 4); + assert(ut + jmu + ue =~= ut + (jmu + ue)); +} + +/// `if`. +proof fn lemma_uc_if(t: Node, e: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + cf_flow(Node::If(Box::new(t), Box::new(e)), nl, h, offs.len()) is Some, + uc_all(t, nl, (h - 1) as nat, seq![ + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 4 + csize( + e, + nl, + (h - 1) as nat, + offs.len() + 1, + ) as int, + ] + shift(offs, 8), p), + uc_all(e, nl, (h - 1) as nat, seq![csize(e, nl, (h - 1) as nat, offs.len() + 1) as int] + + shift(offs, 12 + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int), p), + ensures + uc_all(Node::If(Box::new(t), Box::new(e)), nl, h, offs, p), +{ + let n = Node::If(Box::new(t), Box::new(e)); + let d = offs.len(); + let h0 = (h - 1) as nat; + let st = csize(t, nl, h0, d + 1) as int; + let se = csize(e, nl, h0, d + 1) as int; + let ot = seq![st + 4 + se] + shift(offs, 8); + let oe = seq![se] + shift(offs, 12 + st); + assert(ot.len() == d + 1 && oe.len() == d + 1); + let cp = cond_pre(nl, h, true, st + 8); + let ut = utmpl(t, nl, h0, ot); + let ue = utmpl(e, nl, h0, oe); + let jmu = seq![seq![b_i(se + 4)]]; + lemma_blen_ones(cp); + lemma_cond_pre_simp(nl, h, true, st + 8); + assert(code_len(cp) == 8); + assert(utmpl(n, nl, h, offs) =~= ones(cp) + (ut + jmu + ue)); + assert forall|pos: int, fs: clazel_codecheck::a64::spec::Facts| + pf(n, nl, h, d, pos, p) implies #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some by { + let a1 = ones(cp); + lemma_ones_ucheck(cp, pos, fs, p); + let f1 = ucheck(a1, pos, fs, p)->Some_0; + lemma_uc_if_rest(t, e, nl, h, offs, p, pos, f1); + lemma_ucheck_cat(a1, ut + jmu + ue, pos, fs, p); + } +} + +/// `block`. +proof fn lemma_uc_block(b: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + uc_all(b, nl, h, seq![csize(b, nl, h, offs.len() + 1) as int] + offs, p), + ensures + uc_all(Node::Block(Box::new(b)), nl, h, offs, p), +{ + let n = Node::Block(Box::new(b)); + let ob = seq![csize(b, nl, h, offs.len() + 1) as int] + offs; + assert(ob.len() == offs.len() + 1); + assert forall|pos: int, fs: clazel_codecheck::a64::spec::Facts| + pf(n, nl, h, offs.len(), pos, p) implies #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some by { + assert(pf(b, nl, h, ob.len(), pos, p)) by { + assert forall|x: int| #[trigger] prot(b, nl, h, ob.len(), x) implies !p(pos + x) by { + assert(prot(n, nl, h, offs.len(), x)); + } + } + } +} + +/// `loop`. +proof fn lemma_uc_loop(b: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + uc_all(b, nl, h, seq![0int] + offs, p), + ensures + uc_all(Node::Loop(Box::new(b)), nl, h, offs, p), +{ + let n = Node::Loop(Box::new(b)); + let ob = seq![0int] + offs; + assert(ob.len() == offs.len() + 1); + assert forall|pos: int, fs: clazel_codecheck::a64::spec::Facts| + pf(n, nl, h, offs.len(), pos, p) implies #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some by { + assert(pf(b, nl, h, ob.len(), pos, p)) by { + assert forall|x: int| #[trigger] prot(b, nl, h, ob.len(), x) implies !p(pos + x) by { + assert(prot(n, nl, h, offs.len(), x)); + } + } + } +} + +/// A node whose units are simple instructions, or an exit sequence. +proof fn lemma_uc_leaf(n: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + cf_flow(n, nl, h, offs.len()) is Some, + n is Op || n is Br || n is BrIf || n is BrTable || n is Exit, + ensures + uc_all(n, nl, h, offs, p), +{ + assert forall|pos: int, fs: clazel_codecheck::a64::spec::Facts| + pf(n, nl, h, offs.len(), pos, p) implies #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some by { + match n { + Node::Op(o) => { + lemma_tmpl_straight(o, nl, h); + assert(all_simp(tmpl(o, nl, h))) by { + assert forall|k: int| 0 <= k < tmpl(o, nl, h).len() implies simp( + #[trigger] tmpl(o, nl, h)[k], + ) by { + assert(straight(tmpl(o, nl, h)[k])); + } + } + lemma_ones_ucheck(tmpl(o, nl, h), pos, fs, p); + }, + Node::Br(l) => { + let rel = if l < offs.len() { + offs[l as int] + } else { + 0 + }; + assert(ctmpl(n, nl, h, offs) == seq![b_i(rel)]); + lemma_b_simp(rel); + lemma_ones_ucheck(ctmpl(n, nl, h, offs), pos, fs, p); + }, + Node::BrIf(l) => { + let rel = if l < offs.len() { + offs[l as int] - 4 + } else { + 0 + }; + lemma_cond_pre_simp(nl, h, false, rel); + assert(ctmpl(n, nl, h, offs) == cond_pre(nl, h, false, rel)); + lemma_ones_ucheck(ctmpl(n, nl, h, offs), pos, fs, p); + }, + Node::BrTable(ls, dd) => { + lemma_brt_simp(ls@, dd, nl, h, offs); + lemma_ones_ucheck(ctmpl(n, nl, h, offs), pos, fs, p); + }, + Node::Exit(c, _) => { + lemma_ucheck_one(exit_seq(c as u16), pos, fs, p); + assert(exit_seq(c as u16).len() == 3); + }, + _ => {}, + } + } +} + +/// A memory instruction. +proof fn lemma_uc_mem_node(m: MemOp, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + cf_flow(Node::Mem(m), nl, h, offs.len()) is Some, + ensures + uc_all(Node::Mem(m), nl, h, offs, p), +{ + let n = Node::Mem(m); + let d = offs.len(); + assert(cf_flow(n, nl, h, 0) is Some); + assert forall|pos: int, fs: clazel_codecheck::a64::spec::Facts| + pf(n, nl, h, d, pos, p) implies #[trigger] ucheck(utmpl(n, nl, h, offs), pos, fs, p) is Some by { + let x1 = code_len(mem_pre(m, nl, h)) as int; + let x2 = (code_len(mem_pre(m, nl, h)) + code_len(mem_check(m, nl, h))) as int; + assert(prot(n, nl, h, d, x1)); + assert(prot(n, nl, h, d, x2)); + lemma_uc_mem(m, nl, h, pos, fs, p); + } +} + +/// **The scan accepts the units of every template.** +pub proof fn lemma_utmpl_ucheck(n: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + cf_flow(n, nl, h, offs.len()) is Some, + ensures + uc_all(n, nl, h, offs, p), + decreases n, +{ + let d = offs.len(); + match n { + Node::Mem(m) => { + lemma_uc_mem_node(m, nl, h, offs, p); + }, + Node::Then(a, b) => { + let sa = csize(*a, nl, h, d); + let ob = shift(offs, sa as int); + lemma_utmpl_ucheck(*a, nl, h, offs, p); + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + assert(ob.len() == d); + lemma_utmpl_ucheck(*b, nl, h1, ob, p); + }, + _ => {}, + } + lemma_uc_then(*a, *b, nl, h, offs, p); + }, + Node::Block(b) => { + let ob = seq![csize(*b, nl, h, d + 1) as int] + offs; + assert(ob.len() == d + 1); + lemma_utmpl_ucheck(*b, nl, h, ob, p); + lemma_uc_block(*b, nl, h, offs, p); + }, + Node::Loop(b) => { + let ob = seq![0int] + offs; + assert(ob.len() == d + 1); + lemma_utmpl_ucheck(*b, nl, h, ob, p); + lemma_uc_loop(*b, nl, h, offs, p); + }, + Node::If(t, e) => { + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 4 + se] + shift(offs, 8); + let oe = seq![se] + shift(offs, 12 + st); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_utmpl_ucheck(*t, nl, (h - 1) as nat, ot, p); + lemma_utmpl_ucheck(*e, nl, (h - 1) as nat, oe, p); + lemma_uc_if(*t, *e, nl, h, offs, p); + }, + _ => { + lemma_uc_leaf(n, nl, h, offs, p); + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfunits.rs b/crates/clazel-jit/src/a64/cfunits.rs new file mode 100644 index 000000000..feed73556 --- /dev/null +++ b/crates/clazel-jit/src/a64/cfunits.rs @@ -0,0 +1,505 @@ +//! Units: the bridge from a list of the validator's units to its `scan` +//! (jit.md item J.9, "every emitted blob is accepted", x86-64). +//! +//! The validator's `scan` ([`clazel_codecheck::a64::spec::scan`]) walks the +//! instruction list from the front, recognizing an exit sequence (3 +//! instructions), a bounds-check sequence (7) or a single instruction at each +//! unit start, and forgets what it knows at each jump target. This module +//! says: **if the instruction list is the concatenation of a list of units, +//! each of which is of one of those three shapes, then `scan` walks exactly +//! those units** ([`lemma_scan_units`]), and what it holds at each unit is +//! computed by [`ucheck`], a recursion over the unit list, not over +//! instruction positions. The back end's proof then works on unit lists that +//! mirror its templates (`crate::cfaccept`). +//! +//! The one place `scan` looks past a unit is a single instruction, where it +//! must rule out an exit sequence and a bounds-check sequence starting +//! there. Both need the instruction two places on to be a `ret` and a `jb` +//! (`jcc` 2): no instruction at the first or second place of any unit is +//! either ([`ok2`], [`lemma_uok_ok2`]), so neither starts at a single +//! instruction ([`lemma_scan_units`]). + +#[cfg(verus_only)] +use super::runseq::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A list of units: each is a list of instructions the validator treats as one +/// step of its scan. +pub type Us = Seq>; + +/// All the instructions of the units, in order. +pub open spec fn uflat(us: Us) -> Seq + decreases us.len(), +{ + if us.len() == 0 { + Seq::empty() + } else { + us[0] + uflat(us.skip(1)) + } +} + +/// The length in bytes of all the units. +pub open spec fn blen(us: Us) -> nat + decreases us.len(), +{ + if us.len() == 0 { + 0 + } else { + code_len(us[0]) + blen(us.skip(1)) + } +} + +/// One unit per instruction. +pub open spec fn ones(c: Seq) -> Us { + c.map_values(|i: isa::Instr| seq![i]) +} + +/// `jcc` with condition code 2 (`jb`): the second instruction of a +/// bounds-check sequence. +pub open spec fn jcc2(i: isa::Instr) -> bool { + i is BCond && i->BCond_cond == 2 +} + +/// An instruction that cannot be the third instruction of an exit sequence +/// (a `ret`) or of a bounds-check sequence (a `jcc 2`). +pub open spec fn ok2(i: isa::Instr) -> bool { + !(i is Ret) && !jcc2(i) +} + +/// A unit is well formed: a single instruction that is neither a `ret` nor a +/// `jcc 2`, an exit sequence, or a bounds-check sequence (both as the +/// validator recognizes them). +pub open spec fn uok(u: Seq) -> bool { + (u.len() == 1 && ok2(u[0])) || (u.len() == 3 && clazel_codecheck::a64::spec::exit_unit_at(u, 0)) + || (u.len() == 7 && clazel_codecheck::a64::spec::check_unit_at(u, 0)) +} + +/// What the scan holds after a unit, from what it held before. +pub open spec fn ufacts(u: Seq, fs: clazel_codecheck::a64::spec::Facts) -> Option< + clazel_codecheck::a64::spec::Facts, +> { + if u.len() == 1 { + clazel_codecheck::a64::spec::plain_ok(fs, u[0]) + } else if u.len() == 3 { + Some(clazel_codecheck::a64::spec::no_facts()) + } else if u.len() == 7 { + Some(clazel_codecheck::a64::spec::check_facts(fs, u, 0)) + } else { + None + } +} + +/// The scan over the units from byte position `pos` with facts `fs`, where +/// `p` says which positions are jump targets (the scan forgets its facts +/// there). `None` if some unit is refused. +pub open spec fn ucheck( + us: Us, + pos: int, + fs: clazel_codecheck::a64::spec::Facts, + p: spec_fn(int) -> bool, +) -> Option + decreases us.len(), +{ + if us.len() == 0 { + Some(fs) + } else { + let fs0 = if p(pos) { + clazel_codecheck::a64::spec::no_facts() + } else { + fs + }; + match ufacts(us[0], fs0) { + None => None, + Some(f) => ucheck(us.skip(1), pos + code_len(us[0]) as int, f, p), + } + } +} + +/// The unit starts the scan reports. +pub open spec fn ustarts(us: Us, pos: int) -> Seq + decreases us.len(), +{ + if us.len() == 0 { + Seq::empty() + } else { + seq![pos] + ustarts(us.skip(1), pos + code_len(us[0]) as int) + } +} + +/// `x` is the start of a unit. +pub open spec fn ub(us: Us, pos: int, x: int) -> bool + decreases us.len(), +{ + us.len() > 0 && (x == pos || ub(us.skip(1), pos + code_len(us[0]) as int, x)) +} + +/// `x` is the start of a unit, or the end of the last. +pub open spec fn ubi(us: Us, pos: int, x: int) -> bool { + ub(us, pos, x) || x == pos + blen(us) +} + +// ---------------------------------------------------------------------------- +// Lists of units +// ---------------------------------------------------------------------------- + +/// The instructions of a concatenation of unit lists. +pub proof fn lemma_uflat_cat(a: Us, b: Us) + ensures + uflat(a + b) == uflat(a) + uflat(b), + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + assert(uflat(a) + uflat(b) =~= uflat(b)); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + lemma_uflat_cat(a.skip(1), b); + assert(uflat(a + b) =~= a[0] + (uflat(a.skip(1)) + uflat(b))); + assert(uflat(a) =~= a[0] + uflat(a.skip(1))); + } +} + +/// The length of a concatenation of unit lists. +pub proof fn lemma_blen_cat(a: Us, b: Us) + ensures + blen(a + b) == blen(a) + blen(b), + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + lemma_blen_cat(a.skip(1), b); + } +} + +/// The length of the units is the length of their instructions. +pub proof fn lemma_blen_flat(us: Us) + ensures + blen(us) == code_len(uflat(us)), + decreases us.len(), +{ + if us.len() > 0 { + lemma_blen_flat(us.skip(1)); + lemma_code_len_append(us[0], uflat(us.skip(1))); + } +} + +/// The instructions of a list of single-instruction units are the list. +pub proof fn lemma_uflat_ones(c: Seq) + ensures + uflat(ones(c)) =~= c, + ones(c).len() == c.len(), + forall|k: int| 0 <= k < c.len() ==> #[trigger] ones(c)[k] == seq![c[k]], + decreases c.len(), +{ + if c.len() > 0 { + assert(ones(c).skip(1) =~= ones(c.skip(1))); + lemma_uflat_ones(c.skip(1)); + assert(ones(c)[0] == seq![c[0]]); + assert(uflat(ones(c)) =~= seq![c[0]] + c.skip(1)); + } +} + +/// The length of a one-unit list. +pub proof fn lemma_blen_one(u: Seq) + ensures + blen(seq![u]) == code_len(u), +{ + assert(seq![u].skip(1) =~= Seq::>::empty()); + assert(blen(Seq::>::empty()) == 0); +} + +/// The length of a two-unit list. +pub proof fn lemma_blen_two(u: Seq, v: Seq) + ensures + blen(seq![u, v]) == code_len(u) + code_len(v), +{ + assert(seq![u, v].skip(1) =~= seq![v]); + lemma_blen_one(v); +} + +/// The length of single-instruction units. +pub proof fn lemma_blen_ones(c: Seq) + ensures + blen(ones(c)) == code_len(c), +{ + lemma_uflat_ones(c); + lemma_blen_flat(ones(c)); +} + +// ---------------------------------------------------------------------------- +// The scan over a concatenation +// ---------------------------------------------------------------------------- + +/// The scan over a concatenation is the scan over the first, then over the +/// second from where the first ended, with the facts it ended with. +pub proof fn lemma_ucheck_cat( + a: Us, + b: Us, + pos: int, + fs: clazel_codecheck::a64::spec::Facts, + p: spec_fn(int) -> bool, +) + ensures + ucheck(a + b, pos, fs, p) == match ucheck(a, pos, fs, p) { + None => None, + Some(f) => ucheck(b, pos + blen(a), f, p), + }, + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + let fs0 = if p(pos) { + clazel_codecheck::a64::spec::no_facts() + } else { + fs + }; + match ufacts(a[0], fs0) { + None => {}, + Some(f) => { + lemma_ucheck_cat(a.skip(1), b, pos + code_len(a[0]) as int, f, p); + }, + } + } +} + +/// The unit starts of a concatenation. +pub proof fn lemma_ub_cat(a: Us, b: Us, pos: int, x: int) + ensures + ub(a + b, pos, x) == (ub(a, pos, x) || ub(b, pos + blen(a), x)), + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + lemma_ub_cat(a.skip(1), b, pos + code_len(a[0]) as int, x); + } +} + +/// A boundary of either part is a boundary of the concatenation. +pub proof fn lemma_ubi_cat(a: Us, b: Us, pos: int, x: int) + requires + ubi(a, pos, x) || ubi(b, pos + blen(a), x), + ensures + ubi(a + b, pos, x), +{ + lemma_ub_cat(a, b, pos, x); + lemma_blen_cat(a, b); + if x == pos + blen(a) && b.len() > 0 { + // the end of `a` is the first unit start of `b` + assert(ub(b, pos + blen(a), x)); + } +} + +/// The scan reports unit starts, and `ub` is membership in them. +pub proof fn lemma_seq_has_ustarts(us: Us, pos: int, x: int) + ensures + clazel_codecheck::a64::spec::seq_has(ustarts(us, pos), x) == ub(us, pos, x), + decreases us.len(), +{ + if us.len() > 0 { + let rest = ustarts(us.skip(1), pos + code_len(us[0]) as int); + clazel_codecheck::a64::acc_lemmas::lemma_seq_has_concat(seq![pos], rest, x); + clazel_codecheck::a64::acc_lemmas::lemma_seq_has_single(pos, x); + lemma_seq_has_ustarts(us.skip(1), pos + code_len(us[0]) as int, x); + } else { + reveal_with_fuel(clazel_codecheck::a64::spec::seq_has, 1); + } +} + +// ---------------------------------------------------------------------------- +// Positions +// ---------------------------------------------------------------------------- + +/// The byte position of instruction `j + m` is that of `j` plus the length of +/// the `m` instructions between (4 bytes each). +pub proof fn lemma_start_of_run(ins: Seq, j: int, u: Seq, m: int) + requires + 0 <= j, + 0 <= m <= u.len(), + j + m <= ins.len(), + ensures + clazel_codecheck::a64::spec::start_of(ins, j + m) == clazel_codecheck::a64::spec::start_of( + ins, + j, + ) + 4 * m, +{ +} + +/// `start_of` of a whole unit. +pub proof fn lemma_start_of_unit(ins: Seq, j: int, u: Seq) + requires + 0 <= j, + j + u.len() <= ins.len(), + ensures + clazel_codecheck::a64::spec::start_of(ins, j + u.len()) == clazel_codecheck::a64::spec::start_of( + ins, + j, + ) + code_len(u), +{ +} + +/// A unit's first two instructions are neither a `ret` nor a `jcc 2`. +pub proof fn lemma_uok_ok2(u: Seq, i: int) + requires + uok(u), + 0 <= i <= 1, + i < u.len(), + ensures + ok2(u[i]), +{ + if u.len() == 7 { + let c = clazel_codecheck::a64::spec::check_params(u, 0)->Some_0; + assert(clazel_codecheck::a64::spec::check_unit_at(u, 0)); + assert(u[0] == clazel_codecheck::a64::spec::check_instr(0, c)); + assert(u[1] == clazel_codecheck::a64::spec::check_instr(1, c)); + } +} + +/// The units' lengths are 1, 3 or 7. +pub proof fn lemma_uok_len(u: Seq) + requires + uok(u), + ensures + u.len() == 1 || u.len() == 3 || u.len() == 7, +{ +} + +// ---------------------------------------------------------------------------- +// The bridge +// ---------------------------------------------------------------------------- + +/// The instruction two places on from a single-instruction unit is not a +/// `ret` or a `jcc 2` (or there are fewer than three instructions left). +proof fn lemma_lookahead(ins: Seq, j: int, us: Us) + requires + 0 <= j <= ins.len(), + ins.skip(j) =~= uflat(us), + us.len() >= 1, + us[0].len() == 1, + forall|k: int| 0 <= k < us.len() ==> uok(us[k]), + ensures + j + 2 >= ins.len() || ok2(ins[j + 2]), +{ + let rest = us.skip(1); + if rest.len() == 0 { + assert(uflat(us) =~= us[0] + uflat(rest)); + assert(uflat(rest).len() == 0); + assert(ins.skip(j).len() == 1); + } + if rest.len() >= 1 && j + 2 < ins.len() { + let u1 = rest[0]; + assert(uok(u1)); + lemma_uok_len(u1); + let r1 = rest.skip(1); + assert(uflat(rest) =~= u1 + uflat(r1)); + assert(uflat(us) =~= us[0] + uflat(rest)); + assert(uflat(us) =~= us[0] + (u1 + uflat(r1))); + if u1.len() >= 2 { + lemma_uok_ok2(u1, 1); + assert(ins[j + 2] == uflat(us)[2]); + assert(uflat(us)[2] == u1[1]); + } else { + assert(r1.len() >= 1) by { + assert(uflat(us).len() == 1 + 1 + uflat(r1).len()); + } + let u2 = r1[0]; + assert(uok(u2)); + lemma_uok_ok2(u2, 0); + assert(uflat(r1) =~= u2 + uflat(r1.skip(1))); + assert(ins[j + 2] == uflat(us)[2]); + assert(uflat(us)[2] == u2[0]); + } + } +} + +/// **The scan walks the units.** If the instructions from `j` on are those of +/// the units, every unit is well formed, `p` is membership in the scan's +/// targets, and [`ucheck`] accepts the units, then `scan` accepts them and +/// reports their starts. +pub proof fn lemma_scan_units( + ins: Seq, + tg: Seq, + j: int, + us: Us, + pos: int, + fs: clazel_codecheck::a64::spec::Facts, + p: spec_fn(int) -> bool, +) + requires + 0 <= j <= ins.len(), + ins.skip(j) =~= uflat(us), + clazel_codecheck::a64::spec::start_of(ins, j) == pos, + forall|k: int| 0 <= k < us.len() ==> uok(us[k]), + forall|x: int| clazel_codecheck::a64::spec::seq_has(tg, x) == p(x), + ucheck(us, pos, fs, p) is Some, + ensures + clazel_codecheck::a64::spec::scan(ins, tg, j, fs) == Some(ustarts(us, pos)), + decreases us.len(), +{ + if us.len() == 0 { + assert(uflat(us).len() == 0); + assert(ins.skip(j).len() == 0); + } else { + let u = us[0]; + let rest = us.skip(1); + assert(uok(u)); + lemma_uok_len(u); + assert(uflat(us) =~= u + uflat(rest)); + assert forall|i: int| 0 <= i < u.len() implies ins[j + i] == #[trigger] u[i] by { + assert(ins.skip(j)[i] == uflat(us)[i]); + } + assert(j + u.len() <= ins.len()); + let fs0 = if p(pos) { + clazel_codecheck::a64::spec::no_facts() + } else { + fs + }; + let f = ufacts(u, fs0)->Some_0; + assert(ufacts(u, fs0) is Some); + lemma_start_of_unit(ins, j, u); + // which unit does the scan find at `j`? + if u.len() == 3 { + assert(clazel_codecheck::a64::spec::exit_unit_at(ins, j)); + } else if u.len() == 7 { + let c = clazel_codecheck::a64::spec::check_params(u, 0)->Some_0; + assert(clazel_codecheck::a64::spec::check_unit_at(u, 0)); + assert(u[0] == clazel_codecheck::a64::spec::check_instr(0, c)); + assert(ins[j] == u[0]); + assert(!clazel_codecheck::a64::spec::exit_unit_at(ins, j)); + assert(clazel_codecheck::a64::spec::check_params(ins, j) == clazel_codecheck::a64::spec::check_params( + u, + 0, + )); + assert(clazel_codecheck::a64::spec::check_unit_at(ins, j)); + assert(clazel_codecheck::a64::spec::check_facts(fs0, ins, j) + == clazel_codecheck::a64::spec::check_facts(fs0, u, 0)); + } else { + lemma_lookahead(ins, j, us); + assert(!clazel_codecheck::a64::spec::exit_unit_at(ins, j)); + assert(!clazel_codecheck::a64::spec::check_unit_at(ins, j)); + } + let ul = clazel_codecheck::a64::spec::unit_at(ins, j, fs0)->Some_0; + assert(clazel_codecheck::a64::spec::unit_at(ins, j, fs0) is Some); + assert(ul.len == u.len() && ul.facts == f); + assert(ins.skip(j + u.len()) =~= ins.skip(j).skip(u.len() as int)); + assert((u + uflat(rest)).skip(u.len() as int) =~= uflat(rest)); + assert(ins.skip(j + u.len()) =~= uflat(rest)); + lemma_scan_units(ins, tg, j + u.len(), rest, pos + code_len(u) as int, f, p); + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/cfvalid.rs b/crates/clazel-jit/src/a64/cfvalid.rs new file mode 100644 index 000000000..b21ea4a3a --- /dev/null +++ b/crates/clazel-jit/src/a64/cfvalid.rs @@ -0,0 +1,205 @@ +//! Every instruction the control-flow templates use is in the A64 subset +//! ([`isa::valid_instr`]): the emitter's blob is decoded by the validator only +//! if it is. A branch is in the subset only if its displacement fits (26 bits +//! for `b`, 19 for the conditional ones, in words), which is why this file +//! has the leaf lemmas and [`crate::a64::cfproofs`] carries the size invariant +//! that discharges them. The A64 counterpart of [`crate::cfvalid`]. + +#[cfg(verus_only)] +use super::cffit::*; +#[cfg(verus_only)] +use super::cfspec::*; +#[cfg(verus_only)] +use super::cftemplates::*; +#[cfg(verus_only)] +use super::contract::*; +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use super::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Valid code followed by valid code is valid. +pub proof fn lemma_valid_cat(a: Seq, b: Seq) + requires + all_valid_instrs(a), + all_valid_instrs(b), + ensures + all_valid_instrs(a + b), +{ + super::tlemmas::lemma_valid_concat(a, b); +} + +/// The two powers of two the branch fields need. +pub proof fn lemma_p2_fields() + ensures + isa::p2(18) == 0x4_0000, + isa::p2(25) == 0x200_0000, +{ + vstd::arithmetic::power2::lemma2_to64(); + vstd::arithmetic::power2::lemma2_to64_rest(); +} + +/// `b` to a displacement that fits is valid. +pub proof fn lemma_b_valid(rel: int) + requires + -0x800_0000 <= rel < 0x800_0000, + ensures + isa::valid_instr(b_i(rel)), +{ + lemma_p2_fields(); + assert(((rel / 4) as i32) as int == rel / 4); +} + +/// The condition test is valid code, when its slot is in the frame and the +/// branch reaches. +pub proof fn lemma_cond_pre_valid(nl: nat, h: nat, iszero: bool, rel: int) + requires + 1 <= h, + nl + h <= max_slots(), + -0x10_0000 <= rel < 0x10_0000, + ensures + all_valid_instrs(cond_pre(nl, h, iszero, rel)), +{ + lemma_p2_fields(); + super::tlemmas::lemma_ls_valid(0, (nl + h - 1) as nat); + assert(((rel / 4) as i32) as int == rel / 4); + let c = cond_pre(nl, h, iszero, rel); + assert forall|k: int| 0 <= k < c.len() implies isa::valid_instr(#[trigger] c[k]) by { + assert(k == 0 || k == 1); + } +} + +/// The bounds check for any region, size and small exit displacements is +/// valid code. +pub proof fn lemma_check_valid(k: nat, n: nat, r1: int, r2: int) + requires + k < 16, + 1 <= n <= 8, + 0 <= r1 < 0x100, + 0 <= r2 < 0x100, + ensures + all_valid_instrs(clazel_codecheck::a64::spec::check_seq(mem_chk(k, n, r1, r2))), +{ + lemma_p2_fields(); + super::steps::lemma_abi_consts(); + let c = mem_chk(k, n, r1, r2); + let s = clazel_codecheck::a64::spec::check_seq(c); + assert forall|j: int| 0 <= j < s.len() implies isa::valid_instr(#[trigger] s[j]) by { + assert(j == 0 || j == 1 || j == 2 || j == 3 || j == 4 || j == 5 || j == 6); + assert(s[j] == clazel_codecheck::a64::spec::check_instr(j, c)); + assert(abi::bound_off(k as int) / 8 == 2 * k + 1); + } +} + +/// **A memory instruction's code is valid.** +pub proof fn lemma_mem_valid(m: MemOp, nl: nat, h: nat) + requires + nl + h <= max_slots(), + match m { + MemOp::BoundsCheck { region, size, code } => 1 <= h && region < 16 && 1 <= size <= 8 + && code < 0x1_0000, + MemOp::Load { region, ty } => 1 <= h && region < 16, + MemOp::Store { region, ty } => 2 <= h && region < 16, + }, + ensures + all_valid_instrs(mem_code(m, nl, h)), +{ + lemma_p2_fields(); + super::steps::lemma_abi_consts(); + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let stub = exit_seq(mem_exit_code(m)); + let bb = seq![b_i(code_len(stub) as int + 4)]; + super::tlemmas::lemma_exit_valid(mem_exit_code(m)); + lemma_b_valid(16); + assert(code_len(stub) == 12); + assert(all_valid_instrs(bb)) by { + assert forall|j: int| 0 <= j < bb.len() implies isa::valid_instr(#[trigger] bb[j]) by { + assert(j == 0); + } + } + assert(mem_r1(m, nl, h) == 6 + tail.len() && mem_r2(m, nl, h) == 4 + tail.len()); + lemma_check_valid( + mem_region(m), + mem_n(m), + mem_r1(m, nl, h), + mem_r2(m, nl, h), + ); + let k = mem_region(m); + assert(k < 16); + assert(all_valid_instrs(pre)) by { + assert forall|j: int| 0 <= j < pre.len() implies isa::valid_instr(#[trigger] pre[j]) by { + match m { + MemOp::BoundsCheck { .. } => { + super::tlemmas::lemma_ls_valid(1, (nl + h - 1) as nat); + }, + MemOp::Load { .. } => { + super::tlemmas::lemma_ls_valid(1, (nl + h - 1) as nat); + }, + MemOp::Store { .. } => { + super::tlemmas::lemma_ls_valid(4, (nl + h - 1) as nat); + super::tlemmas::lemma_ls_valid(1, (nl + h - 2) as nat); + }, + } + } + } + assert(all_valid_instrs(tail)) by { + assert forall|j: int| 0 <= j < tail.len() implies isa::valid_instr(#[trigger] tail[j]) by { + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { .. } => { + super::tlemmas::lemma_ls_valid(0, (nl + h - 1) as nat); + }, + MemOp::Store { .. } => {}, + } + } + } + lemma_valid_cat(pre, mem_check(m, nl, h)); + lemma_valid_cat(pre + mem_check(m, nl, h), tail); + lemma_valid_cat(pre + mem_check(m, nl, h) + tail, bb); + lemma_valid_cat(pre + mem_check(m, nl, h) + tail + bb, stub); +} + +/// **`br_table`'s code is valid**, when its labels are in reach. +pub proof fn lemma_brt_valid(ls: Seq, d: usize, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + ls.len() <= max_targets(), + d < offs.len(), + forall|q: int| 0 <= q < ls.len() ==> #[trigger] ls[q] < offs.len(), + labels_fit(offs, 8 + 12 * ls.len()), + ensures + all_valid_instrs(brt_code(ls, d, nl, h, offs)), +{ + lemma_p2_fields(); + super::cfbrt::lemma_brt_shape(ls, d, nl, h, offs); + super::tlemmas::lemma_ls_valid(0, (nl + h - 1) as nat); + let w = brt_code(ls, d, nl, h, offs); + let n = ls.len(); + assert forall|q: int| 0 <= q < w.len() implies isa::valid_instr(#[trigger] w[q]) by { + if q == 0 { + } else if q == 1 + 3 * n { + let rel = brt_rel(offs, d, (4 + n * 12) as int); + assert(-0x800_0000 <= rel < 0x800_0000); + lemma_b_valid(rel); + } else { + let i = ((q - 1) / 3) as nat; + let t = (q - 1) % 3; + assert(i < n); + assert(q == 1 + 3 * i + t); + let rel = brt_rel(offs, ls[i as int], (4 + i * 12 + 8) as int); + assert(-0x10_0000 <= rel < 0x10_0000); + assert(((rel / 4) as i32) as int == rel / 4); + assert(w[(1 + 3 * i) as int + t] == brt_entry(i, rel)[t]); + assert(i < 0x1_0000); + assert(t == 0 || t == 1 || t == 2); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/compose.rs b/crates/clazel-jit/src/a64/compose.rs new file mode 100644 index 000000000..c8c97e722 --- /dev/null +++ b/crates/clazel-jit/src/a64/compose.rs @@ -0,0 +1,128 @@ +//! Composition: a straight-line program's code refines the program +//! ([`lemma_prog_refines`]), by induction on its length from the refinement of +//! each template. The A64 counterpart of [`crate::compose`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// [`preserved`] is transitive. +pub proof fn lemma_preserved_trans(m: isa::State, m1: isa::State, m2: isa::State) + requires + preserved(m, m1), + preserved(m1, m2), + ensures + preserved(m, m2), +{ + super::steps::lemma_abi_consts(); + assert(ctx_of(m1) == ctx_of(m)); + assert forall|b: int| #[trigger] m.mem.dom().contains(b) && !in_exit_area(ctx_of(m), b) + implies m2.mem[b] == m.mem[b] by { + assert(m1.mem.dom().contains(b)); + assert(m1.mem[b] == m.mem[b]); + assert(m2.mem[b] == m1.mem[b]); + } +} + +/// A program's stack height after it is the one the static analysis computes. +pub proof fn lemma_prog_height( + ops: Seq, + nl: nat, + h0: nat, + lay: lir::Layout, + a: lir::St, + a2: lir::St, + fuel: nat, +) + requires + prog_ok(ops, nl, h0), + a.stack.len() == h0, + lir::eval(lay, prog_instr(ops), a, fuel) == lir::Outcome::Normal(a2), + ensures + a2.stack.len() == height_after(ops, h0), + decreases ops.len(), +{ + if ops.len() == 0 { + assert(a2 == a); + } else { + let pre = ops.drop_last(); + let last = ops.last(); + assert(fuel > 0); + match lir::eval(lay, prog_instr(pre), a, (fuel - 1) as nat) { + lir::Outcome::Normal(a1) => { + lemma_prog_height(pre, nl, h0, lay, a, a1, (fuel - 1) as nat); + super::proofs::lemma_op_height(last, nl, height_after(pre, h0), lay, a1, a2, (fuel - 1) as nat); + }, + _ => { + assert(false); + }, + } + } +} + +/// **A straight-line program of templates refines the program** (the +/// left-nested `then` chain of its instructions), when every instruction has a +/// template ([`prog_ok`]). +#[verifier::rlimit(500)] +pub proof fn lemma_prog_refines(ops: Seq, nl: nat, h0: nat) + requires + prog_ok(ops, nl, h0), + ensures + super::contract::refines(prog_instr(ops), nl, h0, super::templates::code_of(ops, nl, h0)), + decreases ops.len(), +{ + super::memlemmas::lemma_p2_consts(); + if ops.len() == 0 { + super::proofs::lemma_op_refines(Op::Nop, nl, h0); + assert(prog_instr(ops) == Op::Nop@); + assert(super::templates::code_of(ops, nl, h0) == super::templates::tmpl(Op::Nop, nl, h0)); + } else { + let pre = ops.drop_last(); + let last = ops.last(); + let hl = height_after(pre, h0); + let ca = super::templates::code_of(pre, nl, h0); + let cb = super::templates::tmpl(last, nl, hl); + lemma_prog_refines(pre, nl, h0); + super::proofs::lemma_op_refines(last, nl, hl); + assert(super::templates::code_of(ops, nl, h0) == ca + cb); + assert forall|lay: lir::Layout, a: lir::St, m: isa::State, fuel: nat| + #![trigger lir::eval(lay, prog_instr(ops), a, fuel), run_seq(m, ca + cb)] + a.stack.len() == h0 && rel(lay, a, m, nl) && (m.pc as int) + code_len(ca + cb) + < isa::p2(64) implies match lir::eval(lay, prog_instr(ops), a, fuel) { + lir::Outcome::Normal(a2) => super::contract::refines_post(lay, a2, m, nl, ca + cb, run_seq(m, ca + cb)), + _ => true, + } by { + match lir::eval(lay, prog_instr(ops), a, fuel) { + lir::Outcome::Normal(a2) => { + assert(fuel > 0); + let f = (fuel - 1) as nat; + // prog_instr(ops) = then(prog_instr(pre), last@) + match lir::eval(lay, prog_instr(pre), a, f) { + lir::Outcome::Normal(a1) => { + assert(lir::eval(lay, last@, a1, f) == lir::Outcome::Normal(a2)); + assert((m.pc as int) + code_len(ca) < isa::p2(64)); + super::proofs::lemma_use_refines(prog_instr(pre), nl, h0, ca, lay, a, m, f); + let m1 = run_seq(m, ca).unwrap(); + lemma_prog_height(pre, nl, h0, lay, a, a1, f); + assert(a1.stack.len() == hl); + assert((m1.pc as int) + code_len(cb) < isa::p2(64)); + super::proofs::lemma_use_refines(last@, nl, hl, cb, lay, a1, m1, f); + let m2 = run_seq(m1, cb).unwrap(); + super::runseq::lemma_run_seq_append(m, ca, cb); + lemma_preserved_trans(m, m1, m2); + }, + _ => { + assert(false); + }, + } + }, + _ => {}, + } + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/contract.rs b/crates/clazel-jit/src/a64/contract.rs new file mode 100644 index 000000000..06b3346cc --- /dev/null +++ b/crates/clazel-jit/src/a64/contract.rs @@ -0,0 +1,153 @@ +//! What the A64 back end must be proven to satisfy (CLAUDE.md rule 3, +//! docs/SPEC-STYLE.md): the refinement statements, and one `requires` and one +//! `ensures` predicate for each method of [`super::api::Backend`], in terms of +//! [`super::spec`] and the two semantics it relates. These are written before +//! any template; reviewing them and `spec` is enough to know what is claimed. +//! They are [`crate::contract`]'s statements with the A64 machine. +//! +//! # The refinement statement +//! +//! [`refines`]`(i, nl, h, code)` says that the A64 instruction list `code` is a +//! correct translation of the LIR instruction `i`, for a function with `nl` +//! locals, when the operand stack has `h` values. For every layout, every LIR +//! state `a` and machine state `m` related by [`rel`]: +//! +//! - if LIR evaluates `i` in `a` (with any fuel) and **falls through** to the +//! state `a2` (a `Stuck` or `OutOfFuel` outcome is not claimed about, and +//! `i` is not required to be well typed: if it is not, LIR is `Stuck`, and +//! the statement says nothing), +//! - and the end of the code does not wrap the address space, +//! - then running `code` from `m` ([`run_seq`]) does not fault, ends in a +//! machine state `m2` related to `a2` by [`rel`], with `pc` after the code, +//! and `m2` differs from `m` only in the exit area ([`preserved`]). +//! +//! That is a **forward simulation of one LIR step by one template**. The exit +//! is [`exit_refines`]; failed steps and branches belong to the control-flow +//! templates, which this slice does not have. A straight-line program is one +//! [`refines`] over [`prog_instr`]. +//! +//! # What is not stated +//! +//! - That the output is **accepted by the A64 validator.** +//! `tests/validator_a64.rs` checks it for every template and generated +//! programs, by running the executable validator. It is not a theorem here. +//! - That a *failed* LIR step is matched by the machine: no template in this +//! slice has one. +//! - Anything about [`rel`]'s `pc`, flags or scratch registers. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// What the machine must be in after `code` ran from `m`, for the LIR state +/// `a2` that `i` reached: no fault, related, `pc` after the code, and nothing +/// outside the exit area changed. +pub open spec fn refines_post( + lay: lir::Layout, + a2: lir::St, + m: isa::State, + nl: nat, + code: Seq, + r: Option, +) -> bool { + match r { + Some(m2) => rel(lay, a2, m2, nl) && m2.pc as int == m.pc as int + code_len(code) + && preserved(m, m2), + None => false, + } +} + +/// `code` refines the LIR instruction `i` at stack height `h` with `nl` +/// locals (see the module doc). +pub open spec fn refines(i: lir::Instr, nl: nat, h: nat, code: Seq) -> bool { + forall|lay: lir::Layout, a: lir::St, m: isa::State, fuel: nat| + #![trigger lir::eval(lay, i, a, fuel), run_seq(m, code)] + a.stack.len() == h && rel(lay, a, m, nl) && (m.pc as int) + code_len(code) + < isa::p2(64) ==> match lir::eval(lay, i, a, fuel) { + lir::Outcome::Normal(a2) => refines_post(lay, a2, m, nl, code, run_seq(m, code)), + _ => true, + } +} + +/// The exit sequence for `c` refines LIR's `exit c` with no operands: running +/// the three instructions does not fault; the machine ends at the return +/// address the enter stub left in the link register, with the exit code `c` in +/// the context's exit-code word, the frame and the regions as they were +/// ([`rel`] still holds, and the memory outside the exit area is unchanged), +/// and the link register and `SP` as they were. +pub open spec fn exit_refines(c: u16, nl: nat) -> bool { + forall|lay: lir::Layout, a: lir::St, m: isa::State, fuel: nat| + #![trigger lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, fuel), run_seq(m, exit_seq(c))] + rel(lay, a, m, nl) && (m.pc as int) + code_len(exit_seq(c)) < isa::p2(64) + ==> match lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, fuel) { + lir::Outcome::Exit(code, _, a2) => match run_seq(m, exit_seq(c)) { + Some(m2) => rel(lay, a2, m2, nl) && preserved(m, m2) && isa::mem_read( + m2.mem, + ctx_of(m) + abi::exit_code_off(), + 8, + ) == Some(code as int) && m2.pc == m.r[abi::link_reg() as int], + None => false, + }, + _ => true, + } +} + +// ---------------------------------------------------------------------------- +// The emitter's contracts (the executable back end, `api::Backend`) +// ---------------------------------------------------------------------------- + +/// The instructions of `code` are all in the A64 subset ([`isa::valid_instr`]). +pub open spec fn all_valid_instrs(code: Seq) -> bool { + forall|j: int| 0 <= j < code.len() ==> isa::valid_instr(#[trigger] code[j]) +} + +/// Precondition of `emit_body` and `emit_blob`: none. Every program and every +/// local count is a legal input (one outside the first slice, or too large +/// for the frame, is answered `None`). +pub open spec fn emit_requires(ops: Seq, nl: usize) -> bool { + true +} + +/// Postcondition of `emit_body`: the program has a translation exactly when +/// [`prog_ok`] says every instruction has a template; the translation is in +/// the subset and refines the program's LIR instruction. +pub open spec fn emit_body_ensures( + ops: Seq, + nl: usize, + r: Option>, +) -> bool { + match r { + Some(code) => prog_ok(ops, nl as nat, 0) && all_valid_instrs(code) && refines( + prog_instr(ops), + nl as nat, + 0, + code, + ), + None => !prog_ok(ops, nl as nat, 0), + } +} + +/// Postcondition of `emit_blob`: the bytes are exactly the canonical +/// encodings ([`clazel_codecheck::a64::spec::decodes_to`], which is what the +/// validator reads back) of a body that refines the program, followed by the +/// exit sequence for `exit_code`. `None` exactly when the program has no +/// translation. +pub open spec fn emit_blob_ensures( + ops: Seq, + nl: usize, + exit_code: u16, + r: Option>, +) -> bool { + match r { + Some(bytes) => prog_ok(ops, nl as nat, 0) && exists|body: Seq| + #![trigger all_valid_instrs(body)] + all_valid_instrs(body) && refines(prog_instr(ops), nl as nat, 0, body) + && clazel_codecheck::a64::spec::decodes_to(bytes, body + exit_seq(exit_code)), + None => !prog_ok(ops, nl as nat, 0), + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/emit.rs b/crates/clazel-jit/src/a64/emit.rs new file mode 100644 index 000000000..3cca36609 --- /dev/null +++ b/crates/clazel-jit/src/a64/emit.rs @@ -0,0 +1,1015 @@ +//! The executable A64 back end: [`A64Backend`] implements +//! [`super::api::Backend`], proven to return exactly the templates' code +//! ([`super::contract::emit_body_ensures`]) and, through `clazel-a64`'s encoder, +//! its canonical encoding ([`super::contract::emit_blob_ensures`]). It is +//! [`crate::emit`]'s counterpart: the same loop, with the A64 instructions. +//! +//! Arithmetic is checked (the workspace denies the unchecked operators), so +//! the helpers saturate where the proofs show they cannot. + +use super::api::Backend; +#[allow(unused_imports)] +use super::spec::*; +use clazel_a64::encoder::encode_instr; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// ---------------------------------------------------------------------------- +// Checked arithmetic +// ---------------------------------------------------------------------------- + +fn add_u(a: usize, b: usize) -> (r: usize) + requires + a + b <= usize::MAX, + ensures + r == a + b, +{ + a.saturating_add(b) +} + +fn sub_u(a: usize, b: usize) -> (r: usize) + requires + b <= a, + ensures + r == a - b, +{ + a.saturating_sub(b) +} + +// ---------------------------------------------------------------------------- +// Instructions +// ---------------------------------------------------------------------------- + +/// The operand size of a template: 32 or 64 bits. +fn wd(w32: bool) -> (r: isa::Width) + ensures + r == (if w32 { + isa::Width::W32 + } else { + isa::Width::W64 + }), +{ + if w32 { + isa::Width::W32 + } else { + isa::Width::W64 + } +} + +fn slot_imm_exec(j: usize) -> (d: u16) + requires + j < 31, + ensures + d == slot_imm(j as nat), +{ + proof { + super::tlemmas::lemma_abi_consts_t(); + super::steps::lemma_abi_consts(); + } + add_u(j, 33) as u16 +} + +pub(crate) fn ex_load(dst: u8, j: usize) -> (i: isa::Instr) + requires + j < 31, + ensures + i == super::templates::load_slot(dst, j as nat), +{ + proof { + super::steps::lemma_abi_consts(); + } + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: true, + t: dst, + n: 28, + imm12: slot_imm_exec(j), + } +} + +pub(crate) fn ex_store(j: usize, src: u8) -> (i: isa::Instr) + requires + j < 31, + ensures + i == super::templates::store_slot(j as nat, src), +{ + proof { + super::steps::lemma_abi_consts(); + } + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: false, + t: src, + n: 28, + imm12: slot_imm_exec(j), + } +} + +fn chunk_exec(v: u64, k: usize) -> (r: u16) + requires + k < 4, + ensures + r == super::templates::chunk(v, k as nat), +{ + if k == 0 { + (v % 0x1_0000) as u16 + } else if k == 1 { + ((v / 0x1_0000) % 0x1_0000) as u16 + } else if k == 2 { + ((v / 0x1_0000_0000) % 0x1_0000) as u16 + } else { + (v / 0x1_0000_0000_0000) as u16 + } +} + +fn ex_movz(w32: bool, imm16: u16, hw: u8) -> (i: isa::Instr) + ensures + i == super::templates::movz0(if w32 { + isa::Width::W32 + } else { + isa::Width::W64 + }, imm16, hw), +{ + isa::Instr::MoveWide { sf: wd(w32), opcode: isa::MoveWideOp::Z, d: 0, imm16, hw } +} + +fn ex_movk(w32: bool, imm16: u16, hw: u8) -> (i: isa::Instr) + ensures + i == super::templates::movk0(if w32 { + isa::Width::W32 + } else { + isa::Width::W64 + }, imm16, hw), +{ + isa::Instr::MoveWide { sf: wd(w32), opcode: isa::MoveWideOp::K, d: 0, imm16, hw } +} + +fn cset_cond_exec(c: &lir::ICmp) -> (r: u8) + ensures + r == super::templates::cset_cond(*c), +{ + match c { + lir::ICmp::Eq => 1, + lir::ICmp::Ne => 0, + lir::ICmp::LtS => 10, + lir::ICmp::LtU => 2, + lir::ICmp::GtS => 13, + lir::ICmp::GtU => 9, + lir::ICmp::LeS => 12, + lir::ICmp::LeU => 8, + lir::ICmp::GeS => 11, + lir::ICmp::GeU => 3, + } +} + +fn int_op_exec(o: &lir::IBin, w32: bool) -> (i: isa::Instr) + ensures + i == super::templates::int_op_instr(*o, if w32 { + isa::Width::W32 + } else { + isa::Width::W64 + }), +{ + let sf = wd(w32); + match o { + lir::IBin::Add => isa::Instr::AddSubShiftedReg { + sf, + sub_op: false, + setflags: false, + d: 0, + n: 0, + m: 1, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + lir::IBin::Sub => isa::Instr::AddSubShiftedReg { + sf, + sub_op: true, + setflags: false, + d: 0, + n: 0, + m: 1, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + lir::IBin::And => isa::Instr::LogicalShiftedReg { + sf, + opc: isa::LogicalOpc::And, + d: 0, + n: 0, + m: 1, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + lir::IBin::Or => isa::Instr::LogicalShiftedReg { + sf, + opc: isa::LogicalOpc::Orr, + d: 0, + n: 0, + m: 1, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + lir::IBin::Xor => isa::Instr::LogicalShiftedReg { + sf, + opc: isa::LogicalOpc::Eor, + d: 0, + n: 0, + m: 1, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + lir::IBin::Shl => isa::Instr::ShiftVariable { + sf, + shift_type: isa::ShiftType::Lsl, + d: 0, + n: 0, + m: 1, + }, + lir::IBin::ShrU => isa::Instr::ShiftVariable { + sf, + shift_type: isa::ShiftType::Lsr, + d: 0, + n: 0, + m: 1, + }, + lir::IBin::ShrS => isa::Instr::ShiftVariable { + sf, + shift_type: isa::ShiftType::Asr, + d: 0, + n: 0, + m: 1, + }, + lir::IBin::Mul => isa::Instr::MulAddSub { sf, sub_op: false, d: 0, n: 0, m: 1, a: 31 }, + lir::IBin::Rotl => isa::Instr::MulAddSub { sf, sub_op: false, d: 0, n: 0, m: 1, a: 31 }, + lir::IBin::Rotr => isa::Instr::MulAddSub { sf, sub_op: false, d: 0, n: 0, m: 1, a: 31 }, + } +} + +fn cmp_rr_exec(w32: bool, n: u8, m: u8) -> (i: isa::Instr) + ensures + i == super::templates::cmp_rr(if w32 { + isa::Width::W32 + } else { + isa::Width::W64 + }, n, m), +{ + isa::Instr::AddSubShiftedReg { + sf: wd(w32), + sub_op: true, + setflags: true, + d: 31, + n, + m, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + } +} + +fn cmp_zero_exec(w32: bool, n: u8) -> (i: isa::Instr) + ensures + i == super::templates::cmp_zero(if w32 { + isa::Width::W32 + } else { + isa::Width::W64 + }, n), +{ + isa::Instr::AddSubImm { + sf: wd(w32), + sub_op: true, + setflags: true, + d: 31, + n, + imm12: 0, + sh: false, + } +} + +fn cset0_exec(cond: u8) -> (i: isa::Instr) + ensures + i == super::templates::cset0(cond), +{ + isa::Instr::CondSelect { + sf: isa::Width::W32, + else_inv: false, + else_inc: true, + d: 0, + n: 31, + m: 31, + cond, + } +} + +// ---------------------------------------------------------------------------- +// Which instructions have templates +// ---------------------------------------------------------------------------- + +fn ibin_supported_exec(o: &lir::IBin) -> (r: bool) + ensures + r == crate::spec::ibin_supported(*o), +{ + !matches!(o, lir::IBin::Rotl | lir::IBin::Rotr) +} + +fn bin_supported_exec(b: &lir::BinOp) -> (r: bool) + ensures + r == crate::spec::bin_supported(*b), +{ + match b { + lir::BinOp::I32(o) => ibin_supported_exec(o), + lir::BinOp::I64(o) => ibin_supported_exec(o), + lir::BinOp::I32Cmp(_) => true, + lir::BinOp::I64Cmp(_) => true, + lir::BinOp::F64(_) => false, + lir::BinOp::F64Cmp(_) => false, + } +} + +fn un_supported_exec(u: &lir::UnOp) -> (r: bool) + ensures + r == crate::spec::un_supported(*u), +{ + match u { + lir::UnOp::I32Eqz => true, + lir::UnOp::I64Eqz => true, + lir::UnOp::F64Neg => true, + lir::UnOp::F64Abs => true, + lir::UnOp::F64Sqrt => false, + lir::UnOp::I32WrapI64 => true, + lir::UnOp::I64ExtendI32S => true, + lir::UnOp::I64ExtendI32U => true, + lir::UnOp::F64ConvertI32S => false, + lir::UnOp::F64ConvertI32U => false, + lir::UnOp::F64ConvertI64S => false, + lir::UnOp::F64ConvertI64U => false, + lir::UnOp::F64ReinterpretI64 => true, + lir::UnOp::I64ReinterpretF64 => true, + } +} + +pub(crate) fn pops_exec(op: &Op) -> (r: usize) + ensures + r == crate::spec::pops(*op), +{ + match op { + Op::Nop => 0, + Op::Const(_) => 0, + Op::LocalGet(_) => 0, + Op::LocalSet(_) => 1, + Op::LocalTee(_) => 1, + Op::Drop => 1, + Op::Select => 3, + Op::Bin(_) => 2, + Op::Un(_) => 1, + } +} + +pub(crate) fn pushes_exec(op: &Op) -> (r: usize) + ensures + r == crate::spec::pushes(*op), +{ + match op { + Op::Nop => 0, + Op::Const(_) => 1, + Op::LocalGet(_) => 1, + Op::LocalSet(_) => 0, + Op::LocalTee(_) => 1, + Op::Drop => 0, + Op::Select => 1, + Op::Bin(_) => 1, + Op::Un(_) => 1, + } +} + +pub(crate) fn op_ok_exec(op: &Op, nl: usize, h: usize) -> (r: bool) + ensures + r == op_ok(*op, nl as nat, h as nat), +{ + proof { + super::tlemmas::lemma_abi_consts_t(); + } + if nl > 31 || h > sub_u(31, nl) { + return false; + } + let p = pops_exec(op); + let q = pushes_exec(op); + if p > h { + return false; + } + let h2 = add_u(sub_u(h, p), q); + if h2 > sub_u(31, nl) { + return false; + } + match op { + Op::LocalGet(x) => *x < nl, + Op::LocalSet(x) => *x < nl, + Op::LocalTee(x) => *x < nl, + Op::Bin(b) => bin_supported_exec(b), + Op::Un(u) => un_supported_exec(u), + _ => true, + } +} + +// ---------------------------------------------------------------------------- +// Templates +// ---------------------------------------------------------------------------- + +#[verifier::rlimit(1000)] +pub(crate) fn emit_op(op: &Op, nl: usize, h: usize, out: &mut Vec) + requires + op_ok(*op, nl as nat, h as nat), + ensures + final(out)@ == old(out)@ + super::templates::tmpl(*op, nl as nat, h as nat), +{ + proof { + super::tlemmas::lemma_abi_consts_t(); + super::steps::lemma_abi_consts(); + } + let ghost old_out = out@; + match op { + Op::Nop => { + proof { + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::Drop => { + proof { + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::Const(v) => { + match v { + lir::Val::I32(c) => { + let c64 = *c as u64; + out.push(ex_movz(true, chunk_exec(c64, 0), 0)); + out.push(ex_movk(true, chunk_exec(c64, 1), 1)); + }, + lir::Val::I64(c) => { + out.push(ex_movz(false, chunk_exec(*c, 0), 0)); + out.push(ex_movk(false, chunk_exec(*c, 1), 1)); + out.push(ex_movk(false, chunk_exec(*c, 2), 2)); + out.push(ex_movk(false, chunk_exec(*c, 3), 3)); + }, + lir::Val::F64(c) => { + out.push(ex_movz(false, chunk_exec(*c, 0), 0)); + out.push(ex_movk(false, chunk_exec(*c, 1), 1)); + out.push(ex_movk(false, chunk_exec(*c, 2), 2)); + out.push(ex_movk(false, chunk_exec(*c, 3), 3)); + }, + } + out.push(ex_store(add_u(nl, h), 0)); + proof { + super::tlemmas::lemma_shape0(super::templates::const_mid(*v), (nl + h) as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::LocalGet(x) => { + out.push(ex_load(0, *x)); + out.push(ex_store(add_u(nl, h), 0)); + proof { + super::tlemmas::lemma_shape1(x0(), *x as nat, seq![], (nl + h) as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::LocalSet(x) => { + out.push(ex_load(0, sub_u(add_u(nl, h), 1))); + out.push(ex_store(*x, 0)); + proof { + super::tlemmas::lemma_shape1(x0(), (nl + h - 1) as nat, seq![], *x as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::LocalTee(x) => { + out.push(ex_load(0, sub_u(add_u(nl, h), 1))); + out.push(ex_store(*x, 0)); + proof { + super::tlemmas::lemma_shape1(x0(), (nl + h - 1) as nat, seq![], *x as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::Select => { + let t = sub_u(add_u(nl, h), 3); + out.push(ex_load(0, t)); + out.push(ex_load(1, add_u(t, 1))); + out.push(ex_load(2, add_u(t, 2))); + out.push(cmp_zero_exec(true, 2)); + out.push( + isa::Instr::CondSelect { + sf: isa::Width::W64, + else_inv: false, + else_inc: false, + d: 0, + n: 0, + m: 1, + cond: 1, + }, + ); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape3( + x0(), + t as nat, + x1(), + (t + 1) as nat, + x2(), + (t + 2) as nat, + super::templates::select_mid(), + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::Bin(b) => { + let t = sub_u(add_u(nl, h), 2); + match b { + lir::BinOp::I32(o) => { + out.push(ex_load(0, t)); + out.push(ex_load(1, add_u(t, 1))); + out.push(int_op_exec(o, true)); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape2( + x0(), + t as nat, + x1(), + (t + 1) as nat, + seq![super::templates::int_op_instr(*o, isa::Width::W32)], + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::BinOp::I64(o) => { + out.push(ex_load(0, t)); + out.push(ex_load(1, add_u(t, 1))); + out.push(int_op_exec(o, false)); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape2( + x0(), + t as nat, + x1(), + (t + 1) as nat, + seq![super::templates::int_op_instr(*o, isa::Width::W64)], + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::BinOp::I32Cmp(c) => { + out.push(ex_load(0, t)); + out.push(ex_load(1, add_u(t, 1))); + out.push(cmp_rr_exec(true, 0, 1)); + out.push(cset0_exec(cset_cond_exec(c))); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape2( + x0(), + t as nat, + x1(), + (t + 1) as nat, + super::templates::cmp_mid(*c, isa::Width::W32), + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::BinOp::I64Cmp(c) => { + out.push(ex_load(0, t)); + out.push(ex_load(1, add_u(t, 1))); + out.push(cmp_rr_exec(false, 0, 1)); + out.push(cset0_exec(cset_cond_exec(c))); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape2( + x0(), + t as nat, + x1(), + (t + 1) as nat, + super::templates::cmp_mid(*c, isa::Width::W64), + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::BinOp::F64(_) => { + proof { + assert(false); + } + }, + lir::BinOp::F64Cmp(_) => { + proof { + assert(false); + } + }, + } + }, + Op::Un(u) => { + let t = sub_u(add_u(nl, h), 1); + match u { + lir::UnOp::I32Eqz => { + out.push(ex_load(0, t)); + out.push(cmp_zero_exec(true, 0)); + out.push(cset0_exec(1)); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1( + x0(), + t as nat, + super::templates::eqz_mid(isa::Width::W32), + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::I64Eqz => { + out.push(ex_load(0, t)); + out.push(cmp_zero_exec(false, 0)); + out.push(cset0_exec(1)); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1( + x0(), + t as nat, + super::templates::eqz_mid(isa::Width::W64), + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::F64Neg => { + out.push(ex_load(0, t)); + out.push( + isa::Instr::MoveWide { + sf: isa::Width::W64, + opcode: isa::MoveWideOp::Z, + d: 1, + imm16: 0x8000, + hw: 3, + }, + ); + out.push( + isa::Instr::LogicalShiftedReg { + sf: isa::Width::W64, + opc: isa::LogicalOpc::Eor, + d: 0, + n: 0, + m: 1, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + ); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1(x0(), t as nat, super::templates::neg_mid(), t as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::F64Abs => { + out.push(ex_load(0, t)); + out.push( + isa::Instr::Bitfield { + sf: isa::Width::W64, + signed: false, + d: 0, + n: 0, + immr: 0, + imms: 62, + }, + ); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1(x0(), t as nat, super::templates::abs_mid(), t as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::I64ExtendI32S => { + out.push(ex_load(0, t)); + out.push( + isa::Instr::Bitfield { + sf: isa::Width::W64, + signed: true, + d: 0, + n: 0, + immr: 0, + imms: 31, + }, + ); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1(x0(), t as nat, super::templates::exts_mid(), t as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::I32WrapI64 => { + out.push(ex_load(0, t)); + out.push( + isa::Instr::LogicalShiftedReg { + sf: isa::Width::W32, + opc: isa::LogicalOpc::Orr, + d: 0, + n: 31, + m: 0, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + ); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1(x0(), t as nat, super::templates::wrap_mid(), t as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::I64ExtendI32U => { + proof { + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::F64ReinterpretI64 => { + proof { + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::I64ReinterpretF64 => { + proof { + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::F64Sqrt => { + proof { + assert(false); + } + }, + lir::UnOp::F64ConvertI32S => { + proof { + assert(false); + } + }, + lir::UnOp::F64ConvertI32U => { + proof { + assert(false); + } + }, + lir::UnOp::F64ConvertI64S => { + proof { + assert(false); + } + }, + lir::UnOp::F64ConvertI64U => { + proof { + assert(false); + } + }, + } + }, + } +} + +// ---------------------------------------------------------------------------- +// Programs +// ---------------------------------------------------------------------------- + +/// A prefix of a program that has templates has them. +pub proof fn lemma_prog_ok_take(ops: Seq, k: int, nl: nat, h0: nat) + requires + prog_ok(ops, nl, h0), + 0 <= k <= ops.len(), + ensures + prog_ok(ops.take(k), nl, h0), + decreases ops.len(), +{ + if k == ops.len() { + assert(ops.take(k) =~= ops); + } else { + let pre = ops.drop_last(); + lemma_prog_ok_take(pre, k, nl, h0); + assert(ops.take(k) =~= pre.take(k)); + } +} + +/// Each instruction of a program that has templates is well formed at the height +/// the instructions before it leave. +pub proof fn lemma_prog_ok_op(ops: Seq, k: int, nl: nat, h0: nat) + requires + prog_ok(ops, nl, h0), + 0 <= k < ops.len(), + ensures + op_ok(ops[k], nl, height_after(ops.take(k), h0)), +{ + lemma_prog_ok_take(ops, k + 1, nl, h0); + let t = ops.take(k + 1); + assert(t.drop_last() =~= ops.take(k)); + assert(t.last() == ops[k]); +} + +fn emit_body_impl(ops: &[Op], nl: usize) -> (r: Option>) + ensures + super::contract::emit_body_ensures(ops@, nl, match r { + Some(v) => Some(v@), + None => None, + }), +{ + if nl > 31 { + proof { + super::tlemmas::lemma_abi_consts_t(); + if prog_ok(ops@, nl as nat, 0) { + lemma_prog_ok_take(ops@, 0, nl as nat, 0); + assert(ops@.take(0) =~= Seq::::empty()); + } + } + return None; + } + let mut out: Vec = Vec::new(); + let mut h: usize = 0; + let mut i: usize = 0; + proof { + assert(ops@.take(0) =~= Seq::::empty()); + super::tlemmas::lemma_abi_consts_t(); + } + while i < ops.len() + invariant + i <= ops.len(), + nl <= 31, + prog_ok(ops@.take(i as int), nl as nat, 0), + h as nat == height_after(ops@.take(i as int), 0), + out@ == super::templates::code_of(ops@.take(i as int), nl as nat, 0), + decreases ops.len() - i, + { + proof { + super::tlemmas::lemma_abi_consts_t(); + } + let op = match ops.get(i) { + Some(op) => op, + None => { + proof { + assert(false); + } + return None; + }, + }; + let ok = op_ok_exec(op, nl, h); + if !ok { + proof { + if prog_ok(ops@, nl as nat, 0) { + lemma_prog_ok_op(ops@, i as int, nl as nat, 0); + } + } + return None; + } + emit_op(op, nl, h, &mut out); + proof { + let t = ops@.take(i as int + 1); + assert(t.drop_last() =~= ops@.take(i as int)); + assert(t.last() == ops@[i as int]); + assert(super::templates::code_of(t, nl as nat, 0) == super::templates::code_of(ops@.take(i as int), nl as nat, 0) + super::templates::tmpl( + ops@[i as int], + nl as nat, + height_after(ops@.take(i as int), 0), + )); + } + let p = pops_exec(op); + let q = pushes_exec(op); + proof { + assert(op == &ops@[i as int]); + } + h = add_u(sub_u(h, p), q); + i = add_u(i, 1); + } + proof { + assert(ops@.take(ops.len() as int) =~= ops@); + super::compose::lemma_prog_refines(ops@, nl as nat, 0); + super::tlemmas::lemma_code_of_valid(ops@, nl as nat, 0); + } + Some(out) +} + +pub(crate) fn emit_exit(code: &mut Vec, exit_code: u16) + ensures + final(code)@ == old(code)@ + exit_seq(exit_code), +{ + let ghost old_code = code@; + code.push( + isa::Instr::MoveWide { + sf: isa::Width::W32, + opcode: isa::MoveWideOp::Z, + d: 0, + imm16: exit_code, + hw: 0, + }, + ); + code.push( + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: false, + t: 0, + n: 28, + imm12: 32, + }, + ); + code.push(isa::Instr::Ret); + proof { + super::steps::lemma_abi_consts(); + assert(code@ =~= old_code + exit_seq(exit_code)); + } +} + +#[verifier::rlimit(500)] +fn emit_blob_impl(ops: &[Op], nl: usize, exit_code: u16) -> (r: Option>) + ensures + super::contract::emit_blob_ensures(ops@, nl, exit_code, match r { + Some(v) => Some(v@), + None => None, + }), +{ + match emit_body_impl(ops, nl) { + None => None, + Some(body) => { + let ghost body_seq = body@; + let mut code = body; + emit_exit(&mut code, exit_code); + proof { + super::tlemmas::lemma_exit_valid(exit_code); + assert(super::contract::all_valid_instrs(body_seq)); + assert forall|k: int| 0 <= k < code@.len() implies isa::valid_instr( + #[trigger] code@[k], + ) by { + if k < body_seq.len() { + assert(code@[k] == body_seq[k]); + } else { + assert(code@[k] == exit_seq(exit_code)[k - body_seq.len()]); + } + } + } + let mut bytes: Vec = Vec::new(); + let mut k: usize = 0; + while k < code.len() + invariant + k <= code.len(), + bytes@.len() == 4 * k, + forall|j: int| + 0 <= j < k ==> #[trigger] bytes@.subrange(4 * j, 4 * j + 4) == isa::word_bytes( + isa::encode(code@[j]), + ), + forall|j: int| 0 <= j < code@.len() ==> isa::valid_instr(#[trigger] code@[j]), + decreases code.len() - k, + { + let ins = match code.get(k) { + Some(ins) => ins, + None => { + proof { + assert(false); + } + return None; + }, + }; + let w = encode_instr(ins); + let ghost old_bytes = bytes@; + bytes.push((w % 256) as u8); + bytes.push(((w / 0x100) % 256) as u8); + bytes.push(((w / 0x1_0000) % 256) as u8); + bytes.push((w / 0x100_0000) as u8); + proof { + assert(ins == &code@[k as int]); + assert(w as int == isa::encode(code@[k as int])); + assert(w as int / 0x100_0000 < 0x100); + let b = isa::word_bytes(isa::encode(code@[k as int])); + assert(bytes@ =~= old_bytes + b); + assert forall|j: int| 0 <= j < k + 1 implies #[trigger] bytes@.subrange(4 * j, 4 * j + 4) + == isa::word_bytes(isa::encode(code@[j])) by { + if j < k { + assert(bytes@.subrange(4 * j, 4 * j + 4) =~= old_bytes.subrange(4 * j, 4 * j + 4)); + } else { + assert(bytes@.subrange(4 * j, 4 * j + 4) =~= b); + } + } + } + k = add_u(k, 1); + } + proof { + assert(code@ == body_seq + exit_seq(exit_code)); + super::compose::lemma_prog_refines(ops@, nl as nat, 0); + assert(clazel_codecheck::a64::spec::decodes_to(bytes@, code@)) by { + assert(bytes@.len() == 4 * code@.len()); + } + } + Some(bytes) + }, + } +} + +/// The LIR → A64 back end. +pub struct A64Backend; + +impl Backend for A64Backend { + fn emit_body(ops: &[Op], nl: usize) -> (r: Option>) { + emit_body_impl(ops, nl) + } + + fn emit_blob(ops: &[Op], nl: usize, exit_code: u16) -> (r: Option>) { + emit_blob_impl(ops, nl, exit_code) + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/exit.rs b/crates/clazel-jit/src/a64/exit.rs new file mode 100644 index 000000000..0e3a911ca --- /dev/null +++ b/crates/clazel-jit/src/a64/exit.rs @@ -0,0 +1,189 @@ +//! The exit sequence refines LIR's `exit c` with no operands +//! ([`lemma_exit_refines`]): `movz w0, #c; str x0, [x28, #256]; ret`. The A64 +//! counterpart of [`crate::exit`]; `ret` reads no memory, so unlike x86-64 there +//! is no stack to describe. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `str xr, [x28, #256]`: the store of the exit code. +pub open spec fn store_exit(src: u8) -> isa::Instr { + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: false, + t: src, + n: abi::ctx_reg(), + imm12: (abi::exit_code_off() / 8) as u16, + } +} + +/// `str xr, [x28, #256]` writes `xr` to the exit-code word and changes nothing +/// else. +pub proof fn lemma_step_store_exit(m: isa::State, src: u8) + requires + isa::wf(m), + ctx_ok(m), + src < 31, + exit_area_mapped(m.mem, ctx_of(m)), + m.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(m, store_exit(src)) is Some, + ({ + let m1 = isa::step(m, store_exit(src)).unwrap(); + &&& m1 == isa::State { mem: m1.mem, pc: (m.pc + 4) as u64, ..m } + &&& isa::mem_read(m1.mem, ctx_of(m) + abi::exit_code_off(), 8) == Some( + m.r[src as int] as int, + ) + &&& m1.mem.dom() =~= m.mem.dom() + &&& forall|b: int| + #![trigger m1.mem.dom().contains(b)] + m.mem.dom().contains(b) && (b < ctx_of(m) + abi::exit_code_off() || ctx_of(m) + + abi::exit_code_off() + 8 <= b) ==> m1.mem[b] == m.mem[b] + }), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + super::alu::lemma_pc4(m.pc); + super::steps::lemma_x_read64(m, 28); + super::steps::lemma_x_read64(m, src); + let ctxb = ctx_of(m); + let addr = ctxb + 256; + let w = m.r[src as int] as int; + super::memlemmas::lemma_trunc64(addr); + super::memlemmas::lemma_trunc64(256); + assert(isa::x_read(m, 28, 64) == ctxb); + assert(((abi::exit_code_off() / 8) as u16) as int == 32); + assert(isa::trunc(64, ctxb + 32 * (isa::datasize(isa::Width::W64) as int / 8)) == addr); + assert forall|i: int| 0 <= i < 8 implies m.mem.dom().contains(#[trigger] (addr + i)) by { + assert(in_exit_area(ctxb, addr + i)); + } + super::memlemmas::lemma_mem_set_some(m.mem, addr, 8, w); + let m2 = isa::mem_set(m.mem, addr, 8, w).unwrap(); + super::memlemmas::lemma_mem_set_frame(m.mem, addr, 8, w, m2); + assert(0 <= w < vstd::arithmetic::power2::pow2(8 * 8) as int) by { + vstd::arithmetic::power2::lemma2_to64_rest(); + } + super::memlemmas::lemma_read_after_write_same(m.mem, addr, 8, w, m2); + assert(isa::execute(m, store_exit(src)) == Some(isa::State { mem: m2, ..m })); +} + +/// **The exit sequence refines `exit c`.** +#[verifier::rlimit(500)] +pub proof fn lemma_exit_refines(c: u16, nl: nat) + ensures + super::contract::exit_refines(c, nl), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + assert forall|lay: lir::Layout, a: lir::St, m: isa::State, fuel: nat| + #![trigger lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, fuel), run_seq(m, exit_seq(c))] + rel(lay, a, m, nl) && (m.pc as int) + code_len(exit_seq(c)) < isa::p2(64) implies match lir::eval( + lay, + lir::Instr::Exit(c as nat, seq![]), + a, + fuel, + ) { + lir::Outcome::Exit(code, _, a2) => match run_seq(m, exit_seq(c)) { + Some(m2) => rel(lay, a2, m2, nl) && preserved(m, m2) && isa::mem_read( + m2.mem, + ctx_of(m) + abi::exit_code_off(), + 8, + ) == Some(code as int) && m2.pc == m.r[abi::link_reg() as int], + None => false, + }, + _ => true, + } by { + match lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, fuel) { + lir::Outcome::Exit(code, vals, a2) => { + assert(fuel > 0); + assert(code == c as nat); + assert(a2.stack =~= a.stack); + assert(a2.locals == a.locals && a2.mem == a.mem); + let i1 = isa::Instr::MoveWide { + sf: isa::Width::W32, + opcode: isa::MoveWideOp::Z, + d: 0, + imm16: c, + hw: 0, + }; + let i2 = store_exit(0); + let i3 = isa::Instr::Ret; + assert(exit_seq(c) == seq![i1, i2, i3]); + assert(isa::wf(m)); + let ctxb = ctx_of(m); + // 1. movz w0, #c + assert(super::alu::bits(isa::Width::W32) == 32); + super::bitmid::lemma_step_movz(m, isa::Width::W32, 0, c, 0); + assert(super::bitmid::chunk_val(c, 0) == c as int); + let s1 = isa::State { r: m.r.update(0, c as u64), pc: (m.pc + 4) as u64, ..m }; + assert(isa::step(m, i1) == Some(s1)); + super::alu::lemma_pc4(m.pc); + assert(rel(lay, a, s1, nl)) by { + super::rel::lemma_rel_regs(lay, a, m, s1, nl); + } + // 2. str x0, [x28, #256] + lemma_step_store_exit(s1, 0); + let s2 = isa::step(s1, i2).unwrap(); + assert(s1.r[0] == c as u64); + assert(isa::wf(s2)) by { + assert(s2.mem.dom() =~= m.mem.dom()); + } + // the frame's slots survive the store to the exit-code word + assert forall|j: nat| j < nl + a.stack.len() implies #[trigger] slot_holds( + s2.mem, + ctxb, + j, + lir::val_bits(slot_val(a, nl, j)), + ) by { + assert(slot_holds(m.mem, ctxb, j, lir::val_bits(slot_val(a, nl, j)))); + assert(j < max_slots()); + super::steps::lemma_slot_addr(m, j); + assert forall|i: int| 0 <= i < 8 implies (m.mem.dom().contains( + #[trigger] (ctxb + slot_disp(j) + i), + ) == s2.mem.dom().contains(ctxb + slot_disp(j) + i)) && (m.mem.dom().contains( + ctxb + slot_disp(j) + i, + ) ==> m.mem[ctxb + slot_disp(j) + i] == s2.mem[ctxb + slot_disp(j) + i]) by { + assert(s2.mem.dom().contains(ctxb + slot_disp(j) + i) == m.mem.dom().contains( + ctxb + slot_disp(j) + i, + )); + } + super::memlemmas::lemma_mem_read_congr(m.mem, s2.mem, ctxb + slot_disp(j), 8); + } + assert(mem_rel(lay, a.mem, s2.mem)) by { + assert forall|b: nat| #[trigger] in_regions(lay, b) implies s2.mem.dom().contains( + b as int, + ) && s2.mem[b as int] == lir::byte_at(a.mem, b) by { + assert(!in_exit_area(ctxb, b as int)); + assert(m.mem.dom().contains(b as int)); + } + } + assert(rel(lay, a2, s2, nl)) by { + assert(isa::wf(s2) && ctx_ok(s2)); + assert(ctx_of(s2) == ctxb); + assert(exit_area_mapped(s2.mem, ctxb)); + } + // 3. ret + super::steps::lemma_x_read64(s2, 30); + let target = s2.r[30] as int; + assert(isa::x_read(s2, 30, 64) == target); + super::memlemmas::lemma_trunc64(target); + let s3 = isa::State { pc: s2.r[30], ..s2 }; + assert(isa::step(s2, i3) == Some(s3)); + super::runseq::run3(m, i1, i2, i3, s1, s2, s3); + assert(s3.r == s2.r && s3.mem == s2.mem); + assert(rel(lay, a2, s3, nl)) by { + super::rel::lemma_rel_regs(lay, a2, s2, s3, nl); + } + assert(s3.r[30] == m.r[30]); + assert(preserved_mem(m, s3)); + }, + _ => {}, + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/flags.rs b/crates/clazel-jit/src/a64/flags.rs new file mode 100644 index 000000000..638153c97 --- /dev/null +++ b/crates/clazel-jit/src/a64/flags.rs @@ -0,0 +1,368 @@ +//! The flags `cmp` leaves and what `cset` and `csel` read from them: the +//! A64 counterpart of [`crate::flags`]. `cmp rn, rm` is `subs xzr, rn, rm`: +//! the flags are `NZCV` of `AddWithCarry(rn, NOT(rm), 1)`. The ten LIR +//! comparisons are ten of the condition codes of `ConditionHolds`, and +//! [`lemma_cc_icmp`] shows each is true exactly when LIR's comparison is. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The `nzcv` that `cmp x, y` leaves at width `n`. +pub open spec fn cmp_nzcv(n: nat, x: int, y: int) -> int { + isa::add_with_carry(n, x, isa::not_vec(n, y), 1).1 +} + +/// `n`, `z`, `c`, `v` are 0 or 1 and are the bits of `nzcv`. +pub proof fn lemma_nzcv_bits(a: int, b: int, c: int, d: int) + requires + 0 <= a <= 1, + 0 <= b <= 1, + 0 <= c <= 1, + 0 <= d <= 1, + ensures + isa::bit(a * 8 + b * 4 + c * 2 + d, 3) == a, + isa::bit(a * 8 + b * 4 + c * 2 + d, 2) == b, + isa::bit(a * 8 + b * 4 + c * 2 + d, 1) == c, + isa::bit(a * 8 + b * 4 + c * 2 + d, 0) == d, +{ + vstd::arithmetic::power2::lemma2_to64(); + assert(isa::p2(0) == 1 && isa::p2(1) == 2 && isa::p2(2) == 4 && isa::p2(3) == 8); +} + +/// The flags of `x - y` at 32 bits: `C` is "unsigned greater or equal", `Z` is +/// "equal", and `N == V` is "signed greater or equal". +#[verifier::rlimit(400)] +pub proof fn lemma_cmp_arith32(x: int, y: int) + requires + 0 <= x < 0x1_0000_0000, + 0 <= y < 0x1_0000_0000, + ensures + ({ + let nzcv = cmp_nzcv(32, x, y); + &&& (isa::bit(nzcv, 1) == 1) <==> (x >= y) + &&& (isa::bit(nzcv, 2) == 1) <==> (x == y) + &&& (isa::bit(nzcv, 3) == isa::bit(nzcv, 0)) <==> (lir::signed_of(32, x) + >= lir::signed_of(32, y)) + }), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + let p: int = 0x1_0000_0000; + let h: int = 0x8000_0000; + assert(isa::p2(32) == p && isa::p2(31) == h && lir::pow2(32) == p); + let ys = p - 1 - y; + assert(isa::not_vec(32, y) == ys); + assert(isa::trunc(32, x) == x && isa::trunc(32, y) == y && isa::trunc(32, ys) == ys); + let us = x + ys + 1; + let res = us % p; + let sx = if x >= h { x - p } else { x }; + let sy = if y >= h { y - p } else { y }; + let sys = if ys >= h { ys - p } else { ys }; + assert(isa::sint(32, x) == sx && isa::sint(32, y) == sy && isa::sint(32, ys) == sys); + assert(lir::signed_of(32, x) == sx && lir::signed_of(32, y) == sy); + let ss = sx + sys + 1; + assert(ss == sx - sy); + let fn_ = (res / h) % 2; + let fz = if res == 0 { + 1int + } else { + 0 + }; + let fc = if res == us { + 0int + } else { + 1 + }; + let sres = if res >= h { + res - p + } else { + res + }; + assert(isa::sint(32, res) == sres); + let fv = if sres == ss { + 0int + } else { + 1 + }; + assert(isa::bit(res, 31) == fn_); + assert(isa::add_with_carry(32, x, isa::not_vec(32, y), 1) == (res, fn_ * 8 + fz * 4 + fc * 2 + fv)); + assert(0 <= fn_ <= 1 && 0 <= res < p); + lemma_nzcv_bits(fn_, fz, fc, fv); +} + +/// The flags of `x - y` at 64 bits (see [`lemma_cmp_arith32`]). +#[verifier::rlimit(400)] +pub proof fn lemma_cmp_arith64(x: int, y: int) + requires + 0 <= x < 0x1_0000_0000_0000_0000, + 0 <= y < 0x1_0000_0000_0000_0000, + ensures + ({ + let nzcv = cmp_nzcv(64, x, y); + &&& (isa::bit(nzcv, 1) == 1) <==> (x >= y) + &&& (isa::bit(nzcv, 2) == 1) <==> (x == y) + &&& (isa::bit(nzcv, 3) == isa::bit(nzcv, 0)) <==> (lir::signed_of(64, x) + >= lir::signed_of(64, y)) + }), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + let p: int = 0x1_0000_0000_0000_0000; + let h: int = 0x8000_0000_0000_0000; + assert(isa::p2(64) == p && isa::p2(63) == h && lir::pow2(64) == p); + let ys = p - 1 - y; + assert(isa::not_vec(64, y) == ys); + assert(isa::trunc(64, x) == x && isa::trunc(64, y) == y && isa::trunc(64, ys) == ys); + let us = x + ys + 1; + let res = us % p; + let sx = if x >= h { x - p } else { x }; + let sy = if y >= h { y - p } else { y }; + let sys = if ys >= h { ys - p } else { ys }; + assert(isa::sint(64, x) == sx && isa::sint(64, y) == sy && isa::sint(64, ys) == sys); + assert(lir::signed_of(64, x) == sx && lir::signed_of(64, y) == sy); + let ss = sx + sys + 1; + assert(ss == sx - sy); + let fn_ = (res / h) % 2; + let fz = if res == 0 { + 1int + } else { + 0 + }; + let fc = if res == us { + 0int + } else { + 1 + }; + let sres = if res >= h { + res - p + } else { + res + }; + assert(isa::sint(64, res) == sres); + let fv = if sres == ss { + 0int + } else { + 1 + }; + assert(isa::bit(res, 63) == fn_); + assert(isa::add_with_carry(64, x, isa::not_vec(64, y), 1) == (res, fn_ * 8 + fz * 4 + fc * 2 + fv)); + assert(0 <= fn_ <= 1 && 0 <= res < p); + lemma_nzcv_bits(fn_, fz, fc, fv); +} + +/// The flags of `x - y` at 32 or 64 bits. +pub proof fn lemma_cmp_arith(n: nat, x: int, y: int) + requires + n == 32 || n == 64, + 0 <= x < isa::p2(n), + 0 <= y < isa::p2(n), + ensures + ({ + let nzcv = cmp_nzcv(n, x, y); + &&& (isa::bit(nzcv, 1) == 1) <==> (x >= y) + &&& (isa::bit(nzcv, 2) == 1) <==> (x == y) + &&& (isa::bit(nzcv, 3) == isa::bit(nzcv, 0)) <==> (lir::signed_of(n, x) + >= lir::signed_of(n, y)) + }), +{ + super::memlemmas::lemma_p2_consts(); + if n == 32 { + lemma_cmp_arith32(x, y); + } else { + lemma_cmp_arith64(x, y); + } +} + +/// `set_nzcv(s, cmp_nzcv(..))` has the four flags of the comparison, and the +/// condition `cset_cond(c)` of the `csinc` fails exactly when LIR's comparison +/// `c` is true. +pub proof fn lemma_cc_icmp(s: isa::State, c: lir::ICmp, n: nat, x: int, y: int) + requires + n == 32 || n == 64, + 0 <= x < isa::p2(n), + 0 <= y < isa::p2(n), + ensures + ({ + let t = isa::set_nzcv(s, cmp_nzcv(n, x, y)); + !isa::condition_holds(t, super::templates::cset_cond(c)) == lir::icmp(c, n, x, y) + }), +{ + lemma_cmp_arith(n, x, y); + let t = isa::set_nzcv(s, cmp_nzcv(n, x, y)); + let nzcv = cmp_nzcv(n, x, y); + assert(t.flag_n == (isa::bit(nzcv, 3) == 1)); + assert(t.flag_z == (isa::bit(nzcv, 2) == 1)); + assert(t.flag_c == (isa::bit(nzcv, 1) == 1)); + assert(t.flag_v == (isa::bit(nzcv, 0) == 1)); + // bits are 0 or 1 + assert(0 <= isa::bit(nzcv, 3) <= 1 && 0 <= isa::bit(nzcv, 0) <= 1) by { + vstd::arithmetic::power2::lemma2_to64(); + } + assert((t.flag_n == t.flag_v) == (isa::bit(nzcv, 3) == isa::bit(nzcv, 0))); + match c { + lir::ICmp::Eq => {}, + lir::ICmp::Ne => {}, + lir::ICmp::LtS => {}, + lir::ICmp::LtU => {}, + lir::ICmp::GtS => {}, + lir::ICmp::GtU => {}, + lir::ICmp::LeS => {}, + lir::ICmp::LeU => {}, + lir::ICmp::GeS => {}, + lir::ICmp::GeU => {}, + } +} + +/// A machine state with nothing in it. `ConditionHolds` reads only the flags, +/// so statements about conditions can be made of this one (and a concrete +/// witness can compute them). +pub open spec fn blank() -> isa::State { + isa::State { + r: Seq::empty(), + sp: 0, + pc: 0, + flag_n: false, + flag_z: false, + flag_c: false, + flag_v: false, + vreg: Seq::empty(), + mem: Map::empty(), + } +} + +/// [`lemma_cc_icmp`] for the blank state: the instance a kill certificate +/// (`mutations.json`) names, with no state to build. +pub proof fn lemma_cc_icmp_blank(c: lir::ICmp, n: nat, x: int, y: int) + requires + n == 32 || n == 64, + 0 <= x < isa::p2(n), + 0 <= y < isa::p2(n), + ensures + !isa::condition_holds( + isa::set_nzcv(blank(), cmp_nzcv(n, x, y)), + super::templates::cset_cond(c), + ) == lir::icmp(c, n, x, y), +{ + lemma_cc_icmp(blank(), c, n, x, y); +} + +/// [`lemma_cc_icmp_blank`] at 32 bits, with the bounds written as numbers so a +/// kill certificate can call it at concrete operands. +pub proof fn lemma_cc_icmp_blank32(c: lir::ICmp, x: int, y: int) + requires + 0 <= x < 0x1_0000_0000, + 0 <= y < 0x1_0000_0000, + ensures + !isa::condition_holds( + isa::set_nzcv(blank(), cmp_nzcv(32, x, y)), + super::templates::cset_cond(c), + ) == lir::icmp(c, 32, x, y), +{ + super::memlemmas::lemma_p2_consts(); + lemma_cc_icmp_blank(c, 32, x, y); +} + +/// `cmp x0, x1` at width `sf` (flags only): the state after it is the state with +/// the flags of `x - y` and `pc` advanced by 4. +pub proof fn lemma_step_cmp(s: isa::State, sf: isa::Width, x: u64, y: u64) + requires + isa::wf(s), + s.r[0] == x, + s.r[1] == y, + x < isa::p2(super::alu::bits(sf)), + y < isa::p2(super::alu::bits(sf)), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::cmp_rr(sf, 0, 1)) == Some( + isa::State { + pc: (s.pc + 4) as u64, + ..isa::set_nzcv(s, cmp_nzcv(super::alu::bits(sf), x as int, y as int)) + }, + ), +{ + let n = super::alu::bits(sf); + super::alu::lemma_bits(sf); + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + super::alu::lemma_trunc_small(n, x as int); + super::alu::lemma_trunc_small(n, y as int); + assert(isa::lsl(n, y as int, 0) == y as int) by { + assert(isa::p2(0) == 1); + } +} + +/// `cmp x0, #0` at width `sf`: the flags of `x - 0`. +pub proof fn lemma_step_cmp_zero(s: isa::State, sf: isa::Width, r: u8, x: u64) + requires + isa::wf(s), + r < 31, + s.r[r as int] == x, + x < isa::p2(super::alu::bits(sf)), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::cmp_zero(sf, r)) == Some( + isa::State { + pc: (s.pc + 4) as u64, + ..isa::set_nzcv(s, cmp_nzcv(super::alu::bits(sf), x as int, 0)) + }, + ), +{ + let n = super::alu::bits(sf); + super::alu::lemma_bits(sf); + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + super::alu::lemma_trunc_small(n, x as int); + // operand2 is NOT(0) = ones, carry in 1 + assert(isa::not_vec(n, 0) == isa::p2(n) - 1); +} + +/// `cset w0, cc` (`csinc w0, wzr, wzr, cond`): `x0` is 0 if `cond` holds and 1 +/// if not. +pub proof fn lemma_step_cset(s: isa::State, cond: u8) + requires + isa::wf(s), + cond < 14, + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::cset0(cond)) == Some( + super::alu::wrote_x0(s, if isa::condition_holds(s, cond) { + 0int + } else { + 1int + }, 32), + ), +{ + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + assert(isa::trunc(32, 0int + 1) == 1); +} + +/// `csel x0, x0, x1, ne` after a `cmp w2, #0`: see [`super::mid::lemma_mid_select`]; +/// the step itself. +pub proof fn lemma_step_csel_ne(s: isa::State, v1: u64, v2: u64) + requires + isa::wf(s), + s.r[0] == v1, + s.r[1] == v2, + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::select_mid()[1]) == Some( + super::alu::wrote_x0(s, if !s.flag_z { + v1 as int + } else { + v2 as int + }, 64), + ), +{ + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_pc4(s.pc); + super::alu::lemma_trunc_small(64, v1 as int); + super::alu::lemma_trunc_small(64, v2 as int); + assert(isa::condition_holds(s, 1) == !s.flag_z); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/intops.rs b/crates/clazel-jit/src/a64/intops.rs new file mode 100644 index 000000000..a7f3db031 --- /dev/null +++ b/crates/clazel-jit/src/a64/intops.rs @@ -0,0 +1,142 @@ +//! The one-instruction middle of an integer binary operator computes LIR's +//! result: for every supported operator and both widths, `int_op_instr(o, sf)` +//! run from a state whose `x0` and `x1` hold the operands leaves +//! [`lir::ibin32`] or [`lir::ibin64`] of them in `x0`. Built on [`super::alu`]'s +//! per-form lemmas. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// LIR's result of integer binary operator `o` on `x` and `y`, as the word +/// the machine holds: for an `i32` the operands are the low halves. +pub open spec fn ibin_w(o: lir::IBin, sf: isa::Width, x: u64, y: u64) -> int { + if sf is W32 { + lir::ibin32(o, x as u32, y as u32) as int + } else { + lir::ibin64(o, x, y) as int + } +} + +/// The result of an integer operator is a word of the operator's width. +pub proof fn lemma_ibin_w_bound(o: lir::IBin, sf: isa::Width, x: u64, y: u64) + ensures + 0 <= ibin_w(o, sf, x, y) < isa::p2(super::alu::bits(sf)), +{ + super::memlemmas::lemma_p2_consts(); +} + +/// A signed reading of a word below `2^n` is `SInt`. +pub proof fn lemma_signed_of_sint(n: nat, x: int) + requires + n == 32 || n == 64, + 0 <= x < isa::p2(n), + ensures + lir::signed_of(n, x) == isa::sint(n, x), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + vstd::arithmetic::power2::lemma2_to64_rest(); + super::alu::lemma_trunc_small(n, x); + if n == 32 { + assert(lir::pow2(32) == 0x1_0000_0000); + assert(isa::p2(31) == 0x8000_0000); + } else { + assert(lir::pow2(64) == 0x1_0000_0000_0000_0000); + assert(isa::p2(63) == 0x8000_0000_0000_0000); + } +} + +/// **`int_op_instr(o, sf)` computes LIR's operator.** +#[verifier::rlimit(500)] +pub proof fn lemma_step_int_op(s: isa::State, sf: isa::Width, o: lir::IBin, x: u64, y: u64) + requires + isa::wf(s), + crate::spec::ibin_supported(o), + s.r[0] == x, + s.r[1] == y, + x < isa::p2(super::alu::bits(sf)), + y < isa::p2(super::alu::bits(sf)), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(s, super::templates::int_op_instr(o, sf)) == Some( + super::alu::wrote_x0(s, ibin_w(o, sf, x, y), super::alu::bits(sf)), + ), +{ + let n = super::alu::bits(sf); + super::alu::lemma_bits(sf); + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + vstd::arithmetic::power2::lemma2_to64_rest(); + lemma_ibin_w_bound(o, sf, x, y); + let p = isa::p2(n); + assert(lir::pow2(32) == 0x1_0000_0000); + assert(lir::pow2(64) == 0x1_0000_0000_0000_0000); + // the operands as the LIR values + if n == 32 { + assert((x as u32) as int == x as int); + assert((y as u32) as int == y as int); + } + match o { + lir::IBin::Add => { + super::alu::lemma_step_addsub(s, sf, false, x, y); + assert(ibin_w(o, sf, x, y) == (x as int + y as int) % p); + }, + lir::IBin::Sub => { + super::alu::lemma_step_addsub(s, sf, true, x, y); + assert(ibin_w(o, sf, x, y) == (x as int - y as int) % p); + }, + lir::IBin::Mul => { + super::alu::lemma_step_mul(s, sf, x, y); + assert(ibin_w(o, sf, x, y) == (x as int * y as int) % p); + }, + lir::IBin::And | lir::IBin::Or | lir::IBin::Xor => { + super::alu::lemma_step_logic(s, sf, o, x, y); + if n == 32 { + assert(((x as u32) & (y as u32)) as u64 == (x & y)) by (bit_vector) + requires + x < 0x1_0000_0000u64, + y < 0x1_0000_0000u64, + ; + assert(((x as u32) | (y as u32)) as u64 == (x | y)) by (bit_vector) + requires + x < 0x1_0000_0000u64, + y < 0x1_0000_0000u64, + ; + assert(((x as u32) ^ (y as u32)) as u64 == (x ^ y)) by (bit_vector) + requires + x < 0x1_0000_0000u64, + y < 0x1_0000_0000u64, + ; + } + assert(ibin_w(o, sf, x, y) == super::alu::logic_val(o, x, y) as int); + }, + lir::IBin::Shl | lir::IBin::ShrU | lir::IBin::ShrS => { + super::alu::lemma_step_shift(s, sf, o, x, y); + let k = (y as int % n as int) as nat; + match o { + lir::IBin::Shl => { + assert(ibin_w(o, sf, x, y) == (x as int * isa::p2(k)) % p); + }, + lir::IBin::ShrU => { + assert(ibin_w(o, sf, x, y) == x as int / isa::p2(k)); + }, + _ => { + lemma_signed_of_sint(n, x as int); + assert(ibin_w(o, sf, x, y) == isa::trunc(n, isa::sint(n, x as int) / isa::p2(k))); + }, + } + }, + lir::IBin::Rotl => { + assert(false); + }, + lir::IBin::Rotr => { + assert(false); + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/memlemmas.rs b/crates/clazel-jit/src/a64/memlemmas.rs new file mode 100644 index 000000000..08a248017 --- /dev/null +++ b/crates/clazel-jit/src/a64/memlemmas.rs @@ -0,0 +1,214 @@ +//! Facts about `clazel-a64`'s byte memory ([`isa::mem_read`], [`isa::mem_set`]): +//! what a write leaves alone, what a read of what was just written gives, and +//! that a read depends only on the bytes it touches. The A64 counterpart of +//! [`crate::memlemmas`]; the addresses are below `2^64`, so +//! [`isa::trunc`]`(64, _)` of each byte's address is the address. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The powers of two the proofs use, evaluated. +pub proof fn lemma_p2_consts() + ensures + isa::p2(0) == 1, + isa::p2(5) == 32, + isa::p2(6) == 64, + isa::p2(8) == 256, + isa::p2(16) == 0x1_0000, + isa::p2(31) == 0x8000_0000, + isa::p2(32) == 0x1_0000_0000, + isa::p2(47) == 0x8000_0000_0000, + isa::p2(48) == 0x1_0000_0000_0000, + isa::p2(62) == 0x4000_0000_0000_0000, + isa::p2(63) == 0x8000_0000_0000_0000, + isa::p2(64) == 0x1_0000_0000_0000_0000, +{ + vstd::arithmetic::power2::lemma2_to64(); + vstd::arithmetic::power2::lemma2_to64_rest(); +} + +/// A number below `2^64` is its own low 64 bits. +pub proof fn lemma_trunc64(a: int) + requires + 0 <= a < 0x1_0000_0000_0000_0000, + ensures + isa::trunc(64, a) == a, +{ + lemma_p2_consts(); + vstd::arithmetic::div_mod::lemma_small_mod(a as nat, 0x1_0000_0000_0000_0000nat); +} + +/// A write that succeeds leaves the mapped addresses as they were and every +/// byte outside the written range as it was. +pub proof fn lemma_mem_set_frame(m: Map, addr: int, n: nat, v: int, m2: Map) + requires + isa::mem_set(m, addr, n, v) == Some(m2), + 0 <= addr, + addr + n < 0x1_0000_0000_0000_0000, + ensures + m2.dom() =~= m.dom(), + forall|b: int| + #![trigger m2.dom().contains(b)] + m.dom().contains(b) && (b < addr || addr + n <= b) ==> m2[b] == m[b], + decreases n, +{ + if n > 0 { + lemma_trunc64(addr); + match isa::mem_set(m, addr + 1, (n - 1) as nat, v / 256) { + Some(m3) => { + lemma_mem_set_frame(m, addr + 1, (n - 1) as nat, v / 256, m3); + assert(m2 == m3.insert(addr, (v % 256) as u8)); + assert(m.dom().contains(addr)); + assert(m2.dom() =~= m.dom()); + }, + None => {}, + } + } +} + +/// A write to mapped bytes succeeds. +pub proof fn lemma_mem_set_some(m: Map, addr: int, n: nat, v: int) + requires + 0 <= addr, + addr + n < 0x1_0000_0000_0000_0000, + forall|i: int| 0 <= i < n ==> m.dom().contains(#[trigger] (addr + i)), + ensures + isa::mem_set(m, addr, n, v) is Some, + decreases n, +{ + if n > 0 { + lemma_trunc64(addr); + assert(m.dom().contains(addr + 0)); + assert forall|i: int| 0 <= i < n - 1 implies m.dom().contains(#[trigger] (addr + 1 + i)) by { + assert(m.dom().contains(addr + (i + 1))); + } + lemma_mem_set_some(m, addr + 1, (n - 1) as nat, v / 256); + } +} + +/// Inserting a byte outside a read's range does not change the read. +pub proof fn lemma_mem_read_insert_other(m: Map, a: int, n: nat, b: int, x: u8) + requires + 0 <= a, + a + n < 0x1_0000_0000_0000_0000, + b < a || a + n <= b, + ensures + isa::mem_read(m.insert(b, x), a, n) == isa::mem_read(m, a, n), + decreases n, +{ + if n > 0 { + lemma_trunc64(a); + lemma_mem_read_insert_other(m, a + 1, (n - 1) as nat, b, x); + assert(m.insert(b, x).dom().contains(a) == m.dom().contains(a)); + if m.dom().contains(a) { + assert(m.insert(b, x)[a] == m[a]); + } + } +} + +/// Two memories that agree on the bytes of a read give the same read. +pub proof fn lemma_mem_read_congr(m: Map, m2: Map, a: int, n: nat) + requires + 0 <= a, + a + n < 0x1_0000_0000_0000_0000, + forall|i: int| + 0 <= i < n ==> (m.dom().contains(#[trigger] (a + i)) == m2.dom().contains(a + i)) && ( + m.dom().contains(a + i) ==> m[a + i] == m2[a + i]), + ensures + isa::mem_read(m, a, n) == isa::mem_read(m2, a, n), + decreases n, +{ + if n > 0 { + lemma_trunc64(a); + assert forall|i: int| 0 <= i < n - 1 implies (m.dom().contains(#[trigger] (a + 1 + i)) + == m2.dom().contains(a + 1 + i)) && (m.dom().contains(a + 1 + i) ==> m[a + 1 + i] + == m2[a + 1 + i]) by { + assert(a + (i + 1) == a + 1 + i); + } + lemma_mem_read_congr(m, m2, a + 1, (n - 1) as nat); + assert(a + 0 == a); + } +} + +/// Reading what was just written gives the value written (below `2^(8n)`). +pub proof fn lemma_read_after_write_same( + m: Map, + addr: int, + n: nat, + v: int, + m2: Map, +) + requires + isa::mem_set(m, addr, n, v) == Some(m2), + 0 <= addr, + addr + n < 0x1_0000_0000_0000_0000, + 0 <= v < vstd::arithmetic::power2::pow2(8 * n) as int, + ensures + isa::mem_read(m2, addr, n) == Some(v), + decreases n, +{ + lemma_p2_consts(); + if n == 0 { + vstd::arithmetic::power2::lemma2_to64(); + assert(v == 0); + } else { + lemma_trunc64(addr); + match isa::mem_set(m, addr + 1, (n - 1) as nat, v / 256) { + Some(m3) => { + let n1 = (n - 1) as nat; + assert(vstd::arithmetic::power2::pow2(8 * n) == 256 * vstd::arithmetic::power2::pow2( + 8 * n1, + )) by { + vstd::arithmetic::power2::lemma_pow2_adds(8, 8 * n1); + assert(8 * n == 8 + 8 * n1); + } + assert(0 <= v / 256 < vstd::arithmetic::power2::pow2(8 * n1) as int) by { + vstd::arithmetic::power2::lemma_pow2_pos(8 * n1); + } + lemma_read_after_write_same(m, addr + 1, (n - 1) as nat, v / 256, m3); + assert(m2 == m3.insert(addr, (v % 256) as u8)); + lemma_mem_set_frame(m, addr + 1, (n - 1) as nat, v / 256, m3); + lemma_mem_read_insert_other(m3, addr + 1, (n - 1) as nat, addr, (v % 256) as u8); + assert(m3.dom().contains(addr)); + assert(m2.dom().contains(addr)); + assert(m2[addr] == (v % 256) as u8); + assert(v == v % 256 + 256 * (v / 256)); + }, + None => {}, + } + } +} + +/// A read of bytes disjoint from a write is not changed by it. +pub proof fn lemma_read_after_write_disjoint( + m: Map, + addr: int, + n: nat, + v: int, + m2: Map, + b: int, + k: nat, +) + requires + isa::mem_set(m, addr, n, v) == Some(m2), + 0 <= addr, + addr + n < 0x1_0000_0000_0000_0000, + 0 <= b, + b + k < 0x1_0000_0000_0000_0000, + b + k <= addr || addr + n <= b, + ensures + isa::mem_read(m2, b, k) == isa::mem_read(m, b, k), +{ + lemma_mem_set_frame(m, addr, n, v, m2); + assert forall|i: int| 0 <= i < k implies (m.dom().contains(#[trigger] (b + i)) + == m2.dom().contains(b + i)) && (m.dom().contains(b + i) ==> m[b + i] == m2[b + i]) by { + assert(m2.dom().contains(b + i) == m.dom().contains(b + i)); + } + lemma_mem_read_congr(m, m2, b, k); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/mid.rs b/crates/clazel-jit/src/a64/mid.rs new file mode 100644 index 000000000..49eb13142 --- /dev/null +++ b/crates/clazel-jit/src/a64/mid.rs @@ -0,0 +1,254 @@ +//! The register-only middle of each template computes LIR's result: from a +//! state whose scratch registers hold the loaded operands, the middle does not +//! fault and leaves the result word in `x0`, changing only scratch registers, +//! the flags and `pc`. The A64 counterpart of [`crate::mid`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// What every middle establishes: register `rd` holds `w`; the register file +/// has its 31 registers; the context register, the link register, `SP`, the +/// vector registers and memory are as they were; `pc` advanced by `len`. +pub open spec fn mid_post(m: isa::State, m1: isa::State, rd: u8, w: nat, len: nat) -> bool { + m1.r.len() == 31 && m1.vreg == m.vreg && m1.r[rd as int] as nat == w && m1.r[abi::ctx_reg() as int] + == m.r[abi::ctx_reg() as int] && m1.r[abi::link_reg() as int] == m.r[abi::link_reg() as int] + && m1.sp == m.sp && m1.mem == m.mem && m1.pc as int == m.pc as int + len +} + +/// A step that sets only the flags and `pc` keeps the state well formed, and +/// every register. +pub proof fn lemma_wf_flags(s: isa::State, s1: isa::State) + requires + isa::wf(s), + s1.r == s.r, + s1.vreg == s.vreg, + s1.mem == s.mem, + s1.sp == s.sp, + ensures + isa::wf(s1), +{ +} + +/// `wrote_x0` establishes [`mid_post`] for a value below `2^n`. +pub proof fn lemma_wrote_post(s: isa::State, v: int, n: nat) + requires + isa::wf(s), + n == 32 || n == 64, + 0 <= v < isa::p2(n), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::wf(super::alu::wrote_x0(s, v, n)), + mid_post(s, super::alu::wrote_x0(s, v, n), 0, v as nat, 4), +{ + super::memlemmas::lemma_p2_consts(); + super::alu::lemma_trunc_small(n, v); + super::alu::lemma_pc4(s.pc); + assert(v < 0x1_0000_0000_0000_0000); + let s1 = super::alu::wrote_x0(s, v, n); + assert(s1.r[0] == v as u64); +} + +/// **`op x0, x0, x1`** computes LIR's integer binary operator. +pub proof fn lemma_mid_int(s: isa::State, sf: isa::Width, o: lir::IBin, x: u64, y: u64) + requires + isa::wf(s), + crate::spec::ibin_supported(o), + s.r[0] == x, + s.r[1] == y, + x < isa::p2(super::alu::bits(sf)), + y < isa::p2(super::alu::bits(sf)), + s.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + run_seq(s, seq![super::templates::int_op_instr(o, sf)]) is Some, + mid_post( + s, + run_seq(s, seq![super::templates::int_op_instr(o, sf)]).unwrap(), + 0, + super::intops::ibin_w(o, sf, x, y) as nat, + 4, + ), +{ + super::alu::lemma_bits(sf); + super::intops::lemma_step_int_op(s, sf, o, x, y); + super::intops::lemma_ibin_w_bound(o, sf, x, y); + super::runseq::lemma_run_seq_one(s, super::templates::int_op_instr(o, sf)); + lemma_wrote_post(s, super::intops::ibin_w(o, sf, x, y), super::alu::bits(sf)); +} + +/// **`cmp x0, x1; cset w0, cc`** computes LIR's integer comparison. +#[verifier::rlimit(300)] +pub proof fn lemma_mid_cmp(s: isa::State, c: lir::ICmp, sf: isa::Width, x: u64, y: u64) + requires + isa::wf(s), + s.r[0] == x, + s.r[1] == y, + x < isa::p2(super::alu::bits(sf)), + y < isa::p2(super::alu::bits(sf)), + s.pc as int + 8 < 0x1_0000_0000_0000_0000, + ensures + run_seq(s, super::templates::cmp_mid(c, sf)) is Some, + mid_post( + s, + run_seq(s, super::templates::cmp_mid(c, sf)).unwrap(), + 0, + lir::bool_i32(lir::icmp(c, super::alu::bits(sf), x as int, y as int)) as nat, + 8, + ), +{ + let n = super::alu::bits(sf); + super::alu::lemma_bits(sf); + super::memlemmas::lemma_p2_consts(); + let i1 = super::templates::cmp_rr(sf, 0, 1); + let i2 = super::templates::cset0(super::templates::cset_cond(c)); + assert(super::templates::cmp_mid(c, sf) == seq![i1, i2]); + super::flags::lemma_step_cmp(s, sf, x, y); + let t = isa::set_nzcv(s, super::flags::cmp_nzcv(n, x as int, y as int)); + let s1 = isa::State { pc: (s.pc + 4) as u64, ..t }; + assert(isa::step(s, i1) == Some(s1)); + lemma_wf_flags(s, s1); + assert(s1.r[0] == x && s1.r[1] == y); + assert(super::templates::cset_cond(c) < 14); + super::alu::lemma_pc4(s.pc); + super::flags::lemma_step_cset(s1, super::templates::cset_cond(c)); + super::flags::lemma_cc_icmp(s, c, n, x as int, y as int); + // the condition reads only the flags, which `t` and `s1` share + assert(isa::condition_holds(s1, super::templates::cset_cond(c)) == isa::condition_holds( + t, + super::templates::cset_cond(c), + )); + let holds = isa::condition_holds(s1, super::templates::cset_cond(c)); + let s2 = super::alu::wrote_x0(s1, if holds { + 0int + } else { + 1int + }, 32); + super::runseq::run2(s, i1, i2, s1, s2); + lemma_wrote_post(s1, if holds { + 0int + } else { + 1int + }, 32); + assert(s2.mem == s.mem && s2.sp == s.sp); + assert(s2.vreg == s.vreg); + assert(mid_post(s, s2, 0, lir::bool_i32(lir::icmp(c, n, x as int, y as int)) as nat, 8)); +} + +/// **`cmp x0, #0; cset w0, eq`** computes LIR's `eqz`. +#[verifier::rlimit(300)] +pub proof fn lemma_mid_eqz(s: isa::State, sf: isa::Width, x: u64) + requires + isa::wf(s), + s.r[0] == x, + x < isa::p2(super::alu::bits(sf)), + s.pc as int + 8 < 0x1_0000_0000_0000_0000, + ensures + run_seq(s, super::templates::eqz_mid(sf)) is Some, + mid_post( + s, + run_seq(s, super::templates::eqz_mid(sf)).unwrap(), + 0, + lir::bool_i32(x == 0) as nat, + 8, + ), +{ + let n = super::alu::bits(sf); + super::alu::lemma_bits(sf); + super::memlemmas::lemma_p2_consts(); + let i1 = super::templates::cmp_zero(sf, 0); + let i2 = super::templates::cset0(1); + assert(super::templates::eqz_mid(sf) == seq![i1, i2]); + super::flags::lemma_step_cmp_zero(s, sf, 0, x); + let t = isa::set_nzcv(s, super::flags::cmp_nzcv(n, x as int, 0)); + let s1 = isa::State { pc: (s.pc + 4) as u64, ..t }; + assert(isa::step(s, i1) == Some(s1)); + lemma_wf_flags(s, s1); + super::alu::lemma_pc4(s.pc); + super::flags::lemma_step_cset(s1, 1); + // `eq` compare against zero: the condition `ne` (the csinc's) fails iff x == 0 + super::memlemmas::lemma_p2_consts(); + assert(0 < isa::p2(n)); + super::flags::lemma_cc_icmp(s, lir::ICmp::Eq, n, x as int, 0); + assert(super::templates::cset_cond(lir::ICmp::Eq) == 1); + assert(isa::condition_holds(s1, 1) == isa::condition_holds(t, 1)); + let holds = isa::condition_holds(s1, 1); + let s2 = super::alu::wrote_x0(s1, if holds { + 0int + } else { + 1int + }, 32); + super::runseq::run2(s, i1, i2, s1, s2); + lemma_wrote_post(s1, if holds { + 0int + } else { + 1int + }, 32); + assert(lir::icmp(lir::ICmp::Eq, n, x as int, 0) == (x == 0)); + assert(s2.mem == s.mem && s2.sp == s.sp); + assert(s2.vreg == s.vreg); +} + +/// **`cmp w2, #0; csel x0, x0, x1, ne`** computes LIR's `select`. +#[verifier::rlimit(300)] +pub proof fn lemma_mid_select(s: isa::State, v1: u64, v2: u64, c: u32) + requires + isa::wf(s), + s.r[0] == v1, + s.r[1] == v2, + s.r[2] == c as u64, + s.pc as int + 8 < 0x1_0000_0000_0000_0000, + ensures + run_seq(s, super::templates::select_mid()) is Some, + mid_post( + s, + run_seq(s, super::templates::select_mid()).unwrap(), + 0, + (if c != 0 { + v1 + } else { + v2 + }) as nat, + 8, + ), +{ + super::memlemmas::lemma_p2_consts(); + let i1 = super::templates::cmp_zero(isa::Width::W32, 2); + let i2 = super::templates::select_mid()[1]; + assert(super::templates::select_mid() == seq![i1, i2]); + assert(super::alu::bits(isa::Width::W32) == 32); + assert((c as u64) < isa::p2(32)); + super::flags::lemma_step_cmp_zero(s, isa::Width::W32, 2, c as u64); + let t = isa::set_nzcv(s, super::flags::cmp_nzcv(32, c as int, 0)); + let s1 = isa::State { pc: (s.pc + 4) as u64, ..t }; + assert(isa::step(s, i1) == Some(s1)); + lemma_wf_flags(s, s1); + super::alu::lemma_pc4(s.pc); + // the Z flag is "c == 0" + super::flags::lemma_cmp_arith(32, c as int, 0); + assert(0 <= isa::bit(super::flags::cmp_nzcv(32, c as int, 0), 2)) by { + vstd::arithmetic::power2::lemma2_to64(); + } + assert(s1.flag_z == (c == 0)); + super::flags::lemma_step_csel_ne(s1, v1, v2); + let s2 = super::alu::wrote_x0(s1, if !s1.flag_z { + v1 as int + } else { + v2 as int + }, 64); + super::runseq::run2(s, i1, i2, s1, s2); + assert(isa::step(s1, i2) == Some(s2)); + let w = if c != 0 { + v1 as int + } else { + v2 as int + }; + assert(0 <= w < isa::p2(64)); + lemma_wrote_post(s1, w, 64); + assert(s2.mem == s.mem && s2.sp == s.sp); + assert(s2.vreg == s.vreg); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/mod.rs b/crates/clazel-jit/src/a64/mod.rs new file mode 100644 index 000000000..d1bee2f00 --- /dev/null +++ b/crates/clazel-jit/src/a64/mod.rs @@ -0,0 +1,162 @@ +//! The LIR → A64 back end (docs/phases/jit.md item J.9, second half). It +//! mirrors the x86-64 back end in the crate root, module for module, with the +//! A64 differences listed in [`spec`]'s header. Like the root, it was written +//! contract first (CLAUDE.md rule 3): [`spec`], [`contract`] and [`api`] came +//! in their own commit, before any template. +//! +//! - [`spec`]: **what the back end is about.** The state relation between a +//! LIR state and an A64 machine state, the frame layout (the A64 ABI of +//! `clazel-codecheck` fixes where the code may write), the exit sequence. +//! Read its header first: it lists the modelling choices. +//! - [`contract`]: the refinement statements (a template's A64 code, run under +//! `clazel-a64`'s `step` from a related state, reaches a state related to +//! LIR's `eval`), and the emitter's contracts. +//! - [`api`]: the contract-only trait [`api::Backend`]. +//! +//! # The templates and their proofs +//! +//! - [`templates`]: the templates, as spec functions (`tmpl`, `code_of`). +//! - [`emit`]: the executable back end, [`emit::A64Backend`], proven equal to +//! the templates and, through `clazel-a64`'s encoder, to their bytes. +//! - Proofs, bottom up: [`memlemmas`] (byte memory), [`steps`] (the load and +//! store of a frame slot), [`alu`], [`intops`], [`flags`] and [`bitfield`] +//! (one `isa::step` of each instruction form the middles use, as numbers), +//! [`runseq`] (running a list), [`mid`] and [`bitmid`] (each template's +//! register-only middle computes LIR's result), [`rel`] and [`skel`] (the +//! loads-middle-store skeleton preserves the state relation), [`proofs`] (the +//! refinement of each template, `lemma_op_refines`), [`compose`] (the +//! program, `lemma_prog_refines`), [`exit`] (the exit sequence), [`tlemmas`] +//! (templates are in the subset, and are what the emitter pushes). +//! +//! # What is proven (Verus, no axiom, no assumption) +//! +//! - **Each template refines its LIR instruction** ([`contract::refines`]), +//! for: `nop`, `drop`, `t.const`, `local.get`, `local.set`, `local.tee`, +//! `select`; `i32` and `i64` `add`, `sub`, `mul`, `and`, `or`, `xor`, +//! `shl`, `shr_s`, `shr_u`, and the ten comparisons; `i32.eqz`, +//! `i64.eqz`, `i32.wrap_i64`, `i64.extend_i32_s`, `i64.extend_i32_u`, +//! `f64.neg`, `f64.abs` and the two `reinterpret`s (the last four are bit +//! operations). The same list as the x86-64 back end. +//! - **A straight-line program of them refines** the left-nested `then` chain +//! ([`compose::lemma_prog_refines`]). +//! - **The exit sequence refines `exit c`** with no operands +//! ([`contract::exit_refines`], [`exit::lemma_exit_refines`]). +//! - **The emitter returns exactly the template code, and its encoding** +//! ([`contract::emit_body_ensures`], [`contract::emit_blob_ensures`]), and +//! refuses (`None`) exactly the programs [`spec::prog_ok`] refuses. +//! +//! # Control flow and memory (the second slice) +//! +//! The A64 counterpart of the x86-64 second slice, written contract first +//! ([`cfspec`], [`cfcontract`], [`cftemplates`] in their own commit); the +//! source language [`crate::cfspec::Node`] and its analysis +//! ([`crate::cfspec::cf_flow`], sound for LIR's `eval`: [`crate::cfheight`]) +//! are shared with x86-64. Differences: a branch is relative to its own +//! address and in words, so a label offset is a multiple of 4 and a +//! conditional branch reaches 1 MiB each way ([`cffit`]; the translation +//! refuses code above [`cftemplates::max_size`] bytes, `2^19`); exit codes are +//! 16 bits; there is no native stack word and no `create_undef`. +//! +//! - **Proven, no axiom, no assumption:** +//! - composition over `then`, `block`, `loop` and `if` ([`cfcompose`], +//! [`cfblock`], [`cfif`]) and the leaves `br` (`b`), `br_if` (`cbnz`), +//! `exit` **with operands** and a straight-line instruction ([`cfleaf`], +//! [`cfexit`], [`cfop`]); +//! - **the memory instructions** `bounds_check`, `load` and `store` +//! ([`cfmemops`]): the code is the validator's own bounds-check sequence +//! (`clazel_codecheck::a64::spec::check_seq`, run by [`cfchk`]) around the +//! access, with the two exits going to a stub after the access; the check +//! fails exactly when LIR's `bounds_check` exits (where a `load` or +//! `store` is `Stuck` nothing is claimed); +//! - **`br_table`** ([`cfbrt`]): a `movz`, `cmp`, `b.eq` chain and a `b` to +//! the default; +//! - by induction over the program, **every templated node's code refines +//! it, is in the subset, and every displacement fits its field** +//! ([`cfproofs::lemma_ctmpl_refines`], [`cffit::labels_fit`]); +//! - **the emitter** [`cfemit::emit_cf_blob`] is proven equal to the +//! templates ([`cfcontract::emit_cf_blob_ensures`]); positions are words, +//! displacements are formed without signed arithmetic; +//! - the validator's access rule admits the access a memory instruction +//! makes ([`cfmemvalid::lemma_load_access_accepted`]). +//! `tests/validator_a64_cf.rs` checks that the A64 validator accepts every +//! memory template, `br_table`, and 1500 generated programs with control +//! flow and memory. +//! - **The validator accepts every emitted blob (proven)** +//! ([`cfblobacc::theorem_blob_accepted`], carried by `emit_cf_blob` as a +//! postcondition, with the exactness of its instructions +//! [`cfblobacc::emitted_ins`]): for every program with a translation, +//! `clazel_codecheck::a64::spec::accepted(blob, [0])`. The A64 counterpart +//! of the x86-64 proof (see `crate::cfblobacc`): units ([`cfunits`], +//! [`cfaccept`]: an exit sequence of 3, the bounds-check sequence of 7, +//! single instructions), the scan over them ([`cfuacc`], [`cfuacc2`], +//! [`cfsimple`]), and where branches land ([`cfjt`], [`cfbs`]). +//! - **Not done:** blocks with parameters or results; `f64` arithmetic; +//! rotates; a frame beyond 31 slots. +//! +//! # What is not (of the first slice) +//! +//! - **That the A64 validator accepts every blob of the first slice's own +//! emitter** is checked by `tests/validator_a64.rs` (every template, 3000 +//! generated programs), not proven; the control-flow emitter is proven +//! above. +//! - **Not covered by any template:** `f64` arithmetic, comparison and +//! conversion (LIR's `f64` operators are uninterpreted functions, so a +//! refinement to `FADD` and the rest would need an axiom this crate does not +//! add); rotates (`clazel-a64`'s subset has no `RORV`); a frame beyond 31 +//! slots (it needs the region frame and the bounds-check sequence on every +//! access). +//! - **No tier is claimed** (`tier` stays 0): the crate has no independent +//! transcription or conformance suite. The proofs are about the spec of +//! `clazel-a64` and the semantics of `clazel-lir`, whose faithfulness is +//! their crates' claim. + +pub mod alu; +pub mod api; +pub mod bitfield; +pub mod bitmid; +pub mod cfaccept; +pub mod cfalg; +pub mod cfblobacc; +pub mod cfblock; +pub mod cfbrt; +pub mod cfbs; +pub mod cfchk; +pub mod cfcompose; +pub mod cfcontract; +pub mod cfemit; +pub mod cfexit; +pub mod cffit; +pub mod cfif; +pub mod cfjt; +pub mod cfleaf; +pub mod cflens; +pub mod cfmemops; +pub mod cfmemrel; +pub mod cfmemsteps; +pub mod cfmemvalid; +pub mod cfop; +pub mod cfproofs; +pub mod cfsimple; +pub mod cfspec; +pub mod cfsteps; +pub mod cftemplates; +pub mod cfuacc; +pub mod cfuacc2; +pub mod cfunits; +pub mod cfvalid; +pub mod compose; +pub mod contract; +pub mod emit; +pub mod exit; +pub mod flags; +pub mod intops; +pub mod memlemmas; +pub mod mid; +pub mod proofs; +pub mod rel; +pub mod runseq; +pub mod skel; +pub mod spec; +pub mod steps; +pub mod templates; +pub mod tlemmas; diff --git a/crates/clazel-jit/src/a64/proofs.rs b/crates/clazel-jit/src/a64/proofs.rs new file mode 100644 index 000000000..9d01fb249 --- /dev/null +++ b/crates/clazel-jit/src/a64/proofs.rs @@ -0,0 +1,1118 @@ +//! The refinement of each template ([`lemma_op_refines`]): for every +//! instruction of the slice, its template satisfies [`refines`]. One lemma per +//! kind of instruction, each an instance of [`super::skel::lemma_shape`] with +//! the middle's lemma from [`super::mid`] or [`super::bitmid`]. The A64 +//! counterpart of [`crate::proofs`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The hypotheses every case starts from: the stack has `h` values, the states +/// are related, the end of the code does not wrap, and LIR's step fell through +/// to `a2`. +pub open spec fn case_pre( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + code: Seq, + i: lir::Instr, + fuel: nat, +) -> bool { + a.stack.len() == h && rel(lay, a, m, nl) && (m.pc as int) + code_len(code) + < 0x1_0000_0000_0000_0000 && lir::eval(lay, i, a, fuel) == lir::Outcome::Normal(a2) +} + +/// A value's stored word is below `2^64`, and below `2^32` for an `i32`. +pub proof fn lemma_bits_u64(v: lir::Val) + ensures + lir::val_bits(v) < 0x1_0000_0000_0000_0000, + v is I32 ==> lir::val_bits(v) < 0x1_0000_0000, +{ + match v { + lir::Val::I32(x) => {}, + lir::Val::I64(x) => {}, + lir::Val::F64(x) => {}, + } +} + +/// `x0`, `x1`, `x2` are distinct scratch registers. +pub proof fn lemma_scratch_regs() + ensures + super::rel::scratch(x0()), + super::rel::scratch(x1()), + super::rel::scratch(x2()), + x0() != x1() && x0() != x2() && x1() != x2(), +{ + super::steps::lemma_abi_consts(); +} + +/// One load of a scratch register from an existing slot is [`super::skel::loads_ok`]. +pub proof fn lemma_loads_ok1(r: u8, j: nat, nl: nat, n: nat) + requires + super::rel::scratch(r), + j < nl + n, + ensures + super::skel::loads_ok(seq![(r, j)], nl, n), +{ + let ls = seq![(r, j)]; + assert forall|k: int| 0 <= k < ls.len() implies super::rel::scratch((#[trigger] ls[k]).0) && ls[k].1 < nl + + n by { + assert(k == 0); + } +} + +/// Two loads into different scratch registers. +pub proof fn lemma_loads_ok2(r1: u8, j1: nat, r2: u8, j2: nat, nl: nat, n: nat) + requires + super::rel::scratch(r1), + super::rel::scratch(r2), + r1 != r2, + j1 < nl + n, + j2 < nl + n, + ensures + super::skel::loads_ok(seq![(r1, j1), (r2, j2)], nl, n), +{ + let ls = seq![(r1, j1), (r2, j2)]; + assert forall|k: int| 0 <= k < ls.len() implies super::rel::scratch((#[trigger] ls[k]).0) && ls[k].1 < nl + + n by { + assert(k == 0 || k == 1); + } + assert forall|k1: int, k2: int| + 0 <= k1 < k2 < ls.len() implies (#[trigger] ls[k1]).0 != (#[trigger] ls[k2]).0 by { + assert(k1 == 0 && k2 == 1); + } +} + +/// Three loads into different scratch registers. +pub proof fn lemma_loads_ok3( + r1: u8, + j1: nat, + r2: u8, + j2: nat, + r3: u8, + j3: nat, + nl: nat, + n: nat, +) + requires + super::rel::scratch(r1), + super::rel::scratch(r2), + super::rel::scratch(r3), + r1 != r2 && r1 != r3 && r2 != r3, + j1 < nl + n, + j2 < nl + n, + j3 < nl + n, + ensures + super::skel::loads_ok(seq![(r1, j1), (r2, j2), (r3, j3)], nl, n), +{ + let ls = seq![(r1, j1), (r2, j2), (r3, j3)]; + assert forall|k: int| 0 <= k < ls.len() implies super::rel::scratch((#[trigger] ls[k]).0) && ls[k].1 < nl + + n by { + assert(k == 0 || k == 1 || k == 2); + } + assert forall|k1: int, k2: int| + 0 <= k1 < k2 < ls.len() implies (#[trigger] ls[k1]).0 != (#[trigger] ls[k2]).0 by { + assert(k1 == 0 || k1 == 1); + } +} + +/// The length of a list of `k` instructions, in bytes. +pub proof fn lemma_len_seq(code: Seq) + ensures + code_len(code) == 4 * code.len(), +{ +} + +// ---------------------------------------------------------------------------- +// Constants, locals, drop and nop +// ---------------------------------------------------------------------------- + +/// `t.const v` refines. +#[verifier::rlimit(500)] +pub proof fn lemma_case_const( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + v: lir::Val, + fuel: nat, +) + requires + op_ok(Op::Const(v), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Const(v), nl, h), Op::Const(v)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Const(v), nl, h), + run_seq(m, super::templates::tmpl(Op::Const(v), nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + assert(a2 == a.with_stack(a.stack.push(v))); + crate::agree::lemma_agree_push(a, a2, nl, h, v); + let mid = super::templates::const_mid(v); + let ls = Seq::<(u8, nat)>::empty(); + assert(super::templates::tmpl(Op::Const(v), nl, h) == super::templates::shape(ls, mid, nl + h)); + assert(super::skel::loads_ok(ls, nl, a.stack.len())); + assert(super::skel::mid_prop(a, nl, ls, mid, lir::val_bits(v))) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + lir::val_bits(v), + s, + ) by { + super::bitmid::lemma_mid_const(s, v); + } + } + super::steps::lemma_abi_consts(); + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, nl + h, lir::val_bits(v)); +} + +/// `local.get x` refines. +#[verifier::rlimit(500)] +pub proof fn lemma_case_local_get( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + x: usize, + fuel: nat, +) + requires + op_ok(Op::LocalGet(x), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::LocalGet(x), nl, h), Op::LocalGet(x)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::LocalGet(x), nl, h), + run_seq(m, super::templates::tmpl(Op::LocalGet(x), nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + let xn = x as nat; + assert(xn < a.locals.len()); + let v = a.locals[xn as int]; + assert(a2 == a.with_stack(a.stack.push(v))); + crate::agree::lemma_agree_push(a, a2, nl, h, v); + let mid = Seq::::empty(); + let ls = seq![(x0(), xn)]; + assert(super::templates::tmpl(Op::LocalGet(x), nl, h) == super::templates::shape(ls, mid, nl + h)); + assert(slot_val(a, nl, xn) == v); + lemma_scratch_regs(); + lemma_loads_ok1(x0(), xn, nl, a.stack.len()); + assert(super::skel::mid_prop(a, nl, ls, mid, lir::val_bits(v))) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + lir::val_bits(v), + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == lir::val_bits(slot_val(a, nl, xn))); + assert(run_seq(s, mid) == Some(s)); + } + } + super::steps::lemma_abi_consts(); + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, nl + h, lir::val_bits(v)); +} + +/// `local.set x` and `local.tee x` refine. +#[verifier::rlimit(500)] +pub proof fn lemma_case_local_set_tee( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + op: Op, + x: usize, + fuel: nat, +) + requires + op == Op::LocalSet(x) || op == Op::LocalTee(x), + op_ok(op, nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(op, nl, h), op@, fuel), + ensures + super::contract::refines_post(lay, a2, m, nl, super::templates::tmpl(op, nl, h), run_seq(m, super::templates::tmpl(op, nl, h))), +{ + super::steps::lemma_frame_agrees(); + let xn = x as nat; + let top = (nl + h - 1) as nat; + if op == Op::LocalSet(x) { + assert(a2 == (lir::St { + stack: a.stack.drop_last(), + locals: a.locals.update(xn as int, a.stack.last()), + mem: a.mem, + })); + crate::agree::lemma_agree_set(a, a2, nl, h, xn); + } else { + assert(a2 == a.with_locals(a.locals.update(xn as int, a.stack.last()))); + crate::agree::lemma_agree_tee(a, a2, nl, h, xn); + } + let mid = Seq::::empty(); + let ls = seq![(x0(), top)]; + let w = lir::val_bits(slot_val(a, nl, top)); + assert(super::templates::tmpl(op, nl, h) == super::templates::shape(ls, mid, xn)); + super::steps::lemma_abi_consts(); + lemma_scratch_regs(); + lemma_loads_ok1(x0(), top, nl, a.stack.len()); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal(mid, w, s) by { + assert(s.r[ls[0].0 as int] as nat == lir::val_bits(slot_val(a, nl, top))); + assert(run_seq(s, mid) == Some(s)); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, xn, w); +} + +/// `drop` and `nop` refine (no code). +pub proof fn lemma_case_drop_nop( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + op: Op, + fuel: nat, +) + requires + op is Drop || op is Nop, + op_ok(op, nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(op, nl, h), op@, fuel), + ensures + super::contract::refines_post(lay, a2, m, nl, super::templates::tmpl(op, nl, h), run_seq(m, super::templates::tmpl(op, nl, h))), +{ + super::steps::lemma_frame_agrees(); + assert(super::templates::tmpl(op, nl, h) == Seq::::empty()); + assert(run_seq(m, super::templates::tmpl(op, nl, h)) == Some(m)); + if op is Drop { + assert(a2 == a.with_stack(a.stack.drop_last())); + crate::agree::lemma_agree_drop(a, a2, nl, h); + } else { + assert(a2 == a); + crate::agree::lemma_agree_refl(a, nl); + } + super::rel::lemma_rel_agree(lay, a, a2, m, nl); + assert(preserved(m, m)); +} + +// ---------------------------------------------------------------------------- +// select +// ---------------------------------------------------------------------------- + +/// `select` refines. +#[verifier::rlimit(500)] +pub proof fn lemma_case_select( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Select, nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Select, nl, h), Op::Select@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Select, nl, h), + run_seq(m, super::templates::tmpl(Op::Select, nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + lemma_scratch_regs(); + let t = (nl + h - 3) as nat; + let v1 = a.stack[(h - 3) as int]; + let v2 = a.stack[(h - 2) as int]; + match a.stack[(h - 1) as int] { + lir::Val::I32(c) => { + let r = if c != 0 { + v1 + } else { + v2 + }; + assert(a2 == a.with_stack(lir::below(a.stack, 3).push(r))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 3, r); + let ls = seq![(x0(), t), (x1(), t + 1), (x2(), t + 2)]; + let mid = super::templates::select_mid(); + let w = lir::val_bits(r); + assert(super::templates::tmpl(Op::Select, nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok3(x0(), t, x1(), t + 1, x2(), t + 2, nl, a.stack.len()); + assert(slot_val(a, nl, t) == v1); + assert(slot_val(a, nl, t + 1) == v2); + assert(slot_val(a, nl, t + 2) == lir::Val::I32(c)); + lemma_bits_u64(v1); + lemma_bits_u64(v2); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == lir::val_bits(v1)); + assert(s.r[ls[1].0 as int] as nat == lir::val_bits(v2)); + assert(s.r[ls[2].0 as int] as nat == c as nat); + assert(s.pc as int + 8 < 0x1_0000_0000_0000_0000); + super::mid::lemma_mid_select(s, lir::val_bits(v1) as u64, lir::val_bits(v2) as u64, c); + assert(code_len(mid) == 8); + } + } + super::steps::lemma_abi_consts(); + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +// ---------------------------------------------------------------------------- +// Binary instructions +// ---------------------------------------------------------------------------- + +/// An `i32` or `i64` arithmetic, logic or shift instruction refines: `sf` is +/// the width, `v1`, `v2` the operand values and `r` the result value. +#[verifier::rlimit(500)] +pub proof fn lemma_case_bin_int( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + b: lir::BinOp, + o: lir::IBin, + sf: isa::Width, + fuel: nat, +) + requires + b == lir::BinOp::I32(o) && sf is W32 || b == lir::BinOp::I64(o) && sf is W64, + op_ok(Op::Bin(b), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Bin(b), nl, h), Op::Bin(b)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Bin(b), nl, h), + run_seq(m, super::templates::tmpl(Op::Bin(b), nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + super::memlemmas::lemma_p2_consts(); + let t = (nl + h - 2) as nat; + let ls = seq![(x0(), t), (x1(), t + 1)]; + let mid = seq![super::templates::int_op_instr(o, sf)]; + assert(super::templates::tmpl(Op::Bin(b), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok2(x0(), t, x1(), t + 1, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 2) as int]); + assert(slot_val(a, nl, t + 1) == a.stack[(h - 1) as int]); + match (a.stack[(h - 2) as int], a.stack[(h - 1) as int]) { + (lir::Val::I32(x), lir::Val::I32(y)) => { + assert(sf is W32); + let v = lir::Val::I32(lir::ibin32(o, x, y)); + assert(a2 == a.with_stack(lir::below(a.stack, 2).push(v))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 2, v); + let w = lir::val_bits(v); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == x as nat); + assert(s.r[ls[1].0 as int] as nat == y as nat); + assert(super::alu::bits(sf) == 32); + assert(s.pc as int + 4 < 0x1_0000_0000_0000_0000); + super::mid::lemma_mid_int(s, sf, o, x as u64, y as u64); + assert(((x as u64) as u32) == x && ((y as u64) as u32) == y); + assert(code_len(mid) == 4); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + (lir::Val::I64(x), lir::Val::I64(y)) => { + assert(sf is W64); + let v = lir::Val::I64(lir::ibin64(o, x, y)); + assert(a2 == a.with_stack(lir::below(a.stack, 2).push(v))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 2, v); + let w = lir::val_bits(v); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == x as nat); + assert(s.r[ls[1].0 as int] as nat == y as nat); + assert(super::alu::bits(sf) == 64); + assert(s.pc as int + 4 < 0x1_0000_0000_0000_0000); + super::mid::lemma_mid_int(s, sf, o, x, y); + assert(code_len(mid) == 4); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// An `i32` or `i64` comparison refines. +#[verifier::rlimit(500)] +pub proof fn lemma_case_cmp_int( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + b: lir::BinOp, + c: lir::ICmp, + sf: isa::Width, + fuel: nat, +) + requires + b == lir::BinOp::I32Cmp(c) && sf is W32 || b == lir::BinOp::I64Cmp(c) && sf is W64, + op_ok(Op::Bin(b), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Bin(b), nl, h), Op::Bin(b)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Bin(b), nl, h), + run_seq(m, super::templates::tmpl(Op::Bin(b), nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + super::memlemmas::lemma_p2_consts(); + let t = (nl + h - 2) as nat; + let ls = seq![(x0(), t), (x1(), t + 1)]; + let mid = super::templates::cmp_mid(c, sf); + assert(super::templates::tmpl(Op::Bin(b), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok2(x0(), t, x1(), t + 1, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 2) as int]); + assert(slot_val(a, nl, t + 1) == a.stack[(h - 1) as int]); + match (a.stack[(h - 2) as int], a.stack[(h - 1) as int]) { + (lir::Val::I32(x), lir::Val::I32(y)) => { + assert(sf is W32); + let v = lir::Val::I32(lir::bool_i32(lir::icmp(c, 32, x as int, y as int))); + assert(a2 == a.with_stack(lir::below(a.stack, 2).push(v))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 2, v); + let w = lir::val_bits(v); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == x as nat); + assert(s.r[ls[1].0 as int] as nat == y as nat); + assert(super::alu::bits(sf) == 32); + assert(s.pc as int + 8 < 0x1_0000_0000_0000_0000); + super::mid::lemma_mid_cmp(s, c, sf, x as u64, y as u64); + assert(code_len(mid) == 8); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + (lir::Val::I64(x), lir::Val::I64(y)) => { + assert(sf is W64); + let v = lir::Val::I32(lir::bool_i32(lir::icmp(c, 64, x as int, y as int))); + assert(a2 == a.with_stack(lir::below(a.stack, 2).push(v))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 2, v); + let w = lir::val_bits(v); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == x as nat); + assert(s.r[ls[1].0 as int] as nat == y as nat); + assert(super::alu::bits(sf) == 64); + assert(s.pc as int + 8 < 0x1_0000_0000_0000_0000); + super::mid::lemma_mid_cmp(s, c, sf, x, y); + assert(code_len(mid) == 8); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +// ---------------------------------------------------------------------------- +// Unary instructions +// ---------------------------------------------------------------------------- + +/// `i32.eqz` and `i64.eqz` refine. +#[verifier::rlimit(500)] +pub proof fn lemma_case_eqz( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + u: lir::UnOp, + sf: isa::Width, + fuel: nat, +) + requires + u == lir::UnOp::I32Eqz && sf is W32 || u == lir::UnOp::I64Eqz && sf is W64, + op_ok(Op::Un(u), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(u), nl, h), Op::Un(u)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(u), nl, h), + run_seq(m, super::templates::tmpl(Op::Un(u), nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + super::memlemmas::lemma_p2_consts(); + let t = (nl + h - 1) as nat; + let ls = seq![(x0(), t)]; + let mid = super::templates::eqz_mid(sf); + assert(super::templates::tmpl(Op::Un(u), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(x0(), t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + match a.stack[(h - 1) as int] { + lir::Val::I32(x) => { + assert(sf is W32); + let v = lir::Val::I32(lir::bool_i32(x == 0)); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let w = lir::val_bits(v); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == x as nat); + assert(super::alu::bits(sf) == 32); + assert(s.pc as int + 8 < 0x1_0000_0000_0000_0000); + super::mid::lemma_mid_eqz(s, sf, x as u64); + assert(code_len(mid) == 8); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + lir::Val::I64(x) => { + assert(sf is W64); + let v = lir::Val::I32(lir::bool_i32(x == 0)); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let w = lir::val_bits(v); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == x as nat); + assert(super::alu::bits(sf) == 64); + assert(s.pc as int + 8 < 0x1_0000_0000_0000_0000); + super::mid::lemma_mid_eqz(s, sf, x); + assert(code_len(mid) == 8); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `f64.neg` refines. +#[verifier::rlimit(500)] +pub proof fn lemma_case_f64_neg( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::F64Neg), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::F64Neg), nl, h), Op::Un(lir::UnOp::F64Neg)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::F64Neg), nl, h), + run_seq(m, super::templates::tmpl(Op::Un(lir::UnOp::F64Neg), nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 1) as nat; + match a.stack[(h - 1) as int] { + lir::Val::F64(x) => { + let v = lir::Val::F64(x ^ 0x8000_0000_0000_0000); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let ls = seq![(x0(), t)]; + let mid = super::templates::neg_mid(); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Un(lir::UnOp::F64Neg), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(x0(), t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == x as nat); + assert(s.pc as int + 8 < 0x1_0000_0000_0000_0000); + super::bitmid::lemma_mid_neg(s, x); + assert(code_len(mid) == 8); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `f64.abs` refines. +#[verifier::rlimit(500)] +pub proof fn lemma_case_f64_abs( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::F64Abs), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::F64Abs), nl, h), Op::Un(lir::UnOp::F64Abs)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::F64Abs), nl, h), + run_seq(m, super::templates::tmpl(Op::Un(lir::UnOp::F64Abs), nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 1) as nat; + match a.stack[(h - 1) as int] { + lir::Val::F64(x) => { + let v = lir::Val::F64(x & 0x7FFF_FFFF_FFFF_FFFF); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let ls = seq![(x0(), t)]; + let mid = super::templates::abs_mid(); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Un(lir::UnOp::F64Abs), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(x0(), t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == x as nat); + assert(s.pc as int + 4 < 0x1_0000_0000_0000_0000); + super::bitmid::lemma_mid_abs(s, x); + assert(code_len(mid) == 4); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `i32.wrap_i64` refines. +#[verifier::rlimit(500)] +pub proof fn lemma_case_wrap( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::I32WrapI64), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::I32WrapI64), nl, h), Op::Un(lir::UnOp::I32WrapI64)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::I32WrapI64), nl, h), + run_seq(m, super::templates::tmpl(Op::Un(lir::UnOp::I32WrapI64), nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 1) as nat; + match a.stack[(h - 1) as int] { + lir::Val::I64(x) => { + let v = lir::Val::I32((x & 0xFFFF_FFFF) as u32); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let ls = seq![(x0(), t)]; + let mid = super::templates::wrap_mid(); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Un(lir::UnOp::I32WrapI64), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(x0(), t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == x as nat); + assert(s.pc as int + 4 < 0x1_0000_0000_0000_0000); + super::bitmid::lemma_mid_wrap(s, x); + assert(code_len(mid) == 4); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `i64.extend_i32_s` refines. +#[verifier::rlimit(500)] +pub proof fn lemma_case_extend_s( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::I64ExtendI32S), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32S), nl, h), Op::Un(lir::UnOp::I64ExtendI32S)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32S), nl, h), + run_seq(m, super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32S), nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 1) as nat; + match a.stack[(h - 1) as int] { + lir::Val::I32(x) => { + let v = lir::Val::I64(lir::wrap(64, lir::signed_of(32, x as int)) as u64); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + crate::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let ls = seq![(x0(), t)]; + let mid = super::templates::exts_mid(); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32S), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(x0(), t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(a, nl, ls, mid, w)) by { + assert forall|s: isa::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + mid, + w, + s, + ) by { + assert(s.r[ls[0].0 as int] as nat == x as nat); + assert(s.r[0] == x as u64); + assert(s.pc as int + 4 < 0x1_0000_0000_0000_0000); + super::bitmid::lemma_mid_exts(s, x); + assert(code_len(mid) == 4); + } + } + super::skel::lemma_shape(lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `i64.extend_i32_u` and the two `reinterpret`s refine (no code): the word is +/// the same. +pub proof fn lemma_case_same_word( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + h: nat, + u: lir::UnOp, + fuel: nat, +) + requires + u is I64ExtendI32U || u is F64ReinterpretI64 || u is I64ReinterpretF64, + op_ok(Op::Un(u), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(u), nl, h), Op::Un(u)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(u), nl, h), + run_seq(m, super::templates::tmpl(Op::Un(u), nl, h)), + ), +{ + super::steps::lemma_frame_agrees(); + assert(super::templates::tmpl(Op::Un(u), nl, h) == Seq::::empty()); + assert(run_seq(m, super::templates::tmpl(Op::Un(u), nl, h)) == Some(m)); + assert(a.stack.last() == a.stack[(h - 1) as int]); + let v = match (u, a.stack[(h - 1) as int]) { + (lir::UnOp::I64ExtendI32U, lir::Val::I32(x)) => lir::Val::I64(x as u64), + (lir::UnOp::F64ReinterpretI64, lir::Val::I64(x)) => lir::Val::F64(x), + (lir::UnOp::I64ReinterpretF64, lir::Val::F64(x)) => lir::Val::I64(x), + _ => lir::Val::I32(0), + }; + match (u, a.stack[(h - 1) as int]) { + (lir::UnOp::I64ExtendI32U, lir::Val::I32(x)) => {}, + (lir::UnOp::F64ReinterpretI64, lir::Val::I64(x)) => {}, + (lir::UnOp::I64ReinterpretF64, lir::Val::F64(x)) => {}, + _ => { + assert(false); + }, + } + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + crate::agree::lemma_agree_same_word(a, a2, nl, h, v); + super::rel::lemma_rel_agree(lay, a, a2, m, nl); + assert(preserved(m, m)); +} + +// ---------------------------------------------------------------------------- +// Every instruction of the slice +// ---------------------------------------------------------------------------- + +/// [`refines`] used at a state. +pub proof fn lemma_use_refines( + i: lir::Instr, + nl: nat, + h: nat, + code: Seq, + lay: lir::Layout, + a: lir::St, + m: isa::State, + fuel: nat, +) + requires + super::contract::refines(i, nl, h, code), + ensures + a.stack.len() == h && rel(lay, a, m, nl) && (m.pc as int) + code_len(code) + < 0x1_0000_0000_0000_0000 ==> match lir::eval(lay, i, a, fuel) { + lir::Outcome::Normal(a2) => super::contract::refines_post(lay, a2, m, nl, code, run_seq(m, code)), + _ => true, + }, +{ + super::memlemmas::lemma_p2_consts(); +} + +/// **Every instruction of the slice has a template that refines it.** +#[verifier::rlimit(1000)] +pub proof fn lemma_op_refines(op: Op, nl: nat, h: nat) + requires + op_ok(op, nl, h), + ensures + super::contract::refines(op@, nl, h, super::templates::tmpl(op, nl, h)), +{ + super::memlemmas::lemma_p2_consts(); + assert forall|lay: lir::Layout, a: lir::St, m: isa::State, fuel: nat| + #![trigger lir::eval(lay, op@, a, fuel), run_seq(m, super::templates::tmpl(op, nl, h))] + a.stack.len() == h && rel(lay, a, m, nl) && (m.pc as int) + code_len(super::templates::tmpl(op, nl, h)) + < 0x1_0000_0000_0000_0000 implies match lir::eval(lay, op@, a, fuel) { + lir::Outcome::Normal(a2) => super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(op, nl, h), + run_seq(m, super::templates::tmpl(op, nl, h)), + ), + _ => true, + } by { + match lir::eval(lay, op@, a, fuel) { + lir::Outcome::Normal(a2) => { + assert(case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(op, nl, h), op@, fuel)); + match op { + Op::Nop => lemma_case_drop_nop(lay, a, a2, m, nl, h, op, fuel), + Op::Drop => lemma_case_drop_nop(lay, a, a2, m, nl, h, op, fuel), + Op::Const(v) => lemma_case_const(lay, a, a2, m, nl, h, v, fuel), + Op::LocalGet(x) => lemma_case_local_get(lay, a, a2, m, nl, h, x, fuel), + Op::LocalSet(x) => lemma_case_local_set_tee(lay, a, a2, m, nl, h, op, x, fuel), + Op::LocalTee(x) => lemma_case_local_set_tee(lay, a, a2, m, nl, h, op, x, fuel), + Op::Select => lemma_case_select(lay, a, a2, m, nl, h, fuel), + Op::Bin(b) => match b { + lir::BinOp::I32(o) => lemma_case_bin_int(lay, a, a2, m, nl, h, b, o, isa::Width::W32, fuel), + lir::BinOp::I64(o) => lemma_case_bin_int(lay, a, a2, m, nl, h, b, o, isa::Width::W64, fuel), + lir::BinOp::I32Cmp(c) => lemma_case_cmp_int(lay, a, a2, m, nl, h, b, c, isa::Width::W32, fuel), + lir::BinOp::I64Cmp(c) => lemma_case_cmp_int(lay, a, a2, m, nl, h, b, c, isa::Width::W64, fuel), + lir::BinOp::F64(_) => { + assert(false); + }, + lir::BinOp::F64Cmp(_) => { + assert(false); + }, + }, + Op::Un(u) => match u { + lir::UnOp::I32Eqz => lemma_case_eqz(lay, a, a2, m, nl, h, u, isa::Width::W32, fuel), + lir::UnOp::I64Eqz => lemma_case_eqz(lay, a, a2, m, nl, h, u, isa::Width::W64, fuel), + lir::UnOp::F64Neg => lemma_case_f64_neg(lay, a, a2, m, nl, h, fuel), + lir::UnOp::F64Abs => lemma_case_f64_abs(lay, a, a2, m, nl, h, fuel), + lir::UnOp::I32WrapI64 => lemma_case_wrap(lay, a, a2, m, nl, h, fuel), + lir::UnOp::I64ExtendI32S => lemma_case_extend_s(lay, a, a2, m, nl, h, fuel), + lir::UnOp::I64ExtendI32U => lemma_case_same_word(lay, a, a2, m, nl, h, u, fuel), + lir::UnOp::F64ReinterpretI64 => lemma_case_same_word(lay, a, a2, m, nl, h, u, fuel), + lir::UnOp::I64ReinterpretF64 => lemma_case_same_word(lay, a, a2, m, nl, h, u, fuel), + lir::UnOp::F64Sqrt => { + assert(false); + }, + lir::UnOp::F64ConvertI32S => { + assert(false); + }, + lir::UnOp::F64ConvertI32U => { + assert(false); + }, + lir::UnOp::F64ConvertI64S => { + assert(false); + }, + lir::UnOp::F64ConvertI64U => { + assert(false); + }, + }, + } + }, + _ => {}, + } + } +} + +/// The stack height after a step that fell through is the one the static +/// analysis computes. +pub proof fn lemma_op_height( + op: Op, + nl: nat, + h: nat, + lay: lir::Layout, + a: lir::St, + a2: lir::St, + fuel: nat, +) + requires + op_ok(op, nl, h), + a.stack.len() == h, + lir::eval(lay, op@, a, fuel) == lir::Outcome::Normal(a2), + ensures + a2.stack.len() == crate::spec::height_after_op(op, h), +{ + assert(fuel > 0); + match op { + Op::Nop => {}, + Op::Drop => {}, + Op::Const(v) => {}, + Op::LocalGet(x) => {}, + Op::LocalSet(x) => {}, + Op::LocalTee(x) => {}, + Op::Select => { + match a.stack[(h - 1) as int] { + lir::Val::I32(c) => { + assert(a2.stack.len() == (h - 3) + 1); + }, + _ => { + assert(false); + }, + } + }, + Op::Bin(b) => { + match lir::bin_eval(b, a.stack[(h - 2) as int], a.stack[(h - 1) as int]) { + Some(v) => { + assert(a2.stack.len() == (h - 2) + 1); + }, + None => { + assert(false); + }, + } + }, + Op::Un(u) => { + match lir::un_eval(u, a.stack[(h - 1) as int]) { + Some(v) => { + assert(a2.stack.len() == (h - 1) + 1); + }, + None => { + assert(false); + }, + } + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/rel.rs b/crates/clazel-jit/src/a64/rel.rs new file mode 100644 index 000000000..02488ed72 --- /dev/null +++ b/crates/clazel-jit/src/a64/rel.rs @@ -0,0 +1,222 @@ +//! Facts about the state relation [`rel`]: it survives a change of scratch +//! registers, flags and `pc`; a load from a slot leaves it intact and puts the +//! slot's word in a register; and a store of the result word to slot `ts` +//! takes the relation to the next LIR state. The A64 counterpart of +//! [`crate::rel`]. The slot-agreement predicates are those of +//! [`crate::rel`] (they speak of LIR states only; [`super::steps::lemma_frame_agrees`] +//! shows the two frames have the same number of slots). + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A register the templates may use: a general register that is neither the +/// context register nor the link register (and not the zero register). +pub open spec fn scratch(r: u8) -> bool { + r < 31 && r != abi::ctx_reg() && r != abi::link_reg() +} + +/// A state with the same memory and context register, and registers and vector +/// registers of the right size, is related to what `m` is. +pub proof fn lemma_rel_regs(lay: lir::Layout, a: lir::St, m: isa::State, m3: isa::State, nl: nat) + requires + rel(lay, a, m, nl), + m3.mem == m.mem, + m3.r.len() == 31, + m3.vreg.len() == 32, + m3.r[abi::ctx_reg() as int] == m.r[abi::ctx_reg() as int], + ensures + rel(lay, a, m3, nl), +{ + super::steps::lemma_abi_consts(); + assert(ctx_of(m3) == ctx_of(m)); + assert(isa::wf(m3)); + assert(ctx_ok(m3)); +} + +/// A load from slot `j` into a scratch register `r` puts the slot's word there +/// and keeps the relation. +pub proof fn lemma_load_rel(lay: lir::Layout, a: lir::St, m: isa::State, r: u8, j: nat, nl: nat) + requires + rel(lay, a, m, nl), + scratch(r), + j < nl + a.stack.len(), + m.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(m, super::templates::load_slot(r, j)) is Some, + ({ + let m1 = isa::step(m, super::templates::load_slot(r, j)).unwrap(); + &&& m1 == isa::State { + r: m.r.update(r as int, lir::val_bits(slot_val(a, nl, j)) as u64), + pc: (m.pc + 4) as u64, + ..m + } + &&& rel(lay, a, m1, nl) + &&& m1.r[r as int] as nat == lir::val_bits(slot_val(a, nl, j)) + }), +{ + super::steps::lemma_abi_consts(); + super::memlemmas::lemma_p2_consts(); + let v = lir::val_bits(slot_val(a, nl, j)); + assert(slot_holds(m.mem, ctx_of(m), j, v)); + assert(v < 0x1_0000_0000_0000_0000) by { + match slot_val(a, nl, j) { + lir::Val::I32(x) => {}, + lir::Val::I64(x) => {}, + lir::Val::F64(x) => {}, + } + } + assert(j < max_slots()); + super::steps::lemma_step_load_slot(m, r, j, v); + let m1 = isa::step(m, super::templates::load_slot(r, j)).unwrap(); + assert(m1.r.len() == 31); + lemma_rel_regs(lay, a, m, m1, nl); + assert(m1.r[r as int] == v as u64); +} + +/// `slots_agree` in this module's terms: the frame of `a2` is what the frame of +/// `a` holds. +pub proof fn lemma_rel_agree(lay: lir::Layout, a: lir::St, a2: lir::St, m: isa::State, nl: nat) + requires + rel(lay, a, m, nl), + crate::rel::slots_agree(a, a2, nl), + ensures + rel(lay, a2, m, nl), +{ + super::steps::lemma_frame_agrees(); + assert forall|j: nat| j < nl + a2.stack.len() implies #[trigger] slot_holds( + m.mem, + ctx_of(m), + j, + lir::val_bits(slot_val(a2, nl, j)), + ) by { + assert(lir::val_bits(slot_val(a2, nl, j)) >= 0); + assert(j < nl + a.stack.len()); + assert(slot_holds(m.mem, ctx_of(m), j, lir::val_bits(slot_val(a, nl, j)))); + } +} + +/// The 8 bytes of a slot are in the exit area. +pub proof fn lemma_slot_in_exit_area(ctxb: int, j: nat, b: int) + requires + j < max_slots(), + ctxb + slot_disp(j) <= b < ctxb + slot_disp(j) + 8, + ensures + in_exit_area(ctxb, b), +{ + super::steps::lemma_abi_consts(); +} + +/// Two different slots do not overlap. +pub proof fn lemma_slots_disjoint(ctxb: int, j: nat, ts: nat, b: int) + requires + j != ts, + ctxb + slot_disp(j) <= b < ctxb + slot_disp(j) + 8, + ensures + b < ctxb + slot_disp(ts) || ctxb + slot_disp(ts) + 8 <= b, +{ + super::steps::lemma_abi_consts(); +} + +/// **The store finishes the template.** With the result word `w` in register +/// `rd`, storing it to slot `ts` takes the machine to a state related to the +/// next LIR state `a2`, `pc` four further, and nothing outside the exit area +/// changed since `m0` (the state the template started from). +#[verifier::rlimit(500)] +pub proof fn lemma_store_finish( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m0: isa::State, + m3: isa::State, + nl: nat, + ts: nat, + rd: u8, + w: nat, +) + requires + rel(lay, a, m3, nl), + rel(lay, a, m0, nl), + m3.mem == m0.mem, + m3.sp == m0.sp, + m3.r[abi::link_reg() as int] == m0.r[abi::link_reg() as int], + m3.r[abi::ctx_reg() as int] == m0.r[abi::ctx_reg() as int], + crate::rel::slots_agree_except(a, a2, nl, ts), + w == lir::val_bits(slot_val(a2, nl, ts)), + ts < max_slots(), + rd < 31, + m3.r[rd as int] as nat == w, + m3.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(m3, super::templates::store_slot(ts, rd)) is Some, + ({ + let m4 = isa::step(m3, super::templates::store_slot(ts, rd)).unwrap(); + &&& rel(lay, a2, m4, nl) + &&& m4.pc as int == m3.pc as int + 4 + &&& preserved(m0, m4) + }), +{ + super::steps::lemma_abi_consts(); + super::steps::lemma_frame_agrees(); + let ctxb = ctx_of(m3); + assert(ctx_of(m0) == ctxb); + super::steps::lemma_step_store_slot(m3, rd, ts); + let m4 = isa::step(m3, super::templates::store_slot(ts, rd)).unwrap(); + assert(m4.r == m3.r && m4.vreg == m3.vreg && m4.sp == m3.sp); + assert(isa::wf(m4)) by { + assert(m4.mem.dom() =~= m3.mem.dom()); + } + assert(ctx_of(m4) == ctxb); + assert(ctx_ok(m4)); + assert(exit_area_mapped(m4.mem, ctxb)); + // every slot of a2 + assert forall|j: nat| j < nl + a2.stack.len() implies #[trigger] slot_holds( + m4.mem, + ctxb, + j, + lir::val_bits(slot_val(a2, nl, j)), + ) by { + assert(lir::val_bits(slot_val(a2, nl, j)) >= 0); + if j == ts { + assert(m3.r[rd as int] as int == w as int); + } else { + assert(j < nl + a.stack.len()); + assert(slot_holds(m3.mem, ctxb, j, lir::val_bits(slot_val(a, nl, j)))); + assert(j < max_slots()); + super::steps::lemma_slot_addr(m3, j); + assert forall|i: int| 0 <= i < 8 implies (m3.mem.dom().contains( + #[trigger] (ctxb + slot_disp(j) + i), + ) == m4.mem.dom().contains(ctxb + slot_disp(j) + i)) && (m3.mem.dom().contains( + ctxb + slot_disp(j) + i, + ) ==> m3.mem[ctxb + slot_disp(j) + i] == m4.mem[ctxb + slot_disp(j) + i]) by { + lemma_slots_disjoint(ctxb, j, ts, ctxb + slot_disp(j) + i); + assert(m4.mem.dom().contains(ctxb + slot_disp(j) + i) == m3.mem.dom().contains( + ctxb + slot_disp(j) + i, + )); + } + super::memlemmas::lemma_mem_read_congr(m3.mem, m4.mem, ctxb + slot_disp(j), 8); + } + } + // memory outside the exit area is untouched + assert forall|b: int| #[trigger] m0.mem.dom().contains(b) && !in_exit_area(ctxb, b) implies m4.mem[b] + == m0.mem[b] by { + if b < ctxb + slot_disp(ts) || ctxb + slot_disp(ts) + 8 <= b { + } else { + lemma_slot_in_exit_area(ctxb, ts, b); + } + } + assert(mem_rel(lay, a2.mem, m4.mem)) by { + assert forall|b: nat| #[trigger] in_regions(lay, b) implies m4.mem.dom().contains(b as int) + && m4.mem[b as int] == lir::byte_at(a2.mem, b) by { + assert(!in_exit_area(ctxb, b as int)); + assert(m3.mem.dom().contains(b as int) && m3.mem[b as int] == lir::byte_at(a.mem, b)); + assert(m0.mem.dom().contains(b as int)); + } + } + assert(preserved_mem(m0, m4)); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/runseq.rs b/crates/clazel-jit/src/a64/runseq.rs new file mode 100644 index 000000000..a21e036f5 --- /dev/null +++ b/crates/clazel-jit/src/a64/runseq.rs @@ -0,0 +1,143 @@ +//! Running a list of instructions ([`run_seq`]): how it behaves on a list +//! built from pieces. The A64 counterpart of [`crate::runseq`]; with no +//! `create_undef` and fixed-size instructions, [`run1`]`..`[`run5`] are the +//! lemmas the middles chain, and [`code_len`] is just `4 * len`. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The length of the encodings of two lists is the sum of their lengths. +pub proof fn lemma_code_len_append(a: Seq, b: Seq) + ensures + code_len(a + b) == code_len(a) + code_len(b), +{ +} + +/// Running `a` then `b` is running `a`, then `b` from the state it reaches. +pub proof fn lemma_run_seq_append(s: isa::State, a: Seq, b: Seq) + ensures + run_seq(s, a + b) == match run_seq(s, a) { + Some(s1) => run_seq(s1, b), + None => None, + }, + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + match isa::step(s, a[0]) { + Some(s1) => { + lemma_run_seq_append(s1, a.skip(1), b); + }, + None => {}, + } + } +} + +/// Running a list that starts with `i` is a step by `i`, then the rest. +pub proof fn lemma_run_seq_cons(s: isa::State, i: isa::Instr, rest: Seq) + ensures + run_seq(s, seq![i] + rest) == match isa::step(s, i) { + Some(s1) => run_seq(s1, rest), + None => None, + }, +{ + let code = seq![i] + rest; + assert(code.len() > 0); + assert(code[0] == i); + assert(code.skip(1) =~= rest); +} + +/// Running one instruction is its step. +pub proof fn lemma_run_seq_one(s: isa::State, i: isa::Instr) + ensures + run_seq(s, seq![i]) == isa::step(s, i), +{ + let e = Seq::::empty(); + assert(seq![i] + e =~= seq![i]); + lemma_run_seq_cons(s, i, e); + match isa::step(s, i) { + Some(s1) => { + assert(run_seq(s1, e) == Some(s1)); + }, + None => {}, + } +} + +/// A list of one instruction whose step is `s1`. +pub proof fn run1(s: isa::State, a: isa::Instr, s1: isa::State) + requires + isa::step(s, a) == Some(s1), + ensures + run_seq(s, seq![a]) == Some(s1), +{ + lemma_run_seq_one(s, a); +} + +/// A list of two instructions whose steps are `s1` and `s2`. +pub proof fn run2(s: isa::State, a: isa::Instr, b: isa::Instr, s1: isa::State, s2: isa::State) + requires + isa::step(s, a) == Some(s1), + isa::step(s1, b) == Some(s2), + ensures + run_seq(s, seq![a, b]) == Some(s2), +{ + run1(s1, b, s2); + assert(seq![a, b] =~= seq![a] + seq![b]); + lemma_run_seq_cons(s, a, seq![b]); +} + +/// A list of three instructions whose steps are `s1`, `s2`, `s3`. +pub proof fn run3( + s: isa::State, + a: isa::Instr, + b: isa::Instr, + c: isa::Instr, + s1: isa::State, + s2: isa::State, + s3: isa::State, +) + requires + isa::step(s, a) == Some(s1), + isa::step(s1, b) == Some(s2), + isa::step(s2, c) == Some(s3), + ensures + run_seq(s, seq![a, b, c]) == Some(s3), +{ + run2(s1, b, c, s2, s3); + assert(seq![a, b, c] =~= seq![a] + seq![b, c]); + lemma_run_seq_cons(s, a, seq![b, c]); +} + +/// A list of four instructions whose steps are `s1` .. `s4`. +pub proof fn run4( + s: isa::State, + a: isa::Instr, + b: isa::Instr, + c: isa::Instr, + d: isa::Instr, + s1: isa::State, + s2: isa::State, + s3: isa::State, + s4: isa::State, +) + requires + isa::step(s, a) == Some(s1), + isa::step(s1, b) == Some(s2), + isa::step(s2, c) == Some(s3), + isa::step(s3, d) == Some(s4), + ensures + run_seq(s, seq![a, b, c, d]) == Some(s4), +{ + run3(s1, b, c, d, s2, s3, s4); + assert(seq![a, b, c, d] =~= seq![a] + seq![b, c, d]); + lemma_run_seq_cons(s, a, seq![b, c, d]); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/skel.rs b/crates/clazel-jit/src/a64/skel.rs new file mode 100644 index 000000000..54be9f1b1 --- /dev/null +++ b/crates/clazel-jit/src/a64/skel.rs @@ -0,0 +1,186 @@ +//! The skeleton every storing template shares: *loads, a register-only middle, +//! one store*. [`lemma_loads`] runs the loads, and [`lemma_shape`] puts the +//! three parts together: given a middle that leaves the result word in `x0` +//! (the hypothesis, proven per template in [`super::mid`] and +//! [`super::bitmid`]), the whole template takes a state related to `a` to a +//! state related to the next LIR state `a2`. The A64 counterpart of +//! [`crate::skel`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The loads in `ls` read distinct scratch registers from existing slots. +pub open spec fn loads_ok(ls: Seq<(u8, nat)>, nl: nat, n: nat) -> bool { + (forall|k: int| + 0 <= k < ls.len() ==> super::rel::scratch((#[trigger] ls[k]).0) && ls[k].1 < nl + n) && (forall| + k1: int, + k2: int, + | + 0 <= k1 < k2 < ls.len() ==> (#[trigger] ls[k1]).0 != (#[trigger] ls[k2]).0) +} + +/// What the middle of a template is started from: a well-formed state whose +/// registers hold the words the loads `ls` would have put there, with room +/// for the middle before the address space ends. +pub open spec fn mid_pre( + a: lir::St, + nl: nat, + ls: Seq<(u8, nat)>, + mid: Seq, + s: isa::State, +) -> bool { + isa::wf(s) && (forall|k: int| + 0 <= k < ls.len() ==> (#[trigger] s.r[ls[k].0 as int]) as nat == lir::val_bits( + slot_val(a, nl, ls[k].1), + )) && (s.pc as int) + code_len(mid) < 0x1_0000_0000_0000_0000 +} + +/// What the middle must do from there: not fault, and leave `w` in `x0` +/// changing only scratch registers, flags and `pc` ([`super::mid::mid_post`]). +pub open spec fn mid_goal(mid: Seq, w: nat, s: isa::State) -> bool { + run_seq(s, mid) is Some && super::mid::mid_post( + s, + run_seq(s, mid).unwrap(), + x0(), + w, + code_len(mid), + ) +} + +/// The middle satisfies [`mid_goal`] from every state satisfying [`mid_pre`]. +pub open spec fn mid_prop( + a: lir::St, + nl: nat, + ls: Seq<(u8, nat)>, + mid: Seq, + w: nat, +) -> bool { + forall|s: isa::State| + #![trigger mid_goal(mid, w, s)] + mid_pre(a, nl, ls, mid, s) ==> mid_goal(mid, w, s) +} + +/// The loads `ls` run from a related state: no fault, each register holds its +/// slot's word, the relation still holds, and nothing but those registers and +/// `pc` changed. +#[verifier::rlimit(1000)] +pub proof fn lemma_loads(lay: lir::Layout, a: lir::St, m: isa::State, ls: Seq<(u8, nat)>, nl: nat) + requires + rel(lay, a, m, nl), + loads_ok(ls, nl, a.stack.len()), + (m.pc as int) + code_len(super::templates::loads_seq(ls)) < 0x1_0000_0000_0000_0000, + ensures + run_seq(m, super::templates::loads_seq(ls)) is Some, + ({ + let m1 = run_seq(m, super::templates::loads_seq(ls)).unwrap(); + &&& rel(lay, a, m1, nl) + &&& m1.pc as int == m.pc as int + code_len(super::templates::loads_seq(ls)) + &&& m1.mem == m.mem + &&& m1.r.len() == 31 + &&& m1.vreg == m.vreg + &&& m1.r[abi::ctx_reg() as int] == m.r[abi::ctx_reg() as int] + &&& m1.r[abi::link_reg() as int] == m.r[abi::link_reg() as int] + &&& m1.sp == m.sp + &&& forall|k: int| + 0 <= k < ls.len() ==> (#[trigger] m1.r[ls[k].0 as int]) as nat == lir::val_bits( + slot_val(a, nl, ls[k].1), + ) + }), + decreases ls.len(), +{ + super::steps::lemma_abi_consts(); + if ls.len() == 0 { + assert(super::templates::loads_seq(ls) =~= seq![]); + } else { + let pre = ls.drop_last(); + let last = ls.last(); + let a_code = super::templates::loads_seq(pre); + let b_code = seq![super::templates::load_slot(last.0, last.1)]; + assert(super::templates::loads_seq(ls) == a_code + b_code); + assert(loads_ok(pre, nl, a.stack.len())) by { + assert forall|k: int| 0 <= k < pre.len() implies super::rel::scratch((#[trigger] pre[k]).0) + && pre[k].1 < nl + a.stack.len() by { + assert(pre[k] == ls[k]); + } + assert forall|k1: int, k2: int| + 0 <= k1 < k2 < pre.len() implies (#[trigger] pre[k1]).0 != (#[trigger] pre[k2]).0 by { + assert(pre[k1] == ls[k1] && pre[k2] == ls[k2]); + } + } + lemma_loads(lay, a, m, pre, nl); + let m1 = run_seq(m, a_code).unwrap(); + super::runseq::lemma_run_seq_append(m, a_code, b_code); + assert(super::rel::scratch(last.0) && last.1 < nl + a.stack.len()); + super::rel::lemma_load_rel(lay, a, m1, last.0, last.1, nl); + super::runseq::lemma_run_seq_one(m1, super::templates::load_slot(last.0, last.1)); + let m2 = isa::step(m1, super::templates::load_slot(last.0, last.1)).unwrap(); + assert(m2.r.len() == 31); + assert(m2.vreg == m1.vreg); + assert forall|k: int| 0 <= k < ls.len() implies (#[trigger] m2.r[ls[k].0 as int]) as nat + == lir::val_bits(slot_val(a, nl, ls[k].1)) by { + if k < ls.len() - 1 { + assert(pre[k] == ls[k]); + assert(ls[k].0 != last.0); + } + } + } +} + +/// **A whole template**: loads, middle, store. From a related state, the code +/// does not fault and ends in a state related to the next LIR state, `pc` +/// after the code, only the exit area changed. +#[verifier::rlimit(500)] +pub proof fn lemma_shape( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: isa::State, + nl: nat, + ls: Seq<(u8, nat)>, + mid: Seq, + ts: nat, + w: nat, +) + requires + rel(lay, a, m, nl), + loads_ok(ls, nl, a.stack.len()), + (m.pc as int) + code_len(super::templates::shape(ls, mid, ts)) < 0x1_0000_0000_0000_0000, + ts < max_slots(), + crate::rel::slots_agree_except(a, a2, nl, ts), + w == lir::val_bits(slot_val(a2, nl, ts)), + mid_prop(a, nl, ls, mid, w), + ensures + run_seq(m, super::templates::shape(ls, mid, ts)) is Some, + ({ + let m4 = run_seq(m, super::templates::shape(ls, mid, ts)).unwrap(); + &&& rel(lay, a2, m4, nl) + &&& m4.pc as int == m.pc as int + code_len(super::templates::shape(ls, mid, ts)) + &&& preserved(m, m4) + }), +{ + super::steps::lemma_abi_consts(); + super::steps::lemma_frame_agrees(); + let ld = super::templates::loads_seq(ls); + let st = seq![super::templates::store_slot(ts, x0())]; + assert(super::templates::shape(ls, mid, ts) == (ld + mid) + st); + lemma_loads(lay, a, m, ls, nl); + let m1 = run_seq(m, ld).unwrap(); + // the middle + assert(isa::wf(m1)); + assert(mid_pre(a, nl, ls, mid, m1)); + assert(mid_goal(mid, w, m1)); + let m3 = run_seq(m1, mid).unwrap(); + super::runseq::lemma_run_seq_append(m, ld, mid); + super::runseq::lemma_run_seq_append(m, ld + mid, st); + super::rel::lemma_rel_regs(lay, a, m1, m3, nl); + // the store + assert(m3.pc as int + 4 < 0x1_0000_0000_0000_0000); + super::rel::lemma_store_finish(lay, a, a2, m, m3, nl, ts, x0(), w); + super::runseq::lemma_run_seq_one(m3, super::templates::store_slot(ts, x0())); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/spec.rs b/crates/clazel-jit/src/a64/spec.rs new file mode 100644 index 000000000..7e4235613 --- /dev/null +++ b/crates/clazel-jit/src/a64/spec.rs @@ -0,0 +1,327 @@ +//! What the LIR → A64 back end is *about* (jit.md item J.9): the state +//! relation between a LIR state and an A64 machine state, the shape of the +//! frame, and the small definitions the refinement statements of +//! [`crate::a64::contract`] are written in. Nothing here runs and nothing is +//! proven. It is the A64 counterpart of [`crate::spec`] and follows its design; +//! this header says where the two differ. Read that header for the choices +//! the two share (the frame is a slot per local and per operand-stack +//! element, a value is a 64-bit word, no register is live between two LIR +//! instructions, memory is related and not yet touched). +//! +//! # Source +//! +//! There is no standard to transcribe. The sources are: +//! +//! - **jit.md, "J.9 Back ends"**: "No register allocation: every LIR local is +//! a frame slot, and each LIR instruction is a fixed template over scratch +//! registers, so each template's proof is local." +//! - **`clazel-lir`'s `spec`** ([`lir::eval`]): the meaning of the source. +//! - **`clazel-a64`'s `spec`** ([`isa::step`]): the meaning of the target. +//! - **`clazel-codecheck`'s `a64::spec::abi`**: the context register `x28`, +//! the reserved registers (`x28`, `x30`, `SP`), the region table, which +//! context bytes the code may write, and the exit sequence (`movz`; `str +//! [x28, #256]`; `ret`). The templates follow it so that what they emit is +//! accepted (the test in `tests/validator_a64.rs`; the validator's own spec +//! is not re-stated here). +//! +//! # Where A64 differs from the x86-64 back end +//! +//! 1. **The frame** is the same bytes: the context's exit area after the exit +//! code, slot `j` at `[x28 + 264 + 8 j]`, `j < 31`. A64's `LDR`/`STR` +//! (unsigned immediate) of an X register takes the whole offset as `imm12 * +//! 8`, so slot `j` is `imm12 = 33 + j` ([`slot_imm`]); the validator accepts +//! a store only at an offset of 256 or more and an access that ends by 512 +//! ([`abi::ctx_size`]), which is exactly the frame ([`max_slots`]). The two +//! ABIs agree on these numbers; the proof module of the next commit shows it +//! against [`crate::spec`]'s. +//! 2. **The scratch registers** are `x0`, `x1` and `x2` ([`x0`], [`x1`], +//! [`x2`]), and so are the temporaries of the templates' middles: no +//! register is live between two LIR instructions, so the relation does not +//! mention them. `x28` (the context), `x30` (the link register) and `SP` are +//! never written ([`preserved`]). The flags `NZCV` are not mentioned either. +//! 3. **There is no undefined-value source.** `clazel-a64`'s [`isa::step`] takes +//! none (its header explains why), so the statements have no `create_undef` +//! parameter. +//! 4. **Instructions are four bytes.** [`code_len`] is `4 * code.len()`, and +//! `pc` advances by 4 per instruction. The end of the code must not wrap the +//! 64-bit address space (a `requires` of each refinement; x86-64's is "the +//! end address is canonical"). That the bytes at `pc` are the canonical +//! encodings ([`isa::fetch_ok`]) is not part of the statement, as for +//! x86-64: [`preserved`] says the code writes only the exit area, so code +//! placed outside it stays what it was, and the bytes are the emitter's +//! contract. +//! 5. **User-space addresses.** The context lies below `2^47` ([`ctx_ok`]), so +//! every address a template forms is far below `2^56`, the bound +//! [`isa::wf`] puts on mapped addresses (no top-byte-ignore subtlety, no +//! wrap-around). +//! 6. **The exit code is 16 bits.** The validator's exit sequence is `movz +//! wt, #code` with `hw = 0`, so the code is a `u16` ([`exit_seq`]); +//! x86-64's `mov eax, imm32` allows 32. +//! 7. **The exit sequence returns through `x30`** and reads no memory: `ret` +//! sets `pc` to `x30` ([`isa::step`]), so there is no stack-pointer +//! precondition ([`crate::spec::stack_ok`]) and `SP` is untouched. +//! +//! # What is not here +//! +//! The memory instructions, structured control flow, `exit` with operands, +//! `f64` arithmetic, comparisons and conversions (LIR's `f64` operators are +//! uninterpreted functions), and rotates (`clazel-a64`'s subset has no `RORV`). + +/// The source subset's instruction type, shared with the x86-64 back end: +/// [`Op`] and its stack effect ([`crate::spec`]) are target-independent. +pub use crate::spec::Op; +/// `clazel-a64`'s semantics (the A64 subset), re-exported so that this crate's +/// modules read one namespace. +pub use clazel_a64::spec as isa; +/// The validator's A64 ABI, re-exported likewise. +pub use clazel_codecheck::a64::spec::abi; +/// LIR's semantics, re-exported likewise. +pub use clazel_lir::spec as lir; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// ============================================================================ +// Section: the source subset +// ============================================================================ + +/// The program as one LIR instruction ([`crate::spec::prog_instr`]). +#[verifier::inline] +pub open spec fn prog_instr(ops: Seq) -> lir::Instr { + crate::spec::prog_instr(ops) +} + +/// The stack height after the program `ops`, started at height `h` +/// ([`crate::spec::height_after`]). +#[verifier::inline] +pub open spec fn height_after(ops: Seq, h: nat) -> nat { + crate::spec::height_after(ops, h) +} + +/// The value in slot `j` of the LIR state `a` ([`crate::spec::slot_val`]). +#[verifier::inline] +pub open spec fn slot_val(a: lir::St, nl: nat, j: nat) -> lir::Val { + crate::spec::slot_val(a, nl, j) +} + +/// `a` is an address inside some region of `lay` ([`crate::spec::in_regions`]). +#[verifier::inline] +pub open spec fn in_regions(lay: lir::Layout, a: nat) -> bool { + crate::spec::in_regions(lay, a) +} + +/// LIR's memory and the machine's agree on every byte of every region +/// ([`crate::spec::mem_rel`]). +#[verifier::inline] +pub open spec fn mem_rel(lay: lir::Layout, lm: lir::Mem, mm: Map) -> bool { + crate::spec::mem_rel(lay, lm, mm) +} + +/// The instruction has a template, and is well formed at a stack of height +/// `h` with `nl` locals: the frame holds the stack as it is (`nl + h` slots) +/// and as it will be, its operands exist, its local index is in range, and so +/// everything it reads or writes is one of the 31 frame slots. As +/// [`crate::spec::op_ok`], with this module's [`max_slots`]. +pub open spec fn op_ok(op: Op, nl: nat, h: nat) -> bool { + nl + h <= max_slots() && crate::spec::pops(op) <= h && nl + crate::spec::height_after_op(op, h) + <= max_slots() && match op { + Op::LocalGet(x) => x < nl, + Op::LocalSet(x) => x < nl, + Op::LocalTee(x) => x < nl, + Op::Bin(b) => crate::spec::bin_supported(b), + Op::Un(u) => crate::spec::un_supported(u), + _ => true, + } +} + +/// Every instruction of `ops` has a template at its place in the program, for +/// `nl` locals and a stack that starts at height `h`, and the starting frame +/// fits. +pub open spec fn prog_ok(ops: Seq, nl: nat, h: nat) -> bool + decreases ops.len(), +{ + if ops.len() == 0 { + nl + h <= max_slots() + } else { + prog_ok(ops.drop_last(), nl, h) && op_ok(ops.last(), nl, height_after(ops.drop_last(), h)) + } +} + +// ============================================================================ +// Section: the frame +// ============================================================================ + +/// `x0`, the first scratch register. +pub open spec fn x0() -> u8 { + 0 +} + +/// `x1`, the second scratch register. +pub open spec fn x1() -> u8 { + 1 +} + +/// `x2`, the third scratch register. +pub open spec fn x2() -> u8 { + 2 +} + +/// The offset from the context register of frame slot 0: the first word +/// after the exit code. +pub open spec fn frame_off() -> int { + abi::exit_code_off() + 8 +} + +/// The number of frame slots: the words of the context after the exit code. +pub open spec fn max_slots() -> nat { + ((abi::ctx_size() - frame_off()) / 8) as nat +} + +/// The displacement of frame slot `j` from the context register. +pub open spec fn slot_disp(j: nat) -> int { + frame_off() + 8 * j +} + +/// The `imm12` of the `LDR`/`STR` (unsigned immediate, X register) of frame +/// slot `j`: the displacement divided by the access size. +pub open spec fn slot_imm(j: nat) -> u16 { + (slot_disp(j) / 8) as u16 +} + +/// The value of the context register in `m`, as an address. +pub open spec fn ctx_of(m: isa::State) -> int { + m.r[abi::ctx_reg() as int] as int +} + +/// The context lies in user space: the 512 bytes from the context register +/// are all below `2^47` (modelling choice 5). +pub open spec fn ctx_ok(m: isa::State) -> bool { + m.r.len() == 31 && 0 <= ctx_of(m) && ctx_of(m) + abi::ctx_size() <= isa::p2(47) +} + +/// `a` is a byte of the context's exit area (what [`abi`] lets code write). +pub open spec fn in_exit_area(ctxb: int, a: int) -> bool { + ctxb + abi::exit_code_off() <= a && a < ctxb + abi::ctx_size() +} + +/// Every byte of the exit area is mapped. +pub open spec fn exit_area_mapped(mm: Map, ctxb: int) -> bool { + forall|a: int| #[trigger] in_exit_area(ctxb, a) ==> mm.dom().contains(a) +} + +/// The 8 bytes at the address of slot `j` read as the word `v`. +pub open spec fn slot_holds(mm: Map, ctxb: int, j: nat, v: nat) -> bool { + isa::mem_read(mm, ctxb + slot_disp(j), 8) == Some(v as int) +} + +// ============================================================================ +// Section: the state relation +// ============================================================================ + +/// No region overlaps the context's exit area, which holds the frame: the +/// frame is the only machine memory the templates write, and it is nobody's +/// region. +pub open spec fn regions_clear_of_exit_area(lay: lir::Layout, ctxb: int) -> bool { + forall|a: nat| #[trigger] in_regions(lay, a) ==> !in_exit_area(ctxb, a as int) +} + +/// **The state relation.** The LIR state `a`, with `nl` locals, is +/// represented by the machine state `m`: +/// +/// 1. `m` is a well-formed machine state ([`isa::wf`]), with the context in +/// user space ([`ctx_ok`]) and the layout well formed and clear of the +/// exit area; +/// 2. the frame fits: `a` has `nl` locals and `nl + height` is at most 31; +/// 3. the exit area is mapped, so a store to a slot does not fault; +/// 4. every local, and every operand-stack element, is in its slot, as the +/// 64-bit word [`lir::val_bits`] of its value; +/// 5. memory is related ([`mem_rel`]). +/// +/// The scratch registers, the flags and `pc` are not constrained; the +/// refinement statements say where `pc` is. +pub open spec fn rel(lay: lir::Layout, a: lir::St, m: isa::State, nl: nat) -> bool { + isa::wf(m) && ctx_ok(m) && lir::layout_wf(lay) && regions_clear_of_exit_area(lay, ctx_of(m)) + && a.locals.len() == nl && nl + a.stack.len() <= max_slots() && exit_area_mapped( + m.mem, + ctx_of(m), + ) && (forall|j: nat| + j < nl + a.stack.len() ==> #[trigger] slot_holds( + m.mem, + ctx_of(m), + j, + lir::val_bits(slot_val(a, nl, j)), + )) && mem_rel(lay, a.mem, m.mem) +} + +/// What a template leaves alone in memory: the context register keeps its +/// value, the set of mapped addresses is the same, and every byte outside the +/// exit area keeps its value. So the code (which lives outside the exit +/// area) and every region are as they were. +pub open spec fn preserved_mem(m: isa::State, m2: isa::State) -> bool { + m.r.len() == 31 && m2.r.len() == 31 && m2.r[abi::ctx_reg() as int] == m.r[abi::ctx_reg() as int] + && m2.mem.dom() == m.mem.dom() && (forall|a: int| + #[trigger] m.mem.dom().contains(a) && !in_exit_area(ctx_of(m), a) ==> m2.mem[a] + == m.mem[a]) +} + +/// [`preserved_mem`], and the link register and the stack pointer keep their +/// values too: the validator forbids writing them, and the exit sequence's +/// `ret` returns through the link register. +pub open spec fn preserved(m: isa::State, m2: isa::State) -> bool { + preserved_mem(m, m2) && m2.r[abi::link_reg() as int] == m.r[abi::link_reg() as int] && m2.sp + == m.sp +} + +// ============================================================================ +// Section: running code +// ============================================================================ + +/// Run the instructions of `code` one after the other from `s`, each by +/// [`isa::step`], stopping at a fault (`None`). `pc` advances by 4 per +/// instruction (and, for a branch, goes to its target; the straight-line +/// templates have none). +pub open spec fn run_seq(s: isa::State, code: Seq) -> Option + decreases code.len(), +{ + if code.len() == 0 { + Some(s) + } else { + match isa::step(s, code[0]) { + Some(s1) => run_seq(s1, code.skip(1)), + None => None, + } + } +} + +/// The exit sequence for `c`, as the validator recognizes it +/// (`clazel_codecheck::a64::spec::exit_unit_at`): `movz w0, #c`, `str x0, +/// [x28, #256]`, `ret`. It is LIR's `exit` with no operands (a function that +/// falls off its end exits with code `c`). +pub open spec fn exit_seq(c: u16) -> Seq { + seq![ + isa::Instr::MoveWide { + sf: isa::Width::W32, + opcode: isa::MoveWideOp::Z, + d: x0(), + imm16: c, + hw: 0, + }, + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: false, + t: x0(), + n: abi::ctx_reg(), + imm12: (abi::exit_code_off() / 8) as u16, + }, + isa::Instr::Ret, + ] +} + +/// The length in bytes of the canonical encodings of `code`: 4 per +/// instruction. +pub open spec fn code_len(code: Seq) -> nat { + 4 * code.len() +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/steps.rs b/crates/clazel-jit/src/a64/steps.rs new file mode 100644 index 000000000..5b043c6af --- /dev/null +++ b/crates/clazel-jit/src/a64/steps.rs @@ -0,0 +1,169 @@ +//! What one [`isa::step`] does for the two instruction forms every template +//! has: a load from, and a store to, a frame slot. Each lemma computes the +//! state `step` returns, in terms of the state it is given, so a template's +//! proof is a chain of them. The A64 counterpart of [`crate::steps`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The ABI constants the lemmas need, evaluated. +pub proof fn lemma_abi_consts() + ensures + abi::ctx_reg() == 28, + abi::link_reg() == 30, + abi::sp_reg() == 31, + abi::exit_code_off() == 256, + abi::ctx_size() == 512, + frame_off() == 264, + max_slots() == 31, + x0() == 0, + x1() == 1, + x2() == 2, +{ +} + +/// The A64 and the x86-64 ABIs put the frame in the same place, so +/// [`crate::spec`]'s frame lemmas and `agree` lemmas (which are stated with its +/// `max_slots`) apply here. +pub proof fn lemma_frame_agrees() + ensures + max_slots() == crate::spec::max_slots(), + frame_off() == crate::spec::frame_off(), + forall|j: nat| slot_disp(j) == crate::spec::slot_disp(j), + abi::exit_code_off() == crate::spec::abi::exit_code_off(), + abi::ctx_size() == crate::spec::abi::ctx_size(), +{ +} + +/// The `imm12` of slot `j`'s `ldr`/`str` is `33 + j`, and times 8 it is the +/// slot's displacement. +pub proof fn lemma_slot_imm(j: nat) + requires + j < max_slots(), + ensures + slot_imm(j) as int == 33 + j, + slot_imm(j) as int * 8 == slot_disp(j), + slot_imm(j) < 4096, +{ +} + +/// The address of frame slot `j`: the context plus the slot's displacement, no +/// wrap-around. +pub proof fn lemma_slot_addr(m: isa::State, j: nat) + requires + ctx_ok(m), + j < max_slots(), + ensures + 0 <= ctx_of(m) + slot_disp(j), + ctx_of(m) + slot_disp(j) + 8 <= ctx_of(m) + abi::ctx_size(), + ctx_of(m) + slot_disp(j) + 8 < 0x1_0000_0000_0000_0000, + isa::trunc(64, ctx_of(m) + slot_disp(j)) == ctx_of(m) + slot_disp(j), + isa::trunc(64, ctx_of(m) + isa::trunc(64, slot_imm(j) as int * 8)) == ctx_of(m) + slot_disp(j), +{ + super::memlemmas::lemma_p2_consts(); + lemma_abi_consts(); + lemma_slot_imm(j); + super::memlemmas::lemma_trunc64(ctx_of(m) + slot_disp(j)); + super::memlemmas::lemma_trunc64(slot_imm(j) as int * 8); + super::memlemmas::lemma_trunc64(ctx_of(m) + slot_disp(j)); +} + +/// `x_read(s, n, 64)` of a register is its value. +pub proof fn lemma_x_read64(s: isa::State, n: u8) + requires + s.r.len() == 31, + n < 31, + ensures + isa::x_read(s, n, 64) == s.r[n as int] as int, +{ + super::memlemmas::lemma_p2_consts(); + super::memlemmas::lemma_trunc64(s.r[n as int] as int); +} + +/// `ldr xr, [x28, #(264 + 8 j)]` loads the word in slot `j` into `xr`, advances +/// `pc` by 4, and changes nothing else. +pub proof fn lemma_step_load_slot(m: isa::State, r: u8, j: nat, v: nat) + requires + isa::wf(m), + ctx_ok(m), + r < 31, + j < max_slots(), + slot_holds(m.mem, ctx_of(m), j, v), + v < 0x1_0000_0000_0000_0000, + m.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(m, super::templates::load_slot(r, j)) == Some( + isa::State { r: m.r.update(r as int, v as u64), pc: (m.pc + 4) as u64, ..m }, + ), +{ + super::memlemmas::lemma_p2_consts(); + lemma_abi_consts(); + lemma_slot_imm(j); + lemma_slot_addr(m, j); + lemma_x_read64(m, 28); + super::memlemmas::lemma_trunc64(v as int); + super::memlemmas::lemma_trunc64(m.pc as int + 4); + let i = super::templates::load_slot(r, j); + assert(isa::x_read(m, 28, 64) == ctx_of(m)); + assert(isa::mem_read(m.mem, ctx_of(m) + slot_disp(j), 8) == Some(v as int)); + assert(isa::execute(m, i) == Some(isa::x_set(m, r, 64, v as int))); + assert(isa::x_set(m, r, 64, v as int) == isa::State { r: m.r.update(r as int, v as u64), ..m }); +} + +/// `str xr, [x28, #(264 + 8 j)]` writes the word in `xr` to slot `j` (the exit +/// area is mapped, so it does not fault), advances `pc` by 4, and changes +/// nothing but those 8 bytes. +pub proof fn lemma_step_store_slot(m: isa::State, src: u8, j: nat) + requires + isa::wf(m), + ctx_ok(m), + src < 31, + j < max_slots(), + exit_area_mapped(m.mem, ctx_of(m)), + m.pc as int + 4 < 0x1_0000_0000_0000_0000, + ensures + isa::step(m, super::templates::store_slot(j, src)) is Some, + ({ + let m1 = isa::step(m, super::templates::store_slot(j, src)).unwrap(); + &&& m1 == isa::State { mem: m1.mem, pc: (m.pc + 4) as u64, ..m } + &&& isa::mem_read(m1.mem, ctx_of(m) + slot_disp(j), 8) == Some(m.r[src as int] as int) + &&& m1.mem.dom() =~= m.mem.dom() + &&& forall|b: int| + #![trigger m1.mem.dom().contains(b)] + m.mem.dom().contains(b) && (b < ctx_of(m) + slot_disp(j) || ctx_of(m) + slot_disp(j) + + 8 <= b) ==> m1.mem[b] == m.mem[b] + }), +{ + super::memlemmas::lemma_p2_consts(); + lemma_abi_consts(); + lemma_slot_imm(j); + lemma_slot_addr(m, j); + lemma_x_read64(m, 28); + lemma_x_read64(m, src); + super::memlemmas::lemma_trunc64(m.pc as int + 4); + let i = super::templates::store_slot(j, src); + let ctxb = ctx_of(m); + let addr = ctxb + slot_disp(j); + let w = m.r[src as int] as int; + assert(isa::x_read(m, 28, 64) == ctxb); + assert(isa::x_read(m, src, 64) == w); + assert forall|k: int| 0 <= k < 8 implies m.mem.dom().contains(#[trigger] (addr + k)) by { + assert(in_exit_area(ctxb, addr + k)); + } + super::memlemmas::lemma_mem_set_some(m.mem, addr, 8, w); + let m2 = isa::mem_set(m.mem, addr, 8, w).unwrap(); + super::memlemmas::lemma_mem_set_frame(m.mem, addr, 8, w, m2); + assert(0 <= w < vstd::arithmetic::power2::pow2(8 * 8) as int) by { + assert(vstd::arithmetic::power2::pow2(64) == 0x1_0000_0000_0000_0000) by { + vstd::arithmetic::power2::lemma2_to64_rest(); + } + } + super::memlemmas::lemma_read_after_write_same(m.mem, addr, 8, w, m2); + assert(isa::execute(m, i) == Some(isa::State { mem: m2, ..m })); +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/templates.rs b/crates/clazel-jit/src/a64/templates.rs new file mode 100644 index 000000000..ed797adb7 --- /dev/null +++ b/crates/clazel-jit/src/a64/templates.rs @@ -0,0 +1,463 @@ +//! The A64 templates (jit.md J.9): for each LIR instruction of the first +//! slice, at a known stack height, a fixed list of A64 instructions over the +//! scratch registers `x0`, `x1` and `x2`, reading and writing frame slots. +//! These are spec functions: the executable emitter (`emit`) is proven equal +//! to them, and the refinement proofs (`proofs`) are about them. They are +//! [`crate::templates`]'s templates for the A64 instruction set. +//! +//! Every template has the shape *load operands from slots into `x0`, `x1`, +//! `x2`, compute in registers, store `x0` to a slot*. A64 is a three-operand +//! machine with no memory operands, so the middles are shorter than x86-64's: +//! one instruction for each integer operator, two for a comparison (`cmp`, +//! `cset`), and a constant is a `movz` and up to three `movk`. Neither the +//! comparison nor `select` needs a flag to be set beforehand: the flags are +//! written by the instruction that precedes the one that reads them. +//! +//! No template reads memory other than the frame, writes anything but the +//! frame, `x0`, `x1`, `x2` and the flags, or touches `x28`, `x30` or `SP`. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `ldr xt, [x28, #(264 + 8 j)]`: load frame slot `j` (64 bits) into `dst`. +pub open spec fn load_slot(dst: u8, j: nat) -> isa::Instr { + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: true, + t: dst, + n: abi::ctx_reg(), + imm12: slot_imm(j), + } +} + +/// `str xt, [x28, #(264 + 8 j)]`: store `src` (64 bits) to frame slot `j`. +pub open spec fn store_slot(j: nat, src: u8) -> isa::Instr { + isa::Instr::LoadStoreImm { + sf: isa::Width::W64, + load: false, + t: src, + n: abi::ctx_reg(), + imm12: slot_imm(j), + } +} + +/// The `cond` field of the `csinc` that sets a register to 1 when the +/// comparison holds and to 0 otherwise, read after `cmp lhs, rhs`. `cset +/// wd, cc` is `csinc wd, wzr, wzr, invert(cc)`, so this is the *inverse* of +/// the condition that is true when the comparison holds: `ne` (1) for `eq`, +/// `eq` (0) for `ne`, `ge` (10) for signed less, `hs` (2) for below, `le` +/// (13) for signed greater, `ls` (9) for above, `gt` (12) for signed less or +/// equal, `hi` (8) for below or equal, `lt` (11) for signed greater or equal, +/// `lo` (3) for above or equal. +pub open spec fn cset_cond(c: lir::ICmp) -> u8 { + match c { + lir::ICmp::Eq => 1, + lir::ICmp::Ne => 0, + lir::ICmp::LtS => 10, + lir::ICmp::LtU => 2, + lir::ICmp::GtS => 13, + lir::ICmp::GtU => 9, + lir::ICmp::LeS => 12, + lir::ICmp::LeU => 8, + lir::ICmp::GeS => 11, + lir::ICmp::GeU => 3, + } +} + +/// The A64 logical operation for an integer binary operator with a +/// one-instruction register-register form (`and`, `orr`, `eor`). +pub open spec fn logic_of(o: lir::IBin) -> Option { + match o { + lir::IBin::And => Some(isa::LogicalOpc::And), + lir::IBin::Or => Some(isa::LogicalOpc::Orr), + lir::IBin::Xor => Some(isa::LogicalOpc::Eor), + _ => None, + } +} + +/// The A64 shift for an integer shift operator (`lslv`, `lsrv`, `asrv`). +pub open spec fn shift_of(o: lir::IBin) -> Option { + match o { + lir::IBin::Shl => Some(isa::ShiftType::Lsl), + lir::IBin::ShrU => Some(isa::ShiftType::Lsr), + lir::IBin::ShrS => Some(isa::ShiftType::Asr), + _ => None, + } +} + +/// The instructions that load the slots `ls` (register, slot pairs) one after +/// the other. +pub open spec fn loads_seq(ls: Seq<(u8, nat)>) -> Seq + decreases ls.len(), +{ + if ls.len() == 0 { + seq![] + } else { + loads_seq(ls.drop_last()) + seq![load_slot(ls.last().0, ls.last().1)] + } +} + +/// A template: the loads, the register-only middle, and one store of `x0` to +/// a slot. +pub open spec fn shape(ls: Seq<(u8, nat)>, mid: Seq, ts: nat) -> Seq { + loads_seq(ls) + mid + seq![store_slot(ts, x0())] +} + +/// `op x0, x0, x1` for the integer operator `o` at width `sf`: `add`, `sub`, +/// `and`, `orr`, `eor`, `mul` (`madd x0, x0, x1, xzr`), or a variable shift. +pub open spec fn int_op_instr(o: lir::IBin, sf: isa::Width) -> isa::Instr { + match o { + lir::IBin::Add => isa::Instr::AddSubShiftedReg { + sf, + sub_op: false, + setflags: false, + d: x0(), + n: x0(), + m: x1(), + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + lir::IBin::Sub => isa::Instr::AddSubShiftedReg { + sf, + sub_op: true, + setflags: false, + d: x0(), + n: x0(), + m: x1(), + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + lir::IBin::And => isa::Instr::LogicalShiftedReg { + sf, + opc: isa::LogicalOpc::And, + d: x0(), + n: x0(), + m: x1(), + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + lir::IBin::Or => isa::Instr::LogicalShiftedReg { + sf, + opc: isa::LogicalOpc::Orr, + d: x0(), + n: x0(), + m: x1(), + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + lir::IBin::Xor => isa::Instr::LogicalShiftedReg { + sf, + opc: isa::LogicalOpc::Eor, + d: x0(), + n: x0(), + m: x1(), + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + lir::IBin::Shl => isa::Instr::ShiftVariable { + sf, + shift_type: isa::ShiftType::Lsl, + d: x0(), + n: x0(), + m: x1(), + }, + lir::IBin::ShrU => isa::Instr::ShiftVariable { + sf, + shift_type: isa::ShiftType::Lsr, + d: x0(), + n: x0(), + m: x1(), + }, + lir::IBin::ShrS => isa::Instr::ShiftVariable { + sf, + shift_type: isa::ShiftType::Asr, + d: x0(), + n: x0(), + m: x1(), + }, + lir::IBin::Mul => isa::Instr::MulAddSub { + sf, + sub_op: false, + d: x0(), + n: x0(), + m: x1(), + a: 31, + }, + // no template (the subset has no rotate): a placeholder, never emitted + lir::IBin::Rotl => isa::Instr::MulAddSub { + sf, + sub_op: false, + d: x0(), + n: x0(), + m: x1(), + a: 31, + }, + lir::IBin::Rotr => isa::Instr::MulAddSub { + sf, + sub_op: false, + d: x0(), + n: x0(), + m: x1(), + a: 31, + }, + } +} + +/// `cmp rn, rm` (`subs xzr, rn, rm`, or `wzr` at 32 bits): sets the flags of +/// `rn - rm` and writes nothing. +pub open spec fn cmp_rr(sf: isa::Width, n: u8, m: u8) -> isa::Instr { + isa::Instr::AddSubShiftedReg { + sf, + sub_op: true, + setflags: true, + d: 31, + n, + m, + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + } +} + +/// `cmp rn, #0` (`subs xzr, rn, #0`): sets the flags of `rn - 0`. +pub open spec fn cmp_zero(sf: isa::Width, n: u8) -> isa::Instr { + isa::Instr::AddSubImm { sf, sub_op: true, setflags: true, d: 31, n, imm12: 0, sh: false } +} + +/// `cset w0, cc`, with `cond` the inverse of `cc` (see [`cset_cond`]): +/// `csinc w0, wzr, wzr, cond`, 1 if `cond` fails and 0 if it holds. +pub open spec fn cset0(cond: u8) -> isa::Instr { + isa::Instr::CondSelect { + sf: isa::Width::W32, + else_inv: false, + else_inc: true, + d: x0(), + n: 31, + m: 31, + cond, + } +} + +/// `cmp x0, x1; cset w0, cc`: `x0` is 0 or 1. +pub open spec fn cmp_mid(c: lir::ICmp, sf: isa::Width) -> Seq { + seq![cmp_rr(sf, x0(), x1()), cset0(cset_cond(c))] +} + +/// `cmp x0, #0; cset w0, eq`: `x0` is 1 if it was 0, else 0. +pub open spec fn eqz_mid(sf: isa::Width) -> Seq { + seq![cmp_zero(sf, x0()), cset0(1)] +} + +/// `movz x1, #0x8000, lsl #48; eor x0, x0, x1`: `f64.neg` (flip the sign bit) +/// on the bits. +pub open spec fn neg_mid() -> Seq { + seq![ + isa::Instr::MoveWide { + sf: isa::Width::W64, + opcode: isa::MoveWideOp::Z, + d: x1(), + imm16: 0x8000, + hw: 3, + }, + isa::Instr::LogicalShiftedReg { + sf: isa::Width::W64, + opc: isa::LogicalOpc::Eor, + d: x0(), + n: x0(), + m: x1(), + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + ] +} + +/// `ubfx x0, x0, #0, #63` (`ubfm x0, x0, #0, #62`): the low 63 bits, which +/// clears the sign bit: `f64.abs` on the bits. +pub open spec fn abs_mid() -> Seq { + seq![ + isa::Instr::Bitfield { sf: isa::Width::W64, signed: false, d: x0(), n: x0(), immr: 0, imms: 62 }, + ] +} + +/// `sxtw x0, w0` (`sbfm x0, x0, #0, #31`): sign-extends the low 32 bits of +/// `x0` to 64. +pub open spec fn exts_mid() -> Seq { + seq![ + isa::Instr::Bitfield { sf: isa::Width::W64, signed: true, d: x0(), n: x0(), immr: 0, imms: 31 }, + ] +} + +/// `mov w0, w0` (`orr w0, wzr, w0`): the low 32 bits, zero-extended. +pub open spec fn wrap_mid() -> Seq { + seq![ + isa::Instr::LogicalShiftedReg { + sf: isa::Width::W32, + opc: isa::LogicalOpc::Orr, + d: x0(), + n: 31, + m: x0(), + shift_type: isa::ShiftType::Lsl, + shift_amount: 0, + }, + ] +} + +/// `cmp w2, #0; csel x0, x0, x1, ne`: `x0` (the first value) stays unless the +/// condition in `x2` is 0, when it becomes `x1` (the second). +pub open spec fn select_mid() -> Seq { + seq![ + cmp_zero(isa::Width::W32, x2()), + isa::Instr::CondSelect { + sf: isa::Width::W64, + else_inv: false, + else_inc: false, + d: x0(), + n: x0(), + m: x1(), + cond: 1, + }, + ] +} + +/// The template of an integer arithmetic, logic or shift instruction at +/// slots `t` (left operand and result) and `t + 1` (right operand): `ldr x0, +/// [t]; ldr x1, [t+1]; op x0, x0, x1; str x0, [t]`. +pub open spec fn int_bin_tmpl(o: lir::IBin, sf: isa::Width, t: nat) -> Seq { + shape(seq![(x0(), t), (x1(), t + 1)], seq![int_op_instr(o, sf)], t) +} + +/// The template of an integer comparison at slots `t` and `t + 1`: `ldr x0, +/// [t]; ldr x1, [t+1]; cmp x0, x1; cset w0, cc; str x0, [t]`. +pub open spec fn int_cmp_tmpl(c: lir::ICmp, sf: isa::Width, t: nat) -> Seq { + shape(seq![(x0(), t), (x1(), t + 1)], cmp_mid(c, sf), t) +} + +/// The template of `eqz` at slot `t`. +pub open spec fn eqz_tmpl(sf: isa::Width, t: nat) -> Seq { + shape(seq![(x0(), t)], eqz_mid(sf), t) +} + +/// The template of a binary instruction at stack height `h` with `nl` +/// locals. An empty list for an instruction outside the slice. +pub open spec fn bin_tmpl(b: lir::BinOp, nl: nat, h: nat) -> Seq { + let t = (nl + h - 2) as nat; + match b { + lir::BinOp::I32(o) => int_bin_tmpl(o, isa::Width::W32, t), + lir::BinOp::I64(o) => int_bin_tmpl(o, isa::Width::W64, t), + lir::BinOp::I32Cmp(c) => int_cmp_tmpl(c, isa::Width::W32, t), + lir::BinOp::I64Cmp(c) => int_cmp_tmpl(c, isa::Width::W64, t), + lir::BinOp::F64(_) => seq![], + lir::BinOp::F64Cmp(_) => seq![], + } +} + +/// The template of a unary instruction at stack height `h` with `nl` locals. +/// The conversions that change no bits of the word (`extend_i32_u`, the +/// reinterprets) are empty: an `i32` is stored zero-extended, and an `f64` +/// is its bits. +pub open spec fn un_tmpl(u: lir::UnOp, nl: nat, h: nat) -> Seq { + let t = (nl + h - 1) as nat; + match u { + lir::UnOp::I32Eqz => eqz_tmpl(isa::Width::W32, t), + lir::UnOp::I64Eqz => eqz_tmpl(isa::Width::W64, t), + lir::UnOp::F64Neg => shape(seq![(x0(), t)], neg_mid(), t), + lir::UnOp::F64Abs => shape(seq![(x0(), t)], abs_mid(), t), + lir::UnOp::I64ExtendI32S => shape(seq![(x0(), t)], exts_mid(), t), + lir::UnOp::I32WrapI64 => shape(seq![(x0(), t)], wrap_mid(), t), + lir::UnOp::I64ExtendI32U => seq![], + lir::UnOp::F64ReinterpretI64 => seq![], + lir::UnOp::I64ReinterpretF64 => seq![], + lir::UnOp::F64Sqrt => seq![], + lir::UnOp::F64ConvertI32S => seq![], + lir::UnOp::F64ConvertI32U => seq![], + lir::UnOp::F64ConvertI64S => seq![], + lir::UnOp::F64ConvertI64U => seq![], + } +} + +/// `movz x0, #imm16, lsl #(16 hw)` at width `sf`. +pub open spec fn movz0(sf: isa::Width, imm16: u16, hw: u8) -> isa::Instr { + isa::Instr::MoveWide { sf, opcode: isa::MoveWideOp::Z, d: x0(), imm16, hw } +} + +/// `movk x0, #imm16, lsl #(16 hw)` at width `sf`. +pub open spec fn movk0(sf: isa::Width, imm16: u16, hw: u8) -> isa::Instr { + isa::Instr::MoveWide { sf, opcode: isa::MoveWideOp::K, d: x0(), imm16, hw } +} + +/// The 16-bit chunk `k` (0 is the lowest) of a 64-bit word. +pub open spec fn chunk(v: u64, k: nat) -> u16 { + if k == 0 { + (v % 0x1_0000) as u16 + } else if k == 1 { + ((v / 0x1_0000) % 0x1_0000) as u16 + } else if k == 2 { + ((v / 0x1_0000_0000) % 0x1_0000) as u16 + } else { + (v / 0x1_0000_0000_0000) as u16 + } +} + +/// The load of a constant into `x0`: `movz w0, #lo; movk w0, #hi, lsl #16` for +/// an `i32`, and `movz x0, #c0; movk x0, #c1, lsl #16; movk x0, #c2, lsl #32; +/// movk x0, #c3, lsl #48` for an `i64` or an `f64`'s bits. (No chunk is +/// skipped: a `movk` of 0 is a harmless instruction, and the proof stays a +/// straight line.) +pub open spec fn const_mid(v: lir::Val) -> Seq { + match v { + lir::Val::I32(c) => seq![ + movz0(isa::Width::W32, chunk(c as u64, 0), 0), + movk0(isa::Width::W32, chunk(c as u64, 1), 1), + ], + lir::Val::I64(c) => seq![ + movz0(isa::Width::W64, chunk(c, 0), 0), + movk0(isa::Width::W64, chunk(c, 1), 1), + movk0(isa::Width::W64, chunk(c, 2), 2), + movk0(isa::Width::W64, chunk(c, 3), 3), + ], + lir::Val::F64(c) => seq![ + movz0(isa::Width::W64, chunk(c, 0), 0), + movk0(isa::Width::W64, chunk(c, 1), 1), + movk0(isa::Width::W64, chunk(c, 2), 2), + movk0(isa::Width::W64, chunk(c, 3), 3), + ], + } +} + +/// **The template of one instruction** at stack height `h` with `nl` locals. +pub open spec fn tmpl(op: Op, nl: nat, h: nat) -> Seq { + match op { + Op::Nop => seq![], + Op::Drop => seq![], + // the new top is slot nl + h + Op::Const(v) => shape(seq![], const_mid(v), nl + h), + Op::LocalGet(x) => shape(seq![(x0(), x as nat)], seq![], nl + h), + // the top is slot nl + h - 1 + Op::LocalSet(x) => shape(seq![(x0(), (nl + h - 1) as nat)], seq![], x as nat), + Op::LocalTee(x) => shape(seq![(x0(), (nl + h - 1) as nat)], seq![], x as nat), + // v1, v2, c at slots t, t + 1, t + 2: c == 0 picks v2 (csel .. ne) + Op::Select => { + let t = (nl + h - 3) as nat; + shape(seq![(x0(), t), (x1(), t + 1), (x2(), t + 2)], select_mid(), t) + }, + Op::Bin(b) => bin_tmpl(b, nl, h), + Op::Un(u) => un_tmpl(u, nl, h), + } +} + +/// The code of a program: the templates one after the other, each at the +/// stack height the instructions before it leave. +pub open spec fn code_of(ops: Seq, nl: nat, h: nat) -> Seq + decreases ops.len(), +{ + if ops.len() == 0 { + seq![] + } else { + code_of(ops.drop_last(), nl, h) + tmpl(ops.last(), nl, height_after(ops.drop_last(), h)) + } +} + +} // verus! diff --git a/crates/clazel-jit/src/a64/tlemmas.rs b/crates/clazel-jit/src/a64/tlemmas.rs new file mode 100644 index 000000000..7f93aba82 --- /dev/null +++ b/crates/clazel-jit/src/a64/tlemmas.rs @@ -0,0 +1,444 @@ +//! Facts about the templates the emitter needs: a template is the loads, the +//! middle and the store written out; every instruction of every template is in +//! the A64 subset ([`isa::valid_instr`]), so the encoder accepts it. The A64 +//! counterpart of [`crate::tlemmas`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `shape` with no loads. +pub proof fn lemma_shape0(mid: Seq, ts: nat) + ensures + super::templates::shape(seq![], mid, ts) == mid + seq![super::templates::store_slot(ts, x0())], +{ + assert(super::templates::loads_seq(Seq::<(u8, nat)>::empty()) == Seq::::empty()); + assert(Seq::::empty() + mid =~= mid); +} + +/// `shape` with one load, written out. +pub proof fn lemma_shape1(r: u8, j: nat, mid: Seq, ts: nat) + ensures + super::templates::shape(seq![(r, j)], mid, ts) == seq![super::templates::load_slot(r, j)] + mid + seq![super::templates::store_slot(ts, x0())], +{ + let ls = seq![(r, j)]; + assert(ls.drop_last() =~= Seq::<(u8, nat)>::empty()); + assert(ls.last() == (r, j)); + assert(super::templates::loads_seq(Seq::<(u8, nat)>::empty()) == Seq::::empty()); + assert(super::templates::loads_seq(ls) =~= seq![super::templates::load_slot(r, j)]); +} + +/// `shape` with two loads, written out. +pub proof fn lemma_shape2(r1: u8, j1: nat, r2: u8, j2: nat, mid: Seq, ts: nat) + ensures + super::templates::shape(seq![(r1, j1), (r2, j2)], mid, ts) == seq![super::templates::load_slot(r1, j1), super::templates::load_slot(r2, j2)] + + mid + seq![super::templates::store_slot(ts, x0())], +{ + let ls = seq![(r1, j1), (r2, j2)]; + assert(ls.drop_last() =~= seq![(r1, j1)]); + assert(ls.last() == (r2, j2)); + lemma_shape1(r1, j1, seq![], 0); + assert(super::templates::loads_seq(seq![(r1, j1)]) =~= seq![super::templates::load_slot(r1, j1)]) by { + let l1 = seq![(r1, j1)]; + assert(l1.drop_last() =~= Seq::<(u8, nat)>::empty()); + assert(l1.last() == (r1, j1)); + assert(super::templates::loads_seq(Seq::<(u8, nat)>::empty()) == Seq::::empty()); + } + assert(super::templates::loads_seq(ls) =~= seq![super::templates::load_slot(r1, j1), super::templates::load_slot(r2, j2)]); +} + +/// `shape` with three loads, written out. +pub proof fn lemma_shape3( + r1: u8, + j1: nat, + r2: u8, + j2: nat, + r3: u8, + j3: nat, + mid: Seq, + ts: nat, +) + ensures + super::templates::shape(seq![(r1, j1), (r2, j2), (r3, j3)], mid, ts) == seq![ + super::templates::load_slot(r1, j1), + super::templates::load_slot(r2, j2), + super::templates::load_slot(r3, j3), + ] + mid + seq![super::templates::store_slot(ts, x0())], +{ + let ls = seq![(r1, j1), (r2, j2), (r3, j3)]; + assert(ls.drop_last() =~= seq![(r1, j1), (r2, j2)]); + assert(ls.last() == (r3, j3)); + let l2 = seq![(r1, j1), (r2, j2)]; + assert(l2.drop_last() =~= seq![(r1, j1)]); + assert(l2.last() == (r2, j2)); + let l1 = seq![(r1, j1)]; + assert(l1.drop_last() =~= Seq::<(u8, nat)>::empty()); + assert(l1.last() == (r1, j1)); + assert(super::templates::loads_seq(Seq::<(u8, nat)>::empty()) == Seq::::empty()); + assert(super::templates::loads_seq(l1) =~= seq![super::templates::load_slot(r1, j1)]); + assert(super::templates::loads_seq(l2) =~= seq![super::templates::load_slot(r1, j1), super::templates::load_slot(r2, j2)]); + assert(super::templates::loads_seq(ls) =~= seq![super::templates::load_slot(r1, j1), super::templates::load_slot(r2, j2), super::templates::load_slot(r3, j3)]); +} + +/// `max_slots()` is 31. +pub proof fn lemma_abi_consts_t() + ensures + max_slots() == 31, +{ +} + +/// The concatenation of valid instruction lists is valid. +pub proof fn lemma_valid_concat(a: Seq, b: Seq) + requires + super::contract::all_valid_instrs(a), + super::contract::all_valid_instrs(b), + ensures + super::contract::all_valid_instrs(a + b), +{ + assert forall|k: int| 0 <= k < (a + b).len() implies isa::valid_instr(#[trigger] (a + b)[k]) by { + if k < a.len() { + assert((a + b)[k] == a[k]); + } else { + assert((a + b)[k] == b[k - a.len()]); + } + } +} + +/// A load from or a store to a frame slot, through any register, is in the subset. +pub proof fn lemma_ls_valid(r: u8, j: nat) + requires + r < 32, + j < max_slots(), + ensures + isa::valid_instr(super::templates::load_slot(r, j)), + isa::valid_instr(super::templates::store_slot(j, r)), +{ + super::steps::lemma_slot_imm(j); +} + +/// The loads of `ls` are valid when every register and slot is. +pub proof fn lemma_loads_valid(ls: Seq<(u8, nat)>) + requires + forall|k: int| 0 <= k < ls.len() ==> (#[trigger] ls[k]).0 < 32 && ls[k].1 < max_slots(), + ensures + super::contract::all_valid_instrs(super::templates::loads_seq(ls)), + decreases ls.len(), +{ + if ls.len() > 0 { + let pre = ls.drop_last(); + let last = ls.last(); + assert forall|k: int| 0 <= k < pre.len() implies (#[trigger] pre[k]).0 < 32 && pre[k].1 + < max_slots() by { + assert(pre[k] == ls[k]); + } + lemma_loads_valid(pre); + assert(last == ls[ls.len() - 1]); + lemma_ls_valid(last.0, last.1); + let one = seq![super::templates::load_slot(last.0, last.1)]; + assert(super::contract::all_valid_instrs(one)) by { + assert forall|k: int| 0 <= k < one.len() implies isa::valid_instr(#[trigger] one[k]) by { + assert(k == 0); + } + } + lemma_valid_concat(super::templates::loads_seq(pre), one); + } +} + +/// A template with valid loads, a valid middle and a slot to store to is valid. +pub proof fn lemma_shape_valid(ls: Seq<(u8, nat)>, mid: Seq, ts: nat) + requires + forall|k: int| 0 <= k < ls.len() ==> (#[trigger] ls[k]).0 < 32 && ls[k].1 < max_slots(), + super::contract::all_valid_instrs(mid), + ts < max_slots(), + ensures + super::contract::all_valid_instrs(super::templates::shape(ls, mid, ts)), +{ + lemma_loads_valid(ls); + lemma_ls_valid(0, ts); + let st = seq![super::templates::store_slot(ts, x0())]; + assert(super::contract::all_valid_instrs(st)) by { + assert forall|k: int| 0 <= k < st.len() implies isa::valid_instr(#[trigger] st[k]) by { + assert(k == 0); + } + } + lemma_valid_concat(super::templates::loads_seq(ls), mid); + lemma_valid_concat(super::templates::loads_seq(ls) + mid, st); +} + +/// The middle of an integer operator is one valid instruction. +pub proof fn lemma_int_op_valid(o: lir::IBin, sf: isa::Width) + requires + crate::spec::ibin_supported(o), + ensures + isa::valid_instr(super::templates::int_op_instr(o, sf)), +{ + match o { + lir::IBin::Add => {}, + lir::IBin::Sub => {}, + lir::IBin::And => {}, + lir::IBin::Or => {}, + lir::IBin::Xor => {}, + lir::IBin::Shl => {}, + lir::IBin::ShrU => {}, + lir::IBin::ShrS => {}, + lir::IBin::Mul => {}, + lir::IBin::Rotl => { + assert(false); + }, + lir::IBin::Rotr => { + assert(false); + }, + } +} + +/// The condition field of a `cset` is below 14. +pub proof fn lemma_cset_cond_ok(c: lir::ICmp) + ensures + super::templates::cset_cond(c) < 14, +{ + match c { + lir::ICmp::Eq => {}, + lir::ICmp::Ne => {}, + lir::ICmp::LtS => {}, + lir::ICmp::LtU => {}, + lir::ICmp::GtS => {}, + lir::ICmp::GtU => {}, + lir::ICmp::LeS => {}, + lir::ICmp::LeU => {}, + lir::ICmp::GeS => {}, + lir::ICmp::GeU => {}, + } +} + +/// Every middle of the first slice is a list of valid instructions. +pub proof fn lemma_mids_valid() + ensures + forall|c: lir::ICmp, sf: isa::Width| + #[trigger] super::contract::all_valid_instrs(super::templates::cmp_mid(c, sf)), + forall|sf: isa::Width| #[trigger] super::contract::all_valid_instrs(super::templates::eqz_mid(sf)), + super::contract::all_valid_instrs(super::templates::neg_mid()), + super::contract::all_valid_instrs(super::templates::abs_mid()), + super::contract::all_valid_instrs(super::templates::exts_mid()), + super::contract::all_valid_instrs(super::templates::wrap_mid()), + super::contract::all_valid_instrs(super::templates::select_mid()), + forall|v: lir::Val| #[trigger] super::contract::all_valid_instrs(super::templates::const_mid(v)), +{ + assert forall|c: lir::ICmp, sf: isa::Width| + #[trigger] super::contract::all_valid_instrs(super::templates::cmp_mid(c, sf)) by { + lemma_cset_cond_ok(c); + let mid = super::templates::cmp_mid(c, sf); + assert forall|k: int| 0 <= k < mid.len() implies isa::valid_instr(#[trigger] mid[k]) by { + assert(k == 0 || k == 1); + } + } + assert forall|sf: isa::Width| #[trigger] super::contract::all_valid_instrs(super::templates::eqz_mid(sf)) by { + let mid = super::templates::eqz_mid(sf); + assert forall|k: int| 0 <= k < mid.len() implies isa::valid_instr(#[trigger] mid[k]) by { + assert(k == 0 || k == 1); + } + } + let nm = super::templates::neg_mid(); + assert forall|k: int| 0 <= k < nm.len() implies isa::valid_instr(#[trigger] nm[k]) by { + assert(k == 0 || k == 1); + } + let am = super::templates::abs_mid(); + assert forall|k: int| 0 <= k < am.len() implies isa::valid_instr(#[trigger] am[k]) by { + assert(k == 0); + } + let em = super::templates::exts_mid(); + assert forall|k: int| 0 <= k < em.len() implies isa::valid_instr(#[trigger] em[k]) by { + assert(k == 0); + } + let wm = super::templates::wrap_mid(); + assert forall|k: int| 0 <= k < wm.len() implies isa::valid_instr(#[trigger] wm[k]) by { + assert(k == 0); + } + let sm = super::templates::select_mid(); + assert forall|k: int| 0 <= k < sm.len() implies isa::valid_instr(#[trigger] sm[k]) by { + assert(k == 0 || k == 1); + } + assert forall|v: lir::Val| #[trigger] super::contract::all_valid_instrs(super::templates::const_mid(v)) by { + let cm = super::templates::const_mid(v); + assert forall|k: int| 0 <= k < cm.len() implies isa::valid_instr(#[trigger] cm[k]) by { + match v { + lir::Val::I32(c) => { + assert(k == 0 || k == 1); + }, + lir::Val::I64(c) => { + assert(k == 0 || k == 1 || k == 2 || k == 3); + }, + lir::Val::F64(c) => { + assert(k == 0 || k == 1 || k == 2 || k == 3); + }, + } + } + } +} + +/// **Every template is in the subset.** +pub proof fn lemma_tmpl_valid(op: Op, nl: nat, h: nat) + requires + op_ok(op, nl, h), + ensures + super::contract::all_valid_instrs(super::templates::tmpl(op, nl, h)), +{ + lemma_abi_consts_t(); + lemma_mids_valid(); + let code = super::templates::tmpl(op, nl, h); + match op { + Op::Nop => { + assert(super::contract::all_valid_instrs(code)); + }, + Op::Drop => { + assert(super::contract::all_valid_instrs(code)); + }, + Op::Const(v) => { + lemma_shape_valid(seq![], super::templates::const_mid(v), nl + h); + }, + Op::LocalGet(x) => { + lemma_shape_valid(seq![(x0(), x as nat)], seq![], nl + h); + }, + Op::LocalSet(x) => { + lemma_shape_valid(seq![(x0(), (nl + h - 1) as nat)], seq![], x as nat); + }, + Op::LocalTee(x) => { + lemma_shape_valid(seq![(x0(), (nl + h - 1) as nat)], seq![], x as nat); + }, + Op::Select => { + let t = (nl + h - 3) as nat; + let ls = seq![(x0(), t), (x1(), t + 1), (x2(), t + 2)]; + assert forall|k: int| 0 <= k < ls.len() implies (#[trigger] ls[k]).0 < 32 && ls[k].1 + < max_slots() by { + assert(k == 0 || k == 1 || k == 2); + } + lemma_shape_valid(ls, super::templates::select_mid(), t); + }, + Op::Bin(b) => { + let t = (nl + h - 2) as nat; + let ls = seq![(x0(), t), (x1(), t + 1)]; + assert forall|k: int| 0 <= k < ls.len() implies (#[trigger] ls[k]).0 < 32 && ls[k].1 + < max_slots() by { + assert(k == 0 || k == 1); + } + match b { + lir::BinOp::I32(o) => { + lemma_int_op_valid(o, isa::Width::W32); + let mid = seq![super::templates::int_op_instr(o, isa::Width::W32)]; + assert(super::contract::all_valid_instrs(mid)) by { + assert forall|k: int| 0 <= k < mid.len() implies isa::valid_instr(#[trigger] mid[k]) by { + assert(k == 0); + } + } + lemma_shape_valid(ls, mid, t); + }, + lir::BinOp::I64(o) => { + lemma_int_op_valid(o, isa::Width::W64); + let mid = seq![super::templates::int_op_instr(o, isa::Width::W64)]; + assert(super::contract::all_valid_instrs(mid)) by { + assert forall|k: int| 0 <= k < mid.len() implies isa::valid_instr(#[trigger] mid[k]) by { + assert(k == 0); + } + } + lemma_shape_valid(ls, mid, t); + }, + lir::BinOp::I32Cmp(c) => { + lemma_shape_valid(ls, super::templates::cmp_mid(c, isa::Width::W32), t); + }, + lir::BinOp::I64Cmp(c) => { + lemma_shape_valid(ls, super::templates::cmp_mid(c, isa::Width::W64), t); + }, + lir::BinOp::F64(_) => { + assert(false); + }, + lir::BinOp::F64Cmp(_) => { + assert(false); + }, + } + }, + Op::Un(u) => { + let t = (nl + h - 1) as nat; + let ls = seq![(x0(), t)]; + assert forall|k: int| 0 <= k < ls.len() implies (#[trigger] ls[k]).0 < 32 && ls[k].1 + < max_slots() by { + assert(k == 0); + } + match u { + lir::UnOp::I32Eqz => { + lemma_shape_valid(ls, super::templates::eqz_mid(isa::Width::W32), t); + }, + lir::UnOp::I64Eqz => { + lemma_shape_valid(ls, super::templates::eqz_mid(isa::Width::W64), t); + }, + lir::UnOp::F64Neg => { + lemma_shape_valid(ls, super::templates::neg_mid(), t); + }, + lir::UnOp::F64Abs => { + lemma_shape_valid(ls, super::templates::abs_mid(), t); + }, + lir::UnOp::I64ExtendI32S => { + lemma_shape_valid(ls, super::templates::exts_mid(), t); + }, + lir::UnOp::I32WrapI64 => { + lemma_shape_valid(ls, super::templates::wrap_mid(), t); + }, + lir::UnOp::I64ExtendI32U => {}, + lir::UnOp::F64ReinterpretI64 => {}, + lir::UnOp::I64ReinterpretF64 => {}, + lir::UnOp::F64Sqrt => { + assert(false); + }, + lir::UnOp::F64ConvertI32S => { + assert(false); + }, + lir::UnOp::F64ConvertI32U => { + assert(false); + }, + lir::UnOp::F64ConvertI64S => { + assert(false); + }, + lir::UnOp::F64ConvertI64U => { + assert(false); + }, + } + }, + } +} + +/// The code of a program is a list of valid instructions. +pub proof fn lemma_code_of_valid(ops: Seq, nl: nat, h0: nat) + requires + prog_ok(ops, nl, h0), + ensures + super::contract::all_valid_instrs(super::templates::code_of(ops, nl, h0)), + decreases ops.len(), +{ + if ops.len() > 0 { + let pre = ops.drop_last(); + let last = ops.last(); + let hl = height_after(pre, h0); + lemma_code_of_valid(pre, nl, h0); + lemma_tmpl_valid(last, nl, hl); + lemma_valid_concat( + super::templates::code_of(pre, nl, h0), + super::templates::tmpl(last, nl, hl), + ); + } +} + +/// The exit sequence is a list of valid instructions. +pub proof fn lemma_exit_valid(c: u16) + ensures + super::contract::all_valid_instrs(exit_seq(c)), +{ + super::steps::lemma_abi_consts(); + assert forall|k: int| 0 <= k < exit_seq(c).len() implies isa::valid_instr( + #[trigger] exit_seq(c)[k], + ) by { + assert(k == 0 || k == 1 || k == 2); + } +} + +} // verus! diff --git a/crates/clazel-jit/src/agree.rs b/crates/clazel-jit/src/agree.rs new file mode 100644 index 000000000..5b66bddfc --- /dev/null +++ b/crates/clazel-jit/src/agree.rs @@ -0,0 +1,188 @@ +//! How the LIR states before and after an instruction relate slot by slot +//! ([`slots_agree_except`], [`slots_agree`]), for each way an instruction +//! changes the locals and the operand stack: push a value, replace the top +//! `k` values by one, drop the top, set a local. They are what the template +//! proofs feed to the skeleton. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Pushing `v`: the new top slot (`nl + h`) is the only slot that changes, and +/// it holds `v`'s word. +pub proof fn lemma_agree_push(a: lir::St, a2: lir::St, nl: nat, h: nat, v: lir::Val) + requires + a.stack.len() == h, + a.locals.len() == nl, + nl + h + 1 <= max_slots(), + a2 == a.with_stack(a.stack.push(v)), + ensures + super::rel::slots_agree_except(a, a2, nl, nl + h), + lir::val_bits(slot_val(a2, nl, nl + h)) == lir::val_bits(v), +{ + assert(a2.stack.len() == h + 1); + assert forall|j: nat| j < nl + a2.stack.len() && j != nl + h implies j < nl + a.stack.len() + && lir::val_bits(#[trigger] slot_val(a2, nl, j)) == lir::val_bits(slot_val(a, nl, j)) by { + if j >= nl { + assert(a2.stack[(j - nl) as int] == a.stack[(j - nl) as int]); + } + } + assert(a2.stack[h as int] == v); +} + +/// Replacing the top `k` values by one value `v`: the slot `nl + h - k` is the +/// only one that changes, and it holds `v`'s word. +pub proof fn lemma_agree_replace( + a: lir::St, + a2: lir::St, + nl: nat, + h: nat, + k: nat, + v: lir::Val, +) + requires + a.stack.len() == h, + a.locals.len() == nl, + 1 <= k <= h, + nl + (h - k) + 1 <= max_slots(), + a2 == a.with_stack(lir::below(a.stack, k).push(v)), + ensures + super::rel::slots_agree_except(a, a2, nl, (nl + h - k) as nat), + lir::val_bits(slot_val(a2, nl, (nl + h - k) as nat)) == lir::val_bits(v), +{ + let hk = (h - k) as nat; + assert(a2.stack.len() == hk + 1); + assert(lir::below(a.stack, k) =~= a.stack.take(hk as int)); + assert forall|j: nat| + j < nl + a2.stack.len() && j != (nl + h - k) as nat implies j < nl + a.stack.len() + && lir::val_bits(#[trigger] slot_val(a2, nl, j)) == lir::val_bits(slot_val(a, nl, j)) by { + if j >= nl { + assert(a2.stack[(j - nl) as int] == a.stack[(j - nl) as int]); + } + } + assert(a2.stack[hk as int] == v); +} + +/// Dropping the top value, or any change to the stack that keeps a prefix: +/// every slot of `a2` is a slot of `a`. +pub proof fn lemma_agree_drop(a: lir::St, a2: lir::St, nl: nat, h: nat) + requires + a.stack.len() == h, + a.locals.len() == nl, + 1 <= h, + nl + h <= max_slots(), + a2 == a.with_stack(a.stack.drop_last()), + ensures + super::rel::slots_agree(a, a2, nl), +{ + assert forall|j: nat| j < nl + a2.stack.len() implies j < nl + a.stack.len() && lir::val_bits( + #[trigger] slot_val(a2, nl, j), + ) == lir::val_bits(slot_val(a, nl, j)) by { + if j >= nl { + assert(a2.stack[(j - nl) as int] == a.stack[(j - nl) as int]); + } + } +} + +/// `local.set x`: the stack loses its top, local `x` takes its value. The only +/// slot that changes is `x`. +pub proof fn lemma_agree_set(a: lir::St, a2: lir::St, nl: nat, h: nat, x: nat) + requires + a.stack.len() == h, + a.locals.len() == nl, + 1 <= h, + x < nl, + nl + h <= max_slots(), + a2 == (lir::St { + stack: a.stack.drop_last(), + locals: a.locals.update(x as int, a.stack.last()), + mem: a.mem, + }), + ensures + super::rel::slots_agree_except(a, a2, nl, x), + lir::val_bits(slot_val(a2, nl, x)) == lir::val_bits(slot_val(a, nl, (nl + h - 1) as nat)), +{ + assert forall|j: nat| j < nl + a2.stack.len() && j != x implies j < nl + a.stack.len() + && lir::val_bits(#[trigger] slot_val(a2, nl, j)) == lir::val_bits(slot_val(a, nl, j)) by { + if j >= nl { + assert(a2.stack[(j - nl) as int] == a.stack[(j - nl) as int]); + } else { + assert(a2.locals[j as int] == a.locals[j as int]); + } + } + assert(a.stack.last() == a.stack[h - 1]); +} + +/// `local.tee x`: the stack is unchanged, local `x` takes the top's value. +pub proof fn lemma_agree_tee(a: lir::St, a2: lir::St, nl: nat, h: nat, x: nat) + requires + a.stack.len() == h, + a.locals.len() == nl, + 1 <= h, + x < nl, + nl + h <= max_slots(), + a2 == a.with_locals(a.locals.update(x as int, a.stack.last())), + ensures + super::rel::slots_agree_except(a, a2, nl, x), + lir::val_bits(slot_val(a2, nl, x)) == lir::val_bits(slot_val(a, nl, (nl + h - 1) as nat)), +{ + assert forall|j: nat| j < nl + a2.stack.len() && j != x implies j < nl + a.stack.len() + && lir::val_bits(#[trigger] slot_val(a2, nl, j)) == lir::val_bits(slot_val(a, nl, j)) by { + if j < nl { + assert(a2.locals[j as int] == a.locals[j as int]); + } + } + assert(a.stack.last() == a.stack[h - 1]); +} + +/// Replacing the top by a value with the same word changes no slot's word. +pub proof fn lemma_agree_same_word(a: lir::St, a2: lir::St, nl: nat, h: nat, v: lir::Val) + requires + a.stack.len() == h, + a.locals.len() == nl, + 1 <= h, + nl + h <= max_slots(), + lir::val_bits(v) == lir::val_bits(a.stack.last()), + a2 == a.with_stack(lir::below(a.stack, 1).push(v)), + ensures + super::rel::slots_agree(a, a2, nl), +{ + assert(lir::below(a.stack, 1) =~= a.stack.take((h - 1) as int)); + assert(a2.stack.len() == h); + assert forall|j: nat| j < nl + a2.stack.len() implies j < nl + a.stack.len() && lir::val_bits( + #[trigger] slot_val(a2, nl, j), + ) == lir::val_bits(slot_val(a, nl, j)) by { + if j >= nl { + if j - nl == h - 1 { + assert(a2.stack[(h - 1) as int] == v); + assert(a.stack.last() == a.stack[(h - 1) as int]); + } else { + assert(a2.stack[(j - nl) as int] == a.stack[(j - nl) as int]); + } + } + } +} + +/// A state is related to itself, to the same machine state: no change. +pub proof fn lemma_agree_refl(a: lir::St, nl: nat) + requires + a.locals.len() == nl, + nl + a.stack.len() <= max_slots(), + ensures + super::rel::slots_agree(a, a, nl), +{ +} + +/// `preserved` holds between a machine state and itself. +pub proof fn lemma_preserved_refl(m: x64::State) + requires + m.rgf.len() == 16, + ensures + preserved(m, m), +{ +} + +} // verus! diff --git a/crates/clazel-jit/src/alu.rs b/crates/clazel-jit/src/alu.rs new file mode 100644 index 000000000..5bdc81aef --- /dev/null +++ b/crates/clazel-jit/src/alu.rs @@ -0,0 +1,444 @@ +//! The arithmetic, logic, shift and multiply instructions of the integer +//! templates: what `x64::step` does to the register file, and that the word +//! it leaves in `rax` is the result LIR's operator defines +//! (`lir::ibin32`, `lir::ibin64`). +//! +//! The results are statements about *values*: the 32-bit forms are given +//! zero-extended words (`x as u64`), and they leave a zero-extended word. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// What a register-only stretch of a template does, as far as the template's +/// other steps are concerned: `m1` is `m` with scratch registers changed +/// (`rd` now holds the word `w`), the same memory, the context register and +/// stack pointer as they were, `rip` moved on by `len`, and still a state +/// with 16 general and 16 XMM registers. +pub open spec fn mid_post(m: x64::State, m1: x64::State, rd: u8, w: nat, len: nat) -> bool { + m1.rgf.len() == 16 && m1.xmm.len() == 16 && m1.rgf[rd as int] as nat == w && m1.rgf[15] + == m.rgf[15] && m1.rgf[4] == m.rgf[4] && m1.mem == m.mem && m1.rip == m.rip + len +} + +/// `write_user_rflags` changes only the flags and the undefined-value seed. +pub proof fn lemma_wur_frame(cu: spec_fn(nat) -> nat, s: x64::State, vec: u32, mask: u32) + ensures + ({ + let t = x64::write_user_rflags(cu, s, vec, mask); + t.rgf == s.rgf && t.mem == s.mem && t.xmm == s.xmm && t.rip == s.rip + }), +{ +} + +/// A two-operand arithmetic or logic instruction (not `cmp`) writes the +/// destination register with the result of the operation, changes the flags, +/// and nothing else. +pub proof fn lemma_step_alu_rr( + cu: spec_fn(nat) -> nat, + m: x64::State, + alu: x64::AluOp, + sz: x64::Size, + dst: u8, + src: u8, +) + requires + x64::wf(m), + dst < 16, + src < 16, + !(alu is Cmp), + x64::canonical( + m.rip + x64::instr_len(x64::Instr::AluRR { alu: alu, sz: sz, dst: dst, src: src }), + ), + ensures + x64::step(cu, m, x64::Instr::AluRR { alu: alu, sz: sz, dst: dst, src: src }) is Some, + ({ + let m1 = x64::step( + cu, + m, + x64::Instr::AluRR { alu: alu, sz: sz, dst: dst, src: src }, + ).unwrap(); + let r = x64::gpr_arith_logic_spec( + x64::nbits(sz), + alu, + x64::rgfi_size(m, sz, dst), + x64::rgfi_size(m, sz, src), + m.rflags, + ); + m1.rgf == x64::set_rgfi_size(m, sz, dst, r.result).rgf && m1.mem == m.mem && m1.xmm + == m.xmm && m1.rip == m.rip + x64::instr_len( + x64::Instr::AluRR { alu: alu, sz: sz, dst: dst, src: src }, + ) + }), +{ + let i = x64::Instr::AluRR { alu: alu, sz: sz, dst: dst, src: src }; + let r = x64::gpr_arith_logic_spec( + x64::nbits(sz), + alu, + x64::rgfi_size(m, sz, dst), + x64::rgfi_size(m, sz, src), + m.rflags, + ); + let s1 = x64::set_rgfi_size(m, sz, dst, r.result); + lemma_wur_frame(cu, s1, r.rflags, r.undef); +} + +/// `imul dst, src` writes the destination register and the flags. +pub proof fn lemma_step_imul_rr( + cu: spec_fn(nat) -> nat, + m: x64::State, + sz: x64::Size, + dst: u8, + src: u8, +) + requires + x64::wf(m), + dst < 16, + src < 16, + x64::canonical( + m.rip + x64::instr_len(x64::Instr::ImulRR { sz: sz, dst: dst, src: src }), + ), + ensures + x64::step(cu, m, x64::Instr::ImulRR { sz: sz, dst: dst, src: src }) is Some, + ({ + let m1 = x64::step(cu, m, x64::Instr::ImulRR { sz: sz, dst: dst, src: src }).unwrap(); + let (low, cf) = x64::imul_spec( + x64::nbits(sz), + x64::rgfi_size(m, sz, src), + x64::rgfi_size(m, sz, dst), + ); + m1.rgf == x64::set_rgfi_size(m, sz, dst, low).rgf && m1.mem == m.mem && m1.xmm + == m.xmm && m1.rip == m.rip + x64::instr_len( + x64::Instr::ImulRR { sz: sz, dst: dst, src: src }, + ) + }), +{ + let (low, cf) = x64::imul_spec( + x64::nbits(sz), + x64::rgfi_size(m, sz, src), + x64::rgfi_size(m, sz, dst), + ); + let s1 = x64::set_rgfi_size(m, sz, dst, low); + // imul_flags is write_user_rflags on s1 + lemma_wur_frame( + cu, + s1, + x64::change_rflags( + m.rflags, + x64::FlagUpd { + cf: Some(cf), + pf: None, + af: None, + zf: None, + sf: None, + of: Some(cf), + }, + ), + x64::mask_pf() | x64::mask_af() | x64::mask_zf() | x64::mask_sf(), + ); +} + +/// `shl/shr/sar r, cl` writes the register and the flags. +pub proof fn lemma_step_shift_cl( + cu: spec_fn(nat) -> nat, + m: x64::State, + sh: x64::ShiftOp, + sz: x64::Size, + r: u8, +) + requires + x64::wf(m), + r < 16, + x64::canonical(m.rip + x64::instr_len(x64::Instr::ShiftCl { shift: sh, sz: sz, r: r })), + ensures + x64::step(cu, m, x64::Instr::ShiftCl { shift: sh, sz: sz, r: r }) is Some, + ({ + let m1 = x64::step(cu, m, x64::Instr::ShiftCl { shift: sh, sz: sz, r: r }).unwrap(); + let cnt = x64::shift_count(sz, x64::loghead(8, x64::rr64(m, 1))); + let o = x64::shift_spec(sh, x64::nbits(sz), x64::rgfi_size(m, sz, r), cnt, m.rflags); + m1.rgf == x64::set_rgfi_size(m, sz, r, o.result).rgf && m1.mem == m.mem && m1.xmm + == m.xmm && m1.rip == m.rip + x64::instr_len( + x64::Instr::ShiftCl { shift: sh, sz: sz, r: r }, + ) + }), +{ + let cnt = x64::shift_count(sz, x64::loghead(8, x64::rr64(m, 1))); + let o = x64::shift_spec(sh, x64::nbits(sz), x64::rgfi_size(m, sz, r), cnt, m.rflags); + let s1 = x64::write_user_rflags(cu, m, o.rflags, o.undef); + lemma_wur_frame(cu, m, o.rflags, o.undef); + let s2 = x64::set_rgfi_size(s1, sz, r, o.result); + assert(s1.rgf == m.rgf); + assert(x64::set_rgfi_size(s1, sz, r, o.result).rgf == x64::set_rgfi_size( + m, + sz, + r, + o.result, + ).rgf); +} + +// ---------------------------------------------------------------------------- +// Values: 32-bit +// ---------------------------------------------------------------------------- + +/// `add`, `sub`, `and`, `or`, `xor` on 32-bit operands compute LIR's result. +pub proof fn lemma_val32_alu(o: lir::IBin, x: u32, y: u32, fl: u32) + requires + super::templates::alu_of(o) is Some, + ensures + x64::gpr_arith_logic_spec(32, super::templates::alu_of(o).unwrap(), x as int, y as int, fl).result + == lir::ibin32(o, x, y) as int, +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::loghead(32, x as int) == x as int); + assert(x64::loghead(32, y as int) == y as int); + match o { + lir::IBin::Add => { + assert(lir::ibin32(o, x, y) as int == (x as int + y as int) % 0x1_0000_0000); + }, + lir::IBin::Sub => { + assert(lir::ibin32(o, x, y) as int == (x as int - y as int) % 0x1_0000_0000); + let a = x64::logext(32, x as int); + let b = x64::logext(32, y as int); + assert((a - b) % 0x1_0000_0000 == (x as int - y as int) % 0x1_0000_0000); + }, + lir::IBin::And => { + assert((x as u64) & (y as u64) == (x & y) as u64) by (bit_vector); + }, + lir::IBin::Or => { + assert((x as u64) | (y as u64) == (x | y) as u64) by (bit_vector); + }, + lir::IBin::Xor => { + assert((x as u64) ^ (y as u64) == (x ^ y) as u64) by (bit_vector); + }, + _ => {}, + } +} + +/// `add`, `sub`, `and`, `or`, `xor` on 64-bit operands compute LIR's result. +pub proof fn lemma_val64_alu(o: lir::IBin, x: u64, y: u64, fl: u32) + requires + super::templates::alu_of(o) is Some, + ensures + x64::gpr_arith_logic_spec(64, super::templates::alu_of(o).unwrap(), x as int, y as int, fl).result + == lir::ibin64(o, x, y) as int, +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::loghead(64, x as int) == x as int); + assert(x64::loghead(64, y as int) == y as int); + match o { + lir::IBin::Add => { + assert(lir::ibin64(o, x, y) as int == (x as int + y as int) % 0x1_0000_0000_0000_0000); + }, + lir::IBin::Sub => { + assert(lir::ibin64(o, x, y) as int == (x as int - y as int) + % 0x1_0000_0000_0000_0000); + let a = x64::logext(64, x as int); + let b = x64::logext(64, y as int); + assert((a - b) % 0x1_0000_0000_0000_0000 == (x as int - y as int) + % 0x1_0000_0000_0000_0000); + }, + lir::IBin::And => { + assert((x as u64) & (y as u64) == (x & y) as u64) by (bit_vector); + }, + lir::IBin::Or => { + assert((x as u64) | (y as u64) == (x | y) as u64) by (bit_vector); + }, + lir::IBin::Xor => { + assert((x as u64) ^ (y as u64) == (x ^ y) as u64) by (bit_vector); + }, + _ => {}, + } +} + +/// `imul` on 32-bit operands is LIR's `mul`: the low 32 bits of the product. +pub proof fn lemma_val32_mul(x: u32, y: u32) + ensures + x64::imul_spec(32, y as int, x as int).0 == lir::ibin32(lir::IBin::Mul, x, y) as int, +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + let m: int = 0x1_0000_0000; + let a = x64::logext(32, x as int); + let b = x64::logext(32, y as int); + assert(x64::loghead(32, x as int) == x as int); + assert(x64::loghead(32, y as int) == y as int); + assert(a % m == x as int % m); + assert(b % m == y as int % m); + vstd::arithmetic::div_mod::lemma_mul_mod_noop(a, b, m); + vstd::arithmetic::div_mod::lemma_mul_mod_noop(x as int, y as int, m); + vstd::arithmetic::div_mod::lemma_mod_mod(a * b, m, m); + assert(x64::p2(64) == m * m); + assert(lir::ibin32(lir::IBin::Mul, x, y) as int == (x as int * y as int) % m); +} + +/// `imul` on 64-bit operands is LIR's `mul`: the low 64 bits of the product. +pub proof fn lemma_val64_mul(x: u64, y: u64) + ensures + x64::imul_spec(64, y as int, x as int).0 == lir::ibin64(lir::IBin::Mul, x, y) as int, +{ + super::memlemmas::lemma_p2_consts(); + let m: int = 0x1_0000_0000_0000_0000; + let a = x64::logext(64, x as int); + let b = x64::logext(64, y as int); + assert(x64::loghead(64, x as int) == x as int); + assert(x64::loghead(64, y as int) == y as int); + assert(a % m == x as int % m); + assert(b % m == y as int % m); + vstd::arithmetic::div_mod::lemma_mul_mod_noop(a, b, m); + vstd::arithmetic::div_mod::lemma_mul_mod_noop(x as int, y as int, m); + vstd::arithmetic::power2::lemma_pow2_adds(64, 64); + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::p2(128) == m * m); + vstd::arithmetic::div_mod::lemma_mod_mod(a * b, m, m); + assert(lir::ibin64(lir::IBin::Mul, x, y) as int == (x as int * y as int) % m); +} + +/// `shl`, `shr` and `sar` by `cl` on 32-bit operands compute LIR's shift: the +/// count is the low bits of `cl`, which is the count modulo the width. +#[verifier::rlimit(100)] +pub proof fn lemma_val32_shift(o: lir::IBin, x: u32, y: u32, fl: u32) + requires + super::templates::shift_of(o) is Some, + ensures + ({ + let cnt = x64::shift_count( + x64::Size::W32, + x64::loghead(8, y as int), + ); + x64::shift_spec(super::templates::shift_of(o).unwrap(), 32, x as int, cnt, fl).result == lir::ibin32(o, x, y) as int + }), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::p2(5) == 32 && x64::p2(6) == 64 && x64::p2(8) == 256); + let yi = y as int; + let cnt = x64::shift_count(x64::Size::W32, x64::loghead(8, yi)); + let k: nat = (yi % 32) as nat; + assert(cnt == yi % 32); + assert(x64::loghead(32, x as int) == x as int); + assert(x64::loghead(6, cnt) == cnt); + assert(cnt == k as int); + let p = x64::p2(k); + vstd::arithmetic::power2::lemma_pow2_pos(k); + assert(p == lir::pow2(k) as int); + assert(p > 0); + match o { + lir::IBin::Shl => { + assert(lir::ibin32(o, x, y) as int == (x as int * p) % 0x1_0000_0000); + }, + lir::IBin::ShrU => { + assert(x as int / p <= x as int) by (nonlinear_arith) + requires + p >= 1, + x as int >= 0, + ; + assert(0 <= x as int / p) by (nonlinear_arith) + requires + p >= 1, + x as int >= 0, + ; + assert(lir::ibin32(o, x, y) as int == x as int / p); + }, + lir::IBin::ShrS => { + let sg = lir::signed_of(32, x as int); + assert(x64::bit(x as int, 31) == (x as int / 0x8000_0000) % 2); + if x as int >= 0x8000_0000 { + assert(x64::logext(32, x as int) == x as int - 0x1_0000_0000); + assert(sg == x as int - 0x1_0000_0000); + } else { + assert(x64::logext(32, x as int) == x as int); + assert(sg == x as int); + assert(x as int / p <= x as int) by (nonlinear_arith) + requires + p >= 1, + x as int >= 0, + ; + assert(0 <= x as int / p) by (nonlinear_arith) + requires + p >= 1, + x as int >= 0, + ; + } + assert(lir::ibin32(o, x, y) as int == (sg / p) % 0x1_0000_0000); + }, + _ => {}, + } +} + +/// `shl`, `shr` and `sar` by `cl` on 64-bit operands compute LIR's shift: the +/// count is the low bits of `cl`, which is the count modulo the width. +#[verifier::rlimit(100)] +pub proof fn lemma_val64_shift(o: lir::IBin, x: u64, y: u64, fl: u32) + requires + super::templates::shift_of(o) is Some, + ensures + ({ + let cnt = x64::shift_count( + x64::Size::W64, + x64::loghead(8, y as int), + ); + x64::shift_spec(super::templates::shift_of(o).unwrap(), 64, x as int, cnt, fl).result == lir::ibin64(o, x, y) as int + }), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::p2(5) == 32 && x64::p2(6) == 64 && x64::p2(8) == 256); + let yi = y as int; + let cnt = x64::shift_count(x64::Size::W64, x64::loghead(8, yi)); + let k: nat = (yi % 64) as nat; + assert(cnt == yi % 64); + assert(x64::loghead(64, x as int) == x as int); + assert(x64::loghead(6, cnt) == cnt); + assert(cnt == k as int); + let p = x64::p2(k); + vstd::arithmetic::power2::lemma_pow2_pos(k); + assert(p == lir::pow2(k) as int); + assert(p > 0); + match o { + lir::IBin::Shl => { + assert(lir::ibin64(o, x, y) as int == (x as int * p) % 0x1_0000_0000_0000_0000); + }, + lir::IBin::ShrU => { + assert(x as int / p <= x as int) by (nonlinear_arith) + requires + p >= 1, + x as int >= 0, + ; + assert(0 <= x as int / p) by (nonlinear_arith) + requires + p >= 1, + x as int >= 0, + ; + assert(lir::ibin64(o, x, y) as int == x as int / p); + }, + lir::IBin::ShrS => { + let sg = lir::signed_of(64, x as int); + assert(x64::bit(x as int, 63) == (x as int / 0x8000_0000_0000_0000) % 2); + if x as int >= 0x8000_0000_0000_0000 { + assert(x64::logext(64, x as int) == x as int - 0x1_0000_0000_0000_0000); + assert(sg == x as int - 0x1_0000_0000_0000_0000); + } else { + assert(x64::logext(64, x as int) == x as int); + assert(sg == x as int); + assert(x as int / p <= x as int) by (nonlinear_arith) + requires + p >= 1, + x as int >= 0, + ; + assert(0 <= x as int / p) by (nonlinear_arith) + requires + p >= 1, + x as int >= 0, + ; + } + assert(lir::ibin64(o, x, y) as int == (sg / p) % 0x1_0000_0000_0000_0000); + }, + _ => {}, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/api.rs b/crates/clazel-jit/src/api.rs new file mode 100644 index 000000000..c89461f17 --- /dev/null +++ b/crates/clazel-jit/src/api.rs @@ -0,0 +1,47 @@ +//! The contract-only API of the back end: a trait with no implementation, so +//! Verus accepts it with no escape hatch (CLAUDE.md rule 4), the pattern of +//! `clazel-x64`'s and `clazel-codecheck`'s `api.rs`. The implementation (the +//! next commit) implements it, or replaces it with free functions whose +//! `requires` and `ensures` are the same predicates. +//! +//! The input is [`Op`]s, not `clazel_lir::spec::Instr`: LIR's own type holds +//! proof-only numbers and sequences, and the JS front end (J.10) will have its +//! own executable form. [`Op`]'s `view` says which LIR instruction each stands +//! for. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The LIR → x86-64 back end, with its contracts. +pub trait Backend { + /// The x86-64 instructions for the straight-line program `ops`, in a + /// function with `nl` locals, entered with an empty operand stack (see + /// [`crate::contract::emit_body_ensures`]). + fn emit_body(ops: &[Op], nl: usize) -> (r: Option>) + requires + crate::contract::emit_requires(ops@, nl), + ensures + crate::contract::emit_body_ensures(ops@, nl, match r { + Some(v) => Some(v@), + None => None, + }), + ; + + /// The bytes of a blob: the canonical encoding of the body, then the exit + /// sequence for `exit_code` (see [`crate::contract::emit_blob_ensures`]). + fn emit_blob(ops: &[Op], nl: usize, exit_code: u32) -> (r: Option>) + requires + crate::contract::emit_requires(ops@, nl), + ensures + crate::contract::emit_blob_ensures(ops@, nl, exit_code, match r { + Some(v) => Some(v@), + None => None, + }), + ; +} + +} // verus! diff --git a/crates/clazel-jit/src/cache.rs b/crates/clazel-jit/src/cache.rs new file mode 100644 index 000000000..25149c15a --- /dev/null +++ b/crates/clazel-jit/src/cache.rs @@ -0,0 +1,934 @@ +//! The code cache's data structure (docs/phases/jit.md item J.11, "bound the +//! code bytes per renderer"). +//! +//! A [`CodeCache`] holds the compiled blobs of one page, one per +//! [`Key`] (bytecode function id and version), under a fixed byte budget. +//! When a new blob does not fit, the least recently used blobs are evicted +//! until it does. +//! +//! # The statement (written first, CLAUDE.md rule 3) +//! +//! [`CodeCache::inv`] is the invariant, and every public operation takes it +//! and returns it ([`CodeCache::insert`], [`CodeCache::lookup`], +//! [`CodeCache::invalidate`], [`CodeCache::new`]): +//! +//! 1. **The bound.** `used <= budget <= MAX_BUDGET`, and `used` is exactly +//! the sum of the byte lengths of the resident entries (`total`). So the +//! code bytes of the resident blobs never exceed the budget, at any +//! point between two operations, whatever sequence of requests came. +//! 2. **The lengths are the regions'.** Each entry's `bytes` equals its +//! region's own length ([`Region::byte_len`]), so the sum in 1 is the sum +//! of what is mapped, not of what a caller claimed. +//! 3. **One blob per key.** No two resident entries have the same key, so a +//! lookup finds the only blob for a function version. +//! +//! And the three consequences the J.11 text asks for: +//! +//! - **An evicted entry is never entered.** `lookup` is the only way to get +//! at a resident region, and it returns a borrow of the cache's own entry +//! (so the borrow cannot outlive the next `insert` or `invalidate`). +//! Eviction moves the region out of the cache and into the returned +//! [`Evicted`] list, for the caller to unmap, and the postcondition of +//! `insert` says no resident key is the key of an evicted blob (other than +//! the key just inserted). After an eviction, `lookup` answers `None` for +//! that key, so the caller runs the interpreter ([`CodeCache::lookup`]'s +//! postcondition: `Some` exactly when the key is resident). +//! - **A rejected insert changes nothing** and hands the region back, with +//! the reason ([`Reject`]). +//! - **A function's blobs can all be dropped** ([`CodeCache::invalidate`]). +//! +//! # What this module is and is not +//! +//! Pure safe Rust, proven in Verus (no axiom, no assumption). It does not map +//! or enter anything. The real region type will be `clazel-sys`'s +//! `CodeRegion` (open PR #272, jit.md J.7); until it merges nothing here +//! depends on it. The cache is generic over [`Region`], whose only job is to +//! state a length; [`Mapped`] pairs any handle with its length, which is how +//! `CodeRegion` will fill it. The cache never reads or writes a region's +//! bytes, so it cannot make anything executable. +//! +//! The eviction *order* (least recently used first) is a policy, not part of +//! the proof: the vector is kept in recency order, with the front the least +//! recently used, and the proof does not depend on the order. + +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The largest budget a cache accepts (1 GiB). A larger request is clamped to +/// it, so that `used + bytes` cannot overflow a `usize` (at least 32 bits). +pub const MAX_BUDGET: usize = 0x4000_0000; + +/// What the cache needs to know of a compiled blob's memory: its length. +pub trait Region { + /// The blob's length in bytes. + spec fn spec_len(&self) -> nat; + + /// The same, as a number. + fn byte_len(&self) -> (n: usize) + ensures + n as nat == self.spec_len(), + ; +} + +/// Any handle (the future `clazel-sys` `CodeRegion`, a test stand-in) with the +/// length of the code in it. +pub struct Mapped { + /// The handle. Dropping it releases the memory. + pub handle: H, + /// The length of the code, in bytes. + pub len: usize, +} + +impl Region for Mapped { + open spec fn spec_len(&self) -> nat { + self.len as nat + } + + fn byte_len(&self) -> (n: usize) { + self.len + } +} + +/// A function version: the bytecode function's id and the version of its +/// bytecode (bumped when the function's code or its inline-cache shape +/// assumptions change). +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Key { + /// The bytecode function id. + pub func: u32, + /// The version of that function's code. + pub version: u32, +} + +/// One resident blob. +pub struct Entry { + /// The function version it was compiled from. + pub key: Key, + /// The memory holding it. + pub region: R, + /// Its length, equal to the region's own (invariant 2). + pub bytes: usize, +} + +/// Why a blob left the cache. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Why { + /// Evicted to make room for another blob. + Capacity, + /// Replaced by a new blob for the same key. + Replaced, + /// Dropped by [`CodeCache::invalidate`]. + Invalidated, +} + +/// A blob that left the cache, handed back to be unmapped. +pub struct Evicted { + /// The function version it was compiled from. + pub key: Key, + /// The memory. The cache no longer refers to it. + pub region: R, + /// Its length. + pub bytes: usize, + /// Why it left. + pub why: Why, +} + +/// Why an insert was refused. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Reject { + /// The blob is empty. + Empty, + /// The blob is longer than the per-function cap given to `insert`. + FunctionTooLarge, + /// The blob is longer than the whole budget. + ExceedsBudget, +} + +/// A refused insert: the reason, and the region handed back. +pub struct Rejected { + /// Why. + pub why: Reject, + /// The region that was offered. + pub region: R, +} + +/// The sum of the byte lengths of the entries. +pub open spec fn total(s: Seq>) -> nat + decreases s.len(), +{ + if s.len() == 0 { + 0 + } else { + s[0].bytes as nat + total(s.subrange(1, s.len() as int)) + } +} + +/// Appending an entry adds its length. +pub proof fn lemma_total_push(s: Seq>, e: Entry) + ensures + total(s.push(e)) == total(s) + e.bytes as nat, + decreases s.len(), +{ + reveal_with_fuel(total, 2); + if s.len() == 0 { + assert(s.push(e).subrange(1, 1) =~= Seq::>::empty()); + } else { + let t = s.subrange(1, s.len() as int); + lemma_total_push(t, e); + assert(s.push(e).subrange(1, s.len() as int + 1) =~= t.push(e)); + assert(s.push(e)[0] == s[0]); + } +} + +/// Removing an entry subtracts its length. +pub proof fn lemma_total_remove(s: Seq>, i: int) + requires + 0 <= i < s.len(), + ensures + total(s) == total(s.remove(i)) + s[i].bytes as nat, + decreases s.len(), +{ + reveal_with_fuel(total, 2); + s.remove_ensures(i); + if i == 0 { + assert(s.remove(0) =~= s.subrange(1, s.len() as int)); + } else { + let t = s.subrange(1, s.len() as int); + lemma_total_remove(t, i - 1); + t.remove_ensures(i - 1); + assert(s.remove(i).subrange(1, s.len() as int - 1) =~= t.remove(i - 1)); + assert(s.remove(i)[0] == s[0]); + assert(t[i - 1] == s[i]); + } +} + +/// Whether [`CodeCache::invalidate_but`] drops a blob with this key. +pub open spec fn drops(k: Key, func: u32, keep: Option) -> bool { + k.func == func && match keep { + None => true, + Some(v) => k.version != v, + } +} + +/// `a + b`, when it does not overflow. +fn add_u(a: usize, b: usize) -> (r: usize) + requires + a + b <= usize::MAX, + ensures + r == a + b, +{ + a.saturating_add(b) +} + +/// `a - b`, when it does not underflow. +fn sub_u(a: usize, b: usize) -> (r: usize) + requires + b <= a, + ensures + r == a - b, +{ + a.saturating_sub(b) +} + +/// The code cache of one page. See the module header for the invariant. +pub struct CodeCache { + /// Resident entries, least recently used first. + entries: Vec>, + /// The sum of the entries' lengths. + used: usize, + /// The byte budget. + budget: usize, +} + +impl CodeCache { + /// The invariant (module header, 1 to 3). + pub closed spec fn inv(&self) -> bool { + &&& self.budget <= MAX_BUDGET + &&& self.used <= self.budget + &&& self.used as nat == total(self.entries@) + &&& forall|i: int| + 0 <= i < self.entries@.len() ==> (#[trigger] self.entries@[i]).bytes as nat + == self.entries@[i].region.spec_len() + &&& forall|i: int, j: int| + 0 <= i < j < self.entries@.len() ==> (#[trigger] self.entries@[i]).key + != (#[trigger] self.entries@[j]).key + } + + /// The budget, as a spec. + pub closed spec fn spec_budget(&self) -> nat { + self.budget as nat + } + + /// The bytes in use, as a spec. + pub closed spec fn spec_used(&self) -> nat { + self.used as nat + } + + /// The resident entries, least recently used first. + pub closed spec fn spec_entries(&self) -> Seq> { + self.entries@ + } + + /// Some resident entry has this key. + pub closed spec fn has(&self, k: Key) -> bool { + exists|i: int| 0 <= i < self.entries@.len() && (#[trigger] self.entries@[i]).key == k + } + + /// **The budget invariant, as a theorem**: under `inv`, the bytes in use + /// never exceed the budget, which never exceeds [`MAX_BUDGET`]. + pub proof fn lemma_within_budget(&self) + requires + self.inv(), + ensures + self.spec_used() <= self.spec_budget(), + self.spec_budget() <= MAX_BUDGET as nat, + self.spec_used() == total(self.spec_entries()), + { + } + + /// An empty cache. A budget above [`MAX_BUDGET`] is clamped to it. + pub fn new(budget: usize) -> (c: Self) + ensures + c.inv(), + c.spec_used() == 0, + c.spec_budget() == if budget <= MAX_BUDGET { + budget as nat + } else { + MAX_BUDGET as nat + }, + forall|k: Key| !c.has(k), + { + let b = if budget < MAX_BUDGET { + budget + } else { + MAX_BUDGET + }; + let c = CodeCache { entries: Vec::new(), used: 0, budget: b }; + proof { + assert(c.entries@ =~= Seq::>::empty()); + } + c + } + + /// The bytes in use. + pub fn used(&self) -> (n: usize) + ensures + n as nat == self.spec_used(), + { + self.used + } + + /// The budget. + pub fn budget(&self) -> (n: usize) + ensures + n as nat == self.spec_budget(), + { + self.budget + } + + /// The number of resident entries. + pub fn len(&self) -> (n: usize) + ensures + n == self.spec_entries().len(), + { + self.entries.len() + } + + /// Whether the cache holds nothing. + pub fn is_empty(&self) -> (b: bool) + ensures + b == (self.spec_entries().len() == 0), + { + self.entries.len() == 0 + } + + /// Whether a blob for this key is resident. Does not change the order. + pub fn contains(&self, key: Key) -> (b: bool) + ensures + b == self.has(key), + { + self.find(key).is_some() + } + + /// The index of the entry with this key. + fn find(&self, key: Key) -> (r: Option) + ensures + match r { + Some(i) => i < self.entries@.len() && self.entries@[i as int].key == key, + None => !self.has(key), + }, + { + let mut i: usize = 0; + while i < self.entries.len() + invariant + i <= self.entries@.len(), + forall|j: int| 0 <= j < i ==> (#[trigger] self.entries@[j]).key != key, + decreases self.entries@.len() - i, + { + let hit = match self.entries.as_slice().get(i) { + Some(e) => e.key.func == key.func && e.key.version == key.version, + None => { + proof { + assert(false); + } + false + }, + }; + if hit { + return Some(i); + } + i = add_u(i, 1); + } + None + } + + /// Remove entry `i`, returning it. The bytes in use drop by its length. + fn remove_at(&mut self, i: usize) -> (e: Entry) + requires + old(self).inv(), + i < old(self).entries@.len(), + ensures + final(self).inv(), + e == old(self).entries@[i as int], + final(self).entries@ == old(self).entries@.remove(i as int), + final(self).budget == old(self).budget, + final(self).used as int == old(self).used as int - e.bytes as int, + e.bytes <= old(self).used, + { + proof { + lemma_total_remove(self.entries@, i as int); + } + let ghost before = self.entries@; + let e = self.entries.remove(i); + proof { + before.remove_ensures(i as int); + } + self.used = sub_u(self.used, e.bytes); + proof { + assert forall|k: int| 0 <= k < self.entries@.len() implies ( + #[trigger] self.entries@[k]).bytes as nat == self.entries@[k].region.spec_len() by { + if k < i as int { + assert(self.entries@[k] == before[k]); + } else { + assert(self.entries@[k] == before[k + 1]); + } + } + assert forall|a: int, b: int| 0 <= a < b < self.entries@.len() implies ( + #[trigger] self.entries@[a]).key != (#[trigger] self.entries@[b]).key by { + let ia = if a < i as int { + a + } else { + a + 1 + }; + let ib = if b < i as int { + b + } else { + b + 1 + }; + assert(self.entries@[a] == before[ia]); + assert(self.entries@[b] == before[ib]); + assert(ia < ib); + } + } + e + } + + /// Append `e`. The caller shows the bytes fit and the key is new. + fn push_back(&mut self, e: Entry) + requires + old(self).inv(), + e.bytes as nat == e.region.spec_len(), + old(self).used + e.bytes <= old(self).budget, + !old(self).has(e.key), + ensures + final(self).inv(), + final(self).entries@ == old(self).entries@.push(e), + final(self).budget == old(self).budget, + final(self).used == old(self).used + e.bytes, + { + proof { + lemma_total_push(self.entries@, e); + } + let ghost before = self.entries@; + let b = e.bytes; + let ghost key = e.key; + self.entries.push(e); + self.used = add_u(self.used, b); + proof { + assert forall|k: int| 0 <= k < self.entries@.len() implies ( + #[trigger] self.entries@[k]).bytes as nat == self.entries@[k].region.spec_len() by { + if k < before.len() { + assert(self.entries@[k] == before[k]); + } + } + assert forall|a: int, c: int| 0 <= a < c < self.entries@.len() implies ( + #[trigger] self.entries@[a]).key != (#[trigger] self.entries@[c]).key by { + if c < before.len() { + assert(self.entries@[a] == before[a]); + assert(self.entries@[c] == before[c]); + } else { + assert(self.entries@[a] == before[a]); + assert(self.entries@[c].key == key); + assert(before[a].key != key); + } + } + } + } + + /// Look a function version up for entry. A hit makes the entry the most + /// recently used and returns the cache's own borrow of its region, so + /// there is no way to hold a region the cache has given up. + /// + /// `Some` exactly when the key is resident; after an eviction it is + /// `None`, and the caller runs the interpreter. + pub fn lookup(&mut self, key: Key) -> (r: Option<&R>) + requires + old(self).inv(), + ensures + final(self).inv(), + final(self).spec_budget() == old(self).spec_budget(), + final(self).spec_used() == old(self).spec_used(), + r is Some <==> old(self).has(key), + match r { + Some(reg) => exists|i: int| + 0 <= i < final(self).spec_entries().len() && (#[trigger] final(self).spec_entries()[i]).key + == key && final(self).spec_entries()[i].region == *reg, + None => true, + }, + forall|k: Key| final(self).has(k) <==> old(self).has(k), + { + match self.find(key) { + None => None, + Some(i) => { + let e = self.remove_at(i); + proof { + assert(!self.has(e.key)) by { + let s = old(self).entries@; + s.remove_ensures(i as int); + assert forall|k: int| 0 <= k < self.entries@.len() implies ( + #[trigger] self.entries@[k]).key != e.key by { + let ik = if k < i as int { + k + } else { + k + 1 + }; + assert(self.entries@[k] == s[ik]); + } + } + } + let ghost removed = self.entries@; + self.push_back(e); + let n = self.entries.len(); + proof { + assert(self.entries@.last() == e); + assert forall|k: Key| self.has(k) <==> old(self).has(k) by { + if self.has(k) { + let j = choose|j: int| + 0 <= j < self.entries@.len() && (#[trigger] self.entries@[j]).key + == k; + if j == self.entries@.len() - 1 { + assert(old(self).entries@[i as int].key == k); + } else { + assert(removed[j] == old(self).entries@.remove(i as int)[j]); + let ij = if j < i as int { + j + } else { + j + 1 + }; + assert(old(self).entries@[ij].key == k); + } + } + if old(self).has(k) { + let j = choose|j: int| + 0 <= j < old(self).entries@.len() && ( + #[trigger] old(self).entries@[j]).key == k; + if j == i as int { + assert(self.entries@[self.entries@.len() - 1].key == k); + } else { + let nj = if j < i as int { + j + } else { + j - 1 + }; + assert(removed[nj] == old(self).entries@[j]); + assert(self.entries@[nj] == removed[nj]); + } + } + } + } + match self.entries.as_slice().get(sub_u(n, 1)) { + Some(en) => { + proof { + let last = self.entries@.len() - 1; + assert(*en == self.entries@[last]); + assert(self.entries@[last].key == key); + assert(self.entries@[last].region == en.region); + assert(0 <= last < self.spec_entries().len()); + assert(self.spec_entries()[last].key == key + && self.spec_entries()[last].region == en.region); + } + Some(&en.region) + }, + None => { + proof { + assert(false); + } + None + }, + } + }, + } + } + + /// Insert a compiled blob under `key`, evicting least recently used blobs + /// until it fits the budget. `max_fn` is the per-function size cap. + /// + /// - On `Ok`, the blob is resident (the most recently used), the bytes in + /// use are within the budget, and the returned list holds every blob + /// that left: the old blob for the same key (`Replaced`), and the + /// evicted ones (`Capacity`). The caller unmaps them. + /// - On `Err`, **the cache is unchanged** and the region comes back. + pub fn insert(&mut self, key: Key, region: R, max_fn: usize) -> (res: Result< + Vec>, + Rejected, + >) + requires + old(self).inv(), + ensures + final(self).inv(), + final(self).spec_budget() == old(self).spec_budget(), + match res { + Ok(ev) => { + &&& final(self).spec_entries().len() > 0 + &&& final(self).spec_entries().last().key == key + &&& final(self).spec_entries().last().region == region + &&& forall|j: int, i: int| + 0 <= j < ev@.len() && 0 <= i < final(self).spec_entries().len() && ( + #[trigger] final(self).spec_entries()[i]).key == (#[trigger] ev@[j]).key + ==> ev@[j].key == key + }, + Err(rej) => { + &&& final(self).spec_entries() == old(self).spec_entries() + &&& final(self).spec_used() == old(self).spec_used() + &&& rej.region == region + }, + }, + { + let bytes = region.byte_len(); + if bytes == 0 { + return Err(Rejected { why: Reject::Empty, region }); + } + if bytes > max_fn { + return Err(Rejected { why: Reject::FunctionTooLarge, region }); + } + if bytes > self.budget { + return Err(Rejected { why: Reject::ExceedsBudget, region }); + } + let mut ev: Vec> = Vec::new(); + if let Some(i) = self.find(key) { + let old_e = self.remove_at(i); + proof { + let s = old(self).entries@; + s.remove_ensures(i as int); + assert forall|k: int| 0 <= k < self.entries@.len() implies ( + #[trigger] self.entries@[k]).key != key by { + let ik = if k < i as int { + k + } else { + k + 1 + }; + assert(self.entries@[k] == s[ik]); + } + } + ev.push( + Evicted { key: old_e.key, region: old_e.region, bytes: old_e.bytes, why: Why::Replaced }, + ); + } + while add_u(self.used, bytes) > self.budget + invariant + self.inv(), + self.budget == old(self).budget, + 0 < bytes <= self.budget, + bytes as nat == region.spec_len(), + !self.has(key), + forall|j: int, i: int| + 0 <= j < ev@.len() && 0 <= i < self.entries@.len() ==> ( + #[trigger] self.entries@[i]).key != (#[trigger] ev@[j]).key, + decreases self.entries@.len(), + { + if self.entries.is_empty() { + proof { + // `used + bytes > budget >= bytes` forces `used > 0`, and + // `used == total(entries)`. + assert(self.entries@ =~= Seq::>::empty()); + assert(self.used == 0); + assert(false); + } + return Err(Rejected { why: Reject::ExceedsBudget, region }); + } + let ghost before = self.entries@; + let victim = self.remove_at(0); + proof { + before.remove_ensures(0); + assert forall|k: int| 0 <= k < self.entries@.len() implies ( + #[trigger] self.entries@[k]).key != victim.key by { + assert(self.entries@[k] == before[k + 1]); + } + assert forall|j: int, i: int| + 0 <= j < ev@.len() && 0 <= i < self.entries@.len() implies ( + #[trigger] self.entries@[i]).key != (#[trigger] ev@[j]).key by { + assert(self.entries@[i] == before[i + 1]); + } + assert(!self.has(key)) by { + assert forall|k: int| 0 <= k < self.entries@.len() implies ( + #[trigger] self.entries@[k]).key != key by { + assert(self.entries@[k] == before[k + 1]); + } + } + } + let ghost ev_before = ev@; + ev.push( + Evicted { key: victim.key, region: victim.region, bytes: victim.bytes, why: Why::Capacity }, + ); + proof { + assert forall|j: int, i: int| + 0 <= j < ev@.len() && 0 <= i < self.entries@.len() implies ( + #[trigger] self.entries@[i]).key != (#[trigger] ev@[j]).key by { + if j < ev_before.len() { + assert(ev@[j] == ev_before[j]); + } else { + assert(ev@[j].key == victim.key); + } + } + } + } + let ghost before = self.entries@; + self.push_back(Entry { key, region, bytes }); + proof { + assert(self.entries@.last().key == key); + assert forall|j: int, i: int| + 0 <= j < ev@.len() && 0 <= i < self.entries@.len() && ( + #[trigger] self.entries@[i]).key == (#[trigger] ev@[j]).key implies ev@[j].key + == key by { + if i < before.len() { + assert(self.entries@[i] == before[i]); + } + } + } + Ok(ev) + } + + /// Drop every blob of function `func` (all versions), returning them to + /// be unmapped. Afterwards no resident entry belongs to `func`. + pub fn invalidate(&mut self, func: u32) -> (ev: Vec>) + requires + old(self).inv(), + ensures + final(self).inv(), + final(self).spec_budget() == old(self).spec_budget(), + forall|i: int| + 0 <= i < final(self).spec_entries().len() ==> ( + #[trigger] final(self).spec_entries()[i]).key.func != func, + { + self.invalidate_but(func, None) + } + + /// Drop the blobs of function `func` whose version is not `keep` (all of + /// them if `keep` is `None`), returning them to be unmapped. Afterwards + /// every resident blob of `func` is of the version `keep`. + pub fn invalidate_but(&mut self, func: u32, keep: Option) -> (ev: Vec>) + requires + old(self).inv(), + ensures + final(self).inv(), + final(self).spec_budget() == old(self).spec_budget(), + forall|i: int| + 0 <= i < final(self).spec_entries().len() ==> !drops( + (#[trigger] final(self).spec_entries()[i]).key, + func, + keep, + ), + { + let mut ev: Vec> = Vec::new(); + let mut i: usize = self.entries.len(); + while i > 0 + invariant + self.inv(), + self.budget == old(self).budget, + i <= self.entries@.len(), + forall|j: int| + i <= j < self.entries@.len() ==> !drops( + (#[trigger] self.entries@[j]).key, + func, + keep, + ), + decreases i, + { + i = sub_u(i, 1); + let hit = match self.entries.as_slice().get(i) { + Some(e) => { + e.key.func == func && match keep { + None => true, + Some(v) => e.key.version != v, + } + }, + None => { + proof { + assert(false); + } + false + }, + }; + if hit { + let ghost before = self.entries@; + let e = self.remove_at(i); + proof { + before.remove_ensures(i as int); + assert forall|j: int| i <= j < self.entries@.len() implies !drops( + (#[trigger] self.entries@[j]).key, + func, + keep, + ) by { + assert(self.entries@[j] == before[j + 1]); + } + } + ev.push( + Evicted { key: e.key, region: e.region, bytes: e.bytes, why: Why::Invalidated }, + ); + } + } + ev + } + + /// The `i`th resident entry's key and length, least recently used first. + pub fn entry_at(&self, i: usize) -> (r: Option<(Key, usize)>) + ensures + match r { + Some((k, n)) => i < self.spec_entries().len() && self.spec_entries()[i as int].key + == k && self.spec_entries()[i as int].bytes == n, + None => i >= self.spec_entries().len(), + }, + { + if i >= self.entries.len() { + return None; + } + match self.entries.as_slice().get(i) { + Some(e) => Some((e.key, e.bytes)), + None => { + proof { + assert(false); + } + None + }, + } + } +} + +} // verus! + +#[cfg(test)] +mod tests { + use super::*; + + type C = CodeCache>; + type Gone = Vec>>; + + fn m(id: u32, len: usize) -> Mapped { + Mapped { handle: id, len } + } + + fn k(func: u32, version: u32) -> Key { + Key { func, version } + } + + fn inserted(r: Result>>) -> Gone { + match r { + Ok(v) => v, + Err(r) => unreachable!("rejected: {:?}", r.why), + } + } + + #[test] + fn budget_is_clamped() { + assert_eq!(C::new(usize::MAX).budget(), MAX_BUDGET); + assert_eq!(C::new(7).budget(), 7); + } + + #[test] + fn evicts_least_recently_used_and_never_enters_it() { + let mut c = C::new(100); + assert!(inserted(c.insert(k(1, 0), m(1, 40), 100)).is_empty()); + assert!(inserted(c.insert(k(2, 0), m(2, 40), 100)).is_empty()); + // Touch 1, so 2 is the oldest. + assert!(matches!(c.lookup(k(1, 0)), Some(r) if r.handle == 1)); + let gone = inserted(c.insert(k(3, 0), m(3, 40), 100)); + assert_eq!(gone.len(), 1); + let first = gone.first().map(|e| (e.key, e.why, e.bytes)); + assert_eq!(first, Some((k(2, 0), Why::Capacity, 40))); + assert!(c.lookup(k(2, 0)).is_none()); + assert!(!c.contains(k(2, 0))); + assert_eq!(c.used(), 80); + assert_eq!(c.len(), 2); + } + + #[test] + fn a_big_blob_evicts_several() { + let mut c = C::new(100); + for i in 0..4u32 { + let _ = inserted(c.insert(k(i, 0), m(i, 25), 100)); + } + assert_eq!(c.used(), 100); + let gone = inserted(c.insert(k(9, 0), m(9, 60), 100)); + assert_eq!(gone.len(), 3); + assert_eq!(c.used(), 85); + } + + #[test] + fn the_same_key_replaces() { + let mut c = C::new(100); + let _ = inserted(c.insert(k(1, 0), m(1, 30), 100)); + let gone = inserted(c.insert(k(1, 0), m(2, 50), 100)); + assert_eq!( + gone.first().map(|e| (e.why, e.region.handle)), + Some((Why::Replaced, 1)) + ); + assert_eq!((c.len(), c.used()), (1, 50)); + assert!(matches!(c.lookup(k(1, 0)), Some(r) if r.handle == 2)); + } + + #[test] + fn rejections_change_nothing_and_give_the_region_back() { + let mut c = C::new(100); + let _ = inserted(c.insert(k(1, 0), m(1, 60), 80)); + for (len, why) in [ + (0, Reject::Empty), + (81, Reject::FunctionTooLarge), + (101, Reject::FunctionTooLarge), + ] { + match c.insert(k(2, 0), m(2, len), 80) { + Err(r) => assert_eq!((r.why, r.region.len), (why, len)), + Ok(_) => unreachable!("accepted"), + } + assert_eq!((c.len(), c.used()), (1, 60)); + } + match c.insert(k(2, 0), m(2, 101), 1000) { + Err(r) => assert_eq!(r.why, Reject::ExceedsBudget), + Ok(_) => unreachable!("accepted"), + } + assert_eq!((c.len(), c.used()), (1, 60)); + } + + #[test] + fn invalidate_but_keeps_one_version() { + let mut c = C::new(1000); + for (f, v) in [(1, 0), (1, 1), (1, 2), (2, 0)] { + let _ = inserted(c.insert(k(f, v), m(f.saturating_mul(10).saturating_add(v), 10), 100)); + } + assert_eq!(c.invalidate_but(1, Some(1)).len(), 2); + assert!(c.contains(k(1, 1)) && c.contains(k(2, 0)) && !c.contains(k(1, 0))); + assert_eq!(c.invalidate(1).len(), 1); + assert_eq!((c.len(), c.used()), (1, 10)); + } +} diff --git a/crates/clazel-jit/src/cfaccept.rs b/crates/clazel-jit/src/cfaccept.rs new file mode 100644 index 000000000..bf7f49a17 --- /dev/null +++ b/crates/clazel-jit/src/cfaccept.rs @@ -0,0 +1,275 @@ +//! The templates as lists of the validator's units, and where a jump may land +//! (jit.md item J.9, "every emitted blob is accepted", x86-64). +//! +//! [`utmpl`] is [`ctmpl`] cut into the units the validator's scan sees: every +//! instruction is a unit of its own, except the bounds-check sequence of a +//! memory instruction (8 instructions) and every exit sequence (3). +//! [`lemma_utmpl_flat`]: the instructions of the units are `ctmpl`. +//! +//! [`bs`] says where a jump inside a node may land, as a byte position from +//! the node's start, other than at one of the labels it was given: the +//! node's own start and end, the starts and ends of its parts, and the stub +//! after a memory instruction (where the check's exits go). [`prot`] is where +//! no jump lands: in a memory instruction, the start of the bounds-check +//! sequence and the start of the access, because the scan forgets what it +//! knows at a jump target and the access needs what the load of the region's +//! base and the check told it. They are disjoint ([`lemma_bs_prot`]), and +//! every position of [`bs`] is a unit start or the end ([`lemma_bs_ubi`]). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::cfunits::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The units of a memory instruction: the operands and the base one +/// instruction at a time, the bounds-check sequence, the access, the `jmp` +/// over the stub, and the stub (an exit sequence). +pub open spec fn mem_units(m: MemOp, nl: nat, h: nat) -> Us { + ones(mem_pre(m, nl, h)) + seq![mem_check(m, nl, h)] + ones(mem_tail(m, nl, h)) + seq![ + seq![jmp_i(code_len(exit_seq(mem_exit_code(m))) as int)], + exit_seq(mem_exit_code(m)), + ] +} + +/// **[`ctmpl`] as units.** +pub open spec fn utmpl(n: Node, nl: nat, h: nat, offs: Seq) -> Us + decreases n, +{ + match n { + Node::Op(o) => ones(tmpl(o, nl, h)), + Node::Mem(m) => mem_units(m, nl, h), + Node::Then(a, b) => utmpl(*a, nl, h, offs) + utmpl( + *b, + nl, + flow_h(*a, nl, h, offs.len()), + shift(offs, csize(*a, nl, h, offs.len()) as int), + ), + Node::Block(b) => utmpl(*b, nl, h, seq![csize(*b, nl, h, offs.len() + 1) as int] + offs), + Node::Loop(b) => utmpl(*b, nl, h, seq![0int] + offs), + Node::If(t, e) => { + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, offs.len() + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, offs.len() + 1) as int; + ones(cond_pre(nl, h, 4, st + 5)) + utmpl( + *t, + nl, + (h - 1) as nat, + seq![st + 5 + se] + shift(offs, p), + ) + seq![seq![jmp_i(se)]] + utmpl( + *e, + nl, + (h - 1) as nat, + seq![se] + shift(offs, p + st + 5), + ) + }, + Node::Br(_) => ones(ctmpl(n, nl, h, offs)), + Node::BrIf(_) => ones(ctmpl(n, nl, h, offs)), + Node::BrTable(_, _) => ones(ctmpl(n, nl, h, offs)), + Node::Exit(c, _) => seq![exit_seq(c)], + } +} + +/// The byte offset of the stub of a memory instruction: after the operands, +/// the check, the access and the `jmp`. +pub open spec fn stub_off(m: MemOp, nl: nat, h: nat) -> int { + (code_len(mem_pre(m, nl, h)) + code_len(mem_check(m, nl, h)) + code_len(mem_tail(m, nl, h)) + + 5) as int +} + +/// **Where a jump of the node may land**, other than at a label it was +/// given, as a byte position from the node's first byte. +pub open spec fn bs(n: Node, nl: nat, h: nat, d: nat, x: int) -> bool + decreases n, +{ + match n { + Node::Mem(m) => x == 0 || x == stub_off(m, nl, h) || x == csize(n, nl, h, d) as int, + Node::Then(a, b) => bs(*a, nl, h, d, x) || bs( + *b, + nl, + flow_h(*a, nl, h, d), + d, + x - csize(*a, nl, h, d) as int, + ), + Node::Block(b) => bs(*b, nl, h, d + 1, x), + Node::Loop(b) => bs(*b, nl, h, d + 1, x), + Node::If(t, e) => { + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + x == 0 || bs(*t, nl, (h - 1) as nat, d + 1, x - p) || bs( + *e, + nl, + (h - 1) as nat, + d + 1, + x - (p + st + 5), + ) + }, + _ => x == 0 || x == csize(n, nl, h, d) as int, + } +} + +/// **Where no jump lands**: inside a memory instruction, the start of the +/// check and the start of the access. +pub open spec fn prot(n: Node, nl: nat, h: nat, d: nat, x: int) -> bool + decreases n, +{ + match n { + Node::Mem(m) => x == code_len(mem_pre(m, nl, h)) as int || x == (code_len( + mem_pre(m, nl, h), + ) + code_len(mem_check(m, nl, h))) as int, + Node::Then(a, b) => prot(*a, nl, h, d, x) || prot( + *b, + nl, + flow_h(*a, nl, h, d), + d, + x - csize(*a, nl, h, d) as int, + ), + Node::Block(b) => prot(*b, nl, h, d + 1, x), + Node::Loop(b) => prot(*b, nl, h, d + 1, x), + Node::If(t, e) => { + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + prot(*t, nl, (h - 1) as nat, d + 1, x - p) || prot( + *e, + nl, + (h - 1) as nat, + d + 1, + x - (p + st + 5), + ) + }, + _ => false, + } +} + +// ---------------------------------------------------------------------------- +// The units are the template +// ---------------------------------------------------------------------------- + +/// The instructions of a one-unit list. +pub proof fn lemma_uflat_one(u: Seq) + ensures + uflat(seq![u]) =~= u, +{ + let s = seq![u]; + assert(s.skip(1) =~= Seq::>::empty()); + assert(uflat(s) =~= u + uflat(s.skip(1))); + assert(u + Seq::::empty() =~= u); +} + +/// The instructions of a two-unit list. +pub proof fn lemma_uflat_two(u: Seq, v: Seq) + ensures + uflat(seq![u, v]) =~= u + v, +{ + let s = seq![u, v]; + assert(s.skip(1) =~= seq![v]); + lemma_uflat_one(v); + assert(uflat(s) =~= u + uflat(s.skip(1))); +} + +/// The units of a memory instruction are its code. +pub proof fn lemma_mem_units_flat(m: MemOp, nl: nat, h: nat) + ensures + uflat(mem_units(m, nl, h)) =~= mem_code(m, nl, h), +{ + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![jmp_i(code_len(exit_seq(mem_exit_code(m))) as int)]; + let stub = exit_seq(mem_exit_code(m)); + lemma_uflat_ones(pre); + lemma_uflat_ones(tail); + lemma_uflat_one(chk); + lemma_uflat_two(jm, stub); + lemma_uflat_cat(ones(pre), seq![chk]); + lemma_uflat_cat(ones(pre) + seq![chk], ones(tail)); + lemma_uflat_cat(ones(pre) + seq![chk] + ones(tail), seq![jm, stub]); + assert(uflat(mem_units(m, nl, h)) =~= pre + chk + tail + (jm + stub)); + assert(mem_code(m, nl, h) =~= pre + chk + tail + jm + stub); +} + +/// **The units of [`utmpl`] are the instructions of [`ctmpl`].** +pub proof fn lemma_utmpl_flat(n: Node, nl: nat, h: nat, offs: Seq) + ensures + uflat(utmpl(n, nl, h, offs)) =~= ctmpl(n, nl, h, offs), + decreases n, +{ + let d = offs.len(); + match n { + Node::Op(o) => { + lemma_uflat_ones(tmpl(o, nl, h)); + }, + Node::Mem(m) => { + lemma_mem_units_flat(m, nl, h); + }, + Node::Then(a, b) => { + let ob = shift(offs, csize(*a, nl, h, d) as int); + lemma_utmpl_flat(*a, nl, h, offs); + lemma_utmpl_flat(*b, nl, flow_h(*a, nl, h, d), ob); + lemma_uflat_cat( + utmpl(*a, nl, h, offs), + utmpl(*b, nl, flow_h(*a, nl, h, d), ob), + ); + }, + Node::Block(b) => { + lemma_utmpl_flat(*b, nl, h, seq![csize(*b, nl, h, d + 1) as int] + offs); + }, + Node::Loop(b) => { + lemma_utmpl_flat(*b, nl, h, seq![0int] + offs); + }, + Node::If(t, e) => { + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p); + let oe = seq![se] + shift(offs, p + st + 5); + let cp = cond_pre(nl, h, 4, st + 5); + lemma_utmpl_flat(*t, nl, (h - 1) as nat, ot); + lemma_utmpl_flat(*e, nl, (h - 1) as nat, oe); + lemma_uflat_ones(cp); + lemma_uflat_one(seq![jmp_i(se)]); + lemma_uflat_cat(ones(cp), utmpl(*t, nl, (h - 1) as nat, ot)); + lemma_uflat_cat(ones(cp) + utmpl(*t, nl, (h - 1) as nat, ot), seq![seq![jmp_i(se)]]); + lemma_uflat_cat( + ones(cp) + utmpl(*t, nl, (h - 1) as nat, ot) + seq![seq![jmp_i(se)]], + utmpl(*e, nl, (h - 1) as nat, oe), + ); + assert(uflat(utmpl(n, nl, h, offs)) =~= cp + ctmpl(*t, nl, (h - 1) as nat, ot) + seq![ + jmp_i(se), + ] + ctmpl(*e, nl, (h - 1) as nat, oe)); + }, + Node::Br(_) => { + lemma_uflat_ones(ctmpl(n, nl, h, offs)); + }, + Node::BrIf(_) => { + lemma_uflat_ones(ctmpl(n, nl, h, offs)); + }, + Node::BrTable(_, _) => { + lemma_uflat_ones(ctmpl(n, nl, h, offs)); + }, + Node::Exit(c, _) => { + lemma_uflat_one(exit_seq(c)); + }, + } +} + +/// The byte length of the units is `csize`. +pub proof fn lemma_utmpl_blen(n: Node, nl: nat, h: nat, offs: Seq) + ensures + blen(utmpl(n, nl, h, offs)) == csize(n, nl, h, offs.len()), +{ + lemma_utmpl_flat(n, nl, h, offs); + lemma_blen_flat(utmpl(n, nl, h, offs)); + crate::cflens::lemma_ctmpl_len(n, nl, h, offs); +} + +} // verus! diff --git a/crates/clazel-jit/src/cfalg.rs b/crates/clazel-jit/src/cfalg.rs new file mode 100644 index 000000000..8c075bc3d --- /dev/null +++ b/crates/clazel-jit/src/cfalg.rs @@ -0,0 +1,245 @@ +//! The algebra of the control-flow statements: [`preserved_mem_r`] is +//! transitive, it carries the native stack word and the region table, the +//! relation survives truncating the stack to a prefix, and a [`cf_refines`] +//! statement can be used at one state. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// [`preserved_r`] is [`preserved_mem_r`] with the stack pointer. +pub proof fn lemma_pr_mem(lay: lir::Layout, m: x64::State, m2: x64::State) + requires + preserved_r(lay, m, m2), + ensures + preserved_mem_r(lay, m, m2), +{ +} + +/// [`preserved_mem_r`] is transitive. +pub proof fn lemma_pmr_trans(lay: lir::Layout, m: x64::State, m1: x64::State, m2: x64::State) + requires + preserved_mem_r(lay, m, m1), + preserved_mem_r(lay, m1, m2), + ensures + preserved_mem_r(lay, m, m2), +{ + super::steps::lemma_abi_consts(); + assert(ctx_of(m1) == ctx_of(m)); + assert forall|b: int| + #[trigger] m.mem.dom().contains(b) && !in_exit_area(ctx_of(m), b) && !in_regions_i( + lay, + b, + ) implies m2.mem[b] == m.mem[b] by { + assert(m1.mem.dom().contains(b)); + assert(m1.mem[b] == m.mem[b]); + assert(m2.mem[b] == m1.mem[b]); + } +} + +/// [`preserved_r`] is transitive. +pub proof fn lemma_pr_trans(lay: lir::Layout, m: x64::State, m1: x64::State, m2: x64::State) + requires + preserved_r(lay, m, m1), + preserved_r(lay, m1, m2), + ensures + preserved_r(lay, m, m2), +{ + lemma_pmr_trans(lay, m, m1, m2); +} + +/// [`preserved_r`] then [`preserved_mem_r`] is [`preserved_mem_r`]. +pub proof fn lemma_pr_pmr_trans(lay: lir::Layout, m: x64::State, m1: x64::State, m2: x64::State) + requires + preserved_r(lay, m, m1), + preserved_mem_r(lay, m1, m2), + ensures + preserved_mem_r(lay, m, m2), +{ + lemma_pmr_trans(lay, m, m1, m2); +} + +/// A successful read of `n` bytes means each byte is mapped. +pub proof fn read_mem_some_dom(mm: Map, a: int, n: nat, i: int) + requires + x64::read_mem(mm, a, n) is Some, + 0 <= i < n, + ensures + mm.dom().contains(a + i), + decreases n, +{ + if i > 0 { + read_mem_some_dom(mm, a + 1, (n - 1) as nat, i - 1); + assert(a + 1 + (i - 1) == a + i); + } +} + +/// A read of mapped bytes that are outside the exit area and every region +/// gives the same result after a run that preserves everything else. +pub proof fn lemma_read_pres( + lay: lir::Layout, + m: x64::State, + m1: x64::State, + a: int, + n: nat, +) + requires + preserved_mem_r(lay, m, m1), + x64::read_mem(m.mem, a, n) is Some, + forall|i: int| 0 <= i < n ==> !in_exit_area(ctx_of(m), #[trigger] (a + i)) && !in_regions_i(lay, a + i), + ensures + x64::read_mem(m1.mem, a, n) == x64::read_mem(m.mem, a, n), +{ + assert forall|i: int| 0 <= i < n implies (m.mem.dom().contains(#[trigger] (a + i)) + == m1.mem.dom().contains(a + i)) && (m.mem.dom().contains(a + i) ==> m.mem[a + i] + == m1.mem[a + i]) by { + read_mem_some_dom(m.mem, a, n, i); + } + super::memlemmas::lemma_read_mem_congr(m.mem, m1.mem, a, n); +} + +/// **The native stack word survives.** After a run that preserves +/// everything outside the exit area and the regions and keeps `rsp`, the stack is +/// still as the exit sequence's `ret` needs it, and holds the same word. +pub proof fn lemma_stack_pres(lay: lir::Layout, m: x64::State, m1: x64::State) + requires + stack_ok_r(lay, m), + preserved_r(lay, m, m1), + ensures + stack_ok_r(lay, m1), + x64::read_mem(m1.mem, x64::logext(64, m.rgf[abi::sp_reg() as int] as int), 8) + == x64::read_mem(m.mem, x64::logext(64, m.rgf[abi::sp_reg() as int] as int), 8), +{ + super::steps::lemma_abi_consts(); + let rsp = x64::logext(64, m.rgf[abi::sp_reg() as int] as int); + assert(ctx_of(m1) == ctx_of(m)); + assert(m1.rgf[abi::sp_reg() as int] == m.rgf[abi::sp_reg() as int]); + assert(x64::read_mem(m.mem, rsp, 8) is Some); + assert forall|i: int| 0 <= i < 8 implies !in_exit_area(ctx_of(m), #[trigger] (rsp + i)) + && !in_regions_i(lay, rsp + i) by { + assert(!in_regions_i(lay, rsp + i)); + } + lemma_read_pres(lay, m, m1, rsp, 8); +} + +/// **The region table survives.** What is outside the exit area and every +/// region is unchanged, and the table is both. +pub proof fn lemma_table_pres(lay: lir::Layout, m: x64::State, m1: x64::State) + requires + table_ok(lay, m), + preserved_mem_r(lay, m, m1), + ensures + table_ok(lay, m1), +{ + super::steps::lemma_abi_consts(); + assert(ctx_of(m1) == ctx_of(m)); + assert forall|k: int| 0 <= k < lay.len() implies { + &&& x64::read_mem(m1.mem, ctx_of(m1) + abi::base_off(k), 8) == Some( + (#[trigger] lay[k]).base as int, + ) + &&& x64::read_mem(m1.mem, ctx_of(m1) + abi::bound_off(k), 8) == Some(lay[k].bound as int) + } by { + let a1 = ctx_of(m) + abi::base_off(k); + let a2 = ctx_of(m) + abi::bound_off(k); + assert(x64::read_mem(m.mem, a1, 8) == Some(lay[k].base as int)); + assert(x64::read_mem(m.mem, a2, 8) == Some(lay[k].bound as int)); + assert forall|i: int| 0 <= i < 8 implies !in_exit_area(ctx_of(m), #[trigger] (a1 + i)) + && !in_regions_i(lay, a1 + i) by { + assert(ctx_of(m) <= a1 + i < ctx_of(m) + abi::table_end()); + } + assert forall|i: int| 0 <= i < 8 implies !in_exit_area(ctx_of(m), #[trigger] (a2 + i)) + && !in_regions_i(lay, a2 + i) by { + assert(ctx_of(m) <= a2 + i < ctx_of(m) + abi::table_end()); + } + lemma_read_pres(lay, m, m1, a1, 8); + lemma_read_pres(lay, m, m1, a2, 8); + } + assert forall|b: int| ctx_of(m1) <= b < ctx_of(m1) + abi::table_end() implies !#[trigger] in_regions_i( + lay, + b, + ) by { + assert(ctx_of(m) <= b < ctx_of(m) + abi::table_end()); + } +} + +/// The relation survives truncating the stack to a prefix. +pub proof fn lemma_rel_prefix( + lay: lir::Layout, + a: lir::St, + full: Seq, + pre: Seq, + m: x64::State, + nl: nat, +) + requires + rel(lay, a.with_stack(full), m, nl), + pre.len() <= full.len(), + pre =~= full.take(pre.len() as int), + ensures + rel(lay, a.with_stack(pre), m, nl), +{ + let a1 = a.with_stack(full); + let a2 = a.with_stack(pre); + assert(super::rel::slots_agree(a1, a2, nl)) by { + assert forall|j: nat| j < nl + a2.stack.len() implies j < nl + a1.stack.len() + && #[trigger] lir::val_bits(slot_val(a2, nl, j)) == lir::val_bits(slot_val(a1, nl, j)) by { + if j >= nl { + assert(a2.stack[(j - nl) as int] == a1.stack[(j - nl) as int]); + } + } + } + super::rel::lemma_rel_agree(lay, a1, a2, m, nl); +} + +/// What [`rel_base`] says, unfolded (it is opaque elsewhere). +pub proof fn lemma_rel_base_unfold( + lay: lir::Layout, + base: Seq, + a: lir::St, + m: x64::State, + nl: nat, +) + requires + rel_base(lay, base, a, m, nl), + ensures + rel(lay, a.with_stack(base + a.stack), m, nl), + table_ok(lay, m), + x64::wf(m), + ctx_ok(m), +{ + reveal(rel_base); +} + +/// **A [`cf_refines`] statement, used at one state.** +pub proof fn lemma_use_cf( + i: lir::Instr, + nl: nat, + h: nat, + offs: Seq, + code: Seq, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, +) + requires + cf_refines(i, nl, h, offs, code), + base.len() + a.stack.len() == h, + rel_base(lay, base, a, m, nl), + stack_ok_r(lay, m), + cf_canon(m.rip, offs, code), + ensures + cf_post(lay, base, nl, offs, code, cu, m, lir::eval(lay, i, a, fuel)), +{ +} + +} // verus! diff --git a/crates/clazel-jit/src/cfblobacc.rs b/crates/clazel-jit/src/cfblobacc.rs new file mode 100644 index 000000000..27bdcfa90 --- /dev/null +++ b/crates/clazel-jit/src/cfblobacc.rs @@ -0,0 +1,310 @@ +//! **J.9: the back end never produces a blob the validator refuses**, for +//! x86-64 (jit.md item J.9): for every program with a translation, the +//! blob the control-flow back end emits ([`crate::cfemit::emit_cf_blob`], the +//! encoding of [`ctmpl`] and the exit sequence) is accepted by the +//! validator's spec ([`clazel_codecheck::spec::accepted`]), from the entry the +//! back end uses (byte 0): [`theorem_blob_accepted`]. +//! +//! # How it is proven +//! +//! - **Units** ([`crate::cfunits`]): if the instructions are the +//! concatenation of units of the three shapes the validator's scan knows +//! (an exit sequence, a bounds-check sequence, a single instruction that is +//! neither a `ret` nor a `jcc 2`), `scan` walks exactly those units +//! ([`lemma_scan_units`]). The templates are cut into units +//! ([`utmpl`]), and every unit is well formed +//! ([`crate::cfuacc2::lemma_utmpl_uok`]): a memory instruction's check is +//! `check_unit_at` ([`crate::cfuacc::lemma_check_unit`]), an exit sequence +//! is `exit_unit_at`. +//! - **Facts**: the scan accepts the units from any state of knowledge +//! ([`crate::cfuacc2::lemma_utmpl_ucheck`]): single instructions are +//! simple ([`crate::cfsimple`]: no reserved register, memory only the +//! context's frame), and the access of a `load` or `store` is admitted +//! because the base was loaded and the index checked just before it, +//! provided no jump lands on the start of the check or of the access +//! (`prot`). +//! - **Targets**: every jump of the code lands on a label it was given or on +//! a position `bs` ([`crate::cfjt::lemma_jt_bs`]); `bs` positions are unit +//! starts ([`crate::cfbs::lemma_bs_ubi`]), and never `prot` +//! ([`crate::cfbs::lemma_bs_facts`]). The only label of the whole function +//! is its return, the start of the exit sequence. +//! - **Ends**: the last instruction is the exit sequence's `ret`. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfaccept::*; +#[cfg(verus_only)] +use crate::cfbs::*; +#[cfg(verus_only)] +use crate::cfjt::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfproofs::labels_fit; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::cfuacc2::*; +#[cfg(verus_only)] +use crate::cfunits::*; +#[cfg(verus_only)] +use crate::cfvalid::*; +#[cfg(verus_only)] +use crate::runseq::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The bytes the back end emits for the program `n` with `nl` locals and the +/// exit code `c` (the function's return label is at the end of the body, where +/// the exit sequence starts): the encoding of [`ctmpl`] and the exit sequence. +pub open spec fn emitted_bytes(n: Node, nl: nat, c: u32) -> Seq { + clazel_codecheck::spec::enc_all( + ctmpl(n, nl, 0, seq![csize(n, nl, 0, 1) as int]) + exit_seq(c), + ) +} + +/// The instructions the back end emits. +pub open spec fn emitted_ins(n: Node, nl: nat, c: u32) -> Seq { + ctmpl(n, nl, 0, seq![csize(n, nl, 0, 1) as int]) + exit_seq(c) +} + +/// A list of numbers is a subset of another if each is in it. +proof fn lemma_subset_intro(a: Seq, b: Seq) + requires + forall|x: int| clazel_codecheck::spec::seq_has(a, x) ==> clazel_codecheck::spec::seq_has( + b, + x, + ), + ensures + clazel_codecheck::spec::subset_of(a, b), + decreases a.len(), +{ + if a.len() > 0 { + let d = a.drop_last(); + assert(clazel_codecheck::spec::seq_has(a, a.last())); + assert forall|x: int| clazel_codecheck::spec::seq_has(d, x) implies clazel_codecheck::spec::seq_has( + b, + x, + ) by { + assert(clazel_codecheck::spec::seq_has(a, x)); + } + lemma_subset_intro(d, b); + } +} + +/// The scan's jump targets are the targets `jt` names. +proof fn lemma_jump_targets_has(ins: Seq, j: int, x: int) + requires + 0 <= j, + ensures + clazel_codecheck::spec::seq_has(clazel_codecheck::spec::jump_targets(ins, j), x) == ( + exists|j2: int| + j <= j2 < ins.len() && #[trigger] clazel_codecheck::spec::jump_rel(ins[j2]) is Some + && x == clazel_codecheck::spec::start_of(ins, j2 + 1) + + clazel_codecheck::spec::jump_rel(ins[j2])->Some_0), + decreases ins.len() - j, +{ + if j >= ins.len() { + reveal_with_fuel(clazel_codecheck::spec::seq_has, 1); + } else { + lemma_jump_targets_has(ins, j + 1, x); + let rest = clazel_codecheck::spec::jump_targets(ins, j + 1); + match clazel_codecheck::spec::jump_rel(ins[j]) { + Some(r) => { + let here = seq![clazel_codecheck::spec::start_of(ins, j + 1) + r]; + clazel_codecheck::acc_lemmas::lemma_seq_has_concat(here, rest, x); + clazel_codecheck::acc_lemmas::lemma_seq_has_single( + clazel_codecheck::spec::start_of(ins, j + 1) + r, + x, + ); + assert(clazel_codecheck::spec::jump_targets(ins, j) =~= here + rest); + }, + None => { + let here = Seq::::empty(); + assert(clazel_codecheck::spec::jump_targets(ins, j) =~= rest); + }, + } + } +} + +/// The targets of the whole blob. +proof fn lemma_targets_has(ins: Seq, x: int) + ensures + clazel_codecheck::spec::seq_has( + clazel_codecheck::spec::all_targets(ins, seq![0int]), + x, + ) == (x == 0 || jt(ins, x)), +{ + lemma_jump_targets_has(ins, 0, x); + clazel_codecheck::acc_lemmas::lemma_seq_has_concat( + seq![0int], + clazel_codecheck::spec::jump_targets(ins, 0), + x, + ); + clazel_codecheck::acc_lemmas::lemma_seq_has_single(0, x); +} + +/// **The validator accepts the blob** the back end emits for a program with a +/// translation, from entry 0. +#[verifier::rlimit(1500)] +pub proof fn theorem_blob_accepted(n: Node, nl: nat, c: u32) + requires + cf_prog_ok(n, nl), + ensures + clazel_codecheck::spec::accepted(emitted_bytes(n, nl, c), seq![0int]), +{ + let size = csize(n, nl, 0, 1); + let offs = seq![size as int]; + assert(offs.len() == 1); + let body = ctmpl(n, nl, 0, offs); + let ins = body + exit_seq(c); + assert(labels_fit(offs, size)) by { + assert forall|l: int| 0 <= l < offs.len() implies -0x8000_0000 + size + 6 <= #[trigger] + offs[l] && offs[l] <= 0x7FFF_FFFF by { + assert(size <= max_size()); + } + } + lemma_ctmpl_valid(n, nl, 0, offs); + crate::tlemmas::lemma_exit_valid(c); + crate::cfvalid::lemma_valid_cat(body, exit_seq(c)); + assert(clazel_codecheck::spec::all_valid(ins)); + assert(clazel_codecheck::spec::decodes_to(emitted_bytes(n, nl, c), ins)); + // units + let ut = utmpl(n, nl, 0, offs); + let us = ut + seq![exit_seq(c)]; + lemma_utmpl_flat(n, nl, 0, offs); + lemma_uflat_one(exit_seq(c)); + lemma_uflat_cat(ut, seq![exit_seq(c)]); + assert(uflat(us) =~= ins); + lemma_utmpl_uok(n, nl, 0, offs); + lemma_exit_uok(c); + assert(uok_all(seq![exit_seq(c)])) by { + assert forall|k: int| 0 <= k < seq![exit_seq(c)].len() implies uok( + #[trigger] seq![exit_seq(c)][k], + ) by { + assert(k == 0); + } + } + lemma_uok_cat(ut, seq![exit_seq(c)]); + // the jump targets + let p = |x: int| x == 0 || jt(ins, x); + assert forall|x: int| clazel_codecheck::spec::seq_has( + clazel_codecheck::spec::all_targets(ins, seq![0int]), + x, + ) == p(x) by { + lemma_targets_has(ins, x); + } + lemma_bs_facts(n, nl, 0, 1); + lemma_bs_ubi(n, nl, 0, offs); + lemma_jt_bs(n, nl, 0, offs); + lemma_utmpl_blen(n, nl, 0, offs); + lemma_exit_seq_len(c); + // every jump target is a boundary of the body, and not protected + assert forall|x: int| jt(ins, x) implies (bs(n, nl, 0, 1, x) && 0 <= x <= size) by { + lemma_jt_cat(body, exit_seq(c), x); + if jt(exit_seq(c), x - code_len(body)) { + let e = exit_seq(c); + assert(forall|j: int| 0 <= j < e.len() ==> nojump_i(#[trigger] e[j])) by { + assert forall|j: int| 0 <= j < e.len() implies nojump_i(e[j]) by { + assert(j == 0 || j == 1 || j == 2); + } + } + lemma_jt_none(e, x - code_len(body)); + } + assert(jt(body, x)); + if inoffs(offs, x) { + assert(x == size as int); + } + } + // the scan + let fs = clazel_codecheck::spec::no_facts(); + assert(pf(n, nl, 0, 1, 0, p)) by { + assert forall|x: int| #[trigger] prot(n, nl, 0, 1, x) implies !p(0 + x) by { + assert(0 < x < size as int); + if jt(ins, x) { + assert(bs(n, nl, 0, 1, x)); + } + } + } + lemma_utmpl_ucheck(n, nl, 0, offs, p); + assert(ucheck(ut, 0, fs, p) is Some); + let f = ucheck(ut, 0, fs, p)->Some_0; + lemma_ucheck_cat(ut, seq![exit_seq(c)], 0, fs, p); + crate::cfuacc::lemma_ucheck_one(exit_seq(c), blen(ut) as int, f, p); + assert(exit_seq(c).len() == 3); + assert(ucheck(us, 0, fs, p) is Some); + let tg = clazel_codecheck::spec::all_targets(ins, seq![0int]); + lemma_scan_units(ins, tg, 0, us, 0, fs, p); + assert(clazel_codecheck::spec::start_of(ins, 0) == 0); + let starts = ustarts(us, 0); + assert(clazel_codecheck::spec::scan(ins, tg, 0, fs) == Some(starts)); + // the targets are unit starts + lemma_blen_cat(ut, seq![exit_seq(c)]); + lemma_blen_one(exit_seq(c)); + assert forall|x: int| clazel_codecheck::spec::seq_has(tg, x) implies clazel_codecheck::spec::seq_has( + starts, + x, + ) by { + lemma_seq_has_ustarts(us, 0, x); + assert(p(x)); + if x == 0 { + assert(us.len() > 0); + assert(ub(us, 0, 0)); + } else { + assert(jt(ins, x)); + assert(bs(n, nl, 0, 1, x)); + assert(ubi(ut, 0, 0 + x)); + lemma_ubi_cat(ut, seq![exit_seq(c)], 0, x); + assert(x <= size as int); + assert(blen(ut) == size); + assert(x < blen(us)); + assert(ub(us, 0, x)); + } + } + lemma_subset_intro(tg, starts); + assert(ins.last() is Ret) by { + assert(ins[ins.len() - 1] == exit_seq(c)[2]); + } + assert(ins.len() > 0); + assert(clazel_codecheck::spec::policy(ins, seq![0int])); +} + +/// **The emitter's blob is accepted**: what the emitter's postcondition says, +/// together with the exactness it also states, gives the validator's +/// acceptance. (`emit_cf_blob` carries this as its own postcondition.) +pub open spec fn emit_cf_blob_accepted_ensures( + n: Node, + nl: usize, + exit_code: u32, + r: Option>, +) -> bool { + match r { + Some(b) => clazel_codecheck::spec::accepted(b, seq![0int]), + None => true, + } +} + +/// From the emitter's two postconditions to the validator's acceptance. +pub proof fn lemma_emitter_accepted(n: Node, nl: usize, exit_code: u32, bytes: Seq) + requires + crate::cfcontract::emit_cf_blob_ensures(n, nl, exit_code, Some(bytes)), + bytes == emitted_bytes(n, nl as nat, exit_code), + ensures + emit_cf_blob_accepted_ensures(n, nl, exit_code, Some(bytes)), +{ + theorem_blob_accepted(n, nl as nat, exit_code); +} + +/// An instruction that is not a jump (spelled for [`jt`]'s own predicate). +pub open spec fn nojump_i(i: x64::Instr) -> bool { + clazel_codecheck::spec::jump_rel(i) is None +} + +} // verus! diff --git a/crates/clazel-jit/src/cfblock.rs b/crates/clazel-jit/src/cfblock.rs new file mode 100644 index 000000000..887500bd0 --- /dev/null +++ b/crates/clazel-jit/src/cfblock.rs @@ -0,0 +1,509 @@ +//! `block` and `loop`: a body's [`cf_post`], on top of the stack the block was +//! entered with, becomes the block's. Four transports (falling out of the +//! body, a branch to the block's own label, a branch past it, an exit), then +//! the two lemmas that use them. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfcompose::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cfheight::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::contract::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The body of a block, entered with the outer stack `ast` as its `base`, is +/// related to the same machine state as the block: `rel_base` with the +/// stacks concatenated. +pub proof fn lemma_rel_base_cat( + lay: lir::Layout, + base: Seq, + ast: Seq, + s: lir::St, + m: x64::State, + nl: nat, +) + requires + rel_base(lay, base + ast, s, m, nl), + ensures + rel_base(lay, base, s.with_stack(ast + s.stack), m, nl), +{ + reveal(rel_base); + assert((base + ast) + s.stack =~= base + (ast + s.stack)); + assert(s.with_stack(ast + s.stack).with_stack(base + (ast + s.stack)) == s.with_stack( + base + (ast + s.stack), + )); +} + +/// The block's start state, as the body's: the outer stack `a.stack` becomes +/// the body's `base`, and the body's own stack is empty. +pub proof fn lemma_rel_base_enter( + lay: lir::Layout, + base: Seq, + a: lir::St, + m: x64::State, + nl: nat, +) + requires + rel_base(lay, base, a, m, nl), + ensures + rel_base(lay, base + a.stack, fresh(a), m, nl), +{ + reveal(rel_base); + assert(base + a.stack + Seq::::empty() =~= base + a.stack); + assert(fresh(a).with_stack(base + a.stack + fresh(a).stack) == a.with_stack(base + a.stack)); +} + +/// Concatenation of stacks is associative inside [`rel_base`]. +pub proof fn lemma_rel_base_assoc( + lay: lir::Layout, + base: Seq, + x: Seq, + y: Seq, + s: lir::St, + m: x64::State, + nl: nat, +) + ensures + rel_base(lay, base + x + y, s, m, nl) == rel_base(lay, base + (x + y), s, m, nl), +{ + reveal(rel_base); + assert(base + x + y =~= base + (x + y)); +} + +/// **Falling out of the body.** The body's `Normal(s)` is the block's +/// `Normal(s)` on top of the outer stack. +pub proof fn lemma_blk_normal( + lay: lir::Layout, + base: Seq, + ast: Seq, + nl: nat, + offs: Seq, + c: Seq, + cu: spec_fn(nat) -> nat, + m: x64::State, + s: lir::St, + offs_b: Seq, +) + requires + cf_post(lay, base + ast, nl, offs_b, c, cu, m, lir::Outcome::Normal(s)), + ensures + cf_post(lay, base, nl, offs, c, cu, m, lir::Outcome::Normal(s.with_stack(ast + s.stack))), +{ + let m2 = choose|m2: x64::State| + reaches(cu, c, m.rip, m, m2) && m2.rip == m.rip + code_len(c) && rel_base( + lay, + base + ast, + s, + m2, + nl, + ) && preserved_r(lay, m, m2); + lemma_rel_base_cat(lay, base, ast, s, m2, nl); +} + +/// Seen from outside the block, the body's extra values include the block's +/// outer stack. +pub proof fn lemma_rel_ext_cat( + lay: lir::Layout, + base: Seq, + ast: Seq, + s: lir::St, + m2: x64::State, + nl: nat, +) + requires + rel_ext(lay, base + ast, s, m2, nl), + ensures + rel_ext(lay, base, s, m2, nl), +{ + let extra = choose|extra: Seq| rel_base(lay, base + ast + extra, s, m2, nl); + lemma_rel_base_assoc(lay, base, ast, extra, s, m2, nl); + let extra2 = ast + extra; + assert(rel_base(lay, base + extra2, s, m2, nl)); +} + +/// As [`lemma_rel_ext_cat`], for an exit's operands. +pub proof fn lemma_exit_rel_cat( + lay: lir::Layout, + base: Seq, + ast: Seq, + s: lir::St, + m2: x64::State, + nl: nat, + ctxb: int, + vals: Seq, +) + requires + exit_rel(lay, base + ast, s, m2, nl, ctxb, vals), + ensures + exit_rel(lay, base, s, m2, nl, ctxb, vals), +{ + let extra = choose|extra: Seq| + rel_base(lay, base + ast + extra, s, m2, nl) && ops_in_frame( + m2.mem, + ctxb, + nl, + (base + ast + extra).len() + s.stack.len(), + vals, + ); + lemma_rel_base_assoc(lay, base, ast, extra, s, m2, nl); + let extra2 = ast + extra; + assert(rel_base(lay, base + extra2, s, m2, nl)); + assert((base + extra2).len() == (base + ast + extra).len()); +} + +/// The relation part of a branch to the block's own label: with the body's +/// base extended by the outer stack and some extra values, the outer stack +/// alone is related. +pub proof fn lemma_br0_rel( + lay: lir::Layout, + base: Seq, + ast: Seq, + s: lir::St, + m2: x64::State, + nl: nat, +) + requires + rel_ext(lay, base + ast, s, m2, nl), + ensures + rel_base(lay, base, s.with_stack(ast), m2, nl), +{ + reveal(rel_base); + let extra = choose|extra: Seq| rel_base(lay, base + ast + extra, s, m2, nl); + let full = base + ast + extra + s.stack; + assert(rel(lay, s.with_stack(full), m2, nl)); + assert(full.take((base + ast).len() as int) =~= base + ast); + lemma_rel_prefix(lay, s, full, base + ast, m2, nl); + assert(s.with_stack(ast).with_stack(base + ast) == s.with_stack(base + ast)); +} + +/// **A branch to the block's own label** (label 0 of the body) is the block +/// ending: the state is related to the outer stack alone, at the end of the +/// block's code (`offs_b[0]` is its length). +pub proof fn lemma_blk_br0( + lay: lir::Layout, + base: Seq, + ast: Seq, + nl: nat, + offs: Seq, + c: Seq, + cu: spec_fn(nat) -> nat, + m: x64::State, + s: lir::St, + offs_b: Seq, +) + requires + offs_b.len() > 0, + offs_b[0] == code_len(c), + cf_post(lay, base + ast, nl, offs_b, c, cu, m, lir::Outcome::Branch(0, s)), + ensures + cf_post(lay, base, nl, offs, c, cu, m, lir::Outcome::Normal(s.with_stack(ast))), +{ + let m2 = choose|m2: x64::State| + reaches(cu, c, m.rip, m, m2) && m2.rip == m.rip + offs_b[0] && rel_ext( + lay, + base + ast, + s, + m2, + nl, + ) && preserved_r(lay, m, m2); + lemma_br0_rel(lay, base, ast, s, m2, nl); +} + +/// **A branch past the block** (label `n + 1` of the body is the block's +/// label `n`): still a branch, on top of more values. +pub proof fn lemma_blk_brn( + lay: lir::Layout, + base: Seq, + ast: Seq, + nl: nat, + offs: Seq, + c: Seq, + cu: spec_fn(nat) -> nat, + m: x64::State, + n: nat, + s: lir::St, + offs_b: Seq, +) + requires + n > 0, + n < offs_b.len(), + ((n - 1) as nat) < offs.len(), + offs_b[n as int] == offs[((n - 1) as nat) as int], + cf_post(lay, base + ast, nl, offs_b, c, cu, m, lir::Outcome::Branch(n, s)), + ensures + cf_post(lay, base, nl, offs, c, cu, m, lir::Outcome::Branch((n - 1) as nat, s)), +{ + let m2 = choose|m2: x64::State| + reaches(cu, c, m.rip, m, m2) && m2.rip == m.rip + offs_b[n as int] && rel_ext( + lay, + base + ast, + s, + m2, + nl, + ) && preserved_r(lay, m, m2); + lemma_rel_ext_cat(lay, base, ast, s, m2, nl); +} + +/// **An exit inside the body** is the block's exit, on top of more values. +pub proof fn lemma_blk_exit( + lay: lir::Layout, + base: Seq, + ast: Seq, + nl: nat, + offs: Seq, + c: Seq, + cu: spec_fn(nat) -> nat, + m: x64::State, + code: nat, + vals: Seq, + s: lir::St, + offs_b: Seq, +) + requires + cf_post(lay, base + ast, nl, offs_b, c, cu, m, lir::Outcome::Exit(code, vals, s)), + ensures + cf_post(lay, base, nl, offs, c, cu, m, lir::Outcome::Exit(code, vals, s)), +{ + let m2 = choose|m2: x64::State| + reaches(cu, c, m.rip, m, m2) && exit_done(m, m2, code) && exit_rel( + lay, + base + ast, + s, + m2, + nl, + ctx_of(m), + vals, + ) && preserved_mem_r(lay, m, m2); + lemma_exit_rel_cat(lay, base, ast, s, m2, nl, ctx_of(m), vals); +} + +/// **`block`.** If the body's code refines the body with the block's label +/// added at the end of the code, the code refines the `block`. +#[verifier::rlimit(400)] +pub proof fn lemma_block(b: Node, nl: nat, h: nat, offs: Seq, cb: Seq) + requires + cf_refines(b@, nl, h, seq![code_len(cb) as int] + offs, cb), + ensures + cf_refines(Node::Block(Box::new(b))@, nl, h, offs, cb), +{ + let t = Node::Block(Box::new(b)); + let offs_b = seq![code_len(cb) as int] + offs; + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, cb, cu, m, lir::eval(lay, t@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, cb) implies cf_post( + lay, + base, + nl, + offs, + cb, + cu, + m, + lir::eval(lay, t@, a, fuel), + ) by { + if fuel > 0 { + let f = (fuel - 1) as nat; + lemma_eval_block(lay, b, a, fuel); + lemma_rel_base_enter(lay, base, a, m, nl); + assert(cf_canon(m.rip, offs_b, cb)) by { + assert forall|l: int| 0 <= l < offs_b.len() implies x64::canonical( + m.rip + #[trigger] offs_b[l], + ) by { + if l > 0 { + assert(offs_b[l] == offs[l - 1]); + } else { + assert(offs_b[0] == code_len(cb) as int); + } + } + } + assert((base + a.stack).len() + fresh(a).stack.len() == h); + lemma_use_cf(b@, nl, h, offs_b, cb, lay, cu, base + a.stack, fresh(a), m, f); + let o = lir::eval(lay, b@, fresh(a), f); + match o { + lir::Outcome::Normal(s) => { + lemma_blk_normal(lay, base, a.stack, nl, offs, cb, cu, m, s, offs_b); + }, + lir::Outcome::Branch(n, s) => { + if n == 0 { + assert(lir::top(s.stack, 0) =~= Seq::::empty()); + assert(a.stack + lir::top(s.stack, 0) =~= a.stack); + lemma_blk_br0(lay, base, a.stack, nl, offs, cb, cu, m, s, offs_b); + } else { + assert(n < offs_b.len()); + assert(((n - 1) as nat) < offs.len()); + assert(offs_b[n as int] == offs[((n - 1) as nat) as int]); + lemma_blk_brn(lay, base, a.stack, nl, offs, cb, cu, m, n, s, offs_b); + } + }, + lir::Outcome::Exit(c, vals, s) => { + lemma_blk_exit(lay, base, a.stack, nl, offs, cb, cu, m, c, vals, s, offs_b); + }, + _ => {}, + } + } + } +} + +/// **`loop`, at one fuel.** By induction on the fuel: a branch to the loop's +/// label restarts it, from the state after the body, which is related to the +/// loop's own entry state with the outer stack alone. +#[verifier::rlimit(400)] +pub proof fn lemma_loop_fuel( + b: Node, + nl: nat, + h: nat, + offs: Seq, + cb: Seq, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, +) + requires + cf_refines(b@, nl, h, seq![0int] + offs, cb), + base.len() + a.stack.len() == h, + rel_base(lay, base, a, m, nl), + stack_ok_r(lay, m), + cf_canon(m.rip, offs, cb), + ensures + cf_post(lay, base, nl, offs, cb, cu, m, lir::eval(lay, Node::Loop(Box::new(b))@, a, fuel)), + decreases fuel, +{ + let t = Node::Loop(Box::new(b)); + let offs_b = seq![0int] + offs; + if fuel > 0 { + let f = (fuel - 1) as nat; + lemma_eval_loop(lay, b, a, fuel); + lemma_rel_base_enter(lay, base, a, m, nl); + lemma_rel_base_unfold(lay, base, a, m, nl); + assert(cf_canon(m.rip, offs_b, cb)) by { + assert forall|l: int| 0 <= l < offs_b.len() implies x64::canonical( + m.rip + #[trigger] offs_b[l], + ) by { + if l > 0 { + assert(offs_b[l] == offs[l - 1]); + } else { + assert(offs_b[0] == 0); + } + } + } + assert((base + a.stack).len() + fresh(a).stack.len() == h); + lemma_use_cf(b@, nl, h, offs_b, cb, lay, cu, base + a.stack, fresh(a), m, f); + let restart = Some(lir::Instr::Loop(void_bt(), Box::new(b@))); + let o = lir::eval(lay, b@, fresh(a), f); + match o { + lir::Outcome::Normal(s) => { + lemma_blk_normal(lay, base, a.stack, nl, offs, cb, cu, m, s, offs_b); + }, + lir::Outcome::Branch(n, s) => { + if n > 0 { + assert(n < offs_b.len()); + assert(((n - 1) as nat) < offs.len()); + assert(offs_b[n as int] == offs[((n - 1) as nat) as int]); + lemma_blk_brn(lay, base, a.stack, nl, offs, cb, cu, m, n, s, offs_b); + } else { + assert(lir::top(s.stack, 0) =~= Seq::::empty()); + assert(a.stack + lir::top(s.stack, 0) =~= a.stack); + if f > 0 { + // the machine is back at the start of the loop + let m1 = choose|m1: x64::State| + reaches(cu, cb, m.rip, m, m1) && m1.rip == m.rip + offs_b[0] + && rel_ext(lay, base + a.stack, s, m1, nl) + && preserved_r(lay, m, m1); + let extra = choose|extra: Seq| + rel_base(lay, base + a.stack + extra, s, m1, nl); + reveal(rel_base); + let full = base + a.stack + extra + s.stack; + assert(rel(lay, s.with_stack(full), m1, nl)); + assert(full.take((base + a.stack).len() as int) =~= base + a.stack); + lemma_rel_prefix(lay, s, full, base + a.stack, m1, nl); + let a1 = s.with_stack(a.stack); + assert(a1.with_stack(base + a1.stack) == s.with_stack(base + a.stack)); + lemma_table_pres(lay, m, m1); + assert(rel_base(lay, base, a1, m1, nl)); + lemma_stack_pres(lay, m, m1); + assert(m1.rip == m.rip); + assert(x64::canonical(m1.rip)); + lemma_loop_fuel(b, nl, h, offs, cb, lay, cu, base, a1, m1, (f - 1) as nat); + assert(Seq::::empty() + cb =~= cb); + assert(code_len(Seq::::empty()) == 0); + assert forall|l: int| 0 <= l < offs.len() implies #[trigger] offs[l] + + code_len(Seq::::empty()) == offs[l] by {} + lemma_lift_pre( + lay, + base, + nl, + offs, + offs, + Seq::empty(), + cb, + cu, + m, + m1, + lir::eval(lay, t@, a1, (f - 1) as nat), + ); + } + } + }, + lir::Outcome::Exit(c, vals, s) => { + lemma_blk_exit(lay, base, a.stack, nl, offs, cb, cu, m, c, vals, s, offs_b); + }, + _ => {}, + } + } +} + +/// **`loop`.** If the body's code refines the body with the loop's label +/// added at the start of the code, the code refines the `loop`. +pub proof fn lemma_loop(b: Node, nl: nat, h: nat, offs: Seq, cb: Seq) + requires + cf_refines(b@, nl, h, seq![0int] + offs, cb), + ensures + cf_refines(Node::Loop(Box::new(b))@, nl, h, offs, cb), +{ + let t = Node::Loop(Box::new(b)); + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, cb, cu, m, lir::eval(lay, t@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, cb) implies cf_post( + lay, + base, + nl, + offs, + cb, + cu, + m, + lir::eval(lay, t@, a, fuel), + ) by { + lemma_loop_fuel(b, nl, h, offs, cb, lay, cu, base, a, m, fuel); + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfbrt.rs b/crates/clazel-jit/src/cfbrt.rs new file mode 100644 index 000000000..cb8cc4bf0 --- /dev/null +++ b/crates/clazel-jit/src/cfbrt.rs @@ -0,0 +1,578 @@ +//! **`br_table` refines its LIR instruction** ([`brt_cf`]): the code is +//! [`crate::cftemplates::brt_code`], `mov rax, [index]`, a compare and `je` +//! per target, and a `jmp` to the default (the subset has no indirect +//! branch). The proof runs the chain by induction on the entry. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfcheck::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cfleaf::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfproofs::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The load of the index is 8 bytes, whatever the slot. +pub proof fn lemma_brt_p0(nl: nat, h: nat) + ensures + brt_p0(nl, h) == 8, +{ + lemma_mem_instr_lens((nl + h - 1) as nat, 0); + super::runseq::lemma_len1(load_slot(rax(), (nl + h - 1) as nat)); +} + +/// The chain from entry `k` is `13` bytes per entry left. +pub proof fn lemma_entries_len(ls: Seq, offs: Seq, p0: int, k: nat) + ensures + code_len(brt_entries(ls, offs, p0, k)) == if k >= ls.len() { + 0nat + } else { + (13 * (ls.len() - k)) as nat + }, + decreases ls.len() - k, +{ + if k < ls.len() { + let e = brt_entry(k, brt_rel(offs, ls[k as int], p0 + (k + 1) * brt_e())); + lemma_brt_entry_len(k, brt_rel(offs, ls[k as int], p0 + (k + 1) * brt_e())); + lemma_entries_len(ls, offs, p0, k + 1); + super::runseq::lemma_code_len_append(e, brt_entries(ls, offs, p0, k + 1)); + } else { + assert(brt_entries(ls, offs, p0, k) =~= Seq::::empty()); + } +} + +/// The three instructions of an entry are at offsets 0, 5 and 7. +pub proof fn lemma_entry_fetch(i: nat, rel: int) + ensures + fetch(brt_entry(i, rel), 0) == Some( + x64::Instr::MovImm32 { dst: rdx(), imm_u32: i as u32 }, + ), + fetch(brt_entry(i, rel), 5) == Some( + x64::Instr::AluRR { + alu: x64::AluOp::Cmp, + sz: x64::Size::W32, + dst: rax(), + src: rdx(), + }, + ), + fetch(brt_entry(i, rel), 7) == Some(jcc_i(4, rel)), + x64::instr_len(x64::Instr::MovImm32 { dst: rdx(), imm_u32: i as u32 }) == 5, + x64::instr_len( + x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: x64::Size::W32, dst: rax(), src: rdx() }, + ) == 2, +{ + lemma_jcc_len(4, rel); + lemma_le_bytes_len(i as u32 as int, 4); + let a = x64::Instr::MovImm32 { dst: rdx(), imm_u32: i as u32 }; + let b = x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: x64::Size::W32, dst: rax(), src: rdx() }; + let c = jcc_i(4, rel); + assert(x64::instr_len(a) == 5); + assert(x64::instr_len(b) == 2); + let e = brt_entry(i, rel); + assert(e.skip(1) =~= seq![b, c]); + assert(seq![b, c].skip(1) =~= seq![c]); + reveal_with_fuel(fetch, 4); +} + +/// Entry `i`'s instructions are at `13 (i - k) + o` in the chain from `k`. +pub proof fn lemma_entries_fetch(ls: Seq, offs: Seq, p0: int, k: nat, i: nat) + requires + k <= i < ls.len(), + ensures + ({ + let rel = brt_rel(offs, ls[i as int], p0 + (i + 1) * brt_e()); + let ch = brt_entries(ls, offs, p0, k); + &&& fetch(ch, (13 * (i - k) + 0) as int) == fetch(brt_entry(i, rel), 0) + &&& fetch(ch, (13 * (i - k) + 5) as int) == fetch(brt_entry(i, rel), 5) + &&& fetch(ch, (13 * (i - k) + 7) as int) == fetch(brt_entry(i, rel), 7) + }), + decreases i - k, +{ + let e = brt_entry(k, brt_rel(offs, ls[k as int], p0 + (k + 1) * brt_e())); + let rest = brt_entries(ls, offs, p0, k + 1); + lemma_brt_entry_len(k, brt_rel(offs, ls[k as int], p0 + (k + 1) * brt_e())); + if k == i { + lemma_entry_fetch(i, brt_rel(offs, ls[i as int], p0 + (i + 1) * brt_e())); + super::cfsteps::lemma_fetch_append_l(e, rest, 0); + super::cfsteps::lemma_fetch_append_l(e, rest, 5); + super::cfsteps::lemma_fetch_append_l(e, rest, 7); + } else { + lemma_entries_fetch(ls, offs, p0, k + 1, i); + super::cfsteps::lemma_fetch_append_r(e, rest, (13 * (i - k) + 0) as int); + super::cfsteps::lemma_fetch_append_r(e, rest, (13 * (i - k) + 5) as int); + super::cfsteps::lemma_fetch_append_r(e, rest, (13 * (i - k) + 7) as int); + } +} + +/// **The positions in the whole code**: entry `i`'s instructions at `8 + 13 i` +/// and `+ 5`, `+ 7`; the final `jmp` at `8 + 13 len`. +pub proof fn lemma_brt_fetch(ls: Seq, d: usize, nl: nat, h: nat, offs: Seq, i: nat) + requires + i < ls.len(), + ensures + ({ + let whole = brt_code(ls, d, nl, h, offs); + let rel = brt_rel(offs, ls[i as int], (8 + (i + 1) * 13) as int); + &&& fetch(whole, (8 + 13 * i + 0) as int) == fetch(brt_entry(i, rel), 0) + &&& fetch(whole, (8 + 13 * i + 5) as int) == fetch(brt_entry(i, rel), 5) + &&& fetch(whole, (8 + 13 * i + 7) as int) == fetch(brt_entry(i, rel), 7) + }), +{ + lemma_brt_p0(nl, h); + lemma_brt_entry_len(0, 0); + let p0 = brt_p0(nl, h); + let a = seq![load_slot(rax(), (nl + h - 1) as nat)]; + let ch = brt_entries(ls, offs, p0, 0); + let jm = seq![jmp_i(brt_rel(offs, d, p0 + ls.len() * brt_e() + 5))]; + let whole = brt_code(ls, d, nl, h, offs); + assert(whole =~= a + (ch + jm)); + lemma_entries_fetch(ls, offs, p0, 0, i); + let rel = brt_rel(offs, ls[i as int], p0 + (i + 1) * brt_e()); + super::cfsteps::lemma_fetch_append_r(a, ch + jm, (8 + 13 * i + 0) as int); + super::cfsteps::lemma_fetch_append_r(a, ch + jm, (8 + 13 * i + 5) as int); + super::cfsteps::lemma_fetch_append_r(a, ch + jm, (8 + 13 * i + 7) as int); + assert(fetch(brt_entry(i, rel), 0) is Some) by { + lemma_entry_fetch(i, rel); + } + assert(fetch(brt_entry(i, rel), 5) is Some) by { + lemma_entry_fetch(i, rel); + } + assert(fetch(brt_entry(i, rel), 7) is Some) by { + lemma_entry_fetch(i, rel); + } + super::cfsteps::lemma_fetch_append_l(ch, jm, (13 * i + 0) as int); + super::cfsteps::lemma_fetch_append_l(ch, jm, (13 * i + 5) as int); + super::cfsteps::lemma_fetch_append_l(ch, jm, (13 * i + 7) as int); +} + +/// The final `jmp` is at `8 + 13 len`. +pub proof fn lemma_brt_fetch_jmp(ls: Seq, d: usize, nl: nat, h: nat, offs: Seq) + ensures + fetch(brt_code(ls, d, nl, h, offs), (8 + 13 * ls.len()) as int) == Some( + jmp_i(brt_rel(offs, d, (8 + ls.len() * 13 + 5) as int)), + ), + code_len(brt_code(ls, d, nl, h, offs)) == 13 + 13 * ls.len(), +{ + lemma_brt_p0(nl, h); + lemma_brt_entry_len(0, 0); + let p0 = brt_p0(nl, h); + let a = seq![load_slot(rax(), (nl + h - 1) as nat)]; + let ch = brt_entries(ls, offs, p0, 0); + let jm = seq![jmp_i(brt_rel(offs, d, p0 + ls.len() * brt_e() + 5))]; + let whole = brt_code(ls, d, nl, h, offs); + assert(whole =~= a + (ch + jm)); + lemma_entries_len(ls, offs, p0, 0); + super::cfsteps::lemma_fetch_append_r(a, ch + jm, (8 + 13 * ls.len()) as int); + super::cfsteps::lemma_fetch_append_r(ch, jm, (13 * ls.len()) as int); + super::runseq::lemma_code_len_append(a, ch + jm); + super::runseq::lemma_code_len_append(ch, jm); + super::runseq::lemma_code_len_one(jmp_i(brt_rel(offs, d, p0 + ls.len() * brt_e() + 5))); + lemma_jmp_len(brt_rel(offs, d, p0 + ls.len() * brt_e() + 5)); + super::runseq::lemma_len1(load_slot(rax(), (nl + h - 1) as nat)); + assert(fetch(jm, 0) == Some(jm[0])); +} + +/// What the chain needs of the labels: in scope, within a 32-bit +/// displacement of every place in the code, canonical. +pub open spec fn brt_labels_ok( + ls: Seq, + d: usize, + offs: Seq, + rip0: int, +) -> bool { + d < offs.len() && (forall|q: int| 0 <= q < ls.len() ==> #[trigger] ls[q] < offs.len()) + && labels_fit(offs, 13 + 13 * ls.len()) && (forall|l: int| + 0 <= l < offs.len() ==> x64::canonical(rip0 + #[trigger] offs[l])) +} + +/// **The final `jmp`** of the chain, to the default label. +#[verifier::rlimit(300)] +pub proof fn lemma_brt_last( + cu: spec_fn(nat) -> nat, + ls: Seq, + d: usize, + nl: nat, + h: nat, + offs: Seq, + s: x64::State, + rip0: int, +) + requires + ls.len() <= max_targets(), + brt_labels_ok(ls, d, offs, rip0), + x64::wf(s), + s.rip == rip0 + 8 + 13 * ls.len(), + x64::canonical(rip0 + 13 + 13 * ls.len()), + x64::canonical(rip0), + ensures + ({ + let whole = brt_code(ls, d, nl, h, offs); + exists|me: x64::State| + #![trigger reaches(cu, whole, rip0, s, me)] + reaches(cu, whole, rip0, s, me) && x64::wf(me) && me.mem == s.mem && me.rgf.len() + == 16 && me.rgf[15] == s.rgf[15] && me.rgf[4] == s.rgf[4] && me.rip == rip0 + + offs[d as int] + }), +{ + let whole = brt_code(ls, d, nl, h, offs); + let n = ls.len(); + assert(reaches(cu, whole, rip0, s, s)) by { + assert(steps(cu, whole, rip0, s, 0) == Some(s)); + } + let end_all = rip0 + 13 + 13 * n; + lemma_brt_fetch_jmp(ls, d, nl, h, offs); + let rel = brt_rel(offs, d, (8 + n * 13 + 5) as int); + assert(rel == offs[d as int] - (8 + n * 13 + 5)); + assert(-0x8000_0000 <= rel < 0x8000_0000); + assert((rel as i32) as int == rel); + super::runseq::lemma_canonical_between(rip0, s.rip + 5, end_all); + super::runseq::lemma_canonical_between(rip0, s.rip, end_all); + assert(s.rip + 5 + rel == rip0 + offs[d as int]); + lemma_step_jmp(cu, s, rel); + let me = x64::step(cu, s, jmp_i(rel)).unwrap(); + assert(me == x64::State { rip: rip0 + offs[d as int], ..s }); + assert(x64::canonical(rip0 + offs[d as int])); + lemma_wf_like2(s, me); + lemma_reach_step(cu, whole, rip0, s, s, jmp_i(rel), me); +} + +/// **One entry of the chain**: `mov edx, i; cmp eax, edx; je`. The run +/// reaches the label of target `i` when `eax == i`, else the next entry. +#[verifier::rlimit(800)] +pub proof fn lemma_brt_entry( + cu: spec_fn(nat) -> nat, + ls: Seq, + d: usize, + nl: nat, + h: nat, + offs: Seq, + i: nat, + c: u32, + s: x64::State, + rip0: int, +) + requires + i < ls.len(), + ls.len() <= max_targets(), + c as nat >= i, + brt_labels_ok(ls, d, offs, rip0), + x64::wf(s), + s.rip == rip0 + 8 + 13 * i, + s.rgf[0] == c as u64, + x64::canonical(rip0 + 13 + 13 * ls.len()), + x64::canonical(rip0), + ensures + ({ + let whole = brt_code(ls, d, nl, h, offs); + exists|s3: x64::State| + #![trigger reaches(cu, whole, rip0, s, s3)] + reaches(cu, whole, rip0, s, s3) && x64::wf(s3) && s3.mem == s.mem && s3.rgf.len() + == 16 && s3.rgf[15] == s.rgf[15] && s3.rgf[4] == s.rgf[4] && s3.rgf[0] + == s.rgf[0] && s3.rip == (if c as nat == i { + rip0 + offs[ls[i as int] as int] + } else { + rip0 + 8 + 13 * (i + 1) + }) + }), +{ + super::memlemmas::lemma_p2_consts(); + let whole = brt_code(ls, d, nl, h, offs); + let n = ls.len(); + lemma_brt_entry_len(0, 0); + assert(reaches(cu, whole, rip0, s, s)) by { + assert(steps(cu, whole, rip0, s, 0) == Some(s)); + } + let end_all = rip0 + 13 + 13 * n; + lemma_brt_fetch(ls, d, nl, h, offs, i); + let li = ls[i as int]; + assert(li < offs.len()); + let rel = brt_rel(offs, li, (8 + (i + 1) * 13) as int); + assert(rel == offs[li as int] - ((8 + (i + 1) * 13) as int)); + assert(-0x8000_0000 <= rel < 0x8000_0000); + assert((rel as i32) as int == rel); + lemma_entry_fetch(i, rel); + let e0 = x64::Instr::MovImm32 { dst: rdx(), imm_u32: i as u32 }; + let e1 = x64::Instr::AluRR { + alu: x64::AluOp::Cmp, + sz: x64::Size::W32, + dst: rax(), + src: rdx(), + }; + let e2 = jcc_i(4, rel); + assert(x64::instr_len(e0) == 5 && x64::instr_len(e1) == 2); + lemma_jcc_len(4, rel); + super::runseq::lemma_canonical_between(rip0, s.rip + 5, end_all); + super::runseq::lemma_canonical_between(rip0, s.rip + 7, end_all); + super::runseq::lemma_canonical_between(rip0, s.rip + 13, end_all); + assert(i < 0x10000); + super::steps::lemma_step_imm(cu, s, e0); + let s1 = x64::step(cu, s, e0).unwrap(); + assert(s1.rgf == s.rgf.update(2, i as u64)); + assert(s1.mem == s.mem && s1.xmm == s.xmm && s1.rip == s.rip + 5); + lemma_wf_like2(s, s1); + lemma_reach_step(cu, whole, rip0, s, s, e0, s1); + super::flags::lemma_step_cmp(cu, s1, x64::Size::W32, 0, 2); + let s2 = x64::step(cu, s1, e1).unwrap(); + assert(s2.rgf == s1.rgf && s2.mem == s1.mem && s2.xmm == s1.xmm); + assert(s2.rip == s1.rip + 2); + lemma_wf_like2(s, s2); + lemma_reach_step(cu, whole, rip0, s, s1, e1, s2); + assert(x64::rgfi_size(s1, x64::Size::W32, 0) == c as int) by { + assert(x64::loghead(32, c as int) == c as int); + } + assert(x64::rgfi_size(s1, x64::Size::W32, 2) == i as int) by { + assert(x64::loghead(32, i as int) == i as int); + } + super::flags::lemma_cc_icmp(s2, lir::ICmp::Eq, 32, c as int, i as int); + assert(super::templates::cc_of(lir::ICmp::Eq) == 4); + assert(x64::canonical(rip0 + offs[li as int])); + assert(x64::canonical(s2.rip + 6 + rel)); + lemma_step_jcc(cu, s2, 4, rel); + let s3 = x64::step(cu, s2, e2).unwrap(); + assert(x64::canonical(s3.rip)); + lemma_wf_like2(s, s3); + lemma_reach_step(cu, whole, rip0, s, s2, e2, s3); + assert(s3.rgf == s1.rgf); +} + +/// **The chain, run from entry `i`**, with `eax` holding `c >= i`: it reaches +/// the label the `br_table` selects, with memory and the context register +/// and stack pointer as they were. +#[verifier::rlimit(400)] +pub proof fn lemma_brt_chain( + cu: spec_fn(nat) -> nat, + ls: Seq, + d: usize, + nl: nat, + h: nat, + offs: Seq, + i: nat, + c: u32, + s: x64::State, + rip0: int, +) + requires + i <= ls.len(), + ls.len() <= max_targets(), + c as nat >= i, + brt_labels_ok(ls, d, offs, rip0), + x64::wf(s), + s.rip == rip0 + 8 + 13 * i, + s.rgf[0] == c as u64, + x64::canonical(rip0 + 13 + 13 * ls.len()), + x64::canonical(rip0), + ensures + ({ + let whole = brt_code(ls, d, nl, h, offs); + exists|me: x64::State| + #![trigger reaches(cu, whole, rip0, s, me)] + reaches(cu, whole, rip0, s, me) && x64::wf(me) && me.mem == s.mem && me.rgf.len() + == 16 && me.rgf[15] == s.rgf[15] && me.rgf[4] == s.rgf[4] && me.rip == rip0 + ( + if (c as nat) < ls.len() { + offs[ls[c as int] as int] + } else { + offs[d as int] + }) + }), + decreases ls.len() - i, +{ + let whole = brt_code(ls, d, nl, h, offs); + let n = ls.len(); + if i == n { + lemma_brt_last(cu, ls, d, nl, h, offs, s, rip0); + assert(c as nat >= n); + } else { + lemma_brt_entry(cu, ls, d, nl, h, offs, i, c, s, rip0); + let s3 = choose|s3: x64::State| + reaches(cu, whole, rip0, s, s3) && x64::wf(s3) && s3.mem == s.mem && s3.rgf.len() + == 16 && s3.rgf[15] == s.rgf[15] && s3.rgf[4] == s.rgf[4] && s3.rgf[0] + == s.rgf[0] && s3.rip == (if c as nat == i { + rip0 + offs[ls[i as int] as int] + } else { + rip0 + 8 + 13 * (i + 1) + }); + if c as nat == i { + assert((c as nat) < n); + } else { + lemma_brt_chain(cu, ls, d, nl, h, offs, i + 1, c, s3, rip0); + let me = choose|me: x64::State| + reaches(cu, whole, rip0, s3, me) && x64::wf(me) && me.mem == s3.mem + && me.rgf.len() == 16 && me.rgf[15] == s3.rgf[15] && me.rgf[4] == s3.rgf[4] + && me.rip == rip0 + (if (c as nat) < n { + offs[ls[c as int] as int] + } else { + offs[d as int] + }); + lemma_reaches_trans(cu, whole, rip0, s, s3, me); + } + } +} + +/// A state with the memory and 16 + 16 registers of a well-formed one, and a +/// canonical `rip`, is well formed (the second form, from a state `s`). +pub proof fn lemma_wf_like2(s: x64::State, t: x64::State) + requires + x64::wf(s), + t.mem == s.mem, + t.rgf.len() == 16, + t.xmm.len() == 16, + x64::canonical(t.rip), + ensures + x64::wf(t), +{ +} + +/// **`br_table ls d`.** +#[verifier::rlimit(1500)] +pub proof fn lemma_brt_cf(ls: Vec, d: usize, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + ls@.len() <= max_targets(), + d < offs.len(), + forall|q: int| 0 <= q < ls@.len() ==> #[trigger] ls@[q] < offs.len(), + labels_fit(offs, 13 + 13 * ls@.len()), + ensures + cf_refines( + Node::BrTable(ls, d)@, + nl, + h, + offs, + brt_code(ls@, d, nl, h, offs), + ), +{ + let lsq = ls@; + let code = brt_code(lsq, d, nl, h, offs); + lemma_brt_fetch_jmp(lsq, d, nl, h, offs); + lemma_brt_p0(nl, h); + let j = (nl + h - 1) as nat; + let i0 = load_slot(rax(), j); + assert(code[0] == i0) by { + assert(code =~= seq![i0] + (brt_entries(lsq, offs, brt_p0(nl, h), 0) + seq![jmp_i( + brt_rel(offs, d, brt_p0(nl, h) + lsq.len() * brt_e() + 5), + )])); + } + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, cu, m, lir::eval(lay, Node::BrTable(ls, d)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, code) implies cf_post( + lay, + base, + nl, + offs, + code, + cu, + m, + lir::eval(lay, Node::BrTable(ls, d)@, a, fuel), + ) by { + let n0 = a.stack.len(); + if fuel > 0 && n0 >= 1 { + match a.stack[(n0 - 1) as int] { + lir::Val::I32(c) => { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + assert(ac.stack[(h - 1) as int] == lir::Val::I32(c)); + super::steps::lemma_abi_consts(); + let big = code_len(code); + assert(big == 13 + 13 * lsq.len()); + assert(x64::canonical(m.rip + big)); + assert(brt_labels_ok(lsq, d, offs, m.rip)) by { + assert forall|l: int| 0 <= l < offs.len() implies x64::canonical( + m.rip + #[trigger] offs[l], + ) by {} + } + super::runseq::lemma_canonical_between(m.rip, m.rip + 8, m.rip + big); + super::rel::lemma_load_rel(cu, lay, ac, m, 0, j, nl); + let m1 = x64::step(cu, m, i0).unwrap(); + lemma_mem_instr_lens(j, 0); + assert(m1.rgf[0] == c as u64); + assert(fetch(code, 0) == Some(i0)); + assert(reaches(cu, code, m.rip, m, m)) by { + assert(steps(cu, code, m.rip, m, 0) == Some(m)); + } + lemma_reach_step(cu, code, m.rip, m, m, i0, m1); + assert(m1.rip == m.rip + 8); + lemma_brt_chain(cu, lsq, d, nl, h, offs, 0, c, m1, m.rip); + let me = choose|q: x64::State| + reaches(cu, code, m.rip, m1, q) && x64::wf(q) && q.mem == m1.mem + && q.rgf.len() == 16 && q.rgf[15] == m1.rgf[15] && q.rgf[4] + == m1.rgf[4] && q.rip == m.rip + (if (c as nat) < lsq.len() { + offs[lsq[c as int] as int] + } else { + offs[d as int] + }); + lemma_reaches_trans(cu, code, m.rip, m, m1, me); + let l: nat = if (c as nat) < lsq.len() { + lsq[c as int] as nat + } else { + d as nat + }; + let mapped = lsq.map_values(|x: usize| x as nat); + assert(mapped.len() == lsq.len()); + assert(lir::eval(lay, Node::BrTable(ls, d)@, a, fuel) == lir::Outcome::Branch( + l, + a.with_stack(lir::below(a.stack, 1)), + )) by { + if (c as nat) < lsq.len() { + assert(mapped[c as int] == lsq[c as int] as nat); + } + } + assert(l < offs.len()); + assert(me.rip == m.rip + offs[l as int]); + assert(me.xmm.len() == 16); + super::rel::lemma_rel_regs(lay, ac, m1, me, nl); + lemma_preserved_same(lay, m, me); + lemma_table_pres(lay, m, me); + let s = a.with_stack(lir::below(a.stack, 1)); + assert((base + a.stack).take((base + lir::below(a.stack, 1)).len() as int) + =~= base + lir::below(a.stack, 1)); + lemma_rel_prefix( + lay, + a, + base + a.stack, + base + lir::below(a.stack, 1), + me, + nl, + ); + assert(s.with_stack(base + s.stack) == a.with_stack( + base + lir::below(a.stack, 1), + )); + reveal(rel_base); + assert(rel_base(lay, base, s, me, nl)); + assert(base + Seq::::empty() =~= base); + assert(rel_ext(lay, base, s, me, nl)); + }, + _ => {}, + } + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfbs.rs b/crates/clazel-jit/src/cfbs.rs new file mode 100644 index 000000000..0473a48e1 --- /dev/null +++ b/crates/clazel-jit/src/cfbs.rs @@ -0,0 +1,612 @@ +//! The positions a jump may and may not land on ([`bs`] and [`prot`] of +//! [`crate::cfaccept`]): they are inside the node, disjoint, and the first +//! are unit starts (jit.md item J.9, "every emitted blob is accepted", +//! x86-64). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfaccept::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::cfunits::*; +#[cfg(verus_only)] +use crate::runseq::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A memory instruction's layout: operands (8 to 24 bytes), the check (47), +/// the access, the `jmp` (5) and the stub (14). +pub proof fn lemma_mem_csize(m: MemOp, nl: nat, h: nat, d: nat) + requires + mem_region(m) < 16, + ensures + csize(Node::Mem(m), nl, h, d) == stub_off(m, nl, h) + 14, + code_len(mem_pre(m, nl, h)) >= 8, + code_len(mem_check(m, nl, h)) == 47, + code_len(mem_tail(m, nl, h)) >= 0, + stub_off(m, nl, h) == code_len(mem_pre(m, nl, h)) + 47 + code_len(mem_tail(m, nl, h)) + 5, +{ + lemma_mem_parts_len(m, nl, h); + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![jmp_i(code_len(exit_seq(mem_exit_code(m))) as int)]; + let stub = exit_seq(mem_exit_code(m)); + crate::cflens::lemma_jmp_len(code_len(stub) as int); + lemma_code_len_one(jmp_i(code_len(stub) as int)); + lemma_code_len_append(pre, chk); + lemma_code_len_append(pre + chk, tail); + lemma_code_len_append(pre + chk + tail, jm); + lemma_code_len_append(pre + chk + tail + jm, stub); + assert(csize(Node::Mem(m), nl, h, d) == code_len(mem_code(m, nl, h))); +} + +/// A `nop` has no code. +pub proof fn lemma_nop_size(nl: nat, h: nat, d: nat) + ensures + csize(Node::Op(Op::Nop), nl, h, d) == 0, +{ + assert(tmpl(Op::Nop, nl, h) =~= Seq::::empty()); +} + +/// What [`lemma_bs_facts`] says about a node. +pub open spec fn bs_facts(n: Node, nl: nat, h: nat, d: nat) -> bool { + bs(n, nl, h, d, 0) && bs(n, nl, h, d, csize(n, nl, h, d) as int) && (forall|x: int| + #[trigger] bs(n, nl, h, d, x) ==> 0 <= x <= csize(n, nl, h, d) as int) && (forall|x: int| + #[trigger] prot(n, nl, h, d, x) ==> 0 < x < csize(n, nl, h, d) as int) && (forall|x: int| + !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x))) +} + +/// `then`, from the facts of its parts: the second part starts `sa` bytes in. +proof fn lemma_bs_then( + a: Node, + b: Node, + nl: nat, + h: nat, + d: nat, + h1: nat, + sb: nat, +) + requires + h1 == flow_h(a, nl, h, d), + csize(Node::Then(Box::new(a), Box::new(b)), nl, h, d) == csize(a, nl, h, d) + sb, + bs_facts(a, nl, h, d), + bs(b, nl, h1, d, 0), + bs(b, nl, h1, d, sb as int), + forall|x: int| #[trigger] bs(b, nl, h1, d, x) ==> 0 <= x <= sb as int, + forall|x: int| #[trigger] prot(b, nl, h1, d, x) ==> 0 < x < sb as int, + forall|x: int| !(#[trigger] bs(b, nl, h1, d, x) && prot(b, nl, h1, d, x)), + ensures + bs_facts(Node::Then(Box::new(a), Box::new(b)), nl, h, d), +{ + let t = Node::Then(Box::new(a), Box::new(b)); + let sa = csize(a, nl, h, d) as int; + assert(bs(t, nl, h, d, 0)); + assert(bs(b, nl, h1, d, sb as int)); + assert(bs(t, nl, h, d, sa + sb as int)); + assert forall|x: int| #[trigger] bs(t, nl, h, d, x) implies 0 <= x <= sa + sb by { + if bs(a, nl, h, d, x) { + } else { + assert(bs(b, nl, h1, d, x - sa)); + } + } + assert forall|x: int| #[trigger] prot(t, nl, h, d, x) implies 0 < x < sa + sb by { + if prot(a, nl, h, d, x) { + } else { + assert(prot(b, nl, h1, d, x - sa)); + } + } + assert forall|x: int| !(#[trigger] bs(t, nl, h, d, x) && prot(t, nl, h, d, x)) by { + if bs(t, nl, h, d, x) && prot(t, nl, h, d, x) { + if bs(a, nl, h, d, x) { + if prot(a, nl, h, d, x) { + } else { + assert(prot(b, nl, h1, d, x - sa)); + } + } else { + assert(bs(b, nl, h1, d, x - sa)); + if prot(a, nl, h, d, x) { + } else { + assert(prot(b, nl, h1, d, x - sa)); + } + } + } + } +} + +/// `if`, from the facts of its arms. +proof fn lemma_bs_if(t: Node, e: Node, nl: nat, h: nat, d: nat) + requires + 1 <= h, + bs_facts(t, nl, (h - 1) as nat, d + 1), + bs_facts(e, nl, (h - 1) as nat, d + 1), + ensures + bs_facts(Node::If(Box::new(t), Box::new(e)), nl, h, d), +{ + lemma_csize_if(t, e, nl, h, d); + let n = Node::If(Box::new(t), Box::new(e)); + let h0 = (h - 1) as nat; + let p = cond_len(nl, h) as int; + let st = csize(t, nl, h0, d + 1) as int; + let se = csize(e, nl, h0, d + 1) as int; + assert(bs(n, nl, h, d, 0)); + assert(bs(e, nl, h0, d + 1, se)); + assert(bs(n, nl, h, d, p + st + 5 + se)); + assert forall|x: int| #[trigger] bs(n, nl, h, d, x) implies 0 <= x <= p + st + 5 + se by { + if x == 0 { + } else if bs(t, nl, h0, d + 1, x - p) { + } else { + assert(bs(e, nl, h0, d + 1, x - (p + st + 5))); + } + } + assert forall|x: int| #[trigger] prot(n, nl, h, d, x) implies 0 < x < p + st + 5 + se by { + if prot(t, nl, h0, d + 1, x - p) { + } else { + assert(prot(e, nl, h0, d + 1, x - (p + st + 5))); + } + } + assert forall|x: int| !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x)) by { + if bs(n, nl, h, d, x) && prot(n, nl, h, d, x) { + if x == 0 { + if prot(t, nl, h0, d + 1, x - p) { + } else { + assert(prot(e, nl, h0, d + 1, x - (p + st + 5))); + } + } else if bs(t, nl, h0, d + 1, x - p) { + if prot(t, nl, h0, d + 1, x - p) { + } else { + assert(prot(e, nl, h0, d + 1, x - (p + st + 5))); + } + } else { + assert(bs(e, nl, h0, d + 1, x - (p + st + 5))); + if prot(t, nl, h0, d + 1, x - p) { + } else { + assert(prot(e, nl, h0, d + 1, x - (p + st + 5))); + } + } + } + } +} + +/// **Where a jump may land is inside the node, where none may is strictly +/// inside it, and the two are disjoint.** The node's start and end are +/// among the first. +pub proof fn lemma_bs_facts(n: Node, nl: nat, h: nat, d: nat) + requires + cf_flow(n, nl, h, d) is Some, + ensures + bs_facts(n, nl, h, d), + decreases n, +{ + match n { + Node::Mem(m) => { + lemma_mem_csize(m, nl, h, d); + assert forall|x: int| !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x)) by {} + }, + Node::Then(a, b) => { + lemma_bs_facts(*a, nl, h, d); + lemma_csize_then(*a, *b, nl, h, d); + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + lemma_bs_facts(*b, nl, h1, d); + assert(flow_h(*a, nl, h, d) == h1); + lemma_bs_then(*a, *b, nl, h, d, h1, csize(*b, nl, h1, d)); + }, + Some(Flow::Dead) => { + assert(*b == Node::Op(Op::Nop)); + lemma_nop_size(nl, h, d); + assert(flow_h(*a, nl, h, d) == h); + assert forall|y: int| #[trigger] bs(*b, nl, h, d, y) == (y == 0) by {} + assert forall|y: int| !prot(*b, nl, h, d, y) by {} + lemma_bs_then(*a, *b, nl, h, d, h, 0); + }, + None => {}, + } + }, + Node::Block(b) => { + lemma_csize_block(*b, nl, h, d); + lemma_bs_facts(*b, nl, h, d + 1); + let sz = csize(*b, nl, h, d + 1) as int; + assert(bs(n, nl, h, d, 0)); + assert(bs(n, nl, h, d, sz)); + assert forall|x: int| #[trigger] bs(n, nl, h, d, x) implies 0 <= x <= sz by { + assert(bs(*b, nl, h, d + 1, x)); + } + assert forall|x: int| #[trigger] prot(n, nl, h, d, x) implies 0 < x < sz by { + assert(prot(*b, nl, h, d + 1, x)); + } + assert forall|x: int| !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x)) by { + assert(bs(n, nl, h, d, x) == bs(*b, nl, h, d + 1, x)); + assert(prot(n, nl, h, d, x) == prot(*b, nl, h, d + 1, x)); + } + }, + Node::Loop(b) => { + lemma_csize_loop(*b, nl, h, d); + lemma_bs_facts(*b, nl, h, d + 1); + let sz = csize(*b, nl, h, d + 1) as int; + assert(bs(n, nl, h, d, 0)); + assert(bs(n, nl, h, d, sz)); + assert forall|x: int| #[trigger] bs(n, nl, h, d, x) implies 0 <= x <= sz by { + assert(bs(*b, nl, h, d + 1, x)); + } + assert forall|x: int| #[trigger] prot(n, nl, h, d, x) implies 0 < x < sz by { + assert(prot(*b, nl, h, d + 1, x)); + } + assert forall|x: int| !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x)) by { + assert(bs(n, nl, h, d, x) == bs(*b, nl, h, d + 1, x)); + assert(prot(n, nl, h, d, x) == prot(*b, nl, h, d + 1, x)); + } + }, + Node::If(t, e) => { + lemma_bs_facts(*t, nl, (h - 1) as nat, d + 1); + lemma_bs_facts(*e, nl, (h - 1) as nat, d + 1); + lemma_bs_if(*t, *e, nl, h, d); + }, + Node::Op(_) => { + lemma_bs_leaf(n, nl, h, d); + }, + Node::Br(_) => { + lemma_bs_leaf(n, nl, h, d); + }, + Node::BrIf(_) => { + lemma_bs_leaf(n, nl, h, d); + }, + Node::BrTable(_, _) => { + lemma_bs_leaf(n, nl, h, d); + }, + Node::Exit(_, _) => { + lemma_bs_leaf(n, nl, h, d); + }, + } +} + +/// The nodes with no inner structure. +proof fn lemma_bs_leaf(n: Node, nl: nat, h: nat, d: nat) + requires + n is Br || n is BrIf || n is BrTable || n is Op || n is Exit, + ensures + bs_facts(n, nl, h, d), +{ + assert(bs(n, nl, h, d, 0)); + assert(bs(n, nl, h, d, csize(n, nl, h, d) as int)); + assert forall|x: int| #[trigger] bs(n, nl, h, d, x) implies 0 <= x <= csize(n, nl, h, d) as int + by {} + assert forall|x: int| !prot(n, nl, h, d, x) by {} + assert forall|x: int| !(#[trigger] bs(n, nl, h, d, x) && prot(n, nl, h, d, x)) by {} +} + +/// What [`lemma_bs_ubi`] says about a node. +pub open spec fn ubi_facts(n: Node, nl: nat, h: nat, offs: Seq) -> bool { + forall|pos: int, x: int| + bs(n, nl, h, offs.len(), x) ==> #[trigger] ubi(utmpl(n, nl, h, offs), pos, pos + x) +} + +/// A memory instruction. +proof fn lemma_ubif_mem(m: MemOp, nl: nat, h: nat, offs: Seq) + requires + mem_region(m) < 16, + ensures + ubi_facts(Node::Mem(m), nl, h, offs), +{ + let n = Node::Mem(m); + let d = offs.len(); + lemma_mem_csize(m, nl, h, d); + lemma_utmpl_blen(n, nl, h, offs); + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![jmp_i(code_len(exit_seq(mem_exit_code(m))) as int)]; + let stub = exit_seq(mem_exit_code(m)); + let u1 = ones(pre) + seq![chk] + ones(tail); + let u2 = seq![jm, stub]; + lemma_blen_ones(pre); + lemma_blen_ones(tail); + lemma_blen_cat(ones(pre), seq![chk]); + lemma_blen_cat(ones(pre) + seq![chk], ones(tail)); + lemma_blen_one(chk); + assert(blen(u1) == code_len(pre) + code_len(chk) + code_len(tail)); + lemma_code_len_one(jmp_i(code_len(stub) as int)); + crate::cflens::lemma_jmp_len(code_len(stub) as int); + lemma_blen_two(jm, stub); + assert(code_len(jm) == 5); + assert(mem_units(m, nl, h) =~= u1 + u2); + assert forall|pos: int, x: int| bs(n, nl, h, d, x) implies #[trigger] ubi( + utmpl(n, nl, h, offs), + pos, + pos + x, + ) by { + if x == 0 { + lemma_uflat_ones(pre); + assert(ones(pre).len() > 0); + assert(ub(ones(pre), pos, pos)); + assert(mem_units(m, nl, h) =~= ones(pre) + (seq![chk] + ones(tail) + u2)); + lemma_ub_cat(ones(pre), seq![chk] + ones(tail) + u2, pos, pos + x); + } else if x == stub_off(m, nl, h) { + assert(u2.len() == 2); + assert(u2.skip(1)[0] == stub); + assert(u2[0] == jm); + assert(code_len(u2[0]) == 5); + assert(ub(u2.skip(1), pos + blen(u1) + 5, pos + x)); + assert(ub(u2, pos + blen(u1), pos + x)); + lemma_ub_cat(u1, u2, pos, pos + x); + } else { + assert(x == csize(n, nl, h, d) as int); + lemma_blen_cat(u1, u2); + } + } +} + +/// `if`. +proof fn lemma_ubif_if(t: Node, e: Node, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + ubi_facts(t, nl, (h - 1) as nat, seq![ + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 5 + csize( + e, + nl, + (h - 1) as nat, + offs.len() + 1, + ) as int, + ] + shift(offs, cond_len(nl, h) as int)), + ubi_facts(e, nl, (h - 1) as nat, seq![csize(e, nl, (h - 1) as nat, offs.len() + 1) as int] + + shift( + offs, + cond_len(nl, h) as int + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 5, + )), + ensures + ubi_facts(Node::If(Box::new(t), Box::new(e)), nl, h, offs), +{ + let n = Node::If(Box::new(t), Box::new(e)); + let d = offs.len(); + let p = cond_len(nl, h) as int; + let st = csize(t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p); + let oe = seq![se] + shift(offs, p + st + 5); + assert(ot.len() == d + 1 && oe.len() == d + 1); + let cp = cond_pre(nl, h, 4, st + 5); + let ut = utmpl(t, nl, (h - 1) as nat, ot); + let ue = utmpl(e, nl, (h - 1) as nat, oe); + let jmu = seq![seq![jmp_i(se)]]; + lemma_cond_pre_len(nl, h, 4, st + 5); + lemma_blen_ones(cp); + lemma_utmpl_blen(t, nl, (h - 1) as nat, ot); + lemma_utmpl_blen(e, nl, (h - 1) as nat, oe); + crate::cflens::lemma_jmp_len(se); + lemma_code_len_one(jmp_i(se)); + lemma_blen_one(seq![jmp_i(se)]); + let a1 = ones(cp); + let a2 = a1 + ut; + let a3 = a2 + jmu; + lemma_blen_cat(a1, ut); + lemma_blen_cat(a2, jmu); + lemma_blen_cat(a3, ue); + assert(utmpl(n, nl, h, offs) =~= a3 + ue); + assert forall|pos: int, x: int| bs(n, nl, h, d, x) implies #[trigger] ubi( + utmpl(n, nl, h, offs), + pos, + pos + x, + ) by { + if x == 0 { + lemma_uflat_ones(cp); + assert(ub(a1, pos, pos)); + assert(utmpl(n, nl, h, offs) =~= a1 + (ut + jmu + ue)); + lemma_ubi_cat(a1, ut + jmu + ue, pos, pos + x); + } else if bs(t, nl, (h - 1) as nat, d + 1, x - p) { + assert(ubi(ut, pos + p, pos + p + (x - p))); + lemma_ubi_cat(a1, ut, pos, pos + x); + lemma_ubi_cat(a2, jmu, pos, pos + x); + lemma_ubi_cat(a3, ue, pos, pos + x); + } else { + assert(bs(e, nl, (h - 1) as nat, d + 1, x - (p + st + 5))); + assert(ubi(ue, pos + (p + st + 5), pos + (p + st + 5) + (x - (p + st + 5)))); + lemma_ubi_cat(a3, ue, pos, pos + x); + } + } +} + +/// `then`. +proof fn lemma_ubif_then(a: Node, b: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(Node::Then(Box::new(a), Box::new(b)), nl, h, offs.len()) is Some, + ubi_facts(a, nl, h, offs), + cf_flow(a, nl, h, offs.len()) is Some, + match cf_flow(a, nl, h, offs.len()) { + Some(Flow::Falls(h1)) => ubi_facts( + b, + nl, + h1, + shift(offs, csize(a, nl, h, offs.len()) as int), + ), + _ => true, + }, + ensures + ubi_facts(Node::Then(Box::new(a), Box::new(b)), nl, h, offs), +{ + let n = Node::Then(Box::new(a), Box::new(b)); + let d = offs.len(); + let sa = csize(a, nl, h, d); + let ob = shift(offs, sa as int); + let h1 = flow_h(a, nl, h, d); + lemma_utmpl_blen(a, nl, h, offs); + assert forall|pos: int, x: int| bs(n, nl, h, d, x) implies #[trigger] ubi( + utmpl(n, nl, h, offs), + pos, + pos + x, + ) by { + if bs(a, nl, h, d, x) { + assert(ubi(utmpl(a, nl, h, offs), pos, pos + x)); + lemma_ubi_cat(utmpl(a, nl, h, offs), utmpl(b, nl, h1, ob), pos, pos + x); + } else { + assert(bs(b, nl, h1, d, x - sa as int)); + match cf_flow(a, nl, h, d) { + Some(Flow::Falls(_)) => { + assert(ob.len() == d); + assert(ubi( + utmpl(b, nl, h1, ob), + pos + sa as int, + pos + sa as int + (x - sa as int), + )); + lemma_ubi_cat(utmpl(a, nl, h, offs), utmpl(b, nl, h1, ob), pos, pos + x); + }, + Some(Flow::Dead) => { + assert(b == Node::Op(Op::Nop)); + lemma_nop_size(nl, h, d); + assert(utmpl(b, nl, h1, ob) =~= Seq::>::empty()); + assert(x == sa as int); + lemma_blen_cat(utmpl(a, nl, h, offs), utmpl(b, nl, h1, ob)); + }, + None => {}, + } + } + } +} + +/// A node whose units are one instruction each, or an exit sequence. +proof fn lemma_ubif_leaf(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + n is Br || n is BrIf || n is BrTable || n is Op || n is Exit, + ensures + ubi_facts(n, nl, h, offs), +{ + let d = offs.len(); + lemma_utmpl_blen(n, nl, h, offs); + let us = utmpl(n, nl, h, offs); + assert forall|pos: int, x: int| bs(n, nl, h, d, x) implies #[trigger] ubi(us, pos, pos + x) by { + if x == 0 { + match n { + Node::Exit(c, _) => { + assert(us.len() == 1); + assert(ub(us, pos, pos)); + }, + _ => { + let c = match n { + Node::Op(o) => tmpl(o, nl, h), + _ => ctmpl(n, nl, h, offs), + }; + assert(us == ones(c)); + if c.len() > 0 { + assert(us.len() > 0); + assert(ub(us, pos, pos)); + } else { + assert(us.len() == 0); + lemma_blen_ones(c); + } + }, + } + } else { + assert(x == csize(n, nl, h, d) as int); + } + } +} + +/// `block`. +proof fn lemma_ubif_block(b: Node, nl: nat, h: nat, offs: Seq) + requires + ubi_facts(b, nl, h, seq![csize(b, nl, h, offs.len() + 1) as int] + offs), + ensures + ubi_facts(Node::Block(Box::new(b)), nl, h, offs), +{ + let n = Node::Block(Box::new(b)); + let ob = seq![csize(b, nl, h, offs.len() + 1) as int] + offs; + assert(ob.len() == offs.len() + 1); + assert forall|pos: int, x: int| bs(n, nl, h, offs.len(), x) implies #[trigger] ubi( + utmpl(n, nl, h, offs), + pos, + pos + x, + ) by { + assert(bs(b, nl, h, offs.len() + 1, x)); + assert(ubi(utmpl(b, nl, h, ob), pos, pos + x)); + } +} + +/// `loop`. +proof fn lemma_ubif_loop(b: Node, nl: nat, h: nat, offs: Seq) + requires + ubi_facts(b, nl, h, seq![0int] + offs), + ensures + ubi_facts(Node::Loop(Box::new(b)), nl, h, offs), +{ + let n = Node::Loop(Box::new(b)); + let ob = seq![0int] + offs; + assert(ob.len() == offs.len() + 1); + assert forall|pos: int, x: int| bs(n, nl, h, offs.len(), x) implies #[trigger] ubi( + utmpl(n, nl, h, offs), + pos, + pos + x, + ) by { + assert(bs(b, nl, h, offs.len() + 1, x)); + assert(ubi(utmpl(b, nl, h, ob), pos, pos + x)); + } +} + +/// **Every position a jump may land on is a unit start or the end of the +/// units.** +pub proof fn lemma_bs_ubi(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + ensures + ubi_facts(n, nl, h, offs), + decreases n, +{ + let d = offs.len(); + match n { + Node::Mem(m) => { + lemma_ubif_mem(m, nl, h, offs); + }, + Node::Then(a, b) => { + let sa = csize(*a, nl, h, d); + let ob = shift(offs, sa as int); + lemma_bs_ubi(*a, nl, h, offs); + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + assert(ob.len() == d); + lemma_bs_ubi(*b, nl, h1, ob); + }, + _ => {}, + } + lemma_ubif_then(*a, *b, nl, h, offs); + }, + Node::Block(b) => { + let ob = seq![csize(*b, nl, h, d + 1) as int] + offs; + assert(ob.len() == d + 1); + lemma_bs_ubi(*b, nl, h, ob); + lemma_ubif_block(*b, nl, h, offs); + }, + Node::Loop(b) => { + let ob = seq![0int] + offs; + assert(ob.len() == d + 1); + lemma_bs_ubi(*b, nl, h, ob); + lemma_ubif_loop(*b, nl, h, offs); + }, + Node::If(t, e) => { + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p); + let oe = seq![se] + shift(offs, p + st + 5); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_bs_ubi(*t, nl, (h - 1) as nat, ot); + lemma_bs_ubi(*e, nl, (h - 1) as nat, oe); + lemma_ubif_if(*t, *e, nl, h, offs); + }, + _ => { + lemma_ubif_leaf(n, nl, h, offs); + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfcheck.rs b/crates/clazel-jit/src/cfcheck.rs new file mode 100644 index 000000000..effdf0f3f --- /dev/null +++ b/crates/clazel-jit/src/cfcheck.rs @@ -0,0 +1,217 @@ +//! **The validator's bounds-check sequence, run** (`clazel_codecheck::spec:: +//! check_seq`, with the index in `rcx`, `rdx` and `rax` as scratch): from a +//! state with the index in `rcx`, it falls through to its end exactly when +//! `index + n <= bound`, and otherwise leaves through one of its two exits. +//! Either way registers other than the scratch ones, and memory, are as they +//! were. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfleaf::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfmemrel::*; +#[cfg(verus_only)] +use crate::cfmemsteps::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// What a stretch of code keeps of the machine: the memory, the context +/// register, the stack pointer, `rsi` and `rdi`, and being a well-formed +/// state. +pub open spec fn keeps(m: x64::State, m2: x64::State) -> bool { + m2.mem == m.mem && x64::wf(m2) && m2.rgf[15] == m.rgf[15] && m2.rgf[4] == m.rgf[4] + && m2.rgf[6] == m.rgf[6] && m2.rgf[7] == m.rgf[7] +} + +/// The instruction at index `j` of `code` is what `fetch` finds at the +/// offset of the bytes before it. +pub proof fn lemma_fetch_idx(code: Seq, j: int) + requires + 0 <= j < code.len(), + ensures + fetch(code, code_len(code.take(j)) as int) == Some(code[j]), +{ + let pre = code.take(j); + let post = seq![code[j]] + code.skip(j + 1); + assert(code =~= pre + post); + assert(code.skip(j) =~= post); + super::cfsteps::lemma_fetch_append_r(pre, post, code_len(pre) as int); + assert(fetch(post, 0) == Some(code[j])); +} + +/// The bytes before instruction `j + 1` are those before `j` and `j`. +pub proof fn lemma_take_len(code: Seq, j: int) + requires + 0 <= j < code.len(), + ensures + code_len(code.take(j + 1)) == code_len(code.take(j)) + x64::instr_len(code[j]), +{ + assert(code.take(j + 1) =~= code.take(j) + seq![code[j]]); + super::runseq::lemma_code_len_append(code.take(j), seq![code[j]]); + super::runseq::lemma_code_len_one(code[j]); +} + +/// One more step of a run: if `s` is reached, the instruction at `rip` is +/// `i`, and it steps to `s1`, then `s1` is reached. +pub proof fn lemma_reach_step( + cu: spec_fn(nat) -> nat, + code: Seq, + base: int, + s0: x64::State, + s: x64::State, + i: x64::Instr, + s1: x64::State, +) + requires + reaches(cu, code, base, s0, s), + fetch(code, s.rip - base) == Some(i), + x64::step(cu, s, i) == Some(s1), + ensures + reaches(cu, code, base, s0, s1), +{ + let n = choose|n: nat| steps(cu, code, base, s0, n) == Some(s); + reveal_with_fuel(steps, 2); + assert(steps(cu, code, base, s, 1) == Some(s1)); + lemma_steps_trans(cu, code, base, s0, s, s1, n, 1); +} + +/// A read through the context register at an offset below the context's +/// size, with no wrap-around. +pub proof fn lemma_ctx_ea(m: x64::State, off: int) + requires + ctx_ok(m), + 0 <= off < 512, + ensures + x64::eff_addr( + m, + x64::Mem { base: abi::ctx_reg(), index: None, disp: off as i32 }, + ) == ctx_of(m) + off, +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + assert((off as i32) as int == off); + let x = ctx_of(m) + off; + assert(0 <= x && x < 0x8000_0000_0000 + 512); + assert(x64::loghead(64, x) == x); + assert(x64::logext(64, x) == x); +} + +/// What the region table gives for region `k`: the base and the bound, as +/// words below `2^64`, readable through the context register. +pub proof fn lemma_table_words(lay: lir::Layout, m: x64::State, k: nat) + requires + x64::wf(m), + ctx_ok(m), + table_ok(lay, m), + k < lay.len(), + ensures + x64::read_mem(m.mem, x64::eff_addr(m, base_mem(k)), 8) == Some(lay[k as int].base as int), + x64::read_mem(m.mem, x64::eff_addr(m, clazel_codecheck::spec::bound_mem(k as int)), 8) + == Some(lay[k as int].bound as int), + lay[k as int].base < 0x1_0000_0000_0000_0000, + lay[k as int].bound < 0x1_0000_0000_0000_0000, +{ + super::steps::lemma_abi_consts(); + assert(abi::base_off(k as int) == 16 * k && abi::bound_off(k as int) == 16 * k + 8); + assert(k < 16); + lemma_ctx_ea(m, abi::base_off(k as int)); + lemma_ctx_ea(m, abi::bound_off(k as int)); + assert(base_mem(k).disp as int == 16 * k) by { + assert((abi::base_off(k as int) as i32) as int == abi::base_off(k as int)); + } + assert(clazel_codecheck::spec::bound_mem(k as int).disp as int == 16 * k + 8) by { + assert((abi::bound_off(k as int) as i32) as int == abi::bound_off(k as int)); + } + assert(x64::eff_addr(m, base_mem(k)) == ctx_of(m) + abi::base_off(k as int)); + assert(x64::eff_addr(m, clazel_codecheck::spec::bound_mem(k as int)) == ctx_of(m) + + abi::bound_off(k as int)); + let lk = lay[k as int]; + assert(x64::read_mem(m.mem, ctx_of(m) + abi::base_off(k as int), 8) == Some(lk.base as int)); + assert(x64::read_mem(m.mem, ctx_of(m) + abi::bound_off(k as int), 8) == Some(lk.bound as int)); + lemma_read_mem_range(m.mem, ctx_of(m) + abi::base_off(k as int), 8, lk.base as int); + lemma_read_mem_range(m.mem, ctx_of(m) + abi::bound_off(k as int), 8, lk.bound as int); + super::memlemmas::lemma_p2_consts(); + assert(vstd::arithmetic::power2::pow2(64) == 0x1_0000_0000_0000_0000); +} + +/// A state with the memory and `rip`'s canonicity of a well-formed one, 16 +/// general and 16 XMM registers, is well formed. +pub proof fn lemma_wf_like(m: x64::State, s: x64::State) + requires + x64::wf(m), + s.mem == m.mem, + s.rgf.len() == 16, + s.xmm.len() == 16, + x64::canonical(s.rip), + ensures + x64::wf(s), +{ +} + +/// The offsets of the bounds-check sequence's instructions: they begin 0, 3, +/// 10, 16, 24, 30, 35 and 43 bytes in, and it is 47 bytes long; the part from +/// the fourth instruction on is 31, from the sixth 17. +pub proof fn lemma_check_offsets(c: clazel_codecheck::spec::Chk) + requires + c.i == rcx() && c.t == rdx() && c.z == rax() && 0 <= c.k < 16, + ensures + ({ + let cs = clazel_codecheck::spec::check_seq(c); + &&& cs.len() == 8 + &&& code_len(cs.take(0)) == 0 + &&& code_len(cs.take(1)) == 3 + &&& code_len(cs.take(2)) == 10 + &&& code_len(cs.take(3)) == 16 + &&& code_len(cs.take(4)) == 24 + &&& code_len(cs.take(5)) == 30 + &&& code_len(cs.take(6)) == 35 + &&& code_len(cs.take(7)) == 43 + &&& code_len(cs) == 47 + &&& code_len(cs.skip(3)) == 31 + &&& code_len(cs.skip(5)) == 17 + }), +{ + let cs = clazel_codecheck::spec::check_seq(c); + lemma_check_instr_lens(c); + assert(cs.len() == 8); + assert(cs.take(0) =~= Seq::::empty()); + assert(code_len(Seq::::empty()) == 0); + assert(cs[0] == clazel_codecheck::spec::check_instr(0, c)); + assert(cs[1] == clazel_codecheck::spec::check_instr(1, c)); + assert(cs[2] == clazel_codecheck::spec::check_instr(2, c)); + assert(cs[3] == clazel_codecheck::spec::check_instr(3, c)); + assert(cs[4] == clazel_codecheck::spec::check_instr(4, c)); + assert(cs[5] == clazel_codecheck::spec::check_instr(5, c)); + assert(cs[6] == clazel_codecheck::spec::check_instr(6, c)); + assert(cs[7] == clazel_codecheck::spec::check_instr(7, c)); + lemma_take_len(cs, 0); + lemma_take_len(cs, 1); + lemma_take_len(cs, 2); + lemma_take_len(cs, 3); + lemma_take_len(cs, 4); + lemma_take_len(cs, 5); + lemma_take_len(cs, 6); + lemma_take_len(cs, 7); + assert(cs.take(8) =~= cs); + assert(cs =~= cs.take(3) + cs.skip(3)); + assert(cs =~= cs.take(5) + cs.skip(5)); + super::runseq::lemma_code_len_append(cs.take(3), cs.skip(3)); + super::runseq::lemma_code_len_append(cs.take(5), cs.skip(5)); +} + +} // verus! diff --git a/crates/clazel-jit/src/cfcheckrun.rs b/crates/clazel-jit/src/cfcheckrun.rs new file mode 100644 index 000000000..61c30c2a5 --- /dev/null +++ b/crates/clazel-jit/src/cfcheckrun.rs @@ -0,0 +1,370 @@ +//! [`lemma_check_run`]: the run of the bounds-check sequence, in three +//! pieces (steps 0 to 2, 3 and 4, 5 to 7). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfcheck::*; +#[cfg(verus_only)] +use crate::cfleaf::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfmemsteps::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The sequence's instructions are where the offsets say. +pub proof fn lemma_check_fetch(c: clazel_codecheck::spec::Chk) + requires + c.i == rcx() && c.t == rdx() && c.z == rax() && 0 <= c.k < 16, + ensures + ({ + let cs = clazel_codecheck::spec::check_seq(c); + &&& fetch(cs, 0) == Some(clazel_codecheck::spec::check_instr(0, c)) + &&& fetch(cs, 3) == Some(clazel_codecheck::spec::check_instr(1, c)) + &&& fetch(cs, 10) == Some(clazel_codecheck::spec::check_instr(2, c)) + &&& fetch(cs, 16) == Some(clazel_codecheck::spec::check_instr(3, c)) + &&& fetch(cs, 24) == Some(clazel_codecheck::spec::check_instr(4, c)) + &&& fetch(cs, 30) == Some(clazel_codecheck::spec::check_instr(5, c)) + &&& fetch(cs, 35) == Some(clazel_codecheck::spec::check_instr(6, c)) + &&& fetch(cs, 43) == Some(clazel_codecheck::spec::check_instr(7, c)) + }), +{ + let cs = clazel_codecheck::spec::check_seq(c); + lemma_check_offsets(c); + assert(cs[0] == clazel_codecheck::spec::check_instr(0, c)); + assert(cs[1] == clazel_codecheck::spec::check_instr(1, c)); + assert(cs[2] == clazel_codecheck::spec::check_instr(2, c)); + assert(cs[3] == clazel_codecheck::spec::check_instr(3, c)); + assert(cs[4] == clazel_codecheck::spec::check_instr(4, c)); + assert(cs[5] == clazel_codecheck::spec::check_instr(5, c)); + assert(cs[6] == clazel_codecheck::spec::check_instr(6, c)); + assert(cs[7] == clazel_codecheck::spec::check_instr(7, c)); + lemma_fetch_idx(cs, 0); + lemma_fetch_idx(cs, 1); + lemma_fetch_idx(cs, 2); + lemma_fetch_idx(cs, 3); + lemma_fetch_idx(cs, 4); + lemma_fetch_idx(cs, 5); + lemma_fetch_idx(cs, 6); + lemma_fetch_idx(cs, 7); +} + +/// Steps 0 to 2: `mov rdx, rcx; add rdx, n; jb`. The run reaches `s3`, with +/// `rdx` the sum, and `rip` at the first exit on a carry, else after the `jb`. +#[verifier::rlimit(400)] +pub proof fn lemma_chk_head( + cu: spec_fn(nat) -> nat, + m: x64::State, + c: clazel_codecheck::spec::Chk, +) + requires + x64::wf(m), + c.i == rcx() && c.t == rdx() && c.z == rax() && 0 <= c.k < 16, + 1 <= c.n <= 8, + -0x8000_0000 <= c.r1 < 0x8000_0000, + x64::canonical(m.rip + 47), + x64::canonical(m.rip + 16 + c.r1), + ensures + ({ + let cs = clazel_codecheck::spec::check_seq(c); + let sum = m.rgf[1] as int + c.n; + exists|s3: x64::State| + #![trigger reaches(cu, cs, m.rip, m, s3)] + reaches(cu, cs, m.rip, m, s3) && x64::wf(s3) && s3.mem == m.mem && s3.xmm + == m.xmm && s3.rgf == m.rgf.update( + 2, + (sum % 0x1_0000_0000_0000_0000) as u64, + ) && s3.rip == (if sum >= 0x1_0000_0000_0000_0000 { + m.rip + 16 + c.r1 + } else { + m.rip + 16 + }) + }), +{ + super::memlemmas::lemma_p2_consts(); + let cs = clazel_codecheck::spec::check_seq(c); + lemma_check_fetch(c); + lemma_check_instr_lens(c); + let ix = m.rgf[1] as int; + assert(0 <= ix < 0x1_0000_0000_0000_0000); + let rip0 = m.rip; + super::runseq::lemma_canonical_between(rip0, rip0 + 3, rip0 + 47); + super::runseq::lemma_canonical_between(rip0, rip0 + 10, rip0 + 47); + super::runseq::lemma_canonical_between(rip0, rip0 + 16, rip0 + 47); + let i0 = clazel_codecheck::spec::check_instr(0, c); + let i1 = clazel_codecheck::spec::check_instr(1, c); + let i2 = clazel_codecheck::spec::check_instr(2, c); + assert(i0 == x64::Instr::MovRR { sz: x64::Size::W64, dst: 2, src: 1 }); + lemma_step_mov_rr64(cu, m, 2, 1); + let s1 = x64::step(cu, m, i0).unwrap(); + lemma_wf_like(m, s1); + assert(reaches(cu, cs, rip0, m, m)) by { + assert(steps(cu, cs, rip0, m, 0) == Some(m)); + } + lemma_reach_step(cu, cs, rip0, m, m, i0, s1); + assert(i1 == x64::Instr::AluRI { + alu: x64::AluOp::Add, + sz: x64::Size::W64, + dst: 2, + imm: c.n as i32, + }); + lemma_step_add_ri64(cu, s1, 2, c.n as i32); + let s2 = x64::step(cu, s1, i1).unwrap(); + assert(s1.rgf[2] as int == ix); + let sum = ix + c.n; + lemma_wf_like(m, s2); + lemma_reach_step(cu, cs, rip0, m, s1, i1, s2); + assert(i2 == jcc_i(2, c.r1 as int)); + lemma_step_jcc(cu, s2, 2, c.r1 as int); + let s3 = x64::step(cu, s2, i2).unwrap(); + assert(x64::cf_bit() == 0); + assert((c.r1 as i32) as int == c.r1); + assert(x64::jcc_cmovcc_setcc_spec(s2, 2) == (sum >= 0x1_0000_0000_0000_0000)); + lemma_wf_like(m, s3); + lemma_reach_step(cu, cs, rip0, m, s2, i2, s3); +} + +/// Steps 3 and 4: `cmp rdx, [bound]; ja`, from after the `jb` with no carry. +/// The run reaches `s5`, at the second exit exactly when `sum > bound`, else +/// after the `ja`, registers as they were. +#[verifier::rlimit(400)] +pub proof fn lemma_chk_mid( + cu: spec_fn(nat) -> nat, + lay: lir::Layout, + m: x64::State, + s3: x64::State, + c: clazel_codecheck::spec::Chk, +) + requires + x64::wf(m), + ctx_ok(m), + table_ok(lay, m), + c.i == rcx() && c.t == rdx() && c.z == rax() && 0 <= c.k < 16, + c.k < lay.len(), + 1 <= c.n <= 8, + -0x8000_0000 <= c.r2 < 0x8000_0000, + x64::canonical(m.rip + 47), + x64::canonical(m.rip + 30 + c.r2), + x64::wf(s3), + s3.mem == m.mem, + s3.xmm == m.xmm, + s3.rgf.len() == 16, + s3.rgf[15] == m.rgf[15], + s3.rgf[2] as int == m.rgf[1] as int + c.n, + s3.rip == m.rip + 16, + reaches(cu, clazel_codecheck::spec::check_seq(c), m.rip, m, s3), + ensures + ({ + let cs = clazel_codecheck::spec::check_seq(c); + let sum = m.rgf[1] as int + c.n; + let bound = lay[c.k].bound as int; + exists|s5: x64::State| + #![trigger reaches(cu, cs, m.rip, m, s5)] + reaches(cu, cs, m.rip, m, s5) && x64::wf(s5) && s5.mem == m.mem && s5.xmm + == m.xmm && s5.rgf == s3.rgf && s5.rip == (if sum > bound { + m.rip + 30 + c.r2 + } else { + m.rip + 30 + }) + }), +{ + super::memlemmas::lemma_p2_consts(); + let cs = clazel_codecheck::spec::check_seq(c); + lemma_check_fetch(c); + lemma_check_instr_lens(c); + lemma_table_words(lay, m, c.k as nat); + let rip0 = m.rip; + let ix = m.rgf[1] as int; + let sum = ix + c.n; + let bound = lay[c.k].bound as int; + super::runseq::lemma_canonical_between(rip0, rip0 + 24, rip0 + 47); + super::runseq::lemma_canonical_between(rip0, rip0 + 30, rip0 + 47); + let bm = clazel_codecheck::spec::bound_mem(c.k); + let i3 = clazel_codecheck::spec::check_instr(3, c); + let i4 = clazel_codecheck::spec::check_instr(4, c); + assert(i3 == x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: 2, mem: bm }); + assert(x64::eff_addr(s3, bm) == x64::eff_addr(m, bm)); + assert(sum < 0x1_0000_0000_0000_0000); + lemma_step_cmp_rm64(cu, s3, 2, bm, bound); + let s4 = x64::step(cu, s3, i3).unwrap(); + lemma_wf_like(m, s4); + lemma_reach_step(cu, cs, rip0, m, s3, i3, s4); + assert(i4 == jcc_i(7, c.r2 as int)); + assert(x64::jcc_cmovcc_setcc_spec(s4, 7) == (sum > bound)); + lemma_step_jcc(cu, s4, 7, c.r2 as int); + let s5 = x64::step(cu, s4, i4).unwrap(); + assert((c.r2 as i32) as int == c.r2); + lemma_wf_like(m, s5); + lemma_reach_step(cu, cs, rip0, m, s4, i4, s5); +} + +/// Steps 5 to 7: `mov eax, 0; cmp rcx, [bound]; cmovae rcx, rax`, from after +/// the `ja` with `ix < bound`: nothing but `rax` and the flags change. +#[verifier::rlimit(400)] +pub proof fn lemma_chk_tail( + cu: spec_fn(nat) -> nat, + lay: lir::Layout, + m: x64::State, + s5: x64::State, + c: clazel_codecheck::spec::Chk, +) + requires + x64::wf(m), + ctx_ok(m), + table_ok(lay, m), + c.i == rcx() && c.t == rdx() && c.z == rax() && 0 <= c.k < 16, + c.k < lay.len(), + 1 <= c.n <= 8, + m.rgf[1] as int + c.n <= lay[c.k].bound as int, + x64::canonical(m.rip + 47), + x64::wf(s5), + s5.mem == m.mem, + s5.xmm == m.xmm, + s5.rgf.len() == 16, + s5.rgf[15] == m.rgf[15], + s5.rgf[1] == m.rgf[1], + s5.rip == m.rip + 30, + reaches(cu, clazel_codecheck::spec::check_seq(c), m.rip, m, s5), + ensures + ({ + let cs = clazel_codecheck::spec::check_seq(c); + exists|s8: x64::State| + #![trigger reaches(cu, cs, m.rip, m, s8)] + reaches(cu, cs, m.rip, m, s8) && x64::wf(s8) && s8.mem == m.mem && s8.xmm == m.xmm + && s8.rgf == s5.rgf.update(0, 0u64) && s8.rip == m.rip + 47 + }), +{ + super::memlemmas::lemma_p2_consts(); + let cs = clazel_codecheck::spec::check_seq(c); + lemma_check_fetch(c); + lemma_check_instr_lens(c); + lemma_table_words(lay, m, c.k as nat); + let rip0 = m.rip; + let ix = m.rgf[1] as int; + let bound = lay[c.k].bound as int; + super::runseq::lemma_canonical_between(rip0, rip0 + 35, rip0 + 47); + super::runseq::lemma_canonical_between(rip0, rip0 + 43, rip0 + 47); + let bm = clazel_codecheck::spec::bound_mem(c.k); + let i5 = clazel_codecheck::spec::check_instr(5, c); + let i6 = clazel_codecheck::spec::check_instr(6, c); + let i7 = clazel_codecheck::spec::check_instr(7, c); + assert(i5 == x64::Instr::MovImm32 { dst: 0, imm_u32: 0 }); + super::steps::lemma_step_imm(cu, s5, i5); + let s6 = x64::step(cu, s5, i5).unwrap(); + lemma_wf_like(m, s6); + lemma_reach_step(cu, cs, rip0, m, s5, i5, s6); + assert(i6 == x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: 1, mem: bm }); + assert(x64::eff_addr(s6, bm) == x64::eff_addr(m, bm)); + assert(s6.rgf[1] == m.rgf[1]); + lemma_step_cmp_rm64(cu, s6, 1, bm, bound); + let s7 = x64::step(cu, s6, i6).unwrap(); + lemma_wf_like(m, s7); + lemma_reach_step(cu, cs, rip0, m, s6, i6, s7); + assert(i7 == x64::Instr::Cmovcc { cc: 3, sz: x64::Size::W64, dst: 1, src: 0 }); + assert(x64::jcc_cmovcc_setcc_spec(s7, 3) == (ix >= bound)); + super::flags::lemma_step_cmov64(cu, s7, 3, 1, 0); + let s8 = x64::step(cu, s7, i7).unwrap(); + lemma_wf_like(m, s8); + lemma_reach_step(cu, cs, rip0, m, s7, i7, s8); + assert(s8.rgf == s7.rgf); +} + +/// **The bounds check, run.** From a state with the index `ix` in `rcx`: if +/// `ix + n <= bound` the check falls through to its end (47 bytes on), else it +/// leaves by the first exit (a carry) or the second (`ix + n > bound`). +#[verifier::rlimit(400)] +pub proof fn lemma_check_run( + cu: spec_fn(nat) -> nat, + lay: lir::Layout, + m: x64::State, + k: nat, + n: nat, + r1: int, + r2: int, +) + requires + x64::wf(m), + ctx_ok(m), + table_ok(lay, m), + k < lay.len(), + 1 <= n <= 8, + -0x8000_0000 <= r1 < 0x8000_0000, + -0x8000_0000 <= r2 < 0x8000_0000, + x64::canonical(m.rip + 47), + x64::canonical(m.rip + 16 + r1), + x64::canonical(m.rip + 30 + r2), + ensures + ({ + let cs = clazel_codecheck::spec::check_seq(mem_chk(k, n, r1, r2)); + let ix = m.rgf[1] as int; + let bound = lay[k as int].bound as int; + &&& (ix + n <= bound ==> exists|m2: x64::State| + #![trigger reaches(cu, cs, m.rip, m, m2)] + reaches(cu, cs, m.rip, m, m2) && m2.rip == m.rip + 47 && keeps(m, m2) + && m2.rgf[1] == m.rgf[1]) + &&& (ix + n > bound ==> exists|m2: x64::State| + #![trigger reaches(cu, cs, m.rip, m, m2)] + reaches(cu, cs, m.rip, m, m2) && (m2.rip == m.rip + 16 + r1 || m2.rip == m.rip + 30 + + r2) && keeps(m, m2) && m2.rgf[1] == m.rgf[1]) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + let c = mem_chk(k, n, r1, r2); + assert(c.n == n as int && c.r1 == r1 as i32 && c.r2 == r2 as i32); + assert(c.r1 as int == r1 && c.r2 as int == r2); + lemma_table_words(lay, m, k); + let ix = m.rgf[1] as int; + let bound = lay[k as int].bound as int; + lemma_chk_head(cu, m, c); + let s3 = choose|s3: x64::State| + reaches(cu, clazel_codecheck::spec::check_seq(c), m.rip, m, s3) && x64::wf(s3) && s3.mem + == m.mem && s3.xmm == m.xmm && s3.rgf == m.rgf.update( + 2, + ((ix + n) % 0x1_0000_0000_0000_0000) as u64, + ) && s3.rip == (if ix + n >= 0x1_0000_0000_0000_0000 { + m.rip + 16 + r1 + } else { + m.rip + 16 + }); + assert(s3.rgf.len() == 16); + if ix + n >= 0x1_0000_0000_0000_0000 { + assert(ix + n > bound); + assert(keeps(m, s3)); + } else { + assert(s3.rgf[2] as int == ix + n); + lemma_chk_mid(cu, lay, m, s3, c); + let s5 = choose|s5: x64::State| + reaches(cu, clazel_codecheck::spec::check_seq(c), m.rip, m, s5) && x64::wf(s5) + && s5.mem == m.mem && s5.xmm == m.xmm && s5.rgf == s3.rgf && s5.rip == (if ix + n + > bound { + m.rip + 30 + r2 + } else { + m.rip + 30 + }); + if ix + n > bound { + assert(keeps(m, s5)); + } else { + lemma_chk_tail(cu, lay, m, s5, c); + let s8 = choose|s8: x64::State| + reaches(cu, clazel_codecheck::spec::check_seq(c), m.rip, m, s8) && x64::wf(s8) + && s8.mem == m.mem && s8.xmm == m.xmm && s8.rgf == s5.rgf.update(0, 0u64) + && s8.rip == m.rip + 47; + assert(keeps(m, s8)); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfcompose.rs b/crates/clazel-jit/src/cfcompose.rs new file mode 100644 index 000000000..3dbca1ed9 --- /dev/null +++ b/crates/clazel-jit/src/cfcompose.rs @@ -0,0 +1,346 @@ +//! **Composition over structured control flow**: how the [`cf_refines`] +//! statement of a node follows from those of its parts. One lemma per way of +//! combining: `then` ([`lemma_then`]), `block` ([`lemma_block`]), `loop` +//! ([`lemma_loop`]), `if` ([`crate::cfif::lemma_if`]). The leaves +//! (`br`, `br_if`, `exit`, straight-line instructions) are in +//! [`crate::cfleaf`]; [`crate::cfproofs`] puts them together by induction on +//! the node. +//! +//! Two lifting lemmas do the bookkeeping the cases share: a statement about a +//! part, from the state after the code before it ([`lemma_lift_pre`]), and one +//! about the code followed by more ([`lemma_lift_post`]). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cfheight::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::contract::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `shift(offs, d)` at `l` is `offs[l] - d`. +pub proof fn lemma_shift_index(offs: Seq, d: int, l: int) + requires + 0 <= l < offs.len(), + ensures + shift(offs, d).len() == offs.len(), + shift(offs, d)[l] == offs[l] - d, +{ +} + +/// **Lifting a part's statement to the code that starts with something +/// else.** `c`'s outcome `o`, seen from `m1` (the state after the code +/// `pre`, which ran from `m`), is the outcome of `pre + c` from `m`, with the +/// labels moved by the length of `pre`. +pub proof fn lemma_lift_pre( + lay: lir::Layout, + base: Seq, + nl: nat, + offs: Seq, + offs_c: Seq, + pre: Seq, + c: Seq, + cu: spec_fn(nat) -> nat, + m: x64::State, + m1: x64::State, + o: lir::Outcome, +) + requires + offs_c.len() == offs.len(), + forall|l: int| 0 <= l < offs.len() ==> #[trigger] offs_c[l] + code_len(pre) == offs[l], + m1.rip == m.rip + code_len(pre), + reaches(cu, pre + c, m.rip, m, m1), + preserved_r(lay, m, m1), + stack_ok_r(lay, m), + cf_post(lay, base, nl, offs_c, c, cu, m1, o), + ensures + cf_post(lay, base, nl, offs, pre + c, cu, m, o), +{ + super::steps::lemma_abi_consts(); + lemma_stack_pres(lay, m, m1); + assert(ctx_of(m1) == ctx_of(m)); + let d = code_len(pre); + match o { + lir::Outcome::Normal(a2) => { + let m2 = choose|m2: x64::State| + reaches(cu, c, m1.rip, m1, m2) && m2.rip == m1.rip + code_len(c) && rel_base( + lay, + base, + a2, + m2, + nl, + ) && preserved_r(lay, m1, m2); + assert(pre + c + Seq::::empty() =~= pre + c); + lemma_reaches_embed(cu, pre, c, Seq::empty(), m1.rip, m1, m2); + assert(m1.rip - d == m.rip); + lemma_reaches_trans(cu, pre + c, m.rip, m, m1, m2); + super::runseq::lemma_code_len_append(pre, c); + lemma_pr_trans(lay, m, m1, m2); + assert(reaches(cu, pre + c, m.rip, m, m2) && m2.rip == m.rip + code_len(pre + c) + && rel_base(lay, base, a2, m2, nl) && preserved_r(lay, m, m2)); + }, + lir::Outcome::Branch(l, s) => { + let m2 = choose|m2: x64::State| + reaches(cu, c, m1.rip, m1, m2) && m2.rip == m1.rip + offs_c[l as int] && rel_ext( + lay, + base, + s, + m2, + nl, + ) && preserved_r(lay, m1, m2); + assert(pre + c + Seq::::empty() =~= pre + c); + lemma_reaches_embed(cu, pre, c, Seq::empty(), m1.rip, m1, m2); + assert(m1.rip - d == m.rip); + lemma_reaches_trans(cu, pre + c, m.rip, m, m1, m2); + lemma_pr_trans(lay, m, m1, m2); + assert(l < offs.len()); + assert(offs_c[l as int] + d == offs[l as int]); + }, + lir::Outcome::Exit(code, vals, s) => { + let m2 = choose|m2: x64::State| + reaches(cu, c, m1.rip, m1, m2) && exit_done(m1, m2, code) && exit_rel( + lay, + base, + s, + m2, + nl, + ctx_of(m1), + vals, + ) && preserved_mem_r(lay, m1, m2); + assert(pre + c + Seq::::empty() =~= pre + c); + lemma_reaches_embed(cu, pre, c, Seq::empty(), m1.rip, m1, m2); + assert(m1.rip - d == m.rip); + lemma_reaches_trans(cu, pre + c, m.rip, m, m1, m2); + lemma_pr_pmr_trans(lay, m, m1, m2); + assert(exit_done(m, m2, code)); + }, + _ => {}, + } +} + +/// **Lifting a statement to the code followed by more**, for an outcome other +/// than falling through (which would end earlier). +pub proof fn lemma_lift_post( + lay: lir::Layout, + base: Seq, + nl: nat, + offs: Seq, + c: Seq, + post: Seq, + cu: spec_fn(nat) -> nat, + m: x64::State, + o: lir::Outcome, +) + requires + !(o is Normal), + cf_post(lay, base, nl, offs, c, cu, m, o), + ensures + cf_post(lay, base, nl, offs, c + post, cu, m, o), +{ + match o { + lir::Outcome::Branch(l, s) => { + let m2 = choose|m2: x64::State| + reaches(cu, c, m.rip, m, m2) && m2.rip == m.rip + offs[l as int] && rel_ext( + lay, + base, + s, + m2, + nl, + ) && preserved_r(lay, m, m2); + assert(Seq::::empty() + c + post =~= c + post); + lemma_reaches_embed(cu, Seq::empty(), c, post, m.rip, m, m2); + assert(code_len(Seq::::empty()) == 0); + }, + lir::Outcome::Exit(code, vals, s) => { + let m2 = choose|m2: x64::State| + reaches(cu, c, m.rip, m, m2) && exit_done(m, m2, code) && exit_rel( + lay, + base, + s, + m2, + nl, + ctx_of(m), + vals, + ) && preserved_mem_r(lay, m, m2); + assert(Seq::::empty() + c + post =~= c + post); + lemma_reaches_embed(cu, Seq::empty(), c, post, m.rip, m, m2); + assert(code_len(Seq::::empty()) == 0); + }, + _ => {}, + } +} + +/// `cf_canon` for the first part of a sequence. +pub proof fn lemma_canon_prefix(rip: int, offs: Seq, a: Seq, b: Seq) + requires + x64::canonical(rip), + cf_canon(rip, offs, a + b), + ensures + cf_canon(rip, offs, a), +{ + super::runseq::lemma_code_len_append(a, b); + super::runseq::lemma_canonical_between(rip, rip + code_len(a), rip + code_len(a + b)); +} + +/// **`then`.** If the first instruction falls through at height `h1` and its +/// code and the second's (whose labels are the first's moved by its length) +/// refine them, their concatenation refines the pair. +#[verifier::rlimit(400)] +pub proof fn lemma_then( + x: Node, + y: Node, + nl: nat, + h: nat, + h1: nat, + offs: Seq, + cx: Seq, + cy: Seq, +) + requires + cf_flow(x, nl, h, offs.len()) == Some(Flow::Falls(h1)), + cf_refines(x@, nl, h, offs, cx), + cf_refines(y@, nl, h1, shift(offs, code_len(cx) as int), cy), + ensures + cf_refines(Node::Then(Box::new(x), Box::new(y))@, nl, h, offs, cx + cy), +{ + let t = Node::Then(Box::new(x), Box::new(y)); + let big = cx + cy; + super::runseq::lemma_code_len_append(cx, cy); + let ll = code_len(cx) as int; + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, big, cu, m, lir::eval(lay, t@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, big) implies cf_post( + lay, + base, + nl, + offs, + big, + cu, + m, + lir::eval(lay, t@, a, fuel), + ) by { + if fuel > 0 { + let f = (fuel - 1) as nat; + lemma_rel_base_unfold(lay, base, a, m, nl); + assert(x64::canonical(m.rip)); + lemma_canon_prefix(m.rip, offs, cx, cy); + lemma_use_cf(x@, nl, h, offs, cx, lay, cu, base, a, m, f); + let o1 = lir::eval(lay, x@, a, f); + match o1 { + lir::Outcome::Normal(a1) => { + let m1 = choose|m1: x64::State| + reaches(cu, cx, m.rip, m, m1) && m1.rip == m.rip + code_len(cx) + && rel_base(lay, base, a1, m1, nl) && preserved_r(lay, m, m1); + lemma_cf_height(x, nl, h, offs.len(), h1, base.len(), lay, a, a1, f); + lemma_stack_pres(lay, m, m1); + assert(x64::canonical(m1.rip)); + assert forall|l: int| 0 <= l < shift(offs, ll).len() implies x64::canonical( + m1.rip + #[trigger] shift(offs, ll)[l], + ) by { + lemma_shift_index(offs, ll, l); + assert(cf_canon(m.rip, offs, big)); + assert(x64::canonical(m.rip + offs[l])); + } + assert(cf_canon(m1.rip, shift(offs, ll), cy)); + lemma_use_cf(y@, nl, h1, shift(offs, ll), cy, lay, cu, base, a1, m1, f); + assert(Seq::::empty() + cx + cy =~= cx + cy); + lemma_reaches_embed(cu, Seq::empty(), cx, cy, m.rip, m, m1); + assert(code_len(Seq::::empty()) == 0); + assert forall|l: int| 0 <= l < offs.len() implies #[trigger] shift(offs, ll)[l] + + code_len(cx) == offs[l] by { + lemma_shift_index(offs, ll, l); + } + lemma_lift_pre( + lay, + base, + nl, + offs, + shift(offs, ll), + cx, + cy, + cu, + m, + m1, + lir::eval(lay, y@, a1, f), + ); + }, + _ => { + lemma_lift_post(lay, base, nl, offs, cx, cy, cu, m, o1); + }, + } + } + } +} + +/// **`then` whose first part is dead** (a `br`, `br_table` or `exit`): only +/// `nop` follows, and it has no code, so the pair's code is the first's. +pub proof fn lemma_then_dead( + x: Node, + nl: nat, + h: nat, + offs: Seq, + cx: Seq, +) + requires + cf_flow(x, nl, h, offs.len()) == Some(Flow::Dead), + cf_refines(x@, nl, h, offs, cx), + ensures + cf_refines( + Node::Then(Box::new(x), Box::new(Node::Op(Op::Nop)))@, + nl, + h, + offs, + cx, + ), +{ + let t = Node::Then(Box::new(x), Box::new(Node::Op(Op::Nop))); + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, cx, cu, m, lir::eval(lay, t@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, cx) implies cf_post( + lay, + base, + nl, + offs, + cx, + cu, + m, + lir::eval(lay, t@, a, fuel), + ) by { + if fuel > 0 { + let f = (fuel - 1) as nat; + lemma_use_cf(x@, nl, h, offs, cx, lay, cu, base, a, m, f); + lemma_dead_not_normal(x, nl, h, offs.len(), lay, a, f); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfcontract.rs b/crates/clazel-jit/src/cfcontract.rs new file mode 100644 index 000000000..80ab82cac --- /dev/null +++ b/crates/clazel-jit/src/cfcontract.rs @@ -0,0 +1,278 @@ +//! What the x86-64 back end must be proven to satisfy for **structured control +//! flow, memory instructions and `exit` with operands** (CLAUDE.md rule 3, +//! docs/SPEC-STYLE.md): the refinement statement [`cf_refines`] and the +//! emitter's contract, in terms of [`crate::cfspec`] and the two semantics it +//! relates. Written before any template; reviewing this file and `cfspec` is +//! enough to know what is claimed. +//! +//! # The refinement statement +//! +//! [`cf_refines`]`(i, nl, h, offs, code)` says the x86-64 list `code` is a +//! correct translation of the LIR instruction `i`, for a function with `nl` +//! locals, when the operand stack is `h` values high (the `base` below `i`'s +//! fresh stack, and its own stack), with the labels in scope at the byte +//! offsets `offs` from the first byte of `code` (label 0 first). For every +//! layout, `create_undef`, `base`, LIR state `a` and machine state `m` +//! related by [`rel_base`], with the native stack as the enter stub leaves it +//! ([`stack_ok_r`]), and every address the code can jump to canonical: +//! +//! - **`Normal(a2)`** (falls through): `code` [`reaches`] a state `m2` at the +//! byte after `code`, related to `a2` on the same `base`, differing from +//! `m` only in the exit area and the regions ([`preserved_r`]). +//! - **`Branch(l, s)`** (a `br` that no label inside `i` catches): `l` is a +//! label in scope, and `code` reaches a state at the label's address, +//! related to `s` on `base` and some `extra` values (choice 13), with the +//! exit area and regions the only change. +//! - **`Exit(c, vals, s)`**: `code` reaches the state the exit sequence +//! leaves: the exit code `c` in the context's exit-code word, `rsp` popped +//! and `rip` at the return address the stub pushed, the frame and regions +//! related to `s`, and the operands `vals` still in the frame, in the slots +//! above `s`'s stack. +//! - **`Stuck`, `OutOfFuel`**: nothing is claimed. A bounds-check failure +//! in a `load` or `store` is `Stuck` in LIR (choice 11). +//! +//! That is a forward simulation of one LIR evaluation by one run of the +//! translation. LIR's evaluation is fuelled, so a divergent program is +//! `OutOfFuel` at every fuel and says nothing, and a terminating one is +//! matched by a terminating run. +//! +//! # What is not stated +//! +//! - That the output is **accepted by the validator** (checked by +//! `tests/validator.rs`, as for the first slice). +//! - Typed blocks, the `f64` operations, a frame beyond 31 slots (see +//! [`crate::cfspec`]). +//! - Anything about `rip`, flags or scratch registers beyond what is said. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::contract::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every address the code can end at or jump to is canonical. +pub open spec fn cf_canon(rip: int, offs: Seq, code: Seq) -> bool { + x64::canonical(rip + code_len(code)) && forall|l: int| + 0 <= l < offs.len() ==> x64::canonical(rip + #[trigger] offs[l]) +} + +/// What the machine is at after an exit sequence for `c` from a state with a +/// stack as the enter stub leaves it, as [`exit_refines`] says: the exit-code +/// word holds `c`, `rsp` is popped, `rip` is the return address. +pub open spec fn exit_done(m: x64::State, m2: x64::State, c: nat) -> bool { + let rsp = x64::logext(64, m.rgf[abi::sp_reg() as int] as int); + x64::read_mem(m2.mem, ctx_of(m) + abi::exit_code_off(), 8) == Some(c as int) + && m2.rgf.len() == 16 && m2.rgf[abi::sp_reg() as int] as int == x64::loghead(64, rsp + 8) + && match x64::read_mem(m.mem, rsp, 8) { + Some(raw) => m2.rip == x64::logext(64, raw), + None => false, + } +} + +/// The operands `vals` of an exit are in the frame, in the slots after the +/// first `n0` stack slots (`nl` locals, then the stack): value `i` in slot +/// `nl + n0 + i`. +pub open spec fn ops_in_frame( + mm: Map, + ctxb: int, + nl: nat, + n0: nat, + vals: Seq, +) -> bool { + forall|i: int| + 0 <= i < vals.len() ==> #[trigger] slot_holds( + mm, + ctxb, + (nl + n0 + i) as nat, + lir::val_bits(vals[i]), + ) +} + +/// The machine represents `s` on top of `base` and **some extra values** +/// (the bases that nested bodies added: see [`crate::cfspec`], choice 13). +pub open spec fn rel_ext( + lay: lir::Layout, + base: Seq, + s: lir::St, + m2: x64::State, + nl: nat, +) -> bool { + exists|extra: Seq| + #![trigger rel_base(lay, base + extra, s, m2, nl)] + rel_base(lay, base + extra, s, m2, nl) +} + +/// [`rel_ext`], and the operands `vals` of an exit are in the frame above the +/// stack (`ctxb` is the context's address). +pub open spec fn exit_rel( + lay: lir::Layout, + base: Seq, + s: lir::St, + m2: x64::State, + nl: nat, + ctxb: int, + vals: Seq, +) -> bool { + exists|extra: Seq| + #![trigger rel_base(lay, base + extra, s, m2, nl)] + rel_base(lay, base + extra, s, m2, nl) && ops_in_frame( + m2.mem, + ctxb, + nl, + (base + extra).len() + s.stack.len(), + vals, + ) +} + +/// What must hold after `code` ran from `m`, for LIR's outcome `o` (see the +/// module doc). +pub open spec fn cf_post( + lay: lir::Layout, + base: Seq, + nl: nat, + offs: Seq, + code: Seq, + cu: spec_fn(nat) -> nat, + m: x64::State, + o: lir::Outcome, +) -> bool { + match o { + lir::Outcome::Normal(a2) => exists|m2: x64::State| + #![trigger reaches(cu, code, m.rip, m, m2)] + reaches(cu, code, m.rip, m, m2) && m2.rip == m.rip + code_len(code) && rel_base( + lay, + base, + a2, + m2, + nl, + ) && preserved_r(lay, m, m2), + lir::Outcome::Branch(l, s) => l < offs.len() && exists|m2: x64::State| + #![trigger reaches(cu, code, m.rip, m, m2)] + reaches(cu, code, m.rip, m, m2) && m2.rip == m.rip + offs[l as int] && rel_ext( + lay, + base, + s, + m2, + nl, + ) && preserved_r(lay, m, m2), + lir::Outcome::Exit(c, vals, s) => exists|m2: x64::State| + #![trigger reaches(cu, code, m.rip, m, m2)] + reaches(cu, code, m.rip, m, m2) && exit_done(m, m2, c) && exit_rel( + lay, + base, + s, + m2, + nl, + ctx_of(m), + vals, + ) && preserved_mem_r(lay, m, m2), + _ => true, + } +} + +/// **`code` refines the LIR instruction `i`** at stack height `h` with `nl` +/// locals, the labels in scope at the byte offsets `offs` (see the module +/// doc). +pub open spec fn cf_refines( + i: lir::Instr, + nl: nat, + h: nat, + offs: Seq, + code: Seq, +) -> bool { + forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, cu, m, lir::eval(lay, i, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, code) ==> cf_post( + lay, + base, + nl, + offs, + code, + cu, + m, + lir::eval(lay, i, a, fuel), + ) +} + +/// The exit sequence for `c` refines LIR's `exit c` **with `k` operands**: +/// [`exit_refines`], and the operands are still in the frame afterwards, in +/// the slots above the stack the exit leaves (the exit sequence writes only +/// the exit-code word, so they are never overwritten; the host reads them +/// from there). +pub open spec fn exit_ops_refines(c: u32, k: nat, nl: nat) -> bool { + forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger lir::eval(lay, exit_instr(c as nat, k), a, fuel), run_seq(cu, m, exit_seq(c))] + rel(lay, a, m, nl) && stack_ok(m) && x64::canonical(m.rip + code_len(exit_seq(c))) + ==> match lir::eval( + lay, + exit_instr(c as nat, k), + a, + fuel, + ) { + lir::Outcome::Exit(code, vals, a2) => match run_seq(cu, m, exit_seq(c)) { + Some(m2) => rel(lay, a2, m2, nl) && preserved_mem(m, m2) && exit_done(m, m2, code) + && forall|i: int| + 0 <= i < vals.len() ==> #[trigger] slot_holds( + m2.mem, + ctx_of(m), + (nl + a2.stack.len() + i) as nat, + lir::val_bits(vals[i]), + ), + None => false, + }, + _ => true, + } +} + +// ---------------------------------------------------------------------------- +// The emitter's contract for control flow (the executable back end) +// ---------------------------------------------------------------------------- + +/// Postcondition of the control-flow emitter's `emit_blob` (the code it must +/// return is [`crate::cftemplates::ctmpl`]): the bytes are the +/// canonical encodings of a body that refines the program `n` with the +/// function's return label at the end of the body (a `br` to it, and falling +/// off the end, reach the exit sequence that follows), followed by the exit +/// sequence for `exit_code`. `None` exactly when [`cf_prog_ok`] refuses. +pub open spec fn emit_cf_blob_ensures( + n: Node, + nl: usize, + exit_code: u32, + r: Option>, +) -> bool { + match r { + Some(bytes) => cf_prog_ok(n, nl as nat) && exists|body: Seq| + #![trigger all_valid_instrs(body)] + all_valid_instrs(body) && cf_refines( + n@, + nl as nat, + 0, + seq![code_len(body) as int], + body, + ) && bytes == clazel_codecheck::spec::enc_all(body + exit_seq(exit_code)), + None => !cf_prog_ok(n, nl as nat), + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfemit.rs b/crates/clazel-jit/src/cfemit.rs new file mode 100644 index 000000000..843dc5e94 --- /dev/null +++ b/crates/clazel-jit/src/cfemit.rs @@ -0,0 +1,1502 @@ +//! The executable back end for **structured control flow, memory +//! instructions and `br_table`** (x86-64): [`emit_cf_blob`], **proven equal +//! to the templates**. +//! +//! [`emit_cf_blob`]'s postcondition is +//! [`crate::cfcontract::emit_cf_blob_ensures`]: the bytes it returns are the +//! encoding of [`crate::cftemplates::ctmpl`] (the body, with the return label +//! at its end) followed by the exit sequence, and it answers `None` exactly +//! when [`crate::cftemplates::cf_prog_ok`] refuses. Together with +//! [`crate::cfproofs::lemma_ctmpl_refines`] that makes every emitted blob a +//! refinement of its program ([`lemma_emitted_refines`]). +//! +//! # How it is proven +//! +//! - **Labels** are absolute byte positions in the body (`labs`, label 0 +//! first); the spec's `offs` of a node that starts at `start` are +//! [`offs_of`]`(labs, start)` (position minus start). `then`, `block`, +//! `loop` and `if` change `labs` and `start` exactly as `ctmpl` changes +//! `offs` ([`lemma_offs_shift`], [`lemma_offs_push`]). +//! - **No signed arithmetic.** Verus's `vstd` specifies `checked_*` and +//! `saturating_*` for unsigned types only, and `i32::try_from(i64)` has no +//! spec. Every position is a `usize` below [`MAX_SIZE`] (the spec's +//! `max_size`), and a displacement is `target - end` when the target is +//! not before the end, else the two's complement of `end - target`, formed +//! as `!(end - target - 1)` on a `u32` ([`rel32`], [`lemma_not_neg`]), so +//! the workspace's `arithmetic_side_effects` lint holds with no `#[allow]` +//! and nothing is trusted. +//! - **The size analysis** [`analyze`] equals `cf_flow` and `csize`: it +//! returns the flow and the size when the node has a translation of at most +//! [`MAX_SIZE`] bytes, and refuses (`None`) only if `cf_flow` does or the +//! size exceeds `max_size` (a node's size is at most that of the nodes +//! around it, `cflens`). Overflow is excluded: a size is summed only after +//! each part is at most [`MAX_SIZE`], and the label count is the length of +//! a `Vec`. +//! - **The emission** [`emit_node`] appends exactly `ctmpl`, by induction on +//! the node, each leaf by its own function ([`mem_code_exec`], +//! [`brt_exec`], `emit_op` from the first slice, ...). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfblobacc::{emit_cf_blob_accepted_ensures, emitted_bytes, lemma_emitter_accepted}; +#[cfg(verus_only)] +use crate::cfcompose::lemma_shift_index; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfmemvalid::lemma_mem_valid; +#[cfg(verus_only)] +use crate::cfproofs::*; +#[cfg(verus_only)] +use crate::cfspec::*; +use crate::cfspec::{MTy, MemOp, Node}; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::cfvalid::lemma_ctmpl_valid; +#[cfg(verus_only)] +use crate::contract::*; +use crate::emit::{emit_exit, emit_op, ex_load, ex_store, op_ok_exec, pops_exec, pushes_exec}; +#[cfg(verus_only)] +use crate::templates::*; +use clazel_x64::encoder::encode_exec; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The largest body this emitter translates, in bytes (the spec's +/// `cftemplates::max_size`). +pub const MAX_SIZE: usize = 0x2000_0000; + +/// The most targets of a `br_table` (the spec's `max_targets`). +pub const MAX_TARGETS: usize = 0x10000; + +/// The emitter's limits are the spec's: a larger body, or a `br_table` with +/// more targets, is refused exactly where `cf_prog_ok` and `cf_flow` refuse it. +pub proof fn lemma_emitter_limits() + ensures + MAX_SIZE as nat == max_size(), + MAX_TARGETS as nat == max_targets(), +{ +} + +// ---------------------------------------------------------------------------- +// Arithmetic (the workspace denies the unchecked operators) +// ---------------------------------------------------------------------------- + +/// `a + b`, when it does not overflow. +fn add_u(a: usize, b: usize) -> (r: usize) + requires + a + b <= usize::MAX, + ensures + r == a + b, +{ + a.saturating_add(b) +} + +/// `a - b`, when it does not underflow. +fn sub_u(a: usize, b: usize) -> (r: usize) + requires + b <= a, + ensures + r == a - b, +{ + a.saturating_sub(b) +} + +/// `a * b`, when it does not overflow. +fn mul_u(a: usize, b: usize) -> (r: usize) + requires + a * b <= usize::MAX, + ensures + r == a * b, +{ + a.saturating_mul(b) +} + +/// The bit-complement of a small `u32`, read as an `i32`, is `-x - 1`. +proof fn lemma_not_neg(x: u32) + requires + x < 0x4000_0000, + ensures + (!x) as i32 as int == -(x as int) - 1, +{ + assert((!x) as i32 == -(x as i32) - 1) by (bit_vector) + requires + x < 0x4000_0000, + ; + assert((x as i32) as int == x as int); +} + +/// The displacement from a jump that ends at `end` to `target`. Both are at +/// most [`MAX_SIZE`], so it fits an `i32`; a negative one is the two's +/// complement `!(end - target - 1)`. +fn rel32(target: usize, end: usize) -> (r: i32) + requires + target <= MAX_SIZE, + end <= MAX_SIZE, + ensures + r as int == target as int - end as int, +{ + if target >= end { + let d = sub_u(target, end); + d as i32 + } else { + let d1 = sub_u(sub_u(end, target), 1); + let x = d1 as u32; + let y = !x; + proof { + lemma_not_neg(x); + } + y as i32 + } +} + +/// The length in bytes of the encodings of `code`, or `None` if it does not +/// fit a `usize`. +fn code_bytes(code: &[x64::Instr]) -> (r: Option) + requires + all_valid_instrs(code@), + ensures + match r { + Some(n) => n == code_len(code@), + None => code_len(code@) > usize::MAX, + }, +{ + let mut acc: usize = 0; + let mut k: usize = 0; + proof { + assert(code@.take(0) =~= Seq::::empty()); + } + while k < code.len() + invariant + k <= code.len(), + acc == code_len(code@.take(k as int)), + all_valid_instrs(code@), + decreases code.len() - k, + { + let ins = match code.get(k) { + Some(ins) => ins, + None => { + proof { + assert(false); + } + return None; + }, + }; + let e = encode_exec(ins); + let l = e.len(); + proof { + assert(e@ =~= x64::encode(code@[k as int])); + let t = code@.take(k as int + 1); + assert(t =~= code@.take(k as int) + seq![code@[k as int]]); + crate::runseq::lemma_code_len_append(code@.take(k as int), seq![code@[k as int]]); + crate::runseq::lemma_code_len_one(code@[k as int]); + assert(code@ =~= t + code@.skip(k as int + 1)); + crate::runseq::lemma_code_len_append(t, code@.skip(k as int + 1)); + } + match acc.checked_add(l) { + Some(a) => { + acc = a; + }, + None => { + return None; + }, + } + k = add_u(k, 1); + proof { + assert(k == code@.take(k as int).len() || true); + } + } + proof { + assert(code@.take(code.len() as int) =~= code@); + } + Some(acc) +} + +// ---------------------------------------------------------------------------- +// Labels +// ---------------------------------------------------------------------------- + +/// The spec's label offsets for a node that starts at `start`, when the labels +/// are at the absolute positions `labs`. +pub open spec fn offs_of(labs: Seq, start: int) -> Seq { + Seq::new(labs.len(), |i: int| labs[i] as int - start) +} + +/// Moving the start on by `d` is [`shift`]. +proof fn lemma_offs_shift(labs: Seq, start: int, d: int) + ensures + offs_of(labs, start + d) =~= shift(offs_of(labs, start), d), +{ + let o = offs_of(labs, start); + assert forall|i: int| 0 <= i < labs.len() implies #[trigger] offs_of(labs, start + d)[i] + == shift(o, d)[i] by { + lemma_shift_index(o, d, i); + } +} + +/// A new label in front, at the absolute position `new`. +proof fn lemma_offs_push(labs: Seq, new: usize, start: int) + ensures + offs_of(seq![new] + labs, start) =~= seq![new as int - start] + offs_of(labs, start), +{ + let l = seq![new] + labs; + let r = seq![new as int - start] + offs_of(labs, start); + assert(l.len() == r.len()); + assert forall|i: int| 0 <= i < l.len() implies #[trigger] offs_of(l, start)[i] == r[i] by { + if i > 0 { + assert(l[i] == labs[i - 1]); + } + } +} + +/// `new` followed by `labs`. +fn push_label(new: usize, labs: &[usize]) -> (v: Vec) + ensures + v@ == seq![new] + labs@, +{ + let mut v: Vec = Vec::new(); + v.push(new); + v.extend_from_slice(labs); + proof { + assert(v@ =~= seq![new] + labs@); + } + v +} + +// ---------------------------------------------------------------------------- +// Leaves +// ---------------------------------------------------------------------------- + +/// The flow of an analysis, as the spec's [`Flow`]: `Some(h)` falls through at +/// height `h`, `None` is dead. +pub open spec fn fl_spec(f: Option) -> Flow { + match f { + Some(x) => Flow::Falls(x as nat), + None => Flow::Dead, + } +} + +/// The condition test is 16 bytes (8, 2, 6). +proof fn lemma_cond_len_16(nl: nat, h: nat) + ensures + cond_len(nl, h) == 16, +{ + let a = load_slot(rax(), (nl + h - 1) as nat); + let b = x64::Instr::TestRR { sz: x64::Size::W32, a: rax(), b: rax() }; + crate::cfmemlens::lemma_mem_instr_lens((nl + h - 1) as nat, 0); + lemma_jcc_len(0, 0); + assert(x64::encode(b).len() == 2); + crate::runseq::lemma_len3(a, b, jcc_i(0, 0)); + crate::runseq::lemma_len1(a); +} + +/// `mov rax, [r15 + 264 + 8 (nl + h - 1)]; test eax, eax; jcc cc rel`. +fn cond_pre_exec(nl: usize, h: usize, cc: u8, rel: i32, v: &mut Vec) + requires + 1 <= h, + nl + h <= 31, + ensures + final(v)@ == old(v)@ + cond_pre(nl as nat, h as nat, cc, rel as int), +{ + let ghost ov = v@; + let top = sub_u(add_u(nl, h), 1); + v.push(ex_load(0, top)); + v.push(x64::Instr::TestRR { sz: x64::Size::W32, a: 0, b: 0 }); + v.push(x64::Instr::Jcc { cc, rel }); + proof { + assert(v@ =~= ov + cond_pre(nl as nat, h as nat, cc, rel as int)); + } +} + +/// The operand size of an access. +fn mty_sz_exec(ty: &MTy) -> (s: x64::Size) + ensures + s == mty_sz(*ty), +{ + match ty { + MTy::I32 => x64::Size::W32, + MTy::I64 => x64::Size::W64, + MTy::F64 => x64::Size::W64, + } +} + +/// The number of bytes of an access. +fn mty_size_exec(ty: &MTy) -> (n: usize) + ensures + n as nat == ty.size(), +{ + match ty { + MTy::I32 => 4, + MTy::I64 => 8, + MTy::F64 => 8, + } +} + +/// `[rsi + rcx]`. +fn ix_mem_exec() -> (m: x64::Mem) + ensures + m == ix_mem(), +{ + x64::Mem { base: 6, index: Some(x64::Index { reg: 1, scale: 0 }), disp: 0 } +} + +/// `[r15 + 16 k]`: region `k`'s base. +fn base_mem_exec(k: usize) -> (m: x64::Mem) + requires + k < 16, + ensures + m == base_mem(k as nat), +{ + proof { + crate::steps::lemma_abi_consts(); + } + let d = mul_u(k, 16); + x64::Mem { base: 15, index: None, disp: d as i32 } +} + +/// The facts a memory instruction's parts need. +proof fn lemma_mem_facts(m: MemOp, nl: nat, h: nat) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + ensures + nl + h <= 31, + 1 <= h, + mem_region(m) < 16, + m is Store ==> 2 <= h, +{ + crate::tlemmas::lemma_abi_consts_t(); +} + +/// What a memory instruction does before its check. +fn mem_pre_exec(m: &MemOp, nl: usize, h: usize, v: &mut Vec) + requires + cf_flow(Node::Mem(*m), nl as nat, h as nat, 0) is Some, + ensures + final(v)@ == old(v)@ + mem_pre(*m, nl as nat, h as nat), +{ + let ghost ov = v@; + proof { + lemma_mem_facts(*m, nl as nat, h as nat); + } + let top = sub_u(add_u(nl, h), 1); + match m { + MemOp::BoundsCheck { .. } => { + v.push(ex_load(1, top)); + }, + MemOp::Load { region, .. } => { + v.push(ex_load(1, top)); + v.push(x64::Instr::MovLoad { sz: x64::Size::W64, dst: 6, mem: base_mem_exec(*region) }); + }, + MemOp::Store { region, .. } => { + v.push(ex_load(7, top)); + v.push(ex_load(1, sub_u(top, 1))); + v.push(x64::Instr::MovLoad { sz: x64::Size::W64, dst: 6, mem: base_mem_exec(*region) }); + }, + } + proof { + assert(v@ =~= ov + mem_pre(*m, nl as nat, h as nat)); + } +} + +/// What a memory instruction does after its check. +fn mem_tail_exec(m: &MemOp, nl: usize, h: usize, v: &mut Vec) + requires + cf_flow(Node::Mem(*m), nl as nat, h as nat, 0) is Some, + ensures + final(v)@ == old(v)@ + mem_tail(*m, nl as nat, h as nat), +{ + let ghost ov = v@; + proof { + lemma_mem_facts(*m, nl as nat, h as nat); + } + let top = sub_u(add_u(nl, h), 1); + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { ty, .. } => { + v.push(x64::Instr::MovLoad { sz: mty_sz_exec(ty), dst: 0, mem: ix_mem_exec() }); + v.push(ex_store(top, 0)); + }, + MemOp::Store { ty, .. } => { + v.push(x64::Instr::MovStore { sz: mty_sz_exec(ty), mem: ix_mem_exec(), src: 7 }); + }, + } + proof { + assert(v@ =~= ov + mem_tail(*m, nl as nat, h as nat)); + } +} + +/// Append the code of a memory instruction (`mem_code`): the operands, the +/// validator's bounds check, the access, a `jmp` over the stub, the stub. +fn mem_code_exec(m: &MemOp, nl: usize, h: usize, out: &mut Vec) + requires + cf_flow(Node::Mem(*m), nl as nat, h as nat, 0) is Some, + ensures + final(out)@ == old(out)@ + mem_code(*m, nl as nat, h as nat), +{ + proof { + lemma_mem_facts(*m, nl as nat, h as nat); + lemma_mem_parts_len(*m, nl as nat, h as nat); + crate::steps::lemma_abi_consts(); + } + let ghost old_out = out@; + let (region, n, exit_code, tl): (usize, usize, u32, usize) = match m { + MemOp::BoundsCheck { region, size, code } => (*region, *size, *code, 0), + MemOp::Load { region, ty } => (*region, mty_size_exec(ty), 0xFFFF_FFFF, match ty { + MTy::I32 => 15, + _ => 16, + }), + MemOp::Store { region, ty } => (*region, mty_size_exec(ty), 0xFFFF_FFFF, match ty { + MTy::I32 => 7, + _ => 8, + }), + }; + proof { + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { ty, .. } => { + assert(mem_n(*m) == ty.size()); + }, + MemOp::Store { ty, .. } => { + assert(mem_n(*m) == ty.size()); + }, + } + assert(n as nat == mem_n(*m)); + assert(region as nat == mem_region(*m)); + assert(exit_code == mem_exit_code(*m)); + assert(tl as nat == code_len(mem_tail(*m, nl as nat, h as nat))); + assert(n <= 8); + } + mem_pre_exec(m, nl, h, out); + let r1 = add_u(36, tl) as i32; + let r2 = add_u(22, tl) as i32; + let bd = add_u(mul_u(region, 16), 8) as i32; + out.push(x64::Instr::MovRR { sz: x64::Size::W64, dst: 2, src: 1 }); + out.push(x64::Instr::AluRI { alu: x64::AluOp::Add, sz: x64::Size::W64, dst: 2, imm: n as i32 }); + out.push(x64::Instr::Jcc { cc: 2, rel: r1 }); + out.push( + x64::Instr::AluRM { + alu: x64::AluOp::Cmp, + sz: x64::Size::W64, + dst: 2, + mem: x64::Mem { base: 15, index: None, disp: bd }, + }, + ); + out.push(x64::Instr::Jcc { cc: 7, rel: r2 }); + out.push(x64::Instr::MovImm32 { dst: 0, imm_u32: 0 }); + out.push( + x64::Instr::AluRM { + alu: x64::AluOp::Cmp, + sz: x64::Size::W64, + dst: 1, + mem: x64::Mem { base: 15, index: None, disp: bd }, + }, + ); + out.push(x64::Instr::Cmovcc { cc: 3, sz: x64::Size::W64, dst: 1, src: 0 }); + mem_tail_exec(m, nl, h, out); + out.push(x64::Instr::Jmp { rel: 14 }); + let ghost before_stub = out@; + emit_exit(out, exit_code); + proof { + let c = mem_chk(mem_region(*m), mem_n(*m), mem_r1(*m, nl as nat, h as nat), mem_r2( + *m, + nl as nat, + h as nat, + )); + let cs = clazel_codecheck::spec::check_seq(c); + assert(r1 as int == mem_r1(*m, nl as nat, h as nat)); + assert(r2 as int == mem_r2(*m, nl as nat, h as nat)); + assert(bd as int == abi::bound_off(mem_region(*m) as int)); + crate::cfmemlens::lemma_exit_seq_len(mem_exit_code(*m)); + assert(cs =~= seq![ + x64::Instr::MovRR { sz: x64::Size::W64, dst: 2, src: 1 }, + x64::Instr::AluRI { alu: x64::AluOp::Add, sz: x64::Size::W64, dst: 2, imm: n as i32 }, + x64::Instr::Jcc { cc: 2, rel: r1 }, + x64::Instr::AluRM { + alu: x64::AluOp::Cmp, + sz: x64::Size::W64, + dst: 2, + mem: x64::Mem { base: 15, index: None, disp: bd }, + }, + x64::Instr::Jcc { cc: 7, rel: r2 }, + x64::Instr::MovImm32 { dst: 0, imm_u32: 0 }, + x64::Instr::AluRM { + alu: x64::AluOp::Cmp, + sz: x64::Size::W64, + dst: 1, + mem: x64::Mem { base: 15, index: None, disp: bd }, + }, + x64::Instr::Cmovcc { cc: 3, sz: x64::Size::W64, dst: 1, src: 0 }, + ]); + assert(out@ =~= old_out + mem_code(*m, nl as nat, h as nat)); + } +} + +/// Spec of the chain's entries `0..i`. +pub open spec fn brt_upto(ls: Seq, offs: Seq, i: nat) -> Seq + decreases i, +{ + if i == 0 { + seq![] + } else { + brt_upto(ls, offs, (i - 1) as nat) + brt_entry( + (i - 1) as nat, + brt_rel(offs, ls[i - 1], (8 + i * 13) as int), + ) + } +} + +/// The entries `0..i` then the entries from `i` are all of them. +proof fn lemma_brt_upto(ls: Seq, offs: Seq, i: nat) + requires + i <= ls.len(), + ensures + brt_upto(ls, offs, i) + brt_entries(ls, offs, 8, i) =~= brt_entries(ls, offs, 8, 0), + decreases i, +{ + crate::cfmemlens::lemma_brt_entry_len(0, 0); + if i > 0 { + lemma_brt_upto(ls, offs, (i - 1) as nat); + let k = (i - 1) as nat; + assert(brt_entries(ls, offs, 8, k) == brt_entry( + k, + brt_rel(offs, ls[k as int], 8 + (k + 1) * brt_e()), + ) + brt_entries(ls, offs, 8, i)); + assert((brt_upto(ls, offs, k) + brt_entry(k, brt_rel(offs, ls[k as int], (8 + i * 13) as int))) + + brt_entries(ls, offs, 8, i) =~= brt_upto(ls, offs, k) + brt_entries( + ls, + offs, + 8, + k, + )); + } +} + +/// Append the code of `br_table ls d` at the absolute position `start`. +fn brt_exec( + ls: &[usize], + d: usize, + nl: usize, + h: usize, + start: usize, + labs: &[usize], + out: &mut Vec, +) + requires + 1 <= h, + nl + h <= 31, + ls@.len() <= 0x10000, + d < labs@.len(), + forall|i: int| 0 <= i < ls@.len() ==> #[trigger] ls@[i] < labs@.len(), + forall|i: int| 0 <= i < labs@.len() ==> #[trigger] labs@[i] <= MAX_SIZE, + start + 13 + 13 * ls@.len() <= MAX_SIZE, + ensures + final(out)@ == old(out)@ + brt_code( + ls@, + d, + nl as nat, + h as nat, + offs_of(labs@, start as int), + ), +{ + proof { + crate::cfbrt::lemma_brt_p0(nl as nat, h as nat); + crate::cfmemlens::lemma_brt_entry_len(0, 0); + } + let ghost old_out = out@; + let ghost offs = offs_of(labs@, start as int); + let top = sub_u(add_u(nl, h), 1); + out.push(ex_load(0, top)); + let mut i: usize = 0; + while i < ls.len() + invariant + i <= ls.len(), + 1 <= h, + nl + h <= 31, + ls@.len() <= 0x10000, + forall|j: int| 0 <= j < ls@.len() ==> #[trigger] ls@[j] < labs@.len(), + forall|j: int| 0 <= j < labs@.len() ==> #[trigger] labs@[j] <= MAX_SIZE, + start + 13 + 13 * ls@.len() <= MAX_SIZE, + offs == offs_of(labs@, start as int), + out@ == old_out + seq![load_slot(rax(), (nl + h - 1) as nat)] + brt_upto( + ls@, + offs, + i as nat, + ), + decreases ls.len() - i, + { + let l = match ls.get(i) { + Some(l) => *l, + None => { + proof { + assert(false); + } + return; + }, + }; + let tgt = match labs.get(l) { + Some(t) => *t, + None => { + proof { + assert(false); + } + return; + }, + }; + let end = add_u(add_u(start, 8), mul_u(13, add_u(i, 1))); + let rel = rel32(tgt, end); + out.push(x64::Instr::MovImm32 { dst: 2, imm_u32: i as u32 }); + out.push(x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: x64::Size::W32, dst: 0, src: 2 }); + out.push(x64::Instr::Jcc { cc: 4, rel }); + proof { + let ii = (i + 1) as nat; + assert(brt_rel(offs, ls@[i as int], (8 + ii * 13) as int) == tgt as int - end as int); + assert(brt_upto(ls@, offs, ii) == brt_upto(ls@, offs, i as nat) + brt_entry( + i as nat, + brt_rel(offs, ls@[i as int], (8 + ii * 13) as int), + )); + assert(brt_entry(i as nat, tgt as int - end as int) =~= seq![ + x64::Instr::MovImm32 { dst: 2, imm_u32: i as u32 }, + x64::Instr::AluRR { + alu: x64::AluOp::Cmp, + sz: x64::Size::W32, + dst: 0, + src: 2, + }, + x64::Instr::Jcc { cc: 4, rel }, + ]); + assert(out@ =~= old_out + seq![load_slot(rax(), (nl + h - 1) as nat)] + brt_upto( + ls@, + offs, + ii, + )); + } + i = add_u(i, 1); + } + let tgt = match labs.get(d) { + Some(t) => *t, + None => { + proof { + assert(false); + } + return; + }, + }; + let end = add_u(add_u(start, add_u(8, mul_u(13, ls.len()))), 5); + out.push(x64::Instr::Jmp { rel: rel32(tgt, end) }); + proof { + lemma_brt_upto(ls@, offs, ls@.len()); + assert(brt_entries(ls@, offs, 8, ls@.len() as nat) =~= Seq::::empty()); + assert(brt_p0(nl as nat, h as nat) == 8); + assert(brt_e() == 13); + assert(brt_upto(ls@, offs, ls@.len()) =~= brt_entries(ls@, offs, 8, 0)); + assert(out@ =~= old_out + brt_code(ls@, d, nl as nat, h as nat, offs)); + } +} + +// ---------------------------------------------------------------------------- +// The analysis +// ---------------------------------------------------------------------------- + +/// The code of a node is valid instructions (the analysis encodes it to +/// measure it). +proof fn lemma_templated_all(n: Node) + ensures + templated(n), + decreases n, +{ + match n { + Node::Then(a, b) => { + lemma_templated_all(*a); + lemma_templated_all(*b); + }, + Node::Block(b) => { + lemma_templated_all(*b); + }, + Node::Loop(b) => { + lemma_templated_all(*b); + }, + Node::If(t, e) => { + lemma_templated_all(*t); + lemma_templated_all(*e); + }, + _ => {}, + } +} + +/// The analysis of `n` (the spec's `cf_flow`), entered at height `h` with +/// `nl` locals and `labs.len()` labels in scope, and the size of its code in +/// bytes (the spec's `csize`). `None` only if it has no translation, or its +/// code is larger than `max_size`. +#[verifier::spinoff_prover] +#[verifier::rlimit(1500)] +fn analyze(n: &Node, nl: usize, h: usize, labs: &[usize]) -> (r: Option<(Option, usize)>) + requires + nl <= 31, + h <= 31, + ensures + match r { + Some((f, s)) => { + &&& cf_flow(*n, nl as nat, h as nat, labs@.len()) == Some(fl_spec(f)) + &&& s == csize(*n, nl as nat, h as nat, labs@.len()) + &&& s <= MAX_SIZE + &&& match f { + Some(x) => x <= 31, + None => true, + } + }, + None => cf_flow(*n, nl as nat, h as nat, labs@.len()) is None || csize( + *n, + nl as nat, + h as nat, + labs@.len(), + ) > max_size(), + }, + decreases *n, +{ + proof { + crate::tlemmas::lemma_abi_consts_t(); + } + let depth = labs.len(); + let room = add_u(nl, h) <= 31; + match n { + Node::Op(o) => { + let ok = op_ok_exec(o, nl, h); + if !ok { + return None; + } + let mut v: Vec = Vec::new(); + emit_op(o, nl, h, &mut v); + proof { + crate::tlemmas::lemma_tmpl_valid(*o, nl as nat, h as nat); + assert(v@ =~= tmpl(*o, nl as nat, h as nat)); + assert(all_valid_instrs(v@)); + assert(csize(*n, nl as nat, h as nat, depth as nat) == code_len(v@)); + } + let sz = match code_bytes(v.as_slice()) { + Some(s) => s, + None => { + return None; + }, + }; + if sz > MAX_SIZE { + return None; + } + let h2 = add_u(sub_u(h, pops_exec(o)), pushes_exec(o)); + Some((Some(h2), sz)) + }, + Node::Mem(m) => { + let ok = room && match m { + MemOp::BoundsCheck { region, size, .. } => h >= 1 && *region < 16 && 1 <= *size + && *size <= 8, + MemOp::Load { region, .. } => h >= 1 && *region < 16, + MemOp::Store { region, .. } => h >= 2 && *region < 16, + }; + if !ok { + return None; + } + let h2 = match m { + MemOp::Store { .. } => sub_u(h, 2), + _ => h, + }; + proof { + assert(cf_flow(*n, nl as nat, h as nat, depth as nat) is Some); + } + let mut v: Vec = Vec::new(); + mem_code_exec(m, nl, h, &mut v); + proof { + lemma_mem_valid(*m, nl as nat, h as nat); + assert(v@ =~= mem_code(*m, nl as nat, h as nat)); + assert(csize(*n, nl as nat, h as nat, depth as nat) == code_len(v@)); + } + let sz = match code_bytes(v.as_slice()) { + Some(s) => s, + None => { + return None; + }, + }; + if sz > MAX_SIZE { + return None; + } + Some((Some(h2), sz)) + }, + Node::Then(a, b) => { + proof { + lemma_csize_then(**a, **b, nl as nat, h as nat, depth as nat); + } + let (fa, sa) = match analyze(a, nl, h, labs) { + Some(x) => x, + None => { + return None; + }, + }; + match fa { + Some(h1) => { + let (fb, sb) = match analyze(b, nl, h1, labs) { + Some(x) => x, + None => { + return None; + }, + }; + let s = add_u(sa, sb); + if s > MAX_SIZE { + return None; + } + Some((fb, s)) + }, + None => { + if matches!(&**b, Node::Op(crate::spec::Op::Nop)) { + proof { + assert(csize(**b, nl as nat, h as nat, depth as nat) == 0) by { + assert(ctmpl(**b, nl as nat, h as nat, zeros(depth as nat)) + =~= Seq::::empty()); + } + } + Some((None, sa)) + } else { + None + } + }, + } + }, + Node::Block(b) | Node::Loop(b) => { + proof { + if n is Block { + lemma_csize_block(**b, nl as nat, h as nat, depth as nat); + } else { + lemma_csize_loop(**b, nl as nat, h as nat, depth as nat); + } + } + let l0 = push_label(0, labs); + let (fb, sb) = match analyze(b, nl, h, l0.as_slice()) { + Some(x) => x, + None => { + return None; + }, + }; + let bad = match fb { + Some(h1) => h1 != h, + None => false, + }; + if bad { + return None; + } + Some((Some(h), sb)) + }, + Node::If(t, e) => { + if h < 1 || !room { + return None; + } + let h1 = sub_u(h, 1); + proof { + lemma_csize_if(**t, **e, nl as nat, h as nat, depth as nat); + lemma_cond_len_16(nl as nat, h as nat); + } + let l0 = push_label(0, labs); + let (ft, st) = match analyze(t, nl, h1, l0.as_slice()) { + Some(x) => x, + None => { + return None; + }, + }; + let (fe, se) = match analyze(e, nl, h1, l0.as_slice()) { + Some(x) => x, + None => { + return None; + }, + }; + let fits_t = match ft { + Some(x) => x == h1, + None => true, + }; + let fits_e = match fe { + Some(x) => x == h1, + None => true, + }; + if !fits_t || !fits_e { + return None; + } + let s = add_u(add_u(add_u(16, st), 5), se); + if s > MAX_SIZE { + return None; + } + Some((Some(h1), s)) + }, + Node::Br(l) => { + proof { + lemma_csize_br(*l, nl as nat, h as nat, depth as nat); + } + if *l < depth { + Some((None, 5)) + } else { + None + } + }, + Node::BrIf(l) => { + proof { + lemma_csize_brif(*l, nl as nat, h as nat, depth as nat); + lemma_cond_len_16(nl as nat, h as nat); + } + if *l < depth && h >= 1 && room { + Some((Some(sub_u(h, 1)), 16)) + } else { + None + } + }, + Node::BrTable(ls, d) => { + if !(*d < depth && h >= 1 && room && ls.len() <= MAX_TARGETS) { + return None; + } + let mut i: usize = 0; + while i < ls.len() + invariant + i <= ls.len(), + depth == labs@.len(), + *n == Node::BrTable(*ls, *d), + *d < depth, + 1 <= h, + nl + h <= 31, + ls@.len() <= 0x10000, + forall|j: int| 0 <= j < i ==> #[trigger] ls@[j] < depth, + decreases ls.len() - i, + { + let l = match ls.get(i) { + Some(l) => *l, + None => { + proof { + assert(false); + } + return None; + }, + }; + if l >= depth { + proof { + assert(ls@[i as int] >= depth); + } + return None; + } + i = add_u(i, 1); + } + proof { + crate::cfbrt::lemma_brt_fetch_jmp( + ls@, + *d, + nl as nat, + h as nat, + zeros(depth as nat), + ); + assert(csize(*n, nl as nat, h as nat, depth as nat) == 13 + 13 * ls@.len()); + } + Some((None, add_u(13, mul_u(13, ls.len())))) + }, + Node::Exit(c, k) => { + let _ = c; + proof { + crate::cfmemlens::lemma_exit_seq_len(*c); + assert(csize(*n, nl as nat, h as nat, depth as nat) == 14); + } + if *k <= h && room { + Some((None, 14)) + } else { + None + } + }, + } +} + +// ---------------------------------------------------------------------------- +// The emission +// ---------------------------------------------------------------------------- + +/// The facts every emission function needs of its node. +pub open spec fn emit_pre(n: Node, nl: usize, h: usize, start: usize, labs: Seq) -> bool { + nl <= 31 && h <= 31 && cf_flow(n, nl as nat, h as nat, labs.len()) is Some && start + csize( + n, + nl as nat, + h as nat, + labs.len(), + ) <= MAX_SIZE && forall|i: int| 0 <= i < labs.len() ==> #[trigger] labs[i] <= MAX_SIZE +} + +/// `then a b`. +fn emit_then(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is Then, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), + decreases *n, 0nat, +{ + let ghost old_out = out@; + let ghost offs = offs_of(labs@, start as int); + if let Node::Then(a, b) = n { + { + proof { + crate::tlemmas::lemma_abi_consts_t(); + lemma_csize_then(**a, **b, nl as nat, h as nat, labs@.len()); + } + let (fa, sa) = emit_node(a, nl, h, start, labs, out); + if let Some(h1) = fa { + proof { + lemma_offs_shift(labs@, start as int, sa as int); + } + emit_node(b, nl, h1, add_u(start, sa), labs, out); + proof { + assert(flow_h(**a, nl as nat, h as nat, labs@.len()) == h1 as nat); + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + } else { + proof { + lemma_offs_shift(labs@, start as int, sa as int); + assert(ctmpl(**b, nl as nat, h as nat, shift(offs, sa as int)) + =~= Seq::::empty()); + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + } + } + } +} + +/// `block b`. +fn emit_block(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is Block, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), + decreases *n, 0nat, +{ + let ghost offs = offs_of(labs@, start as int); + if let Node::Block(b) = n { + { + proof { + crate::tlemmas::lemma_abi_consts_t(); + lemma_csize_block(**b, nl as nat, h as nat, labs@.len()); + } + let l0 = push_label(0, labs); + let sb = match analyze(b, nl, h, l0.as_slice()) { + Some(x) => x.1, + None => { + proof { + assert(false); + } + return ; + }, + }; + let l = push_label(add_u(start, sb), labs); + proof { + lemma_offs_push(labs@, (start + sb) as usize, start as int); + assert(offs_of(l@, start as int) =~= seq![sb as int] + offs); + } + emit_node(b, nl, h, start, l.as_slice(), out); + } + } +} + +/// `loop b`. +fn emit_loop(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is Loop, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), + decreases *n, 0nat, +{ + let ghost offs = offs_of(labs@, start as int); + if let Node::Loop(b) = n { + { + proof { + crate::tlemmas::lemma_abi_consts_t(); + lemma_csize_loop(**b, nl as nat, h as nat, labs@.len()); + } + let l = push_label(start, labs); + proof { + lemma_offs_push(labs@, start, start as int); + assert(offs_of(l@, start as int) =~= seq![0int] + offs); + } + emit_node(b, nl, h, start, l.as_slice(), out); + } + } +} + +/// `if t e`. +fn emit_if(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is If, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), + decreases *n, 0nat, +{ + let ghost old_out = out@; + let ghost offs = offs_of(labs@, start as int); + if let Node::If(t, e) = n { + { + proof { + crate::tlemmas::lemma_abi_consts_t(); + } + let h1 = sub_u(h, 1); + proof { + lemma_csize_if(**t, **e, nl as nat, h as nat, labs@.len()); + lemma_cond_len_16(nl as nat, h as nat); + } + let l0 = push_label(0, labs); + let st = match analyze(t, nl, h1, l0.as_slice()) { + Some(x) => x.1, + None => { + proof { + assert(false); + } + return ; + }, + }; + let se = match analyze(e, nl, h1, l0.as_slice()) { + Some(x) => x.1, + None => { + proof { + assert(false); + } + return ; + }, + }; + let end = add_u(add_u(add_u(add_u(start, 16), st), 5), se); + let l = push_label(end, labs); + let skip = add_u(st, 5) as i32; + cond_pre_exec(nl, h, 4, skip, out); + let tstart = add_u(start, 16); + proof { + lemma_offs_push(labs@, end, tstart as int); + lemma_offs_shift(labs@, start as int, 16); + assert(offs_of(l@, tstart as int) =~= seq![st as int + 5 + se as int] + shift( + offs, + 16, + )); + } + emit_node(t, nl, h1, tstart, l.as_slice(), out); + out.push(x64::Instr::Jmp { rel: se as i32 }); + let estart = add_u(tstart, add_u(st, 5)); + proof { + lemma_offs_push(labs@, end, estart as int); + lemma_offs_shift(labs@, start as int, 16 + st as int + 5); + assert(offs_of(l@, estart as int) =~= seq![se as int] + shift( + offs, + 16 + st as int + 5, + )); + } + emit_node(e, nl, h1, estart, l.as_slice(), out); + proof { + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + } + } +} + +/// `br l` and `br_if l`. +fn emit_br(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is Br || n is BrIf, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), +{ + let ghost old_out = out@; + let ghost offs = offs_of(labs@, start as int); + proof { + crate::tlemmas::lemma_abi_consts_t(); + } + match n { + Node::Br(l) => { + proof { + lemma_csize_br(*l, nl as nat, h as nat, labs@.len()); + } + let tgt = match labs.get(*l) { + Some(t) => *t, + None => { + proof { + assert(false); + } + return ; + }, + }; + out.push(x64::Instr::Jmp { rel: rel32(tgt, add_u(start, 5)) }); + proof { + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + }, + Node::BrIf(l) => { + proof { + lemma_csize_brif(*l, nl as nat, h as nat, labs@.len()); + lemma_cond_len_16(nl as nat, h as nat); + } + let tgt = match labs.get(*l) { + Some(t) => *t, + None => { + proof { + assert(false); + } + return ; + }, + }; + let rel = rel32(tgt, add_u(start, 16)); + cond_pre_exec(nl, h, 5, rel, out); + proof { + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + }, + _ => {}, + } +} + +/// `br_table`, `exit`, memory and straight-line instructions. +fn emit_leaf(n: &Node, nl: usize, h: usize, start: usize, labs: &[usize], out: &mut Vec) + requires + n is BrTable || n is Exit || n is Mem || n is Op, + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl(*n, nl as nat, h as nat, offs_of(labs@, start as int)), +{ + let ghost old_out = out@; + let ghost offs = offs_of(labs@, start as int); + proof { + crate::tlemmas::lemma_abi_consts_t(); + } + match n { + Node::Op(o) => { + emit_op(o, nl, h, out); + proof { + assert(out@ =~= old_out + ctmpl(*n, nl as nat, h as nat, offs)); + } + }, + Node::Mem(m) => { + mem_code_exec(m, nl, h, out); + }, + Node::BrTable(ls, d) => { + proof { + crate::cfbrt::lemma_brt_fetch_jmp( + ls@, + *d, + nl as nat, + h as nat, + zeros(labs@.len()), + ); + } + brt_exec(ls.as_slice(), *d, nl, h, start, labs, out); + }, + Node::Exit(c, _) => { + emit_exit(out, *c); + }, + _ => {}, + } +} + +/// Append the code of `n` at the absolute position `start`, with the labels in +/// scope at the absolute positions `labs`: exactly [`ctmpl`]. Returns the +/// flow and the size, as [`analyze`] does. +fn emit_node( + n: &Node, + nl: usize, + h: usize, + start: usize, + labs: &[usize], + out: &mut Vec, +) -> (r: (Option, usize)) + requires + emit_pre(*n, nl, h, start, labs@), + ensures + final(out)@ == old(out)@ + ctmpl( + *n, + nl as nat, + h as nat, + offs_of(labs@, start as int), + ), + cf_flow(*n, nl as nat, h as nat, labs@.len()) == Some(fl_spec(r.0)), + r.1 == csize(*n, nl as nat, h as nat, labs@.len()), + match r.0 { + Some(x) => x <= 31, + None => true, + }, + decreases *n, 1nat, +{ + let (flow, size) = match analyze(n, nl, h, labs) { + Some(x) => x, + None => { + proof { + assert(false); + } + return (None, 0); + }, + }; + match n { + Node::Then(..) => emit_then(n, nl, h, start, labs, out), + Node::Block(..) => emit_block(n, nl, h, start, labs, out), + Node::Loop(..) => emit_loop(n, nl, h, start, labs, out), + Node::If(..) => emit_if(n, nl, h, start, labs, out), + Node::Br(..) => emit_br(n, nl, h, start, labs, out), + Node::BrIf(..) => emit_br(n, nl, h, start, labs, out), + _ => emit_leaf(n, nl, h, start, labs, out), + } + (flow, size) +} + +/// Encode `code` to bytes. +fn encode_code(code: &[x64::Instr]) -> (bytes: Vec) + requires + all_valid_instrs(code@), + ensures + bytes@ == clazel_codecheck::spec::enc_all(code@), +{ + let mut bytes: Vec = Vec::new(); + let mut k: usize = 0; + proof { + assert(code@.take(0) =~= Seq::::empty()); + } + while k < code.len() + invariant + k <= code.len(), + bytes@ == clazel_codecheck::spec::enc_all(code@.take(k as int)), + all_valid_instrs(code@), + decreases code.len() - k, + { + let ins = match code.get(k) { + Some(ins) => ins, + None => { + proof { + assert(false); + } + return bytes; + }, + }; + let e = encode_exec(ins); + bytes.extend_from_slice(e.as_slice()); + proof { + let t = code@.take(k as int + 1); + assert(t.drop_last() =~= code@.take(k as int)); + assert(t.last() == code@[k as int]); + assert(e@ =~= x64::encode(code@[k as int])); + } + k = add_u(k, 1); + } + proof { + assert(code@.take(code@.len() as int) =~= code@); + } + bytes +} + +/// **Every emitted blob refines its program.** If the emitter answers with +/// `bytes`, the encoded body is [`ctmpl`] of the program with the return +/// label at its end, which refines it. +pub proof fn lemma_emitted_refines(n: Node, nl: usize, exit_code: u32, bytes: Seq) + requires + emit_cf_blob_ensures(n, nl, exit_code, Some(bytes)), + ensures + cf_prog_ok(n, nl as nat), + exists|body: Seq| + #![trigger all_valid_instrs(body)] + all_valid_instrs(body) && cf_refines( + n@, + nl as nat, + 0, + seq![code_len(body) as int], + body, + ) && bytes == clazel_codecheck::spec::enc_all(body + exit_seq(exit_code)), +{ +} + +/// Emit the body of the function `n` with `nl` locals as encoded bytes, +/// followed by the exit sequence for `exit_code`. The function's return label +/// (a `br` to it, and falling off the end) is the exit sequence. `None` if the +/// program has no translation. +/// +/// **Proven**: [`crate::cfcontract::emit_cf_blob_ensures`]. +#[verifier::rlimit(500)] +pub fn emit_cf_blob(n: &Node, nl: usize, exit_code: u32) -> (r: Option>) + ensures + emit_cf_blob_ensures(*n, nl, exit_code, match r { + Some(v) => Some(v@), + None => None, + }), + // **Exactly** the template bytes: `crate::cfblobacc::emitted_bytes` + // (an addition to the contract, which says only that some body + // refines; the validator-acceptance theorem is about these bytes). + match r { + Some(v) => v@ == emitted_bytes(*n, nl as nat, exit_code), + None => true, + }, + // **The validator accepts it** (J.9): `crate::cfblobacc::theorem_blob_accepted`. + emit_cf_blob_accepted_ensures(*n, nl, exit_code, match r { + Some(v) => Some(v@), + None => None, + }), +{ + proof { + crate::tlemmas::lemma_abi_consts_t(); + lemma_templated_all(*n); + } + if nl > 31 { + return None; + } + let l1: Vec = push_label(0, &[]); + let size = match analyze(n, nl, 0, l1.as_slice()) { + Some((_, s)) => s, + None => { + return None; + }, + }; + let labs: Vec = push_label(size, &[]); + let mut body: Vec = Vec::new(); + proof { + assert(csize(*n, nl as nat, 0, 1) == size); + } + emit_node(n, nl, 0, 0, labs.as_slice(), &mut body); + let ghost body_seq = body@; + emit_exit(&mut body, exit_code); + proof { + let offs = offs_of(labs@, 0); + assert(offs =~= seq![size as int]); + lemma_ctmpl_valid(*n, nl as nat, 0, offs); + lemma_ctmpl_len(*n, nl as nat, 0, offs); + assert(labels_fit(offs, size as nat)); + lemma_ctmpl_refines(*n, nl as nat, 0, offs); + crate::tlemmas::lemma_exit_valid(exit_code); + assert(Seq::::empty() + body_seq =~= body_seq); + assert(all_valid_instrs(body@)) by { + assert forall|k: int| 0 <= k < body@.len() implies x64::valid_instr( + #[trigger] body@[k], + ) by { + if k < body_seq.len() { + assert(body@[k] == body_seq[k]); + } else { + assert(body@[k] == exit_seq(exit_code)[k - body_seq.len()]); + } + } + } + } + let bytes = encode_code(body.as_slice()); + proof { + let offs = offs_of(labs@, 0); + assert(offs =~= seq![size as int]); + assert(csize(*n, nl as nat, 0, 1) == size); + assert(body_seq =~= ctmpl(*n, nl as nat, 0, offs)) by { + assert(Seq::::empty() + ctmpl(*n, nl as nat, 0, offs) =~= ctmpl( + *n, + nl as nat, + 0, + offs, + )); + } + assert(body@ =~= body_seq + exit_seq(exit_code)); + assert(bytes@ == emitted_bytes(*n, nl as nat, exit_code)); + assert(emit_cf_blob_ensures(*n, nl, exit_code, Some(bytes@))); + lemma_emitter_accepted(*n, nl, exit_code, bytes@); + } + Some(bytes) +} + +} // verus! diff --git a/crates/clazel-jit/src/cfexit.rs b/crates/clazel-jit/src/cfexit.rs new file mode 100644 index 000000000..f9be3c942 --- /dev/null +++ b/crates/clazel-jit/src/cfexit.rs @@ -0,0 +1,222 @@ +//! `exit` with operands, in the control-flow statement. The code is the exit +//! sequence the first slice proved ([`crate::exit::lemma_exit_refines`]); the +//! operands are the top `k` values of the stack, and the exit sequence +//! writes nothing but the exit-code word, so they are still in their frame +//! slots afterwards ([`exit_ops_refines`], [`cf_refines`]). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cfleaf::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::contract::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The exit sequence is in two parts: two plain instructions, then `ret`. +pub proof fn lemma_exit_parts(c: u32) + ensures + exit_seq(c) == seq![ + x64::Instr::MovImm32 { dst: rax(), imm_u32: c }, + x64::Instr::MovStore { + sz: x64::Size::W64, + mem: crate::exit::exit_mem(), + src: rax(), + }, + ] + seq![x64::Instr::Ret], + no_jumps( + seq![ + x64::Instr::MovImm32 { dst: rax(), imm_u32: c }, + x64::Instr::MovStore { + sz: x64::Size::W64, + mem: crate::exit::exit_mem(), + src: rax(), + }, + ], + ), +{ + let pre = seq![ + x64::Instr::MovImm32 { dst: rax(), imm_u32: c }, + x64::Instr::MovStore { sz: x64::Size::W64, mem: crate::exit::exit_mem(), src: rax() }, + ]; + assert(exit_seq(c) =~= pre + seq![x64::Instr::Ret]); +} + +/// [`exit_refines`] used at one state, with fuel 1. +pub proof fn lemma_use_exit( + c: u32, + nl: nat, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + m: x64::State, +) + requires + rel(lay, a, m, nl), + stack_ok(m), + x64::canonical(m.rip + code_len(exit_seq(c))), + ensures + run_seq(cu, m, exit_seq(c)) is Some, + ({ + let m2 = run_seq(cu, m, exit_seq(c)).unwrap(); + let rsp = x64::logext(64, m.rgf[abi::sp_reg() as int] as int); + &&& rel(lay, a, m2, nl) + &&& preserved_mem(m, m2) + &&& exit_done(m, m2, c as nat) + }), +{ + crate::exit::lemma_exit_refines(c, nl); + assert(a.with_stack(lir::below(a.stack, 0)) == a) by { + assert(lir::below(a.stack, 0) =~= a.stack); + } + assert(lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, 1) == lir::Outcome::Exit( + c as nat, + lir::top(a.stack, 0), + a.with_stack(lir::below(a.stack, 0)), + )); +} + +/// **The run of the exit sequence** from a state related to `a`: it reaches a +/// state that is done ([`exit_done`]), still related to `a`, with nothing +/// outside the exit area changed. +#[verifier::rlimit(200)] +pub proof fn lemma_exit_reach( + c: u32, + nl: nat, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + m: x64::State, +) + requires + rel(lay, a, m, nl), + stack_ok(m), + x64::canonical(m.rip + code_len(exit_seq(c))), + ensures + exists|m2: x64::State| + #![trigger reaches(cu, exit_seq(c), m.rip, m, m2)] + reaches(cu, exit_seq(c), m.rip, m, m2) && exit_done(m, m2, c as nat) && rel( + lay, + a, + m2, + nl, + ) && preserved_mem(m, m2), +{ + let code = exit_seq(c); + let pre = seq![ + x64::Instr::MovImm32 { dst: rax(), imm_u32: c }, + x64::Instr::MovStore { sz: x64::Size::W64, mem: crate::exit::exit_mem(), src: rax() }, + ]; + lemma_exit_parts(c); + lemma_use_exit(c, nl, lay, cu, a, m); + let m2 = run_seq(cu, m, code).unwrap(); + super::runseq::lemma_run_seq_append(cu, m, pre, seq![x64::Instr::Ret]); + let m1 = run_seq(cu, m, pre).unwrap(); + super::runseq::lemma_run_seq_one(cu, m1, x64::Instr::Ret); + lemma_reaches_plain_then(cu, pre, x64::Instr::Ret, m, m1, m2); + assert(pre + seq![x64::Instr::Ret] =~= code); +} + +/// **`exit c` with `k` operands.** The code is the exit sequence for `c`. +#[verifier::rlimit(400)] +pub proof fn lemma_exit_cf(c: u32, k: usize, nl: nat, h: nat, offs: Seq) + requires + k <= h, + nl + h <= max_slots(), + ensures + cf_refines(Node::Exit(c, k)@, nl, h, offs, exit_seq(c)), +{ + let code = exit_seq(c); + let pre = seq![ + x64::Instr::MovImm32 { dst: rax(), imm_u32: c }, + x64::Instr::MovStore { sz: x64::Size::W64, mem: crate::exit::exit_mem(), src: rax() }, + ]; + lemma_exit_parts(c); + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, cu, m, lir::eval(lay, Node::Exit(c, k)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, code) implies cf_post( + lay, + base, + nl, + offs, + code, + cu, + m, + lir::eval(lay, Node::Exit(c, k)@, a, fuel), + ) by { + if fuel > 0 && a.stack.len() >= k { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + let n = a.stack.len(); + lemma_exit_reach(c, nl, lay, cu, ac, m); + let m2 = choose|m2: x64::State| + reaches(cu, code, m.rip, m, m2) && exit_done(m, m2, c as nat) && rel(lay, ac, m2, nl) + && preserved_mem(m, m2); + // the outcome + let vals = lir::top(a.stack, k as nat); + let s = a.with_stack(lir::below(a.stack, k as nat)); + assert(lir::eval(lay, Node::Exit(c, k)@, a, fuel) == lir::Outcome::Exit( + c as nat, + vals, + s, + )); + // the relation, below the operands + assert((base + a.stack).take((base + s.stack).len() as int) =~= base + s.stack); + lemma_rel_prefix(lay, a, base + a.stack, base + s.stack, m2, nl); + assert(preserved_mem_r(lay, m, m2)) by { + assert(ctx_of(m2) == ctx_of(m)) by { + crate::steps::lemma_abi_consts(); + } + } + lemma_table_pres(lay, m, m2); + assert(s.with_stack(base + s.stack) == a.with_stack(base + s.stack)); + reveal(rel_base); + assert(rel_base(lay, base + Seq::::empty(), s, m2, nl)) by { + assert(base + Seq::::empty() =~= base); + } + // the operands are still in their slots + assert forall|i: int| 0 <= i < vals.len() implies #[trigger] slot_holds( + m2.mem, + ctx_of(m), + (nl + (base + Seq::::empty()).len() + s.stack.len() + i) as nat, + lir::val_bits(vals[i]), + ) by { + let jj = (nl + base.len() + (n - k) + i) as nat; + assert(vals[i] == a.stack[(n - k) as int + i]); + assert((base + a.stack)[(base.len() + n - k) as int + i] == a.stack[(n - k) as int + + i]); + assert(ctx_of(m2) == ctx_of(m)); + assert(slot_val(ac, nl, jj) == vals[i]); + } + assert(ops_in_frame( + m2.mem, + ctx_of(m), + nl, + (base + Seq::::empty()).len() + s.stack.len(), + vals, + )); + assert(exit_rel(lay, base, s, m2, nl, ctx_of(m), vals)); + assert(reaches(cu, code, m.rip, m, m2) && exit_done(m, m2, c as nat)); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfheight.rs b/crates/clazel-jit/src/cfheight.rs new file mode 100644 index 000000000..a9448b90e --- /dev/null +++ b/crates/clazel-jit/src/cfheight.rs @@ -0,0 +1,364 @@ +//! The static analysis [`cf_flow`] is sound for LIR's evaluation: where +//! it says an instruction falls through at height `h1`, a `Normal` outcome is a +//! stack of that height; where it says `Dead`, there is no `Normal` outcome. +//! Heights are counted from `hb`, the number of values below the instruction's +//! own (fresh) stack, which is what [`crate::cfspec::rel_base`]'s `base` holds. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A straight-line instruction changes the height as `pops` and `pushes` +/// say, counted from `hb`. +pub proof fn lemma_op_height_off( + op: Op, + nl: nat, + h: nat, + hb: nat, + lay: lir::Layout, + a: lir::St, + a2: lir::St, + fuel: nat, +) + requires + op_ok(op, nl, h), + hb + a.stack.len() == h, + lir::eval(lay, op@, a, fuel) == lir::Outcome::Normal(a2), + ensures + hb + a2.stack.len() == height_after_op(op, h), +{ + assert(fuel > 0); + let n = a.stack.len(); + match op { + Op::Nop => {}, + Op::Drop => {}, + Op::Const(v) => {}, + Op::LocalGet(x) => {}, + Op::LocalSet(x) => {}, + Op::LocalTee(x) => {}, + Op::Select => { + match a.stack[n - 1] { + lir::Val::I32(c) => { + assert(a2.stack.len() == (n - 3) + 1); + }, + _ => { + assert(false); + }, + } + }, + Op::Bin(b) => { + match lir::bin_eval(b, a.stack[n - 2], a.stack[n - 1]) { + Some(v) => { + assert(a2.stack.len() == (n - 2) + 1); + }, + None => { + assert(false); + }, + } + }, + Op::Un(u) => { + match lir::un_eval(u, a.stack[n - 1]) { + Some(v) => { + assert(a2.stack.len() == (n - 1) + 1); + }, + None => { + assert(false); + }, + } + }, + } +} + +/// An instruction the analysis calls [`Flow::Dead`] never has a `Normal` +/// outcome: it branches, exits, or is stuck. +pub proof fn lemma_dead_not_normal( + n: Node, + nl: nat, + h: nat, + depth: nat, + lay: lir::Layout, + a: lir::St, + fuel: nat, +) + requires + cf_flow(n, nl, h, depth) == Some(Flow::Dead), + ensures + !(lir::eval(lay, n@, a, fuel) is Normal), + decreases n, +{ + match n { + Node::Then(x, y) => { + if fuel > 0 { + let f = (fuel - 1) as nat; + match cf_flow(*x, nl, h, depth) { + Some(Flow::Falls(h1)) => { + match lir::eval(lay, (*x)@, a, f) { + lir::Outcome::Normal(a1) => { + lemma_dead_not_normal(*y, nl, h1, depth, lay, a1, f); + }, + _ => {}, + } + }, + Some(Flow::Dead) => { + lemma_dead_not_normal(*x, nl, h, depth, lay, a, f); + }, + None => {}, + } + } + }, + _ => {}, + } +} + +/// The stack a block or loop body runs on: empty (a void block has no +/// parameters). +pub open spec fn fresh(a: lir::St) -> lir::St { + a.with_stack(seq![]) +} + +/// What `eval` makes of a `block` (`restart == None`) or `loop` +/// (`restart == Some(it)`) with void type, in terms of its body's outcome. +pub open spec fn label_out( + lay: lir::Layout, + body: Node, + st: lir::St, + restart: Option, + f: nat, +) -> lir::Outcome { + lir::finish_label(lay, st.stack, 0, restart, lir::eval(lay, body@, fresh(st), f), f) +} + +/// `eval` of a void `block` is [`label_out`]. +pub proof fn lemma_eval_block(lay: lir::Layout, body: Node, st: lir::St, fuel: nat) + requires + fuel > 0, + ensures + lir::eval(lay, lir::Instr::Block(void_bt(), Box::new(body@)), st, fuel) == label_out( + lay, + body, + st, + None, + (fuel - 1) as nat, + ), +{ + assert(lir::below(st.stack, 0) =~= st.stack); + assert(lir::top(st.stack, 0) =~= seq![]); + assert(st.with_stack(lir::top(st.stack, 0)) == fresh(st)); +} + +/// `eval` of a void `loop` is [`label_out`], restarting the loop. +pub proof fn lemma_eval_loop(lay: lir::Layout, body: Node, st: lir::St, fuel: nat) + requires + fuel > 0, + ensures + lir::eval(lay, lir::Instr::Loop(void_bt(), Box::new(body@)), st, fuel) == label_out( + lay, + body, + st, + Some(lir::Instr::Loop(void_bt(), Box::new(body@))), + (fuel - 1) as nat, + ), +{ + assert(lir::below(st.stack, 0) =~= st.stack); + assert(lir::top(st.stack, 0) =~= seq![]); + assert(st.with_stack(lir::top(st.stack, 0)) == fresh(st)); +} + +/// The body of a block or loop is well formed: its analysis, entered at height +/// `hh` one label deeper, falls through at `hh` or is dead. +pub open spec fn body_fits(body: Node, nl: nat, hh: nat, depth: nat) -> bool { + match cf_flow(body, nl, hh, depth + 1) { + Some(fl) => flow_fits(fl, hh), + None => false, + } +} + +/// **The static heights are right.** If the analysis says `n` falls through +/// at height `h1` when entered at `h`, and the stack has `hb` values under +/// `n`'s own, then a `Normal` outcome's stack has `h1 - hb` values. +#[verifier::rlimit(200)] +pub proof fn lemma_cf_height( + n: Node, + nl: nat, + h: nat, + depth: nat, + h1: nat, + hb: nat, + lay: lir::Layout, + a: lir::St, + a2: lir::St, + fuel: nat, +) + requires + cf_flow(n, nl, h, depth) == Some(Flow::Falls(h1)), + hb + a.stack.len() == h, + lir::eval(lay, n@, a, fuel) == lir::Outcome::Normal(a2), + ensures + hb + a2.stack.len() == h1, + decreases fuel, 2nat, +{ + assert(fuel > 0); + let f = (fuel - 1) as nat; + match n { + Node::Op(o) => { + lemma_op_height_off(o, nl, h, hb, lay, a, a2, fuel); + }, + Node::Mem(m) => { + let k = a.stack.len(); + match m { + MemOp::BoundsCheck { region, size, code } => { + assert(a2 == a); + }, + MemOp::Load { region, ty } => { + assert(a2.stack.len() == k); + }, + MemOp::Store { region, ty } => { + assert(a2.stack.len() == k - 2); + }, + } + }, + Node::Then(x, y) => { + match cf_flow(*x, nl, h, depth) { + Some(Flow::Falls(hx)) => { + match lir::eval(lay, (*x)@, a, f) { + lir::Outcome::Normal(a1) => { + lemma_cf_height(*x, nl, h, depth, hx, hb, lay, a, a1, f); + lemma_cf_height(*y, nl, hx, depth, h1, hb, lay, a1, a2, f); + }, + _ => {}, + } + }, + _ => {}, + } + }, + Node::Block(b) => { + lemma_eval_block(lay, *b, a, fuel); + lemma_label_height(*b, nl, h, depth, hb, lay, a, a2, f, false); + }, + Node::Loop(b) => { + lemma_eval_loop(lay, *b, a, fuel); + lemma_label_height(*b, nl, h, depth, hb, lay, a, a2, f, true); + }, + Node::If(t, e) => { + let k = a.stack.len(); + match a.stack[k - 1 as int] { + lir::Val::I32(c) => { + let a0 = a.with_stack(lir::below(a.stack, 1)); + let arm = if c != 0 { *t } else { *e }; + assert(k >= 1); + assert(f > 0) by { + if f == 0 { + assert(lir::eval(lay, lir::Instr::Block(void_bt(), Box::new(arm@)), a0, 0) + == lir::Outcome::OutOfFuel); + } + } + lemma_eval_block(lay, arm, a0, f); + lemma_label_height(arm, nl, (h - 1) as nat, depth, hb, lay, a0, a2, (f - 1) as nat, false); + }, + _ => {}, + } + }, + Node::BrIf(l) => { + let k = a.stack.len(); + match a.stack[k - 1 as int] { + lir::Val::I32(c) => {}, + _ => {}, + } + }, + _ => {}, + } +} + +/// The body of a block or loop leaves the stack as it found it, or branches +/// to the label (which truncates it to what was below): a `Normal` outcome of +/// [`label_out`] has the height `hh`, counted from `hb`. +#[verifier::rlimit(200)] +pub proof fn lemma_label_height( + body: Node, + nl: nat, + hh: nat, + depth: nat, + hb: nat, + lay: lir::Layout, + st: lir::St, + a2: lir::St, + f: nat, + is_loop: bool, +) + requires + body_fits(body, nl, hh, depth), + hb + st.stack.len() == hh, + label_out( + lay, + body, + st, + if is_loop { + Some(lir::Instr::Loop(void_bt(), Box::new(body@))) + } else { + None + }, + f, + ) == lir::Outcome::Normal(a2), + ensures + hb + a2.stack.len() == hh, + decreases f + 1, 1nat, +{ + let restart = if is_loop { + Some(lir::Instr::Loop(void_bt(), Box::new(body@))) + } else { + None + }; + match lir::eval(lay, body@, fresh(st), f) { + lir::Outcome::Normal(s) => { + match cf_flow(body, nl, hh, depth + 1) { + Some(Flow::Falls(hb1)) => { + lemma_cf_height(body, nl, hh, depth + 1, hb1, hh, lay, fresh(st), s, f); + assert(s.stack.len() == 0); + assert(a2.stack =~= st.stack + s.stack); + }, + Some(Flow::Dead) => { + lemma_dead_not_normal(body, nl, hh, depth + 1, lay, fresh(st), f); + }, + None => {}, + } + }, + lir::Outcome::Branch(m, s) => { + if m == 0 { + assert(lir::top(s.stack, 0) =~= seq![]); + let carried = st.stack + lir::top(s.stack, 0); + assert(carried =~= st.stack); + match restart { + None => { + assert(a2.stack =~= st.stack); + }, + Some(i) => { + if f > 0 { + let st1 = s.with_stack(carried); + assert(lir::eval(lay, i, st1, (f - 1) as nat) == lir::Outcome::Normal(a2)); + lemma_cf_height( + Node::Loop(Box::new(body)), + nl, + hh, + depth, + hh, + hb, + lay, + st1, + a2, + (f - 1) as nat, + ); + } + }, + } + } + }, + _ => {}, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfif.rs b/crates/clazel-jit/src/cfif.rs new file mode 100644 index 000000000..1fee6e49d --- /dev/null +++ b/crates/clazel-jit/src/cfif.rs @@ -0,0 +1,908 @@ +//! `if`: the condition test, a `je` over the first arm, the arms, and a `jmp` +//! over the second. LIR evaluates `if` as a void `block` around the arm the +//! condition picks, run with the condition popped, so the arm's label 0 is the +//! end of the whole `if`. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfblock::*; +#[cfg(verus_only)] +use crate::cfcompose::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cfheight::*; +#[cfg(verus_only)] +use crate::cfleaf::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::contract::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The length of the condition test. +pub open spec fn if_p(nl: nat, h: nat) -> int { + cond_len(nl, h) as int +} + +/// The code of an `if` whose arms have the code `ct` and `ce`. +pub open spec fn if_code(nl: nat, h: nat, ct: Seq, ce: Seq) -> Seq< + x64::Instr, +> { + cond_pre(nl, h, 4, code_len(ct) as int + 5) + ct + seq![jmp_i(code_len(ce) as int)] + ce +} + +/// The labels in scope in the first arm: its own (the end of the `if`), then +/// the outer ones. +pub open spec fn if_offs_t( + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, +) -> Seq { + seq![code_len(ct) as int + 5 + code_len(ce) as int] + shift(offs, if_p(nl, h)) +} + +/// The labels in scope in the second arm. +pub open spec fn if_offs_e( + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, +) -> Seq { + seq![code_len(ce) as int] + shift(offs, if_p(nl, h) + code_len(ct) as int + 5) +} + +/// A `jmp` in the middle of `pre + [jmp] + post`, reached at the end of `pre`, +/// is one step. +pub proof fn lemma_reach_jmp( + cu: spec_fn(nat) -> nat, + pre: Seq, + post: Seq, + m: x64::State, + m4: x64::State, + rel: int, +) + requires + m4.rip == m.rip + code_len(pre), + x64::canonical(m4.rip + 5), + x64::canonical(m4.rip + 5 + (rel as i32) as int), + ensures + reaches( + cu, + pre + seq![jmp_i(rel)] + post, + m.rip, + m4, + x64::State { rip: m4.rip + 5 + (rel as i32) as int, ..m4 }, + ), +{ + let whole = pre + seq![jmp_i(rel)] + post; + let m5 = x64::State { rip: m4.rip + 5 + (rel as i32) as int, ..m4 }; + assert(whole =~= pre + (seq![jmp_i(rel)] + post)); + lemma_fetch_append_r(pre, seq![jmp_i(rel)] + post, code_len(pre) as int); + assert(code_len(pre) as int - code_len(pre) == 0); + assert(fetch(seq![jmp_i(rel)] + post, 0) == Some(jmp_i(rel))); + assert(m4.rip - m.rip == code_len(pre)); + lemma_step_jmp(cu, m4, rel); + reveal_with_fuel(steps, 2); + assert(steps(cu, whole, m.rip, m4, 1) == Some(m5)); +} + +/// `rel` survives a step that changes only `rip` and flags. +pub proof fn lemma_rel_regs_c2( + lay: lir::Layout, + a: lir::St, + m: x64::State, + m3: x64::State, + nl: nat, +) + requires + rel(lay, a, m, nl), + m3.mem == m.mem, + m3.rgf == m.rgf, + m3.xmm.len() == 16, + x64::canonical(m3.rip), + ensures + rel(lay, a, m3, nl), +{ + super::rel::lemma_rel_regs(lay, a, m, m3, nl); +} + + +/// Where the arm starts, from the `if`'s first byte. +pub open spec fn if_q(is_then: bool, nl: nat, h: nat, ct: Seq) -> int { + if is_then { + if_p(nl, h) + } else { + if_p(nl, h) + code_len(ct) as int + 5 + } +} + +/// The arm's code. +pub open spec fn if_cx(is_then: bool, ct: Seq, ce: Seq) -> Seq { + if is_then { + ct + } else { + ce + } +} + +/// The labels in scope in the arm. +pub open spec fn if_offs_x( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, +) -> Seq { + if is_then { + if_offs_t(nl, h, offs, ct, ce) + } else { + if_offs_e(nl, h, offs, ct, ce) + } +} + +/// The code before the arm. +pub open spec fn if_pre( + is_then: bool, + nl: nat, + h: nat, + ct: Seq, + ce: Seq, +) -> Seq { + let cp = cond_pre(nl, h, 4, code_len(ct) as int + 5); + if is_then { + cp + } else { + cp + ct + seq![jmp_i(code_len(ce) as int)] + } +} + +/// The code after the arm. +pub open spec fn if_post( + is_then: bool, + nl: nat, + h: nat, + ct: Seq, + ce: Seq, +) -> Seq { + if is_then { + seq![jmp_i(code_len(ce) as int)] + ce + } else { + Seq::empty() + } +} + +/// The whole code is the code before the arm, the arm, and the code after. +pub proof fn lemma_if_split(is_then: bool, nl: nat, h: nat, ct: Seq, ce: Seq) + ensures + if_pre(is_then, nl, h, ct, ce) + if_cx(is_then, ct, ce) + if_post(is_then, nl, h, ct, ce) + == if_code(nl, h, ct, ce), + code_len(if_pre(is_then, nl, h, ct, ce)) == if_q(is_then, nl, h, ct), + code_len(if_code(nl, h, ct, ce)) == if_p(nl, h) + code_len(ct) + 5 + code_len(ce), + if_code(nl, h, ct, ce) == cond_pre(nl, h, 4, code_len(ct) as int + 5) + (ct + seq![jmp_i( + code_len(ce) as int, + )] + ce), + is_then ==> if_pre(is_then, nl, h, ct, ce) + if_cx(is_then, ct, ce) == cond_pre( + nl, + h, + 4, + code_len(ct) as int + 5, + ) + ct, + !is_then ==> if_pre(is_then, nl, h, ct, ce) + if_cx(is_then, ct, ce) == if_code( + nl, + h, + ct, + ce, + ), +{ + let cp = cond_pre(nl, h, 4, code_len(ct) as int + 5); + let jm = jmp_i(code_len(ce) as int); + lemma_cond_pre_len(nl, h, 4, code_len(ct) as int + 5); + lemma_jmp_len(code_len(ce) as int); + super::runseq::lemma_code_len_one(jm); + super::runseq::lemma_code_len_append(cp, ct); + super::runseq::lemma_code_len_append(cp + ct, seq![jm]); + super::runseq::lemma_code_len_append(cp + ct + seq![jm], ce); + assert(Seq::::empty() + ce =~= ce); + assert(cp + ct + seq![jm] + ce =~= cp + (ct + seq![jm] + ce)); + if is_then { + assert(cp + ct + (seq![jm] + ce) =~= cp + ct + seq![jm] + ce); + } else { + assert(cp + ct + seq![jm] + ce + Seq::::empty() =~= cp + ct + seq![jm] + ce); + } +} + +/// **What running the condition test and the `je` does**, for `if`: the machine +/// is at the start of the arm the condition picks, related to the `if`'s state +/// with the condition popped, and the arm's statement can be used there. +pub open spec fn if_head( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + m3: x64::State, +) -> bool { + let a0 = a.with_stack(lir::below(a.stack, 1)); + let cx = if_cx(is_then, ct, ce); + let q = if_q(is_then, nl, h, ct); + &&& m3.rip == m.rip + q + &&& reaches(cu, cond_pre(nl, h, 4, code_len(ct) as int + 5), m.rip, m, m3) + &&& rel_base(lay, base + a0.stack, fresh(a0), m3, nl) + &&& preserved_r(lay, m, m3) + &&& stack_ok_r(lay, m3) + &&& cf_canon(m3.rip, if_offs_x(is_then, nl, h, offs, ct, ce), cx) +} + +/// **The head of an `if`**: the condition test and the `je` take the machine to +/// the start of the arm ([`if_head`]). +#[verifier::rlimit(400)] +pub proof fn lemma_if_head( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + c: u32, +) + requires + 1 <= h, + nl + h <= max_slots(), + code_len(ct) < 0x4000_0000, + code_len(ce) < 0x4000_0000, + base.len() + a.stack.len() == h, + a.stack.len() >= 1, + a.stack[(a.stack.len() - 1) as int] == lir::Val::I32(c), + (c != 0) == is_then, + rel_base(lay, base, a, m, nl), + stack_ok_r(lay, m), + cf_canon(m.rip, offs, if_code(nl, h, ct, ce)), + ensures + exists|m3: x64::State| if_head(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, m3), +{ + super::steps::lemma_abi_consts(); + lemma_if_split(is_then, nl, h, ct, ce); + let p = if_p(nl, h); + let st = code_len(ct) as int; + let se = code_len(ce) as int; + let cp = cond_pre(nl, h, 4, st + 5); + let whole = if_code(nl, h, ct, ce); + let ee = p + st + 5 + se; + let j = (nl + h - 1) as nat; + let i1 = load_slot(rax(), j); + let i2 = x64::Instr::TestRR { sz: x64::Size::W32, a: rax(), b: rax() }; + let i3 = jcc_i(4, st + 5); + let pre = seq![i1, i2]; + lemma_cond_pre_len(nl, h, 4, st + 5); + lemma_jcc_len(4, st + 5); + super::runseq::lemma_len3(i1, i2, i3); + super::runseq::lemma_len2(i1, i2); + assert(cp == pre + seq![i3]); + assert(code_len(cp) == p); + assert(code_len(pre) + 6 == p); + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + assert(ac.stack[(h - 1) as int] == lir::Val::I32(c)); + assert(x64::canonical(m.rip + ee)); + lemma_cond_test(cu, lay, ac, m, nl, h, c, ee as nat); + let m2 = run_seq(cu, m, pre).unwrap(); + assert(m2.rip == m.rip + code_len(pre)); + assert(x64::canonical(m2.rip + 6)) by { + super::runseq::lemma_canonical_between(m.rip, m.rip + p, m.rip + ee); + } + assert(x64::canonical(m2.rip + 6 + (st + 5))) by { + super::runseq::lemma_canonical_between(m.rip, m.rip + p + st + 5, m.rip + ee); + } + lemma_step_jcc(cu, m2, 4, st + 5); + let m3 = x64::step(cu, m2, i3).unwrap(); + assert(no_jumps(pre)); + lemma_reaches_plain_then(cu, pre, i3, m, m2, m3); + let q = if_q(is_then, nl, h, ct); + assert(m3.rip == m.rip + q); + assert(reaches(cu, cp, m.rip, m, m3)); + // relation at m3 + lemma_rel_regs_c2(lay, ac, m2, m3, nl); + let a0 = a.with_stack(lir::below(a.stack, 1)); + assert((base + a.stack).take((base + a0.stack).len() as int) =~= base + a0.stack); + lemma_rel_prefix(lay, a, base + a.stack, base + a0.stack, m3, nl); + lemma_preserved_same(lay, m, m3); + lemma_table_pres(lay, m, m3); + lemma_stack_pres(lay, m, m3); + assert(a0.with_stack(base + a0.stack) == a.with_stack(base + a0.stack)); + reveal(rel_base); + assert(rel_base(lay, base, a0, m3, nl)); + lemma_rel_base_enter(lay, base, a0, m3, nl); + // the arm's labels are canonical + let cx = if_cx(is_then, ct, ce); + let offs_x = if_offs_x(is_then, nl, h, offs, ct, ce); + assert(x64::canonical(m.rip)); + assert(code_len(cx) <= (if is_then { st } else { se })); + super::runseq::lemma_canonical_between(m.rip, m3.rip + code_len(cx), m.rip + ee); + assert forall|l: int| 0 <= l < offs_x.len() implies x64::canonical( + m3.rip + #[trigger] offs_x[l], + ) by { + if l == 0 { + assert(offs_x[0] == ee - q); + } else { + lemma_shift_index(offs, q, l - 1); + assert(offs_x[l] == offs[l - 1] - q); + assert(x64::canonical(m.rip + offs[l - 1])); + } + } + assert(cf_canon(m3.rip, offs_x, cx)); + assert(if_head(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, m3)); +} + + +/// The condition test's run, in the whole `if`'s code. +pub proof fn lemma_head_reach_whole( + nl: nat, + h: nat, + ct: Seq, + ce: Seq, + cu: spec_fn(nat) -> nat, + m: x64::State, + m3: x64::State, +) + requires + reaches(cu, cond_pre(nl, h, 4, code_len(ct) as int + 5), m.rip, m, m3), + ensures + reaches(cu, if_code(nl, h, ct, ce), m.rip, m, m3), +{ + lemma_if_split(true, nl, h, ct, ce); + lemma_reaches_post( + cu, + cond_pre(nl, h, 4, code_len(ct) as int + 5), + ct + seq![jmp_i(code_len(ce) as int)] + ce, + m.rip, + m, + m3, + ); +} + +/// The arm's reach, embedded in the whole `if`'s code, from the `if`'s start. +pub proof fn lemma_if_arm_reach( + is_then: bool, + nl: nat, + h: nat, + ct: Seq, + ce: Seq, + cu: spec_fn(nat) -> nat, + m: x64::State, + m3: x64::State, + m4: x64::State, +) + requires + m3.rip == m.rip + if_q(is_then, nl, h, ct), + reaches(cu, if_cx(is_then, ct, ce), m3.rip, m3, m4), + ensures + reaches(cu, if_code(nl, h, ct, ce), m.rip, m3, m4), +{ + lemma_if_split(is_then, nl, h, ct, ce); + let pre = if_pre(is_then, nl, h, ct, ce); + let post = if_post(is_then, nl, h, ct, ce); + let cx = if_cx(is_then, ct, ce); + lemma_reaches_embed(cu, pre, cx, post, m3.rip, m3, m4); + assert(m3.rip - code_len(pre) == m.rip); +} + +/// **The arm's run to its end**: from the start of the arm, its code reaches +/// the end of the arm, related to the arm's final state on top of the `if`'s +/// own stack. +#[verifier::rlimit(100)] +pub proof fn lemma_if_arm_end( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + m3: x64::State, + s: lir::St, +) + requires + if_head(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, m3), + cf_post( + lay, + base + a.with_stack(lir::below(a.stack, 1)).stack, + nl, + if_offs_x(is_then, nl, h, offs, ct, ce), + if_cx(is_then, ct, ce), + cu, + m3, + lir::Outcome::Normal(s), + ), + ensures + exists|m4: x64::State| + #![trigger reaches(cu, if_code(nl, h, ct, ce), m.rip, m, m4)] + reaches(cu, if_code(nl, h, ct, ce), m.rip, m, m4) && m4.rip == m.rip + if_q( + is_then, + nl, + h, + ct, + ) + code_len(if_cx(is_then, ct, ce)) && rel_base( + lay, + base, + s.with_stack(a.with_stack(lir::below(a.stack, 1)).stack + s.stack), + m4, + nl, + ) && preserved_r(lay, m, m4), +{ + lemma_if_split(is_then, nl, h, ct, ce); + let a0 = a.with_stack(lir::below(a.stack, 1)); + let cx = if_cx(is_then, ct, ce); + let m4 = choose|m4: x64::State| + reaches(cu, cx, m3.rip, m3, m4) && m4.rip == m3.rip + code_len(cx) && rel_base( + lay, + base + a0.stack, + s, + m4, + nl, + ) && preserved_r(lay, m3, m4); + lemma_head_reach_whole(nl, h, ct, ce, cu, m, m3); + lemma_if_arm_reach(is_then, nl, h, ct, ce, cu, m, m3, m4); + lemma_reaches_trans(cu, if_code(nl, h, ct, ce), m.rip, m, m3, m4); + lemma_pr_trans(lay, m, m3, m4); + lemma_rel_base_cat(lay, base, a0.stack, s, m4, nl); +} + +/// **Running a `jmp` that follows code already run**: the machine at the end of +/// `pre`, in `pre + [jmp] + post`, takes the `jmp`, and stays related. +#[verifier::rlimit(100)] +pub proof fn lemma_jmp_finish( + lay: lir::Layout, + base: Seq, + nl: nat, + pre: Seq, + post: Seq, + cu: spec_fn(nat) -> nat, + m: x64::State, + m4: x64::State, + sn: lir::St, + se: int, +) + requires + reaches(cu, pre + seq![jmp_i(se)] + post, m.rip, m, m4), + m4.rip == m.rip + code_len(pre), + x64::canonical(m4.rip + 5), + x64::canonical(m4.rip + 5 + (se as i32) as int), + rel_base(lay, base, sn, m4, nl), + preserved_r(lay, m, m4), + ensures + exists|m5: x64::State| + #![trigger reaches(cu, pre + seq![jmp_i(se)] + post, m.rip, m, m5)] + reaches(cu, pre + seq![jmp_i(se)] + post, m.rip, m, m5) && m5.rip == m4.rip + 5 + + (se as i32) as int && rel_base(lay, base, sn, m5, nl) && preserved_r(lay, m, m5), +{ + lemma_rel_base_unfold(lay, base, sn, m4, nl); + lemma_reach_jmp(cu, pre, post, m, m4, se); + let m5 = x64::State { rip: m4.rip + 5 + (se as i32) as int, ..m4 }; + lemma_reaches_trans(cu, pre + seq![jmp_i(se)] + post, m.rip, m, m4, m5); + lemma_rel_base_same(lay, base, sn, m4, m5, nl); + lemma_preserved_same(lay, m4, m5); + lemma_pr_trans(lay, m, m4, m5); +} + +/// **The arm falls through.** For the first arm there is the `jmp` over the +/// second still to run. +#[verifier::rlimit(100)] +pub proof fn lemma_if_normal( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + m3: x64::State, + s: lir::St, +) + requires + code_len(ct) < 0x4000_0000, + code_len(ce) < 0x4000_0000, + x64::canonical(m.rip), + cf_canon(m.rip, offs, if_code(nl, h, ct, ce)), + if_head(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, m3), + cf_post( + lay, + base + a.with_stack(lir::below(a.stack, 1)).stack, + nl, + if_offs_x(is_then, nl, h, offs, ct, ce), + if_cx(is_then, ct, ce), + cu, + m3, + lir::Outcome::Normal(s), + ), + ensures + cf_post( + lay, + base, + nl, + offs, + if_code(nl, h, ct, ce), + cu, + m, + lir::Outcome::Normal( + s.with_stack(a.with_stack(lir::below(a.stack, 1)).stack + s.stack), + ), + ), +{ + lemma_if_split(is_then, nl, h, ct, ce); + lemma_if_arm_end(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, m3, s); + let p = if_p(nl, h); + let st = code_len(ct) as int; + let se = code_len(ce) as int; + let whole = if_code(nl, h, ct, ce); + let ee = p + st + 5 + se; + let a0 = a.with_stack(lir::below(a.stack, 1)); + let sn = s.with_stack(a0.stack + s.stack); + let m4 = choose|m4: x64::State| + reaches(cu, whole, m.rip, m, m4) && m4.rip == m.rip + if_q(is_then, nl, h, ct) + code_len( + if_cx(is_then, ct, ce), + ) && rel_base(lay, base, sn, m4, nl) && preserved_r(lay, m, m4); + if is_then { + let cp = cond_pre(nl, h, 4, st + 5); + assert(m4.rip == m.rip + p + st); + assert(x64::canonical(m4.rip + 5)) by { + super::runseq::lemma_canonical_between(m.rip, m4.rip + 5, m.rip + ee); + } + assert(x64::canonical(m4.rip + 5 + (se as i32) as int)) by { + super::runseq::lemma_canonical_between(m.rip, m4.rip + 5 + se, m.rip + ee); + } + assert(code_len(cp + ct) == p + st) by { + lemma_cond_pre_len(nl, h, 4, st + 5); + super::runseq::lemma_code_len_append(cp, ct); + } + assert(whole == (cp + ct) + seq![jmp_i(se)] + ce); + lemma_jmp_finish(lay, base, nl, cp + ct, ce, cu, m, m4, sn, se); + } else { + assert(m4.rip == m.rip + ee); + } +} + +/// **A branch to the arm's own label** is the `if` ending. +pub proof fn lemma_if_br0( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + m3: x64::State, + s: lir::St, +) + requires + code_len(ct) < 0x4000_0000, + code_len(ce) < 0x4000_0000, + if_head(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, m3), + cf_post( + lay, + base + a.with_stack(lir::below(a.stack, 1)).stack, + nl, + if_offs_x(is_then, nl, h, offs, ct, ce), + if_cx(is_then, ct, ce), + cu, + m3, + lir::Outcome::Branch(0, s), + ), + ensures + cf_post( + lay, + base, + nl, + offs, + if_code(nl, h, ct, ce), + cu, + m, + lir::Outcome::Normal(s.with_stack(a.with_stack(lir::below(a.stack, 1)).stack)), + ), +{ + lemma_if_split(is_then, nl, h, ct, ce); + let a0 = a.with_stack(lir::below(a.stack, 1)); + let cx = if_cx(is_then, ct, ce); + let offs_x = if_offs_x(is_then, nl, h, offs, ct, ce); + let q = if_q(is_then, nl, h, ct); + let ee = if_p(nl, h) + code_len(ct) as int + 5 + code_len(ce) as int; + let m4 = choose|m4: x64::State| + reaches(cu, cx, m3.rip, m3, m4) && m4.rip == m3.rip + offs_x[0] && rel_ext( + lay, + base + a0.stack, + s, + m4, + nl, + ) && preserved_r(lay, m3, m4); + lemma_head_reach_whole(nl, h, ct, ce, cu, m, m3); + lemma_if_arm_reach(is_then, nl, h, ct, ce, cu, m, m3, m4); + lemma_reaches_trans(cu, if_code(nl, h, ct, ce), m.rip, m, m3, m4); + lemma_pr_trans(lay, m, m3, m4); + lemma_br0_rel(lay, base, a0.stack, s, m4, nl); + assert(offs_x[0] == ee - q); + assert(m4.rip == m.rip + ee); +} + +/// **The arm branches past the `if`, or exits**: the outcome is the `if`'s, +/// seen from the start of the code. +pub proof fn lemma_if_out( + is_then: bool, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + m3: x64::State, + o: lir::Outcome, + o2: lir::Outcome, +) + requires + code_len(ct) < 0x4000_0000, + code_len(ce) < 0x4000_0000, + !(o2 is Normal), + if_head(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, m3), + stack_ok_r(lay, m), + cf_post( + lay, + base, + nl, + shift(offs, if_q(is_then, nl, h, ct)), + if_cx(is_then, ct, ce), + cu, + m3, + o2, + ), + ensures + cf_post(lay, base, nl, offs, if_code(nl, h, ct, ce), cu, m, o2), +{ + lemma_if_split(is_then, nl, h, ct, ce); + let cx = if_cx(is_then, ct, ce); + let q = if_q(is_then, nl, h, ct); + let pre = if_pre(is_then, nl, h, ct, ce); + let post = if_post(is_then, nl, h, ct, ce); + let whole = if_code(nl, h, ct, ce); + assert forall|l: int| 0 <= l < offs.len() implies #[trigger] shift(offs, q)[l] + code_len(pre) + == offs[l] by { + lemma_shift_index(offs, q, l); + } + // the run from m to m3, inside pre + cx + let cp = cond_pre(nl, h, 4, code_len(ct) as int + 5); + assert(reaches(cu, cp, m.rip, m, m3)); + if is_then { + lemma_reaches_post(cu, cp, ct, m.rip, m, m3); + assert(pre + cx == cp + ct); + } else { + lemma_reaches_post(cu, cp, ct + seq![jmp_i(code_len(ce) as int)] + ce, m.rip, m, m3); + assert(pre + cx == whole); + assert(whole == cp + (ct + seq![jmp_i(code_len(ce) as int)] + ce)); + } + assert(reaches(cu, pre + cx, m.rip, m, m3)); + lemma_lift_pre(lay, base, nl, offs, shift(offs, q), pre, cx, cu, m, m3, o2); + lemma_lift_post(lay, base, nl, offs, pre + cx, post, cu, m, o2); +} + +/// **`if`.** If the arms' code refines the arms, with the labels as the `if` +/// puts them (the `if`'s own label, at its end, in front), the `if`'s code +/// refines the `if`. +#[verifier::rlimit(150)] +pub proof fn lemma_if( + t: Node, + e: Node, + nl: nat, + h: nat, + offs: Seq, + ct: Seq, + ce: Seq, +) + requires + 1 <= h, + nl + h <= max_slots(), + code_len(ct) < 0x4000_0000, + code_len(ce) < 0x4000_0000, + cf_refines(t@, nl, (h - 1) as nat, if_offs_t(nl, h, offs, ct, ce), ct), + cf_refines(e@, nl, (h - 1) as nat, if_offs_e(nl, h, offs, ct, ce), ce), + ensures + cf_refines( + Node::If(Box::new(t), Box::new(e))@, + nl, + h, + offs, + if_code(nl, h, ct, ce), + ), +{ + let node = Node::If(Box::new(t), Box::new(e)); + let whole = if_code(nl, h, ct, ce); + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, whole, cu, m, lir::eval(lay, node@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, whole) implies cf_post( + lay, + base, + nl, + offs, + whole, + cu, + m, + lir::eval(lay, node@, a, fuel), + ) by { + if fuel > 0 { + let f = (fuel - 1) as nat; + let n = a.stack.len(); + if n >= 1 { + match a.stack[(n - 1) as int] { + lir::Val::I32(c) => { + let is_then = c != 0; + let arm = if is_then { + t + } else { + e + }; + let a0 = a.with_stack(lir::below(a.stack, 1)); + assert(f > 0 || lir::eval(lay, lir::Instr::Block(void_bt(), Box::new(arm@)), a0, f) + == lir::Outcome::OutOfFuel); + if f > 0 { + lemma_rel_base_unfold(lay, base, a, m, nl); + lemma_if_head(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, c); + let m3 = choose|m3: x64::State| + if_head(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, m3); + lemma_eval_block(lay, arm, a0, f); + let g = (f - 1) as nat; + let cx = if_cx(is_then, ct, ce); + let offs_x = if_offs_x(is_then, nl, h, offs, ct, ce); + let q = if_q(is_then, nl, h, ct); + assert((base + a0.stack).len() + fresh(a0).stack.len() == (h - 1) as nat); + lemma_use_cf( + arm@, + nl, + (h - 1) as nat, + offs_x, + cx, + lay, + cu, + base + a0.stack, + fresh(a0), + m3, + g, + ); + let o = lir::eval(lay, arm@, fresh(a0), g); + match o { + lir::Outcome::Normal(s) => { + lemma_if_normal(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, m3, s); + }, + lir::Outcome::Branch(k, s) => { + if k == 0 { + assert(lir::top(s.stack, 0) =~= Seq::::empty()); + assert(a0.stack + lir::top(s.stack, 0) =~= a0.stack); + lemma_if_br0(is_then, nl, h, offs, ct, ce, lay, cu, base, a, m, m3, s); + } else { + assert(k < offs_x.len()); + assert(((k - 1) as nat) < shift(offs, q).len()); + assert(offs_x[k as int] == shift(offs, q)[((k - 1) as nat) as int]); + lemma_blk_brn( + lay, + base, + a0.stack, + nl, + shift(offs, q), + cx, + cu, + m3, + k, + s, + offs_x, + ); + lemma_if_out( + is_then, + nl, + h, + offs, + ct, + ce, + lay, + cu, + base, + a, + m, + m3, + o, + lir::Outcome::Branch((k - 1) as nat, s), + ); + } + }, + lir::Outcome::Exit(code, vals, s) => { + lemma_blk_exit( + lay, + base, + a0.stack, + nl, + shift(offs, q), + cx, + cu, + m3, + code, + vals, + s, + offs_x, + ); + lemma_if_out( + is_then, + nl, + h, + offs, + ct, + ce, + lay, + cu, + base, + a, + m, + m3, + o, + lir::Outcome::Exit(code, vals, s), + ); + }, + _ => {}, + } + } + }, + _ => {}, + } + } + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfjt.rs b/crates/clazel-jit/src/cfjt.rs new file mode 100644 index 000000000..a96545ddd --- /dev/null +++ b/crates/clazel-jit/src/cfjt.rs @@ -0,0 +1,900 @@ +//! Where the jumps of the templates land (jit.md item J.9, "every emitted +//! blob is accepted", x86-64): every jump target of a node's code is one of +//! the labels it was given or one of [`bs`] (the node's own start, end, the +//! starts and ends of its parts, and the stub after a memory instruction). +//! +//! `jt c x` says that some jump of `c` lands on byte `x` of `c`, computed as +//! the validator does ([`clazel_codecheck::spec::jump_targets`]: the end of +//! the jump plus its displacement). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfaccept::*; +#[cfg(verus_only)] +use crate::cfbs::*; +#[cfg(verus_only)] +use crate::cfcompose::lemma_shift_index; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfproofs::{labels_fit, lemma_fit_cons, lemma_fit_mono, lemma_fit_shift}; +#[cfg(verus_only)] +use crate::cfsimple::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::cfunits::*; +#[cfg(verus_only)] +use crate::runseq::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Some jump of `c` lands on byte `x` of `c`. +pub open spec fn jt(c: Seq, x: int) -> bool { + exists|j: int| + 0 <= j < c.len() && #[trigger] clazel_codecheck::spec::jump_rel(c[j]) is Some && x + == clazel_codecheck::spec::start_of(c, j + 1) + clazel_codecheck::spec::jump_rel( + c[j], + )->Some_0 +} + +/// `x` is one of the labels. +pub open spec fn inoffs(offs: Seq, x: int) -> bool { + exists|l: int| 0 <= l < offs.len() && #[trigger] offs[l] == x +} + +// ---------------------------------------------------------------------------- +// Positions in a list of instructions +// ---------------------------------------------------------------------------- + +/// The byte position of instruction `j` is the length of the first `j`. +pub proof fn lemma_start_of_take(c: Seq, j: int) + requires + 0 <= j <= c.len(), + ensures + clazel_codecheck::spec::start_of(c, j) == code_len(c.take(j)), + decreases j, +{ + if j == 0 { + assert(c.take(0) =~= Seq::::empty()); + } else { + lemma_start_of_take(c, j - 1); + assert(c.take(j) =~= c.take(j - 1) + seq![c[j - 1]]); + lemma_code_len_append(c.take(j - 1), seq![c[j - 1]]); + lemma_code_len_one(c[j - 1]); + } +} + +/// The position of the end of the list is its length. +pub proof fn lemma_start_of_end(c: Seq) + ensures + clazel_codecheck::spec::start_of(c, c.len() as int) == code_len(c), +{ + lemma_start_of_take(c, c.len() as int); + assert(c.take(c.len() as int) =~= c); +} + +/// Positions inside the first part of a concatenation. +pub proof fn lemma_start_of_cat_l(a: Seq, b: Seq, j: int) + requires + 0 <= j <= a.len(), + ensures + clazel_codecheck::spec::start_of(a + b, j) == clazel_codecheck::spec::start_of(a, j), +{ + lemma_start_of_take(a + b, j); + lemma_start_of_take(a, j); + assert((a + b).take(j) =~= a.take(j)); +} + +/// Positions inside the second part of a concatenation. +pub proof fn lemma_start_of_cat_r(a: Seq, b: Seq, j: int) + requires + 0 <= j <= b.len(), + ensures + clazel_codecheck::spec::start_of(a + b, a.len() + j) == code_len(a) + + clazel_codecheck::spec::start_of(b, j), +{ + lemma_start_of_take(a + b, a.len() + j); + lemma_start_of_take(b, j); + assert((a + b).take(a.len() + j) =~= a + b.take(j)); + lemma_code_len_append(a, b.take(j)); +} + +/// A jump of a concatenation is a jump of a part. +pub proof fn lemma_jt_cat(a: Seq, b: Seq, x: int) + requires + jt(a + b, x), + ensures + jt(a, x) || jt(b, x - code_len(a)), +{ + let j = choose|j: int| + 0 <= j < (a + b).len() && #[trigger] clazel_codecheck::spec::jump_rel((a + b)[j]) is Some + && x == clazel_codecheck::spec::start_of(a + b, j + 1) + + clazel_codecheck::spec::jump_rel((a + b)[j])->Some_0; + if j < a.len() { + assert((a + b)[j] == a[j]); + lemma_start_of_cat_l(a, b, j + 1); + assert(jt(a, x)); + } else { + let k = j - a.len(); + assert((a + b)[j] == b[k]); + lemma_start_of_cat_r(a, b, k + 1); + assert(clazel_codecheck::spec::jump_rel(b[k]) is Some); + assert(x - code_len(a) == clazel_codecheck::spec::start_of(b, k + 1) + + clazel_codecheck::spec::jump_rel(b[k])->Some_0); + assert(jt(b, x - code_len(a))); + } +} + +/// Code with no jump has no jump target. +pub proof fn lemma_jt_none(c: Seq, x: int) + requires + forall|k: int| 0 <= k < c.len() ==> nojump(#[trigger] c[k]), + ensures + !jt(c, x), +{ +} + +/// The only jump is the last instruction. +pub proof fn lemma_jt_last(c: Seq, x: int) + requires + c.len() > 0, + forall|k: int| 0 <= k < c.len() - 1 ==> nojump(#[trigger] c[k]), + clazel_codecheck::spec::jump_rel(c[c.len() - 1]) is Some, + jt(c, x), + ensures + x == code_len(c) + clazel_codecheck::spec::jump_rel(c[c.len() - 1])->Some_0, +{ + let j = choose|j: int| + 0 <= j < c.len() && #[trigger] clazel_codecheck::spec::jump_rel(c[j]) is Some && x + == clazel_codecheck::spec::start_of(c, j + 1) + clazel_codecheck::spec::jump_rel( + c[j], + )->Some_0; + assert(j == c.len() - 1); + lemma_start_of_end(c); +} + +/// A displacement that fits 32 bits survives the cast. +pub proof fn lemma_fit_i32(r: int) + requires + -0x8000_0000 <= r <= 0x7FFF_FFFF, + ensures + (r as i32) as int == r, +{ +} + +/// A `jmp` to `rel`. +pub proof fn lemma_jt_jmp(rel: int, x: int) + requires + -0x8000_0000 <= rel <= 0x7FFF_FFFF, + jt(seq![jmp_i(rel)], x), + ensures + x == 5 + rel, +{ + let c = seq![jmp_i(rel)]; + lemma_jmp_len(rel); + lemma_code_len_one(jmp_i(rel)); + lemma_fit_i32(rel); + assert(clazel_codecheck::spec::jump_rel(c[0]) is Some); + lemma_jt_last(c, x); +} + +// ---------------------------------------------------------------------------- +// The leaves +// ---------------------------------------------------------------------------- + +/// The statement of [`lemma_jt_bs`] for a node. +pub open spec fn jt_facts(n: Node, nl: nat, h: nat, offs: Seq) -> bool { + forall|x: int| + jt(ctmpl(n, nl, h, offs), x) ==> #[trigger] inoffs(offs, x) || bs( + n, + nl, + h, + offs.len(), + x, + ) +} + +/// `br l`: the jump lands on the label. +proof fn lemma_jtf_br(l: usize, nl: nat, h: nat, offs: Seq) + requires + l < offs.len(), + labels_fit(offs, 5), + ensures + jt_facts(Node::Br(l), nl, h, offs), +{ + let rel = offs[l as int] - 5; + assert(-0x8000_0000 <= offs[l as int] <= 0x7FFF_FFFF); + assert forall|x: int| jt(ctmpl(Node::Br(l), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::Br(l), nl, h, offs.len(), x) by { + assert(ctmpl(Node::Br(l), nl, h, offs) == seq![jmp_i(rel)]); + lemma_jt_jmp(rel, x); + assert(offs[l as int] == x); + } +} + +/// The condition test: its `jcc` is the last instruction. +proof fn lemma_jt_cond(nl: nat, h: nat, cc: u8, rel: int, x: int) + requires + 1 <= h, + nl + h <= max_slots(), + -0x8000_0000 <= rel <= 0x7FFF_FFFF, + jt(cond_pre(nl, h, cc, rel), x), + ensures + x == cond_len(nl, h) + rel, +{ + let c = cond_pre(nl, h, cc, rel); + lemma_cond_pre_len(nl, h, cc, rel); + lemma_fit_i32(rel); + assert(c.len() == 3); + assert(forall|k: int| 0 <= k < 2 ==> nojump(#[trigger] c[k])) by { + assert forall|k: int| 0 <= k < 2 implies nojump(c[k]) by { + assert(k == 0 || k == 1); + } + } + assert(clazel_codecheck::spec::jump_rel(c[2]) is Some); + lemma_jt_last(c, x); +} + +/// `br_if l`. +proof fn lemma_jtf_brif(l: usize, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + l < offs.len(), + labels_fit(offs, cond_len(nl, h)), + ensures + jt_facts(Node::BrIf(l), nl, h, offs), +{ + let rel = offs[l as int] - cond_len(nl, h) as int; + assert forall|x: int| jt(ctmpl(Node::BrIf(l), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::BrIf(l), nl, h, offs.len(), x) by { + assert(ctmpl(Node::BrIf(l), nl, h, offs) == cond_pre(nl, h, 5, rel)); + lemma_jt_cond(nl, h, 5, rel, x); + assert(offs[l as int] == x); + } +} + +/// `exit`: no jump. +proof fn lemma_jtf_exit(c: u32, k: usize, nl: nat, h: nat, offs: Seq) + ensures + jt_facts(Node::Exit(c, k), nl, h, offs), +{ + assert forall|x: int| jt(ctmpl(Node::Exit(c, k), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::Exit(c, k), nl, h, offs.len(), x) by { + let e = exit_seq(c); + assert(forall|j: int| 0 <= j < e.len() ==> nojump(#[trigger] e[j])) by { + assert forall|j: int| 0 <= j < e.len() implies nojump(e[j]) by { + assert(j == 0 || j == 1 || j == 2); + } + } + lemma_jt_none(e, x); + } +} + +/// A straight-line instruction: no jump. +proof fn lemma_jtf_op(o: Op, nl: nat, h: nat, offs: Seq) + requires + op_ok(o, nl, h), + ensures + jt_facts(Node::Op(o), nl, h, offs), +{ + lemma_tmpl_straight(o, nl, h); + assert forall|x: int| jt(ctmpl(Node::Op(o), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::Op(o), nl, h, offs.len(), x) by { + let c = tmpl(o, nl, h); + assert(forall|k: int| 0 <= k < c.len() ==> nojump(#[trigger] c[k])) by { + assert forall|k: int| 0 <= k < c.len() implies nojump(c[k]) by { + assert(straight(c[k])); + } + } + lemma_jt_none(c, x); + } +} + +// ---------------------------------------------------------------------------- +// br_table +// ---------------------------------------------------------------------------- + +/// The chain from entry `i` on: a jump lands on a target label. +proof fn lemma_jt_entries(ls: Seq, offs: Seq, p0: int, i: nat, x: int) + requires + i <= ls.len(), + forall|k: int| 0 <= k < ls.len() ==> #[trigger] ls[k] < offs.len(), + forall|l: int| + 0 <= l < offs.len() ==> -0x8000_0000 + p0 + ls.len() * 13 + 5 <= #[trigger] offs[l] + && offs[l] <= 0x7FFF_FFFF, + 0 <= p0, + jt(brt_entries(ls, offs, p0, i), x), + ensures + exists|k: int| + i <= k < ls.len() && x + p0 + i * 13 == #[trigger] offs[ls[k] as int], + decreases ls.len() - i, +{ + if i >= ls.len() { + assert(brt_entries(ls, offs, p0, i) =~= Seq::::empty()); + assert(false); + } else { + lemma_brt_entry_len(i, 0); + let rel = brt_rel(offs, ls[i as int], p0 + (i + 1) * brt_e()); + let e = brt_entry(i, rel); + let rest = brt_entries(ls, offs, p0, i + 1); + assert(brt_entries(ls, offs, p0, i) == e + rest); + lemma_jt_cat(e, rest, x); + lemma_brt_entry_len(i, rel); + if jt(e, x) { + assert(ls[i as int] < offs.len()); + let t = ls[i as int] as int; + assert(rel == offs[t] - (p0 + (i + 1) * 13)); + assert((i + 1) * 13 <= ls.len() * 13) by (nonlinear_arith) + requires + i < ls.len(), + ; + assert(-0x8000_0000 <= rel <= 0x7FFF_FFFF); + lemma_fit_i32(rel); + assert(forall|k: int| 0 <= k < e.len() - 1 ==> nojump(#[trigger] e[k])) by { + assert forall|k: int| 0 <= k < e.len() - 1 implies nojump(e[k]) by { + assert(k == 0 || k == 1); + } + } + assert(e.len() == 3); + assert(clazel_codecheck::spec::jump_rel(e[2]) is Some); + lemma_jt_last(e, x); + assert(x + p0 + i * 13 == offs[t]); + assert(offs[ls[i as int] as int] == x + p0 + i * 13); + assert(i <= i && i < ls.len()); + } else { + assert(jt(rest, x - 13)); + lemma_jt_entries(ls, offs, p0, i + 1, x - 13); + let k = choose|k: int| + i + 1 <= k < ls.len() && (x - 13) + p0 + (i + 1) * 13 == #[trigger] offs[ls[k] as int]; + assert(x + p0 + i * 13 == offs[ls[k] as int]); + } + } +} + +/// `br_table`. +proof fn lemma_jtf_brt(ls: Vec, d: usize, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + d < offs.len(), + forall|k: int| 0 <= k < ls@.len() ==> #[trigger] ls@[k] < offs.len(), + labels_fit(offs, 13 + 13 * ls@.len()), + ensures + jt_facts(Node::BrTable(ls, d), nl, h, offs), +{ + crate::cfbrt::lemma_brt_p0(nl, h); + let p0 = brt_p0(nl, h); + let n = ls@.len(); + let a = seq![load_slot(rax(), (nl + h - 1) as nat)]; + let ch = brt_entries(ls@, offs, p0, 0); + let jm = seq![jmp_i(brt_rel(offs, d, p0 + n * brt_e() + 5))]; + let whole = brt_code(ls@, d, nl, h, offs); + lemma_brt_entry_len(0, 0); + assert(whole =~= a + (ch + jm)); + crate::cfbrt::lemma_entries_len(ls@, offs, p0, 0); + assert forall|x: int| jt(ctmpl(Node::BrTable(ls, d), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::BrTable(ls, d), nl, h, offs.len(), x) by { + assert(ctmpl(Node::BrTable(ls, d), nl, h, offs) == whole); + lemma_jt_cat(a, ch + jm, x); + assert(!jt(a, x)) by { + assert(nojump(a[0])); + lemma_jt_none(a, x); + } + lemma_jt_cat(ch, jm, x - code_len(a)); + lemma_len1(load_slot(rax(), (nl + h - 1) as nat)); + lemma_mem_instr_lens((nl + h - 1) as nat, 0); + if jt(ch, x - code_len(a)) { + assert forall|l: int| + 0 <= l < offs.len() implies -0x8000_0000 + p0 + n * 13 + 5 <= #[trigger] offs[l] + && offs[l] <= 0x7FFF_FFFF by {} + lemma_jt_entries(ls@, offs, p0, 0, x - code_len(a)); + let k = choose|k: int| + 0 <= k < n && (x - code_len(a)) + p0 + 0 * 13 == #[trigger] offs[ls@[k] as int]; + assert(offs[ls@[k] as int] == x); + assert(inoffs(offs, x)); + } else { + let z = x - code_len(a) - code_len(ch); + assert(jt(jm, z)); + let rel = brt_rel(offs, d, p0 + n * brt_e() + 5); + assert(rel == offs[d as int] - (13 + 13 * n)); + lemma_fit_i32(rel); + assert(clazel_codecheck::spec::jump_rel(jm[0]) is Some); + lemma_jmp_len(rel); + lemma_code_len_one(jmp_i(rel)); + lemma_jt_last(jm, z); + assert(x == offs[d as int]); + assert(inoffs(offs, x)); + } + } +} + +// ---------------------------------------------------------------------------- +// Memory instructions +// ---------------------------------------------------------------------------- + +/// The check's two exits: the validator's own sequence, whose `jcc`s are the +/// third and fifth instructions. +proof fn lemma_jt_chk(c: clazel_codecheck::spec::Chk, x: int) + requires + c.i == rcx() && c.t == rdx() && c.z == rax() && 0 <= c.k < 16, + jt(clazel_codecheck::spec::check_seq(c), x), + ensures + x == 16 + c.r1 as int || x == 30 + c.r2 as int, +{ + let cs = clazel_codecheck::spec::check_seq(c); + lemma_check_instr_lens(c); + assert(cs[0] == clazel_codecheck::spec::check_instr(0, c)); + assert(cs[1] == clazel_codecheck::spec::check_instr(1, c)); + assert(cs[2] == clazel_codecheck::spec::check_instr(2, c)); + assert(cs[3] == clazel_codecheck::spec::check_instr(3, c)); + assert(cs[4] == clazel_codecheck::spec::check_instr(4, c)); + assert(cs[5] == clazel_codecheck::spec::check_instr(5, c)); + assert(cs[6] == clazel_codecheck::spec::check_instr(6, c)); + assert(cs[7] == clazel_codecheck::spec::check_instr(7, c)); + let j = choose|j: int| + 0 <= j < cs.len() && #[trigger] clazel_codecheck::spec::jump_rel(cs[j]) is Some && x + == clazel_codecheck::spec::start_of(cs, j + 1) + clazel_codecheck::spec::jump_rel( + cs[j], + )->Some_0; + assert(j == 2 || j == 4); + reveal_with_fuel(clazel_codecheck::spec::start_of, 6); + assert(clazel_codecheck::spec::start_of(cs, 3) == 16); + assert(clazel_codecheck::spec::start_of(cs, 5) == 30); +} + +/// A memory instruction: its jumps land on the stub, or at its end. +proof fn lemma_jtf_mem(m: MemOp, nl: nat, h: nat, offs: Seq) + requires + mem_region(m) < 16, + ensures + jt_facts(Node::Mem(m), nl, h, offs), +{ + lemma_mem_csize(m, nl, h, offs.len()); + lemma_mem_parts_len(m, nl, h); + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![jmp_i(code_len(exit_seq(mem_exit_code(m))) as int)]; + let stub = exit_seq(mem_exit_code(m)); + lemma_exit_seq_len(mem_exit_code(m)); + let c0 = mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)); + assert forall|x: int| jt(ctmpl(Node::Mem(m), nl, h, offs), x) implies #[trigger] inoffs( + offs, + x, + ) || bs(Node::Mem(m), nl, h, offs.len(), x) by { + let a1 = pre + chk; + let a2 = a1 + tail; + let a3 = a2 + jm; + assert(ctmpl(Node::Mem(m), nl, h, offs) == mem_code(m, nl, h)); + assert(mem_code(m, nl, h) == a3 + stub); + lemma_jt_cat(a3, stub, x); + assert(!jt(stub, x - code_len(a3))) by { + assert(forall|j: int| 0 <= j < stub.len() ==> nojump(#[trigger] stub[j])) by { + assert forall|j: int| 0 <= j < stub.len() implies nojump(stub[j]) by { + assert(j == 0 || j == 1 || j == 2); + } + } + lemma_jt_none(stub, x - code_len(a3)); + } + lemma_jt_cat(a2, jm, x); + lemma_code_len_append(a1, tail); + lemma_code_len_append(pre, chk); + if jt(jm, x - code_len(a2)) { + lemma_jmp_len(code_len(stub) as int); + lemma_jt_jmp(code_len(stub) as int, x - code_len(a2)); + assert(x == csize(Node::Mem(m), nl, h, offs.len())); + assert(bs(Node::Mem(m), nl, h, offs.len(), x)); + } else { + lemma_jt_cat(a1, tail, x); + assert(jt(a1, x)) by { + if jt(tail, x - code_len(a1)) { + assert(forall|j: int| 0 <= j < tail.len() ==> nojump(#[trigger] tail[j])) by { + assert forall|j: int| 0 <= j < tail.len() implies nojump(tail[j]) by { + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { .. } => { + assert(j == 0 || j == 1); + }, + MemOp::Store { .. } => { + assert(j == 0); + }, + } + } + } + lemma_jt_none(tail, x - code_len(a1)); + } + } + lemma_jt_cat(pre, chk, x); + assert(jt(chk, x - code_len(pre))) by { + if jt(pre, x) { + assert(forall|j: int| 0 <= j < pre.len() ==> nojump(#[trigger] pre[j])) by { + assert forall|j: int| 0 <= j < pre.len() implies nojump(pre[j]) by { + match m { + MemOp::BoundsCheck { .. } => { + assert(j == 0); + }, + MemOp::Load { .. } => { + assert(j == 0 || j == 1); + }, + MemOp::Store { .. } => { + assert(j == 0 || j == 1 || j == 2); + }, + } + } + } + lemma_jt_none(pre, x); + } + } + lemma_jt_chk(c0, x - code_len(pre)); + assert(c0.r1 == mem_r1(m, nl, h) as i32); + assert(c0.r2 == mem_r2(m, nl, h) as i32); + assert(x == stub_off(m, nl, h)); + assert(bs(Node::Mem(m), nl, h, offs.len(), x)); + } + } +} + +// ---------------------------------------------------------------------------- +// Composition +// ---------------------------------------------------------------------------- + +/// `then`. +proof fn lemma_jtf_then(a: Node, b: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(Node::Then(Box::new(a), Box::new(b)), nl, h, offs.len()) is Some, + cf_flow(a, nl, h, offs.len()) is Some, + jt_facts(a, nl, h, offs), + match cf_flow(a, nl, h, offs.len()) { + Some(Flow::Falls(h1)) => jt_facts( + b, + nl, + flow_h(a, nl, h, offs.len()), + shift(offs, csize(a, nl, h, offs.len()) as int), + ), + _ => true, + }, + ensures + jt_facts(Node::Then(Box::new(a), Box::new(b)), nl, h, offs), +{ + let n = Node::Then(Box::new(a), Box::new(b)); + let d = offs.len(); + let sa = csize(a, nl, h, d) as int; + let h1 = flow_h(a, nl, h, d); + let ob = shift(offs, sa); + lemma_ctmpl_len(a, nl, h, offs); + assert forall|x: int| jt(ctmpl(n, nl, h, offs), x) implies #[trigger] inoffs(offs, x) || bs( + n, + nl, + h, + d, + x, + ) by { + lemma_jt_cat(ctmpl(a, nl, h, offs), ctmpl(b, nl, h1, ob), x); + if jt(ctmpl(a, nl, h, offs), x) { + if inoffs(offs, x) { + } else { + assert(bs(a, nl, h, d, x)); + } + } else { + let y = x - sa; + assert(jt(ctmpl(b, nl, h1, ob), y)); + match cf_flow(a, nl, h, d) { + Some(Flow::Falls(_)) => { + assert(ob.len() == d); + if inoffs(ob, y) { + let l = choose|l: int| 0 <= l < ob.len() && #[trigger] ob[l] == y; + lemma_shift_index(offs, sa, l); + assert(offs[l] == x); + assert(inoffs(offs, x)); + } else { + assert(bs(b, nl, h1, d, y)); + } + }, + Some(Flow::Dead) => { + assert(b == Node::Op(Op::Nop)); + assert(ctmpl(b, nl, h1, ob) =~= Seq::::empty()); + assert(false); + }, + None => {}, + } + } + } +} + +/// `block`. +proof fn lemma_jtf_block(b: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(Node::Block(Box::new(b)), nl, h, offs.len()) is Some, + jt_facts(b, nl, h, seq![csize(b, nl, h, offs.len() + 1) as int] + offs), + ensures + jt_facts(Node::Block(Box::new(b)), nl, h, offs), +{ + let n = Node::Block(Box::new(b)); + let d = offs.len(); + let sb = csize(b, nl, h, d + 1) as int; + let ob = seq![sb] + offs; + assert(ob.len() == d + 1); + lemma_bs_facts(b, nl, h, d + 1); + assert forall|x: int| jt(ctmpl(n, nl, h, offs), x) implies #[trigger] inoffs(offs, x) || bs( + n, + nl, + h, + d, + x, + ) by { + assert(jt(ctmpl(b, nl, h, ob), x)); + if inoffs(ob, x) { + let l = choose|l: int| 0 <= l < ob.len() && #[trigger] ob[l] == x; + if l == 0 { + assert(x == sb); + assert(bs(b, nl, h, d + 1, sb)); + } else { + assert(ob[l] == offs[l - 1]); + assert(inoffs(offs, x)); + } + } else { + assert(bs(b, nl, h, d + 1, x)); + } + } +} + +/// `loop`. +proof fn lemma_jtf_loop(b: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(Node::Loop(Box::new(b)), nl, h, offs.len()) is Some, + jt_facts(b, nl, h, seq![0int] + offs), + ensures + jt_facts(Node::Loop(Box::new(b)), nl, h, offs), +{ + let n = Node::Loop(Box::new(b)); + let d = offs.len(); + let ob = seq![0int] + offs; + assert(ob.len() == d + 1); + lemma_bs_facts(b, nl, h, d + 1); + assert forall|x: int| jt(ctmpl(n, nl, h, offs), x) implies #[trigger] inoffs(offs, x) || bs( + n, + nl, + h, + d, + x, + ) by { + assert(jt(ctmpl(b, nl, h, ob), x)); + if inoffs(ob, x) { + let l = choose|l: int| 0 <= l < ob.len() && #[trigger] ob[l] == x; + if l == 0 { + assert(x == 0); + assert(bs(b, nl, h, d + 1, 0)); + } else { + assert(ob[l] == offs[l - 1]); + assert(inoffs(offs, x)); + } + } else { + assert(bs(b, nl, h, d + 1, x)); + } + } +} + +/// `if`. +#[verifier::rlimit(1500)] +proof fn lemma_jtf_if(t: Node, e: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(Node::If(Box::new(t), Box::new(e)), nl, h, offs.len()) is Some, + csize(Node::If(Box::new(t), Box::new(e)), nl, h, offs.len()) <= max_size(), + jt_facts(t, nl, (h - 1) as nat, seq![ + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 5 + csize( + e, + nl, + (h - 1) as nat, + offs.len() + 1, + ) as int, + ] + shift(offs, cond_len(nl, h) as int)), + jt_facts(e, nl, (h - 1) as nat, seq![csize(e, nl, (h - 1) as nat, offs.len() + 1) as int] + + shift( + offs, + cond_len(nl, h) as int + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 5, + )), + ensures + jt_facts(Node::If(Box::new(t), Box::new(e)), nl, h, offs), +{ + let n = Node::If(Box::new(t), Box::new(e)); + let d = offs.len(); + let h0 = (h - 1) as nat; + let p = cond_len(nl, h) as int; + let st = csize(t, nl, h0, d + 1) as int; + let se = csize(e, nl, h0, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p); + let oe = seq![se] + shift(offs, p + st + 5); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_csize_if(t, e, nl, h, d); + lemma_bs_facts(t, nl, h0, d + 1); + lemma_bs_facts(e, nl, h0, d + 1); + let cp = cond_pre(nl, h, 4, st + 5); + let ct = ctmpl(t, nl, h0, ot); + let ce = ctmpl(e, nl, h0, oe); + let jm = seq![jmp_i(se)]; + lemma_ctmpl_len(t, nl, h0, ot); + lemma_ctmpl_len(e, nl, h0, oe); + lemma_cond_pre_len(nl, h, 4, st + 5); + lemma_jmp_len(se); + lemma_code_len_one(jmp_i(se)); + assert(st + 5 + se <= max_size()); + assert forall|x: int| jt(ctmpl(n, nl, h, offs), x) implies #[trigger] inoffs(offs, x) || bs( + n, + nl, + h, + d, + x, + ) by { + assert(ctmpl(n, nl, h, offs) == cp + ct + jm + ce); + lemma_jt_cat(cp + ct + jm, ce, x); + lemma_code_len_append(cp, ct); + lemma_code_len_append(cp + ct, jm); + if jt(cp + ct + jm, x) { + lemma_jt_cat(cp + ct, jm, x); + if jt(cp + ct, x) { + lemma_jt_cat(cp, ct, x); + if jt(cp, x) { + lemma_jt_cond(nl, h, 4, st + 5, x); + assert(bs(e, nl, h0, d + 1, 0)); + assert(x - (p + st + 5) == 0); + } else { + let y = x - p; + assert(jt(ct, y)); + if inoffs(ot, y) { + let l = choose|l: int| 0 <= l < ot.len() && #[trigger] ot[l] == y; + if l == 0 { + assert(x == p + st + 5 + se); + assert(bs(e, nl, h0, d + 1, se)); + } else { + assert(ot[l] == shift(offs, p)[l - 1]); + lemma_shift_index(offs, p, l - 1); + assert(offs[l - 1] == x); + assert(inoffs(offs, x)); + } + } else { + assert(bs(t, nl, h0, d + 1, y)); + } + } + } else { + lemma_jt_jmp(se, x - code_len(cp + ct)); + assert(x == p + st + 5 + se); + assert(bs(e, nl, h0, d + 1, se)); + } + } else { + let y = x - (p + st + 5); + assert(jt(ce, y)); + if inoffs(oe, y) { + let l = choose|l: int| 0 <= l < oe.len() && #[trigger] oe[l] == y; + if l == 0 { + assert(bs(e, nl, h0, d + 1, se)); + } else { + assert(oe[l] == shift(offs, p + st + 5)[l - 1]); + lemma_shift_index(offs, p + st + 5, l - 1); + assert(offs[l - 1] == x); + assert(inoffs(offs, x)); + } + } else { + assert(bs(e, nl, h0, d + 1, y)); + } + } + } +} + +/// **Every jump of a node's code lands on a label it was given, or on a +/// position of [`bs`].** +#[verifier::rlimit(1500)] +pub proof fn lemma_jt_bs(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + csize(n, nl, h, offs.len()) <= max_size(), + labels_fit(offs, csize(n, nl, h, offs.len())), + ensures + jt_facts(n, nl, h, offs), + decreases n, +{ + let d = offs.len(); + let size = csize(n, nl, h, d); + match n { + Node::Op(o) => { + lemma_jtf_op(o, nl, h, offs); + }, + Node::Mem(m) => { + lemma_jtf_mem(m, nl, h, offs); + }, + Node::Then(a, b) => { + lemma_csize_then(*a, *b, nl, h, d); + let sa = csize(*a, nl, h, d); + lemma_fit_mono(offs, size, sa); + lemma_jt_bs(*a, nl, h, offs); + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + assert(flow_h(*a, nl, h, d) == h1); + let sb = csize(*b, nl, h1, d); + let ob = shift(offs, sa as int); + lemma_fit_shift(offs, sa, size, sb); + assert(ob.len() == d); + lemma_jt_bs(*b, nl, h1, ob); + }, + _ => {}, + } + lemma_jtf_then(*a, *b, nl, h, offs); + }, + Node::Block(b) => { + lemma_csize_block(*b, nl, h, d); + let sb = csize(*b, nl, h, d + 1); + let ob = seq![sb as int] + offs; + lemma_fit_mono(offs, size, sb); + lemma_fit_cons(sb as int, offs, sb); + assert(ob.len() == d + 1); + lemma_jt_bs(*b, nl, h, ob); + lemma_jtf_block(*b, nl, h, offs); + }, + Node::Loop(b) => { + lemma_csize_loop(*b, nl, h, d); + let sb = csize(*b, nl, h, d + 1); + let ob = seq![0int] + offs; + lemma_fit_mono(offs, size, sb); + lemma_fit_cons(0, offs, sb); + assert(ob.len() == d + 1); + lemma_jt_bs(*b, nl, h, ob); + lemma_jtf_loop(*b, nl, h, offs); + }, + Node::If(t, e) => { + lemma_csize_if(*t, *e, nl, h, d); + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p); + let oe = seq![se] + shift(offs, p + st + 5); + lemma_fit_shift(offs, p as nat, size, st as nat); + lemma_fit_cons(st + 5 + se, shift(offs, p), st as nat); + lemma_fit_shift(offs, (p + st + 5) as nat, size, se as nat); + lemma_fit_cons(se, shift(offs, p + st + 5), se as nat); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_jt_bs(*t, nl, (h - 1) as nat, ot); + lemma_jt_bs(*e, nl, (h - 1) as nat, oe); + lemma_jtf_if(*t, *e, nl, h, offs); + }, + Node::Br(l) => { + lemma_csize_br(l, nl, h, d); + lemma_jtf_br(l, nl, h, offs); + }, + Node::BrIf(l) => { + lemma_csize_brif(l, nl, h, d); + lemma_jtf_brif(l, nl, h, offs); + }, + Node::BrTable(ls, dd) => { + crate::cfbrt::lemma_brt_fetch_jmp(ls@, dd, nl, h, seq![]); + crate::cflens::lemma_ctmpl_len(n, nl, h, offs); + crate::cfbrt::lemma_brt_fetch_jmp(ls@, dd, nl, h, offs); + assert(size == 13 + 13 * ls@.len()); + lemma_jtf_brt(ls, dd, nl, h, offs); + }, + Node::Exit(c, k) => { + lemma_jtf_exit(c, k, nl, h, offs); + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfleaf.rs b/crates/clazel-jit/src/cfleaf.rs new file mode 100644 index 000000000..32835c0e3 --- /dev/null +++ b/crates/clazel-jit/src/cfleaf.rs @@ -0,0 +1,450 @@ +//! The leaves of the control-flow composition: `br` (a `jmp`), `br_if` (a +//! condition test and a `jcc`) and the pieces they need: one step of a +//! `jmp` and of a `jcc`, the test of a condition word, a run of plain +//! instructions followed by one more, and the relation surviving a change of +//! `rip`. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `jmp rel` goes to the end of the instruction plus `rel`. +pub proof fn lemma_step_jmp(cu: spec_fn(nat) -> nat, m: x64::State, rel: int) + requires + x64::canonical(m.rip + 5), + x64::canonical(m.rip + 5 + (rel as i32) as int), + ensures + x64::step(cu, m, jmp_i(rel)) == Some( + x64::State { rip: m.rip + 5 + (rel as i32) as int, ..m }, + ), +{ + lemma_jmp_len(rel); +} + +/// `jcc cc rel` goes to the end of the instruction plus `rel` when the +/// condition holds, and to the end of the instruction when not. +pub proof fn lemma_step_jcc(cu: spec_fn(nat) -> nat, m: x64::State, cc: u8, rel: int) + requires + x64::canonical(m.rip + 6), + x64::canonical(m.rip + 6 + (rel as i32) as int), + ensures + x64::step(cu, m, jcc_i(cc, rel)) == Some( + x64::State { + rip: if x64::jcc_cmovcc_setcc_spec(m, cc) { + m.rip + 6 + (rel as i32) as int + } else { + m.rip + 6 + }, + ..m + }, + ), +{ + lemma_jcc_len(cc, rel); +} + +/// `test eax, eax` on a word `x` below `2^32` sets `zf` exactly when `x` is +/// zero, and changes nothing but flags and `rip`. +pub proof fn lemma_test_eax(cu: spec_fn(nat) -> nat, m: x64::State, x: u32) + requires + x64::wf(m), + m.rgf[0] == x as u64, + x64::canonical( + m.rip + x64::instr_len(x64::Instr::TestRR { sz: x64::Size::W32, a: 0, b: 0 }), + ), + ensures + x64::step(cu, m, x64::Instr::TestRR { sz: x64::Size::W32, a: 0, b: 0 }) is Some, + ({ + let m1 = x64::step(cu, m, x64::Instr::TestRR { sz: x64::Size::W32, a: 0, b: 0 }).unwrap(); + &&& m1.rgf == m.rgf && m1.mem == m.mem && m1.xmm == m.xmm + &&& m1.rip == m.rip + x64::instr_len( + x64::Instr::TestRR { sz: x64::Size::W32, a: 0, b: 0 }, + ) + &&& x64::jcc_cmovcc_setcc_spec(m1, 4) == (x == 0) + &&& x64::jcc_cmovcc_setcc_spec(m1, 5) == (x != 0) + }), +{ + super::memlemmas::lemma_p2_consts(); + super::flags::lemma_step_test(cu, m, x64::Size::W32, 0, 0); + let m1 = x64::step(cu, m, x64::Instr::TestRR { sz: x64::Size::W32, a: 0, b: 0 }).unwrap(); + assert(x64::rgfi_size(m, x64::Size::W32, 0) == x as int); + assert(x64::loghead(32, x as int) == x as int); + assert(((x as u64) & (x as u64)) == x as u64) by (bit_vector); + assert(x64::zf_bit() == 6); +} + +/// `m2` differs from `m` in registers other than `r15` (and `rip`, flags): +/// the relation, and the region table, carry over. +pub proof fn lemma_rel_base_same( + lay: lir::Layout, + base: Seq, + a: lir::St, + m: x64::State, + m2: x64::State, + nl: nat, +) + requires + rel_base(lay, base, a, m, nl), + m2.mem == m.mem, + m2.rgf.len() == 16, + m2.xmm.len() == 16, + m2.rgf[15] == m.rgf[15], + x64::canonical(m2.rip), + ensures + rel_base(lay, base, a, m2, nl), +{ + reveal(rel_base); + super::steps::lemma_abi_consts(); + super::rel::lemma_rel_regs(lay, a.with_stack(base + a.stack), m, m2, nl); + assert(ctx_of(m2) == ctx_of(m)); +} + +/// A state with the same memory and the same `r15` and `rsp` preserves +/// everything. +pub proof fn lemma_preserved_same(lay: lir::Layout, m: x64::State, m2: x64::State) + requires + m.rgf.len() == 16, + m2.rgf.len() == 16, + m2.mem == m.mem, + m2.rgf[15] == m.rgf[15], + m2.rgf[4] == m.rgf[4], + ensures + preserved_r(lay, m, m2), +{ + assert(ctx_of(m2) == ctx_of(m)); +} + +/// **Plain instructions, then one more**, for an abstract step function `f` +/// (so the solver does not unfold `x64::step` on the unknown instruction +/// `last`; see [`steps_g`]). If `pre` has no jumps and runs from `m` to `m1`, +/// and `last` steps from `m1` to `m2`, then `pre + [last]` placed at `m.rip` +/// reaches `m2` by `steps_g`, with `m1` at the end of `pre`. +pub proof fn lemma_steps_plain_then_g( + f: spec_fn(x64::State, x64::Instr) -> Option, + pre: Seq, + last: x64::Instr, + m: x64::State, + m1: x64::State, + m2: x64::State, +) + requires + rip_plain(f), + no_jumps(pre), + super::runseq::run_gen(f, m, pre) == Some(m1), + f(m1, last) == Some(m2), + ensures + m1.rip == m.rip + code_len(pre), + steps_g(f, pre + seq![last], m.rip, m, pre.len() + 1) == Some(m2), +{ + let whole = pre + seq![last]; + assert(Seq::::empty() + pre + seq![last] =~= whole); + assert(code_len(Seq::::empty()) == 0); + lemma_run_gen_steps(f, Seq::empty(), pre, seq![last], m.rip, m, m1); + super::runseq::lemma_code_len_append(pre, seq![last]); + lemma_fetch_append_r(pre, seq![last], code_len(pre) as int); + assert(fetch(seq![last], 0) == Some(last)); + assert(m1.rip - m.rip == code_len(pre)); + assert(code_len(pre) as int - code_len(pre) == 0); + assert(fetch(whole, m1.rip - m.rip) == Some(last)); + reveal_with_fuel(steps_g, 2); + assert(steps_g(f, whole, m.rip, m1, 1) == Some(m2)); + lemma_steps_g_trans(f, whole, m.rip, m, m1, m2, pre.len(), 1); +} + +/// [`lemma_steps_plain_then_g`] for the machine's step: the `reaches` form. +pub proof fn lemma_reaches_plain_then( + cu: spec_fn(nat) -> nat, + pre: Seq, + last: x64::Instr, + m: x64::State, + m1: x64::State, + m2: x64::State, +) + requires + no_jumps(pre), + run_seq(cu, m, pre) == Some(m1), + x64::step(cu, m1, last) == Some(m2), + ensures + m1.rip == m.rip + code_len(pre), + reaches(cu, pre + seq![last], m.rip, m, m2), +{ + let f = super::runseq::stepper(cu); + lemma_stepper_rip_plain(cu); + super::runseq::lemma_run_seq_is_gen(cu, m, pre); + assert(f(m1, last) == Some(m2)); + lemma_steps_plain_then_g(f, pre, last, m, m1, m2); + lemma_steps_is_gen(cu, pre + seq![last], m.rip, m, pre.len() + 1); +} + +/// **`br l` is a `jmp`.** The code is `jmp` to label `l`; the machine is +/// then at the label, related to the same LIR state. +pub proof fn lemma_br(nl: nat, h: nat, offs: Seq, l: usize) + requires + l < offs.len(), + -0x8000_0000 <= offs[l as int] - 5 < 0x8000_0000, + ensures + cf_refines(Node::Br(l)@, nl, h, offs, seq![jmp_i(offs[l as int] - 5)]), +{ + let rel = offs[l as int] - 5; + let code = seq![jmp_i(rel)]; + lemma_jmp_len(rel); + super::runseq::lemma_code_len_one(jmp_i(rel)); + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, cu, m, lir::eval(lay, Node::Br(l)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, code) implies cf_post( + lay, + base, + nl, + offs, + code, + cu, + m, + lir::eval(lay, Node::Br(l)@, a, fuel), + ) by { + if fuel > 0 { + lemma_rel_base_unfold(lay, base, a, m, nl); + assert(code_len(code) == 5); + assert(x64::canonical(m.rip + offs[l as int])); + lemma_step_jmp(cu, m, rel); + let m2 = x64::State { rip: m.rip + 5 + rel, ..m }; + assert(m2.rip == m.rip + offs[l as int]); + assert(fetch(code, 0) == Some(jmp_i(rel))); + reveal_with_fuel(steps, 2); + assert(steps(cu, code, m.rip, m, 1) == Some(m2)); + assert(reaches(cu, code, m.rip, m, m2)); + assert(base + Seq::::empty() =~= base); + lemma_rel_base_same(lay, base, a, m, m2, nl); + lemma_preserved_same(lay, m, m2); + assert(rel_base(lay, base + Seq::::empty(), a, m2, nl)); + assert(rel_ext(lay, base, a, m2, nl)); + } + } +} + +/// **The condition test.** At height `h` with `nl` locals and the top of the +/// frame's stack holding the `i32` `c`, the first two instructions of +/// [`cond_pre`] (`mov rax, [top]; test eax, eax`) run from `m` to a state +/// `m2` with the relation unchanged, `rax` holding `c`, and `zf` saying whether +/// `c` is zero. `rel_a` is the relation to the LIR state `a`, whose stack +/// is `h` high with `c` on top. +pub proof fn lemma_cond_test( + cu: spec_fn(nat) -> nat, + lay: lir::Layout, + a: lir::St, + m: x64::State, + nl: nat, + h: nat, + c: u32, + rest: nat, +) + requires + rel(lay, a, m, nl), + a.stack.len() == h, + 1 <= h, + a.stack[(h - 1) as int] == lir::Val::I32(c), + x64::canonical(m.rip + rest), + rest >= code_len(seq![ + load_slot(rax(), (nl + h - 1) as nat), + x64::Instr::TestRR { sz: x64::Size::W32, a: rax(), b: rax() }, + ]), + ensures + run_seq(cu, m, seq![ + load_slot(rax(), (nl + h - 1) as nat), + x64::Instr::TestRR { sz: x64::Size::W32, a: rax(), b: rax() }, + ]) is Some, + ({ + let pre = seq![ + load_slot(rax(), (nl + h - 1) as nat), + x64::Instr::TestRR { sz: x64::Size::W32, a: rax(), b: rax() }, + ]; + let m2 = run_seq(cu, m, pre).unwrap(); + &&& m2.rip == m.rip + code_len(pre) + &&& m2.mem == m.mem && m2.rgf.len() == 16 && m2.xmm.len() == 16 + &&& m2.rgf[15] == m.rgf[15] && m2.rgf[4] == m.rgf[4] + &&& rel(lay, a, m2, nl) + &&& x64::jcc_cmovcc_setcc_spec(m2, 4) == (c == 0) + &&& x64::jcc_cmovcc_setcc_spec(m2, 5) == (c != 0) + }), +{ + super::steps::lemma_abi_consts(); + let j = (nl + h - 1) as nat; + let i1 = load_slot(rax(), j); + let i2 = x64::Instr::TestRR { sz: x64::Size::W32, a: rax(), b: rax() }; + let pre = seq![i1, i2]; + super::runseq::lemma_len2(i1, i2); + assert(rax() == 0); + assert(slot_val(a, nl, j) == lir::Val::I32(c)); + assert(j < nl + a.stack.len()); + assert(x64::canonical(m.rip + x64::instr_len(i1))) by { + assert(x64::canonical(m.rip)); + super::runseq::lemma_canonical_between(m.rip, m.rip + x64::instr_len(i1), m.rip + rest); + } + super::rel::lemma_load_rel(cu, lay, a, m, 0, j, nl); + let m1 = x64::step(cu, m, i1).unwrap(); + assert(m1.rgf[0] == c as u64); + assert(x64::canonical(m1.rip + x64::instr_len(i2))) by { + assert(x64::canonical(m.rip)); + super::runseq::lemma_canonical_between( + m.rip, + m.rip + x64::instr_len(i1) + x64::instr_len(i2), + m.rip + rest, + ); + } + assert(x64::wf(m1)); + lemma_test_eax(cu, m1, c); + let m2 = x64::step(cu, m1, i2).unwrap(); + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen2(f, m, i1, i2, m1, m2); + super::runseq::lemma_run_seq_is_gen(cu, m, pre); + assert(m2.rgf == m1.rgf); + assert(m1.rgf == m.rgf.update(0, c as u64)); + assert(m2.rgf[15] == m.rgf[15] && m2.rgf[4] == m.rgf[4]); + assert(m2.rgf.len() == 16 && m2.xmm.len() == 16); + assert(x64::canonical(m2.rip)); + super::rel::lemma_rel_regs(lay, a, m1, m2, nl); +} + +/// **`br_if l`**: `mov rax, [cond]; test eax, eax; jne` to label `l`. +#[verifier::rlimit(400)] +pub proof fn lemma_brif(nl: nat, h: nat, offs: Seq, l: usize) + requires + l < offs.len(), + 1 <= h, + nl + h <= max_slots(), + -0x8000_0000 <= offs[l as int] - (cond_len(nl, h) as int) < 0x8000_0000, + ensures + cf_refines( + Node::BrIf(l)@, + nl, + h, + offs, + cond_pre(nl, h, 5, offs[l as int] - cond_len(nl, h) as int), + ), +{ + super::steps::lemma_abi_consts(); + let pl = cond_len(nl, h) as int; + let rel = offs[l as int] - pl; + let code = cond_pre(nl, h, 5, rel); + let j = (nl + h - 1) as nat; + let i1 = load_slot(rax(), j); + let i2 = x64::Instr::TestRR { sz: x64::Size::W32, a: rax(), b: rax() }; + let i3 = jcc_i(5, rel); + let pre = seq![i1, i2]; + lemma_cond_pre_len(nl, h, 5, rel); + lemma_jcc_len(5, rel); + super::runseq::lemma_len3(i1, i2, i3); + super::runseq::lemma_len2(i1, i2); + assert(code == pre + seq![i3]); + assert(code_len(code) == pl); + assert(code_len(pre) + 6 == pl); + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, cu, m, lir::eval(lay, Node::BrIf(l)@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, code) implies cf_post( + lay, + base, + nl, + offs, + code, + cu, + m, + lir::eval(lay, Node::BrIf(l)@, a, fuel), + ) by { + if fuel > 0 { + let n = a.stack.len(); + if n >= 1 { + match a.stack[(n - 1) as int] { + lir::Val::I32(c) => { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + assert(ac.stack[(h - 1) as int] == lir::Val::I32(c)); + assert(x64::canonical(m.rip + pl)); + lemma_cond_test(cu, lay, ac, m, nl, h, c, pl as nat); + let m2 = run_seq(cu, m, pre).unwrap(); + assert(m2.rip == m.rip + code_len(pre)); + assert(x64::canonical(m2.rip + 6)); + assert(x64::canonical(m.rip + offs[l as int])); + lemma_step_jcc(cu, m2, 5, rel); + let m3 = x64::step(cu, m2, i3).unwrap(); + assert(no_jumps(pre)); + lemma_reaches_plain_then(cu, pre, i3, m, m2, m3); + lemma_rel_regs_c(lay, ac, m2, m3, nl); + let s1 = a.with_stack(lir::below(a.stack, 1)); + assert((base + a.stack).take((base + lir::below(a.stack, 1)).len() as int) + =~= base + lir::below(a.stack, 1)); + lemma_rel_prefix(lay, a, base + a.stack, base + lir::below(a.stack, 1), m3, nl); + lemma_preserved_same(lay, m, m3); + lemma_table_pres(lay, m, m3); + assert(s1.with_stack(base + s1.stack) == a.with_stack( + base + lir::below(a.stack, 1), + )); + reveal(rel_base); + assert(rel_base(lay, base, s1, m3, nl)); + assert(base + Seq::::empty() =~= base); + assert(rel_ext(lay, base, s1, m3, nl)); + assert(reaches(cu, code, m.rip, m, m3)); + if c != 0 { + assert(m3.rip == m.rip + offs[l as int]); + } else { + assert(m3.rip == m.rip + code_len(code)); + } + }, + _ => {}, + } + } + } + } +} + +/// `rel` survives a step that changes only `rip` and flags. +pub proof fn lemma_rel_regs_c( + lay: lir::Layout, + a: lir::St, + m: x64::State, + m3: x64::State, + nl: nat, +) + requires + rel(lay, a, m, nl), + m3.mem == m.mem, + m3.rgf == m.rgf, + m3.xmm.len() == 16, + x64::canonical(m3.rip), + ensures + rel(lay, a, m3, nl), +{ + super::rel::lemma_rel_regs(lay, a, m, m3, nl); +} + +} // verus! diff --git a/crates/clazel-jit/src/cflens.rs b/crates/clazel-jit/src/cflens.rs new file mode 100644 index 000000000..a79337b03 --- /dev/null +++ b/crates/clazel-jit/src/cflens.rs @@ -0,0 +1,245 @@ +//! The lengths of the control-flow templates: a jump's length does not depend +//! on its displacement, so [`csize`] is the length of [`ctmpl`] whatever the +//! labels' offsets are. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `le_bytes(v, n)` has `n` bytes. +pub proof fn lemma_le_bytes_len(v: int, n: nat) + ensures + x64::le_bytes(v, n).len() == n, + decreases n, +{ + if n > 0 { + lemma_le_bytes_len(v / 256, (n - 1) as nat); + } +} + +/// `jmp rel32` is 5 bytes. +pub proof fn lemma_jmp_len(rel: int) + ensures + x64::instr_len(jmp_i(rel)) == 5, +{ + lemma_le_bytes_len(rel as i32 as int, 4); + assert(x64::encode(jmp_i(rel)) == seq![0xE9u8] + x64::le_bytes(rel as i32 as int, 4)); +} + +/// `jcc rel32` is 6 bytes. +pub proof fn lemma_jcc_len(cc: u8, rel: int) + ensures + x64::instr_len(jcc_i(cc, rel)) == 6, +{ + lemma_le_bytes_len(rel as i32 as int, 4); + assert(x64::encode(jcc_i(cc, rel)) == seq![0x0Fu8, (0x80 + cc as int) as u8] + x64::le_bytes( + rel as i32 as int, + 4, + )); +} + +/// The condition test's length does not depend on `cc` or `rel`. +pub proof fn lemma_cond_pre_len(nl: nat, h: nat, cc: u8, rel: int) + ensures + code_len(cond_pre(nl, h, cc, rel)) == cond_len(nl, h), +{ + let a = load_slot(rax(), (nl + h - 1) as nat); + let b = x64::Instr::TestRR { sz: x64::Size::W32, a: rax(), b: rax() }; + lemma_jcc_len(cc, rel); + lemma_jcc_len(0, 0); + super::runseq::lemma_len3(a, b, jcc_i(cc, rel)); + super::runseq::lemma_len3(a, b, jcc_i(0, 0)); +} + +/// The length of a node's code does not depend on the values of the label +/// offsets, only on their number. +pub proof fn lemma_ctmpl_len_eq(n: Node, nl: nat, h: nat, o1: Seq, o2: Seq) + requires + o1.len() == o2.len(), + ensures + code_len(ctmpl(n, nl, h, o1)) == code_len(ctmpl(n, nl, h, o2)), + decreases n, +{ + match n { + Node::Op(o) => {}, + Node::Mem(_) => {}, + Node::Then(a, b) => { + lemma_ctmpl_len_eq(*a, nl, h, o1, o2); + let d = csize(*a, nl, h, o1.len()) as int; + let h1 = flow_h(*a, nl, h, o1.len()); + assert(shift(o1, d).len() == shift(o2, d).len()); + lemma_ctmpl_len_eq(*b, nl, h1, shift(o1, d), shift(o2, d)); + super::runseq::lemma_code_len_append(ctmpl(*a, nl, h, o1), ctmpl(*b, nl, h1, shift(o1, d))); + super::runseq::lemma_code_len_append(ctmpl(*a, nl, h, o2), ctmpl(*b, nl, h1, shift(o2, d))); + }, + Node::Block(b) => { + let s1 = seq![csize(*b, nl, h, o1.len() + 1) as int] + o1; + let s2 = seq![csize(*b, nl, h, o2.len() + 1) as int] + o2; + lemma_ctmpl_len_eq(*b, nl, h, s1, s2); + }, + Node::Loop(b) => { + lemma_ctmpl_len_eq(*b, nl, h, seq![0int] + o1, seq![0int] + o2); + }, + Node::If(t, e) => { + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, o1.len() + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, o1.len() + 1) as int; + let t1 = seq![st + 5 + se] + shift(o1, p); + let t2 = seq![st + 5 + se] + shift(o2, p); + let e1 = seq![se] + shift(o1, p + st + 5); + let e2 = seq![se] + shift(o2, p + st + 5); + lemma_ctmpl_len_eq(*t, nl, (h - 1) as nat, t1, t2); + lemma_ctmpl_len_eq(*e, nl, (h - 1) as nat, e1, e2); + lemma_cond_pre_len(nl, h, 4, st + 5); + lemma_jmp_len(se); + let cp = cond_pre(nl, h, 4, st + 5); + let ct1 = ctmpl(*t, nl, (h - 1) as nat, t1); + let ct2 = ctmpl(*t, nl, (h - 1) as nat, t2); + let ce1 = ctmpl(*e, nl, (h - 1) as nat, e1); + let ce2 = ctmpl(*e, nl, (h - 1) as nat, e2); + super::runseq::lemma_code_len_append(cp, ct1); + super::runseq::lemma_code_len_append(cp + ct1, seq![jmp_i(se)]); + super::runseq::lemma_code_len_append(cp + ct1 + seq![jmp_i(se)], ce1); + super::runseq::lemma_code_len_append(cp, ct2); + super::runseq::lemma_code_len_append(cp + ct2, seq![jmp_i(se)]); + super::runseq::lemma_code_len_append(cp + ct2 + seq![jmp_i(se)], ce2); + super::runseq::lemma_code_len_one(jmp_i(se)); + }, + Node::Br(l) => { + lemma_jmp_len(if l < o1.len() { o1[l as int] - 5 } else { 0 }); + lemma_jmp_len(if l < o2.len() { o2[l as int] - 5 } else { 0 }); + super::runseq::lemma_code_len_one(jmp_i(if l < o1.len() { o1[l as int] - 5 } else { 0 })); + super::runseq::lemma_code_len_one(jmp_i(if l < o2.len() { o2[l as int] - 5 } else { 0 })); + }, + Node::BrIf(l) => { + lemma_cond_pre_len(nl, h, 5, if l < o1.len() { o1[l as int] - cond_len(nl, h) as int } else { 0 }); + lemma_cond_pre_len(nl, h, 5, if l < o2.len() { o2[l as int] - cond_len(nl, h) as int } else { 0 }); + }, + Node::BrTable(ls, d) => { + crate::cfbrt::lemma_brt_fetch_jmp(ls@, d, nl, h, o1); + crate::cfbrt::lemma_brt_fetch_jmp(ls@, d, nl, h, o2); + }, + Node::Exit(c, _) => {}, + } +} + +/// [`csize`] is the length of the code, for any labels. +pub proof fn lemma_ctmpl_len(n: Node, nl: nat, h: nat, offs: Seq) + ensures + code_len(ctmpl(n, nl, h, offs)) == csize(n, nl, h, offs.len()), +{ + lemma_ctmpl_len_eq(n, nl, h, offs, zeros(offs.len())); +} + +/// `csize` of `then`: the first's, and the second's at the height the first +/// leaves. +pub proof fn lemma_csize_then(a: Node, b: Node, nl: nat, h: nat, depth: nat) + ensures + csize(Node::Then(Box::new(a), Box::new(b)), nl, h, depth) == csize(a, nl, h, depth) + + csize(b, nl, flow_h(a, nl, h, depth), depth), +{ + let t = Node::Then(Box::new(a), Box::new(b)); + let z = zeros(depth); + let d = csize(a, nl, h, depth) as int; + let h1 = flow_h(a, nl, h, depth); + assert(z.len() == depth); + lemma_ctmpl_len(b, nl, h1, shift(z, d)); + assert(shift(z, d).len() == depth); + super::runseq::lemma_code_len_append( + ctmpl(a, nl, h, z), + ctmpl(b, nl, h1, shift(z, d)), + ); +} + +/// `csize` of `block`: the body's, one label deeper. +pub proof fn lemma_csize_block(b: Node, nl: nat, h: nat, depth: nat) + ensures + csize(Node::Block(Box::new(b)), nl, h, depth) == csize(b, nl, h, depth + 1), +{ + let z = zeros(depth); + lemma_ctmpl_len(b, nl, h, seq![csize(b, nl, h, depth + 1) as int] + z); + assert((seq![csize(b, nl, h, depth + 1) as int] + z).len() == depth + 1); +} + +/// `csize` of `loop`: the body's, one label deeper. +pub proof fn lemma_csize_loop(b: Node, nl: nat, h: nat, depth: nat) + ensures + csize(Node::Loop(Box::new(b)), nl, h, depth) == csize(b, nl, h, depth + 1), +{ + let z = zeros(depth); + lemma_ctmpl_len(b, nl, h, seq![0int] + z); + assert((seq![0int] + z).len() == depth + 1); +} + +/// `csize` of `br`: one `jmp`. +pub proof fn lemma_csize_br(l: usize, nl: nat, h: nat, depth: nat) + ensures + csize(Node::Br(l), nl, h, depth) == 5, +{ + let z = zeros(depth); + let rel = if l < z.len() { + z[l as int] - 5 + } else { + 0 + }; + lemma_jmp_len(rel); + super::runseq::lemma_code_len_one(jmp_i(rel)); +} + +/// `csize` of `br_if`: the condition test. +pub proof fn lemma_csize_brif(l: usize, nl: nat, h: nat, depth: nat) + ensures + csize(Node::BrIf(l), nl, h, depth) == cond_len(nl, h), +{ + let z = zeros(depth); + lemma_cond_pre_len( + nl, + h, + 5, + if l < z.len() { + z[l as int] - cond_len(nl, h) as int + } else { + 0 + }, + ); +} + +/// `csize` of `if`: the test, the first arm, a `jmp`, the second arm. +pub proof fn lemma_csize_if(t: Node, e: Node, nl: nat, h: nat, depth: nat) + requires + 1 <= h, + ensures + csize(Node::If(Box::new(t), Box::new(e)), nl, h, depth) == cond_len(nl, h) + + csize(t, nl, (h - 1) as nat, depth + 1) + 5 + csize(e, nl, (h - 1) as nat, depth + 1), +{ + let z = zeros(depth); + let p = cond_len(nl, h) as int; + let st = csize(t, nl, (h - 1) as nat, depth + 1) as int; + let se = csize(e, nl, (h - 1) as nat, depth + 1) as int; + let ot = seq![st + 5 + se] + shift(z, p); + let oe = seq![se] + shift(z, p + st + 5); + lemma_ctmpl_len(t, nl, (h - 1) as nat, ot); + lemma_ctmpl_len(e, nl, (h - 1) as nat, oe); + assert(ot.len() == depth + 1); + assert(oe.len() == depth + 1); + lemma_cond_pre_len(nl, h, 4, st + 5); + lemma_jmp_len(se); + let cp = cond_pre(nl, h, 4, st + 5); + let ct = ctmpl(t, nl, (h - 1) as nat, ot); + let ce = ctmpl(e, nl, (h - 1) as nat, oe); + super::runseq::lemma_code_len_append(cp, ct); + super::runseq::lemma_code_len_append(cp + ct, seq![jmp_i(se)]); + super::runseq::lemma_code_len_append(cp + ct + seq![jmp_i(se)], ce); + super::runseq::lemma_code_len_one(jmp_i(se)); +} + +} // verus! diff --git a/crates/clazel-jit/src/cfmem.rs b/crates/clazel-jit/src/cfmem.rs new file mode 100644 index 000000000..f4da51e7d --- /dev/null +++ b/crates/clazel-jit/src/cfmem.rs @@ -0,0 +1,253 @@ +//! **`bounds_check`, `load` and `store` refine their LIR instructions**, in +//! the control-flow statement ([`cf_refines`]): the code is +//! [`crate::cftemplates::mem_code`], with the validator's bounds-check +//! sequence. [`crate::cfcheckrun`] runs the check, [`crate::cfmemflow`] the +//! shape around it, [`crate::cfmemrel`] relates memory (`mem_rel`). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfcheck::*; +#[cfg(verus_only)] +use crate::cfcheckrun::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cfexit::*; +#[cfg(verus_only)] +use crate::cfleaf::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfmemflow::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfmemrel::*; +#[cfg(verus_only)] +use crate::cfmemsteps::*; +#[cfg(verus_only)] +use crate::cfop::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `preserved_mem` (only the exit area changed) is `preserved_mem_r`. +pub proof fn lemma_pm_to_pmr(lay: lir::Layout, m: x64::State, m2: x64::State) + requires + preserved_mem(m, m2), + ensures + preserved_mem_r(lay, m, m2), +{ +} + +/// **The state a run of register-only code and a `jmp` leaves**, related to +/// the same LIR state, with everything preserved: `m2` has the memory and +/// context register of `m`. +pub proof fn lemma_regs_only( + lay: lir::Layout, + base: Seq, + a: lir::St, + m: x64::State, + m2: x64::State, + nl: nat, +) + requires + rel_base(lay, base, a, m, nl), + m2.mem == m.mem, + m2.rgf.len() == 16, + m2.xmm.len() == 16, + m2.rgf[15] == m.rgf[15], + m2.rgf[4] == m.rgf[4], + x64::canonical(m2.rip), + m.rgf.len() == 16, + ensures + rel_base(lay, base, a, m2, nl), + preserved_r(lay, m, m2), +{ + lemma_rel_base_same(lay, base, a, m, m2, nl); + lemma_preserved_same(lay, m, m2); +} + +/// **`bounds_check`.** +#[verifier::rlimit(1000)] +pub proof fn lemma_bc_cf(region: usize, size: usize, code: u32, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + region < 16, + 1 <= size <= 8, + ensures + cf_refines( + MemOp::BoundsCheck { region, size, code }@, + nl, + h, + offs, + mem_code(MemOp::BoundsCheck { region, size, code }, nl, h), + ), +{ + let mo = MemOp::BoundsCheck { region, size, code }; + let k = region as nat; + let n = size as nat; + lemma_mem_parts_len(mo, nl, h); + let pre = mem_pre(mo, nl, h); + let cs = mem_check(mo, nl, h); + let tail = mem_tail(mo, nl, h); + let stub = exit_seq(code); + let r1 = mem_r1(mo, nl, h); + let r2 = mem_r2(mo, nl, h); + let whole = mem_code(mo, nl, h); + assert(whole == flow_code(pre, cs, tail, stub)); + lemma_stub_start(pre, cs, tail, stub); + assert(code_len(pre) == 8 && code_len(tail) == 0 && code_len(cs) == 47 && code_len(stub) == 14); + assert(code_len(whole) == 74); + assert(r1 == 36 && r2 == 22); + assert(no_jumps(pre) && no_jumps(tail)) by { + assert forall|j: int| 0 <= j < pre.len() implies !(#[trigger] pre[j] is Jcc || pre[j] is Jmp + || pre[j] is Ret) by { + assert(j == 0); + } + assert(tail.len() == 0); + } + let i1 = load_slot(rcx(), (nl + h - 1) as nat); + assert(pre =~= seq![i1]); + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, whole, cu, m, lir::eval(lay, mo@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, whole) implies cf_post( + lay, + base, + nl, + offs, + whole, + cu, + m, + lir::eval(lay, mo@, a, fuel), + ) by { + let n0 = a.stack.len(); + if fuel > 0 && n0 >= 1 && k < lay.len() { + match a.stack[(n0 - 1) as int] { + lir::Val::I64(ix) => { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + let j = (nl + h - 1) as nat; + assert(ac.stack[(h - 1) as int] == lir::Val::I64(ix)); + assert(x64::canonical(m.rip + 74)); + super::runseq::lemma_canonical_between(m.rip, m.rip + 8, m.rip + 74); + super::steps::lemma_abi_consts(); + // the operand + super::rel::lemma_load_rel(cu, lay, ac, m, 1, j, nl); + let m1 = x64::step(cu, m, i1).unwrap(); + assert(m1.rgf[1] as int == ix as int); + assert(run_seq(cu, m, pre) == Some(m1)) by { + super::runseq::lemma_run_seq_one(cu, m, i1); + } + assert(m1.rgf.len() == 16 && m1.xmm.len() == 16); + assert(table_ok(lay, m1)) by { + assert(ctx_of(m1) == ctx_of(m)); + } + lemma_plain_reaches(cu, pre, m, m1); + assert(m1.rip == m.rip + 8); + // the check + super::runseq::lemma_canonical_between(m.rip, m1.rip + 47, m.rip + 74); + super::runseq::lemma_canonical_between(m.rip, m1.rip + 16 + r1, m.rip + 74); + super::runseq::lemma_canonical_between(m.rip, m1.rip + 30 + r2, m.rip + 74); + lemma_check_run(cu, lay, m1, k, n, r1, r2); + let bound = lay[k as int].bound; + assert(lir::eval(lay, mo@, a, fuel) == if ix as nat + n <= bound { + lir::Outcome::Normal(a) + } else { + lir::Outcome::Exit( + code as nat, + seq![], + a.with_stack(lir::below(a.stack, 1)), + ) + }); + if ix as nat + n <= bound { + let mb = choose|m2: x64::State| + reaches(cu, cs, m1.rip, m1, m2) && m2.rip == m1.rip + 47 && keeps( + m1, + m2, + ) && m2.rgf[1] == m1.rgf[1]; + assert(run_seq(cu, mb, tail) == Some(mb)); + lemma_flow_pass(cu, pre, cs, tail, stub, m, m1, mb, mb); + let me = x64::State { rip: m.rip + 74, ..mb }; + assert(reaches(cu, whole, m.rip, m, me)); + assert(me.mem == m.mem && me.rgf == mb.rgf); + lemma_regs_only(lay, base, a, m, me, nl); + assert(reaches(cu, whole, m.rip, m, me)); + } else { + let ms = choose|m2: x64::State| + reaches(cu, cs, m1.rip, m1, m2) && (m2.rip == m1.rip + 16 + r1 + || m2.rip == m1.rip + 30 + r2) && keeps(m1, m2) && m2.rgf[1] + == m1.rgf[1]; + assert(ms.rip == m.rip + 60); + lemma_flow_fail(cu, pre, cs, tail, stub, m, m1, ms); + assert(ms.rgf[4] == m.rgf[4] && ms.rgf[15] == m.rgf[15]); + lemma_regs_only(lay, base, a, m, ms, nl); + lemma_rel_base_unfold(lay, base, a, ms, nl); + lemma_stack_pres(lay, m, ms); + assert(stack_ok_r(lay, ms)); + assert(x64::canonical(ms.rip + code_len(exit_seq(code)))); + lemma_exit_reach(code, nl, lay, cu, ac, ms); + let m2 = choose|m2: x64::State| + reaches(cu, exit_seq(code), ms.rip, ms, m2) && exit_done( + ms, + m2, + code as nat, + ) && rel(lay, ac, m2, nl) && preserved_mem(ms, m2); + let head = pre + cs + tail + seq![jmp_i(code_len(stub) as int)]; + assert(whole =~= head + stub + Seq::::empty()); + super::runseq::lemma_code_len_append(head, stub); + assert(code_len(head) == 60); + lemma_embed_mid(cu, head, stub, Seq::empty(), m.rip, ms, m2); + lemma_reaches_trans(cu, whole, m.rip, m, ms, m2); + // the outcome + let s = a.with_stack(lir::below(a.stack, 1)); + lemma_pm_to_pmr(lay, ms, m2); + lemma_pmr_trans(lay, m, ms, m2); + lemma_table_pres(lay, m, m2); + assert((base + a.stack).take((base + s.stack).len() as int) =~= base + + s.stack); + lemma_rel_prefix(lay, a, base + a.stack, base + s.stack, m2, nl); + assert(s.with_stack(base + s.stack) == a.with_stack(base + s.stack)); + reveal(rel_base); + assert(base + Seq::::empty() =~= base); + assert(rel_base(lay, base + Seq::::empty(), s, m2, nl)); + assert(exit_done(m, m2, code as nat)); + let vals = Seq::::empty(); + assert(ops_in_frame( + m2.mem, + ctx_of(m), + nl, + (base + Seq::::empty()).len() + s.stack.len(), + vals, + )); + assert(exit_rel(lay, base, s, m2, nl, ctx_of(m), vals)); + } + }, + _ => {}, + } + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfmemacc.rs b/crates/clazel-jit/src/cfmemacc.rs new file mode 100644 index 000000000..800e37925 --- /dev/null +++ b/crates/clazel-jit/src/cfmemacc.rs @@ -0,0 +1,407 @@ +//! The access of a `load` and of a `store`, after the check passed: the +//! machine state with `rsi` the region's base and `rcx` the index runs the +//! access (and a load's store to the slot) to a state related to LIR's result, +//! using `mem_rel` ([`crate::cfmemrel`]). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfmemrel::*; +#[cfg(verus_only)] +use crate::cfmemsteps::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// LIR's state after `t.load k` from the state `ac` whose top is the index +/// `ix`. +pub open spec fn load_result(lay: lir::Layout, ac: lir::St, k: nat, ty: MTy, ix: u64) -> lir::St { + let r = lay[k as int]; + let a_ = r.base + lir::masked(r.bound, ix as nat); + ac.with_stack( + lir::below(ac.stack, 1).push( + lir::val_of_bits(ty.lir_ty(), lir::read_le(ac.mem, a_, ty.size())), + ), + ) +} + +/// LIR's state after `t.store k` from the state `ac` whose top is the value +/// `v` and next the index `ix`. +pub open spec fn store_result( + lay: lir::Layout, + ac: lir::St, + k: nat, + ty: MTy, + ix: u64, + v: lir::Val, +) -> lir::St { + let r = lay[k as int]; + let a_ = r.base + lir::masked(r.bound, ix as nat); + ac.with_stack(lir::below(ac.stack, 2)).with_mem( + lir::write_le(ac.mem, a_, lir::val_bits(v), ty.size()), + ) +} + +/// Region bytes the access touches are in the region: `base + ix + i` for +/// `i < sz` when `ix + sz <= bound`. +pub proof fn lemma_access_in_regions(lay: lir::Layout, k: nat, ix: nat, sz: nat) + requires + k < lay.len(), + ix + sz <= lay[k as int].bound, + ensures + forall|i: nat| i < sz ==> in_regions(lay, #[trigger] (lay[k as int].base + ix + i)), +{ + assert forall|i: nat| i < sz implies in_regions(lay, #[trigger] (lay[k as int].base + ix + i)) by { + let r = k as int; + assert(0 <= r < lay.len() && lay[r].base <= lay[r].base + ix + i && lay[r].base + ix + i + < lay[r].base + lay[r].bound); + } +} + +/// **The access address is a machine address**: with the region byte mapped +/// (`mem_rel`) and the state well formed, `base + ix` is below `2^47`, and the +/// effective address `[rsi + rcx]` is it. +pub proof fn lemma_access_ea( + lay: lir::Layout, + lm: lir::Mem, + m: x64::State, + k: nat, + ix: nat, + sz: nat, +) + requires + x64::wf(m), + mem_rel(lay, lm, m.mem), + k < lay.len(), + 1 <= sz, + ix + sz <= lay[k as int].bound, + m.rgf.len() == 16, + m.rgf[6] as int == lay[k as int].base as int, + m.rgf[1] as int == ix as int, + ensures + x64::eff_addr(m, ix_mem()) == (lay[k as int].base + ix) as int, + 0 <= (lay[k as int].base + ix) as int, + lay[k as int].base < 0x1_0000_0000_0000_0000, +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + let r = k as int; + let b = lay[r].base; + assert(in_regions(lay, b + ix)) by { + assert(0 <= r < lay.len() && lay[r].base <= b + ix && b + ix < lay[r].base + lay[r].bound); + } + assert(m.mem.dom().contains((b + ix) as int)); + assert(x64::canonical((b + ix) as int)); + assert(b + ix < 0x8000_0000_0000); + let x = (b + ix) as int; + assert(x64::p2(0) == 1); + assert(x64::eff_addr(m, ix_mem()) == x64::logext(64, m.rgf[6] as int + m.rgf[1] as int * x64::p2( + 0, + ) + 0)); + assert(x64::loghead(64, x) == x); + assert(x64::logext(64, x) == x); + assert(b < 0x1_0000_0000_0000_0000); +} + +/// **A load's access and its store to the slot.** From a state `mb` with `rsi` +/// the region's base and `rcx` the index, related to `ac`, whose top is the +/// index and within the bound, the two instructions of [`mem_tail`] run to a +/// state related to [`load_result`], with only the exit area changed. +pub proof fn lemma_load_access( + cu: spec_fn(nat) -> nat, + lay: lir::Layout, + ac: lir::St, + m: x64::State, + mb: x64::State, + nl: nat, + h: nat, + k: nat, + ty: MTy, + ix: u64, +) + requires + rel(lay, ac, m, nl), + rel(lay, ac, mb, nl), + mb.mem == m.mem, + mb.rgf.len() == 16, + m.rgf.len() == 16, + mb.rgf[4] == m.rgf[4], + mb.rgf[15] == m.rgf[15], + mb.rgf[6] as int == lay[k as int].base as int, + mb.rgf[1] as int == ix as int, + k < lay.len(), + ac.stack.len() == h, + 1 <= h, + nl + h <= max_slots(), + ix as nat + ty.size() <= lay[k as int].bound, + x64::canonical(mb.rip + 16), + ensures + run_seq(cu, mb, mem_tail(MemOp::Load { region: k as usize, ty }, nl, h)) is Some, + ({ + let m4 = run_seq(cu, mb, mem_tail(MemOp::Load { region: k as usize, ty }, nl, h)).unwrap(); + &&& rel(lay, load_result(lay, ac, k, ty, ix), m4, nl) + &&& preserved(m, m4) + &&& m4.rip == mb.rip + 15 + (if ty is I32 { + 0nat + } else { + 1nat + }) + }), +{ + super::steps::lemma_abi_consts(); + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + let sz = ty.size(); + let mo = MemOp::Load { region: k as usize, ty }; + let j = (nl + h - 1) as nat; + let r = lay[k as int]; + let szz = mty_sz(ty); + lemma_mem_instr_lens(j, k); + let t1 = x64::Instr::MovLoad { sz: szz, dst: rax(), mem: ix_mem() }; + let t2 = store_slot(j, rax()); + assert(mem_tail(mo, nl, h) =~= seq![t1, t2]); + super::runseq::lemma_len2(t1, t2); + assert(x64::nbytes(szz) == sz); + lemma_access_ea(lay, ac.mem, mb, k, ix as nat, sz); + lemma_access_in_regions(lay, k, ix as nat, sz); + assert(mem_rel(lay, ac.mem, mb.mem)); + lemma_read_rel(lay, ac.mem, mb.mem, r.base + ix as nat, sz); + let v = lir::read_le(ac.mem, r.base + ix as nat, sz); + lemma_read_le_range(ac.mem, r.base + ix as nat, sz); + assert(vstd::arithmetic::power2::pow2(8 * sz) == if ty is I32 { + 0x1_0000_0000nat + } else { + 0x1_0000_0000_0000_0000nat + }); + // the access + assert(x64::canonical(mb.rip + x64::instr_len(t1))) by { + assert(x64::canonical(mb.rip)); + super::runseq::lemma_canonical_between(mb.rip, mb.rip + x64::instr_len(t1), mb.rip + 16); + } + assert(x64::wf(mb)); + lemma_step_movload(cu, mb, szz, rax(), ix_mem(), v as int); + let m3 = x64::step(cu, mb, t1).unwrap(); + assert(m3.rgf == mb.rgf.update(0, v as u64)) by { + vstd::arithmetic::div_mod::lemma_small_mod(v, 0x1_0000_0000_0000_0000nat); + if ty is I32 { + vstd::arithmetic::div_mod::lemma_small_mod(v, 0x1_0000_0000nat); + } + } + assert(m3.mem == mb.mem && m3.xmm == mb.xmm); + assert(m3.rip == mb.rip + x64::instr_len(t1)); + super::rel::lemma_rel_regs(lay, ac, mb, m3, nl); + // the new LIR state + let res = load_result(lay, ac, k, ty, ix); + let val = lir::val_of_bits(ty.lir_ty(), v); + assert(lir::masked(r.bound, ix as nat) == ix as nat); + assert(lir::val_bits(val) == v) by { + match ty { + MTy::I32 => { + assert((v as u32) as nat == v); + }, + MTy::I64 => { + assert((v as u64) as nat == v); + }, + MTy::F64 => { + assert((v as u64) as nat == v); + }, + } + } + assert(res.stack =~= lir::below(ac.stack, 1).push(val)); + assert(res.stack.len() == h); + assert(res.stack[(h - 1) as int] == val); + assert(slot_val(res, nl, j) == val); + assert(super::rel::slots_agree_except(ac, res, nl, j)) by { + assert forall|jj: nat| + #![trigger lir::val_bits(slot_val(res, nl, jj))] + jj < nl + res.stack.len() && jj != j implies jj < nl + ac.stack.len() && lir::val_bits( + slot_val(res, nl, jj), + ) == lir::val_bits(slot_val(ac, nl, jj)) by { + if jj >= nl { + assert(res.stack[(jj - nl) as int] == ac.stack[(jj - nl) as int]); + } + } + } + assert(m3.rgf[0] as nat == lir::val_bits(slot_val(res, nl, j))); + assert(x64::canonical(m3.rip + x64::instr_len(t2))) by { + super::runseq::lemma_canonical_between(mb.rip, m3.rip + x64::instr_len(t2), mb.rip + 16); + } + super::rel::lemma_store_finish(cu, lay, ac, res, m, m3, nl, j, rax(), v); + let m4 = x64::step(cu, m3, t2).unwrap(); + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen2(f, mb, t1, t2, m3, m4); + super::runseq::lemma_run_seq_is_gen(cu, mb, seq![t1, t2]); +} + +/// **A store's access.** From a state `mb` with `rsi` the region's base, `rcx` +/// the index and `rdi` the value's bits, related to `ac` (value on top, index +/// below), the access runs to a state related to [`store_result`]; only +/// region bytes changed. +pub proof fn lemma_store_access( + cu: spec_fn(nat) -> nat, + lay: lir::Layout, + ac: lir::St, + m: x64::State, + mb: x64::State, + nl: nat, + h: nat, + k: nat, + ty: MTy, + ix: u64, + v: lir::Val, +) + requires + rel(lay, ac, m, nl), + rel(lay, ac, mb, nl), + mb.mem == m.mem, + mb.rgf.len() == 16, + m.rgf.len() == 16, + mb.rgf[4] == m.rgf[4], + mb.rgf[15] == m.rgf[15], + mb.rgf[6] as int == lay[k as int].base as int, + mb.rgf[1] as int == ix as int, + mb.rgf[7] as nat == lir::val_bits(v), + lir::val_has_base_ty(v, ty.lir_ty()), + k < lay.len(), + ac.stack.len() == h, + 2 <= h, + nl + h <= max_slots(), + ix as nat + ty.size() <= lay[k as int].bound, + x64::canonical(mb.rip + 8), + ensures + run_seq(cu, mb, mem_tail(MemOp::Store { region: k as usize, ty }, nl, h)) is Some, + ({ + let m4 = run_seq(cu, mb, mem_tail(MemOp::Store { region: k as usize, ty }, nl, h)).unwrap(); + &&& rel(lay, store_result(lay, ac, k, ty, ix, v), m4, nl) + &&& preserved_mem_r(lay, m, m4) + &&& m4.rgf[4] == m.rgf[4] + &&& m4.rip == mb.rip + 7 + (if ty is I32 { + 0nat + } else { + 1nat + }) + }), +{ + super::steps::lemma_abi_consts(); + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + let sz = ty.size(); + let mo = MemOp::Store { region: k as usize, ty }; + let r = lay[k as int]; + let szz = mty_sz(ty); + lemma_mem_instr_lens(0, k); + let t1 = x64::Instr::MovStore { sz: szz, mem: ix_mem(), src: rdi() }; + assert(mem_tail(mo, nl, h) =~= seq![t1]); + super::runseq::lemma_len1(t1); + assert(x64::nbytes(szz) == sz); + assert(x64::wf(mb)); + lemma_access_ea(lay, ac.mem, mb, k, ix as nat, sz); + lemma_access_in_regions(lay, k, ix as nat, sz); + let a_ = r.base + ix as nat; + let vb = lir::val_bits(v); + assert(vb < vstd::arithmetic::power2::pow2(8 * sz)) by { + match v { + lir::Val::I32(x) => { + assert(ty is I32); + }, + lir::Val::I64(x) => { + assert(!(ty is I32)); + }, + lir::Val::F64(x) => { + assert(!(ty is I32)); + }, + } + assert(vstd::arithmetic::power2::pow2(32) == 0x1_0000_0000nat); + } + assert(x64::rgfi_size(mb, szz, rdi()) == vb as int) by { + assert(vb < 0x1_0000_0000_0000_0000); + vstd::arithmetic::div_mod::lemma_small_mod(vb, 0x1_0000_0000_0000_0000nat); + if ty is I32 { + vstd::arithmetic::div_mod::lemma_small_mod(vb, 0x1_0000_0000nat); + } + } + // the bytes are mapped + assert forall|i: int| 0 <= i < sz implies mb.mem.dom().contains(#[trigger] (a_ as int + i)) by { + assert(in_regions(lay, a_ + i as nat)); + } + super::memlemmas::lemma_write_mem_some(mb.mem, a_ as int, sz, vb as int); + let mem2 = x64::write_mem(mb.mem, a_ as int, sz, vb as int).unwrap(); + assert(x64::canonical(mb.rip + x64::instr_len(t1))); + lemma_step_movstore(cu, mb, szz, ix_mem(), rdi(), mem2); + let m4 = x64::step(cu, mb, t1).unwrap(); + assert(m4 == x64::State { mem: mem2, rip: mb.rip + x64::instr_len(t1), ..mb }); + super::memlemmas::lemma_write_mem_frame(mb.mem, a_ as int, sz, vb as int, mem2); + lemma_write_rel(lay, ac.mem, mb.mem, mem2, a_, sz, vb); + let res = store_result(lay, ac, k, ty, ix, v); + assert(lir::masked(r.bound, ix as nat) == ix as nat); + let ctxb = ctx_of(m); + assert(ctx_of(m4) == ctxb); + // written bytes are region bytes, so outside the exit area + assert forall|b: int| a_ as int <= b < a_ as int + sz implies !in_exit_area( + ctxb, + b, + ) && in_regions_i(lay, b) by { + assert(in_regions(lay, (b as nat))); + assert(in_regions(lay, a_ + (b - a_ as int) as nat)); + } + // preserved + assert(preserved_mem_r(lay, m, m4)) by { + assert forall|b: int| + #[trigger] m.mem.dom().contains(b) && !in_exit_area(ctxb, b) && !in_regions_i( + lay, + b, + ) implies m4.mem[b] == m.mem[b] by { + assert(!(a_ as int <= b && b < a_ as int + sz)); + } + } + // the relation + assert(rel(lay, res, m4, nl)) by { + assert(x64::wf(m4)) by { + assert(m4.mem.dom() =~= mb.mem.dom()); + } + assert(ctx_ok(m4)); + assert(exit_area_mapped(m4.mem, ctxb)); + assert(res.locals.len() == nl); + assert(res.stack.len() == h - 2); + assert(res.mem == lir::write_le(ac.mem, a_, vb, sz)); + assert forall|jj: nat| jj < nl + res.stack.len() implies #[trigger] slot_holds( + m4.mem, + ctxb, + jj, + lir::val_bits(slot_val(res, nl, jj)), + ) by { + assert(slot_val(res, nl, jj) == slot_val(ac, nl, jj)) by { + if jj >= nl { + assert(res.stack[(jj - nl) as int] == ac.stack[(jj - nl) as int]); + } + } + assert(slot_holds(m.mem, ctxb, jj, lir::val_bits(slot_val(ac, nl, jj)))); + let sa = ctxb + slot_disp(jj); + assert forall|i: int| 0 <= i < 8 implies (m.mem.dom().contains(#[trigger] (sa + i)) + == m4.mem.dom().contains(sa + i)) && (m.mem.dom().contains(sa + i) ==> m.mem[sa + i] + == m4.mem[sa + i]) by { + assert(in_exit_area(ctxb, sa + i)) by { + super::rel::lemma_slot_in_exit_area(ctxb, jj, sa + i); + } + assert(!(a_ as int <= sa + i && sa + i < a_ as int + sz)); + } + super::memlemmas::lemma_read_mem_congr(m.mem, m4.mem, sa, 8); + } + } + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen1(f, mb, t1, m4); + super::runseq::lemma_run_seq_is_gen(cu, mb, seq![t1]); +} + +} // verus! diff --git a/crates/clazel-jit/src/cfmemflow.rs b/crates/clazel-jit/src/cfmemflow.rs new file mode 100644 index 000000000..68f193b76 --- /dev/null +++ b/crates/clazel-jit/src/cfmemflow.rs @@ -0,0 +1,330 @@ +//! The shape of a memory instruction's run: operands, check, access, a `jmp` +//! over the stub, the stub. [`lemma_flow_pass`] says what a run that passes +//! the check does; [`lemma_flow_fail`] a run that leaves by an exit to the stub. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfleaf::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The code of a memory instruction, from its five parts. +pub open spec fn flow_code( + pre: Seq, + cs: Seq, + tail: Seq, + stub: Seq, +) -> Seq { + pre + cs + tail + seq![jmp_i(code_len(stub) as int)] + stub +} + +/// A run in a piece is a run in the whole, when the piece is where the whole +/// puts it. +pub proof fn lemma_embed_mid( + cu: spec_fn(nat) -> nat, + pre_all: Seq, + seg: Seq, + post_all: Seq, + base: int, + s: x64::State, + s2: x64::State, +) + requires + reaches(cu, seg, s.rip, s, s2), + s.rip == base + code_len(pre_all), + ensures + reaches(cu, pre_all + seg + post_all, base, s, s2), +{ + lemma_reaches_embed(cu, pre_all, seg, post_all, s.rip, s, s2); + assert(s.rip - code_len(pre_all) == base); +} + +/// Jump-free code run by [`run_seq`] is a [`reaches`] of itself, placed at +/// the state's `rip`. +pub proof fn lemma_plain_reaches( + cu: spec_fn(nat) -> nat, + code: Seq, + s: x64::State, + s2: x64::State, +) + requires + no_jumps(code), + run_seq(cu, s, code) == Some(s2), + ensures + reaches(cu, code, s.rip, s, s2), + s2.rip == s.rip + code_len(code), +{ + assert(Seq::::empty() + code + Seq::::empty() =~= code); + assert(code_len(Seq::::empty()) == 0); + lemma_run_seq_steps(cu, Seq::empty(), code, Seq::empty(), s.rip, s, s2); +} + +/// The associativity facts the segments need: the whole code split at each +/// segment. +pub proof fn lemma_flow_assoc( + pre: Seq, + cs: Seq, + tail: Seq, + jm: Seq, + stub: Seq, +) + ensures + pre + cs + tail + jm + stub == pre + cs + (tail + jm + stub), + pre + cs + tail + jm + stub == pre + cs + tail + (jm + stub), +{ + vstd::seq_lib::lemma_concat_associative(pre + cs + tail, jm, stub); + vstd::seq_lib::lemma_concat_associative(pre + cs, tail, jm + stub); + vstd::seq_lib::lemma_concat_associative(tail, jm, stub); + assert(tail + jm + stub == tail + (jm + stub)); + assert(pre + cs + tail + (jm + stub) == pre + cs + (tail + (jm + stub))); +} + +/// A run in the operands is a run in the whole code. +pub proof fn lemma_flow_a( + cu: spec_fn(nat) -> nat, + pre: Seq, + cs: Seq, + tail: Seq, + stub: Seq, + m: x64::State, + ma: x64::State, +) + requires + no_jumps(pre), + run_seq(cu, m, pre) == Some(ma), + ensures + ma.rip == m.rip + code_len(pre), + reaches(cu, flow_code(pre, cs, tail, stub), m.rip, m, ma), +{ + let jm = seq![jmp_i(code_len(stub) as int)]; + lemma_plain_reaches(cu, pre, m, ma); + lemma_reaches_post(cu, pre, cs + tail + jm + stub, m.rip, m, ma); + assert(pre + (cs + tail + jm + stub) =~= flow_code(pre, cs, tail, stub)); +} + +/// A run in the check is a run in the whole code. +pub proof fn lemma_flow_b( + cu: spec_fn(nat) -> nat, + pre: Seq, + cs: Seq, + tail: Seq, + stub: Seq, + m: x64::State, + ma: x64::State, + mb: x64::State, +) + requires + ma.rip == m.rip + code_len(pre), + reaches(cu, cs, ma.rip, ma, mb), + ensures + reaches(cu, flow_code(pre, cs, tail, stub), m.rip, ma, mb), +{ + let jm = seq![jmp_i(code_len(stub) as int)]; + lemma_flow_assoc(pre, cs, tail, jm, stub); + lemma_embed_mid(cu, pre, cs, tail + jm + stub, m.rip, ma, mb); +} + +/// A run in the tail is a run in the whole code. +pub proof fn lemma_flow_c( + cu: spec_fn(nat) -> nat, + pre: Seq, + cs: Seq, + tail: Seq, + stub: Seq, + m: x64::State, + mb: x64::State, + mu: x64::State, +) + requires + no_jumps(tail), + mb.rip == m.rip + code_len(pre) + code_len(cs), + run_seq(cu, mb, tail) == Some(mu), + ensures + mu.rip == m.rip + code_len(pre) + code_len(cs) + code_len(tail), + reaches(cu, flow_code(pre, cs, tail, stub), m.rip, mb, mu), +{ + let jm = seq![jmp_i(code_len(stub) as int)]; + super::runseq::lemma_code_len_append(pre, cs); + lemma_plain_reaches(cu, tail, mb, mu); + lemma_flow_assoc(pre, cs, tail, jm, stub); + lemma_embed_mid(cu, pre + cs, tail, jm + stub, m.rip, mb, mu); +} + +/// The operands, the check and the tail, run: the whole code reaches the +/// state the tail left, still before the `jmp`. +pub proof fn lemma_flow_upto( + cu: spec_fn(nat) -> nat, + pre: Seq, + cs: Seq, + tail: Seq, + stub: Seq, + m: x64::State, + ma: x64::State, + mb: x64::State, + mu: x64::State, +) + requires + no_jumps(pre), + no_jumps(tail), + run_seq(cu, m, pre) == Some(ma), + reaches(cu, cs, ma.rip, ma, mb), + mb.rip == ma.rip + code_len(cs), + run_seq(cu, mb, tail) == Some(mu), + ensures + mu.rip == m.rip + code_len(pre) + code_len(cs) + code_len(tail), + reaches(cu, flow_code(pre, cs, tail, stub), m.rip, m, mu), +{ + let whole = flow_code(pre, cs, tail, stub); + lemma_flow_a(cu, pre, cs, tail, stub, m, ma); + lemma_flow_b(cu, pre, cs, tail, stub, m, ma, mb); + lemma_reaches_trans(cu, whole, m.rip, m, ma, mb); + lemma_flow_c(cu, pre, cs, tail, stub, m, mb, mu); + lemma_reaches_trans(cu, whole, m.rip, m, mb, mu); +} + +/// The `jmp` over the stub, from the state the tail left. +#[verifier::rlimit(500)] +pub proof fn lemma_flow_jmp( + cu: spec_fn(nat) -> nat, + pre: Seq, + cs: Seq, + tail: Seq, + stub: Seq, + m: x64::State, + mu: x64::State, +) + requires + reaches(cu, flow_code(pre, cs, tail, stub), m.rip, m, mu), + mu.rip == m.rip + code_len(pre) + code_len(cs) + code_len(tail), + x64::canonical(m.rip + code_len(flow_code(pre, cs, tail, stub))), + x64::canonical(m.rip), + code_len(stub) < 0x8000_0000, + ensures + reaches( + cu, + flow_code(pre, cs, tail, stub), + m.rip, + m, + x64::State { rip: m.rip + code_len(flow_code(pre, cs, tail, stub)), ..mu }, + ), +{ + let jm = seq![jmp_i(code_len(stub) as int)]; + let whole = flow_code(pre, cs, tail, stub); + lemma_stub_start(pre, cs, tail, stub); + let rel = code_len(stub) as int; + let end = m.rip + code_len(whole); + assert(mu.rip + 5 + rel == end); + super::runseq::lemma_canonical_between(m.rip, mu.rip + 5, end); + super::runseq::lemma_canonical_between(m.rip, mu.rip, end); + assert((rel as i32) as int == rel); + lemma_step_jmp(cu, mu, rel); + let me = x64::State { rip: end, ..mu }; + assert(x64::step(cu, mu, jmp_i(rel)) == Some(me)); + assert(fetch(jm, 0) == Some(jmp_i(rel))); + reveal_with_fuel(steps, 2); + assert(steps(cu, jm, mu.rip, mu, 1) == Some(me)); + assert(reaches(cu, jm, mu.rip, mu, me)); + assert(pre + cs + tail + jm + stub =~= whole); + super::runseq::lemma_code_len_append(pre, cs); + super::runseq::lemma_code_len_append(pre + cs, tail); + lemma_embed_mid(cu, pre + cs + tail, jm, stub, m.rip, mu, me); + lemma_reaches_trans(cu, whole, m.rip, m, mu, me); +} + +/// **A run that passes the check.** The operands run (`pre`, jump-free), the +/// check falls through, the tail runs (jump-free), and the `jmp` skips the +/// stub: the whole code reaches the state at its end that the tail left, with +/// only `rip` moved. +pub proof fn lemma_flow_pass( + cu: spec_fn(nat) -> nat, + pre: Seq, + cs: Seq, + tail: Seq, + stub: Seq, + m: x64::State, + ma: x64::State, + mb: x64::State, + mu: x64::State, +) + requires + no_jumps(pre), + no_jumps(tail), + run_seq(cu, m, pre) == Some(ma), + reaches(cu, cs, ma.rip, ma, mb), + mb.rip == ma.rip + code_len(cs), + run_seq(cu, mb, tail) == Some(mu), + x64::canonical(m.rip + code_len(flow_code(pre, cs, tail, stub))), + x64::canonical(m.rip), + code_len(stub) < 0x8000_0000, + ensures + mu.rip == m.rip + code_len(pre) + code_len(cs) + code_len(tail), + reaches( + cu, + flow_code(pre, cs, tail, stub), + m.rip, + m, + x64::State { rip: m.rip + code_len(flow_code(pre, cs, tail, stub)), ..mu }, + ), +{ + lemma_flow_upto(cu, pre, cs, tail, stub, m, ma, mb, mu); + lemma_flow_jmp(cu, pre, cs, tail, stub, m, mu); +} + +/// **A run that leaves the check by an exit**: the whole code reaches the +/// state `ms` at the start of the stub, when the operands ran and the check +/// reached `ms` there. +pub proof fn lemma_flow_fail( + cu: spec_fn(nat) -> nat, + pre: Seq, + cs: Seq, + tail: Seq, + stub: Seq, + m: x64::State, + ma: x64::State, + ms: x64::State, +) + requires + no_jumps(pre), + run_seq(cu, m, pre) == Some(ma), + reaches(cu, cs, ma.rip, ma, ms), + ensures + reaches(cu, flow_code(pre, cs, tail, stub), m.rip, m, ms), +{ + let whole = flow_code(pre, cs, tail, stub); + lemma_flow_a(cu, pre, cs, tail, stub, m, ma); + lemma_flow_b(cu, pre, cs, tail, stub, m, ma, ms); + lemma_reaches_trans(cu, whole, m.rip, m, ma, ms); +} + +/// The stub starts after everything else. +pub proof fn lemma_stub_start( + pre: Seq, + cs: Seq, + tail: Seq, + stub: Seq, +) + ensures + code_len(flow_code(pre, cs, tail, stub)) == code_len(pre) + code_len(cs) + code_len(tail) + + 5 + code_len(stub), +{ + let jm = seq![jmp_i(code_len(stub) as int)]; + super::runseq::lemma_code_len_append(pre, cs); + super::runseq::lemma_code_len_append(pre + cs, tail); + super::runseq::lemma_code_len_append(pre + cs + tail, jm); + super::runseq::lemma_code_len_append(pre + cs + tail + jm, stub); + super::runseq::lemma_code_len_one(jmp_i(code_len(stub) as int)); + lemma_jmp_len(code_len(stub) as int); +} + +} // verus! diff --git a/crates/clazel-jit/src/cfmemlens.rs b/crates/clazel-jit/src/cfmemlens.rs new file mode 100644 index 000000000..4ddedde04 --- /dev/null +++ b/crates/clazel-jit/src/cfmemlens.rs @@ -0,0 +1,216 @@ +//! The lengths of the memory and `br_table` templates: each instruction's +//! length is a number, so the displacements of the check's exits and of the +//! `br_table` chain are, and fit a `rel32`. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A memory instruction with a 64-bit operand size and a 32-bit displacement +/// is 8 bytes (`REX`, opcode, ModR/M, SIB, displacement). +pub proof fn lemma_len_rm64(i: x64::Instr) + requires + (i is MovLoad && i->MovLoad_sz is W64) || (i is MovStore && i->MovStore_sz is W64), + ensures + x64::instr_len(i) == 8, +{ + lemma_le_bytes_len(match i { + x64::Instr::MovLoad { mem, .. } => mem.disp as int, + x64::Instr::MovStore { mem, .. } => mem.disp as int, + _ => 0, + }, 4); +} + +/// The lengths of the instructions the memory templates use. +pub proof fn lemma_mem_instr_lens(j: nat, k: nat) + ensures + x64::instr_len(load_slot(rcx(), j)) == 8, + x64::instr_len(load_slot(rdi(), j)) == 8, + x64::instr_len(load_slot(rax(), j)) == 8, + x64::instr_len(store_slot(j, rax())) == 8, + x64::instr_len( + x64::Instr::MovLoad { sz: x64::Size::W64, dst: rsi(), mem: base_mem(k) }, + ) == 8, + x64::instr_len( + x64::Instr::MovLoad { sz: x64::Size::W64, dst: rax(), mem: ix_mem() }, + ) == 8, + x64::instr_len( + x64::Instr::MovLoad { sz: x64::Size::W32, dst: rax(), mem: ix_mem() }, + ) == 7, + x64::instr_len( + x64::Instr::MovStore { sz: x64::Size::W64, mem: ix_mem(), src: rdi() }, + ) == 8, + x64::instr_len( + x64::Instr::MovStore { sz: x64::Size::W32, mem: ix_mem(), src: rdi() }, + ) == 7, +{ + lemma_len_rm64(load_slot(rcx(), j)); + lemma_len_rm64(load_slot(rdi(), j)); + lemma_len_rm64(load_slot(rax(), j)); + lemma_len_rm64(store_slot(j, rax())); + lemma_len_rm64(x64::Instr::MovLoad { sz: x64::Size::W64, dst: rsi(), mem: base_mem(k) }); + lemma_len_rm64(x64::Instr::MovLoad { sz: x64::Size::W64, dst: rax(), mem: ix_mem() }); + lemma_len_rm64(x64::Instr::MovStore { sz: x64::Size::W64, mem: ix_mem(), src: rdi() }); + lemma_le_bytes_len(0, 4); + assert(x64::encode(x64::Instr::MovLoad { sz: x64::Size::W32, dst: rax(), mem: ix_mem() }).len() + == 7); + assert(x64::encode(x64::Instr::MovStore { sz: x64::Size::W32, mem: ix_mem(), src: rdi() }).len() + == 7); +} + +/// The lengths of the instructions of the bounds-check sequence (`rcx`, +/// `rdx`, `rax`, region `k`): 3, 7, 6, 8, 6, 5, 8, 4 bytes. +pub proof fn lemma_check_instr_lens(c: clazel_codecheck::spec::Chk) + requires + c.i == rcx() && c.t == rdx() && c.z == rax() && 0 <= c.k < 16, + ensures + x64::instr_len(clazel_codecheck::spec::check_instr(0, c)) == 3, + x64::instr_len(clazel_codecheck::spec::check_instr(1, c)) == 7, + x64::instr_len(clazel_codecheck::spec::check_instr(2, c)) == 6, + x64::instr_len(clazel_codecheck::spec::check_instr(3, c)) == 8, + x64::instr_len(clazel_codecheck::spec::check_instr(4, c)) == 6, + x64::instr_len(clazel_codecheck::spec::check_instr(5, c)) == 5, + x64::instr_len(clazel_codecheck::spec::check_instr(6, c)) == 8, + x64::instr_len(clazel_codecheck::spec::check_instr(7, c)) == 4, +{ + lemma_le_bytes_len(c.n as i32 as int, 4); + lemma_jcc_len(2, c.r1 as int); + lemma_jcc_len(7, c.r2 as int); + lemma_le_bytes_len(clazel_codecheck::spec::bound_mem(c.k).disp as int, 4); + lemma_le_bytes_len(0, 4); + let i3 = clazel_codecheck::spec::check_instr(3, c); + let i6 = clazel_codecheck::spec::check_instr(6, c); + assert(x64::encode(i3).len() == 8); + assert(x64::encode(i6).len() == 8); + assert(x64::encode(clazel_codecheck::spec::check_instr(0, c)).len() == 3); + assert(x64::encode(clazel_codecheck::spec::check_instr(1, c)).len() == 7); + assert(x64::encode(clazel_codecheck::spec::check_instr(5, c)).len() == 5); + assert(x64::encode(clazel_codecheck::spec::check_instr(7, c)).len() == 4); +} + +/// The exit sequence is 14 bytes (5, 8, 1). +pub proof fn lemma_exit_seq_len(c: u32) + ensures + code_len(exit_seq(c)) == 14, +{ + lemma_le_bytes_len(c as int, 4); + lemma_le_bytes_len(abi::exit_code_off(), 4); + let i0 = x64::Instr::MovImm32 { dst: rax(), imm_u32: c }; + let i1 = x64::Instr::MovStore { + sz: x64::Size::W64, + mem: x64::Mem { base: abi::ctx_reg(), index: None, disp: abi::exit_code_off() as i32 }, + src: rax(), + }; + lemma_len_rm64(i1); + assert(x64::encode(i0).len() == 5); + assert(x64::encode(x64::Instr::Ret).len() == 1); + super::runseq::lemma_len3(i0, i1, x64::Instr::Ret); + assert(exit_seq(c) =~= seq![i0, i1, x64::Instr::Ret]); +} + +/// The lengths of the parts of a memory instruction's code, for a region of +/// the table: the operands 8, 16 or 24 bytes, the check 47, the tail 0, 15 or +/// 16 (a load), 7 or 8 (a store), the stub 14; so the exits' displacements are +/// small and non-negative. +pub proof fn lemma_mem_parts_len(mo: MemOp, nl: nat, h: nat) + requires + mem_region(mo) < 16, + ensures + code_len(mem_check(mo, nl, h)) == 47, + code_len(exit_seq(mem_exit_code(mo))) == 14, + mem_check(mo, nl, h).len() == 8, + match mo { + MemOp::BoundsCheck { .. } => code_len(mem_pre(mo, nl, h)) == 8 && code_len( + mem_tail(mo, nl, h), + ) == 0, + MemOp::Load { ty, .. } => code_len(mem_pre(mo, nl, h)) == 16 && code_len( + mem_tail(mo, nl, h), + ) == (if ty is I32 { + 15nat + } else { + 16nat + }), + MemOp::Store { ty, .. } => code_len(mem_pre(mo, nl, h)) == 24 && code_len( + mem_tail(mo, nl, h), + ) == (if ty is I32 { + 7nat + } else { + 8nat + }), + }, + mem_r1(mo, nl, h) == 36 + code_len(mem_tail(mo, nl, h)), + mem_r2(mo, nl, h) == 22 + code_len(mem_tail(mo, nl, h)), + 0 <= mem_r1(mo, nl, h) < 100, + 0 <= mem_r2(mo, nl, h) < 100, +{ + lemma_mem_instr_lens(0, 0); + lemma_exit_seq_len(mem_exit_code(mo)); + let k = mem_region(mo); + lemma_mem_instr_lens((nl + h - 1) as nat, k); + lemma_mem_instr_lens((nl + h - 2) as nat, k); + let c0 = mem_chk(0, 1, 0, 0); + super::cfcheck::lemma_check_offsets(c0); + let c = mem_chk(k, mem_n(mo), mem_r1(mo, nl, h), mem_r2(mo, nl, h)); + super::cfcheck::lemma_check_offsets(c); + assert(chk_len_from(3) == 31 && chk_len_from(5) == 17); + match mo { + MemOp::BoundsCheck { .. } => { + assert(mem_tail(mo, nl, h) =~= Seq::::empty()); + super::runseq::lemma_len1(load_slot(rcx(), (nl + h - 1) as nat)); + }, + MemOp::Load { region, ty } => { + let a = x64::Instr::MovLoad { sz: mty_sz(ty), dst: rax(), mem: ix_mem() }; + super::runseq::lemma_len2(a, store_slot((nl + h - 1) as nat, rax())); + super::runseq::lemma_len2( + load_slot(rcx(), (nl + h - 1) as nat), + x64::Instr::MovLoad { sz: x64::Size::W64, dst: rsi(), mem: base_mem(region as nat) }, + ); + }, + MemOp::Store { region, ty } => { + let a = x64::Instr::MovStore { sz: mty_sz(ty), mem: ix_mem(), src: rdi() }; + super::runseq::lemma_len1(a); + super::runseq::lemma_len3( + load_slot(rdi(), (nl + h - 1) as nat), + load_slot(rcx(), (nl + h - 2) as nat), + x64::Instr::MovLoad { sz: x64::Size::W64, dst: rsi(), mem: base_mem(region as nat) }, + ); + }, + } +} + +/// The chain's entry is 13 bytes whatever `i` and `rel` are; the load of the +/// index is 8. +pub proof fn lemma_brt_entry_len(i: nat, rel: int) + ensures + code_len(brt_entry(i, rel)) == 13, + brt_e() == 13, + brt_p0(0, 1) == 8, +{ + lemma_jcc_len(4, rel); + lemma_le_bytes_len(i as u32 as int, 4); + let a = x64::Instr::MovImm32 { dst: rdx(), imm_u32: i as u32 }; + let b = x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: x64::Size::W32, dst: rax(), src: rdx() }; + assert(x64::encode(a).len() == 5); + assert(x64::encode(b).len() == 2); + super::runseq::lemma_len3(a, b, jcc_i(4, rel)); + lemma_jcc_len(4, 0); + lemma_le_bytes_len(0, 4); + let a0 = x64::Instr::MovImm32 { dst: rdx(), imm_u32: 0 }; + assert(x64::encode(a0).len() == 5); + super::runseq::lemma_len3(a0, b, jcc_i(4, 0)); + lemma_mem_instr_lens(0, 0); + super::runseq::lemma_len1(load_slot(rax(), 0)); +} + +} // verus! diff --git a/crates/clazel-jit/src/cfmemops.rs b/crates/clazel-jit/src/cfmemops.rs new file mode 100644 index 000000000..8e9c4ca19 --- /dev/null +++ b/crates/clazel-jit/src/cfmemops.rs @@ -0,0 +1,427 @@ +//! **`load` and `store` refine their LIR instructions** +//! ([`crate::cfcontract::cf_refines`]), from the pieces: the operands, the +//! bounds check ([`crate::cfcheckrun`]), the access ([`crate::cfmemacc`]) and +//! the shape of the run ([`crate::cfmemflow`]). When the check fails LIR is +//! `Stuck` and nothing is claimed. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfcheck::*; +#[cfg(verus_only)] +use crate::cfcheckrun::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cfleaf::*; +#[cfg(verus_only)] +use crate::cfmem::*; +#[cfg(verus_only)] +use crate::cfmemacc::*; +#[cfg(verus_only)] +use crate::cfmemflow::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfmemsteps::*; +#[cfg(verus_only)] +use crate::cfop::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `ty_size` of the LIR type is the access size. +pub proof fn lemma_ty_size(ty: MTy) + ensures + lir::ty_size(ty.lir_ty()) == ty.size(), + ty.size() >= 4, +{ +} + +/// **`t.load`.** +#[verifier::rlimit(1500)] +pub proof fn lemma_load_cf(region: usize, ty: MTy, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + region < 16, + ensures + cf_refines( + MemOp::Load { region, ty }@, + nl, + h, + offs, + mem_code(MemOp::Load { region, ty }, nl, h), + ), +{ + let mo = MemOp::Load { region, ty }; + let k = region as nat; + let sz = ty.size(); + lemma_ty_size(ty); + lemma_mem_parts_len(mo, nl, h); + let pre = mem_pre(mo, nl, h); + let cs = mem_check(mo, nl, h); + let tail = mem_tail(mo, nl, h); + let stub = exit_seq(trap_code()); + let r1 = mem_r1(mo, nl, h); + let r2 = mem_r2(mo, nl, h); + let whole = mem_code(mo, nl, h); + assert(whole == flow_code(pre, cs, tail, stub)); + lemma_stub_start(pre, cs, tail, stub); + let tl = code_len(tail); + assert(code_len(pre) == 16 && code_len(cs) == 47 && code_len(stub) == 14); + assert(tl == 15 || tl == 16); + assert(code_len(whole) == 82 + tl); + let j = (nl + h - 1) as nat; + let i1 = load_slot(rcx(), j); + let i2 = x64::Instr::MovLoad { sz: x64::Size::W64, dst: rsi(), mem: base_mem(k) }; + assert(pre =~= seq![i1, i2]); + assert(no_jumps(pre)) by { + assert forall|q: int| 0 <= q < pre.len() implies !(#[trigger] pre[q] is Jcc || pre[q] + is Jmp || pre[q] is Ret) by { + assert(q == 0 || q == 1); + } + } + assert(no_jumps(tail)) by { + let t1 = x64::Instr::MovLoad { sz: mty_sz(ty), dst: rax(), mem: ix_mem() }; + assert(tail =~= seq![t1, store_slot(j, rax())]); + assert forall|q: int| 0 <= q < tail.len() implies !(#[trigger] tail[q] is Jcc || tail[q] + is Jmp || tail[q] is Ret) by { + assert(q == 0 || q == 1); + } + } + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, whole, cu, m, lir::eval(lay, mo@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, whole) implies cf_post( + lay, + base, + nl, + offs, + whole, + cu, + m, + lir::eval(lay, mo@, a, fuel), + ) by { + let n0 = a.stack.len(); + if fuel > 0 && n0 >= 1 && k < lay.len() { + match a.stack[(n0 - 1) as int] { + lir::Val::I64(ix) => { + let bound = lay[k as int].bound; + if ix as nat + sz <= bound { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + assert(ac.stack[(h - 1) as int] == lir::Val::I64(ix)); + super::steps::lemma_abi_consts(); + let big = code_len(whole); + assert(x64::canonical(m.rip + big)); + super::runseq::lemma_canonical_between(m.rip, m.rip + 8, m.rip + big); + super::runseq::lemma_canonical_between(m.rip, m.rip + 16, m.rip + big); + // the operands + super::rel::lemma_load_rel(cu, lay, ac, m, 1, j, nl); + let m1 = x64::step(cu, m, i1).unwrap(); + assert(m1.rgf[1] as int == ix as int); + assert(table_ok(lay, m1)) by { + assert(ctx_of(m1) == ctx_of(m)); + } + assert(x64::wf(m1) && ctx_ok(m1)); + lemma_table_words(lay, m1, k); + let base_w = lay[k as int].base; + assert(x64::nbytes(x64::Size::W64) == 8); + assert(x64::eff_addr(m1, base_mem(k)) == x64::eff_addr(m1, base_mem(k))); + lemma_step_movload(cu, m1, x64::Size::W64, 6, base_mem(k), base_w as int); + let m2 = x64::step(cu, m1, i2).unwrap(); + lemma_mem_instr_lens(j, k); + super::memlemmas::lemma_p2_consts(); + assert(m2.rgf == m1.rgf.update(6, base_w as u64)) by { + vstd::arithmetic::div_mod::lemma_small_mod( + base_w, + 0x1_0000_0000_0000_0000nat, + ); + } + assert(m2.mem == m.mem && m2.xmm == m1.xmm && m2.rip == m.rip + 16); + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen2(f, m, i1, i2, m1, m2); + super::runseq::lemma_run_seq_is_gen(cu, m, pre); + assert(run_seq(cu, m, pre) == Some(m2)); + super::rel::lemma_rel_regs(lay, ac, m1, m2, nl); + assert(table_ok(lay, m2)) by { + assert(ctx_of(m2) == ctx_of(m)); + } + assert(m2.rgf[1] as int == ix as int && m2.rgf[6] as int == base_w as int); + // the check + assert(r1 == 36 + tl && r2 == 22 + tl); + super::runseq::lemma_canonical_between(m.rip, m2.rip + 47, m.rip + big); + super::runseq::lemma_canonical_between( + m.rip, + m2.rip + 16 + r1, + m.rip + big, + ); + super::runseq::lemma_canonical_between( + m.rip, + m2.rip + 30 + r2, + m.rip + big, + ); + lemma_check_run(cu, lay, m2, k, sz, r1, r2); + let mb = choose|q: x64::State| + reaches(cu, cs, m2.rip, m2, q) && q.rip == m2.rip + 47 && keeps(m2, q) + && q.rgf[1] == m2.rgf[1]; + super::rel::lemma_rel_regs(lay, ac, m2, mb, nl); + assert(mb.rgf[6] as int == base_w as int); + // the access + super::runseq::lemma_canonical_between(m.rip, mb.rip + 16, m.rip + big); + lemma_load_access(cu, lay, ac, m, mb, nl, h, k, ty, ix); + let m4 = run_seq(cu, mb, tail).unwrap(); + lemma_flow_pass(cu, pre, cs, tail, stub, m, m2, mb, m4); + let me = x64::State { rip: m.rip + big, ..m4 }; + // LIR's result + let a2 = a.with_stack( + lir::below(a.stack, 1).push( + lir::val_of_bits( + ty.lir_ty(), + lir::read_le( + a.mem, + lay[k as int].base + lir::masked(bound, ix as nat), + sz, + ), + ), + ), + ); + assert(lir::eval(lay, mo@, a, fuel) == lir::Outcome::Normal(a2)); + let a2c = a2.with_stack(base + a2.stack); + let res = load_result(lay, ac, k, ty, ix); + assert(ac.mem == a.mem); + assert(a2c.stack =~= res.stack) by { + lemma_below_cat(base, a.stack, 1); + let v = lir::val_of_bits( + ty.lir_ty(), + lir::read_le( + a.mem, + lay[k as int].base + lir::masked(bound, ix as nat), + sz, + ), + ); + assert((base + lir::below(a.stack, 1)).push(v) =~= base + + lir::below(a.stack, 1).push(v)); + } + assert(a2c == res); + assert(me.mem == m4.mem && me.rgf == m4.rgf); + super::rel::lemma_rel_regs(lay, res, m4, me, nl); + assert(preserved(m, me)); + lemma_preserved_to_r(lay, m, me); + lemma_table_pres(lay, m, me); + reveal(rel_base); + assert(rel_base(lay, base, a2, me, nl)); + } + }, + _ => {}, + } + } + } +} + +/// **`t.store`.** +#[verifier::rlimit(1500)] +pub proof fn lemma_store_cf(region: usize, ty: MTy, nl: nat, h: nat, offs: Seq) + requires + 2 <= h, + nl + h <= max_slots(), + region < 16, + ensures + cf_refines( + MemOp::Store { region, ty }@, + nl, + h, + offs, + mem_code(MemOp::Store { region, ty }, nl, h), + ), +{ + let mo = MemOp::Store { region, ty }; + let k = region as nat; + let sz = ty.size(); + lemma_ty_size(ty); + lemma_mem_parts_len(mo, nl, h); + let pre = mem_pre(mo, nl, h); + let cs = mem_check(mo, nl, h); + let tail = mem_tail(mo, nl, h); + let stub = exit_seq(trap_code()); + let r1 = mem_r1(mo, nl, h); + let r2 = mem_r2(mo, nl, h); + let whole = mem_code(mo, nl, h); + assert(whole == flow_code(pre, cs, tail, stub)); + lemma_stub_start(pre, cs, tail, stub); + let tl = code_len(tail); + assert(code_len(pre) == 24 && code_len(cs) == 47 && code_len(stub) == 14); + assert(tl == 7 || tl == 8); + assert(code_len(whole) == 90 + tl); + let j = (nl + h - 1) as nat; + let j2 = (nl + h - 2) as nat; + let i1 = load_slot(rdi(), j); + let i2 = load_slot(rcx(), j2); + let i3 = x64::Instr::MovLoad { sz: x64::Size::W64, dst: rsi(), mem: base_mem(k) }; + assert(pre =~= seq![i1, i2, i3]); + assert(no_jumps(pre)) by { + assert forall|q: int| 0 <= q < pre.len() implies !(#[trigger] pre[q] is Jcc || pre[q] + is Jmp || pre[q] is Ret) by { + assert(q == 0 || q == 1 || q == 2); + } + } + assert(no_jumps(tail)) by { + let t1 = x64::Instr::MovStore { sz: mty_sz(ty), mem: ix_mem(), src: rdi() }; + assert(tail =~= seq![t1]); + assert forall|q: int| 0 <= q < tail.len() implies !(#[trigger] tail[q] is Jcc || tail[q] + is Jmp || tail[q] is Ret) by { + assert(q == 0); + } + } + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, whole, cu, m, lir::eval(lay, mo@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, whole) implies cf_post( + lay, + base, + nl, + offs, + whole, + cu, + m, + lir::eval(lay, mo@, a, fuel), + ) by { + let n0 = a.stack.len(); + if fuel > 0 && n0 >= 2 && k < lay.len() && lir::val_has_base_ty( + a.stack[(n0 - 1) as int], + ty.lir_ty(), + ) { + match a.stack[(n0 - 2) as int] { + lir::Val::I64(ix) => { + let bound = lay[k as int].bound; + let v = a.stack[(n0 - 1) as int]; + if ix as nat + sz <= bound { + lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + assert(ac.stack[(h - 1) as int] == v); + assert(ac.stack[(h - 2) as int] == lir::Val::I64(ix)); + super::steps::lemma_abi_consts(); + let big = code_len(whole); + assert(x64::canonical(m.rip + big)); + super::runseq::lemma_canonical_between(m.rip, m.rip + 8, m.rip + big); + super::runseq::lemma_canonical_between(m.rip, m.rip + 16, m.rip + big); + super::runseq::lemma_canonical_between(m.rip, m.rip + 24, m.rip + big); + // the operands + super::rel::lemma_load_rel(cu, lay, ac, m, 7, j, nl); + let m1 = x64::step(cu, m, i1).unwrap(); + assert(m1.rgf[7] as nat == lir::val_bits(v)); + super::rel::lemma_load_rel(cu, lay, ac, m1, 1, j2, nl); + let m2 = x64::step(cu, m1, i2).unwrap(); + assert(m2.rgf[1] as int == ix as int); + assert(m2.rgf[7] == m1.rgf[7]); + assert(table_ok(lay, m2)) by { + assert(ctx_of(m2) == ctx_of(m)); + } + assert(x64::wf(m2) && ctx_ok(m2)); + lemma_table_words(lay, m2, k); + let base_w = lay[k as int].base; + assert(x64::nbytes(x64::Size::W64) == 8); + lemma_step_movload(cu, m2, x64::Size::W64, 6, base_mem(k), base_w as int); + let m3 = x64::step(cu, m2, i3).unwrap(); + lemma_mem_instr_lens(j, k); + super::memlemmas::lemma_p2_consts(); + assert(m3.rgf == m2.rgf.update(6, base_w as u64)) by { + vstd::arithmetic::div_mod::lemma_small_mod( + base_w, + 0x1_0000_0000_0000_0000nat, + ); + } + assert(m3.mem == m.mem && m3.xmm == m2.xmm && m3.rip == m.rip + 24); + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen3(f, m, i1, i2, i3, m1, m2, m3); + super::runseq::lemma_run_seq_is_gen(cu, m, pre); + assert(run_seq(cu, m, pre) == Some(m3)); + super::rel::lemma_rel_regs(lay, ac, m2, m3, nl); + assert(table_ok(lay, m3)) by { + assert(ctx_of(m3) == ctx_of(m)); + } + assert(m3.rgf[1] as int == ix as int && m3.rgf[6] as int == base_w as int); + assert(m3.rgf[7] as nat == lir::val_bits(v)); + // the check + assert(r1 == 36 + tl && r2 == 22 + tl); + super::runseq::lemma_canonical_between(m.rip, m3.rip + 47, m.rip + big); + super::runseq::lemma_canonical_between( + m.rip, + m3.rip + 16 + r1, + m.rip + big, + ); + super::runseq::lemma_canonical_between( + m.rip, + m3.rip + 30 + r2, + m.rip + big, + ); + lemma_check_run(cu, lay, m3, k, sz, r1, r2); + let mb = choose|q: x64::State| + reaches(cu, cs, m3.rip, m3, q) && q.rip == m3.rip + 47 && keeps(m3, q) + && q.rgf[1] == m3.rgf[1]; + super::rel::lemma_rel_regs(lay, ac, m3, mb, nl); + assert(mb.rgf[6] as int == base_w as int && mb.rgf[7] == m3.rgf[7]); + // the access + super::runseq::lemma_canonical_between(m.rip, mb.rip + 8, m.rip + big); + lemma_store_access(cu, lay, ac, m, mb, nl, h, k, ty, ix, v); + let m4 = run_seq(cu, mb, tail).unwrap(); + lemma_flow_pass(cu, pre, cs, tail, stub, m, m3, mb, m4); + let me = x64::State { rip: m.rip + big, ..m4 }; + // LIR's result + let a2 = a.with_stack(lir::below(a.stack, 2)).with_mem( + lir::write_le( + a.mem, + lay[k as int].base + lir::masked(bound, ix as nat), + lir::val_bits(v), + sz, + ), + ); + assert(lir::eval(lay, mo@, a, fuel) == lir::Outcome::Normal(a2)); + let a2c = a2.with_stack(base + a2.stack); + let res = store_result(lay, ac, k, ty, ix, v); + assert(ac.mem == a.mem); + assert(a2c.stack =~= res.stack) by { + lemma_below_cat(base, a.stack, 2); + } + assert(a2c == res); + assert(me.mem == m4.mem && me.rgf == m4.rgf); + super::rel::lemma_rel_regs(lay, res, m4, me, nl); + assert(preserved_r(lay, m, me)); + lemma_table_pres(lay, m, me); + reveal(rel_base); + assert(rel_base(lay, base, a2, me, nl)); + } + }, + _ => {}, + } + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfmemrel.rs b/crates/clazel-jit/src/cfmemrel.rs new file mode 100644 index 000000000..e542bebec --- /dev/null +++ b/crates/clazel-jit/src/cfmemrel.rs @@ -0,0 +1,159 @@ +//! `mem_rel` used: a machine read of bytes inside regions is LIR's `read_le` +//! of its memory, and a machine write of them gives a memory related to +//! LIR's `write_le`. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The byte `k` of `v`, little-endian: `(v / 256^k) % 256`. +pub open spec fn bytek(v: nat, k: nat) -> nat + decreases k, +{ + if k == 0 { + v % 256 + } else { + bytek(v / 256, (k - 1) as nat) + } +} + +/// `read_le` is below `2^(8n)`. +pub proof fn lemma_read_le_range(m: lir::Mem, a: nat, n: nat) + ensures + lir::read_le(m, a, n) < vstd::arithmetic::power2::pow2(8 * n), + decreases n, +{ + vstd::arithmetic::power2::lemma2_to64(); + if n > 0 { + let n1 = (n - 1) as nat; + lemma_read_le_range(m, a + 1, n1); + vstd::arithmetic::power2::lemma_pow2_adds(8, 8 * n1); + assert(8 * n == 8 + 8 * n1); + assert(lir::byte_at(m, a) < 256); + } +} + +/// `read_mem` is below `2^(8n)`. +pub proof fn lemma_read_mem_range(mm: Map, a: int, n: nat, v: int) + requires + x64::read_mem(mm, a, n) == Some(v), + ensures + 0 <= v < vstd::arithmetic::power2::pow2(8 * n) as int, + decreases n, +{ + vstd::arithmetic::power2::lemma2_to64(); + if n > 0 { + let n1 = (n - 1) as nat; + match x64::read_mem(mm, a + 1, n1) { + Some(rest) => { + lemma_read_mem_range(mm, a + 1, n1, rest); + vstd::arithmetic::power2::lemma_pow2_adds(8, 8 * n1); + assert(8 * n == 8 + 8 * n1); + }, + None => {}, + } + } +} + +/// **A read of bytes inside regions is LIR's read.** +pub proof fn lemma_read_rel(lay: lir::Layout, lm: lir::Mem, mm: Map, a: nat, n: nat) + requires + mem_rel(lay, lm, mm), + forall|i: nat| i < n ==> in_regions(lay, #[trigger] (a + i)), + ensures + x64::read_mem(mm, a as int, n) == Some(lir::read_le(lm, a, n) as int), + decreases n, +{ + if n > 0 { + let n1 = (n - 1) as nat; + assert(in_regions(lay, a + 0)); + assert forall|i: nat| i < n1 implies in_regions(lay, #[trigger] (a + 1 + i)) by { + assert(in_regions(lay, a + (i + 1))); + } + lemma_read_rel(lay, lm, mm, a + 1, n1); + assert((a + 1) as int == a as int + 1); + assert(mm.dom().contains(a as int) && mm[a as int] == lir::byte_at(lm, a)); + } +} + +/// `write_le` changes byte `b` to byte `b - a` of `v` inside the range, and +/// nothing outside it. +pub proof fn lemma_write_le_byte(lm: lir::Mem, a: nat, v: nat, n: nat, b: nat) + ensures + lir::byte_at(lir::write_le(lm, a, v, n), b) == if a <= b && b < a + n { + bytek(v, (b - a) as nat) as u8 + } else { + lir::byte_at(lm, b) + }, + decreases n, +{ + if n > 0 { + let lm1 = lm.insert(a, (v % 256) as u8); + lemma_write_le_byte(lm1, a + 1, v / 256, (n - 1) as nat, b); + if b == a { + assert(lir::byte_at(lm1, b) == (v % 256) as u8); + } else if b > a && b < a + n { + assert(bytek(v / 256, (b - (a + 1)) as nat) == bytek(v, (b - a) as nat)); + } else { + assert(lir::byte_at(lm1, b) == lir::byte_at(lm, b)); + } + } +} + +/// `write_mem` puts byte `b - a` of `v` at `b` inside the range. +pub proof fn lemma_write_mem_byte(mm: Map, a: int, n: nat, v: int, mm2: Map, b: int) + requires + 0 <= v, + x64::write_mem(mm, a, n, v) == Some(mm2), + a <= b < a + n, + ensures + mm2[b] == bytek(v as nat, (b - a) as nat) as u8, + decreases n, +{ + let n1 = (n - 1) as nat; + match x64::write_mem(mm, a + 1, n1, v / 256) { + Some(m3) => { + assert(mm2 == m3.insert(a, (v % 256) as u8)); + if b == a { + } else { + lemma_write_mem_byte(mm, a + 1, n1, v / 256, m3, b); + assert(bytek(v as nat, (b - a) as nat) == bytek((v / 256) as nat, (b - (a + 1)) as nat)); + } + }, + None => {}, + } +} + +/// **A write of bytes inside regions keeps the memories related.** The machine +/// writes the low `n` bytes of `v`, LIR the same. +pub proof fn lemma_write_rel( + lay: lir::Layout, + lm: lir::Mem, + mm: Map, + mm2: Map, + a: nat, + n: nat, + v: nat, +) + requires + mem_rel(lay, lm, mm), + forall|i: nat| i < n ==> in_regions(lay, #[trigger] (a + i)), + x64::write_mem(mm, a as int, n, v as int) == Some(mm2), + ensures + mem_rel(lay, lir::write_le(lm, a, v, n), mm2), +{ + super::memlemmas::lemma_write_mem_frame(mm, a as int, n, v as int, mm2); + assert forall|b: nat| #[trigger] in_regions(lay, b) implies mm2.dom().contains(b as int) + && mm2[b as int] == lir::byte_at(lir::write_le(lm, a, v, n), b) by { + assert(mm.dom().contains(b as int) && mm[b as int] == lir::byte_at(lm, b)); + lemma_write_le_byte(lm, a, v, n, b); + if a <= b && b < a + n { + lemma_write_mem_byte(mm, a as int, n, v as int, mm2, b as int); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfmemsteps.rs b/crates/clazel-jit/src/cfmemsteps.rs new file mode 100644 index 000000000..ccbdb393b --- /dev/null +++ b/crates/clazel-jit/src/cfmemsteps.rs @@ -0,0 +1,298 @@ +//! One `x64::step` of each instruction form the memory templates and the +//! `br_table` chain add to the first slice's: a register move, `add` with an +//! immediate (its carry), `cmp` with a memory operand, and a load or store +//! through any memory operand. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `mov dst, src` (64-bit). +pub proof fn lemma_step_mov_rr64(cu: spec_fn(nat) -> nat, m: x64::State, dst: u8, src: u8) + requires + x64::wf(m), + dst < 16, + src < 16, + x64::canonical( + m.rip + x64::instr_len(x64::Instr::MovRR { sz: x64::Size::W64, dst: dst, src: src }), + ), + ensures + x64::step(cu, m, x64::Instr::MovRR { sz: x64::Size::W64, dst: dst, src: src }) == Some( + x64::State { + rgf: m.rgf.update(dst as int, m.rgf[src as int]), + rip: m.rip + x64::instr_len( + x64::Instr::MovRR { sz: x64::Size::W64, dst: dst, src: src }, + ), + ..m + }, + ), +{ + super::memlemmas::lemma_p2_consts(); + let v = m.rgf[src as int] as int; + assert(x64::loghead(64, v) == v); +} + +/// `add dst, imm` (64-bit, `imm >= 0`): the sum modulo `2^64`, and the carry +/// flag says the sum did not fit. +pub proof fn lemma_step_add_ri64(cu: spec_fn(nat) -> nat, m: x64::State, dst: u8, imm: i32) + requires + x64::wf(m), + dst < 16, + 0 <= imm, + x64::canonical( + m.rip + x64::instr_len( + x64::Instr::AluRI { alu: x64::AluOp::Add, sz: x64::Size::W64, dst: dst, imm: imm }, + ), + ), + ensures + x64::step( + cu, + m, + x64::Instr::AluRI { alu: x64::AluOp::Add, sz: x64::Size::W64, dst: dst, imm: imm }, + ) is Some, + ({ + let m1 = x64::step( + cu, + m, + x64::Instr::AluRI { alu: x64::AluOp::Add, sz: x64::Size::W64, dst: dst, imm: imm }, + ).unwrap(); + let sum = m.rgf[dst as int] as int + imm as int; + &&& m1.rgf == m.rgf.update(dst as int, (sum % 0x1_0000_0000_0000_0000) as u64) + &&& m1.mem == m.mem && m1.xmm == m.xmm + &&& m1.rip == m.rip + x64::instr_len( + x64::Instr::AluRI { alu: x64::AluOp::Add, sz: x64::Size::W64, dst: dst, imm: imm }, + ) + &&& x64::flgi(m1, x64::cf_bit()) == x64::bool_to_bit(sum >= 0x1_0000_0000_0000_0000) + }), +{ + super::memlemmas::lemma_p2_consts(); + let x = m.rgf[dst as int] as int; + let y = imm as int; + assert(0 <= x && x < 0x1_0000_0000_0000_0000); + assert(x64::loghead(64, y) == y); + vstd::arithmetic::div_mod::lemma_small_mod(x as nat, 0x1_0000_0000_0000_0000nat); + assert(x64::loghead(64, x) == x); + let r = x64::gpr_add_spec(64, x, y, m.rflags); + let s1 = x64::wr64(m, dst, r.result); + super::alu::lemma_wur_frame(cu, s1, r.rflags, r.undef); + assert(r.undef == 0); + assert(x64::bit(0, 0) == 0) by { + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::p2(0) == 1); + } + super::flags::lemma_wur_flag(cu, s1, r.rflags, r.undef, 0); + let sum = x + y; + let f = x64::FlagUpd { + cf: Some(x64::cf_spec(64, sum)), + pf: Some(x64::pf_spec(x64::loghead(64, sum))), + af: Some(x64::add_af_spec(x, y)), + zf: Some(x64::zf_spec(x64::loghead(64, sum))), + sf: Some(x64::sf_spec(64, x64::loghead(64, sum))), + of: Some(x64::of_spec(64, x64::logext(64, x) + x64::logext(64, y))), + }; + assert(r.rflags == x64::change_rflags(m.rflags, f)); + super::flags::lemma_change_rflags_bit(m.rflags, f, 0); + assert(x64::loghead(64, sum) == sum % 0x1_0000_0000_0000_0000); + assert(x64::cf_spec(64, sum) == x64::bool_to_bit(sum >= 0x1_0000_0000_0000_0000)); + assert(x64::cf_spec(64, sum) % 2 == x64::cf_spec(64, sum)); +} + +/// `cmp dst, [mem]` (64-bit) with the memory operand reading the word `e`: +/// the registers and memory are unchanged, and the flags are those of +/// `dst - e`. +pub proof fn lemma_step_cmp_rm64_flags( + cu: spec_fn(nat) -> nat, + m: x64::State, + a: u8, + mem: x64::Mem, + e: int, +) + requires + x64::wf(m), + a < 16, + x64::read_mem(m.mem, x64::eff_addr(m, mem), 8) == Some(e), + 0 <= e < 0x1_0000_0000_0000_0000, + x64::canonical( + m.rip + x64::instr_len( + x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: a, mem: mem }, + ), + ), + ensures + x64::step( + cu, + m, + x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: a, mem: mem }, + ) is Some, + ({ + let m1 = x64::step( + cu, + m, + x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: a, mem: mem }, + ).unwrap(); + let lx = m.rgf[a as int] as int; + let sraw = x64::logext(64, lx) - x64::logext(64, e); + let res = x64::loghead(64, sraw); + &&& m1.rgf == m.rgf && m1.mem == m.mem && m1.xmm == m.xmm + &&& m1.rip == m.rip + x64::instr_len( + x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: a, mem: mem }, + ) + &&& x64::flgi(m1, x64::cf_bit()) == x64::bool_to_bit(lx < e) + &&& x64::flgi(m1, x64::zf_bit()) == x64::zf_spec(res) + &&& x64::flgi(m1, x64::sf_bit()) == x64::sf_spec(64, res) % 2 + &&& x64::flgi(m1, x64::of_bit()) == x64::of_spec(64, sraw) + }), +{ + super::memlemmas::lemma_p2_consts(); + let n: nat = 64; + let x = x64::rgfi_size(m, x64::Size::W64, a); + assert(x == m.rgf[a as int] as int); + assert(0 <= x < 0x1_0000_0000_0000_0000); + let r = x64::gpr_sub_spec(n, x, e, m.rflags); + assert(x64::gpr_arith_logic_spec(n, x64::AluOp::Cmp, x, e, m.rflags) == r); + super::alu::lemma_wur_frame(cu, m, r.rflags, r.undef); + assert(r.undef == 0); + assert(x64::bit(0, 0) == 0 && x64::bit(0, 6) == 0 && x64::bit(0, 7) == 0 && x64::bit(0, 11) + == 0) by { + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::p2(0) == 1 && x64::p2(6) == 64 && x64::p2(7) == 128 && x64::p2(11) == 2048); + } + super::flags::lemma_wur_flag(cu, m, r.rflags, r.undef, 0); + super::flags::lemma_wur_flag(cu, m, r.rflags, r.undef, 6); + super::flags::lemma_wur_flag(cu, m, r.rflags, r.undef, 7); + super::flags::lemma_wur_flag(cu, m, r.rflags, r.undef, 11); + vstd::arithmetic::div_mod::lemma_small_mod(x as nat, 0x1_0000_0000_0000_0000nat); + vstd::arithmetic::div_mod::lemma_small_mod(e as nat, 0x1_0000_0000_0000_0000nat); + let lx = x64::loghead(n, x); + let ly = x64::loghead(n, e); + assert(lx == x && ly == e); + let sraw = x64::logext(n, lx) - x64::logext(n, ly); + let res = x64::loghead(n, sraw); + let f = x64::FlagUpd { + cf: Some(x64::bool_to_bit(lx < ly)), + pf: Some(x64::pf_spec(res)), + af: Some(x64::sub_af_spec(lx, ly)), + zf: Some(x64::zf_spec(res)), + sf: Some(x64::sf_spec(n, res)), + of: Some(x64::of_spec(n, sraw)), + }; + assert(r.rflags == x64::change_rflags(m.rflags, f)); + super::flags::lemma_change_rflags_bit(m.rflags, f, 0); + super::flags::lemma_change_rflags_bit(m.rflags, f, 6); + super::flags::lemma_change_rflags_bit(m.rflags, f, 7); + super::flags::lemma_change_rflags_bit(m.rflags, f, 11); +} + +/// [`lemma_step_cmp_rm64_flags`], and what the three condition codes of the +/// bounds check read from those flags (`b`, `ae`, `a`). +pub proof fn lemma_step_cmp_rm64( + cu: spec_fn(nat) -> nat, + m: x64::State, + a: u8, + mem: x64::Mem, + e: int, +) + requires + x64::wf(m), + a < 16, + x64::read_mem(m.mem, x64::eff_addr(m, mem), 8) == Some(e), + 0 <= e < 0x1_0000_0000_0000_0000, + x64::canonical( + m.rip + x64::instr_len( + x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: a, mem: mem }, + ), + ), + ensures + x64::step( + cu, + m, + x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: a, mem: mem }, + ) is Some, + ({ + let m1 = x64::step( + cu, + m, + x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: a, mem: mem }, + ).unwrap(); + let lx = m.rgf[a as int] as int; + &&& m1.rgf == m.rgf && m1.mem == m.mem && m1.xmm == m.xmm + &&& m1.rip == m.rip + x64::instr_len( + x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: a, mem: mem }, + ) + &&& x64::jcc_cmovcc_setcc_spec(m1, 2) == (lx < e) + &&& x64::jcc_cmovcc_setcc_spec(m1, 3) == (lx >= e) + &&& x64::jcc_cmovcc_setcc_spec(m1, 7) == (lx > e) + }), +{ + lemma_step_cmp_rm64_flags(cu, m, a, mem, e); + let i = x64::Instr::AluRM { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: a, mem: mem }; + let m1 = x64::step(cu, m, i).unwrap(); + let lx = m.rgf[a as int] as int; + super::memlemmas::lemma_p2_consts(); + assert(0 <= lx < 0x1_0000_0000_0000_0000); + assert(x64::p2(64) == 0x1_0000_0000_0000_0000); + super::flags::lemma_cc_icmp(m1, lir::ICmp::LtU, 64, lx, e); + super::flags::lemma_cc_icmp(m1, lir::ICmp::GeU, 64, lx, e); + super::flags::lemma_cc_icmp(m1, lir::ICmp::GtU, 64, lx, e); + assert(super::templates::cc_of(lir::ICmp::LtU) == 2 && super::templates::cc_of(lir::ICmp::GeU) + == 3 && super::templates::cc_of(lir::ICmp::GtU) == 7); +} + +/// `mov dst, [mem]` of either size, when the read gives `v`. +pub proof fn lemma_step_movload( + cu: spec_fn(nat) -> nat, + m: x64::State, + sz: x64::Size, + dst: u8, + mem: x64::Mem, + v: int, +) + requires + x64::wf(m), + dst < 16, + x64::read_mem(m.mem, x64::eff_addr(m, mem), x64::nbytes(sz)) == Some(v), + x64::canonical( + m.rip + x64::instr_len(x64::Instr::MovLoad { sz: sz, dst: dst, mem: mem }), + ), + ensures + x64::step(cu, m, x64::Instr::MovLoad { sz: sz, dst: dst, mem: mem }) == Some( + x64::State { + rgf: x64::set_rgfi_size(m, sz, dst, v).rgf, + rip: m.rip + x64::instr_len(x64::Instr::MovLoad { sz: sz, dst: dst, mem: mem }), + ..m + }, + ), +{ +} + +/// `mov [mem], src` of either size, when the write succeeds. +pub proof fn lemma_step_movstore( + cu: spec_fn(nat) -> nat, + m: x64::State, + sz: x64::Size, + mem: x64::Mem, + src: u8, + mem2: Map, +) + requires + x64::wf(m), + src < 16, + x64::write_mem(m.mem, x64::eff_addr(m, mem), x64::nbytes(sz), x64::rgfi_size(m, sz, src)) + == Some(mem2), + x64::canonical( + m.rip + x64::instr_len(x64::Instr::MovStore { sz: sz, mem: mem, src: src }), + ), + ensures + x64::step(cu, m, x64::Instr::MovStore { sz: sz, mem: mem, src: src }) == Some( + x64::State { + mem: mem2, + rip: m.rip + x64::instr_len(x64::Instr::MovStore { sz: sz, mem: mem, src: src }), + ..m + }, + ), +{ +} + +} // verus! diff --git a/crates/clazel-jit/src/cfmemvalid.rs b/crates/clazel-jit/src/cfmemvalid.rs new file mode 100644 index 000000000..69af15d0f --- /dev/null +++ b/crates/clazel-jit/src/cfmemvalid.rs @@ -0,0 +1,204 @@ +//! The code of the memory instructions and of `br_table` is in the x86-64 +//! subset ([`x64::valid_instr`]), and the validator's access rule admits the +//! access a memory instruction makes after its check. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::cfvalid::*; +#[cfg(verus_only)] +use crate::contract::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every instruction of the bounds-check sequence is in the subset. +pub proof fn lemma_check_valid(c: clazel_codecheck::spec::Chk) + requires + c.i == rcx() && c.t == rdx() && c.z == rax() && 0 <= c.k < 16, + ensures + all_valid_instrs(clazel_codecheck::spec::check_seq(c)), +{ + let cs = clazel_codecheck::spec::check_seq(c); + assert forall|j: int| 0 <= j < cs.len() implies x64::valid_instr(#[trigger] cs[j]) by { + assert(j == 0 || j == 1 || j == 2 || j == 3 || j == 4 || j == 5 || j == 6 || j == 7); + assert(cs[0] == clazel_codecheck::spec::check_instr(0, c)); + assert(cs[1] == clazel_codecheck::spec::check_instr(1, c)); + assert(cs[2] == clazel_codecheck::spec::check_instr(2, c)); + assert(cs[3] == clazel_codecheck::spec::check_instr(3, c)); + assert(cs[4] == clazel_codecheck::spec::check_instr(4, c)); + assert(cs[5] == clazel_codecheck::spec::check_instr(5, c)); + assert(cs[6] == clazel_codecheck::spec::check_instr(6, c)); + assert(cs[7] == clazel_codecheck::spec::check_instr(7, c)); + } +} + +/// **A memory instruction's code is in the subset.** +pub proof fn lemma_mem_valid(mo: MemOp, nl: nat, h: nat) + requires + cf_flow(Node::Mem(mo), nl, h, 0) is Some, + ensures + all_valid_instrs(mem_code(mo, nl, h)), +{ + let k = mem_region(mo); + assert(k < 16); + let pre = mem_pre(mo, nl, h); + let cs = mem_check(mo, nl, h); + let tail = mem_tail(mo, nl, h); + let stub = exit_seq(mem_exit_code(mo)); + let jm = seq![jmp_i(code_len(stub) as int)]; + crate::tlemmas::lemma_exit_valid(mem_exit_code(mo)); + lemma_check_valid(mem_chk(k, mem_n(mo), mem_r1(mo, nl, h), mem_r2(mo, nl, h))); + assert(all_valid_instrs(jm)); + assert(all_valid_instrs(pre)) by { + assert forall|q: int| 0 <= q < pre.len() implies x64::valid_instr(#[trigger] pre[q]) by { + match mo { + MemOp::BoundsCheck { .. } => { + crate::tlemmas::lemma_ls_valid(rcx(), (nl + h - 1) as nat); + assert(q == 0); + }, + MemOp::Load { .. } => { + crate::tlemmas::lemma_ls_valid(rcx(), (nl + h - 1) as nat); + assert(q == 0 || q == 1); + }, + MemOp::Store { .. } => { + crate::tlemmas::lemma_ls_valid(rdi(), (nl + h - 1) as nat); + crate::tlemmas::lemma_ls_valid(rcx(), (nl + h - 2) as nat); + assert(q == 0 || q == 1 || q == 2); + }, + } + } + } + assert(all_valid_instrs(tail)) by { + assert forall|q: int| 0 <= q < tail.len() implies x64::valid_instr(#[trigger] tail[q]) by { + match mo { + MemOp::BoundsCheck { .. } => { + assert(false); + }, + MemOp::Load { .. } => { + crate::tlemmas::lemma_ls_valid(rax(), (nl + h - 1) as nat); + assert(q == 0 || q == 1); + }, + MemOp::Store { .. } => { + assert(q == 0); + }, + } + } + } + lemma_valid_cat(pre, cs); + lemma_valid_cat(pre + cs, tail); + lemma_valid_cat(pre + cs + tail, jm); + lemma_valid_cat(pre + cs + tail + jm, stub); +} + +/// The chain's entries are in the subset. +pub proof fn lemma_entries_valid(ls: Seq, offs: Seq, p0: int, i: nat) + ensures + all_valid_instrs(brt_entries(ls, offs, p0, i)), + decreases ls.len() - i, +{ + if i < ls.len() { + let e = brt_entry(i, brt_rel(offs, ls[i as int], p0 + (i + 1) * brt_e())); + assert(all_valid_instrs(e)) by { + assert forall|q: int| 0 <= q < e.len() implies x64::valid_instr(#[trigger] e[q]) by { + assert(q == 0 || q == 1 || q == 2); + } + } + lemma_entries_valid(ls, offs, p0, i + 1); + lemma_valid_cat(e, brt_entries(ls, offs, p0, i + 1)); + } else { + assert(brt_entries(ls, offs, p0, i) =~= Seq::::empty()); + } +} + +/// **`br_table`'s code is in the subset.** +pub proof fn lemma_brt_valid(ls: Seq, d: usize, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + ensures + all_valid_instrs(brt_code(ls, d, nl, h, offs)), +{ + let p0 = brt_p0(nl, h); + let a = seq![load_slot(rax(), (nl + h - 1) as nat)]; + crate::tlemmas::lemma_ls_valid(rax(), (nl + h - 1) as nat); + assert(all_valid_instrs(a)); + lemma_entries_valid(ls, offs, p0, 0); + let jm = seq![jmp_i(brt_rel(offs, d, p0 + ls.len() * brt_e() + 5))]; + assert(all_valid_instrs(jm)); + lemma_valid_cat(a, brt_entries(ls, offs, p0, 0)); + lemma_valid_cat(a + brt_entries(ls, offs, p0, 0), jm); +} + +/// **The validator accepts the access.** After `mov rsi, [r15 + 16 k]` and +/// the bounds check of `n` bytes on `rcx`, the facts the validator holds +/// ([`clazel_codecheck::spec::check_facts`]: `rsi` is region `k`'s base, `rdx` +/// and `rax` are unknown, `rcx` is checked for `n` bytes) admit `[rsi + rcx]` +/// for an access of at most `n` bytes, a load or a store +/// ([`clazel_codecheck::spec::access_ok`]). +pub proof fn lemma_access_accepted(k: int, n: int, size: int, store: bool) + requires + size <= n, + ensures + clazel_codecheck::spec::access_ok( + seq![ + clazel_codecheck::spec::Binding { + reg: 6, + fact: clazel_codecheck::spec::Fact::Base(k), + }, + clazel_codecheck::spec::Binding { + reg: 2, + fact: clazel_codecheck::spec::Fact::Unknown, + }, + clazel_codecheck::spec::Binding { + reg: 0, + fact: clazel_codecheck::spec::Fact::Unknown, + }, + clazel_codecheck::spec::Binding { + reg: 1, + fact: clazel_codecheck::spec::Fact::Idx(k, n), + }, + ], + clazel_codecheck::spec::Access { mem: ix_mem(), size: size, store: store }, + ), +{ + reveal_with_fuel(clazel_codecheck::spec::fact_of, 6); +} + +/// The operand size of an access writes or reads as many bytes as its type +/// has (so the access is within the `n` bytes the check admitted). +pub proof fn lemma_mty_sz_bytes(ty: MTy) + ensures + x64::nbytes(mty_sz(ty)) == ty.size(), +{ +} + +/// The access size of a type is LIR's. +pub proof fn lemma_mty_size_is_lir(ty: MTy) + ensures + lir::ty_size(ty.lir_ty()) == ty.size(), +{ +} + +/// A `bounds_check`'s stub exits with the instruction's own code. +pub proof fn lemma_bc_exit_code(region: usize, size: usize, code: u32) + ensures + mem_exit_code(MemOp::BoundsCheck { region, size, code }) == code, +{ +} + +/// The chain's entry jumps on equal (`je`: the condition code of LIR's `eq`). +pub proof fn lemma_brt_cc(i: nat, rel: int) + ensures + brt_entry(i, rel)[2]->Jcc_cc == crate::templates::cc_of(lir::ICmp::Eq), +{ +} + +} // verus! diff --git a/crates/clazel-jit/src/cfop.rs b/crates/clazel-jit/src/cfop.rs new file mode 100644 index 000000000..565bbaece --- /dev/null +++ b/crates/clazel-jit/src/cfop.rs @@ -0,0 +1,460 @@ +//! The straight-line templates have no jumps, and run by `steps` as they do +//! by `run_seq`: the bridge from the first slice's [`refines`] to the +//! control-flow statement [`cf_refines`] for a straight-line instruction. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfalg::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cfsteps::*; +#[cfg(verus_only)] +use crate::contract::*; +#[cfg(verus_only)] +use crate::templates::*; +#[cfg(verus_only)] +use crate::tlemmas::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// No instruction of a template is a jump or `ret`. +pub proof fn lemma_tmpl_no_jumps(op: Op, nl: nat, h: nat) + requires + op_ok(op, nl, h), + ensures + no_jumps(crate::templates::tmpl(op, nl, h)), +{ + assert(max_slots() == 31) by { + lemma_abi_consts_t(); + } + let code = crate::templates::tmpl(op, nl, h); + assert forall|k: int| 0 <= k < code.len() implies !(#[trigger] code[k] is Jcc || code[k] is Jmp + || code[k] is Ret) by { + match op { + Op::Nop => {}, + Op::Drop => {}, + Op::Const(v) => { + lemma_shape0(seq![crate::templates::const_instr(v)], nl + h); + lemma_ls_valid(0, nl + h); + assert(code =~= seq![crate::templates::const_instr(v), crate::templates::store_slot(nl + h, rax())]); + }, + Op::LocalGet(x) => { + lemma_shape1(rax(), x as nat, seq![], nl + h); + lemma_ls_valid(0, nl + h); + lemma_ls_valid(0, x as nat); + assert(code =~= seq![crate::templates::load_slot(rax(), x as nat), crate::templates::store_slot(nl + h, rax())]); + }, + Op::LocalSet(x) => { + lemma_shape1(rax(), (nl + h - 1) as nat, seq![], x as nat); + lemma_ls_valid(0, x as nat); + lemma_ls_valid(0, (nl + h - 1) as nat); + assert(code =~= seq![crate::templates::load_slot(rax(), (nl + h - 1) as nat), crate::templates::store_slot(x as nat, rax())]); + }, + Op::LocalTee(x) => { + lemma_shape1(rax(), (nl + h - 1) as nat, seq![], x as nat); + lemma_ls_valid(0, x as nat); + lemma_ls_valid(0, (nl + h - 1) as nat); + assert(code =~= seq![crate::templates::load_slot(rax(), (nl + h - 1) as nat), crate::templates::store_slot(x as nat, rax())]); + }, + Op::Select => { + let t = (nl + h - 3) as nat; + lemma_shape3(rax(), t, rcx(), t + 1, rdx(), t + 2, crate::templates::select_mid(), t); + lemma_ls_valid(0, t); + lemma_ls_valid(1, t + 1); + lemma_ls_valid(2, t + 2); + assert(code =~= seq![ + crate::templates::load_slot(rax(), t), + crate::templates::load_slot(rcx(), t + 1), + crate::templates::load_slot(rdx(), t + 2), + crate::templates::select_mid()[0], + crate::templates::select_mid()[1], + crate::templates::store_slot(t, rax()), + ]); + }, + Op::Bin(b) => { + let t = (nl + h - 2) as nat; + match b { + lir::BinOp::I32(o) => { + lemma_shape2(rax(), t, rcx(), t + 1, seq![crate::templates::int_op_instr(o, x64::Size::W32)], t); + lemma_ls_valid(0, t); + lemma_ls_valid(1, t + 1); + assert(code =~= seq![ + crate::templates::load_slot(rax(), t), + crate::templates::load_slot(rcx(), t + 1), + crate::templates::int_op_instr(o, x64::Size::W32), + crate::templates::store_slot(t, rax()), + ]); + }, + lir::BinOp::I64(o) => { + lemma_shape2(rax(), t, rcx(), t + 1, seq![crate::templates::int_op_instr(o, x64::Size::W64)], t); + lemma_ls_valid(0, t); + lemma_ls_valid(1, t + 1); + assert(code =~= seq![ + crate::templates::load_slot(rax(), t), + crate::templates::load_slot(rcx(), t + 1), + crate::templates::int_op_instr(o, x64::Size::W64), + crate::templates::store_slot(t, rax()), + ]); + }, + lir::BinOp::I32Cmp(c) => { + lemma_shape2(rcx(), t, rdx(), t + 1, crate::templates::cmp_mid(c, x64::Size::W32), t); + lemma_ls_valid(1, t); + lemma_ls_valid(2, t + 1); + lemma_ls_valid(0, t); + assert(code =~= seq![ + crate::templates::load_slot(rcx(), t), + crate::templates::load_slot(rdx(), t + 1), + crate::templates::cmp_mid(c, x64::Size::W32)[0], + crate::templates::cmp_mid(c, x64::Size::W32)[1], + crate::templates::cmp_mid(c, x64::Size::W32)[2], + crate::templates::store_slot(t, rax()), + ]); + }, + lir::BinOp::I64Cmp(c) => { + lemma_shape2(rcx(), t, rdx(), t + 1, crate::templates::cmp_mid(c, x64::Size::W64), t); + lemma_ls_valid(1, t); + lemma_ls_valid(2, t + 1); + lemma_ls_valid(0, t); + assert(code =~= seq![ + crate::templates::load_slot(rcx(), t), + crate::templates::load_slot(rdx(), t + 1), + crate::templates::cmp_mid(c, x64::Size::W64)[0], + crate::templates::cmp_mid(c, x64::Size::W64)[1], + crate::templates::cmp_mid(c, x64::Size::W64)[2], + crate::templates::store_slot(t, rax()), + ]); + }, + lir::BinOp::F64(_) => { + assert(false); + }, + lir::BinOp::F64Cmp(_) => { + assert(false); + }, + } + }, + Op::Un(u) => { + let t = (nl + h - 1) as nat; + lemma_ls_valid(0, t); + lemma_ls_valid(1, t); + match u { + lir::UnOp::I32Eqz => { + lemma_shape1(rcx(), t, crate::templates::eqz_mid(x64::Size::W32), t); + assert(code =~= seq![ + crate::templates::load_slot(rcx(), t), + crate::templates::eqz_mid(x64::Size::W32)[0], + crate::templates::eqz_mid(x64::Size::W32)[1], + crate::templates::eqz_mid(x64::Size::W32)[2], + crate::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::I64Eqz => { + lemma_shape1(rcx(), t, crate::templates::eqz_mid(x64::Size::W64), t); + assert(code =~= seq![ + crate::templates::load_slot(rcx(), t), + crate::templates::eqz_mid(x64::Size::W64)[0], + crate::templates::eqz_mid(x64::Size::W64)[1], + crate::templates::eqz_mid(x64::Size::W64)[2], + crate::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::F64Neg => { + lemma_shape1(rax(), t, crate::templates::mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000), t); + assert(code =~= seq![ + crate::templates::load_slot(rax(), t), + crate::templates::mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000)[0], + crate::templates::mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000)[1], + crate::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::F64Abs => { + lemma_shape1(rax(), t, crate::templates::mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF), t); + assert(code =~= seq![ + crate::templates::load_slot(rax(), t), + crate::templates::mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF)[0], + crate::templates::mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF)[1], + crate::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::I64ExtendI32S => { + lemma_shape1(rax(), t, crate::templates::exts_mid(), t); + assert(code =~= seq![ + crate::templates::load_slot(rax(), t), + crate::templates::exts_mid()[0], + crate::templates::exts_mid()[1], + crate::templates::exts_mid()[2], + crate::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::I32WrapI64 => { + lemma_shape1(rax(), t, crate::templates::wrap_mid(), t); + assert(code =~= seq![ + crate::templates::load_slot(rax(), t), + crate::templates::wrap_mid()[0], + crate::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::I64ExtendI32U => {}, + lir::UnOp::F64ReinterpretI64 => {}, + lir::UnOp::I64ReinterpretF64 => {}, + lir::UnOp::F64Sqrt => { + assert(false); + }, + lir::UnOp::F64ConvertI32S => { + assert(false); + }, + lir::UnOp::F64ConvertI32U => { + assert(false); + }, + lir::UnOp::F64ConvertI64S => { + assert(false); + }, + lir::UnOp::F64ConvertI64U => { + assert(false); + }, + } + }, + } + } +} + + + +/// Taking `k` values off the top of a stack with a base under it. +pub proof fn lemma_below_cat(base: Seq, s: Seq, k: nat) + requires + k <= s.len(), + ensures + lir::below(base + s, k) =~= base + lir::below(s, k), +{ +} + +/// **A straight-line instruction's evaluation does not look below its +/// operands.** If it falls through from `a`, then from the same state with +/// a base under the stack it falls through to the same state with the same +/// base under the stack. +pub proof fn lemma_op_frame( + op: Op, + lay: lir::Layout, + a: lir::St, + a2: lir::St, + base: Seq, + fuel: nat, +) + requires + lir::eval(lay, op@, a, fuel) == lir::Outcome::Normal(a2), + ensures + lir::eval(lay, op@, a.with_stack(base + a.stack), fuel) == lir::Outcome::Normal( + a2.with_stack(base + a2.stack), + ), +{ + assert(fuel > 0); + let ac = a.with_stack(base + a.stack); + let n = a.stack.len(); + let nc = ac.stack.len(); + assert(nc == base.len() + n); + match op { + Op::Nop => {}, + Op::Const(v) => { + assert((base + a.stack).push(v) =~= base + a.stack.push(v)); + }, + Op::LocalGet(x) => { + assert((base + a.stack).push(a.locals[x as int]) =~= base + a.stack.push( + a.locals[x as int], + )); + }, + Op::LocalSet(x) => { + assert(n >= 1); + assert((base + a.stack)[nc - 1] == a.stack[n - 1]); + assert((base + a.stack).drop_last() =~= base + a.stack.drop_last()); + }, + Op::LocalTee(x) => { + assert(n >= 1); + assert((base + a.stack)[nc - 1] == a.stack[n - 1]); + }, + Op::Drop => { + assert(n >= 1); + assert((base + a.stack).drop_last() =~= base + a.stack.drop_last()); + }, + Op::Select => { + assert(n >= 3); + assert((base + a.stack)[nc - 1] == a.stack[n - 1]); + assert((base + a.stack)[nc - 2] == a.stack[n - 2]); + assert((base + a.stack)[nc - 3] == a.stack[n - 3]); + lemma_below_cat(base, a.stack, 3); + match a.stack[n - 1] { + lir::Val::I32(c) => { + let v = if c != 0 { + a.stack[n - 3] + } else { + a.stack[n - 2] + }; + assert(lir::below(base + a.stack, 3).push(v) =~= base + lir::below( + a.stack, + 3, + ).push(v)); + }, + _ => { + assert(false); + }, + } + }, + Op::Bin(b) => { + assert(n >= 2); + assert((base + a.stack)[nc - 1] == a.stack[n - 1]); + assert((base + a.stack)[nc - 2] == a.stack[n - 2]); + lemma_below_cat(base, a.stack, 2); + match lir::bin_eval(b, a.stack[n - 2], a.stack[n - 1]) { + Some(v) => { + assert(lir::below(base + a.stack, 2).push(v) =~= base + lir::below( + a.stack, + 2, + ).push(v)); + }, + None => { + assert(false); + }, + } + }, + Op::Un(u) => { + assert(n >= 1); + assert((base + a.stack)[nc - 1] == a.stack[n - 1]); + lemma_below_cat(base, a.stack, 1); + match lir::un_eval(u, a.stack[n - 1]) { + Some(v) => { + assert(lir::below(base + a.stack, 1).push(v) =~= base + lir::below( + a.stack, + 1, + ).push(v)); + }, + None => { + assert(false); + }, + } + }, + } +} + +/// A straight-line instruction's evaluation is a fall-through, stuck, or out +/// of fuel: never a branch or an exit. +pub proof fn lemma_op_shape(op: Op, lay: lir::Layout, a: lir::St, fuel: nat) + ensures + !(lir::eval(lay, op@, a, fuel) is Branch), + !(lir::eval(lay, op@, a, fuel) is Exit), +{ + if fuel > 0 { + match op { + Op::Select => { + let n = a.stack.len(); + if n >= 3 { + match a.stack[n - 1] { + lir::Val::I32(c) => {}, + _ => {}, + } + } + }, + Op::Bin(b) => { + let n = a.stack.len(); + if n >= 2 { + match lir::bin_eval(b, a.stack[n - 2], a.stack[n - 1]) { + Some(v) => {}, + None => {}, + } + } + }, + Op::Un(u) => { + let n = a.stack.len(); + if n >= 1 { + match lir::un_eval(u, a.stack[n - 1]) { + Some(v) => {}, + None => {}, + } + } + }, + _ => {}, + } + } +} + +/// `preserved` (nothing outside the exit area changed) is [`preserved_r`]. +pub proof fn lemma_preserved_to_r(lay: lir::Layout, m: x64::State, m2: x64::State) + requires + preserved(m, m2), + ensures + preserved_r(lay, m, m2), +{ + assert(ctx_of(m2) == ctx_of(m)) by { + crate::steps::lemma_abi_consts(); + } +} + +/// **A straight-line instruction's template refines it, in the +/// control-flow statement.** The first slice's [`refines`] (for the template, +/// as one block of code) becomes [`cf_refines`] for any labels: the base +/// under the stack is untouched (the frame lemma), and a run by +/// [`run_seq`] of jump-free code is a run by [`steps`]. +#[verifier::rlimit(400)] +pub proof fn lemma_op_cf(op: Op, nl: nat, h: nat, offs: Seq) + requires + op_ok(op, nl, h), + ensures + cf_refines(op@, nl, h, offs, crate::templates::tmpl(op, nl, h)), +{ + let code = crate::templates::tmpl(op, nl, h); + crate::proofs::lemma_op_refines(op, nl, h); + lemma_tmpl_no_jumps(op, nl, h); + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + base: Seq, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger cf_post(lay, base, nl, offs, code, cu, m, lir::eval(lay, op@, a, fuel))] + base.len() + a.stack.len() == h && rel_base(lay, base, a, m, nl) && stack_ok_r(lay, m) + && cf_canon(m.rip, offs, code) implies cf_post( + lay, + base, + nl, + offs, + code, + cu, + m, + lir::eval(lay, op@, a, fuel), + ) by { + lemma_op_shape(op, lay, a, fuel); + match lir::eval(lay, op@, a, fuel) { + lir::Outcome::Normal(a2) => { + crate::cfalg::lemma_rel_base_unfold(lay, base, a, m, nl); + let ac = a.with_stack(base + a.stack); + lemma_op_frame(op, lay, a, a2, base, fuel); + let a2c = a2.with_stack(base + a2.stack); + crate::proofs::lemma_use_refines(op@, nl, h, code, lay, cu, ac, m, fuel); + assert(ac.stack.len() == h); + assert(x64::canonical(m.rip + code_len(code))); + let m2 = run_seq(cu, m, code).unwrap(); + assert(rel(lay, a2c, m2, nl) && m2.rip == m.rip + code_len(code) && preserved( + m, + m2, + )); + assert(Seq::::empty() + code + Seq::::empty() =~= code); + assert(code_len(Seq::::empty()) == 0); + lemma_run_seq_steps(cu, Seq::empty(), code, Seq::empty(), m.rip, m, m2); + assert(reaches(cu, code, m.rip, m, m2)); + lemma_preserved_to_r(lay, m, m2); + crate::cfalg::lemma_table_pres(lay, m, m2); + reveal(rel_base); + assert(rel_base(lay, base, a2, m2, nl)); + }, + _ => {}, + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfproofs.rs b/crates/clazel-jit/src/cfproofs.rs new file mode 100644 index 000000000..bf3259009 --- /dev/null +++ b/crates/clazel-jit/src/cfproofs.rs @@ -0,0 +1,243 @@ +//! **The refinement of every templated node** ([`lemma_ctmpl_refines`]): by +//! induction on the node, from the leaves ([`crate::cfop`], [`crate::cfleaf`], +//! [`crate::cfexit`]) and the compositions ([`crate::cfcompose`], +//! [`crate::cfblock`], [`crate::cfif`]). The induction also carries the size +//! invariant that every `jmp` and `jcc` displacement is a 32-bit number. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfblock::*; +#[cfg(verus_only)] +use crate::cfbrt::*; +#[cfg(verus_only)] +use crate::cfcompose::*; +#[cfg(verus_only)] +use crate::cfcontract::*; +#[cfg(verus_only)] +use crate::cfexit::*; +#[cfg(verus_only)] +use crate::cfif::*; +#[cfg(verus_only)] +use crate::cfleaf::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfmem::*; +#[cfg(verus_only)] +use crate::cfmemops::*; +#[cfg(verus_only)] +use crate::cfop::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::contract::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every label offset is far enough from every place a jump in code of +/// `size` bytes can be, that the displacement is a 32-bit number: from a +/// jump ending at most `size + 5` bytes in, to a label at `o`, is `o - (size + +/// 5) >= -2^31` and `o <= 2^31 - 1`. +pub open spec fn labels_fit(offs: Seq, size: nat) -> bool { + forall|l: int| + 0 <= l < offs.len() ==> -0x8000_0000 + size + 6 <= #[trigger] offs[l] && offs[l] + <= 0x7FFF_FFFF +} + +/// Labels seen from code that starts `d` bytes in, for a smaller piece. +pub proof fn lemma_fit_shift(offs: Seq, d: nat, size: nat, size2: nat) + requires + labels_fit(offs, size), + d + size2 <= size, + ensures + labels_fit(shift(offs, d as int), size2), +{ + assert forall|l: int| 0 <= l < shift(offs, d as int).len() implies -0x8000_0000 + size2 + 6 + <= #[trigger] shift(offs, d as int)[l] && shift(offs, d as int)[l] <= 0x7FFF_FFFF by { + lemma_shift_index(offs, d as int, l); + } +} + +/// A new label in front. +pub proof fn lemma_fit_cons(x: int, offs: Seq, size: nat) + requires + labels_fit(offs, size), + -0x8000_0000 + size + 6 <= x <= 0x7FFF_FFFF, + ensures + labels_fit(seq![x] + offs, size), +{ + let all = seq![x] + offs; + assert forall|l: int| 0 <= l < all.len() implies -0x8000_0000 + size + 6 <= #[trigger] all[l] + && all[l] <= 0x7FFF_FFFF by { + if l > 0 { + assert(all[l] == offs[l - 1]); + } + } +} + +/// `labels_fit` for a smaller size. +pub proof fn lemma_fit_mono(offs: Seq, size: nat, size2: nat) + requires + labels_fit(offs, size), + size2 <= size, + ensures + labels_fit(offs, size2), +{ +} + +/// **Every templated node's code refines it.** For a node the analysis +/// accepts and that has a template, entered at height `h` with `nl` locals +/// and the labels at the byte offsets `offs`, [`ctmpl`] refines it +/// ([`cf_refines`]), provided the code is small enough for its displacements +/// ([`labels_fit`]). +#[verifier::rlimit(1000)] +pub proof fn lemma_ctmpl_refines(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + templated(n), + csize(n, nl, h, offs.len()) <= max_size(), + labels_fit(offs, csize(n, nl, h, offs.len())), + ensures + cf_refines(n@, nl, h, offs, ctmpl(n, nl, h, offs)), + decreases n, +{ + let d = offs.len(); + let size = csize(n, nl, h, d); + lemma_ctmpl_len(n, nl, h, offs); + match n { + Node::Op(o) => { + lemma_op_cf(o, nl, h, offs); + }, + Node::Mem(m) => { + match m { + MemOp::BoundsCheck { region, size, code } => { + lemma_bc_cf(region, size, code, nl, h, offs); + }, + MemOp::Load { region, ty } => { + lemma_load_cf(region, ty, nl, h, offs); + }, + MemOp::Store { region, ty } => { + lemma_store_cf(region, ty, nl, h, offs); + }, + } + }, + Node::Then(a, b) => { + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + lemma_csize_then(*a, *b, nl, h, d); + assert(flow_h(*a, nl, h, d) == h1); + let sa = csize(*a, nl, h, d); + let sb = csize(*b, nl, h1, d); + lemma_fit_mono(offs, size, sa); + lemma_ctmpl_refines(*a, nl, h, offs); + let ob = shift(offs, sa as int); + lemma_fit_shift(offs, sa, size, sb); + assert(ob.len() == d); + lemma_ctmpl_refines(*b, nl, h1, ob); + lemma_ctmpl_len(*a, nl, h, offs); + lemma_then( + *a, + *b, + nl, + h, + h1, + offs, + ctmpl(*a, nl, h, offs), + ctmpl(*b, nl, h1, ob), + ); + }, + Some(Flow::Dead) => { + // the second is `nop`: no code + lemma_csize_then(*a, *b, nl, h, d); + lemma_fit_mono(offs, size, csize(*a, nl, h, d)); + lemma_ctmpl_refines(*a, nl, h, offs); + lemma_then_dead(*a, nl, h, offs, ctmpl(*a, nl, h, offs)); + assert(ctmpl(n, nl, h, offs) == ctmpl(*a, nl, h, offs) + ctmpl( + *b, + nl, + h, + shift(offs, csize(*a, nl, h, d) as int), + )); + assert(ctmpl(*b, nl, h, shift(offs, csize(*a, nl, h, d) as int)) =~= Seq::< + x64::Instr, + >::empty()); + assert(ctmpl(*a, nl, h, offs) + Seq::::empty() =~= ctmpl( + *a, + nl, + h, + offs, + )); + }, + None => {}, + } + }, + Node::Block(b) => { + lemma_csize_block(*b, nl, h, d); + let sb = csize(*b, nl, h, d + 1); + let ob = seq![sb as int] + offs; + lemma_fit_cons(sb as int, offs, sb); + lemma_fit_mono(offs, size, sb); + lemma_fit_cons(sb as int, offs, sb); + lemma_ctmpl_refines(*b, nl, h, ob); + lemma_ctmpl_len(*b, nl, h, ob); + lemma_block(*b, nl, h, offs, ctmpl(*b, nl, h, ob)); + }, + Node::Loop(b) => { + lemma_csize_loop(*b, nl, h, d); + let sb = csize(*b, nl, h, d + 1); + let ob = seq![0int] + offs; + lemma_fit_mono(offs, size, sb); + lemma_fit_cons(0, offs, sb); + lemma_ctmpl_refines(*b, nl, h, ob); + lemma_loop(*b, nl, h, offs, ctmpl(*b, nl, h, ob)); + }, + Node::If(t, e) => { + lemma_csize_if(*t, *e, nl, h, d); + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p); + let oe = seq![se] + shift(offs, p + st + 5); + lemma_fit_shift(offs, p as nat, size, st as nat); + lemma_fit_cons(st + 5 + se, shift(offs, p), st as nat); + lemma_fit_shift(offs, (p + st + 5) as nat, size, se as nat); + lemma_fit_cons(se, shift(offs, p + st + 5), se as nat); + assert(cf_flow(*t, nl, (h - 1) as nat, d + 1) is Some); + assert(cf_flow(*e, nl, (h - 1) as nat, d + 1) is Some); + lemma_ctmpl_refines(*t, nl, (h - 1) as nat, ot); + lemma_ctmpl_refines(*e, nl, (h - 1) as nat, oe); + let ct = ctmpl(*t, nl, (h - 1) as nat, ot); + let ce = ctmpl(*e, nl, (h - 1) as nat, oe); + lemma_ctmpl_len(*t, nl, (h - 1) as nat, ot); + lemma_ctmpl_len(*e, nl, (h - 1) as nat, oe); + assert(code_len(ct) == st && code_len(ce) == se); + assert(if_offs_t(nl, h, offs, ct, ce) == ot); + assert(if_offs_e(nl, h, offs, ct, ce) == oe); + lemma_if(*t, *e, nl, h, offs, ct, ce); + assert(ctmpl(n, nl, h, offs) == if_code(nl, h, ct, ce)); + }, + Node::Br(l) => { + lemma_csize_br(l, nl, h, d); + assert(l < offs.len()); + lemma_br(nl, h, offs, l); + }, + Node::BrIf(l) => { + lemma_csize_brif(l, nl, h, d); + lemma_brif(nl, h, offs, l); + }, + Node::BrTable(ls, d) => { + crate::cfbrt::lemma_brt_fetch_jmp(ls@, d, nl, h, offs); + lemma_brt_cf(ls, d, nl, h, offs); + }, + Node::Exit(c, k) => { + lemma_exit_cf(c, k, nl, h, offs); + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfsimple.rs b/crates/clazel-jit/src/cfsimple.rs new file mode 100644 index 000000000..0cba5251e --- /dev/null +++ b/crates/clazel-jit/src/cfsimple.rs @@ -0,0 +1,321 @@ +//! The straight-line templates are single instructions the validator accepts +//! whatever it knows (jit.md item J.9, "every emitted blob is accepted", +//! x86-64): none writes the context register or the stack pointer, none +//! touches memory except the context (the frame), none is a jump, and none is +//! a `ret` or a `jcc 2`. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::cfunits::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The instruction's memory access, if any, is to the context at a fixed +/// offset, and a store is to the exit area: the validator admits it whatever +/// it knows. +pub open spec fn ctx_only(i: x64::Instr) -> bool { + match clazel_codecheck::spec::mem_access(i) { + None => true, + Some(a) => a.mem.base == abi::ctx_reg() && a.mem.index is None && 0 <= a.mem.disp + && a.mem.disp + a.size <= abi::ctx_size() && (!a.store || abi::exit_code_off() + <= a.mem.disp), + } +} + +/// **A simple instruction**: not a `ret` or a `jcc 2`, writes no reserved +/// register, and touches memory only as [`ctx_only`] says. +pub open spec fn simp(i: x64::Instr) -> bool { + ok2(i) && !clazel_codecheck::spec::writes_reserved(i) && ctx_only(i) +} + +/// A simple instruction is accepted by the validator's single-instruction +/// rule, whatever it knows. +pub proof fn lemma_simp_plain(fs: clazel_codecheck::spec::Facts, i: x64::Instr) + requires + simp(i), + ensures + clazel_codecheck::spec::plain_ok(fs, i) is Some, +{ + if clazel_codecheck::spec::mem_access(i) is Some { + let a = clazel_codecheck::spec::mem_access(i)->Some_0; + assert(clazel_codecheck::spec::access_ok(fs, a)); + } +} + +/// An instruction that is not a jump. +pub open spec fn nojump(i: x64::Instr) -> bool { + clazel_codecheck::spec::jump_rel(i) is None +} + +/// A simple instruction that is not a jump. +pub open spec fn straight(i: x64::Instr) -> bool { + simp(i) && nojump(i) +} + +/// Every instruction of the list is [`straight`]. +pub open spec fn all_straight(c: Seq) -> bool { + forall|k: int| 0 <= k < c.len() ==> straight(#[trigger] c[k]) +} + +/// Straight code followed by straight code is straight. +pub proof fn lemma_straight_cat(a: Seq, b: Seq) + requires + all_straight(a), + all_straight(b), + ensures + all_straight(a + b), +{ + assert forall|k: int| 0 <= k < (a + b).len() implies straight(#[trigger] (a + b)[k]) by { + if k < a.len() { + assert((a + b)[k] == a[k]); + } else { + assert((a + b)[k] == b[k - a.len()]); + } + } +} + +/// A load of a frame slot into a register other than the reserved ones is +/// straight. +pub proof fn lemma_load_slot_straight(r: u8, j: nat) + requires + r < 8, + r != 4, + j < 31, + ensures + straight(load_slot(r, j)), + straight(store_slot(j, r)), +{ + assert(max_slots() == 31) by { + crate::tlemmas::lemma_abi_consts_t(); + } +} + +/// The loads of a template are straight. +pub proof fn lemma_loads_straight(ls: Seq<(u8, nat)>) + requires + forall|q: int| 0 <= q < ls.len() ==> #[trigger] ls[q].0 < 8 && ls[q].0 != 4 && ls[q].1 < 31, + ensures + all_straight(loads_seq(ls)), + decreases ls.len(), +{ + if ls.len() > 0 { + let d = ls.drop_last(); + assert forall|q: int| 0 <= q < d.len() implies #[trigger] d[q].0 < 8 && d[q].0 != 4 + && d[q].1 < 31 by { + assert(d[q] == ls[q]); + } + lemma_loads_straight(d); + let l = ls.last(); + assert(l == ls[ls.len() - 1]); + lemma_load_slot_straight(l.0, l.1); + let one = seq![load_slot(l.0, l.1)]; + assert(all_straight(one)); + lemma_straight_cat(loads_seq(d), one); + } +} + +/// A template of the shape loads, middle, store is straight. +pub proof fn lemma_shape_straight(ls: Seq<(u8, nat)>, mid: Seq, ts: nat) + requires + forall|q: int| 0 <= q < ls.len() ==> #[trigger] ls[q].0 < 8 && ls[q].0 != 4 && ls[q].1 < 31, + all_straight(mid), + ts < 31, + ensures + all_straight(shape(ls, mid, ts)), +{ + lemma_loads_straight(ls); + lemma_load_slot_straight(0, ts); + let st = seq![store_slot(ts, rax())]; + assert(all_straight(st)); + lemma_straight_cat(loads_seq(ls), mid); + lemma_straight_cat(loads_seq(ls) + mid, st); +} + +/// Every instruction of every middle the templates use is straight. +pub proof fn lemma_mids_straight(o: lir::IBin, c: lir::ICmp, sz: x64::Size, v: lir::Val) + ensures + all_straight(seq![int_op_instr(o, sz)]), + all_straight(cmp_mid(c, sz)), + all_straight(eqz_mid(sz)), + all_straight(mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000)), + all_straight(mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF)), + all_straight(exts_mid()), + all_straight(wrap_mid()), + all_straight(select_mid()), + all_straight(seq![const_instr(v)]), +{ + assert forall|k: int| 0 <= k < cmp_mid(c, sz).len() implies straight( + #[trigger] cmp_mid(c, sz)[k], + ) by { + assert(k == 0 || k == 1 || k == 2); + assert(cc_of(c) < 16 && cc_of(c) != 2 || cc_of(c) == 2); + } + assert forall|k: int| 0 <= k < eqz_mid(sz).len() implies straight(#[trigger] eqz_mid(sz)[k]) by { + assert(k == 0 || k == 1 || k == 2); + } + let mx = mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000); + assert forall|k: int| 0 <= k < mx.len() implies straight(#[trigger] mx[k]) by { + assert(k == 0 || k == 1); + } + let ma = mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF); + assert forall|k: int| 0 <= k < ma.len() implies straight(#[trigger] ma[k]) by { + assert(k == 0 || k == 1); + } + assert forall|k: int| 0 <= k < exts_mid().len() implies straight(#[trigger] exts_mid()[k]) by { + assert(k == 0 || k == 1 || k == 2); + } + assert forall|k: int| 0 <= k < select_mid().len() implies straight( + #[trigger] select_mid()[k], + ) by { + assert(k == 0 || k == 1); + } +} + +/// **A template of a straight-line instruction is straight.** +pub proof fn lemma_tmpl_straight(op: Op, nl: nat, h: nat) + requires + op_ok(op, nl, h), + ensures + all_straight(tmpl(op, nl, h)), +{ + assert(max_slots() == 31) by { + crate::tlemmas::lemma_abi_consts_t(); + } + lemma_mids_straight(lir::IBin::Add, lir::ICmp::Eq, x64::Size::W32, lir::Val::I32(0)); + match op { + Op::Nop => {}, + Op::Drop => {}, + Op::Const(v) => { + lemma_mids_straight(lir::IBin::Add, lir::ICmp::Eq, x64::Size::W32, v); + lemma_shape_straight(seq![], seq![const_instr(v)], nl + h); + }, + Op::LocalGet(x) => { + lemma_shape_straight(seq![(rax(), x as nat)], seq![], nl + h); + }, + Op::LocalSet(x) => { + lemma_shape_straight(seq![(rax(), (nl + h - 1) as nat)], seq![], x as nat); + }, + Op::LocalTee(x) => { + lemma_shape_straight(seq![(rax(), (nl + h - 1) as nat)], seq![], x as nat); + }, + Op::Select => { + let t = (nl + h - 3) as nat; + lemma_shape_straight( + seq![(rax(), t), (rcx(), t + 1), (rdx(), t + 2)], + select_mid(), + t, + ); + }, + Op::Bin(b) => { + let t = (nl + h - 2) as nat; + match b { + lir::BinOp::I32(o) => { + lemma_mids_straight(o, lir::ICmp::Eq, x64::Size::W32, lir::Val::I32(0)); + lemma_shape_straight( + seq![(rax(), t), (rcx(), t + 1)], + seq![int_op_instr(o, x64::Size::W32)], + t, + ); + }, + lir::BinOp::I64(o) => { + lemma_mids_straight(o, lir::ICmp::Eq, x64::Size::W64, lir::Val::I32(0)); + lemma_shape_straight( + seq![(rax(), t), (rcx(), t + 1)], + seq![int_op_instr(o, x64::Size::W64)], + t, + ); + }, + lir::BinOp::I32Cmp(c) => { + lemma_mids_straight(lir::IBin::Add, c, x64::Size::W32, lir::Val::I32(0)); + lemma_shape_straight( + seq![(rcx(), t), (rdx(), t + 1)], + cmp_mid(c, x64::Size::W32), + t, + ); + }, + lir::BinOp::I64Cmp(c) => { + lemma_mids_straight(lir::IBin::Add, c, x64::Size::W64, lir::Val::I32(0)); + lemma_shape_straight( + seq![(rcx(), t), (rdx(), t + 1)], + cmp_mid(c, x64::Size::W64), + t, + ); + }, + lir::BinOp::F64(_) => { + assert(false); + }, + lir::BinOp::F64Cmp(_) => { + assert(false); + }, + } + }, + Op::Un(u) => { + let t = (nl + h - 1) as nat; + match u { + lir::UnOp::I32Eqz => { + lemma_shape_straight( + seq![(rcx(), t)], + eqz_mid(x64::Size::W32), + t, + ); + }, + lir::UnOp::I64Eqz => { + lemma_shape_straight( + seq![(rcx(), t)], + eqz_mid(x64::Size::W64), + t, + ); + }, + lir::UnOp::F64Neg => { + lemma_shape_straight( + seq![(rax(), t)], + mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000), + t, + ); + }, + lir::UnOp::F64Abs => { + lemma_shape_straight( + seq![(rax(), t)], + mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF), + t, + ); + }, + lir::UnOp::I64ExtendI32S => { + lemma_shape_straight(seq![(rax(), t)], exts_mid(), t); + }, + lir::UnOp::I32WrapI64 => { + lemma_shape_straight(seq![(rax(), t)], wrap_mid(), t); + }, + lir::UnOp::I64ExtendI32U => {}, + lir::UnOp::F64ReinterpretI64 => {}, + lir::UnOp::I64ReinterpretF64 => {}, + lir::UnOp::F64Sqrt => { + assert(false); + }, + lir::UnOp::F64ConvertI32S => { + assert(false); + }, + lir::UnOp::F64ConvertI32U => { + assert(false); + }, + lir::UnOp::F64ConvertI64S => { + assert(false); + }, + lir::UnOp::F64ConvertI64U => { + assert(false); + }, + } + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfspec.rs b/crates/clazel-jit/src/cfspec.rs new file mode 100644 index 000000000..938632154 --- /dev/null +++ b/crates/clazel-jit/src/cfspec.rs @@ -0,0 +1,533 @@ +//! What the x86-64 back end's **structured control flow and memory +//! instructions** are about (jit.md item J.9, second slice): the source +//! language that has them ([`Node`]), which programs have a translation +//! ([`cf_flow`]), the state relation that includes the region table +//! ([`rel_base`]), and the machine's running of code with jumps ([`steps`], +//! [`reaches`]). Nothing here runs and nothing is proven; the statements are +//! in [`crate::cfcontract`], the templates and proofs come after them. +//! +//! # Source +//! +//! As in [`crate::spec`]: jit.md "J.9 Back ends", `clazel-lir`'s `eval` +//! (the meaning of the source: `block`, `loop`, `if`, `br`, `br_if`, +//! `br_table`, `bounds_check`, `load`, `store`, `exit`), `clazel-x64`'s `step` +//! (the target), and `clazel-codecheck`'s `spec` (the ABI, and the +//! bounds-check sequence [`clazel_codecheck::spec::check_seq`] that every +//! region access must be preceded by in its basic block). +//! +//! # Modelling choices +//! +//! These add to the list in [`crate::spec`] (choices 1 to 7 there still hold, +//! except 7: memory is now touched). +//! +//! 8. **Blocks have no parameters and no results.** `block`, `loop` and `if` +//! are *void*: a branch carries no values, so a branch is a `jmp`, with no +//! moves between frame slots. LIR's typed blocks (jit.md: Wasm's +//! `blocktype`) are not translated: [`Node`] has no block type, and +//! [`View`] gives every block the empty one. The operand stack *below* a +//! block is untouched by it (a branch to a block's label leaves the stack +//! the block was entered with, as LIR's `finish_label` does for arity 0), +//! which is why a branch needs no code beyond the jump. +//! 9. **Heights are static.** Like a straight-line template, a template is a +//! function of the operand-stack height `h` at that point, found by +//! [`cf_flow`], which is the typing LIR's soundness proves: a block's +//! body must leave the height it entered with, or end in a branch or exit +//! (the stack is then unconstrained). Code after such an instruction in the +//! same sequence is dead and refused ([`cf_flow`], `Then`), so no template +//! is chosen for a height that is never reached. +//! 10. **Labels are offsets.** The label `l` of the node being translated is +//! a byte offset `offs[l]` from the node's first byte (negative for a +//! `loop`'s label, past the code for a `block`'s). A `jmp` or `jcc` to it +//! is `rel32` with the displacement from the end of the jump, so the code +//! does not depend on where it is placed. `br_table` is a chain of +//! compare and `je`, since the subset has no indirect branch (jit.md: no +//! indirect branch except the exit sequence). +//! 11. **Bounds checks.** A `load` or `store` is its own bounds-check +//! sequence (the 8 instructions of `check_seq`) and the access, with the +//! check's two exits going to an exit stub after the access, so that the +//! validator sees check and access in one basic block. That check can +//! only fail where LIR's `load` is `Stuck` (typing rules it out), so its +//! exit code is [`trap_code`] and nothing is claimed about it. A +//! `bounds_check` instruction is the same check without the access, with +//! its own exit code, and *is* claimed about: LIR's `Exit`. +//! 12. **The region table is related, not assumed.** [`table_ok`] says the +//! context's table holds LIR's layout (`base` at `16 k`, `bound` at `16 k +//! + 8`). The table is outside the exit area, so no template writes it +//! and [`preserved_mem_r`] carries it. [`rel`] is unchanged. +//! 13. **The fresh stack of a block is a `base`.** LIR runs a block's body +//! on a fresh stack, with the stack below set aside; the machine has all +//! of it in the frame. [`rel_base`]`(lay, base, a, m, nl)` says the +//! machine represents the LIR state `a` *on top of* the values `base`, in +//! slot order: statements about a body are statements about `a`, with the +//! enclosing stacks as `base`. A `Branch` or `Exit` outcome may have been +//! reached in a nested body, so its state is related on top of `base` +//! *and some `extra`* values the nested bodies' bases added. +//! 14. **Running with jumps.** [`steps`] fetches the instruction at `rip` +//! from a list placed at a base address (the bytes are not modelled, as +//! in [`crate::spec`] choice 5), steps it, and goes on. It fails (`None`) +//! at a `rip` that is not an instruction boundary of the list, so a +//! claim that code `reaches` a state includes that the code never leaves +//! the list except by the step that ends the run. [`reaches`] is +//! existential in the number of steps: LIR's evaluation is fuelled, the +//! machine's is not. +//! 15. **Regions are not code, not the stack.** A region store changes bytes +//! outside the exit area, so the statements use [`preserved_mem_r`]: +//! everything outside the exit area *and every region* is as it was. +//! That the code's bytes and the native stack are outside every region +//! is a precondition ([`stack_ok_r`] for the stack; the placement of the +//! code is the caller's, as before). +//! +//! # What is not here +//! +//! Typed blocks (choice 8), `f64` arithmetic, rotates, frames beyond 31 +//! slots, a `br_table` of more than [`max_targets`] targets, and the A64 back +//! end. + +use crate::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// ============================================================================ +// Section: the source subset with control flow +// ============================================================================ + +/// `rsi`, a fourth scratch register: a region's base, in an access. +pub open spec fn rsi() -> u8 { + 6 +} + +/// `rdi`, a fifth scratch register: the value a `store` writes. +pub open spec fn rdi() -> u8 { + 7 +} + +/// The exit code of a bounds check inside a `load` or `store` (choice 11). +pub open spec fn trap_code() -> u32 { + 0xFFFF_FFFF +} + +/// The value types of a `load` or `store`: the ones with a size. +pub enum MTy { + /// `i32`: 4 bytes. + I32, + /// `i64`: 8 bytes. + I64, + /// `f64`: 8 bytes (its bits). + F64, +} + +impl MTy { + /// The LIR type. + pub open spec fn lir_ty(self) -> lir::Ty { + match self { + MTy::I32 => lir::Ty::I32, + MTy::I64 => lir::Ty::I64, + MTy::F64 => lir::Ty::F64, + } + } + + /// The access size in bytes. + pub open spec fn size(self) -> nat { + match self { + MTy::I32 => 4, + MTy::I64 => 8, + MTy::F64 => 8, + } + } +} + +/// The memory instructions of LIR. +pub enum MemOp { + /// `bounds_check region size code`: the index on top of the stack; exit + /// with `code` unless `index + size <= bound(region)`. + BoundsCheck { + /// The region (0 to 15). + region: usize, + /// The number of bytes the access that follows touches. + size: usize, + /// The exit code if the check fails. + code: u32, + }, + /// `t.load region`: pop the index, push the value in the region. + Load { + /// The region (0 to 15). + region: usize, + /// The type loaded. + ty: MTy, + }, + /// `t.store region`: pop the value, pop the index, write the value. + Store { + /// The region (0 to 15). + region: usize, + /// The type stored. + ty: MTy, + }, +} + +impl vstd::view::View for MemOp { + type V = lir::Instr; + + open spec fn view(&self) -> lir::Instr { + match *self { + MemOp::BoundsCheck { region, size, code } => lir::Instr::BoundsCheck { + region: region as nat, + size: size as nat, + code: code as nat, + }, + MemOp::Load { region, ty } => lir::Instr::Load { + region: region as nat, + ty: ty.lir_ty(), + }, + MemOp::Store { region, ty } => lir::Instr::Store { + region: region as nat, + ty: ty.lir_ty(), + }, + } + } +} + +/// A LIR instruction with structured control flow, in a form executable code +/// can build (choice 8: every block is void). [`View`] is the LIR instruction +/// it stands for. +pub enum Node { + /// A straight-line instruction ([`Op`]). + Op(Op), + /// A memory instruction. + Mem(MemOp), + /// The first, then the second, unless the first branched or exited. + Then(Box, Box), + /// `block` with no parameters and no results. + Block(Box), + /// `loop` with no parameters and no results. + Loop(Box), + /// `if` with no parameters and no results: the condition is popped from + /// the enclosing body's stack. + If(Box, Box), + /// `br l`. + Br(usize), + /// `br_if l`. + BrIf(usize), + /// `br_table ls d`. + BrTable(Vec, usize), + /// `exit code` with `k` operands (the top `k` values of the stack). + Exit(u32, usize), +} + +/// The empty block type: no parameters, no results. +pub open spec fn void_bt() -> lir::BlockType { + lir::BlockType { params: seq![], results: seq![] } +} + +/// LIR's `exit c` with `k` operands (their types are not used by `eval`). +pub open spec fn exit_instr(c: nat, k: nat) -> lir::Instr { + lir::Instr::Exit(c, Seq::new(k, |i: int| lir::Ty::I64)) +} + +impl vstd::view::View for Node { + type V = lir::Instr; + + open spec fn view(&self) -> lir::Instr + decreases *self, + { + match *self { + Node::Op(o) => o@, + Node::Mem(m) => m@, + Node::Then(a, b) => lir::Instr::Then(Box::new((*a)@), Box::new((*b)@)), + Node::Block(b) => lir::Instr::Block(void_bt(), Box::new((*b)@)), + Node::Loop(b) => lir::Instr::Loop(void_bt(), Box::new((*b)@)), + Node::If(t, e) => lir::Instr::If( + void_bt(), + Box::new((*t)@), + Box::new((*e)@), + ), + Node::Br(l) => lir::Instr::Br(l as nat), + Node::BrIf(l) => lir::Instr::BrIf(l as nat), + Node::BrTable(ls, d) => lir::Instr::BrTable( + ls@.map_values(|x: usize| x as nat), + d as nat, + ), + Node::Exit(c, k) => exit_instr(c as nat, k as nat), + } + } +} + +// ============================================================================ +// Section: which programs have a translation +// ============================================================================ + +/// What the static analysis says about the end of an instruction. +pub enum Flow { + /// It can fall through, leaving the operand stack at this height. + Falls(nat), + /// It always branches or exits: nothing follows in its sequence. + Dead, +} + +/// The analysis of `n`, entered at stack height `h` with `nl` locals and +/// `depth` labels in scope (the outermost is the function's return): `None` +/// if it has no translation. This is LIR's typing for the void subset, plus +/// the frame limit. +/// +/// - A straight-line instruction: [`op_ok`]. +/// - A memory instruction: the frame holds the stack, its operands exist, the +/// region is in the table (16 regions), and a `bounds_check` size is 1 to 8. +/// - `then`: the second runs at the height the first leaves. After a first +/// that is [`Flow::Dead`] only `nop` may follow (choice 9). +/// - `block`, `loop`: the body, one label deeper, leaves the height it +/// entered with or is dead. +/// - `if`: needs the condition; each arm is a body at the height below it. +/// - `br l`, `br_if l`, `br_table`: every label is in scope; `br_if` and +/// `br_table` need their condition, and a `br_table` has at most +/// [`max_targets`] targets. +/// - `exit` needs its `k` operands and a code below `2^32` (a `u32`). +pub open spec fn cf_flow(n: Node, nl: nat, h: nat, depth: nat) -> Option + decreases n, +{ + match n { + Node::Op(o) => if op_ok(o, nl, h) { + Some(Flow::Falls(height_after_op(o, h))) + } else { + None + }, + Node::Mem(m) => match m { + MemOp::BoundsCheck { region, size, code } => if nl + h <= max_slots() && 1 <= h + && region < 16 && 1 <= size <= 8 { + Some(Flow::Falls(h)) + } else { + None + }, + MemOp::Load { region, ty } => if nl + h <= max_slots() && 1 <= h && region < 16 { + Some(Flow::Falls(h)) + } else { + None + }, + MemOp::Store { region, ty } => if nl + h <= max_slots() && 2 <= h && region < 16 { + Some(Flow::Falls((h - 2) as nat)) + } else { + None + }, + }, + Node::Then(a, b) => match cf_flow(*a, nl, h, depth) { + None => None, + Some(Flow::Falls(h1)) => cf_flow(*b, nl, h1, depth), + Some(Flow::Dead) => match *b { + Node::Op(Op::Nop) => Some(Flow::Dead), + _ => None, + }, + }, + Node::Block(b) => match cf_flow(*b, nl, h, depth + 1) { + Some(Flow::Falls(h1)) => if h1 == h { + Some(Flow::Falls(h)) + } else { + None + }, + Some(Flow::Dead) => Some(Flow::Falls(h)), + None => None, + }, + Node::Loop(b) => match cf_flow(*b, nl, h, depth + 1) { + Some(Flow::Falls(h1)) => if h1 == h { + Some(Flow::Falls(h)) + } else { + None + }, + Some(Flow::Dead) => Some(Flow::Falls(h)), + None => None, + }, + Node::If(t, e) => if 1 <= h && nl + h <= max_slots() { + match (cf_flow(*t, nl, (h - 1) as nat, depth + 1), cf_flow(*e, nl, (h - 1) as nat, depth + 1)) { + (Some(ft), Some(fe)) => if flow_fits(ft, (h - 1) as nat) && flow_fits( + fe, + (h - 1) as nat, + ) { + Some(Flow::Falls((h - 1) as nat)) + } else { + None + }, + _ => None, + } + } else { + None + }, + Node::Br(l) => if l < depth { + Some(Flow::Dead) + } else { + None + }, + Node::BrIf(l) => if l < depth && 1 <= h && nl + h <= max_slots() { + Some(Flow::Falls((h - 1) as nat)) + } else { + None + }, + Node::BrTable(ls, d) => if d < depth && 1 <= h && nl + h <= max_slots() && ls@.len() + <= max_targets() && (forall|i: int| 0 <= i < ls@.len() ==> #[trigger] ls@[i] < depth) { + Some(Flow::Dead) + } else { + None + }, + Node::Exit(c, k) => if k <= h && nl + h <= max_slots() { + Some(Flow::Dead) + } else { + None + }, + } +} + +/// A block's body ends as it must: dead, or at height `h`. +pub open spec fn flow_fits(f: Flow, h: nat) -> bool { + match f { + Flow::Falls(h1) => h1 == h, + Flow::Dead => true, + } +} + +/// The most targets (not counting the default) of a `br_table` that has a +/// translation: each is a compare and a `je` in the chain (choice 10), and a +/// target's index is a 32-bit immediate. A larger table is refused, as a +/// frame beyond 31 slots is. +pub open spec fn max_targets() -> nat { + 0x10000 +} + +/// The node has a template in this slice: every node of [`Node`] has one +/// (the memory instructions `bounds_check`, `load`, `store` and `br_table` +/// included). +pub open spec fn templated(n: Node) -> bool + decreases n, +{ + match n { + Node::Op(_) => true, + Node::Mem(_) => true, + Node::Then(a, b) => templated(*a) && templated(*b), + Node::Block(b) => templated(*b), + Node::Loop(b) => templated(*b), + Node::If(t, e) => templated(*t) && templated(*e), + Node::Br(_) => true, + Node::BrIf(_) => true, + Node::BrTable(_, _) => true, + Node::Exit(_, _) => true, + } +} + +// ============================================================================ +// Section: the state relation with a base, and the region table +// ============================================================================ + +/// The context's region table holds LIR's layout: at most 16 regions, and for +/// each, `base` at `r15 + 16 k` and `bound` at `r15 + 16 k + 8`, as 64-bit +/// words (choice 12). No region overlaps the table, so a store to a region +/// never changes it (the exit area is already clear: [`rel`]). +pub open spec fn table_ok(lay: lir::Layout, m: x64::State) -> bool { + lay.len() <= abi::num_regions() && (forall|k: int| + 0 <= k < lay.len() ==> { + &&& x64::read_mem(m.mem, ctx_of(m) + abi::base_off(k), 8) == Some( + (#[trigger] lay[k]).base as int, + ) + &&& x64::read_mem(m.mem, ctx_of(m) + abi::bound_off(k), 8) == Some(lay[k].bound as int) + }) && forall|b: int| + ctx_of(m) <= b < ctx_of(m) + abi::table_end() ==> !#[trigger] in_regions_i(lay, b) +} + +/// The machine `m` represents the LIR state `a` on top of the values `base` +/// (choice 13), and the region table is as LIR's layout. +#[verifier::opaque] +pub open spec fn rel_base( + lay: lir::Layout, + base: Seq, + a: lir::St, + m: x64::State, + nl: nat, +) -> bool { + rel(lay, a.with_stack(base + a.stack), m, nl) && table_ok(lay, m) +} + +/// `a` is a byte of some region, `a` an `int`. +pub open spec fn in_regions_i(lay: lir::Layout, a: int) -> bool { + 0 <= a && in_regions(lay, a as nat) +} + +/// What a template leaves alone in memory, now that a store may write a +/// region: the context register keeps its value, the mapped addresses are the +/// same, and every byte outside the exit area **and outside every region** +/// keeps its value (choice 15). +pub open spec fn preserved_mem_r(lay: lir::Layout, m: x64::State, m2: x64::State) -> bool { + m.rgf.len() == 16 && m2.rgf.len() == 16 && m2.rgf[abi::ctx_reg() as int] + == m.rgf[abi::ctx_reg() as int] && m2.mem.dom() == m.mem.dom() && (forall|a: int| + #[trigger] m.mem.dom().contains(a) && !in_exit_area(ctx_of(m), a) && !in_regions_i(lay, a) + ==> m2.mem[a] == m.mem[a]) +} + +/// [`preserved_mem_r`], and the stack pointer keeps its value too. +pub open spec fn preserved_r(lay: lir::Layout, m: x64::State, m2: x64::State) -> bool { + preserved_mem_r(lay, m, m2) && m2.rgf[abi::sp_reg() as int] == m.rgf[abi::sp_reg() as int] +} + +/// [`stack_ok`], and the stack word the `ret` of an exit sequence reads is in +/// no region (so no store to a region changes it). +pub open spec fn stack_ok_r(lay: lir::Layout, m: x64::State) -> bool { + stack_ok(m) && forall|b: int| + x64::logext(64, m.rgf[abi::sp_reg() as int] as int) <= b < x64::logext( + 64, + m.rgf[abi::sp_reg() as int] as int, + ) + 8 ==> !#[trigger] in_regions_i(lay, b) +} + +// ============================================================================ +// Section: running code with jumps +// ============================================================================ + +/// The instruction of `code` that begins `off` bytes after the first byte: +/// `None` if no instruction begins there (past the end, or inside one). +pub open spec fn fetch(code: Seq, off: int) -> Option + decreases code.len(), +{ + if code.len() == 0 || off < 0 { + None + } else if off == 0 { + Some(code[0]) + } else if off < x64::instr_len(code[0]) as int { + None + } else { + fetch(code.skip(1), off - x64::instr_len(code[0]) as int) + } +} + +/// `n` steps of the machine from `s`, the code `code` being placed at address +/// `base` (choice 14): each fetches the instruction at `rip` and steps it. A +/// fault, or a `rip` with no instruction of `code`, is `None`. +pub open spec fn steps( + cu: spec_fn(nat) -> nat, + code: Seq, + base: int, + s: x64::State, + n: nat, +) -> Option + decreases n, +{ + if n == 0 { + Some(s) + } else { + match fetch(code, s.rip - base) { + Some(i) => match x64::step(cu, s, i) { + Some(s1) => steps(cu, code, base, s1, (n - 1) as nat), + None => None, + }, + None => None, + } + } +} + +/// Running `code` (placed at `base`) from `s` reaches `s2` after some number +/// of steps, none of which faults. +pub open spec fn reaches( + cu: spec_fn(nat) -> nat, + code: Seq, + base: int, + s: x64::State, + s2: x64::State, +) -> bool { + exists|n: nat| steps(cu, code, base, s, n) == Some(s2) +} + +} // verus! diff --git a/crates/clazel-jit/src/cfsteps.rs b/crates/clazel-jit/src/cfsteps.rs new file mode 100644 index 000000000..4b67b95c6 --- /dev/null +++ b/crates/clazel-jit/src/cfsteps.rs @@ -0,0 +1,444 @@ +//! Facts about [`fetch`], [`steps`] and [`reaches`]: a run in a piece of code +//! is a run in any list that contains it, runs compose, and a run of +//! jump-free code by [`run_seq`] is a run by [`steps`]. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `code` has no instruction that changes `rip` other than by its length: +/// no jump, no `ret`. +pub open spec fn no_jumps(code: Seq) -> bool { + forall|j: int| + 0 <= j < code.len() ==> !(#[trigger] code[j] is Jcc || code[j] is Jmp || code[j] is Ret) +} + +/// A step of an instruction that is not a jump or `ret` advances `rip` by +/// the instruction's length. +pub proof fn lemma_step_rip_plain( + cu: spec_fn(nat) -> nat, + s: x64::State, + i: x64::Instr, + s1: x64::State, +) + requires + x64::step(cu, s, i) == Some(s1), + !(i is Jcc || i is Jmp || i is Ret), + ensures + s1.rip == s.rip + x64::instr_len(i), +{ +} + +/// `fetch` in a list sees the same instruction in the list followed by more. +pub proof fn lemma_fetch_append_l(a: Seq, b: Seq, off: int) + requires + fetch(a, off) is Some, + ensures + fetch(a + b, off) == fetch(a, off), + decreases a.len(), +{ + if a.len() > 0 && off > 0 && off >= x64::instr_len(a[0]) as int { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + lemma_fetch_append_l(a.skip(1), b, off - x64::instr_len(a[0]) as int); + } +} + +/// `fetch` at or past the end of `a` is `fetch` in `b`. +pub proof fn lemma_fetch_append_r(a: Seq, b: Seq, off: int) + requires + off >= code_len(a), + ensures + fetch(a + b, off) == fetch(b, off - code_len(a)), + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + let l0 = x64::instr_len(a[0]) as int; + assert(code_len(a) == l0 + code_len(a.skip(1))); + assert(x64::instr_len(a[0]) > 0) by { + lemma_instr_len_pos(a[0]); + } + lemma_fetch_append_r(a.skip(1), b, off - l0); + } +} + +/// An instruction is at least one byte. +#[verifier::rlimit(50)] +pub proof fn lemma_instr_len_pos(i: x64::Instr) + ensures + x64::instr_len(i) > 0, +{ + assert(x64::encode(i).len() > 0); +} + + +/// [`steps`] with the machine's step function abstracted, so that the +/// lemmas below, which are about fetching and counting, do not make the +/// solver unfold `x64::step` on an instruction it knows nothing about (the +/// device of [`crate::runseq::run_gen`]). +pub open spec fn steps_g( + f: spec_fn(x64::State, x64::Instr) -> Option, + code: Seq, + base: int, + s: x64::State, + n: nat, +) -> Option + decreases n, +{ + if n == 0 { + Some(s) + } else { + match fetch(code, s.rip - base) { + Some(i) => match f(s, i) { + Some(s1) => steps_g(f, code, base, s1, (n - 1) as nat), + None => None, + }, + None => None, + } + } +} + +/// [`steps`] is [`steps_g`] of the machine's step. +pub proof fn lemma_steps_is_gen( + cu: spec_fn(nat) -> nat, + code: Seq, + base: int, + s: x64::State, + n: nat, +) + ensures + steps(cu, code, base, s, n) == steps_g(super::runseq::stepper(cu), code, base, s, n), + decreases n, +{ + if n > 0 { + match fetch(code, s.rip - base) { + Some(i) => match x64::step(cu, s, i) { + Some(s1) => { + lemma_steps_is_gen(cu, code, base, s1, (n - 1) as nat); + }, + None => {}, + }, + None => {}, + } + } +} + +/// Generic [`lemma_steps_trans`]. +pub proof fn lemma_steps_g_trans( + f: spec_fn(x64::State, x64::Instr) -> Option, + code: Seq, + base: int, + s: x64::State, + s1: x64::State, + s2: x64::State, + n1: nat, + n2: nat, +) + requires + steps_g(f, code, base, s, n1) == Some(s1), + steps_g(f, code, base, s1, n2) == Some(s2), + ensures + steps_g(f, code, base, s, n1 + n2) == Some(s2), + decreases n1, +{ + if n1 > 0 { + match fetch(code, s.rip - base) { + Some(i) => match f(s, i) { + Some(t) => { + lemma_steps_g_trans(f, code, base, t, s1, s2, (n1 - 1) as nat, n2); + }, + None => {}, + }, + None => {}, + } + } +} + +/// Runs compose: `n1` steps, then `n2` more. +pub proof fn lemma_steps_trans( + cu: spec_fn(nat) -> nat, + code: Seq, + base: int, + s: x64::State, + s1: x64::State, + s2: x64::State, + n1: nat, + n2: nat, +) + requires + steps(cu, code, base, s, n1) == Some(s1), + steps(cu, code, base, s1, n2) == Some(s2), + ensures + steps(cu, code, base, s, n1 + n2) == Some(s2), +{ + let f = super::runseq::stepper(cu); + lemma_steps_is_gen(cu, code, base, s, n1); + lemma_steps_is_gen(cu, code, base, s1, n2); + lemma_steps_is_gen(cu, code, base, s, n1 + n2); + lemma_steps_g_trans(f, code, base, s, s1, s2, n1, n2); +} + +/// [`reaches`] is transitive. +pub proof fn lemma_reaches_trans( + cu: spec_fn(nat) -> nat, + code: Seq, + base: int, + s: x64::State, + s1: x64::State, + s2: x64::State, +) + requires + reaches(cu, code, base, s, s1), + reaches(cu, code, base, s1, s2), + ensures + reaches(cu, code, base, s, s2), +{ + let n1 = choose|n: nat| steps(cu, code, base, s, n) == Some(s1); + let n2 = choose|n: nat| steps(cu, code, base, s1, n) == Some(s2); + lemma_steps_trans(cu, code, base, s, s1, s2, n1, n2); +} + +/// Generic [`lemma_steps_embed`]. +pub proof fn lemma_steps_g_embed( + f: spec_fn(x64::State, x64::Instr) -> Option, + pre: Seq, + c: Seq, + post: Seq, + base: int, + s: x64::State, + s2: x64::State, + n: nat, +) + requires + steps_g(f, c, base, s, n) == Some(s2), + ensures + steps_g(f, pre + c + post, base - code_len(pre), s, n) == Some(s2), + decreases n, +{ + if n > 0 { + let off = s.rip - base; + match fetch(c, off) { + Some(i) => { + lemma_fetch_append_l(c, post, off); + assert(off >= 0); + lemma_fetch_append_r(pre, c + post, off + code_len(pre)); + assert(pre + c + post =~= pre + (c + post)); + assert(off + code_len(pre) - code_len(pre) == off); + assert(s.rip - (base - code_len(pre)) == off + code_len(pre)); + match f(s, i) { + Some(t) => { + lemma_steps_g_embed(f, pre, c, post, base, t, s2, (n - 1) as nat); + }, + None => {}, + } + }, + None => {}, + } + } +} + +/// **A run in a piece is a run in the whole.** If `c` placed at `base` runs +/// from `s` to `s2` in `n` steps, then `pre + c + post`, placed so that `c` +/// is where it was, does too. +pub proof fn lemma_steps_embed( + cu: spec_fn(nat) -> nat, + pre: Seq, + c: Seq, + post: Seq, + base: int, + s: x64::State, + s2: x64::State, + n: nat, +) + requires + steps(cu, c, base, s, n) == Some(s2), + ensures + steps(cu, pre + c + post, base - code_len(pre), s, n) == Some(s2), +{ + let f = super::runseq::stepper(cu); + lemma_steps_is_gen(cu, c, base, s, n); + lemma_steps_is_gen(cu, pre + c + post, base - code_len(pre), s, n); + lemma_steps_g_embed(f, pre, c, post, base, s, s2, n); +} + +/// [`reaches`] in a piece is [`reaches`] in the whole. +pub proof fn lemma_reaches_embed( + cu: spec_fn(nat) -> nat, + pre: Seq, + c: Seq, + post: Seq, + base: int, + s: x64::State, + s2: x64::State, +) + requires + reaches(cu, c, base, s, s2), + ensures + reaches(cu, pre + c + post, base - code_len(pre), s, s2), +{ + let n = choose|n: nat| steps(cu, c, base, s, n) == Some(s2); + lemma_steps_embed(cu, pre, c, post, base, s, s2, n); +} + +/// The step function advances `rip` by the instruction's length for every +/// instruction that is not a jump or `ret`. +pub open spec fn rip_plain(f: spec_fn(x64::State, x64::Instr) -> Option) -> bool { + forall|s: x64::State, i: x64::Instr| + !(i is Jcc || i is Jmp || i is Ret) ==> match #[trigger] f(s, i) { + Some(s1) => s1.rip == s.rip + x64::instr_len(i), + None => true, + } +} + +/// The machine's step is [`rip_plain`]. +pub proof fn lemma_stepper_rip_plain(cu: spec_fn(nat) -> nat) + ensures + rip_plain(super::runseq::stepper(cu)), +{ + let f = super::runseq::stepper(cu); + assert forall|s: x64::State, i: x64::Instr| + !(i is Jcc || i is Jmp || i is Ret) implies match #[trigger] f(s, i) { + Some(s1) => s1.rip == s.rip + x64::instr_len(i), + None => true, + } by { + match x64::step(cu, s, i) { + Some(s1) => { + lemma_step_rip_plain(cu, s, i, s1); + }, + None => {}, + } + } +} + +/// Generic [`lemma_run_seq_steps`]. +pub proof fn lemma_run_gen_steps( + f: spec_fn(x64::State, x64::Instr) -> Option, + pre: Seq, + code: Seq, + post: Seq, + b: int, + s: x64::State, + s2: x64::State, +) + requires + rip_plain(f), + no_jumps(code), + s.rip == b + code_len(pre), + super::runseq::run_gen(f, s, code) == Some(s2), + ensures + steps_g(f, pre + code + post, b, s, code.len()) == Some(s2), + s2.rip == s.rip + code_len(code), + decreases code.len(), +{ + if code.len() == 0 { + } else { + let whole = pre + code + post; + let i = code[0]; + assert(!(i is Jcc || i is Jmp || i is Ret)); + lemma_instr_len_pos(i); + let off: int = code_len(pre) as int; + assert(whole =~= pre + (code + post)); + lemma_fetch_append_r(pre, code + post, off); + assert(code + post == seq![i] + (code.skip(1) + post)) by { + assert((code + post).skip(1) =~= code.skip(1) + post); + } + assert(fetch(code + post, 0) == Some(i)); + assert(s.rip - b == off); + match f(s, i) { + Some(s1) => { + assert(s1.rip == s.rip + x64::instr_len(i)); + let pre1 = pre + seq![i]; + let rest = code.skip(1); + assert(code_len(pre1) == code_len(pre) + x64::instr_len(i)) by { + super::runseq::lemma_code_len_append(pre, seq![i]); + super::runseq::lemma_code_len_one(i); + } + assert(pre1 + rest + post =~= whole); + assert(no_jumps(rest)) by { + assert forall|j: int| 0 <= j < rest.len() implies !(#[trigger] rest[j] is Jcc + || rest[j] is Jmp || rest[j] is Ret) by { + assert(rest[j] == code[j + 1]); + } + } + lemma_run_gen_steps(f, pre1, rest, post, b, s1, s2); + assert(code_len(code) == x64::instr_len(i) + code_len(rest)); + }, + None => {}, + } + } +} + +/// **Jump-free code run by [`run_seq`] is run by [`steps`].** `whole` is +/// `pre + code + post` placed at `b`; the state is at the first byte of +/// `code`; the run of `code` by [`run_seq`] reaches `s2`. +pub proof fn lemma_run_seq_steps( + cu: spec_fn(nat) -> nat, + pre: Seq, + code: Seq, + post: Seq, + b: int, + s: x64::State, + s2: x64::State, +) + requires + no_jumps(code), + s.rip == b + code_len(pre), + run_seq(cu, s, code) == Some(s2), + ensures + steps(cu, pre + code + post, b, s, code.len()) == Some(s2), + s2.rip == s.rip + code_len(code), +{ + let f = super::runseq::stepper(cu); + lemma_stepper_rip_plain(cu); + super::runseq::lemma_run_seq_is_gen(cu, s, code); + lemma_run_gen_steps(f, pre, code, post, b, s, s2); + lemma_steps_is_gen(cu, pre + code + post, b, s, code.len()); +} + +/// [`reaches`] in code followed by more. +pub proof fn lemma_reaches_post( + cu: spec_fn(nat) -> nat, + c: Seq, + post: Seq, + base: int, + s: x64::State, + s2: x64::State, +) + requires + reaches(cu, c, base, s, s2), + ensures + reaches(cu, c + post, base, s, s2), +{ + assert(Seq::::empty() + c + post =~= c + post); + assert(code_len(Seq::::empty()) == 0); + lemma_reaches_embed(cu, Seq::empty(), c, post, base, s, s2); +} + +/// [`reaches`] in code preceded by more (the base moves back). +pub proof fn lemma_reaches_pre( + cu: spec_fn(nat) -> nat, + pre: Seq, + c: Seq, + base: int, + s: x64::State, + s2: x64::State, +) + requires + reaches(cu, c, base, s, s2), + ensures + reaches(cu, pre + c, base - code_len(pre), s, s2), +{ + assert(pre + c + Seq::::empty() =~= pre + c); + lemma_reaches_embed(cu, pre, c, Seq::empty(), base, s, s2); +} + +} // verus! diff --git a/crates/clazel-jit/src/cftemplates.rs b/crates/clazel-jit/src/cftemplates.rs new file mode 100644 index 000000000..ae93f0e56 --- /dev/null +++ b/crates/clazel-jit/src/cftemplates.rs @@ -0,0 +1,342 @@ +//! The templates for structured control flow (jit.md J.9, second slice), as +//! spec functions: [`ctmpl`] is the x86-64 code of a [`Node`], given the +//! offsets of the labels in scope; [`csize`] is its length in bytes, which is +//! what a `block`'s label offset is made from. The refinement proofs are in +//! [`crate::cfproofs`]; the statement is [`crate::cfcontract::cf_refines`]. +//! +//! The layout (jit.md J.9: "composition over structured control flow", with +//! the labels resolved to code offsets): +//! +//! - `then a b`: the code of `a`, then the code of `b`; `b`'s labels are `a`'s +//! shifted by the length of `a`'s code. +//! - `block b`: the code of `b`, with a new label 0 at the end of it. +//! - `loop b`: the code of `b`, with a new label 0 at its start. +//! - `if t e`: `mov rax, [cond]; test eax, eax; je Lelse; t; jmp Lend; Lelse: +//! e; Lend:`, with a new label 0 at `Lend` in each arm. +//! - `br l`: `jmp` to label `l`. `br_if l`: `mov rax, [cond]; test eax, eax; +//! jne` to label `l`. +//! - `exit c k`: the exit sequence; the operands stay in their frame slots. +//! +//! A `jmp` or `jcc` has a 32-bit displacement from the end of the +//! instruction, so every label offset is `target - end of this jump`. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfproofs::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `jmp` to the byte `rel` after the end of the instruction. +pub open spec fn jmp_i(rel: int) -> x64::Instr { + x64::Instr::Jmp { rel: rel as i32 } +} + +/// `jcc cc` to the byte `rel` after the end of the instruction. +pub open spec fn jcc_i(cc: u8, rel: int) -> x64::Instr { + x64::Instr::Jcc { cc: cc, rel: rel as i32 } +} + +/// The offsets [`csize`] is defined with: `depth` labels, all at 0 (the length +/// of the code does not depend on them: [`crate::cflens::lemma_ctmpl_len`]). +pub open spec fn zeros(depth: nat) -> Seq { + Seq::new(depth, |i: int| 0int) +} + +/// The labels of `offs`, as seen from `d` bytes further on. +pub open spec fn shift(offs: Seq, d: int) -> Seq { + offs.map_values(|o: int| o - d) +} + +/// The condition test: `mov rax, [top of stack]; test eax, eax; jcc cc rel`, +/// at height `h` with `nl` locals. +pub open spec fn cond_pre(nl: nat, h: nat, cc: u8, rel: int) -> Seq { + seq![ + load_slot(rax(), (nl + h - 1) as nat), + x64::Instr::TestRR { sz: x64::Size::W32, a: rax(), b: rax() }, + jcc_i(cc, rel), + ] +} + +/// The length of [`cond_pre`] (it does not depend on `rel` or `cc`). +pub open spec fn cond_len(nl: nat, h: nat) -> nat { + code_len(cond_pre(nl, h, 0, 0)) +} + +/// The height after a node, when it falls through (else the same height). +pub open spec fn flow_h(n: Node, nl: nat, h: nat, depth: nat) -> nat { + match cf_flow(n, nl, h, depth) { + Some(Flow::Falls(h1)) => h1, + _ => h, + } +} + +/// **The code of a node** at height `h` with `nl` locals, the labels in scope +/// at the byte offsets `offs` from its first byte. Empty for the nodes this +/// slice has no template for ([`cf_flow`] refuses them). +pub open spec fn ctmpl(n: Node, nl: nat, h: nat, offs: Seq) -> Seq + decreases n, 0nat, +{ + match n { + Node::Op(o) => tmpl(o, nl, h), + Node::Mem(m) => mem_code(m, nl, h), + Node::Then(a, b) => ctmpl(*a, nl, h, offs) + ctmpl( + *b, + nl, + flow_h(*a, nl, h, offs.len()), + shift(offs, csize(*a, nl, h, offs.len()) as int), + ), + Node::Block(b) => ctmpl(*b, nl, h, seq![csize(*b, nl, h, offs.len() + 1) as int] + offs), + Node::Loop(b) => ctmpl(*b, nl, h, seq![0int] + offs), + Node::If(t, e) => { + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, offs.len() + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, offs.len() + 1) as int; + cond_pre(nl, h, 4, st + 5) + ctmpl( + *t, + nl, + (h - 1) as nat, + seq![st + 5 + se] + shift(offs, p), + ) + seq![jmp_i(se)] + ctmpl( + *e, + nl, + (h - 1) as nat, + seq![se] + shift(offs, p + st + 5), + ) + }, + Node::Br(l) => seq![jmp_i(if l < offs.len() { + offs[l as int] - 5 + } else { + 0 + })], + Node::BrIf(l) => cond_pre(nl, h, 5, if l < offs.len() { + offs[l as int] - cond_len(nl, h) as int + } else { + 0 + }), + Node::BrTable(ls, d) => brt_code(ls@, d, nl, h, offs), + Node::Exit(c, _) => exit_seq(c), + } +} + +// ---------------------------------------------------------------------------- +// Memory instructions +// ---------------------------------------------------------------------------- + +/// The operand size of an access of type `ty`: `i32` 4 bytes, the others 8. +pub open spec fn mty_sz(ty: MTy) -> x64::Size { + match ty { + MTy::I32 => x64::Size::W32, + _ => x64::Size::W64, + } +} + +/// `[rsi + rcx]`: a region base plus the checked index (scale 1). +pub open spec fn ix_mem() -> x64::Mem { + x64::Mem { base: rsi(), index: Some(x64::Index { reg: rcx(), scale: 0 }), disp: 0 } +} + +/// `[r15 + 16 k]`: region `k`'s base, in the context's table. +pub open spec fn base_mem(k: nat) -> x64::Mem { + x64::Mem { base: abi::ctx_reg(), index: None, disp: abi::base_off(k as int) as i32 } +} + +/// The bounds check of the validator (`clazel_codecheck::spec::check_seq`) +/// on `rcx`, with `rdx` and `rax` as its scratch registers, region `k`, `n` +/// bytes, and the displacements `r1`, `r2` of its two exits. +pub open spec fn mem_chk(k: nat, n: nat, r1: int, r2: int) -> clazel_codecheck::spec::Chk { + clazel_codecheck::spec::Chk { + k: k as int, + n: n as int, + i: rcx(), + t: rdx(), + z: rax(), + r1: r1 as i32, + r2: r2 as i32, + } +} + +/// The region of a memory instruction. +pub open spec fn mem_region(m: MemOp) -> nat { + match m { + MemOp::BoundsCheck { region, .. } => region as nat, + MemOp::Load { region, .. } => region as nat, + MemOp::Store { region, .. } => region as nat, + } +} + +/// The number of bytes the check of a memory instruction admits. +pub open spec fn mem_n(m: MemOp) -> nat { + match m { + MemOp::BoundsCheck { size, .. } => size as nat, + MemOp::Load { ty, .. } => ty.size(), + MemOp::Store { ty, .. } => ty.size(), + } +} + +/// The exit code of the stub after a memory instruction. +pub open spec fn mem_exit_code(m: MemOp) -> u32 { + match m { + MemOp::BoundsCheck { code, .. } => code, + _ => trap_code(), + } +} + +/// What a memory instruction does before its check: the operands into +/// registers (the index in `rcx`, the value of a store in `rdi`), and, for an +/// access, the region's base into `rsi`. +pub open spec fn mem_pre(m: MemOp, nl: nat, h: nat) -> Seq { + match m { + MemOp::BoundsCheck { .. } => seq![load_slot(rcx(), (nl + h - 1) as nat)], + MemOp::Load { region, .. } => seq![ + load_slot(rcx(), (nl + h - 1) as nat), + x64::Instr::MovLoad { sz: x64::Size::W64, dst: rsi(), mem: base_mem(region as nat) }, + ], + MemOp::Store { region, .. } => seq![ + load_slot(rdi(), (nl + h - 1) as nat), + load_slot(rcx(), (nl + h - 2) as nat), + x64::Instr::MovLoad { sz: x64::Size::W64, dst: rsi(), mem: base_mem(region as nat) }, + ], + } +} + +/// What a memory instruction does after its check passed: the access, and for +/// a load the store of the result to the slot of the index. +pub open spec fn mem_tail(m: MemOp, nl: nat, h: nat) -> Seq { + match m { + MemOp::BoundsCheck { .. } => seq![], + MemOp::Load { ty, .. } => seq![ + x64::Instr::MovLoad { sz: mty_sz(ty), dst: rax(), mem: ix_mem() }, + store_slot((nl + h - 1) as nat, rax()), + ], + MemOp::Store { ty, .. } => seq![ + x64::Instr::MovStore { sz: mty_sz(ty), mem: ix_mem(), src: rdi() }, + ], + } +} + +/// The length of the bounds-check sequence's instructions from the `j`th on +/// (what an exit jumps over: `j` is 3 for the first, 5 for the second). +pub open spec fn chk_len_from(j: nat) -> nat { + code_len(clazel_codecheck::spec::check_seq(mem_chk(0, 1, 0, 0)).skip(j as int)) +} + +/// The displacement of the check's first exit: over the rest of the check, +/// the tail and the `jmp` over the stub. +pub open spec fn mem_r1(m: MemOp, nl: nat, h: nat) -> int { + (chk_len_from(3) + code_len(mem_tail(m, nl, h)) + 5) as int +} + +/// The displacement of the check's second exit. +pub open spec fn mem_r2(m: MemOp, nl: nat, h: nat) -> int { + (chk_len_from(5) + code_len(mem_tail(m, nl, h)) + 5) as int +} + +/// The bounds-check sequence of a memory instruction. +pub open spec fn mem_check(m: MemOp, nl: nat, h: nat) -> Seq { + clazel_codecheck::spec::check_seq( + mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)), + ) +} + +/// **The code of a memory instruction** (choice 11): the operands and the +/// region's base in registers; the validator's bounds check, whose two exits +/// go to the stub; the access; a `jmp` over the stub; the stub (an exit +/// sequence), so that check and access are one basic block. +pub open spec fn mem_code(m: MemOp, nl: nat, h: nat) -> Seq { + mem_pre(m, nl, h) + mem_check(m, nl, h) + mem_tail(m, nl, h) + seq![ + jmp_i(code_len(exit_seq(mem_exit_code(m))) as int), + ] + exit_seq(mem_exit_code(m)) +} + +// ---------------------------------------------------------------------------- +// br_table +// ---------------------------------------------------------------------------- + +/// The chain's entry for target `i`: `mov edx, i; cmp eax, edx; je rel`. +pub open spec fn brt_entry(i: nat, rel: int) -> Seq { + seq![ + x64::Instr::MovImm32 { dst: rdx(), imm_u32: i as u32 }, + x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: x64::Size::W32, dst: rax(), src: rdx() }, + jcc_i(4, rel), + ] +} + +/// The length of a chain entry (it does not depend on `i` or `rel`). +pub open spec fn brt_e() -> int { + code_len(brt_entry(0, 0)) as int +} + +/// The length of what comes before the chain: `mov rax, [index]`. +pub open spec fn brt_p0(nl: nat, h: nat) -> int { + code_len(seq![load_slot(rax(), (nl + h - 1) as nat)]) as int +} + +/// The displacement to label `l` from a jump that ends `end` bytes in. +pub open spec fn brt_rel(offs: Seq, l: usize, end: int) -> int { + if l < offs.len() { + offs[l as int] - end + } else { + 0 + } +} + +/// The chain from entry `i` on: entry `j` ends `p0 + (j + 1) e` bytes in. +pub open spec fn brt_entries(ls: Seq, offs: Seq, p0: int, i: nat) -> Seq + decreases ls.len() - i, +{ + if i >= ls.len() { + seq![] + } else { + brt_entry(i, brt_rel(offs, ls[i as int], p0 + (i + 1) * brt_e())) + brt_entries( + ls, + offs, + p0, + i + 1, + ) + } +} + +/// **The code of `br_table ls d`** (choice 10): `mov rax, [index]`, a compare +/// and `je` per target, then a `jmp` to the default. +pub open spec fn brt_code(ls: Seq, d: usize, nl: nat, h: nat, offs: Seq) -> Seq< + x64::Instr, +> { + let p0 = brt_p0(nl, h); + seq![load_slot(rax(), (nl + h - 1) as nat)] + brt_entries(ls, offs, p0, 0) + seq![ + jmp_i(brt_rel(offs, d, p0 + ls.len() * brt_e() + 5)), + ] +} + +/// The length in bytes of a node's code (it does not depend on the values of +/// the label offsets, only on how many there are: [`lemma_ctmpl_len`]). +pub open spec fn csize(n: Node, nl: nat, h: nat, depth: nat) -> nat + decreases n, 1nat, +{ + code_len(ctmpl(n, nl, h, zeros(depth))) +} + +/// The largest code this slice translates, in bytes: well below `2^31`, so +/// every displacement fits a `rel32` ([`crate::cfproofs::labels_fit`]). +pub open spec fn max_size() -> nat { + 0x2000_0000 +} + +/// The program `n` of a function with `nl` locals has a translation: its +/// frame holds the locals, its analysis, entered with an empty stack and the return label in scope, is +/// defined, every node in it has a template ([`templated`]), and the code is at most +/// [`max_size`] bytes. +pub open spec fn cf_prog_ok(n: Node, nl: nat) -> bool { + nl <= max_slots() && cf_flow(n, nl, 0, 1) is Some && templated(n) && csize(n, nl, 0, 1) + <= max_size() +} + +} // verus! diff --git a/crates/clazel-jit/src/cfuacc.rs b/crates/clazel-jit/src/cfuacc.rs new file mode 100644 index 000000000..c6605ab8a --- /dev/null +++ b/crates/clazel-jit/src/cfuacc.rs @@ -0,0 +1,683 @@ +//! The validator accepts the units of every template (jit.md item J.9, +//! "every emitted blob is accepted", x86-64): every unit of [`utmpl`] is +//! well formed ([`crate::cfunits::uok`]) and the scan ([`ucheck`]) accepts +//! them, whatever it knew before, provided no jump lands on the start of a +//! memory instruction's check or access ([`prot`]). +//! +//! The one place the scan needs what it learnt earlier is the access of a +//! `load` or `store`: it needs `rsi` to hold region `k`'s base (the load +//! before the check) and `rcx` to be checked for the access's size (the check +//! itself), and nothing may have made it forget in between, which is what +//! [`prot`] rules out. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfaccept::*; +#[cfg(verus_only)] +use crate::cfbs::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfmemvalid::*; +#[cfg(verus_only)] +use crate::cfsimple::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::cfunits::*; +#[cfg(verus_only)] +use crate::runseq::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every instruction of the list is simple. +pub open spec fn all_simp(c: Seq) -> bool { + forall|k: int| 0 <= k < c.len() ==> simp(#[trigger] c[k]) +} + +/// Single-instruction units of simple instructions are well formed. +pub proof fn lemma_ones_uok(c: Seq) + requires + forall|k: int| 0 <= k < c.len() ==> ok2(#[trigger] c[k]), + ensures + forall|k: int| 0 <= k < ones(c).len() ==> uok(#[trigger] ones(c)[k]), +{ + lemma_uflat_ones(c); + assert forall|k: int| 0 <= k < ones(c).len() implies uok(#[trigger] ones(c)[k]) by { + assert(ones(c)[k] == seq![c[k]]); + assert(ok2(c[k])); + } +} + +/// The scan accepts single-instruction units of simple instructions, whatever +/// it knew. +pub proof fn lemma_ones_ucheck( + c: Seq, + pos: int, + fs: clazel_codecheck::spec::Facts, + p: spec_fn(int) -> bool, +) + requires + all_simp(c), + ensures + ucheck(ones(c), pos, fs, p) is Some, + decreases c.len(), +{ + if c.len() > 0 { + lemma_uflat_ones(c); + assert(ones(c)[0] == seq![c[0]]); + let fs0 = if p(pos) { + clazel_codecheck::spec::no_facts() + } else { + fs + }; + lemma_simp_plain(fs0, c[0]); + let f = clazel_codecheck::spec::plain_ok(fs0, c[0])->Some_0; + assert(ufacts(seq![c[0]], fs0) == Some(f)); + assert(ones(c).skip(1) =~= ones(c.skip(1))); + assert(all_simp(c.skip(1))) by { + assert forall|k: int| 0 <= k < c.skip(1).len() implies simp(#[trigger] c.skip(1)[k]) by { + assert(c.skip(1)[k] == c[k + 1]); + } + } + lemma_ones_ucheck(c.skip(1), pos + code_len(seq![c[0]]) as int, f, p); + } +} + +/// A simple instruction can follow a check. +proof fn lemma_simp_ok2(i: x64::Instr) + requires + simp(i), + ensures + ok2(i), +{ +} + +// ---------------------------------------------------------------------------- +// The simple instructions of the templates +// ---------------------------------------------------------------------------- + +/// The condition test is simple. +pub proof fn lemma_cond_pre_simp(nl: nat, h: nat, cc: u8, rel: int) + requires + 1 <= h, + nl + h <= max_slots(), + cc != 2, + ensures + all_simp(cond_pre(nl, h, cc, rel)), +{ + assert(max_slots() == 31) by { + crate::tlemmas::lemma_abi_consts_t(); + } + let c = cond_pre(nl, h, cc, rel); + assert forall|k: int| 0 <= k < c.len() implies simp(#[trigger] c[k]) by { + assert(k == 0 || k == 1 || k == 2); + } +} + +/// A `jmp` is simple. +pub proof fn lemma_jmp_simp(rel: int) + ensures + all_simp(seq![jmp_i(rel)]), +{ + let c = seq![jmp_i(rel)]; + assert forall|k: int| 0 <= k < c.len() implies simp(#[trigger] c[k]) by { + assert(k == 0); + } +} + +/// The chain of `br_table` is simple. +pub proof fn lemma_entries_simp(ls: Seq, offs: Seq, p0: int, i: nat) + ensures + all_simp(brt_entries(ls, offs, p0, i)), + decreases ls.len() - i, +{ + if i < ls.len() { + let e = brt_entry(i, brt_rel(offs, ls[i as int], p0 + (i + 1) * brt_e())); + assert forall|k: int| 0 <= k < e.len() implies simp(#[trigger] e[k]) by { + assert(k == 0 || k == 1 || k == 2); + } + lemma_entries_simp(ls, offs, p0, i + 1); + let r = brt_entries(ls, offs, p0, i + 1); + assert(brt_entries(ls, offs, p0, i) == e + r); + assert forall|k: int| 0 <= k < (e + r).len() implies simp(#[trigger] (e + r)[k]) by { + if k < e.len() { + assert((e + r)[k] == e[k]); + } else { + assert((e + r)[k] == r[k - e.len()]); + } + } + } else { + assert(brt_entries(ls, offs, p0, i) =~= Seq::::empty()); + } +} + +/// The whole `br_table` is simple. +pub proof fn lemma_brt_simp(ls: Seq, d: usize, nl: nat, h: nat, offs: Seq) + requires + 1 <= h, + nl + h <= max_slots(), + ensures + all_simp(brt_code(ls, d, nl, h, offs)), +{ + assert(max_slots() == 31) by { + crate::tlemmas::lemma_abi_consts_t(); + } + let p0 = brt_p0(nl, h); + let a = seq![load_slot(rax(), (nl + h - 1) as nat)]; + let ch = brt_entries(ls, offs, p0, 0); + let jm = seq![jmp_i(brt_rel(offs, d, p0 + ls.len() * brt_e() + 5))]; + lemma_entries_simp(ls, offs, p0, 0); + assert(all_simp(a)) by { + assert forall|k: int| 0 <= k < a.len() implies simp(#[trigger] a[k]) by { + assert(k == 0); + } + } + lemma_jmp_simp(brt_rel(offs, d, p0 + ls.len() * brt_e() + 5)); + let whole = brt_code(ls, d, nl, h, offs); + assert(whole == a + ch + jm); + assert forall|k: int| 0 <= k < whole.len() implies simp(#[trigger] whole[k]) by { + if k < a.len() { + assert(whole[k] == a[k]); + } else if k < a.len() + ch.len() { + assert(whole[k] == ch[k - a.len()]); + } else { + assert(whole[k] == jm[k - a.len() - ch.len()]); + } + } +} + +// ---------------------------------------------------------------------------- +// Memory instructions +// ---------------------------------------------------------------------------- + +/// The validator reads the bounds-check sequence of the template as a unit. +pub proof fn lemma_check_unit(c: clazel_codecheck::spec::Chk) + requires + clazel_codecheck::spec::check_ok(c), + 0 <= c.n <= 8, + ensures + clazel_codecheck::spec::check_unit_at(clazel_codecheck::spec::check_seq(c), 0), + clazel_codecheck::spec::check_params(clazel_codecheck::spec::check_seq(c), 0) == Some(c), +{ + let cs = clazel_codecheck::spec::check_seq(c); + assert(cs[0] == clazel_codecheck::spec::check_instr(0, c)); + assert(cs[1] == clazel_codecheck::spec::check_instr(1, c)); + assert(cs[2] == clazel_codecheck::spec::check_instr(2, c)); + assert(cs[3] == clazel_codecheck::spec::check_instr(3, c)); + assert(cs[4] == clazel_codecheck::spec::check_instr(4, c)); + assert(cs[5] == clazel_codecheck::spec::check_instr(5, c)); + assert(cs[6] == clazel_codecheck::spec::check_instr(6, c)); + assert(cs[7] == clazel_codecheck::spec::check_instr(7, c)); + let bo = abi::bound_off(c.k); + assert(bo == 16 * c.k + 8); + assert((bo as i32) as int == bo); + assert((c.n as i32) as int == c.n); + assert(((bo as i32) as int - 8) / 16 == c.k) by (nonlinear_arith) + requires + bo == 16 * c.k + 8, + 0 <= c.k < 16, + (bo as i32) as int == bo, + ; + let c2 = clazel_codecheck::spec::check_params(cs, 0)->Some_0; + assert(c2 == c); +} + +/// `fact_of` after one more binding. +pub proof fn lemma_fact_of_push( + fs: clazel_codecheck::spec::Facts, + b: clazel_codecheck::spec::Binding, + r: u8, +) + ensures + clazel_codecheck::spec::fact_of(fs.push(b), r) == if b.reg == r { + b.fact + } else { + clazel_codecheck::spec::fact_of(fs, r) + }, +{ + assert(fs.push(b).drop_last() =~= fs); + assert(fs.push(b).last() == b); +} + +/// The load of a region's base gives `rsi` that region, whatever the scan +/// knew. +pub proof fn lemma_base_load(fs: clazel_codecheck::spec::Facts, k: nat) + requires + k < 16, + ensures + clazel_codecheck::spec::plain_ok( + fs, + x64::Instr::MovLoad { sz: x64::Size::W64, dst: rsi(), mem: base_mem(k) }, + ) == Some( + fs.push( + clazel_codecheck::spec::Binding { + reg: 6, + fact: clazel_codecheck::spec::Fact::Base(k as int), + }, + ), + ), +{ + let i = x64::Instr::MovLoad { sz: x64::Size::W64, dst: rsi(), mem: base_mem(k) }; + let m = base_mem(k); + assert(abi::base_off(k as int) == 16 * k); + assert((m.disp as int) == 16 * k); + assert(clazel_codecheck::spec::base_load_region(i) == Some(k as int)) by { + assert((16 * k) / 16 == k) by (nonlinear_arith); + } + assert(clazel_codecheck::spec::access_ok(fs, clazel_codecheck::spec::mem_access(i)->Some_0)); +} + +/// What the scan knows after the check: `rsi` is region `k`'s base, and `rcx` +/// is checked for `n` bytes. Then an access of at most `n` bytes through +/// `[rsi + rcx]` is admitted. +pub proof fn lemma_access_after( + fs: clazel_codecheck::spec::Facts, + k: int, + n: int, + size: int, + store: bool, +) + requires + size <= n, + clazel_codecheck::spec::fact_of(fs, 6) == clazel_codecheck::spec::Fact::Base(k), + ensures + clazel_codecheck::spec::access_ok( + fs.push(clazel_codecheck::spec::Binding { + reg: 2, + fact: clazel_codecheck::spec::Fact::Unknown, + }).push( + clazel_codecheck::spec::Binding { + reg: 0, + fact: clazel_codecheck::spec::Fact::Unknown, + }, + ).push( + clazel_codecheck::spec::Binding { + reg: 1, + fact: clazel_codecheck::spec::Fact::Idx(k, n), + }, + ), + clazel_codecheck::spec::Access { mem: ix_mem(), size: size, store: store }, + ), +{ + let b2 = clazel_codecheck::spec::Binding { + reg: 2, + fact: clazel_codecheck::spec::Fact::Unknown, + }; + let b0 = clazel_codecheck::spec::Binding { + reg: 0, + fact: clazel_codecheck::spec::Fact::Unknown, + }; + let b1 = clazel_codecheck::spec::Binding { + reg: 1, + fact: clazel_codecheck::spec::Fact::Idx(k, n), + }; + let f1 = fs.push(b2); + let f2 = f1.push(b0); + let f3 = f2.push(b1); + lemma_fact_of_push(fs, b2, 6); + lemma_fact_of_push(f1, b0, 6); + lemma_fact_of_push(f2, b1, 6); + lemma_fact_of_push(f2, b1, 1); +} + +/// What the scan knows after the check, as the validator computes it from the +/// check's instructions. +proof fn lemma_check_facts_of(c: clazel_codecheck::spec::Chk, fs: clazel_codecheck::spec::Facts) + requires + clazel_codecheck::spec::check_ok(c), + 0 <= c.n <= 8, + ensures + clazel_codecheck::spec::check_facts(fs, clazel_codecheck::spec::check_seq(c), 0) + == fs.push(clazel_codecheck::spec::Binding { + reg: c.t, + fact: clazel_codecheck::spec::Fact::Unknown, + }).push( + clazel_codecheck::spec::Binding { + reg: c.z, + fact: clazel_codecheck::spec::Fact::Unknown, + }, + ).push( + clazel_codecheck::spec::Binding { + reg: c.i, + fact: clazel_codecheck::spec::Fact::Idx(c.k, c.n), + }, + ), +{ + lemma_check_unit(c); +} + +/// The check of a memory instruction is well formed for the validator. +proof fn lemma_mem_check_ok(m: MemOp, nl: nat, h: nat) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + ensures + clazel_codecheck::spec::check_ok( + mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)), + ), + 0 <= mem_n(m) <= 8, + mem_region(m) < 16, +{ + assert(abi::reserved(0) == false && abi::reserved(1) == false && abi::reserved(2) == false); + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { ty, .. } => { + assert(ty.size() == 4 || ty.size() == 8); + }, + MemOp::Store { ty, .. } => { + assert(ty.size() == 4 || ty.size() == 8); + }, + } +} + +/// The scan over one unit. +pub proof fn lemma_ucheck_one( + u: Seq, + pos: int, + fs: clazel_codecheck::spec::Facts, + p: spec_fn(int) -> bool, +) + ensures + ucheck(seq![u], pos, fs, p) == ufacts(u, if p(pos) { + clazel_codecheck::spec::no_facts() + } else { + fs + }), +{ + assert(seq![u].skip(1) =~= Seq::>::empty()); + let fs0 = if p(pos) { + clazel_codecheck::spec::no_facts() + } else { + fs + }; + match ufacts(u, fs0) { + None => {}, + Some(f) => { + assert(ucheck(Seq::>::empty(), pos + code_len(u) as int, f, p) == Some( + f, + )); + }, + } +} + +/// One instruction as a one-unit list. +pub proof fn lemma_ones_one(i: x64::Instr) + ensures + ones(seq![i]) =~= seq![seq![i]], +{ + lemma_uflat_ones(seq![i]); + assert(ones(seq![i]).len() == 1); + assert(ones(seq![i])[0] == seq![i]); +} + +/// The scan over the first part of a memory instruction: the operands and the +/// base. For a load or a store it ends knowing `rsi` is region `k`'s base. +proof fn lemma_pre_ucheck( + m: MemOp, + nl: nat, + h: nat, + pos: int, + fs: clazel_codecheck::spec::Facts, + p: spec_fn(int) -> bool, +) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + ensures + ucheck(ones(mem_pre(m, nl, h)), pos, fs, p) is Some, + !(m is BoundsCheck) ==> clazel_codecheck::spec::fact_of( + ucheck(ones(mem_pre(m, nl, h)), pos, fs, p)->Some_0, + 6, + ) == clazel_codecheck::spec::Fact::Base(mem_region(m) as int), +{ + assert(max_slots() == 31) by { + crate::tlemmas::lemma_abi_consts_t(); + } + let pre = mem_pre(m, nl, h); + let k = mem_region(m); + match m { + MemOp::BoundsCheck { .. } => { + assert(all_simp(pre)) by { + assert forall|q: int| 0 <= q < pre.len() implies simp(#[trigger] pre[q]) by { + assert(q == 0); + } + } + lemma_ones_ucheck(pre, pos, fs, p); + }, + MemOp::Load { .. } | MemOp::Store { .. } => { + let last = x64::Instr::MovLoad { + sz: x64::Size::W64, + dst: rsi(), + mem: base_mem(k), + }; + let init = pre.drop_last(); + assert(pre.last() == last); + assert(all_simp(init)) by { + assert forall|q: int| 0 <= q < init.len() implies simp(#[trigger] init[q]) by { + assert(init[q] == pre[q]); + assert(q == 0 || q == 1); + } + } + assert(pre =~= init + seq![last]); + lemma_ones_cat(init, seq![last]); + lemma_ones_one(last); + lemma_ucheck_cat(ones(init), ones(seq![last]), pos, fs, p); + lemma_ones_ucheck(init, pos, fs, p); + let f0 = ucheck(ones(init), pos, fs, p)->Some_0; + lemma_blen_ones(init); + let q = pos + code_len(init); + let fs0 = if p(q) { + clazel_codecheck::spec::no_facts() + } else { + f0 + }; + lemma_base_load(fs0, k); + lemma_ucheck_one(seq![last], q, f0, p); + let b6 = clazel_codecheck::spec::Binding { + reg: 6, + fact: clazel_codecheck::spec::Fact::Base(k as int), + }; + assert(ufacts(seq![last], fs0) == Some(fs0.push(b6))); + lemma_fact_of_push(fs0, b6, 6); + }, + } +} + +/// `ones` of a concatenation. +proof fn lemma_ones_cat(a: Seq, b: Seq) + ensures + ones(a + b) =~= ones(a) + ones(b), +{ + lemma_uflat_ones(a); + lemma_uflat_ones(b); + lemma_uflat_ones(a + b); + assert forall|q: int| 0 <= q < ones(a + b).len() implies ones(a + b)[q] == (ones(a) + + ones(b))[q] by { + if q < a.len() { + assert((a + b)[q] == a[q]); + } else { + assert((a + b)[q] == b[q - a.len()]); + } + } +} + +/// The access of a load or a store is admitted after the check. +proof fn lemma_tail_ucheck( + m: MemOp, + nl: nat, + h: nat, + pos: int, + f1: clazel_codecheck::spec::Facts, + p: spec_fn(int) -> bool, +) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + !(m is BoundsCheck) ==> clazel_codecheck::spec::fact_of(f1, 6) + == clazel_codecheck::spec::Fact::Base(mem_region(m) as int), + !p(pos), + ensures + ucheck( + ones(mem_tail(m, nl, h)), + pos, + f1.push(clazel_codecheck::spec::Binding { + reg: 2, + fact: clazel_codecheck::spec::Fact::Unknown, + }).push( + clazel_codecheck::spec::Binding { + reg: 0, + fact: clazel_codecheck::spec::Fact::Unknown, + }, + ).push( + clazel_codecheck::spec::Binding { + reg: 1, + fact: clazel_codecheck::spec::Fact::Idx(mem_region(m) as int, mem_n(m) as int), + }, + ), + p, + ) is Some, +{ + assert(max_slots() == 31) by { + crate::tlemmas::lemma_abi_consts_t(); + } + let tail = mem_tail(m, nl, h); + let k = mem_region(m) as int; + let n = mem_n(m) as int; + let f3 = f1.push( + clazel_codecheck::spec::Binding { reg: 2, fact: clazel_codecheck::spec::Fact::Unknown }, + ).push( + clazel_codecheck::spec::Binding { reg: 0, fact: clazel_codecheck::spec::Fact::Unknown }, + ).push( + clazel_codecheck::spec::Binding { reg: 1, fact: clazel_codecheck::spec::Fact::Idx(k, n) }, + ); + match m { + MemOp::BoundsCheck { .. } => { + assert(tail =~= Seq::::empty()); + assert(ones(tail) =~= Seq::>::empty()); + assert(ucheck(ones(tail), pos, f3, p) == Some(f3)); + }, + MemOp::Load { ty, .. } => { + let acc = x64::Instr::MovLoad { sz: mty_sz(ty), dst: rax(), mem: ix_mem() }; + let st = store_slot((nl + h - 1) as nat, rax()); + assert(tail == seq![acc, st]); + lemma_access_after(f1, k, n, x64::nbytes(mty_sz(ty)) as int, false); + let a = clazel_codecheck::spec::mem_access(acc)->Some_0; + assert(clazel_codecheck::spec::access_ok(f3, a)); + assert(!clazel_codecheck::spec::writes_reserved(acc)); + assert(clazel_codecheck::spec::plain_ok(f3, acc) is Some); + let f4 = clazel_codecheck::spec::plain_ok(f3, acc)->Some_0; + assert(tail =~= seq![acc] + seq![st]); + lemma_ones_cat(seq![acc], seq![st]); + lemma_ones_one(acc); + lemma_ucheck_cat(ones(seq![acc]), ones(seq![st]), pos, f3, p); + lemma_ucheck_one(seq![acc], pos, f3, p); + assert(ufacts(seq![acc], f3) == Some(f4)); + lemma_blen_ones(seq![acc]); + lemma_load_slot_straight(0, (nl + h - 1) as nat); + assert(all_simp(seq![st])) by { + assert forall|q: int| 0 <= q < 1 implies simp(#[trigger] seq![st][q]) by { + assert(q == 0); + } + } + lemma_ones_ucheck(seq![st], pos + code_len(seq![acc]) as int, f4, p); + }, + MemOp::Store { ty, .. } => { + let acc = x64::Instr::MovStore { sz: mty_sz(ty), mem: ix_mem(), src: rdi() }; + assert(tail == seq![acc]); + lemma_access_after(f1, k, n, x64::nbytes(mty_sz(ty)) as int, true); + let a = clazel_codecheck::spec::mem_access(acc)->Some_0; + assert(clazel_codecheck::spec::access_ok(f3, a)); + assert(!clazel_codecheck::spec::writes_reserved(acc)); + assert(clazel_codecheck::spec::plain_ok(f3, acc) is Some); + lemma_ones_one(acc); + lemma_ucheck_one(seq![acc], pos, f3, p); + }, + } +} + +/// **A memory instruction is accepted**, whatever the scan knew, if no jump +/// lands on the start of its check or of its access. +pub proof fn lemma_uc_mem( + m: MemOp, + nl: nat, + h: nat, + pos: int, + fs: clazel_codecheck::spec::Facts, + p: spec_fn(int) -> bool, +) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + !p(pos + code_len(mem_pre(m, nl, h))), + !p(pos + code_len(mem_pre(m, nl, h)) + code_len(mem_check(m, nl, h))), + ensures + ucheck(mem_units(m, nl, h), pos, fs, p) is Some, +{ + lemma_mem_check_ok(m, nl, h); + lemma_mem_parts_len(m, nl, h); + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![jmp_i(code_len(exit_seq(mem_exit_code(m))) as int)]; + let stub = exit_seq(mem_exit_code(m)); + let c0 = mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)); + let k = mem_region(m) as int; + let n = mem_n(m) as int; + let b1 = code_len(pre) as int; + let b2 = b1 + code_len(chk) as int; + lemma_pre_ucheck(m, nl, h, pos, fs, p); + let f1 = ucheck(ones(pre), pos, fs, p)->Some_0; + lemma_blen_ones(pre); + // the check + lemma_check_unit(c0); + assert(chk == clazel_codecheck::spec::check_seq(c0)); + lemma_check_facts_of(c0, f1); + let f2 = clazel_codecheck::spec::check_facts(f1, chk, 0); + lemma_ucheck_one(chk, pos + b1, f1, p); + assert(ufacts(chk, f1) == Some(f2)); + lemma_blen_one(chk); + // the access + lemma_tail_ucheck(m, nl, h, pos + b2, f1, p); + let f3 = ucheck(ones(tail), pos + b2, f2, p)->Some_0; + lemma_blen_ones(tail); + // the jmp and the stub + lemma_jmp_simp(code_len(stub) as int); + let q = pos + b2 + code_len(tail) as int; + let fj = if p(q) { + clazel_codecheck::spec::no_facts() + } else { + f3 + }; + lemma_simp_plain(fj, jm[0]); + assert(ufacts(jm, fj) is Some); + lemma_exit_seq_len(mem_exit_code(m)); + assert(seq![jm, stub] =~= seq![jm] + seq![stub]); + lemma_ucheck_cat(seq![jm], seq![stub], q, f3, p); + lemma_ucheck_one(jm, q, f3, p); + lemma_blen_one(jm); + let fj1 = ufacts(jm, fj)->Some_0; + lemma_ucheck_one(stub, q + code_len(jm) as int, fj1, p); + assert(stub.len() == 3); + // put the parts together + let u1 = ones(pre); + let u2 = seq![chk]; + let u3 = ones(tail); + let u4 = seq![jm, stub]; + assert(mem_units(m, nl, h) =~= u1 + u2 + u3 + u4); + lemma_ucheck_cat(u1, u2, pos, fs, p); + lemma_ucheck_cat(u1 + u2, u3, pos, fs, p); + lemma_ucheck_cat(u1 + u2 + u3, u4, pos, fs, p); + lemma_blen_cat(u1, u2); + lemma_blen_cat(u1 + u2, u3); + assert(blen(u1) == b1); + assert(blen(u1 + u2) == b2); + assert(blen(u1 + u2 + u3) == b2 + code_len(tail)); +} + +} // verus! diff --git a/crates/clazel-jit/src/cfuacc2.rs b/crates/clazel-jit/src/cfuacc2.rs new file mode 100644 index 000000000..e0fc32b59 --- /dev/null +++ b/crates/clazel-jit/src/cfuacc2.rs @@ -0,0 +1,717 @@ +//! The units of every template are well formed and accepted by the +//! validator's scan, by induction over the program (jit.md item J.9, "every +//! emitted blob is accepted", x86-64): [`lemma_utmpl_uok`] and +//! [`lemma_utmpl_ucheck`]. The leaves are in [`crate::cfuacc`]. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfaccept::*; +#[cfg(verus_only)] +use crate::cfbs::*; +#[cfg(verus_only)] +use crate::cflens::*; +#[cfg(verus_only)] +use crate::cfmemlens::*; +#[cfg(verus_only)] +use crate::cfsimple::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::cfuacc::*; +#[cfg(verus_only)] +use crate::cfunits::*; +#[cfg(verus_only)] +use crate::runseq::*; +#[cfg(verus_only)] +use crate::templates::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every unit of the list is well formed. +pub open spec fn uok_all(us: Us) -> bool { + forall|k: int| 0 <= k < us.len() ==> uok(#[trigger] us[k]) +} + +/// Well-formed units followed by well-formed units. +pub proof fn lemma_uok_cat(a: Us, b: Us) + requires + uok_all(a), + uok_all(b), + ensures + uok_all(a + b), +{ + assert forall|k: int| 0 <= k < (a + b).len() implies uok(#[trigger] (a + b)[k]) by { + if k < a.len() { + assert((a + b)[k] == a[k]); + } else { + assert((a + b)[k] == b[k - a.len()]); + } + } +} + +/// A list of simple instructions is a list of well-formed units. +proof fn lemma_simp_uok(c: Seq) + requires + all_simp(c), + ensures + uok_all(ones(c)), +{ + assert forall|k: int| 0 <= k < c.len() implies ok2(#[trigger] c[k]) by { + assert(simp(c[k])); + } + lemma_ones_uok(c); +} + +/// An exit sequence is a well-formed unit. +pub proof fn lemma_exit_uok(c: u32) + ensures + uok(exit_seq(c)), +{ + let e = exit_seq(c); + assert(e.len() == 3); + assert(clazel_codecheck::spec::exit_unit_at(e, 0)); +} + +/// A memory instruction's units are well formed. +proof fn lemma_mem_uok(m: MemOp, nl: nat, h: nat) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + ensures + uok_all(mem_units(m, nl, h)), +{ + assert(max_slots() == 31) by { + crate::tlemmas::lemma_abi_consts_t(); + } + let pre = mem_pre(m, nl, h); + let tail = mem_tail(m, nl, h); + let chk = mem_check(m, nl, h); + let jm = seq![jmp_i(code_len(exit_seq(mem_exit_code(m))) as int)]; + let stub = exit_seq(mem_exit_code(m)); + let c0 = mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)); + lemma_mem_check_ok_pub(m, nl, h); + lemma_check_unit(c0); + assert(chk == clazel_codecheck::spec::check_seq(c0)); + // operands + assert(forall|q: int| 0 <= q < pre.len() ==> ok2(#[trigger] pre[q])) by { + assert forall|q: int| 0 <= q < pre.len() implies ok2(pre[q]) by { + match m { + MemOp::BoundsCheck { .. } => { + assert(q == 0); + }, + MemOp::Load { .. } => { + assert(q == 0 || q == 1); + }, + MemOp::Store { .. } => { + assert(q == 0 || q == 1 || q == 2); + }, + } + } + } + lemma_ones_uok(pre); + assert(forall|q: int| 0 <= q < tail.len() ==> ok2(#[trigger] tail[q])) by { + assert forall|q: int| 0 <= q < tail.len() implies ok2(tail[q]) by { + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { .. } => { + assert(q == 0 || q == 1); + }, + MemOp::Store { .. } => { + assert(q == 0); + }, + } + } + } + lemma_ones_uok(tail); + lemma_exit_uok(mem_exit_code(m)); + let u2 = seq![chk]; + let u4 = seq![jm, stub]; + assert(uok_all(u2)) by { + assert forall|k: int| 0 <= k < u2.len() implies uok(#[trigger] u2[k]) by { + assert(k == 0); + assert(u2[0] == chk); + } + } + assert(uok_all(u4)) by { + assert forall|k: int| 0 <= k < u4.len() implies uok(#[trigger] u4[k]) by { + assert(k == 0 || k == 1); + } + } + lemma_uok_cat(ones(pre), u2); + lemma_uok_cat(ones(pre) + u2, ones(tail)); + lemma_uok_cat(ones(pre) + u2 + ones(tail), u4); +} + +/// The check of a memory instruction is well formed (public face). +pub proof fn lemma_mem_check_ok_pub(m: MemOp, nl: nat, h: nat) + requires + cf_flow(Node::Mem(m), nl, h, 0) is Some, + ensures + clazel_codecheck::spec::check_ok( + mem_chk(mem_region(m), mem_n(m), mem_r1(m, nl, h), mem_r2(m, nl, h)), + ), + 0 <= mem_n(m) <= 8, + mem_region(m) < 16, +{ + assert(abi::reserved(0) == false && abi::reserved(1) == false && abi::reserved(2) == false); + match m { + MemOp::BoundsCheck { .. } => {}, + MemOp::Load { ty, .. } => { + assert(ty.size() == 4 || ty.size() == 8); + }, + MemOp::Store { ty, .. } => { + assert(ty.size() == 4 || ty.size() == 8); + }, + } +} + +/// **Every unit of a template is well formed.** +pub proof fn lemma_utmpl_uok(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + ensures + uok_all(utmpl(n, nl, h, offs)), + decreases n, +{ + let d = offs.len(); + match n { + Node::Op(o) => { + lemma_tmpl_straight(o, nl, h); + assert(all_simp(tmpl(o, nl, h))) by { + assert forall|k: int| 0 <= k < tmpl(o, nl, h).len() implies simp( + #[trigger] tmpl(o, nl, h)[k], + ) by { + assert(straight(tmpl(o, nl, h)[k])); + } + } + lemma_simp_uok(tmpl(o, nl, h)); + }, + Node::Mem(m) => { + assert(cf_flow(n, nl, h, 0) is Some); + lemma_mem_uok(m, nl, h); + }, + Node::Then(a, b) => { + let ob = shift(offs, csize(*a, nl, h, d) as int); + lemma_utmpl_uok(*a, nl, h, offs); + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + assert(ob.len() == d); + lemma_utmpl_uok(*b, nl, flow_h(*a, nl, h, d), ob); + lemma_uok_cat(utmpl(*a, nl, h, offs), utmpl(*b, nl, flow_h(*a, nl, h, d), ob)); + }, + Some(Flow::Dead) => { + assert(*b == Node::Op(Op::Nop)); + assert(utmpl(*b, nl, flow_h(*a, nl, h, d), ob) =~= Seq::< + Seq, + >::empty()); + assert(utmpl(*a, nl, h, offs) + utmpl(*b, nl, flow_h(*a, nl, h, d), ob) + =~= utmpl(*a, nl, h, offs)); + }, + None => {}, + } + }, + Node::Block(b) => { + let ob = seq![csize(*b, nl, h, d + 1) as int] + offs; + assert(ob.len() == d + 1); + lemma_utmpl_uok(*b, nl, h, ob); + }, + Node::Loop(b) => { + let ob = seq![0int] + offs; + assert(ob.len() == d + 1); + lemma_utmpl_uok(*b, nl, h, ob); + }, + Node::If(t, e) => { + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p); + let oe = seq![se] + shift(offs, p + st + 5); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_utmpl_uok(*t, nl, (h - 1) as nat, ot); + lemma_utmpl_uok(*e, nl, (h - 1) as nat, oe); + let cp = cond_pre(nl, h, 4, st + 5); + lemma_cond_pre_simp(nl, h, 4, st + 5); + lemma_simp_uok(cp); + lemma_jmp_simp(se); + lemma_simp_uok(seq![jmp_i(se)]); + let jmu = seq![seq![jmp_i(se)]]; + assert(uok_all(jmu)) by { + assert forall|k: int| 0 <= k < jmu.len() implies uok(#[trigger] jmu[k]) by { + assert(k == 0); + assert(ones(seq![jmp_i(se)])[0] == seq![jmp_i(se)]); + } + } + lemma_uok_cat(ones(cp), utmpl(*t, nl, (h - 1) as nat, ot)); + lemma_uok_cat(ones(cp) + utmpl(*t, nl, (h - 1) as nat, ot), jmu); + lemma_uok_cat( + ones(cp) + utmpl(*t, nl, (h - 1) as nat, ot) + jmu, + utmpl(*e, nl, (h - 1) as nat, oe), + ); + }, + Node::Br(l) => { + assert(ctmpl(n, nl, h, offs) == seq![jmp_i(if l < offs.len() { + offs[l as int] - 5 + } else { + 0 + })]); + lemma_jmp_simp(if l < offs.len() { + offs[l as int] - 5 + } else { + 0 + }); + lemma_simp_uok(ctmpl(n, nl, h, offs)); + }, + Node::BrIf(l) => { + let rel = if l < offs.len() { + offs[l as int] - cond_len(nl, h) as int + } else { + 0 + }; + lemma_cond_pre_simp(nl, h, 5, rel); + assert(ctmpl(n, nl, h, offs) == cond_pre(nl, h, 5, rel)); + lemma_simp_uok(ctmpl(n, nl, h, offs)); + }, + Node::BrTable(ls, dd) => { + lemma_brt_simp(ls@, dd, nl, h, offs); + lemma_simp_uok(ctmpl(n, nl, h, offs)); + }, + Node::Exit(c, _) => { + lemma_exit_uok(c); + let us = seq![exit_seq(c)]; + assert(uok_all(us)) by { + assert forall|k: int| 0 <= k < us.len() implies uok(#[trigger] us[k]) by { + assert(k == 0); + } + } + }, + } +} + +// ---------------------------------------------------------------------------- +/// No protected position of the node (starting at `pos`) is a jump target. +pub open spec fn pf(n: Node, nl: nat, h: nat, d: nat, pos: int, p: spec_fn(int) -> bool) -> bool { + forall|x: int| #[trigger] prot(n, nl, h, d, x) ==> !p(pos + x) +} + +/// The statement of [`lemma_utmpl_ucheck`] for a node: from any position, with +/// whatever the scan knew, and any set of jump targets that avoids +/// [`prot`], the scan accepts the units. +pub open spec fn uc_all(n: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) -> bool { + forall|pos: int, fs: clazel_codecheck::spec::Facts| + pf(n, nl, h, offs.len(), pos, p) ==> #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some +} + +/// `then`. +proof fn lemma_uc_then( + a: Node, + b: Node, + nl: nat, + h: nat, + offs: Seq, + p: spec_fn(int) -> bool, +) + requires + cf_flow(Node::Then(Box::new(a), Box::new(b)), nl, h, offs.len()) is Some, + cf_flow(a, nl, h, offs.len()) is Some, + uc_all(a, nl, h, offs, p), + match cf_flow(a, nl, h, offs.len()) { + Some(Flow::Falls(h1)) => uc_all( + b, + nl, + flow_h(a, nl, h, offs.len()), + shift(offs, csize(a, nl, h, offs.len()) as int), + p, + ), + _ => true, + }, + ensures + uc_all(Node::Then(Box::new(a), Box::new(b)), nl, h, offs, p), +{ + let n = Node::Then(Box::new(a), Box::new(b)); + let d = offs.len(); + let sa = csize(a, nl, h, d); + let h1 = flow_h(a, nl, h, d); + let ob = shift(offs, sa as int); + lemma_utmpl_blen(a, nl, h, offs); + assert forall|pos: int, fs: clazel_codecheck::spec::Facts| + pf(n, nl, h, d, pos, p) implies #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some by { + let ua = utmpl(a, nl, h, offs); + let ub = utmpl(b, nl, h1, ob); + assert(pf(a, nl, h, d, pos, p)) by { + assert forall|x: int| #[trigger] prot(a, nl, h, d, x) implies !p(pos + x) by { + assert(prot(n, nl, h, d, x)); + } + } + assert(ucheck(ua, pos, fs, p) is Some); + let f = ucheck(ua, pos, fs, p)->Some_0; + lemma_ucheck_cat(ua, ub, pos, fs, p); + match cf_flow(a, nl, h, d) { + Some(Flow::Falls(_)) => { + assert(ob.len() == d); + assert(pf(b, nl, h1, d, pos + sa as int, p)) by { + assert forall|y: int| #[trigger] prot(b, nl, h1, d, y) implies !p(pos + sa as int + y) by { + assert(prot(n, nl, h, d, y + sa as int)); + } + } + assert(ucheck(ub, pos + sa as int, f, p) is Some); + }, + Some(Flow::Dead) => { + assert(b == Node::Op(Op::Nop)); + assert(ub =~= Seq::>::empty()); + }, + None => {}, + } + } +} + +/// The part of `if` after the condition, from the scan's facts after it. +proof fn lemma_uc_if_rest( + t: Node, + e: Node, + nl: nat, + h: nat, + offs: Seq, + p: spec_fn(int) -> bool, + pos: int, + f1: clazel_codecheck::spec::Facts, +) + requires + cf_flow(Node::If(Box::new(t), Box::new(e)), nl, h, offs.len()) is Some, + uc_all(t, nl, (h - 1) as nat, seq![ + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 5 + csize( + e, + nl, + (h - 1) as nat, + offs.len() + 1, + ) as int, + ] + shift(offs, cond_len(nl, h) as int), p), + uc_all(e, nl, (h - 1) as nat, seq![csize(e, nl, (h - 1) as nat, offs.len() + 1) as int] + + shift( + offs, + cond_len(nl, h) as int + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 5, + ), p), + pf(Node::If(Box::new(t), Box::new(e)), nl, h, offs.len(), pos, p), + ensures + ucheck( + utmpl(t, nl, (h - 1) as nat, seq![ + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 5 + csize( + e, + nl, + (h - 1) as nat, + offs.len() + 1, + ) as int, + ] + shift(offs, cond_len(nl, h) as int)) + + seq![seq![jmp_i(csize(e, nl, (h - 1) as nat, offs.len() + 1) as int)]] + + utmpl(e, nl, (h - 1) as nat, seq![ + csize(e, nl, (h - 1) as nat, offs.len() + 1) as int, + ] + shift( + offs, + cond_len(nl, h) as int + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 5, + )), + pos + cond_len(nl, h) as int, + f1, + p, + ) is Some, +{ + let n = Node::If(Box::new(t), Box::new(e)); + let d = offs.len(); + let h0 = (h - 1) as nat; + let p0 = cond_len(nl, h) as int; + let st = csize(t, nl, h0, d + 1) as int; + let se = csize(e, nl, h0, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p0); + let oe = seq![se] + shift(offs, p0 + st + 5); + assert(ot.len() == d + 1 && oe.len() == d + 1); + let ut = utmpl(t, nl, h0, ot); + let ue = utmpl(e, nl, h0, oe); + let jmu = seq![seq![jmp_i(se)]]; + lemma_utmpl_blen(t, nl, h0, ot); + lemma_utmpl_blen(e, nl, h0, oe); + crate::cflens::lemma_jmp_len(se); + lemma_code_len_one(jmp_i(se)); + lemma_blen_one(seq![jmp_i(se)]); + lemma_jmp_simp(se); + assert(pf(t, nl, h0, d + 1, pos + p0, p)) by { + assert forall|x: int| #[trigger] prot(t, nl, h0, d + 1, x) implies !p(pos + p0 + x) by { + assert(prot(n, nl, h, d, x + p0)); + } + } + assert(ucheck(ut, pos + p0, f1, p) is Some); + let f2 = ucheck(ut, pos + p0, f1, p)->Some_0; + let q = pos + p0 + st; + let fj = if p(q) { + clazel_codecheck::spec::no_facts() + } else { + f2 + }; + lemma_simp_plain(fj, jmp_i(se)); + lemma_ucheck_one(seq![jmp_i(se)], q, f2, p); + let f3 = ufacts(seq![jmp_i(se)], fj)->Some_0; + assert(pf(e, nl, h0, d + 1, pos + (p0 + st + 5), p)) by { + assert forall|x: int| #[trigger] prot(e, nl, h0, d + 1, x) implies !p(pos + (p0 + st + 5) + x) by { + assert(prot(n, nl, h, d, x + (p0 + st + 5))); + } + } + assert(ucheck(ue, pos + (p0 + st + 5), f3, p) is Some); + lemma_ucheck_cat(ut, jmu + ue, pos + p0, f1, p); + lemma_ucheck_cat(jmu, ue, pos + p0 + st, f2, p); + lemma_ucheck_one(seq![jmp_i(se)], q, f2, p); + assert(ucheck(jmu, q, f2, p) == Some(f3)) by { + lemma_ucheck_one(seq![jmp_i(se)], q, f2, p); + } + assert(blen(jmu) == 5); + assert(ut + jmu + ue =~= ut + (jmu + ue)); +} + +/// `if`. +proof fn lemma_uc_if(t: Node, e: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + cf_flow(Node::If(Box::new(t), Box::new(e)), nl, h, offs.len()) is Some, + uc_all(t, nl, (h - 1) as nat, seq![ + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 5 + csize( + e, + nl, + (h - 1) as nat, + offs.len() + 1, + ) as int, + ] + shift(offs, cond_len(nl, h) as int), p), + uc_all(e, nl, (h - 1) as nat, seq![csize(e, nl, (h - 1) as nat, offs.len() + 1) as int] + + shift( + offs, + cond_len(nl, h) as int + csize(t, nl, (h - 1) as nat, offs.len() + 1) as int + 5, + ), p), + ensures + uc_all(Node::If(Box::new(t), Box::new(e)), nl, h, offs, p), +{ + let n = Node::If(Box::new(t), Box::new(e)); + let d = offs.len(); + let h0 = (h - 1) as nat; + let p0 = cond_len(nl, h) as int; + let st = csize(t, nl, h0, d + 1) as int; + let se = csize(e, nl, h0, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p0); + let oe = seq![se] + shift(offs, p0 + st + 5); + assert(ot.len() == d + 1 && oe.len() == d + 1); + let cp = cond_pre(nl, h, 4, st + 5); + let ut = utmpl(t, nl, h0, ot); + let ue = utmpl(e, nl, h0, oe); + let jmu = seq![seq![jmp_i(se)]]; + lemma_cond_pre_len(nl, h, 4, st + 5); + lemma_blen_ones(cp); + lemma_cond_pre_simp(nl, h, 4, st + 5); + assert(utmpl(n, nl, h, offs) =~= ones(cp) + (ut + jmu + ue)); + assert forall|pos: int, fs: clazel_codecheck::spec::Facts| + pf(n, nl, h, d, pos, p) implies #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some by { + let a1 = ones(cp); + lemma_ones_ucheck(cp, pos, fs, p); + let f1 = ucheck(a1, pos, fs, p)->Some_0; + lemma_uc_if_rest(t, e, nl, h, offs, p, pos, f1); + lemma_ucheck_cat(a1, ut + jmu + ue, pos, fs, p); + } +} + +/// `block`. +proof fn lemma_uc_block(b: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + uc_all(b, nl, h, seq![csize(b, nl, h, offs.len() + 1) as int] + offs, p), + ensures + uc_all(Node::Block(Box::new(b)), nl, h, offs, p), +{ + let n = Node::Block(Box::new(b)); + let ob = seq![csize(b, nl, h, offs.len() + 1) as int] + offs; + assert(ob.len() == offs.len() + 1); + assert forall|pos: int, fs: clazel_codecheck::spec::Facts| + pf(n, nl, h, offs.len(), pos, p) implies #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some by { + assert(pf(b, nl, h, ob.len(), pos, p)) by { + assert forall|x: int| #[trigger] prot(b, nl, h, ob.len(), x) implies !p(pos + x) by { + assert(prot(n, nl, h, offs.len(), x)); + } + } + } +} + +/// `loop`. +proof fn lemma_uc_loop(b: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + uc_all(b, nl, h, seq![0int] + offs, p), + ensures + uc_all(Node::Loop(Box::new(b)), nl, h, offs, p), +{ + let n = Node::Loop(Box::new(b)); + let ob = seq![0int] + offs; + assert(ob.len() == offs.len() + 1); + assert forall|pos: int, fs: clazel_codecheck::spec::Facts| + pf(n, nl, h, offs.len(), pos, p) implies #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some by { + assert(pf(b, nl, h, ob.len(), pos, p)) by { + assert forall|x: int| #[trigger] prot(b, nl, h, ob.len(), x) implies !p(pos + x) by { + assert(prot(n, nl, h, offs.len(), x)); + } + } + } +} + +/// A node whose units are simple instructions, or an exit sequence. +proof fn lemma_uc_leaf(n: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + cf_flow(n, nl, h, offs.len()) is Some, + n is Op || n is Br || n is BrIf || n is BrTable || n is Exit, + ensures + uc_all(n, nl, h, offs, p), +{ + assert forall|pos: int, fs: clazel_codecheck::spec::Facts| + pf(n, nl, h, offs.len(), pos, p) implies #[trigger] ucheck( + utmpl(n, nl, h, offs), + pos, + fs, + p, + ) is Some by { + match n { + Node::Op(o) => { + lemma_tmpl_straight(o, nl, h); + assert(all_simp(tmpl(o, nl, h))) by { + assert forall|k: int| 0 <= k < tmpl(o, nl, h).len() implies simp( + #[trigger] tmpl(o, nl, h)[k], + ) by { + assert(straight(tmpl(o, nl, h)[k])); + } + } + lemma_ones_ucheck(tmpl(o, nl, h), pos, fs, p); + }, + Node::Br(l) => { + let rel = if l < offs.len() { + offs[l as int] - 5 + } else { + 0 + }; + assert(ctmpl(n, nl, h, offs) == seq![jmp_i(rel)]); + lemma_jmp_simp(rel); + lemma_ones_ucheck(ctmpl(n, nl, h, offs), pos, fs, p); + }, + Node::BrIf(l) => { + let rel = if l < offs.len() { + offs[l as int] - cond_len(nl, h) as int + } else { + 0 + }; + lemma_cond_pre_simp(nl, h, 5, rel); + assert(ctmpl(n, nl, h, offs) == cond_pre(nl, h, 5, rel)); + lemma_ones_ucheck(ctmpl(n, nl, h, offs), pos, fs, p); + }, + Node::BrTable(ls, dd) => { + lemma_brt_simp(ls@, dd, nl, h, offs); + lemma_ones_ucheck(ctmpl(n, nl, h, offs), pos, fs, p); + }, + Node::Exit(c, _) => { + lemma_ucheck_one(exit_seq(c), pos, fs, p); + assert(exit_seq(c).len() == 3); + }, + _ => {}, + } + } +} + +/// A memory instruction. +proof fn lemma_uc_mem_node(m: MemOp, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + cf_flow(Node::Mem(m), nl, h, offs.len()) is Some, + ensures + uc_all(Node::Mem(m), nl, h, offs, p), +{ + let n = Node::Mem(m); + let d = offs.len(); + assert(cf_flow(n, nl, h, 0) is Some); + assert forall|pos: int, fs: clazel_codecheck::spec::Facts| + pf(n, nl, h, d, pos, p) implies #[trigger] ucheck(utmpl(n, nl, h, offs), pos, fs, p) is Some by { + let x1 = code_len(mem_pre(m, nl, h)) as int; + let x2 = (code_len(mem_pre(m, nl, h)) + code_len(mem_check(m, nl, h))) as int; + assert(prot(n, nl, h, d, x1)); + assert(prot(n, nl, h, d, x2)); + lemma_uc_mem(m, nl, h, pos, fs, p); + } +} + +/// **The scan accepts the units of every template.** +pub proof fn lemma_utmpl_ucheck(n: Node, nl: nat, h: nat, offs: Seq, p: spec_fn(int) -> bool) + requires + cf_flow(n, nl, h, offs.len()) is Some, + ensures + uc_all(n, nl, h, offs, p), + decreases n, +{ + let d = offs.len(); + match n { + Node::Mem(m) => { + lemma_uc_mem_node(m, nl, h, offs, p); + }, + Node::Then(a, b) => { + let sa = csize(*a, nl, h, d); + let ob = shift(offs, sa as int); + lemma_utmpl_ucheck(*a, nl, h, offs, p); + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + assert(ob.len() == d); + lemma_utmpl_ucheck(*b, nl, h1, ob, p); + }, + _ => {}, + } + lemma_uc_then(*a, *b, nl, h, offs, p); + }, + Node::Block(b) => { + let ob = seq![csize(*b, nl, h, d + 1) as int] + offs; + assert(ob.len() == d + 1); + lemma_utmpl_ucheck(*b, nl, h, ob, p); + lemma_uc_block(*b, nl, h, offs, p); + }, + Node::Loop(b) => { + let ob = seq![0int] + offs; + assert(ob.len() == d + 1); + lemma_utmpl_ucheck(*b, nl, h, ob, p); + lemma_uc_loop(*b, nl, h, offs, p); + }, + Node::If(t, e) => { + let p0 = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p0); + let oe = seq![se] + shift(offs, p0 + st + 5); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_utmpl_ucheck(*t, nl, (h - 1) as nat, ot, p); + lemma_utmpl_ucheck(*e, nl, (h - 1) as nat, oe, p); + lemma_uc_if(*t, *e, nl, h, offs, p); + }, + _ => { + lemma_uc_leaf(n, nl, h, offs, p); + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfunits.rs b/crates/clazel-jit/src/cfunits.rs new file mode 100644 index 000000000..5a10e18ea --- /dev/null +++ b/crates/clazel-jit/src/cfunits.rs @@ -0,0 +1,517 @@ +//! Units: the bridge from a list of the validator's units to its `scan` +//! (jit.md item J.9, "every emitted blob is accepted", x86-64). +//! +//! The validator's `scan` ([`clazel_codecheck::spec::scan`]) walks the +//! instruction list from the front, recognizing an exit sequence (3 +//! instructions), a bounds-check sequence (8) or a single instruction at each +//! unit start, and forgets what it knows at each jump target. This module +//! says: **if the instruction list is the concatenation of a list of units, +//! each of which is of one of those three shapes, then `scan` walks exactly +//! those units** ([`lemma_scan_units`]), and what it holds at each unit is +//! computed by [`ucheck`], a recursion over the unit list, not over +//! instruction positions. The back end's proof then works on unit lists that +//! mirror its templates (`crate::cfaccept`). +//! +//! The one place `scan` looks past a unit is a single instruction, where it +//! must rule out an exit sequence and a bounds-check sequence starting +//! there. Both need the instruction two places on to be a `ret` and a `jb` +//! (`jcc` 2): no instruction at the first or second place of any unit is +//! either ([`ok2`], [`lemma_uok_ok2`]), so neither starts at a single +//! instruction ([`lemma_scan_units`]). + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::runseq::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A list of units: each is a list of instructions the validator treats as one +/// step of its scan. +pub type Us = Seq>; + +/// All the instructions of the units, in order. +pub open spec fn uflat(us: Us) -> Seq + decreases us.len(), +{ + if us.len() == 0 { + Seq::empty() + } else { + us[0] + uflat(us.skip(1)) + } +} + +/// The length in bytes of all the units. +pub open spec fn blen(us: Us) -> nat + decreases us.len(), +{ + if us.len() == 0 { + 0 + } else { + code_len(us[0]) + blen(us.skip(1)) + } +} + +/// One unit per instruction. +pub open spec fn ones(c: Seq) -> Us { + c.map_values(|i: x64::Instr| seq![i]) +} + +/// `jcc` with condition code 2 (`jb`): the second instruction of a +/// bounds-check sequence. +pub open spec fn jcc2(i: x64::Instr) -> bool { + i is Jcc && i->Jcc_cc == 2 +} + +/// An instruction that cannot be the third instruction of an exit sequence +/// (a `ret`) or of a bounds-check sequence (a `jcc 2`). +pub open spec fn ok2(i: x64::Instr) -> bool { + !(i is Ret) && !jcc2(i) +} + +/// A unit is well formed: a single instruction that is neither a `ret` nor a +/// `jcc 2`, an exit sequence, or a bounds-check sequence (both as the +/// validator recognizes them). +pub open spec fn uok(u: Seq) -> bool { + (u.len() == 1 && ok2(u[0])) || (u.len() == 3 && clazel_codecheck::spec::exit_unit_at(u, 0)) + || (u.len() == 8 && clazel_codecheck::spec::check_unit_at(u, 0)) +} + +/// What the scan holds after a unit, from what it held before. +pub open spec fn ufacts(u: Seq, fs: clazel_codecheck::spec::Facts) -> Option< + clazel_codecheck::spec::Facts, +> { + if u.len() == 1 { + clazel_codecheck::spec::plain_ok(fs, u[0]) + } else if u.len() == 3 { + Some(clazel_codecheck::spec::no_facts()) + } else if u.len() == 8 { + Some(clazel_codecheck::spec::check_facts(fs, u, 0)) + } else { + None + } +} + +/// The scan over the units from byte position `pos` with facts `fs`, where +/// `p` says which positions are jump targets (the scan forgets its facts +/// there). `None` if some unit is refused. +pub open spec fn ucheck( + us: Us, + pos: int, + fs: clazel_codecheck::spec::Facts, + p: spec_fn(int) -> bool, +) -> Option + decreases us.len(), +{ + if us.len() == 0 { + Some(fs) + } else { + let fs0 = if p(pos) { + clazel_codecheck::spec::no_facts() + } else { + fs + }; + match ufacts(us[0], fs0) { + None => None, + Some(f) => ucheck(us.skip(1), pos + code_len(us[0]) as int, f, p), + } + } +} + +/// The unit starts the scan reports. +pub open spec fn ustarts(us: Us, pos: int) -> Seq + decreases us.len(), +{ + if us.len() == 0 { + Seq::empty() + } else { + seq![pos] + ustarts(us.skip(1), pos + code_len(us[0]) as int) + } +} + +/// `x` is the start of a unit. +pub open spec fn ub(us: Us, pos: int, x: int) -> bool + decreases us.len(), +{ + us.len() > 0 && (x == pos || ub(us.skip(1), pos + code_len(us[0]) as int, x)) +} + +/// `x` is the start of a unit, or the end of the last. +pub open spec fn ubi(us: Us, pos: int, x: int) -> bool { + ub(us, pos, x) || x == pos + blen(us) +} + +// ---------------------------------------------------------------------------- +// Lists of units +// ---------------------------------------------------------------------------- + +/// The instructions of a concatenation of unit lists. +pub proof fn lemma_uflat_cat(a: Us, b: Us) + ensures + uflat(a + b) == uflat(a) + uflat(b), + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + assert(uflat(a) + uflat(b) =~= uflat(b)); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + lemma_uflat_cat(a.skip(1), b); + assert(uflat(a + b) =~= a[0] + (uflat(a.skip(1)) + uflat(b))); + assert(uflat(a) =~= a[0] + uflat(a.skip(1))); + } +} + +/// The length of a concatenation of unit lists. +pub proof fn lemma_blen_cat(a: Us, b: Us) + ensures + blen(a + b) == blen(a) + blen(b), + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + lemma_blen_cat(a.skip(1), b); + } +} + +/// The length of the units is the length of their instructions. +pub proof fn lemma_blen_flat(us: Us) + ensures + blen(us) == code_len(uflat(us)), + decreases us.len(), +{ + if us.len() > 0 { + lemma_blen_flat(us.skip(1)); + lemma_code_len_append(us[0], uflat(us.skip(1))); + } +} + +/// The instructions of a list of single-instruction units are the list. +pub proof fn lemma_uflat_ones(c: Seq) + ensures + uflat(ones(c)) =~= c, + ones(c).len() == c.len(), + forall|k: int| 0 <= k < c.len() ==> #[trigger] ones(c)[k] == seq![c[k]], + decreases c.len(), +{ + if c.len() > 0 { + assert(ones(c).skip(1) =~= ones(c.skip(1))); + lemma_uflat_ones(c.skip(1)); + assert(ones(c)[0] == seq![c[0]]); + assert(uflat(ones(c)) =~= seq![c[0]] + c.skip(1)); + } +} + +/// The length of a one-unit list. +pub proof fn lemma_blen_one(u: Seq) + ensures + blen(seq![u]) == code_len(u), +{ + assert(seq![u].skip(1) =~= Seq::>::empty()); + assert(blen(Seq::>::empty()) == 0); +} + +/// The length of a two-unit list. +pub proof fn lemma_blen_two(u: Seq, v: Seq) + ensures + blen(seq![u, v]) == code_len(u) + code_len(v), +{ + assert(seq![u, v].skip(1) =~= seq![v]); + lemma_blen_one(v); +} + +/// The length of single-instruction units. +pub proof fn lemma_blen_ones(c: Seq) + ensures + blen(ones(c)) == code_len(c), +{ + lemma_uflat_ones(c); + lemma_blen_flat(ones(c)); +} + +// ---------------------------------------------------------------------------- +// The scan over a concatenation +// ---------------------------------------------------------------------------- + +/// The scan over a concatenation is the scan over the first, then over the +/// second from where the first ended, with the facts it ended with. +pub proof fn lemma_ucheck_cat( + a: Us, + b: Us, + pos: int, + fs: clazel_codecheck::spec::Facts, + p: spec_fn(int) -> bool, +) + ensures + ucheck(a + b, pos, fs, p) == match ucheck(a, pos, fs, p) { + None => None, + Some(f) => ucheck(b, pos + blen(a), f, p), + }, + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + let fs0 = if p(pos) { + clazel_codecheck::spec::no_facts() + } else { + fs + }; + match ufacts(a[0], fs0) { + None => {}, + Some(f) => { + lemma_ucheck_cat(a.skip(1), b, pos + code_len(a[0]) as int, f, p); + }, + } + } +} + +/// The unit starts of a concatenation. +pub proof fn lemma_ub_cat(a: Us, b: Us, pos: int, x: int) + ensures + ub(a + b, pos, x) == (ub(a, pos, x) || ub(b, pos + blen(a), x)), + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + lemma_ub_cat(a.skip(1), b, pos + code_len(a[0]) as int, x); + } +} + +/// A boundary of either part is a boundary of the concatenation. +pub proof fn lemma_ubi_cat(a: Us, b: Us, pos: int, x: int) + requires + ubi(a, pos, x) || ubi(b, pos + blen(a), x), + ensures + ubi(a + b, pos, x), +{ + lemma_ub_cat(a, b, pos, x); + lemma_blen_cat(a, b); + if x == pos + blen(a) && b.len() > 0 { + // the end of `a` is the first unit start of `b` + assert(ub(b, pos + blen(a), x)); + } +} + +/// The scan reports unit starts, and `ub` is membership in them. +pub proof fn lemma_seq_has_ustarts(us: Us, pos: int, x: int) + ensures + clazel_codecheck::spec::seq_has(ustarts(us, pos), x) == ub(us, pos, x), + decreases us.len(), +{ + if us.len() > 0 { + let rest = ustarts(us.skip(1), pos + code_len(us[0]) as int); + clazel_codecheck::acc_lemmas::lemma_seq_has_concat(seq![pos], rest, x); + clazel_codecheck::acc_lemmas::lemma_seq_has_single(pos, x); + lemma_seq_has_ustarts(us.skip(1), pos + code_len(us[0]) as int, x); + } else { + reveal_with_fuel(clazel_codecheck::spec::seq_has, 1); + } +} + +// ---------------------------------------------------------------------------- +// Positions +// ---------------------------------------------------------------------------- + +/// The byte position of instruction `j + m` is that of `j` plus the length of +/// the `m` instructions between, which are `u`'s. +pub proof fn lemma_start_of_run(ins: Seq, j: int, u: Seq, m: int) + requires + 0 <= j, + 0 <= m <= u.len(), + j + m <= ins.len(), + forall|i: int| 0 <= i < m ==> ins[j + i] == #[trigger] u[i], + ensures + clazel_codecheck::spec::start_of(ins, j + m) == clazel_codecheck::spec::start_of(ins, j) + + code_len(u.take(m)), + decreases m, +{ + if m == 0 { + assert(u.take(0) =~= Seq::::empty()); + } else { + lemma_start_of_run(ins, j, u, m - 1); + assert(u.take(m) =~= u.take(m - 1) + seq![u[m - 1]]); + lemma_code_len_append(u.take(m - 1), seq![u[m - 1]]); + lemma_code_len_one(u[m - 1]); + assert(ins[j + (m - 1)] == u[m - 1]); + } +} + +/// `start_of` of a whole unit. +pub proof fn lemma_start_of_unit(ins: Seq, j: int, u: Seq) + requires + 0 <= j, + j + u.len() <= ins.len(), + forall|i: int| 0 <= i < u.len() ==> ins[j + i] == #[trigger] u[i], + ensures + clazel_codecheck::spec::start_of(ins, j + u.len()) == clazel_codecheck::spec::start_of( + ins, + j, + ) + code_len(u), +{ + lemma_start_of_run(ins, j, u, u.len() as int); + assert(u.take(u.len() as int) =~= u); +} + +/// A unit's first two instructions are neither a `ret` nor a `jcc 2`. +pub proof fn lemma_uok_ok2(u: Seq, i: int) + requires + uok(u), + 0 <= i <= 1, + i < u.len(), + ensures + ok2(u[i]), +{ + if u.len() == 8 { + let c = clazel_codecheck::spec::check_params(u, 0)->Some_0; + assert(clazel_codecheck::spec::check_unit_at(u, 0)); + assert(u[0] == clazel_codecheck::spec::check_instr(0, c)); + assert(u[1] == clazel_codecheck::spec::check_instr(1, c)); + } +} + +/// The units' lengths are 1, 3 or 8. +pub proof fn lemma_uok_len(u: Seq) + requires + uok(u), + ensures + u.len() == 1 || u.len() == 3 || u.len() == 8, +{ +} + +// ---------------------------------------------------------------------------- +// The bridge +// ---------------------------------------------------------------------------- + +/// The instruction two places on from a single-instruction unit is not a +/// `ret` or a `jcc 2` (or there are fewer than three instructions left). +proof fn lemma_lookahead(ins: Seq, j: int, us: Us) + requires + 0 <= j <= ins.len(), + ins.skip(j) =~= uflat(us), + us.len() >= 1, + us[0].len() == 1, + forall|k: int| 0 <= k < us.len() ==> uok(us[k]), + ensures + j + 2 >= ins.len() || ok2(ins[j + 2]), +{ + let rest = us.skip(1); + if rest.len() == 0 { + assert(uflat(us) =~= us[0] + uflat(rest)); + assert(uflat(rest).len() == 0); + assert(ins.skip(j).len() == 1); + } + if rest.len() >= 1 && j + 2 < ins.len() { + let u1 = rest[0]; + assert(uok(u1)); + lemma_uok_len(u1); + let r1 = rest.skip(1); + assert(uflat(rest) =~= u1 + uflat(r1)); + assert(uflat(us) =~= us[0] + uflat(rest)); + assert(uflat(us) =~= us[0] + (u1 + uflat(r1))); + if u1.len() >= 2 { + lemma_uok_ok2(u1, 1); + assert(ins[j + 2] == uflat(us)[2]); + assert(uflat(us)[2] == u1[1]); + } else { + assert(r1.len() >= 1) by { + assert(uflat(us).len() == 1 + 1 + uflat(r1).len()); + } + let u2 = r1[0]; + assert(uok(u2)); + lemma_uok_ok2(u2, 0); + assert(uflat(r1) =~= u2 + uflat(r1.skip(1))); + assert(ins[j + 2] == uflat(us)[2]); + assert(uflat(us)[2] == u2[0]); + } + } +} + +/// **The scan walks the units.** If the instructions from `j` on are those of +/// the units, every unit is well formed, `p` is membership in the scan's +/// targets, and [`ucheck`] accepts the units, then `scan` accepts them and +/// reports their starts. +pub proof fn lemma_scan_units( + ins: Seq, + tg: Seq, + j: int, + us: Us, + pos: int, + fs: clazel_codecheck::spec::Facts, + p: spec_fn(int) -> bool, +) + requires + 0 <= j <= ins.len(), + ins.skip(j) =~= uflat(us), + clazel_codecheck::spec::start_of(ins, j) == pos, + forall|k: int| 0 <= k < us.len() ==> uok(us[k]), + forall|x: int| clazel_codecheck::spec::seq_has(tg, x) == p(x), + ucheck(us, pos, fs, p) is Some, + ensures + clazel_codecheck::spec::scan(ins, tg, j, fs) == Some(ustarts(us, pos)), + decreases us.len(), +{ + if us.len() == 0 { + assert(uflat(us).len() == 0); + assert(ins.skip(j).len() == 0); + } else { + let u = us[0]; + let rest = us.skip(1); + assert(uok(u)); + lemma_uok_len(u); + assert(uflat(us) =~= u + uflat(rest)); + assert forall|i: int| 0 <= i < u.len() implies ins[j + i] == #[trigger] u[i] by { + assert(ins.skip(j)[i] == uflat(us)[i]); + } + assert(j + u.len() <= ins.len()); + let fs0 = if p(pos) { + clazel_codecheck::spec::no_facts() + } else { + fs + }; + let f = ufacts(u, fs0)->Some_0; + assert(ufacts(u, fs0) is Some); + lemma_start_of_unit(ins, j, u); + // which unit does the scan find at `j`? + if u.len() == 3 { + assert(clazel_codecheck::spec::exit_unit_at(ins, j)); + } else if u.len() == 8 { + let c = clazel_codecheck::spec::check_params(u, 0)->Some_0; + assert(clazel_codecheck::spec::check_unit_at(u, 0)); + assert(u[0] == clazel_codecheck::spec::check_instr(0, c)); + assert(ins[j] == u[0]); + assert(!clazel_codecheck::spec::exit_unit_at(ins, j)); + assert(clazel_codecheck::spec::check_params(ins, j) == clazel_codecheck::spec::check_params( + u, + 0, + )); + assert(clazel_codecheck::spec::check_unit_at(ins, j)); + assert(clazel_codecheck::spec::check_facts(fs0, ins, j) + == clazel_codecheck::spec::check_facts(fs0, u, 0)); + } else { + lemma_lookahead(ins, j, us); + assert(!clazel_codecheck::spec::exit_unit_at(ins, j)); + assert(!clazel_codecheck::spec::check_unit_at(ins, j)); + } + let ul = clazel_codecheck::spec::unit_at(ins, j, fs0)->Some_0; + assert(clazel_codecheck::spec::unit_at(ins, j, fs0) is Some); + assert(ul.len == u.len() && ul.facts == f); + assert(ins.skip(j + u.len()) =~= ins.skip(j).skip(u.len() as int)); + assert((u + uflat(rest)).skip(u.len() as int) =~= uflat(rest)); + assert(ins.skip(j + u.len()) =~= uflat(rest)); + lemma_scan_units(ins, tg, j + u.len(), rest, pos + code_len(u) as int, f, p); + } +} + +} // verus! diff --git a/crates/clazel-jit/src/cfvalid.rs b/crates/clazel-jit/src/cfvalid.rs new file mode 100644 index 000000000..d1005e8ff --- /dev/null +++ b/crates/clazel-jit/src/cfvalid.rs @@ -0,0 +1,142 @@ +//! Every instruction the control-flow templates use is in the x86-64 subset +//! ([`x64::valid_instr`]): the emitter's blob is decoded by the validator +//! only if it is. + +#[allow(unused_imports)] +use super::spec::*; +#[cfg(verus_only)] +use crate::cfspec::*; +#[cfg(verus_only)] +use crate::cftemplates::*; +#[cfg(verus_only)] +use crate::contract::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Valid code followed by valid code is valid. +pub proof fn lemma_valid_cat(a: Seq, b: Seq) + requires + all_valid_instrs(a), + all_valid_instrs(b), + ensures + all_valid_instrs(a + b), +{ + assert forall|k: int| 0 <= k < (a + b).len() implies x64::valid_instr(#[trigger] (a + b)[k]) by { + if k < a.len() { + assert((a + b)[k] == a[k]); + } else { + assert((a + b)[k] == b[k - a.len()]); + } + } +} + +/// The condition test is valid code, when its slot is in the frame. +pub proof fn lemma_cond_pre_valid(nl: nat, h: nat, cc: u8, rel: int) + requires + 1 <= h, + nl + h <= max_slots(), + cc < 16, + ensures + all_valid_instrs(cond_pre(nl, h, cc, rel)), +{ + crate::tlemmas::lemma_ls_valid(0, (nl + h - 1) as nat); + let c = cond_pre(nl, h, cc, rel); + assert forall|k: int| 0 <= k < c.len() implies x64::valid_instr(#[trigger] c[k]) by { + assert(k == 0 || k == 1 || k == 2); + } +} + +/// **The code of every templated node is in the subset.** +pub proof fn lemma_ctmpl_valid(n: Node, nl: nat, h: nat, offs: Seq) + requires + cf_flow(n, nl, h, offs.len()) is Some, + templated(n), + ensures + all_valid_instrs(ctmpl(n, nl, h, offs)), + decreases n, +{ + let d = offs.len(); + match n { + Node::Op(o) => { + crate::tlemmas::lemma_tmpl_valid(o, nl, h); + }, + Node::Mem(m) => { + crate::cfmemvalid::lemma_mem_valid(m, nl, h); + }, + Node::Then(a, b) => { + match cf_flow(*a, nl, h, d) { + Some(Flow::Falls(h1)) => { + lemma_ctmpl_valid(*a, nl, h, offs); + let ob = shift(offs, csize(*a, nl, h, d) as int); + assert(ob.len() == d); + lemma_ctmpl_valid(*b, nl, h1, ob); + lemma_valid_cat(ctmpl(*a, nl, h, offs), ctmpl(*b, nl, h1, ob)); + }, + Some(Flow::Dead) => { + lemma_ctmpl_valid(*a, nl, h, offs); + let ob = shift(offs, csize(*a, nl, h, d) as int); + assert(ctmpl(*b, nl, h, ob) =~= Seq::::empty()); + lemma_valid_cat(ctmpl(*a, nl, h, offs), Seq::empty()); + assert(ctmpl(*a, nl, h, offs) + Seq::::empty() =~= ctmpl( + *a, + nl, + h, + offs, + )); + }, + None => {}, + } + }, + Node::Block(b) => { + lemma_ctmpl_valid(*b, nl, h, seq![csize(*b, nl, h, d + 1) as int] + offs); + }, + Node::Loop(b) => { + lemma_ctmpl_valid(*b, nl, h, seq![0int] + offs); + }, + Node::If(t, e) => { + let p = cond_len(nl, h) as int; + let st = csize(*t, nl, (h - 1) as nat, d + 1) as int; + let se = csize(*e, nl, (h - 1) as nat, d + 1) as int; + let ot = seq![st + 5 + se] + shift(offs, p); + let oe = seq![se] + shift(offs, p + st + 5); + assert(ot.len() == d + 1 && oe.len() == d + 1); + lemma_ctmpl_valid(*t, nl, (h - 1) as nat, ot); + lemma_ctmpl_valid(*e, nl, (h - 1) as nat, oe); + lemma_cond_pre_valid(nl, h, 4, st + 5); + let cp = cond_pre(nl, h, 4, st + 5); + let ct = ctmpl(*t, nl, (h - 1) as nat, ot); + let ce = ctmpl(*e, nl, (h - 1) as nat, oe); + let jm = seq![jmp_i(se)]; + assert(all_valid_instrs(jm)); + lemma_valid_cat(cp, ct); + lemma_valid_cat(cp + ct, jm); + lemma_valid_cat(cp + ct + jm, ce); + }, + Node::Br(l) => { + let c = ctmpl(n, nl, h, offs); + assert(all_valid_instrs(c)); + }, + Node::BrIf(l) => { + lemma_cond_pre_valid( + nl, + h, + 5, + if l < offs.len() { + offs[l as int] - cond_len(nl, h) as int + } else { + 0 + }, + ); + }, + Node::BrTable(ls, d) => { + crate::cfmemvalid::lemma_brt_valid(ls@, d, nl, h, offs); + }, + Node::Exit(c, _) => { + crate::tlemmas::lemma_exit_valid(c); + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/compose.rs b/crates/clazel-jit/src/compose.rs new file mode 100644 index 000000000..bdeacc951 --- /dev/null +++ b/crates/clazel-jit/src/compose.rs @@ -0,0 +1,152 @@ +//! **Composition.** The templates of a straight-line program, one after the +//! other, refine the program: [`lemma_prog_refines`] is the induction over +//! [`prog_instr`] (the left-nested `then` chain) that turns the per-template +//! refinements of [`crate::proofs`] into one for the whole program. +//! +//! This is composition over *sequencing* only. Structured control flow +//! (`block`, `loop`, `if`, `br`) needs jump templates, a relation at labels, +//! and a proof about the validator's targets rule; it is not in this slice. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// [`preserved`] is transitive. +pub proof fn lemma_preserved_trans(m: x64::State, m1: x64::State, m2: x64::State) + requires + preserved(m, m1), + preserved(m1, m2), + ensures + preserved(m, m2), +{ + lemma_abi_consts_c(); + assert(ctx_of(m1) == ctx_of(m)); + assert forall|b: int| #[trigger] m.mem.dom().contains(b) && !in_exit_area(ctx_of(m), b) + implies m2.mem[b] == m.mem[b] by { + assert(m1.mem.dom().contains(b)); + assert(m1.mem[b] == m.mem[b]); + assert(m2.mem[b] == m1.mem[b]); + } +} + +/// The constants the lemma above needs (`ctx_reg`, `sp_reg`). +pub proof fn lemma_abi_consts_c() + ensures + abi::ctx_reg() == 15, + abi::sp_reg() == 4, +{ +} + +/// A program's stack height after it is the one the static analysis computes. +pub proof fn lemma_prog_height( + ops: Seq, + nl: nat, + h0: nat, + lay: lir::Layout, + a: lir::St, + a2: lir::St, + fuel: nat, +) + requires + prog_ok(ops, nl, h0), + a.stack.len() == h0, + lir::eval(lay, prog_instr(ops), a, fuel) == lir::Outcome::Normal(a2), + ensures + a2.stack.len() == height_after(ops, h0), + decreases ops.len(), +{ + if ops.len() == 0 { + assert(a2 == a); + } else { + let pre = ops.drop_last(); + let last = ops.last(); + assert(fuel > 0); + match lir::eval(lay, prog_instr(pre), a, (fuel - 1) as nat) { + lir::Outcome::Normal(a1) => { + lemma_prog_height(pre, nl, h0, lay, a, a1, (fuel - 1) as nat); + super::proofs::lemma_op_height(last, nl, height_after(pre, h0), lay, a1, a2, (fuel - 1) as nat); + }, + _ => { + assert(false); + }, + } + } +} + +/// **A straight-line program's code refines it.** The templates of the +/// instructions of `ops`, each at the height the ones before it leave, run +/// in order from a state related to the start and reach one related to the +/// end of LIR's evaluation of [`prog_instr`]. +#[verifier::rlimit(500)] +pub proof fn lemma_prog_refines(ops: Seq, nl: nat, h0: nat) + requires + prog_ok(ops, nl, h0), + ensures + super::contract::refines(prog_instr(ops), nl, h0, super::templates::code_of(ops, nl, h0)), + decreases ops.len(), +{ + if ops.len() == 0 { + super::proofs::lemma_op_refines(Op::Nop, nl, h0); + assert(prog_instr(ops) == Op::Nop@); + assert(super::templates::code_of(ops, nl, h0) == super::templates::tmpl(Op::Nop, nl, h0)); + } else { + let pre = ops.drop_last(); + let last = ops.last(); + let hl = height_after(pre, h0); + let ca = super::templates::code_of(pre, nl, h0); + let cb = super::templates::tmpl(last, nl, hl); + lemma_prog_refines(pre, nl, h0); + super::proofs::lemma_op_refines(last, nl, hl); + super::runseq::lemma_code_len_append(ca, cb); + assert(super::templates::code_of(ops, nl, h0) == ca + cb); + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger lir::eval(lay, prog_instr(ops), a, fuel), run_seq(cu, m, ca + cb)] + a.stack.len() == h0 && rel(lay, a, m, nl) && x64::canonical( + m.rip + code_len(ca + cb), + ) implies match lir::eval(lay, prog_instr(ops), a, fuel) { + lir::Outcome::Normal(a2) => super::contract::refines_post(lay, a2, m, nl, ca + cb, run_seq(cu, m, ca + cb)), + _ => true, + } by { + match lir::eval(lay, prog_instr(ops), a, fuel) { + lir::Outcome::Normal(a2) => { + assert(fuel > 0); + let f = (fuel - 1) as nat; + // prog_instr(ops) = then(prog_instr(pre), last@) + match lir::eval(lay, prog_instr(pre), a, f) { + lir::Outcome::Normal(a1) => { + assert(lir::eval(lay, last@, a1, f) == lir::Outcome::Normal(a2)); + assert(x64::wf(m)); + assert(x64::canonical(m.rip + code_len(ca))) by { + super::runseq::lemma_canonical_between(m.rip, m.rip + code_len(ca), m.rip + code_len(ca + cb)); + } + super::proofs::lemma_use_refines(prog_instr(pre), nl, h0, ca, lay, cu, a, m, f); + let m1 = run_seq(cu, m, ca).unwrap(); + lemma_prog_height(pre, nl, h0, lay, a, a1, f); + assert(a1.stack.len() == hl); + assert(x64::canonical(m1.rip + code_len(cb))); + super::proofs::lemma_use_refines(last@, nl, hl, cb, lay, cu, a1, m1, f); + let m2 = run_seq(cu, m1, cb).unwrap(); + super::runseq::lemma_run_seq_append(cu, m, ca, cb); + lemma_preserved_trans(m, m1, m2); + }, + _ => { + assert(false); + }, + } + }, + _ => {}, + } + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/contract.rs b/crates/clazel-jit/src/contract.rs new file mode 100644 index 000000000..10357a695 --- /dev/null +++ b/crates/clazel-jit/src/contract.rs @@ -0,0 +1,178 @@ +//! What the back end must be proven to satisfy (CLAUDE.md rule 3, +//! docs/SPEC-STYLE.md): the refinement statements, and one `requires` and one +//! `ensures` predicate for each method of [`super::api::Backend`], in terms of +//! [`super::spec`] and the two semantics it relates. These are written before +//! any template (the templates are the next commit); reviewing them and +//! `spec` is enough to know what is claimed. +//! +//! # The refinement statement +//! +//! [`refines`]`(i, nl, h, code)` says that the x86-64 instruction list `code` +//! is a correct translation of the LIR instruction `i`, for a function with +//! `nl` locals, when the operand stack has `h` values. For every layout and +//! every `create_undef` (the machine's unspecified function for undefined +//! flags, so the statement holds for all of them), every LIR state `a` and +//! machine state `m` related by [`rel`]: +//! +//! - if LIR evaluates `i` in `a` (with any fuel) and **falls through** to the +//! state `a2` (a `Stuck` or `OutOfFuel` outcome is not claimed about, and +//! `i` is not required to be well typed: if it is not, LIR is `Stuck`, and +//! the statement says nothing), +//! - and the end of the code is a canonical address, +//! - then running `code` from `m` ([`run_seq`]) does not fault, ends in a +//! machine state `m2` related to `a2` by [`rel`], with `rip` after the code, +//! and `m2` differs from `m` only in the exit area ([`preserved`]). +//! +//! That is a **forward simulation of one LIR step by one template**, for the +//! LIR semantics that is deterministic. Soundness of the whole translation +//! (every execution of the machine code is the image of a LIR execution) needs +//! the converse for the exits, branches and `Stuck`: the exit is +//! [`exit_refines`]; the others are for the control-flow templates. A +//! straight-line program is one [`refines`] over [`prog_instr`]. +//! +//! # What is not stated +//! +//! - That the output is **accepted by the validator.** jit.md requires it and +//! `tests/validator.rs` checks it for every template and generated +//! programs, by running the executable validator. It is not a theorem here. +//! - That a *failed* LIR step (a bounds-check exit, `Stuck`) is matched by the +//! machine: no template in this slice has one. +//! - Anything about [`rel`]'s `rip`, flags or scratch registers. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// What the machine must be in after `code` ran from `m`, for the LIR state +/// `a2` that `i` reached: no fault, related, `rip` after the code, and +/// nothing outside the exit area changed. +pub open spec fn refines_post( + lay: lir::Layout, + a2: lir::St, + m: x64::State, + nl: nat, + code: Seq, + r: Option, +) -> bool { + match r { + Some(m2) => rel(lay, a2, m2, nl) && m2.rip == m.rip + code_len(code) && preserved(m, m2), + None => false, + } +} + +/// `code` refines the LIR instruction `i` at stack height `h` with `nl` +/// locals (see the module doc). +pub open spec fn refines(i: lir::Instr, nl: nat, h: nat, code: Seq) -> bool { + forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger lir::eval(lay, i, a, fuel), run_seq(cu, m, code)] + a.stack.len() == h && rel(lay, a, m, nl) && x64::canonical(m.rip + code_len(code)) + ==> match lir::eval(lay, i, a, fuel) { + lir::Outcome::Normal(a2) => refines_post(lay, a2, m, nl, code, run_seq(cu, m, code)), + _ => true, + } +} + +/// The exit sequence for `c` refines LIR's `exit c` with no operands, given +/// the stack the enter stub leaves ([`stack_ok`]): running the three +/// instructions does not fault; the machine ends at the return address the +/// stub pushed, with the exit code `c` in the context's exit-code word, the +/// frame and the regions as they were ([`rel`] still holds, and the +/// memory outside the exit area is unchanged), and `rsp` popped by the +/// `ret`. +pub open spec fn exit_refines(c: u32, nl: nat) -> bool { + forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, fuel), run_seq(cu, m, exit_seq(c))] + rel(lay, a, m, nl) && stack_ok(m) && x64::canonical(m.rip + code_len(exit_seq(c))) + ==> match lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, fuel) { + lir::Outcome::Exit(code, _, a2) => match run_seq(cu, m, exit_seq(c)) { + Some(m2) => { + let rsp = x64::logext(64, m.rgf[abi::sp_reg() as int] as int); + rel(lay, a2, m2, nl) && preserved_mem(m, m2) && x64::read_mem( + m2.mem, + ctx_of(m) + abi::exit_code_off(), + 8, + ) == Some(code as int) && m2.rgf[abi::sp_reg() as int] as int == x64::loghead( + 64, + rsp + 8, + ) && match x64::read_mem(m.mem, rsp, 8) { + Some(raw) => m2.rip == x64::logext(64, raw), + None => false, + } + }, + None => false, + }, + _ => true, + } +} + +// ---------------------------------------------------------------------------- +// The emitter's contracts (the executable back end, `api::Backend`) +// ---------------------------------------------------------------------------- + +/// The instructions of `code` are all in the x86-64 subset +/// ([`x64::valid_instr`]). +pub open spec fn all_valid_instrs(code: Seq) -> bool { + forall|j: int| 0 <= j < code.len() ==> x64::valid_instr(#[trigger] code[j]) +} + +/// Precondition of `emit_body` and `emit_blob`: none. Every program and every +/// local count is a legal input (one outside the first slice, or too large +/// for the frame, is answered `None`). +pub open spec fn emit_requires(ops: Seq, nl: usize) -> bool { + true +} + +/// Postcondition of `emit_body`: the program has a translation exactly when +/// [`prog_ok`] says every instruction has a template; the translation is in +/// the subset and refines the program's LIR instruction. +pub open spec fn emit_body_ensures( + ops: Seq, + nl: usize, + r: Option>, +) -> bool { + match r { + Some(code) => prog_ok(ops, nl as nat, 0) && all_valid_instrs(code) && refines( + prog_instr(ops), + nl as nat, + 0, + code, + ), + None => !prog_ok(ops, nl as nat, 0), + } +} + +/// Postcondition of `emit_blob`: the bytes are the canonical encodings +/// ([`clazel_codecheck::spec::enc_all`], which is what the validator reads +/// back) of a body that refines the program, followed by the exit sequence +/// for `exit_code`. `None` exactly when the program has no translation. +pub open spec fn emit_blob_ensures( + ops: Seq, + nl: usize, + exit_code: u32, + r: Option>, +) -> bool { + match r { + Some(bytes) => prog_ok(ops, nl as nat, 0) && exists|body: Seq| + #![trigger all_valid_instrs(body)] + all_valid_instrs(body) && refines(prog_instr(ops), nl as nat, 0, body) && bytes + == clazel_codecheck::spec::enc_all(body + exit_seq(exit_code)), + None => !prog_ok(ops, nl as nat, 0), + } +} + +} // verus! diff --git a/crates/clazel-jit/src/emit.rs b/crates/clazel-jit/src/emit.rs new file mode 100644 index 000000000..54e4fef25 --- /dev/null +++ b/crates/clazel-jit/src/emit.rs @@ -0,0 +1,786 @@ +//! The executable back end: [`X64Backend`] implements [`Backend`]. It +//! pushes, for each instruction of the program, exactly the instructions of +//! its template ([`tmpl`]), at the stack height the instructions before it +//! leave, and encodes them with `clazel-x64`'s proven encoder. Each function +//! is proven equal to the spec it implements, so the refinement proofs +//! ([`crate::compose::lemma_prog_refines`]) are about the bytes it returns. + +use super::api::Backend; +#[allow(unused_imports)] +use super::spec::*; +use clazel_x64::encoder::encode_exec; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// ---------------------------------------------------------------------------- +// Checked arithmetic (the workspace denies the unchecked operators) +// ---------------------------------------------------------------------------- + +/// `a + b`, when it does not overflow. +fn add_u(a: usize, b: usize) -> (r: usize) + requires + a + b <= usize::MAX, + ensures + r == a + b, +{ + a.saturating_add(b) +} + +/// `a - b`, when it does not underflow. +fn sub_u(a: usize, b: usize) -> (r: usize) + requires + b <= a, + ensures + r == a - b, +{ + a.saturating_sub(b) +} + + +// ---------------------------------------------------------------------------- +// Instructions +// ---------------------------------------------------------------------------- + +/// The displacement of frame slot `j` from the context register. +fn slot_disp_exec(j: usize) -> (d: i32) + requires + j < 31, + ensures + d as int == slot_disp(j as nat), + slot_mem(j as nat).disp == d, +{ + proof { + super::tlemmas::lemma_abi_consts_t(); + super::steps::lemma_abi_consts(); + } + let d = j.saturating_mul(8).saturating_add(264); + d as i32 +} + +/// `mov dst, [r15 + 264 + 8 j]`. +pub(crate) fn ex_load(dst: u8, j: usize) -> (i: x64::Instr) + requires + j < 31, + ensures + i == super::templates::load_slot(dst, j as nat), +{ + proof { + super::tlemmas::lemma_abi_consts_t(); + crate::steps::lemma_abi_consts(); + } + x64::Instr::MovLoad { + sz: x64::Size::W64, + dst, + mem: x64::Mem { base: 15, index: None, disp: slot_disp_exec(j) }, + } +} + +/// `mov [r15 + 264 + 8 j], src`. +pub(crate) fn ex_store(j: usize, src: u8) -> (i: x64::Instr) + requires + j < 31, + ensures + i == super::templates::store_slot(j as nat, src), +{ + proof { + super::tlemmas::lemma_abi_consts_t(); + crate::steps::lemma_abi_consts(); + } + x64::Instr::MovStore { + sz: x64::Size::W64, + mem: x64::Mem { base: 15, index: None, disp: slot_disp_exec(j) }, + src, + } +} + +/// The instruction that loads a constant into `rax`. +fn ex_const(v: &lir::Val) -> (i: x64::Instr) + ensures + i == super::templates::const_instr(*v), +{ + match v { + lir::Val::I32(c) => x64::Instr::MovImm32 { dst: 0, imm_u32: *c }, + lir::Val::I64(c) => x64::Instr::MovImm64 { dst: 0, imm_u64: *c }, + lir::Val::F64(c) => x64::Instr::MovImm64 { dst: 0, imm_u64: *c }, + } +} + +/// The condition code of an integer comparison. +fn cc_exec(c: &lir::ICmp) -> (r: u8) + ensures + r == super::templates::cc_of(*c), +{ + match c { + lir::ICmp::Eq => 4, + lir::ICmp::Ne => 5, + lir::ICmp::LtS => 12, + lir::ICmp::LtU => 2, + lir::ICmp::GtS => 15, + lir::ICmp::GtU => 7, + lir::ICmp::LeS => 14, + lir::ICmp::LeU => 6, + lir::ICmp::GeS => 13, + lir::ICmp::GeU => 3, + } +} + +/// The instruction of an integer operator on `rax` and `rcx`. +fn int_op_exec(o: &lir::IBin, sz: x64::Size) -> (i: x64::Instr) + ensures + i == super::templates::int_op_instr(*o, sz), +{ + match o { + lir::IBin::Add => x64::Instr::AluRR { alu: x64::AluOp::Add, sz, dst: 0, src: 1 }, + lir::IBin::Sub => x64::Instr::AluRR { alu: x64::AluOp::Sub, sz, dst: 0, src: 1 }, + lir::IBin::And => x64::Instr::AluRR { alu: x64::AluOp::And, sz, dst: 0, src: 1 }, + lir::IBin::Or => x64::Instr::AluRR { alu: x64::AluOp::Or, sz, dst: 0, src: 1 }, + lir::IBin::Xor => x64::Instr::AluRR { alu: x64::AluOp::Xor, sz, dst: 0, src: 1 }, + lir::IBin::Shl => x64::Instr::ShiftCl { shift: x64::ShiftOp::Shl, sz, r: 0 }, + lir::IBin::ShrU => x64::Instr::ShiftCl { shift: x64::ShiftOp::Shr, sz, r: 0 }, + lir::IBin::ShrS => x64::Instr::ShiftCl { shift: x64::ShiftOp::Sar, sz, r: 0 }, + lir::IBin::Mul => x64::Instr::ImulRR { sz, dst: 0, src: 1 }, + lir::IBin::Rotl => x64::Instr::ImulRR { sz, dst: 0, src: 1 }, + lir::IBin::Rotr => x64::Instr::ImulRR { sz, dst: 0, src: 1 }, + } +} + +// ---------------------------------------------------------------------------- +// Which instructions have templates +// ---------------------------------------------------------------------------- + +/// `ibin_supported`. +fn ibin_supported_exec(o: &lir::IBin) -> (r: bool) + ensures + r == ibin_supported(*o), +{ + !matches!(o, lir::IBin::Rotl | lir::IBin::Rotr) +} + +/// `bin_supported`. +fn bin_supported_exec(b: &lir::BinOp) -> (r: bool) + ensures + r == bin_supported(*b), +{ + match b { + lir::BinOp::I32(o) => ibin_supported_exec(o), + lir::BinOp::I64(o) => ibin_supported_exec(o), + lir::BinOp::I32Cmp(_) => true, + lir::BinOp::I64Cmp(_) => true, + lir::BinOp::F64(_) => false, + lir::BinOp::F64Cmp(_) => false, + } +} + +/// `un_supported`. +fn un_supported_exec(u: &lir::UnOp) -> (r: bool) + ensures + r == un_supported(*u), +{ + match u { + lir::UnOp::I32Eqz => true, + lir::UnOp::I64Eqz => true, + lir::UnOp::F64Neg => true, + lir::UnOp::F64Abs => true, + lir::UnOp::F64Sqrt => false, + lir::UnOp::I32WrapI64 => true, + lir::UnOp::I64ExtendI32S => true, + lir::UnOp::I64ExtendI32U => true, + lir::UnOp::F64ConvertI32S => false, + lir::UnOp::F64ConvertI32U => false, + lir::UnOp::F64ConvertI64S => false, + lir::UnOp::F64ConvertI64U => false, + lir::UnOp::F64ReinterpretI64 => true, + lir::UnOp::I64ReinterpretF64 => true, + } +} + +/// `pops`. +pub(crate) fn pops_exec(op: &Op) -> (r: usize) + ensures + r == pops(*op), +{ + match op { + Op::Nop => 0, + Op::Const(_) => 0, + Op::LocalGet(_) => 0, + Op::LocalSet(_) => 1, + Op::LocalTee(_) => 1, + Op::Drop => 1, + Op::Select => 3, + Op::Bin(_) => 2, + Op::Un(_) => 1, + } +} + +/// `pushes`. +pub(crate) fn pushes_exec(op: &Op) -> (r: usize) + ensures + r == pushes(*op), +{ + match op { + Op::Nop => 0, + Op::Const(_) => 1, + Op::LocalGet(_) => 1, + Op::LocalSet(_) => 0, + Op::LocalTee(_) => 1, + Op::Drop => 0, + Op::Select => 1, + Op::Bin(_) => 1, + Op::Un(_) => 1, + } +} + +/// [`op_ok`], executable. +pub(crate) fn op_ok_exec(op: &Op, nl: usize, h: usize) -> (r: bool) + ensures + r == op_ok(*op, nl as nat, h as nat), +{ + proof { + super::tlemmas::lemma_abi_consts_t(); + } + if nl > 31 || h > sub_u(31, nl) { + return false; + } + let p = pops_exec(op); + let q = pushes_exec(op); + if p > h { + return false; + } + let h2 = add_u(sub_u(h, p), q); + if h2 > sub_u(31, nl) { + return false; + } + match op { + Op::LocalGet(x) => *x < nl, + Op::LocalSet(x) => *x < nl, + Op::LocalTee(x) => *x < nl, + Op::Bin(b) => bin_supported_exec(b), + Op::Un(u) => un_supported_exec(u), + _ => true, + } +} + +// ---------------------------------------------------------------------------- +// Templates +// ---------------------------------------------------------------------------- + +/// Append the template of `op` at height `h` with `nl` locals. +#[verifier::rlimit(500)] +pub(crate) fn emit_op(op: &Op, nl: usize, h: usize, out: &mut Vec) + requires + op_ok(*op, nl as nat, h as nat), + ensures + final(out)@ == old(out)@ + super::templates::tmpl(*op, nl as nat, h as nat), +{ + proof { + super::tlemmas::lemma_abi_consts_t(); + } + let ghost old_out = out@; + match op { + Op::Nop => { + proof { + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::Drop => { + proof { + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::Const(v) => { + let c = ex_const(v); + out.push(c); + out.push(ex_store(add_u(nl, h), 0)); + proof { + super::tlemmas::lemma_shape0(seq![super::templates::const_instr(*v)], (nl + h) as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::LocalGet(x) => { + out.push(ex_load(0, *x)); + out.push(ex_store(add_u(nl, h), 0)); + proof { + super::tlemmas::lemma_shape1(rax(), *x as nat, seq![], (nl + h) as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::LocalSet(x) => { + out.push(ex_load(0, sub_u(add_u(nl, h), 1))); + out.push(ex_store(*x, 0)); + proof { + super::tlemmas::lemma_shape1(rax(), (nl + h - 1) as nat, seq![], *x as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::LocalTee(x) => { + out.push(ex_load(0, sub_u(add_u(nl, h), 1))); + out.push(ex_store(*x, 0)); + proof { + super::tlemmas::lemma_shape1(rax(), (nl + h - 1) as nat, seq![], *x as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::Select => { + let t = sub_u(add_u(nl, h), 3); + out.push(ex_load(0, t)); + out.push(ex_load(1, add_u(t, 1))); + out.push(ex_load(2, add_u(t, 2))); + out.push(x64::Instr::TestRR { sz: x64::Size::W32, a: 2, b: 2 }); + out.push(x64::Instr::Cmovcc { cc: 4, sz: x64::Size::W64, dst: 0, src: 1 }); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape3( + rax(), + t as nat, + rcx(), + (t + 1) as nat, + rdx(), + (t + 2) as nat, + super::templates::select_mid(), + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + Op::Bin(b) => { + let t = sub_u(add_u(nl, h), 2); + match b { + lir::BinOp::I32(o) => { + out.push(ex_load(0, t)); + out.push(ex_load(1, add_u(t, 1))); + out.push(int_op_exec(o, x64::Size::W32)); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape2( + rax(), + t as nat, + rcx(), + (t + 1) as nat, + seq![super::templates::int_op_instr(*o, x64::Size::W32)], + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::BinOp::I64(o) => { + out.push(ex_load(0, t)); + out.push(ex_load(1, add_u(t, 1))); + out.push(int_op_exec(o, x64::Size::W64)); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape2( + rax(), + t as nat, + rcx(), + (t + 1) as nat, + seq![super::templates::int_op_instr(*o, x64::Size::W64)], + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::BinOp::I32Cmp(c) => { + out.push(ex_load(1, t)); + out.push(ex_load(2, add_u(t, 1))); + out.push(x64::Instr::MovImm32 { dst: 0, imm_u32: 0 }); + out.push( + x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: x64::Size::W32, dst: 1, src: 2 }, + ); + out.push(x64::Instr::Setcc { cc: cc_exec(c), dst: 0 }); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape2( + rcx(), + t as nat, + rdx(), + (t + 1) as nat, + super::templates::cmp_mid(*c, x64::Size::W32), + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::BinOp::I64Cmp(c) => { + out.push(ex_load(1, t)); + out.push(ex_load(2, add_u(t, 1))); + out.push(x64::Instr::MovImm32 { dst: 0, imm_u32: 0 }); + out.push( + x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: x64::Size::W64, dst: 1, src: 2 }, + ); + out.push(x64::Instr::Setcc { cc: cc_exec(c), dst: 0 }); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape2( + rcx(), + t as nat, + rdx(), + (t + 1) as nat, + super::templates::cmp_mid(*c, x64::Size::W64), + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::BinOp::F64(_) => { + proof { + assert(false); + } + }, + lir::BinOp::F64Cmp(_) => { + proof { + assert(false); + } + }, + } + }, + Op::Un(u) => { + let t = sub_u(add_u(nl, h), 1); + match u { + lir::UnOp::I32Eqz => { + out.push(ex_load(1, t)); + out.push(x64::Instr::MovImm32 { dst: 0, imm_u32: 0 }); + out.push(x64::Instr::TestRR { sz: x64::Size::W32, a: 1, b: 1 }); + out.push(x64::Instr::Setcc { cc: 4, dst: 0 }); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1(rcx(), t as nat, super::templates::eqz_mid(x64::Size::W32), t as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::I64Eqz => { + out.push(ex_load(1, t)); + out.push(x64::Instr::MovImm32 { dst: 0, imm_u32: 0 }); + out.push(x64::Instr::TestRR { sz: x64::Size::W64, a: 1, b: 1 }); + out.push(x64::Instr::Setcc { cc: 4, dst: 0 }); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1(rcx(), t as nat, super::templates::eqz_mid(x64::Size::W64), t as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::F64Neg => { + out.push(ex_load(0, t)); + out.push(x64::Instr::MovImm64 { dst: 1, imm_u64: 0x8000_0000_0000_0000 }); + out.push( + x64::Instr::AluRR { alu: x64::AluOp::Xor, sz: x64::Size::W64, dst: 0, src: 1 }, + ); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1( + rax(), + t as nat, + super::templates::mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000), + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::F64Abs => { + out.push(ex_load(0, t)); + out.push(x64::Instr::MovImm64 { dst: 1, imm_u64: 0x7FFF_FFFF_FFFF_FFFF }); + out.push( + x64::Instr::AluRR { alu: x64::AluOp::And, sz: x64::Size::W64, dst: 0, src: 1 }, + ); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1( + rax(), + t as nat, + super::templates::mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF), + t as nat, + ); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::I64ExtendI32S => { + out.push(ex_load(0, t)); + out.push(x64::Instr::MovImm32 { dst: 1, imm_u32: 0x8000_0000 }); + out.push( + x64::Instr::AluRR { alu: x64::AluOp::Xor, sz: x64::Size::W64, dst: 0, src: 1 }, + ); + out.push( + x64::Instr::AluRR { alu: x64::AluOp::Sub, sz: x64::Size::W64, dst: 0, src: 1 }, + ); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1(rax(), t as nat, super::templates::exts_mid(), t as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::I32WrapI64 => { + out.push(ex_load(0, t)); + out.push(x64::Instr::MovRR { sz: x64::Size::W32, dst: 0, src: 0 }); + out.push(ex_store(t, 0)); + proof { + super::tlemmas::lemma_shape1(rax(), t as nat, super::templates::wrap_mid(), t as nat); + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::I64ExtendI32U => { + proof { + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::F64ReinterpretI64 => { + proof { + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::I64ReinterpretF64 => { + proof { + assert(out@ =~= old_out + super::templates::tmpl(*op, nl as nat, h as nat)); + } + }, + lir::UnOp::F64Sqrt => { + proof { + assert(false); + } + }, + lir::UnOp::F64ConvertI32S => { + proof { + assert(false); + } + }, + lir::UnOp::F64ConvertI32U => { + proof { + assert(false); + } + }, + lir::UnOp::F64ConvertI64S => { + proof { + assert(false); + } + }, + lir::UnOp::F64ConvertI64U => { + proof { + assert(false); + } + }, + } + }, + } +} + +// ---------------------------------------------------------------------------- +// Programs +// ---------------------------------------------------------------------------- + +/// `prog_ok` of a prefix. +pub proof fn lemma_prog_ok_take(ops: Seq, k: int, nl: nat, h0: nat) + requires + prog_ok(ops, nl, h0), + 0 <= k <= ops.len(), + ensures + prog_ok(ops.take(k), nl, h0), + decreases ops.len(), +{ + if k == ops.len() { + assert(ops.take(k) =~= ops); + } else { + let pre = ops.drop_last(); + lemma_prog_ok_take(pre, k, nl, h0); + assert(ops.take(k) =~= pre.take(k)); + } +} + +/// The instruction at `k` has a template at the height before it. +pub proof fn lemma_prog_ok_op(ops: Seq, k: int, nl: nat, h0: nat) + requires + prog_ok(ops, nl, h0), + 0 <= k < ops.len(), + ensures + op_ok(ops[k], nl, height_after(ops.take(k), h0)), +{ + lemma_prog_ok_take(ops, k + 1, nl, h0); + let t = ops.take(k + 1); + assert(t.drop_last() =~= ops.take(k)); + assert(t.last() == ops[k]); +} + +/// [`Backend::emit_body`], and the proof that the code refines. +fn emit_body_impl(ops: &[Op], nl: usize) -> (r: Option>) + ensures + super::contract::emit_body_ensures(ops@, nl, match r { + Some(v) => Some(v@), + None => None, + }), +{ + if nl > 31 { + proof { + super::tlemmas::lemma_abi_consts_t(); + if prog_ok(ops@, nl as nat, 0) { + lemma_prog_ok_take(ops@, 0, nl as nat, 0); + assert(ops@.take(0) =~= Seq::::empty()); + } + } + return None; + } + let mut out: Vec = Vec::new(); + let mut h: usize = 0; + let mut i: usize = 0; + proof { + assert(ops@.take(0) =~= Seq::::empty()); + super::tlemmas::lemma_abi_consts_t(); + } + while i < ops.len() + invariant + i <= ops.len(), + nl <= 31, + prog_ok(ops@.take(i as int), nl as nat, 0), + h as nat == height_after(ops@.take(i as int), 0), + out@ == super::templates::code_of(ops@.take(i as int), nl as nat, 0), + decreases ops.len() - i, + { + proof { + super::tlemmas::lemma_abi_consts_t(); + } + let op = match ops.get(i) { + Some(op) => op, + None => { + proof { + assert(false); + } + return None; + }, + }; + let ok = op_ok_exec(op, nl, h); + if !ok { + proof { + if prog_ok(ops@, nl as nat, 0) { + lemma_prog_ok_op(ops@, i as int, nl as nat, 0); + } + } + return None; + } + emit_op(op, nl, h, &mut out); + proof { + let t = ops@.take(i as int + 1); + assert(t.drop_last() =~= ops@.take(i as int)); + assert(t.last() == ops@[i as int]); + assert(super::templates::code_of(t, nl as nat, 0) == super::templates::code_of(ops@.take(i as int), nl as nat, 0) + super::templates::tmpl( + ops@[i as int], + nl as nat, + height_after(ops@.take(i as int), 0), + )); + } + let p = pops_exec(op); + let q = pushes_exec(op); + proof { + assert(op == &ops@[i as int]); + } + h = add_u(sub_u(h, p), q); + i = add_u(i, 1); + } + proof { + assert(ops@.take(ops.len() as int) =~= ops@); + super::compose::lemma_prog_refines(ops@, nl as nat, 0); + super::tlemmas::lemma_code_of_valid(ops@, nl as nat, 0); + } + Some(out) +} + +/// The exit sequence appended to `code`. +pub(crate) fn emit_exit(code: &mut Vec, exit_code: u32) + ensures + final(code)@ == old(code)@ + exit_seq(exit_code), +{ + let ghost old_code = code@; + code.push(x64::Instr::MovImm32 { dst: 0, imm_u32: exit_code }); + code.push( + x64::Instr::MovStore { + sz: x64::Size::W64, + mem: x64::Mem { base: 15, index: None, disp: 256 }, + src: 0, + }, + ); + code.push(x64::Instr::Ret); + proof { + crate::steps::lemma_abi_consts(); + assert(code@ =~= old_code + exit_seq(exit_code)); + } +} + +/// [`Backend::emit_blob`]: the code and the exit sequence, encoded. +#[verifier::rlimit(500)] +fn emit_blob_impl(ops: &[Op], nl: usize, exit_code: u32) -> (r: Option>) + ensures + super::contract::emit_blob_ensures(ops@, nl, exit_code, match r { + Some(v) => Some(v@), + None => None, + }), +{ + match emit_body_impl(ops, nl) { + None => None, + Some(body) => { + let ghost body_seq = body@; + let mut code = body; + emit_exit(&mut code, exit_code); + proof { + super::tlemmas::lemma_exit_valid(exit_code); + assert(super::contract::all_valid_instrs(body_seq)); + assert forall|k: int| 0 <= k < code@.len() implies x64::valid_instr( + #[trigger] code@[k], + ) by { + if k < body_seq.len() { + assert(code@[k] == body_seq[k]); + } else { + assert(code@[k] == exit_seq(exit_code)[k - body_seq.len()]); + } + } + } + let mut bytes: Vec = Vec::new(); + let mut k: usize = 0; + proof { + assert(code@.take(0) =~= Seq::::empty()); + } + while k < code.len() + invariant + k <= code.len(), + bytes@ == clazel_codecheck::spec::enc_all(code@.take(k as int)), + forall|j: int| 0 <= j < code@.len() ==> x64::valid_instr(#[trigger] code@[j]), + decreases code.len() - k, + { + let ins = match code.get(k) { + Some(ins) => ins, + None => { + proof { + assert(false); + } + return None; + }, + }; + let e = encode_exec(ins); + bytes.extend_from_slice(e.as_slice()); + proof { + let t = code@.take(k as int + 1); + assert(t.drop_last() =~= code@.take(k as int)); + assert(t.last() == code@[k as int]); + assert(e@ =~= x64::encode(code@[k as int])); + } + k = add_u(k, 1); + } + proof { + assert(code@.take(code@.len() as int) =~= code@); + assert(code@ == body_seq + exit_seq(exit_code)); + } + Some(bytes) + }, + } +} + +/// The x86-64 back end. +pub struct X64Backend; + +impl Backend for X64Backend { + fn emit_body(ops: &[Op], nl: usize) -> (r: Option>) { + emit_body_impl(ops, nl) + } + + fn emit_blob(ops: &[Op], nl: usize, exit_code: u32) -> (r: Option>) { + emit_blob_impl(ops, nl, exit_code) + } +} + +} // verus! diff --git a/crates/clazel-jit/src/exit.rs b/crates/clazel-jit/src/exit.rs new file mode 100644 index 000000000..228802925 --- /dev/null +++ b/crates/clazel-jit/src/exit.rs @@ -0,0 +1,218 @@ +//! The exit sequence's refinement ([`exit_refines`]): `mov eax, c; mov [r15 + +//! 256], rax; ret`, for LIR's `exit c` with no operands. The first two +//! instructions put the code in the context's exit-code word; the `ret` pops +//! the return address the enter stub pushed. The frame, the regions and the +//! code are untouched. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The memory operand of the exit-code word: `[r15 + 256]`. +pub open spec fn exit_mem() -> x64::Mem { + x64::Mem { base: abi::ctx_reg(), index: None, disp: abi::exit_code_off() as i32 } +} + +/// `mov [r15 + 256], src` writes the exit-code word: it reads back as `src`'s +/// value, every byte outside those 8 is as it was, and the registers and +/// flags are unchanged. +pub proof fn lemma_step_store_exit(cu: spec_fn(nat) -> nat, m: x64::State, src: u8) + requires + x64::wf(m), + ctx_ok(m), + src < 16, + exit_area_mapped(m.mem, ctx_of(m)), + x64::canonical( + m.rip + x64::instr_len( + x64::Instr::MovStore { sz: x64::Size::W64, mem: exit_mem(), src: src }, + ), + ), + ensures + x64::step( + cu, + m, + x64::Instr::MovStore { sz: x64::Size::W64, mem: exit_mem(), src: src }, + ) is Some, + ({ + let m1 = x64::step( + cu, + m, + x64::Instr::MovStore { sz: x64::Size::W64, mem: exit_mem(), src: src }, + ).unwrap(); + &&& m1 == x64::State { + mem: m1.mem, + rip: m.rip + x64::instr_len( + x64::Instr::MovStore { sz: x64::Size::W64, mem: exit_mem(), src: src }, + ), + ..m + } + &&& x64::read_mem(m1.mem, ctx_of(m) + abi::exit_code_off(), 8) == Some( + m.rgf[src as int] as int, + ) + &&& m1.mem.dom() =~= m.mem.dom() + &&& forall|b: int| + #![trigger m1.mem.dom().contains(b)] + m.mem.dom().contains(b) && (b < ctx_of(m) + abi::exit_code_off() || ctx_of(m) + + abi::exit_code_off() + 8 <= b) ==> m1.mem[b] == m.mem[b] + }), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + let ctxb = ctx_of(m); + let addr = ctxb + 256; + assert((256int as i32) as int == 256); + assert(x64::eff_addr(m, exit_mem()) == addr) by { + assert(0 <= addr && addr < 0x8000_0000_0000 + 512); + assert(x64::loghead(64, addr) == addr); + assert(x64::logext(64, addr) == addr); + } + let w = m.rgf[src as int] as int; + assert(x64::rgfi_size(m, x64::Size::W64, src) == w); + assert forall|i: int| 0 <= i < 8 implies m.mem.dom().contains(#[trigger] (addr + i)) by { + assert(in_exit_area(ctxb, addr + i)); + } + super::memlemmas::lemma_write_mem_some(m.mem, addr, 8, w); + let m2 = x64::write_mem(m.mem, addr, 8, w).unwrap(); + super::memlemmas::lemma_write_mem_frame(m.mem, addr, 8, w, m2); + assert(0 <= w < vstd::arithmetic::power2::pow2(8 * 8) as int) by { + assert(vstd::arithmetic::power2::pow2(64) == 0x1_0000_0000_0000_0000); + } + super::memlemmas::lemma_read_after_write_same(m.mem, addr, 8, w, m2); + assert(x64::store_mem(m, exit_mem(), x64::Size::W64, w) == Some(x64::State { mem: m2, ..m })); +} + +/// **The exit sequence refines `exit c`.** +#[verifier::rlimit(500)] +pub proof fn lemma_exit_refines(c: u32, nl: nat) + ensures + super::contract::exit_refines(c, nl), +{ + super::memlemmas::lemma_p2_consts(); + super::steps::lemma_abi_consts(); + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, fuel), run_seq(cu, m, exit_seq(c))] + rel(lay, a, m, nl) && stack_ok(m) && x64::canonical(m.rip + code_len(exit_seq(c))) + implies match lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, fuel) { + lir::Outcome::Exit(code, _, a2) => match run_seq(cu, m, exit_seq(c)) { + Some(m2) => { + let rsp = x64::logext(64, m.rgf[abi::sp_reg() as int] as int); + rel(lay, a2, m2, nl) && preserved_mem(m, m2) && x64::read_mem( + m2.mem, + ctx_of(m) + abi::exit_code_off(), + 8, + ) == Some(code as int) && m2.rgf[abi::sp_reg() as int] as int == x64::loghead( + 64, + rsp + 8, + ) && match x64::read_mem(m.mem, rsp, 8) { + Some(raw) => m2.rip == x64::logext(64, raw), + None => false, + } + }, + None => false, + }, + _ => true, + } by { + match lir::eval(lay, lir::Instr::Exit(c as nat, seq![]), a, fuel) { + lir::Outcome::Exit(code, vals, a2) => { + assert(fuel > 0); + assert(code == c as nat); + assert(a2.stack =~= a.stack); + assert(a2.locals == a.locals && a2.mem == a.mem); + let i1 = x64::Instr::MovImm32 { dst: rax(), imm_u32: c }; + let i2 = x64::Instr::MovStore { sz: x64::Size::W64, mem: exit_mem(), src: rax() }; + let i3 = x64::Instr::Ret; + assert(exit_seq(c) == seq![i1, i2, i3]); + super::runseq::lemma_len3(i1, i2, i3); + assert(x64::wf(m)); + let ctxb = ctx_of(m); + // 1. mov eax, c + super::steps::lemma_step_imm(cu, m, i1); + let s1 = x64::State { + rgf: m.rgf.update(0, c as u64), + rip: m.rip + x64::instr_len(i1), + ..m + }; + assert(x64::canonical(s1.rip)); + assert(rel(lay, a, s1, nl)) by { + super::rel::lemma_rel_regs(lay, a, m, s1, nl); + } + // 2. mov [r15 + 256], rax + lemma_step_store_exit(cu, s1, 0); + let s2 = x64::step(cu, s1, i2).unwrap(); + assert(s1.rgf[0] == c as u64); + assert(x64::canonical(s2.rip)); + assert(x64::wf(s2)) by { + assert(s2.mem.dom() =~= m.mem.dom()); + } + // the frame's slots survive the store to the exit-code word + assert forall|j: nat| j < nl + a.stack.len() implies #[trigger] slot_holds( + s2.mem, + ctxb, + j, + lir::val_bits(slot_val(a, nl, j)), + ) by { + assert(slot_holds(m.mem, ctxb, j, lir::val_bits(slot_val(a, nl, j)))); + assert(j < max_slots()); + super::memlemmas::lemma_read_mem_congr(m.mem, s2.mem, ctxb + slot_disp(j), 8); + } + assert(mem_rel(lay, a.mem, s2.mem)) by { + assert forall|b: nat| #[trigger] in_regions(lay, b) implies s2.mem.dom().contains( + b as int, + ) && s2.mem[b as int] == lir::byte_at(a.mem, b) by { + assert(!in_exit_area(ctxb, b as int)); + assert(m.mem.dom().contains(b as int)); + } + } + assert(rel(lay, a2, s2, nl)) by { + assert(x64::wf(s2) && ctx_ok(s2)); + assert(ctx_of(s2) == ctxb); + assert(exit_area_mapped(s2.mem, ctxb)); + } + // 3. ret + let rsp = x64::logext(64, m.rgf[4] as int); + assert(s2.rgf[4] == m.rgf[4]); + assert(rsp + 8 <= ctxb + 256 || ctxb + 512 <= rsp); + match x64::read_mem(m.mem, rsp, 8) { + Some(raw) => { + super::memlemmas::lemma_read_mem_congr(m.mem, s2.mem, rsp, 8); + assert(x64::read_mem(s2.mem, rsp, 8) == Some(raw)); + let new_rsp = x64::logext(64, rsp + 8); + let tos = x64::logext(64, raw); + assert(x64::canonical(new_rsp) && x64::canonical(tos)); + let s3 = x64::step(cu, s2, i3).unwrap(); + assert(x64::step(cu, s2, i3) == Some( + x64::write_ip(x64::wr64(s2, 4, new_rsp), tos), + )); + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen3(f, m, i1, i2, i3, s1, s2, s3); + super::runseq::lemma_run_seq_is_gen(cu, m, seq![i1, i2, i3]); + assert(s3.rgf.len() == 16); + assert(s3.rgf[15] == m.rgf[15]); + assert(s3.mem == s2.mem); + assert(x64::wf(s3)); + assert(rel(lay, a2, s3, nl)) by { + super::rel::lemma_rel_regs(lay, a2, s2, s3, nl); + } + assert(preserved_mem(m, s3)); + assert(x64::loghead(64, new_rsp) == x64::loghead(64, rsp + 8)); + }, + None => { + assert(false); + }, + } + }, + _ => {}, + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/flags.rs b/crates/clazel-jit/src/flags.rs new file mode 100644 index 000000000..ff886a6fa --- /dev/null +++ b/crates/clazel-jit/src/flags.rs @@ -0,0 +1,782 @@ +//! The status flags: what `cmp`, `test` and `setcc` do with them, and that +//! the condition code a comparison template uses holds exactly when LIR's +//! comparison does. +//! +//! The machine keeps the flags as bits of a 32-bit word (`rflags`), changed +//! one flag at a time by `set_bit32`. The six flags the subset writes are at +//! bit positions 0 (`cf`), 2 (`pf`), 4 (`af`), 6 (`zf`), 7 (`sf`) and 11 +//! (`of`). The first lemmas say that changing one flag's bit changes no other +//! of the six; the rest compute the flags `cmp` and `test` leave. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `k` is the bit position of one of the six flags the subset writes. +pub open spec fn flagpos(k: nat) -> bool { + k == 0 || k == 2 || k == 4 || k == 6 || k == 7 || k == 11 +} + +/// Setting the flag at bit 0: the new value there, the other five unchanged. +#[verifier::rlimit(2000)] +pub proof fn lemma_sb_0(w: u32, v: int) + ensures + x64::bit(x64::set_bit32(w, 0, v) as int, 0) == v % 2, + x64::bit(x64::set_bit32(w, 0, v) as int, 2) == x64::bit(w as int, 2), + x64::bit(x64::set_bit32(w, 0, v) as int, 4) == x64::bit(w as int, 4), + x64::bit(x64::set_bit32(w, 0, v) as int, 6) == x64::bit(w as int, 6), + x64::bit(x64::set_bit32(w, 0, v) as int, 7) == x64::bit(w as int, 7), + x64::bit(x64::set_bit32(w, 0, v) as int, 11) == x64::bit(w as int, 11), +{ + vstd::arithmetic::power2::lemma2_to64(); + let c = v % 2; + assert(0 <= c < 2); + assert(x64::p2(0) == 1); + assert(x64::p2(2) == 4); + assert(x64::p2(4) == 16); + assert(x64::p2(6) == 64); + assert(x64::p2(7) == 128); + assert(x64::p2(11) == 2048); + let q = (w as int) / 2; + let r = (w as int) % 1; + let b = x64::bit(w as int, 0); + assert(b == ((w as int) / 1) % 2); + assert(0 <= r < 1 && 0 <= b < 2); + assert((w as int) == 2 * q + b * 1 + r); + let n = (w as int) - b * 1 + c * 1; + assert(n == 2 * q + c * 1 + r); + assert(0 <= n < 0x1_0000_0000); + assert(x64::set_bit32(w, 0, v) as int == n); + assert(x64::bit(n, 0) == c); + assert(x64::bit(n, 2) == x64::bit(w as int, 2)); + assert(x64::bit(n, 4) == x64::bit(w as int, 4)); + assert(x64::bit(n, 6) == x64::bit(w as int, 6)); + assert(x64::bit(n, 7) == x64::bit(w as int, 7)); + assert(x64::bit(n, 11) == x64::bit(w as int, 11)); +} + +/// Setting the flag at bit 2: the new value there, the other five unchanged. +#[verifier::rlimit(2000)] +pub proof fn lemma_sb_2(w: u32, v: int) + ensures + x64::bit(x64::set_bit32(w, 2, v) as int, 0) == x64::bit(w as int, 0), + x64::bit(x64::set_bit32(w, 2, v) as int, 2) == v % 2, + x64::bit(x64::set_bit32(w, 2, v) as int, 4) == x64::bit(w as int, 4), + x64::bit(x64::set_bit32(w, 2, v) as int, 6) == x64::bit(w as int, 6), + x64::bit(x64::set_bit32(w, 2, v) as int, 7) == x64::bit(w as int, 7), + x64::bit(x64::set_bit32(w, 2, v) as int, 11) == x64::bit(w as int, 11), +{ + vstd::arithmetic::power2::lemma2_to64(); + let c = v % 2; + assert(0 <= c < 2); + assert(x64::p2(0) == 1); + assert(x64::p2(2) == 4); + assert(x64::p2(4) == 16); + assert(x64::p2(6) == 64); + assert(x64::p2(7) == 128); + assert(x64::p2(11) == 2048); + let q = (w as int) / 8; + let r = (w as int) % 4; + let b = x64::bit(w as int, 2); + assert(b == ((w as int) / 4) % 2); + assert(0 <= r < 4 && 0 <= b < 2); + assert((w as int) == 8 * q + b * 4 + r); + let n = (w as int) - b * 4 + c * 4; + assert(n == 8 * q + c * 4 + r); + assert(0 <= n < 0x1_0000_0000); + assert(x64::set_bit32(w, 2, v) as int == n); + assert(x64::bit(n, 0) == x64::bit(w as int, 0)); + assert(x64::bit(n, 2) == c); + assert(x64::bit(n, 4) == x64::bit(w as int, 4)); + assert(x64::bit(n, 6) == x64::bit(w as int, 6)); + assert(x64::bit(n, 7) == x64::bit(w as int, 7)); + assert(x64::bit(n, 11) == x64::bit(w as int, 11)); +} + +/// Setting the flag at bit 4: the new value there, the other five unchanged. +#[verifier::rlimit(2000)] +pub proof fn lemma_sb_4(w: u32, v: int) + ensures + x64::bit(x64::set_bit32(w, 4, v) as int, 0) == x64::bit(w as int, 0), + x64::bit(x64::set_bit32(w, 4, v) as int, 2) == x64::bit(w as int, 2), + x64::bit(x64::set_bit32(w, 4, v) as int, 4) == v % 2, + x64::bit(x64::set_bit32(w, 4, v) as int, 6) == x64::bit(w as int, 6), + x64::bit(x64::set_bit32(w, 4, v) as int, 7) == x64::bit(w as int, 7), + x64::bit(x64::set_bit32(w, 4, v) as int, 11) == x64::bit(w as int, 11), +{ + vstd::arithmetic::power2::lemma2_to64(); + let c = v % 2; + assert(0 <= c < 2); + assert(x64::p2(0) == 1); + assert(x64::p2(2) == 4); + assert(x64::p2(4) == 16); + assert(x64::p2(6) == 64); + assert(x64::p2(7) == 128); + assert(x64::p2(11) == 2048); + let q = (w as int) / 32; + let r = (w as int) % 16; + let b = x64::bit(w as int, 4); + assert(b == ((w as int) / 16) % 2); + assert(0 <= r < 16 && 0 <= b < 2); + assert((w as int) == 32 * q + b * 16 + r); + let n = (w as int) - b * 16 + c * 16; + assert(n == 32 * q + c * 16 + r); + assert(0 <= n < 0x1_0000_0000); + assert(x64::set_bit32(w, 4, v) as int == n); + assert(x64::bit(n, 0) == x64::bit(w as int, 0)); + assert(x64::bit(n, 2) == x64::bit(w as int, 2)); + assert(x64::bit(n, 4) == c); + assert(x64::bit(n, 6) == x64::bit(w as int, 6)); + assert(x64::bit(n, 7) == x64::bit(w as int, 7)); + assert(x64::bit(n, 11) == x64::bit(w as int, 11)); +} + +/// Setting the flag at bit 6: the new value there, the other five unchanged. +#[verifier::rlimit(2000)] +pub proof fn lemma_sb_6(w: u32, v: int) + ensures + x64::bit(x64::set_bit32(w, 6, v) as int, 0) == x64::bit(w as int, 0), + x64::bit(x64::set_bit32(w, 6, v) as int, 2) == x64::bit(w as int, 2), + x64::bit(x64::set_bit32(w, 6, v) as int, 4) == x64::bit(w as int, 4), + x64::bit(x64::set_bit32(w, 6, v) as int, 6) == v % 2, + x64::bit(x64::set_bit32(w, 6, v) as int, 7) == x64::bit(w as int, 7), + x64::bit(x64::set_bit32(w, 6, v) as int, 11) == x64::bit(w as int, 11), +{ + vstd::arithmetic::power2::lemma2_to64(); + let c = v % 2; + assert(0 <= c < 2); + assert(x64::p2(0) == 1); + assert(x64::p2(2) == 4); + assert(x64::p2(4) == 16); + assert(x64::p2(6) == 64); + assert(x64::p2(7) == 128); + assert(x64::p2(11) == 2048); + let q = (w as int) / 128; + let r = (w as int) % 64; + let b = x64::bit(w as int, 6); + assert(b == ((w as int) / 64) % 2); + assert(0 <= r < 64 && 0 <= b < 2); + assert((w as int) == 128 * q + b * 64 + r); + let n = (w as int) - b * 64 + c * 64; + assert(n == 128 * q + c * 64 + r); + assert(0 <= n < 0x1_0000_0000); + assert(x64::set_bit32(w, 6, v) as int == n); + assert(x64::bit(n, 0) == x64::bit(w as int, 0)); + assert(x64::bit(n, 2) == x64::bit(w as int, 2)); + assert(x64::bit(n, 4) == x64::bit(w as int, 4)); + assert(x64::bit(n, 6) == c); + assert(x64::bit(n, 7) == x64::bit(w as int, 7)); + assert(x64::bit(n, 11) == x64::bit(w as int, 11)); +} + +/// Setting the flag at bit 7: the new value there, the other five unchanged. +#[verifier::rlimit(2000)] +pub proof fn lemma_sb_7(w: u32, v: int) + ensures + x64::bit(x64::set_bit32(w, 7, v) as int, 0) == x64::bit(w as int, 0), + x64::bit(x64::set_bit32(w, 7, v) as int, 2) == x64::bit(w as int, 2), + x64::bit(x64::set_bit32(w, 7, v) as int, 4) == x64::bit(w as int, 4), + x64::bit(x64::set_bit32(w, 7, v) as int, 6) == x64::bit(w as int, 6), + x64::bit(x64::set_bit32(w, 7, v) as int, 7) == v % 2, + x64::bit(x64::set_bit32(w, 7, v) as int, 11) == x64::bit(w as int, 11), +{ + vstd::arithmetic::power2::lemma2_to64(); + let c = v % 2; + assert(0 <= c < 2); + assert(x64::p2(0) == 1); + assert(x64::p2(2) == 4); + assert(x64::p2(4) == 16); + assert(x64::p2(6) == 64); + assert(x64::p2(7) == 128); + assert(x64::p2(11) == 2048); + let q = (w as int) / 256; + let r = (w as int) % 128; + let b = x64::bit(w as int, 7); + assert(b == ((w as int) / 128) % 2); + assert(0 <= r < 128 && 0 <= b < 2); + assert((w as int) == 256 * q + b * 128 + r); + let n = (w as int) - b * 128 + c * 128; + assert(n == 256 * q + c * 128 + r); + assert(0 <= n < 0x1_0000_0000); + assert(x64::set_bit32(w, 7, v) as int == n); + assert(x64::bit(n, 0) == x64::bit(w as int, 0)); + assert(x64::bit(n, 2) == x64::bit(w as int, 2)); + assert(x64::bit(n, 4) == x64::bit(w as int, 4)); + assert(x64::bit(n, 6) == x64::bit(w as int, 6)); + assert(x64::bit(n, 7) == c); + assert(x64::bit(n, 11) == x64::bit(w as int, 11)); +} + +/// Setting the flag at bit 11: the new value there, the other five unchanged. +#[verifier::rlimit(2000)] +pub proof fn lemma_sb_11(w: u32, v: int) + ensures + x64::bit(x64::set_bit32(w, 11, v) as int, 0) == x64::bit(w as int, 0), + x64::bit(x64::set_bit32(w, 11, v) as int, 2) == x64::bit(w as int, 2), + x64::bit(x64::set_bit32(w, 11, v) as int, 4) == x64::bit(w as int, 4), + x64::bit(x64::set_bit32(w, 11, v) as int, 6) == x64::bit(w as int, 6), + x64::bit(x64::set_bit32(w, 11, v) as int, 7) == x64::bit(w as int, 7), + x64::bit(x64::set_bit32(w, 11, v) as int, 11) == v % 2, +{ + vstd::arithmetic::power2::lemma2_to64(); + let c = v % 2; + assert(0 <= c < 2); + assert(x64::p2(0) == 1); + assert(x64::p2(2) == 4); + assert(x64::p2(4) == 16); + assert(x64::p2(6) == 64); + assert(x64::p2(7) == 128); + assert(x64::p2(11) == 2048); + let q = (w as int) / 4096; + let r = (w as int) % 2048; + let b = x64::bit(w as int, 11); + assert(b == ((w as int) / 2048) % 2); + assert(0 <= r < 2048 && 0 <= b < 2); + assert((w as int) == 4096 * q + b * 2048 + r); + let n = (w as int) - b * 2048 + c * 2048; + assert(n == 4096 * q + c * 2048 + r); + assert(0 <= n < 0x1_0000_0000); + assert(x64::set_bit32(w, 11, v) as int == n); + assert(x64::bit(n, 0) == x64::bit(w as int, 0)); + assert(x64::bit(n, 2) == x64::bit(w as int, 2)); + assert(x64::bit(n, 4) == x64::bit(w as int, 4)); + assert(x64::bit(n, 6) == x64::bit(w as int, 6)); + assert(x64::bit(n, 7) == x64::bit(w as int, 7)); + assert(x64::bit(n, 11) == c); +} + +/// Setting the flag at `k` sets that bit and no other of the six. +pub proof fn lemma_set_bit32_bit(w: u32, k: nat, v: int, j: nat) + requires + flagpos(k), + flagpos(j), + ensures + x64::bit(x64::set_bit32(w, k, v) as int, j) == if j == k { + v % 2 + } else { + x64::bit(w as int, j) + }, +{ + lemma_sb_0(w, v); + lemma_sb_2(w, v); + lemma_sb_4(w, v); + lemma_sb_6(w, v); + lemma_sb_7(w, v); + lemma_sb_11(w, v); +} + +// ---------------------------------------------------------------------------- +// Reading the flags back out of `change_rflags` and `write_user_rflags` +// ---------------------------------------------------------------------------- + +/// What bit `j` of the flags is after the update `f`, when it was `old`: +/// the new value of the flag at `j` if `f` names one, else `old`. +pub open spec fn upd_bit(f: x64::FlagUpd, j: nat, old: int) -> int { + if j == 0 { + match f.cf { + Some(x) => x % 2, + None => old, + } + } else if j == 2 { + match f.pf { + Some(x) => x % 2, + None => old, + } + } else if j == 4 { + match f.af { + Some(x) => x % 2, + None => old, + } + } else if j == 6 { + match f.zf { + Some(x) => x % 2, + None => old, + } + } else if j == 7 { + match f.sf { + Some(x) => x % 2, + None => old, + } + } else if j == 11 { + match f.of { + Some(x) => x % 2, + None => old, + } + } else { + old + } +} + +/// One optional flag update changes only its own bit. +pub proof fn lemma_opt_set_bit_bit(w: u32, k: nat, v: Option, j: nat) + requires + flagpos(k), + flagpos(j), + ensures + x64::bit(x64::opt_set_bit(w, k, v) as int, j) == match v { + Some(x) => if j == k { + x % 2 + } else { + x64::bit(w as int, j) + }, + None => x64::bit(w as int, j), + }, +{ + match v { + Some(x) => { + lemma_set_bit32_bit(w, k, x, j); + }, + None => {}, + } +} + +/// `change_rflags` sets exactly the flags the update names. +pub proof fn lemma_change_rflags_bit(inp: u32, f: x64::FlagUpd, j: nat) + requires + flagpos(j), + ensures + x64::bit(x64::change_rflags(inp, f) as int, j) == upd_bit(f, j, x64::bit(inp as int, j)), +{ + let w1 = x64::opt_set_bit(inp, x64::cf_bit(), f.cf); + let w2 = x64::opt_set_bit(w1, x64::pf_bit(), f.pf); + let w3 = x64::opt_set_bit(w2, x64::af_bit(), f.af); + let w4 = x64::opt_set_bit(w3, x64::zf_bit(), f.zf); + let w5 = x64::opt_set_bit(w4, x64::sf_bit(), f.sf); + lemma_opt_set_bit_bit(inp, 0, f.cf, j); + lemma_opt_set_bit_bit(w1, 2, f.pf, j); + lemma_opt_set_bit_bit(w2, 4, f.af, j); + lemma_opt_set_bit_bit(w3, 6, f.zf, j); + lemma_opt_set_bit_bit(w4, 7, f.sf, j); + lemma_opt_set_bit_bit(w5, 11, f.of, j); +} + +/// One step of `write_user_rflags` changes only its own flag. +pub proof fn lemma_wfou_flag( + cu: spec_fn(nat) -> nat, + s: x64::State, + k: nat, + vec: u32, + mask: u32, + j: nat, +) + requires + flagpos(k), + flagpos(j), + ensures + x64::flgi(x64::write_flag_or_undefined(cu, s, k, vec, mask), j) == if j == k { + if x64::bit(mask as int, k) == 1 { + (cu(s.undef) % 2) as int + } else { + x64::bit(vec as int, k) + } + } else { + x64::flgi(s, j) + }, +{ + if x64::bit(mask as int, k) == 1 { + lemma_set_bit32_bit(s.rflags, k, (cu(s.undef) % 2) as int, j); + } else { + lemma_set_bit32_bit(s.rflags, k, x64::bit(vec as int, k), j); + } +} + +/// A flag whose bit is clear in the undefined mask is set from the vector by +/// `write_user_rflags`. +pub proof fn lemma_wur_flag(cu: spec_fn(nat) -> nat, s: x64::State, vec: u32, mask: u32, j: nat) + requires + flagpos(j), + x64::bit(mask as int, j) == 0, + ensures + x64::flgi(x64::write_user_rflags(cu, s, vec, mask), j) == x64::bit(vec as int, j), +{ + let s1 = x64::write_flag_or_undefined(cu, s, x64::cf_bit(), vec, mask); + let s2 = x64::write_flag_or_undefined(cu, s1, x64::pf_bit(), vec, mask); + let s3 = x64::write_flag_or_undefined(cu, s2, x64::af_bit(), vec, mask); + let s4 = x64::write_flag_or_undefined(cu, s3, x64::zf_bit(), vec, mask); + let s5 = x64::write_flag_or_undefined(cu, s4, x64::sf_bit(), vec, mask); + lemma_wfou_flag(cu, s, 0, vec, mask, j); + lemma_wfou_flag(cu, s1, 2, vec, mask, j); + lemma_wfou_flag(cu, s2, 4, vec, mask, j); + lemma_wfou_flag(cu, s3, 6, vec, mask, j); + lemma_wfou_flag(cu, s4, 7, vec, mask, j); + lemma_wfou_flag(cu, s5, 11, vec, mask, j); +} + +// ---------------------------------------------------------------------------- +// `cmp` and `test` +// ---------------------------------------------------------------------------- + +/// `cmp a, b` leaves the registers and memory alone and sets the flags of +/// `a - b`: `cf` is "a is below b" (unsigned), `zf` is "the difference is +/// 0", `sf` its top bit, `of` "the signed difference does not fit". +pub proof fn lemma_step_cmp( + cu: spec_fn(nat) -> nat, + m: x64::State, + sz: x64::Size, + a: u8, + b: u8, +) + requires + x64::wf(m), + a < 16, + b < 16, + x64::canonical( + m.rip + x64::instr_len( + x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: sz, dst: a, src: b }, + ), + ), + ensures + x64::step( + cu, + m, + x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: sz, dst: a, src: b }, + ) is Some, + ({ + let m1 = x64::step( + cu, + m, + x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: sz, dst: a, src: b }, + ).unwrap(); + let n = x64::nbits(sz); + let lx = x64::loghead(n, x64::rgfi_size(m, sz, a)); + let ly = x64::loghead(n, x64::rgfi_size(m, sz, b)); + let sraw = x64::logext(n, lx) - x64::logext(n, ly); + let res = x64::loghead(n, sraw); + &&& m1.rgf == m.rgf && m1.mem == m.mem && m1.xmm == m.xmm && m1.rip == m.rip + + x64::instr_len(x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: sz, dst: a, src: b }) + &&& x64::flgi(m1, x64::cf_bit()) == x64::bool_to_bit(lx < ly) + &&& x64::flgi(m1, x64::zf_bit()) == x64::zf_spec(res) + &&& x64::flgi(m1, x64::sf_bit()) == x64::sf_spec(n, res) % 2 + &&& x64::flgi(m1, x64::of_bit()) == x64::of_spec(n, sraw) + }), +{ + let n = x64::nbits(sz); + let x = x64::rgfi_size(m, sz, a); + let y = x64::rgfi_size(m, sz, b); + let r = x64::gpr_sub_spec(n, x, y, m.rflags); + assert(x64::gpr_arith_logic_spec(n, x64::AluOp::Cmp, x, y, m.rflags) == r); + super::alu::lemma_wur_frame(cu, m, r.rflags, r.undef); + assert(r.undef == 0); + assert(x64::bit(0, 0) == 0 && x64::bit(0, 6) == 0 && x64::bit(0, 7) == 0 && x64::bit(0, 11) + == 0) by { + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::p2(0) == 1 && x64::p2(6) == 64 && x64::p2(7) == 128 && x64::p2(11) == 2048); + } + lemma_wur_flag(cu, m, r.rflags, r.undef, 0); + lemma_wur_flag(cu, m, r.rflags, r.undef, 6); + lemma_wur_flag(cu, m, r.rflags, r.undef, 7); + lemma_wur_flag(cu, m, r.rflags, r.undef, 11); + let lx = x64::loghead(n, x); + let ly = x64::loghead(n, y); + let sraw = x64::logext(n, lx) - x64::logext(n, ly); + let res = x64::loghead(n, sraw); + let f = x64::FlagUpd { + cf: Some(x64::bool_to_bit(lx < ly)), + pf: Some(x64::pf_spec(res)), + af: Some(x64::sub_af_spec(lx, ly)), + zf: Some(x64::zf_spec(res)), + sf: Some(x64::sf_spec(n, res)), + of: Some(x64::of_spec(n, sraw)), + }; + assert(r.rflags == x64::change_rflags(m.rflags, f)); + lemma_change_rflags_bit(m.rflags, f, 0); + lemma_change_rflags_bit(m.rflags, f, 6); + lemma_change_rflags_bit(m.rflags, f, 7); + lemma_change_rflags_bit(m.rflags, f, 11); +} + +/// Bit 6 of `0x10` is clear. +pub proof fn lemma_bit_0x10() + ensures + x64::bit(16, 6) == 0, +{ + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::p2(6) == 64); +} + +/// The flags of an `and`: `cf` and `of` cleared, `pf zf sf` from the result, +/// `af` undefined (mask `0x10`). +pub proof fn lemma_and_spec_shape(n: nat, x: int, y: int, rfl: u32) + ensures + x64::gpr_and_spec(n, x, y, rfl).undef == 0x10u32, + x64::gpr_and_spec(n, x, y, rfl).rflags == x64::change_rflags( + rfl, + x64::FlagUpd { + cf: Some(0), + pf: Some(x64::pf_spec(x64::gpr_and_spec(n, x, y, rfl).result)), + af: None, + zf: Some(x64::zf_spec(x64::gpr_and_spec(n, x, y, rfl).result)), + sf: Some(x64::sf_spec(n, x64::gpr_and_spec(n, x, y, rfl).result)), + of: Some(0), + }, + ), +{ +} + +/// `test a, b` leaves the registers and memory alone and sets `zf` to "the +/// bitwise and of the operands is 0". +pub proof fn lemma_step_test( + cu: spec_fn(nat) -> nat, + m: x64::State, + sz: x64::Size, + a: u8, + b: u8, +) + requires + x64::wf(m), + a < 16, + b < 16, + x64::canonical( + m.rip + x64::instr_len(x64::Instr::TestRR { sz: sz, a: a, b: b }), + ), + ensures + x64::step(cu, m, x64::Instr::TestRR { sz: sz, a: a, b: b }) is Some, + ({ + let m1 = x64::step(cu, m, x64::Instr::TestRR { sz: sz, a: a, b: b }).unwrap(); + let n = x64::nbits(sz); + let r = x64::gpr_and_spec( + n, + x64::rgfi_size(m, sz, a), + x64::rgfi_size(m, sz, b), + m.rflags, + ); + &&& m1.rgf == m.rgf && m1.mem == m.mem && m1.xmm == m.xmm && m1.rip == m.rip + + x64::instr_len(x64::Instr::TestRR { sz: sz, a: a, b: b }) + &&& x64::flgi(m1, x64::zf_bit()) == x64::zf_spec(r.result) + }), +{ + let n = x64::nbits(sz); + let x = x64::rgfi_size(m, sz, a); + let y = x64::rgfi_size(m, sz, b); + lemma_and_spec_shape(n, x, y, m.rflags); + let r = x64::gpr_and_spec(n, x, y, m.rflags); + super::alu::lemma_wur_frame(cu, m, r.rflags, r.undef); + lemma_bit_0x10(); + assert(r.undef as int == 16); + assert(x64::bit(r.undef as int, 6) == 0); + lemma_wur_flag(cu, m, r.rflags, r.undef, 6); + let f = x64::FlagUpd { + cf: Some(0), + pf: Some(x64::pf_spec(r.result)), + af: None, + zf: Some(x64::zf_spec(r.result)), + sf: Some(x64::sf_spec(n, r.result)), + of: Some(0), + }; + lemma_change_rflags_bit(m.rflags, f, 6); +} + +// ---------------------------------------------------------------------------- +// Condition codes +// ---------------------------------------------------------------------------- + +/// The flags of `a - b` at 32 bits: `zf` is "equal", `sf != of` is "signed less". +pub proof fn lemma_cmp_arith32(lx: int, ly: int) + requires + 0 <= lx < 0x1_0000_0000, + 0 <= ly < 0x1_0000_0000, + ensures + ({ + let sraw = x64::logext(32, lx) - x64::logext(32, ly); + let res = x64::loghead(32, sraw); + &&& (x64::zf_spec(res) == 1) <==> (lx == ly) + &&& ((x64::sf_spec(32, res) % 2 != x64::of_spec(32, sraw)) <==> (lir::signed_of( + 32, + lx, + ) < lir::signed_of(32, ly))) + &&& lir::signed_of(32, lx) == x64::logext(32, lx) + &&& lir::signed_of(32, ly) == x64::logext(32, ly) + }), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::p2(32) == 0x1_0000_0000); + assert(x64::p2(31) == 0x8000_0000); + assert(lir::pow2(32) == 0x1_0000_0000); + assert(lir::pow2(32) / 2 == 0x8000_0000); + assert(x64::loghead(32, lx) == lx && x64::loghead(32, ly) == ly); + let sx = if lx >= 0x8000_0000 { lx - 0x1_0000_0000 } else { lx }; + let sy = if ly >= 0x8000_0000 { ly - 0x1_0000_0000 } else { ly }; + assert(x64::logext(32, lx) == sx && x64::logext(32, ly) == sy); + assert(lir::signed_of(32, lx) == sx && lir::signed_of(32, ly) == sy); + let sraw = sx - sy; + let res = sraw % 0x1_0000_0000; + assert(x64::loghead(32, sraw) == res); + assert(x64::bit(res, 31) == (res / 0x8000_0000) % 2); + assert(x64::sf_spec(32, res) == (res / 0x8000_0000) % 2); + assert(x64::zf_spec(res) == 1 <==> res == 0); + assert((sraw % 0x1_0000_0000 == 0) <==> (lx == ly)); + assert(x64::of_spec(32, sraw) == if -0x8000_0000 <= sraw && sraw < 0x8000_0000 { 0int } else { 1int }); + assert(((res / 0x8000_0000) % 2 != (if -0x8000_0000 <= sraw && sraw < 0x8000_0000 { 0int } else { 1int })) <==> (sx < sy)); +} + +/// The flags of `a - b` at 64 bits: `zf` is "equal", `sf != of` is "signed less". +pub proof fn lemma_cmp_arith64(lx: int, ly: int) + requires + 0 <= lx < 0x1_0000_0000_0000_0000, + 0 <= ly < 0x1_0000_0000_0000_0000, + ensures + ({ + let sraw = x64::logext(64, lx) - x64::logext(64, ly); + let res = x64::loghead(64, sraw); + &&& (x64::zf_spec(res) == 1) <==> (lx == ly) + &&& ((x64::sf_spec(64, res) % 2 != x64::of_spec(64, sraw)) <==> (lir::signed_of( + 64, + lx, + ) < lir::signed_of(64, ly))) + &&& lir::signed_of(64, lx) == x64::logext(64, lx) + &&& lir::signed_of(64, ly) == x64::logext(64, ly) + }), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + assert(x64::p2(64) == 0x1_0000_0000_0000_0000); + assert(x64::p2(63) == 0x8000_0000_0000_0000); + assert(lir::pow2(64) == 0x1_0000_0000_0000_0000); + assert(lir::pow2(64) / 2 == 0x8000_0000_0000_0000); + assert(x64::loghead(64, lx) == lx && x64::loghead(64, ly) == ly); + let sx = if lx >= 0x8000_0000_0000_0000 { lx - 0x1_0000_0000_0000_0000 } else { lx }; + let sy = if ly >= 0x8000_0000_0000_0000 { ly - 0x1_0000_0000_0000_0000 } else { ly }; + assert(x64::logext(64, lx) == sx && x64::logext(64, ly) == sy); + assert(lir::signed_of(64, lx) == sx && lir::signed_of(64, ly) == sy); + let sraw = sx - sy; + let res = sraw % 0x1_0000_0000_0000_0000; + assert(x64::loghead(64, sraw) == res); + assert(x64::bit(res, 63) == (res / 0x8000_0000_0000_0000) % 2); + assert(x64::sf_spec(64, res) == (res / 0x8000_0000_0000_0000) % 2); + assert(x64::zf_spec(res) == 1 <==> res == 0); + assert((sraw % 0x1_0000_0000_0000_0000 == 0) <==> (lx == ly)); + assert(x64::of_spec(64, sraw) == if -0x8000_0000_0000_0000 <= sraw && sraw < 0x8000_0000_0000_0000 { 0int } else { 1int }); + assert(((res / 0x8000_0000_0000_0000) % 2 != (if -0x8000_0000_0000_0000 <= sraw && sraw < 0x8000_0000_0000_0000 { 0int } else { 1int })) <==> (sx < sy)); +} + +/// The arithmetic of the flags of `a - b`, at 32 or 64 bits. +pub proof fn lemma_cmp_arith(n: nat, lx: int, ly: int) + requires + n == 32 || n == 64, + 0 <= lx < x64::p2(n), + 0 <= ly < x64::p2(n), + ensures + ({ + let sraw = x64::logext(n, lx) - x64::logext(n, ly); + let res = x64::loghead(n, sraw); + &&& (x64::zf_spec(res) == 1) <==> (lx == ly) + &&& ((x64::sf_spec(n, res) % 2 != x64::of_spec(n, sraw)) <==> (lir::signed_of( + n, + lx, + ) < lir::signed_of(n, ly))) + &&& lir::signed_of(n, lx) == x64::logext(n, lx) + &&& lir::signed_of(n, ly) == x64::logext(n, ly) + }), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + if n == 32 { + lemma_cmp_arith32(lx, ly); + } else { + lemma_cmp_arith64(lx, ly); + } +} + +/// Given the flags `cmp lx, ly` leaves, the condition code of the comparison +/// template holds exactly when LIR's comparison does. +pub proof fn lemma_cc_icmp(s: x64::State, op: lir::ICmp, n: nat, lx: int, ly: int) + requires + n == 32 || n == 64, + 0 <= lx < x64::p2(n), + 0 <= ly < x64::p2(n), + ({ + let sraw = x64::logext(n, lx) - x64::logext(n, ly); + let res = x64::loghead(n, sraw); + &&& x64::flgi(s, x64::cf_bit()) == x64::bool_to_bit(lx < ly) + &&& x64::flgi(s, x64::zf_bit()) == x64::zf_spec(res) + &&& x64::flgi(s, x64::sf_bit()) == x64::sf_spec(n, res) % 2 + &&& x64::flgi(s, x64::of_bit()) == x64::of_spec(n, sraw) + }), + ensures + x64::jcc_cmovcc_setcc_spec(s, super::templates::cc_of(op)) == lir::icmp(op, n, lx, ly), +{ + lemma_cmp_arith(n, lx, ly); + assert(x64::cf_bit() == 0 && x64::zf_bit() == 6 && x64::sf_bit() == 7 && x64::of_bit() == 11); +} + +/// `setcc dst` sets the low byte of `dst` to 1 or 0 by the condition, keeping +/// the other 56 bits. +pub proof fn lemma_step_setcc(cu: spec_fn(nat) -> nat, m: x64::State, cc: u8, dst: u8) + requires + x64::wf(m), + cc < 16, + dst < 16, + x64::canonical(m.rip + x64::instr_len(x64::Instr::Setcc { cc: cc, dst: dst })), + ensures + x64::step(cu, m, x64::Instr::Setcc { cc: cc, dst: dst }) == Some( + x64::State { + rgf: m.rgf.update( + dst as int, + ((m.rgf[dst as int] as int) - (m.rgf[dst as int] as int) % 256 + if x64::jcc_cmovcc_setcc_spec( + m, + cc, + ) { + 1int + } else { + 0int + }) as u64, + ), + rip: m.rip + x64::instr_len(x64::Instr::Setcc { cc: cc, dst: dst }), + ..m + }, + ), +{ + super::memlemmas::lemma_p2_consts(); + let old = m.rgf[dst as int] as int; + let v: int = if x64::jcc_cmovcc_setcc_spec(m, cc) { + 1 + } else { + 0 + }; + assert(x64::loghead(8, v) == v); + assert(old - old % 256 + v < 0x1_0000_0000_0000_0000); + assert(x64::loghead(64, old - old % 256 + v) == old - old % 256 + v); +} + +/// `cmovcc dst, src` (64-bit): `dst := src` when the condition holds, else +/// unchanged. +pub proof fn lemma_step_cmov64(cu: spec_fn(nat) -> nat, m: x64::State, cc: u8, dst: u8, src: u8) + requires + x64::wf(m), + cc < 16, + dst < 16, + src < 16, + x64::canonical( + m.rip + x64::instr_len( + x64::Instr::Cmovcc { cc: cc, sz: x64::Size::W64, dst: dst, src: src }, + ), + ), + ensures + x64::step( + cu, + m, + x64::Instr::Cmovcc { cc: cc, sz: x64::Size::W64, dst: dst, src: src }, + ) == Some( + x64::State { + rgf: if x64::jcc_cmovcc_setcc_spec(m, cc) { + m.rgf.update(dst as int, m.rgf[src as int]) + } else { + m.rgf + }, + rip: m.rip + x64::instr_len( + x64::Instr::Cmovcc { cc: cc, sz: x64::Size::W64, dst: dst, src: src }, + ), + ..m + }, + ), +{ + super::memlemmas::lemma_p2_consts(); + let v = m.rgf[src as int] as int; + assert(x64::loghead(64, v) == v); +} + +} // verus! diff --git a/crates/clazel-jit/src/front/arith.rs b/crates/clazel-jit/src/front/arith.rs new file mode 100644 index 000000000..c9b01d95c --- /dev/null +++ b/crates/clazel-jit/src/front/arith.rs @@ -0,0 +1,357 @@ +//! The arithmetic of the small-integer lowerings: what LIR's operators +//! compute on NaN-boxed int32s, shown equal to the bytecode semantics' +//! `arith_add`, `arith_sub` and `arith_mul`. The lowerings work on the +//! 64-bit words: test the tag, take each payload and sign-extend it, do the +//! operation in 64 bits (where an int32 operation cannot overflow), check +//! that the result is still an int32, and re-tag it. +//! +//! `k` is the operation: 0 add, 1 subtract, 2 multiply. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The low 32 bits of a word (`i32.wrap_i64`). +pub open spec fn lo32(v: u64) -> u32 { + (v & 0xFFFF_FFFF) as u32 +} + +/// An `i32` sign-extended to 64 bits, as a word (`i64.extend_i32_s`). +pub open spec fn sext(x: u32) -> u64 { + ((x as i32) as i64) as u64 +} + +/// The tag bits of an int32 value. +pub open spec fn tag_bits() -> u64 { + 0xFFF9_0000_0000_0000 +} + +/// The value of `v >> 32` for an int32 value: the tag and 16 zero bits. +pub open spec fn tag_hi() -> u64 { + 0xFFF9_0000 +} + +/// The 64-bit operation of `k`. +pub open spec fn ibin_of(k: nat) -> lir::IBin { + if k == 0 { + lir::IBin::Add + } else if k == 1 { + lir::IBin::Sub + } else { + lir::IBin::Mul + } +} + +/// The exact integer result of `k` on two int32s. +pub open spec fn int_result(k: nat, ia: int, ib: int) -> int { + if k == 0 { + ia + ib + } else if k == 1 { + ia - ib + } else { + ia * ib + } +} + +/// The int32 result is the encoding the bytecode semantics picks: it fits, +/// and (for a product) it is not a zero that should be `-0`. +pub open spec fn int_ok(k: nat, ia: int, ib: int) -> bool { + let n = int_result(k, ia, ib); + js::fits_i32(n) && !(k == 2 && n == 0 && (ia < 0 || ib < 0)) +} + +/// The bytecode semantics' result of `k` on two Numbers. +pub open spec fn spec_arith(k: nat, a: u64, b: u64) -> u64 { + if k == 0 { + js::arith_add(a, b) + } else if k == 1 { + js::arith_sub(a, b) + } else { + js::arith_mul(a, b) + } +} + +/// The 64-bit result the lowering computes, from the two operand words. +pub open spec fn s64(k: nat, va: u64, vb: u64) -> u64 { + lir::ibin64(ibin_of(k), sext(lo32(va)), sext(lo32(vb))) +} + +/// `1` if the 64-bit word `s` is the sign extension of its low 32 bits. +pub open spec fn fits_flag(s: u64) -> u32 { + lir::bool_i32(sext(lo32(s)) == s) +} + +/// The test of a product being a zero from a negative operand +/// (`+0 × -5` is `-0`, not an int32): non-zero exactly then. +pub open spec fn zneg_flag(va: u64, vb: u64, s: u64) -> u32 { + lir::ibin32( + lir::IBin::And, + lir::bool_i32(s == 0), + lir::bool_i32( + lir::signed_of(64, (sext(lo32(va)) | sext(lo32(vb))) as int) < lir::signed_of(64, 0), + ), + ) +} + +/// The second guard's value: non-zero when the result is an int32 the +/// semantics encodes as one. +pub open spec fn guard2(k: nat, va: u64, vb: u64) -> u32 { + let s = s64(k, va, vb); + if k == 2 { + lir::ibin32(lir::IBin::And, fits_flag(s), lir::bool_i32(zneg_flag(va, vb, s) == 0)) + } else { + fits_flag(s) + } +} + +/// The word the lowering stores: the low 32 bits of `s`, tagged. +pub open spec fn tagged(s: u64) -> u64 { + (s & 0xFFFF_FFFF) | tag_bits() +} + +/// `i64.extend_i32_s (i32.wrap_i64 v)` computes [`sext`] of [`lo32`]. +pub proof fn lemma_sx(y: u32) + ensures + lir::wrap(64, lir::signed_of(32, y as int)) as u64 == sext(y), +{ + vstd::arithmetic::power2::lemma2_to64(); + assert(lir::pow2(32) == 0x1_0000_0000); + assert(lir::pow2(64) == 0x1_0000_0000_0000_0000); + if y < 0x8000_0000 { + assert(sext(y) == y as u64) by (bit_vector) + requires + y < 0x8000_0000u32, + ; + } else { + assert(sext(y) == (y as u64) + 0xFFFF_FFFF_0000_0000u64) by (bit_vector) + requires + y >= 0x8000_0000u32, + ; + } +} + +/// The 64-bit operators are the wrapping machine operations. +pub proof fn lemma_ibin64(k: nat, x: u64, y: u64) + requires + k <= 2, + ensures + lir::ibin64(ibin_of(k), x, y) == (if k == 0 { + x.wrapping_add(y) + } else if k == 1 { + x.wrapping_sub(y) + } else { + x.wrapping_mul(y) + }), +{ + vstd::arithmetic::power2::lemma2_to64(); + assert(lir::pow2(64) == 0x1_0000_0000_0000_0000); + if k == 0 { + assert((((x as int) + (y as int)) % 0x1_0000_0000_0000_0000int) as u64 == x.wrapping_add(y)) + by (bit_vector); + } else if k == 1 { + assert((((x as int) - (y as int)) % 0x1_0000_0000_0000_0000int) as u64 == x.wrapping_sub(y)) + by (bit_vector); + } else { + assert((((x as int) * (y as int)) % 0x1_0000_0000_0000_0000int) as u64 == x.wrapping_mul(y)) + by (bit_vector); + } +} + + +/// `signed_of(64, x)` is the machine's signed reading. +pub proof fn lemma_signed64(x: u64) + ensures + lir::signed_of(64, x as int) == (x as i64) as int, +{ + vstd::arithmetic::power2::lemma2_to64(); + assert(lir::pow2(64) == 0x1_0000_0000_0000_0000); + if x < 0x8000_0000_0000_0000 { + assert((x as i64) as int == x as int) by (bit_vector) + requires + x < 0x8000_0000_0000_0000u64, + ; + } else { + assert((x as i64) as int == (x as int) - 0x1_0000_0000_0000_0000int) by (bit_vector) + requires + x >= 0x8000_0000_0000_0000u64, + ; + } +} + +/// An int32 value's payload, and its sign extension. +pub proof fn lemma_payload(v: u64) + requires + js::is_int32(v), + ensures + js::int32_of(v) == (lo32(v) as i32), + sext(lo32(v)) == (js::int32_of(v) as i64) as u64, +{ +} + +/// `bool_i32` is 0 or 1. +pub proof fn lemma_bool_i32(b: bool) + ensures + lir::bool_i32(b) == (if b { 1u32 } else { 0u32 }), +{ +} + + +/// `sext (lo32 s) == s` exactly when the signed reading of `s` is an int32. +pub proof fn lemma_fits_bv(m: i64) + ensures + (sext(lo32(m as u64)) == (m as u64)) <==> (-2147483648 <= m && m <= 2147483647), + (-2147483648 <= m && m <= 2147483647) ==> tagged(m as u64) == js::enc_int32(m as i32), +{ + assert(((((((m as u64) & 0xFFFF_FFFFu64) as u32) as i32) as i64) as u64 == (m as u64)) + <==> (-2147483648 <= m && m <= 2147483647)) by (bit_vector); + assert((-2147483648 <= m && m <= 2147483647) ==> (((m as u64) & 0xFFFF_FFFFu64) | 0xFFF9_0000_0000_0000u64) + == (0xFFF9_0000_0000_0000u64 | ((((m as i32) as u32) as u64) & 0xFFFF_FFFFu64))) by (bit_vector); +} + +/// The sign of the or of two sign-extended int32s. +pub proof fn lemma_neg_or_bv(ia: i32, ib: i32) + ensures + (((((ia as i64) as u64) | ((ib as i64) as u64)) as i64) < 0) <==> (ia < 0 || ib < 0), +{ + assert((((((ia as i64) as u64) | ((ib as i64) as u64)) as i64) < 0) <==> (ia < 0 || ib < 0)) + by (bit_vector); +} + +/// The generic part of [`lemma_arith_facts`], once the 64-bit result is +/// known to be the exact integer result `m`. +pub proof fn lemma_arith_generic(k: nat, va: u64, vb: u64, m: i64) + requires + k <= 2, + js::is_int32(va), + js::is_int32(vb), + s64(k, va, vb) == (m as u64), + m as int == int_result(k, js::int32_of(va) as int, js::int32_of(vb) as int), + ensures + (guard2(k, va, vb) != 0) <==> int_ok(k, js::int32_of(va) as int, js::int32_of(vb) as int), + int_ok(k, js::int32_of(va) as int, js::int32_of(vb) as int) ==> tagged(s64(k, va, vb)) + == spec_arith(k, va, vb), +{ + lemma_payload(va); + lemma_payload(vb); + let ia = js::int32_of(va); + let ib = js::int32_of(vb); + let s = s64(k, va, vb); + lemma_fits_bv(m); + assert(fits_flag(s) == (if js::fits_i32(m as int) { 1u32 } else { 0u32 })); + if k == 2 { + lemma_signed64((sext(lo32(va)) | sext(lo32(vb)))); + lemma_neg_or_bv(ia, ib); + assert(sext(lo32(va)) == (ia as i64) as u64); + assert(sext(lo32(vb)) == (ib as i64) as u64); + let z = zneg_flag(va, vb, s); + let xo = sext(lo32(va)) | sext(lo32(vb)); + lemma_signed64(0); + assert(lir::signed_of(64, 0) == 0); + assert(xo == (((ia as i64) as u64) | ((ib as i64) as u64))); + assert((lir::signed_of(64, xo as int) < lir::signed_of(64, 0)) <==> (ia < 0 || ib < 0)); + assert((s == 0) <==> (m == 0)) by (bit_vector) + requires + s == (m as u64), + ; + let e1 = lir::bool_i32(s == 0); + let e2 = lir::bool_i32(lir::signed_of(64, xo as int) < lir::signed_of(64, 0)); + assert(z == lir::ibin32(lir::IBin::And, e1, e2)); + assert(e1 == 0 || e1 == 1); + assert(e2 == 0 || e2 == 1); + assert(((e1 & e2) != 0) <==> (e1 == 1 && e2 == 1)) by (bit_vector) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ; + assert((z != 0) <==> (m == 0 && (ia < 0 || ib < 0))); + assert((guard2(k, va, vb) != 0) <==> int_ok(k, ia as int, ib as int)) by { + assert(guard2(k, va, vb) == lir::ibin32( + lir::IBin::And, + fits_flag(s), + lir::bool_i32(z == 0), + )); + assert(fits_flag(s) == 0 || fits_flag(s) == 1); + assert(lir::bool_i32(z == 0) == 0 || lir::bool_i32(z == 0) == 1); + let f = fits_flag(s); + let g = lir::bool_i32(z == 0); + assert(((f & g) != 0) <==> (f == 1 && g == 1)) by (bit_vector) + requires + f == 0 || f == 1, + g == 0 || g == 1, + ; + } + } else { + assert(fits_flag(s) == 0 || fits_flag(s) == 1); + } + if int_ok(k, ia as int, ib as int) { + assert(m as int == int_result(k, ia as int, ib as int)); + assert(tagged(s) == js::enc_int32(m as i32)); + assert(spec_arith(k, va, vb) == js::enc_int32(m as i32)); + } +} + +/// The facts the lowering's proof needs, for two int32 words: the second +/// guard is non-zero exactly when the result is an int32 the semantics +/// encodes as one, and then the tagged low half of the 64-bit result is the +/// semantics' result. +pub proof fn lemma_arith_facts(k: nat, va: u64, vb: u64) + requires + k <= 2, + js::is_int32(va), + js::is_int32(vb), + ensures + (guard2(k, va, vb) != 0) <==> int_ok(k, js::int32_of(va) as int, js::int32_of(vb) as int), + int_ok(k, js::int32_of(va) as int, js::int32_of(vb) as int) ==> tagged(s64(k, va, vb)) + == spec_arith(k, va, vb), +{ + lemma_payload(va); + lemma_payload(vb); + let ia = js::int32_of(va); + let ib = js::int32_of(vb); + let xa = sext(lo32(va)); + let xb = sext(lo32(vb)); + lemma_ibin64(k, xa, xb); + let s = s64(k, va, vb); + assert(xa == (ia as i64) as u64); + assert(xb == (ib as i64) as u64); + let n = int_result(k, ia as int, ib as int); + assert(-0x4000_0000_0000_0000int <= n <= 0x4000_0000_0000_0000int) by (nonlinear_arith) + requires + n == int_result(k, ia as int, ib as int), + -2147483648 <= ia <= 2147483647, + -2147483648 <= ib <= 2147483647, + ; + let m = n as i64; + assert(m as int == n); + if k == 0 { + assert(s == (((ia as i64) + (ib as i64)) as u64)) by (bit_vector) + requires + s == xa.wrapping_add(xb), + xa == (ia as i64) as u64, + xb == (ib as i64) as u64, + ; + assert(s == (m as u64)); + } else if k == 1 { + assert(s == (((ia as i64) - (ib as i64)) as u64)) by (bit_vector) + requires + s == xa.wrapping_sub(xb), + xa == (ia as i64) as u64, + xb == (ib as i64) as u64, + ; + assert(s == (m as u64)); + } else { + assert(s == (((ia as i64) * (ib as i64)) as u64)) by (bit_vector) + requires + s == xa.wrapping_mul(xb), + xa == (ia as i64) as u64, + xb == (ib as i64) as u64, + ; + assert(s == (m as u64)); + } + lemma_arith_generic(k, va, vb, m); +} + +} // verus! diff --git a/crates/clazel-jit/src/front/bitops.rs b/crates/clazel-jit/src/front/bitops.rs new file mode 100644 index 000000000..730e1a3bd --- /dev/null +++ b/crates/clazel-jit/src/front/bitops.rs @@ -0,0 +1,461 @@ +//! The int32 bitwise operations and shifts: `BitAnd`, `BitOr`, `BitXor`, +//! `Shl`, `Shr`. +//! +//! The semantics takes `ToInt32` of the doubles two int32s stand for, which +//! [`super::numord`] shows is the identity on an int32 (and `ToUint32` its +//! unsigned reading). So the lowering is: both operands int32, sign-extend +//! the two payloads to 64 bits, do the operation on the 64-bit words (for a +//! shift, with the count masked to its low five bits first, as the semantics +//! reduces it modulo 32), keep the low 32 bits of the result and tag them. +//! Every other pair of operands exits to the handler. +//! +//! `k` is the operation: 0 `BitAnd`, 1 `BitOr`, 2 `BitXor`, 3 `Shl`, +//! 4 `Shr`. (`UShr` is not lowered: its result is a double when the shifted +//! value has its top bit set, which needs a second guard; `BitNot` is a +//! unary op.) + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The 64-bit operator of operation `k`. +pub open spec fn bit_ibin(k: nat) -> lir::IBin { + if k == 0 { + lir::IBin::And + } else if k == 1 { + lir::IBin::Or + } else if k == 2 { + lir::IBin::Xor + } else if k == 3 { + lir::IBin::Shl + } else { + lir::IBin::ShrS + } +} + +/// The bytecode op of operation `k`. +pub open spec fn bit_op(k: nat, dst: nat, a: nat, b: nat) -> js::Op { + if k == 0 { + js::Op::BitAnd { dst, a, b } + } else if k == 1 { + js::Op::BitOr { dst, a, b } + } else if k == 2 { + js::Op::BitXor { dst, a, b } + } else if k == 3 { + js::Op::Shl { dst, a, b } + } else { + js::Op::Shr { dst, a, b } + } +} + +/// The shift count the semantics uses: `ToUint32` of the right operand, +/// modulo 32. +pub open spec fn shift_count(ib: i32) -> nat { + ((ib as u32) as nat) % 32 +} + +/// The int32 result of operation `k` on two int32s, as the semantics writes +/// it (`Number::bitwiseAND`, `leftShift`, `signedRightShift`, ... on the +/// `ToInt32`s, which are the int32s themselves). +pub open spec fn bit_res(k: nat, ia: i32, ib: i32) -> i32 { + if k == 0 { + ia & ib + } else if k == 1 { + ia | ib + } else if k == 2 { + ia ^ ib + } else if k == 3 { + js::wrap_i32(ia as int * vstd::arithmetic::power2::pow2(shift_count(ib)) as int) + } else { + ((ia as int) / (vstd::arithmetic::power2::pow2(shift_count(ib)) as int)) as i32 + } +} + +/// After the two sign-extended payloads: (for a shift, mask the count), +/// operate, keep the low half (`and` with `0xFFFF_FFFF`), tag (`or` with the +/// int32 tag). +pub open spec fn bit_tail(k: nat) -> Seq { + if k <= 2 { + seq![ + lir::Instr::Bin(lir::BinOp::I64(bit_ibin(k))), + super::lower::i64c(0xFFFF_FFFF), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::And)), + super::lower::i64c(super::arith::tag_bits()), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), + ] + } else { + seq![ + super::lower::i64c(31), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::And)), + lir::Instr::Bin(lir::BinOp::I64(bit_ibin(k))), + super::lower::i64c(0xFFFF_FFFF), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::And)), + super::lower::i64c(super::arith::tag_bits()), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), + ] + } +} + +/// The value block: the tagged result of operation `k` on locals 1 and 2. +pub open spec fn bit_val(k: nat) -> Seq { + super::lower::sext_local(1) + (super::lower::sext_local(2) + bit_tail(k)) +} + +/// The word the value block pushes, from the sign-extended payloads. +pub open spec fn bit_word(k: nat, xa: u64, xb: u64) -> u64 { + super::arith::tagged( + lir::ibin64( + bit_ibin(k), + xa, + if k >= 3 { + xb & 31 + } else { + xb + }, + ), + ) +} + +/// The lowering of operation `k` on registers `a` and `b` into `dst`. +pub open spec fn lower_bit(k: nat, dst: nat, a: nat, b: nat, op: js::Op, pc: nat) -> lir::Instr { + super::blocks::lower_bin(bit_val(k), dst, a, b, op, pc) +} + +/// The value block is simple, and short. +pub proof fn lemma_bit_val_simple(k: nat) + ensures + super::run::all_simple(bit_val(k)), + bit_val(k).len() < 40, +{ + super::intops::lemma_literals_simple(0); + let t = bit_tail(k); + assert(super::run::all_simple(t)) by { + assert forall|i: int| 0 <= i < t.len() implies super::run::is_simple(#[trigger] t[i]) by { + assert(i == 0 || i == 1 || i == 2 || i == 3 || i == 4 || i == 5 || i == 6); + } + } + super::intops::lemma_simple_concat(super::lower::sext_local(2), t); + super::intops::lemma_simple_concat(super::lower::sext_local(1), super::lower::sext_local(2) + t); +} + +/// The tail's trace: from the two sign-extended payloads on the stack it +/// leaves the word. +pub proof fn lemma_bit_tail_trace(lay: lir::Layout, k: nat, st: lir::St, base: Seq, xa: u64, xb: u64) + requires + st.stack == base.push(lir::Val::I64(xa)).push(lir::Val::I64(xb)), + ensures + super::run::exec(lay, bit_tail(k), st) == lir::Outcome::Normal( + st.with_stack(base.push(lir::Val::I64(bit_word(k, xa, xb)))), + ), +{ + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + reveal_with_fuel(super::run::exec, 8); +} + +/// The value block's trace. +pub proof fn lemma_bit_val_trace(lay: lir::Layout, k: nat, va: u64, vb: u64) + ensures + super::blocks::val_computes( + lay, + bit_val(k), + va, + vb, + bit_word(k, super::arith::sext(super::arith::lo32(va)), super::arith::sext(super::arith::lo32(vb))), + ), +{ + let xa = super::arith::sext(super::arith::lo32(va)); + let xb = super::arith::sext(super::arith::lo32(vb)); + let w = bit_word(k, xa, xb); + let t = bit_tail(k); + assert forall|st1: lir::St| + #![trigger super::run::exec(lay, bit_val(k), st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) && st1.locals[2] + == lir::Val::I64(vb) implies super::run::exec(lay, bit_val(k), st1) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I64(w))), + ) by { + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + super::trace::lemma_exec_sext_local(lay, st1, 1, va); + let s1 = st1.with_stack(st1.stack.push(lir::Val::I64(xa))); + super::trace::lemma_exec_app( + lay, + super::lower::sext_local(1), + super::lower::sext_local(2) + t, + st1, + s1, + ); + super::trace::lemma_exec_sext_local(lay, s1, 2, vb); + let s2 = s1.with_stack(s1.stack.push(lir::Val::I64(xb))); + super::trace::lemma_exec_app(lay, super::lower::sext_local(2), t, s1, s2); + lemma_bit_tail_trace(lay, k, s2, st1.stack, xa, xb); + } +} + +/// The low half of a left shift: shifting the sign extension and keeping 32 +/// bits is shifting the 32-bit value. +pub proof fn lemma_shl_low(ia: i32, c: u32) + requires + c < 32, + ensures + ((((ia as i64) as u64) << (c as u64)) & 0xFFFF_FFFFu64) == (((ia as u32) << c) as u64), + ((ia as int) * (vstd::arithmetic::power2::pow2(c as nat) as int)) % 4294967296int == (((ia as u32) << c) as int), +{ + assert(((((ia as i64) as u64) << (c as u64)) & 0xFFFF_FFFFu64) == (((ia as u32) << c) as u64)) by (bit_vector) + requires + c < 32, + ; + assert((((ia as int) * ((1u32 << c) as int)) % 4294967296int) == (((ia as u32) << c) as int)) by (bit_vector) + requires + c < 32, + ; + vstd::bits::lemma_u32_pow2_no_overflow(c as nat); + vstd::bits::lemma_u32_shl_is_mul(1u32, c); + assert((1u32 << c) as int == vstd::arithmetic::power2::pow2(c as nat) as int); +} + +/// LIR's `shl` of a word by a small count is the machine's. +pub proof fn lemma_shl64(x: u64, y: u64) + requires + y < 64, + ensures + lir::ibin64(lir::IBin::Shl, x, y) == (x << y), +{ + vstd::arithmetic::power2::lemma2_to64_rest(); + vstd::bits::lemma_u64_pow2_no_overflow(y as nat); + vstd::bits::lemma_u64_shl_is_mul(1u64, y); + assert((1u64 << y) as int == vstd::arithmetic::power2::pow2(y as nat) as int); + assert((y as int % 64) as nat == y as nat); + assert((((x as int) * ((1u64 << y) as int)) % 0x1_0000_0000_0000_0000int) as u64 == (x << y)) by (bit_vector) + requires + y < 64, + ; +} + +/// `wrap_i32` reduces modulo 2^32: its low 32 bits are `n mod 2^32`. +pub proof fn lemma_wrap_i32_low(n: int) + ensures + ((js::wrap_i32(n) as u32) as int) == n % 4294967296int, +{ + let m: int = n % 4294967296int; + assert(0 <= m < 4294967296int); + let mu = m as u32; + assert(mu as int == m); + if m >= 2147483648int { + assert(((((mu as int) - 4294967296int) as i32) as u32) == mu) by (bit_vector) + requires + mu >= 2147483648u32, + ; + } else { + assert((((mu as int) as i32) as u32) == mu) by (bit_vector) + requires + mu < 2147483648u32, + ; + } +} + +/// The arithmetic right shift's quotient is an int32. +pub proof fn lemma_shr_range(ia: i32, c: nat) + requires + c < 32, + ensures + -2147483648int <= (ia as int) / (vstd::arithmetic::power2::pow2(c) as int) <= 2147483647int, +{ + let d = vstd::arithmetic::power2::pow2(c) as int; + vstd::arithmetic::power2::lemma_pow2_pos(c); + assert(d >= 1); + vstd::arithmetic::div_mod::lemma_div_is_ordered(ia as int, 2147483647int, d); + vstd::arithmetic::div_mod::lemma_div_is_ordered_by_denominator(2147483647int, 1, d); + assert(2147483647int / 1 == 2147483647int) by { + vstd::arithmetic::div_mod::lemma_div_basics(2147483647int); + } + let q = (ia as int) / d; + vstd::arithmetic::div_mod::lemma_fundamental_div_mod(ia as int, d); + vstd::arithmetic::div_mod::lemma_mod_bound(ia as int, d); + let r = (ia as int) % d; + if q <= -2147483649int { + assert(d * (q + 2147483649int) <= 0) by (nonlinear_arith) + requires + d >= 1, + q + 2147483649int <= 0, + ; + assert(d * q + d * 2147483649int <= 0) by (nonlinear_arith) + requires + d * (q + 2147483649int) <= 0, + ; + assert(d * 2147483649int >= 2147483649int) by (nonlinear_arith) + requires + d >= 1, + ; + assert(false); + } +} + +/// LIR's arithmetic right shift of a sign-extended int32 by a small count is +/// the sign extension of the int32 shifted. +pub proof fn lemma_shrs64(ia: i32, y: u64) + requires + y < 32, + ensures + lir::ibin64(lir::IBin::ShrS, (ia as i64) as u64, y) == ((((ia as int) / (vstd::arithmetic::power2::pow2(y as nat) as int)) as i32) + as i64) as u64, +{ + let xa = (ia as i64) as u64; + super::arith::lemma_signed64(xa); + assert(((ia as i64) as u64) as i64 == (ia as i64)) by (bit_vector); + assert(lir::signed_of(64, xa as int) == ia as int); + lemma_shr_range(ia, y as nat); + let q: int = (ia as int) / (vstd::arithmetic::power2::pow2(y as nat) as int); + let r: i32 = q as i32; + assert(r as int == q); + vstd::arithmetic::power2::lemma2_to64_rest(); + assert((y as int % 64) as nat == y as nat); + assert((((r as int) % 0x1_0000_0000_0000_0000int) as u64) == ((r as i64) as u64)) by (bit_vector); +} + +/// The word of operation `k` is the encoding of the int32 result. +pub proof fn lemma_bit_word(k: nat, va: u64, vb: u64) + requires + k <= 4, + js::is_int32(va), + js::is_int32(vb), + ensures + bit_word(k, super::arith::sext(super::arith::lo32(va)), super::arith::sext(super::arith::lo32(vb))) + == js::enc_int32(bit_res(k, js::int32_of(va), js::int32_of(vb))), +{ + super::arith::lemma_payload(va); + super::arith::lemma_payload(vb); + let ia = js::int32_of(va); + let ib = js::int32_of(vb); + let xa = super::arith::sext(super::arith::lo32(va)); + let xb = super::arith::sext(super::arith::lo32(vb)); + assert(xa == (ia as i64) as u64); + assert(xb == (ib as i64) as u64); + if k == 0 { + assert(super::arith::tagged(xa & xb) == js::enc_int32(ia & ib)) by (bit_vector) + requires + xa == (ia as i64) as u64, + xb == (ib as i64) as u64, + ; + } else if k == 1 { + assert(super::arith::tagged(xa | xb) == js::enc_int32(ia | ib)) by (bit_vector) + requires + xa == (ia as i64) as u64, + xb == (ib as i64) as u64, + ; + } else if k == 2 { + assert(super::arith::tagged(xa ^ xb) == js::enc_int32(ia ^ ib)) by (bit_vector) + requires + xa == (ia as i64) as u64, + xb == (ib as i64) as u64, + ; + } else { + let y = xb & 31; + assert((xb & 31) == (((ib as u32) % 32u32) as u64)) by (bit_vector) + requires + xb == (ib as i64) as u64, + ; + let c: u32 = (ib as u32) % 32u32; + assert(y == c as u64); + assert(c as nat == shift_count(ib)); + if k == 3 { + lemma_shl64(xa, y); + lemma_shl_low(ia, c); + let n: int = ia as int * vstd::arithmetic::power2::pow2(shift_count(ib)) as int; + lemma_wrap_i32_low(n); + let r = js::wrap_i32(n); + let s = xa << y; + assert(((s & 0xFFFF_FFFFu64)) == (((ia as u32) << c) as u64)); + assert((r as u32) as int == (((ia as u32) << c) as int)); + assert((s & 0xFFFF_FFFFu64) == ((r as u32) as u64)) by (bit_vector) + requires + (s & 0xFFFF_FFFFu64) == (((ia as u32) << c) as u64), + (r as u32) as int == (((ia as u32) << c) as int), + ; + assert(super::arith::tagged(s) == js::enc_int32(r)) by (bit_vector) + requires + (s & 0xFFFF_FFFFu64) == ((r as u32) as u64), + ; + } else { + lemma_shrs64(ia, y); + let r: i32 = ((ia as int) / (vstd::arithmetic::power2::pow2(shift_count(ib)) as int)) as i32; + let s = lir::ibin64(lir::IBin::ShrS, xa, y); + assert(s == ((r as i64) as u64)); + assert(super::arith::tagged(s) == js::enc_int32(r)) by (bit_vector) + requires + s == ((r as i64) as u64), + ; + } + } +} + +/// The semantics of operation `k` on two int32 registers. +pub proof fn lemma_bit_sem(s: js::State, k: nat, dst: nat, a: nat, b: nat) + requires + k <= 4, + js::is_int32(js::reg(s, a)), + js::is_int32(js::reg(s, b)), + ensures + js::fast_path(s, bit_op(k, dst, a, b)), + js::fast_step(s, bit_op(k, dst, a, b)) == js::next( + js::set_reg( + s, + dst, + js::enc_int32(bit_res(k, js::int32_of(js::reg(s, a)), js::int32_of(js::reg(s, b)))), + ), + ), +{ + let va = js::reg(s, a); + let vb = js::reg(s, b); + let ia = js::int32_of(va); + let ib = js::int32_of(vb); + super::numord::lemma_to_int32(ia); + super::numord::lemma_to_int32(ib); + super::numord::lemma_to_uint32(ib); + assert(js::num_bits(va) == js::f64_of_int(ia as int)); + assert(js::num_bits(vb) == js::f64_of_int(ib as int)); + assert(js::is_number(va) && js::is_number(vb)); +} + +/// An int32 bitwise or shift op: the lowering refines `step`. +pub proof fn lemma_bit_op( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + k: nat, + dst: nat, + a: nat, + b: nat, +) + requires + super::proofs::pre(c, op, pc, lay, s, st, fuel), + k <= 4, + op == bit_op(k, dst, a, b), + js::reg_ok(js::cur_code(s), dst), + js::reg_ok(js::cur_code(s), a), + js::reg_ok(js::cur_code(s), b), + super::lower::lower(c, op, pc, lbl) == lower_bit(k, dst, a, b, op, pc), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_bit_val_simple(k); + let va = js::reg(s, a); + let vb = js::reg(s, b); + let w = js::enc_int32(bit_res(k, js::int32_of(va), js::int32_of(vb))); + if js::is_int32(va) && js::is_int32(vb) { + lemma_bit_sem(s, k, dst, a, b); + lemma_bit_val_trace(lay, k, va, vb); + lemma_bit_word(k, va, vb); + } + super::blocks::lemma_bin_op(op, pc, lbl, lay, s, st, fuel, dst, a, b, bit_val(k), w); +} + +} // verus! diff --git a/crates/clazel-jit/src/front/blocks.rs b/crates/clazel-jit/src/front/blocks.rs new file mode 100644 index 000000000..318188841 --- /dev/null +++ b/crates/clazel-jit/src/front/blocks.rs @@ -0,0 +1,652 @@ +//! The generic pieces the second slice's lowerings are built from, and the +//! lemmas about them that do not depend on the op. +//! +//! # Shape of a lowering +//! +//! A lowering is a *prefix* of simple instructions that leaves a guard flag +//! (an `i32`) on the stack, then [`gx`], which falls through when the flag +//! is non-zero and exits to the op's handler otherwise, then the *rest* +//! ([`guarded`]). Guards nest by making the rest another [`guarded`]. This is +//! the form of the first slice's `if`-based lowerings with the exit moved +//! out of an `if` block: the rest then runs at the lowering's own label depth +//! (a `br_if` in it targets the same label the jumps of the first slice do), +//! and the proof is one lemma ([`lemma_guarded`]) instead of one per nesting. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `below` of a stack with one value pushed. +pub broadcast proof fn lemma_below_push1(s: Seq, x: A) + ensures + #[trigger] lir::below(s.push(x), 1) == s, +{ + assert(lir::below(s.push(x), 1) =~= s); +} + +/// `below` of a stack with two values pushed. +pub broadcast proof fn lemma_below_push2(s: Seq, x: A, y: A) + ensures + #[trigger] lir::below(s.push(x).push(y), 2) == s, +{ + assert(lir::below(s.push(x).push(y), 2) =~= s); +} + +/// `drop_last` of a stack with one value pushed. +pub broadcast proof fn lemma_drop_last_push(s: Seq, x: A) + ensures + #[trigger] s.push(x).drop_last() == s, +{ + assert(s.push(x).drop_last() =~= s); +} + +/// The guard's exit: fall through (`Nop`) when the flag on the stack is +/// non-zero, exit to the handler of `op` (from the op at `pc`) otherwise. +pub open spec fn gx(op: js::Op, pc: nat) -> lir::Instr { + lir::Instr::If( + super::lower::bt0(), + Box::new(lir::Instr::Nop), + Box::new(super::run::chain(super::lower::exit_ins(js::op_handler(op), pc))), + ) +} + +/// The prefix `p` (simple instructions leaving the guard flag), the guard's +/// exit, then `rest`. +pub open spec fn guarded(p: Seq, op: js::Op, pc: nat, rest: lir::Instr) -> lir::Instr { + super::run::then(super::run::chain(p), super::run::then(gx(op, pc), rest)) +} + +/// The meaning of [`guarded`]: when the prefix leaves a zero flag the +/// outcome is an exit that refines `step`; otherwise `rest` runs from the +/// state with the flag popped, with two units less fuel. +pub proof fn lemma_guarded( + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + st2: lir::St, + p: Seq, + rest: lir::Instr, + g: u32, + fuel: nat, +) + requires + rel(lay, s, st2), + s.pc == pc, + handler_complete(s, op), + super::run::all_simple(p), + fuel > p.len() + 8, + super::run::exec(lay, p, st) == lir::Outcome::Normal(st2), + st2.stack == seq![lir::Val::I32(g)], + ensures + g == 0 ==> outcome_refines(lay, s, op, lbl, lir::eval(lay, guarded(p, op, pc, rest), st, fuel)), + g != 0 ==> lir::eval(lay, guarded(p, op, pc, rest), st, fuel) == lir::eval( + lay, + rest, + st2.with_stack(Seq::::empty()), + (fuel - 2) as nat, + ), +{ + super::run::lemma_chain(lay, p, st, (fuel - 1) as nat); + let exit = super::run::chain(super::lower::exit_ins(js::op_handler(op), pc)); + let st3 = st2.with_stack(Seq::::empty()); + reveal_with_fuel(lir::eval, 3); + super::intops::lemma_if0(lay, lir::Instr::Nop, exit, st2, g, (fuel - 2) as nat); + if g == 0 { + assert(rel(lay, s, st3)); + super::intops::lemma_exit_chain(op, pc, lay, s, st3, (fuel - 4) as nat); + } else { + assert(lir::eval(lay, lir::Instr::Nop, st3, (fuel - 4) as nat) == lir::Outcome::Normal(st3)); + } +} + +/// `eval` of a chain of simple instructions whose last element is not +/// needed by the caller: a thin wrapper that restates [`super::run::lemma_chain`] +/// for a chain that is the whole `rest` of a guard. +pub proof fn lemma_rest_chain(lay: lir::Layout, ins: Seq, st: lir::St, fuel: nat) + requires + super::run::all_simple(ins), + fuel > ins.len(), + ensures + lir::eval(lay, super::run::chain(ins), st, fuel) == super::run::exec(lay, ins, st), +{ + super::run::lemma_chain(lay, ins, st, fuel); +} + +/// A value block computes the word `w` from the operand words `va` and `vb` +/// held in scratch locals 1 and 2: from any state with those locals it +/// pushes `w` and changes nothing else. +pub open spec fn val_computes(lay: lir::Layout, val: Seq, va: u64, vb: u64, w: u64) -> bool { + forall|st1: lir::St| + #![trigger super::run::exec(lay, val, st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) && st1.locals[2] + == lir::Val::I64(vb) ==> super::run::exec(lay, val, st1) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I64(w))), + ) +} + +/// A value block computes the word `w` from the one operand word `va` held +/// in scratch local 1. +pub open spec fn val_computes1(lay: lir::Layout, val: Seq, va: u64, w: u64) -> bool { + forall|st1: lir::St| + #![trigger super::run::exec(lay, val, st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) + ==> super::run::exec(lay, val, st1) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I64(w))), + ) +} + +/// A store of `v` in register `dst` by the value block `val`, after which the +/// op falls through. [`super::proofs::lemma_store_reg_outcome`] with a fuel +/// bound that a nested lowering can meet. +pub proof fn lemma_store_outcome( + op: js::Op, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + dst: nat, + val: Seq, + v: u64, +) + requires + rel(lay, s, st), + st.stack == Seq::::empty(), + js::state_wf(s), + js::reg_ok(js::cur_code(s), dst), + super::run::all_simple(val), + val.len() < 40, + fuel > 6 + val.len(), + js::fast_path(s, op), + js::fast_step(s, op) == js::next(js::set_reg(s, dst, v)), + ({ + let sa = st.with_stack(st.stack.push(lir::Val::I64((8 * s.fp + reg_off(dst)) as u64))); + super::run::exec(lay, val, sa) == lir::Outcome::Normal( + sa.with_stack(sa.stack.push(lir::Val::I64(v))), + ) + }), + ensures + outcome_refines( + lay, + s, + op, + lbl, + lir::eval(lay, super::run::chain(super::lower::store_ins(reg_off(dst), val)), st, fuel), + ), +{ + super::proofs::lemma_reg_slot_ok(s, dst); + let off = reg_off(dst); + let ins = super::lower::store_ins(off, val); + super::proofs::lemma_blocks_simple(off, 0); + assert(super::run::all_simple(ins)) by { + assert forall|i: int| 0 <= i < ins.len() implies super::run::is_simple(#[trigger] ins[i]) by { + if i < 4 { + assert(ins[i] == super::lower::addr_ins(off)[i]); + } else if i < 4 + val.len() { + assert(ins[i] == val[i - 4]); + } else { + assert(i == 4 + val.len()); + } + } + } + assert(ins.len() == 5 + val.len()); + super::run::lemma_chain(lay, ins, st, fuel); + let sa = st.with_stack(st.stack.push(lir::Val::I64((8 * s.fp + reg_off(dst)) as u64))); + let mid = sa.with_stack(sa.stack.push(lir::Val::I64(v))); + assert(locals_rel(s, mid.locals)); + super::frame::lemma_exec_store(lay, s, st, off, val, v, mid); + let j = (s.fp + off / 8) as int; + let s2 = js::fast_step(s, op); + assert(s2.region == s.region.update(j, v)); + assert(s2 == js::State { pc: s.pc + 1, ..js::State { region: s.region.update(j, v), ..s } }); + super::proofs::lemma_rel_pc(lay, js::State { region: s.region.update(j, v), ..s }, lir::St { + stack: st.stack, + locals: mid.locals, + mem: lir::write_le(st.mem, slot_addr(lay, j), v as nat, 8), + }, s.pc + 1); +} + +/// The binary prefix: both registers loaded into scratch locals 1 and 2 and +/// the flag "both are int32 words" left on the stack. +pub proof fn lemma_pre1(lay: lir::Layout, s: js::State, st: lir::St, a: nat, b: nat) + requires + rel(lay, s, st), + st.stack == Seq::::empty(), + js::state_wf(s), + js::reg_ok(js::cur_code(s), a), + js::reg_ok(js::cur_code(s), b), + ensures + ({ + let va = js::reg(s, a); + let vb = js::reg(s, b); + let pre = super::lower::operands_ins(a, b) + super::lower::guard1_ins(); + let g1 = lir::ibin32( + lir::IBin::And, + lir::bool_i32((va >> 32u64) == super::arith::tag_hi()), + lir::bool_i32((vb >> 32u64) == super::arith::tag_hi()), + ); + let st3 = lir::St { + stack: seq![lir::Val::I32(g1)], + locals: st.locals.update(1, lir::Val::I64(va)).update(2, lir::Val::I64(vb)), + mem: st.mem, + }; + &&& super::run::all_simple(pre) + &&& pre.len() == 23 + &&& super::run::exec(lay, pre, st) == lir::Outcome::Normal(st3) + &&& (g1 != 0) <==> (js::is_int32(va) && js::is_int32(vb)) + &&& rel(lay, s, st3) + &&& st3.locals.len() == nlocals() + &&& st3.locals[1] == lir::Val::I64(va) + &&& st3.locals[2] == lir::Val::I64(vb) + }), +{ + let va = js::reg(s, a); + let vb = js::reg(s, b); + super::intops::lemma_arith_blocks_simple(0, a, b, 0); + super::trace::lemma_operands_trace(lay, s, st, a, b); + let st2 = lir::St { + stack: Seq::::empty(), + locals: st.locals.update(1, lir::Val::I64(va)).update(2, lir::Val::I64(vb)), + mem: st.mem, + }; + super::intops::lemma_locals_after_operands(s, st.locals, va, vb); + super::trace::lemma_exec_app(lay, super::lower::operands_ins(a, b), super::lower::guard1_ins(), st, st2); + super::trace::lemma_guard1_trace(lay, st2, va, vb); +} + +/// A binary lowering: the prefix of [`super::blocks::lemma_pre1`], the guard +/// exit, then a store of the value block's word in `dst`. +pub open spec fn lower_bin(val: Seq, dst: nat, a: nat, b: nat, op: js::Op, pc: nat) -> lir::Instr { + guarded( + super::lower::operands_ins(a, b) + super::lower::guard1_ins(), + op, + pc, + super::run::chain(super::lower::store_ins(reg_off(dst), val)), + ) +} + +/// A binary lowering of two int32 operands whose value block computes the +/// word `w` refines `step`: the guard sends everything but two int32 words to +/// the handler (whose result is `step`'s by [`handler_complete`]), and for two +/// int32 words the op's fast path holds and its result is `w` in `dst`. +pub proof fn lemma_bin_op( + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + dst: nat, + a: nat, + b: nat, + val: Seq, + w: u64, +) + requires + fuel >= 100, + rel(lay, s, st), + st.stack == Seq::::empty(), + js::state_wf(s), + s.pc == pc, + handler_complete(s, op), + js::reg_ok(js::cur_code(s), dst), + js::reg_ok(js::cur_code(s), a), + js::reg_ok(js::cur_code(s), b), + super::run::all_simple(val), + val.len() < 40, + js::is_int32(js::reg(s, a)) && js::is_int32(js::reg(s, b)) ==> js::fast_path(s, op) + && js::fast_step(s, op) == js::next(js::set_reg(s, dst, w)), + js::is_int32(js::reg(s, a)) && js::is_int32(js::reg(s, b)) ==> val_computes( + lay, + val, + js::reg(s, a), + js::reg(s, b), + w, + ), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, lower_bin(val, dst, a, b, op, pc), st, fuel)), +{ + lemma_pre1(lay, s, st, a, b); + let va = js::reg(s, a); + let vb = js::reg(s, b); + let pre = super::lower::operands_ins(a, b) + super::lower::guard1_ins(); + let g1 = lir::ibin32( + lir::IBin::And, + lir::bool_i32((va >> 32u64) == super::arith::tag_hi()), + lir::bool_i32((vb >> 32u64) == super::arith::tag_hi()), + ); + let st3 = lir::St { + stack: seq![lir::Val::I32(g1)], + locals: st.locals.update(1, lir::Val::I64(va)).update(2, lir::Val::I64(vb)), + mem: st.mem, + }; + let rest = super::run::chain(super::lower::store_ins(reg_off(dst), val)); + lemma_guarded(op, pc, lbl, lay, s, st, st3, pre, rest, g1, fuel); + if g1 != 0 { + let st4 = st3.with_stack(Seq::::empty()); + assert(rel(lay, s, st4)); + assert(js::is_int32(va) && js::is_int32(vb)); + super::proofs::lemma_reg_slot_ok(s, dst); + let sa = st4.with_stack(st4.stack.push(lir::Val::I64((8 * s.fp + reg_off(dst)) as u64))); + assert(sa.locals == st3.locals); + assert(sa.locals.len() == nlocals()); + assert(sa.locals[1] == lir::Val::I64(va)); + assert(sa.locals[2] == lir::Val::I64(vb)); + assert(super::run::exec(lay, val, sa) == lir::Outcome::Normal( + sa.with_stack(sa.stack.push(lir::Val::I64(w))), + )); + lemma_store_outcome(op, lbl, lay, s, st4, (fuel - 2) as nat, dst, val, w); + } +} + +/// A flag block computes the `i32` flag `g` from the operand word `va` held +/// in scratch local 1: it pushes `g` and changes nothing else. +pub open spec fn flag_computes(lay: lir::Layout, fl: Seq, va: u64, g: u32) -> bool { + forall|st1: lir::St| + #![trigger super::run::exec(lay, fl, st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) + ==> super::run::exec(lay, fl, st1) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I32(g))), + ) +} + +/// The unary prefix: the register loaded into scratch local 1, then the flag +/// block `fl`. +pub open spec fn un_pre(src: nat, fl: Seq) -> Seq { + super::lower::load_ins(reg_off(src)) + (seq![lir::Instr::LocalSet(1)] + fl) +} + +/// A unary lowering: the unary prefix, the guard exit, then a store of the +/// value block's word in `dst`. +pub open spec fn lower_un( + fl: Seq, + val: Seq, + dst: nat, + src: nat, + op: js::Op, + pc: nat, +) -> lir::Instr { + guarded( + un_pre(src, fl), + op, + pc, + super::run::chain(super::lower::store_ins(reg_off(dst), val)), + ) +} + +/// The unary prefix: leaves the flag `g` on the stack, with the operand in +/// local 1. +pub proof fn lemma_pre_un( + lay: lir::Layout, + s: js::State, + st: lir::St, + src: nat, + fl: Seq, + g: u32, +) + requires + rel(lay, s, st), + st.stack == Seq::::empty(), + js::state_wf(s), + js::reg_ok(js::cur_code(s), src), + super::run::all_simple(fl), + fl.len() < 40, + flag_computes(lay, fl, js::reg(s, src), g), + ensures + ({ + let v = js::reg(s, src); + let pre = un_pre(src, fl); + let st3 = lir::St { + stack: seq![lir::Val::I32(g)], + locals: st.locals.update(1, lir::Val::I64(v)), + mem: st.mem, + }; + &&& super::run::all_simple(pre) + &&& pre.len() == 6 + fl.len() + &&& super::run::exec(lay, pre, st) == lir::Outcome::Normal(st3) + &&& rel(lay, s, st3) + &&& st3.locals.len() == nlocals() + &&& st3.locals[1] == lir::Val::I64(v) + }), +{ + broadcast use lemma_below_push1, lemma_below_push2, lemma_drop_last_push; + + let v = js::reg(s, src); + super::proofs::lemma_reg_slot_ok(s, src); + super::proofs::lemma_blocks_simple(reg_off(src), 0); + let l = super::lower::load_ins(reg_off(src)); + let set1 = seq![lir::Instr::LocalSet(1)]; + assert(super::run::all_simple(set1)) by { + assert forall|i: int| 0 <= i < set1.len() implies super::run::is_simple(#[trigger] set1[i]) by { + assert(i == 0); + } + } + super::intops::lemma_simple_concat(set1, fl); + super::intops::lemma_simple_concat(l, set1 + fl); + super::frame::lemma_exec_load(lay, s, st, reg_off(src)); + let sl = st.with_stack(st.stack.push(lir::Val::I64(v))); + super::trace::lemma_exec_app(lay, l, set1 + fl, st, sl); + reveal_with_fuel(super::run::exec, 2); + let loc = st.locals.update(1, lir::Val::I64(v)); + let sb = lir::St { stack: Seq::::empty(), locals: loc, mem: st.mem }; + assert(super::run::exec(lay, set1, sl) == lir::Outcome::Normal(sb)); + super::trace::lemma_exec_app(lay, set1, fl, sl, sb); + super::trace::lemma_locals_set(s, st.locals, 1, v); + assert(rel(lay, s, sb)); + assert(sb.locals.len() == nlocals()); + assert(sb.locals[1] == lir::Val::I64(v)); + assert(super::run::exec(lay, fl, sb) == lir::Outcome::Normal(sb.with_stack(sb.stack.push(lir::Val::I32(g))))); + assert(sb.stack.push(lir::Val::I32(g)) =~= seq![lir::Val::I32(g)]); +} + +/// A unary lowering whose flag block computes `g` and whose value block +/// computes `w` refines `step`: a zero flag exits to the handler, otherwise +/// the op's fast path holds and its result is `w` in `dst`. +pub proof fn lemma_un_op( + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + dst: nat, + src: nat, + fl: Seq, + val: Seq, + g: u32, + w: u64, +) + requires + fuel >= 100, + rel(lay, s, st), + st.stack == Seq::::empty(), + js::state_wf(s), + s.pc == pc, + handler_complete(s, op), + js::reg_ok(js::cur_code(s), dst), + js::reg_ok(js::cur_code(s), src), + super::run::all_simple(fl), + fl.len() < 40, + super::run::all_simple(val), + val.len() < 40, + flag_computes(lay, fl, js::reg(s, src), g), + g != 0 ==> js::fast_path(s, op) && js::fast_step(s, op) == js::next(js::set_reg(s, dst, w)), + g != 0 ==> val_computes1(lay, val, js::reg(s, src), w), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, lower_un(fl, val, dst, src, op, pc), st, fuel)), +{ + lemma_pre_un(lay, s, st, src, fl, g); + let v = js::reg(s, src); + let pre = un_pre(src, fl); + let st3 = lir::St { + stack: seq![lir::Val::I32(g)], + locals: st.locals.update(1, lir::Val::I64(v)), + mem: st.mem, + }; + let rest = super::run::chain(super::lower::store_ins(reg_off(dst), val)); + lemma_guarded(op, pc, lbl, lay, s, st, st3, pre, rest, g, fuel); + if g != 0 { + let st4 = st3.with_stack(Seq::::empty()); + assert(rel(lay, s, st4)); + super::proofs::lemma_reg_slot_ok(s, dst); + let sa = st4.with_stack(st4.stack.push(lir::Val::I64((8 * s.fp + reg_off(dst)) as u64))); + assert(sa.locals == st3.locals); + assert(sa.locals.len() == nlocals()); + assert(sa.locals[1] == lir::Val::I64(v)); + assert(super::run::exec(lay, val, sa) == lir::Outcome::Normal( + sa.with_stack(sa.stack.push(lir::Val::I64(w))), + )); + lemma_store_outcome(op, lbl, lay, s, st4, (fuel - 2) as nat, dst, val, w); + } +} + +/// An extra flag block of a binary lowering: from the stack `base` + the +/// first guard's flag `g1`, and the operand words in locals 1 and 2, it leaves +/// `base` + `g1 & f`. +pub open spec fn flag_and_computes(lay: lir::Layout, ex: Seq, va: u64, vb: u64, f: u32) -> bool { + forall|st1: lir::St, base: Seq, g1: u32| + #![trigger super::run::exec(lay, ex, st1), base.push(lir::Val::I32(g1))] + st1.stack == base.push(lir::Val::I32(g1)) && st1.locals.len() == nlocals() && st1.locals[1] + == lir::Val::I64(va) && st1.locals[2] == lir::Val::I64(vb) ==> super::run::exec(lay, ex, st1) + == lir::Outcome::Normal( + st1.with_stack(base.push(lir::Val::I32(lir::ibin32(lir::IBin::And, g1, f)))), + ) +} + +/// A binary lowering with a second flag: the operands, the int32 tests, the +/// extra flag block `ex` (which `and`s its flag in), the guard exit, then a +/// store of the value block's word in `dst`. +pub open spec fn lower_bin2( + ex: Seq, + val: Seq, + dst: nat, + a: nat, + b: nat, + op: js::Op, + pc: nat, +) -> lir::Instr { + guarded( + super::lower::operands_ins(a, b) + (super::lower::guard1_ins() + ex), + op, + pc, + super::run::chain(super::lower::store_ins(reg_off(dst), val)), + ) +} + +/// [`lemma_bin_op`] with a second flag: a zero flag (an operand that is not +/// an int32, or the extra flag clear) exits; otherwise the fast path holds +/// and the result is `w`. +pub proof fn lemma_bin_op2( + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + dst: nat, + a: nat, + b: nat, + ex: Seq, + val: Seq, + f: u32, + w: u64, +) + requires + fuel >= 100, + rel(lay, s, st), + st.stack == Seq::::empty(), + js::state_wf(s), + s.pc == pc, + handler_complete(s, op), + js::reg_ok(js::cur_code(s), dst), + js::reg_ok(js::cur_code(s), a), + js::reg_ok(js::cur_code(s), b), + super::run::all_simple(ex), + ex.len() < 30, + super::run::all_simple(val), + val.len() < 40, + flag_and_computes(lay, ex, js::reg(s, a), js::reg(s, b), f), + js::is_int32(js::reg(s, a)) && js::is_int32(js::reg(s, b)) && f != 0 ==> js::fast_path(s, op) + && js::fast_step(s, op) == js::next(js::set_reg(s, dst, w)), + js::is_int32(js::reg(s, a)) && js::is_int32(js::reg(s, b)) && f != 0 ==> val_computes( + lay, + val, + js::reg(s, a), + js::reg(s, b), + w, + ), + f == 0 || f == 1, + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, lower_bin2(ex, val, dst, a, b, op, pc), st, fuel)), +{ + lemma_pre1(lay, s, st, a, b); + let va = js::reg(s, a); + let vb = js::reg(s, b); + let pre1 = super::lower::operands_ins(a, b) + super::lower::guard1_ins(); + let g1 = lir::ibin32( + lir::IBin::And, + lir::bool_i32((va >> 32u64) == super::arith::tag_hi()), + lir::bool_i32((vb >> 32u64) == super::arith::tag_hi()), + ); + let st3 = lir::St { + stack: seq![lir::Val::I32(g1)], + locals: st.locals.update(1, lir::Val::I64(va)).update(2, lir::Val::I64(vb)), + mem: st.mem, + }; + let o = super::lower::operands_ins(a, b); + let g1i = super::lower::guard1_ins(); + let pre = o + (g1i + ex); + assert(pre =~= pre1 + ex); + super::intops::lemma_simple_concat(pre1, ex); + super::trace::lemma_exec_app(lay, pre1, ex, st, st3); + let g = lir::ibin32(lir::IBin::And, g1, f); + let st5 = st3.with_stack(Seq::::empty().push(lir::Val::I32(g))); + let b0 = Seq::::empty(); + assert(st3.stack == b0.push(lir::Val::I32(g1))); + assert(super::run::exec(lay, ex, st3) == lir::Outcome::Normal(st5)); + assert(st5.stack =~= seq![lir::Val::I32(g)]); + assert(super::run::exec(lay, pre, st) == lir::Outcome::Normal(st5)); + assert(rel(lay, s, st5)); + let rest = super::run::chain(super::lower::store_ins(reg_off(dst), val)); + assert(pre.len() == 23 + ex.len()); + lemma_guarded(op, pc, lbl, lay, s, st, st5, pre, rest, g, fuel); + if g != 0 { + let st4 = st5.with_stack(Seq::::empty()); + assert(rel(lay, s, st4)); + super::trace::lemma_guard1_trace(lay, st3.with_stack(Seq::::empty()), va, vb); + assert((g1 != 0) <==> (js::is_int32(va) && js::is_int32(vb))); + let e1 = g1; + assert(e1 == 0 || e1 == 1) by { + let t1 = lir::bool_i32((va >> 32u64) == super::arith::tag_hi()); + let t2 = lir::bool_i32((vb >> 32u64) == super::arith::tag_hi()); + assert((t1 & t2) == 0 || (t1 & t2) == 1) by (bit_vector) + requires + t1 == 0 || t1 == 1, + t2 == 0 || t2 == 1, + ; + } + assert(((e1 & f) != 0) ==> (e1 != 0 && f != 0)) by (bit_vector); + assert(js::is_int32(va) && js::is_int32(vb) && f != 0); + super::proofs::lemma_reg_slot_ok(s, dst); + let sa = st4.with_stack(st4.stack.push(lir::Val::I64((8 * s.fp + reg_off(dst)) as u64))); + assert(sa.locals == st3.locals); + assert(sa.locals.len() == nlocals()); + assert(sa.locals[1] == lir::Val::I64(va)); + assert(sa.locals[2] == lir::Val::I64(vb)); + assert(super::run::exec(lay, val, sa) == lir::Outcome::Normal( + sa.with_stack(sa.stack.push(lir::Val::I64(w))), + )); + lemma_store_outcome(op, lbl, lay, s, st4, (fuel - 2) as nat, dst, val, w); + } +} + +} // verus! diff --git a/crates/clazel-jit/src/front/contract.rs b/crates/clazel-jit/src/front/contract.rs new file mode 100644 index 000000000..ffcc34b5c --- /dev/null +++ b/crates/clazel-jit/src/front/contract.rs @@ -0,0 +1,67 @@ +//! What the front end must be proven to satisfy (CLAUDE.md rule 3, +//! docs/SPEC-STYLE.md): the refinement statement, in terms of +//! [`super::spec`] and the two semantics it relates. It is written before any +//! lowering (the lowerings are the next commit); reviewing it and `spec` is +//! enough to know what is claimed. +//! +//! # The refinement statement +//! +//! [`lowers`]`(c, op, pc, lbl, code)` says that the LIR instruction `code` is +//! a correct lowering of the bytecode op `op` at index `pc` of a function whose +//! code is `c`, with `lbl` the label a jump goes to. (`pc` is in the lowering +//! because an exit tells the handler where it is: modelling choice 7.) For +//! every layout, every bytecode state +//! `s` and LIR state `st` that are related ([`rel`]), where +//! +//! - LIR's operand stack is empty, +//! - `s` is well-formed ([`js::state_wf`]), its function is `c`, its `pc` is +//! `pc`, and the op there is `op` (so the host's handler call reads the same +//! op), and +//! - the handlers are complete at `s` ([`handler_complete`], the one +//! hypothesis about code outside this crate, modelling choice 8), +//! +//! then for every amount of fuel from [`lower_fuel`], LIR's `eval` of `code` +//! has an outcome [`outcome_refines`] allows: fall through, branch or exit, +//! each as that function says, and never `Stuck` or out of fuel. +//! +//! That is a **forward simulation of one bytecode step by one lowering, with +//! the exit as the handler call**, in the shape of `step` itself: +//! `step(s, op)` is `fast_step` when `fast_path` holds and the handler's +//! result otherwise, and the lowering reaches the first by falling through or +//! branching and the second by exiting. A whole function is the sequence of +//! these, composed by the later slice that lays out labels. +//! +//! # What is not stated +//! +//! - **That the handler is right.** The exit's claim is "the handler's +//! result is `step`'s", true of `step` by definition when `fast_path` +//! fails and by [`handler_complete`] otherwise. What a handler does is +//! 7.8's and Test262's. +//! - **That the LIR type-checks.** The proof shows directly that evaluation +//! is never stuck from a related state; LIR's typing judgment is not applied. +//! - **A whole function, a call, a return, tiering and entry at a loop +//! head.** One op at a time. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `code` is a correct lowering of `op` (see the module doc). +pub open spec fn lowers(c: js::Code, op: js::Op, pc: nat, lbl: nat, code: lir::Instr) -> bool { + forall|lay: lir::Layout, s: js::State, st: lir::St, fuel: nat| + #![trigger lir::eval(lay, code, st, fuel), rel(lay, s, st)] + fuel >= lower_fuel() && rel(lay, s, st) && st.stack == Seq::::empty() + && js::state_wf(s) && js::cur_code(s) == c && s.pc == pc && js::fetch(s) == op + && handler_complete(s, op) ==> outcome_refines( + lay, + s, + op, + lbl, + lir::eval(lay, code, st, fuel), + ) +} + +} // verus! diff --git a/crates/clazel-jit/src/front/dispatch.rs b/crates/clazel-jit/src/front/dispatch.rs new file mode 100644 index 000000000..52b760f64 --- /dev/null +++ b/crates/clazel-jit/src/front/dispatch.rs @@ -0,0 +1,227 @@ +//! The dispatch: the refinement theorem of this slice, [`lemma_lower_refines`], +//! by cases on the op, each case one of the lemmas of [`super::proofs`] and +//! [`super::intops`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + + +/// The outcome of the lowering of one lowered op from related states. +pub proof fn lemma_op_case( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, +) + requires + super::proofs::pre(c, op, pc, lay, s, st, fuel), + super::lower::lowered(op), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + super::proofs::lemma_state_facts(s); + let cc = js::cur_code(s); + assert(js::op_wf(cc, op)); + if js::op_class(op) is Handler { + super::proofs::lemma_handler_op(c, op, pc, lbl, lay, s, st, fuel); + } else { + assert(js::op_class(op) is Core || js::op_class(op) is Guarded); + match op { + js::Op::Nop => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + assert(super::lower::lower(c, op, pc, lbl) == lir::Instr::Nop); + super::proofs::lemma_rel_pc(lay, s, st, s.pc + 1); + }, + js::Op::Move { dst, src } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_reg_slot_ok(s, src); + super::proofs::lemma_copy_op(c, op, pc, lbl, lay, s, st, fuel, dst, reg_off(src)); + }, + js::Op::LoadConst { dst, k } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_const_op(c, op, pc, lbl, lay, s, st, fuel, dst, c.consts[k as int]); + }, + js::Op::LoadInt { dst, imm } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_const_op(c, op, pc, lbl, lay, s, st, fuel, dst, js::enc_int32(imm)); + }, + js::Op::LoadUndefined { dst } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_const_op(c, op, pc, lbl, lay, s, st, fuel, dst, js::UNDEFINED); + }, + js::Op::LoadNull { dst } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_const_op(c, op, pc, lbl, lay, s, st, fuel, dst, js::NULL); + }, + js::Op::LoadBool { dst, flag } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_const_op(c, op, pc, lbl, lay, s, st, fuel, dst, js::enc_bool(flag)); + }, + js::Op::LoadHole { dst } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_const_op(c, op, pc, lbl, lay, s, st, fuel, dst, js::HOLE); + }, + js::Op::GetThis { dst } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_hdr_slot_ok(s, js::hdr_this()); + super::proofs::lemma_copy_op(c, op, pc, lbl, lay, s, st, fuel, dst, hdr_off(js::hdr_this())); + }, + js::Op::GetCallee { dst } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_hdr_slot_ok(s, js::hdr_callee()); + super::proofs::lemma_copy_op(c, op, pc, lbl, lay, s, st, fuel, dst, hdr_off(js::hdr_callee())); + }, + js::Op::Jump { target } => { + super::proofs::lemma_jump_op(c, op, pc, lbl, lay, s, st, fuel, target); + }, + js::Op::JumpIfUndefined { src, target } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_jump_if_is(c, op, pc, lbl, lay, s, st, fuel, src, js::UNDEFINED, true, target); + }, + js::Op::JumpIfNotUndefined { src, target } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_jump_if_is(c, op, pc, lbl, lay, s, st, fuel, src, js::UNDEFINED, false, target); + }, + js::Op::JumpIfNullish { src, target } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_jump_nullish(c, op, pc, lbl, lay, s, st, fuel, src, false, target); + }, + js::Op::JumpIfNotNullish { src, target } => { + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + super::proofs::lemma_jump_nullish(c, op, pc, lbl, lay, s, st, fuel, src, true, target); + }, + js::Op::Add { dst, a, b } => { + assert(jump_target(op) is None); + super::intops::lemma_arith_op(c, op, pc, lbl, lay, s, st, fuel, 0, dst, a, b); + }, + js::Op::Sub { dst, a, b } => { + assert(jump_target(op) is None); + super::intops::lemma_arith_op(c, op, pc, lbl, lay, s, st, fuel, 1, dst, a, b); + }, + js::Op::Mul { dst, a, b } => { + assert(jump_target(op) is None); + super::intops::lemma_arith_op(c, op, pc, lbl, lay, s, st, fuel, 2, dst, a, b); + }, + js::Op::Lt { dst, a, b } => { + assert(jump_target(op) is None); + super::intcmp::lemma_cmp_op(c, op, pc, lbl, lay, s, st, fuel, 0, dst, a, b); + }, + js::Op::Le { dst, a, b } => { + assert(jump_target(op) is None); + super::intcmp::lemma_cmp_op(c, op, pc, lbl, lay, s, st, fuel, 1, dst, a, b); + }, + js::Op::Gt { dst, a, b } => { + assert(jump_target(op) is None); + super::intcmp::lemma_cmp_op(c, op, pc, lbl, lay, s, st, fuel, 2, dst, a, b); + }, + js::Op::Ge { dst, a, b } => { + assert(jump_target(op) is None); + super::intcmp::lemma_cmp_op(c, op, pc, lbl, lay, s, st, fuel, 3, dst, a, b); + }, + js::Op::StrictEq { dst, a, b } => { + assert(jump_target(op) is None); + super::intcmp::lemma_cmp_op(c, op, pc, lbl, lay, s, st, fuel, 4, dst, a, b); + }, + js::Op::StrictNe { dst, a, b } => { + assert(jump_target(op) is None); + super::intcmp::lemma_cmp_op(c, op, pc, lbl, lay, s, st, fuel, 5, dst, a, b); + }, + js::Op::BitAnd { dst, a, b } => { + assert(jump_target(op) is None); + super::bitops::lemma_bit_op(c, op, pc, lbl, lay, s, st, fuel, 0, dst, a, b); + }, + js::Op::BitOr { dst, a, b } => { + assert(jump_target(op) is None); + super::bitops::lemma_bit_op(c, op, pc, lbl, lay, s, st, fuel, 1, dst, a, b); + }, + js::Op::BitXor { dst, a, b } => { + assert(jump_target(op) is None); + super::bitops::lemma_bit_op(c, op, pc, lbl, lay, s, st, fuel, 2, dst, a, b); + }, + js::Op::Shl { dst, a, b } => { + assert(jump_target(op) is None); + super::bitops::lemma_bit_op(c, op, pc, lbl, lay, s, st, fuel, 3, dst, a, b); + }, + js::Op::Shr { dst, a, b } => { + assert(jump_target(op) is None); + super::bitops::lemma_bit_op(c, op, pc, lbl, lay, s, st, fuel, 4, dst, a, b); + }, + js::Op::Inc { dst, src } => { + assert(jump_target(op) is None); + super::unops::lemma_un_op_k(c, op, pc, lbl, lay, s, st, fuel, 0, dst, src); + }, + js::Op::Dec { dst, src } => { + assert(jump_target(op) is None); + super::unops::lemma_un_op_k(c, op, pc, lbl, lay, s, st, fuel, 1, dst, src); + }, + js::Op::Neg { dst, src } => { + assert(jump_target(op) is None); + super::unops::lemma_un_op_k(c, op, pc, lbl, lay, s, st, fuel, 2, dst, src); + }, + js::Op::BitNot { dst, src } => { + assert(jump_target(op) is None); + super::unops::lemma_un_op_k(c, op, pc, lbl, lay, s, st, fuel, 3, dst, src); + }, + js::Op::Not { dst, src } => { + assert(jump_target(op) is None); + super::truth::lemma_not_op(c, op, pc, lbl, lay, s, st, fuel, dst, src); + }, + js::Op::JumpIfTrue { cond, target } => { + super::truth::lemma_jump_op(c, op, pc, lbl, lay, s, st, fuel, false, cond, target); + }, + js::Op::JumpIfFalse { cond, target } => { + super::truth::lemma_jump_op(c, op, pc, lbl, lay, s, st, fuel, true, cond, target); + }, + js::Op::UShr { dst, a, b } => { + assert(jump_target(op) is None); + super::ushr::lemma_ushr_op(c, op, pc, lbl, lay, s, st, fuel, dst, a, b); + }, + _ => { + assert(false); + }, + } + } +} + +/// **The refinement theorem of this slice**: for every op the front end +/// lowers, its lowering satisfies the statement [`lowers`]. +pub proof fn lemma_lower_refines(c: js::Code, op: js::Op, pc: nat, lbl: nat) + requires + super::lower::lowered(op), + ensures + super::contract::lowers(c, op, pc, lbl, super::lower::lower(c, op, pc, lbl)), +{ + let code = super::lower::lower(c, op, pc, lbl); + assert forall|lay: lir::Layout, s: js::State, st: lir::St, fuel: nat| + #![trigger lir::eval(lay, code, st, fuel), rel(lay, s, st)] + fuel >= lower_fuel() && rel(lay, s, st) && st.stack == Seq::::empty() + && js::state_wf(s) && js::cur_code(s) == c && s.pc == pc && js::fetch(s) == op + && handler_complete(s, op) implies outcome_refines( + lay, + s, + op, + lbl, + lir::eval(lay, code, st, fuel), + ) by { + lemma_op_case(c, op, pc, lbl, lay, s, st, fuel); + } +} + + +/// The host's decoding of an exit code inverts [`handler_index`]: an exit's +/// code names exactly one handler. +pub proof fn lemma_handler_index_inverse(h: js::HandlerId) + ensures + handler_of_index(handler_index(h)) == Some(h), +{ +} + +} // verus! diff --git a/crates/clazel-jit/src/front/frame.rs b/crates/clazel-jit/src/front/frame.rs new file mode 100644 index 000000000..2ed615d18 --- /dev/null +++ b/crates/clazel-jit/src/front/frame.rs @@ -0,0 +1,138 @@ +//! The effect of the frame-access blocks of [`super::lower`] on LIR's state, +//! under the relation [`rel`]: computing a slot's address (never failing its +//! bounds check), loading a slot, storing to a slot. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + + +/// `i64.add` of two small numbers is their sum. +pub proof fn lemma_add_small(x: u64, y: u64) + requires + (x as int) + (y as int) < 0x1_0000_0000_0000_0000, + ensures + lir::ibin64(lir::IBin::Add, x, y) == (x + y) as u64, +{ + vstd::arithmetic::power2::lemma2_to64(); + assert(lir::pow2(64) == 0x1_0000_0000_0000_0000); + assert(((x as int) + (y as int)) % 0x1_0000_0000_0000_0000int == (x as int) + (y as int)) by (nonlinear_arith) + requires + 0 <= (x as int) + (y as int) < 0x1_0000_0000_0000_0000int, + ; +} + +/// The slot `off` bytes from the frame base is inside region 0. +pub open spec fn slot_ok(s: js::State, off: nat) -> bool { + off % 8 == 0 && s.fp + off / 8 < s.region.len() +} + +/// The address block pushes the slot's byte index and never exits. +pub proof fn lemma_exec_addr(lay: lir::Layout, s: js::State, st: lir::St, off: nat) + requires + rel(lay, s, st), + slot_ok(s, off), + ensures + super::run::exec(lay, super::lower::addr_ins(off), st) == lir::Outcome::Normal( + st.with_stack(st.stack.push(lir::Val::I64((8 * s.fp + off) as u64))), + ), +{ + reveal_with_fuel(super::run::exec, 5); + let base = (8 * s.fp) as u64; + assert(8 * s.fp + off <= 8 * s.region.len()); + assert(lay[0].bound == 8 * s.region.len()); + assert(lir::layout_wf(lay)); + assert(lay[0].base + lay[0].bound <= 0x1_0000_0000_0000_0000); + assert(8 * s.fp + off + 8 <= lay[0].bound); + assert(lay[0].bound <= 0x1_0000_0000_0000_0000); + lemma_add_small(base, off as u64); + let s1 = st.stack.push(lir::Val::I64(base)); + let s2 = s1.push(lir::Val::I64(off as u64)); + let s3 = st.stack.push(lir::Val::I64((8 * s.fp + off) as u64)); + assert(lir::below(s2, 2) =~= st.stack); + assert(lir::below(s2, 2).push(lir::Val::I64((8 * s.fp + off) as u64)) =~= s3); +} + + +/// The load block pushes the slot's value, and never exits. +pub proof fn lemma_exec_load(lay: lir::Layout, s: js::State, st: lir::St, off: nat) + requires + rel(lay, s, st), + slot_ok(s, off), + ensures + super::run::exec(lay, super::lower::load_ins(off), st) == lir::Outcome::Normal( + st.with_stack(st.stack.push(lir::Val::I64(s.region[(s.fp + off / 8) as int]))), + ), +{ + lemma_exec_addr(lay, s, st, off); + let sa = st.with_stack(st.stack.push(lir::Val::I64((8 * s.fp + off) as u64))); + let ld = seq![lir::Instr::Load { region: 0, ty: lir::Ty::I64 }]; + assert(super::lower::load_ins(off) =~= super::lower::addr_ins(off) + ld); + super::run::lemma_exec_concat(lay, super::lower::addr_ins(off), ld, st); + reveal_with_fuel(super::run::exec, 2); + let j = (s.fp + off / 8) as int; + assert(8 * s.fp + off == 8 * j); + assert(lay[0].base + lir::masked(lay[0].bound, (8 * s.fp + off) as nat) == slot_addr(lay, j)); + assert(mem_rel(lay, s.region, st.mem)); + assert(lir::read_le(st.mem, slot_addr(lay, j), 8) == s.region[j] as nat); + assert(lir::below(sa.stack, 1) =~= st.stack); +} + +/// A store: the address block, the value block `val` (which pushes `v` above +/// the address and leaves the memory alone), then the store. The memory +/// holds `v` in the slot, and the relation holds for the region updated. +pub proof fn lemma_exec_store( + lay: lir::Layout, + s: js::State, + st: lir::St, + off: nat, + val: Seq, + v: u64, + mid: lir::St, +) + requires + rel(lay, s, st), + slot_ok(s, off), + super::run::exec( + lay, + val, + st.with_stack(st.stack.push(lir::Val::I64((8 * s.fp + off) as u64))), + ) == lir::Outcome::Normal(mid), + mid.stack == st.stack.push(lir::Val::I64((8 * s.fp + off) as u64)).push(lir::Val::I64(v)), + mid.mem == st.mem, + locals_rel(s, mid.locals), + ensures + ({ + let j = (s.fp + off / 8) as int; + let st2 = lir::St { + stack: st.stack, + locals: mid.locals, + mem: lir::write_le(st.mem, slot_addr(lay, j), v as nat, 8), + }; + &&& super::run::exec(lay, super::lower::store_ins(off, val), st) == lir::Outcome::Normal(st2) + &&& rel(lay, js::State { region: s.region.update(j, v), ..s }, st2) + }), +{ + lemma_exec_addr(lay, s, st, off); + let sa = st.with_stack(st.stack.push(lir::Val::I64((8 * s.fp + off) as u64))); + let stor = seq![lir::Instr::Store { region: 0, ty: lir::Ty::I64 }]; + assert(super::lower::store_ins(off, val) =~= (super::lower::addr_ins(off) + val) + stor); + super::run::lemma_exec_concat(lay, super::lower::addr_ins(off) + val, stor, st); + super::run::lemma_exec_concat(lay, super::lower::addr_ins(off), val, st); + reveal_with_fuel(super::run::exec, 2); + let j = (s.fp + off / 8) as int; + assert(8 * s.fp + off == 8 * j); + assert(lay[0].base + lir::masked(lay[0].bound, (8 * s.fp + off) as nat) == slot_addr(lay, j)); + let n = mid.stack.len(); + assert(mid.stack[n - 1] == lir::Val::I64(v)); + assert(mid.stack[n - 2] == lir::Val::I64((8 * s.fp + off) as u64)); + assert(lir::below(mid.stack, 2) =~= st.stack); + super::lirmem::lemma_store_mem_rel(lay, s.region, st.mem, j, v); + let s2 = js::State { region: s.region.update(j, v), ..s }; + assert(layout_ok(lay, s2)); +} + +} // verus! diff --git a/crates/clazel-jit/src/front/intcmp.rs b/crates/clazel-jit/src/front/intcmp.rs new file mode 100644 index 000000000..6ee8947bb --- /dev/null +++ b/crates/clazel-jit/src/front/intcmp.rs @@ -0,0 +1,259 @@ +//! The int32 comparisons: `Lt`, `Le`, `Gt`, `Ge`, `StrictEq`, `StrictNe`. +//! +//! The semantics compares the *doubles* two int32s stand for +//! (`num_bits`, `f64_of_int`) with `Number::lessThan` and `Number::equal`; +//! [`super::numord`] proves that this is the order and equality of the +//! integers. So the lowering is: both operands int32 (the tag test of the +//! first slice), sign-extend the two payloads to 64 bits, compare them with +//! the signed (or `eq`, `ne`) `i64` comparison, widen the `i32` flag and add +//! it to the encoding of `false` (`true` is that plus one), and store. Any +//! other operands exit to the handler, which for `StrictEq` and `StrictNe` +//! includes cases where `fast_path` holds (`undefined === undefined`, two +//! doubles, ...) and so relies on [`handler_complete`]. +//! +//! `k` is the operator: 0 `Lt`, 1 `Le`, 2 `Gt`, 3 `Ge`, 4 `StrictEq`, +//! 5 `StrictNe`. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The `i64` comparison of operator `k`. +pub open spec fn cmp_icmp(k: nat) -> lir::ICmp { + if k == 0 { + lir::ICmp::LtS + } else if k == 1 { + lir::ICmp::LeS + } else if k == 2 { + lir::ICmp::GtS + } else if k == 3 { + lir::ICmp::GeS + } else if k == 4 { + lir::ICmp::Eq + } else { + lir::ICmp::Ne + } +} + +/// The bytecode op of operator `k`. +pub open spec fn cmp_op(k: nat, dst: nat, a: nat, b: nat) -> js::Op { + if k == 0 { + js::Op::Lt { dst, a, b } + } else if k == 1 { + js::Op::Le { dst, a, b } + } else if k == 2 { + js::Op::Gt { dst, a, b } + } else if k == 3 { + js::Op::Ge { dst, a, b } + } else if k == 4 { + js::Op::StrictEq { dst, a, b } + } else { + js::Op::StrictNe { dst, a, b } + } +} + +/// The truth value of operator `k` on two integers. +pub open spec fn cmp_truth(k: nat, ia: int, ib: int) -> bool { + if k == 0 { + ia < ib + } else if k == 1 { + ia <= ib + } else if k == 2 { + ia > ib + } else if k == 3 { + ia >= ib + } else if k == 4 { + ia == ib + } else { + ia != ib + } +} + +/// After the two sign-extended payloads: compare, widen, add `false`. +pub open spec fn cmp_tail(k: nat) -> Seq { + seq![ + super::lower::cmp64(cmp_icmp(k)), + lir::Instr::Un(lir::UnOp::I64ExtendI32U), + super::lower::i64c(js::FALSE), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Add)), + ] +} + +/// The value block: the encoded boolean of the comparison of locals 1 and 2. +pub open spec fn cmp_val(k: nat) -> Seq { + super::lower::sext_local(1) + (super::lower::sext_local(2) + cmp_tail(k)) +} + +/// The word the value block pushes, from the two sign-extended payloads. +pub open spec fn cmp_word(k: nat, xa: u64, xb: u64) -> u64 { + (lir::bool_i32(lir::icmp(cmp_icmp(k), 64, xa as int, xb as int)) as u64 + js::FALSE) as u64 +} + +/// The lowering of operator `k` on registers `a` and `b` into `dst`. +pub open spec fn lower_cmp(k: nat, dst: nat, a: nat, b: nat, op: js::Op, pc: nat) -> lir::Instr { + super::blocks::lower_bin(cmp_val(k), dst, a, b, op, pc) +} + +/// The value block is simple and ten instructions long. +pub proof fn lemma_cmp_val_simple(k: nat) + ensures + super::run::all_simple(cmp_val(k)), + cmp_val(k).len() == 10, +{ + super::intops::lemma_literals_simple(0); + let t = cmp_tail(k); + assert(super::run::all_simple(t)) by { + assert forall|i: int| 0 <= i < t.len() implies super::run::is_simple(#[trigger] t[i]) by { + assert(i == 0 || i == 1 || i == 2 || i == 3); + } + } + super::intops::lemma_simple_concat(super::lower::sext_local(2), t); + super::intops::lemma_simple_concat(super::lower::sext_local(1), super::lower::sext_local(2) + t); +} + +/// The value block's trace: from locals 1 and 2 holding two words, it pushes +/// the comparison of their sign-extended low halves. +pub proof fn lemma_cmp_val_trace(lay: lir::Layout, k: nat, va: u64, vb: u64) + ensures + super::blocks::val_computes( + lay, + cmp_val(k), + va, + vb, + cmp_word(k, super::arith::sext(super::arith::lo32(va)), super::arith::sext(super::arith::lo32(vb))), + ), +{ + let xa = super::arith::sext(super::arith::lo32(va)); + let xb = super::arith::sext(super::arith::lo32(vb)); + let w = cmp_word(k, xa, xb); + let t = cmp_tail(k); + assert forall|st1: lir::St| + #![trigger super::run::exec(lay, cmp_val(k), st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) && st1.locals[2] + == lir::Val::I64(vb) implies super::run::exec(lay, cmp_val(k), st1) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I64(w))), + ) by { + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + super::trace::lemma_exec_sext_local(lay, st1, 1, va); + let s1 = st1.with_stack(st1.stack.push(lir::Val::I64(xa))); + super::trace::lemma_exec_app( + lay, + super::lower::sext_local(1), + super::lower::sext_local(2) + t, + st1, + s1, + ); + super::trace::lemma_exec_sext_local(lay, s1, 2, vb); + let s2 = s1.with_stack(s1.stack.push(lir::Val::I64(xb))); + super::trace::lemma_exec_app(lay, super::lower::sext_local(2), t, s1, s2); + let f = lir::bool_i32(lir::icmp(cmp_icmp(k), 64, xa as int, xb as int)); + super::frame::lemma_add_small(f as u64, js::FALSE); + reveal_with_fuel(super::run::exec, 5); + assert(super::run::exec(lay, t, s2) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I64(w))), + )); + } +} + +/// The comparison's word is the encoding of the integers' comparison. +pub proof fn lemma_cmp_word(k: nat, va: u64, vb: u64) + requires + k <= 5, + js::is_int32(va), + js::is_int32(vb), + ensures + cmp_word(k, super::arith::sext(super::arith::lo32(va)), super::arith::sext(super::arith::lo32(vb))) + == js::enc_bool(cmp_truth(k, js::int32_of(va) as int, js::int32_of(vb) as int)), +{ + super::arith::lemma_payload(va); + super::arith::lemma_payload(vb); + let ia = js::int32_of(va); + let ib = js::int32_of(vb); + let xa = super::arith::sext(super::arith::lo32(va)); + let xb = super::arith::sext(super::arith::lo32(vb)); + assert(xa == (ia as i64) as u64); + assert(xb == (ib as i64) as u64); + super::arith::lemma_signed64(xa); + super::arith::lemma_signed64(xb); + assert((((ia as i64) as u64) as i64) == (ia as i64)) by (bit_vector); + assert((((ib as i64) as u64) as i64) == (ib as i64)) by (bit_vector); + assert((((ia as i64) as u64) == ((ib as i64) as u64)) <==> (ia == ib)) by (bit_vector); + let t = cmp_truth(k, ia as int, ib as int); + assert(lir::icmp(cmp_icmp(k), 64, xa as int, xb as int) == t); + assert(js::FALSE + 1 == js::TRUE); +} + +/// The semantics of an operator `k` on two int32 registers: the fast path +/// holds, and the result is the encoded comparison of the integers. +pub proof fn lemma_cmp_sem(s: js::State, k: nat, dst: nat, a: nat, b: nat) + requires + k <= 5, + js::is_int32(js::reg(s, a)), + js::is_int32(js::reg(s, b)), + ensures + js::fast_path(s, cmp_op(k, dst, a, b)), + js::fast_step(s, cmp_op(k, dst, a, b)) == js::next( + js::set_reg( + s, + dst, + js::enc_bool( + cmp_truth(k, js::int32_of(js::reg(s, a)) as int, js::int32_of(js::reg(s, b)) as int), + ), + ), + ), +{ + let va = js::reg(s, a); + let vb = js::reg(s, b); + let ia = js::int32_of(va); + let ib = js::int32_of(vb); + super::numord::lemma_less_than_int(ia as int, ib as int); + super::numord::lemma_less_than_int(ib as int, ia as int); + super::numord::lemma_equal_int(ia as int, ib as int); + assert(js::num_bits(va) == js::f64_of_int(ia as int)); + assert(js::num_bits(vb) == js::f64_of_int(ib as int)); + assert(js::is_number(va) && js::is_number(vb)); +} + +/// An int32 comparison op: the lowering refines `step`. +pub proof fn lemma_cmp_op( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + k: nat, + dst: nat, + a: nat, + b: nat, +) + requires + super::proofs::pre(c, op, pc, lay, s, st, fuel), + k <= 5, + op == cmp_op(k, dst, a, b), + js::reg_ok(js::cur_code(s), dst), + js::reg_ok(js::cur_code(s), a), + js::reg_ok(js::cur_code(s), b), + super::lower::lower(c, op, pc, lbl) == lower_cmp(k, dst, a, b, op, pc), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_cmp_val_simple(k); + let va = js::reg(s, a); + let vb = js::reg(s, b); + let w = js::enc_bool(cmp_truth(k, js::int32_of(va) as int, js::int32_of(vb) as int)); + if js::is_int32(va) && js::is_int32(vb) { + lemma_cmp_sem(s, k, dst, a, b); + lemma_cmp_val_trace(lay, k, va, vb); + lemma_cmp_word(k, va, vb); + } + super::blocks::lemma_bin_op(op, pc, lbl, lay, s, st, fuel, dst, a, b, cmp_val(k), w); +} + +} // verus! diff --git a/crates/clazel-jit/src/front/intops.rs b/crates/clazel-jit/src/front/intops.rs new file mode 100644 index 000000000..b3cae14e0 --- /dev/null +++ b/crates/clazel-jit/src/front/intops.rs @@ -0,0 +1,557 @@ +//! The refinement proof of the small-integer lowerings (add, subtract, +//! multiply): the blocks are simple, the traces of [`super::trace`] give the +//! state after each, and the arithmetic of [`super::arith`] says what the +//! guards and the stored word are. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + + +/// An exit chain from a related state exits with the handler's code and +/// `pc`, and the handler's result is `step`'s (by [`handler_complete`]). +pub proof fn lemma_exit_chain( + op: js::Op, + pc: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, +) + requires + rel(lay, s, st), + fuel > 2, + s.pc == pc, + handler_complete(s, op), + ensures + ({ + let o = lir::eval(lay, super::run::chain(super::lower::exit_ins(js::op_handler(op), pc)), st, fuel); + &&& o is Exit + &&& match o { + lir::Outcome::Exit(code, vals, st2) => { + &&& code == handler_index(js::op_handler(op)) + &&& vals == seq![lir::Val::I32(s.pc as u32)] + &&& rel(lay, s, st2) + &&& js::handler(js::op_handler(op), s, op) == js::step(s, op) + }, + _ => false, + } + }), +{ + let h = js::op_handler(op); + let ins = super::lower::exit_ins(h, pc); + assert(super::run::all_simple(ins)) by { + assert forall|i: int| 0 <= i < ins.len() implies super::run::is_simple(#[trigger] ins[i]) by { + assert(i == 0 || i == 1); + } + } + super::run::lemma_chain(lay, ins, st, fuel); + reveal_with_fuel(super::run::exec, 3); + let st1 = st.with_stack(st.stack.push(lir::Val::I32(pc as u32))); + assert(lir::top(st1.stack, 1) =~= seq![lir::Val::I32(pc as u32)]); +} + +/// `if` with the empty block type, the condition alone on the stack: the +/// chosen branch runs from the state with the stack empty, and a normal +/// completion or an exit of the branch is the instruction's. +pub proof fn lemma_if0( + lay: lir::Layout, + t: lir::Instr, + e: lir::Instr, + st: lir::St, + c: u32, + fuel: nat, +) + requires + fuel >= 3, + st.stack == seq![lir::Val::I32(c)], + ensures + ({ + let br = if c != 0 { + t + } else { + e + }; + let o = lir::eval(lay, br, st.with_stack(Seq::::empty()), (fuel - 2) as nat); + let r = lir::eval( + lay, + lir::Instr::If(super::lower::bt0(), Box::new(t), Box::new(e)), + st, + fuel, + ); + &&& (o is Normal ==> r == o) + &&& (o is Exit ==> r == o) + }), +{ + let br = if c != 0 { + t + } else { + e + }; + let o = lir::eval(lay, br, st.with_stack(Seq::::empty()), (fuel - 2) as nat); + assert(lir::below(st.stack, 1) =~= Seq::::empty()); + assert(lir::top(Seq::::empty(), 0) =~= Seq::::empty()); + reveal_with_fuel(lir::eval, 3); + match o { + lir::Outcome::Normal(s1) => { + assert(Seq::::empty() + s1.stack =~= s1.stack); + assert(s1.with_stack(s1.stack) == s1); + }, + _ => {}, + } +} + +/// The scratch locals after the operands are loaded, as the traces want them. +pub proof fn lemma_locals_after_operands(s: js::State, l: Seq, va: u64, vb: u64) + requires + locals_rel(s, l), + ensures + ({ + let l2 = l.update(1, lir::Val::I64(va)).update(2, lir::Val::I64(vb)); + &&& l2.len() == nlocals() + &&& l2[1] == lir::Val::I64(va) + &&& l2[2] == lir::Val::I64(vb) + &&& locals_rel(s, l2) + }), +{ + super::trace::lemma_locals_set(s, l, 1, va); + super::trace::lemma_locals_set(s, l.update(1, lir::Val::I64(va)), 2, vb); +} + +/// Two lists of simple instructions make one. +pub proof fn lemma_simple_concat(a: Seq, b: Seq) + requires + super::run::all_simple(a), + super::run::all_simple(b), + ensures + super::run::all_simple(a + b), +{ + assert forall|i: int| 0 <= i < (a + b).len() implies super::run::is_simple(#[trigger] (a + b)[i]) by { + if i < a.len() { + assert((a + b)[i] == a[i]); + } else { + assert((a + b)[i] == b[i - a.len()]); + } + } +} + +/// The small literal blocks of the arithmetic lowerings are simple. +pub proof fn lemma_literals_simple(k: nat) + ensures + super::run::all_simple(super::lower::tag_test(1)), + super::run::all_simple(super::lower::tag_test(2)), + super::run::all_simple(super::lower::sext_local(1)), + super::run::all_simple(super::lower::sext_local(2)), + super::run::all_simple(super::lower::fits_ins()), + super::run::all_simple(super::lower::tagged_ins()), + super::run::all_simple(seq![lir::Instr::LocalSet(1)]), + super::run::all_simple(seq![lir::Instr::LocalSet(2)]), + super::run::all_simple(seq![lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And))]), + super::run::all_simple(seq![lir::Instr::Bin(lir::BinOp::I64(super::arith::ibin_of(k))), lir::Instr::LocalSet(3)]), + super::run::all_simple(seq![lir::Instr::LocalGet(3), lir::Instr::Un(lir::UnOp::I64Eqz)]), + super::run::all_simple( + seq![ + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), + super::lower::i64c(0), + super::lower::cmp64(lir::ICmp::LtS), + ] + seq![ + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + ], + ), +{ + let g1 = super::lower::tag_test(1); + assert(super::run::all_simple(g1)) by { + assert forall|i: int| 0 <= i < g1.len() implies super::run::is_simple(#[trigger] g1[i]) by { + assert(i == 0 || i == 1 || i == 2 || i == 3 || i == 4); + } + } + let g2 = super::lower::tag_test(2); + assert(super::run::all_simple(g2)) by { + assert forall|i: int| 0 <= i < g2.len() implies super::run::is_simple(#[trigger] g2[i]) by { + assert(i == 0 || i == 1 || i == 2 || i == 3 || i == 4); + } + } + let x1 = super::lower::sext_local(1); + assert(super::run::all_simple(x1)) by { + assert forall|i: int| 0 <= i < x1.len() implies super::run::is_simple(#[trigger] x1[i]) by { + assert(i == 0 || i == 1 || i == 2); + } + } + let x2 = super::lower::sext_local(2); + assert(super::run::all_simple(x2)) by { + assert forall|i: int| 0 <= i < x2.len() implies super::run::is_simple(#[trigger] x2[i]) by { + assert(i == 0 || i == 1 || i == 2); + } + } + let f = super::lower::fits_ins(); + assert(super::run::all_simple(f)) by { + assert forall|i: int| 0 <= i < f.len() implies super::run::is_simple(#[trigger] f[i]) by { + assert(i == 0 || i == 1 || i == 2 || i == 3 || i == 4); + } + } + let t = super::lower::tagged_ins(); + assert(super::run::all_simple(t)) by { + assert forall|i: int| 0 <= i < t.len() implies super::run::is_simple(#[trigger] t[i]) by { + assert(i == 0 || i == 1 || i == 2 || i == 3 || i == 4); + } + } + let s1 = seq![lir::Instr::LocalSet(1)]; + assert(super::run::all_simple(s1)) by { + assert forall|i: int| 0 <= i < s1.len() implies super::run::is_simple(#[trigger] s1[i]) by { + assert(i == 0); + } + } + let s2 = seq![lir::Instr::LocalSet(2)]; + assert(super::run::all_simple(s2)) by { + assert forall|i: int| 0 <= i < s2.len() implies super::run::is_simple(#[trigger] s2[i]) by { + assert(i == 0); + } + } + let a1 = seq![lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And))]; + assert(super::run::all_simple(a1)) by { + assert forall|i: int| 0 <= i < a1.len() implies super::run::is_simple(#[trigger] a1[i]) by { + assert(i == 0); + } + } + let c3 = seq![lir::Instr::Bin(lir::BinOp::I64(super::arith::ibin_of(k))), lir::Instr::LocalSet(3)]; + assert(super::run::all_simple(c3)) by { + assert forall|i: int| 0 <= i < c3.len() implies super::run::is_simple(#[trigger] c3[i]) by { + assert(i == 0 || i == 1); + } + } + let e3 = seq![lir::Instr::LocalGet(3), lir::Instr::Un(lir::UnOp::I64Eqz)]; + assert(super::run::all_simple(e3)) by { + assert forall|i: int| 0 <= i < e3.len() implies super::run::is_simple(#[trigger] e3[i]) by { + assert(i == 0 || i == 1); + } + } + let ta = seq![lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), super::lower::i64c(0), super::lower::cmp64(lir::ICmp::LtS)]; + let tb = seq![ + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + ]; + assert(super::run::all_simple(ta)) by { + assert forall|i: int| 0 <= i < ta.len() implies super::run::is_simple(#[trigger] ta[i]) by { + assert(i == 0 || i == 1 || i == 2); + } + } + assert(super::run::all_simple(tb)) by { + assert forall|i: int| 0 <= i < tb.len() implies super::run::is_simple(#[trigger] tb[i]) by { + assert(i == 0 || i == 1 || i == 2); + } + } + lemma_simple_concat(ta, tb); +} + +/// The blocks of an arithmetic lowering are simple, and of known length. +pub proof fn lemma_arith_blocks_simple(k: nat, a: nat, b: nat, dst: nat) + requires + k <= 2, + ensures + super::run::all_simple(super::lower::operands_ins(a, b) + super::lower::guard1_ins()), + (super::lower::operands_ins(a, b) + super::lower::guard1_ins()).len() == 23, + super::run::all_simple(super::lower::compute_ins(k) + super::lower::guard2_ins(k)), + (super::lower::compute_ins(k) + super::lower::guard2_ins(k)).len() <= 27, + super::run::all_simple(super::lower::store_ins(reg_off(dst), super::lower::tagged_ins())), + super::lower::store_ins(reg_off(dst), super::lower::tagged_ins()).len() == 10, +{ + lemma_literals_simple(k); + super::proofs::lemma_blocks_simple(reg_off(a), 0); + super::proofs::lemma_blocks_simple(reg_off(b), 0); + super::proofs::lemma_blocks_simple(reg_off(dst), 0); + let s1 = seq![lir::Instr::LocalSet(1)]; + let s2 = seq![lir::Instr::LocalSet(2)]; + lemma_simple_concat(super::lower::load_ins(reg_off(a)), s1); + lemma_simple_concat(super::lower::load_ins(reg_off(a)) + s1, super::lower::load_ins(reg_off(b))); + lemma_simple_concat((super::lower::load_ins(reg_off(a)) + s1) + super::lower::load_ins(reg_off(b)), s2); + assert(super::lower::operands_ins(a, b) =~= ((super::lower::load_ins(reg_off(a)) + s1) + super::lower::load_ins(reg_off(b))) + s2); + lemma_simple_concat(super::lower::tag_test(1), super::lower::tag_test(2)); + lemma_simple_concat( + super::lower::tag_test(1) + super::lower::tag_test(2), + seq![lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And))], + ); + assert(super::lower::guard1_ins() =~= (super::lower::tag_test(1) + super::lower::tag_test(2)) + seq![ + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + ]); + lemma_simple_concat(super::lower::operands_ins(a, b), super::lower::guard1_ins()); + // compute and guard2 + lemma_simple_concat(super::lower::sext_local(1), super::lower::sext_local(2)); + let c3 = seq![lir::Instr::Bin(lir::BinOp::I64(super::arith::ibin_of(k))), lir::Instr::LocalSet(3)]; + lemma_simple_concat(super::lower::sext_local(1) + super::lower::sext_local(2), c3); + assert(super::lower::compute_ins(k) =~= (super::lower::sext_local(1) + super::lower::sext_local(2)) + c3); + let e3 = seq![lir::Instr::LocalGet(3), lir::Instr::Un(lir::UnOp::I64Eqz)]; + let tail = seq![ + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), + super::lower::i64c(0), + super::lower::cmp64(lir::ICmp::LtS), + ] + seq![ + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + ]; + if k == 2 { + lemma_simple_concat(e3, super::lower::sext_local(1)); + lemma_simple_concat(e3 + super::lower::sext_local(1), super::lower::sext_local(2)); + lemma_simple_concat((e3 + super::lower::sext_local(1)) + super::lower::sext_local(2), tail); + assert(super::lower::mul_tail_ins() =~= ((e3 + super::lower::sext_local(1)) + super::lower::sext_local(2)) + tail); + lemma_simple_concat(super::lower::fits_ins(), super::lower::mul_tail_ins()); + } + assert(super::run::all_simple(super::lower::guard2_ins(k))); + assert(super::lower::guard2_ins(k).len() <= 19); + lemma_simple_concat(super::lower::compute_ins(k), super::lower::guard2_ins(k)); + // the store + let sto = seq![lir::Instr::Store { region: 0, ty: lir::Ty::I64 }]; + assert(super::run::all_simple(sto)) by { + assert forall|i: int| 0 <= i < sto.len() implies super::run::is_simple(#[trigger] sto[i]) by { + assert(i == 0); + } + } + lemma_simple_concat(super::lower::addr_ins(reg_off(dst)), super::lower::tagged_ins()); + lemma_simple_concat(super::lower::addr_ins(reg_off(dst)) + super::lower::tagged_ins(), sto); +} + +/// The facts about `op` and `s` the three stages share. +pub open spec fn arith_ctx( + op: js::Op, + s: js::State, + k: nat, + dst: nat, + a: nat, + b: nat, +) -> bool { + &&& k <= 2 + &&& jump_target(op) is None + &&& js::reg_ok(js::cur_code(s), dst) + &&& js::reg_ok(js::cur_code(s), a) + &&& js::reg_ok(js::cur_code(s), b) + &&& js::fast_path(s, op) <==> (js::is_number(js::reg(s, a)) && js::is_number(js::reg(s, b))) + &&& js::fast_step(s, op) == js::next( + js::set_reg(s, dst, super::arith::spec_arith(k, js::reg(s, a), js::reg(s, b))), + ) +} + +/// The last stage: the result is an int32, so the tagged word is stored. +pub proof fn lemma_arith_store( + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st4: lir::St, + fuel: nat, + k: nat, + dst: nat, + a: nat, + b: nat, +) + requires + arith_ctx(op, s, k, dst, a, b), + js::state_wf(s), + rel(lay, s, st4), + st4.stack == Seq::::empty(), + fuel >= 12, + s.pc == pc, + js::is_int32(js::reg(s, a)), + js::is_int32(js::reg(s, b)), + st4.locals[3] == lir::Val::I64(super::arith::s64(k, js::reg(s, a), js::reg(s, b))), + super::arith::int_ok(k, js::int32_of(js::reg(s, a)) as int, js::int32_of(js::reg(s, b)) as int), + ensures + outcome_refines( + lay, + s, + op, + lbl, + lir::eval(lay, super::run::chain(super::lower::store_ins(reg_off(dst), super::lower::tagged_ins())), st4, fuel), + ), +{ + let va = js::reg(s, a); + let vb = js::reg(s, b); + lemma_arith_blocks_simple(k, a, b, dst); + super::proofs::lemma_reg_slot_ok(s, dst); + super::arith::lemma_arith_facts(k, va, vb); + let sum = super::arith::s64(k, va, vb); + let v = super::arith::tagged(sum); + assert(v == super::arith::spec_arith(k, va, vb)); + let off = reg_off(dst); + let sa = st4.with_stack(st4.stack.push(lir::Val::I64((8 * s.fp + off) as u64))); + reveal_with_fuel(super::run::exec, 6); + let m1 = sa.with_stack(sa.stack.push(lir::Val::I64(sum))); + let m2 = sa.with_stack(sa.stack.push(lir::Val::I64(sum)).push(lir::Val::I64(0xFFFF_FFFF))); + let m3 = sa.with_stack(sa.stack.push(lir::Val::I64(sum & 0xFFFF_FFFFu64))); + let m4 = sa.with_stack( + sa.stack.push(lir::Val::I64(sum & 0xFFFF_FFFFu64)).push(lir::Val::I64(super::arith::tag_bits())), + ); + let mid = sa.with_stack(sa.stack.push(lir::Val::I64(v))); + assert(lir::below(m2.stack, 2) =~= sa.stack); + assert(lir::below(m4.stack, 2) =~= sa.stack); + assert(super::run::exec(lay, super::lower::tagged_ins(), sa) == lir::Outcome::Normal(mid)); + super::frame::lemma_exec_store(lay, s, st4, off, super::lower::tagged_ins(), v, mid); + let j = (s.fp + off / 8) as int; + let s2 = js::fast_step(s, op); + assert(s2.region == s.region.update(j, v)); + assert(s2 == js::State { pc: s.pc + 1, ..js::State { region: s.region.update(j, v), ..s } }); + let ins3 = super::lower::store_ins(off, super::lower::tagged_ins()); + super::run::lemma_chain(lay, ins3, st4, fuel); + let st6 = lir::St { + stack: st4.stack, + locals: mid.locals, + mem: lir::write_le(st4.mem, slot_addr(lay, j), v as nat, 8), + }; + super::proofs::lemma_rel_pc(lay, js::State { region: s.region.update(j, v), ..s }, st6, s.pc + 1); +} + +/// The second stage, with both operands int32: compute, test the result, +/// then store or exit. +pub proof fn lemma_arith_inner( + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st2: lir::St, + fuel: nat, + k: nat, + dst: nat, + a: nat, + b: nat, +) + requires + arith_ctx(op, s, k, dst, a, b), + js::state_wf(s), + rel(lay, s, st2), + st2.stack == Seq::::empty(), + fuel >= 100, + s.pc == pc, + handler_complete(s, op), + st2.locals[1] == lir::Val::I64(js::reg(s, a)), + st2.locals[2] == lir::Val::I64(js::reg(s, b)), + js::is_int32(js::reg(s, a)), + js::is_int32(js::reg(s, b)), + ensures + ({ + let h = js::op_handler(op); + let exit = super::run::chain(super::lower::exit_ins(h, pc)); + let store = super::run::chain(super::lower::store_ins(reg_off(dst), super::lower::tagged_ins())); + let inner = super::run::chain((super::lower::compute_ins(k) + super::lower::guard2_ins(k)).push(lir::Instr::If( + super::lower::bt0(), + Box::new(store), + Box::new(exit), + ))); + outcome_refines(lay, s, op, lbl, lir::eval(lay, inner, st2, fuel)) + }), +{ + let h = js::op_handler(op); + let va = js::reg(s, a); + let vb = js::reg(s, b); + lemma_arith_blocks_simple(k, a, b, dst); + let exit = super::run::chain(super::lower::exit_ins(h, pc)); + let store = super::run::chain(super::lower::store_ins(reg_off(dst), super::lower::tagged_ins())); + let if2 = lir::Instr::If(super::lower::bt0(), Box::new(store), Box::new(exit)); + let ins2 = super::lower::compute_ins(k) + super::lower::guard2_ins(k); + super::run::lemma_chain_last(lay, ins2, if2, st2, fuel); + super::trace::lemma_compute_trace(lay, st2, k, va, vb); + let sum = super::arith::s64(k, va, vb); + let st4 = lir::St { + stack: Seq::::empty(), + locals: st2.locals.update(3, lir::Val::I64(sum)), + mem: st2.mem, + }; + super::trace::lemma_locals_set(s, st2.locals, 3, sum); + assert(rel(lay, s, st4)); + super::trace::lemma_exec_app(lay, super::lower::compute_ins(k), super::lower::guard2_ins(k), st2, st4); + super::trace::lemma_guard2_trace(lay, st4, k, va, vb); + let g2 = super::arith::guard2(k, va, vb); + let st5 = st4.with_stack(seq![lir::Val::I32(g2)]); + assert(super::run::exec(lay, ins2, st2) == lir::Outcome::Normal(st5)); + let f3 = (fuel - ins2.len()) as nat; + lemma_if0(lay, store, exit, st5, g2, f3); + assert(st5.with_stack(Seq::::empty()) == st4); + super::arith::lemma_arith_facts(k, va, vb); + if g2 == 0 { + lemma_exit_chain(op, pc, lay, s, st4, (f3 - 2) as nat); + let o = lir::eval(lay, exit, st4, (f3 - 2) as nat); + assert(o is Exit); + } else { + lemma_arith_store(op, pc, lbl, lay, s, st4, (f3 - 2) as nat, k, dst, a, b); + let o = lir::eval(lay, store, st4, (f3 - 2) as nat); + assert(o is Normal || o is Exit); + } +} + +/// Small-integer add, subtract or multiply of registers `a` and `b` into +/// `dst`: two guards and an exit, then a tagged store. +pub proof fn lemma_arith_op( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + k: nat, + dst: nat, + a: nat, + b: nat, +) + requires + super::proofs::pre(c, op, pc, lay, s, st, fuel), + arith_ctx(op, s, k, dst, a, b), + super::lower::lower(c, op, pc, lbl) == super::lower::lower_arith(k, dst, a, b, js::op_handler(op), pc), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + let h = js::op_handler(op); + let va = js::reg(s, a); + let vb = js::reg(s, b); + lemma_arith_blocks_simple(k, a, b, dst); + let exit = super::run::chain(super::lower::exit_ins(h, pc)); + let store = super::run::chain(super::lower::store_ins(reg_off(dst), super::lower::tagged_ins())); + let inner = super::run::chain((super::lower::compute_ins(k) + super::lower::guard2_ins(k)).push(lir::Instr::If( + super::lower::bt0(), + Box::new(store), + Box::new(exit), + ))); + let if1 = lir::Instr::If(super::lower::bt0(), Box::new(inner), Box::new(exit)); + let pre1 = super::lower::operands_ins(a, b) + super::lower::guard1_ins(); + assert(st.locals.len() == nlocals()); + super::run::lemma_chain_last(lay, pre1, if1, st, fuel); + super::trace::lemma_operands_trace(lay, s, st, a, b); + let st2 = lir::St { + stack: Seq::::empty(), + locals: st.locals.update(1, lir::Val::I64(va)).update(2, lir::Val::I64(vb)), + mem: st.mem, + }; + lemma_locals_after_operands(s, st.locals, va, vb); + super::trace::lemma_exec_app(lay, super::lower::operands_ins(a, b), super::lower::guard1_ins(), st, st2); + super::trace::lemma_guard1_trace(lay, st2, va, vb); + let g1 = lir::ibin32( + lir::IBin::And, + lir::bool_i32((va >> 32u64) == super::arith::tag_hi()), + lir::bool_i32((vb >> 32u64) == super::arith::tag_hi()), + ); + let st3 = st2.with_stack(seq![lir::Val::I32(g1)]); + assert(super::run::exec(lay, pre1, st) == lir::Outcome::Normal(st3)); + let f1 = (fuel - 23) as nat; + lemma_if0(lay, inner, exit, st3, g1, f1); + assert(st3.with_stack(Seq::::empty()) == st2); + if g1 == 0 { + lemma_exit_chain(op, pc, lay, s, st2, (f1 - 2) as nat); + let o = lir::eval(lay, exit, st2, (f1 - 2) as nat); + assert(o is Exit); + } else { + assert(js::is_int32(va) && js::is_int32(vb)); + assert(js::is_number(va) && js::is_number(vb)); + lemma_arith_inner(op, pc, lbl, lay, s, st2, (f1 - 2) as nat, k, dst, a, b); + let o = lir::eval(lay, inner, st2, (f1 - 2) as nat); + assert(o is Normal || o is Exit); + } +} + +} // verus! diff --git a/crates/clazel-jit/src/front/lirmem.rs b/crates/clazel-jit/src/front/lirmem.rs new file mode 100644 index 000000000..b3740f5c8 --- /dev/null +++ b/crates/clazel-jit/src/front/lirmem.rs @@ -0,0 +1,111 @@ +//! Facts about LIR's byte memory ([`lir::read_le`], [`lir::write_le`]) that +//! the frame accesses need: a store changes only the bytes it writes, and +//! reading back what was stored gives the value. Proven here because +//! `clazel-lir` states the semantics but proves no such lemma. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `256^n`, the number of values `n` bytes hold. +pub open spec fn p256(n: nat) -> nat + decreases n, +{ + if n == 0 { + 1 + } else { + 256 * p256((n - 1) as nat) + } +} + +/// Eight bytes hold `2^64` values. +pub proof fn lemma_p256_8() + ensures + p256(8) == 0x1_0000_0000_0000_0000, +{ + reveal_with_fuel(p256, 9); +} + +/// A byte outside the written range is not changed by [`lir::write_le`]. +pub proof fn lemma_write_le_other(m: lir::Mem, a: nat, v: nat, n: nat, b: nat) + requires + b < a || b >= a + n, + ensures + lir::byte_at(lir::write_le(m, a, v, n), b) == lir::byte_at(m, b), + decreases n, +{ + if n > 0 { + let m1 = m.insert(a, (v % 256) as u8); + assert(lir::byte_at(m1, b) == lir::byte_at(m, b)); + lemma_write_le_other(m1, (a + 1) as nat, v / 256, (n - 1) as nat, b); + } +} + +/// Reading back `n` bytes just written gives the value, when it fits. +pub proof fn lemma_read_write_same(m: lir::Mem, a: nat, v: nat, n: nat) + requires + v < p256(n), + ensures + lir::read_le(lir::write_le(m, a, v, n), a, n) == v, + decreases n, +{ + if n > 0 { + let m1 = m.insert(a, (v % 256) as u8); + let w = lir::write_le(m1, (a + 1) as nat, v / 256, (n - 1) as nat); + assert(lir::write_le(m, a, v, n) == w); + lemma_write_le_other(m1, (a + 1) as nat, v / 256, (n - 1) as nat, a); + assert(lir::byte_at(m1, a) == (v % 256) as u8); + assert(p256(n) == 256 * p256((n - 1) as nat)); + assert(v / 256 < p256((n - 1) as nat)) by (nonlinear_arith) + requires + v < 256 * p256((n - 1) as nat), + ; + lemma_read_write_same(m1, (a + 1) as nat, v / 256, (n - 1) as nat); + assert(lir::read_le(w, a, n) == lir::byte_at(w, a) + 256 * lir::read_le(w, (a + 1) as nat, (n - 1) as nat)); + assert(v % 256 + 256 * (v / 256) == v) by (nonlinear_arith); + } +} + +/// Reading `k` bytes that do not overlap the written range gives what was +/// there before. +pub proof fn lemma_read_le_other(m: lir::Mem, a: nat, v: nat, n: nat, b: nat, k: nat) + requires + b + k <= a || b >= a + n, + ensures + lir::read_le(lir::write_le(m, a, v, n), b, k) == lir::read_le(m, b, k), + decreases k, +{ + if k > 0 { + lemma_write_le_other(m, a, v, n, b); + lemma_read_le_other(m, a, v, n, (b + 1) as nat, (k - 1) as nat); + } +} + +/// Storing the 8 bytes of `v` in slot `j` of the value stack keeps the +/// relation, for the region with that slot set to `v`. +pub proof fn lemma_store_mem_rel(lay: lir::Layout, region: Seq, m: lir::Mem, j: int, v: u64) + requires + lay.len() >= 1, + mem_rel(lay, region, m), + 0 <= j < region.len(), + ensures + mem_rel(lay, region.update(j, v), lir::write_le(m, slot_addr(lay, j), v as nat, 8)), +{ + let m2 = lir::write_le(m, slot_addr(lay, j), v as nat, 8); + let r2 = region.update(j, v); + assert forall|i: int| 0 <= i < r2.len() implies lir::read_le(m2, slot_addr(lay, i), 8) == #[trigger] r2[i] as nat by { + if i == j { + lemma_p256_8(); + lemma_read_write_same(m, slot_addr(lay, j), v as nat, 8); + } else { + assert(r2[i] == region[i]); + assert(region[i] == region[i]); + lemma_read_le_other(m, slot_addr(lay, j), v as nat, 8, slot_addr(lay, i), 8); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/front/lower.rs b/crates/clazel-jit/src/front/lower.rs new file mode 100644 index 000000000..5242aba62 --- /dev/null +++ b/crates/clazel-jit/src/front/lower.rs @@ -0,0 +1,325 @@ +//! The lowerings, as spec functions: for an op, the LIR instruction that +//! implements it ([`lower`]), built from a few blocks (a frame slot's +//! address, a load, a store, an exit). Which ops have one is [`lowered`]; +//! the proof that each refines `step` is in [`super::proofs`]. +//! +//! Every access to a frame slot is `local.get 0` (the frame base), +//! `i64.const offset`, `i64.add`, `bounds_check`, then `load` or `store`: +//! register `r` is at [`reg_off`]`(r)`, a header slot at [`hdr_off`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + + +/// The exit code of a failed bounds check. No handler has it, and under the +/// state relation the check never fails (the proofs show it). +pub open spec fn bounds_code() -> nat { + 1000 +} + +/// `i64.const n`. +pub open spec fn i64c(n: u64) -> lir::Instr { + lir::Instr::Const(lir::Val::I64(n)) +} + +/// An `i64` comparison, as an instruction. +pub open spec fn cmp64(o: lir::ICmp) -> lir::Instr { + lir::Instr::Bin(lir::BinOp::I64Cmp(o)) +} + +/// Push the address (index into region 0) of the slot at byte offset `off` +/// from the frame base, bounds-checked for 8 bytes. +pub open spec fn addr_ins(off: nat) -> Seq { + seq![ + lir::Instr::LocalGet(0), + i64c(off as u64), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Add)), + lir::Instr::BoundsCheck { region: 0, size: 8, code: bounds_code() }, + ] +} + +/// Push the value of the slot at byte offset `off`. +pub open spec fn load_ins(off: nat) -> Seq { + addr_ins(off).push(lir::Instr::Load { region: 0, ty: lir::Ty::I64 }) +} + +/// Store, in the slot at byte offset `off`, the value `val` pushes. +pub open spec fn store_ins(off: nat, val: Seq) -> Seq { + addr_ins(off) + val + seq![lir::Instr::Store { region: 0, ty: lir::Ty::I64 }] +} + +/// The exit to handler `h` from the op at `pc`: push `pc`, exit with the +/// handler's code and that one operand. +pub open spec fn exit_ins(h: js::HandlerId, pc: nat) -> Seq { + seq![ + lir::Instr::Const(lir::Val::I32(pc as u32)), + lir::Instr::Exit(handler_index(h), seq![lir::Ty::I32]), + ] +} + +/// The op is lowered by this crate. +pub open spec fn lowered(op: js::Op) -> bool { + if js::op_class(op) is Handler { + true + } else { + match op { + js::Op::Nop => true, + js::Op::Move { .. } => true, + js::Op::LoadConst { .. } => true, + js::Op::LoadInt { .. } => true, + js::Op::LoadUndefined { .. } => true, + js::Op::LoadNull { .. } => true, + js::Op::LoadBool { .. } => true, + js::Op::LoadHole { .. } => true, + js::Op::GetThis { .. } => true, + js::Op::GetCallee { .. } => true, + js::Op::Jump { .. } => true, + js::Op::JumpIfUndefined { .. } => true, + js::Op::JumpIfNotUndefined { .. } => true, + js::Op::JumpIfNullish { .. } => true, + js::Op::JumpIfNotNullish { .. } => true, + js::Op::Add { .. } => true, + js::Op::Sub { .. } => true, + js::Op::Mul { .. } => true, + js::Op::Lt { .. } => true, + js::Op::Le { .. } => true, + js::Op::Gt { .. } => true, + js::Op::Ge { .. } => true, + js::Op::StrictEq { .. } => true, + js::Op::StrictNe { .. } => true, + js::Op::BitAnd { .. } => true, + js::Op::BitOr { .. } => true, + js::Op::BitXor { .. } => true, + js::Op::Shl { .. } => true, + js::Op::Shr { .. } => true, + js::Op::Inc { .. } => true, + js::Op::Dec { .. } => true, + js::Op::Neg { .. } => true, + js::Op::BitNot { .. } => true, + js::Op::UShr { .. } => true, + js::Op::Not { .. } => true, + js::Op::JumpIfTrue { .. } => true, + js::Op::JumpIfFalse { .. } => true, + _ => false, + } + } +} + +/// Store the constant `v` in register `dst`. +pub open spec fn set_const(dst: nat, v: u64) -> Seq { + store_ins(reg_off(dst), seq![i64c(v)]) +} + +/// Branch to `lbl` if the register `src` holds `v` (`eq`) or does not (`!eq`). +pub open spec fn jump_if_is(src: nat, v: u64, eq: bool, lbl: nat) -> Seq { + load_ins(reg_off(src)) + seq![ + i64c(v), + cmp64(if eq { lir::ICmp::Eq } else { lir::ICmp::Ne }), + lir::Instr::BrIf(lbl), + ] +} + +/// After the register's value is on the stack: keep it in scratch local 1, +/// compare it with `undefined` and with `null` (`==` and or, or `!=` and +/// and), and branch to `lbl` if the result is non-zero. +pub open spec fn nullish_rest(not_nullish: bool, lbl: nat) -> Seq { + let o1 = if not_nullish { lir::ICmp::Ne } else { lir::ICmp::Eq }; + let comb = if not_nullish { lir::IBin::And } else { lir::IBin::Or }; + seq![ + lir::Instr::LocalTee(1), + i64c(js::UNDEFINED), + cmp64(o1), + lir::Instr::LocalGet(1), + i64c(js::NULL), + cmp64(o1), + lir::Instr::Bin(lir::BinOp::I32(comb)), + lir::Instr::BrIf(lbl), + ] +} + +/// The block type of a block that takes and leaves nothing. +pub open spec fn bt0() -> lir::BlockType { + lir::BlockType { params: seq![], results: seq![] } +} + +/// Test that scratch local `x` holds an int32 value: its high 32 bits are +/// the tag and 16 zero bits. Pushes an `i32`. +pub open spec fn tag_test(x: nat) -> Seq { + seq![ + lir::Instr::LocalGet(x), + i64c(32), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::ShrU)), + i64c(super::arith::tag_hi()), + cmp64(lir::ICmp::Eq), + ] +} + +/// Load the two operands into scratch locals 1 and 2. +pub open spec fn operands_ins(a: nat, b: nat) -> Seq { + load_ins(reg_off(a)) + seq![lir::Instr::LocalSet(1)] + load_ins(reg_off(b)) + + seq![lir::Instr::LocalSet(2)] +} + +/// Both operands are int32 values: the `and` of the two tests. +pub open spec fn guard1_ins() -> Seq { + tag_test(1) + tag_test(2) + seq![lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And))] +} + +/// Push the sign extension of the payload of scratch local `x`. +pub open spec fn sext_local(x: nat) -> Seq { + seq![ + lir::Instr::LocalGet(x), + lir::Instr::Un(lir::UnOp::I32WrapI64), + lir::Instr::Un(lir::UnOp::I64ExtendI32S), + ] +} + +/// The operation in 64 bits, into scratch local 3. +pub open spec fn compute_ins(k: nat) -> Seq { + sext_local(1) + sext_local(2) + seq![ + lir::Instr::Bin(lir::BinOp::I64(super::arith::ibin_of(k))), + lir::Instr::LocalSet(3), + ] +} + +/// The part of the second guard every operation has: local 3 is its own +/// sign-extended low half. Pushes an `i32`. +pub open spec fn fits_ins() -> Seq { + seq![ + lir::Instr::LocalGet(3), + lir::Instr::Un(lir::UnOp::I32WrapI64), + lir::Instr::Un(lir::UnOp::I64ExtendI32S), + lir::Instr::LocalGet(3), + cmp64(lir::ICmp::Eq), + ] +} + +/// A product's extra test, with the fits flag on the stack: the result is +/// not a zero from a negative operand (`+0 × -5` is `-0`, not an int32). +/// Leaves one `i32`: the fits flag and that. +pub open spec fn mul_tail_ins() -> Seq { + seq![lir::Instr::LocalGet(3), lir::Instr::Un(lir::UnOp::I64Eqz)] + sext_local(1) + + sext_local(2) + seq![ + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), + i64c(0), + cmp64(lir::ICmp::LtS), + ] + seq![ + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + ] +} + +/// The test that the result in local 3 is an int32 the semantics encodes as +/// one: it is its own sign-extended low half and, for a product, not a zero +/// from a negative operand. Pushes an `i32`. +pub open spec fn guard2_ins(k: nat) -> Seq { + if k == 2 { + fits_ins() + mul_tail_ins() + } else { + fits_ins() + } +} + +/// The value to store: the low half of local 3, tagged. +pub open spec fn tagged_ins() -> Seq { + seq![ + lir::Instr::LocalGet(3), + i64c(0xFFFF_FFFF), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::And)), + i64c(super::arith::tag_bits()), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), + ] +} + +/// The lowering of small-integer add (`k` 0), subtract (1) or multiply (2) +/// of registers `a` and `b` into `dst`, with an exit to handler `h` from +/// the op at `pc` when an operand is not an int32 or the result is not. +pub open spec fn lower_arith( + k: nat, + dst: nat, + a: nat, + b: nat, + h: js::HandlerId, + pc: nat, +) -> lir::Instr { + let exit = super::run::chain(exit_ins(h, pc)); + let store = super::run::chain(store_ins(reg_off(dst), tagged_ins())); + let inner = super::run::chain((compute_ins(k) + guard2_ins(k)).push(lir::Instr::If( + bt0(), + Box::new(store), + Box::new(exit), + ))); + super::run::chain((operands_ins(a, b) + guard1_ins()).push(lir::Instr::If( + bt0(), + Box::new(inner), + Box::new(exit), + ))) +} + +/// The LIR instruction that implements `op`, the op at `pc` of the function +/// whose code is `c`, with `lbl` the label of a jump's target. Meaningful +/// for the ops [`lowered`] holds of. +pub open spec fn lower(c: js::Code, op: js::Op, pc: nat, lbl: nat) -> lir::Instr { + if js::op_class(op) is Handler { + // Every handler op: an exit to its handler. + super::run::chain(exit_ins(js::op_handler(op), pc)) + } else { + match op { + js::Op::Nop => lir::Instr::Nop, + js::Op::Move { dst, src } => super::run::chain(store_ins(reg_off(dst), load_ins(reg_off(src)))), + js::Op::LoadConst { dst, k } => super::run::chain(set_const(dst, c.consts[k as int])), + js::Op::LoadInt { dst, imm } => super::run::chain(set_const(dst, js::enc_int32(imm))), + js::Op::LoadUndefined { dst } => super::run::chain(set_const(dst, js::UNDEFINED)), + js::Op::LoadNull { dst } => super::run::chain(set_const(dst, js::NULL)), + js::Op::LoadBool { dst, flag } => super::run::chain(set_const(dst, js::enc_bool(flag))), + js::Op::LoadHole { dst } => super::run::chain(set_const(dst, js::HOLE)), + js::Op::GetThis { dst } => super::run::chain( + store_ins(reg_off(dst), load_ins(hdr_off(js::hdr_this()))), + ), + js::Op::GetCallee { dst } => super::run::chain( + store_ins(reg_off(dst), load_ins(hdr_off(js::hdr_callee()))), + ), + js::Op::Jump { .. } => lir::Instr::Br(lbl), + js::Op::JumpIfUndefined { src, .. } => super::run::chain(jump_if_is(src, js::UNDEFINED, true, lbl)), + js::Op::JumpIfNotUndefined { src, .. } => super::run::chain( + jump_if_is(src, js::UNDEFINED, false, lbl), + ), + js::Op::JumpIfNullish { src, .. } => super::run::chain( + load_ins(reg_off(src)) + nullish_rest(false, lbl), + ), + js::Op::JumpIfNotNullish { src, .. } => super::run::chain( + load_ins(reg_off(src)) + nullish_rest(true, lbl), + ), + js::Op::Add { dst, a, b } => lower_arith(0, dst, a, b, js::op_handler(op), pc), + js::Op::Sub { dst, a, b } => lower_arith(1, dst, a, b, js::op_handler(op), pc), + js::Op::Mul { dst, a, b } => lower_arith(2, dst, a, b, js::op_handler(op), pc), + js::Op::Lt { dst, a, b } => super::intcmp::lower_cmp(0, dst, a, b, op, pc), + js::Op::Le { dst, a, b } => super::intcmp::lower_cmp(1, dst, a, b, op, pc), + js::Op::Gt { dst, a, b } => super::intcmp::lower_cmp(2, dst, a, b, op, pc), + js::Op::Ge { dst, a, b } => super::intcmp::lower_cmp(3, dst, a, b, op, pc), + js::Op::StrictEq { dst, a, b } => super::intcmp::lower_cmp(4, dst, a, b, op, pc), + js::Op::StrictNe { dst, a, b } => super::intcmp::lower_cmp(5, dst, a, b, op, pc), + js::Op::BitAnd { dst, a, b } => super::bitops::lower_bit(0, dst, a, b, op, pc), + js::Op::BitOr { dst, a, b } => super::bitops::lower_bit(1, dst, a, b, op, pc), + js::Op::BitXor { dst, a, b } => super::bitops::lower_bit(2, dst, a, b, op, pc), + js::Op::Shl { dst, a, b } => super::bitops::lower_bit(3, dst, a, b, op, pc), + js::Op::Shr { dst, a, b } => super::bitops::lower_bit(4, dst, a, b, op, pc), + js::Op::Inc { dst, src } => super::unops::lower_unary(0, dst, src, op, pc), + js::Op::Dec { dst, src } => super::unops::lower_unary(1, dst, src, op, pc), + js::Op::Neg { dst, src } => super::unops::lower_unary(2, dst, src, op, pc), + js::Op::BitNot { dst, src } => super::unops::lower_unary(3, dst, src, op, pc), + js::Op::UShr { dst, a, b } => super::ushr::lower_ushr(dst, a, b, op, pc), + js::Op::Not { dst, src } => super::truth::lower_not(dst, src, op, pc), + js::Op::JumpIfTrue { cond, .. } => super::truth::lower_jump(false, cond, lbl, op, pc), + js::Op::JumpIfFalse { cond, .. } => super::truth::lower_jump(true, cond, lbl, op, pc), + _ => lir::Instr::Nop, + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/front/mod.rs b/crates/clazel-jit/src/front/mod.rs new file mode 100644 index 000000000..465252298 --- /dev/null +++ b/crates/clazel-jit/src/front/mod.rs @@ -0,0 +1,134 @@ +//! The JS front end (docs/phases/jit.md item J.10): bytecode → LIR, with a +//! proof per op that the LIR **refines `clazel-js-vm`'s `step`**. It sits +//! above LIR, so what it proves holds on both instruction sets: it stops at +//! LIR and needs nothing from the back ends' control flow. +//! +//! The first slice (handler exits, moves and constants, the nullish jumps, +//! small-integer `Add`/`Sub`/`Mul`) was written contract first (CLAUDE.md +//! rule 3): [`spec`] and [`contract`], the statement, came in their own +//! commit, before any lowering. The second slice does not touch the +//! statement; it adds lowerings for the int32 cases of the comparisons, +//! truthiness, unary and bitwise ops (below), and the lemma they rest on: +//! the semantics compares and truncates the doubles int32s stand for, which +//! [`numord`] shows is order, equality and the identity of the integers. +//! +//! # The statement +//! +//! - [`spec`]: **what the front end is about.** The relation between a +//! bytecode machine state and a LIR state (the shared frame layout of +//! phase-7.md "Shapes fixed here for the JIT": the value stack is LIR's +//! region 0, the frame base is local 0), the exit protocol (the exit code +//! names the handler, the operand is `pc`), and the per-op outcome a +//! lowering must have. Read its header first: it lists the modelling +//! choices, among them the one hypothesis about code outside this crate, +//! `handler_complete` (not an axiom; this crate has no `axioms.rs`). +//! - [`contract`]: the refinement statement, [`contract::lowers`]. +//! +//! # The lowerings and their proofs +//! +//! - [`lower`]: the lowerings, as spec functions (`lower`, `lowered`). There +//! is no executable emitter yet: `lir::Instr` holds `nat`s and `Seq`s, so +//! building it needs an executable mirror like [`crate::spec::Op`] first. +//! - Proofs, bottom up: [`lirmem`] (LIR's byte memory), [`run`] (running a +//! list of simple instructions), [`frame`] (a frame slot's address, load +//! and store under the relation), [`proofs`] (the handler exit, constants, +//! copies and jumps), [`arith`] (what LIR computes on NaN-boxed int32s, +//! against `arith_add`, `arith_sub`, `arith_mul`), [`trace`] (the blocks of +//! the integer lowerings), [`intops`] (the integer lowerings), [`numord`] +//! (`f64_of_int` on int32s is strictly order-preserving, injective, and +//! `ToInt32` inverts it), [`blocks`] (a guard flag, a guard exit, then the +//! rest; generic binary and unary lowerings over a value block), +//! [`intcmp`], [`bitops`], [`ushr`], [`unops`] and [`truth`] (the second +//! slice's lowerings and their proofs), [`dispatch`] (`lemma_lower_refines`, +//! by cases on the op). +//! +//! # What is proven (Verus, no axiom, no assumption but `handler_complete`) +//! +//! `lemma_lower_refines`: for every op [`lower::lowered`] holds of, the +//! lowering satisfies [`contract::lowers`]: +//! +//! - **every handler-class op** (`Exp`, `LooseEq`, `LooseNe`, `TypeOf`, +//! `InstanceOf`, `HasProperty`, `DeleteProp`, `GetGlobal`, `SetGlobal`, +//! `NewObject`, `NewArray`, `NewEnv`, `NewClosure`, `New`, `CallSpread`, +//! `GetIterator`, `IterNext`, `IterClose`, `Yield`, `Await`, `Throw`, +//! `ThrowError`): an exit to its handler; +//! - **moves, constants and register loads**: `Nop`, `Move`, `LoadConst`, +//! `LoadInt`, `LoadUndefined`, `LoadNull`, `LoadBool`, `LoadHole`, +//! `GetThis`, `GetCallee`; +//! - **jumps**: `Jump`, `JumpIfUndefined`, `JumpIfNotUndefined`, +//! `JumpIfNullish`, `JumpIfNotNullish` (a `br` or `br_if` to the label the +//! whole-function layout will give the target); +//! - **small-integer `Add`, `Sub`, `Mul`**: two int32 operands and an int32 +//! result (and, for `Mul`, not `-0`) are computed inline; every other case +//! is an exit to the op's handler, which for two doubles or an overflow +//! relies on `handler_complete`. +//! - **int32 comparisons** `Lt`, `Le`, `Gt`, `Ge`, `StrictEq`, `StrictNe` +//! ([`intcmp`]): two int32 operands are compared as integers (the +//! `Number::lessThan` and `Number::equal` of the doubles they stand for are +//! `<` and `==`, [`numord`]) and the encoded boolean stored; anything else +//! exits, which for `StrictEq` and `StrictNe` includes cases where +//! `fast_path` holds and relies on `handler_complete`. +//! - **truthiness** `Not`, `JumpIfTrue`, `JumpIfFalse` ([`truth`]) on a +//! boolean or an int32; anything else exits (`undefined`, `null` and +//! doubles included, again by `handler_complete`). +//! - **int32 unary ops** `Inc`, `Dec`, `Neg`, `BitNot` ([`unops`]): the +//! operand is an int32 whose result is an int32 (not `2^31 - 1` for `Inc`, +//! not `-2^31` for `Dec`, neither `0` (that is `-0`) nor `-2^31` for `Neg`); +//! everything else exits. +//! - **int32 bitwise ops and shifts** `BitAnd`, `BitOr`, `BitXor`, `Shl`, +//! `Shr` ([`bitops`]) and `UShr` ([`ushr`]), on two int32 operands +//! (`ToInt32` of an int32 is the identity, [`numord`]); `UShr` only when +//! the result is below `2^31` (otherwise it is a double); the rest exits. +//! +//! The proofs add no axiom and no assumption beyond `handler_complete`: the +//! facts about `f64_of_int` are theorems about its definition, not axioms. +//! +//! # What is not +//! +//! - **Not lowered** (each is a later slice, with its reason): +//! - `f64` arithmetic: LIR's `f64` operators are uninterpreted functions +//! (`f64_add`) and the bytecode semantics' rounding is a different +//! uninterpreted function (`f64_add_finite`); relating them needs an +//! axiom this crate does not add. So the doubles of `Add`, `Sub` and +//! `Mul`, of the comparisons, `Inc`, `Dec`, `Neg`, the bitwise ops and +//! the shifts exit, and `Div`, `Mod` and `ToNumeric` have no lowering yet +//! (`Div` always gives a double; LIR has no integer `rem` for `Mod`; +//! `ToNumeric` of an int32 is a copy, simply not done yet). +//! - The cases of `StrictEq`/`StrictNe` and the truthiness ops that need no +//! heap (`undefined`, `null`, a boolean against a boolean, doubles) exit +//! rather than being computed inline; only two int32s, or a boolean or an +//! int32 to test, are. +//! - **Property and element access through inline caches** (`GetProp`, +//! `SetProp`, `GetElem`, `SetElem`) and the environment ops: the heap is +//! an abstract map and the cache table a `Seq` of entries, with no byte +//! layout (phase-7.md 7.5 has not fixed one), so there is nothing in +//! LIR's memory to relate them to. +//! - `Call`, `Return` (they move `fp`), `Loop` (reads the termination flag), +//! `GetException`, `GetArgc` (they read state with no byte home yet), and +//! `ThrowIfHole`. +//! - The newer bytecode ops of the unmerged branches (#224, #227, #230, +//! #234): they are not in `main`'s `step`. +//! - **No emitter, no whole function, no tiering, no code cache.** One op at +//! a time, as spec functions. +//! - **The LIR is not shown to type-check** with `clazel-lir`'s `tc`; the +//! proofs show directly that it is never stuck from a related state. +//! - **No tier is claimed.** `tier` stays 0 (CLAUDE.md rule 7). + +pub mod arith; +pub mod bitops; +pub mod blocks; +pub mod contract; +pub mod dispatch; +pub mod frame; +pub mod intcmp; +pub mod intops; +pub mod lirmem; +pub mod lower; +pub mod numord; +pub mod proofs; +pub mod run; +pub mod spec; +pub mod trace; +pub mod truth; +pub mod unops; +pub mod ushr; diff --git a/crates/clazel-jit/src/front/numord.rs b/crates/clazel-jit/src/front/numord.rs new file mode 100644 index 000000000..9d5c3beb0 --- /dev/null +++ b/crates/clazel-jit/src/front/numord.rs @@ -0,0 +1,461 @@ +//! What the semantics' conversion of an int32 to a double does to order, +//! equality and ToInt32: the facts the comparison and bitwise lowerings of +//! [`super::intcmp`] and [`super::bitops`] need, because the bytecode +//! semantics compares and truncates the doubles that int32s stand for +//! (`num_bits`, `f64_of_int`), while LIR compares the integers themselves. +//! +//! Everything here is a proof about `clazel-js-vm`'s `f64_of_int` as it is +//! defined (an open function, not an uninterpreted one): no axiom. For an +//! integer `n` with `1 <= |n| < 2^32`, `f64_of_int(n)` has sign `n < 0`, +//! exponent `ilog2(|n|) + 1023` and fraction `|n| * 2^(52 - k) - 2^52` +//! (`k = ilog2(|n|)`), so its sign-magnitude key is [`korder`], which is +//! strictly increasing. From that: [`lemma_less_than_int`] (`Number::lessThan` +//! of two such doubles is `i < j`), [`lemma_equal_int`] (`Number::equal` is +//! `i == j`), [`lemma_to_int32`] and [`lemma_to_uint32`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `2^52`. +pub open spec fn t52() -> int { + 4503599627370496 +} + +/// The fraction field of `f64_of_int(±m)`. +pub open spec fn mag_frac(m: nat) -> int { + (m * vstd::arithmetic::power2::pow2((52 - js::ilog2(m)) as nat)) as int - vstd::arithmetic::power2::pow2(52) as int +} + +/// The magnitude bits (exponent and fraction) of `f64_of_int(±m)`. +pub open spec fn pos_bits(m: nat) -> int { + (js::ilog2(m) + 1023) * t52() + mag_frac(m) +} + +/// The sign-magnitude key `f64_of_int(n)` has. +pub open spec fn korder(n: int) -> int { + if n == 0 { + 0 + } else if n > 0 { + pos_bits(n as nat) + } else { + -pos_bits((-n) as nat) + } +} + +/// `2^ilog2(n) <= n < 2^(ilog2(n) + 1)`. +pub proof fn lemma_ilog2_bounds(n: nat) + requires + n >= 1, + ensures + vstd::arithmetic::power2::pow2(js::ilog2(n)) <= n, + n < vstd::arithmetic::power2::pow2(js::ilog2(n) + 1), + decreases n, +{ + vstd::arithmetic::power2::lemma2_to64(); + if n <= 1 { + assert(n == 1); + } else { + let h = (n / 2) as nat; + lemma_ilog2_bounds(h); + let l = js::ilog2(h); + assert(js::ilog2(n) == 1 + l); + vstd::arithmetic::power2::lemma_pow2_adds(1, l); + vstd::arithmetic::power2::lemma_pow2_adds(1, l + 1); + assert(vstd::arithmetic::power2::pow2(1 + l) == 2 * vstd::arithmetic::power2::pow2(l)); + assert(vstd::arithmetic::power2::pow2(l + 2) == 2 * vstd::arithmetic::power2::pow2(l + 1)); + } +} + +/// `ilog2` is monotone. +pub proof fn lemma_ilog2_mono(a: nat, b: nat) + requires + 1 <= a <= b, + ensures + js::ilog2(a) <= js::ilog2(b), +{ + lemma_ilog2_bounds(a); + lemma_ilog2_bounds(b); + if js::ilog2(a) > js::ilog2(b) { + let lb = js::ilog2(b); + if js::ilog2(a) > lb + 1 { + vstd::arithmetic::power2::lemma_pow2_strictly_increases(lb + 1, js::ilog2(a)); + } + assert(vstd::arithmetic::power2::pow2(js::ilog2(a)) >= vstd::arithmetic::power2::pow2(lb + 1)); + assert(false); + } +} + +/// The fraction of a magnitude below `2^32` is in `[0, 2^52)`, and its +/// `ilog2` is at most 31. +pub proof fn lemma_frac_bounds(m: nat) + requires + 1 <= m < 0x1_0000_0000, + ensures + js::ilog2(m) <= 31, + 0 <= mag_frac(m) < t52(), +{ + lemma_ilog2_bounds(m); + vstd::arithmetic::power2::lemma2_to64(); + let k = js::ilog2(m); + if k > 31 { + vstd::arithmetic::power2::lemma_pow2_strictly_increases(32, k); + assert(false); + } + let p = vstd::arithmetic::power2::pow2(k); + let q = vstd::arithmetic::power2::pow2((52 - k) as nat); + vstd::arithmetic::power2::lemma_pow2_adds(k, (52 - k) as nat); + vstd::arithmetic::power2::lemma_pow2_adds(k, 1); + vstd::arithmetic::power2::lemma_pow2_pos(k); + vstd::arithmetic::power2::lemma_pow2_pos((52 - k) as nat); + assert(p * q == vstd::arithmetic::power2::pow2(52)); + vstd::arithmetic::power2::lemma2_to64_rest(); + assert(vstd::arithmetic::power2::pow2(52) == t52()); + assert(m * q >= p * q) by (nonlinear_arith) + requires + m >= p, + q > 0, + ; + assert(m * q < 2 * p * q) by (nonlinear_arith) + requires + m < 2 * p, + q > 0, + p > 0, + ; + assert(2 * p * q == 2 * (p * q)) by (nonlinear_arith); +} + +/// `pos_bits` is in `[1023 * 2^52, 1055 * 2^52)`, so positive. +pub proof fn lemma_pos_bits_range(m: nat) + requires + 1 <= m < 0x1_0000_0000, + ensures + 1023 * t52() <= pos_bits(m) < 1055 * t52(), +{ + lemma_frac_bounds(m); +} + +/// `pos_bits` is strictly increasing. +pub proof fn lemma_pos_mono(m1: nat, m2: nat) + requires + 1 <= m1 < m2 < 0x1_0000_0000, + ensures + pos_bits(m1) < pos_bits(m2), +{ + lemma_frac_bounds(m1); + lemma_frac_bounds(m2); + lemma_ilog2_mono(m1, m2); + let k1 = js::ilog2(m1); + let k2 = js::ilog2(m2); + if k1 == k2 { + let q = vstd::arithmetic::power2::pow2((52 - k1) as nat); + vstd::arithmetic::power2::lemma_pow2_pos((52 - k1) as nat); + assert((m2 - m1) * q > 0) by (nonlinear_arith) + requires + m2 > m1, + q > 0, + ; + assert(m2 * q - m1 * q == (m2 - m1) * q) by (nonlinear_arith); + } else { + assert(k1 < k2); + } +} + +/// `korder` is strictly increasing on `(-2^32, 2^32)`. +pub proof fn lemma_korder_mono(i: int, j: int) + requires + -0x1_0000_0000 < i < j < 0x1_0000_0000, + ensures + korder(i) < korder(j), +{ + if i > 0 { + lemma_pos_mono(i as nat, j as nat); + } else if j < 0 { + lemma_pos_mono((-j) as nat, (-i) as nat); + } else if i == 0 { + lemma_pos_bits_range(j as nat); + } else if j == 0 { + lemma_pos_bits_range((-i) as nat); + } else { + lemma_pos_bits_range(j as nat); + lemma_pos_bits_range((-i) as nat); + } +} + +/// Packing a sign, an exponent and a fraction into a double's bits and +/// reading them back. +pub proof fn lemma_pack(sg: u64, e: u64, f: u64) + requires + sg < 2, + e < 2048, + f < 0x10_0000_0000_0000, + ensures + ({ + let b = (sg << 63u64) | (e << 52u64) | f; + &&& b as int == sg as int * 0x8000_0000_0000_0000int + e as int * 0x10_0000_0000_0000int + + f as int + &&& js::f64_sign(b) == sg + &&& js::f64_exp(b) == e + &&& js::f64_frac(b) == f + &&& (b & 0x7FFF_FFFF_FFFF_FFFFu64) as int == e as int * 0x10_0000_0000_0000int + f as int + }), +{ + assert((((sg << 63u64) | (e << 52u64) | f) as int) == sg as int * 0x8000_0000_0000_0000int + + e as int * 0x10_0000_0000_0000int + f as int) by (bit_vector) + requires + sg < 2, + e < 2048, + f < 0x10_0000_0000_0000u64, + ; + assert((((sg << 63u64) | (e << 52u64) | f) >> 63u64) == sg) by (bit_vector) + requires + sg < 2, + e < 2048, + f < 0x10_0000_0000_0000u64, + ; + assert(((((sg << 63u64) | (e << 52u64) | f) >> 52u64) & 0x7FFu64) == e) by (bit_vector) + requires + sg < 2, + e < 2048, + f < 0x10_0000_0000_0000u64, + ; + assert((((sg << 63u64) | (e << 52u64) | f) & 0xF_FFFF_FFFF_FFFFu64) == f) by (bit_vector) + requires + sg < 2, + e < 2048, + f < 0x10_0000_0000_0000u64, + ; + assert(((((sg << 63u64) | (e << 52u64) | f) & 0x7FFF_FFFF_FFFF_FFFFu64) as int) == e as int + * 0x10_0000_0000_0000int + f as int) by (bit_vector) + requires + sg < 2, + e < 2048, + f < 0x10_0000_0000_0000u64, + ; +} + +/// The fields of `f64_of_int(n)` for `n != 0` and `|n| < 2^32`: sign, +/// exponent, fraction, and the key. +pub proof fn lemma_fields(n: int) + requires + n != 0, + -0x1_0000_0000 < n < 0x1_0000_0000, + ensures + ({ + let b = js::f64_of_int(n); + let m = (if n < 0 { -n } else { n }) as nat; + &&& js::f64_sign(b) == (if n < 0 { 1u64 } else { 0u64 }) + &&& js::f64_exp(b) as int == js::ilog2(m) + 1023 + &&& js::f64_frac(b) as int == mag_frac(m) + &&& js::f64_key(b) == korder(n) + }), +{ + let m = (if n < 0 { -n } else { n }) as nat; + lemma_frac_bounds(m); + vstd::arithmetic::power2::lemma2_to64(); + vstd::arithmetic::power2::lemma2_to64_rest(); + assert(vstd::arithmetic::power2::pow2(52) == t52()); + let k = js::ilog2(m); + let sg: u64 = if n < 0 { 1 } else { 0 }; + let e: u64 = (k + 1023) as u64; + let f: u64 = mag_frac(m) as u64; + lemma_pack(sg, e, f); + let pk = (sg << 63u64) | (e << 52u64) | f; + let b = js::f64_of_int(n); + assert(b as int == pk as int) by { + assert(pos_bits(m) == (k + 1023) * t52() + mag_frac(m)); + } + assert(b == pk); + if n > 0 { + assert(js::f64_key(b) == pos_bits(m)); + } else { + assert(js::f64_key(b) == -pos_bits(m)); + } +} + +/// `f64_of_int(n)` is neither NaN, an infinity, nor a negative zero, and is +/// a zero exactly for `0`, which is `+0`. +pub proof fn lemma_shape(n: int) + requires + -0x1_0000_0000 < n < 0x1_0000_0000, + ensures + ({ + let b = js::f64_of_int(n); + &&& !js::is_nan(b) + &&& !js::is_inf(b) + &&& b != 0x8000_0000_0000_0000u64 + &&& (n == 0) <==> b == 0 + &&& js::f64_key(b) == korder(n) + }), +{ + if n == 0 { + assert(js::f64_of_int(0) == 0u64); + assert(js::f64_exp(0u64) == 0) by (bit_vector); + assert((0u64 & 0x7FFF_FFFF_FFFF_FFFFu64) == 0u64) by (bit_vector); + assert((0u64 >> 63u64) == 0u64) by (bit_vector); + assert(js::f64_key(0u64) == 0); + } else { + lemma_fields(n); + let b = js::f64_of_int(n); + let m = (if n < 0 { -n } else { n }) as nat; + lemma_frac_bounds(m); + assert(js::f64_exp(b) != 0x7FF); + assert(js::f64_exp(b) != 0); + assert(b != 0u64) by { + if b == 0 { + assert(js::f64_exp(0u64) == 0) by (bit_vector); + } + } + assert(b != 0x8000_0000_0000_0000u64) by { + if b == 0x8000_0000_0000_0000u64 { + assert(js::f64_exp(0x8000_0000_0000_0000u64) == 0) by (bit_vector); + } + } + assert(b != 0x7FF0_0000_0000_0000u64) by { + if b == 0x7FF0_0000_0000_0000u64 { + assert(js::f64_exp(0x7FF0_0000_0000_0000u64) == 0x7FF) by (bit_vector); + } + } + assert(b != 0xFFF0_0000_0000_0000u64) by { + if b == 0xFFF0_0000_0000_0000u64 { + assert(js::f64_exp(0xFFF0_0000_0000_0000u64) == 0x7FF) by (bit_vector); + } + } + } +} + +/// `Number::lessThan` of the doubles two integers stand for is `i < j` +/// (never `undefined`). +pub proof fn lemma_less_than_int(i: int, j: int) + requires + -0x1_0000_0000 < i < 0x1_0000_0000, + -0x1_0000_0000 < j < 0x1_0000_0000, + ensures + js::number_less_than(js::f64_of_int(i), js::f64_of_int(j)) == Some(i < j), +{ + lemma_shape(i); + lemma_shape(j); + let x = js::f64_of_int(i); + let y = js::f64_of_int(j); + if i == j { + } else if i < j { + lemma_korder_mono(i, j); + assert(x != y); + } else { + lemma_korder_mono(j, i); + assert(x != y); + } +} + +/// `Number::equal` of the doubles two integers stand for is `i == j`. +pub proof fn lemma_equal_int(i: int, j: int) + requires + -0x1_0000_0000 < i < 0x1_0000_0000, + -0x1_0000_0000 < j < 0x1_0000_0000, + ensures + js::number_equal(js::f64_of_int(i), js::f64_of_int(j)) == (i == j), +{ + lemma_shape(i); + lemma_shape(j); + let x = js::f64_of_int(i); + let y = js::f64_of_int(j); + if i == j { + } else if i < j { + lemma_korder_mono(i, j); + assert(x != y); + } else { + lemma_korder_mono(j, i); + assert(x != y); + } +} + +/// The truncated magnitude of `f64_of_int(n)` is `|n|`. +pub proof fn lemma_trunc_abs(n: int) + requires + n != 0, + -0x1_0000_0000 < n < 0x1_0000_0000, + ensures + js::f64_trunc_abs(js::f64_of_int(n)) == (if n < 0 { -n } else { n }) as nat, +{ + lemma_fields(n); + let b = js::f64_of_int(n); + let m = (if n < 0 { -n } else { n }) as nat; + lemma_frac_bounds(m); + vstd::arithmetic::power2::lemma2_to64(); + let k = js::ilog2(m); + let q = vstd::arithmetic::power2::pow2((52 - k) as nat); + vstd::arithmetic::power2::lemma_pow2_pos((52 - k) as nat); + let e: int = js::f64_exp(b) as int; + assert(e == k + 1023); + let mm: int = vstd::arithmetic::power2::pow2(52) as int + js::f64_frac(b) as int; + vstd::arithmetic::power2::lemma2_to64_rest(); + assert(vstd::arithmetic::power2::pow2(52) == t52()); + assert(mm == m * q); + let kk: int = e - 1075; + assert(kk < 0); + assert(-kk == 52 - k); + assert(((m * q) as int) / (q as int) == m as int) by { + vstd::arithmetic::div_mod::lemma_div_multiples_vanish(m as int, q as int); + assert(q * m == m * q) by (nonlinear_arith); + } +} + +/// `ToInt32` of the double an int32 stands for is that int32. +pub proof fn lemma_to_int32(i: i32) + ensures + js::to_int32(js::f64_of_int(i as int)) == i, +{ + let n = i as int; + lemma_shape(n); + let b = js::f64_of_int(n); + if n == 0 { + assert(b == 0u64); + } else { + lemma_fields(n); + lemma_trunc_abs(n); + assert(js::is_finite(b)); + assert(!js::is_zero(b)); + let t: int = js::f64_trunc_abs(b) as int; + let sgn: int = if js::f64_sign(b) == 1 { -t } else { t }; + assert(sgn == n); + assert(js::f64_int32bit(b) as int == n % 4294967296int); + if n > 0 { + assert(n % 4294967296int == n); + } else { + assert(n % 4294967296int == n + 4294967296int); + } + } +} + +/// `ToUint32` of the double an int32 stands for is the int32 read unsigned. +pub proof fn lemma_to_uint32(i: i32) + ensures + js::to_uint32(js::f64_of_int(i as int)) == (i as u32), +{ + let n = i as int; + lemma_shape(n); + let b = js::f64_of_int(n); + assert(((i as u32) as int) == (if i >= 0 { i as int } else { i as int + 4294967296int })) by (bit_vector); + if n == 0 { + assert(b == 0u64); + } else { + lemma_fields(n); + lemma_trunc_abs(n); + assert(js::is_finite(b)); + assert(!js::is_zero(b)); + let t: int = js::f64_trunc_abs(b) as int; + let sgn: int = if js::f64_sign(b) == 1 { -t } else { t }; + assert(sgn == n); + assert(js::f64_int32bit(b) as int == n % 4294967296int); + if n > 0 { + assert(n % 4294967296int == n); + } else { + assert(n % 4294967296int == n + 4294967296int); + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/front/proofs.rs b/crates/clazel-jit/src/front/proofs.rs new file mode 100644 index 000000000..e95852702 --- /dev/null +++ b/crates/clazel-jit/src/front/proofs.rs @@ -0,0 +1,582 @@ +//! The refinement proofs: for each lowered op, [`lower`]'s instruction +//! satisfies [`lowers`] ([`lemma_lower_refines`]). Each case runs LIR's `eval` +//! on the instruction from a related state, with the block lemmas of +//! [`super::frame`], and compares the outcome with `step`. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + + +/// What a lowering's proof may assume of the states (the hypotheses of +/// [`lowers`]). +pub open spec fn pre( + c: js::Code, + op: js::Op, + pc: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, +) -> bool { + &&& fuel >= lower_fuel() + &&& rel(lay, s, st) + &&& st.stack == Seq::::empty() + &&& js::state_wf(s) + &&& js::cur_code(s) == c + &&& s.pc == pc + &&& js::fetch(s) == op + &&& handler_complete(s, op) +} + +/// What `state_wf` gives the proofs: the frame is inside the region, and the +/// op's operands are in range. +pub proof fn lemma_state_facts(s: js::State) + requires + js::state_wf(s), + ensures + s.fp + js::frame_size(js::cur_code(s)) <= s.region.len(), + js::op_wf(js::cur_code(s), js::fetch(s)), + s.fp + js::hdr_slots() <= s.region.len(), +{ + assert(js::frame_wf(s, s.fp)); + let c = js::cur_code(s); + assert(js::code_wf(c)); + assert(0 <= s.pc < c.ops.len()); + assert(js::op_wf(c, c.ops[s.pc as int])); +} + +/// A register of the frame is a slot of the region. +pub proof fn lemma_reg_slot_ok(s: js::State, r: nat) + requires + js::state_wf(s), + js::reg_ok(js::cur_code(s), r), + ensures + super::frame::slot_ok(s, reg_off(r)), + js::reg_slot(s.fp, r) == s.fp + reg_off(r) / 8, + js::reg_slot(s.fp, r) < s.region.len(), +{ + lemma_state_facts(s); + assert(reg_off(r) == 8 * (4 + r)); + assert(reg_off(r) / 8 == 4 + r) by (nonlinear_arith) + requires + reg_off(r) == 8 * (4 + r), + ; +} + +/// A header slot of the frame is a slot of the region. +pub proof fn lemma_hdr_slot_ok(s: js::State, k: nat) + requires + js::state_wf(s), + k < js::hdr_slots(), + ensures + super::frame::slot_ok(s, hdr_off(k)), + hdr_off(k) / 8 == k, +{ + lemma_state_facts(s); + assert(hdr_off(k) / 8 == k) by (nonlinear_arith) + requires + hdr_off(k) == 8 * k, + ; +} + +/// `rel` does not look at `pc`. +pub proof fn lemma_rel_pc(lay: lir::Layout, s: js::State, st: lir::St, pc: nat) + requires + rel(lay, s, st), + ensures + rel(lay, js::State { pc: pc, ..s }, st), +{ +} + +/// A handler op is an exit to its handler. +pub proof fn lemma_handler_op( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, +) + requires + pre(c, op, pc, lay, s, st, fuel), + js::op_class(op) is Handler, + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + clazel_js_vm::lemmas::lemma_handler_never_fast(s, op); + let h = js::op_handler(op); + let ins = super::lower::exit_ins(h, pc); + assert(super::run::all_simple(ins)) by { + assert forall|i: int| 0 <= i < ins.len() implies super::run::is_simple(#[trigger] ins[i]) by { + assert(i == 0 || i == 1); + } + } + assert(super::lower::lower(c, op, pc, lbl) == super::run::chain(ins)); + super::run::lemma_chain(lay, ins, st, fuel); + reveal_with_fuel(super::run::exec, 3); + let st1 = st.with_stack(seq![lir::Val::I32(pc as u32)]); + assert(st.stack.push(lir::Val::I32(pc as u32)) =~= seq![lir::Val::I32(pc as u32)]); + assert(lir::top(st1.stack, 1) =~= seq![lir::Val::I32(pc as u32)]); +} + + +/// A store of `v` in register `dst`, by a value block `val`, after which the +/// op falls through: the outcome refines `step`, whose result is +/// `next(set_reg(s, dst, v))`. +pub proof fn lemma_store_reg_outcome( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + dst: nat, + val: Seq, + v: u64, + mid: lir::St, +) + requires + pre(c, op, pc, lay, s, st, fuel), + js::reg_ok(js::cur_code(s), dst), + super::lower::lower(c, op, pc, lbl) == super::run::chain(super::lower::store_ins(reg_off(dst), val)), + super::run::all_simple(val), + val.len() < 40, + js::fast_path(s, op), + js::fast_step(s, op) == js::next(js::set_reg(s, dst, v)), + super::run::exec( + lay, + val, + st.with_stack(st.stack.push(lir::Val::I64((8 * s.fp + reg_off(dst)) as u64))), + ) == lir::Outcome::Normal(mid), + mid.stack == st.stack.push(lir::Val::I64((8 * s.fp + reg_off(dst)) as u64)).push( + lir::Val::I64(v), + ), + mid.mem == st.mem, + locals_rel(s, mid.locals), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_reg_slot_ok(s, dst); + let off = reg_off(dst); + let ins = super::lower::store_ins(off, val); + assert(super::run::all_simple(ins)) by { + assert forall|i: int| 0 <= i < ins.len() implies super::run::is_simple(#[trigger] ins[i]) by { + if i < 4 { + assert(ins[i] == super::lower::addr_ins(off)[i]); + } else if i < 4 + val.len() { + assert(ins[i] == val[i - 4]); + } else { + assert(i == 4 + val.len()); + } + } + } + assert(ins.len() == 5 + val.len()); + super::run::lemma_chain(lay, ins, st, fuel); + super::frame::lemma_exec_store(lay, s, st, off, val, v, mid); + let j = (s.fp + off / 8) as int; + let s2 = js::fast_step(s, op); + assert(s2.region == s.region.update(j, v)); + assert(s2 == js::State { pc: s.pc + 1, ..js::State { region: s.region.update(j, v), ..s } }); + lemma_rel_pc(lay, js::State { region: s.region.update(j, v), ..s }, lir::St { + stack: st.stack, + locals: mid.locals, + mem: lir::write_le(st.mem, slot_addr(lay, j), v as nat, 8), + }, s.pc + 1); +} + + +/// The address, load and constant blocks are simple. +pub proof fn lemma_blocks_simple(off: nat, v: u64) + ensures + super::run::all_simple(super::lower::addr_ins(off)), + super::run::all_simple(super::lower::load_ins(off)), + super::run::all_simple(seq![super::lower::i64c(v)]), + super::lower::load_ins(off).len() == 5, +{ + let a = super::lower::addr_ins(off); + assert(super::run::all_simple(a)) by { + assert forall|i: int| 0 <= i < a.len() implies super::run::is_simple(#[trigger] a[i]) by { + assert(0 <= i < 4); + } + } + let l = super::lower::load_ins(off); + assert(super::run::all_simple(l)) by { + assert forall|i: int| 0 <= i < l.len() implies super::run::is_simple(#[trigger] l[i]) by { + if i < 4 { + assert(l[i] == a[i]); + } + } + } + assert(super::run::all_simple(seq![super::lower::i64c(v)])) by { + assert forall|i: int| 0 <= i < 1 implies super::run::is_simple(#[trigger] seq![super::lower::i64c(v)][i]) by {} + } +} + +/// An op that stores a constant in a register. +pub proof fn lemma_const_op( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + dst: nat, + v: u64, +) + requires + pre(c, op, pc, lay, s, st, fuel), + js::reg_ok(js::cur_code(s), dst), + super::lower::lower(c, op, pc, lbl) == super::run::chain(super::lower::set_const(dst, v)), + js::fast_path(s, op), + js::fast_step(s, op) == js::next(js::set_reg(s, dst, v)), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_blocks_simple(0, v); + lemma_reg_slot_ok(s, dst); + let sa = st.with_stack(st.stack.push(lir::Val::I64((8 * s.fp + reg_off(dst)) as u64))); + reveal_with_fuel(super::run::exec, 2); + let mid = sa.with_stack(sa.stack.push(lir::Val::I64(v))); + assert(super::run::exec(lay, seq![super::lower::i64c(v)], sa) == lir::Outcome::Normal(mid)); + lemma_store_reg_outcome(c, op, pc, lbl, lay, s, st, fuel, dst, seq![super::lower::i64c(v)], v, mid); +} + +/// An op that copies a slot (a register or a header slot) into a register. +pub proof fn lemma_copy_op( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + dst: nat, + src_off: nat, +) + requires + pre(c, op, pc, lay, s, st, fuel), + js::reg_ok(js::cur_code(s), dst), + super::frame::slot_ok(s, src_off), + super::lower::lower(c, op, pc, lbl) == super::run::chain(super::lower::store_ins(reg_off(dst), super::lower::load_ins(src_off))), + js::fast_path(s, op), + js::fast_step(s, op) == js::next( + js::set_reg(s, dst, s.region[(s.fp + src_off / 8) as int]), + ), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_blocks_simple(src_off, 0); + lemma_reg_slot_ok(s, dst); + let sa = st.with_stack(st.stack.push(lir::Val::I64((8 * s.fp + reg_off(dst)) as u64))); + assert(rel(lay, s, sa)); + super::frame::lemma_exec_load(lay, s, sa, src_off); + let v = s.region[(s.fp + src_off / 8) as int]; + let mid = sa.with_stack(sa.stack.push(lir::Val::I64(v))); + assert(super::run::exec(lay, super::lower::load_ins(src_off), sa) == lir::Outcome::Normal(mid)); + lemma_store_reg_outcome( + c, op, pc, lbl, lay, s, st, fuel, dst, super::lower::load_ins(src_off), v, mid, + ); +} + + +/// `Jump`: a branch to the label. +pub proof fn lemma_jump_op( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + target: nat, +) + requires + pre(c, op, pc, lay, s, st, fuel), + op == (js::Op::Jump { target }), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + assert(js::op_class(op) is Core); + clazel_js_vm::lemmas::lemma_core_always_fast(s, op); + assert(super::lower::lower(c, op, pc, lbl) == lir::Instr::Br(lbl)); + lemma_rel_pc(lay, s, st, target); +} + +/// A conditional jump on the register `src` being (or not being) the value +/// `v`: `br_if` of a comparison with the constant. +/// +/// The resource limit is raised: with the second slice's lowerings in scope +/// (`lower::lower` names them all) the proof is near the default limit. +#[verifier::rlimit(100)] +pub proof fn lemma_jump_if_is( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + src: nat, + v: u64, + eq: bool, + target: nat, +) + requires + pre(c, op, pc, lay, s, st, fuel), + js::reg_ok(js::cur_code(s), src), + super::lower::lower(c, op, pc, lbl) == super::run::chain(super::lower::jump_if_is(src, v, eq, lbl)), + jump_target(op) == Some(target), + js::fast_path(s, op), + js::fast_step(s, op) == (if (js::reg(s, src) == v) == eq { + js::jump(s, target) + } else { + js::next(s) + }), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_blocks_simple(reg_off(src), v); + lemma_reg_slot_ok(s, src); + let rest = seq![ + super::lower::i64c(v), + super::lower::cmp64(if eq { lir::ICmp::Eq } else { lir::ICmp::Ne }), + lir::Instr::BrIf(lbl), + ]; + let ins = super::lower::jump_if_is(src, v, eq, lbl); + assert(super::run::all_simple(ins)) by { + let l = super::lower::load_ins(reg_off(src)); + assert forall|i: int| 0 <= i < ins.len() implies super::run::is_simple(#[trigger] ins[i]) by { + if i < 5 { + assert(ins[i] == l[i]); + } else { + assert(ins[i] == rest[i - 5]); + } + } + } + super::run::lemma_chain(lay, ins, st, fuel); + super::run::lemma_exec_concat(lay, super::lower::load_ins(reg_off(src)), rest, st); + super::frame::lemma_exec_load(lay, s, st, reg_off(src)); + let x = js::reg(s, src); + assert(x == s.region[(s.fp + reg_off(src) / 8) as int]); + let sl = st.with_stack(st.stack.push(lir::Val::I64(x))); + assert(st.stack.push(lir::Val::I64(x)) =~= seq![lir::Val::I64(x)]); + reveal_with_fuel(super::run::exec, 4); + let cond = lir::bool_i32(lir::icmp(if eq { lir::ICmp::Eq } else { lir::ICmp::Ne }, 64, x as int, v as int)); + assert(cond != 0 <==> ((x == v) == eq)); + let s1 = st.with_stack(Seq::::empty()); + assert(lir::below(seq![lir::Val::I32(cond)], 1) =~= Seq::::empty()); + assert(lir::below(seq![lir::Val::I64(x), lir::Val::I64(v)], 2) =~= Seq::::empty()); + assert(st.stack =~= Seq::::empty()); + let sc = sl.with_stack(seq![lir::Val::I64(x), lir::Val::I64(v)]); + assert(sl.stack.push(lir::Val::I64(v)) =~= seq![lir::Val::I64(x), lir::Val::I64(v)]); + assert(lir::eval(lay, super::lower::i64c(v), sl, 1) == lir::Outcome::Normal(sc)); + let sd = sl.with_stack(seq![lir::Val::I32(cond)]); + assert(lir::eval(lay, rest[1], sc, 1) == lir::Outcome::Normal(sd)); + assert(super::run::exec(lay, rest, sl) == (if cond != 0 { + lir::Outcome::Branch(lbl, s1) + } else { + lir::Outcome::Normal(s1) + })); + assert(s1 == sd.with_stack(lir::below(sd.stack, 1))); + lemma_rel_pc(lay, s, s1, target); + lemma_rel_pc(lay, s, s1, s.pc + 1); +} + + +/// The nullish test as a number: `e1`, `e2` are the two comparisons' +/// results, and `cond` their combination is non-zero exactly when the value is +/// (not) nullish. +pub proof fn lemma_nullish_cond(x: u64, not_nullish: bool) + ensures + ({ + let o1 = if not_nullish { lir::ICmp::Ne } else { lir::ICmp::Eq }; + let comb = if not_nullish { lir::IBin::And } else { lir::IBin::Or }; + let e1 = lir::bool_i32(lir::icmp(o1, 64, x as int, js::UNDEFINED as int)); + let e2 = lir::bool_i32(lir::icmp(o1, 64, x as int, js::NULL as int)); + lir::ibin32(comb, e1, e2) != 0 <==> (if not_nullish { + x != js::UNDEFINED && x != js::NULL + } else { + x == js::UNDEFINED || x == js::NULL + }) + }), +{ + let o1 = if not_nullish { lir::ICmp::Ne } else { lir::ICmp::Eq }; + let comb = if not_nullish { lir::IBin::And } else { lir::IBin::Or }; + let e1 = lir::bool_i32(lir::icmp(o1, 64, x as int, js::UNDEFINED as int)); + let e2 = lir::bool_i32(lir::icmp(o1, 64, x as int, js::NULL as int)); + assert(e1 == 0 || e1 == 1); + assert(e2 == 0 || e2 == 1); + if not_nullish { + assert(lir::ibin32(comb, e1, e2) == (e1 & e2)); + assert((e1 & e2) != 0 <==> (e1 == 1 && e2 == 1)) by (bit_vector) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ; + } else { + assert(lir::ibin32(comb, e1, e2) == (e1 | e2)); + assert((e1 | e2) != 0 <==> (e1 == 1 || e2 == 1)) by (bit_vector) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ; + } +} + +/// What the nullish block does to LIR's state: the value of the register +/// is on the stack; it is kept in scratch local 1, compared twice, combined, +/// and branched on. +pub proof fn lemma_nullish_trace( + lay: lir::Layout, + sl: lir::St, + x: u64, + not_nullish: bool, + lbl: nat, +) + requires + sl.stack == seq![lir::Val::I64(x)], + sl.locals.len() == nlocals(), + ensures + ({ + let o1 = if not_nullish { lir::ICmp::Ne } else { lir::ICmp::Eq }; + let comb = if not_nullish { lir::IBin::And } else { lir::IBin::Or }; + let e1 = lir::bool_i32(lir::icmp(o1, 64, x as int, js::UNDEFINED as int)); + let e2 = lir::bool_i32(lir::icmp(o1, 64, x as int, js::NULL as int)); + let s1 = lir::St { + stack: Seq::::empty(), + locals: sl.locals.update(1, lir::Val::I64(x)), + mem: sl.mem, + }; + super::run::exec(lay, super::lower::nullish_rest(not_nullish, lbl), sl) == (if lir::ibin32(comb, e1, e2) != 0 { + lir::Outcome::Branch(lbl, s1) + } else { + lir::Outcome::Normal(s1) + }) + }), +{ + let o1 = if not_nullish { lir::ICmp::Ne } else { lir::ICmp::Eq }; + let comb = if not_nullish { lir::IBin::And } else { lir::IBin::Or }; + let e1 = lir::bool_i32(lir::icmp(o1, 64, x as int, js::UNDEFINED as int)); + let e2 = lir::bool_i32(lir::icmp(o1, 64, x as int, js::NULL as int)); + let loc2 = sl.locals.update(1, lir::Val::I64(x)); + let rest = super::lower::nullish_rest(not_nullish, lbl); + let a1 = lir::St { stack: seq![lir::Val::I64(x)], locals: loc2, mem: sl.mem }; + let a2 = lir::St { stack: seq![lir::Val::I64(x), lir::Val::I64(js::UNDEFINED)], locals: loc2, mem: sl.mem }; + let a3 = lir::St { stack: seq![lir::Val::I32(e1)], locals: loc2, mem: sl.mem }; + let a4 = lir::St { stack: seq![lir::Val::I32(e1), lir::Val::I64(x)], locals: loc2, mem: sl.mem }; + let a5 = lir::St { + stack: seq![lir::Val::I32(e1), lir::Val::I64(x), lir::Val::I64(js::NULL)], + locals: loc2, + mem: sl.mem, + }; + let a6 = lir::St { stack: seq![lir::Val::I32(e1), lir::Val::I32(e2)], locals: loc2, mem: sl.mem }; + let cond = lir::ibin32(comb, e1, e2); + let a7 = lir::St { stack: seq![lir::Val::I32(cond)], locals: loc2, mem: sl.mem }; + let s1 = lir::St { stack: Seq::::empty(), locals: loc2, mem: sl.mem }; + assert(lir::eval(lay, rest[0], sl, 1) == lir::Outcome::Normal(a1)); + assert(a1.stack.push(lir::Val::I64(js::UNDEFINED)) =~= a2.stack); + assert(lir::eval(lay, rest[1], a1, 1) == lir::Outcome::Normal(a2)); + assert(lir::below(a2.stack, 2) =~= Seq::::empty()); + assert(lir::eval(lay, rest[2], a2, 1) == lir::Outcome::Normal(a3)); + assert(loc2[1] == lir::Val::I64(x)); + assert(a3.stack.push(lir::Val::I64(x)) =~= a4.stack); + assert(lir::eval(lay, rest[3], a3, 1) == lir::Outcome::Normal(a4)); + assert(a4.stack.push(lir::Val::I64(js::NULL)) =~= a5.stack); + assert(lir::eval(lay, rest[4], a4, 1) == lir::Outcome::Normal(a5)); + assert(lir::below(a5.stack, 2) =~= seq![lir::Val::I32(e1)]); + assert(seq![lir::Val::I32(e1)].push(lir::Val::I32(e2)) =~= a6.stack); + assert(lir::eval(lay, rest[5], a5, 1) == lir::Outcome::Normal(a6)); + assert(lir::below(a6.stack, 2) =~= Seq::::empty()); + assert(Seq::::empty().push(lir::Val::I32(cond)) =~= a7.stack); + assert(lir::eval(lay, rest[6], a6, 1) == lir::Outcome::Normal(a7)); + assert(lir::below(a7.stack, 1) =~= Seq::::empty()); + reveal_with_fuel(super::run::exec, 9); +} + + +/// A conditional jump on the register `src` being nullish (`undefined` or +/// `null`), or not. +pub proof fn lemma_jump_nullish( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + src: nat, + not_nullish: bool, + target: nat, +) + requires + pre(c, op, pc, lay, s, st, fuel), + js::reg_ok(js::cur_code(s), src), + super::lower::lower(c, op, pc, lbl) == super::run::chain(super::lower::load_ins(reg_off(src)) + super::lower::nullish_rest(not_nullish, lbl)), + jump_target(op) == Some(target), + js::fast_path(s, op), + js::fast_step(s, op) == (if (if not_nullish { + js::reg(s, src) != js::UNDEFINED && js::reg(s, src) != js::NULL + } else { + js::reg(s, src) == js::UNDEFINED || js::reg(s, src) == js::NULL + }) { + js::jump(s, target) + } else { + js::next(s) + }), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_blocks_simple(reg_off(src), 0); + lemma_reg_slot_ok(s, src); + let rest = super::lower::nullish_rest(not_nullish, lbl); + let l = super::lower::load_ins(reg_off(src)); + let ins = l + rest; + assert(super::run::all_simple(ins)) by { + assert forall|i: int| 0 <= i < ins.len() implies super::run::is_simple(#[trigger] ins[i]) by { + if i < 5 { + assert(ins[i] == l[i]); + } else { + assert(ins[i] == rest[i - 5]); + } + } + } + assert(ins.len() == 13); + super::run::lemma_chain(lay, ins, st, fuel); + super::run::lemma_exec_concat(lay, l, rest, st); + super::frame::lemma_exec_load(lay, s, st, reg_off(src)); + let x = js::reg(s, src); + assert(x == s.region[(s.fp + reg_off(src) / 8) as int]); + let sl = st.with_stack(seq![lir::Val::I64(x)]); + assert(st.stack.push(lir::Val::I64(x)) =~= seq![lir::Val::I64(x)]); + lemma_nullish_trace(lay, sl, x, not_nullish, lbl); + lemma_nullish_cond(x, not_nullish); + let loc2 = st.locals.update(1, lir::Val::I64(x)); + let s1 = lir::St { stack: Seq::::empty(), locals: loc2, mem: st.mem }; + assert(locals_rel(s, loc2)) by { + assert forall|j: int| 1 <= j < loc2.len() implies #[trigger] loc2[j] is I64 by { + if j != 1 { + assert(loc2[j] == st.locals[j]); + } + } + assert(loc2[0] == st.locals[0]); + } + assert(rel(lay, s, s1)); + lemma_rel_pc(lay, s, s1, target); + lemma_rel_pc(lay, s, s1, s.pc + 1); +} + +} // verus! diff --git a/crates/clazel-jit/src/front/run.rs b/crates/clazel-jit/src/front/run.rs new file mode 100644 index 000000000..8199deeea --- /dev/null +++ b/crates/clazel-jit/src/front/run.rs @@ -0,0 +1,190 @@ +//! Running a list of simple LIR instructions: [`chain`] builds one `Then` +//! chain from a list, [`exec`] runs the list instruction by instruction, and +//! [`lemma_chain`] says `eval` of the chain is `exec` of the list. The +//! lowerings are chains, so each block's effect is proven on `exec`, with no +//! fuel in sight. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// LIR's `Then`. +pub open spec fn then(a: lir::Instr, b: lir::Instr) -> lir::Instr { + lir::Instr::Then(Box::new(a), Box::new(b)) +} + +/// The instructions, one after the other, as one instruction (right-nested +/// `Then`; `Nop` for the empty list). +pub open spec fn chain(ins: Seq) -> lir::Instr + decreases ins.len(), +{ + if ins.len() == 0 { + lir::Instr::Nop + } else if ins.len() == 1 { + ins[0] + } else { + then(ins[0], chain(ins.skip(1))) + } +} + +/// The instruction has no structure: `eval` of it does not recurse. +pub open spec fn is_simple(i: lir::Instr) -> bool { + match i { + lir::Instr::Nop => true, + lir::Instr::Const(_) => true, + lir::Instr::LocalGet(_) => true, + lir::Instr::LocalSet(_) => true, + lir::Instr::LocalTee(_) => true, + lir::Instr::Drop => true, + lir::Instr::Select => true, + lir::Instr::Bin(_) => true, + lir::Instr::Un(_) => true, + lir::Instr::BoundsCheck { .. } => true, + lir::Instr::Load { .. } => true, + lir::Instr::Store { .. } => true, + lir::Instr::Exit(_, _) => true, + lir::Instr::Br(_) => true, + lir::Instr::BrIf(_) => true, + _ => false, + } +} + +/// Every instruction of the list is simple. +pub open spec fn all_simple(ins: Seq) -> bool { + forall|i: int| 0 <= i < ins.len() ==> is_simple(#[trigger] ins[i]) +} + +/// Run the list in order, each instruction with fuel 1 (enough for a simple +/// one), stopping at the first outcome that is not `Normal`. +pub open spec fn exec(lay: lir::Layout, ins: Seq, st: lir::St) -> lir::Outcome + decreases ins.len(), +{ + if ins.len() == 0 { + lir::Outcome::Normal(st) + } else { + match lir::eval(lay, ins[0], st, 1) { + lir::Outcome::Normal(s1) => exec(lay, ins.skip(1), s1), + o => o, + } + } +} + +/// A simple instruction's outcome does not depend on the fuel, from 1. +pub proof fn lemma_simple_fuel(lay: lir::Layout, i: lir::Instr, st: lir::St, f1: nat, f2: nat) + requires + is_simple(i), + f1 >= 1, + f2 >= 1, + ensures + lir::eval(lay, i, st, f1) == lir::eval(lay, i, st, f2), +{ +} + +/// `eval` of the chain of a list of simple instructions is [`exec`] of it, +/// given enough fuel. +pub proof fn lemma_chain(lay: lir::Layout, ins: Seq, st: lir::St, fuel: nat) + requires + all_simple(ins), + fuel > ins.len(), + ensures + lir::eval(lay, chain(ins), st, fuel) == exec(lay, ins, st), + decreases ins.len(), +{ + if ins.len() == 0 { + assert(chain(ins) == lir::Instr::Nop); + } else if ins.len() == 1 { + assert(chain(ins) == ins[0]); + lemma_simple_fuel(lay, ins[0], st, fuel, 1); + assert(ins.skip(1).len() == 0); + match lir::eval(lay, ins[0], st, 1) { + lir::Outcome::Normal(s1) => { + assert(exec(lay, ins, st) == exec(lay, ins.skip(1), s1)); + }, + _ => {}, + } + } else { + let rest = ins.skip(1); + assert(chain(ins) == then(ins[0], chain(rest))); + assert(all_simple(rest)) by { + assert forall|i: int| 0 <= i < rest.len() implies is_simple(#[trigger] rest[i]) by { + assert(rest[i] == ins[i + 1]); + } + } + lemma_simple_fuel(lay, ins[0], st, (fuel - 1) as nat, 1); + match lir::eval(lay, ins[0], st, 1) { + lir::Outcome::Normal(s1) => { + lemma_chain(lay, rest, s1, (fuel - 1) as nat); + }, + _ => {}, + } + } +} + +/// Running two lists in a row. +pub proof fn lemma_exec_concat(lay: lir::Layout, a: Seq, b: Seq, st: lir::St) + ensures + exec(lay, a + b, st) == match exec(lay, a, st) { + lir::Outcome::Normal(s1) => exec(lay, b, s1), + o => o, + }, + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + } else { + assert((a + b)[0] == a[0]); + assert((a + b).skip(1) =~= a.skip(1) + b); + match lir::eval(lay, a[0], st, 1) { + lir::Outcome::Normal(s1) => { + lemma_exec_concat(lay, a.skip(1), b, s1); + }, + _ => {}, + } + } +} + +/// `eval` of a chain of simple instructions followed by one arbitrary +/// instruction: the simple ones run, then `last` with the fuel that is left. +pub proof fn lemma_chain_last( + lay: lir::Layout, + ins: Seq, + last: lir::Instr, + st: lir::St, + fuel: nat, +) + requires + all_simple(ins), + fuel > ins.len(), + ensures + lir::eval(lay, chain(ins.push(last)), st, fuel) == match exec(lay, ins, st) { + lir::Outcome::Normal(s1) => lir::eval(lay, last, s1, (fuel - ins.len()) as nat), + o => o, + }, + decreases ins.len(), +{ + if ins.len() == 0 { + assert(ins.push(last) =~= seq![last]); + } else { + let rest = ins.skip(1); + assert(all_simple(rest)) by { + assert forall|i: int| 0 <= i < rest.len() implies is_simple(#[trigger] rest[i]) by { + assert(rest[i] == ins[i + 1]); + } + } + assert(ins.push(last).len() >= 2); + assert(ins.push(last).skip(1) =~= rest.push(last)); + assert(ins.push(last)[0] == ins[0]); + lemma_simple_fuel(lay, ins[0], st, (fuel - 1) as nat, 1); + match lir::eval(lay, ins[0], st, 1) { + lir::Outcome::Normal(s1) => { + lemma_chain_last(lay, rest, last, s1, (fuel - 1) as nat); + }, + _ => {}, + } + } +} + +} // verus! diff --git a/crates/clazel-jit/src/front/spec.rs b/crates/clazel-jit/src/front/spec.rs new file mode 100644 index 000000000..9cdc80e95 --- /dev/null +++ b/crates/clazel-jit/src/front/spec.rs @@ -0,0 +1,444 @@ +//! What the JS front end is *about* (jit.md item J.10): the state relation +//! between a bytecode machine state and a LIR state, the exit protocol, and +//! the per-op outcome a lowering must have. Nothing here runs and nothing is +//! proven; the lowerings are in `lower` and their proofs after it. +//! +//! # Source +//! +//! There is no standard to transcribe. The sources are: +//! +//! - **jit.md, "J.10 JS front end"**: "One lowering per bytecode op: fast +//! paths inline [...]; everything else is an exit to the shared handler. +//! Proof per op: the LIR refines `step`. This is written once and holds on +//! both instruction sets, because it stops at LIR." +//! - **phase-7.md, "Shapes fixed here for the JIT"**: "Each frame is a window +//! of `u64` slots in one value-stack region: header slots [...], then +//! arguments, then registers. The interpreter and the JIT read and write +//! the same slots." And: "Every op that can allocate, call out, or throw is +//! distinguished, and is an exit in the JIT and a handler call in the +//! interpreter. Handlers are plain Rust functions over the frame region and +//! the heap, shared by both." (item 7.8 repeats it.) +//! - **`clazel-js-vm`'s `spec`** ([`js::step`], [`js::fast_path`], +//! [`js::fast_step`], [`js::handler`]): the meaning of the source. +//! - **`clazel-lir`'s `spec`** ([`lir::eval`]): the meaning of the target. +//! +//! # Modelling choices +//! +//! 1. **The value stack is LIR region 0.** Slot `j` of the bytecode machine's +//! `region` is the 8 bytes of LIR memory at `base(0) + 8 j`, little-endian +//! ([`mem_rel`]), and the region's bound is `8 × region.len()` +//! ([`layout_ok`]). A NaN-boxed value is stored as the `u64` it is, so the +//! interpreter and the JIT read and write the same bytes. +//! 2. **The frame base is LIR local 0**: the `i64` `8 × fp`, the byte offset +//! of the current frame in region 0 ([`locals_rel`]). Register `r` is at +//! byte `8 (hdr_slots + r)` from it ([`reg_off`]), a header slot `k` at +//! `8 k` ([`hdr_off`]). Every access is a `bounds_check` followed by a +//! `load` or `store`, as LIR requires; under the relation and +//! [`js::state_wf`] the check never fails, and the statement says so (no +//! exit but a handler exit). +//! 3. **Locals `1 ..` are scratch.** A lowering keeps intermediate values in +//! locals `1 .. nlocals()`, all `i64`, so the relation says they have that +//! type and nothing else about them. +//! 4. **`pc` is a position in the code, not a value.** Code of one function is +//! one LIR body whose control flow is the bytecode's; the lowering of the +//! op at `pc` runs when control reaches that op. Falling off the end of a +//! lowering is "continue with the op at `pc + 1`", and a branch to label +//! `lbl` is "continue at the jump's target" (the lowering takes `lbl`, the +//! depth the whole-function layout gives that target; that layout is a +//! later slice). The statement therefore says what `pc` *becomes* +//! ([`outcome_refines`]), not what a LIR value is. +//! 5. **The operand stack is empty between ops.** Every bytecode value lives +//! in region 0, so LIR's operand stack is empty when a lowering starts and +//! when it falls through or branches. At an exit the stack holds scratch +//! values the handler does not read, so the relation does not mention it. +//! 6. **Only the frame is represented.** A bytecode state has more than the +//! region: the heap, the code tables, the inline-cache tables, the thrown +//! value and the termination flag. LIR has no such thing in this slice, and +//! none of the lowered ops changes any of them, so a lowering's +//! [`outcome_refines`] says they are *unchanged* ([`same_unrepresented`]), +//! and an exit hands the handler the state the relation names, with those +//! fields as they were. How the heap and the caches are laid out in bytes +//! is the heap crate's (phase-7.md 7.5) and not yet fixed, so no op that +//! reads or writes them is lowered (`GetProp`, `SetProp` and the rest). +//! 7. **An exit names its handler, and carries `pc`.** `exit(c, [i32])` with +//! `c =` [`handler_index`]`(op_handler(op))` and the one operand `pc`. The +//! host reads the op at `pc` of the current function, as the interpreter +//! does, and runs `handler(h, s, op)` on the frame region in memory and the +//! rest of the state it already has. [`handler_of_index`] is the host's +//! decoding. +//! 8. **A lowering may exit before the fast path fails.** `step` calls the +//! handler when `fast_path` fails, and jit.md reads that as "inline the +//! fast path, exit otherwise". That is exact only when the inline test is +//! exactly `fast_path`. Small-integer `Add` cannot be: its `fast_path` is +//! "two Numbers", whose result for two doubles is `f64_add_finite`, which +//! LIR cannot compute (modelling choice 6 of both specs: the rounding is +//! uninterpreted, and two different uninterpreted functions are unrelated). +//! So the lowering tests the cases it can do (two int32 operands, the sum +//! in range) and exits for the rest, **including cases where `fast_path` +//! holds**. That is a correct implementation of `step` only if the handler +//! computes `fast_step` there, which is the handler's contract (7.8: a +//! handler is the op's whole semantics, the fast path only an inlined part +//! of it). **That is [`handler_complete`], an explicit hypothesis of +//! [`crate::front::contract::lowers`], not an axiom**: it is `step`'s own +//! shape, `handler(h, s, op) == fast_step(s, op)` wherever `fast_path` +//! holds, and 7.8 has to prove it of each handler. Where a lowering's exits +//! are exactly `!fast_path` it is not used. + +/// The bytecode machine's semantics, re-exported so that this crate's +/// modules read one namespace. +pub use clazel_js_vm::spec as js; +/// LIR's semantics, re-exported likewise. +pub use clazel_lir::spec as lir; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// ============================================================================ +// Section: the frame in LIR +// ============================================================================ + +/// The LIR region that holds the value stack (modelling choice 1). +pub open spec fn stack_region() -> lir::RegionId { + 0 +} + +/// The number of LIR locals a lowering uses: local 0 is the frame base, the +/// others are scratch (modelling choices 2 and 3). +pub open spec fn nlocals() -> nat { + 4 +} + +/// The byte offset from the frame base of header slot `k` +/// ([`js::hdr_callee`], [`js::hdr_this`], ...). +pub open spec fn hdr_off(k: nat) -> nat { + 8 * k +} + +/// The byte offset from the frame base of register `r` +/// ([`js::reg_slot`]: the header, then the arguments and registers). +pub open spec fn reg_off(r: nat) -> nat { + 8 * (js::hdr_slots() + r) +} + +/// The LIR address of slot `j` of the value-stack region. +pub open spec fn slot_addr(lay: lir::Layout, j: int) -> nat + recommends + lay.len() > 0, +{ + (lay[0].base + 8 * j) as nat +} + +// ============================================================================ +// Section: the state relation +// ============================================================================ + +/// The layout has the value stack as region 0, exactly as large as the +/// bytecode machine's region, and every region lies below `2^64`. +pub open spec fn layout_ok(lay: lir::Layout, s: js::State) -> bool { + &&& lay.len() >= 1 + &&& lir::layout_wf(lay) + &&& lay[0].bound == 8 * s.region.len() +} + +/// LIR's memory holds the bytecode machine's region: slot `j` is the 8 bytes +/// at its address, little-endian (modelling choice 1). +pub open spec fn mem_rel(lay: lir::Layout, region: Seq, m: lir::Mem) -> bool + recommends + lay.len() > 0, +{ + forall|j: int| + #![trigger region[j]] + 0 <= j < region.len() ==> lir::read_le(m, slot_addr(lay, j), 8) == region[j] as nat +} + +/// The LIR locals: local 0 is the frame base in bytes, the rest are `i64` +/// scratch (modelling choices 2 and 3). +pub open spec fn locals_rel(s: js::State, l: Seq) -> bool { + &&& l.len() == nlocals() + &&& l[0] == lir::Val::I64((8 * s.fp) as u64) + &&& forall|j: int| 1 <= j < l.len() ==> #[trigger] l[j] is I64 +} + +/// **The state relation**: the bytecode machine state `s` and the LIR state +/// `st` agree on the frame. The operand stack is not part of it (modelling +/// choice 5), and nothing of `s` but its region and `fp` is represented +/// (modelling choice 6). +pub open spec fn rel(lay: lir::Layout, s: js::State, st: lir::St) -> bool { + &&& layout_ok(lay, s) + &&& locals_rel(s, st.locals) + &&& mem_rel(lay, s.region, st.mem) +} + +/// The parts of the bytecode state that LIR does not represent are the same +/// in `s` and `s2` (modelling choice 6). +pub open spec fn same_unrepresented(s: js::State, s2: js::State) -> bool { + &&& s2.heap == s.heap + &&& s2.codes == s.codes + &&& s2.ics == s.ics + &&& s2.exception == s.exception + &&& s2.terminate == s.terminate +} + +// ============================================================================ +// Section: exits +// ============================================================================ + +/// The exit code of a handler (modelling choice 7): its position in +/// [`js::HandlerId`]'s declaration. [`handler_of_index`] inverts it. +pub open spec fn handler_index(h: js::HandlerId) -> nat { + match h { + js::HandlerId::Add => 0, + js::HandlerId::Sub => 1, + js::HandlerId::Mul => 2, + js::HandlerId::Div => 3, + js::HandlerId::Mod => 4, + js::HandlerId::Exp => 5, + js::HandlerId::Neg => 6, + js::HandlerId::Inc => 7, + js::HandlerId::Dec => 8, + js::HandlerId::BitAnd => 9, + js::HandlerId::BitOr => 10, + js::HandlerId::BitXor => 11, + js::HandlerId::Shl => 12, + js::HandlerId::Shr => 13, + js::HandlerId::UShr => 14, + js::HandlerId::BitNot => 15, + js::HandlerId::Lt => 16, + js::HandlerId::Le => 17, + js::HandlerId::Gt => 18, + js::HandlerId::Ge => 19, + js::HandlerId::StrictEq => 20, + js::HandlerId::LooseEq => 21, + js::HandlerId::Not => 22, + js::HandlerId::ToNumeric => 23, + js::HandlerId::ToBoolean => 24, + js::HandlerId::Terminate => 25, + js::HandlerId::ThrowHole => 26, + js::HandlerId::GetProp => 27, + js::HandlerId::SetProp => 28, + js::HandlerId::GetElem => 29, + js::HandlerId::SetElem => 30, + js::HandlerId::Call => 31, + js::HandlerId::Return => 32, + js::HandlerId::EnvAccess => 33, + js::HandlerId::TypeOf => 34, + js::HandlerId::InstanceOf => 35, + js::HandlerId::HasProperty => 36, + js::HandlerId::DeleteProp => 37, + js::HandlerId::GetGlobal => 38, + js::HandlerId::SetGlobal => 39, + js::HandlerId::NewObject => 40, + js::HandlerId::NewArray => 41, + js::HandlerId::NewEnv => 42, + js::HandlerId::NewClosure => 43, + js::HandlerId::New => 44, + js::HandlerId::CallSpread => 45, + js::HandlerId::GetIterator => 46, + js::HandlerId::IterNext => 47, + js::HandlerId::IterClose => 48, + js::HandlerId::Yield => 49, + js::HandlerId::Await => 50, + js::HandlerId::Throw => 51, + js::HandlerId::ThrowError => 52, + } +} + +/// The handler an exit code names: the host's decoding of +/// [`handler_index`], `None` for a code that is no handler's. +pub open spec fn handler_of_index(n: nat) -> Option { + if n == 0 { + Some(js::HandlerId::Add) + } else if n == 1 { + Some(js::HandlerId::Sub) + } else if n == 2 { + Some(js::HandlerId::Mul) + } else if n == 3 { + Some(js::HandlerId::Div) + } else if n == 4 { + Some(js::HandlerId::Mod) + } else if n == 5 { + Some(js::HandlerId::Exp) + } else if n == 6 { + Some(js::HandlerId::Neg) + } else if n == 7 { + Some(js::HandlerId::Inc) + } else if n == 8 { + Some(js::HandlerId::Dec) + } else if n == 9 { + Some(js::HandlerId::BitAnd) + } else if n == 10 { + Some(js::HandlerId::BitOr) + } else if n == 11 { + Some(js::HandlerId::BitXor) + } else if n == 12 { + Some(js::HandlerId::Shl) + } else if n == 13 { + Some(js::HandlerId::Shr) + } else if n == 14 { + Some(js::HandlerId::UShr) + } else if n == 15 { + Some(js::HandlerId::BitNot) + } else if n == 16 { + Some(js::HandlerId::Lt) + } else if n == 17 { + Some(js::HandlerId::Le) + } else if n == 18 { + Some(js::HandlerId::Gt) + } else if n == 19 { + Some(js::HandlerId::Ge) + } else if n == 20 { + Some(js::HandlerId::StrictEq) + } else if n == 21 { + Some(js::HandlerId::LooseEq) + } else if n == 22 { + Some(js::HandlerId::Not) + } else if n == 23 { + Some(js::HandlerId::ToNumeric) + } else if n == 24 { + Some(js::HandlerId::ToBoolean) + } else if n == 25 { + Some(js::HandlerId::Terminate) + } else if n == 26 { + Some(js::HandlerId::ThrowHole) + } else if n == 27 { + Some(js::HandlerId::GetProp) + } else if n == 28 { + Some(js::HandlerId::SetProp) + } else if n == 29 { + Some(js::HandlerId::GetElem) + } else if n == 30 { + Some(js::HandlerId::SetElem) + } else if n == 31 { + Some(js::HandlerId::Call) + } else if n == 32 { + Some(js::HandlerId::Return) + } else if n == 33 { + Some(js::HandlerId::EnvAccess) + } else if n == 34 { + Some(js::HandlerId::TypeOf) + } else if n == 35 { + Some(js::HandlerId::InstanceOf) + } else if n == 36 { + Some(js::HandlerId::HasProperty) + } else if n == 37 { + Some(js::HandlerId::DeleteProp) + } else if n == 38 { + Some(js::HandlerId::GetGlobal) + } else if n == 39 { + Some(js::HandlerId::SetGlobal) + } else if n == 40 { + Some(js::HandlerId::NewObject) + } else if n == 41 { + Some(js::HandlerId::NewArray) + } else if n == 42 { + Some(js::HandlerId::NewEnv) + } else if n == 43 { + Some(js::HandlerId::NewClosure) + } else if n == 44 { + Some(js::HandlerId::New) + } else if n == 45 { + Some(js::HandlerId::CallSpread) + } else if n == 46 { + Some(js::HandlerId::GetIterator) + } else if n == 47 { + Some(js::HandlerId::IterNext) + } else if n == 48 { + Some(js::HandlerId::IterClose) + } else if n == 49 { + Some(js::HandlerId::Yield) + } else if n == 50 { + Some(js::HandlerId::Await) + } else if n == 51 { + Some(js::HandlerId::Throw) + } else if n == 52 { + Some(js::HandlerId::ThrowError) + } else { + None + } +} + +/// The handler computes `fast_step` wherever `fast_path` holds +/// (modelling choice 8). A hypothesis of the refinement statement about the +/// handlers, which phase-7.md 7.8 must prove of each; **not an axiom**. +pub open spec fn handler_complete(s: js::State, op: js::Op) -> bool { + js::fast_path(s, op) ==> js::handler(js::op_handler(op), s, op) == js::fast_step(s, op) +} + +/// The target of a jump op, `None` for any other. +pub open spec fn jump_target(op: js::Op) -> Option { + match op { + js::Op::Jump { target } => Some(target), + js::Op::JumpIfUndefined { target, .. } => Some(target), + js::Op::JumpIfNotUndefined { target, .. } => Some(target), + js::Op::JumpIfNullish { target, .. } => Some(target), + js::Op::JumpIfNotNullish { target, .. } => Some(target), + js::Op::JumpIfTrue { target, .. } => Some(target), + js::Op::JumpIfFalse { target, .. } => Some(target), + js::Op::Loop { target } => Some(target), + _ => None, + } +} + +// ============================================================================ +// Section: what a lowering's outcome must be +// ============================================================================ + +/// The amount of fuel from which every lowering of this crate has its +/// outcome (LIR's `eval` is fuelled; "not enough fuel" is not a claim). A +/// lowering has no loop, so its depth is bounded by a constant. +pub open spec fn lower_fuel() -> nat { + 128 +} + +/// What LIR's outcome `o` of a lowering of `op` must be, from the bytecode +/// state `s` (modelling choices 4 to 8): +/// +/// - **`Normal`**: the op took its fast path and fell through. The bytecode +/// state is `step(s, op)`, at `pc + 1`, related to LIR's, with the +/// unrepresented parts unchanged and LIR's operand stack empty. +/// - **`Branch(lbl)`**: the op took its fast path and jumped to its target, +/// which is at the label the lowering was given. +/// - **`Exit`**: with the code that names the op's handler and `pc` as the +/// one operand; the frame as it was (`rel(s)`), so the handler's result, +/// `handler(h, s, op)`, is `step(s, op)`. +/// - **Anything else** (`Stuck`, `OutOfFuel`) is false: the lowering is total +/// and never leaves through the bounds check. +pub open spec fn outcome_refines( + lay: lir::Layout, + s: js::State, + op: js::Op, + lbl: nat, + o: lir::Outcome, +) -> bool { + match o { + lir::Outcome::Normal(st2) => { + let s2 = js::fast_step(s, op); + &&& js::fast_path(s, op) + &&& js::step(s, op) == s2 + &&& s2.pc == s.pc + 1 + &&& st2.stack == Seq::::empty() + &&& rel(lay, s2, st2) + &&& same_unrepresented(s, s2) + }, + lir::Outcome::Branch(l, st2) => { + let s2 = js::fast_step(s, op); + &&& js::fast_path(s, op) + &&& js::step(s, op) == s2 + &&& l == lbl + &&& jump_target(op) == Some(s2.pc) + &&& st2.stack == Seq::::empty() + &&& rel(lay, s2, st2) + &&& same_unrepresented(s, s2) + }, + lir::Outcome::Exit(c, vals, st2) => { + &&& c == handler_index(js::op_handler(op)) + &&& vals == seq![lir::Val::I32(s.pc as u32)] + &&& rel(lay, s, st2) + &&& js::handler(js::op_handler(op), s, op) == js::step(s, op) + }, + _ => false, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/front/trace.rs b/crates/clazel-jit/src/front/trace.rs new file mode 100644 index 000000000..a17c28b41 --- /dev/null +++ b/crates/clazel-jit/src/front/trace.rs @@ -0,0 +1,431 @@ +//! What the blocks of the small-integer lowerings do to LIR's state: the +//! tag test, the sign extension, the operation and the two guards, each as +//! [`exec`] of its instruction list from a state whose scratch locals hold +//! the operands. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + + +/// Running `a` and then `b`, when `a` ends in `s1`. +pub proof fn lemma_exec_app( + lay: lir::Layout, + a: Seq, + b: Seq, + st: lir::St, + s1: lir::St, +) + requires + super::run::exec(lay, a, st) == lir::Outcome::Normal(s1), + ensures + super::run::exec(lay, a + b, st) == super::run::exec(lay, b, s1), +{ + super::run::lemma_exec_concat(lay, a, b, st); +} + +/// Setting a scratch local to an `i64` keeps the locals related. +pub proof fn lemma_locals_set(s: js::State, l: Seq, x: nat, v: u64) + requires + locals_rel(s, l), + 1 <= x < nlocals(), + ensures + locals_rel(s, l.update(x as int, lir::Val::I64(v))), +{ + let l2 = l.update(x as int, lir::Val::I64(v)); + assert(l2.len() == l.len()); + assert(l2[0] == l[0]); + assert forall|j: int| 1 <= j < l2.len() implies #[trigger] l2[j] is I64 by { + if j != x { + assert(l2[j] == l[j]); + } + } +} + +/// `i64.shr_u` by 32 is the machine shift. +pub proof fn lemma_shr32(v: u64) + ensures + lir::ibin64(lir::IBin::ShrU, v, 32) == (v >> 32u64), +{ + vstd::arithmetic::power2::lemma2_to64(); + assert(lir::pow2(32) == 0x1_0000_0000); + assert(((v as int) / 0x1_0000_0000int) as u64 == (v >> 32u64)) by (bit_vector); +} + +/// The tag word's test: a word's high half is `tag_hi` exactly when it is an +/// int32 value. +pub proof fn lemma_tag_test(v: u64) + ensures + ((v >> 32u64) == super::arith::tag_hi()) <==> js::is_int32(v), +{ + assert((((v >> 32u64) == 0xFFF9_0000u64) <==> ((v >> 48u64) == 0xFFF9u64 && ((v >> 32u64) & 0xFFFFu64) == 0u64))) + by (bit_vector); +} + +/// The sign extension block pushes [`sext`] of the low half. +pub proof fn lemma_exec_sext_local(lay: lir::Layout, st: lir::St, x: nat, v: u64) + requires + x < st.locals.len(), + st.locals[x as int] == lir::Val::I64(v), + ensures + super::run::exec(lay, super::lower::sext_local(x), st) == lir::Outcome::Normal( + st.with_stack(st.stack.push(lir::Val::I64(super::arith::sext(super::arith::lo32(v))))), + ), +{ + super::arith::lemma_sx(super::arith::lo32(v)); + reveal_with_fuel(super::run::exec, 4); + let s1 = st.with_stack(st.stack.push(lir::Val::I64(v))); + let s2 = st.with_stack(st.stack.push(lir::Val::I32(super::arith::lo32(v)))); + assert(lir::below(s1.stack, 1) =~= st.stack); + assert(lir::below(s2.stack, 1) =~= st.stack); +} + +/// The tag test block pushes the result of the comparison. +pub proof fn lemma_exec_tag_test(lay: lir::Layout, st: lir::St, x: nat, v: u64) + requires + x < st.locals.len(), + st.locals[x as int] == lir::Val::I64(v), + ensures + super::run::exec(lay, super::lower::tag_test(x), st) == lir::Outcome::Normal( + st.with_stack(st.stack.push(lir::Val::I32(lir::bool_i32((v >> 32u64) == super::arith::tag_hi())))), + ), +{ + lemma_shr32(v); + reveal_with_fuel(super::run::exec, 6); + let s1 = st.with_stack(st.stack.push(lir::Val::I64(v))); + let s2 = s1.with_stack(s1.stack.push(lir::Val::I64(32))); + let s3 = st.with_stack(st.stack.push(lir::Val::I64(v >> 32u64))); + let s4 = s3.with_stack(s3.stack.push(lir::Val::I64(super::arith::tag_hi()))); + assert(lir::below(s2.stack, 2) =~= st.stack); + assert(lir::below(s4.stack, 2) =~= st.stack); +} + + +/// The operand block loads both registers into scratch locals 1 and 2 and +/// leaves the stack empty and the relation intact. +pub proof fn lemma_operands_trace(lay: lir::Layout, s: js::State, st: lir::St, a: nat, b: nat) + requires + rel(lay, s, st), + st.stack == Seq::::empty(), + js::state_wf(s), + js::reg_ok(js::cur_code(s), a), + js::reg_ok(js::cur_code(s), b), + ensures + ({ + let st2 = lir::St { + stack: Seq::::empty(), + locals: st.locals.update(1, lir::Val::I64(js::reg(s, a))).update( + 2, + lir::Val::I64(js::reg(s, b)), + ), + mem: st.mem, + }; + &&& super::run::exec(lay, super::lower::operands_ins(a, b), st) == lir::Outcome::Normal(st2) + &&& rel(lay, s, st2) + }), +{ + super::proofs::lemma_reg_slot_ok(s, a); + super::proofs::lemma_reg_slot_ok(s, b); + let va = js::reg(s, a); + let vb = js::reg(s, b); + let la = super::lower::load_ins(reg_off(a)); + let lb = super::lower::load_ins(reg_off(b)); + let set1 = seq![lir::Instr::LocalSet(1)]; + let set2 = seq![lir::Instr::LocalSet(2)]; + // Load a. + super::frame::lemma_exec_load(lay, s, st, reg_off(a)); + let s_a = st.with_stack(st.stack.push(lir::Val::I64(va))); + // Set local 1. + reveal_with_fuel(super::run::exec, 2); + let loc1 = st.locals.update(1, lir::Val::I64(va)); + let s_b = lir::St { stack: Seq::::empty(), locals: loc1, mem: st.mem }; + assert(s_a.stack.drop_last() =~= Seq::::empty()); + assert(super::run::exec(lay, set1, s_a) == lir::Outcome::Normal(s_b)); + lemma_exec_app(lay, la, set1, st, s_a); + assert(super::run::exec(lay, la + set1, st) == lir::Outcome::Normal(s_b)); + lemma_locals_set(s, st.locals, 1, va); + assert(rel(lay, s, s_b)); + // Load b. + super::frame::lemma_exec_load(lay, s, s_b, reg_off(b)); + let s_c = s_b.with_stack(s_b.stack.push(lir::Val::I64(vb))); + lemma_exec_app(lay, la + set1, lb, st, s_b); + assert(super::run::exec(lay, (la + set1) + lb, st) == lir::Outcome::Normal(s_c)); + // Set local 2. + let loc2 = loc1.update(2, lir::Val::I64(vb)); + let st2 = lir::St { stack: Seq::::empty(), locals: loc2, mem: st.mem }; + assert(s_c.stack.drop_last() =~= Seq::::empty()); + assert(super::run::exec(lay, set2, s_c) == lir::Outcome::Normal(st2)); + lemma_exec_app(lay, (la + set1) + lb, set2, st, s_c); + assert(super::lower::operands_ins(a, b) =~= ((la + set1) + lb) + set2); + lemma_locals_set(s, loc1, 2, vb); + assert(rel(lay, s, st2)); +} + +/// The first guard pushes whether both operands are int32 words. +pub proof fn lemma_guard1_trace(lay: lir::Layout, st: lir::St, va: u64, vb: u64) + requires + st.stack == Seq::::empty(), + st.locals.len() == nlocals(), + st.locals[1] == lir::Val::I64(va), + st.locals[2] == lir::Val::I64(vb), + ensures + ({ + let g1 = lir::ibin32( + lir::IBin::And, + lir::bool_i32((va >> 32u64) == super::arith::tag_hi()), + lir::bool_i32((vb >> 32u64) == super::arith::tag_hi()), + ); + &&& super::run::exec(lay, super::lower::guard1_ins(), st) == lir::Outcome::Normal( + st.with_stack(seq![lir::Val::I32(g1)]), + ) + &&& (g1 != 0) <==> (js::is_int32(va) && js::is_int32(vb)) + }), +{ + let e1 = lir::bool_i32((va >> 32u64) == super::arith::tag_hi()); + let e2 = lir::bool_i32((vb >> 32u64) == super::arith::tag_hi()); + let g1 = lir::ibin32(lir::IBin::And, e1, e2); + lemma_exec_tag_test(lay, st, 1, va); + let s1 = st.with_stack(st.stack.push(lir::Val::I32(e1))); + lemma_exec_app(lay, super::lower::tag_test(1), super::lower::tag_test(2), st, s1); + lemma_exec_tag_test(lay, s1, 2, vb); + let s2 = s1.with_stack(s1.stack.push(lir::Val::I32(e2))); + lemma_exec_app( + lay, + super::lower::tag_test(1) + super::lower::tag_test(2), + seq![lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And))], + st, + s2, + ); + assert(super::lower::guard1_ins() =~= (super::lower::tag_test(1) + super::lower::tag_test(2)) + seq![ + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + ]); + reveal_with_fuel(super::run::exec, 2); + assert(s2.stack =~= seq![lir::Val::I32(e1), lir::Val::I32(e2)]); + assert(lir::below(s2.stack, 2) =~= Seq::::empty()); + assert(Seq::::empty().push(lir::Val::I32(g1)) =~= seq![lir::Val::I32(g1)]); + lemma_tag_test(va); + lemma_tag_test(vb); + assert(e1 == 0 || e1 == 1); + assert(e2 == 0 || e2 == 1); + assert(((e1 & e2) != 0) <==> (e1 == 1 && e2 == 1)) by (bit_vector) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ; +} + +/// The operation block computes the 64-bit result into local 3. +pub proof fn lemma_compute_trace(lay: lir::Layout, st: lir::St, k: nat, va: u64, vb: u64) + requires + st.stack == Seq::::empty(), + st.locals.len() == nlocals(), + st.locals[1] == lir::Val::I64(va), + st.locals[2] == lir::Val::I64(vb), + ensures + super::run::exec(lay, super::lower::compute_ins(k), st) == lir::Outcome::Normal( + lir::St { + stack: Seq::::empty(), + locals: st.locals.update(3, lir::Val::I64(super::arith::s64(k, va, vb))), + mem: st.mem, + }, + ), +{ + let xa = super::arith::sext(super::arith::lo32(va)); + let xb = super::arith::sext(super::arith::lo32(vb)); + lemma_exec_sext_local(lay, st, 1, va); + let s1 = st.with_stack(st.stack.push(lir::Val::I64(xa))); + lemma_exec_app(lay, super::lower::sext_local(1), super::lower::sext_local(2), st, s1); + lemma_exec_sext_local(lay, s1, 2, vb); + let s2 = s1.with_stack(s1.stack.push(lir::Val::I64(xb))); + let tail = seq![lir::Instr::Bin(lir::BinOp::I64(super::arith::ibin_of(k))), lir::Instr::LocalSet(3)]; + lemma_exec_app(lay, super::lower::sext_local(1) + super::lower::sext_local(2), tail, st, s2); + assert(super::lower::compute_ins(k) =~= (super::lower::sext_local(1) + super::lower::sext_local(2)) + tail); + reveal_with_fuel(super::run::exec, 3); + assert(s2.stack =~= seq![lir::Val::I64(xa), lir::Val::I64(xb)]); + assert(lir::below(s2.stack, 2) =~= Seq::::empty()); + assert(Seq::::empty().push(lir::Val::I64(super::arith::s64(k, va, vb))).drop_last() + =~= Seq::::empty()); +}/// The fits test pushes whether local 3 is the sign extension of its low half. +pub proof fn lemma_fits_trace(lay: lir::Layout, st: lir::St, s: u64) + requires + st.locals.len() == nlocals(), + st.locals[3] == lir::Val::I64(s), + ensures + super::run::exec(lay, super::lower::fits_ins(), st) == lir::Outcome::Normal( + st.with_stack(st.stack.push(lir::Val::I32(super::arith::fits_flag(s)))), + ), +{ + super::arith::lemma_sx(super::arith::lo32(s)); + reveal_with_fuel(super::run::exec, 6); + let a1 = st.with_stack(st.stack.push(lir::Val::I64(s))); + let a2 = st.with_stack(st.stack.push(lir::Val::I32(super::arith::lo32(s)))); + let a3 = st.with_stack(st.stack.push(lir::Val::I64(super::arith::sext(super::arith::lo32(s))))); + let a4 = st.with_stack(st.stack.push(lir::Val::I64(super::arith::sext(super::arith::lo32(s)))).push(lir::Val::I64(s))); + assert(lir::below(a1.stack, 1) =~= st.stack); + assert(lir::below(a2.stack, 1) =~= st.stack); + assert(lir::below(a4.stack, 2) =~= st.stack); +} + +/// The first half of a product's extra test: the stack `[f]` becomes +/// `[f, s == 0, xa, xb]`. +pub proof fn lemma_mul_tail_a(lay: lir::Layout, st: lir::St, va: u64, vb: u64, s: u64, f: u32) + requires + st.stack == seq![lir::Val::I32(f)], + st.locals.len() == nlocals(), + st.locals[1] == lir::Val::I64(va), + st.locals[2] == lir::Val::I64(vb), + st.locals[3] == lir::Val::I64(s), + ensures + ({ + let sx = seq![lir::Instr::LocalGet(3), lir::Instr::Un(lir::UnOp::I64Eqz)] + + super::lower::sext_local(1) + super::lower::sext_local(2); + super::run::exec(lay, sx, st) == lir::Outcome::Normal( + st.with_stack( + seq![ + lir::Val::I32(f), + lir::Val::I32(lir::bool_i32(s == 0)), + lir::Val::I64(super::arith::sext(super::arith::lo32(va))), + lir::Val::I64(super::arith::sext(super::arith::lo32(vb))), + ], + ), + ) + }), +{ + let t1 = seq![lir::Instr::LocalGet(3), lir::Instr::Un(lir::UnOp::I64Eqz)]; + reveal_with_fuel(super::run::exec, 3); + let z0 = lir::bool_i32(s == 0); + let b1 = st.with_stack(seq![lir::Val::I32(f), lir::Val::I64(s)]); + let b2 = st.with_stack(seq![lir::Val::I32(f), lir::Val::I32(z0)]); + assert(st.stack.push(lir::Val::I64(s)) =~= b1.stack); + assert(lir::below(b1.stack, 1) =~= seq![lir::Val::I32(f)]); + assert(seq![lir::Val::I32(f)].push(lir::Val::I32(z0)) =~= b2.stack); + assert(super::run::exec(lay, t1, st) == lir::Outcome::Normal(b2)); + lemma_exec_app(lay, t1, super::lower::sext_local(1), st, b2); + lemma_exec_sext_local(lay, b2, 1, va); + let b3 = b2.with_stack(b2.stack.push(lir::Val::I64(super::arith::sext(super::arith::lo32(va))))); + lemma_exec_app(lay, t1 + super::lower::sext_local(1), super::lower::sext_local(2), st, b3); + lemma_exec_sext_local(lay, b3, 2, vb); + let b4 = b3.with_stack(b3.stack.push(lir::Val::I64(super::arith::sext(super::arith::lo32(vb))))); + assert(b4.stack =~= seq![ + lir::Val::I32(f), + lir::Val::I32(z0), + lir::Val::I64(super::arith::sext(super::arith::lo32(va))), + lir::Val::I64(super::arith::sext(super::arith::lo32(vb))), + ]); +} + +/// The second half: `[f, z0, xa, xb]` becomes the guard's value. +pub proof fn lemma_mul_tail_b(lay: lir::Layout, st: lir::St, f: u32, z0: u32, xa: u64, xb: u64) + requires + st.stack == seq![ + lir::Val::I32(f), + lir::Val::I32(z0), + lir::Val::I64(xa), + lir::Val::I64(xb), + ], + ensures + ({ + let tail = seq![ + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), + super::lower::i64c(0), + super::lower::cmp64(lir::ICmp::LtS), + ] + seq![ + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + ]; + let lt = lir::bool_i32( + lir::signed_of(64, (xa | xb) as int) < lir::signed_of(64, 0), + ); + let zn = lir::ibin32(lir::IBin::And, z0, lt); + super::run::exec(lay, tail, st) == lir::Outcome::Normal( + st.with_stack( + seq![ + lir::Val::I32( + lir::ibin32(lir::IBin::And, f, lir::bool_i32(zn == 0)), + ), + ], + ), + ) + }), +{ + reveal_with_fuel(super::run::exec, 7); + let xo = xa | xb; + let lt = lir::bool_i32(lir::signed_of(64, xo as int) < lir::signed_of(64, 0)); + let zn = lir::ibin32(lir::IBin::And, z0, lt); + let zz = lir::bool_i32(zn == 0); + assert(lir::below(st.stack, 2) =~= seq![lir::Val::I32(f), lir::Val::I32(z0)]); + let c1 = seq![lir::Val::I32(f), lir::Val::I32(z0), lir::Val::I64(xo)]; + assert(seq![lir::Val::I32(f), lir::Val::I32(z0)].push(lir::Val::I64(xo)) =~= c1); + let c2 = seq![lir::Val::I32(f), lir::Val::I32(z0), lir::Val::I64(xo), lir::Val::I64(0)]; + assert(c1.push(lir::Val::I64(0)) =~= c2); + assert(lir::below(c2, 2) =~= seq![lir::Val::I32(f), lir::Val::I32(z0)]); + let c3 = seq![lir::Val::I32(f), lir::Val::I32(z0), lir::Val::I32(lt)]; + assert(seq![lir::Val::I32(f), lir::Val::I32(z0)].push(lir::Val::I32(lt)) =~= c3); + assert(lir::below(c3, 2) =~= seq![lir::Val::I32(f)]); + let c4 = seq![lir::Val::I32(f), lir::Val::I32(zn)]; + assert(seq![lir::Val::I32(f)].push(lir::Val::I32(zn)) =~= c4); + assert(lir::below(c4, 1) =~= seq![lir::Val::I32(f)]); + let c5 = seq![lir::Val::I32(f), lir::Val::I32(zz)]; + assert(seq![lir::Val::I32(f)].push(lir::Val::I32(zz)) =~= c5); + assert(lir::below(c5, 2) =~= Seq::::empty()); + assert(Seq::::empty().push(lir::Val::I32(lir::ibin32(lir::IBin::And, f, zz))) + =~= seq![lir::Val::I32(lir::ibin32(lir::IBin::And, f, zz))]); +} + +/// The second guard pushes whether the result is an int32 the semantics +/// encodes as one. +pub proof fn lemma_guard2_trace(lay: lir::Layout, st: lir::St, k: nat, va: u64, vb: u64) + requires + k <= 2, + st.stack == Seq::::empty(), + st.locals.len() == nlocals(), + st.locals[1] == lir::Val::I64(va), + st.locals[2] == lir::Val::I64(vb), + st.locals[3] == lir::Val::I64(super::arith::s64(k, va, vb)), + ensures + super::run::exec(lay, super::lower::guard2_ins(k), st) == lir::Outcome::Normal( + st.with_stack(seq![lir::Val::I32(super::arith::guard2(k, va, vb))]), + ), +{ + let s = super::arith::s64(k, va, vb); + lemma_fits_trace(lay, st, s); + let f = super::arith::fits_flag(s); + let sf = st.with_stack(st.stack.push(lir::Val::I32(f))); + assert(st.stack.push(lir::Val::I32(f)) =~= seq![lir::Val::I32(f)]); + if k != 2 { + } else { + lemma_exec_app(lay, super::lower::fits_ins(), super::lower::mul_tail_ins(), st, sf); + let ta = seq![lir::Instr::LocalGet(3), lir::Instr::Un(lir::UnOp::I64Eqz)] + + super::lower::sext_local(1) + super::lower::sext_local(2); + let tb = seq![ + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), + super::lower::i64c(0), + super::lower::cmp64(lir::ICmp::LtS), + ] + seq![ + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + ]; + assert(super::lower::mul_tail_ins() =~= ta + tb); + lemma_mul_tail_a(lay, sf, va, vb, s, f); + let z0 = lir::bool_i32(s == 0); + let xa = super::arith::sext(super::arith::lo32(va)); + let xb = super::arith::sext(super::arith::lo32(vb)); + let sg = sf.with_stack( + seq![lir::Val::I32(f), lir::Val::I32(z0), lir::Val::I64(xa), lir::Val::I64(xb)], + ); + lemma_exec_app(lay, ta, tb, sf, sg); + lemma_mul_tail_b(lay, sg, f, z0, xa, xb); + assert(super::arith::zneg_flag(va, vb, s) == lir::ibin32( + lir::IBin::And, + z0, + lir::bool_i32(lir::signed_of(64, (xa | xb) as int) < lir::signed_of(64, 0)), + )); + } +} + +} // verus! diff --git a/crates/clazel-jit/src/front/truth.rs b/crates/clazel-jit/src/front/truth.rs new file mode 100644 index 000000000..0d35b9b8d --- /dev/null +++ b/crates/clazel-jit/src/front/truth.rs @@ -0,0 +1,535 @@ +//! Truthiness: `Not`, `JumpIfTrue`, `JumpIfFalse`, on booleans and int32s. +//! +//! The guard is "the operand is an int32, or a boolean" (`(v | 1) == true` +//! holds for `false` and `true` and no other value); every other operand +//! (`undefined`, `null`, doubles, heap references, ...) exits to the handler, +//! which for `undefined`, `null` and doubles includes cases where `fast_path` +//! holds and so relies on [`handler_complete`]. For an int32 or a boolean the +//! truth value is `isint & (payload != 0) | (v == true)`: an int32 word is +//! never `true`'s word and a boolean word is never an int32's. +//! +//! `Not` stores the encoded boolean of the negation; `JumpIfTrue` and +//! `JumpIfFalse` are a `br_if` on the truth value (or its negation). + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The guard's instructions after the tag test: is the word `false` or +/// `true`, and `or` that into the tag test's flag. +pub open spec fn tr_guard_tail() -> Seq { + seq![ + lir::Instr::LocalGet(1), + super::lower::i64c(1), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), + super::lower::i64c(js::TRUE), + super::lower::cmp64(lir::ICmp::Eq), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::Or)), + ] +} + +/// The guard: an int32 or a boolean. +pub open spec fn tr_guard() -> Seq { + super::lower::tag_test(1) + tr_guard_tail() +} + +/// The truth flag's instructions after the tag test. +pub open spec fn tflag_tail() -> Seq { + seq![ + lir::Instr::LocalGet(1), + lir::Instr::Un(lir::UnOp::I32WrapI64), + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + lir::Instr::LocalGet(1), + super::lower::i64c(js::TRUE), + super::lower::cmp64(lir::ICmp::Eq), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::Or)), + ] +} + +/// The truth flag of the word in local 1 (right for an int32 or a boolean). +pub open spec fn tflag() -> Seq { + super::lower::tag_test(1) + tflag_tail() +} + +/// The `Not` value block: the encoded negation of the truth flag. +pub open spec fn not_val() -> Seq { + tflag() + seq![ + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Un(lir::UnOp::I64ExtendI32U), + super::lower::i64c(js::FALSE), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Add)), + ] +} + +/// The `Not` lowering. +pub open spec fn lower_not(dst: nat, src: nat, op: js::Op, pc: nat) -> lir::Instr { + super::blocks::lower_un(tr_guard(), not_val(), dst, src, op, pc) +} + +/// What follows the guard in a conditional jump: the truth flag (negated for +/// `JumpIfFalse`), then a `br_if`. +pub open spec fn jump_rest(jf: bool, lbl: nat) -> Seq { + tflag() + (if jf { + seq![lir::Instr::Un(lir::UnOp::I32Eqz), lir::Instr::BrIf(lbl)] + } else { + seq![lir::Instr::BrIf(lbl)] + }) +} + +/// The conditional jump lowering: `jf` is `JumpIfFalse`. +pub open spec fn lower_jump(jf: bool, cond: nat, lbl: nat, op: js::Op, pc: nat) -> lir::Instr { + super::blocks::guarded( + super::blocks::un_pre(cond, tr_guard()), + op, + pc, + super::run::chain(jump_rest(jf, lbl)), + ) +} + +/// The guard's flag for the operand word `va`. +pub open spec fn tr_g(va: u64) -> u32 { + lir::ibin32( + lir::IBin::Or, + lir::bool_i32((va >> 32u64) == super::arith::tag_hi()), + lir::bool_i32(lir::icmp(lir::ICmp::Eq, 64, (va | 1u64) as int, js::TRUE as int)), + ) +} + +/// The truth flag for the operand word `va`. +pub open spec fn tr_t(va: u64) -> u32 { + lir::ibin32( + lir::IBin::Or, + lir::ibin32( + lir::IBin::And, + lir::bool_i32((va >> 32u64) == super::arith::tag_hi()), + lir::bool_i32(lir::bool_i32(super::arith::lo32(va) == 0) == 0), + ), + lir::bool_i32(lir::icmp(lir::ICmp::Eq, 64, va as int, js::TRUE as int)), + ) +} + +/// The value block of `Not`. +pub open spec fn not_word(va: u64) -> u64 { + (lir::bool_i32(tr_t(va) == 0) as u64 + js::FALSE) as u64 +} + +/// `or` of two flags: non-zero when either is. +pub proof fn lemma_or01(e1: u32, e2: u32) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ensures + ((e1 | e2) != 0) <==> (e1 == 1 || e2 == 1), + (e1 | e2) == 0 || (e1 | e2) == 1, +{ + assert((((e1 | e2) != 0) <==> (e1 == 1 || e2 == 1)) && ((e1 | e2) == 0 || (e1 | e2) == 1)) + by (bit_vector) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ; +} + +/// The guard, truth flag and value blocks are simple and short. +pub proof fn lemma_tr_simple() + ensures + super::run::all_simple(tr_guard()), + tr_guard().len() < 40, + super::run::all_simple(tflag()), + tflag().len() == 14, + super::run::all_simple(not_val()), + not_val().len() < 40, + forall|jf: bool, lbl: nat| + #![trigger jump_rest(jf, lbl)] + super::run::all_simple(jump_rest(jf, lbl)) && jump_rest(jf, lbl).len() <= 16, +{ + super::intops::lemma_literals_simple(0); + let g = tr_guard_tail(); + assert(super::run::all_simple(g)) by { + assert forall|i: int| 0 <= i < g.len() implies super::run::is_simple(#[trigger] g[i]) by { + assert(0 <= i < 6); + } + } + super::intops::lemma_simple_concat(super::lower::tag_test(1), g); + let t = tflag_tail(); + assert(super::run::all_simple(t)) by { + assert forall|i: int| 0 <= i < t.len() implies super::run::is_simple(#[trigger] t[i]) by { + assert(0 <= i < 9); + } + } + super::intops::lemma_simple_concat(super::lower::tag_test(1), t); + let nv = seq![ + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Un(lir::UnOp::I64ExtendI32U), + super::lower::i64c(js::FALSE), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Add)), + ]; + assert(super::run::all_simple(nv)) by { + assert forall|i: int| 0 <= i < nv.len() implies super::run::is_simple(#[trigger] nv[i]) by { + assert(0 <= i < 4); + } + } + super::intops::lemma_simple_concat(tflag(), nv); + assert forall|jf: bool, lbl: nat| #![trigger jump_rest(jf, lbl)] + super::run::all_simple(jump_rest(jf, lbl)) && jump_rest(jf, lbl).len() <= 16 by { + let tl = if jf { + seq![lir::Instr::Un(lir::UnOp::I32Eqz), lir::Instr::BrIf(lbl)] + } else { + seq![lir::Instr::BrIf(lbl)] + }; + assert(super::run::all_simple(tl)) by { + assert forall|i: int| 0 <= i < tl.len() implies super::run::is_simple(#[trigger] tl[i]) by { + assert(0 <= i < 2); + } + } + super::intops::lemma_simple_concat(tflag(), tl); + } +} + +/// The guard block's trace. +pub proof fn lemma_tr_guard_trace(lay: lir::Layout, va: u64) + ensures + super::blocks::flag_computes(lay, tr_guard(), va, tr_g(va)), +{ + let isint = lir::bool_i32((va >> 32u64) == super::arith::tag_hi()); + assert forall|st1: lir::St| + #![trigger super::run::exec(lay, tr_guard(), st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) implies super::run::exec( + lay, + tr_guard(), + st1, + ) == lir::Outcome::Normal(st1.with_stack(st1.stack.push(lir::Val::I32(tr_g(va))))) by { + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + super::trace::lemma_exec_tag_test(lay, st1, 1, va); + let s1 = st1.with_stack(st1.stack.push(lir::Val::I32(isint))); + super::trace::lemma_exec_app(lay, super::lower::tag_test(1), tr_guard_tail(), st1, s1); + reveal_with_fuel(super::run::exec, 7); + assert(super::run::exec(lay, tr_guard_tail(), s1) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I32(tr_g(va)))), + )); + } +} + +/// The truth flag block's trace. +pub proof fn lemma_tflag_trace(lay: lir::Layout, va: u64) + ensures + super::blocks::flag_computes(lay, tflag(), va, tr_t(va)), +{ + let isint = lir::bool_i32((va >> 32u64) == super::arith::tag_hi()); + assert forall|st1: lir::St| + #![trigger super::run::exec(lay, tflag(), st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) implies super::run::exec( + lay, + tflag(), + st1, + ) == lir::Outcome::Normal(st1.with_stack(st1.stack.push(lir::Val::I32(tr_t(va))))) by { + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + super::trace::lemma_exec_tag_test(lay, st1, 1, va); + let s1 = st1.with_stack(st1.stack.push(lir::Val::I32(isint))); + super::trace::lemma_exec_app(lay, super::lower::tag_test(1), tflag_tail(), st1, s1); + reveal_with_fuel(super::run::exec, 10); + assert(super::run::exec(lay, tflag_tail(), s1) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I32(tr_t(va)))), + )); + } +} + +/// The `Not` value block's trace. +pub proof fn lemma_not_val_trace(lay: lir::Layout, va: u64) + ensures + super::blocks::val_computes1(lay, not_val(), va, not_word(va)), +{ + lemma_tflag_trace(lay, va); + let tail = seq![ + lir::Instr::Un(lir::UnOp::I32Eqz), + lir::Instr::Un(lir::UnOp::I64ExtendI32U), + super::lower::i64c(js::FALSE), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Add)), + ]; + assert forall|st1: lir::St| + #![trigger super::run::exec(lay, not_val(), st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) implies super::run::exec( + lay, + not_val(), + st1, + ) == lir::Outcome::Normal(st1.with_stack(st1.stack.push(lir::Val::I64(not_word(va))))) by { + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + let t = tr_t(va); + assert(super::run::exec(lay, tflag(), st1) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I32(t))), + )); + let s1 = st1.with_stack(st1.stack.push(lir::Val::I32(t))); + super::trace::lemma_exec_app(lay, tflag(), tail, st1, s1); + super::frame::lemma_add_small(lir::bool_i32(t == 0) as u64, js::FALSE); + reveal_with_fuel(super::run::exec, 5); + assert(super::run::exec(lay, tail, s1) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I64(not_word(va)))), + )); + } +} + +/// The guard's flag is non-zero for an int32 or a boolean only. +pub proof fn lemma_tr_g(va: u64) + ensures + (tr_g(va) != 0) <==> (js::is_int32(va) || va == js::FALSE || va == js::TRUE), +{ + super::trace::lemma_tag_test(va); + let isint = lir::bool_i32((va >> 32u64) == super::arith::tag_hi()); + let isbool = lir::bool_i32(lir::icmp(lir::ICmp::Eq, 64, (va | 1u64) as int, js::TRUE as int)); + assert(isint == 0 || isint == 1); + assert(isbool == 0 || isbool == 1); + lemma_or01(isint, isbool); + assert(((va | 1u64) == js::TRUE) <==> (va == js::FALSE || va == js::TRUE)) by (bit_vector); +} + +/// For an operand the guard lets through, `to_boolean_fast` is `Some`, and +/// the truth flag says which. +pub proof fn lemma_tr_t(va: u64) + requires + tr_g(va) != 0, + ensures + js::to_boolean_fast(va) is Some, + (tr_t(va) != 0) <==> (js::to_boolean_fast(va) == Some(true)), +{ + lemma_tr_g(va); + clazel_js_vm::lemmas::lemma_specials(); + let isint = lir::bool_i32((va >> 32u64) == super::arith::tag_hi()); + super::trace::lemma_tag_test(va); + let nz = lir::bool_i32(lir::bool_i32(super::arith::lo32(va) == 0) == 0); + let eqt = lir::bool_i32(lir::icmp(lir::ICmp::Eq, 64, va as int, js::TRUE as int)); + assert(isint == 0 || isint == 1); + assert(nz == 0 || nz == 1); + assert(eqt == 0 || eqt == 1); + lemma_and01_local(isint, nz); + let a = lir::ibin32(lir::IBin::And, isint, nz); + lemma_or01(a, eqt); + if js::is_int32(va) { + super::arith::lemma_payload(va); + let ia = js::int32_of(va); + assert(isint == 1); + assert((super::arith::lo32(va) == 0) <==> (ia == 0)) by (bit_vector) + requires + ia == (super::arith::lo32(va) as i32), + ; + assert(va != js::TRUE) by { + assert((va >> 48u64) == 0xFFF9u64 ==> va != js::TRUE) by (bit_vector); + } + assert(eqt == 0); + assert(js::to_boolean_fast(va) == Some(ia != 0)); + } else { + assert(va == js::FALSE || va == js::TRUE); + assert(isint == 0) by { + assert((va == js::FALSE || va == js::TRUE) ==> (va >> 32u64) != super::arith::tag_hi()) by (bit_vector); + } + assert(a == 0); + if va == js::TRUE { + assert(eqt == 1); + } else { + assert(eqt == 0); + } + } +} + +/// `and` of two flags, non-zero exactly when both are. +pub proof fn lemma_and01_local(e1: u32, e2: u32) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ensures + ((e1 & e2) != 0) <==> (e1 == 1 && e2 == 1), + (e1 & e2) == 0 || (e1 & e2) == 1, +{ + assert((((e1 & e2) != 0) <==> (e1 == 1 && e2 == 1)) && ((e1 & e2) == 0 || (e1 & e2) == 1)) + by (bit_vector) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ; +} + +/// `Not` on an operand the guard lets through. +pub proof fn lemma_not_sem(s: js::State, dst: nat, src: nat) + requires + tr_g(js::reg(s, src)) != 0, + ensures + js::fast_path(s, js::Op::Not { dst, src }), + js::fast_step(s, js::Op::Not { dst, src }) == js::next( + js::set_reg(s, dst, not_word(js::reg(s, src))), + ), +{ + let va = js::reg(s, src); + lemma_tr_t(va); + let t = tr_t(va); + assert(js::FALSE + 1 == js::TRUE); + assert(not_word(va) == js::enc_bool(js::to_boolean_fast(va) != Some(true))); +} + +/// A `Not` op: the lowering refines `step`. +pub proof fn lemma_not_op( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + dst: nat, + src: nat, +) + requires + super::proofs::pre(c, op, pc, lay, s, st, fuel), + op == (js::Op::Not { dst, src }), + js::reg_ok(js::cur_code(s), dst), + js::reg_ok(js::cur_code(s), src), + super::lower::lower(c, op, pc, lbl) == lower_not(dst, src, op, pc), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_tr_simple(); + let va = js::reg(s, src); + lemma_tr_guard_trace(lay, va); + if tr_g(va) != 0 { + lemma_not_sem(s, dst, src); + lemma_not_val_trace(lay, va); + } + super::blocks::lemma_un_op( + op, + pc, + lbl, + lay, + s, + st, + fuel, + dst, + src, + tr_guard(), + not_val(), + tr_g(va), + not_word(va), + ); +} + +/// The rest of a conditional jump, after the guard: from a state with the +/// operand in local 1 and an empty stack, a branch to `lbl` when the +/// (possibly negated) truth flag is non-zero, otherwise a fall through. +pub proof fn lemma_jump_rest(lay: lir::Layout, st: lir::St, va: u64, jf: bool, lbl: nat, fuel: nat) + requires + st.stack == Seq::::empty(), + st.locals.len() == nlocals(), + st.locals[1] == lir::Val::I64(va), + fuel > 20, + ensures + ({ + let t = tr_t(va); + let cond = if jf { + lir::bool_i32(t == 0) + } else { + t + }; + lir::eval(lay, super::run::chain(jump_rest(jf, lbl)), st, fuel) == (if cond != 0 { + lir::Outcome::Branch(lbl, st) + } else { + lir::Outcome::Normal(st) + }) + }), +{ + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + lemma_tr_simple(); + lemma_tflag_trace(lay, va); + let t = tr_t(va); + let ins = jump_rest(jf, lbl); + super::run::lemma_chain(lay, ins, st, fuel); + let tl = if jf { + seq![lir::Instr::Un(lir::UnOp::I32Eqz), lir::Instr::BrIf(lbl)] + } else { + seq![lir::Instr::BrIf(lbl)] + }; + assert(ins == tflag() + tl); + let s1 = st.with_stack(st.stack.push(lir::Val::I32(t))); + super::trace::lemma_exec_app(lay, tflag(), tl, st, s1); + reveal_with_fuel(super::run::exec, 3); + assert(st.stack.push(lir::Val::I32(t)) =~= seq![lir::Val::I32(t)]); + assert(lir::below(seq![lir::Val::I32(t)], 1) =~= Seq::::empty()); +} + +/// A conditional jump on the truth of a register. +pub proof fn lemma_jump_op( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + jf: bool, + cond: nat, + target: nat, +) + requires + super::proofs::pre(c, op, pc, lay, s, st, fuel), + op == (if jf { + js::Op::JumpIfFalse { cond, target } + } else { + js::Op::JumpIfTrue { cond, target } + }), + js::reg_ok(js::cur_code(s), cond), + super::lower::lower(c, op, pc, lbl) == lower_jump(jf, cond, lbl, op, pc), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_tr_simple(); + let va = js::reg(s, cond); + lemma_tr_guard_trace(lay, va); + let g = tr_g(va); + super::blocks::lemma_pre_un(lay, s, st, cond, tr_guard(), g); + let pre = super::blocks::un_pre(cond, tr_guard()); + let st3 = lir::St { + stack: seq![lir::Val::I32(g)], + locals: st.locals.update(1, lir::Val::I64(va)), + mem: st.mem, + }; + let rest = super::run::chain(jump_rest(jf, lbl)); + super::blocks::lemma_guarded(op, pc, lbl, lay, s, st, st3, pre, rest, g, fuel); + if g != 0 { + lemma_tr_t(va); + let t = tr_t(va); + let st4 = st3.with_stack(Seq::::empty()); + assert(rel(lay, s, st4)); + lemma_jump_rest(lay, st4, va, jf, lbl, (fuel - 2) as nat); + let cnd = if jf { + lir::bool_i32(t == 0) + } else { + t + }; + let tb = js::to_boolean_fast(va); + assert(tb is Some); + assert(jump_target(op) == Some(target)); + let take = if jf { + tb == Some(false) + } else { + tb == Some(true) + }; + assert((cnd != 0) <==> take); + assert(js::fast_path(s, op)); + assert(js::fast_step(s, op) == (if take { + js::jump(s, target) + } else { + js::next(s) + })); + super::proofs::lemma_rel_pc(lay, s, st4, target); + super::proofs::lemma_rel_pc(lay, s, st4, s.pc + 1); + } +} + +} // verus! diff --git a/crates/clazel-jit/src/front/unops.rs b/crates/clazel-jit/src/front/unops.rs new file mode 100644 index 000000000..2bca8bde8 --- /dev/null +++ b/crates/clazel-jit/src/front/unops.rs @@ -0,0 +1,544 @@ +//! The int32 unary ops: `Inc`, `Dec`, `Neg`, `BitNot`. +//! +//! One guard each, computed from the operand word alone: the operand is an +//! int32 and (for `Inc`) not `2^31 - 1`, (for `Dec`) not `-2^31`, (for `Neg`) +//! neither `0` (`-0` is not an int32) nor `-2^31`. Those are exactly the cases +//! where the semantics' result is an int32; the rest exit to the handler. The +//! value is the 64-bit operation on the sign-extended payload, low half, +//! tagged. (No overflow test follows the operation: the guard has already +//! ruled it out, so the result fits.) +//! +//! `k` is the op: 0 `Inc`, 1 `Dec`, 2 `Neg`, 3 `BitNot`. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The bytecode op of `k`. +pub open spec fn un_op(k: nat, dst: nat, src: nat) -> js::Op { + if k == 0 { + js::Op::Inc { dst, src } + } else if k == 1 { + js::Op::Dec { dst, src } + } else if k == 2 { + js::Op::Neg { dst, src } + } else { + js::Op::BitNot { dst, src } + } +} + +/// `-2^31` sign-extended to 64 bits, as a word. +pub open spec fn min32_word() -> u64 { + 0xFFFF_FFFF_8000_0000 +} + +/// One comparison of the sign-extended payload of local 1 with `c` +/// (`ne`), `and`ed into the flag on the stack. +pub open spec fn ne_half(c: u64) -> Seq { + seq![ + lir::Instr::LocalGet(1), + lir::Instr::Un(lir::UnOp::I32WrapI64), + lir::Instr::Un(lir::UnOp::I64ExtendI32S), + super::lower::i64c(c), + super::lower::cmp64(lir::ICmp::Ne), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + ] +} + +/// The flag block's instructions after the tag test: compare the +/// sign-extended payload with the excluded values. +pub open spec fn un_flag_tail(k: nat) -> Seq { + if k == 0 { + ne_half(0x7FFF_FFFF) + } else if k == 1 { + ne_half(min32_word()) + } else if k == 2 { + ne_half(0) + ne_half(min32_word()) + } else { + seq![] + } +} + +/// The flag block: the tag test of local 1, then the tail. +pub open spec fn un_flag(k: nat) -> Seq { + super::lower::tag_test(1) + un_flag_tail(k) +} + +/// `1` if the sign-extended payload `xa` differs from `c`. +pub open spec fn ne_flag(xa: u64, c: u64) -> u32 { + lir::bool_i32(lir::icmp(lir::ICmp::Ne, 64, xa as int, c as int)) +} + +/// The flag's value for operand word `va`. +pub open spec fn un_g(k: nat, va: u64) -> u32 { + let isint = lir::bool_i32((va >> 32u64) == super::arith::tag_hi()); + let xa = super::arith::sext(super::arith::lo32(va)); + if k == 0 { + lir::ibin32(lir::IBin::And, isint, ne_flag(xa, 0x7FFF_FFFF)) + } else if k == 1 { + lir::ibin32(lir::IBin::And, isint, ne_flag(xa, min32_word())) + } else if k == 2 { + lir::ibin32( + lir::IBin::And, + lir::ibin32(lir::IBin::And, isint, ne_flag(xa, 0)), + ne_flag(xa, min32_word()), + ) + } else { + isint + } +} + +/// What the guard lets through, for an int32 `ia`. +pub open spec fn un_cond(k: nat, ia: i32) -> bool { + if k == 0 { + ia != 2147483647 + } else if k == 1 { + ia != -2147483648 + } else if k == 2 { + ia != 0 && ia != -2147483648 + } else { + true + } +} + +/// The value block: the tagged result of the operation on the payload in +/// local 1. +pub open spec fn un_val(k: nat) -> Seq { + let sx0 = lir::Instr::LocalGet(1); + let sx1 = lir::Instr::Un(lir::UnOp::I32WrapI64); + let sx2 = lir::Instr::Un(lir::UnOp::I64ExtendI32S); + let mask = super::lower::i64c(0xFFFF_FFFF); + let and64 = lir::Instr::Bin(lir::BinOp::I64(lir::IBin::And)); + let tag = super::lower::i64c(super::arith::tag_bits()); + let or64 = lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)); + if k == 0 { + seq![ + sx0, + sx1, + sx2, + super::lower::i64c(1), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Add)), + mask, + and64, + tag, + or64, + ] + } else if k == 1 { + seq![ + sx0, + sx1, + sx2, + super::lower::i64c(1), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Sub)), + mask, + and64, + tag, + or64, + ] + } else if k == 2 { + seq![ + super::lower::i64c(0), + sx0, + sx1, + sx2, + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Sub)), + mask, + and64, + tag, + or64, + ] + } else { + seq![ + sx0, + sx1, + sx2, + super::lower::i64c(0xFFFF_FFFF_FFFF_FFFF), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Xor)), + mask, + and64, + tag, + or64, + ] + } +} + +/// The 64-bit result of the operation on the sign-extended payload. +pub open spec fn un_s(k: nat, xa: u64) -> u64 { + if k == 0 { + lir::ibin64(lir::IBin::Add, xa, 1) + } else if k == 1 { + lir::ibin64(lir::IBin::Sub, xa, 1) + } else if k == 2 { + lir::ibin64(lir::IBin::Sub, 0, xa) + } else { + lir::ibin64(lir::IBin::Xor, xa, 0xFFFF_FFFF_FFFF_FFFF) + } +} + +/// The word the value block pushes. +pub open spec fn un_word(k: nat, xa: u64) -> u64 { + super::arith::tagged(un_s(k, xa)) +} + +/// The int32 result of the op on an int32, as the semantics writes it. +pub open spec fn un_res(k: nat, ia: i32) -> i32 { + if k == 0 { + ((ia as int) + 1) as i32 + } else if k == 1 { + ((ia as int) - 1) as i32 + } else if k == 2 { + (-(ia as int)) as i32 + } else { + (-1 - (ia as int)) as i32 + } +} + +/// The lowering of op `k` on register `src` into `dst`. +pub open spec fn lower_unary(k: nat, dst: nat, src: nat, op: js::Op, pc: nat) -> lir::Instr { + super::blocks::lower_un(un_flag(k), un_val(k), dst, src, op, pc) +} + +/// A comparison half is simple. +pub proof fn lemma_half_simple(c: u64) + ensures + super::run::all_simple(ne_half(c)), + ne_half(c).len() == 6, +{ + let h = ne_half(c); + assert(super::run::all_simple(h)) by { + assert forall|i: int| 0 <= i < h.len() implies super::run::is_simple(#[trigger] h[i]) by { + assert(0 <= i < 6); + } + } +} + +/// The flag and value blocks are simple and short. +pub proof fn lemma_un_simple(k: nat) + ensures + super::run::all_simple(un_flag(k)), + un_flag(k).len() < 40, + super::run::all_simple(un_val(k)), + un_val(k).len() < 40, +{ + super::intops::lemma_literals_simple(0); + lemma_half_simple(0x7FFF_FFFF); + lemma_half_simple(min32_word()); + lemma_half_simple(0); + super::intops::lemma_simple_concat(ne_half(0), ne_half(min32_word())); + let t = un_flag_tail(k); + assert(super::run::all_simple(t)) by { + if k > 2 { + assert(t =~= Seq::::empty()); + } + } + super::intops::lemma_simple_concat(super::lower::tag_test(1), t); + let v = un_val(k); + assert(super::run::all_simple(v)) by { + assert forall|i: int| 0 <= i < v.len() implies super::run::is_simple(#[trigger] v[i]) by { + assert(0 <= i < 9); + } + } +} + +/// One comparison half's trace: the flag `e` on the stack becomes `e & ne`. +pub proof fn lemma_half_trace(lay: lir::Layout, st: lir::St, base: Seq, va: u64, e: u32, c: u64) + requires + st.stack == base.push(lir::Val::I32(e)), + st.locals.len() == nlocals(), + st.locals[1] == lir::Val::I64(va), + ensures + super::run::exec(lay, ne_half(c), st) == lir::Outcome::Normal( + st.with_stack( + base.push( + lir::Val::I32( + lir::ibin32( + lir::IBin::And, + e, + ne_flag(super::arith::sext(super::arith::lo32(va)), c), + ), + ), + ), + ), + ), +{ + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + super::arith::lemma_sx(super::arith::lo32(va)); + reveal_with_fuel(super::run::exec, 7); +} + +/// The flag block's trace. +pub proof fn lemma_un_flag_trace(lay: lir::Layout, k: nat, va: u64) + ensures + super::blocks::flag_computes(lay, un_flag(k), va, un_g(k, va)), +{ + let isint = lir::bool_i32((va >> 32u64) == super::arith::tag_hi()); + assert forall|st1: lir::St| + #![trigger super::run::exec(lay, un_flag(k), st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) implies super::run::exec( + lay, + un_flag(k), + st1, + ) == lir::Outcome::Normal(st1.with_stack(st1.stack.push(lir::Val::I32(un_g(k, va))))) by { + super::trace::lemma_exec_tag_test(lay, st1, 1, va); + let s1 = st1.with_stack(st1.stack.push(lir::Val::I32(isint))); + super::trace::lemma_exec_app(lay, super::lower::tag_test(1), un_flag_tail(k), st1, s1); + let xa = super::arith::sext(super::arith::lo32(va)); + if k == 0 { + lemma_half_trace(lay, s1, st1.stack, va, isint, 0x7FFF_FFFF); + } else if k == 1 { + lemma_half_trace(lay, s1, st1.stack, va, isint, min32_word()); + } else if k == 2 { + lemma_half_trace(lay, s1, st1.stack, va, isint, 0); + let e1 = lir::ibin32(lir::IBin::And, isint, ne_flag(xa, 0)); + let s2 = st1.with_stack(st1.stack.push(lir::Val::I32(e1))); + super::trace::lemma_exec_app(lay, ne_half(0), ne_half(min32_word()), s1, s2); + lemma_half_trace(lay, s2, st1.stack, va, e1, min32_word()); + } else { + assert(un_flag_tail(k) =~= Seq::::empty()); + reveal_with_fuel(super::run::exec, 1); + } + } +} + +/// `1 & 1` is the only non-zero `and` of two flags. +pub proof fn lemma_and01(e1: u32, e2: u32) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ensures + ((e1 & e2) != 0) <==> (e1 == 1 && e2 == 1), +{ + assert(((e1 & e2) != 0) <==> (e1 == 1 && e2 == 1)) by (bit_vector) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ; +} + +/// The `and` of two flags is a flag. +pub proof fn lemma_and01_range(e1: u32, e2: u32) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ensures + (e1 & e2) == 0 || (e1 & e2) == 1, +{ + assert(((e1 & e2) == 0 || (e1 & e2) == 1)) by (bit_vector) + requires + e1 == 0 || e1 == 1, + e2 == 0 || e2 == 1, + ; +} + +/// The flag is non-zero exactly for an int32 the semantics' fast result +/// covers. +pub proof fn lemma_un_g(k: nat, va: u64) + requires + k <= 3, + ensures + (un_g(k, va) != 0) <==> (js::is_int32(va) && un_cond(k, js::int32_of(va))), +{ + super::trace::lemma_tag_test(va); + let isint = lir::bool_i32((va >> 32u64) == super::arith::tag_hi()); + let xa = super::arith::sext(super::arith::lo32(va)); + assert(isint == 0 || isint == 1); + if js::is_int32(va) { + super::arith::lemma_payload(va); + let ia = js::int32_of(va); + assert(xa == (ia as i64) as u64); + assert(isint == 1); + assert(ne_flag(xa, 0x7FFF_FFFF) == 0 || ne_flag(xa, 0x7FFF_FFFF) == 1); + assert(ne_flag(xa, min32_word()) == 0 || ne_flag(xa, min32_word()) == 1); + assert(ne_flag(xa, 0) == 0 || ne_flag(xa, 0) == 1); + assert((xa != 0x7FFF_FFFFu64) <==> (ia != 2147483647)) by (bit_vector) + requires + xa == (ia as i64) as u64, + ; + assert((xa != 0xFFFF_FFFF_8000_0000u64) <==> (ia != -2147483648)) by (bit_vector) + requires + xa == (ia as i64) as u64, + ; + assert((xa != 0u64) <==> (ia != 0)) by (bit_vector) + requires + xa == (ia as i64) as u64, + ; + if k == 0 { + lemma_and01(isint, ne_flag(xa, 0x7FFF_FFFF)); + } else if k == 1 { + lemma_and01(isint, ne_flag(xa, min32_word())); + } else if k == 2 { + lemma_and01(isint, ne_flag(xa, 0)); + let e = lir::ibin32(lir::IBin::And, isint, ne_flag(xa, 0)); + lemma_and01_range(isint, ne_flag(xa, 0)); + assert(e == 0 || e == 1); + lemma_and01(e, ne_flag(xa, min32_word())); + } + } else { + assert(isint == 0); + if k == 0 { + lemma_and01(isint, ne_flag(xa, 0x7FFF_FFFF)); + } else if k == 1 { + lemma_and01(isint, ne_flag(xa, min32_word())); + } else if k == 2 { + lemma_and01(isint, ne_flag(xa, 0)); + let e = lir::ibin32(lir::IBin::And, isint, ne_flag(xa, 0)); + lemma_and01_range(isint, ne_flag(xa, 0)); + assert(e == 0 || e == 1); + lemma_and01(e, ne_flag(xa, min32_word())); + } + } +} + +/// The value block's trace, for one op (the case split is by `k`, one lemma +/// each, so that every proof stays small). The resource limit is raised: the +/// four blocks are nine instructions each and `k` is symbolic. +#[verifier::rlimit(100)] +pub proof fn lemma_un_val_trace_k(lay: lir::Layout, k: nat, st1: lir::St, xa: u64) + requires + k <= 3, + st1.locals.len() == nlocals(), + st1.locals[1] == lir::Val::I64(xa), + ensures + super::run::exec(lay, un_val(k), st1) == lir::Outcome::Normal( + st1.with_stack( + st1.stack.push(lir::Val::I64(un_word(k, super::arith::sext(super::arith::lo32(xa))))), + ), + ), +{ + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + super::arith::lemma_sx(super::arith::lo32(xa)); + reveal_with_fuel(super::run::exec, 10); +} + +/// The value block's trace. +pub proof fn lemma_un_val_trace(lay: lir::Layout, k: nat, va: u64) + ensures + super::blocks::val_computes1(lay, un_val(k), va, un_word(k, super::arith::sext(super::arith::lo32(va)))), +{ + assert forall|st1: lir::St| + #![trigger super::run::exec(lay, un_val(k), st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) implies super::run::exec( + lay, + un_val(k), + st1, + ) == lir::Outcome::Normal( + st1.with_stack( + st1.stack.push(lir::Val::I64(un_word(k, super::arith::sext(super::arith::lo32(va))))), + ), + ) by { + if k <= 3 { + lemma_un_val_trace_k(lay, k, st1, va); + } else { + assert(un_val(k) == un_val(3)); + lemma_un_val_trace_k(lay, 3, st1, va); + } + } +} + +/// The value block's word is the encoding of the int32 result. +pub proof fn lemma_un_word(k: nat, va: u64) + requires + k <= 3, + js::is_int32(va), + ensures + un_word(k, super::arith::sext(super::arith::lo32(va))) == js::enc_int32(un_res(k, js::int32_of(va))), +{ + super::arith::lemma_payload(va); + let ia = js::int32_of(va); + let xa = super::arith::sext(super::arith::lo32(va)); + assert(xa == (ia as i64) as u64); + if k == 0 { + super::arith::lemma_ibin64(0, xa, 1); + assert(super::arith::tagged(xa.wrapping_add(1)) == js::enc_int32(((ia as int) + 1) as i32)) by (bit_vector) + requires + xa == (ia as i64) as u64, + ; + } else if k == 1 { + super::arith::lemma_ibin64(1, xa, 1); + assert(super::arith::tagged(xa.wrapping_sub(1)) == js::enc_int32(((ia as int) - 1) as i32)) by (bit_vector) + requires + xa == (ia as i64) as u64, + ; + } else if k == 2 { + super::arith::lemma_ibin64(1, 0, xa); + assert(super::arith::tagged((0u64).wrapping_sub(xa)) == js::enc_int32((-(ia as int)) as i32)) by (bit_vector) + requires + xa == (ia as i64) as u64, + ; + } else { + assert(super::arith::tagged(xa ^ 0xFFFF_FFFF_FFFF_FFFFu64) == js::enc_int32((-1 - (ia as int)) as i32)) + by (bit_vector) + requires + xa == (ia as i64) as u64, + ; + } +} + +/// The semantics of op `k` on an int32 register the guard lets through. +pub proof fn lemma_un_sem(s: js::State, k: nat, dst: nat, src: nat) + requires + k <= 3, + js::is_int32(js::reg(s, src)), + un_cond(k, js::int32_of(js::reg(s, src))), + ensures + js::fast_path(s, un_op(k, dst, src)), + js::fast_step(s, un_op(k, dst, src)) == js::next( + js::set_reg(s, dst, js::enc_int32(un_res(k, js::int32_of(js::reg(s, src))))), + ), +{ + let va = js::reg(s, src); + let ia = js::int32_of(va); + clazel_js_vm::lemmas::lemma_decode_enc_int32(1); + assert(js::is_int32(js::enc_int32(1))); + assert(js::int32_of(js::enc_int32(1)) == 1); + if k == 3 { + super::numord::lemma_to_int32(ia); + assert(js::num_bits(va) == js::f64_of_int(ia as int)); + } + assert(js::is_number(va)); +} + +/// An int32 unary op: the lowering refines `step`. +pub proof fn lemma_un_op_k( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + k: nat, + dst: nat, + src: nat, +) + requires + super::proofs::pre(c, op, pc, lay, s, st, fuel), + k <= 3, + op == un_op(k, dst, src), + js::reg_ok(js::cur_code(s), dst), + js::reg_ok(js::cur_code(s), src), + super::lower::lower(c, op, pc, lbl) == lower_unary(k, dst, src, op, pc), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_un_simple(k); + let va = js::reg(s, src); + let g = un_g(k, va); + let w = js::enc_int32(un_res(k, js::int32_of(va))); + lemma_un_flag_trace(lay, k, va); + lemma_un_g(k, va); + if g != 0 { + lemma_un_sem(s, k, dst, src); + lemma_un_val_trace(lay, k, va); + lemma_un_word(k, va); + } + super::blocks::lemma_un_op(op, pc, lbl, lay, s, st, fuel, dst, src, un_flag(k), un_val(k), g, w); +} + +} // verus! diff --git a/crates/clazel-jit/src/front/ushr.rs b/crates/clazel-jit/src/front/ushr.rs new file mode 100644 index 000000000..5fc1452bd --- /dev/null +++ b/crates/clazel-jit/src/front/ushr.rs @@ -0,0 +1,241 @@ +//! `UShr` (`>>>`) on int32s. +//! +//! The semantics shifts the *unsigned* reading of the left operand right by +//! the count reduced modulo 32, and the result, a `uint32`, is an int32 only +//! when it is below `2^31` (otherwise it is a double, which LIR cannot compute +//! in this slice). So there are two guards, `and`ed into one flag: both +//! operands are int32s, and the shifted value (`(a & 0xFFFF_FFFF) >>u (b & 31)` +//! on the 64-bit words) is below `2^31`. The value is that shifted value, +//! tagged. Everything else, a result of `2^31` or more included, exits to the +//! handler (which relies on [`handler_complete`] where `fast_path` holds). + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The extra flag block: the shifted value is below `2^31`, `and`ed into the +/// flag on the stack. +pub open spec fn ushr_ex() -> Seq { + seq![ + lir::Instr::LocalGet(1), + super::lower::i64c(0xFFFF_FFFF), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::And)), + lir::Instr::LocalGet(2), + super::lower::i64c(31), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::And)), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::ShrU)), + super::lower::i64c(0x8000_0000), + super::lower::cmp64(lir::ICmp::LtU), + lir::Instr::Bin(lir::BinOp::I32(lir::IBin::And)), + ] +} + +/// The value block: the shifted value, tagged. +pub open spec fn ushr_val() -> Seq { + seq![ + lir::Instr::LocalGet(1), + super::lower::i64c(0xFFFF_FFFF), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::And)), + lir::Instr::LocalGet(2), + super::lower::i64c(31), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::And)), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::ShrU)), + super::lower::i64c(0xFFFF_FFFF), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::And)), + super::lower::i64c(super::arith::tag_bits()), + lir::Instr::Bin(lir::BinOp::I64(lir::IBin::Or)), + ] +} + +/// The shifted value, from the two operand words. +pub open spec fn ushr_s(va: u64, vb: u64) -> u64 { + lir::ibin64(lir::IBin::ShrU, va & 0xFFFF_FFFFu64, vb & 31u64) +} + +/// The extra flag: the shifted value is below `2^31`. +pub open spec fn ushr_f(va: u64, vb: u64) -> u32 { + lir::bool_i32(lir::icmp(lir::ICmp::LtU, 64, ushr_s(va, vb) as int, 0x8000_0000u64 as int)) +} + +/// The word the value block pushes. +pub open spec fn ushr_word(va: u64, vb: u64) -> u64 { + super::arith::tagged(ushr_s(va, vb)) +} + +/// The `UShr` lowering. +pub open spec fn lower_ushr(dst: nat, a: nat, b: nat, op: js::Op, pc: nat) -> lir::Instr { + super::blocks::lower_bin2(ushr_ex(), ushr_val(), dst, a, b, op, pc) +} + +/// The blocks are simple and short. +pub proof fn lemma_ushr_simple() + ensures + super::run::all_simple(ushr_ex()), + ushr_ex().len() == 10, + super::run::all_simple(ushr_val()), + ushr_val().len() == 11, +{ + let e = ushr_ex(); + assert(super::run::all_simple(e)) by { + assert forall|i: int| 0 <= i < e.len() implies super::run::is_simple(#[trigger] e[i]) by { + assert(0 <= i < 10); + } + } + let v = ushr_val(); + assert(super::run::all_simple(v)) by { + assert forall|i: int| 0 <= i < v.len() implies super::run::is_simple(#[trigger] v[i]) by { + assert(0 <= i < 11); + } + } +} + +/// The extra flag block's trace. +pub proof fn lemma_ushr_ex_trace(lay: lir::Layout, va: u64, vb: u64) + ensures + super::blocks::flag_and_computes(lay, ushr_ex(), va, vb, ushr_f(va, vb)), +{ + assert forall|st1: lir::St, base: Seq, g1: u32| + #![trigger super::run::exec(lay, ushr_ex(), st1), base.push(lir::Val::I32(g1))] + st1.stack == base.push(lir::Val::I32(g1)) && st1.locals.len() == nlocals() && st1.locals[1] + == lir::Val::I64(va) && st1.locals[2] == lir::Val::I64(vb) implies super::run::exec( + lay, + ushr_ex(), + st1, + ) == lir::Outcome::Normal( + st1.with_stack(base.push(lir::Val::I32(lir::ibin32(lir::IBin::And, g1, ushr_f(va, vb))))), + ) by { + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + reveal_with_fuel(super::run::exec, 11); + } +} + +/// The value block's trace. +pub proof fn lemma_ushr_val_trace(lay: lir::Layout, va: u64, vb: u64) + ensures + super::blocks::val_computes(lay, ushr_val(), va, vb, ushr_word(va, vb)), +{ + assert forall|st1: lir::St| + #![trigger super::run::exec(lay, ushr_val(), st1)] + st1.locals.len() == nlocals() && st1.locals[1] == lir::Val::I64(va) && st1.locals[2] + == lir::Val::I64(vb) implies super::run::exec(lay, ushr_val(), st1) == lir::Outcome::Normal( + st1.with_stack(st1.stack.push(lir::Val::I64(ushr_word(va, vb)))), + ) by { + broadcast use super::blocks::lemma_below_push1, super::blocks::lemma_below_push2, super::blocks::lemma_drop_last_push; + + reveal_with_fuel(super::run::exec, 12); + } +} + +/// The semantics' result on two int32 registers whose shifted value is below +/// `2^31`: the fast path holds and the result is the tagged shifted value. +pub proof fn lemma_ushr_sem(s: js::State, dst: nat, a: nat, b: nat) + requires + js::is_int32(js::reg(s, a)), + js::is_int32(js::reg(s, b)), + ushr_f(js::reg(s, a), js::reg(s, b)) != 0, + ensures + js::fast_path(s, js::Op::UShr { dst, a, b }), + js::fast_step(s, js::Op::UShr { dst, a, b }) == js::next( + js::set_reg(s, dst, ushr_word(js::reg(s, a), js::reg(s, b))), + ), +{ + let va = js::reg(s, a); + let vb = js::reg(s, b); + let ia = js::int32_of(va); + let ib = js::int32_of(vb); + super::arith::lemma_payload(va); + super::arith::lemma_payload(vb); + super::numord::lemma_to_uint32(ia); + super::numord::lemma_to_uint32(ib); + assert(js::num_bits(va) == js::f64_of_int(ia as int)); + assert(js::num_bits(vb) == js::f64_of_int(ib as int)); + assert(js::is_number(va) && js::is_number(vb)); + let c: u32 = (ib as u32) % 32u32; + assert((vb & 31u64) == (c as u64)) by (bit_vector) + requires + ib == (super::arith::lo32(vb) as i32), + c == (ib as u32) % 32u32, + ; + assert((va & 0xFFFF_FFFFu64) == ((ia as u32) as u64)) by (bit_vector) + requires + ia == (super::arith::lo32(va) as i32), + ; + assert(c as nat == super::bitops::shift_count(ib)); + vstd::arithmetic::power2::lemma2_to64_rest(); + assert((c as int % 64) as nat == c as nat); + let sc = super::bitops::shift_count(ib); + let q: int = ((ia as u32) as int) / (vstd::arithmetic::power2::pow2(sc) as int); + let s_ = ushr_s(va, vb); + assert(s_ == (q as u64)); + vstd::arithmetic::power2::lemma_pow2_pos(sc); + vstd::arithmetic::div_mod::lemma_div_is_ordered_by_denominator(((ia as u32) as int), 1, vstd::arithmetic::power2::pow2(sc) as int); + assert(0 <= q); + assert(q < 0x8000_0000int); + assert(js::fits_i32(q)); + let r: nat = ((ia as u32) as nat) / vstd::arithmetic::power2::pow2(sc); + assert(r as int == q); + assert(super::arith::tagged(s_) == js::enc_int32(s_ as i32)) by (bit_vector) + requires + s_ < 0x8000_0000u64, + ; + assert(s_ as int == q); + assert((s_ as i32) == (q as i32)); +} + +/// A `UShr` op: the lowering refines `step`. +pub proof fn lemma_ushr_op( + c: js::Code, + op: js::Op, + pc: nat, + lbl: nat, + lay: lir::Layout, + s: js::State, + st: lir::St, + fuel: nat, + dst: nat, + a: nat, + b: nat, +) + requires + super::proofs::pre(c, op, pc, lay, s, st, fuel), + op == (js::Op::UShr { dst, a, b }), + js::reg_ok(js::cur_code(s), dst), + js::reg_ok(js::cur_code(s), a), + js::reg_ok(js::cur_code(s), b), + super::lower::lower(c, op, pc, lbl) == lower_ushr(dst, a, b, op, pc), + ensures + outcome_refines(lay, s, op, lbl, lir::eval(lay, super::lower::lower(c, op, pc, lbl), st, fuel)), +{ + lemma_ushr_simple(); + let va = js::reg(s, a); + let vb = js::reg(s, b); + let f = ushr_f(va, vb); + lemma_ushr_ex_trace(lay, va, vb); + assert(f == 0 || f == 1); + if js::is_int32(va) && js::is_int32(vb) && f != 0 { + lemma_ushr_sem(s, dst, a, b); + lemma_ushr_val_trace(lay, va, vb); + } + super::blocks::lemma_bin_op2( + op, + pc, + lbl, + lay, + s, + st, + fuel, + dst, + a, + b, + ushr_ex(), + ushr_val(), + f, + ushr_word(va, vb), + ); +} + +} // verus! diff --git a/crates/clazel-jit/src/governor.rs b/crates/clazel-jit/src/governor.rs new file mode 100644 index 000000000..22412f267 --- /dev/null +++ b/crates/clazel-jit/src/governor.rs @@ -0,0 +1,802 @@ +//! The page's JIT governor (docs/phases/jit.md item J.11): the tiering +//! counters ([`crate::tier`]), the bounds ([`crate::limits`]) and the code +//! cache ([`crate::cache`], whose budget invariant is proven) put together, +//! with the measurement counters. +//! +//! The governor is the one place that answers "this function was entered: run +//! it compiled, run it in the interpreter, or compile it first?", and, when +//! something is refused, says why ([`Fallback`]). +//! +//! # What it does and does not do +//! +//! - It is plain safe Rust over [`CodeCache`]. The bound it adds to the +//! proven one is arithmetic on counters (`compiles_started <= +//! max_compiles`, tracked functions `<= max_tracked_functions`), checked by +//! the tests, not proven. +//! - **It maps nothing and enters nothing.** A region arrives from the caller +//! (compiled, validated and mapped by someone else), and leaves in an +//! [`Evicted`] list for the caller to unmap. Mapping and `enter` wait for +//! `clazel-sys`'s code regions (open PR #272); the region type will be +//! [`Mapped`](crate::cache::Mapped) around its `CodeRegion`. +//! - **It reads no clock.** Compile time is passed in by the caller. +//! +//! # Who must do what +//! +//! 1. Call [`Governor::on_entry`] at each function entry in the interpreter +//! loop (and [`Governor::on_back_edge`] at each loop back-edge). +//! 2. On [`Dispatch::Compiled`], run the code. +//! 3. On [`Dispatch::Compile`], compile, validate, map, and then call +//! [`Governor::submit`] (or [`Governor::compile_failed`]). Until then the +//! function runs in the interpreter and asks for nothing more. +//! 4. Unmap everything returned in an [`Evicted`] list, and drop a refused +//! region. + +use std::collections::BTreeMap; + +use crate::cache::{CodeCache, Evicted, Key, Region, Reject, Why}; +use crate::limits::{Fallback, Limits, Stats}; +use crate::tier::{Counters, Decision, State, Trigger, decide}; + +/// One function's bookkeeping. +#[derive(Clone, Copy, Debug)] +struct FnRecord { + /// The version the counters and state are about. + version: u32, + counters: Counters, + state: State, + /// Evictions and version changes so far (the backoff exponent). + strikes: u32, +} + +/// What to do at a function entry. +#[derive(Debug)] +pub enum Dispatch<'a, R> { + /// Run this compiled code. The borrow is of the cache's own entry, so it + /// cannot be held across an eviction. + Compiled(&'a R), + /// Run the interpreter loop. `Some` says why this function is not going + /// to be compiled; `None` means it is still warming up (or its compile is + /// in flight). + Interpret(Option), + /// Run the interpreter loop for now, and compile this function. + Compile(Trigger), +} + +/// What to do at a back-edge (the function is not compiled). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Verdict { + /// Keep interpreting; `Some` says why the function will not be compiled. + Interpret(Option), + /// Compile this function. + Compile(Trigger), +} + +/// A refused blob. +pub struct Refusal { + /// Why. The function stays interpreted. + pub why: Fallback, + /// The region that was offered, handed back to be dropped. + pub region: R, + /// Blobs of older versions of the same function that were dropped on the + /// way (to be unmapped). + pub unmap: Vec>, +} + +/// What a later report reads. +#[derive(Clone, Debug)] +pub struct Report { + /// The counters. + pub stats: Stats, + /// Code bytes resident now. + pub resident_bytes: usize, + /// Blobs resident now. + pub resident_blobs: usize, + /// The page's code budget. + pub page_budget: usize, + /// Compilations started. + pub compiles_started: u32, + /// Functions tracked. + pub tracked_functions: usize, +} + +/// One page's JIT policy and code cache. +pub struct Governor { + cache: CodeCache, + limits: Limits, + funcs: BTreeMap, + compiles_started: u32, + stats: Stats, +} + +impl Governor { + /// A governor for one page with these limits. + pub fn new(limits: Limits) -> Governor { + Governor { + cache: CodeCache::new(limits.page_budget), + limits, + funcs: BTreeMap::new(), + compiles_started: 0, + stats: Stats::default(), + } + } + + /// The limits in force (the cache's budget is clamped to + /// [`crate::cache::MAX_BUDGET`]; see [`Governor::report`]). + pub fn limits(&self) -> &Limits { + &self.limits + } + + /// The counters so far. + pub fn stats(&self) -> &Stats { + &self.stats + } + + /// The cache, read-only. + pub fn cache(&self) -> &CodeCache { + &self.cache + } + + /// A snapshot for the report. + pub fn report(&self) -> Report { + Report { + stats: self.stats.clone(), + resident_bytes: self.cache.used(), + resident_blobs: self.cache.len(), + page_budget: self.cache.budget(), + compiles_started: self.compiles_started, + tracked_functions: self.funcs.len(), + } + } + + /// The record for `func` at `version`, made if there is room. A version + /// change starts the counters over and is a strike (inline-cache churn + /// must not buy a page a free recompile each time). + fn record(&mut self, func: u32, version: u32) -> Option<&mut FnRecord> { + if !self.funcs.contains_key(&func) { + if self.funcs.len() >= self.limits.max_tracked_functions { + return None; + } + self.funcs.insert( + func, + FnRecord { + version, + counters: Counters::default(), + state: State::Interpreting, + strikes: 0, + }, + ); + } + let rec = self.funcs.get_mut(&func)?; + if rec.version != version { + rec.version = version; + rec.counters = Counters::default(); + rec.state = State::Interpreting; + rec.strikes = rec.strikes.saturating_add(1); + } + Some(rec) + } + + /// Count one step of the function in the interpreter and decide. + fn step(&mut self, func: u32, version: u32, entry: bool) -> Verdict { + let thresholds = self.limits.thresholds; + let quota_left = self.compiles_started < self.limits.max_compiles; + let Some(rec) = self.record(func, version) else { + self.stats.note_fallback(Fallback::TooManyFunctions); + return Verdict::Interpret(Some(Fallback::TooManyFunctions)); + }; + if entry { + rec.counters.enter(); + } else { + rec.counters.back_edge(); + } + // A function marked compiled that the cache no longer holds was + // evicted without our noticing; treat it as interpreting. + if rec.state == State::Compiled { + rec.state = State::Interpreting; + } + match rec.state { + State::Refused(why) => Verdict::Interpret(Some(why)), + State::Compiling | State::Compiled => Verdict::Interpret(None), + State::Interpreting => { + match decide(&rec.counters, &thresholds, rec.strikes, rec.state) { + Decision::Stay => Verdict::Interpret(None), + Decision::Compile(trigger) => { + if quota_left { + rec.state = State::Compiling; + self.compiles_started = self.compiles_started.saturating_add(1); + Verdict::Compile(trigger) + } else { + rec.state = State::Refused(Fallback::CompileQuotaExhausted); + self.stats.note_fallback(Fallback::CompileQuotaExhausted); + Verdict::Interpret(Some(Fallback::CompileQuotaExhausted)) + } + } + } + } + } + } + + /// A function was entered. See [`Dispatch`]. + pub fn on_entry(&mut self, func: u32, version: u32) -> Dispatch<'_, R> { + let key = Key { func, version }; + if self.cache.contains(key) { + self.stats.note_hit(); + // `lookup` borrows the cache for as long as the answer lives, so + // the borrow is created only on the path that returns it. + return match self.cache.lookup(key) { + Some(region) => Dispatch::Compiled(region), + None => Dispatch::Interpret(None), + }; + } + self.stats.note_miss(); + match self.step(func, version, true) { + Verdict::Interpret(why) => Dispatch::Interpret(why), + Verdict::Compile(t) => Dispatch::Compile(t), + } + } + + /// A loop back-edge was taken in the interpreter. See [`Verdict`]. + pub fn on_back_edge(&mut self, func: u32, version: u32) -> Verdict { + self.step(func, version, false) + } + + /// The back end refused the function: it stays interpreted at this + /// version. + pub fn compile_failed(&mut self, func: u32, version: u32) { + if let Some(rec) = self.record(func, version) { + rec.state = State::Refused(Fallback::BackEndRefused); + self.stats.note_fallback(Fallback::BackEndRefused); + } + } + + /// Mark a function whose blob left the cache as interpreting again, with + /// a strike. + fn struck(&mut self, key: Key) { + if let Some(rec) = self.funcs.get_mut(&key.func) + && rec.version == key.version + && rec.state == State::Compiled + { + rec.state = State::Interpreting; + rec.counters = Counters::default(); + rec.strikes = rec.strikes.saturating_add(1); + } + } + + /// Account for blobs that left the cache. + fn account(&mut self, left: &[Evicted]) { + for e in left { + match e.why { + Why::Capacity => { + self.stats.note_eviction(e.bytes); + self.struck(e.key); + } + Why::Replaced => self.stats.note_replacement(), + Why::Invalidated => self.stats.note_invalidation(), + } + } + } + + /// A compiled, validated and mapped blob for `key`, `compile_nanos` being + /// what the caller measured for the whole compile. + /// + /// On `Ok` the blob is resident and the returned blobs (evicted, replaced + /// or stale) are to be unmapped. On `Err` the function stays interpreted + /// for the stated reason. + pub fn submit( + &mut self, + key: Key, + region: R, + compile_nanos: u64, + ) -> Result>, Refusal> { + let current = matches!(self.funcs.get(&key.func), Some(r) if r.version == key.version); + if !current { + self.stats.note_fallback(Fallback::StaleVersion); + return Err(Refusal { + why: Fallback::StaleVersion, + region, + unmap: Vec::new(), + }); + } + let bytes = region.byte_len(); + // Older versions of this function can never be entered again. + let mut unmap = self.cache.invalidate_but(key.func, Some(key.version)); + match self + .cache + .insert(key, region, self.limits.max_function_bytes) + { + Ok(left) => { + unmap.extend(left); + self.account(&unmap); + self.stats.note_compile(bytes, compile_nanos); + self.stats.note_resident(self.cache.used()); + if let Some(rec) = self.funcs.get_mut(&key.func) { + rec.state = State::Compiled; + } + Ok(unmap) + } + Err(rej) => { + let why = match rej.why { + Reject::Empty => Fallback::EmptyBlob, + Reject::FunctionTooLarge => Fallback::FunctionTooLarge, + Reject::ExceedsBudget => Fallback::ExceedsPageBudget, + }; + self.account(&unmap); + self.stats.note_compile(0, compile_nanos); + self.stats.note_fallback(why); + if let Some(rec) = self.funcs.get_mut(&key.func) { + rec.state = State::Refused(why); + } + Err(Refusal { + why, + region: rej.region, + unmap, + }) + } + } + } + + /// The function's bytecode changed: drop all its blobs (returned, to be + /// unmapped) and start its counters over, with a strike. + pub fn invalidate_function(&mut self, func: u32) -> Vec> { + let left = self.cache.invalidate(func); + self.account(&left); + if let Some(rec) = self.funcs.get_mut(&func) { + rec.state = State::Interpreting; + rec.counters = Counters::default(); + rec.strikes = rec.strikes.saturating_add(1); + } + left + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::cache::Mapped; + use crate::tier::Thresholds; + use std::cell::Cell; + use std::rc::Rc; + + /// A handle that counts how many are alive. + struct Token(Rc>); + + impl Token { + fn new(live: &Rc>) -> Token { + live.set(live.get().saturating_add(1)); + Token(Rc::clone(live)) + } + } + + impl Drop for Token { + fn drop(&mut self) { + self.0.set(self.0.get().saturating_sub(1)); + } + } + + type Blob = Mapped; + type Live = Rc>; + + fn blob(live: &Live, len: usize) -> Blob { + Mapped { + handle: Token::new(live), + len, + } + } + + /// The `Ok` of a submit. + fn ok(r: Result>, Refusal>) -> Vec> { + match r { + Ok(v) => v, + Err(r) => unreachable!("refused: {:?}", r.why), + } + } + + /// The `Err` of a submit. + fn refused(r: Result>, Refusal>) -> Refusal { + match r { + Ok(_) => unreachable!("expected a refusal"), + Err(r) => r, + } + } + + fn key(func: u32, version: u32) -> Key { + Key { func, version } + } + + fn small_limits() -> Limits { + Limits { + max_function_bytes: 400, + page_budget: 1000, + max_compiles: 50, + max_tracked_functions: 20, + thresholds: Thresholds { + entries: 3, + back_edges: 10, + }, + } + } + + /// Drive `func` until it asks to be compiled. + fn warm(g: &mut Governor, func: u32, version: u32) -> Trigger { + for _ in 0..10_000 { + match g.on_entry(func, version) { + Dispatch::Compile(t) => return t, + Dispatch::Compiled(_) => unreachable!("already compiled"), + Dispatch::Interpret(Some(why)) => unreachable!("refused: {why:?}"), + Dispatch::Interpret(None) => {} + } + } + unreachable!("never asked to compile") + } + + fn resident_sum(g: &Governor) -> usize { + let mut sum = 0usize; + let mut i = 0usize; + while let Some((_, n)) = g.cache().entry_at(i) { + sum = sum.saturating_add(n); + i = i.saturating_add(1); + } + sum + } + + #[test] + fn warm_up_then_compile_then_hit() { + let live = Live::default(); + let mut g = Governor::new(small_limits()); + for _ in 0..2 { + assert!(matches!(g.on_entry(1, 0), Dispatch::Interpret(None))); + } + assert!(matches!( + g.on_entry(1, 0), + Dispatch::Compile(Trigger::Entries) + )); + // The compile is in flight: no second request. + assert!(matches!(g.on_entry(1, 0), Dispatch::Interpret(None))); + let left = ok(g.submit(key(1, 0), blob(&live, 100), 1234)); + assert!(left.is_empty()); + assert!(matches!(g.on_entry(1, 0), Dispatch::Compiled(r) if r.len == 100)); + let s = g.stats(); + assert_eq!( + (s.compiles, s.compile_nanos, s.code_bytes_emitted), + (1, 1234, 100) + ); + assert_eq!((s.hits, s.misses), (1, 4)); + assert_eq!(g.report().resident_bytes, 100); + } + + #[test] + fn a_loop_entered_once_compiles() { + let mut g: Governor = Governor::new(small_limits()); + assert!(matches!(g.on_entry(7, 0), Dispatch::Interpret(None))); + for _ in 0..9 { + assert_eq!(g.on_back_edge(7, 0), Verdict::Interpret(None)); + } + assert_eq!(g.on_back_edge(7, 0), Verdict::Compile(Trigger::BackEdges)); + } + + #[test] + fn oversize_function_falls_back_with_a_reason_and_stays_out() { + let live = Live::default(); + let mut g = Governor::new(small_limits()); + warm(&mut g, 1, 0); + let r = refused(g.submit(key(1, 0), blob(&live, 401), 5)); + assert_eq!(r.why, Fallback::FunctionTooLarge); + assert_eq!(r.region.len, 401); + drop(r); + assert_eq!(live.get(), 0, "a refused region is handed back, not kept"); + assert_eq!(g.report().resident_bytes, 0); + // Never asked again at this version. + for _ in 0..100 { + assert!(matches!( + g.on_entry(1, 0), + Dispatch::Interpret(Some(Fallback::FunctionTooLarge)) + )); + } + assert_eq!(g.stats().fallback_count(Fallback::FunctionTooLarge), 1); + // A new version is a new chance. + assert!(matches!(g.on_entry(1, 1), Dispatch::Interpret(None))); + } + + #[test] + fn blob_over_the_page_budget_is_refused() { + let live = Live::default(); + let mut g = Governor::new(Limits { + max_function_bytes: 5000, + ..small_limits() + }); + warm(&mut g, 1, 0); + let r = refused(g.submit(key(1, 0), blob(&live, 1001), 0)); + assert_eq!(r.why, Fallback::ExceedsPageBudget); + assert!(r.why.reason().contains("budget")); + } + + #[test] + fn empty_blob_and_failed_compile_are_refused() { + let live = Live::default(); + let mut g = Governor::new(small_limits()); + warm(&mut g, 1, 0); + let r = refused(g.submit(key(1, 0), blob(&live, 0), 0)); + assert_eq!(r.why, Fallback::EmptyBlob); + warm(&mut g, 2, 0); + g.compile_failed(2, 0); + assert!(matches!( + g.on_entry(2, 0), + Dispatch::Interpret(Some(Fallback::BackEndRefused)) + )); + } + + #[test] + fn eviction_makes_the_function_interpret_and_earn_its_blob_again() { + let live = Live::default(); + let mut g = Governor::new(small_limits()); + for f in 1..=2u32 { + warm(&mut g, f, 0); + drop(ok(g.submit(key(f, 0), blob(&live, 400), 1))); + } + // 800 of 1000 used. Function 1 is the least recently used. + warm(&mut g, 3, 0); + let left = ok(g.submit(key(3, 0), blob(&live, 400), 1)); + assert_eq!(left.len(), 1); + let first = left.first().map(|e| (e.key, e.why)); + assert_eq!(first, Some((key(1, 0), Why::Capacity))); + drop(left); + assert_eq!(live.get(), 2); + assert!(g.report().resident_bytes <= 1000); + assert_eq!(g.stats().evictions, 1); + assert_eq!(g.stats().evicted_bytes, 400); + // Function 1 is not entered compiled any more. It had a strike, so + // its threshold is 6 entries, not 3. + let mut entries = 0u32; + loop { + entries = entries.saturating_add(1); + match g.on_entry(1, 0) { + Dispatch::Compile(_) => break, + Dispatch::Interpret(None) => {} + _ => unreachable!("unexpected dispatch"), + } + assert!(entries < 100); + } + assert_eq!(entries, 6); + } + + #[test] + fn a_hit_keeps_a_blob_resident_over_an_older_one() { + let live = Live::default(); + let mut g = Governor::new(small_limits()); + for f in 1..=2u32 { + warm(&mut g, f, 0); + drop(ok(g.submit(key(f, 0), blob(&live, 400), 1))); + } + // Touch function 1: function 2 becomes the least recently used. + assert!(matches!(g.on_entry(1, 0), Dispatch::Compiled(_))); + warm(&mut g, 3, 0); + let left = ok(g.submit(key(3, 0), blob(&live, 400), 1)); + assert_eq!(left.first().map(|e| e.key.func), Some(2)); + assert!(matches!(g.on_entry(1, 0), Dispatch::Compiled(_))); + assert!(matches!(g.on_entry(2, 0), Dispatch::Interpret(_))); + } + + #[test] + fn a_new_version_drops_the_old_blob() { + let live = Live::default(); + let mut g = Governor::new(small_limits()); + warm(&mut g, 1, 0); + drop(ok(g.submit(key(1, 0), blob(&live, 100), 1))); + warm(&mut g, 1, 1); + let left = ok(g.submit(key(1, 1), blob(&live, 120), 1)); + assert_eq!(left.len(), 1); + assert_eq!( + left.first().map(|e| (e.key.version, e.why)), + Some((0, Why::Invalidated)) + ); + drop(left); + assert_eq!(live.get(), 1); + assert_eq!(g.report().resident_bytes, 120); + assert_eq!(g.stats().invalidations, 1); + assert!(matches!(g.on_entry(1, 1), Dispatch::Compiled(_))); + } + + #[test] + fn a_blob_for_a_version_that_moved_on_is_refused() { + let live = Live::default(); + let mut g = Governor::new(small_limits()); + warm(&mut g, 1, 0); + // Version 1 is entered while version 0 compiles. + let _ = g.on_entry(1, 1); + let r = refused(g.submit(key(1, 0), blob(&live, 100), 1)); + assert_eq!(r.why, Fallback::StaleVersion); + assert_eq!(g.report().resident_bytes, 0); + } + + #[test] + fn invalidate_function_drops_all_its_blobs() { + let live = Live::default(); + let mut g = Governor::new(small_limits()); + warm(&mut g, 1, 0); + drop(ok(g.submit(key(1, 0), blob(&live, 100), 1))); + let left = g.invalidate_function(1); + assert_eq!(left.len(), 1); + drop(left); + assert_eq!(live.get(), 0); + assert_eq!(g.report().resident_bytes, 0); + assert!(matches!(g.on_entry(1, 0), Dispatch::Interpret(None))); + } + + #[test] + fn compile_quota_stops_new_compiles() { + let live = Live::default(); + let mut g = Governor::new(Limits { + max_compiles: 3, + ..small_limits() + }); + for f in 0..3u32 { + warm(&mut g, f, 0); + drop(ok(g.submit(key(f, 0), blob(&live, 10), 1))); + } + for _ in 0..2 { + assert!(matches!(g.on_entry(9, 0), Dispatch::Interpret(None))); + } + assert!(matches!( + g.on_entry(9, 0), + Dispatch::Interpret(Some(Fallback::CompileQuotaExhausted)) + )); + assert_eq!(g.report().compiles_started, 3); + } + + #[test] + fn tracked_functions_are_bounded() { + let mut g: Governor = Governor::new(small_limits()); + for f in 0..1000u32 { + let _ = g.on_entry(f, 0); + } + assert_eq!(g.report().tracked_functions, 20); + assert!(matches!( + g.on_entry(999, 0), + Dispatch::Interpret(Some(Fallback::TooManyFunctions)) + )); + assert_eq!(g.stats().fallback_count(Fallback::TooManyFunctions), 981); + } + + /// A xorshift generator, so the flood is the same on every run. + struct Rng(u64); + + impl Rng { + fn next(&mut self) -> u64 { + self.0 ^= self.0.wrapping_shl(13); + self.0 ^= self.0.wrapping_shr(7); + self.0 ^= self.0.wrapping_shl(17); + self.0 + } + + /// A number below `n` (`n > 0`). + fn below(&mut self, n: u64) -> u64 { + self.next().checked_rem(n).unwrap_or(0) + } + } + + /// A flood of compile requests, with sizes from nothing to far over every + /// cap, never lets the resident code pass the budget, and never leaks a + /// region: what is alive is exactly what is resident. + #[test] + fn a_flood_of_compile_requests_stays_within_the_budget() { + let rounds: u32 = if cfg!(miri) { 300 } else { 60_000 }; + let live = Live::default(); + let limits = Limits { + max_function_bytes: 3000, + page_budget: 20_000, + max_compiles: u32::MAX, + max_tracked_functions: 500, + thresholds: Thresholds { + entries: 2, + back_edges: 5, + }, + }; + let mut g: Governor = Governor::new(limits); + let mut rng = Rng(0x9E37_79B9_7F4A_7C15); + let mut version = [0u32; 600]; + for round in 0..rounds { + let func = rng.below(600) as u32; + let churn = rng.below(17) == 0; + let v = match version.get_mut(func as usize) { + Some(v) => { + // Inline-cache churn: now and then the function changes. + if churn { + *v = v.saturating_add(1); + } + *v + } + None => 0, + }; + let verdict = if rng.below(4) == 0 { + g.on_back_edge(func, v) + } else { + match g.on_entry(func, v) { + Dispatch::Compile(t) => Verdict::Compile(t), + Dispatch::Compiled(_) => Verdict::Interpret(None), + Dispatch::Interpret(w) => Verdict::Interpret(w), + } + }; + if let Verdict::Compile(_) = verdict { + let len = match rng.below(10) { + 0 => 0, + 1 => 1_000_000, + 2 => 20_001, + 3 => 3001, + _ => (rng.below(3000) as usize).saturating_add(1), + }; + if rng.below(12) == 0 { + g.compile_failed(func, v); + } else { + match g.submit(key(func, v), blob(&live, len), 10) { + Ok(left) => drop(left), + Err(r) => { + assert!(!r.why.reason().is_empty()); + drop(r); + } + } + } + } + if round % 97 == 0 { + drop(g.invalidate_function(rng.below(600) as u32)); + } + // The invariants, after every request. + let used = g.cache().used(); + assert!(used <= 20_000, "resident code {used} passed the budget"); + assert_eq!(used, resident_sum(&g)); + assert_eq!( + live.get(), + g.cache().len(), + "a region is alive but not resident" + ); + assert!(g.report().tracked_functions <= 500); + } + let s = g.stats(); + assert!(s.peak_resident_bytes <= 20_000); + assert!( + s.evictions > 0 || rounds < 1000, + "the flood should have forced evictions" + ); + assert!(s.total_fallbacks() > 0); + assert!(s.compiles > 0); + } + + /// A page that cycles through more equally hot functions than fit does + /// not compile for ever: the backoff makes each round cost more. + #[test] + fn thrashing_is_damped_by_backoff() { + let live = Live::default(); + let limits = Limits { + max_function_bytes: 1000, + page_budget: 2000, + max_compiles: u32::MAX, + max_tracked_functions: 100, + thresholds: Thresholds { + entries: 2, + back_edges: 1000, + }, + }; + let mut g: Governor = Governor::new(limits); + // Three functions of 1000 bytes each, two fit. + let steps: u32 = if cfg!(miri) { 2_000 } else { 200_000 }; + let mut compiles_early = 0u64; + for step in 0..steps { + let func = step % 3; + if let Dispatch::Compile(_) = g.on_entry(func, 0) { + drop(ok(g.submit(key(func, 0), blob(&live, 1000), 1))); + } + if step == steps / 10 { + compiles_early = g.stats().compiles; + } + } + let total = g.stats().compiles; + assert!( + total < u64::from(steps) / 20, + "{total} compiles in {steps} steps" + ); + // Compiles slow down: the last nine tenths add few compared with the + // first tenth. + let late = total.saturating_sub(compiles_early); + assert!(late < compiles_early.saturating_mul(4).saturating_add(10)); + assert!(g.cache().used() <= 2000); + } +} diff --git a/crates/clazel-jit/src/lib.rs b/crates/clazel-jit/src/lib.rs new file mode 100644 index 000000000..d5396069d --- /dev/null +++ b/crates/clazel-jit/src/lib.rs @@ -0,0 +1,236 @@ +//! The JIT's back ends (docs/phases/jit.md item J.9), the x86-64 one first: +//! LIR → x86-64 with **no register allocation**. Every LIR local, and every +//! element of LIR's operand stack, is a frame slot; each LIR instruction is a +//! fixed template over the scratch registers `rax`, `rcx` and `rdx`, so each +//! template's proof is local. +//! +//! This is the first slice of J.9: straight-line integer code. It was written +//! contract first (CLAUDE.md rule 3): [`spec`], [`contract`] and [`api`] came +//! in their own commit, before any template. +//! +//! **The A64 back end is [`a64`]**: the same design and the same slice over +//! `clazel-a64`'s machine, module for module (its header says what is proven +//! there and what differs). The rest of this header, and the modules below, +//! are the x86-64 one. +//! +//! **The JS front end is [`front`]** (item J.10): bytecode → LIR, a proof per +//! op that the LIR refines `clazel-js-vm`'s `step`. It stops at LIR, so it +//! holds for both back ends. Its header says which ops are lowered and proven +//! and which are not. +//! +//! # The statements +//! +//! - [`spec`]: **what the back end is about.** The state relation between a +//! LIR state and an x86-64 machine state, the frame layout (the codecheck +//! ABI fixes where the code may write), the straight-line source subset +//! [`spec::Op`]. Read its header first: it lists the modelling choices. +//! - [`contract`]: the refinement statements (a template's x86-64 code, +//! run under `clazel-x64`'s `step` from a related state, reaches a state +//! related to LIR's `eval`), and the emitter's contracts. +//! - [`api`]: the contract-only trait [`api::Backend`]. +//! +//! # The templates and their proofs +//! +//! - [`templates`]: the templates, as spec functions (`tmpl`, `code_of`). +//! - [`emit`]: the executable back end, [`emit::X64Backend`], proven equal to +//! the templates and, through `clazel-x64`'s encoder, to their bytes. +//! - Proofs, bottom up: [`memlemmas`] (byte memory), [`steps`], [`alu`] and +//! [`flags`] (one `x64::step` of each instruction form the templates use), +//! [`runseq`] (running a list), [`mid`] (each template's register-only +//! middle computes LIR's result), [`rel`], [`skel`] and [`agree`] (the +//! loads-middle-store skeleton preserves the state relation), [`proofs`] +//! (the refinement of each template, `lemma_op_refines`), [`compose`] (the +//! program, `lemma_prog_refines`), [`exit`] (the exit sequence), +//! [`tlemmas`] (templates are in the subset, and are what the emitter +//! pushes). +//! +//! # What is proven (Verus, no axiom, no assumption) +//! +//! - **Each template refines its LIR instruction** ([`contract::refines`]), +//! for: `nop`, `drop`, `t.const`, `local.get`, `local.set`, `local.tee`, +//! `select`; `i32` and `i64` `add`, `sub`, `mul`, `and`, `or`, `xor`, +//! `shl`, `shr_s`, `shr_u`, and the ten comparisons; `i32.eqz`, +//! `i64.eqz`, `i32.wrap_i64`, `i64.extend_i32_s`, `i64.extend_i32_u`, +//! `f64.neg`, `f64.abs` and the two `reinterpret`s (the last four are bit +//! operations). +//! - **A straight-line program of them refines** the left-nested `then` +//! chain ([`compose::lemma_prog_refines`]). +//! - **The exit sequence refines `exit c`** with no operands +//! ([`contract::exit_refines`], [`exit::lemma_exit_refines`]). +//! - **The emitter returns exactly the template code, and its encoding** +//! ([`contract::emit_body_ensures`], [`contract::emit_blob_ensures`]), +//! and refuses (`None`) exactly the programs [`spec::prog_ok`] refuses. +//! +//! # Control flow (the second slice, x86-64 only) +//! +//! Written contract first like the first ([`cfspec`], [`cfcontract`] in +//! their own commit; the statement was refined while proving, see the PR). +//! The source language is [`cfspec::Node`] (void blocks only), the statement +//! is [`cfcontract::cf_refines`] (a forward simulation over a run with jumps, +//! [`cfspec::reaches`]: `Normal`, `Branch` to a label's address, `Exit`). +//! +//! - **Proven, no axiom, no assumption:** +//! - the static analysis [`cfspec::cf_flow`] is sound for LIR's `eval` +//! ([`cfheight`]); +//! - composition over `then`, `block`, `loop` and `if` +//! ([`cfcompose::lemma_then`], [`cfblock::lemma_block`], +//! [`cfblock::lemma_loop`], [`cfif::lemma_if`]), and the leaves `br` +//! (a `jmp`), `br_if` (condition test and `jcc`), `exit` **with +//! operands** and a straight-line instruction ([`cfleaf`], [`cfexit`], +//! [`cfop`]); +//! - by induction over the program, **every templated node's code refines +//! it** ([`cfproofs::lemma_ctmpl_refines`]), with every `jmp` and `jcc` +//! displacement a 32-bit number ([`cfproofs::labels_fit`]) and every +//! instruction in the subset ([`cfvalid`]); +//! - **the memory instructions** `bounds_check`, `load` and `store` +//! ([`cfmem`], [`cfmemops`]): the code is the validator's own +//! bounds-check sequence (`clazel_codecheck::spec::check_seq`, run by +//! [`cfcheckrun`]) around the access, with the two exits going to a stub +//! after the access ([`cftemplates::mem_code`]); the access reads and +//! writes through `mem_rel` ([`cfmemrel`], [`cfmemacc`]); the check +//! fails exactly when LIR's `bounds_check` exits (and where a load or +//! store is `Stuck` nothing is claimed); +//! - **`br_table`** ([`cfbrt`]): a compare-and-`je` chain and a `jmp` to +//! the default, by induction on the entry; +//! - that the validator's access rule admits the access the template +//! makes ([`cfmemvalid::lemma_access_accepted`]). +//! - **The emitter** [`cfemit::emit_cf_blob`] is **proven equal to the +//! templates**: [`cfcontract::emit_cf_blob_ensures`] is its postcondition. +//! The bytes are the encoding of [`cftemplates::ctmpl`] (with the return +//! label at the end) and the exit sequence, and it answers `None` exactly +//! when [`cftemplates::cf_prog_ok`] refuses, so every emitted blob refines +//! its program ([`cfemit::lemma_emitted_refines`], with +//! [`cfproofs::lemma_ctmpl_refines`]). No signed arithmetic is needed: +//! positions are `usize`, a backward displacement is `!(end - target - 1)` +//! on a `u32`. `tests/validator_cf.rs` additionally checks that the +//! validator accepts every memory template, `br_table`, and 1500 generated +//! programs with control flow and memory. +//! - **The validator accepts every emitted blob (proven)** +//! ([`cfblobacc::theorem_blob_accepted`], carried by `emit_cf_blob` as a +//! postcondition, with the exactness of its bytes +//! [`cfblobacc::emitted_bytes`]): for every program with a translation, +//! `clazel_codecheck::spec::accepted(blob, [0])`. The proof ([`cfblobacc`]'s +//! header): the code is cut into the validator's units ([`cfunits`], +//! [`cfaccept::utmpl`]: an exit sequence, the bounds-check sequence as +//! `check_unit_at` recognizes it, single instructions that are neither a +//! `ret` nor a `jcc 2`), the scan walks exactly those units +//! ([`cfunits::lemma_scan_units`]) and accepts them whatever it knew +//! ([`cfuacc`], [`cfuacc2`], with [`cfsimple`] for the straight-line +//! templates): the access of a `load` or `store` is admitted because the +//! base was loaded and the index checked just before, and no jump lands +//! between ([`cfaccept::prot`]); every jump lands on a label or on a unit +//! start ([`cfjt`], [`cfbs`]). +//! - **Not done:** +//! - the corollary with `clazel-codecheck`'s confinement theorem (every +//! emitted blob, run from an accepted entry with the context invariant, +//! stays in its regions and leaves only through the exit): that theorem is +//! not in this stack's base, so it is not stated here; +//! - blocks with parameters or results (modelling choice 8 of [`cfspec`]). +//! +//! The A64 back end's control flow, memory and emitter are the same slice +//! over `clazel-a64`, proven likewise: see [`a64`]. +//! +//! # The code cache and the page's bounds (J.11, first slice) +//! +//! Pure safe Rust, with nothing mapped and nothing entered (that waits for +//! `clazel-sys`'s code regions, open PR #272). +//! +//! - [`cache`]: the code cache, a byte-budgeted store of compiled blobs keyed +//! by bytecode function id and version, generic over [`cache::Region`]. +//! **Proven in Verus (no axiom, no assumption):** the resident bytes are +//! the sum of the regions' own lengths and never exceed the budget; keys +//! are unique; a rejected insert changes nothing; an evicted region leaves +//! the cache by value, and no resident key is an evicted one. See its +//! header for the statement. +//! - [`tier`]: when to compile (entry and back-edge thresholds, with backoff +//! per eviction), as plain functions with tests. +//! - [`limits`]: the hostile-page bounds (per-function size, per-page budget, +//! compile quota, tracked functions), the [`limits::Fallback`] reasons and +//! the measurement counters ([`limits::Stats`]; no clock is read). +//! - [`governor`]: the three together, with tests that a flood of compile +//! requests stays within the budget (arithmetic on counters, tested, not +//! proven). +//! +//! # What is not (of the first slice) +//! +//! - **That the validator accepts every blob of the first slice's own +//! emitter** (`emit::X64Backend`) is checked by `tests/validator.rs` (every +//! template, 3000 generated programs), not proven; the control-flow emitter +//! is proven above, and covers straight-line programs (a `then` chain of +//! instructions). +//! - **Not covered by any template of the first slice:** the memory +//! instructions, structured control flow, `br_table` and `exit` with +//! operands (those are the second slice, above, for x86-64); `f64` arithmetic, comparison +//! and conversion +//! (LIR's `f64` operators are uninterpreted functions, so a refinement to +//! SSE would need an axiom this crate does not add); rotates (the subset +//! has no rotate instruction); a frame beyond 31 slots (it needs the +//! region frame and the bounds-check sequence on every access). +//! - **No tier is claimed.** `tier` stays 0 until CI's full verification of +//! this crate is green (CLAUDE.md rule 7), and the crate has no independent +//! transcription and no conformance suite, which +//! tier 2 requires. The proofs are about the spec of `clazel-x64` and the +//! semantics of `clazel-lir`, whose faithfulness is their crates' claim. +//! +//! Tier: see `[package.metadata.clazel]` in `Cargo.toml` and +//! `docs/VERIFICATION.md`. + +pub mod a64; +pub mod agree; +pub mod alu; +pub mod api; +pub mod cache; +pub mod cfaccept; +pub mod cfalg; +pub mod cfblobacc; +pub mod cfblock; +pub mod cfbrt; +pub mod cfbs; +pub mod cfcheck; +pub mod cfcheckrun; +pub mod cfcompose; +pub mod cfcontract; +pub mod cfemit; +pub mod cfexit; +pub mod cfheight; +pub mod cfif; +pub mod cfjt; +pub mod cfleaf; +pub mod cflens; +pub mod cfmem; +pub mod cfmemacc; +pub mod cfmemflow; +pub mod cfmemlens; +pub mod cfmemops; +pub mod cfmemrel; +pub mod cfmemsteps; +pub mod cfmemvalid; +pub mod cfop; +pub mod cfproofs; +pub mod cfsimple; +pub mod cfspec; +pub mod cfsteps; +pub mod cftemplates; +pub mod cfuacc; +pub mod cfuacc2; +pub mod cfunits; +pub mod cfvalid; +pub mod compose; +pub mod contract; +pub mod emit; +pub mod exit; +pub mod flags; +pub mod front; +pub mod governor; +pub mod limits; +pub mod memlemmas; +pub mod mid; +pub mod proofs; +pub mod rel; +pub mod runseq; +pub mod skel; +pub mod spec; +pub mod steps; +pub mod templates; +pub mod tier; +pub mod tlemmas; diff --git a/crates/clazel-jit/src/limits.rs b/crates/clazel-jit/src/limits.rs new file mode 100644 index 000000000..8c0482e82 --- /dev/null +++ b/crates/clazel-jit/src/limits.rs @@ -0,0 +1,243 @@ +//! The hostile-page bounds and the measurement counters of the JIT's code +//! cache (docs/phases/jit.md item J.11). +//! +//! - [`Limits`]: what one page may use. Every limit has a default and a stated +//! reason, and every way of exceeding one ends in the same place: the +//! function stays in the interpreter loop, for a stated [`Fallback`]. +//! - [`Stats`]: the counters a later report reads (compile time, code bytes, +//! evictions, fallbacks). **No clock is read here**: the caller measures a +//! compile with its own clock and passes the nanoseconds in, so nothing in +//! this crate depends on wall-clock time (and nothing in a proof could). + +use crate::tier::Thresholds; + +/// The largest blob the A64 back end emits (`a64::cfemit::MAX_WORDS` words of +/// four bytes, 512 KiB). The x86-64 emitter's own cap is the same size +/// (`cftemplates::max_size`). +pub const BACKEND_MAX_BYTES: usize = 0x8_0000; + +/// What one page's code cache may use. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Limits { + /// The most bytes of compiled code one function may take. A blob above + /// this is refused ([`Fallback::FunctionTooLarge`]) and its function + /// stays interpreted. Never above [`BACKEND_MAX_BYTES`] in practice, since + /// the back ends refuse larger bodies themselves. + pub max_function_bytes: usize, + /// The most bytes of compiled code the page may hold at once (J.11: + /// "bound the code bytes per renderer"). Past it the least recently used + /// blobs are evicted. Clamped to [`crate::cache::MAX_BUDGET`]. + pub page_budget: usize, + /// The most compilations the page may start over its lifetime. Eviction + /// and recompilation could otherwise burn CPU for ever on a page that + /// cycles through more hot functions than fit. Past it nothing new + /// compiles ([`Fallback::CompileQuotaExhausted`]). + pub max_compiles: u32, + /// The most functions whose counters are tracked. Past it a new function + /// is simply interpreted ([`Fallback::TooManyFunctions`]), so the + /// bookkeeping is bounded whatever the page defines. + pub max_tracked_functions: usize, + /// When to compile (jit.md J.10). + pub thresholds: Thresholds, +} + +impl Default for Limits { + /// Starting values, to be set from J.11's measurements: 512 KiB per + /// function (the back ends' own cap), 8 MiB per page, 4096 compilations + /// and 65,536 tracked functions. + fn default() -> Limits { + Limits { + max_function_bytes: BACKEND_MAX_BYTES, + page_budget: 8 * 1024 * 1024, + max_compiles: 4096, + max_tracked_functions: 65_536, + thresholds: Thresholds::default(), + } + } +} + +/// Why a function runs in the interpreter loop instead of compiled. The +/// interpreter is complete, so every one of these is a slower run, never a +/// wrong one. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum Fallback { + /// The compiled blob is longer than [`Limits::max_function_bytes`]. + FunctionTooLarge, + /// The compiled blob is longer than the whole page budget. + ExceedsPageBudget, + /// The back end produced no code. + EmptyBlob, + /// The page has started [`Limits::max_compiles`] compilations. + CompileQuotaExhausted, + /// The page already has [`Limits::max_tracked_functions`] functions. + TooManyFunctions, + /// The back end refused the function (a construct it has no template for, + /// or a body over its own size cap). + BackEndRefused, + /// The function changed (its version moved on) while its blob was being + /// compiled, so the blob is for code that no longer exists. + StaleVersion, +} + +impl Fallback { + /// The number of reasons. + pub const COUNT: usize = 7; + + /// Every reason, in [`Fallback::index`] order. + pub const ALL: [Fallback; Fallback::COUNT] = [ + Fallback::FunctionTooLarge, + Fallback::ExceedsPageBudget, + Fallback::EmptyBlob, + Fallback::CompileQuotaExhausted, + Fallback::TooManyFunctions, + Fallback::BackEndRefused, + Fallback::StaleVersion, + ]; + + /// The position of this reason in [`Fallback::ALL`] and in + /// [`Stats::fallbacks`]. + pub fn index(self) -> usize { + match self { + Fallback::FunctionTooLarge => 0, + Fallback::ExceedsPageBudget => 1, + Fallback::EmptyBlob => 2, + Fallback::CompileQuotaExhausted => 3, + Fallback::TooManyFunctions => 4, + Fallback::BackEndRefused => 5, + Fallback::StaleVersion => 6, + } + } + + /// The stated reason, for a report or a console line. + pub fn reason(self) -> &'static str { + match self { + Fallback::FunctionTooLarge => "compiled code exceeds the per-function size cap", + Fallback::ExceedsPageBudget => "compiled code exceeds the page's code budget", + Fallback::EmptyBlob => "the back end produced no code", + Fallback::CompileQuotaExhausted => "the page's compilation quota is used up", + Fallback::TooManyFunctions => "too many functions to track", + Fallback::BackEndRefused => "the back end has no translation for the function", + Fallback::StaleVersion => "the function changed during compilation", + } + } +} + +/// `*counter + by`, stopping at the maximum. +fn bump(counter: &mut u64, by: u64) { + *counter = counter.saturating_add(by); +} + +/// The measurement counters (J.11, "measurement"). All saturate, so a hostile +/// page cannot make them wrap. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Stats { + /// Compilations whose blob reached the cache or was refused by it. + pub compiles: u64, + /// Nanoseconds spent compiling, as the caller measured them. + pub compile_nanos: u64, + /// Bytes of code emitted over the page's life (accepted blobs only). + pub code_bytes_emitted: u64, + /// The most code bytes resident at once. + pub peak_resident_bytes: u64, + /// Blobs evicted for capacity. + pub evictions: u64, + /// Bytes of code evicted for capacity. + pub evicted_bytes: u64, + /// Blobs replaced by a new blob for the same function version. + pub replacements: u64, + /// Blobs dropped because their function changed. + pub invalidations: u64, + /// Entries that found compiled code. + pub hits: u64, + /// Entries that did not (they ran in the interpreter). + pub misses: u64, + /// Refusals, by [`Fallback::index`]. + pub fallbacks: [u64; Fallback::COUNT], +} + +impl Stats { + /// How many times this reason was given. + pub fn fallback_count(&self, why: Fallback) -> u64 { + self.fallbacks.get(why.index()).copied().unwrap_or(0) + } + + /// The total number of refusals. + pub fn total_fallbacks(&self) -> u64 { + self.fallbacks + .iter() + .fold(0u64, |a, b| a.saturating_add(*b)) + } + + pub(crate) fn note_fallback(&mut self, why: Fallback) { + if let Some(c) = self.fallbacks.get_mut(why.index()) { + bump(c, 1); + } + } + + pub(crate) fn note_compile(&mut self, bytes: usize, nanos: u64) { + bump(&mut self.compiles, 1); + bump(&mut self.compile_nanos, nanos); + bump(&mut self.code_bytes_emitted, bytes as u64); + } + + pub(crate) fn note_eviction(&mut self, bytes: usize) { + bump(&mut self.evictions, 1); + bump(&mut self.evicted_bytes, bytes as u64); + } + + pub(crate) fn note_replacement(&mut self) { + bump(&mut self.replacements, 1); + } + + pub(crate) fn note_invalidation(&mut self) { + bump(&mut self.invalidations, 1); + } + + pub(crate) fn note_hit(&mut self) { + bump(&mut self.hits, 1); + } + + pub(crate) fn note_miss(&mut self) { + bump(&mut self.misses, 1); + } + + pub(crate) fn note_resident(&mut self, resident: usize) { + self.peak_resident_bytes = self.peak_resident_bytes.max(resident as u64); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn fallback_index_matches_all() { + for (i, f) in Fallback::ALL.iter().enumerate() { + assert_eq!(f.index(), i); + assert!(!f.reason().is_empty()); + } + assert_eq!(Fallback::ALL.len(), Fallback::COUNT); + } + + #[test] + fn counters_saturate() { + let mut s = Stats { + compile_nanos: u64::MAX - 1, + ..Stats::default() + }; + s.note_compile(10, 5); + assert_eq!(s.compile_nanos, u64::MAX); + s.note_fallback(Fallback::EmptyBlob); + assert_eq!(s.fallback_count(Fallback::EmptyBlob), 1); + assert_eq!(s.total_fallbacks(), 1); + } + + #[test] + fn default_function_cap_is_the_backend_cap() { + assert_eq!( + Limits::default().max_function_bytes, + 4 * crate::a64::cfemit::MAX_WORDS + ); + assert_eq!(BACKEND_MAX_BYTES, 4 * crate::a64::cfemit::MAX_WORDS); + } +} diff --git a/crates/clazel-jit/src/memlemmas.rs b/crates/clazel-jit/src/memlemmas.rs new file mode 100644 index 000000000..b073f250e --- /dev/null +++ b/crates/clazel-jit/src/memlemmas.rs @@ -0,0 +1,178 @@ +//! Facts about `clazel-x64`'s byte-addressed memory (`read_mem`, +//! `write_mem`), and about the 8-byte frame slots built on them. They are +//! what every template's proof uses to say "this store changed this slot and +//! no other byte". + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The concrete values of the powers of two the proofs use. +pub proof fn lemma_p2_consts() + ensures + x64::p2(8) == 256, + x64::p2(32) == 0x1_0000_0000, + x64::p2(47) == 0x8000_0000_0000, + x64::p2(63) == 0x8000_0000_0000_0000, + x64::p2(64) == 0x1_0000_0000_0000_0000, +{ + vstd::arithmetic::power2::lemma2_to64(); + vstd::arithmetic::power2::lemma2_to64_rest(); +} + +/// A write that succeeds touches only the bytes it writes, and maps nothing +/// new: the domain is unchanged and every byte outside `[addr, addr + n)` keeps +/// its value. +pub proof fn lemma_write_mem_frame(m: Map, addr: int, n: nat, v: int, m2: Map) + requires + x64::write_mem(m, addr, n, v) == Some(m2), + ensures + m2.dom() =~= m.dom(), + forall|b: int| + #![trigger m2.dom().contains(b)] + m.dom().contains(b) && (b < addr || addr + n <= b) ==> m2[b] == m[b], + decreases n, +{ + if n > 0 { + match x64::write_mem(m, addr + 1, (n - 1) as nat, v / 256) { + Some(m3) => { + lemma_write_mem_frame(m, addr + 1, (n - 1) as nat, v / 256, m3); + assert(m2 == m3.insert(addr, (v % 256) as u8)); + assert(m.dom().contains(addr)); + assert(m2.dom() =~= m.dom()); + }, + None => {}, + } + } +} + +/// A write succeeds when every byte it writes is mapped. +pub proof fn lemma_write_mem_some(m: Map, addr: int, n: nat, v: int) + requires + forall|i: int| 0 <= i < n ==> m.dom().contains(#[trigger] (addr + i)), + ensures + x64::write_mem(m, addr, n, v) is Some, + decreases n, +{ + if n > 0 { + assert(m.dom().contains(addr + 0)); + assert forall|i: int| 0 <= i < n - 1 implies m.dom().contains(#[trigger] (addr + 1 + i)) by { + assert(m.dom().contains(addr + (i + 1))); + } + lemma_write_mem_some(m, addr + 1, (n - 1) as nat, v / 256); + } +} + +/// Inserting at an address outside the range read does not change the read. +pub proof fn lemma_read_mem_insert_other(m: Map, a: int, n: nat, b: int, x: u8) + requires + b < a || a + n <= b, + ensures + x64::read_mem(m.insert(b, x), a, n) == x64::read_mem(m, a, n), + decreases n, +{ + if n > 0 { + lemma_read_mem_insert_other(m, a + 1, (n - 1) as nat, b, x); + assert(m.insert(b, x).dom().contains(a) == m.dom().contains(a)); + if m.dom().contains(a) { + assert(m.insert(b, x)[a] == m[a]); + } + } +} + +/// Two memories that agree on every byte of a range read the same there. +pub proof fn lemma_read_mem_congr(m: Map, m2: Map, a: int, n: nat) + requires + forall|i: int| + 0 <= i < n ==> (m.dom().contains(#[trigger] (a + i)) == m2.dom().contains(a + i)) && ( + m.dom().contains(a + i) ==> m[a + i] == m2[a + i]), + ensures + x64::read_mem(m, a, n) == x64::read_mem(m2, a, n), + decreases n, +{ + if n > 0 { + assert forall|i: int| 0 <= i < n - 1 implies (m.dom().contains(#[trigger] (a + 1 + i)) + == m2.dom().contains(a + 1 + i)) && (m.dom().contains(a + 1 + i) ==> m[a + 1 + i] + == m2[a + 1 + i]) by { + assert(a + (i + 1) == a + 1 + i); + } + lemma_read_mem_congr(m, m2, a + 1, (n - 1) as nat); + assert(a + 0 == a); + } +} + +/// Reading back what was just written: the `n` bytes at `addr` read as `v`, +/// for `v` below `2^(8n)`. +pub proof fn lemma_read_after_write_same( + m: Map, + addr: int, + n: nat, + v: int, + m2: Map, +) + requires + x64::write_mem(m, addr, n, v) == Some(m2), + 0 <= v < vstd::arithmetic::power2::pow2(8 * n) as int, + ensures + x64::read_mem(m2, addr, n) == Some(v), + decreases n, +{ + lemma_p2_consts(); + if n == 0 { + vstd::arithmetic::power2::lemma2_to64(); + assert(v == 0); + } else { + match x64::write_mem(m, addr + 1, (n - 1) as nat, v / 256) { + Some(m3) => { + let n1 = (n - 1) as nat; + assert(vstd::arithmetic::power2::pow2(8 * n) == 256 * vstd::arithmetic::power2::pow2( + 8 * n1, + )) by { + vstd::arithmetic::power2::lemma_pow2_adds(8, 8 * n1); + assert(8 * n == 8 + 8 * n1); + } + assert(0 <= v / 256 < vstd::arithmetic::power2::pow2(8 * n1) as int) by { + vstd::arithmetic::power2::lemma_pow2_pos(8 * n1); + } + lemma_read_after_write_same(m, addr + 1, (n - 1) as nat, v / 256, m3); + assert(m2 == m3.insert(addr, (v % 256) as u8)); + lemma_write_mem_frame(m, addr + 1, (n - 1) as nat, v / 256, m3); + lemma_read_mem_insert_other(m3, addr + 1, (n - 1) as nat, addr, (v % 256) as u8); + assert(m3.dom().contains(addr)); + assert(m2.dom().contains(addr)); + assert(m2[addr] == (v % 256) as u8); + assert(v == v % 256 + 256 * (v / 256)); + }, + None => {}, + } + } +} + +/// A read of a range disjoint from a successful write sees the old bytes. +pub proof fn lemma_read_after_write_disjoint( + m: Map, + addr: int, + n: nat, + v: int, + m2: Map, + b: int, + k: nat, +) + requires + x64::write_mem(m, addr, n, v) == Some(m2), + b + k <= addr || addr + n <= b, + ensures + x64::read_mem(m2, b, k) == x64::read_mem(m, b, k), +{ + lemma_write_mem_frame(m, addr, n, v, m2); + assert forall|i: int| 0 <= i < k implies (m.dom().contains(#[trigger] (b + i)) + == m2.dom().contains(b + i)) && (m.dom().contains(b + i) ==> m[b + i] == m2[b + i]) by { + assert(m2.dom().contains(b + i) == m.dom().contains(b + i)); + } + lemma_read_mem_congr(m, m2, b, k); +} + +} // verus! diff --git a/crates/clazel-jit/src/mid.rs b/crates/clazel-jit/src/mid.rs new file mode 100644 index 000000000..e729eaa3b --- /dev/null +++ b/crates/clazel-jit/src/mid.rs @@ -0,0 +1,531 @@ +//! The register-only middle of each template: from a machine state whose +//! scratch registers hold the operands, running the template's computation +//! leaves the LIR result in `rax` ([`mid_post`]). One lemma per shape, each a +//! chain of the single-step lemmas of [`crate::steps`], [`crate::alu`] and +//! [`crate::flags`]. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The integer operators with a one-instruction middle. +pub open spec fn int_op_ok(o: lir::IBin) -> bool { + ibin_supported(o) +} + +/// Writing a register with a 32-bit result leaves the zero-extended word, and +/// changes that register only. +pub proof fn lemma_set_rgfi(m: x64::State, sz: x64::Size, dst: u8, v: int) + requires + m.rgf.len() == 16, + dst < 16, + 0 <= v, + sz is W32 ==> v < 0x1_0000_0000, + v < 0x1_0000_0000_0000_0000, + ensures + x64::set_rgfi_size(m, sz, dst, v).rgf == m.rgf.update(dst as int, v as u64), +{ + super::memlemmas::lemma_p2_consts(); + assert(x64::loghead(32, v) == v || sz is W64); + assert(x64::loghead(64, v) == v); +} + +/// `op rax, rcx` for the integer operators of the first slice, 32-bit: leaves +/// LIR's result (`ibin32`) in `rax`. +#[verifier::rlimit(1000)] +pub proof fn lemma_mid_int32(cu: spec_fn(nat) -> nat, s: x64::State, o: lir::IBin, x: u32, y: u32) + requires + x64::wf(s), + int_op_ok(o), + s.rgf[0] == x as u64, + s.rgf[1] == y as u64, + x64::canonical(s.rip + x64::instr_len(super::templates::int_op_instr(o, x64::Size::W32))), + ensures + run_seq(cu, s, seq![super::templates::int_op_instr(o, x64::Size::W32)]) is Some, + super::alu::mid_post( + s, + run_seq(cu, s, seq![super::templates::int_op_instr(o, x64::Size::W32)]).unwrap(), + rax(), + lir::ibin32(o, x, y) as nat, + code_len(seq![super::templates::int_op_instr(o, x64::Size::W32)]), + ), +{ + super::memlemmas::lemma_p2_consts(); + let i = super::templates::int_op_instr(o, x64::Size::W32); + super::runseq::lemma_run_seq_one(cu, s, i); + super::runseq::lemma_code_len_one(i); + assert(x64::rgfi_size(s, x64::Size::W32, 0) == x as int); + assert(x64::rgfi_size(s, x64::Size::W32, 1) == y as int); + let m1 = x64::step(cu, s, i).unwrap(); + if super::templates::alu_of(o) is Some { + let alu = super::templates::alu_of(o).unwrap(); + super::alu::lemma_step_alu_rr(cu, s, alu, x64::Size::W32, 0, 1); + super::alu::lemma_val32_alu(o, x, y, s.rflags); + let r = x64::gpr_arith_logic_spec(32, alu, x as int, y as int, s.rflags); + lemma_set_rgfi(s, x64::Size::W32, 0, r.result); + } else if super::templates::shift_of(o) is Some { + let sh = super::templates::shift_of(o).unwrap(); + super::alu::lemma_step_shift_cl(cu, s, sh, x64::Size::W32, 0); + super::alu::lemma_val32_shift(o, x, y, s.rflags); + assert(x64::rr64(s, 1) == y as int); + let cnt = x64::shift_count(x64::Size::W32, x64::loghead(8, y as int)); + let res = x64::shift_spec(sh, 32, x as int, cnt, s.rflags).result; + lemma_set_rgfi(s, x64::Size::W32, 0, res); + } else { + assert(o == lir::IBin::Mul); + super::alu::lemma_step_imul_rr(cu, s, x64::Size::W32, 0, 1); + super::alu::lemma_val32_mul(x, y); + let (low, cf) = x64::imul_spec(32, y as int, x as int); + lemma_set_rgfi(s, x64::Size::W32, 0, low); + } +} + +/// `op rax, rcx` for the integer operators of the first slice, 64-bit. +#[verifier::rlimit(1000)] +pub proof fn lemma_mid_int64(cu: spec_fn(nat) -> nat, s: x64::State, o: lir::IBin, x: u64, y: u64) + requires + x64::wf(s), + int_op_ok(o), + s.rgf[0] == x, + s.rgf[1] == y, + x64::canonical(s.rip + x64::instr_len(super::templates::int_op_instr(o, x64::Size::W64))), + ensures + run_seq(cu, s, seq![super::templates::int_op_instr(o, x64::Size::W64)]) is Some, + super::alu::mid_post( + s, + run_seq(cu, s, seq![super::templates::int_op_instr(o, x64::Size::W64)]).unwrap(), + rax(), + lir::ibin64(o, x, y) as nat, + code_len(seq![super::templates::int_op_instr(o, x64::Size::W64)]), + ), +{ + super::memlemmas::lemma_p2_consts(); + let i = super::templates::int_op_instr(o, x64::Size::W64); + super::runseq::lemma_run_seq_one(cu, s, i); + super::runseq::lemma_code_len_one(i); + assert(x64::rgfi_size(s, x64::Size::W64, 0) == x as int); + assert(x64::rgfi_size(s, x64::Size::W64, 1) == y as int); + let m1 = x64::step(cu, s, i).unwrap(); + if super::templates::alu_of(o) is Some { + let alu = super::templates::alu_of(o).unwrap(); + super::alu::lemma_step_alu_rr(cu, s, alu, x64::Size::W64, 0, 1); + super::alu::lemma_val64_alu(o, x, y, s.rflags); + let r = x64::gpr_arith_logic_spec(64, alu, x as int, y as int, s.rflags); + lemma_set_rgfi(s, x64::Size::W64, 0, r.result); + } else if super::templates::shift_of(o) is Some { + let sh = super::templates::shift_of(o).unwrap(); + super::alu::lemma_step_shift_cl(cu, s, sh, x64::Size::W64, 0); + super::alu::lemma_val64_shift(o, x, y, s.rflags); + assert(x64::rr64(s, 1) == y as int); + let cnt = x64::shift_count(x64::Size::W64, x64::loghead(8, y as int)); + let res = x64::shift_spec(sh, 64, x as int, cnt, s.rflags).result; + lemma_set_rgfi(s, x64::Size::W64, 0, res); + } else { + assert(o == lir::IBin::Mul); + super::alu::lemma_step_imul_rr(cu, s, x64::Size::W64, 0, 1); + super::alu::lemma_val64_mul(x, y); + let (low, cf) = x64::imul_spec(64, y as int, x as int); + lemma_set_rgfi(s, x64::Size::W64, 0, low); + } +} + +/// A state with the same memory as a well-formed one, 16 registers of each +/// kind and a canonical `rip` is well formed. +pub proof fn lemma_wf_from(s: x64::State, s2: x64::State) + requires + x64::wf(s), + s2.rgf.len() == 16, + s2.xmm.len() == 16, + s2.mem == s.mem, + x64::canonical(s2.rip), + ensures + x64::wf(s2), +{ +} + +/// The `rip` after some of a run's instructions is canonical, when the end of +/// the whole run is. +pub proof fn lemma_rip_between(s: x64::State, ip: int, end: int) + requires + x64::wf(s), + x64::canonical(end), + s.rip <= ip <= end, + ensures + x64::canonical(ip), +{ +} + +/// `mov eax, 0; cmp rcx, rdx; setcc al` leaves 0 or 1 in `rax`: LIR's +/// comparison result. +#[verifier::rlimit(400)] +pub proof fn lemma_mid_cmp( + cu: spec_fn(nat) -> nat, + s: x64::State, + op: lir::ICmp, + sz: x64::Size, + x: u64, + y: u64, +) + requires + x64::wf(s), + s.rgf[1] == x, + s.rgf[2] == y, + sz is W32 ==> x < 0x1_0000_0000 && y < 0x1_0000_0000, + x64::canonical(s.rip + code_len(super::templates::cmp_mid(op, sz))), + ensures + run_seq(cu, s, super::templates::cmp_mid(op, sz)) is Some, + super::alu::mid_post( + s, + run_seq(cu, s, super::templates::cmp_mid(op, sz)).unwrap(), + rax(), + lir::bool_i32(lir::icmp(op, x64::nbits(sz), x as int, y as int)) as nat, + code_len(super::templates::cmp_mid(op, sz)), + ), +{ + super::memlemmas::lemma_p2_consts(); + let n = x64::nbits(sz); + let i1 = x64::Instr::MovImm32 { dst: rax(), imm_u32: 0 }; + let i2 = x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: sz, dst: rcx(), src: rdx() }; + let i3 = x64::Instr::Setcc { cc: super::templates::cc_of(op), dst: rax() }; + super::runseq::lemma_len3(i1, i2, i3); + let total = code_len(super::templates::cmp_mid(op, sz)); + assert(super::templates::cmp_mid(op, sz) == seq![i1, i2, i3]); + // 1. mov eax, 0 + super::steps::lemma_step_imm(cu, s, i1); + let s1 = x64::State { + rgf: s.rgf.update(0, 0u32 as u64), + rip: s.rip + x64::instr_len(i1), + ..s + }; + assert(x64::step(cu, s, i1) == Some(s1)); + assert(x64::canonical(s1.rip)); + lemma_wf_from(s, s1); + // 2. cmp rcx, rdx + super::flags::lemma_step_cmp(cu, s1, sz, 1, 2); + let s2 = x64::step(cu, s1, i2).unwrap(); + assert(x64::canonical(s2.rip)); + lemma_wf_from(s, s2); + assert(s1.rgf[1] == x && s1.rgf[2] == y); + assert(x64::rgfi_size(s1, sz, 1) == x as int); + assert(x64::rgfi_size(s1, sz, 2) == y as int); + assert(x64::loghead(n, x as int) == x as int); + assert(x64::loghead(n, y as int) == y as int); + super::flags::lemma_cc_icmp(s2, op, n, x as int, y as int); + // 3. setcc al + super::flags::lemma_step_setcc(cu, s2, super::templates::cc_of(op), 0); + let s3 = x64::step(cu, s2, i3).unwrap(); + assert(s2.rgf[0] == 0); + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen3(f, s, i1, i2, i3, s1, s2, s3); + super::runseq::lemma_run_seq_is_gen(cu, s, seq![i1, i2, i3]); + assert(s3.rgf[0] as nat == lir::bool_i32(lir::icmp(op, n, x as int, y as int)) as nat); +} + +/// `mov eax, 0; test rcx, rcx; sete al` leaves `x == 0` in `rax`. +#[verifier::rlimit(100)] +pub proof fn lemma_mid_eqz(cu: spec_fn(nat) -> nat, s: x64::State, sz: x64::Size, x: u64) + requires + x64::wf(s), + s.rgf[1] == x, + sz is W32 ==> x < 0x1_0000_0000, + x64::canonical(s.rip + code_len(super::templates::eqz_mid(sz))), + ensures + run_seq(cu, s, super::templates::eqz_mid(sz)) is Some, + super::alu::mid_post( + s, + run_seq(cu, s, super::templates::eqz_mid(sz)).unwrap(), + rax(), + lir::bool_i32(x == 0) as nat, + code_len(super::templates::eqz_mid(sz)), + ), +{ + super::memlemmas::lemma_p2_consts(); + let n = x64::nbits(sz); + let i1 = x64::Instr::MovImm32 { dst: rax(), imm_u32: 0 }; + let i2 = x64::Instr::TestRR { sz: sz, a: rcx(), b: rcx() }; + let i3 = x64::Instr::Setcc { cc: 4, dst: rax() }; + super::runseq::lemma_len3(i1, i2, i3); + assert(super::templates::eqz_mid(sz) == seq![i1, i2, i3]); + super::steps::lemma_step_imm(cu, s, i1); + let s1 = x64::State { + rgf: s.rgf.update(0, 0u32 as u64), + rip: s.rip + x64::instr_len(i1), + ..s + }; + assert(x64::canonical(s1.rip)); + lemma_wf_from(s, s1); + super::flags::lemma_step_test(cu, s1, sz, 1, 1); + let s2 = x64::step(cu, s1, i2).unwrap(); + assert(x64::canonical(s2.rip)); + lemma_wf_from(s, s2); + assert(x64::rgfi_size(s1, sz, 1) == x as int); + assert(x64::loghead(n, x as int) == x as int); + assert((x & x) == x) by (bit_vector); + assert(((x as u64) & (x as u64)) as int == x as int); + super::flags::lemma_step_setcc(cu, s2, 4, 0); + let s3 = x64::step(cu, s2, i3).unwrap(); + assert(s2.rgf[0] == 0); + assert(x64::zf_bit() == 6); + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen3(f, s, i1, i2, i3, s1, s2, s3); + super::runseq::lemma_run_seq_is_gen(cu, s, seq![i1, i2, i3]); +} + +/// `mov rcx, mask; xor/and rax, rcx` leaves `x ^ mask` or `x & mask` in `rax`. +#[verifier::rlimit(100)] +pub proof fn lemma_mid_mask( + cu: spec_fn(nat) -> nat, + s: x64::State, + alu: x64::AluOp, + mask: u64, + x: u64, +) + requires + x64::wf(s), + alu is Xor || alu is And, + s.rgf[0] == x, + x64::canonical(s.rip + code_len(super::templates::mask_mid(alu, mask))), + ensures + run_seq(cu, s, super::templates::mask_mid(alu, mask)) is Some, + super::alu::mid_post( + s, + run_seq(cu, s, super::templates::mask_mid(alu, mask)).unwrap(), + rax(), + (if alu is Xor { + x ^ mask + } else { + x & mask + }) as nat, + code_len(super::templates::mask_mid(alu, mask)), + ), +{ + super::memlemmas::lemma_p2_consts(); + let i1 = x64::Instr::MovImm64 { dst: rcx(), imm_u64: mask }; + let i2 = x64::Instr::AluRR { alu: alu, sz: x64::Size::W64, dst: rax(), src: rcx() }; + super::runseq::lemma_len2(i1, i2); + assert(super::templates::mask_mid(alu, mask) == seq![i1, i2]); + super::steps::lemma_step_imm(cu, s, i1); + let s1 = x64::State { + rgf: s.rgf.update(1, mask), + rip: s.rip + x64::instr_len(i1), + ..s + }; + assert(x64::canonical(s1.rip)); + lemma_wf_from(s, s1); + super::alu::lemma_step_alu_rr(cu, s1, alu, x64::Size::W64, 0, 1); + let s2 = x64::step(cu, s1, i2).unwrap(); + assert(x64::rgfi_size(s1, x64::Size::W64, 0) == x as int); + assert(x64::rgfi_size(s1, x64::Size::W64, 1) == mask as int); + assert(x64::loghead(64, x as int) == x as int); + assert(x64::loghead(64, mask as int) == mask as int); + let r = x64::gpr_arith_logic_spec(64, alu, x as int, mask as int, s1.rflags); + if alu is Xor { + assert(r.result == ((x as u64) ^ (mask as u64)) as int); + } else { + assert(r.result == ((x as u64) & (mask as u64)) as int); + } + lemma_set_rgfi(s1, x64::Size::W64, 0, r.result); + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen2(f, s, i1, i2, s1, s2); + super::runseq::lemma_run_seq_is_gen(cu, s, seq![i1, i2]); +} + +/// `mov ecx, 0x80000000; xor rax, rcx; sub rax, rcx` sign-extends the low 32 +/// bits of `rax` to 64: LIR's `i64.extend_i32_s`. +#[verifier::rlimit(100)] +pub proof fn lemma_mid_exts(cu: spec_fn(nat) -> nat, s: x64::State, x: u32) + requires + x64::wf(s), + s.rgf[0] == x as u64, + x64::canonical(s.rip + code_len(super::templates::exts_mid())), + ensures + run_seq(cu, s, super::templates::exts_mid()) is Some, + super::alu::mid_post( + s, + run_seq(cu, s, super::templates::exts_mid()).unwrap(), + rax(), + lir::wrap(64, lir::signed_of(32, x as int)) as u64 as nat, + code_len(super::templates::exts_mid()), + ), +{ + super::memlemmas::lemma_p2_consts(); + vstd::arithmetic::power2::lemma2_to64(); + let i1 = x64::Instr::MovImm32 { dst: rcx(), imm_u32: 0x8000_0000 }; + let i2 = x64::Instr::AluRR { + alu: x64::AluOp::Xor, + sz: x64::Size::W64, + dst: rax(), + src: rcx(), + }; + let i3 = x64::Instr::AluRR { + alu: x64::AluOp::Sub, + sz: x64::Size::W64, + dst: rax(), + src: rcx(), + }; + super::runseq::lemma_len3(i1, i2, i3); + assert(super::templates::exts_mid() == seq![i1, i2, i3]); + super::steps::lemma_step_imm(cu, s, i1); + let s1 = x64::State { + rgf: s.rgf.update(1, 0x8000_0000u32 as u64), + rip: s.rip + x64::instr_len(i1), + ..s + }; + assert(x64::canonical(s1.rip)); + lemma_wf_from(s, s1); + // xor + super::alu::lemma_step_alu_rr(cu, s1, x64::AluOp::Xor, x64::Size::W64, 0, 1); + let s2 = x64::step(cu, s1, i2).unwrap(); + assert(x64::rgfi_size(s1, x64::Size::W64, 0) == x as int); + assert(x64::rgfi_size(s1, x64::Size::W64, 1) == 0x8000_0000); + assert(x64::loghead(64, x as int) == x as int); + assert(x64::loghead(64, 0x8000_0000) == 0x8000_0000); + let rx = x64::gpr_arith_logic_spec(64, x64::AluOp::Xor, x as int, 0x8000_0000, s1.rflags); + assert(rx.result == ((x as u64) ^ 0x8000_0000u64) as int); + let v: u64 = (x as u64) ^ 0x8000_0000u64; + assert(((x as u64) ^ 0x8000_0000u64) == if x < 0x8000_0000 { + (x as u64) + 0x8000_0000 + } else { + (x as u64) - 0x8000_0000 + }) by (bit_vector); + assert(v == if x < 0x8000_0000 { + (x as u64) + 0x8000_0000 + } else { + (x as u64) - 0x8000_0000 + }); + lemma_set_rgfi(s1, x64::Size::W64, 0, rx.result); + assert(s2.rgf[0] == v); + assert(s2.rgf[1] == 0x8000_0000u64); + lemma_wf_from(s, s2); + // sub + super::alu::lemma_step_alu_rr(cu, s2, x64::AluOp::Sub, x64::Size::W64, 0, 1); + let s3 = x64::step(cu, s2, i3).unwrap(); + assert(x64::rgfi_size(s2, x64::Size::W64, 0) == v as int); + assert(x64::rgfi_size(s2, x64::Size::W64, 1) == 0x8000_0000); + let rs = x64::gpr_sub_spec(64, v as int, 0x8000_0000, s2.rflags); + assert(x64::loghead(64, v as int) == v as int); + assert(x64::loghead(64, 0x8000_0000) == 0x8000_0000); + assert(v < 0x1_0000_0000); + assert(x64::logext(64, v as int) == v as int); + assert(x64::logext(64, 0x8000_0000) == 0x8000_0000); + assert(rs.result == (v as int - 0x8000_0000) % 0x1_0000_0000_0000_0000); + assert(x64::gpr_arith_logic_spec(64, x64::AluOp::Sub, v as int, 0x8000_0000, s2.rflags) + == rs); + let sg = lir::signed_of(32, x as int); + assert(lir::pow2(32) == 0x1_0000_0000); + assert(lir::pow2(32) / 2 == 0x8000_0000); + assert(sg == if x < 0x8000_0000 { + x as int + } else { + x as int - 0x1_0000_0000 + }); + assert(lir::pow2(64) == 0x1_0000_0000_0000_0000); + assert(rs.result == lir::wrap(64, sg)); + lemma_set_rgfi(s2, x64::Size::W64, 0, rs.result); + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen3(f, s, i1, i2, i3, s1, s2, s3); + super::runseq::lemma_run_seq_is_gen(cu, s, seq![i1, i2, i3]); +} + +/// `mov eax, eax` leaves the low 32 bits of `rax`: LIR's `i32.wrap_i64`. +#[verifier::rlimit(100)] +pub proof fn lemma_mid_wrap(cu: spec_fn(nat) -> nat, s: x64::State, x: u64) + requires + x64::wf(s), + s.rgf[0] == x, + x64::canonical(s.rip + code_len(super::templates::wrap_mid())), + ensures + run_seq(cu, s, super::templates::wrap_mid()) is Some, + super::alu::mid_post( + s, + run_seq(cu, s, super::templates::wrap_mid()).unwrap(), + rax(), + ((x & 0xFFFF_FFFF) as u32) as nat, + code_len(super::templates::wrap_mid()), + ), +{ + super::memlemmas::lemma_p2_consts(); + let i = x64::Instr::MovRR { sz: x64::Size::W32, dst: rax(), src: rax() }; + assert(super::templates::wrap_mid() == seq![i]); + super::runseq::lemma_run_seq_one(cu, s, i); + super::runseq::lemma_code_len_one(i); + super::steps::lemma_step_mov_rr32(cu, s, 0, 0); + assert((x & 0xFFFF_FFFFu64) as u32 as u64 == x % 0x1_0000_0000u64) by (bit_vector); +} + +/// `test edx, edx; cmove rax, rcx` leaves the first value if the condition is +/// nonzero, else the second: LIR's `select`. +#[verifier::rlimit(100)] +pub proof fn lemma_mid_select( + cu: spec_fn(nat) -> nat, + s: x64::State, + v1: u64, + v2: u64, + c: u32, +) + requires + x64::wf(s), + s.rgf[0] == v1, + s.rgf[1] == v2, + s.rgf[2] == c as u64, + x64::canonical(s.rip + code_len(super::templates::select_mid())), + ensures + run_seq(cu, s, super::templates::select_mid()) is Some, + super::alu::mid_post( + s, + run_seq(cu, s, super::templates::select_mid()).unwrap(), + rax(), + (if c != 0 { + v1 + } else { + v2 + }) as nat, + code_len(super::templates::select_mid()), + ), +{ + super::memlemmas::lemma_p2_consts(); + let i1 = x64::Instr::TestRR { sz: x64::Size::W32, a: rdx(), b: rdx() }; + let i2 = x64::Instr::Cmovcc { cc: 4, sz: x64::Size::W64, dst: rax(), src: rcx() }; + super::runseq::lemma_len2(i1, i2); + assert(super::templates::select_mid() == seq![i1, i2]); + super::flags::lemma_step_test(cu, s, x64::Size::W32, 2, 2); + let s1 = x64::step(cu, s, i1).unwrap(); + assert(x64::canonical(s1.rip)); + lemma_wf_from(s, s1); + assert(x64::rgfi_size(s, x64::Size::W32, 2) == c as int); + assert(x64::loghead(32, c as int) == c as int); + assert((c & c) == c) by (bit_vector); + assert(((c as u64) & (c as u64)) as int == c as int); + assert(x64::zf_bit() == 6); + super::flags::lemma_step_cmov64(cu, s1, 4, 0, 1); + let s2 = x64::step(cu, s1, i2).unwrap(); + let f = super::runseq::stepper(cu); + super::runseq::lemma_gen2(f, s, i1, i2, s1, s2); + super::runseq::lemma_run_seq_is_gen(cu, s, seq![i1, i2]); +} + +/// A constant's load puts its word in `rax`. +pub proof fn lemma_mid_const(cu: spec_fn(nat) -> nat, s: x64::State, v: lir::Val) + requires + x64::wf(s), + x64::canonical(s.rip + code_len(seq![super::templates::const_instr(v)])), + ensures + run_seq(cu, s, seq![super::templates::const_instr(v)]) is Some, + super::alu::mid_post( + s, + run_seq(cu, s, seq![super::templates::const_instr(v)]).unwrap(), + rax(), + lir::val_bits(v), + code_len(seq![super::templates::const_instr(v)]), + ), +{ + let i = super::templates::const_instr(v); + super::runseq::lemma_run_seq_one(cu, s, i); + super::runseq::lemma_code_len_one(i); + super::steps::lemma_step_imm(cu, s, i); +} + +} // verus! diff --git a/crates/clazel-jit/src/proofs.rs b/crates/clazel-jit/src/proofs.rs new file mode 100644 index 000000000..ff47291f8 --- /dev/null +++ b/crates/clazel-jit/src/proofs.rs @@ -0,0 +1,1407 @@ +//! **The per-template refinement proofs.** For each instruction of the first +//! slice, [`lemma_op_refines`] proves [`refines`] of its template +//! ([`tmpl`]): running the x86-64 code from a related state reaches a state +//! related to what LIR's [`lir::eval`] says the instruction does. +//! +//! Each case unpacks LIR's semantics for the instruction (the operand +//! values, the new state), shows which slots change ([`crate::agree`]), proves +//! the template's register-only middle correct ([`crate::mid`]), and applies +//! the skeleton ([`crate::skel`]). + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The common hypotheses of a case: the LIR state has height `h`, the machine +/// state is related to it, the code ends at a canonical address, and LIR +/// falls through to `a2`. +pub open spec fn case_pre( + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + code: Seq, + i: lir::Instr, + fuel: nat, +) -> bool { + a.stack.len() == h && rel(lay, a, m, nl) && x64::canonical(m.rip + code_len(code)) + && lir::eval(lay, i, a, fuel) == lir::Outcome::Normal(a2) +} + +/// A word in a slot, as the `u64` register value. +pub proof fn lemma_bits_u64(v: lir::Val) + ensures + lir::val_bits(v) < 0x1_0000_0000_0000_0000, + v is I32 ==> lir::val_bits(v) < 0x1_0000_0000, +{ + match v { + lir::Val::I32(x) => {}, + lir::Val::I64(x) => {}, + lir::Val::F64(x) => {}, + } +} + +// ---------------------------------------------------------------------------- +// Constants, locals, drop and nop +// ---------------------------------------------------------------------------- + +/// `t.const v`. +#[verifier::rlimit(200)] +pub proof fn lemma_case_const( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + v: lir::Val, + fuel: nat, +) + requires + op_ok(Op::Const(v), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Const(v), nl, h), Op::Const(v)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Const(v), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Const(v), nl, h)), + ), +{ + assert(a2 == a.with_stack(a.stack.push(v))); + super::agree::lemma_agree_push(a, a2, nl, h, v); + let mid = seq![super::templates::const_instr(v)]; + let ls = Seq::<(u8, nat)>::empty(); + assert(super::templates::tmpl(Op::Const(v), nl, h) == super::templates::shape(ls, mid, nl + h)); + assert(super::skel::loads_ok(ls, nl, a.stack.len())); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, lir::val_bits(v))) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + lir::val_bits(v), + s, + ) by { + super::mid::lemma_mid_const(cu, s, v); + } + } + super::steps::lemma_abi_consts(); + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, nl + h, lir::val_bits(v)); +} + +/// `local.get x`. +#[verifier::rlimit(200)] +pub proof fn lemma_case_local_get( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + x: usize, + fuel: nat, +) + requires + op_ok(Op::LocalGet(x), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::LocalGet(x), nl, h), Op::LocalGet(x)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::LocalGet(x), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::LocalGet(x), nl, h)), + ), +{ + let xn = x as nat; + assert(xn < a.locals.len()); + let v = a.locals[xn as int]; + assert(a2 == a.with_stack(a.stack.push(v))); + super::agree::lemma_agree_push(a, a2, nl, h, v); + let mid = Seq::::empty(); + let ls = seq![(rax(), xn)]; + assert(super::templates::tmpl(Op::LocalGet(x), nl, h) == super::templates::shape(ls, mid, nl + h)); + assert(slot_val(a, nl, xn) == v); + assert(super::skel::loads_ok(ls, nl, a.stack.len())) by { + assert(super::rel::scratch(rax())) by { + super::steps::lemma_abi_consts(); + } + assert forall|k: int| 0 <= k < ls.len() implies super::rel::scratch((#[trigger] ls[k]).0) && ls[k].1 + < nl + a.stack.len() by { + assert(k == 0); + } + } + assert(super::skel::mid_prop(cu, a, nl, ls, mid, lir::val_bits(v))) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + lir::val_bits(v), + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == lir::val_bits(slot_val(a, nl, xn))); + assert(code_len(mid) == 0); + assert(run_seq(cu, s, mid) == Some(s)); + } + } + super::steps::lemma_abi_consts(); + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, nl + h, lir::val_bits(v)); +} + +/// `local.set x` and `local.tee x`: the top's word goes to local `x`. +#[verifier::rlimit(200)] +pub proof fn lemma_case_local_set_tee( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + op: Op, + x: usize, + fuel: nat, +) + requires + op == Op::LocalSet(x) || op == Op::LocalTee(x), + op_ok(op, nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(op, nl, h), op@, fuel), + ensures + super::contract::refines_post(lay, a2, m, nl, super::templates::tmpl(op, nl, h), run_seq(cu, m, super::templates::tmpl(op, nl, h))), +{ + let xn = x as nat; + let top = (nl + h - 1) as nat; + if op == Op::LocalSet(x) { + assert(a2 == (lir::St { + stack: a.stack.drop_last(), + locals: a.locals.update(xn as int, a.stack.last()), + mem: a.mem, + })); + super::agree::lemma_agree_set(a, a2, nl, h, xn); + } else { + assert(a2 == a.with_locals(a.locals.update(xn as int, a.stack.last()))); + super::agree::lemma_agree_tee(a, a2, nl, h, xn); + } + let mid = Seq::::empty(); + let ls = seq![(rax(), top)]; + let w = lir::val_bits(slot_val(a, nl, top)); + assert(super::templates::tmpl(op, nl, h) == super::templates::shape(ls, mid, xn)); + super::steps::lemma_abi_consts(); + assert(super::skel::loads_ok(ls, nl, a.stack.len())) by { + assert(super::rel::scratch(rax())); + assert forall|k: int| 0 <= k < ls.len() implies super::rel::scratch((#[trigger] ls[k]).0) && ls[k].1 + < nl + a.stack.len() by { + assert(k == 0); + } + } + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal(cu, mid, w, s) by { + assert(s.rgf[ls[0].0 as int] as nat == lir::val_bits(slot_val(a, nl, top))); + assert(code_len(mid) == 0); + assert(run_seq(cu, s, mid) == Some(s)); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, xn, w); +} + +/// `drop` and `nop`: no code. LIR changes only the stack, and the machine's +/// frame already holds every slot that is left. +pub proof fn lemma_case_drop_nop( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + op: Op, + fuel: nat, +) + requires + op is Drop || op is Nop, + op_ok(op, nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(op, nl, h), op@, fuel), + ensures + super::contract::refines_post(lay, a2, m, nl, super::templates::tmpl(op, nl, h), run_seq(cu, m, super::templates::tmpl(op, nl, h))), +{ + assert(super::templates::tmpl(op, nl, h) == Seq::::empty()); + assert(run_seq(cu, m, super::templates::tmpl(op, nl, h)) == Some(m)); + assert(code_len(super::templates::tmpl(op, nl, h)) == 0); + if op is Drop { + assert(a2 == a.with_stack(a.stack.drop_last())); + super::agree::lemma_agree_drop(a, a2, nl, h); + } else { + assert(a2 == a); + super::agree::lemma_agree_refl(a, nl); + } + super::rel::lemma_rel_agree(lay, a, a2, m, nl); + super::agree::lemma_preserved_refl(m); +} + +// ---------------------------------------------------------------------------- +// Loads +// ---------------------------------------------------------------------------- + +/// `rax`, `rcx` and `rdx` are scratch registers. +pub proof fn lemma_scratch_regs() + ensures + super::rel::scratch(rax()), + super::rel::scratch(rcx()), + super::rel::scratch(rdx()), + rax() != rcx() && rax() != rdx() && rcx() != rdx(), +{ + super::steps::lemma_abi_consts(); +} + +/// One load of a scratch register from an existing slot. +pub proof fn lemma_loads_ok1(r: u8, j: nat, nl: nat, n: nat) + requires + super::rel::scratch(r), + j < nl + n, + ensures + super::skel::loads_ok(seq![(r, j)], nl, n), +{ + let ls = seq![(r, j)]; + assert forall|k: int| 0 <= k < ls.len() implies super::rel::scratch((#[trigger] ls[k]).0) && ls[k].1 < nl + + n by { + assert(k == 0); + } +} + +/// Two loads into distinct scratch registers from existing slots. +pub proof fn lemma_loads_ok2(r1: u8, j1: nat, r2: u8, j2: nat, nl: nat, n: nat) + requires + super::rel::scratch(r1), + super::rel::scratch(r2), + r1 != r2, + j1 < nl + n, + j2 < nl + n, + ensures + super::skel::loads_ok(seq![(r1, j1), (r2, j2)], nl, n), +{ + let ls = seq![(r1, j1), (r2, j2)]; + assert forall|k: int| 0 <= k < ls.len() implies super::rel::scratch((#[trigger] ls[k]).0) && ls[k].1 < nl + + n by { + assert(k == 0 || k == 1); + } + assert forall|k1: int, k2: int| + 0 <= k1 < k2 < ls.len() implies (#[trigger] ls[k1]).0 != (#[trigger] ls[k2]).0 by { + assert(k1 == 0 && k2 == 1); + } +} + +/// Three loads into distinct scratch registers from existing slots. +pub proof fn lemma_loads_ok3( + r1: u8, + j1: nat, + r2: u8, + j2: nat, + r3: u8, + j3: nat, + nl: nat, + n: nat, +) + requires + super::rel::scratch(r1), + super::rel::scratch(r2), + super::rel::scratch(r3), + r1 != r2 && r1 != r3 && r2 != r3, + j1 < nl + n, + j2 < nl + n, + j3 < nl + n, + ensures + super::skel::loads_ok(seq![(r1, j1), (r2, j2), (r3, j3)], nl, n), +{ + let ls = seq![(r1, j1), (r2, j2), (r3, j3)]; + assert forall|k: int| 0 <= k < ls.len() implies super::rel::scratch((#[trigger] ls[k]).0) && ls[k].1 < nl + + n by { + assert(k == 0 || k == 1 || k == 2); + } + assert forall|k1: int, k2: int| + 0 <= k1 < k2 < ls.len() implies (#[trigger] ls[k1]).0 != (#[trigger] ls[k2]).0 by { + assert(k1 == 0 || k1 == 1); + } +} + +// ---------------------------------------------------------------------------- +// select +// ---------------------------------------------------------------------------- + +/// `select`: `c == 0` picks the second value. +#[verifier::rlimit(300)] +pub proof fn lemma_case_select( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Select, nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Select, nl, h), Op::Select@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Select, nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Select, nl, h)), + ), +{ + lemma_scratch_regs(); + let t = (nl + h - 3) as nat; + let v1 = a.stack[(h - 3) as int]; + let v2 = a.stack[(h - 2) as int]; + match a.stack[(h - 1) as int] { + lir::Val::I32(c) => { + let r = if c != 0 { + v1 + } else { + v2 + }; + assert(a2 == a.with_stack(lir::below(a.stack, 3).push(r))); + super::agree::lemma_agree_replace(a, a2, nl, h, 3, r); + let ls = seq![(rax(), t), (rcx(), t + 1), (rdx(), t + 2)]; + let mid = super::templates::select_mid(); + let w = lir::val_bits(r); + assert(super::templates::tmpl(Op::Select, nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok3(rax(), t, rcx(), t + 1, rdx(), t + 2, nl, a.stack.len()); + assert(slot_val(a, nl, t) == v1); + assert(slot_val(a, nl, t + 1) == v2); + assert(slot_val(a, nl, t + 2) == lir::Val::I32(c)); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == lir::val_bits(v1)); + assert(s.rgf[ls[1].0 as int] as nat == lir::val_bits(v2)); + assert(s.rgf[ls[2].0 as int] as nat == c as nat); + super::mid::lemma_mid_select( + cu, + s, + lir::val_bits(v1) as u64, + lir::val_bits(v2) as u64, + c, + ); + assert(w == (if c != 0 { + lir::val_bits(v1) + } else { + lir::val_bits(v2) + })); + } + } + super::steps::lemma_abi_consts(); + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + + +// ---------------------------------------------------------------------------- +// Binary instructions +// ---------------------------------------------------------------------------- + +/// A 32-bit integer arithmetic, logic or shift instruction. +#[verifier::rlimit(500)] +pub proof fn lemma_case_bin_i32( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + o: lir::IBin, + fuel: nat, +) + requires + op_ok(Op::Bin(lir::BinOp::I32(o)), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Bin(lir::BinOp::I32(o)), nl, h), Op::Bin(lir::BinOp::I32(o))@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Bin(lir::BinOp::I32(o)), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Bin(lir::BinOp::I32(o)), nl, h)), + ), +{ + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 2) as nat; + match (a.stack[(h - 2) as int], a.stack[(h - 1) as int]) { + (lir::Val::I32(x), lir::Val::I32(y)) => { + let v = lir::Val::I32(lir::ibin32(o, x, y)); + assert(a2 == a.with_stack(lir::below(a.stack, 2).push(v))); + super::agree::lemma_agree_replace(a, a2, nl, h, 2, v); + let ls = seq![(rax(), t), (rcx(), t + 1)]; + let mid = seq![super::templates::int_op_instr(o, x64::Size::W32)]; + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Bin(lir::BinOp::I32(o)), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok2(ls[0].0, t, ls[1].0, t + 1, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 2) as int]); + assert(slot_val(a, nl, t + 1) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == x as nat); + assert(s.rgf[ls[1].0 as int] as nat == y as nat); + assert(s.rgf[0] == x as u64); + assert(s.rgf[1] == y as u64); + super::runseq::lemma_code_len_one(super::templates::int_op_instr(o, x64::Size::W32)); + super::mid::lemma_mid_int32(cu, s, o, x, y); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// A 64-bit integer arithmetic, logic or shift instruction. +#[verifier::rlimit(500)] +pub proof fn lemma_case_bin_i64( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + o: lir::IBin, + fuel: nat, +) + requires + op_ok(Op::Bin(lir::BinOp::I64(o)), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Bin(lir::BinOp::I64(o)), nl, h), Op::Bin(lir::BinOp::I64(o))@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Bin(lir::BinOp::I64(o)), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Bin(lir::BinOp::I64(o)), nl, h)), + ), +{ + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 2) as nat; + match (a.stack[(h - 2) as int], a.stack[(h - 1) as int]) { + (lir::Val::I64(x), lir::Val::I64(y)) => { + let v = lir::Val::I64(lir::ibin64(o, x, y)); + assert(a2 == a.with_stack(lir::below(a.stack, 2).push(v))); + super::agree::lemma_agree_replace(a, a2, nl, h, 2, v); + let ls = seq![(rax(), t), (rcx(), t + 1)]; + let mid = seq![super::templates::int_op_instr(o, x64::Size::W64)]; + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Bin(lir::BinOp::I64(o)), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok2(ls[0].0, t, ls[1].0, t + 1, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 2) as int]); + assert(slot_val(a, nl, t + 1) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == x as nat); + assert(s.rgf[ls[1].0 as int] as nat == y as nat); + assert(s.rgf[0] == x); + assert(s.rgf[1] == y); + super::runseq::lemma_code_len_one(super::templates::int_op_instr(o, x64::Size::W64)); + super::mid::lemma_mid_int64(cu, s, o, x, y); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// A 32-bit integer comparison. +#[verifier::rlimit(500)] +pub proof fn lemma_case_cmp_i32( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + c: lir::ICmp, + fuel: nat, +) + requires + op_ok(Op::Bin(lir::BinOp::I32Cmp(c)), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Bin(lir::BinOp::I32Cmp(c)), nl, h), Op::Bin(lir::BinOp::I32Cmp(c))@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Bin(lir::BinOp::I32Cmp(c)), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Bin(lir::BinOp::I32Cmp(c)), nl, h)), + ), +{ + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 2) as nat; + match (a.stack[(h - 2) as int], a.stack[(h - 1) as int]) { + (lir::Val::I32(x), lir::Val::I32(y)) => { + let v = lir::Val::I32(lir::bool_i32(lir::icmp(c, 32, x as int, y as int))); + assert(a2 == a.with_stack(lir::below(a.stack, 2).push(v))); + super::agree::lemma_agree_replace(a, a2, nl, h, 2, v); + let ls = seq![(rcx(), t), (rdx(), t + 1)]; + let mid = super::templates::cmp_mid(c, x64::Size::W32); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Bin(lir::BinOp::I32Cmp(c)), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok2(ls[0].0, t, ls[1].0, t + 1, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 2) as int]); + assert(slot_val(a, nl, t + 1) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == x as nat); + assert(s.rgf[ls[1].0 as int] as nat == y as nat); + assert(s.rgf[1] == x as u64); + assert(s.rgf[2] == y as u64); + assert(x64::nbits(x64::Size::W32) == 32); + super::mid::lemma_mid_cmp(cu, s, c, x64::Size::W32, x as u64, y as u64); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// A 64-bit integer comparison. +#[verifier::rlimit(500)] +pub proof fn lemma_case_cmp_i64( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + c: lir::ICmp, + fuel: nat, +) + requires + op_ok(Op::Bin(lir::BinOp::I64Cmp(c)), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Bin(lir::BinOp::I64Cmp(c)), nl, h), Op::Bin(lir::BinOp::I64Cmp(c))@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Bin(lir::BinOp::I64Cmp(c)), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Bin(lir::BinOp::I64Cmp(c)), nl, h)), + ), +{ + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 2) as nat; + match (a.stack[(h - 2) as int], a.stack[(h - 1) as int]) { + (lir::Val::I64(x), lir::Val::I64(y)) => { + let v = lir::Val::I32(lir::bool_i32(lir::icmp(c, 64, x as int, y as int))); + assert(a2 == a.with_stack(lir::below(a.stack, 2).push(v))); + super::agree::lemma_agree_replace(a, a2, nl, h, 2, v); + let ls = seq![(rcx(), t), (rdx(), t + 1)]; + let mid = super::templates::cmp_mid(c, x64::Size::W64); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Bin(lir::BinOp::I64Cmp(c)), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok2(ls[0].0, t, ls[1].0, t + 1, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 2) as int]); + assert(slot_val(a, nl, t + 1) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == x as nat); + assert(s.rgf[ls[1].0 as int] as nat == y as nat); + assert(s.rgf[1] == x); + assert(s.rgf[2] == y); + assert(x64::nbits(x64::Size::W64) == 64); + super::mid::lemma_mid_cmp(cu, s, c, x64::Size::W64, x, y); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `i32.eqz`. +#[verifier::rlimit(500)] +pub proof fn lemma_case_eqz_i32( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::I32Eqz), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::I32Eqz), nl, h), Op::Un(lir::UnOp::I32Eqz)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::I32Eqz), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::I32Eqz), nl, h)), + ), +{ + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 1) as nat; + match a.stack[(h - 1) as int] { + lir::Val::I32(x) => { + let v = lir::Val::I32(lir::bool_i32(x == 0)); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + super::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let ls = seq![(rcx(), t)]; + let mid = super::templates::eqz_mid(x64::Size::W32); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Un(lir::UnOp::I32Eqz), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(ls[0].0, t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == x as nat); + assert(s.rgf[1] == x as u64); + super::mid::lemma_mid_eqz(cu, s, x64::Size::W32, x as u64); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `i64.eqz`. +#[verifier::rlimit(500)] +pub proof fn lemma_case_eqz_i64( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::I64Eqz), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::I64Eqz), nl, h), Op::Un(lir::UnOp::I64Eqz)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::I64Eqz), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::I64Eqz), nl, h)), + ), +{ + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 1) as nat; + match a.stack[(h - 1) as int] { + lir::Val::I64(x) => { + let v = lir::Val::I32(lir::bool_i32(x == 0)); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + super::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let ls = seq![(rcx(), t)]; + let mid = super::templates::eqz_mid(x64::Size::W64); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Un(lir::UnOp::I64Eqz), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(ls[0].0, t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == x as nat); + assert(s.rgf[1] == x); + super::mid::lemma_mid_eqz(cu, s, x64::Size::W64, x); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `f64.neg`: flip the sign bit of the word. +#[verifier::rlimit(500)] +pub proof fn lemma_case_f64_neg( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::F64Neg), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::F64Neg), nl, h), Op::Un(lir::UnOp::F64Neg)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::F64Neg), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::F64Neg), nl, h)), + ), +{ + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 1) as nat; + match a.stack[(h - 1) as int] { + lir::Val::F64(x) => { + let v = lir::Val::F64(x ^ 0x8000_0000_0000_0000); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + super::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let ls = seq![(rax(), t)]; + let mid = super::templates::mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Un(lir::UnOp::F64Neg), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(ls[0].0, t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == x as nat); + assert(s.rgf[0] == x); + super::mid::lemma_mid_mask(cu, s, x64::AluOp::Xor, 0x8000_0000_0000_0000, x); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `f64.abs`: clear the sign bit of the word. +#[verifier::rlimit(500)] +pub proof fn lemma_case_f64_abs( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::F64Abs), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::F64Abs), nl, h), Op::Un(lir::UnOp::F64Abs)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::F64Abs), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::F64Abs), nl, h)), + ), +{ + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 1) as nat; + match a.stack[(h - 1) as int] { + lir::Val::F64(x) => { + let v = lir::Val::F64(x & 0x7FFF_FFFF_FFFF_FFFF); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + super::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let ls = seq![(rax(), t)]; + let mid = super::templates::mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Un(lir::UnOp::F64Abs), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(ls[0].0, t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == x as nat); + assert(s.rgf[0] == x); + super::mid::lemma_mid_mask(cu, s, x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF, x); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `i32.wrap_i64`. +#[verifier::rlimit(500)] +pub proof fn lemma_case_wrap( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::I32WrapI64), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::I32WrapI64), nl, h), Op::Un(lir::UnOp::I32WrapI64)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::I32WrapI64), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::I32WrapI64), nl, h)), + ), +{ + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 1) as nat; + match a.stack[(h - 1) as int] { + lir::Val::I64(x) => { + let v = lir::Val::I32((x & 0xFFFF_FFFF) as u32); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + super::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let ls = seq![(rax(), t)]; + let mid = super::templates::wrap_mid(); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Un(lir::UnOp::I32WrapI64), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(ls[0].0, t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == x as nat); + assert(s.rgf[0] == x); + super::mid::lemma_mid_wrap(cu, s, x); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `i64.extend_i32_s`. +#[verifier::rlimit(500)] +pub proof fn lemma_case_extend_s( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::I64ExtendI32S), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32S), nl, h), Op::Un(lir::UnOp::I64ExtendI32S)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32S), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32S), nl, h)), + ), +{ + lemma_scratch_regs(); + super::steps::lemma_abi_consts(); + let t = (nl + h - 1) as nat; + match a.stack[(h - 1) as int] { + lir::Val::I32(x) => { + let v = lir::Val::I64(lir::wrap(64, lir::signed_of(32, x as int)) as u64); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + super::agree::lemma_agree_replace(a, a2, nl, h, 1, v); + let ls = seq![(rax(), t)]; + let mid = super::templates::exts_mid(); + let w = lir::val_bits(v); + assert(super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32S), nl, h) == super::templates::shape(ls, mid, t)); + lemma_loads_ok1(ls[0].0, t, nl, a.stack.len()); + assert(slot_val(a, nl, t) == a.stack[(h - 1) as int]); + assert(super::skel::mid_prop(cu, a, nl, ls, mid, w)) by { + assert forall|s: x64::State| super::skel::mid_pre(a, nl, ls, mid, s) implies super::skel::mid_goal( + cu, + mid, + w, + s, + ) by { + assert(s.rgf[ls[0].0 as int] as nat == x as nat); + assert(s.rgf[0] == x as u64); + super::mid::lemma_mid_exts(cu, s, x); + } + } + super::skel::lemma_shape(cu, lay, a, a2, m, nl, ls, mid, t, w); + }, + _ => { + assert(false); + }, + } +} + +/// `i64.extend_i32_u`: the word is already zero-extended. +pub proof fn lemma_case_extend_u( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::I64ExtendI32U), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32U), nl, h), Op::Un(lir::UnOp::I64ExtendI32U)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32U), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32U), nl, h)), + ), +{ + match a.stack[(h - 1) as int] { + lir::Val::I32(x) => { + let v = lir::Val::I64(x as u64); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + assert(super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32U), nl, h) == Seq::::empty()); + assert(run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32U), nl, h)) == Some(m)); + assert(code_len(super::templates::tmpl(Op::Un(lir::UnOp::I64ExtendI32U), nl, h)) == 0); + assert(a.stack.last() == a.stack[(h - 1) as int]); + super::agree::lemma_agree_same_word(a, a2, nl, h, v); + super::rel::lemma_rel_agree(lay, a, a2, m, nl); + super::agree::lemma_preserved_refl(m); + }, + _ => { + assert(false); + }, + } +} + +/// `f64.reinterpret_i64`: the same bits. +pub proof fn lemma_case_reinterpret_i64( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::F64ReinterpretI64), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::F64ReinterpretI64), nl, h), Op::Un(lir::UnOp::F64ReinterpretI64)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::F64ReinterpretI64), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::F64ReinterpretI64), nl, h)), + ), +{ + match a.stack[(h - 1) as int] { + lir::Val::I64(x) => { + let v = lir::Val::F64(x); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + assert(super::templates::tmpl(Op::Un(lir::UnOp::F64ReinterpretI64), nl, h) == Seq::::empty()); + assert(run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::F64ReinterpretI64), nl, h)) == Some(m)); + assert(code_len(super::templates::tmpl(Op::Un(lir::UnOp::F64ReinterpretI64), nl, h)) == 0); + assert(a.stack.last() == a.stack[(h - 1) as int]); + super::agree::lemma_agree_same_word(a, a2, nl, h, v); + super::rel::lemma_rel_agree(lay, a, a2, m, nl); + super::agree::lemma_preserved_refl(m); + }, + _ => { + assert(false); + }, + } +} + +/// `i64.reinterpret_f64`: the same bits. +pub proof fn lemma_case_reinterpret_f64( + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + h: nat, + fuel: nat, +) + requires + op_ok(Op::Un(lir::UnOp::I64ReinterpretF64), nl, h), + case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(Op::Un(lir::UnOp::I64ReinterpretF64), nl, h), Op::Un(lir::UnOp::I64ReinterpretF64)@, fuel), + ensures + super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(Op::Un(lir::UnOp::I64ReinterpretF64), nl, h), + run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::I64ReinterpretF64), nl, h)), + ), +{ + match a.stack[(h - 1) as int] { + lir::Val::F64(x) => { + let v = lir::Val::I64(x); + assert(a2 == a.with_stack(lir::below(a.stack, 1).push(v))); + assert(super::templates::tmpl(Op::Un(lir::UnOp::I64ReinterpretF64), nl, h) == Seq::::empty()); + assert(run_seq(cu, m, super::templates::tmpl(Op::Un(lir::UnOp::I64ReinterpretF64), nl, h)) == Some(m)); + assert(code_len(super::templates::tmpl(Op::Un(lir::UnOp::I64ReinterpretF64), nl, h)) == 0); + assert(a.stack.last() == a.stack[(h - 1) as int]); + super::agree::lemma_agree_same_word(a, a2, nl, h, v); + super::rel::lemma_rel_agree(lay, a, a2, m, nl); + super::agree::lemma_preserved_refl(m); + }, + _ => { + assert(false); + }, + } +} + +// ---------------------------------------------------------------------------- +// Every instruction of the slice +// ---------------------------------------------------------------------------- + +/// Instantiating [`refines`] at one state. +pub proof fn lemma_use_refines( + i: lir::Instr, + nl: nat, + h: nat, + code: Seq, + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + m: x64::State, + fuel: nat, +) + requires + super::contract::refines(i, nl, h, code), + ensures + a.stack.len() == h && rel(lay, a, m, nl) && x64::canonical(m.rip + code_len(code)) + ==> match lir::eval(lay, i, a, fuel) { + lir::Outcome::Normal(a2) => super::contract::refines_post(lay, a2, m, nl, code, run_seq(cu, m, code)), + _ => true, + }, +{ +} + +/// **The refinement of every template of the slice.** An instruction with a +/// template ([`op_ok`]) is refined by it ([`refines`]). +#[verifier::rlimit(1000)] +pub proof fn lemma_op_refines(op: Op, nl: nat, h: nat) + requires + op_ok(op, nl, h), + ensures + super::contract::refines(op@, nl, h, super::templates::tmpl(op, nl, h)), +{ + assert forall| + lay: lir::Layout, + cu: spec_fn(nat) -> nat, + a: lir::St, + m: x64::State, + fuel: nat, + | + #![trigger lir::eval(lay, op@, a, fuel), run_seq(cu, m, super::templates::tmpl(op, nl, h))] + a.stack.len() == h && rel(lay, a, m, nl) && x64::canonical(m.rip + code_len(super::templates::tmpl(op, nl, h))) + implies match lir::eval(lay, op@, a, fuel) { + lir::Outcome::Normal(a2) => super::contract::refines_post( + lay, + a2, + m, + nl, + super::templates::tmpl(op, nl, h), + run_seq(cu, m, super::templates::tmpl(op, nl, h)), + ), + _ => true, + } by { + match lir::eval(lay, op@, a, fuel) { + lir::Outcome::Normal(a2) => { + assert(case_pre(lay, a, a2, m, nl, h, super::templates::tmpl(op, nl, h), op@, fuel)); + match op { + Op::Nop => lemma_case_drop_nop(lay, cu, a, a2, m, nl, h, op, fuel), + Op::Drop => lemma_case_drop_nop(lay, cu, a, a2, m, nl, h, op, fuel), + Op::Const(v) => lemma_case_const(lay, cu, a, a2, m, nl, h, v, fuel), + Op::LocalGet(x) => lemma_case_local_get(lay, cu, a, a2, m, nl, h, x, fuel), + Op::LocalSet(x) => lemma_case_local_set_tee( + lay, + cu, + a, + a2, + m, + nl, + h, + op, + x, + fuel, + ), + Op::LocalTee(x) => lemma_case_local_set_tee( + lay, + cu, + a, + a2, + m, + nl, + h, + op, + x, + fuel, + ), + Op::Select => lemma_case_select(lay, cu, a, a2, m, nl, h, fuel), + Op::Bin(b) => match b { + lir::BinOp::I32(o) => lemma_case_bin_i32(lay, cu, a, a2, m, nl, h, o, fuel), + lir::BinOp::I64(o) => lemma_case_bin_i64(lay, cu, a, a2, m, nl, h, o, fuel), + lir::BinOp::I32Cmp(c) => lemma_case_cmp_i32( + lay, + cu, + a, + a2, + m, + nl, + h, + c, + fuel, + ), + lir::BinOp::I64Cmp(c) => lemma_case_cmp_i64( + lay, + cu, + a, + a2, + m, + nl, + h, + c, + fuel, + ), + lir::BinOp::F64(_) => { + assert(false); + }, + lir::BinOp::F64Cmp(_) => { + assert(false); + }, + }, + Op::Un(u) => match u { + lir::UnOp::I32Eqz => lemma_case_eqz_i32(lay, cu, a, a2, m, nl, h, fuel), + lir::UnOp::I64Eqz => lemma_case_eqz_i64(lay, cu, a, a2, m, nl, h, fuel), + lir::UnOp::F64Neg => lemma_case_f64_neg(lay, cu, a, a2, m, nl, h, fuel), + lir::UnOp::F64Abs => lemma_case_f64_abs(lay, cu, a, a2, m, nl, h, fuel), + lir::UnOp::I32WrapI64 => lemma_case_wrap(lay, cu, a, a2, m, nl, h, fuel), + lir::UnOp::I64ExtendI32S => lemma_case_extend_s( + lay, + cu, + a, + a2, + m, + nl, + h, + fuel, + ), + lir::UnOp::I64ExtendI32U => lemma_case_extend_u( + lay, + cu, + a, + a2, + m, + nl, + h, + fuel, + ), + lir::UnOp::F64ReinterpretI64 => lemma_case_reinterpret_i64( + lay, + cu, + a, + a2, + m, + nl, + h, + fuel, + ), + lir::UnOp::I64ReinterpretF64 => lemma_case_reinterpret_f64( + lay, + cu, + a, + a2, + m, + nl, + h, + fuel, + ), + lir::UnOp::F64Sqrt => { + assert(false); + }, + lir::UnOp::F64ConvertI32S => { + assert(false); + }, + lir::UnOp::F64ConvertI32U => { + assert(false); + }, + lir::UnOp::F64ConvertI64S => { + assert(false); + }, + lir::UnOp::F64ConvertI64U => { + assert(false); + }, + }, + } + }, + _ => {}, + } + } +} + +/// An instruction with a template changes the operand stack's height as +/// `pops` and `pushes` say, keeps the locals' count and memory. +pub proof fn lemma_op_height( + op: Op, + nl: nat, + h: nat, + lay: lir::Layout, + a: lir::St, + a2: lir::St, + fuel: nat, +) + requires + op_ok(op, nl, h), + a.stack.len() == h, + lir::eval(lay, op@, a, fuel) == lir::Outcome::Normal(a2), + ensures + a2.stack.len() == height_after_op(op, h), +{ + assert(fuel > 0); + match op { + Op::Nop => {}, + Op::Drop => {}, + Op::Const(v) => {}, + Op::LocalGet(x) => {}, + Op::LocalSet(x) => {}, + Op::LocalTee(x) => {}, + Op::Select => { + match a.stack[(h - 1) as int] { + lir::Val::I32(c) => { + assert(a2.stack.len() == (h - 3) + 1); + }, + _ => { + assert(false); + }, + } + }, + Op::Bin(b) => { + match lir::bin_eval(b, a.stack[(h - 2) as int], a.stack[(h - 1) as int]) { + Some(v) => { + assert(a2.stack.len() == (h - 2) + 1); + }, + None => { + assert(false); + }, + } + }, + Op::Un(u) => { + match lir::un_eval(u, a.stack[(h - 1) as int]) { + Some(v) => { + assert(a2.stack.len() == (h - 1) + 1); + }, + None => { + assert(false); + }, + } + }, + } +} + +} // verus! diff --git a/crates/clazel-jit/src/rel.rs b/crates/clazel-jit/src/rel.rs new file mode 100644 index 000000000..42aeace95 --- /dev/null +++ b/crates/clazel-jit/src/rel.rs @@ -0,0 +1,229 @@ +//! Lemmas about the state relation [`rel`]: it survives a change that only +//! touches scratch registers, a load into a scratch register, and a store to +//! one frame slot (when the LIR state changes at that slot only). + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A register a template may write: not the context register, not `rsp`. +pub open spec fn scratch(r: u8) -> bool { + r < 16 && r != abi::ctx_reg() && r != abi::sp_reg() +} + +/// `rel` does not mention registers other than `r15`, nor the flags, `rip` +/// (beyond being canonical) or the XMM registers: a state with the same +/// memory and context register is related to what `m` was. +pub proof fn lemma_rel_regs( + lay: lir::Layout, + a: lir::St, + m: x64::State, + m3: x64::State, + nl: nat, +) + requires + rel(lay, a, m, nl), + m3.mem == m.mem, + m3.rgf.len() == 16, + m3.xmm.len() == 16, + m3.rgf[abi::ctx_reg() as int] == m.rgf[abi::ctx_reg() as int], + x64::canonical(m3.rip), + ensures + rel(lay, a, m3, nl), +{ + super::steps::lemma_abi_consts(); + assert(ctx_of(m3) == ctx_of(m)); + assert(x64::wf(m3)); + assert(ctx_ok(m3)); +} + +/// `mov r, [slot j]` for a scratch register `r`: loads the slot's word, and the +/// state stays related to the same LIR state. +pub proof fn lemma_load_rel( + cu: spec_fn(nat) -> nat, + lay: lir::Layout, + a: lir::St, + m: x64::State, + r: u8, + j: nat, + nl: nat, +) + requires + rel(lay, a, m, nl), + scratch(r), + j < nl + a.stack.len(), + x64::canonical(m.rip + x64::instr_len(super::templates::load_slot(r, j))), + ensures + x64::step(cu, m, super::templates::load_slot(r, j)) is Some, + ({ + let m1 = x64::step(cu, m, super::templates::load_slot(r, j)).unwrap(); + &&& m1 == x64::State { + rgf: m.rgf.update(r as int, lir::val_bits(slot_val(a, nl, j)) as u64), + rip: m.rip + x64::instr_len(super::templates::load_slot(r, j)), + ..m + } + &&& rel(lay, a, m1, nl) + &&& m1.rgf[r as int] as nat == lir::val_bits(slot_val(a, nl, j)) + }), +{ + super::steps::lemma_abi_consts(); + super::memlemmas::lemma_p2_consts(); + let v = lir::val_bits(slot_val(a, nl, j)); + assert(slot_holds(m.mem, ctx_of(m), j, v)); + assert(v < 0x1_0000_0000_0000_0000) by { + match slot_val(a, nl, j) { + lir::Val::I32(x) => {}, + lir::Val::I64(x) => {}, + lir::Val::F64(x) => {}, + } + } + assert(j < max_slots()); + super::steps::lemma_step_load_slot(cu, m, r, j, v); + let m1 = x64::step(cu, m, super::templates::load_slot(r, j)).unwrap(); + assert(m1.rgf.len() == 16); + lemma_rel_regs(lay, a, m, m1, nl); + assert(m1.rgf[r as int] == v as u64); +} + +/// The LIR state `a2` has the slots of `a` except at `ts`: same locals count, +/// same memory, a frame that fits, and every other slot holds the same word. +pub open spec fn slots_agree_except(a: lir::St, a2: lir::St, nl: nat, ts: nat) -> bool { + a2.locals.len() == nl && nl + a2.stack.len() <= max_slots() && a2.mem == a.mem && ts < nl + + a2.stack.len() && forall|j: nat| + #![trigger lir::val_bits(slot_val(a2, nl, j))] + j < nl + a2.stack.len() && j != ts ==> j < nl + a.stack.len() && lir::val_bits( + slot_val(a2, nl, j), + ) == lir::val_bits(slot_val(a, nl, j)) +} + +/// As [`slots_agree_except`], with no exception: the slots of `a2` are all +/// slots of `a`, with the same words. +pub open spec fn slots_agree(a: lir::St, a2: lir::St, nl: nat) -> bool { + a2.locals.len() == nl && nl + a2.stack.len() <= max_slots() && a2.mem == a.mem && forall| + j: nat, + | + #![trigger lir::val_bits(slot_val(a2, nl, j))] + j < nl + a2.stack.len() ==> j < nl + a.stack.len() && lir::val_bits(slot_val(a2, nl, j)) + == lir::val_bits(slot_val(a, nl, j)) +} + +/// If the slots of `a2` are slots of `a` with the same words, `a2` is related +/// to whatever `a` was. +pub proof fn lemma_rel_agree(lay: lir::Layout, a: lir::St, a2: lir::St, m: x64::State, nl: nat) + requires + rel(lay, a, m, nl), + slots_agree(a, a2, nl), + ensures + rel(lay, a2, m, nl), +{ + assert forall|j: nat| j < nl + a2.stack.len() implies #[trigger] slot_holds( + m.mem, + ctx_of(m), + j, + lir::val_bits(slot_val(a2, nl, j)), + ) by { + assert(lir::val_bits(slot_val(a2, nl, j)) >= 0); + assert(j < nl + a.stack.len()); + assert(slot_holds(m.mem, ctx_of(m), j, lir::val_bits(slot_val(a, nl, j)))); + } +} + +/// The exit area is within the context. +pub proof fn lemma_slot_in_exit_area(ctxb: int, j: nat, b: int) + requires + j < max_slots(), + ctxb + slot_disp(j) <= b < ctxb + slot_disp(j) + 8, + ensures + in_exit_area(ctxb, b), +{ + super::steps::lemma_abi_consts(); +} + +/// `mov [slot ts], rd` finishes a template: from a state `m3` related to the +/// LIR state `a` (the template's registers computed, memory untouched since +/// `m0`, where the template began), with `rd` holding the word `w` that `a2` +/// keeps in slot `ts`, the store leaves a state related to `a2`, and `m0` +/// and the result differ in the exit area only ([`preserved`]). +pub proof fn lemma_store_finish( + cu: spec_fn(nat) -> nat, + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m0: x64::State, + m3: x64::State, + nl: nat, + ts: nat, + rd: u8, + w: nat, +) + requires + rel(lay, a, m3, nl), + rel(lay, a, m0, nl), + m3.mem == m0.mem, + m3.rgf[abi::sp_reg() as int] == m0.rgf[abi::sp_reg() as int], + m3.rgf[abi::ctx_reg() as int] == m0.rgf[abi::ctx_reg() as int], + slots_agree_except(a, a2, nl, ts), + w == lir::val_bits(slot_val(a2, nl, ts)), + ts < max_slots(), + rd < 16, + m3.rgf[rd as int] as nat == w, + x64::canonical(m3.rip + x64::instr_len(super::templates::store_slot(ts, rd))), + ensures + x64::step(cu, m3, super::templates::store_slot(ts, rd)) is Some, + ({ + let m4 = x64::step(cu, m3, super::templates::store_slot(ts, rd)).unwrap(); + &&& rel(lay, a2, m4, nl) + &&& m4.rip == m3.rip + x64::instr_len(super::templates::store_slot(ts, rd)) + &&& preserved(m0, m4) + }), +{ + super::steps::lemma_abi_consts(); + let ctxb = ctx_of(m3); + assert(ctx_of(m0) == ctxb); + super::steps::lemma_step_store_slot(cu, m3, rd, ts); + let m4 = x64::step(cu, m3, super::templates::store_slot(ts, rd)).unwrap(); + assert(m4.rgf == m3.rgf && m4.xmm == m3.xmm); + assert(x64::wf(m4)) by { + assert(m4.mem.dom() =~= m3.mem.dom()); + } + assert(ctx_of(m4) == ctxb); + assert(ctx_ok(m4)); + assert(exit_area_mapped(m4.mem, ctxb)); + // every slot of a2 + assert forall|j: nat| j < nl + a2.stack.len() implies #[trigger] slot_holds( + m4.mem, + ctxb, + j, + lir::val_bits(slot_val(a2, nl, j)), + ) by { + assert(lir::val_bits(slot_val(a2, nl, j)) >= 0); + if j == ts { + } else { + assert(j < nl + a.stack.len()); + assert(slot_holds(m3.mem, ctxb, j, lir::val_bits(slot_val(a, nl, j)))); + assert(j < max_slots()); + } + } + // memory outside the exit area is untouched + assert forall|b: int| #[trigger] m0.mem.dom().contains(b) && !in_exit_area(ctxb, b) implies m4.mem[b] + == m0.mem[b] by { + if b < ctxb + slot_disp(ts) || ctxb + slot_disp(ts) + 8 <= b { + } else { + lemma_slot_in_exit_area(ctxb, ts, b); + } + } + assert(mem_rel(lay, a2.mem, m4.mem)) by { + assert forall|b: nat| #[trigger] in_regions(lay, b) implies m4.mem.dom().contains(b as int) + && m4.mem[b as int] == lir::byte_at(a2.mem, b) by { + assert(!in_exit_area(ctxb, b as int)); + assert(m3.mem.dom().contains(b as int) && m3.mem[b as int] == lir::byte_at(a.mem, b)); + assert(m0.mem.dom().contains(b as int)); + } + } + assert(preserved_mem(m0, m4)); +} + +} // verus! diff --git a/crates/clazel-jit/src/runseq.rs b/crates/clazel-jit/src/runseq.rs new file mode 100644 index 000000000..39c8b46c2 --- /dev/null +++ b/crates/clazel-jit/src/runseq.rs @@ -0,0 +1,353 @@ +//! Facts about [`run_seq`], [`code_len`] and the machine states a run passes +//! through: running a concatenation is running one list and then the other, +//! lengths add, and `rip` stays canonical along the way. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `code_len` of a concatenation. +pub proof fn lemma_code_len_append(a: Seq, b: Seq) + ensures + code_len(a + b) == code_len(a) + code_len(b), + decreases a.len(), +{ + if a.len() > 0 { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + lemma_code_len_append(a.skip(1), b); + } else { + assert(a + b =~= b); + } +} + +/// Running `a + b` is running `a`, then `b` from where `a` ended (and a +/// fault in `a` is a fault). +pub proof fn lemma_run_seq_append( + cu: spec_fn(nat) -> nat, + s: x64::State, + a: Seq, + b: Seq, +) + ensures + run_seq(cu, s, a + b) == match run_seq(cu, s, a) { + Some(s1) => run_seq(cu, s1, b), + None => None, + }, + decreases a.len(), +{ + if a.len() == 0 { + assert(a + b =~= b); + } else { + assert((a + b).skip(1) =~= a.skip(1) + b); + assert((a + b)[0] == a[0]); + match x64::step(cu, s, a[0]) { + Some(s1) => { + lemma_run_seq_append(cu, s1, a.skip(1), b); + }, + None => {}, + } + } +} + +/// A one-instruction run is the step. +pub proof fn lemma_run_seq_one(cu: spec_fn(nat) -> nat, s: x64::State, i: x64::Instr) + ensures + run_seq(cu, s, seq![i]) == x64::step(cu, s, i), +{ + let e = Seq::::empty(); + assert(seq![i].len() == 1); + assert(seq![i][0] == i); + assert(seq![i].skip(1) =~= e); + match x64::step(cu, s, i) { + Some(s1) => { + assert(run_seq(cu, s1, e) == Some(s1)); + }, + None => {}, + } +} + +/// The code length of a one-instruction list. +pub proof fn lemma_code_len_one(i: x64::Instr) + ensures + code_len(seq![i]) == x64::instr_len(i), +{ + let e = Seq::::empty(); + assert(seq![i].len() == 1); + assert(seq![i][0] == i); + assert(seq![i].skip(1) =~= e); + assert(code_len(e) == 0); +} + +/// An address between two canonical addresses is canonical. +pub proof fn lemma_canonical_between(lo: int, mid: int, hi: int) + requires + x64::canonical(lo), + x64::canonical(hi), + lo <= mid <= hi, + ensures + x64::canonical(mid), +{ +} + +/// Running an instruction followed by more: the step, then the rest. +pub proof fn lemma_run_seq_cons( + cu: spec_fn(nat) -> nat, + s: x64::State, + i: x64::Instr, + rest: Seq, +) + ensures + run_seq(cu, s, seq![i] + rest) == match x64::step(cu, s, i) { + Some(s1) => run_seq(cu, s1, rest), + None => None, + }, + code_len(seq![i] + rest) == x64::instr_len(i) + code_len(rest), +{ + let code = seq![i] + rest; + assert(code.len() > 0); + assert(code[0] == i); + assert(code.skip(1) =~= rest); +} + +/// [`run_seq`] with the step function abstracted: the lemmas about long +/// instruction lists are proven for an arbitrary `f`, so the solver does not +/// unfold `x64::step` on instructions it knows nothing about. +pub open spec fn run_gen( + f: spec_fn(x64::State, x64::Instr) -> Option, + s: x64::State, + code: Seq, +) -> Option + decreases code.len(), +{ + if code.len() == 0 { + Some(s) + } else { + match f(s, code[0]) { + Some(s1) => run_gen(f, s1, code.skip(1)), + None => None, + } + } +} + +/// The machine's step as a function value. +pub open spec fn stepper(cu: spec_fn(nat) -> nat) -> spec_fn(x64::State, x64::Instr) -> Option< + x64::State, +> { + |s1: x64::State, i: x64::Instr| x64::step(cu, s1, i) +} + +/// `run_seq` is `run_gen` for the machine's step. +pub proof fn lemma_run_seq_is_gen(cu: spec_fn(nat) -> nat, s: x64::State, code: Seq) + ensures + run_seq(cu, s, code) == run_gen(stepper(cu), s, code), + decreases code.len(), +{ + if code.len() > 0 { + match x64::step(cu, s, code[0]) { + Some(s1) => { + lemma_run_seq_is_gen(cu, s1, code.skip(1)); + }, + None => {}, + } + } +} + +/// A list is its head consed on its tail. Generic in the element type, so the +/// solver does not unfold `Instr`. +pub proof fn lemma_seq_cons(whole: Seq, x: A, rest: Seq) + requires + whole.len() == rest.len() + 1, + whole[0] == x, + forall|k: int| 0 <= k < rest.len() ==> #[trigger] whole[k + 1] == rest[k], + ensures + whole =~= seq![x] + rest, +{ + assert forall|k: int| 0 <= k < whole.len() implies whole[k] == (seq![x] + rest)[k] by { + if k > 0 { + assert(whole[(k - 1) + 1] == rest[k - 1]); + } + } +} + +/// The code length of a list is its head's length and its tail's. +pub proof fn lemma_code_len_cons(i: x64::Instr, rest: Seq) + ensures + code_len(seq![i] + rest) == x64::instr_len(i) + code_len(rest), +{ + let code = seq![i] + rest; + assert(code.len() > 0); + assert(code[0] == i); + assert(code.skip(1) =~= rest); +} + +/// `run_gen` of a list is the head's step, then the tail. +pub proof fn lemma_run_gen_cons( + f: spec_fn(x64::State, x64::Instr) -> Option, + s: x64::State, + i: x64::Instr, + rest: Seq, +) + ensures + run_gen(f, s, seq![i] + rest) == match f(s, i) { + Some(s1) => run_gen(f, s1, rest), + None => None, + }, +{ + let code = seq![i] + rest; + assert(code.len() > 0); + assert(code[0] == i); + assert(code.skip(1) =~= rest); +} + +/// `run_gen` of a list of 1 instruction whose steps are known. +pub proof fn lemma_gen1(f: spec_fn(x64::State, x64::Instr) -> Option, s: x64::State, a: x64::Instr, s1: x64::State) + requires + f(s, a) == Some(s1), + ensures + run_gen(f, s, seq![a]) == Some(s1), +{ + let e = Seq::::empty(); + lemma_seq_cons(seq![a], a, e); + lemma_run_gen_cons(f, s, a, e); +} + +/// The code length of a list of 1 instruction. +pub proof fn lemma_len1(a: x64::Instr) + ensures + code_len(seq![a]) == x64::instr_len(a), +{ + let e = Seq::::empty(); + lemma_seq_cons(seq![a], a, e); + lemma_code_len_cons(a, e); +} + +/// `run_gen` of a list of 2 instructions whose steps are known. +pub proof fn lemma_gen2(f: spec_fn(x64::State, x64::Instr) -> Option, s: x64::State, a: x64::Instr, b: x64::Instr, s1: x64::State, s2: x64::State) + requires + f(s, a) == Some(s1), + f(s1, b) == Some(s2), + ensures + run_gen(f, s, seq![a, b]) == Some(s2), +{ + lemma_gen1(f, s1, b, s2); + lemma_seq_cons(seq![a, b], a, seq![b]); + lemma_run_gen_cons(f, s, a, seq![b]); +} + +/// The code length of a list of 2 instructions. +pub proof fn lemma_len2(a: x64::Instr, b: x64::Instr) + ensures + code_len(seq![a, b]) == x64::instr_len(a) + x64::instr_len(b), +{ + lemma_len1(b); + lemma_seq_cons(seq![a, b], a, seq![b]); + lemma_code_len_cons(a, seq![b]); +} + +/// `run_gen` of a list of 3 instructions whose steps are known. +pub proof fn lemma_gen3(f: spec_fn(x64::State, x64::Instr) -> Option, s: x64::State, a: x64::Instr, b: x64::Instr, c: x64::Instr, s1: x64::State, s2: x64::State, s3: x64::State) + requires + f(s, a) == Some(s1), + f(s1, b) == Some(s2), + f(s2, c) == Some(s3), + ensures + run_gen(f, s, seq![a, b, c]) == Some(s3), +{ + lemma_gen2(f, s1, b, c, s2, s3); + lemma_seq_cons(seq![a, b, c], a, seq![b, c]); + lemma_run_gen_cons(f, s, a, seq![b, c]); +} + +/// The code length of a list of 3 instructions. +pub proof fn lemma_len3(a: x64::Instr, b: x64::Instr, c: x64::Instr) + ensures + code_len(seq![a, b, c]) == x64::instr_len(a) + x64::instr_len(b) + x64::instr_len(c), +{ + lemma_len2(b, c); + lemma_seq_cons(seq![a, b, c], a, seq![b, c]); + lemma_code_len_cons(a, seq![b, c]); +} + +/// `run_gen` of a list of 4 instructions whose steps are known. +pub proof fn lemma_gen4(f: spec_fn(x64::State, x64::Instr) -> Option, s: x64::State, a: x64::Instr, b: x64::Instr, c: x64::Instr, d: x64::Instr, s1: x64::State, s2: x64::State, s3: x64::State, s4: x64::State) + requires + f(s, a) == Some(s1), + f(s1, b) == Some(s2), + f(s2, c) == Some(s3), + f(s3, d) == Some(s4), + ensures + run_gen(f, s, seq![a, b, c, d]) == Some(s4), +{ + lemma_gen3(f, s1, b, c, d, s2, s3, s4); + lemma_seq_cons(seq![a, b, c, d], a, seq![b, c, d]); + lemma_run_gen_cons(f, s, a, seq![b, c, d]); +} + +/// The code length of a list of 4 instructions. +pub proof fn lemma_len4(a: x64::Instr, b: x64::Instr, c: x64::Instr, d: x64::Instr) + ensures + code_len(seq![a, b, c, d]) == x64::instr_len(a) + x64::instr_len(b) + x64::instr_len(c) + x64::instr_len(d), +{ + lemma_len3(b, c, d); + lemma_seq_cons(seq![a, b, c, d], a, seq![b, c, d]); + lemma_code_len_cons(a, seq![b, c, d]); +} + +/// `run_gen` of a list of 5 instructions whose steps are known. +pub proof fn lemma_gen5(f: spec_fn(x64::State, x64::Instr) -> Option, s: x64::State, a: x64::Instr, b: x64::Instr, c: x64::Instr, d: x64::Instr, e: x64::Instr, s1: x64::State, s2: x64::State, s3: x64::State, s4: x64::State, s5: x64::State) + requires + f(s, a) == Some(s1), + f(s1, b) == Some(s2), + f(s2, c) == Some(s3), + f(s3, d) == Some(s4), + f(s4, e) == Some(s5), + ensures + run_gen(f, s, seq![a, b, c, d, e]) == Some(s5), +{ + lemma_gen4(f, s1, b, c, d, e, s2, s3, s4, s5); + lemma_seq_cons(seq![a, b, c, d, e], a, seq![b, c, d, e]); + lemma_run_gen_cons(f, s, a, seq![b, c, d, e]); +} + +/// The code length of a list of 5 instructions. +pub proof fn lemma_len5(a: x64::Instr, b: x64::Instr, c: x64::Instr, d: x64::Instr, e: x64::Instr) + ensures + code_len(seq![a, b, c, d, e]) == x64::instr_len(a) + x64::instr_len(b) + x64::instr_len(c) + x64::instr_len(d) + x64::instr_len(e), +{ + lemma_len4(b, c, d, e); + lemma_seq_cons(seq![a, b, c, d, e], a, seq![b, c, d, e]); + lemma_code_len_cons(a, seq![b, c, d, e]); +} + +/// `run_gen` of a list of 6 instructions whose steps are known. +pub proof fn lemma_gen6(f: spec_fn(x64::State, x64::Instr) -> Option, s: x64::State, a: x64::Instr, b: x64::Instr, c: x64::Instr, d: x64::Instr, e: x64::Instr, g: x64::Instr, s1: x64::State, s2: x64::State, s3: x64::State, s4: x64::State, s5: x64::State, s6: x64::State) + requires + f(s, a) == Some(s1), + f(s1, b) == Some(s2), + f(s2, c) == Some(s3), + f(s3, d) == Some(s4), + f(s4, e) == Some(s5), + f(s5, g) == Some(s6), + ensures + run_gen(f, s, seq![a, b, c, d, e, g]) == Some(s6), +{ + lemma_gen5(f, s1, b, c, d, e, g, s2, s3, s4, s5, s6); + lemma_seq_cons(seq![a, b, c, d, e, g], a, seq![b, c, d, e, g]); + lemma_run_gen_cons(f, s, a, seq![b, c, d, e, g]); +} + +/// The code length of a list of 6 instructions. +pub proof fn lemma_len6(a: x64::Instr, b: x64::Instr, c: x64::Instr, d: x64::Instr, e: x64::Instr, g: x64::Instr) + ensures + code_len(seq![a, b, c, d, e, g]) == x64::instr_len(a) + x64::instr_len(b) + x64::instr_len(c) + x64::instr_len(d) + x64::instr_len(e) + x64::instr_len(g), +{ + lemma_len5(b, c, d, e, g); + lemma_seq_cons(seq![a, b, c, d, e, g], a, seq![b, c, d, e, g]); + lemma_code_len_cons(a, seq![b, c, d, e, g]); +} + +} // verus! diff --git a/crates/clazel-jit/src/skel.rs b/crates/clazel-jit/src/skel.rs new file mode 100644 index 000000000..d6ecdee2c --- /dev/null +++ b/crates/clazel-jit/src/skel.rs @@ -0,0 +1,209 @@ +//! The skeleton every storing template shares: *loads, a register-only middle, +//! one store*. [`lemma_loads`] runs the loads, and [`lemma_shape`] puts the +//! three parts together: given a middle that leaves the result word in `rax` +//! (the hypothesis, proven per template in [`crate::mid`]), the whole template +//! takes a state related to `a` to a state related to the next LIR state `a2`. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The loads in `ls` read distinct scratch registers from existing slots. +pub open spec fn loads_ok(ls: Seq<(u8, nat)>, nl: nat, n: nat) -> bool { + (forall|k: int| + 0 <= k < ls.len() ==> super::rel::scratch((#[trigger] ls[k]).0) && ls[k].1 < nl + n) && (forall| + k1: int, + k2: int, + | + 0 <= k1 < k2 < ls.len() ==> (#[trigger] ls[k1]).0 != (#[trigger] ls[k2]).0) +} + +/// What the middle of a template is started from: a well-formed state whose +/// registers hold the words the loads `ls` would have put there. +pub open spec fn mid_pre( + a: lir::St, + nl: nat, + ls: Seq<(u8, nat)>, + mid: Seq, + s: x64::State, +) -> bool { + x64::wf(s) && s.rgf.len() == 16 && (forall|k: int| + 0 <= k < ls.len() ==> (#[trigger] s.rgf[ls[k].0 as int]) as nat == lir::val_bits( + slot_val(a, nl, ls[k].1), + )) && x64::canonical(s.rip + code_len(mid)) +} + +/// What the middle must do from there: not fault, and leave `w` in `rax` +/// changing only scratch registers and `rip` ([`mid_post`]). +pub open spec fn mid_goal( + cu: spec_fn(nat) -> nat, + mid: Seq, + w: nat, + s: x64::State, +) -> bool { + run_seq(cu, s, mid) is Some && super::alu::mid_post( + s, + run_seq(cu, s, mid).unwrap(), + rax(), + w, + code_len(mid), + ) +} + +/// The middle does what it must from every state it can be started from. +pub open spec fn mid_prop( + cu: spec_fn(nat) -> nat, + a: lir::St, + nl: nat, + ls: Seq<(u8, nat)>, + mid: Seq, + w: nat, +) -> bool { + forall|s: x64::State| + #![trigger mid_goal(cu, mid, w, s)] + mid_pre(a, nl, ls, mid, s) ==> mid_goal(cu, mid, w, s) +} + +/// Running the loads `ls`: each register holds its slot's word, and the state +/// is still related to `a`, with the same memory. +#[verifier::rlimit(1000)] +pub proof fn lemma_loads( + cu: spec_fn(nat) -> nat, + lay: lir::Layout, + a: lir::St, + m: x64::State, + ls: Seq<(u8, nat)>, + nl: nat, +) + requires + rel(lay, a, m, nl), + loads_ok(ls, nl, a.stack.len()), + x64::canonical(m.rip + code_len(super::templates::loads_seq(ls))), + ensures + run_seq(cu, m, super::templates::loads_seq(ls)) is Some, + ({ + let m1 = run_seq(cu, m, super::templates::loads_seq(ls)).unwrap(); + &&& rel(lay, a, m1, nl) + &&& m1.rip == m.rip + code_len(super::templates::loads_seq(ls)) + &&& m1.mem == m.mem + &&& m1.rgf.len() == 16 + &&& m1.xmm.len() == 16 + &&& m1.rgf[abi::ctx_reg() as int] == m.rgf[abi::ctx_reg() as int] + &&& m1.rgf[abi::sp_reg() as int] == m.rgf[abi::sp_reg() as int] + &&& forall|k: int| + 0 <= k < ls.len() ==> (#[trigger] m1.rgf[ls[k].0 as int]) as nat == lir::val_bits( + slot_val(a, nl, ls[k].1), + ) + }), + decreases ls.len(), +{ + super::steps::lemma_abi_consts(); + if ls.len() == 0 { + assert(super::templates::loads_seq(ls) =~= seq![]); + assert(code_len(Seq::::empty()) == 0); + } else { + let pre = ls.drop_last(); + let last = ls.last(); + let a_code = super::templates::loads_seq(pre); + let b_code = seq![super::templates::load_slot(last.0, last.1)]; + assert(super::templates::loads_seq(ls) == a_code + b_code); + super::runseq::lemma_code_len_append(a_code, b_code); + super::runseq::lemma_code_len_one(super::templates::load_slot(last.0, last.1)); + assert(loads_ok(pre, nl, a.stack.len())) by { + assert forall|k: int| 0 <= k < pre.len() implies super::rel::scratch((#[trigger] pre[k]).0) + && pre[k].1 < nl + a.stack.len() by { + assert(pre[k] == ls[k]); + } + assert forall|k1: int, k2: int| + 0 <= k1 < k2 < pre.len() implies (#[trigger] pre[k1]).0 != (#[trigger] pre[k2]).0 by { + assert(pre[k1] == ls[k1] && pre[k2] == ls[k2]); + } + } + lemma_loads(cu, lay, a, m, pre, nl); + let m1 = run_seq(cu, m, a_code).unwrap(); + super::runseq::lemma_run_seq_append(cu, m, a_code, b_code); + assert(super::rel::scratch(last.0) && last.1 < nl + a.stack.len()); + super::rel::lemma_load_rel(cu, lay, a, m1, last.0, last.1, nl); + super::runseq::lemma_run_seq_one(cu, m1, super::templates::load_slot(last.0, last.1)); + let m2 = x64::step(cu, m1, super::templates::load_slot(last.0, last.1)).unwrap(); + assert(m2.rgf.len() == 16); + assert forall|k: int| 0 <= k < ls.len() implies (#[trigger] m2.rgf[ls[k].0 as int]) as nat + == lir::val_bits(slot_val(a, nl, ls[k].1)) by { + if k < ls.len() - 1 { + assert(pre[k] == ls[k]); + assert(ls[k].0 != last.0); + } + } + } +} + +/// **The skeleton.** A template made of loads `ls`, a register-only middle +/// `mid` and a store of `rax` to slot `ts` takes a state related to `a` to one +/// related to `a2`, if: +/// +/// - `a2` differs from `a` in slot `ts` only, where it holds `w` +/// ([`slots_agree_except`]); +/// - the middle, started from any well-formed state whose registers hold the +/// loaded words, leaves `w` in `rax` and changes only scratch registers and +/// `rip` ([`mid_post`]). +pub proof fn lemma_shape( + cu: spec_fn(nat) -> nat, + lay: lir::Layout, + a: lir::St, + a2: lir::St, + m: x64::State, + nl: nat, + ls: Seq<(u8, nat)>, + mid: Seq, + ts: nat, + w: nat, +) + requires + rel(lay, a, m, nl), + loads_ok(ls, nl, a.stack.len()), + x64::canonical(m.rip + code_len(super::templates::shape(ls, mid, ts))), + ts < max_slots(), + super::rel::slots_agree_except(a, a2, nl, ts), + w == lir::val_bits(slot_val(a2, nl, ts)), + mid_prop(cu, a, nl, ls, mid, w), + ensures + run_seq(cu, m, super::templates::shape(ls, mid, ts)) is Some, + ({ + let m4 = run_seq(cu, m, super::templates::shape(ls, mid, ts)).unwrap(); + &&& rel(lay, a2, m4, nl) + &&& m4.rip == m.rip + code_len(super::templates::shape(ls, mid, ts)) + &&& preserved(m, m4) + }), +{ + super::steps::lemma_abi_consts(); + let ld = super::templates::loads_seq(ls); + let st = seq![super::templates::store_slot(ts, rax())]; + super::runseq::lemma_code_len_append(ld, mid); + super::runseq::lemma_code_len_append(ld + mid, st); + super::runseq::lemma_code_len_one(super::templates::store_slot(ts, rax())); + assert(super::templates::shape(ls, mid, ts) == (ld + mid) + st); + lemma_loads(cu, lay, a, m, ls, nl); + let m1 = run_seq(cu, m, ld).unwrap(); + // the middle + assert(x64::wf(m1)); + assert(x64::canonical(m1.rip + code_len(mid))); + assert(mid_pre(a, nl, ls, mid, m1)); + assert(mid_goal(cu, mid, w, m1)); + let m3 = run_seq(cu, m1, mid).unwrap(); + super::runseq::lemma_run_seq_append(cu, m, ld, mid); + super::runseq::lemma_run_seq_append(cu, m, ld + mid, st); + assert(x64::canonical(m3.rip)) by { + super::mid::lemma_rip_between(m, m3.rip, m.rip + code_len(super::templates::shape(ls, mid, ts))); + } + super::rel::lemma_rel_regs(lay, a, m1, m3, nl); + // the store + assert(m3.rip + x64::instr_len(super::templates::store_slot(ts, rax())) <= m.rip + code_len(super::templates::shape(ls, mid, ts))); + assert(x64::canonical(m3.rip + x64::instr_len(super::templates::store_slot(ts, rax())))); + super::rel::lemma_store_finish(cu, lay, a, a2, m, m3, nl, ts, rax(), w); + super::runseq::lemma_run_seq_one(cu, m3, super::templates::store_slot(ts, rax())); +} + +} // verus! diff --git a/crates/clazel-jit/src/spec.rs b/crates/clazel-jit/src/spec.rs new file mode 100644 index 000000000..ca5c7a633 --- /dev/null +++ b/crates/clazel-jit/src/spec.rs @@ -0,0 +1,487 @@ +//! What the LIR → x86-64 back end is *about* (jit.md item J.9): the state +//! relation between a LIR state and an x86-64 machine state, the shape of a +//! template, and the small definitions the refinement statements of +//! [`crate::contract`] are written in. Nothing here runs and nothing is +//! proven; the templates are in `templates` and their proofs in the modules +//! after it. +//! +//! # Source +//! +//! There is no standard to transcribe. The sources are: +//! +//! - **jit.md, "J.9 Back ends"**: "No register allocation: every LIR local is +//! a frame slot, and each LIR instruction is a fixed template over scratch +//! registers, so each template's proof is local. Per-template refinement of +//! LIR semantics, then composition over structured control flow." +//! - **`clazel-lir`'s `spec`** ([`lir::eval`]): the meaning of the source. +//! - **`clazel-x64`'s `spec`** ([`x64::step`]): the meaning of the target. +//! - **`clazel-codecheck`'s `spec::abi`**: the context register, the region +//! table, and which context bytes the code may write. The templates follow +//! it, so that what they emit is accepted (the test in +//! `tests/validator.rs`; the validator's own spec is not re-stated here). +//! +//! # Modelling choices +//! +//! These are *ours*: jit.md fixes "a frame slot per local" but not where the +//! frame is, or what happens to LIR's operand stack. +//! +//! 1. **The frame is the context's writable area.** The only memory the +//! validator lets code write without a bounds-check sequence is the exit +//! area of the context, bytes `256..512` from `r15` +//! ([`abi::exit_code_off`], [`abi::ctx_size`]). The frame is the part of it +//! after the exit code: slot `j` is the 8 bytes at `r15 + 264 + 8 j` +//! ([`slot_disp`]), for `j < 31` ([`max_slots`]). A function whose locals +//! and operand stack need more than 31 slots is refused ([`prog_ok`]). A +//! larger frame is a region (jit.md J.0: the interpreter's value stack), +//! reached through the 8-instruction bounds-check sequence on every +//! access; that needs the memory templates and is future work. **The exit +//! code word (offset 256) is not a slot**, so an exit sequence does not +//! overwrite a local. +//! 2. **LIR's operand stack is in the frame too.** LIR is a stack machine (it +//! mirrors WebAssembly), and "every local is a slot" needs a home for the +//! stack. Locals are slots `0 .. nl`; the operand stack's element `k` +//! (bottom first) is slot `nl + k`. The height of the stack at each +//! instruction is a static fact of well-typed straight-line code, so a +//! template is a function of `(op, nl, h)` ([`templates::tmpl`]). +//! 3. **A value is a 64-bit word.** An `i64` and an `f64` (its bit pattern) +//! are stored as they are, and an `i32` zero-extended +//! ([`lir::val_bits`], which is below `2^32` for an `i32`). The type is +//! not stored: it is static. +//! 4. **No register is live between two LIR instructions.** The scratch +//! registers `rax`, `rcx` and `rdx` ([`rax`], [`rcx`], [`rdx`]) and the +//! flags are dead at each template's start and end, so the relation does +//! not mention them. `r15` (the context) and `rsp` are never written. +//! 5. **Running code is [`run_seq`]**: [`x64::step`] on each instruction of a +//! `Seq` in turn. That the bytes at `rip` are the canonical +//! encoding of the instruction ([`x64::fetch_ok`]) is not part of it: +//! instead [`preserved`] says the code writes nothing but the exit area, +//! so code placed outside the exit area stays what it was, and the bytes +//! are `enc_all` of the instruction list (the emitter's contract). +//! 6. **Addresses are user-space addresses.** The context lies below +//! `2^47` ([`ctx_ok`]), so every address a template forms is canonical and +//! equals the effective address (no wrap-around), and the code's end +//! address is canonical (a `requires` of each refinement). +//! 7. **LIR memory is related, not yet touched.** [`mem_rel`] relates each +//! region byte of LIR's memory to the machine's, and every template in +//! this slice leaves both unchanged. The memory templates (`bounds_check`, +//! `load`, `store`) are not here. +//! +//! # What is not here +//! +//! The memory instructions, structured control flow (`block`, `loop`, `if`, +//! `br`, `br_if`, `br_table`), `exit` with operands, `f64` arithmetic, +//! comparisons and conversions (LIR's `f64` operators are uninterpreted +//! functions, so a refinement to SSE needs an axiom this crate does not +//! add), rotates, and the A64 back end. + +/// The validator's ABI, re-exported so that this crate's modules read one namespace. +pub use clazel_codecheck::spec::abi; +/// LIR's semantics, re-exported likewise. +pub use clazel_lir::spec as lir; +/// The x86-64 subset's semantics, re-exported likewise. +pub use clazel_x64::spec as x64; + +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// ============================================================================ +// Section: the source subset +// ============================================================================ + +/// A LIR instruction of the first slice, in a form executable code can build: +/// `clazel-lir`'s own `Instr` holds `nat`s and `Seq`s, which exist only in +/// proofs. [`View`] is the LIR instruction it stands for. +pub enum Op { + /// `nop`. + Nop, + /// `t.const v`. + Const(lir::Val), + /// `local.get x`. + LocalGet(usize), + /// `local.set x`. + LocalSet(usize), + /// `local.tee x`. + LocalTee(usize), + /// `drop`. + Drop, + /// `select`. + Select, + /// A binary numeric instruction. + Bin(lir::BinOp), + /// A unary numeric instruction. + Un(lir::UnOp), +} + +impl vstd::view::View for Op { + type V = lir::Instr; + + open spec fn view(&self) -> lir::Instr { + match *self { + Op::Nop => lir::Instr::Nop, + Op::Const(v) => lir::Instr::Const(v), + Op::LocalGet(x) => lir::Instr::LocalGet(x as nat), + Op::LocalSet(x) => lir::Instr::LocalSet(x as nat), + Op::LocalTee(x) => lir::Instr::LocalTee(x as nat), + Op::Drop => lir::Instr::Drop, + Op::Select => lir::Instr::Select, + Op::Bin(b) => lir::Instr::Bin(b), + Op::Un(u) => lir::Instr::Un(u), + } + } +} + +/// A straight-line program as one LIR instruction: `((nop; op_0); op_1); ...`, +/// the left-nested `then` chain, so that the program minus its last +/// instruction is the first operand of the outermost `then`. +pub open spec fn prog_instr(ops: Seq) -> lir::Instr + decreases ops.len(), +{ + if ops.len() == 0 { + lir::Instr::Nop + } else { + lir::Instr::Then(Box::new(prog_instr(ops.drop_last())), Box::new(ops.last()@)) + } +} + +/// How many operand-stack values the instruction takes. +pub open spec fn pops(op: Op) -> nat { + match op { + Op::Nop => 0, + Op::Const(_) => 0, + Op::LocalGet(_) => 0, + Op::LocalSet(_) => 1, + Op::LocalTee(_) => 1, + Op::Drop => 1, + Op::Select => 3, + Op::Bin(_) => 2, + Op::Un(_) => 1, + } +} + +/// How many operand-stack values the instruction leaves. +pub open spec fn pushes(op: Op) -> nat { + match op { + Op::Nop => 0, + Op::Const(_) => 1, + Op::LocalGet(_) => 1, + Op::LocalSet(_) => 0, + Op::LocalTee(_) => 1, + Op::Drop => 0, + Op::Select => 1, + Op::Bin(_) => 1, + Op::Un(_) => 1, + } +} + +/// The stack height after `op` at height `h`. +pub open spec fn height_after_op(op: Op, h: nat) -> nat { + (h - pops(op) + pushes(op)) as nat +} + +/// The integer binary operators this slice has templates for: everything but +/// `rotl` and `rotr`, which the x86-64 subset has no instruction for. +pub open spec fn ibin_supported(o: lir::IBin) -> bool { + match o { + lir::IBin::Rotl => false, + lir::IBin::Rotr => false, + _ => true, + } +} + +/// The binary instructions of this slice: integer arithmetic and logic, and +/// integer comparisons. Not the `f64` ones (see the header). +pub open spec fn bin_supported(b: lir::BinOp) -> bool { + match b { + lir::BinOp::I32(o) => ibin_supported(o), + lir::BinOp::I64(o) => ibin_supported(o), + lir::BinOp::I32Cmp(_) => true, + lir::BinOp::I64Cmp(_) => true, + lir::BinOp::F64(_) => false, + lir::BinOp::F64Cmp(_) => false, + } +} + +/// The unary instructions of this slice: the integer tests and conversions, +/// and the `f64` instructions that are bit operations (`neg`, `abs`, +/// `reinterpret`). Not `sqrt` or the integer-to-`f64` conversions. +pub open spec fn un_supported(u: lir::UnOp) -> bool { + match u { + lir::UnOp::I32Eqz => true, + lir::UnOp::I64Eqz => true, + lir::UnOp::F64Neg => true, + lir::UnOp::F64Abs => true, + lir::UnOp::F64Sqrt => false, + lir::UnOp::I32WrapI64 => true, + lir::UnOp::I64ExtendI32S => true, + lir::UnOp::I64ExtendI32U => true, + lir::UnOp::F64ConvertI32S => false, + lir::UnOp::F64ConvertI32U => false, + lir::UnOp::F64ConvertI64S => false, + lir::UnOp::F64ConvertI64U => false, + lir::UnOp::F64ReinterpretI64 => true, + lir::UnOp::I64ReinterpretF64 => true, + } +} + +/// The instruction has a template, and is well formed at a stack of height +/// `h` with `nl` locals: the frame holds the stack as it is (`nl + h` slots) +/// and as it will be, its operands exist, its local index is in range, and so +/// everything it reads or writes is one of the 31 frame slots. +pub open spec fn op_ok(op: Op, nl: nat, h: nat) -> bool { + nl + h <= max_slots() && pops(op) <= h && nl + height_after_op(op, h) <= max_slots() && match op { + Op::LocalGet(x) => x < nl, + Op::LocalSet(x) => x < nl, + Op::LocalTee(x) => x < nl, + Op::Bin(b) => bin_supported(b), + Op::Un(u) => un_supported(u), + _ => true, + } +} + +/// The stack height after the program `ops`, started at height `h`. +pub open spec fn height_after(ops: Seq, h: nat) -> nat + decreases ops.len(), +{ + if ops.len() == 0 { + h + } else { + height_after_op(ops.last(), height_after(ops.drop_last(), h)) + } +} + +/// Every instruction of `ops` has a template at its place in the program, for +/// `nl` locals and a stack that starts at height `h`, and the starting frame +/// fits. +pub open spec fn prog_ok(ops: Seq, nl: nat, h: nat) -> bool + decreases ops.len(), +{ + if ops.len() == 0 { + nl + h <= max_slots() + } else { + prog_ok(ops.drop_last(), nl, h) && op_ok(ops.last(), nl, height_after(ops.drop_last(), h)) + } +} + +// ============================================================================ +// Section: the frame +// ============================================================================ + +/// `rax`, the first scratch register. +pub open spec fn rax() -> u8 { + 0 +} + +/// `rcx`, the second scratch register (and the shift count: `shl r, cl`). +pub open spec fn rcx() -> u8 { + 1 +} + +/// `rdx`, the third scratch register. +pub open spec fn rdx() -> u8 { + 2 +} + +/// The offset from the context register of frame slot 0: the first word +/// after the exit code (modelling choice 1). +pub open spec fn frame_off() -> int { + abi::exit_code_off() + 8 +} + +/// The number of frame slots: the words of the context after the exit code. +pub open spec fn max_slots() -> nat { + ((abi::ctx_size() - frame_off()) / 8) as nat +} + +/// The displacement of frame slot `j` from the context register. +pub open spec fn slot_disp(j: nat) -> int { + frame_off() + 8 * j +} + +/// The memory operand of frame slot `j`: `[r15 + 264 + 8 j]`. +pub open spec fn slot_mem(j: nat) -> x64::Mem { + x64::Mem { base: abi::ctx_reg(), index: None, disp: slot_disp(j) as i32 } +} + +/// The value of the context register in `m`, as an address. +pub open spec fn ctx_of(m: x64::State) -> int { + m.rgf[abi::ctx_reg() as int] as int +} + +/// The context lies in user space: the 512 bytes from the context register +/// are all below `2^47` (modelling choice 6). +pub open spec fn ctx_ok(m: x64::State) -> bool { + m.rgf.len() == 16 && 0 <= ctx_of(m) && ctx_of(m) + abi::ctx_size() <= x64::p2(47) +} + +/// `a` is a byte of the context's exit area (what [`abi`] lets code write). +pub open spec fn in_exit_area(ctxb: int, a: int) -> bool { + ctxb + abi::exit_code_off() <= a && a < ctxb + abi::ctx_size() +} + +/// Every byte of the exit area is mapped. +pub open spec fn exit_area_mapped(mm: Map, ctxb: int) -> bool { + forall|a: int| #[trigger] in_exit_area(ctxb, a) ==> mm.dom().contains(a) +} + +/// The 8 bytes at the address of slot `j` read as the word `v`. +pub open spec fn slot_holds(mm: Map, ctxb: int, j: nat, v: nat) -> bool { + x64::read_mem(mm, ctxb + slot_disp(j), 8) == Some(v as int) +} + +// ============================================================================ +// Section: the state relation +// ============================================================================ + +/// The value in slot `j` of the LIR state `a`, which has `nl` locals: a local +/// for `j < nl`, else the operand stack's element `j - nl`. +pub open spec fn slot_val(a: lir::St, nl: nat, j: nat) -> lir::Val { + if j < nl { + a.locals[j as int] + } else { + a.stack[(j - nl) as int] + } +} + +/// The addresses of the regions of `lay` are the addresses of its bytes: `a` +/// is inside some region. +pub open spec fn in_regions(lay: lir::Layout, a: nat) -> bool { + exists|r: int| + #![trigger lay[r]] + 0 <= r < lay.len() && lay[r].base <= a && a < lay[r].base + lay[r].bound +} + +/// LIR's memory and the machine's agree on every byte of every region: the +/// machine maps it, and holds the byte LIR's memory reads (absent bytes read +/// 0, `lir::byte_at`). +pub open spec fn mem_rel(lay: lir::Layout, lm: lir::Mem, mm: Map) -> bool { + forall|a: nat| #[trigger] in_regions(lay, a) ==> mm.dom().contains(a as int) && mm[a as int] + == lir::byte_at(lm, a) +} + +/// No region overlaps the context's exit area, which holds the frame: the +/// frame is the only machine memory the templates write, and it is nobody's +/// region. +pub open spec fn regions_clear_of_exit_area(lay: lir::Layout, ctxb: int) -> bool { + forall|a: nat| #[trigger] in_regions(lay, a) ==> !in_exit_area(ctxb, a as int) +} + +/// **The state relation.** The LIR state `a`, with `nl` locals, is +/// represented by the machine state `m`: +/// +/// 1. `m` is a well-formed machine state ([`x64::wf`]), with the context in +/// user space ([`ctx_ok`]) and the layout well formed and clear of the +/// exit area; +/// 2. the frame fits: `a` has `nl` locals and `nl + height` is at most 31; +/// 3. the exit area is mapped, so a store to a slot does not fault; +/// 4. every local, and every operand-stack element, is in its slot, as the +/// 64-bit word [`lir::val_bits`] of its value; +/// 5. memory is related ([`mem_rel`]). +/// +/// The scratch registers, the flags, `rip` and the XMM registers are not +/// constrained (modelling choice 4); the refinement statements say where +/// `rip` is. +pub open spec fn rel(lay: lir::Layout, a: lir::St, m: x64::State, nl: nat) -> bool { + x64::wf(m) && ctx_ok(m) && lir::layout_wf(lay) && regions_clear_of_exit_area(lay, ctx_of(m)) + && a.locals.len() == nl && nl + a.stack.len() <= max_slots() && exit_area_mapped( + m.mem, + ctx_of(m), + ) && (forall|j: nat| + j < nl + a.stack.len() ==> #[trigger] slot_holds( + m.mem, + ctx_of(m), + j, + lir::val_bits(slot_val(a, nl, j)), + )) && mem_rel(lay, a.mem, m.mem) +} + +/// What a template leaves alone in memory: the context register keeps its +/// value, the set of mapped addresses is the same, and every byte outside the +/// exit area keeps its value. So the code (which lives outside the exit +/// area) and every region are as they were. +pub open spec fn preserved_mem(m: x64::State, m2: x64::State) -> bool { + m.rgf.len() == 16 && m2.rgf.len() == 16 && m2.rgf[abi::ctx_reg() as int] + == m.rgf[abi::ctx_reg() as int] && m2.mem.dom() == m.mem.dom() && (forall|a: int| + #[trigger] m.mem.dom().contains(a) && !in_exit_area(ctx_of(m), a) ==> m2.mem[a] + == m.mem[a]) +} + +/// [`preserved_mem`], and the stack pointer keeps its value too (no +/// template but the exit sequence's `ret` touches it, and the validator +/// forbids writing it). +pub open spec fn preserved(m: x64::State, m2: x64::State) -> bool { + preserved_mem(m, m2) && m2.rgf[abi::sp_reg() as int] == m.rgf[abi::sp_reg() as int] +} + +// ============================================================================ +// Section: running code +// ============================================================================ + +/// Run the instructions of `code` one after the other from `s`, each by +/// [`x64::step`], stopping at a fault (`None`). `rip` advances by each +/// instruction's length (and, for a jump, goes to its target; the straight-line +/// templates have none). +pub open spec fn run_seq( + cu: spec_fn(nat) -> nat, + s: x64::State, + code: Seq, +) -> Option + decreases code.len(), +{ + if code.len() == 0 { + Some(s) + } else { + match x64::step(cu, s, code[0]) { + Some(s1) => run_seq(cu, s1, code.skip(1)), + None => None, + } + } +} + +/// The exit sequence for `c`, as the validator recognizes it +/// (`clazel_codecheck::spec::exit_unit_at`): `mov rax, c`, `mov [r15 + 256], +/// rax`, `ret`. It is LIR's `exit` with no operands (a function that falls +/// off its end exits with code `c`). +pub open spec fn exit_seq(c: u32) -> Seq { + seq![ + x64::Instr::MovImm32 { dst: rax(), imm_u32: c }, + x64::Instr::MovStore { + sz: x64::Size::W64, + mem: x64::Mem { base: abi::ctx_reg(), index: None, disp: abi::exit_code_off() as i32 }, + src: rax(), + }, + x64::Instr::Ret, + ] +} + +/// The machine's stack is as the enter stub leaves it for the `ret` of an +/// exit sequence: `rsp` points at 8 mapped bytes holding a canonical return +/// address, `rsp + 8` is canonical, and the stack word is not in the context +/// (so the exit code's store cannot change it). +pub open spec fn stack_ok(m: x64::State) -> bool { + m.rgf.len() == 16 && { + let rsp = x64::logext(64, m.rgf[abi::sp_reg() as int] as int); + match x64::read_mem(m.mem, rsp, 8) { + Some(raw) => x64::canonical(x64::logext(64, rsp + 8)) && x64::canonical( + x64::logext(64, raw), + ) && (rsp + 8 <= ctx_of(m) + abi::exit_code_off() || ctx_of(m) + abi::ctx_size() + <= rsp), + None => false, + } + } +} + +/// The length in bytes of the canonical encodings of `code`, one after the +/// other. +pub open spec fn code_len(code: Seq) -> nat + decreases code.len(), +{ + if code.len() == 0 { + 0 + } else { + x64::instr_len(code[0]) + code_len(code.skip(1)) + } +} + +} // verus! diff --git a/crates/clazel-jit/src/steps.rs b/crates/clazel-jit/src/steps.rs new file mode 100644 index 000000000..ffcfe830e --- /dev/null +++ b/crates/clazel-jit/src/steps.rs @@ -0,0 +1,199 @@ +//! What one `x64::step` does, for each instruction form the templates use: +//! a load from or store to a frame slot, an immediate move, a register move, +//! and the arithmetic, logic, shift, multiply, compare, test, set and +//! conditional-move forms. Each lemma computes the state `step` returns, in +//! terms of the state it is given, so a template's proof is a chain of them. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The ABI constants the lemmas need, evaluated. +pub proof fn lemma_abi_consts() + ensures + abi::ctx_reg() == 15, + abi::sp_reg() == 4, + abi::exit_code_off() == 256, + abi::ctx_size() == 512, + frame_off() == 264, + max_slots() == 31, +{ +} + +/// The address of frame slot `j`, formed by the effective-address rule from the +/// context register, is the context plus the slot's displacement, with no +/// wrap-around. +pub proof fn lemma_slot_ea(m: x64::State, j: nat) + requires + ctx_ok(m), + j < max_slots(), + ensures + x64::eff_addr(m, slot_mem(j)) == ctx_of(m) + slot_disp(j), + (slot_disp(j) as i32) as int == slot_disp(j), + 0 <= slot_disp(j) < 512, +{ + super::memlemmas::lemma_p2_consts(); + lemma_abi_consts(); + let d = slot_disp(j); + assert(d == 264 + 8 * j); + assert((d as i32) as int == d); + let x = ctx_of(m) + d; + assert(0 <= x && x < 0x8000_0000_0000 + 512); + assert(x64::loghead(64, x) == x); + assert(x64::logext(64, x) == x); + assert(x64::eff_addr(m, slot_mem(j)) == x64::logext(64, ctx_of(m) + 0 + d)); +} + +/// `mov dst, [slot j]` puts the slot's word in `dst` and advances `rip`. +pub proof fn lemma_step_load_slot( + cu: spec_fn(nat) -> nat, + m: x64::State, + dst: u8, + j: nat, + v: nat, +) + requires + x64::wf(m), + ctx_ok(m), + dst < 16, + j < max_slots(), + slot_holds(m.mem, ctx_of(m), j, v), + v < 0x1_0000_0000_0000_0000, + x64::canonical(m.rip + x64::instr_len(super::templates::load_slot(dst, j))), + ensures + x64::step(cu, m, super::templates::load_slot(dst, j)) == Some( + x64::State { + rgf: m.rgf.update(dst as int, v as u64), + rip: m.rip + x64::instr_len(super::templates::load_slot(dst, j)), + ..m + }, + ), +{ + super::memlemmas::lemma_p2_consts(); + lemma_slot_ea(m, j); + let i = super::templates::load_slot(dst, j); + assert(x64::read_operand_mem(m, x64::Size::W64, slot_mem(j)) == Some(v as int)); + assert(x64::loghead(64, v as int) == v as int); + assert(x64::wr64(m, dst, v as int) == x64::State { rgf: m.rgf.update(dst as int, v as u64), ..m }); +} + +/// `mov [slot j], src` writes the word in `src` to the slot, and to no other +/// byte of memory: every other slot, and every byte outside the slot, is as +/// it was. Registers, flags and the XMM registers are unchanged. +pub proof fn lemma_step_store_slot(cu: spec_fn(nat) -> nat, m: x64::State, src: u8, j: nat) + requires + x64::wf(m), + ctx_ok(m), + src < 16, + j < max_slots(), + exit_area_mapped(m.mem, ctx_of(m)), + x64::canonical(m.rip + x64::instr_len(super::templates::store_slot(j, src))), + ensures + x64::step(cu, m, super::templates::store_slot(j, src)) is Some, + ({ + let m1 = x64::step(cu, m, super::templates::store_slot(j, src)).unwrap(); + &&& m1 == x64::State { mem: m1.mem, rip: m.rip + x64::instr_len(super::templates::store_slot(j, src)), ..m } + &&& slot_holds(m1.mem, ctx_of(m), j, m.rgf[src as int] as nat) + &&& forall|k: nat, w: nat| + k < max_slots() && k != j && #[trigger] slot_holds(m.mem, ctx_of(m), k, w) + ==> slot_holds(m1.mem, ctx_of(m), k, w) + &&& m1.mem.dom() =~= m.mem.dom() + &&& forall|b: int| + #![trigger m1.mem.dom().contains(b)] + m.mem.dom().contains(b) && (b < ctx_of(m) + slot_disp(j) || ctx_of(m) + + slot_disp(j) + 8 <= b) ==> m1.mem[b] == m.mem[b] + }), +{ + super::memlemmas::lemma_p2_consts(); + lemma_slot_ea(m, j); + let ctxb = ctx_of(m); + let addr = ctxb + slot_disp(j); + let w = m.rgf[src as int] as int; + assert(x64::rgfi_size(m, x64::Size::W64, src) == w); + assert forall|i: int| 0 <= i < 8 implies m.mem.dom().contains(#[trigger] (addr + i)) by { + assert(in_exit_area(ctxb, addr + i)); + } + super::memlemmas::lemma_write_mem_some(m.mem, addr, 8, w); + let m2 = x64::write_mem(m.mem, addr, 8, w).unwrap(); + super::memlemmas::lemma_write_mem_frame(m.mem, addr, 8, w, m2); + assert(0 <= w < vstd::arithmetic::power2::pow2(8 * 8) as int) by { + assert(vstd::arithmetic::power2::pow2(64) == 0x1_0000_0000_0000_0000); + } + super::memlemmas::lemma_read_after_write_same(m.mem, addr, 8, w, m2); + assert(x64::store_mem(m, slot_mem(j), x64::Size::W64, w) == Some(x64::State { mem: m2, ..m })); + let m1 = x64::step(cu, m, super::templates::store_slot(j, src)).unwrap(); + assert(m1.mem == m2); + assert forall|k: nat, v: nat| + k < max_slots() && k != j && #[trigger] slot_holds(m.mem, ctxb, k, v) implies slot_holds( + m1.mem, + ctxb, + k, + v, + ) by { + lemma_abi_consts(); + super::memlemmas::lemma_read_after_write_disjoint(m.mem, addr, 8, w, m2, ctxb + slot_disp(k), 8); + } +} + +/// `mov dst, imm64` and `mov dst, imm32` (zero-extending). +pub proof fn lemma_step_imm(cu: spec_fn(nat) -> nat, m: x64::State, i: x64::Instr) + requires + x64::wf(m), + x64::canonical(m.rip + x64::instr_len(i)), + i is MovImm64 || i is MovImm32, + i is MovImm64 ==> i->MovImm64_dst < 16, + i is MovImm32 ==> i->MovImm32_dst < 16, + ensures + i is MovImm64 ==> x64::step(cu, m, i) == Some( + x64::State { + rgf: m.rgf.update(i->MovImm64_dst as int, i->MovImm64_imm_u64), + rip: m.rip + x64::instr_len(i), + ..m + }, + ), + i is MovImm32 ==> x64::step(cu, m, i) == Some( + x64::State { + rgf: m.rgf.update(i->MovImm32_dst as int, i->MovImm32_imm_u32 as u64), + rip: m.rip + x64::instr_len(i), + ..m + }, + ), +{ + super::memlemmas::lemma_p2_consts(); + if i is MovImm64 { + assert(x64::loghead(64, i->MovImm64_imm_u64 as int) == i->MovImm64_imm_u64 as int); + } else { + assert(x64::loghead(32, i->MovImm32_imm_u32 as int) == i->MovImm32_imm_u32 as int); + assert(x64::loghead(64, i->MovImm32_imm_u32 as int) == i->MovImm32_imm_u32 as int); + } +} + +/// `mov dst32, src32`: the low 32 bits of `src`, zero-extended, into `dst`. +pub proof fn lemma_step_mov_rr32(cu: spec_fn(nat) -> nat, m: x64::State, dst: u8, src: u8) + requires + x64::wf(m), + dst < 16, + src < 16, + x64::canonical( + m.rip + x64::instr_len(x64::Instr::MovRR { sz: x64::Size::W32, dst: dst, src: src }), + ), + ensures + x64::step(cu, m, x64::Instr::MovRR { sz: x64::Size::W32, dst: dst, src: src }) == Some( + x64::State { + rgf: m.rgf.update(dst as int, (m.rgf[src as int] % 0x1_0000_0000) as u64), + rip: m.rip + x64::instr_len( + x64::Instr::MovRR { sz: x64::Size::W32, dst: dst, src: src }, + ), + ..m + }, + ), +{ + super::memlemmas::lemma_p2_consts(); + let v = m.rgf[src as int] as int % 0x1_0000_0000; + assert(x64::loghead(64, v) == v); +} + +} // verus! diff --git a/crates/clazel-jit/src/templates.rs b/crates/clazel-jit/src/templates.rs new file mode 100644 index 000000000..af62f557e --- /dev/null +++ b/crates/clazel-jit/src/templates.rs @@ -0,0 +1,277 @@ +//! The templates (jit.md J.9): for each LIR instruction of the first slice, +//! at a known stack height, a fixed list of x86-64 instructions over the +//! scratch registers `rax`, `rcx` and `rdx`, reading and writing frame slots. +//! These are spec functions: the executable emitter (`emit`) is proven equal +//! to them, and the refinement proofs (`proofs`) are about them. +//! +//! Every template has the shape *load operands from slots into scratch +//! registers, compute in registers, store the result to a slot*. The +//! operand stack's top is slot `nl + h - 1` for `nl` locals and height `h` +//! (see `spec`, modelling choice 2), so a binary operation at height `h` +//! reads slots `nl + h - 2` and `nl + h - 1` and writes the first. +//! +//! No template reads memory other than the frame, writes anything but the +//! frame and scratch registers, or touches `r15` or `rsp`. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `mov r, [r15 + 264 + 8 j]`: load frame slot `j` (64 bits) into `dst`. +pub open spec fn load_slot(dst: u8, j: nat) -> x64::Instr { + x64::Instr::MovLoad { sz: x64::Size::W64, dst: dst, mem: slot_mem(j) } +} + +/// `mov [r15 + 264 + 8 j], r`: store `src` (64 bits) to frame slot `j`. +pub open spec fn store_slot(j: nat, src: u8) -> x64::Instr { + x64::Instr::MovStore { sz: x64::Size::W64, mem: slot_mem(j), src: src } +} + +/// The condition code of `setcc` for an integer comparison, read after +/// `cmp lhs, rhs` (so the flags are those of `lhs - rhs`): `e`, `ne`, `l` +/// (signed less), `b` (below: carry), `g`, `a`, `le`, `be`, `ge`, `ae`. +pub open spec fn cc_of(c: lir::ICmp) -> u8 { + match c { + lir::ICmp::Eq => 4, + lir::ICmp::Ne => 5, + lir::ICmp::LtS => 12, + lir::ICmp::LtU => 2, + lir::ICmp::GtS => 15, + lir::ICmp::GtU => 7, + lir::ICmp::LeS => 14, + lir::ICmp::LeU => 6, + lir::ICmp::GeS => 13, + lir::ICmp::GeU => 3, + } +} + +/// The x86-64 two-operand operation for an integer binary operator with a +/// one-instruction register-register form (`add`, `sub`, `and`, `or`, `xor`). +pub open spec fn alu_of(o: lir::IBin) -> Option { + match o { + lir::IBin::Add => Some(x64::AluOp::Add), + lir::IBin::Sub => Some(x64::AluOp::Sub), + lir::IBin::And => Some(x64::AluOp::And), + lir::IBin::Or => Some(x64::AluOp::Or), + lir::IBin::Xor => Some(x64::AluOp::Xor), + _ => None, + } +} + +/// The x86-64 shift for an integer shift operator (`shl`, `shr`, `sar`). +pub open spec fn shift_of(o: lir::IBin) -> Option { + match o { + lir::IBin::Shl => Some(x64::ShiftOp::Shl), + lir::IBin::ShrU => Some(x64::ShiftOp::Shr), + lir::IBin::ShrS => Some(x64::ShiftOp::Sar), + _ => None, + } +} + +/// The instructions that load the slots `ls` (register, slot pairs) one after +/// the other. +pub open spec fn loads_seq(ls: Seq<(u8, nat)>) -> Seq + decreases ls.len(), +{ + if ls.len() == 0 { + seq![] + } else { + loads_seq(ls.drop_last()) + seq![load_slot(ls.last().0, ls.last().1)] + } +} + +/// A template: the loads, the register-only middle, and one store of `rax` to +/// a slot. +pub open spec fn shape( + ls: Seq<(u8, nat)>, + mid: Seq, + ts: nat, +) -> Seq { + loads_seq(ls) + mid + seq![store_slot(ts, rax())] +} + +/// `ra := ra op rb` with the operands in `rax` and `rcx`: the operator's +/// instruction (`add sub and or xor imul`, or a shift by `cl`). +pub open spec fn int_op_instr(o: lir::IBin, sz: x64::Size) -> x64::Instr { + match alu_of(o) { + Some(alu) => x64::Instr::AluRR { alu: alu, sz: sz, dst: rax(), src: rcx() }, + None => match shift_of(o) { + Some(sh) => x64::Instr::ShiftCl { shift: sh, sz: sz, r: rax() }, + None => x64::Instr::ImulRR { sz: sz, dst: rax(), src: rcx() }, + }, + } +} + +/// `mov eax, 0; cmp rcx, rdx; setcc al`: `rax` is 0 or 1. The `mov` is +/// before the compare because it leaves the flags alone. +pub open spec fn cmp_mid(c: lir::ICmp, sz: x64::Size) -> Seq { + seq![ + x64::Instr::MovImm32 { dst: rax(), imm_u32: 0 }, + x64::Instr::AluRR { alu: x64::AluOp::Cmp, sz: sz, dst: rcx(), src: rdx() }, + x64::Instr::Setcc { cc: cc_of(c), dst: rax() }, + ] +} + +/// `mov eax, 0; test rcx, rcx; sete al`. +pub open spec fn eqz_mid(sz: x64::Size) -> Seq { + seq![ + x64::Instr::MovImm32 { dst: rax(), imm_u32: 0 }, + x64::Instr::TestRR { sz: sz, a: rcx(), b: rcx() }, + x64::Instr::Setcc { cc: 4, dst: rax() }, + ] +} + +/// `mov rcx, mask; xor/and rax, rcx`: `f64.neg` (flip the sign bit) and +/// `f64.abs` (clear it) on the bits. +pub open spec fn mask_mid(alu: x64::AluOp, mask: u64) -> Seq { + seq![ + x64::Instr::MovImm64 { dst: rcx(), imm_u64: mask }, + x64::Instr::AluRR { alu: alu, sz: x64::Size::W64, dst: rax(), src: rcx() }, + ] +} + +/// `mov ecx, 0x80000000; xor rax, rcx; sub rax, rcx`: sign-extends the low +/// 32 bits of `rax`, `(x ^ 2^31) - 2^31`. +pub open spec fn exts_mid() -> Seq { + seq![ + x64::Instr::MovImm32 { dst: rcx(), imm_u32: 0x8000_0000 }, + x64::Instr::AluRR { + alu: x64::AluOp::Xor, + sz: x64::Size::W64, + dst: rax(), + src: rcx(), + }, + x64::Instr::AluRR { + alu: x64::AluOp::Sub, + sz: x64::Size::W64, + dst: rax(), + src: rcx(), + }, + ] +} + +/// `mov eax, eax`: the low 32 bits, zero-extended. +pub open spec fn wrap_mid() -> Seq { + seq![x64::Instr::MovRR { sz: x64::Size::W32, dst: rax(), src: rax() }] +} + +/// `test edx, edx; cmove rax, rcx`: `rax` (the first value) stays unless the +/// condition in `rdx` is 0, when it becomes `rcx` (the second). +pub open spec fn select_mid() -> Seq { + seq![ + x64::Instr::TestRR { sz: x64::Size::W32, a: rdx(), b: rdx() }, + x64::Instr::Cmovcc { cc: 4, sz: x64::Size::W64, dst: rax(), src: rcx() }, + ] +} + +/// The template of an integer arithmetic, logic or shift instruction at +/// slots `t` (left operand and result) and `t + 1` (right operand): `mov rax, +/// [t]; mov rcx, [t+1]; op rax, rcx; mov [t], rax`. +pub open spec fn int_bin_tmpl(o: lir::IBin, sz: x64::Size, t: nat) -> Seq { + shape(seq![(rax(), t), (rcx(), t + 1)], seq![int_op_instr(o, sz)], t) +} + +/// The template of an integer comparison at slots `t` and `t + 1`: `mov rcx, +/// [t]; mov rdx, [t+1]; mov eax, 0; cmp rcx, rdx; setcc al; mov [t], rax`. +pub open spec fn int_cmp_tmpl(c: lir::ICmp, sz: x64::Size, t: nat) -> Seq { + shape(seq![(rcx(), t), (rdx(), t + 1)], cmp_mid(c, sz), t) +} + +/// The template of `eqz` at slot `t`. +pub open spec fn eqz_tmpl(sz: x64::Size, t: nat) -> Seq { + shape(seq![(rcx(), t)], eqz_mid(sz), t) +} + +/// The template of a binary instruction at stack height `h` with `nl` +/// locals. An empty list for an instruction outside the slice. +pub open spec fn bin_tmpl(b: lir::BinOp, nl: nat, h: nat) -> Seq { + let t = (nl + h - 2) as nat; + match b { + lir::BinOp::I32(o) => int_bin_tmpl(o, x64::Size::W32, t), + lir::BinOp::I64(o) => int_bin_tmpl(o, x64::Size::W64, t), + lir::BinOp::I32Cmp(c) => int_cmp_tmpl(c, x64::Size::W32, t), + lir::BinOp::I64Cmp(c) => int_cmp_tmpl(c, x64::Size::W64, t), + lir::BinOp::F64(_) => seq![], + lir::BinOp::F64Cmp(_) => seq![], + } +} + +/// The template of a unary instruction at stack height `h` with `nl` locals. +/// The conversions that change no bits of the word (`extend_i32_u`, the +/// reinterprets) are empty: an `i32` is stored zero-extended, and an `f64` +/// is its bits. +pub open spec fn un_tmpl(u: lir::UnOp, nl: nat, h: nat) -> Seq { + let t = (nl + h - 1) as nat; + match u { + lir::UnOp::I32Eqz => eqz_tmpl(x64::Size::W32, t), + lir::UnOp::I64Eqz => eqz_tmpl(x64::Size::W64, t), + lir::UnOp::F64Neg => shape( + seq![(rax(), t)], + mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000), + t, + ), + lir::UnOp::F64Abs => shape( + seq![(rax(), t)], + mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF), + t, + ), + lir::UnOp::I64ExtendI32S => shape(seq![(rax(), t)], exts_mid(), t), + lir::UnOp::I32WrapI64 => shape(seq![(rax(), t)], wrap_mid(), t), + lir::UnOp::I64ExtendI32U => seq![], + lir::UnOp::F64ReinterpretI64 => seq![], + lir::UnOp::I64ReinterpretF64 => seq![], + lir::UnOp::F64Sqrt => seq![], + lir::UnOp::F64ConvertI32S => seq![], + lir::UnOp::F64ConvertI32U => seq![], + lir::UnOp::F64ConvertI64S => seq![], + lir::UnOp::F64ConvertI64U => seq![], + } +} + +/// The load of a constant into `rax`: `mov eax, c` (zero-extending) for an +/// `i32`, `mov rax, c` for an `i64` or an `f64`'s bits. +pub open spec fn const_instr(v: lir::Val) -> x64::Instr { + match v { + lir::Val::I32(c) => x64::Instr::MovImm32 { dst: rax(), imm_u32: c }, + lir::Val::I64(c) => x64::Instr::MovImm64 { dst: rax(), imm_u64: c }, + lir::Val::F64(c) => x64::Instr::MovImm64 { dst: rax(), imm_u64: c }, + } +} + +/// **The template of one instruction** at stack height `h` with `nl` locals. +pub open spec fn tmpl(op: Op, nl: nat, h: nat) -> Seq { + match op { + Op::Nop => seq![], + Op::Drop => seq![], + // the new top is slot nl + h + Op::Const(v) => shape(seq![], seq![const_instr(v)], nl + h), + Op::LocalGet(x) => shape(seq![(rax(), x as nat)], seq![], nl + h), + // the top is slot nl + h - 1 + Op::LocalSet(x) => shape(seq![(rax(), (nl + h - 1) as nat)], seq![], x as nat), + Op::LocalTee(x) => shape(seq![(rax(), (nl + h - 1) as nat)], seq![], x as nat), + // v1, v2, c at slots t, t + 1, t + 2: c == 0 picks v2 (cmove) + Op::Select => { + let t = (nl + h - 3) as nat; + shape(seq![(rax(), t), (rcx(), t + 1), (rdx(), t + 2)], select_mid(), t) + }, + Op::Bin(b) => bin_tmpl(b, nl, h), + Op::Un(u) => un_tmpl(u, nl, h), + } +} + +/// The code of a program: the templates one after the other, each at the +/// stack height the instructions before it leave. +pub open spec fn code_of(ops: Seq, nl: nat, h: nat) -> Seq + decreases ops.len(), +{ + if ops.len() == 0 { + seq![] + } else { + code_of(ops.drop_last(), nl, h) + tmpl(ops.last(), nl, height_after(ops.drop_last(), h)) + } +} + +} // verus! diff --git a/crates/clazel-jit/src/tier.rs b/crates/clazel-jit/src/tier.rs new file mode 100644 index 000000000..927c34f34 --- /dev/null +++ b/crates/clazel-jit/src/tier.rs @@ -0,0 +1,292 @@ +//! The tiering counters' policy (docs/phases/jit.md items J.10 and J.11): when +//! does a function in the interpreter loop get compiled? +//! +//! > Compile after N entries or loop back-edges ... Code that runs once stays +//! > in the interpreter loop. (J.10) +//! +//! > The estimate is 30 to 150 runs per function at the baseline tier, which +//! > is where the tiering threshold N starts. (J.11) +//! +//! The defaults below are those starting values, **not measurements**: J.11's +//! break-even measurement (compile, validation and mapping cost against the +//! interpreter's per-run cost) sets them later. +//! +//! The policy is plain functions over plain numbers, so it is tested at small +//! values and is easy to read. It has no clock and no randomness. +//! +//! # Backoff +//! +//! A function that has been evicted (or whose version changed, through +//! inline-cache churn) must earn its next compilation again, at a threshold +//! that doubles with each such *strike*, up to [`MAX_BACKOFF_SHIFT`] doublings. +//! Without this a page that cycles through more hot functions than the +//! budget holds would compile, evict and recompile for ever. + +use crate::limits::Fallback; + +/// Entries before a function is compiled (inside J.11's 30 to 150). +pub const DEFAULT_ENTRY_THRESHOLD: u32 = 50; + +/// Loop back-edges before a function is compiled. A back-edge is cheaper than +/// a call, so the number is larger; it is what lets a function that is entered +/// once and loops for ever be compiled (J.10: entry at a loop head). +pub const DEFAULT_BACK_EDGE_THRESHOLD: u32 = 1000; + +/// The threshold doubles per strike up to this many doublings (a factor of +/// 1024). +pub const MAX_BACKOFF_SHIFT: u32 = 10; + +/// The thresholds that trigger a compile. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Thresholds { + /// Entries (calls) of the function. + pub entries: u32, + /// Loop back-edges taken in the function. + pub back_edges: u32, +} + +impl Default for Thresholds { + fn default() -> Thresholds { + Thresholds { + entries: DEFAULT_ENTRY_THRESHOLD, + back_edges: DEFAULT_BACK_EDGE_THRESHOLD, + } + } +} + +/// One function version's counters. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Counters { + /// Entries so far. + pub entries: u32, + /// Back-edges so far. + pub back_edges: u32, +} + +impl Counters { + /// Count an entry. Saturates. + pub fn enter(&mut self) { + self.entries = self.entries.saturating_add(1); + } + + /// Count a back-edge. Saturates. + pub fn back_edge(&mut self) { + self.back_edges = self.back_edges.saturating_add(1); + } +} + +/// Where a function is in its life. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum State { + /// In the interpreter loop, counting. + Interpreting, + /// A compile was requested and has not come back. No second request is + /// made meanwhile. + Compiling, + /// Its blob is (or was, until an eviction says otherwise) in the cache. + Compiled, + /// Never to be compiled at this version, for this reason. + Refused(Fallback), +} + +/// What made the function hot. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Trigger { + /// Enough entries. + Entries, + /// Enough loop back-edges. + BackEdges, +} + +/// What to do now. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Decision { + /// Keep interpreting. + Stay, + /// Compile the function. + Compile(Trigger), +} + +/// `base`, doubled once per strike, at most [`MAX_BACKOFF_SHIFT`] times, and +/// never below 1. +pub fn threshold_after(base: u32, strikes: u32) -> u32 { + let doublings = strikes.min(MAX_BACKOFF_SHIFT); + base.max(1).saturating_mul(2u32.saturating_pow(doublings)) +} + +/// Whether to compile a function with these counters and `strikes`. +/// +/// Only a function that is `Interpreting` is ever compiled; entries are +/// checked before back-edges, so the trigger is the first threshold reached. +pub fn decide(c: &Counters, t: &Thresholds, strikes: u32, state: State) -> Decision { + if state != State::Interpreting { + return Decision::Stay; + } + if c.entries >= threshold_after(t.entries, strikes) { + Decision::Compile(Trigger::Entries) + } else if c.back_edges >= threshold_after(t.back_edges, strikes) { + Decision::Compile(Trigger::BackEdges) + } else { + Decision::Stay + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn default_entry_threshold_is_in_the_estimated_range() { + const { assert!(DEFAULT_ENTRY_THRESHOLD >= 30 && DEFAULT_ENTRY_THRESHOLD <= 150) }; + const { assert!(DEFAULT_BACK_EDGE_THRESHOLD > DEFAULT_ENTRY_THRESHOLD) }; + } + + #[test] + fn code_that_runs_once_stays_interpreted() { + let t = Thresholds::default(); + let mut c = Counters::default(); + c.enter(); + assert_eq!(decide(&c, &t, 0, State::Interpreting), Decision::Stay); + } + + #[test] + fn compiles_exactly_at_the_entry_threshold() { + let t = Thresholds { + entries: 5, + back_edges: 100, + }; + let mut c = Counters::default(); + for n in 1..=5u32 { + c.enter(); + let want = if n < 5 { + Decision::Stay + } else { + Decision::Compile(Trigger::Entries) + }; + assert_eq!( + decide(&c, &t, 0, State::Interpreting), + want, + "after {n} entries" + ); + } + } + + #[test] + fn a_loop_entered_once_compiles_on_back_edges() { + let t = Thresholds { + entries: 5, + back_edges: 7, + }; + let mut c = Counters::default(); + c.enter(); + for _ in 0..6 { + c.back_edge(); + } + assert_eq!(decide(&c, &t, 0, State::Interpreting), Decision::Stay); + c.back_edge(); + assert_eq!( + decide(&c, &t, 0, State::Interpreting), + Decision::Compile(Trigger::BackEdges) + ); + } + + #[test] + fn entries_win_when_both_are_reached() { + let t = Thresholds { + entries: 1, + back_edges: 1, + }; + let c = Counters { + entries: 1, + back_edges: 1, + }; + assert_eq!( + decide(&c, &t, 0, State::Interpreting), + Decision::Compile(Trigger::Entries) + ); + } + + #[test] + fn only_an_interpreting_function_is_compiled() { + let t = Thresholds { + entries: 1, + back_edges: 1, + }; + let c = Counters { + entries: 1_000, + back_edges: 1_000, + }; + for st in [ + State::Compiling, + State::Compiled, + State::Refused(Fallback::FunctionTooLarge), + ] { + assert_eq!(decide(&c, &t, 0, st), Decision::Stay); + } + } + + #[test] + fn strikes_double_the_threshold_up_to_a_cap() { + assert_eq!(threshold_after(50, 0), 50); + assert_eq!(threshold_after(50, 1), 100); + assert_eq!(threshold_after(50, 3), 400); + assert_eq!(threshold_after(50, MAX_BACKOFF_SHIFT), 50 * 1024); + assert_eq!(threshold_after(50, MAX_BACKOFF_SHIFT + 5), 50 * 1024); + assert_eq!(threshold_after(50, u32::MAX), 50 * 1024); + } + + #[test] + fn threshold_never_overflows_or_reaches_zero() { + assert_eq!(threshold_after(0, 0), 1); + assert_eq!(threshold_after(u32::MAX, 10), u32::MAX); + let t = Thresholds { + entries: 0, + back_edges: 0, + }; + assert_eq!( + decide(&Counters::default(), &t, 0, State::Interpreting), + Decision::Stay + ); + } + + #[test] + fn a_struck_function_needs_more_entries() { + let t = Thresholds { + entries: 4, + back_edges: 1000, + }; + let mut c = Counters::default(); + for _ in 0..4 { + c.enter(); + } + assert_eq!( + decide(&c, &t, 0, State::Interpreting), + Decision::Compile(Trigger::Entries) + ); + assert_eq!(decide(&c, &t, 1, State::Interpreting), Decision::Stay); + for _ in 0..4 { + c.enter(); + } + assert_eq!( + decide(&c, &t, 1, State::Interpreting), + Decision::Compile(Trigger::Entries) + ); + } + + #[test] + fn counters_saturate() { + let mut c = Counters { + entries: u32::MAX, + back_edges: u32::MAX, + }; + c.enter(); + c.back_edge(); + assert_eq!( + c, + Counters { + entries: u32::MAX, + back_edges: u32::MAX + } + ); + } +} diff --git a/crates/clazel-jit/src/tlemmas.rs b/crates/clazel-jit/src/tlemmas.rs new file mode 100644 index 000000000..3aa98c00e --- /dev/null +++ b/crates/clazel-jit/src/tlemmas.rs @@ -0,0 +1,353 @@ +//! Facts about the template definitions the emitter's proof needs: a template +//! is the explicit list of instructions the emitter pushes, and every +//! instruction of the code is in the x86-64 subset. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `shape` with no loads. +pub proof fn lemma_shape0(mid: Seq, ts: nat) + ensures + super::templates::shape(seq![], mid, ts) == mid + seq![super::templates::store_slot(ts, rax())], +{ + assert(super::templates::loads_seq(Seq::<(u8, nat)>::empty()) == Seq::::empty()); + assert(Seq::::empty() + mid =~= mid); +} + +/// `shape` with one load. +pub proof fn lemma_shape1(r: u8, j: nat, mid: Seq, ts: nat) + ensures + super::templates::shape(seq![(r, j)], mid, ts) == seq![super::templates::load_slot(r, j)] + mid + seq![super::templates::store_slot(ts, rax())], +{ + let ls = seq![(r, j)]; + assert(ls.drop_last() =~= Seq::<(u8, nat)>::empty()); + assert(ls.last() == (r, j)); + assert(super::templates::loads_seq(Seq::<(u8, nat)>::empty()) == Seq::::empty()); + assert(super::templates::loads_seq(ls) =~= seq![super::templates::load_slot(r, j)]); +} + +/// `shape` with two loads. +pub proof fn lemma_shape2(r1: u8, j1: nat, r2: u8, j2: nat, mid: Seq, ts: nat) + ensures + super::templates::shape(seq![(r1, j1), (r2, j2)], mid, ts) == seq![super::templates::load_slot(r1, j1), super::templates::load_slot(r2, j2)] + + mid + seq![super::templates::store_slot(ts, rax())], +{ + let ls = seq![(r1, j1), (r2, j2)]; + assert(ls.drop_last() =~= seq![(r1, j1)]); + assert(ls.last() == (r2, j2)); + lemma_shape1(r1, j1, seq![], 0); + assert(super::templates::loads_seq(seq![(r1, j1)]) =~= seq![super::templates::load_slot(r1, j1)]) by { + let l1 = seq![(r1, j1)]; + assert(l1.drop_last() =~= Seq::<(u8, nat)>::empty()); + assert(l1.last() == (r1, j1)); + assert(super::templates::loads_seq(Seq::<(u8, nat)>::empty()) == Seq::::empty()); + } + assert(super::templates::loads_seq(ls) =~= seq![super::templates::load_slot(r1, j1), super::templates::load_slot(r2, j2)]); +} + +/// `shape` with three loads. +pub proof fn lemma_shape3( + r1: u8, + j1: nat, + r2: u8, + j2: nat, + r3: u8, + j3: nat, + mid: Seq, + ts: nat, +) + ensures + super::templates::shape(seq![(r1, j1), (r2, j2), (r3, j3)], mid, ts) == seq![ + super::templates::load_slot(r1, j1), + super::templates::load_slot(r2, j2), + super::templates::load_slot(r3, j3), + ] + mid + seq![super::templates::store_slot(ts, rax())], +{ + let ls = seq![(r1, j1), (r2, j2), (r3, j3)]; + assert(ls.drop_last() =~= seq![(r1, j1), (r2, j2)]); + assert(ls.last() == (r3, j3)); + let l2 = seq![(r1, j1), (r2, j2)]; + assert(l2.drop_last() =~= seq![(r1, j1)]); + assert(l2.last() == (r2, j2)); + let l1 = seq![(r1, j1)]; + assert(l1.drop_last() =~= Seq::<(u8, nat)>::empty()); + assert(l1.last() == (r1, j1)); + assert(super::templates::loads_seq(Seq::<(u8, nat)>::empty()) == Seq::::empty()); + assert(super::templates::loads_seq(l1) =~= seq![super::templates::load_slot(r1, j1)]); + assert(super::templates::loads_seq(l2) =~= seq![super::templates::load_slot(r1, j1), super::templates::load_slot(r2, j2)]); + assert(super::templates::loads_seq(ls) =~= seq![super::templates::load_slot(r1, j1), super::templates::load_slot(r2, j2), super::templates::load_slot(r3, j3)]); +} + +/// The slot operand of a frame slot below 31 is a valid memory operand. +pub proof fn lemma_slot_valid(j: nat) + requires + j < max_slots(), + ensures + x64::mem_ok(slot_mem(j)), +{ +} + +/// A load or store of an existing slot with scratch registers is in the subset. +pub proof fn lemma_ls_valid(r: u8, j: nat) + requires + r < 16, + j < max_slots(), + ensures + x64::valid_instr(super::templates::load_slot(r, j)), + x64::valid_instr(super::templates::store_slot(j, r)), +{ + lemma_slot_valid(j); +} + +/// Every instruction of a template is in the subset. +pub proof fn lemma_tmpl_valid(op: Op, nl: nat, h: nat) + requires + op_ok(op, nl, h), + ensures + forall|k: int| 0 <= k < super::templates::tmpl(op, nl, h).len() ==> x64::valid_instr(#[trigger] super::templates::tmpl(op, nl, h)[k]), +{ + assert(max_slots() == 31) by { + lemma_abi_consts_t(); + } + let code = super::templates::tmpl(op, nl, h); + assert forall|k: int| 0 <= k < code.len() implies x64::valid_instr(#[trigger] code[k]) by { + match op { + Op::Nop => {}, + Op::Drop => {}, + Op::Const(v) => { + lemma_shape0(seq![super::templates::const_instr(v)], nl + h); + lemma_ls_valid(0, nl + h); + assert(code =~= seq![super::templates::const_instr(v), super::templates::store_slot(nl + h, rax())]); + }, + Op::LocalGet(x) => { + lemma_shape1(rax(), x as nat, seq![], nl + h); + lemma_ls_valid(0, nl + h); + lemma_ls_valid(0, x as nat); + assert(code =~= seq![super::templates::load_slot(rax(), x as nat), super::templates::store_slot(nl + h, rax())]); + }, + Op::LocalSet(x) => { + lemma_shape1(rax(), (nl + h - 1) as nat, seq![], x as nat); + lemma_ls_valid(0, x as nat); + lemma_ls_valid(0, (nl + h - 1) as nat); + assert(code =~= seq![super::templates::load_slot(rax(), (nl + h - 1) as nat), super::templates::store_slot(x as nat, rax())]); + }, + Op::LocalTee(x) => { + lemma_shape1(rax(), (nl + h - 1) as nat, seq![], x as nat); + lemma_ls_valid(0, x as nat); + lemma_ls_valid(0, (nl + h - 1) as nat); + assert(code =~= seq![super::templates::load_slot(rax(), (nl + h - 1) as nat), super::templates::store_slot(x as nat, rax())]); + }, + Op::Select => { + let t = (nl + h - 3) as nat; + lemma_shape3(rax(), t, rcx(), t + 1, rdx(), t + 2, super::templates::select_mid(), t); + lemma_ls_valid(0, t); + lemma_ls_valid(1, t + 1); + lemma_ls_valid(2, t + 2); + assert(code =~= seq![ + super::templates::load_slot(rax(), t), + super::templates::load_slot(rcx(), t + 1), + super::templates::load_slot(rdx(), t + 2), + super::templates::select_mid()[0], + super::templates::select_mid()[1], + super::templates::store_slot(t, rax()), + ]); + }, + Op::Bin(b) => { + let t = (nl + h - 2) as nat; + match b { + lir::BinOp::I32(o) => { + lemma_shape2(rax(), t, rcx(), t + 1, seq![super::templates::int_op_instr(o, x64::Size::W32)], t); + lemma_ls_valid(0, t); + lemma_ls_valid(1, t + 1); + assert(code =~= seq![ + super::templates::load_slot(rax(), t), + super::templates::load_slot(rcx(), t + 1), + super::templates::int_op_instr(o, x64::Size::W32), + super::templates::store_slot(t, rax()), + ]); + }, + lir::BinOp::I64(o) => { + lemma_shape2(rax(), t, rcx(), t + 1, seq![super::templates::int_op_instr(o, x64::Size::W64)], t); + lemma_ls_valid(0, t); + lemma_ls_valid(1, t + 1); + assert(code =~= seq![ + super::templates::load_slot(rax(), t), + super::templates::load_slot(rcx(), t + 1), + super::templates::int_op_instr(o, x64::Size::W64), + super::templates::store_slot(t, rax()), + ]); + }, + lir::BinOp::I32Cmp(c) => { + lemma_shape2(rcx(), t, rdx(), t + 1, super::templates::cmp_mid(c, x64::Size::W32), t); + lemma_ls_valid(1, t); + lemma_ls_valid(2, t + 1); + lemma_ls_valid(0, t); + assert(code =~= seq![ + super::templates::load_slot(rcx(), t), + super::templates::load_slot(rdx(), t + 1), + super::templates::cmp_mid(c, x64::Size::W32)[0], + super::templates::cmp_mid(c, x64::Size::W32)[1], + super::templates::cmp_mid(c, x64::Size::W32)[2], + super::templates::store_slot(t, rax()), + ]); + }, + lir::BinOp::I64Cmp(c) => { + lemma_shape2(rcx(), t, rdx(), t + 1, super::templates::cmp_mid(c, x64::Size::W64), t); + lemma_ls_valid(1, t); + lemma_ls_valid(2, t + 1); + lemma_ls_valid(0, t); + assert(code =~= seq![ + super::templates::load_slot(rcx(), t), + super::templates::load_slot(rdx(), t + 1), + super::templates::cmp_mid(c, x64::Size::W64)[0], + super::templates::cmp_mid(c, x64::Size::W64)[1], + super::templates::cmp_mid(c, x64::Size::W64)[2], + super::templates::store_slot(t, rax()), + ]); + }, + lir::BinOp::F64(_) => { + assert(false); + }, + lir::BinOp::F64Cmp(_) => { + assert(false); + }, + } + }, + Op::Un(u) => { + let t = (nl + h - 1) as nat; + lemma_ls_valid(0, t); + lemma_ls_valid(1, t); + match u { + lir::UnOp::I32Eqz => { + lemma_shape1(rcx(), t, super::templates::eqz_mid(x64::Size::W32), t); + assert(code =~= seq![ + super::templates::load_slot(rcx(), t), + super::templates::eqz_mid(x64::Size::W32)[0], + super::templates::eqz_mid(x64::Size::W32)[1], + super::templates::eqz_mid(x64::Size::W32)[2], + super::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::I64Eqz => { + lemma_shape1(rcx(), t, super::templates::eqz_mid(x64::Size::W64), t); + assert(code =~= seq![ + super::templates::load_slot(rcx(), t), + super::templates::eqz_mid(x64::Size::W64)[0], + super::templates::eqz_mid(x64::Size::W64)[1], + super::templates::eqz_mid(x64::Size::W64)[2], + super::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::F64Neg => { + lemma_shape1(rax(), t, super::templates::mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000), t); + assert(code =~= seq![ + super::templates::load_slot(rax(), t), + super::templates::mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000)[0], + super::templates::mask_mid(x64::AluOp::Xor, 0x8000_0000_0000_0000)[1], + super::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::F64Abs => { + lemma_shape1(rax(), t, super::templates::mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF), t); + assert(code =~= seq![ + super::templates::load_slot(rax(), t), + super::templates::mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF)[0], + super::templates::mask_mid(x64::AluOp::And, 0x7FFF_FFFF_FFFF_FFFF)[1], + super::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::I64ExtendI32S => { + lemma_shape1(rax(), t, super::templates::exts_mid(), t); + assert(code =~= seq![ + super::templates::load_slot(rax(), t), + super::templates::exts_mid()[0], + super::templates::exts_mid()[1], + super::templates::exts_mid()[2], + super::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::I32WrapI64 => { + lemma_shape1(rax(), t, super::templates::wrap_mid(), t); + assert(code =~= seq![ + super::templates::load_slot(rax(), t), + super::templates::wrap_mid()[0], + super::templates::store_slot(t, rax()), + ]); + }, + lir::UnOp::I64ExtendI32U => {}, + lir::UnOp::F64ReinterpretI64 => {}, + lir::UnOp::I64ReinterpretF64 => {}, + lir::UnOp::F64Sqrt => { + assert(false); + }, + lir::UnOp::F64ConvertI32S => { + assert(false); + }, + lir::UnOp::F64ConvertI32U => { + assert(false); + }, + lir::UnOp::F64ConvertI64S => { + assert(false); + }, + lir::UnOp::F64ConvertI64U => { + assert(false); + }, + } + }, + } + } +} + +/// The ABI constants the lemmas above need. +pub proof fn lemma_abi_consts_t() + ensures + max_slots() == 31, +{ +} + +/// Every instruction of a program's code is in the subset. +pub proof fn lemma_code_of_valid(ops: Seq, nl: nat, h0: nat) + requires + prog_ok(ops, nl, h0), + ensures + super::contract::all_valid_instrs(super::templates::code_of(ops, nl, h0)), + decreases ops.len(), +{ + if ops.len() > 0 { + let pre = ops.drop_last(); + let last = ops.last(); + let hl = height_after(pre, h0); + lemma_code_of_valid(pre, nl, h0); + lemma_tmpl_valid(last, nl, hl); + let ca = super::templates::code_of(pre, nl, h0); + let cb = super::templates::tmpl(last, nl, hl); + assert(super::templates::code_of(ops, nl, h0) == ca + cb); + assert forall|k: int| 0 <= k < (ca + cb).len() implies x64::valid_instr( + #[trigger] (ca + cb)[k], + ) by { + if k < ca.len() { + assert((ca + cb)[k] == ca[k]); + } else { + assert((ca + cb)[k] == cb[k - ca.len()]); + } + } + } +} + +/// The exit sequence is in the subset. +pub proof fn lemma_exit_valid(c: u32) + ensures + super::contract::all_valid_instrs(exit_seq(c)), +{ + assert forall|k: int| 0 <= k < exit_seq(c).len() implies x64::valid_instr( + #[trigger] exit_seq(c)[k], + ) by { + assert(k == 0 || k == 1 || k == 2); + } +} + +} // verus! diff --git a/crates/clazel-jit/tests/validator.rs b/crates/clazel-jit/tests/validator.rs new file mode 100644 index 000000000..f39b96a73 --- /dev/null +++ b/crates/clazel-jit/tests/validator.rs @@ -0,0 +1,395 @@ +//! The back end never produces a blob the validator refuses (docs/phases/jit.md +//! J.9: "a test checks that the back end never produces a blob the validator +//! refuses"). +//! +//! The blob is `emit_blob`'s output: the encoded templates and the exit +//! sequence. The validator is `clazel-codecheck`'s executable one +//! ([`clazel_codecheck::validate::validate`], proven sound and complete for +//! its spec), entered at offset 0. +//! +//! 1. **Every template.** For each instruction of the first slice, with every +//! operator and comparison, a program that sets up the operands and runs it. +//! 2. **Generated programs.** A few thousand small random well-formed +//! programs (a fixed seed), each of which must have a blob that is +//! accepted. +//! 3. **What has no template is refused by the back end, not emitted**: the +//! `f64` arithmetic, the rotates, a bad local, a short stack, a frame of +//! more than 31 slots. +//! 4. **Controls.** The validator does refuse the blob when its last byte is +//! changed or cut off, so a `true` above is not the validator saying yes +//! to everything. +//! +//! This test is evidence, not proof: the proofs are in the `verus!` modules +//! (`compose::lemma_prog_refines` for the meaning of the code, +//! `emit::emit_blob_impl` for the bytes). That every blob is accepted is not +//! yet a theorem. + +use clazel_codecheck::validate::validate; +use clazel_jit::api::Backend; +use clazel_jit::emit::X64Backend; +use clazel_jit::spec::Op; +use clazel_lir::spec::{BinOp, FBin, FCmp, IBin, ICmp, UnOp, Val}; + +// The spec's types are not `Copy`, so each use builds a fresh value. + +const IBINS: [fn() -> IBin; 9] = [ + || IBin::Add, + || IBin::Sub, + || IBin::Mul, + || IBin::And, + || IBin::Or, + || IBin::Xor, + || IBin::Shl, + || IBin::ShrU, + || IBin::ShrS, +]; + +const ICMPS: [fn() -> ICmp; 10] = [ + || ICmp::Eq, + || ICmp::Ne, + || ICmp::LtS, + || ICmp::LtU, + || ICmp::GtS, + || ICmp::GtU, + || ICmp::LeS, + || ICmp::LeU, + || ICmp::GeS, + || ICmp::GeU, +]; + +const UNS: [fn() -> UnOp; 8] = [ + || UnOp::I32Eqz, + || UnOp::I64Eqz, + || UnOp::F64Neg, + || UnOp::F64Abs, + || UnOp::I32WrapI64, + || UnOp::I64ExtendI32S, + || UnOp::I64ExtendI32U, + || UnOp::F64ReinterpretI64, +]; + +/// Every operation of the slice that is one constructor away, with the +/// number of operands it takes. +fn all_ops() -> Vec<(Op, usize)> { + let mut v = vec![ + (Op::Nop, 0), + (Op::Drop, 1), + (Op::Select, 3), + (Op::LocalGet(0), 0), + (Op::LocalGet(1), 0), + (Op::LocalSet(0), 1), + (Op::LocalSet(1), 1), + (Op::LocalTee(0), 1), + (Op::LocalTee(1), 1), + (Op::Const(Val::I32(0)), 0), + (Op::Const(Val::I32(u32::MAX)), 0), + (Op::Const(Val::I64(0x1234_5678_9abc_def0)), 0), + (Op::Const(Val::F64(0x7ff8_0000_0000_0001)), 0), + (Op::Un(UnOp::I64ReinterpretF64), 1), + ]; + for u in UNS { + v.push((Op::Un(u()), 1)); + } + for o in IBINS { + v.push((Op::Bin(BinOp::I32(o())), 2)); + v.push((Op::Bin(BinOp::I64(o())), 2)); + } + for c in ICMPS { + v.push((Op::Bin(BinOp::I32Cmp(c())), 2)); + v.push((Op::Bin(BinOp::I64Cmp(c())), 2)); + } + v +} + +/// A program that pushes `n` constants, then runs `op`. +fn with_operands(op: Op, n: usize) -> Vec { + let mut p: Vec = (0..n).map(|k| Op::Const(Val::I64(k as u64))).collect(); + p.push(op); + p +} + +fn blob(ops: &[Op], nl: usize) -> Option> { + X64Backend::emit_blob(ops, nl, 7) +} + +fn accepted(b: &[u8]) -> bool { + validate(b, &[0]) +} + +#[test] +fn every_template_is_accepted() { + let mut count = 0usize; + for (op, n) in all_ops() { + let prog = with_operands(op, n); + let b = blob(&prog, 2); + assert!( + b.is_some(), + "no blob for template {count} ({} ops)", + prog.len() + ); + let b = b.unwrap_or_default(); + assert!( + accepted(&b), + "the validator refuses template {count}: {b:02x?}" + ); + count = count.saturating_add(1); + } + // 14 fixed + 8 unary + 18 integer + 20 comparisons + assert_eq!(count, 60); +} + +#[test] +fn the_body_alone_is_not_a_blob_but_the_blob_is() { + // The blob is the body plus the exit sequence: cut the `ret` off and the + // validator refuses (every path must end in an exit sequence or a + // branch). + let prog = with_operands(Op::Bin(BinOp::I32(IBin::Add)), 2); + let body = X64Backend::emit_body(&prog, 0).unwrap_or_default(); + assert!(!body.is_empty()); + let b = blob(&prog, 0).unwrap_or_default(); + assert!(accepted(&b)); + // The exit sequence is `mov eax, 7` (5 bytes), `mov [r15+256], rax` + // (8) and `ret` (1). + assert_eq!(b.last().copied(), Some(0xc3)); + assert!(!accepted(b.get(..b.len().saturating_sub(1)).unwrap_or(&[]))); +} + +#[test] +fn an_empty_program_is_just_the_exit_sequence() { + let b = blob(&[], 0).unwrap_or_default(); + assert_eq!(b.len(), 14); + assert!(accepted(&b)); +} + +/// xorshift64: a fixed pseudo-random sequence. +struct Rng(u64); + +impl Rng { + fn next(&mut self) -> u64 { + let mut x = self.0; + x ^= x.wrapping_shl(13); + x ^= x.wrapping_shr(7); + x ^= x.wrapping_shl(17); + self.0 = x; + x + } + fn below(&mut self, n: usize) -> usize { + usize::try_from( + self.next() + .checked_rem(u64::try_from(n).unwrap_or(1)) + .unwrap_or(0), + ) + .unwrap_or(0) + } +} + +/// A random well-formed program for `nl` locals: each instruction has the +/// operands it needs, local indices are in range, the frame fits. Returns the +/// program. +fn random_program(rng: &mut Rng, nl: usize, len: usize) -> Vec { + let ops = all_ops(); + let mut prog = Vec::new(); + let mut h = 0usize; + for _ in 0..len { + let pick = rng.below(ops.len()); + let Some((op, pops)) = ops.get(pick) else { + continue; + }; + let pops = *pops; + // keep the frame inside 31 slots and the operands available + if h < pops { + let c = match rng.below(3) { + 0 => Val::I32(u32::try_from(rng.next() & 0xffff_ffff).unwrap_or(0)), + 1 => Val::I64(rng.next()), + _ => Val::F64(rng.next()), + }; + if nl.saturating_add(h) < 31 { + prog.push(Op::Const(c)); + h = h.saturating_add(1); + } + continue; + } + let pushes = match op { + Op::Nop | Op::Drop | Op::LocalSet(_) => 0, + _ => 1, + }; + let after = h.saturating_sub(pops).saturating_add(pushes); + if nl.saturating_add(after) > 31 { + continue; + } + // local indices 0 and 1 need two locals + if matches!(op, Op::LocalGet(_) | Op::LocalSet(_) | Op::LocalTee(_)) && nl < 2 { + continue; + } + prog.push(clone_op(op)); + h = after; + } + prog +} + +fn clone_op(op: &Op) -> Op { + match op { + Op::Nop => Op::Nop, + Op::Const(Val::I32(c)) => Op::Const(Val::I32(*c)), + Op::Const(Val::I64(c)) => Op::Const(Val::I64(*c)), + Op::Const(Val::F64(c)) => Op::Const(Val::F64(*c)), + Op::LocalGet(x) => Op::LocalGet(*x), + Op::LocalSet(x) => Op::LocalSet(*x), + Op::LocalTee(x) => Op::LocalTee(*x), + Op::Drop => Op::Drop, + Op::Select => Op::Select, + Op::Bin(b) => Op::Bin(match b { + BinOp::I32(o) => BinOp::I32(clone_ibin(o)), + BinOp::I64(o) => BinOp::I64(clone_ibin(o)), + BinOp::I32Cmp(c) => BinOp::I32Cmp(clone_icmp(c)), + BinOp::I64Cmp(c) => BinOp::I64Cmp(clone_icmp(c)), + BinOp::F64(_) => BinOp::F64(FBin::Add), + BinOp::F64Cmp(_) => BinOp::F64Cmp(FCmp::Eq), + }), + Op::Un(u) => Op::Un(clone_un(u)), + } +} + +fn clone_ibin(o: &IBin) -> IBin { + match o { + IBin::Add => IBin::Add, + IBin::Sub => IBin::Sub, + IBin::Mul => IBin::Mul, + IBin::And => IBin::And, + IBin::Or => IBin::Or, + IBin::Xor => IBin::Xor, + IBin::Shl => IBin::Shl, + IBin::ShrS => IBin::ShrS, + IBin::ShrU => IBin::ShrU, + IBin::Rotl => IBin::Rotl, + IBin::Rotr => IBin::Rotr, + } +} + +fn clone_icmp(c: &ICmp) -> ICmp { + match c { + ICmp::Eq => ICmp::Eq, + ICmp::Ne => ICmp::Ne, + ICmp::LtS => ICmp::LtS, + ICmp::LtU => ICmp::LtU, + ICmp::GtS => ICmp::GtS, + ICmp::GtU => ICmp::GtU, + ICmp::LeS => ICmp::LeS, + ICmp::LeU => ICmp::LeU, + ICmp::GeS => ICmp::GeS, + ICmp::GeU => ICmp::GeU, + } +} + +fn clone_un(u: &UnOp) -> UnOp { + match u { + UnOp::I32Eqz => UnOp::I32Eqz, + UnOp::I64Eqz => UnOp::I64Eqz, + UnOp::F64Neg => UnOp::F64Neg, + UnOp::F64Abs => UnOp::F64Abs, + UnOp::F64Sqrt => UnOp::F64Sqrt, + UnOp::I32WrapI64 => UnOp::I32WrapI64, + UnOp::I64ExtendI32S => UnOp::I64ExtendI32S, + UnOp::I64ExtendI32U => UnOp::I64ExtendI32U, + UnOp::F64ConvertI32S => UnOp::F64ConvertI32S, + UnOp::F64ConvertI32U => UnOp::F64ConvertI32U, + UnOp::F64ConvertI64S => UnOp::F64ConvertI64S, + UnOp::F64ConvertI64U => UnOp::F64ConvertI64U, + UnOp::F64ReinterpretI64 => UnOp::F64ReinterpretI64, + UnOp::I64ReinterpretF64 => UnOp::I64ReinterpretF64, + } +} + +#[test] +fn generated_programs_are_accepted() { + let mut rng = Rng(0x9e37_79b9_7f4a_7c15); + let mut longest = 0usize; + // Miri interprets every step: 3000 programs took 4772 s there, over + // xtask's 300 s limit, so it checks the first 40 of the same sequence + // (about a minute). The normal test job checks all 3000. + let rounds = if cfg!(miri) { 40usize } else { 3000usize }; + for round in 0..rounds { + let nl = rng.below(6); + let len = rng.below(60); + let prog = random_program(&mut rng, nl, len); + let b = blob(&prog, nl); + assert!( + b.is_some(), + "round {round}: no blob ({} ops, {nl} locals)", + prog.len() + ); + let b = b.unwrap_or_default(); + assert!( + accepted(&b), + "round {round}: refused ({} ops, {nl} locals): {b:02x?}", + prog.len() + ); + longest = longest.max(b.len()); + } + // the programs are not all tiny + assert!( + longest > if cfg!(miri) { 100 } else { 300 }, + "longest blob {longest}" + ); +} + +#[test] +fn no_template_means_no_blob() { + // `f64` arithmetic, comparisons and conversions: LIR's are uninterpreted. + let none = |op: Op, n: usize| { + let prog = with_operands(op, n); + assert!( + blob(&prog, 2).is_none(), + "unexpected blob for a program of {} ops", + prog.len() + ); + assert!(X64Backend::emit_body(&prog, 2).is_none()); + }; + none(Op::Bin(BinOp::F64(FBin::Add)), 2); + none(Op::Bin(BinOp::F64(FBin::Div)), 2); + none(Op::Bin(BinOp::F64Cmp(FCmp::Lt)), 2); + none(Op::Un(UnOp::F64Sqrt), 1); + none(Op::Un(UnOp::F64ConvertI32S), 1); + none(Op::Un(UnOp::F64ConvertI64U), 1); + // rotates + none(Op::Bin(BinOp::I32(IBin::Rotl)), 2); + none(Op::Bin(BinOp::I64(IBin::Rotr)), 2); + // a local that does not exist + none(Op::LocalGet(2), 0); + none(Op::LocalSet(5), 1); + // a short stack + none(Op::Bin(BinOp::I32(IBin::Add)), 1); + none(Op::Select, 2); + none(Op::Drop, 0); +} + +#[test] +fn the_frame_has_31_slots() { + // 31 constants fit with no locals; the 32nd does not. + let consts = |n: usize| -> Vec { (0..n).map(|_| Op::Const(Val::I32(1))).collect() }; + let b = blob(&consts(31), 0); + assert!(b.as_deref().map(accepted).unwrap_or(false)); + assert!(blob(&consts(32), 0).is_none()); + // locals and stack share the 31 + assert!(blob(&consts(21), 10).is_some()); + assert!(blob(&consts(22), 10).is_none()); + assert!(blob(&[], 31).is_some()); + assert!(blob(&[], 32).is_none()); +} + +#[test] +fn the_validator_refuses_what_is_not_a_blob() { + let prog = with_operands(Op::Bin(BinOp::I64(IBin::Xor)), 2); + let b = blob(&prog, 0).unwrap_or_default(); + assert!(accepted(&b)); + // cut off + assert!(!accepted(b.get(..b.len().saturating_sub(1)).unwrap_or(&[]))); + // the `ret` replaced by a byte that is no instruction of the subset + let mut bad = b.clone(); + if let Some(last) = bad.last_mut() { + *last = 0xf4; // hlt + } + assert!(!accepted(&bad)); + // entered in the middle of an instruction + assert!(!validate(&b, &[1])); +} diff --git a/crates/clazel-jit/tests/validator_a64.rs b/crates/clazel-jit/tests/validator_a64.rs new file mode 100644 index 000000000..3d21f61cf --- /dev/null +++ b/crates/clazel-jit/tests/validator_a64.rs @@ -0,0 +1,426 @@ +//! The A64 back end never produces a blob the A64 validator refuses +//! (docs/phases/jit.md J.9: "a test checks that the back end never produces a +//! blob the validator refuses"). The A64 counterpart of `validator.rs`. +//! +//! The blob is `emit_blob`'s output: the encoded templates and the exit +//! sequence. The validator is `clazel-codecheck`'s executable A64 one +//! ([`clazel_codecheck::a64::validate::validate`], proven sound and complete +//! for its spec), entered at offset 0. +//! +//! 1. **Every template.** For each instruction of the first slice, with every +//! operator and comparison, a program that sets up the operands and runs it. +//! 2. **Generated programs.** A few thousand small random well-formed +//! programs (a fixed seed), each of which must have a blob that is +//! accepted. +//! 3. **What has no template is refused by the back end, not emitted**: the +//! `f64` arithmetic, the rotates, a bad local, a short stack, a frame of +//! more than 31 slots. +//! 4. **Controls.** The validator does refuse the blob when its last byte is +//! changed or cut off, so a `true` above is not the validator saying yes +//! to everything. +//! +//! This test is evidence, not proof: the proofs are in the `verus!` modules +//! (`a64::compose::lemma_prog_refines` for the meaning of the code, +//! `a64::emit::emit_blob_impl` for the bytes). That every blob is accepted is +//! not yet a theorem. Under Miri the generated programs are fewer (Miri's +//! interpreter is slow; it is there to find undefined behaviour in the code +//! paths the test runs, and the full count runs in the normal test job). + +use clazel_a64::decoder::decode_instr; +use clazel_a64::encoder::encode_instr; +use clazel_codecheck::a64::validate::validate; +use clazel_jit::a64::api::Backend; +use clazel_jit::a64::emit::A64Backend; +use clazel_jit::spec::Op; +use clazel_lir::spec::{BinOp, FBin, FCmp, IBin, ICmp, UnOp, Val}; + +// The spec's types are not `Copy`, so each use builds a fresh value. + +const IBINS: [fn() -> IBin; 9] = [ + || IBin::Add, + || IBin::Sub, + || IBin::Mul, + || IBin::And, + || IBin::Or, + || IBin::Xor, + || IBin::Shl, + || IBin::ShrU, + || IBin::ShrS, +]; + +const ICMPS: [fn() -> ICmp; 10] = [ + || ICmp::Eq, + || ICmp::Ne, + || ICmp::LtS, + || ICmp::LtU, + || ICmp::GtS, + || ICmp::GtU, + || ICmp::LeS, + || ICmp::LeU, + || ICmp::GeS, + || ICmp::GeU, +]; + +const UNS: [fn() -> UnOp; 8] = [ + || UnOp::I32Eqz, + || UnOp::I64Eqz, + || UnOp::F64Neg, + || UnOp::F64Abs, + || UnOp::I32WrapI64, + || UnOp::I64ExtendI32S, + || UnOp::I64ExtendI32U, + || UnOp::F64ReinterpretI64, +]; + +/// Every operation of the slice that is one constructor away, with the +/// number of operands it takes. +fn all_ops() -> Vec<(Op, usize)> { + let mut v = vec![ + (Op::Nop, 0), + (Op::Drop, 1), + (Op::Select, 3), + (Op::LocalGet(0), 0), + (Op::LocalGet(1), 0), + (Op::LocalSet(0), 1), + (Op::LocalSet(1), 1), + (Op::LocalTee(0), 1), + (Op::LocalTee(1), 1), + (Op::Const(Val::I32(0)), 0), + (Op::Const(Val::I32(u32::MAX)), 0), + (Op::Const(Val::I64(0x1234_5678_9abc_def0)), 0), + (Op::Const(Val::F64(0x7ff8_0000_0000_0001)), 0), + (Op::Un(UnOp::I64ReinterpretF64), 1), + ]; + for u in UNS { + v.push((Op::Un(u()), 1)); + } + for o in IBINS { + v.push((Op::Bin(BinOp::I32(o())), 2)); + v.push((Op::Bin(BinOp::I64(o())), 2)); + } + for c in ICMPS { + v.push((Op::Bin(BinOp::I32Cmp(c())), 2)); + v.push((Op::Bin(BinOp::I64Cmp(c())), 2)); + } + v +} + +/// A program that pushes `n` constants, then runs `op`. +fn with_operands(op: Op, n: usize) -> Vec { + let mut p: Vec = (0..n).map(|k| Op::Const(Val::I64(k as u64))).collect(); + p.push(op); + p +} + +fn blob(ops: &[Op], nl: usize) -> Option> { + A64Backend::emit_blob(ops, nl, 7) +} + +fn accepted(b: &[u8]) -> bool { + validate(b, &[0]) +} + +#[test] +fn every_template_is_accepted() { + let mut count = 0usize; + for (op, n) in all_ops() { + let prog = with_operands(op, n); + let b = blob(&prog, 2); + assert!( + b.is_some(), + "no blob for template {count} ({} ops)", + prog.len() + ); + let b = b.unwrap_or_default(); + assert!( + accepted(&b), + "the validator refuses template {count}: {b:02x?}" + ); + count = count.saturating_add(1); + } + // 14 fixed + 8 unary + 18 integer + 20 comparisons + assert_eq!(count, 60); +} + +#[test] +fn the_body_alone_is_not_a_blob_but_the_blob_is() { + // The blob is the body plus the exit sequence: cut the `ret` off and the + // validator refuses (every path must end in an exit sequence or a + // branch). + let prog = with_operands(Op::Bin(BinOp::I32(IBin::Add)), 2); + let body = A64Backend::emit_body(&prog, 0).unwrap_or_default(); + assert!(!body.is_empty()); + let b = blob(&prog, 0).unwrap_or_default(); + assert!(accepted(&b)); + // The exit sequence is `movz w0, #7`, `str x0, [x28, #256]` and `ret`, + // four bytes each; `ret` is `d65f03c0`. + assert_eq!( + b.get(b.len().saturating_sub(4)..), + Some(&[0xc0, 0x03, 0x5f, 0xd6][..]) + ); + assert!(!accepted(b.get(..b.len().saturating_sub(4)).unwrap_or(&[]))); +} + +#[test] +fn an_empty_program_is_just_the_exit_sequence() { + let b = blob(&[], 0).unwrap_or_default(); + assert_eq!(b.len(), 12); + assert!(accepted(&b)); +} + +/// xorshift64: a fixed pseudo-random sequence. +struct Rng(u64); + +impl Rng { + fn next(&mut self) -> u64 { + let mut x = self.0; + x ^= x.wrapping_shl(13); + x ^= x.wrapping_shr(7); + x ^= x.wrapping_shl(17); + self.0 = x; + x + } + fn below(&mut self, n: usize) -> usize { + usize::try_from( + self.next() + .checked_rem(u64::try_from(n).unwrap_or(1)) + .unwrap_or(0), + ) + .unwrap_or(0) + } +} + +/// A random well-formed program for `nl` locals: each instruction has the +/// operands it needs, local indices are in range, the frame fits. Returns the +/// program. +fn random_program(rng: &mut Rng, nl: usize, len: usize) -> Vec { + let ops = all_ops(); + let mut prog = Vec::new(); + let mut h = 0usize; + for _ in 0..len { + let pick = rng.below(ops.len()); + let Some((op, pops)) = ops.get(pick) else { + continue; + }; + let pops = *pops; + // keep the frame inside 31 slots and the operands available + if h < pops { + let c = match rng.below(3) { + 0 => Val::I32(u32::try_from(rng.next() & 0xffff_ffff).unwrap_or(0)), + 1 => Val::I64(rng.next()), + _ => Val::F64(rng.next()), + }; + if nl.saturating_add(h) < 31 { + prog.push(Op::Const(c)); + h = h.saturating_add(1); + } + continue; + } + let pushes = match op { + Op::Nop | Op::Drop | Op::LocalSet(_) => 0, + _ => 1, + }; + let after = h.saturating_sub(pops).saturating_add(pushes); + if nl.saturating_add(after) > 31 { + continue; + } + // local indices 0 and 1 need two locals + if matches!(op, Op::LocalGet(_) | Op::LocalSet(_) | Op::LocalTee(_)) && nl < 2 { + continue; + } + prog.push(clone_op(op)); + h = after; + } + prog +} + +fn clone_op(op: &Op) -> Op { + match op { + Op::Nop => Op::Nop, + Op::Const(Val::I32(c)) => Op::Const(Val::I32(*c)), + Op::Const(Val::I64(c)) => Op::Const(Val::I64(*c)), + Op::Const(Val::F64(c)) => Op::Const(Val::F64(*c)), + Op::LocalGet(x) => Op::LocalGet(*x), + Op::LocalSet(x) => Op::LocalSet(*x), + Op::LocalTee(x) => Op::LocalTee(*x), + Op::Drop => Op::Drop, + Op::Select => Op::Select, + Op::Bin(b) => Op::Bin(match b { + BinOp::I32(o) => BinOp::I32(clone_ibin(o)), + BinOp::I64(o) => BinOp::I64(clone_ibin(o)), + BinOp::I32Cmp(c) => BinOp::I32Cmp(clone_icmp(c)), + BinOp::I64Cmp(c) => BinOp::I64Cmp(clone_icmp(c)), + BinOp::F64(_) => BinOp::F64(FBin::Add), + BinOp::F64Cmp(_) => BinOp::F64Cmp(FCmp::Eq), + }), + Op::Un(u) => Op::Un(clone_un(u)), + } +} + +fn clone_ibin(o: &IBin) -> IBin { + match o { + IBin::Add => IBin::Add, + IBin::Sub => IBin::Sub, + IBin::Mul => IBin::Mul, + IBin::And => IBin::And, + IBin::Or => IBin::Or, + IBin::Xor => IBin::Xor, + IBin::Shl => IBin::Shl, + IBin::ShrS => IBin::ShrS, + IBin::ShrU => IBin::ShrU, + IBin::Rotl => IBin::Rotl, + IBin::Rotr => IBin::Rotr, + } +} + +fn clone_icmp(c: &ICmp) -> ICmp { + match c { + ICmp::Eq => ICmp::Eq, + ICmp::Ne => ICmp::Ne, + ICmp::LtS => ICmp::LtS, + ICmp::LtU => ICmp::LtU, + ICmp::GtS => ICmp::GtS, + ICmp::GtU => ICmp::GtU, + ICmp::LeS => ICmp::LeS, + ICmp::LeU => ICmp::LeU, + ICmp::GeS => ICmp::GeS, + ICmp::GeU => ICmp::GeU, + } +} + +fn clone_un(u: &UnOp) -> UnOp { + match u { + UnOp::I32Eqz => UnOp::I32Eqz, + UnOp::I64Eqz => UnOp::I64Eqz, + UnOp::F64Neg => UnOp::F64Neg, + UnOp::F64Abs => UnOp::F64Abs, + UnOp::F64Sqrt => UnOp::F64Sqrt, + UnOp::I32WrapI64 => UnOp::I32WrapI64, + UnOp::I64ExtendI32S => UnOp::I64ExtendI32S, + UnOp::I64ExtendI32U => UnOp::I64ExtendI32U, + UnOp::F64ConvertI32S => UnOp::F64ConvertI32S, + UnOp::F64ConvertI32U => UnOp::F64ConvertI32U, + UnOp::F64ConvertI64S => UnOp::F64ConvertI64S, + UnOp::F64ConvertI64U => UnOp::F64ConvertI64U, + UnOp::F64ReinterpretI64 => UnOp::F64ReinterpretI64, + UnOp::I64ReinterpretF64 => UnOp::I64ReinterpretF64, + } +} + +#[test] +fn generated_programs_are_accepted() { + let mut rng = Rng(0x9e37_79b9_7f4a_7c15); + let mut longest = 0usize; + // Under Miri (see the module doc) a stride of the rounds. + let rounds = if cfg!(miri) { 40usize } else { 3000usize }; + for round in 0..rounds { + let nl = rng.below(6); + let len = rng.below(60); + let prog = random_program(&mut rng, nl, len); + let b = blob(&prog, nl); + assert!( + b.is_some(), + "round {round}: no blob ({} ops, {nl} locals)", + prog.len() + ); + let b = b.unwrap_or_default(); + assert!( + accepted(&b), + "round {round}: refused ({} ops, {nl} locals): {b:02x?}", + prog.len() + ); + longest = longest.max(b.len()); + } + // the programs are not all tiny + assert!( + longest > if cfg!(miri) { 100 } else { 300 }, + "longest blob {longest}" + ); +} + +#[test] +fn no_template_means_no_blob() { + // `f64` arithmetic, comparisons and conversions: LIR's are uninterpreted. + let none = |op: Op, n: usize| { + let prog = with_operands(op, n); + assert!( + blob(&prog, 2).is_none(), + "unexpected blob for a program of {} ops", + prog.len() + ); + assert!(A64Backend::emit_body(&prog, 2).is_none()); + }; + none(Op::Bin(BinOp::F64(FBin::Add)), 2); + none(Op::Bin(BinOp::F64(FBin::Div)), 2); + none(Op::Bin(BinOp::F64Cmp(FCmp::Lt)), 2); + none(Op::Un(UnOp::F64Sqrt), 1); + none(Op::Un(UnOp::F64ConvertI32S), 1); + none(Op::Un(UnOp::F64ConvertI64U), 1); + // rotates + none(Op::Bin(BinOp::I32(IBin::Rotl)), 2); + none(Op::Bin(BinOp::I64(IBin::Rotr)), 2); + // a local that does not exist + none(Op::LocalGet(2), 0); + none(Op::LocalSet(5), 1); + // a short stack + none(Op::Bin(BinOp::I32(IBin::Add)), 1); + none(Op::Select, 2); + none(Op::Drop, 0); +} + +#[test] +fn the_frame_has_31_slots() { + // 31 constants fit with no locals; the 32nd does not. + let consts = |n: usize| -> Vec { (0..n).map(|_| Op::Const(Val::I32(1))).collect() }; + let b = blob(&consts(31), 0); + assert!(b.as_deref().map(accepted).unwrap_or(false)); + assert!(blob(&consts(32), 0).is_none()); + // locals and stack share the 31 + assert!(blob(&consts(21), 10).is_some()); + assert!(blob(&consts(22), 10).is_none()); + assert!(blob(&[], 31).is_some()); + assert!(blob(&[], 32).is_none()); +} + +#[test] +fn the_validator_refuses_what_is_not_a_blob() { + let prog = with_operands(Op::Bin(BinOp::I64(IBin::Xor)), 2); + let b = blob(&prog, 0).unwrap_or_default(); + assert!(accepted(&b)); + // cut off (not a whole number of words, and then without its `ret`) + assert!(!accepted(b.get(..b.len().saturating_sub(1)).unwrap_or(&[]))); + assert!(!accepted(b.get(..b.len().saturating_sub(4)).unwrap_or(&[]))); + // the `ret` replaced by a word that is no instruction of the subset + // (`hlt #0`, d4400000) + let mut bad = b.clone(); + let n = bad.len(); + if let Some(w) = bad.get_mut(n.saturating_sub(4)..) { + w.copy_from_slice(&0xd440_0000u32.to_le_bytes()); + } + assert!(!accepted(&bad)); + // entered in the middle of an instruction + assert!(!validate(&b, &[1])); +} + +/// The bytes are the words of the encoder, and decode back to the body and +/// the exit sequence: a cross-check of the emitter against `clazel-a64`'s +/// decoder (the emitter's contract is proven; this reads the bytes back the +/// way the validator does). +#[test] +fn the_blob_decodes_to_the_body_and_the_exit_sequence() { + let prog = with_operands(Op::Bin(BinOp::I32Cmp(ICmp::LtS)), 2); + let body = A64Backend::emit_body(&prog, 1).unwrap_or_default(); + let b = A64Backend::emit_blob(&prog, 1, 0xbeef).unwrap_or_default(); + assert_eq!(b.len(), body.len().saturating_mul(4).saturating_add(12)); + for (k, ins) in body.iter().enumerate() { + let at = k.saturating_mul(4); + let w = b + .get(at..at.saturating_add(4)) + .and_then(|s| <[u8; 4]>::try_from(s).ok()) + .map(u32::from_le_bytes) + .unwrap_or(0); + assert_eq!(w, encode_instr(ins)); + assert!(decode_instr(w).is_some()); + } + assert!(accepted(&b)); +} diff --git a/crates/clazel-jit/tests/validator_a64_cf.rs b/crates/clazel-jit/tests/validator_a64_cf.rs new file mode 100644 index 000000000..7dd931362 --- /dev/null +++ b/crates/clazel-jit/tests/validator_a64_cf.rs @@ -0,0 +1,515 @@ +//! The A64 control-flow back end never produces a blob the A64 validator +//! refuses (docs/phases/jit.md J.9: "a test checks that the back end never +//! produces a blob the validator refuses"), for **structured control flow, +//! memory instructions and `br_table`**. The A64 counterpart of +//! `validator_cf.rs`. +//! +//! The blob is [`clazel_jit::a64::cfemit::emit_cf_blob`]'s output, the encoded +//! body and the exit sequence, and the validator is `clazel-codecheck`'s +//! executable A64 one, entered at offset 0. +//! +//! 1. **Every memory template**: every region, every access type, every check +//! size, with its operands set up. +//! 2. **`br_table`** with every position of the index, nested in blocks. +//! 3. **Generated programs** with blocks, loops, `if`, branches, memory +//! accesses, `br_table` and `exit` (a fixed seed), each of which must +//! have a blob that is accepted. +//! 4. **Refusals**: what has no translation is not emitted. +//! 5. **Controls**: the validator does refuse a blob whose bounds check is +//! broken, whose exit branch lands inside the exit sequence, or whose last +//! byte is cut. +//! +//! That the validator accepts the blobs is evidence, not proof; that the emitter returns the templates is proven (`clazel_jit::a64::cfemit`). + +use clazel_codecheck::a64::validate::validate; +use clazel_jit::a64::cfemit::emit_cf_blob; +use clazel_jit::cfspec::{MTy, MemOp, Node}; +use clazel_jit::spec::Op; +use clazel_lir::spec::{BinOp, IBin, Val}; + +fn nop() -> Node { + Node::Op(Op::Nop) +} + +fn then(a: Node, b: Node) -> Node { + Node::Then(Box::new(a), Box::new(b)) +} + +fn seq(nodes: Vec) -> Node { + nodes.into_iter().fold(nop(), then) +} + +fn c64(x: u64) -> Node { + Node::Op(Op::Const(Val::I64(x))) +} + +fn c32(x: u32) -> Node { + Node::Op(Op::Const(Val::I32(x))) +} + +fn accepted(b: &[u8]) -> bool { + validate(b, &[0]) +} + +fn blob(n: &Node, nl: usize) -> Option> { + emit_cf_blob(n, nl, 7) +} + +const TYS: [fn() -> MTy; 3] = [|| MTy::I32, || MTy::I64, || MTy::F64]; + +fn const_of(ty: &MTy) -> Node { + match ty { + MTy::I32 => c32(5), + MTy::I64 => c64(5), + MTy::F64 => Node::Op(Op::Const(Val::F64(5))), + } +} + +#[test] +fn every_memory_template_is_accepted() { + let regions: Vec = if cfg!(miri) { + vec![0, 15] + } else { + (0..16).collect() + }; + let mut count = 0usize; + for region in regions { + for ty in TYS { + let load = seq(vec![c64(3), Node::Mem(MemOp::Load { region, ty: ty() })]); + let store = seq(vec![ + c64(3), + const_of(&ty()), + Node::Mem(MemOp::Store { region, ty: ty() }), + ]); + for (name, p) in [("load", load), ("store", store)] { + let b = blob(&p, 2).unwrap_or_default(); + assert!(!b.is_empty(), "no blob for {name} region {region}"); + assert!(accepted(&b), "refused: {name} region {region}: {b:02x?}"); + count = count.saturating_add(1); + } + } + let sizes: Vec = if cfg!(miri) { + vec![1, 8] + } else { + (1..=8).collect() + }; + for size in sizes { + let p = seq(vec![ + c64(3), + Node::Mem(MemOp::BoundsCheck { + region, + size, + code: 99, + }), + ]); + let b = blob(&p, 0).unwrap_or_default(); + assert!(!b.is_empty(), "no blob for bounds_check {region} {size}"); + assert!(accepted(&b), "refused: bounds_check {region} {size}"); + count = count.saturating_add(1); + } + } + assert!(count >= if cfg!(miri) { 14 } else { 224 }); +} + +#[test] +fn br_table_is_accepted() { + // block 2 { block 1 { block 0 { index; br_table [0 1 2 1] 2 } } } + for pos in 0..6u32 { + let table = Node::BrTable(vec![0, 1, 2, 1], 2); + let inner = Node::Block(Box::new(seq(vec![c32(pos), table]))); + let mid = Node::Block(Box::new(then(inner, nop()))); + let outer = Node::Block(Box::new(then(mid, nop()))); + let b = blob(&outer, 0).unwrap_or_default(); + assert!(!b.is_empty(), "no blob for br_table {pos}"); + assert!(accepted(&b), "refused: br_table {pos}: {b:02x?}"); + } + // a table with no targets is a `b` to the default + let p = Node::Block(Box::new(seq(vec![c32(0), Node::BrTable(vec![], 0)]))); + assert!(accepted(&blob(&p, 0).unwrap_or_default())); + // and one with many + let many: Vec = (0..300usize) + .map(|i| i.checked_rem(2).unwrap_or(0)) + .collect(); + let p = Node::Block(Box::new(Node::Block(Box::new(seq(vec![ + c32(1), + Node::BrTable(many, 1), + ]))))); + let b = blob(&p, 0).unwrap_or_default(); + assert!(b.len() > 3600); + assert!(accepted(&b)); +} + +#[test] +fn a_loop_with_memory_is_accepted() { + // loop { local.get 0; i64.const 8; i64.add; local.tee 0; load; drop; br_if 0 } + let body = seq(vec![ + Node::Op(Op::LocalGet(0)), + c64(8), + Node::Op(Op::Bin(BinOp::I64(IBin::Add))), + Node::Op(Op::LocalTee(0)), + Node::Mem(MemOp::Load { + region: 1, + ty: MTy::I32, + }), + Node::BrIf(0), + ]); + let p = seq(vec![ + c64(0), + Node::Op(Op::LocalSet(0)), + Node::Loop(Box::new(body)), + ]); + let b = blob(&p, 1).unwrap_or_default(); + assert!(!b.is_empty()); + assert!(accepted(&b), "refused: {b:02x?}"); +} + +/// xorshift64: a fixed pseudo-random sequence. +struct Rng(u64); + +impl Rng { + fn next(&mut self) -> u64 { + let mut x = self.0; + x ^= x.wrapping_shl(13); + x ^= x.wrapping_shr(7); + x ^= x.wrapping_shl(17); + self.0 = x; + x + } + fn below(&mut self, n: usize) -> usize { + usize::try_from( + self.next() + .checked_rem(u64::try_from(n).unwrap_or(1)) + .unwrap_or(0), + ) + .unwrap_or(0) + } +} + +/// The generator's context: locals, and the most height it uses. +const MAX_H: usize = 8; + +/// One random instruction valid at height `h` with `depth` labels in scope, +/// with `nest` more levels of nesting allowed; the height after it, or +/// `None` if it never falls through. +fn gen_one(rng: &mut Rng, nl: usize, h: usize, depth: usize, nest: usize) -> (Node, Option) { + loop { + let pick = rng.below(18); + let room = h < MAX_H; + match pick { + 0 if room => { + return ( + c32(u32::try_from(rng.below(5)).unwrap_or(0)), + Some(h.saturating_add(1)), + ); + } + 1 if room => { + return ( + c64(rng.next().checked_rem(40).unwrap_or(0)), + Some(h.saturating_add(1)), + ); + } + 2 if h >= 1 => return (Node::Op(Op::Drop), Some(h.saturating_sub(1))), + 3 if nl >= 1 && room => { + return ( + Node::Op(Op::LocalGet(rng.below(nl))), + Some(h.saturating_add(1)), + ); + } + 4 if nl >= 1 && h >= 1 => { + return ( + Node::Op(Op::LocalSet(rng.below(nl))), + Some(h.saturating_sub(1)), + ); + } + 5 => return (nop(), Some(h)), + 6 if h >= 2 => { + return ( + Node::Op(Op::Bin(BinOp::I64(IBin::Add))), + Some(h.saturating_sub(1)), + ); + } + 7 if h >= 1 => { + let ty = TYS.get(rng.below(3)).map_or(MTy::I64, |f| f()); + return ( + Node::Mem(MemOp::Load { + region: rng.below(16), + ty, + }), + Some(h), + ); + } + 8 if h >= 2 => { + let ty = TYS.get(rng.below(3)).map_or(MTy::I64, |f| f()); + return ( + Node::Mem(MemOp::Store { + region: rng.below(16), + ty, + }), + Some(h.saturating_sub(2)), + ); + } + 9 if h >= 1 => { + return ( + Node::Mem(MemOp::BoundsCheck { + region: rng.below(16), + size: rng.below(8).saturating_add(1), + code: u32::try_from(rng.below(1000)).unwrap_or(0), + }), + Some(h), + ); + } + 10 if nest > 0 => { + let (b, _) = gen_body( + rng, + nl, + h, + depth.saturating_add(1), + nest.saturating_sub(1), + h, + ); + return (Node::Block(Box::new(b)), Some(h)); + } + 11 if nest > 0 => { + let (b, _) = gen_body( + rng, + nl, + h, + depth.saturating_add(1), + nest.saturating_sub(1), + h, + ); + return (Node::Loop(Box::new(b)), Some(h)); + } + 12 if nest > 0 && h >= 1 => { + let (t, _) = gen_body( + rng, + nl, + h.saturating_sub(1), + depth.saturating_add(1), + nest.saturating_sub(1), + h.saturating_sub(1), + ); + let (e, _) = gen_body( + rng, + nl, + h.saturating_sub(1), + depth.saturating_add(1), + nest.saturating_sub(1), + h.saturating_sub(1), + ); + return ( + Node::If(Box::new(t), Box::new(e)), + Some(h.saturating_sub(1)), + ); + } + 13 if depth >= 1 => return (Node::Br(rng.below(depth)), None), + 14 if depth >= 1 && h >= 1 => { + return (Node::BrIf(rng.below(depth)), Some(h.saturating_sub(1))); + } + 15 if depth >= 1 && h >= 1 => { + let len = rng.below(6); + let ls: Vec = (0..len).map(|_| rng.below(depth)).collect(); + return (Node::BrTable(ls, rng.below(depth)), None); + } + 16 if rng.below(8) == 0 => { + let k = rng.below(h.saturating_add(1)); + return ( + Node::Exit(u32::try_from(rng.below(100)).unwrap_or(0), k), + None, + ); + } + _ => {} + } + } +} + +/// A random body at height `h` that ends at height `target` (or never falls +/// through): up to a few instructions, then drops or constants to reach the +/// target. +fn gen_body( + rng: &mut Rng, + nl: usize, + h: usize, + depth: usize, + nest: usize, + target: usize, +) -> (Node, Option) { + let steps = rng.below(7); + let mut acc = nop(); + let mut cur = h; + for _ in 0..steps { + let (n, next) = gen_one(rng, nl, cur, depth, nest); + acc = then(acc, n); + match next { + Some(x) => cur = x, + None => return (acc, None), + } + } + while cur > target { + acc = then(acc, Node::Op(Op::Drop)); + cur = cur.saturating_sub(1); + } + while cur < target { + acc = then(acc, c32(1)); + cur = cur.saturating_add(1); + } + (acc, Some(target)) +} + +#[test] +fn generated_programs_are_accepted() { + let mut rng = Rng(0x9e37_79b9_7f4a_7c15); + // Miri interprets every step, so it checks the first few programs of the + // same sequence; the normal test job checks all of them. + let rounds = if cfg!(miri) { 5usize } else { 1500usize }; + let mut longest = 0usize; + let mut with_jumps = 0usize; + for round in 0..rounds { + let nl = rng.below(4); + let (p, _) = gen_body(&mut rng, nl, 0, 1, 3, 0); + let b = blob(&p, nl); + assert!(b.is_some(), "round {round}: no blob ({nl} locals)"); + let b = b.unwrap_or_default(); + assert!( + accepted(&b), + "round {round}: refused ({nl} locals): {b:02x?}" + ); + longest = longest.max(b.len()); + // a `b`, `b.cond`, `cbz` or `cbnz` is in the blob (the high byte of + // a word is its last byte; a crude count, but the programs are not + // only straight-line code) + if b.chunks(4).any(|w| { + matches!( + w.get(3), + Some(0x14..=0x17 | 0x54 | 0x34 | 0x35 | 0xB4 | 0xB5) + ) + }) { + with_jumps = with_jumps.saturating_add(1); + } + } + assert!( + longest > if cfg!(miri) { 40 } else { 400 }, + "longest {longest}" + ); + assert!(with_jumps > rounds / 4, "only {with_jumps} with jumps"); +} + +#[test] +fn no_translation_means_no_blob() { + let load = |region| { + Node::Mem(MemOp::Load { + region, + ty: MTy::I32, + }) + }; + // region 16 is not in the table + assert!(blob(&seq(vec![c64(0), load(16)]), 0).is_none()); + // no index to load + assert!(blob(&load(0), 0).is_none()); + // a check of 0 or 9 bytes + for size in [0usize, 9] { + let p = seq(vec![ + c64(0), + Node::Mem(MemOp::BoundsCheck { + region: 0, + size, + code: 1, + }), + ]); + assert!(blob(&p, 0).is_none(), "size {size}"); + } + // store needs two operands + let p = seq(vec![ + c64(0), + Node::Mem(MemOp::Store { + region: 0, + ty: MTy::I64, + }), + ]); + assert!(blob(&p, 0).is_none()); + // a branch past the outermost label + assert!(blob(&Node::Br(1), 0).is_none()); + assert!(blob(&Node::Br(0), 0).is_some()); + // br_table: a label out of scope, a missing index + assert!(blob(&seq(vec![c32(0), Node::BrTable(vec![0, 1], 0)]), 0).is_none()); + assert!(blob(&Node::BrTable(vec![], 0), 0).is_none()); + // too many targets + let many: Vec = vec![0; 0x10001]; + assert!(blob(&seq(vec![c32(0), Node::BrTable(many, 0)]), 0).is_none()); + // `if` arms that leave different heights + let p = seq(vec![c32(1), Node::If(Box::new(c32(1)), Box::new(nop()))]); + assert!(blob(&p, 0).is_none()); + // a block that leaves a value + assert!(blob(&Node::Block(Box::new(c32(1))), 0).is_none()); + // code after a branch + assert!(blob(&then(Node::Br(0), c32(1)), 0).is_none()); + // a frame beyond 31 slots + assert!(blob(&nop(), 32).is_none()); + assert!(blob(&nop(), 31).is_some()); + // an exit code of more than 16 bits (the A64 exit sequence is `movz`) + assert!(blob(&Node::Exit(0x1_0000, 0), 0).is_none()); + assert!(blob(&Node::Exit(0xFFFF, 0), 0).is_some()); +} + +#[test] +fn exit_with_operands_is_accepted() { + let p = seq(vec![c64(1), c64(2), Node::Exit(42, 2)]); + assert!(accepted(&blob(&p, 0).unwrap_or_default())); +} + +/// The words of a blob. +fn words(b: &[u8]) -> Vec { + b.chunks(4) + .map(|c| { + let mut w = [0u8; 4]; + for (d, x) in w.iter_mut().zip(c.iter()) { + *d = *x; + } + u32::from_le_bytes(w) + }) + .collect() +} + +fn bytes_of(w: &[u32]) -> Vec { + w.iter().flat_map(|x| x.to_le_bytes()).collect() +} + +#[test] +fn the_validator_refuses_a_broken_check() { + let p = seq(vec![ + c64(3), + Node::Mem(MemOp::Load { + region: 0, + ty: MTy::I64, + }), + ]); + let b = blob(&p, 0).unwrap_or_default(); + assert!(accepted(&b)); + let w = words(&b); + // `b.hs` is `0x54000002 | imm19 << 5`, `b.hi` is `0x54000008 | imm19 << 5` + let hs = w + .iter() + .position(|x| x & 0xFF00_001F == 0x5400_0002) + .unwrap_or(usize::MAX); + assert_ne!(hs, usize::MAX); + // branch one word further: into the middle of the exit sequence + let mut moved = w.clone(); + if let Some(x) = moved.get_mut(hs) { + *x = x.wrapping_add(0x20); + } + assert!(!accepted(&bytes_of(&moved))); + // `b.hi` becomes `b.ls`: the sequence is no longer the check, so the + // access is not preceded by one + let hi = w + .iter() + .position(|x| x & 0xFF00_001F == 0x5400_0008) + .unwrap_or(usize::MAX); + assert_ne!(hi, usize::MAX); + let mut broken = w.clone(); + if let Some(x) = broken.get_mut(hi) { + *x ^= 1; + } + assert!(!accepted(&bytes_of(&broken))); + // cut the last byte + assert!(!accepted(b.get(..b.len().saturating_sub(1)).unwrap_or(&[]))); +} diff --git a/crates/clazel-jit/tests/validator_cf.rs b/crates/clazel-jit/tests/validator_cf.rs new file mode 100644 index 000000000..00026999d --- /dev/null +++ b/crates/clazel-jit/tests/validator_cf.rs @@ -0,0 +1,488 @@ +//! The control-flow back end never produces a blob the validator refuses +//! (docs/phases/jit.md J.9: "a test checks that the back end never produces a +//! blob the validator refuses"), for **structured control flow, memory +//! instructions and `br_table`**. +//! +//! The blob is [`clazel_jit::cfemit::emit_cf_blob`]'s output, the encoded body +//! and the exit sequence, and the validator is `clazel-codecheck`'s executable +//! one, entered at offset 0. +//! +//! 1. **Every memory template**: every region, every access type, every check +//! size, with its operands set up. +//! 2. **`br_table`** with every position of the index, nested in blocks. +//! 3. **Generated programs** with blocks, loops, `if`, branches, memory +//! accesses, `br_table` and `exit` (a fixed seed), each of which must +//! have a blob that is accepted. +//! 4. **Refusals**: what has no translation is not emitted. +//! 5. **Controls**: the validator does refuse a blob whose bounds check is +//! broken, whose jump lands inside the check, or whose last byte is cut. +//! +//! That the validator accepts the blobs is evidence, not proof; that the emitter returns the templates is proven (`clazel_jit::cfemit`). + +use clazel_codecheck::validate::validate; +use clazel_jit::cfemit::emit_cf_blob; +use clazel_jit::cfspec::{MTy, MemOp, Node}; +use clazel_jit::spec::Op; +use clazel_lir::spec::{BinOp, IBin, Val}; + +fn nop() -> Node { + Node::Op(Op::Nop) +} + +fn then(a: Node, b: Node) -> Node { + Node::Then(Box::new(a), Box::new(b)) +} + +fn seq(nodes: Vec) -> Node { + nodes.into_iter().fold(nop(), then) +} + +fn c64(x: u64) -> Node { + Node::Op(Op::Const(Val::I64(x))) +} + +fn c32(x: u32) -> Node { + Node::Op(Op::Const(Val::I32(x))) +} + +fn accepted(b: &[u8]) -> bool { + validate(b, &[0]) +} + +fn blob(n: &Node, nl: usize) -> Option> { + emit_cf_blob(n, nl, 7) +} + +const TYS: [fn() -> MTy; 3] = [|| MTy::I32, || MTy::I64, || MTy::F64]; + +fn const_of(ty: &MTy) -> Node { + match ty { + MTy::I32 => c32(5), + MTy::I64 => c64(5), + MTy::F64 => Node::Op(Op::Const(Val::F64(5))), + } +} + +#[test] +fn every_memory_template_is_accepted() { + let regions: Vec = if cfg!(miri) { + vec![0, 15] + } else { + (0..16).collect() + }; + let mut count = 0usize; + for region in regions { + for ty in TYS { + let load = seq(vec![c64(3), Node::Mem(MemOp::Load { region, ty: ty() })]); + let store = seq(vec![ + c64(3), + const_of(&ty()), + Node::Mem(MemOp::Store { region, ty: ty() }), + ]); + for (name, p) in [("load", load), ("store", store)] { + let b = blob(&p, 2).unwrap_or_default(); + assert!(!b.is_empty(), "no blob for {name} region {region}"); + assert!(accepted(&b), "refused: {name} region {region}: {b:02x?}"); + count = count.saturating_add(1); + } + } + let sizes: Vec = if cfg!(miri) { + vec![1, 8] + } else { + (1..=8).collect() + }; + for size in sizes { + let p = seq(vec![ + c64(3), + Node::Mem(MemOp::BoundsCheck { + region, + size, + code: 99, + }), + ]); + let b = blob(&p, 0).unwrap_or_default(); + assert!(!b.is_empty(), "no blob for bounds_check {region} {size}"); + assert!(accepted(&b), "refused: bounds_check {region} {size}"); + count = count.saturating_add(1); + } + } + assert!(count >= if cfg!(miri) { 14 } else { 224 }); +} + +#[test] +fn br_table_is_accepted() { + // block 2 { block 1 { block 0 { index; br_table [0 1 2 1] 2 } } } + for pos in 0..6u32 { + let table = Node::BrTable(vec![0, 1, 2, 1], 2); + let inner = Node::Block(Box::new(seq(vec![c32(pos), table]))); + let mid = Node::Block(Box::new(then(inner, nop()))); + let outer = Node::Block(Box::new(then(mid, nop()))); + let b = blob(&outer, 0).unwrap_or_default(); + assert!(!b.is_empty(), "no blob for br_table {pos}"); + assert!(accepted(&b), "refused: br_table {pos}: {b:02x?}"); + } + // a table with no targets is a `jmp` to the default + let p = Node::Block(Box::new(seq(vec![c32(0), Node::BrTable(vec![], 0)]))); + assert!(accepted(&blob(&p, 0).unwrap_or_default())); + // and one with many + let many: Vec = (0..300usize) + .map(|i| i.checked_rem(2).unwrap_or(0)) + .collect(); + let p = Node::Block(Box::new(Node::Block(Box::new(seq(vec![ + c32(1), + Node::BrTable(many, 1), + ]))))); + let b = blob(&p, 0).unwrap_or_default(); + assert!(b.len() > 3900); + assert!(accepted(&b)); +} + +#[test] +fn a_loop_with_memory_is_accepted() { + // loop { local.get 0; i64.const 8; i64.add; local.tee 0; load; drop; br_if 0 } + let body = seq(vec![ + Node::Op(Op::LocalGet(0)), + c64(8), + Node::Op(Op::Bin(BinOp::I64(IBin::Add))), + Node::Op(Op::LocalTee(0)), + Node::Mem(MemOp::Load { + region: 1, + ty: MTy::I32, + }), + Node::BrIf(0), + ]); + let p = seq(vec![ + c64(0), + Node::Op(Op::LocalSet(0)), + Node::Loop(Box::new(body)), + ]); + let b = blob(&p, 1).unwrap_or_default(); + assert!(!b.is_empty()); + assert!(accepted(&b), "refused: {b:02x?}"); +} + +/// xorshift64: a fixed pseudo-random sequence. +struct Rng(u64); + +impl Rng { + fn next(&mut self) -> u64 { + let mut x = self.0; + x ^= x.wrapping_shl(13); + x ^= x.wrapping_shr(7); + x ^= x.wrapping_shl(17); + self.0 = x; + x + } + fn below(&mut self, n: usize) -> usize { + usize::try_from( + self.next() + .checked_rem(u64::try_from(n).unwrap_or(1)) + .unwrap_or(0), + ) + .unwrap_or(0) + } +} + +/// The generator's context: locals, and the most height it uses. +const MAX_H: usize = 8; + +/// One random instruction valid at height `h` with `depth` labels in scope, +/// with `nest` more levels of nesting allowed; the height after it, or +/// `None` if it never falls through. +fn gen_one(rng: &mut Rng, nl: usize, h: usize, depth: usize, nest: usize) -> (Node, Option) { + loop { + let pick = rng.below(18); + let room = h < MAX_H; + match pick { + 0 if room => { + return ( + c32(u32::try_from(rng.below(5)).unwrap_or(0)), + Some(h.saturating_add(1)), + ); + } + 1 if room => { + return ( + c64(rng.next().checked_rem(40).unwrap_or(0)), + Some(h.saturating_add(1)), + ); + } + 2 if h >= 1 => return (Node::Op(Op::Drop), Some(h.saturating_sub(1))), + 3 if nl >= 1 && room => { + return ( + Node::Op(Op::LocalGet(rng.below(nl))), + Some(h.saturating_add(1)), + ); + } + 4 if nl >= 1 && h >= 1 => { + return ( + Node::Op(Op::LocalSet(rng.below(nl))), + Some(h.saturating_sub(1)), + ); + } + 5 => return (nop(), Some(h)), + 6 if h >= 2 => { + return ( + Node::Op(Op::Bin(BinOp::I64(IBin::Add))), + Some(h.saturating_sub(1)), + ); + } + 7 if h >= 1 => { + let ty = TYS.get(rng.below(3)).map_or(MTy::I64, |f| f()); + return ( + Node::Mem(MemOp::Load { + region: rng.below(16), + ty, + }), + Some(h), + ); + } + 8 if h >= 2 => { + let ty = TYS.get(rng.below(3)).map_or(MTy::I64, |f| f()); + return ( + Node::Mem(MemOp::Store { + region: rng.below(16), + ty, + }), + Some(h.saturating_sub(2)), + ); + } + 9 if h >= 1 => { + return ( + Node::Mem(MemOp::BoundsCheck { + region: rng.below(16), + size: rng.below(8).saturating_add(1), + code: u32::try_from(rng.below(1000)).unwrap_or(0), + }), + Some(h), + ); + } + 10 if nest > 0 => { + let (b, _) = gen_body( + rng, + nl, + h, + depth.saturating_add(1), + nest.saturating_sub(1), + h, + ); + return (Node::Block(Box::new(b)), Some(h)); + } + 11 if nest > 0 => { + let (b, _) = gen_body( + rng, + nl, + h, + depth.saturating_add(1), + nest.saturating_sub(1), + h, + ); + return (Node::Loop(Box::new(b)), Some(h)); + } + 12 if nest > 0 && h >= 1 => { + let (t, _) = gen_body( + rng, + nl, + h.saturating_sub(1), + depth.saturating_add(1), + nest.saturating_sub(1), + h.saturating_sub(1), + ); + let (e, _) = gen_body( + rng, + nl, + h.saturating_sub(1), + depth.saturating_add(1), + nest.saturating_sub(1), + h.saturating_sub(1), + ); + return ( + Node::If(Box::new(t), Box::new(e)), + Some(h.saturating_sub(1)), + ); + } + 13 if depth >= 1 => return (Node::Br(rng.below(depth)), None), + 14 if depth >= 1 && h >= 1 => { + return (Node::BrIf(rng.below(depth)), Some(h.saturating_sub(1))); + } + 15 if depth >= 1 && h >= 1 => { + let len = rng.below(6); + let ls: Vec = (0..len).map(|_| rng.below(depth)).collect(); + return (Node::BrTable(ls, rng.below(depth)), None); + } + 16 if rng.below(8) == 0 => { + let k = rng.below(h.saturating_add(1)); + return ( + Node::Exit(u32::try_from(rng.below(100)).unwrap_or(0), k), + None, + ); + } + _ => {} + } + } +} + +/// A random body at height `h` that ends at height `target` (or never falls +/// through): up to a few instructions, then drops or constants to reach the +/// target. +fn gen_body( + rng: &mut Rng, + nl: usize, + h: usize, + depth: usize, + nest: usize, + target: usize, +) -> (Node, Option) { + let steps = rng.below(7); + let mut acc = nop(); + let mut cur = h; + for _ in 0..steps { + let (n, next) = gen_one(rng, nl, cur, depth, nest); + acc = then(acc, n); + match next { + Some(x) => cur = x, + None => return (acc, None), + } + } + while cur > target { + acc = then(acc, Node::Op(Op::Drop)); + cur = cur.saturating_sub(1); + } + while cur < target { + acc = then(acc, c32(1)); + cur = cur.saturating_add(1); + } + (acc, Some(target)) +} + +#[test] +fn generated_programs_are_accepted() { + let mut rng = Rng(0x9e37_79b9_7f4a_7c15); + // Miri interprets every step, so it checks the first few programs of the + // same sequence; the normal test job checks all of them. + let rounds = if cfg!(miri) { 5usize } else { 1500usize }; + let mut longest = 0usize; + let mut with_jumps = 0usize; + for round in 0..rounds { + let nl = rng.below(4); + let (p, _) = gen_body(&mut rng, nl, 0, 1, 3, 0); + let b = blob(&p, nl); + assert!(b.is_some(), "round {round}: no blob ({nl} locals)"); + let b = b.unwrap_or_default(); + assert!( + accepted(&b), + "round {round}: refused ({nl} locals): {b:02x?}" + ); + longest = longest.max(b.len()); + // a `jmp` or a `jcc` is in the blob (a crude count, but the programs + // are not only straight-line code) + if b.iter().any(|x| *x == 0xE9 || *x == 0x0F) { + with_jumps = with_jumps.saturating_add(1); + } + } + assert!( + longest > if cfg!(miri) { 40 } else { 400 }, + "longest {longest}" + ); + assert!(with_jumps > rounds / 4, "only {with_jumps} with jumps"); +} + +#[test] +fn no_translation_means_no_blob() { + let load = |region| { + Node::Mem(MemOp::Load { + region, + ty: MTy::I32, + }) + }; + // region 16 is not in the table + assert!(blob(&seq(vec![c64(0), load(16)]), 0).is_none()); + // no index to load + assert!(blob(&load(0), 0).is_none()); + // a check of 0 or 9 bytes + for size in [0usize, 9] { + let p = seq(vec![ + c64(0), + Node::Mem(MemOp::BoundsCheck { + region: 0, + size, + code: 1, + }), + ]); + assert!(blob(&p, 0).is_none(), "size {size}"); + } + // store needs two operands + let p = seq(vec![ + c64(0), + Node::Mem(MemOp::Store { + region: 0, + ty: MTy::I64, + }), + ]); + assert!(blob(&p, 0).is_none()); + // a branch past the outermost label + assert!(blob(&Node::Br(1), 0).is_none()); + assert!(blob(&Node::Br(0), 0).is_some()); + // br_table: a label out of scope, a missing index + assert!(blob(&seq(vec![c32(0), Node::BrTable(vec![0, 1], 0)]), 0).is_none()); + assert!(blob(&Node::BrTable(vec![], 0), 0).is_none()); + // too many targets + let many: Vec = vec![0; 0x10001]; + assert!(blob(&seq(vec![c32(0), Node::BrTable(many, 0)]), 0).is_none()); + // `if` arms that leave different heights + let p = seq(vec![c32(1), Node::If(Box::new(c32(1)), Box::new(nop()))]); + assert!(blob(&p, 0).is_none()); + // a block that leaves a value + assert!(blob(&Node::Block(Box::new(c32(1))), 0).is_none()); + // code after a branch + assert!(blob(&then(Node::Br(0), c32(1)), 0).is_none()); + // a frame beyond 31 slots + assert!(blob(&nop(), 32).is_none()); + assert!(blob(&nop(), 31).is_some()); +} + +#[test] +fn exit_with_operands_is_accepted() { + let p = seq(vec![c64(1), c64(2), Node::Exit(42, 2)]); + assert!(accepted(&blob(&p, 0).unwrap_or_default())); +} + +#[test] +fn the_validator_refuses_a_broken_check() { + let p = seq(vec![ + c64(3), + Node::Mem(MemOp::Load { + region: 0, + ty: MTy::I64, + }), + ]); + let b = blob(&p, 0).unwrap_or_default(); + assert!(accepted(&b)); + // the check begins `mov rdx, rcx` (3 bytes), `add rdx, 8` (7); then comes + // `jb rel32` (`0f 82`), whose displacement follows + let at = b + .windows(2) + .position(|w| w == [0x0f, 0x82]) + .unwrap_or(usize::MAX); + assert_ne!(at, usize::MAX); + // jump one byte further: into the middle of an instruction + let mut moved = b.clone(); + if let Some(x) = moved.get_mut(at.saturating_add(2)) { + *x = x.wrapping_add(1); + } + assert!(!accepted(&moved)); + // the check's second compare is the one that bounds the access: turn + // `cmovae` (`0f 43`) into `cmovb` (`0f 42`) and the sequence is no + // longer the check + let mut broken = b.clone(); + let cm = b + .windows(2) + .position(|w| w == [0x0f, 0x43]) + .unwrap_or(usize::MAX); + assert_ne!(cm, usize::MAX); + if let Some(x) = broken.get_mut(cm.saturating_add(1)) { + *x = 0x42; + } + assert!(!accepted(&broken)); + // cut the last byte + assert!(!accepted(b.get(..b.len().saturating_sub(1)).unwrap_or(&[]))); +} diff --git a/crates/clazel-js-builtins/Cargo.toml b/crates/clazel-js-builtins/Cargo.toml new file mode 100644 index 000000000..10383368a --- /dev/null +++ b/crates/clazel-js-builtins/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "clazel-js-builtins" +version = "0.0.0" +edition.workspace = true +license.workspace = true +publish.workspace = true +description = "The global object's built-ins (ECMA-262 clauses 19 to 28) for Clazel's JavaScript engine (phase-7.md item 7.9): the first group, Object, Function, the Error constructors, Boolean, Number, String, Math and the global functions; the second group, Array and JSON." + +[dependencies] +clazel-js-heap = { path = "../clazel-js-heap" } +clazel-js-number = { path = "../clazel-js-number" } +clazel-js-vm = { path = "../clazel-js-vm" } +clazel-json = { path = "../clazel-json" } + +[dev-dependencies] +clazel-js-syntax = { path = "../clazel-js-syntax", version = "=0.0.0" } + +# Verification tier (docs/VERIFICATION.md). Plain Rust with unit tests and a +# Test262 run through the interpreter (tests/); `target-tier` is 0, as the +# Crates table of phase-7.md states for this crate. +[package.metadata.clazel] +tier = 0 +target-tier = 0 + +[lints] +workspace = true diff --git a/crates/clazel-js-builtins/src/array.rs b/crates/clazel-js-builtins/src/array.rs new file mode 100644 index 000000000..a5c864c5d --- /dev/null +++ b/crates/clazel-js-builtins/src/array.rs @@ -0,0 +1,1283 @@ +//! `Array` (§23.1†): the constructor, `Array.isArray`, `Array.of`, and the +//! core of `Array.prototype`: `push`, `pop`, `shift`, `unshift`, `slice`, +//! `splice`, `concat`, `join`, `toString`, `indexOf`, `lastIndexOf`, +//! `includes`, `reverse`, `fill`, `forEach`, `map`, `filter`, `some`, `every`, +//! `reduce`, `reduceRight`, `find`, `findIndex` and `sort`. +//! +//! The methods are generic, as the standard writes them: each reads and +//! writes through `Get`, `Set`, `HasProperty` and `DeletePropertyOrThrow` on +//! `ToObject(this value)`, and does not assume an Array exotic object. The +//! "mathematical values" of the algorithms (indices up to 2^53 - 1) are `f64`, +//! which holds them exactly. +//! +//! Left for when Symbols and iterators exist: `Array.from`, `entries`, +//! `keys`, `values` and `@@iterator`, `@@species`, `@@isConcatSpreadable`, +//! `@@unscopables`. `ArraySpeciesCreate` therefore uses the receiver's +//! `constructor` itself where the standard reads its `@@species`. + +use crate::util::{arg, constructor, js_string, method, number, to_integer_or_infinity}; +use clazel_js_heap::string::{JsString, Key}; +use clazel_js_heap::value::Value; +use clazel_js_vm::interp::{Abrupt, Intrinsic, Vm}; + +/// 2^53 - 1: the largest integer index and length (§6.1.6.1†, §7.1.20†). +const MAX_SAFE: f64 = 9_007_199_254_740_991.0; + +/// Installs `Array`, `Array.prototype` and their methods. +/// +/// # Errors +/// As [`crate::install`]. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + // §23.1.3†: "The Array prototype object is an Array exotic object ... with + // a [[Prototype]] of %Object.prototype% and a length of 0". + let object_proto = vm.intrinsic(Intrinsic::ObjectPrototype); + let proto = vm.heap_mut().new_array(object_proto, 0)?; + vm.set_intrinsic(Intrinsic::ArrayPrototype, proto); + let ctor = constructor(vm, "Array", 1, array_constructor, proto)?; + vm.set_intrinsic(Intrinsic::ArrayConstructor, ctor); + method(vm, ctor, "isArray", 1, is_array)?; + method(vm, ctor, "of", 0, array_of)?; + let methods: [(&str, i32, clazel_js_vm::interp::NativeFn); 24] = [ + ("concat", 1, concat), + ("every", 1, every), + ("fill", 1, fill), + ("filter", 1, filter), + ("find", 1, find), + ("findIndex", 1, find_index), + ("forEach", 1, for_each), + ("includes", 1, includes), + ("indexOf", 1, index_of), + ("join", 1, join), + ("lastIndexOf", 1, last_index_of), + ("map", 1, map), + ("pop", 0, pop), + ("push", 1, push), + ("reduce", 1, reduce), + ("reduceRight", 1, reduce_right), + ("reverse", 0, reverse), + ("shift", 0, shift), + ("slice", 2, slice), + ("some", 1, some), + ("sort", 1, sort), + ("splice", 2, splice), + ("toString", 0, to_string), + ("unshift", 1, unshift), + ]; + for (name, len, f) in methods { + method(vm, proto, name, len, f)?; + } + crate::util::global(vm, "Array", ctor) +} + +// --------------------------------------------------------------------- +// Abstract operations +// --------------------------------------------------------------------- + +/// The key `ToString(𝔽(k))` of an integer `k`. +fn pk(vm: &mut Vm, k: f64) -> Result { + vm.index_key(k as u64) +} + +/// `Get(O, ToString(k))`, §7.3.2†. +fn get_at(vm: &mut Vm, o: Value, k: f64) -> Result { + let key = pk(vm, k)?; + vm.get_value(o, key) +} + +/// `HasProperty(O, ToString(k))`, §7.3.12†. +fn has_at(vm: &mut Vm, o: Value, k: f64) -> Result { + let key = pk(vm, k)?; + Ok(vm.heap().find(o, key)?.is_some()) +} + +/// `Set(O, ToString(k), v, true)`, §7.3.4†. +fn set_at(vm: &mut Vm, o: Value, k: f64, v: Value) -> Result<(), Abrupt> { + let key = pk(vm, k)?; + vm.put_value(o, key, v, true) +} + +/// `DeletePropertyOrThrow(O, ToString(k))`, §7.3.10†. +fn delete_at(vm: &mut Vm, o: Value, k: f64) -> Result<(), Abrupt> { + let key = pk(vm, k)?; + vm.delete_property_or_throw(o, key) +} + +/// `CreateDataPropertyOrThrow(O, ToString(k), v)`, §7.3.7†. +fn create_at(vm: &mut Vm, o: Value, k: f64, v: Value) -> Result<(), Abrupt> { + let key = pk(vm, k)?; + vm.create_data_property_or_throw(o, key, v) +} + +/// `Set(O, "length", n, true)`. +fn set_length(vm: &mut Vm, o: Value, n: f64) -> Result<(), Abrupt> { + let key = crate::util::key(vm, "length")?; + vm.put_value(o, key, number(n), true) +} + +/// LengthOfArrayLike ( obj ), §7.3.19†: `ToLength(? Get(obj, "length"))`, and +/// ToLength (§7.1.20†): ToIntegerOrInfinity, then clamp to `[0, 2^53 - 1]`. +fn length_of(vm: &mut Vm, o: Value) -> Result { + let key = crate::util::key(vm, "length")?; + let v = vm.get_value(o, key)?; + let n = to_integer_or_infinity(vm, v)?; + Ok(n.clamp(0.0, MAX_SAFE)) +} + +/// The relative index of `start`, `end` and `fromIndex` arguments: ToIntegerOrInfinity, +/// then "if relative is -infinity, let k be 0; else if relative < 0, let k be +/// max(len + relative, 0); else let k be min(relative, len)". +fn relative(vm: &mut Vm, v: Value, len: f64) -> Result { + let rel = to_integer_or_infinity(vm, v)?; + Ok(if rel == f64::NEG_INFINITY { + 0.0 + } else if rel < 0.0 { + (len + rel).max(0.0) + } else { + rel.min(len) + }) +} + +/// The `end` argument: `len` when `undefined`, else as [`relative`]. +fn relative_end(vm: &mut Vm, v: Value, len: f64) -> Result { + if v == Value::UNDEFINED { + Ok(len) + } else { + relative(vm, v, len) + } +} + +/// A callable argument, else a TypeError (the first steps of `forEach`, `map` +/// and the others: "If IsCallable(callbackfn) is false, throw a TypeError"). +fn callable(vm: &mut Vm, f: Value) -> Result { + if vm.heap().is_callable(f) { + Ok(f) + } else { + let d = vm.describe(f); + Err(vm.type_error(&format!("{d} is not a function"))) + } +} + +/// SameValueZero, §7.2.11†: as SameValue except that `+0` equals `-0`. +fn same_value_zero(vm: &Vm, a: Value, b: Value) -> bool { + if let (Some(x), Some(y)) = (crate::util::as_number(a), crate::util::as_number(b)) { + return x == y || (x.is_nan() && y.is_nan()); + } + vm.same_value(a, b) +} + +/// ArraySpeciesCreate ( originalArray, length ), §10.4.2.3†: +/// 1. If IsArray(originalArray) is false, return ? ArrayCreate(length). +/// 2. Let C be ? Get(originalArray, "constructor"). +/// 3. If IsConstructor(C) is true, then (the cross-realm check: one realm). +/// 4. If C is an Object, then a. Set C to ? Get(C, @@species). (Not +/// consulted: no Symbols; C stays the constructor.) +/// 5. If C is undefined, return ? ArrayCreate(length). +/// 6. If IsConstructor(C) is false, throw a TypeError exception. +/// 7. Return ? Construct(C, « 𝔽(length) »). +fn species_create(vm: &mut Vm, original: Value, length: f64) -> Result { + if !vm.is_array(original) { + return vm.array_create(length, None); + } + let c = vm.get_named(original, "constructor")?; + if c == Value::UNDEFINED || c == vm.intrinsic(Intrinsic::ArrayConstructor) { + return vm.array_create(length, None); + } + if !vm.is_constructor(c) { + return Err(vm.type_error("object.constructor is not a constructor")); + } + vm.construct_value(c, &[number(length)]) +} + +// --------------------------------------------------------------------- +// The constructor and the static functions +// --------------------------------------------------------------------- + +/// `Array ( ...values )`, §23.1.1.1†: +/// 1. If NewTarget is undefined, let newTarget be the active function object; +/// else let newTarget be NewTarget. +/// 2. Let proto be ? GetPrototypeFromConstructor(newTarget, +/// "%Array.prototype%"). +/// 3. Let numberOfArgs be the number of elements in values. +/// 4. If numberOfArgs = 0, then a. Return ! ArrayCreate(0, proto). +/// 5. Else if numberOfArgs = 1, then +/// a. Let len be values[0]. b. Let array be ! ArrayCreate(0, proto). +/// c. If len is not a Number, then i. Perform ! +/// CreateDataPropertyOrThrow(array, "0", len). ii. Let intLen be 1𝔽. +/// d. Else, i. Let intLen be ! ToUint32(len). ii. If SameValueZero(intLen, +/// len) is false, throw a RangeError exception. +/// e. Perform ! Set(array, "length", intLen, true). f. Return array. +/// 6. Else, a. (Assert: numberOfArgs >= 2.) b. Let array be ? +/// ArrayCreate(numberOfArgs, proto). c. Let k be 0. d. Repeat, while k < +/// numberOfArgs, i. Let Pk be ! ToString(𝔽(k)). ii. Let itemK be +/// values[k]. iii. Perform ! CreateDataPropertyOrThrow(array, Pk, itemK). +/// iv. Set k to k + 1. e. (Assert: The mathematical value of array's +/// "length" property is numberOfArgs.) f. Return array. +fn array_constructor(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let nt = vm.new_target(); + let new_target = if nt == Value::UNDEFINED { + vm.active_function() + } else { + nt + }; + // GetPrototypeFromConstructor, §10.1.14†. + let proto = vm.get_named(new_target, "prototype")?; + let proto = if vm.heap().is_object(proto) { + proto + } else { + vm.array_prototype() + }; + match args { + [] => vm.array_create(0.0, Some(proto)), + [len] => { + let array = vm.array_create(0.0, Some(proto))?; + let int_len = if let Some(n) = crate::util::as_number(*len) { + let u = clazel_js_vm::numeric::to_uint32(clazel_js_vm::numeric::narrow(n)); + if f64::from(u) != n { + return Err(vm.range_error("Invalid array length")); + } + f64::from(u) + } else { + create_at(vm, array, 0.0, *len)?; + 1.0 + }; + set_length(vm, array, int_len)?; + Ok(array) + } + _ => { + let array = vm.array_create(args.len() as f64, Some(proto))?; + for (k, item) in args.iter().enumerate() { + create_at(vm, array, k as f64, *item)?; + } + Ok(array) + } + } +} + +/// `Array.isArray ( arg )`, §23.1.2.2†: Return ? IsArray(arg) (§7.2.2†). +fn is_array(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(Value::boolean(vm.is_array(arg(args, 0)))) +} + +/// `Array.of ( ...items )`, §23.1.2.3†: +/// 1. Let len be the number of elements in items. +/// 2. Let lenNumber be 𝔽(len). +/// 3. Let C be the this value. +/// 4. If IsConstructor(C) is true, then a. Let A be ? Construct(C, « +/// lenNumber »). +/// 5. Else, a. Let A be ? ArrayCreate(len). +/// 6. Let k be 0. +/// 7. Repeat, while k < len, a. Let kValue be items[k]. b. Let Pk be ! +/// ToString(𝔽(k)). c. Perform ? CreateDataPropertyOrThrow(A, Pk, kValue). +/// d. Set k to k + 1. +/// 8. Perform ? Set(A, "length", lenNumber, true). +/// 9. Return A. +fn array_of(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let len = args.len() as f64; + let a = if vm.is_constructor(this) { + vm.construct_value(this, &[number(len)])? + } else { + vm.array_create(len, None)? + }; + for (k, v) in args.iter().enumerate() { + create_at(vm, a, k as f64, *v)?; + } + set_length(vm, a, len)?; + Ok(a) +} + +// --------------------------------------------------------------------- +// Mutators +// --------------------------------------------------------------------- + +/// `Array.prototype.push ( ...items )`, §23.1.3.23†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. Let argCount be the number of elements in items. +/// 4. If len + argCount > 2^53 - 1, throw a TypeError exception. +/// 5. For each element E of items, do a. Perform ? Set(O, ! ToString(𝔽(len)), +/// E, true). b. Set len to len + 1. +/// 6. Perform ? Set(O, "length", 𝔽(len), true). +/// 7. Return 𝔽(len). +fn push(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let mut len = length_of(vm, o)?; + if len + args.len() as f64 > MAX_SAFE { + return Err(vm.type_error("Pushing elements past 2^53-1 length")); + } + for e in args { + set_at(vm, o, len, *e)?; + len += 1.0; + } + set_length(vm, o, len)?; + Ok(number(len)) +} + +/// `Array.prototype.pop ( )`, §23.1.3.22†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If len = 0, then a. Perform ? Set(O, "length", +0𝔽, true). b. Return +/// undefined. +/// 4. Else, a. (Assert: len > 0.) b. Let newLen be 𝔽(len - 1). c. Let index +/// be ! ToString(newLen). d. Let element be ? Get(O, index). e. Perform ? +/// DeletePropertyOrThrow(O, index). f. Perform ? Set(O, "length", newLen, +/// true). g. Return element. +fn pop(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + if len == 0.0 { + set_length(vm, o, 0.0)?; + return Ok(Value::UNDEFINED); + } + let new_len = len - 1.0; + let element = get_at(vm, o, new_len)?; + delete_at(vm, o, new_len)?; + set_length(vm, o, new_len)?; + Ok(element) +} + +/// `Array.prototype.shift ( )`, §23.1.3.27†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If len = 0, then a. Perform ? Set(O, "length", +0𝔽, true). b. Return +/// undefined. +/// 4. Let first be ? Get(O, "0"). +/// 5. Let k be 1. +/// 6. Repeat, while k < len, a. Let from be ! ToString(𝔽(k)). b. Let to be ! +/// ToString(𝔽(k - 1)). c. Let fromPresent be ? HasProperty(O, from). d. If +/// fromPresent is true, then i. Let fromVal be ? Get(O, from). ii. Perform +/// ? Set(O, to, fromVal, true). e. Else, i. (Assert: fromPresent is false.) +/// ii. Perform ? DeletePropertyOrThrow(O, to). f. Set k to k + 1. +/// 7. Perform ? DeletePropertyOrThrow(O, ! ToString(𝔽(len - 1))). +/// 8. Perform ? Set(O, "length", 𝔽(len - 1), true). +/// 9. Return first. +fn shift(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + if len == 0.0 { + set_length(vm, o, 0.0)?; + return Ok(Value::UNDEFINED); + } + let first = get_at(vm, o, 0.0)?; + let mut k = 1.0; + while k < len { + vm.charge_step()?; + if has_at(vm, o, k)? { + let v = get_at(vm, o, k)?; + set_at(vm, o, k - 1.0, v)?; + } else { + delete_at(vm, o, k - 1.0)?; + } + k += 1.0; + } + delete_at(vm, o, len - 1.0)?; + set_length(vm, o, len - 1.0)?; + Ok(first) +} + +/// `Array.prototype.unshift ( ...items )`, §23.1.3.34†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. Let argCount be the number of elements in items. +/// 4. If argCount > 0, then a. If len + argCount > 2^53 - 1, throw a +/// TypeError exception. b. Let k be len. c. Repeat, while k > 0, i. Let +/// from be ! ToString(𝔽(k - 1)). ii. Let to be ! ToString(𝔽(k + argCount - +/// 1)). iii. Let fromPresent be ? HasProperty(O, from). iv. If fromPresent +/// is true, then 1. Let fromValue be ? Get(O, from). 2. Perform ? Set(O, +/// to, fromValue, true). v. Else, 1. Perform ? DeletePropertyOrThrow(O, +/// to). vi. Set k to k - 1. d. Let j be +0𝔽. e. For each element E of +/// items, do i. Perform ? Set(O, ! ToString(j), E, true). ii. Set j to j + +/// 1𝔽. +/// 5. Perform ? Set(O, "length", 𝔽(len + argCount), true). +/// 6. Return 𝔽(len + argCount). +fn unshift(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let argc = args.len() as f64; + if argc > 0.0 { + if len + argc > MAX_SAFE { + return Err(vm.type_error("Invalid array length")); + } + let mut k = len; + while k > 0.0 { + vm.charge_step()?; + if has_at(vm, o, k - 1.0)? { + let v = get_at(vm, o, k - 1.0)?; + set_at(vm, o, k + argc - 1.0, v)?; + } else { + delete_at(vm, o, k + argc - 1.0)?; + } + k -= 1.0; + } + for (j, e) in args.iter().enumerate() { + set_at(vm, o, j as f64, *e)?; + } + } + set_length(vm, o, len + argc)?; + Ok(number(len + argc)) +} + +/// `Array.prototype.splice ( start, deleteCount, ...items )`, §23.1.3.31†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. Let relativeStart be ? ToIntegerOrInfinity(start). +/// 4. to 6. Let actualStart be len clamped as `relative`. +/// 7. Let itemCount be the number of elements in items. +/// 8. If start is not present, then a. Let actualDeleteCount be 0. +/// 9. Else if deleteCount is not present, then a. Let actualDeleteCount be +/// len - actualStart. +/// 10. Else, a. Let dc be ? ToIntegerOrInfinity(deleteCount). b. Let +/// actualDeleteCount be the result of clamping dc between 0 and len - +/// actualStart. +/// 11. If len + itemCount - actualDeleteCount > 2^53 - 1, throw a TypeError +/// exception. +/// 12. Let A be ? ArraySpeciesCreate(O, actualDeleteCount). +/// 13. Let k be 0. +/// 14. Repeat, while k < actualDeleteCount, a. Let from be ! +/// ToString(𝔽(actualStart plus k)). b. If ? HasProperty(O, from) is true, then i. Let fromValue be ? +/// Get(O, from). ii. Perform ? CreateDataPropertyOrThrow(A, ! ToString(𝔽(k)), +/// fromValue). c. Set k to k + 1. +/// 15. Perform ? Set(A, "length", 𝔽(actualDeleteCount), true). +/// 16. If itemCount < actualDeleteCount, then a. Set k to actualStart. b. +/// Repeat, while k < (len - actualDeleteCount), i. from is k + +/// actualDeleteCount, ii. to is k + itemCount, iii. if HasProperty(from) +/// Set(to, Get(from)), else DeletePropertyOrThrow(to), v. Set k to k + 1. +/// c. Set k to len. d. Repeat, while k > (len - actualDeleteCount + +/// itemCount), i. Perform ? DeletePropertyOrThrow(O, ! ToString(𝔽(k - +/// 1))). ii. Set k to k - 1. +/// 17. Else if itemCount > actualDeleteCount, then a. Set k to (len - +/// actualDeleteCount). b. Repeat, while k > actualStart, i. from is k + +/// actualDeleteCount - 1, ii. to is k + itemCount - 1, iii. as in step +/// 16.b.iii, v. Set k to k - 1. +/// 18. Set k to actualStart. +/// 19. For each element E of items, do a. Perform ? Set(O, ! ToString(𝔽(k)), E, +/// true). b. Set k to k + 1. +/// 20. Perform ? Set(O, "length", 𝔽(len - actualDeleteCount + itemCount), +/// true). +/// 21. Return A. +fn splice(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let actual_start = relative(vm, arg(args, 0), len)?; + let items: &[Value] = args.get(2..).unwrap_or(&[]); + let item_count = items.len() as f64; + let actual_delete_count = match args.len() { + 0 => 0.0, + 1 => len - actual_start, + _ => { + let dc = to_integer_or_infinity(vm, arg(args, 1))?; + dc.clamp(0.0, len - actual_start) + } + }; + if len + item_count - actual_delete_count > MAX_SAFE { + return Err(vm.type_error("Invalid array length")); + } + let a = species_create(vm, o, actual_delete_count)?; + let mut k = 0.0; + while k < actual_delete_count { + vm.charge_step()?; + if has_at(vm, o, actual_start + k)? { + let v = get_at(vm, o, actual_start + k)?; + create_at(vm, a, k, v)?; + } + k += 1.0; + } + set_length(vm, a, actual_delete_count)?; + let move_one = |vm: &mut Vm, from: f64, to: f64| -> Result<(), Abrupt> { + vm.charge_step()?; + if has_at(vm, o, from)? { + let v = get_at(vm, o, from)?; + set_at(vm, o, to, v) + } else { + delete_at(vm, o, to) + } + }; + if item_count < actual_delete_count { + let mut k = actual_start; + while k < len - actual_delete_count { + move_one(vm, k + actual_delete_count, k + item_count)?; + k += 1.0; + } + let mut k = len; + while k > len - actual_delete_count + item_count { + delete_at(vm, o, k - 1.0)?; + k -= 1.0; + } + } else if item_count > actual_delete_count { + let mut k = len - actual_delete_count; + while k > actual_start { + move_one(vm, k + actual_delete_count - 1.0, k + item_count - 1.0)?; + k -= 1.0; + } + } + for (i, e) in items.iter().enumerate() { + set_at(vm, o, actual_start + i as f64, *e)?; + } + set_length(vm, o, len - actual_delete_count + item_count)?; + Ok(a) +} + +/// `Array.prototype.reverse ( )`, §23.1.3.26†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. Let middle be floor(len / 2). +/// 4. Let lower be 0. +/// 5. Repeat, while lower ≠ middle, a. Let upper be len - lower - 1. b. Let +/// upperP be ! ToString(𝔽(upper)). c. Let lowerP be ! ToString(𝔽(lower)). +/// d. Let lowerExists be ? HasProperty(O, lowerP). e. If lowerExists is +/// true, then i. Let lowerValue be ? Get(O, lowerP). f. Let upperExists be +/// ? HasProperty(O, upperP). g. If upperExists is true, then i. Let +/// upperValue be ? Get(O, upperP). h. If lowerExists is true and +/// upperExists is true, then i. Perform ? Set(O, lowerP, upperValue, +/// true). ii. Perform ? Set(O, upperP, lowerValue, true). i. Else if +/// lowerExists is false and upperExists is true, then i. Perform ? Set(O, +/// lowerP, upperValue, true). ii. Perform ? DeletePropertyOrThrow(O, +/// upperP). j. Else if lowerExists is true and upperExists is false, then +/// i. Perform ? DeletePropertyOrThrow(O, lowerP). ii. Perform ? Set(O, +/// upperP, lowerValue, true). k. Else, (neither exists) no action. l. Set +/// lower to lower + 1. +/// 6. Return O. +fn reverse(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let middle = (len / 2.0).floor(); + let mut lower = 0.0; + while lower != middle { + vm.charge_step()?; + let upper = len - lower - 1.0; + let lower_exists = has_at(vm, o, lower)?; + let lower_value = if lower_exists { + get_at(vm, o, lower)? + } else { + Value::UNDEFINED + }; + let upper_exists = has_at(vm, o, upper)?; + let upper_value = if upper_exists { + get_at(vm, o, upper)? + } else { + Value::UNDEFINED + }; + match (lower_exists, upper_exists) { + (true, true) => { + set_at(vm, o, lower, upper_value)?; + set_at(vm, o, upper, lower_value)?; + } + (false, true) => { + set_at(vm, o, lower, upper_value)?; + delete_at(vm, o, upper)?; + } + (true, false) => { + delete_at(vm, o, lower)?; + set_at(vm, o, upper, lower_value)?; + } + (false, false) => {} + } + lower += 1.0; + } + Ok(o) +} + +/// `Array.prototype.fill ( value [ , start [ , end ] ] )`, §23.1.3.7†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. to 5. Let k be the start, clamped as `relative`. +/// 6. to 8. Let final be the end (len if undefined), clamped. +/// 9. Repeat, while k < final, a. Let Pk be ! ToString(𝔽(k)). b. Perform ? +/// Set(O, Pk, value, true). c. Set k to k + 1. +/// 10. Return O. +fn fill(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let mut k = relative(vm, arg(args, 1), len)?; + let fin = relative_end(vm, arg(args, 2), len)?; + while k < fin { + vm.charge_step()?; + set_at(vm, o, k, arg(args, 0))?; + k += 1.0; + } + Ok(o) +} + +// --------------------------------------------------------------------- +// Accessors +// --------------------------------------------------------------------- + +/// `Array.prototype.slice ( start, end )`, §23.1.3.28†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. to 5. Let k be the start, clamped as `relative`. +/// 6. to 8. Let final be the end (len if undefined), clamped. +/// 9. Let count be max(final - k, 0). +/// 10. Let A be ? ArraySpeciesCreate(O, count). +/// 11. Let n be 0. +/// 12. Repeat, while k < final, a. Let Pk be ! ToString(𝔽(k)). b. Let kPresent +/// be ? HasProperty(O, Pk). c. If kPresent is true, then i. Let kValue be +/// ? Get(O, Pk). ii. Perform ? CreateDataPropertyOrThrow(A, ! +/// ToString(𝔽(n)), kValue). d. Set k to k + 1. e. Set n to n + 1. +/// 13. Perform ? Set(A, "length", 𝔽(n), true). +/// 14. Return A. +fn slice(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let mut k = relative(vm, arg(args, 0), len)?; + let fin = relative_end(vm, arg(args, 1), len)?; + let count = (fin - k).max(0.0); + let a = species_create(vm, o, count)?; + let mut n = 0.0; + while k < fin { + vm.charge_step()?; + if has_at(vm, o, k)? { + let v = get_at(vm, o, k)?; + create_at(vm, a, n, v)?; + } + k += 1.0; + n += 1.0; + } + set_length(vm, a, n)?; + Ok(a) +} + +/// `Array.prototype.concat ( ...items )`, §23.1.3.2†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let A be ? ArraySpeciesCreate(O, 0). +/// 3. Let n be 0. +/// 4. Prepend O to items. +/// 5. For each element E of items, do a. Let spreadable be ? +/// IsConcatSpreadable(E) (§23.1.3.2.1†: if E is not an Object, false; (the +/// `@@isConcatSpreadable` lookup is not consulted: no Symbols;) else +/// IsArray(E)). b. If spreadable is true, then i. Let k be 0. ii. Let len +/// be ? LengthOfArrayLike(E). iii. If n + len > 2^53 - 1, throw a +/// TypeError exception. iv. Repeat, while k < len, 1. Let P be ! +/// ToString(𝔽(k)). 2. Let exists be ? HasProperty(E, P). 3. If exists is +/// true, then a. Let subElement be ? Get(E, P). b. Perform ? +/// CreateDataPropertyOrThrow(A, ! ToString(𝔽(n)), subElement). 4. Set n to +/// n + 1. 5. Set k to k + 1. c. Else, i. NOTE: E is added as a single +/// item rather than spread. ii. If n ≥ 2^53 - 1, throw a TypeError +/// exception. iii. Perform ? CreateDataPropertyOrThrow(A, ! ToString(𝔽(n)), +/// E). iv. Set n to n + 1. +/// 6. Perform ? Set(A, "length", 𝔽(n), true). +/// 7. Return A. +fn concat(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let a = species_create(vm, o, 0.0)?; + let mut n = 0.0; + let mut items = vec![o]; + items.extend_from_slice(args); + for e in items { + if vm.is_array(e) { + let len = length_of(vm, e)?; + if n + len > MAX_SAFE { + return Err(vm.type_error("Invalid array length")); + } + let mut k = 0.0; + while k < len { + vm.charge_step()?; + if has_at(vm, e, k)? { + let v = get_at(vm, e, k)?; + create_at(vm, a, n, v)?; + } + n += 1.0; + k += 1.0; + } + } else { + if n >= MAX_SAFE { + return Err(vm.type_error("Invalid array length")); + } + create_at(vm, a, n, e)?; + n += 1.0; + } + } + set_length(vm, a, n)?; + Ok(a) +} + +/// `Array.prototype.join ( separator )`, §23.1.3.18†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If separator is undefined, let sep be ",". +/// 4. Else, let sep be ? ToString(separator). +/// 5. Let R be the empty String. +/// 6. Let k be 0. +/// 7. Repeat, while k < len, a. If k > 0, set R to the string-concatenation of +/// R and sep. b. Let element be ? Get(O, ! ToString(𝔽(k))). c. If element +/// is either undefined or null, let next be the empty String; otherwise, +/// let next be ? ToString(element). d. Set R to the string-concatenation of +/// R and next. e. Set k to k + 1. +/// 8. Return R. +fn join(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let sep: Vec = if arg(args, 0) == Value::UNDEFINED { + vec![u16::from(b',')] + } else { + vm.to_js_string(arg(args, 0))?.units().collect() + }; + let mut r: Vec = Vec::new(); + let mut k = 0.0; + while k < len { + vm.charge_step()?; + if k > 0.0 { + r.extend_from_slice(&sep); + } + let element = get_at(vm, o, k)?; + if element != Value::UNDEFINED && element != Value::NULL { + r.extend(vm.to_js_string(element)?.units()); + } + k += 1.0; + } + let s = JsString::from_units(&r).map_err(|_| Abrupt::OutOfMemory)?; + js_string(vm, s) +} + +/// `Array.prototype.toString ( )`, §23.1.3.33†: +/// 1. Let array be ? ToObject(this value). +/// 2. Let func be ? Get(array, "join"). +/// 3. If IsCallable(func) is false, then a. Set func to the intrinsic +/// function %Object.prototype.toString%. +/// 4. Return ? Call(func, array). +fn to_string(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + let array = vm.to_object(this)?; + let func = vm.get_named(array, "join")?; + let func = if vm.heap().is_callable(func) { + func + } else { + let op = vm.intrinsic(Intrinsic::ObjectPrototype); + vm.get_named(op, "toString")? + }; + vm.call(func, array, &[]) +} + +/// `Array.prototype.indexOf ( searchElement [ , fromIndex ] )`, §23.1.3.17†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If len = 0, return -1𝔽. +/// 4. Let n be ? ToIntegerOrInfinity(fromIndex). +/// 5. (Assert: If fromIndex is undefined, then n is 0.) +/// 6. If n = +∞, return -1𝔽. +/// 7. Else if n = -∞, set n to 0. +/// 8. If n ≥ 0, then a. Let k be n. +/// 9. Else, a. Let k be len + n. b. If k < 0, set k to 0. +/// 10. Repeat, while k < len, a. Let kPresent be ? HasProperty(O, ! +/// ToString(𝔽(k))). b. If kPresent is true, then i. Let elementK be ? +/// Get(O, ! ToString(𝔽(k))). ii. If IsStrictlyEqual(searchElement, +/// elementK) is true, return 𝔽(k). c. Set k to k + 1. +/// 11. Return -1𝔽. +fn index_of(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + if len == 0.0 { + return Ok(Value::int32(-1)); + } + let n = to_integer_or_infinity(vm, arg(args, 1))?; + if n == f64::INFINITY { + return Ok(Value::int32(-1)); + } + let mut k = if n == f64::NEG_INFINITY { + 0.0 + } else if n >= 0.0 { + n + } else { + (len + n).max(0.0) + }; + while k < len { + vm.charge_step()?; + if has_at(vm, o, k)? { + let e = get_at(vm, o, k)?; + if vm.strict_equals(arg(args, 0), e) { + return Ok(number(k)); + } + } + k += 1.0; + } + Ok(Value::int32(-1)) +} + +/// `Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )`, +/// §23.1.3.20†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If len = 0, return -1𝔽. +/// 4. If fromIndex is present, let n be ? ToIntegerOrInfinity(fromIndex); else +/// let n be len - 1. +/// 5. If n = -∞, return -1𝔽. +/// 6. If n ≥ 0, then a. Let k be min(n, len - 1). +/// 7. Else, a. Let k be len + n. +/// 8. Repeat, while k ≥ 0, a. Let kPresent be ? HasProperty(O, ! ToString(𝔽(k))). +/// b. If kPresent is true, then i. Let elementK be ? Get(O, ! ToString(𝔽(k))). +/// ii. If IsStrictlyEqual(searchElement, elementK) is true, return 𝔽(k). +/// c. Set k to k - 1. +/// 9. Return -1𝔽. +fn last_index_of(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + if len == 0.0 { + return Ok(Value::int32(-1)); + } + let n = if args.len() > 1 { + to_integer_or_infinity(vm, arg(args, 1))? + } else { + len - 1.0 + }; + if n == f64::NEG_INFINITY { + return Ok(Value::int32(-1)); + } + let mut k = if n >= 0.0 { n.min(len - 1.0) } else { len + n }; + while k >= 0.0 { + vm.charge_step()?; + if has_at(vm, o, k)? { + let e = get_at(vm, o, k)?; + if vm.strict_equals(arg(args, 0), e) { + return Ok(number(k)); + } + } + k -= 1.0; + } + Ok(Value::int32(-1)) +} + +/// `Array.prototype.includes ( searchElement [ , fromIndex ] )`, §23.1.3.16†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If len = 0, return false. +/// 4. Let n be ? ToIntegerOrInfinity(fromIndex). +/// 5. (Assert: If fromIndex is undefined, then n is 0.) +/// 6. If n = +∞, return false. +/// 7. Else if n = -∞, set n to 0. +/// 8. If n ≥ 0, then a. Let k be n. +/// 9. Else, a. Let k be len + n. b. If k < 0, set k to 0. +/// 10. Repeat, while k < len, a. Let elementK be ? Get(O, ! ToString(𝔽(k))). +/// b. If SameValueZero(searchElement, elementK) is true, return true. c. +/// Set k to k + 1. +/// 11. Return false. +fn includes(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + if len == 0.0 { + return Ok(Value::FALSE); + } + let n = to_integer_or_infinity(vm, arg(args, 1))?; + if n == f64::INFINITY { + return Ok(Value::FALSE); + } + let mut k = if n == f64::NEG_INFINITY { + 0.0 + } else if n >= 0.0 { + n + } else { + (len + n).max(0.0) + }; + while k < len { + vm.charge_step()?; + let e = get_at(vm, o, k)?; + if same_value_zero(vm, arg(args, 0), e) { + return Ok(Value::TRUE); + } + k += 1.0; + } + Ok(Value::FALSE) +} + +// --------------------------------------------------------------------- +// Callback methods +// --------------------------------------------------------------------- + +/// The callback of one element: `Call(callbackfn, thisArg, « kValue, 𝔽(k), O +/// »)`. +fn call3( + vm: &mut Vm, + f: Value, + this_arg: Value, + v: Value, + k: f64, + o: Value, +) -> Result { + vm.call(f, this_arg, &[v, number(k), o]) +} + +/// `Array.prototype.forEach ( callbackfn [ , thisArg ] )`, §23.1.3.15†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If IsCallable(callbackfn) is false, throw a TypeError exception. +/// 4. Let k be 0. +/// 5. Repeat, while k < len, a. Let Pk be ! ToString(𝔽(k)). b. Let kPresent be +/// ? HasProperty(O, Pk). c. If kPresent is true, then i. Let kValue be ? +/// Get(O, Pk). ii. Perform ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O +/// »). d. Set k to k + 1. +/// 6. Return undefined. +fn for_each(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let f = callable(vm, arg(args, 0))?; + let mut k = 0.0; + while k < len { + vm.charge_step()?; + if has_at(vm, o, k)? { + let v = get_at(vm, o, k)?; + call3(vm, f, arg(args, 1), v, k, o)?; + } + k += 1.0; + } + Ok(Value::UNDEFINED) +} + +/// `Array.prototype.map ( callbackfn [ , thisArg ] )`, §23.1.3.21†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If IsCallable(callbackfn) is false, throw a TypeError exception. +/// 4. Let A be ? ArraySpeciesCreate(O, len). +/// 5. Let k be 0. +/// 6. Repeat, while k < len, a. Let Pk be ! ToString(𝔽(k)). b. Let kPresent be +/// ? HasProperty(O, Pk). c. If kPresent is true, then i. Let kValue be ? +/// Get(O, Pk). ii. Let mappedValue be ? Call(callbackfn, thisArg, « kValue, +/// 𝔽(k), O »). iii. Perform ? CreateDataPropertyOrThrow(A, Pk, +/// mappedValue). d. Set k to k + 1. +/// 7. Return A. +fn map(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let f = callable(vm, arg(args, 0))?; + let a = species_create(vm, o, len)?; + let mut k = 0.0; + while k < len { + vm.charge_step()?; + if has_at(vm, o, k)? { + let v = get_at(vm, o, k)?; + let mapped = call3(vm, f, arg(args, 1), v, k, o)?; + create_at(vm, a, k, mapped)?; + } + k += 1.0; + } + Ok(a) +} + +/// `Array.prototype.filter ( callbackfn [ , thisArg ] )`, §23.1.3.8†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If IsCallable(callbackfn) is false, throw a TypeError exception. +/// 4. Let A be ? ArraySpeciesCreate(O, 0). +/// 5. Let k be 0. +/// 6. Let to be 0. +/// 7. Repeat, while k < len, a. Let Pk be ! ToString(𝔽(k)). b. Let kPresent be +/// ? HasProperty(O, Pk). c. If kPresent is true, then i. Let kValue be ? +/// Get(O, Pk). ii. Let selected be ToBoolean(? Call(callbackfn, thisArg, « +/// kValue, 𝔽(k), O »)). iii. If selected is true, then 1. Perform ? +/// CreateDataPropertyOrThrow(A, ! ToString(𝔽(to)), kValue). 2. Set to to to +/// + 1. d. Set k to k + 1. +/// 8. Return A. +fn filter(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let f = callable(vm, arg(args, 0))?; + let a = species_create(vm, o, 0.0)?; + let (mut k, mut to) = (0.0, 0.0); + while k < len { + vm.charge_step()?; + if has_at(vm, o, k)? { + let v = get_at(vm, o, k)?; + let r = call3(vm, f, arg(args, 1), v, k, o)?; + if vm.to_boolean(r) { + create_at(vm, a, to, v)?; + to += 1.0; + } + } + k += 1.0; + } + Ok(a) +} + +/// `Array.prototype.some ( callbackfn [ , thisArg ] )`, §23.1.3.29†, and +/// `Array.prototype.every`, §23.1.3.6†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If IsCallable(callbackfn) is false, throw a TypeError exception. +/// 4. Let k be 0. +/// 5. Repeat, while k < len, a. Let Pk be ! ToString(𝔽(k)). b. Let kPresent be +/// ? HasProperty(O, Pk). c. If kPresent is true, then i. Let kValue be ? +/// Get(O, Pk). ii. Let testResult be ToBoolean(? Call(callbackfn, thisArg, +/// « kValue, 𝔽(k), O »)). iii. (`some`:) If testResult is true, return +/// true. (`every`:) If testResult is false, return false. d. Set k to k + +/// 1. +/// 6. Return false (`some`) or true (`every`). +fn some_or_every(vm: &mut Vm, this: Value, args: &[Value], is_some: bool) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let f = callable(vm, arg(args, 0))?; + let mut k = 0.0; + while k < len { + vm.charge_step()?; + if has_at(vm, o, k)? { + let v = get_at(vm, o, k)?; + let r = call3(vm, f, arg(args, 1), v, k, o)?; + if vm.to_boolean(r) == is_some { + return Ok(Value::boolean(is_some)); + } + } + k += 1.0; + } + Ok(Value::boolean(!is_some)) +} + +fn some(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + some_or_every(vm, this, args, true) +} + +fn every(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + some_or_every(vm, this, args, false) +} + +/// `Array.prototype.reduce ( callbackfn [ , initialValue ] )`, §23.1.3.24†, +/// and `reduceRight`, §23.1.3.25†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. If IsCallable(callbackfn) is false, throw a TypeError exception. +/// 4. If len = 0 and initialValue is not present, throw a TypeError exception. +/// 5. Let k be 0 (`reduceRight`: len - 1). +/// 6. Let accumulator be undefined. +/// 7. If initialValue is present, then a. Set accumulator to initialValue. +/// 8. Else, a. Let kPresent be false. b. Repeat, while kPresent is false and k +/// < len (k ≥ 0), i. Let Pk be ! ToString(𝔽(k)). ii. Set kPresent to ? +/// HasProperty(O, Pk). iii. If kPresent is true, then 1. Set accumulator to +/// ? Get(O, Pk). iv. Set k to k + 1 (k - 1). c. If kPresent is false, throw +/// a TypeError exception. +/// 9. Repeat, while k < len (k ≥ 0), a. Let Pk be ! ToString(𝔽(k)). b. Let +/// kPresent be ? HasProperty(O, Pk). c. If kPresent is true, then i. Let +/// kValue be ? Get(O, Pk). ii. Set accumulator to ? Call(callbackfn, +/// undefined, « accumulator, kValue, 𝔽(k), O »). d. Set k to k + 1 (k - 1). +/// 10. Return accumulator. +fn reduce_impl(vm: &mut Vm, this: Value, args: &[Value], right: bool) -> Result { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let f = callable(vm, arg(args, 0))?; + if len == 0.0 && args.len() < 2 { + return Err(vm.type_error("Reduce of empty array with no initial value")); + } + let (mut k, step) = if right { (len - 1.0, -1.0) } else { (0.0, 1.0) }; + let in_range = |k: f64| if right { k >= 0.0 } else { k < len }; + let mut acc = Value::UNDEFINED; + if args.len() >= 2 { + acc = arg(args, 1); + } else { + let mut present = false; + while !present && in_range(k) { + vm.charge_step()?; + present = has_at(vm, o, k)?; + if present { + acc = get_at(vm, o, k)?; + } + k += step; + } + if !present { + return Err(vm.type_error("Reduce of empty array with no initial value")); + } + } + while in_range(k) { + vm.charge_step()?; + if has_at(vm, o, k)? { + let v = get_at(vm, o, k)?; + acc = vm.call(f, Value::UNDEFINED, &[acc, v, number(k), o])?; + } + k += step; + } + Ok(acc) +} + +fn reduce(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + reduce_impl(vm, this, args, false) +} + +fn reduce_right(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + reduce_impl(vm, this, args, true) +} + +/// `Array.prototype.find ( predicate [ , thisArg ] )`, §23.1.3.9†, and +/// `findIndex`, §23.1.3.10†, over FindViaPredicate ( O, len, direction, +/// predicate, thisArg ), §23.1.3.9.1†: +/// 1. Let O be ? ToObject(this value). +/// 2. Let len be ? LengthOfArrayLike(O). +/// 3. Let findRec be ? FindViaPredicate(O, len, ascending, predicate, +/// thisArg). +/// 4. Return findRec.[[Value]] (`find`) or findRec.[[Index]] (`findIndex`). +/// +/// FindViaPredicate: +/// 1. If IsCallable(predicate) is false, throw a TypeError exception. +/// 2. If direction is ascending, let indices be a List of the integers in the +/// interval from 0 to len, exclusive, in ascending order. +/// 4. For each integer k of indices, do a. Let Pk be ! ToString(𝔽(k)). b. NOTE: +/// If O is a TypedArray, the following invocation of Get will return a +/// normal completion. c. Let kValue be ? Get(O, Pk). d. Let testResult be ? +/// Call(predicate, thisArg, « kValue, 𝔽(k), O »). e. If ToBoolean(testResult) +/// is true, return the Record { [[Index]]: 𝔽(k), [[Value]]: kValue }. +/// 5. Return the Record { [[Index]]: -1𝔽, [[Value]]: undefined }. +fn find_via_predicate(vm: &mut Vm, this: Value, args: &[Value]) -> Result<(f64, Value), Abrupt> { + let o = vm.to_object(this)?; + let len = length_of(vm, o)?; + let f = callable(vm, arg(args, 0))?; + let mut k = 0.0; + while k < len { + vm.charge_step()?; + let v = get_at(vm, o, k)?; + let r = call3(vm, f, arg(args, 1), v, k, o)?; + if vm.to_boolean(r) { + return Ok((k, v)); + } + k += 1.0; + } + Ok((-1.0, Value::UNDEFINED)) +} + +fn find(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + Ok(find_via_predicate(vm, this, args)?.1) +} + +fn find_index(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + Ok(number(find_via_predicate(vm, this, args)?.0)) +} + +// --------------------------------------------------------------------- +// sort +// --------------------------------------------------------------------- + +/// SortCompare ( x, y ), §23.1.3.30.2†, with the `comparefn` of the call: +/// 1. If x and y are both undefined, return +0𝔽. +/// 2. If x is undefined, return 1𝔽. +/// 3. If y is undefined, return -1𝔽. +/// 4. If comparefn is not undefined, then a. Let v be ? ToNumber(? Call(comparefn, +/// undefined, « x, y »)). b. If v is NaN, return +0𝔽. c. Return v. +/// 5. Let xString be ? ToString(x). +/// 6. Let yString be ? ToString(y). +/// 7. Let xSmaller be ! IsLessThan(xString, yString, true) (by code units). +/// 8. If xSmaller is true, return -1𝔽. +/// 9. Let ySmaller be ! IsLessThan(yString, xString, true). +/// 10. If ySmaller is true, return 1𝔽. +/// 11. Return +0𝔽. +fn sort_compare(vm: &mut Vm, cmp: Value, x: Value, y: Value) -> Result { + if x == Value::UNDEFINED && y == Value::UNDEFINED { + return Ok(0.0); + } + if x == Value::UNDEFINED { + return Ok(1.0); + } + if y == Value::UNDEFINED { + return Ok(-1.0); + } + if cmp != Value::UNDEFINED { + let r = vm.call(cmp, Value::UNDEFINED, &[x, y])?; + let v = vm.to_number(r)?; + return Ok(if v.is_nan() { 0.0 } else { v }); + } + let xs = vm.to_js_string(x)?; + let ys = vm.to_js_string(y)?; + Ok(match xs.units().cmp(ys.units()) { + std::cmp::Ordering::Less => -1.0, + std::cmp::Ordering::Greater => 1.0, + std::cmp::Ordering::Equal => 0.0, + }) +} + +/// A stable merge sort over `items` by `SortCompare` (the standard requires a +/// stable sort, §23.1.3.30† NOTE 2; an exception from the comparator ends it). +fn merge_sort(vm: &mut Vm, cmp: Value, items: Vec) -> Result, Abrupt> { + let n = items.len(); + if n < 2 { + return Ok(items); + } + let mut src = items; + let mut width = 1usize; + while width < n { + let mut dst: Vec = Vec::with_capacity(n); + let mut start = 0usize; + while start < n { + let mid = start.saturating_add(width).min(n); + let end = start.saturating_add(width.saturating_mul(2)).min(n); + let (mut i, mut j) = (start, mid); + while i < mid && j < end { + vm.charge_step()?; + let (l, r) = ( + *src.get(i).ok_or(Abrupt::Internal("sort index"))?, + *src.get(j).ok_or(Abrupt::Internal("sort index"))?, + ); + // Stable: the right one goes first only when it is smaller. + if sort_compare(vm, cmp, l, r)? > 0.0 { + dst.push(r); + j = j.saturating_add(1); + } else { + dst.push(l); + i = i.saturating_add(1); + } + } + while let Some(v) = src.get(i).filter(|_| i < mid) { + dst.push(*v); + i = i.saturating_add(1); + } + while let Some(v) = src.get(j).filter(|_| j < end) { + dst.push(*v); + j = j.saturating_add(1); + } + start = end; + } + src = dst; + width = width.saturating_mul(2); + } + Ok(src) +} + +/// `Array.prototype.sort ( comparefn )`, §23.1.3.30†: +/// 1. If comparefn is not undefined and IsCallable(comparefn) is false, throw +/// a TypeError exception. +/// 2. Let obj be ? ToObject(this value). +/// 3. Let len be ? LengthOfArrayLike(obj). +/// 4. Let SortCompare be a new Abstract Closure ... that performs +/// SortCompare(x, y) with comparefn. +/// 5. Let sortedList be ? SortIndexedProperties(obj, len, SortCompare, +/// skip-holes). +/// 6. Let itemCount be the number of elements in sortedList. +/// 7. Let j be 0. +/// 8. Repeat, while j < itemCount, a. Perform ? Set(obj, ! ToString(𝔽(j)), +/// sortedList[j], true). b. Set j to j + 1. +/// 9. NOTE: The call to SortIndexedProperties in step 5 uses skip-holes. The +/// remaining indices are deleted to preserve the number of holes that were +/// detected and excluded from the sort. +/// 10. Repeat, while j < len, a. Perform ? DeletePropertyOrThrow(obj, ! +/// ToString(𝔽(j))). b. Set j to j + 1. +/// 11. Return obj. +/// +/// SortIndexedProperties ( obj, len, SortCompare, holes ), §23.1.3.30.1†: +/// 1. Let items be a new empty List. +/// 2. Let k be 0. +/// 3. Repeat, while k < len, a. Let Pk be ! ToString(𝔽(k)). b. If holes is +/// skip-holes, then i. Let kRead be ? HasProperty(obj, Pk). c. Else, (read-through-holes) +/// ... d. If kRead is true, then i. Let kValue be ? Get(obj, Pk). ii. +/// Append kValue to items. e. Set k to k + 1. +/// 4. Sort items using an implementation-defined sequence of calls to +/// SortCompare. If any call returns an abrupt completion, stop before +/// performing any further calls and return that Completion Record. (The +/// sort is stable.) +/// 5. Return items. +fn sort(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let cmp = arg(args, 0); + if cmp != Value::UNDEFINED && !vm.heap().is_callable(cmp) { + return Err(vm.type_error("The comparison function must be either a function or undefined")); + } + let obj = vm.to_object(this)?; + let len = length_of(vm, obj)?; + let mut items = Vec::new(); + let mut k = 0.0; + while k < len { + vm.charge_step()?; + if has_at(vm, obj, k)? { + items.push(get_at(vm, obj, k)?); + } + k += 1.0; + } + let sorted = merge_sort(vm, cmp, items)?; + let mut j = 0.0; + for v in &sorted { + set_at(vm, obj, j, *v)?; + j += 1.0; + } + while j < len { + vm.charge_step()?; + delete_at(vm, obj, j)?; + j += 1.0; + } + Ok(obj) +} diff --git a/crates/clazel-js-builtins/src/boolean.rs b/crates/clazel-js-builtins/src/boolean.rs new file mode 100644 index 000000000..f2b695a37 --- /dev/null +++ b/crates/clazel-js-builtins/src/boolean.rs @@ -0,0 +1,69 @@ +//! `Boolean` (§20.3†). + +use crate::util::{arg, constructor, method, string}; +use clazel_js_heap::value::{Decoded, Value}; +use clazel_js_vm::heap::ObjKind; +use clazel_js_vm::interp::{Abrupt, Intrinsic, Vm}; + +/// Installs `Boolean` and `Boolean.prototype`. +/// +/// # Errors +/// As [`crate::install`]. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + // §20.3.4†: the prototype is itself a Boolean object whose [[BooleanData]] + // is false. + let object_proto = vm.intrinsic(Intrinsic::ObjectPrototype); + let proto = vm.new_wrapper(Value::FALSE, object_proto)?; + vm.set_intrinsic(Intrinsic::BooleanPrototype, proto); + let ctor = constructor(vm, "Boolean", 1, boolean, proto)?; + method(vm, proto, "toString", 0, to_string)?; + method(vm, proto, "valueOf", 0, value_of)?; + crate::util::global(vm, "Boolean", ctor) +} + +/// `Boolean ( value )`, §20.3.1.1†: +/// 1. Let b be ToBoolean(value). +/// 2. If NewTarget is undefined, return b. +/// 3. Let O be ? OrdinaryCreateFromConstructor(NewTarget, +/// "%Boolean.prototype%", « [[BooleanData]] »). +/// 4. Set O.[[BooleanData]] to b. +/// 5. Return O. +fn boolean(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let b = Value::boolean(vm.to_boolean(arg(args, 0))); + let nt = vm.new_target(); + if nt == Value::UNDEFINED { + return Ok(b); + } + vm.create_from_constructor(nt, Intrinsic::BooleanPrototype, ObjKind::Primitive(b)) +} + +/// thisBooleanValue ( value ), §20.3.3.3.1†: +/// 1. If value is a Boolean, return value. +/// 2. If value is an Object and value has a [[BooleanData]] internal slot, +/// then return value.[[BooleanData]]. +/// 3. Throw a TypeError exception. +fn this_boolean_value(vm: &mut Vm, v: Value) -> Result { + if let Decoded::Bool(b) = v.decode() { + return Ok(b); + } + if let Some(ObjKind::Primitive(p)) = vm.heap().obj(v).map(|o| o.kind) + && let Decoded::Bool(b) = p.decode() + { + return Ok(b); + } + Err(vm.type_error("not a Boolean")) +} + +/// `Boolean.prototype.toString ( )`, §20.3.3.2†: +/// 1. Let b be ? thisBooleanValue(this value). +/// 2. If b is true, return "true"; else return "false". +fn to_string(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + let b = this_boolean_value(vm, this)?; + string(vm, if b { "true" } else { "false" }) +} + +/// `Boolean.prototype.valueOf ( )`, §20.3.3.3†: +/// 1. Return ? thisBooleanValue(this value). +fn value_of(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + Ok(Value::boolean(this_boolean_value(vm, this)?)) +} diff --git a/crates/clazel-js-builtins/src/error.rs b/crates/clazel-js-builtins/src/error.rs new file mode 100644 index 000000000..559ff00ad --- /dev/null +++ b/crates/clazel-js-builtins/src/error.rs @@ -0,0 +1,152 @@ +//! `Error` and the native errors (§20.5†): `TypeError`, `RangeError`, +//! `SyntaxError`, `ReferenceError`. The VM's own throws +//! (`Vm::type_error`, ...) build the same objects, from the prototypes +//! installed here. + +use crate::util::{METHOD, arg, constructor, define, has_property, key, new_object_with, string}; +use clazel_js_heap::shape::Attrs; +use clazel_js_heap::value::Value; +use clazel_js_vm::heap::ObjKind; +use clazel_js_vm::interp::{Abrupt, Intrinsic, Vm}; + +/// Installs the five error constructors and their prototypes. +/// +/// # Errors +/// As [`crate::install`]. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + let object_proto = vm.intrinsic(Intrinsic::ObjectPrototype); + // §20.5.3†: %Error.prototype% is an ordinary object (it has no + // [[ErrorData]]) with `name` "Error" and `message` "". + let error_proto = new_object_with(vm, object_proto, ObjKind::Plain)?; + let error = constructor(vm, "Error", 1, error_constructor, error_proto)?; + describe(vm, error_proto, "Error")?; + crate::util::method(vm, error_proto, "toString", 0, to_string)?; + vm.set_intrinsic(Intrinsic::ErrorPrototype, error_proto); + crate::util::global(vm, "Error", error)?; + for (name, which) in [ + ("TypeError", Intrinsic::TypeErrorPrototype), + ("RangeError", Intrinsic::RangeErrorPrototype), + ("SyntaxError", Intrinsic::SyntaxErrorPrototype), + ("ReferenceError", Intrinsic::ReferenceErrorPrototype), + ] { + // §20.5.6.2†: the prototype's [[Prototype]] is %Error.prototype%; + // §20.5.6.1†: the constructor's is %Error%. + let proto = new_object_with(vm, error_proto, ObjKind::Plain)?; + let ctor = constructor(vm, name, 1, error_constructor, proto)?; + vm.heap_mut().set_proto(ctor, error)?; + describe(vm, proto, name)?; + vm.set_intrinsic(which, proto); + crate::util::global(vm, name, ctor)?; + } + Ok(()) +} + +/// The `name` and `message` properties of a prototype (§20.5.3.3†, +/// §20.5.3.2†, §20.5.6.3†, §20.5.6.4†). +fn describe(vm: &mut Vm, proto: Value, name: &str) -> Result<(), Abrupt> { + let n = string(vm, name)?; + define(vm, proto, "name", n, METHOD)?; + let m = string(vm, "")?; + define(vm, proto, "message", m, METHOD) +} + +/// `Error ( message [ , options ] )`, §20.5.1.1†, and +/// `NativeError ( message [ , options ] )`, §20.5.6.1.1† (one function +/// serves all five: the running constructor says which): +/// 1. If NewTarget is undefined, let newTarget be the active function +/// object; else let newTarget be NewTarget. +/// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%Error.prototype%" +/// (or the native error's), « [[ErrorData]] »). +/// 3. If message is not undefined, then +/// a. Let msg be ? ToString(message). +/// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg). +/// 4. Perform ? InstallErrorCause(O, options). +/// 5. Return O. +fn error_constructor(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let active = vm.active_function(); + let nt = if vm.new_target() == Value::UNDEFINED { + active + } else { + vm.new_target() + }; + // The fallback prototype is the running constructor's own `prototype` + // (the intrinsic of its error type). + let kp = key(vm, "prototype")?; + let fallback = vm.get_value(active, kp)?; + let from_nt = if vm.heap().is_object(nt) { + vm.get_value(nt, kp)? + } else { + Value::UNDEFINED + }; + let proto = if vm.heap().is_object(from_nt) { + from_nt + } else { + fallback + }; + let o = new_object_with(vm, proto, ObjKind::Error)?; + let message = arg(args, 0); + if message != Value::UNDEFINED { + let msg = vm.to_js_string(message)?; + let msg = crate::util::js_string(vm, msg)?; + define(vm, o, "message", msg, METHOD)?; + } + // InstallErrorCause ( O, options ), §20.5.8.1†: + // 1. If options is an Object and ? HasProperty(options, "cause") is + // true, then a. Let cause be ? Get(options, "cause"). + // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause). + let options = arg(args, 1); + if vm.heap().is_object(options) { + let kc = key(vm, "cause")?; + if has_property(vm, options, kc)? { + let cause = vm.get_value(options, kc)?; + define( + vm, + o, + "cause", + cause, + Attrs(Attrs::WRITABLE | Attrs::CONFIGURABLE), + )?; + } + } + Ok(o) +} + +/// `Error.prototype.toString ( )`, §20.5.3.4†: +/// 1. Let O be the this value. +/// 2. If O is not an Object, throw a TypeError exception. +/// 3. Let name be ? Get(O, "name"). +/// 4. If name is undefined, set name to "Error"; otherwise set name to ? +/// ToString(name). +/// 5. Let msg be ? Get(O, "message"). +/// 6. If msg is undefined, set msg to the empty String; otherwise set msg to +/// ? ToString(msg). +/// 7. If name is the empty String, return msg. +/// 8. If msg is the empty String, return name. +/// 9. Return the string-concatenation of name, the code unit 0x003A (COLON), +/// the code unit 0x0020 (SPACE), and msg. +fn to_string(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + if !vm.heap().is_object(this) { + return Err(vm.type_error("Error.prototype.toString called on a non-object")); + } + let kn = key(vm, "name")?; + let name = vm.get_value(this, kn)?; + let name = if name == Value::UNDEFINED { + "Error".to_string() + } else { + vm.to_js_string(name)?.to_string_lossy() + }; + let km = key(vm, "message")?; + let msg = vm.get_value(this, km)?; + let msg = if msg == Value::UNDEFINED { + String::new() + } else { + vm.to_js_string(msg)?.to_string_lossy() + }; + if name.is_empty() { + return string(vm, &msg); + } + if msg.is_empty() { + return string(vm, &name); + } + string(vm, &format!("{name}: {msg}")) +} diff --git a/crates/clazel-js-builtins/src/function.rs b/crates/clazel-js-builtins/src/function.rs new file mode 100644 index 000000000..62829cd1d --- /dev/null +++ b/crates/clazel-js-builtins/src/function.rs @@ -0,0 +1,148 @@ +//! `Function` (§20.2†): the constructor and `Function.prototype.call`, +//! `apply`, `bind`. + +use crate::util::{ + CONFIGURABLE_ONLY, arg, constructor, define, has_own_property, key, method, number, string, + to_integer_or_infinity, +}; +use clazel_js_heap::value::Value; +use clazel_js_vm::interp::{Abrupt, Intrinsic, Vm}; + +/// Installs `Function` and the methods of `Function.prototype`. +/// +/// # Errors +/// As [`crate::install`]. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + let proto = vm.intrinsic(Intrinsic::FunctionPrototype); + // §20.2.3†: `length` is 0 and `name` is "" (the prototype is an ordinary + // object here, not a function: a listed deviation). + define(vm, proto, "length", Value::int32(0), CONFIGURABLE_ONLY)?; + let empty = string(vm, "")?; + define(vm, proto, "name", empty, CONFIGURABLE_ONLY)?; + let ctor = constructor(vm, "Function", 1, function, proto)?; + method(vm, proto, "apply", 2, apply)?; + method(vm, proto, "bind", 1, bind)?; + method(vm, proto, "call", 1, call)?; + crate::util::global(vm, "Function", ctor) +} + +/// `Function ( ...parameterArgs, bodyArg )`, §20.2.1.1†: CreateDynamicFunction +/// compiles source text, which needs the compiler; `NotYet`. +fn function(_vm: &mut Vm, _this: Value, _args: &[Value]) -> Result { + Err(Abrupt::NotYet( + "the Function constructor (CreateDynamicFunction needs the compiler)".into(), + )) +} + +/// `Function.prototype.call ( thisArg, ...args )`, §20.2.3.3†: +/// 1. Let func be the this value. +/// 2. If IsCallable(func) is false, throw a TypeError exception. +/// 3. Perform PrepareForTailCall(). +/// 4. Return ? Call(func, thisArg, args). +fn call(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + if !vm.heap().is_callable(this) { + return Err(vm.type_error("Function.prototype.call called on a non-function")); + } + let rest = args.get(1..).unwrap_or(&[]); + vm.call(this, arg(args, 0), rest) +} + +/// `Function.prototype.apply ( thisArg, argArray )`, §20.2.3.1†: +/// 1. Let func be the this value. +/// 2. If IsCallable(func) is false, throw a TypeError exception. +/// 3. If argArray is either undefined or null, then +/// a. Perform PrepareForTailCall(). +/// b. Return ? Call(func, thisArg). +/// 4. Let argList be ? CreateListFromArrayLike(argArray). +/// 5. Perform PrepareForTailCall(). +/// 6. Return ? Call(func, thisArg, argList). +fn apply(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + if !vm.heap().is_callable(this) { + return Err(vm.type_error("Function.prototype.apply called on a non-function")); + } + let array = arg(args, 1); + if array == Value::UNDEFINED || array == Value::NULL { + return vm.call(this, arg(args, 0), &[]); + } + let list = create_list_from_array_like(vm, array)?; + vm.call(this, arg(args, 0), &list) +} + +/// The most elements `apply` accepts: the frame region's limit is far lower, +/// but the list is built before the call, so it is bounded here. +const MAX_APPLY_ARGS: f64 = 1_048_576.0; + +/// CreateListFromArrayLike ( obj ), §7.3.19†: +/// 2. If obj is not an Object, throw a TypeError exception. +/// 3. Let len be ? LengthOfArrayLike(obj). +/// 4 to 6. For each index from 0 to len - 1, append ? Get(obj, ToString(index)). +fn create_list_from_array_like(vm: &mut Vm, obj: Value) -> Result, Abrupt> { + if !vm.heap().is_object(obj) { + return Err(vm.type_error("CreateListFromArrayLike called on non-object")); + } + let kl = key(vm, "length")?; + let len = vm.get_value(obj, kl)?; + // LengthOfArrayLike: ToLength(? Get(obj, "length")), §7.1.20†. + let len = to_integer_or_infinity(vm, len)?.clamp(0.0, 9_007_199_254_740_991.0); + if len > MAX_APPLY_ARGS { + return Err(vm.range_error("Too many arguments in function application")); + } + let mut out = Vec::new(); + let n = len as usize; + for i in 0..n { + let k = key(vm, &i.to_string())?; + out.push(vm.get_value(obj, k)?); + } + Ok(out) +} + +/// `Function.prototype.bind ( thisArg, ...args )`, §20.2.3.2†: +/// 1. Let Target be the this value. +/// 2. If IsCallable(Target) is false, throw a TypeError exception. +/// 3. Let F be ? BoundFunctionCreate(Target, thisArg, args). +/// 4. Let L be 0. +/// 5. Let targetHasLength be ? HasOwnProperty(Target, "length"). +/// 6. If targetHasLength is true, then +/// a. Let targetLen be ? Get(Target, "length"). +/// b. If targetLen is a Number, then +/// i. If targetLen is +∞, set L to +∞. +/// ii. Else if targetLen is -∞, set L to 0. +/// iii. Else, let targetLenAsInt be ! ToIntegerOrInfinity(targetLen); +/// set L to max(targetLenAsInt - argCount, 0). +/// 7. Perform SetFunctionLength(F, L). +/// 8. Let targetName be ? Get(Target, "name"). +/// 9. If targetName is not a String, set targetName to the empty String. +/// 10. Perform SetFunctionName(F, targetName, "bound"). +/// 11. Return F. +fn bind(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + if !vm.heap().is_callable(this) { + return Err(vm.type_error("Bind must be called on a function")); + } + let bound_args: Vec = args.get(1..).unwrap_or(&[]).to_vec(); + let arg_count = bound_args.len() as f64; + let f = vm.new_bound_function(this, arg(args, 0), bound_args)?; + let mut l = 0.0_f64; + let kl = key(vm, "length")?; + if has_own_property(vm, this, kl)? { + let target_len = vm.get_value(this, kl)?; + if let Some(n) = crate::util::as_number(target_len) { + if n == f64::INFINITY { + l = f64::INFINITY; + } else if n == f64::NEG_INFINITY || n.is_nan() { + l = 0.0; + } else { + l = (n.trunc() - arg_count).max(0.0); + } + } + } + define(vm, f, "length", number(l), CONFIGURABLE_ONLY)?; + let kn = key(vm, "name")?; + let target_name = vm.get_value(this, kn)?; + let name = vm + .string_text(target_name) + .map(|s| format!("bound {s}")) + .unwrap_or_else(|| "bound ".to_string()); + let name = string(vm, &name)?; + define(vm, f, "name", name, CONFIGURABLE_ONLY)?; + Ok(f) +} diff --git a/crates/clazel-js-builtins/src/global.rs b/crates/clazel-js-builtins/src/global.rs new file mode 100644 index 000000000..bf6a5a9ba --- /dev/null +++ b/crates/clazel-js-builtins/src/global.rs @@ -0,0 +1,202 @@ +//! The function and value properties of the global object (§19†): +//! `globalThis`, `isNaN`, `isFinite`, `parseInt`, `parseFloat`. (`undefined`, +//! `NaN` and `Infinity` are made by the VM.) + +use crate::util::{METHOD, arg, define, method, number}; +use clazel_js_heap::value::Value; +use clazel_js_number::parse::is_str_whitespace; +use clazel_js_vm::interp::{Abrupt, Vm}; +use clazel_js_vm::numeric::{narrow, to_int32}; + +/// Installs the global functions. +/// +/// # Errors +/// As [`crate::install`]. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + let g = vm.global(); + // §19.1.1†: `globalThis` is writable, not enumerable, configurable. + define(vm, g, "globalThis", g, METHOD)?; + method(vm, g, "isFinite", 1, is_finite)?; + method(vm, g, "isNaN", 1, is_nan)?; + let parse_float_fn = method(vm, g, "parseFloat", 1, parse_float)?; + let parse_int_fn = method(vm, g, "parseInt", 2, parse_int)?; + crate::number::share_parse_functions(vm, parse_float_fn, parse_int_fn) +} + +/// `isNaN ( number )`, §19.2.3†: +/// 1. Let num be ? ToNumber(number). +/// 2. If num is NaN, return true. +/// 3. Otherwise, return false. +fn is_nan(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(Value::boolean(vm.to_number(arg(args, 0))?.is_nan())) +} + +/// `isFinite ( number )`, §19.2.2†: +/// 1. Let num be ? ToNumber(number). +/// 2. If num is not finite, return false. +/// 3. Otherwise, return true. +fn is_finite(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(Value::boolean(vm.to_number(arg(args, 0))?.is_finite())) +} + +/// TrimString ( string, start ), §22.1.3.32.1†, for the start only: the +/// text without its leading white space and line terminators +/// (`StrWhiteSpaceChar`). +fn trim_start(s: &str) -> &str { + s.trim_start_matches(is_str_whitespace) +} + +/// `parseFloat ( string )`, §19.2.4†: +/// 1. Let inputString be ? ToString(string). +/// 2. Let trimmedString be ! TrimString(inputString, start). +/// 3. Let trimmed be StringToCodePoints(trimmedString). +/// 4. Let trimmedPrefix be the longest prefix of trimmed that satisfies the +/// syntax of a StrDecimalLiteral, which might be trimmed itself. If there +/// is no such prefix, return NaN. +/// 5. Let parsedNumber be ParseText(trimmedPrefix, StrDecimalLiteral). +/// 6. Assert: parsedNumber is a Parse Node. +/// 7. Return StringNumericValue of parsedNumber. +fn parse_float(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let input = vm.to_js_string(arg(args, 0))?.to_string_lossy(); + let trimmed = trim_start(&input); + let Some(prefix) = decimal_literal_prefix(trimmed) else { + return Ok(Value::double(f64::NAN)); + }; + Ok(number(clazel_js_number::string_to_number(prefix))) +} + +/// The longest prefix of `s` that is a StrDecimalLiteral (§7.1.4.1†): an +/// optional sign, then `Infinity`, or decimal digits with an optional point +/// and fraction (at least one digit in all) and an optional exponent that +/// has digits. +fn decimal_literal_prefix(s: &str) -> Option<&str> { + let b = s.as_bytes(); + let mut i = 0usize; + if matches!(b.first(), Some(b'+' | b'-')) { + i = i.checked_add(1)?; + } + let rest = s.get(i..)?; + if rest.starts_with("Infinity") { + return s.get(..i.checked_add("Infinity".len())?); + } + let digits = |from: usize| -> usize { + let mut j = from; + while b.get(j).is_some_and(u8::is_ascii_digit) { + j = j.saturating_add(1); + } + j + }; + let int_end = digits(i); + let mut end = int_end; + let mut mantissa_digits = int_end.saturating_sub(i); + if b.get(end) == Some(&b'.') { + let frac_end = digits(end.saturating_add(1)); + let frac_digits = frac_end.saturating_sub(end.saturating_add(1)); + if mantissa_digits > 0 || frac_digits > 0 { + end = frac_end; + mantissa_digits = mantissa_digits.saturating_add(frac_digits); + } + } + if mantissa_digits == 0 { + return None; + } + if matches!(b.get(end), Some(b'e' | b'E')) { + let mut j = end.saturating_add(1); + if matches!(b.get(j), Some(b'+' | b'-')) { + j = j.saturating_add(1); + } + let exp_end = digits(j); + if exp_end > j { + end = exp_end; + } + } + s.get(..end) +} + +/// `parseInt ( string, radix )`, §19.2.5†: +/// 1. Let inputString be ? ToString(string). +/// 2. Let S be ! TrimString(inputString, start). +/// 3. Let sign be 1. +/// 4. If S is not empty and the first code unit of S is the code unit +/// 0x002D (HYPHEN-MINUS), set sign to -1. +/// 5. If S is not empty and the first code unit of S is either 0x002B (PLUS +/// SIGN) or 0x002D (HYPHEN-MINUS), set S to the substring of S from index 1. +/// 6. Let R be ℝ(? ToInt32(radix)). +/// 7. Let stripPrefix be true. +/// 8. If R ≠ 0, then +/// a. If R < 2 or R > 36, return NaN. +/// b. If R ≠ 16, set stripPrefix to false. +/// 9. Else, set R to 10. +/// 10. If stripPrefix is true, then +/// a. If the length of S is at least 2 and the first two code units of S +/// are either "0x" or "0X", then i. Set S to the substring of S from +/// index 2. ii. Set R to 16. +/// 11. If S contains a code unit that is not a radix-R digit, let end be the +/// index within S of the first such code unit; otherwise, let end be the +/// length of S. +/// 12. Let Z be the substring of S from 0 to end. +/// 13. If Z is empty, return NaN. +/// 14. Let mathInt be the integer value that is represented by Z in radix-R +/// notation. +/// 15. If mathInt = 0, then a. If sign = -1, return -0𝔽. b. Return +0𝔽. +/// 16. Return 𝔽(sign × mathInt). +/// +/// The integer is exact (correctly rounded to a Number) up to 2^128; beyond +/// that digits accumulate in an `f64`, which §19.2.5† permits for radixes +/// other than 2, 4, 8, 10, 16 and 32 after 20 significant digits and +/// this slice accepts for the rest. +fn parse_int(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let input = vm.to_js_string(arg(args, 0))?.to_string_lossy(); + let mut s = trim_start(&input); + let mut sign = 1.0_f64; + if s.starts_with('-') { + sign = -1.0; + } + if s.starts_with(['+', '-']) { + s = s.get(1..).unwrap_or(""); + } + let radix_arg = vm.to_number(arg(args, 1))?; + let mut r = to_int32(narrow(radix_arg)); + let mut strip_prefix = true; + if r != 0 { + if !(2..=36).contains(&r) { + return Ok(Value::double(f64::NAN)); + } + if r != 16 { + strip_prefix = false; + } + } else { + r = 10; + } + if strip_prefix && (s.starts_with("0x") || s.starts_with("0X")) { + s = s.get(2..).unwrap_or(""); + r = 16; + } + let radix = u32::try_from(r).unwrap_or(10); + let end = s + .char_indices() + .find(|(_, c)| !c.is_digit(radix)) + .map_or(s.len(), |(i, _)| i); + let z = s.get(..end).unwrap_or(""); + if z.is_empty() { + return Ok(Value::double(f64::NAN)); + } + let math_int = if radix == 10 { + // Correctly rounded for any number of digits. + clazel_js_number::string_to_number(z) + } else { + let mut exact: Option = Some(0); + let mut approx = 0.0_f64; + for c in z.chars() { + let d = c.to_digit(radix).unwrap_or(0); + exact = + exact.and_then(|v| v.checked_mul(u128::from(radix))?.checked_add(u128::from(d))); + approx = approx * f64::from(radix) + f64::from(d); + } + exact.map_or(approx, |v| v as f64) + }; + if math_int == 0.0 { + return Ok(number(if sign < 0.0 { -0.0 } else { 0.0 })); + } + Ok(number(sign * math_int)) +} diff --git a/crates/clazel-js-builtins/src/json.rs b/crates/clazel-js-builtins/src/json.rs new file mode 100644 index 000000000..8d5011e17 --- /dev/null +++ b/crates/clazel-js-builtins/src/json.rs @@ -0,0 +1,583 @@ +//! `JSON` (§25.5†): `JSON.parse` and `JSON.stringify`, over `clazel-json`. +//! +//! `JSON.parse` reads the text with `clazel-json`'s parser (ECMA-404, and the +//! parse step of §25.5.1† `JSON.parse`, proven against the grammar there); +//! this module builds the values and runs the reviver. `JSON.stringify` quotes +//! strings with `clazel-json`'s `quote` (QuoteJSONString, §25.5.2.3†) and +//! implements §25.5.2† itself. +//! +//! Listed deviations: +//! +//! - **The nesting bound.** `clazel-json` reads at most 512 levels of arrays +//! and objects; deeper text is a `SyntaxError` (phase-7.md 7.11, listed). +//! `JSON.stringify` stops at 128 levels with a `RangeError`, where the +//! standard has no bound (it recurses on the host stack, which must not +//! overflow; an unoptimised frame is several KB, and tests run on 2 MB). +//! - No `BigInt` (`toJSON` is looked up on Objects only) and no Symbols (a +//! Symbol is never a value here), so those steps of the algorithms do not +//! exist yet. +//! - The reviver's third argument (the source text context of the JSON +//! parse-with-source proposal) is not passed. + +use crate::util::{arg, js_string, method, number, to_integer_or_infinity}; +use clazel_js_heap::shape::Attrs; +use clazel_js_heap::string::{JsString, Key}; +use clazel_js_heap::value::{Decoded, Value}; +use clazel_js_vm::heap::ObjKind; +use clazel_js_vm::interp::{Abrupt, Vm}; +use clazel_json::tree::Json; + +/// The deepest nesting `JSON.stringify` serializes. +const MAX_DEPTH: usize = 128; + +/// Installs `JSON`. +/// +/// # Errors +/// As [`crate::install`]. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + let json = vm.new_object()?; + method(vm, json, "parse", 2, parse)?; + method(vm, json, "stringify", 3, stringify)?; + // §25.5.3†: `JSON[@@toStringTag]` is "JSON" (no Symbols yet, so absent). + crate::util::global(vm, "JSON", json) +} + +fn units_of(vm: &mut Vm, v: Value) -> Result, Abrupt> { + Ok(vm.to_js_string(v)?.units().collect()) +} + +fn key_of(vm: &mut Vm, units: &[u16]) -> Result { + let s = JsString::from_units(units).map_err(|_| Abrupt::OutOfMemory)?; + Ok(vm.heap_mut().intern(&s)?) +} + +fn str_value(vm: &mut Vm, units: &[u16]) -> Result { + let s = JsString::from_units(units).map_err(|_| Abrupt::OutOfMemory)?; + js_string(vm, s) +} + +// --------------------------------------------------------------------- +// JSON.parse +// --------------------------------------------------------------------- + +/// `JSON.parse ( text [ , reviver ] )`, §25.5.1†: +/// 1. Let jsonString be ? ToString(text). +/// 2. Parse StringToCodePoints(jsonString) as a JSON text as specified in +/// ECMA-404. Throw a SyntaxError exception if it is not a valid JSON text +/// as defined in that specification. +/// 3. to 5. (The script-goal evaluation of the standard's text is replaced by +/// building the value of the JSON text directly: each JSON object member +/// is CreateDataProperty on a new ordinary object, a later duplicate name +/// replacing an earlier value; an array is ArrayCreate and +/// CreateDataProperty of each element; a number is StringToNumber of its +/// text; a string is its code units.) +/// 6. Let unfiltered be the result. +/// 7. If IsCallable(reviver) is true, then a. Let root be +/// OrdinaryObjectCreate(%Object.prototype%). b. Let rootName be the empty +/// String. c. Perform ! CreateDataPropertyOrThrow(root, rootName, +/// unfiltered). d. Return ? InternalizeJSONProperty(root, rootName, +/// reviver). +/// 8. Else, a. Return unfiltered. +fn parse(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let text = units_of(vm, arg(args, 0))?; + let cps = code_points(&text); + let Some(tree) = clazel_json::parse::parse(&cps) else { + return Err(vm.syntax_error("Unexpected token in JSON, or nesting too deep")); + }; + let unfiltered = build(vm, &tree)?; + let reviver = arg(args, 1); + if vm.heap().is_callable(reviver) { + let root = vm.new_object()?; + let name = key_of(vm, &[])?; + vm.create_data_property_or_throw(root, name, unfiltered)?; + return internalize(vm, root, name, reviver); + } + Ok(unfiltered) +} + +/// StringToCodePoints (§6.1.4†): surrogate pairs join; a lone surrogate is +/// its own code point. +fn code_points(units: &[u16]) -> Vec { + let mut out = Vec::with_capacity(units.len()); + let mut i = 0usize; + while let Some(&u) = units.get(i) { + let next = units.get(i.saturating_add(1)).copied(); + match next { + Some(lo) if (0xD800..0xDC00).contains(&u) && (0xDC00..0xE000).contains(&lo) => { + let hi = u32::from(u).saturating_sub(0xD800); + let lo = u32::from(lo).saturating_sub(0xDC00); + out.push( + hi.saturating_mul(0x400) + .saturating_add(lo) + .saturating_add(0x10000), + ); + i = i.saturating_add(2); + } + _ => { + out.push(u32::from(u)); + i = i.saturating_add(1); + } + } + } + out +} + +/// The value of a parsed JSON text. +fn build(vm: &mut Vm, j: &Json) -> Result { + match j { + Json::Null => Ok(Value::NULL), + Json::Bool(b) => Ok(Value::boolean(*b)), + Json::Number(text) => { + let s: String = text.iter().filter_map(|c| char::from_u32(*c)).collect(); + Ok(number(clazel_js_number::string_to_number(&s))) + } + Json::String(u) => str_value(vm, u), + Json::Array(items) => { + let a = vm.array_create(0.0, None)?; + for (i, item) in items.iter().enumerate() { + let v = build(vm, item)?; + let k = vm.index_key(i as u64)?; + vm.create_data_property_or_throw(a, k, v)?; + } + Ok(a) + } + Json::Object(members) => { + let o = vm.new_object()?; + for m in members { + let v = build(vm, &m.value)?; + let k = key_of(vm, &m.key)?; + vm.create_data_property_or_throw(o, k, v)?; + } + Ok(o) + } + } +} + +/// EnumerableOwnProperties ( O, key ), §7.3.23†, as a list of keys. +fn enumerable_keys(vm: &Vm, o: Value) -> Vec { + vm.heap() + .own_properties(o) + .into_iter() + .filter(|(_, a, _)| a.0 & Attrs::ENUMERABLE != 0) + .map(|(k, _, _)| k) + .collect() +} + +/// InternalizeJSONProperty ( holder, name, reviver ), §25.5.1.1†: +/// 1. Let val be ? Get(holder, name). +/// 2. If val is an Object, then +/// a. Let isArray be ? IsArray(val). +/// b. If isArray is true, then i. Let len be ? LengthOfArrayLike(val). +/// ii. Let I be 0. iii. Repeat, while I < len, 1. Let prop be ! +/// ToString(𝔽(I)). 2. Let newElement be ? InternalizeJSONProperty(val, +/// prop, reviver). 3. If newElement is undefined, then a. Perform ? +/// val.[[Delete]](prop). 4. Else, a. Perform ? CreateDataProperty(val, +/// prop, newElement). 5. Set I to I + 1. +/// c. Else, i. Let keys be ? EnumerableOwnProperties(val, key). ii. For +/// each String P of keys, do 1. Let newElement be ? +/// InternalizeJSONProperty(val, P, reviver). 2. If newElement is +/// undefined, then a. Perform ? val.[[Delete]](P). 3. Else, a. Perform ? +/// CreateDataProperty(val, P, newElement). +/// 3. Return ? Call(reviver, holder, « name, val »). +fn internalize(vm: &mut Vm, holder: Value, name: Key, reviver: Value) -> Result { + vm.charge_step()?; + let val = vm.get_value(holder, name)?; + if vm.heap().is_object(val) { + let keys: Vec = if vm.is_array(val) { + let lk = key_of(vm, &"length".encode_utf16().collect::>())?; + let lv = vm.get_value(val, lk)?; + let len = to_integer_or_infinity(vm, lv)?.clamp(0.0, 9_007_199_254_740_991.0); + let mut ks = Vec::new(); + let mut i = 0.0f64; + while i < len { + vm.charge_step()?; + ks.push(vm.index_key(i as u64)?); + i += 1.0; + } + ks + } else { + enumerable_keys(vm, val) + }; + for p in keys { + let new_element = internalize(vm, val, p, reviver)?; + if new_element == Value::UNDEFINED { + vm.delete_property(val, p)?; + } else { + vm.create_data_property(val, p, new_element)?; + } + } + } + let name_v = { + let s = vm + .heap() + .key_string(name) + .cloned() + .ok_or(Abrupt::Internal("key"))?; + js_string(vm, s)? + }; + vm.call(reviver, holder, &[name_v, val]) +} + +// --------------------------------------------------------------------- +// JSON.stringify +// --------------------------------------------------------------------- + +/// The state of a stringify (§25.5.2†): the ReplacerFunction, the +/// PropertyList, the gap, the indent and the stack. +struct State { + replacer: Value, + property_list: Option>, + gap: Vec, + indent: Vec, + stack: Vec, +} + +/// The primitive inside a Boolean, Number or String wrapper object. +fn wrapped(vm: &Vm, v: Value) -> Option { + match vm.heap().obj(v).map(|o| o.kind) { + Some(ObjKind::Primitive(p)) => Some(p), + _ => None, + } +} + +/// `JSON.stringify ( value [ , replacer [ , space ] ] )`, §25.5.2†: +/// 1. Let stack be a new empty List. +/// 2. Let indent be the empty String. +/// 3. Let PropertyList be undefined. +/// 4. Let ReplacerFunction be undefined. +/// 5. If replacer is an Object, then +/// a. If IsCallable(replacer) is true, then i. Set ReplacerFunction to +/// replacer. +/// b. Else, i. Let isArray be ? IsArray(replacer). ii. If isArray is true, +/// then 1. Set PropertyList to a new empty List. 2. Let len be ? +/// LengthOfArrayLike(replacer). 3. Let k be 0. 4. Repeat, while k < len, a. +/// Let prop be ! ToString(𝔽(k)). b. Let v be ? Get(replacer, prop). c. Let +/// item be undefined. d. If v is a String, then i. Set item to v. e. Else +/// if v is a Number, then i. Set item to ! ToString(v). f. Else if v is an +/// Object, then i. If v has a [[StringData]] or [[NumberData]] internal +/// slot, set item to ? ToString(v). g. If item is not undefined and +/// PropertyList does not contain item, then i. Append item to +/// PropertyList. h. Set k to k + 1. +/// 6. If space is an Object, then a. If space has a [[NumberData]] internal +/// slot, then i. Set space to ? ToNumber(space). b. Else if space has a +/// [[StringData]] internal slot, then i. Set space to ? ToString(space). +/// 7. If space is a Number, then a. Let spaceMV be ! ToIntegerOrInfinity(space). +/// b. Set spaceMV to min(10, spaceMV). c. If spaceMV < 1, let gap be the +/// empty String; otherwise let gap be the String value containing spaceMV +/// occurrences of the code unit 0x0020 (SPACE). +/// 8. Else if space is a String, then a. If the length of space is 10 or less, +/// let gap be space; otherwise let gap be the substring of space from 0 to +/// 10. +/// 9. Else, a. Let gap be the empty String. +/// 10. Let wrapper be OrdinaryObjectCreate(%Object.prototype%). +/// 11. Perform ! CreateDataPropertyOrThrow(wrapper, the empty String, value). +/// 12. Let state be the JSON Serialization Record { [[ReplacerFunction]], +/// [[Stack]], [[Indent]], [[Gap]], [[PropertyList]] }. +/// 13. Return ? SerializeJSONProperty(state, the empty String, wrapper). +fn stringify(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let (value, replacer, mut space) = (arg(args, 0), arg(args, 1), arg(args, 2)); + let mut state = State { + replacer: Value::UNDEFINED, + property_list: None, + gap: Vec::new(), + indent: Vec::new(), + stack: Vec::new(), + }; + if vm.heap().is_object(replacer) { + if vm.heap().is_callable(replacer) { + state.replacer = replacer; + } else if vm.is_array(replacer) { + let lk = key_of(vm, &"length".encode_utf16().collect::>())?; + let lv = vm.get_value(replacer, lk)?; + let len = to_integer_or_infinity(vm, lv)?.clamp(0.0, 9_007_199_254_740_991.0); + let mut list: Vec = Vec::new(); + let mut k = 0.0f64; + while k < len { + vm.charge_step()?; + let pk = vm.index_key(k as u64)?; + let v = vm.get_value(replacer, pk)?; + let item = if vm.heap().is_string(v) || crate::util::as_number(v).is_some() { + Some(units_of(vm, v)?) + } else if let Some(p) = wrapped(vm, v) + && !matches!(p.decode(), Decoded::Bool(_)) + { + Some(units_of(vm, v)?) + } else { + None + }; + if let Some(u) = item { + let key = key_of(vm, &u)?; + if !list.contains(&key) { + list.push(key); + } + } + k += 1.0; + } + state.property_list = Some(list); + } + } + if let Some(p) = wrapped(vm, space) { + if crate::util::as_number(p).is_some() { + let n = vm.to_number(space)?; + space = number(n); + } else if vm.heap().is_string(p) { + let s = vm.to_js_string(space)?; + space = js_string(vm, s)?; + } + } + if let Some(n) = crate::util::as_number(space) { + let mv = if n.is_nan() { 0.0 } else { n.trunc() }.min(10.0); + let count = if mv < 1.0 { 0 } else { mv as usize }; + state.gap = vec![0x20; count]; + } else if vm.heap().is_string(space) { + state.gap = units_of(vm, space)?.into_iter().take(10).collect(); + } + let wrapper = vm.new_object()?; + let empty = key_of(vm, &[])?; + vm.create_data_property_or_throw(wrapper, empty, value)?; + match serialize_property(vm, &mut state, empty, wrapper)? { + Some(units) => str_value(vm, &units), + None => Ok(Value::UNDEFINED), + } +} + +/// SerializeJSONProperty ( state, key, holder ), §25.5.2.2†: the text, or +/// `None` for undefined. +/// 1. Let value be ? Get(holder, key). +/// 2. If value is an Object, then a. Let toJSON be ? GetV(value, "toJSON"). +/// b. If IsCallable(toJSON) is true, then i. Set value to ? Call(toJSON, +/// value, « key »). +/// 3. If state.[[ReplacerFunction]] is not undefined, then a. Set value to ? +/// Call(state.[[ReplacerFunction]], holder, « key, value »). +/// 4. If value is an Object, then a. If value has a [[NumberData]] internal +/// slot, then i. Set value to ? ToNumber(value). b. Else if value has a +/// [[StringData]] internal slot, then i. Set value to ? ToString(value). +/// c. Else if value has a [[BooleanData]] internal slot, then i. Set value +/// to value.[[BooleanData]]. +/// 5. If value is null, return "null". +/// 6. If value is true, return "true". +/// 7. If value is false, return "false". +/// 8. If value is a String, return QuoteJSONString(value). +/// 9. If value is a Number, then a. If value is finite, return ! +/// ToString(value). b. Return "null". +/// 11. If value is an Object and IsCallable(value) is false, then a. Let +/// isArray be ? IsArray(value). b. If isArray is true, return ? +/// SerializeJSONArray(state, value). c. Return ? SerializeJSONObject(state, +/// value). +/// 12. Return undefined. +fn serialize_property( + vm: &mut Vm, + state: &mut State, + key: Key, + holder: Value, +) -> Result>, Abrupt> { + vm.charge_step()?; + let mut value = vm.get_value(holder, key)?; + let key_v = { + let s = vm + .heap() + .key_string(key) + .cloned() + .ok_or(Abrupt::Internal("key"))?; + js_string(vm, s)? + }; + if vm.heap().is_object(value) { + let to_json = vm.get_named(value, "toJSON")?; + if vm.heap().is_callable(to_json) { + value = vm.call(to_json, value, &[key_v])?; + } + } + if state.replacer != Value::UNDEFINED { + value = vm.call(state.replacer, holder, &[key_v, value])?; + } + if let Some(p) = wrapped(vm, value) { + value = match p.decode() { + Decoded::Bool(_) => p, + Decoded::Int32(_) | Decoded::Double(_) => number(vm.to_number(value)?), + _ => { + let s = vm.to_js_string(value)?; + js_string(vm, s)? + } + }; + } + if value == Value::NULL { + return Ok(Some("null".encode_utf16().collect())); + } + if value == Value::TRUE { + return Ok(Some("true".encode_utf16().collect())); + } + if value == Value::FALSE { + return Ok(Some("false".encode_utf16().collect())); + } + if let Some(s) = vm.heap().string(value) { + let units: Vec = s.units().collect(); + return Ok(Some(clazel_json::quote::quote(&units))); + } + if let Some(n) = crate::util::as_number(value) { + return Ok(Some(if n.is_finite() { + vm.to_js_string(value)?.units().collect() + } else { + "null".encode_utf16().collect() + })); + } + if vm.heap().is_object(value) && !vm.heap().is_callable(value) { + return if vm.is_array(value) { + serialize_array(vm, state, value).map(Some) + } else { + serialize_object(vm, state, value).map(Some) + }; + } + Ok(None) +} + +/// Pushes `value` on the stack, TypeError for a cycle (§25.5.2.4† step 1, +/// §25.5.2.5† step 1), RangeError past [`MAX_DEPTH`] (listed deviation). +fn enter(vm: &mut Vm, state: &mut State, value: Value) -> Result<(), Abrupt> { + if state.stack.contains(&value) { + return Err(vm.type_error("Converting circular structure to JSON")); + } + if state.stack.len() >= MAX_DEPTH { + return Err(vm.range_error("JSON.stringify: nesting too deep")); + } + state.stack.push(value); + Ok(()) +} + +/// SerializeJSONObject ( state, value ), §25.5.2.4†: +/// 1. If state.[[Stack]] contains value, throw a TypeError exception because +/// the structure is cyclical. +/// 2. Append value to state.[[Stack]]. +/// 3. Let stepback be state.[[Indent]]. +/// 4. Set state.[[Indent]] to the string-concatenation of state.[[Indent]] and +/// state.[[Gap]]. +/// 5. If state.[[PropertyList]] is not undefined, then a. Let K be +/// state.[[PropertyList]]. +/// 6. Else, a. Let K be ? EnumerableOwnProperties(value, key). +/// 7. Let partial be a new empty List. +/// 8. For each element P of K, do a. Let strP be ? SerializeJSONProperty(state, +/// P, value). b. If strP is not undefined, then i. Let member be +/// QuoteJSONString(P). ii. Set member to the string-concatenation of member +/// and ":". iii. If state.[[Gap]] is not the empty String, then 1. Set +/// member to the string-concatenation of member and the code unit 0x0020 +/// (SPACE). iv. Set member to the string-concatenation of member and strP. +/// v. Append member to partial. +/// 9. If partial is empty, then a. Let final be "{}". +/// 10. Else, a. If state.[[Gap]] is the empty String, then i. Let properties +/// be the String value formed by concatenating all the element Strings of +/// partial with each adjacent pair of Strings separated with the code unit +/// 0x002C (COMMA). ii. Let final be the string-concatenation of "{", +/// properties, and "}". b. Else, i. Let separator be the string-concatenation +/// of the code unit 0x002C (COMMA), the code unit 0x000A (LINE FEED), and +/// state.[[Indent]]. ii. Let properties be the String value formed by +/// concatenating all the element Strings of partial with each adjacent pair +/// separated with separator. iii. Let final be the string-concatenation of +/// "{", the code unit 0x000A (LINE FEED), state.[[Indent]], properties, the +/// code unit 0x000A (LINE FEED), stepback, and "}". +/// 11. Remove the last element of state.[[Stack]]. +/// 12. Set state.[[Indent]] to stepback. +/// 13. Return final. +fn serialize_object(vm: &mut Vm, state: &mut State, value: Value) -> Result, Abrupt> { + enter(vm, state, value)?; + let stepback = state.indent.clone(); + state.indent.extend_from_slice(&state.gap.clone()); + let keys: Vec = match &state.property_list { + Some(l) => l.clone(), + None => enumerable_keys(vm, value), + }; + let mut partial: Vec> = Vec::new(); + for p in keys { + if let Some(str_p) = serialize_property(vm, state, p, value)? { + let name: Vec = vm + .heap() + .key_string(p) + .map(|s| s.units().collect()) + .ok_or(Abrupt::Internal("key"))?; + let mut member = clazel_json::quote::quote(&name); + member.push(u16::from(b':')); + if !state.gap.is_empty() { + member.push(0x20); + } + member.extend(str_p); + partial.push(member); + } + } + let out = wrap(&partial, state, &stepback, b'{', b'}'); + state.stack.pop(); + state.indent = stepback; + Ok(out) +} + +/// Steps 9 and 10 of both serializers: `open`, the members and `close`. +fn wrap(partial: &[Vec], state: &State, stepback: &[u16], open: u8, close: u8) -> Vec { + let (open, close) = (u16::from(open), u16::from(close)); + if partial.is_empty() { + return vec![open, close]; + } + let mut out = vec![open]; + let sep: Vec = if state.gap.is_empty() { + vec![u16::from(b',')] + } else { + let mut s = vec![u16::from(b','), 0x0A]; + s.extend_from_slice(&state.indent); + s + }; + if !state.gap.is_empty() { + out.push(0x0A); + out.extend_from_slice(&state.indent); + } + for (i, m) in partial.iter().enumerate() { + if i > 0 { + out.extend_from_slice(&sep); + } + out.extend_from_slice(m); + } + if !state.gap.is_empty() { + out.push(0x0A); + out.extend_from_slice(stepback); + } + out.push(close); + out +} + +/// SerializeJSONArray ( state, value ), §25.5.2.6†: +/// 1. If state.[[Stack]] contains value, throw a TypeError exception because +/// the structure is cyclical. +/// 2. Append value to state.[[Stack]]. +/// 3. Let stepback be state.[[Indent]]. +/// 4. Set state.[[Indent]] to the string-concatenation of state.[[Indent]] and +/// state.[[Gap]]. +/// 5. Let partial be a new empty List. +/// 6. Let len be ? LengthOfArrayLike(value). +/// 7. Let index be 0. +/// 8. Repeat, while index < len, a. Let strP be ? SerializeJSONProperty(state, +/// ! ToString(𝔽(index)), value). b. If strP is undefined, then i. Append +/// "null" to partial. c. Else, i. Append strP to partial. d. Set index to +/// index + 1. +/// 9. If partial is empty, then a. Let final be "[]". +/// 10. Else, as step 10 of SerializeJSONObject with "[" and "]" and the +/// elements without names. +/// 11. Remove the last element of state.[[Stack]]. +/// 12. Set state.[[Indent]] to stepback. +/// 13. Return final. +fn serialize_array(vm: &mut Vm, state: &mut State, value: Value) -> Result, Abrupt> { + enter(vm, state, value)?; + let stepback = state.indent.clone(); + state.indent.extend_from_slice(&state.gap.clone()); + let lk = key_of(vm, &"length".encode_utf16().collect::>())?; + let lv = vm.get_value(value, lk)?; + let len = to_integer_or_infinity(vm, lv)?.clamp(0.0, 9_007_199_254_740_991.0); + let mut partial: Vec> = Vec::new(); + let mut index = 0.0f64; + while index < len { + let k = vm.index_key(index as u64)?; + match serialize_property(vm, state, k, value)? { + Some(s) => partial.push(s), + None => partial.push("null".encode_utf16().collect()), + } + index += 1.0; + } + let out = wrap(&partial, state, &stepback, b'[', b']'); + state.stack.pop(); + state.indent = stepback; + Ok(out) +} diff --git a/crates/clazel-js-builtins/src/lib.rs b/crates/clazel-js-builtins/src/lib.rs new file mode 100644 index 000000000..2b2a5deba --- /dev/null +++ b/crates/clazel-js-builtins/src/lib.rs @@ -0,0 +1,89 @@ +//! The global object's built-ins (phase-7.md item 7.9, first group). +//! +//! [`install`] adds to a [`Vm`]'s global object, and to its intrinsics table +//! ([`clazel_js_vm::interp::Intrinsic`]), these built-ins of ECMA-262: +//! +//! - [`object`]: `Object` (§20.1†), `Object.prototype.toString`, +//! `hasOwnProperty`, `valueOf`, and `Object.keys`, `defineProperty`, +//! `getPrototypeOf`, `create`; +//! - [`function`]: `Function` (§20.2†, whose constructor needs the compiler +//! and so throws `NotYet`) and `Function.prototype.call`, `apply`, `bind`; +//! - [`error`]: `Error` and the `TypeError`, `RangeError`, `SyntaxError` and +//! `ReferenceError` native errors (§20.5†), which the VM's own throws use; +//! - [`boolean`]: `Boolean` (§20.3†); +//! - [`number`]: `Number` (§21.1†) with `toString` and `toFixed` over +//! `clazel-js-number`; +//! - [`string`]: `String` (§22.1†) with `charAt`, `charCodeAt`, `indexOf`, +//! `slice`, `substring`; +//! - [`array`]: `Array` (§23.1†), `Array.isArray`, `Array.of` and the core of +//! `Array.prototype` (`push`, `pop`, `shift`, `unshift`, `slice`, `splice`, +//! `concat`, `join`, `toString`, `indexOf`, `lastIndexOf`, `includes`, +//! `reverse`, `fill`, `forEach`, `map`, `filter`, `some`, `every`, `reduce`, +//! `reduceRight`, `find`, `findIndex`, `sort`); +//! - [`json`]: `JSON.parse` and `JSON.stringify` (§25.5†) over `clazel-json`; +//! - [`math`]: the pure functions of `Math` (§21.3†); +//! - [`global`]: `globalThis`, `isNaN`, `isFinite`, `parseInt`, `parseFloat` +//! (§19†). +//! +//! # What this slice does not have, all listed +//! +//! - **Array iteration is missing**: `Array.from`, `entries`, `keys`, +//! `values` and `@@iterator` wait for Symbols and iterators; so does array +//! spread (`GetIterator` is `NotYet`). `@@species` and +//! `@@isConcatSpreadable` are not consulted: `ArraySpeciesCreate` uses the +//! receiver's `constructor` itself. Not yet: `at`, `copyWithin`, `flat`, +//! `flatMap`, `findLast`, `findLastIndex`, `toLocaleString`, the change-by- +//! copy methods. Array elements are ordinary shape properties (the heap's +//! elements are an open pull request), so a huge sparse index or a long +//! `shift` is slow, not wrong. +//! - **No Symbols**, so `@@toPrimitive`, `@@toStringTag` and `@@hasInstance` +//! are not consulted; `Object.prototype.toString` tags by internal slot +//! only. +//! - **Data properties only**: no accessors, so a descriptor with `get` or +//! `set` is `NotYet`; objects are always extensible. +//! - **`Function.prototype` is an ordinary object**, not a function (a +//! `typeof Function.prototype` of `"object"`). +//! - **A `String` wrapper object has no index properties**, only `length` +//! (a String exotic object's `[[GetOwnProperty]]`, §10.4.3.1†, is a later +//! slice). +//! - **`Number.prototype.toString` with a radix other than 10** gives the +//! exact digits of the integer part and up to 52 binary places of the +//! fraction; §21.1.3.6† leaves other radixes implementation-approximated. +//! +//! ECMA-262 is cited as `§n†`, the numbering of the pinned edition; each +//! built-in's comment quotes the algorithm's steps it covers. +//! +//! Tier: see `[package.metadata.clazel]` in `Cargo.toml` and +//! `docs/VERIFICATION.md`. + +pub mod array; +pub mod boolean; +pub mod error; +pub mod function; +pub mod global; +pub mod json; +pub mod math; +pub mod number; +pub mod object; +pub mod string; +pub mod util; + +use clazel_js_vm::interp::{Abrupt, Vm}; + +/// Installs the built-ins of this crate in the realm of `vm`. +/// +/// # Errors +/// [`Abrupt::OutOfMemory`] if the heap is too small, or the error of a +/// heap operation that fails. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + object::install(vm)?; + function::install(vm)?; + array::install(vm)?; + error::install(vm)?; + boolean::install(vm)?; + number::install(vm)?; + string::install(vm)?; + math::install(vm)?; + json::install(vm)?; + global::install(vm) +} diff --git a/crates/clazel-js-builtins/src/math.rs b/crates/clazel-js-builtins/src/math.rs new file mode 100644 index 000000000..25677951c --- /dev/null +++ b/crates/clazel-js-builtins/src/math.rs @@ -0,0 +1,183 @@ +//! `Math` (§21.3†): the constants and the pure functions `abs`, `floor`, +//! `ceil`, `round`, `trunc`, `sign`, `min`, `max`, `pow`, `sqrt`. +//! +//! Each result is exact or the correctly rounded value, as Rust's `f64` +//! methods are: `floor`, `ceil`, `trunc` and `abs` are exact, and `sqrt` is +//! the IEEE 754 correctly rounded square root. `pow` is +//! `Number::exponentiate` (§6.1.6.1.3†) as the VM's `numeric` module has +//! it, over the C library's `pow`, which is not specified to be correctly +//! rounded (§21.3†: "the choice of algorithms is implementation-approximated" +//! for these). + +use crate::util::{CONSTANT, arg, define, method, new_plain, number}; +use clazel_js_heap::value::Value; +use clazel_js_vm::interp::{Abrupt, Vm}; +use clazel_js_vm::numeric::exponentiate; + +/// Installs `Math`. +/// +/// # Errors +/// As [`crate::install`]. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + let math = new_plain(vm)?; + // §21.3.1†: value properties, not writable, enumerable or configurable. + for (name, v) in [ + ("E", std::f64::consts::E), + ("LN10", std::f64::consts::LN_10), + ("LN2", std::f64::consts::LN_2), + ("LOG10E", std::f64::consts::LOG10_E), + ("LOG2E", std::f64::consts::LOG2_E), + ("PI", std::f64::consts::PI), + ("SQRT1_2", std::f64::consts::FRAC_1_SQRT_2), + ("SQRT2", std::f64::consts::SQRT_2), + ] { + define(vm, math, name, number(v), CONSTANT)?; + } + method(vm, math, "abs", 1, abs)?; + method(vm, math, "ceil", 1, ceil)?; + method(vm, math, "floor", 1, floor)?; + method(vm, math, "max", 2, max)?; + method(vm, math, "min", 2, min)?; + method(vm, math, "pow", 2, pow)?; + method(vm, math, "round", 1, round)?; + method(vm, math, "sign", 1, sign)?; + method(vm, math, "sqrt", 1, sqrt)?; + method(vm, math, "trunc", 1, trunc)?; + crate::util::global(vm, "Math", math) +} + +/// The first argument as a Number: `Let n be ? ToNumber(x)`. +fn arg_number(vm: &mut Vm, args: &[Value]) -> Result { + vm.to_number(arg(args, 0)) +} + +/// `Math.abs ( x )`, §21.3.2.1†: the magnitude of n; `abs(-0)` is `+0`. +fn abs(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(number(arg_number(vm, args)?.abs())) +} + +/// `Math.ceil ( x )`, §21.3.2.10†: the smallest integral Number not less +/// than n; NaN, the infinities and the zeros are themselves. +fn ceil(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(number(arg_number(vm, args)?.ceil())) +} + +/// `Math.floor ( x )`, §21.3.2.16†: the greatest integral Number not greater +/// than n. +fn floor(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(number(arg_number(vm, args)?.floor())) +} + +/// `Math.trunc ( x )`, §21.3.2.35†: the integral part, toward zero. +fn trunc(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(number(arg_number(vm, args)?.trunc())) +} + +/// `Math.sqrt ( x )`, §21.3.2.32†: NaN for NaN and a negative n, `-0` for +/// `-0`, else the square root. +fn sqrt(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(number(arg_number(vm, args)?.sqrt())) +} + +/// `Math.sign ( x )`, §21.3.2.29†: +/// 3. If n is one of NaN, +0𝔽, or -0𝔽, return n. +/// 4. If n < -0𝔽, return -1𝔽. +/// 5. Return 1𝔽. +fn sign(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let n = arg_number(vm, args)?; + Ok(number(if n.is_nan() || n == 0.0 { + n + } else if n < 0.0 { + -1.0 + } else { + 1.0 + })) +} + +/// `Math.round ( x )`, §21.3.2.28†: +/// 3. If n is not finite or n is an integral Number, return n. +/// 4. If n < 0.5𝔽 and n > +0𝔽, return +0𝔽. +/// 5. If n < -0𝔽 and n ≥ -0.5𝔽, return -0𝔽. +/// 6. Return the integral Number closest to n, preferring the one closer to +/// +∞ in the case of a tie. +/// +/// `n - floor(n)` is exact for a Number that is not integral, so the tie +/// test is exact (`floor(n + 0.5)` is not: it rounds `0.49999999999999994` +/// up). +fn round(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let n = arg_number(vm, args)?; + if !n.is_finite() || n.trunc() == n { + return Ok(number(n)); + } + let r = n.floor(); + let rounded = if n - r >= 0.5 { r + 1.0 } else { r }; + // A negative Number that rounds to zero is -0 (step 5). + Ok(number(if rounded == 0.0 && n < 0.0 { + -0.0 + } else { + rounded + })) +} + +/// `Math.pow ( base, exponent )`, §21.3.2.26†: +/// 1. Set base to ? ToNumber(base). +/// 2. Set exponent to ? ToNumber(exponent). +/// 3. Return Number::exponentiate(base, exponent). +fn pow(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let base = vm.to_number(arg(args, 0))?; + let exponent = vm.to_number(arg(args, 1))?; + Ok(number(exponentiate(base, exponent))) +} + +/// `Math.max ( ...args )`, §21.3.2.24†: +/// 1. Let coerced be a new empty List. +/// 2. For each element arg of args, do +/// a. Let n be ? ToNumber(arg). +/// b. Append n to coerced. +/// 3. Let highest be -∞𝔽. +/// 4. For each element number of coerced, do +/// a. If number is NaN, return NaN. +/// b. If number is +0𝔽 and highest is -0𝔽, set highest to +0𝔽. +/// c. If number > highest, set highest to number. +/// 5. Return highest. +fn max(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let mut coerced = Vec::with_capacity(args.len()); + for a in args { + coerced.push(vm.to_number(*a)?); + } + let mut highest = f64::NEG_INFINITY; + for n in coerced { + if n.is_nan() { + return Ok(Value::double(f64::NAN)); + } + if n == 0.0 && n.is_sign_positive() && highest == 0.0 && highest.is_sign_negative() { + highest = 0.0; + } + if n > highest { + highest = n; + } + } + Ok(number(highest)) +} + +/// `Math.min ( ...args )`, §21.3.2.25†: as `max`, with `+∞` to start, `-0` +/// preferred over `+0`, and the lesser Number kept. +fn min(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let mut coerced = Vec::with_capacity(args.len()); + for a in args { + coerced.push(vm.to_number(*a)?); + } + let mut lowest = f64::INFINITY; + for n in coerced { + if n.is_nan() { + return Ok(Value::double(f64::NAN)); + } + if n == 0.0 && n.is_sign_negative() && lowest == 0.0 { + lowest = -0.0; + } + if n < lowest { + lowest = n; + } + } + Ok(number(lowest)) +} diff --git a/crates/clazel-js-builtins/src/number.rs b/crates/clazel-js-builtins/src/number.rs new file mode 100644 index 000000000..653baae22 --- /dev/null +++ b/crates/clazel-js-builtins/src/number.rs @@ -0,0 +1,235 @@ +//! `Number` (§21.1†): the constructor, its constants and predicates, and +//! `Number.prototype.toString`, `toFixed`, `valueOf`. + +use crate::util::{ + CONSTANT, arg, as_number, constructor, define, method, number, string, to_integer_or_infinity, +}; +use clazel_js_heap::value::Value; +use clazel_js_vm::heap::ObjKind; +use clazel_js_vm::interp::{Abrupt, Intrinsic, Vm}; + +/// Installs `Number` and `Number.prototype`. +/// +/// # Errors +/// As [`crate::install`]. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + // §21.1.4†: the prototype is itself a Number object whose [[NumberData]] + // is +0. + let object_proto = vm.intrinsic(Intrinsic::ObjectPrototype); + let proto = vm.new_wrapper(Value::int32(0), object_proto)?; + vm.set_intrinsic(Intrinsic::NumberPrototype, proto); + let ctor = constructor(vm, "Number", 1, number_constructor, proto)?; + // §21.1.2† value properties: not writable, not enumerable, not + // configurable. + for (name, v) in [ + ("EPSILON", f64::EPSILON), + ("MAX_SAFE_INTEGER", 9_007_199_254_740_991.0), + ("MAX_VALUE", f64::MAX), + ("MIN_SAFE_INTEGER", -9_007_199_254_740_991.0), + // 5e-324, the smallest positive subnormal. + ("MIN_VALUE", f64::from_bits(1)), + ("NaN", f64::NAN), + ("NEGATIVE_INFINITY", f64::NEG_INFINITY), + ("POSITIVE_INFINITY", f64::INFINITY), + ] { + define(vm, ctor, name, number(v), CONSTANT)?; + } + method(vm, ctor, "isFinite", 1, is_finite)?; + method(vm, ctor, "isInteger", 1, is_integer)?; + method(vm, ctor, "isNaN", 1, is_nan)?; + method(vm, ctor, "isSafeInteger", 1, is_safe_integer)?; + method(vm, proto, "toFixed", 1, to_fixed)?; + method(vm, proto, "toString", 1, to_string)?; + method(vm, proto, "valueOf", 0, value_of)?; + crate::util::global(vm, "Number", ctor) +} + +/// Makes `Number.parseFloat` and `Number.parseInt` the same function +/// objects as the globals (§21.1.2.12†, §21.1.2.13†); [`crate::global`] calls +/// it once those exist. +/// +/// # Errors +/// As [`crate::install`]. +pub fn share_parse_functions( + vm: &mut Vm, + parse_float: Value, + parse_int: Value, +) -> Result<(), Abrupt> { + let g = vm.global(); + let kn = crate::util::key(vm, "Number")?; + let ctor = vm.get_value(g, kn)?; + crate::util::define(vm, ctor, "parseFloat", parse_float, crate::util::METHOD)?; + crate::util::define(vm, ctor, "parseInt", parse_int, crate::util::METHOD) +} + +/// `Number ( value )`, §21.1.1.1†: +/// 1. If value is present, then +/// a. Let prim be ? ToNumeric(value). +/// b. If prim is a BigInt, let n be 𝔽(ℝ(prim)). (No BigInt yet.) +/// c. Otherwise, let n be prim. +/// 2. Else, let n be +0𝔽. +/// 3. If NewTarget is undefined, return n. +/// 4. Let O be ? OrdinaryCreateFromConstructor(NewTarget, +/// "%Number.prototype%", « [[NumberData]] »). +/// 5. Set O.[[NumberData]] to n. +/// 6. Return O. +fn number_constructor(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let n = match args.first() { + Some(v) => number(vm.to_number(*v)?), + None => Value::int32(0), + }; + let nt = vm.new_target(); + if nt == Value::UNDEFINED { + return Ok(n); + } + vm.create_from_constructor(nt, Intrinsic::NumberPrototype, ObjKind::Primitive(n)) +} + +/// `Number.isFinite ( number )`, §21.1.2.2†: a Number that is neither NaN nor +/// an infinity. +fn is_finite(_vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(Value::boolean( + as_number(arg(args, 0)).is_some_and(f64::is_finite), + )) +} + +/// `Number.isInteger ( number )`, §21.1.2.3†: IsIntegralNumber, §7.2.6†: a +/// finite Number equal to its truncation. +fn is_integer(_vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(Value::boolean( + as_number(arg(args, 0)).is_some_and(|n| n.is_finite() && n.trunc() == n), + )) +} + +/// `Number.isNaN ( number )`, §21.1.2.4†: a Number that is NaN. +fn is_nan(_vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(Value::boolean( + as_number(arg(args, 0)).is_some_and(f64::is_nan), + )) +} + +/// `Number.isSafeInteger ( number )`, §21.1.2.5†: an integral Number whose +/// magnitude is at most 2^53 - 1. +fn is_safe_integer(_vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + Ok(Value::boolean(as_number(arg(args, 0)).is_some_and(|n| { + n.is_finite() && n.trunc() == n && n.abs() <= 9_007_199_254_740_991.0 + }))) +} + +/// thisNumberValue ( value ), §21.1.3.7.1†: +/// 1. If value is a Number, return value. +/// 2. If value is an Object and value has a [[NumberData]] internal slot, +/// then return value.[[NumberData]]. +/// 3. Throw a TypeError exception. +fn this_number_value(vm: &mut Vm, v: Value) -> Result { + if let Some(n) = as_number(v) { + return Ok(n); + } + if let Some(ObjKind::Primitive(p)) = vm.heap().obj(v).map(|o| o.kind) + && let Some(n) = as_number(p) + { + return Ok(n); + } + Err(vm.type_error("not a Number")) +} + +/// `Number.prototype.valueOf ( )`, §21.1.3.7†: +/// 1. Return ? thisNumberValue(this value). +fn value_of(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + Ok(number(this_number_value(vm, this)?)) +} + +/// `Number.prototype.toFixed ( fractionDigits )`, §21.1.3.3†: +/// 1. Let x be ? thisNumberValue(this value). +/// 2. Let f be ? ToIntegerOrInfinity(fractionDigits). +/// 3. Assert: If fractionDigits is undefined, then f is 0. +/// 4. If f is not finite, throw a RangeError exception. +/// 5. If f < 0 or f > 100, throw a RangeError exception. +/// 6. If x is not finite, return Number::toString(x, 10). +/// - Steps 7 to 13, the digits: `clazel-js-number`'s `to_fixed`. +fn to_fixed(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let x = this_number_value(vm, this)?; + let f = to_integer_or_infinity(vm, arg(args, 0))?; + if !f.is_finite() || !(0.0..=100.0).contains(&f) { + return Err(vm.range_error("toFixed() digits argument must be between 0 and 100")); + } + if !x.is_finite() { + return string(vm, &clazel_js_number::number_to_string(x)); + } + match clazel_js_number::to_fixed(x, f as i64) { + Ok(s) => string(vm, &s), + Err(_) => Err(vm.range_error("toFixed() digits argument must be between 0 and 100")), + } +} + +/// `Number.prototype.toString ( [ radix ] )`, §21.1.3.6†: +/// 1. Let x be ? thisNumberValue(this value). +/// 2. If radix is undefined, let radixMV be 10. +/// 3. Else, let radixMV be ? ToIntegerOrInfinity(radix). +/// 4. If radixMV is not in the inclusive interval from 2 to 36, throw a +/// RangeError exception. +/// 5. Return Number::toString(x, radixMV). +fn to_string(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let x = this_number_value(vm, this)?; + let radix = match arg(args, 0) { + r if r == Value::UNDEFINED => 10.0, + r => to_integer_or_infinity(vm, r)?, + }; + if !(2.0..=36.0).contains(&radix) { + return Err(vm.range_error("toString() radix must be between 2 and 36")); + } + let text = if radix == 10.0 { + clazel_js_number::number_to_string(x) + } else { + radix_string(x, radix as u32) + }; + string(vm, &text) +} + +/// Number::toString ( x, radix ), §6.1.6.1.20†, for a radix other than 10: +/// NaN, the zeros and the infinities as for radix 10; otherwise the sign, +/// the integer part's digits and, for a fraction, a point and its digits. +/// The standard leaves the digits of radixes other than 10 to the +/// implementation ("the algorithm is implementation-approximated"); here the +/// integer part is divided down exactly (`fmod` is exact, and the quotient is +/// exact for the power-of-two radixes) and the fraction takes digits until it +/// is spent or 52 have been produced. +fn radix_string(x: f64, radix: u32) -> String { + if x.is_nan() || x.is_infinite() || x == 0.0 { + return clazel_js_number::number_to_string(x); + } + let digit = |d: u32| char::from_digit(d, 36).unwrap_or('0'); + let r = f64::from(radix); + let neg = x < 0.0; + let x = x.abs(); + let mut int = x.trunc(); + let mut frac = x - int; + let mut int_digits: Vec = Vec::new(); + if int == 0.0 { + int_digits.push('0'); + } + while int > 0.0 { + let d = int % r; + int_digits.push(digit(d as u32)); + int = ((int - d) / r).trunc(); + } + int_digits.reverse(); + let mut out = String::new(); + if neg { + out.push('-'); + } + out.extend(int_digits); + if frac > 0.0 { + out.push('.'); + for _ in 0..52 { + frac *= r; + let d = frac.trunc(); + out.push(digit(d as u32)); + frac -= d; + if frac <= 0.0 { + break; + } + } + } + out +} diff --git a/crates/clazel-js-builtins/src/object.rs b/crates/clazel-js-builtins/src/object.rs new file mode 100644 index 000000000..7e0614a0a --- /dev/null +++ b/crates/clazel-js-builtins/src/object.rs @@ -0,0 +1,286 @@ +//! `Object` (§20.1†): the constructor, `Object.prototype.toString`, +//! `hasOwnProperty`, `valueOf`, and `Object.keys`, `defineProperty`, +//! `getPrototypeOf`, `create`. + +use crate::util::{ + arg, constructor, has_own_property, has_property, key, method, new_array_like, new_object_with, + string, +}; +use clazel_js_heap::shape::Attrs; +use clazel_js_heap::value::{Decoded, Value}; +use clazel_js_vm::heap::ObjKind; +use clazel_js_vm::interp::{Abrupt, Intrinsic, Vm}; +use clazel_js_vm::props::PropDesc; + +/// Installs `Object`, `Object.prototype` and their methods. +/// +/// # Errors +/// As [`crate::install`]. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + let proto = vm.intrinsic(Intrinsic::ObjectPrototype); + let ctor = constructor(vm, "Object", 1, object, proto)?; + method(vm, proto, "hasOwnProperty", 1, has_own_property_fn)?; + method(vm, proto, "toString", 0, to_string)?; + method(vm, proto, "valueOf", 0, value_of)?; + method(vm, ctor, "keys", 1, keys)?; + method(vm, ctor, "defineProperty", 3, define_property)?; + method(vm, ctor, "getPrototypeOf", 1, get_prototype_of)?; + method(vm, ctor, "create", 2, create)?; + crate::util::global(vm, "Object", ctor) +} + +/// `Object ( [ value ] )`, §20.1.1.1†: +/// 1. If NewTarget is neither undefined nor the active function object, then +/// a. Return ? OrdinaryCreateFromConstructor(NewTarget, +/// "%Object.prototype%"). +/// 2. If value is either undefined or null, return +/// OrdinaryObjectCreate(%Object.prototype%). +/// 3. Return ! ToObject(value). +fn object(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let nt = vm.new_target(); + if nt != Value::UNDEFINED && nt != vm.active_function() { + return vm.create_from_constructor(nt, Intrinsic::ObjectPrototype, ObjKind::Plain); + } + let value = arg(args, 0); + if value == Value::UNDEFINED || value == Value::NULL { + return vm.new_object(); + } + vm.to_object(value) +} + +/// `Object.prototype.hasOwnProperty ( V )`, §20.1.3.2†: +/// 1. Let P be ? ToPropertyKey(V). +/// 2. Let O be ? ToObject(this value). +/// 3. Return ? HasOwnProperty(O, P). +fn has_own_property_fn(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let k = vm.coerce_property_key(arg(args, 0))?; + let o = vm.to_object(this)?; + Ok(Value::boolean(has_own_property(vm, o, k)?)) +} + +/// `Object.prototype.toString ( )`, §20.1.3.6†: +/// 1. If the this value is undefined, return "[object Undefined]". +/// 2. If the this value is null, return "[object Null]". +/// 3. Let O be ! ToObject(this value). +/// 4. Let isArray be ? IsArray(O). +/// 5. If isArray is true, let builtinTag be "Array". +/// 6. to 14. Otherwise builtinTag is "Arguments", "Function" (O has +/// [[Call]]), "Error" ([[ErrorData]]), "Boolean", "Number" or "String" +/// ([[BooleanData]], [[NumberData]], [[StringData]]), "Date", "RegExp", +/// or else "Object". +/// 15. and 16. `@@toStringTag` is not consulted (no Symbols yet). +/// 17. Return the string-concatenation of "[object ", tag, and "]". +fn to_string(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + if this == Value::UNDEFINED { + return string(vm, "[object Undefined]"); + } + if this == Value::NULL { + return string(vm, "[object Null]"); + } + let o = vm.to_object(this)?; + let tag = if vm.heap().is_callable(o) { + "Function" + } else { + match vm.heap().obj(o).map(|o| o.kind) { + Some(ObjKind::Array) => "Array", + Some(ObjKind::Error) => "Error", + Some(ObjKind::Primitive(p)) => match p.decode() { + Decoded::Bool(_) => "Boolean", + Decoded::Int32(_) | Decoded::Double(_) => "Number", + _ => "String", + }, + _ => "Object", + } + }; + string(vm, &format!("[object {tag}]")) +} + +/// `Object.prototype.valueOf ( )`, §20.1.3.7†: +/// 1. Return ? ToObject(this value). +fn value_of(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + vm.to_object(this) +} + +/// `Object.keys ( O )`, §20.1.2.19†: +/// 1. Let obj be ? ToObject(O). +/// 2. Let keyList be ? EnumerableOwnProperties(obj, key). +/// 3. Return CreateArrayFromList(keyList). +/// +/// EnumerableOwnProperties (§7.3.23†) lists the own string keys in +/// `[[OwnPropertyKeys]]` order whose property is enumerable. The array is +/// an array-like object until arrays exist ([`new_array_like`]). +fn keys(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let obj = vm.to_object(arg(args, 0))?; + let names = enumerable_own_keys(vm, obj)?; + let mut items = Vec::with_capacity(names.len()); + for k in names { + let s = vm + .heap() + .key_string(k) + .cloned() + .ok_or(Abrupt::Internal("key"))?; + items.push(crate::util::js_string(vm, s)?); + } + new_array_like(vm, &items) +} + +fn enumerable_own_keys(vm: &Vm, obj: Value) -> Result, Abrupt> { + Ok(vm + .heap() + .own_properties(obj) + .into_iter() + .filter(|(_, a, _)| a.0 & Attrs::ENUMERABLE != 0) + .map(|(k, _, _)| k) + .collect()) +} + +/// `Object.getPrototypeOf ( O )`, §20.1.2.12†: +/// 1. Let obj be ? ToObject(O). +/// 2. Return ? obj.[[GetPrototypeOf]](). +fn get_prototype_of(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let obj = vm.to_object(arg(args, 0))?; + vm.heap() + .proto_of(obj) + .ok_or(Abrupt::Internal("prototype of a non-object")) +} + +/// `Object.create ( O, Properties )`, §20.1.2.2†: +/// 1. If O is not an Object and O is not null, throw a TypeError exception. +/// 2. Let obj be OrdinaryObjectCreate(O). +/// 3. If Properties is not undefined, then +/// a. Return ? ObjectDefineProperties(obj, Properties). +/// 4. Return obj. +fn create(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let proto = arg(args, 0); + if proto != Value::NULL && !vm.heap().is_object(proto) { + return Err(vm.type_error("Object prototype may only be an Object or null")); + } + let obj = new_object_with(vm, proto, ObjKind::Plain)?; + let props = arg(args, 1); + if props != Value::UNDEFINED { + define_properties(vm, obj, props)?; + } + Ok(obj) +} + +/// ObjectDefineProperties ( O, Properties ), §20.1.2.3.1†: +/// 1. Let props be ? ToObject(Properties). +/// 2. Let keys be ? props.[[OwnPropertyKeys]](). +/// 3. Let descriptors be a new empty List. +/// 4. For each element nextKey of keys, do +/// a. Let propDesc be ? props.[[GetOwnProperty]](nextKey). +/// b. If propDesc is not undefined and propDesc.[[Enumerable]] is true, +/// then i. Let descObj be ? Get(props, nextKey). ii. Let desc be ? +/// ToPropertyDescriptor(descObj). iii. Append the pair to descriptors. +/// 5. For each element property of descriptors, do +/// a. Perform ? DefinePropertyOrThrow(O, property.[[Key]], +/// property.[[Descriptor]]). +/// 6. Return O. +fn define_properties(vm: &mut Vm, obj: Value, props: Value) -> Result<(), Abrupt> { + let props = vm.to_object(props)?; + let mut descriptors = Vec::new(); + for (k, attrs, _) in vm.heap().own_properties(props) { + if attrs.0 & Attrs::ENUMERABLE == 0 { + continue; + } + let desc_obj = vm.get_value(props, k)?; + descriptors.push((k, to_property_descriptor(vm, desc_obj)?)); + } + for (k, desc) in descriptors { + define_property_or_throw(vm, obj, k, &desc)?; + } + Ok(()) +} + +/// A property descriptor for a data property (§6.2.6†): each field present or +/// absent. +#[derive(Clone, Copy, Debug)] +pub struct Descriptor { + /// `[[Value]]`. + pub value: Option, + /// `[[Writable]]`. + pub writable: Option, + /// `[[Enumerable]]`. + pub enumerable: Option, + /// `[[Configurable]]`. + pub configurable: Option, +} + +/// ToPropertyDescriptor ( Obj ), §6.2.6.5†: +/// 1. If Obj is not an Object, throw a TypeError exception. +/// 2. Let desc be a new Property Descriptor that initially has no fields. +/// 3. Let hasEnumerable be ? HasProperty(Obj, "enumerable"). +/// 4. If hasEnumerable is true, then +/// a. Let enumerable be ToBoolean(? Get(Obj, "enumerable")). +/// b. Set desc.[[Enumerable]] to enumerable. +/// 5. to 10. The same for "configurable", "value" and "writable". +/// 11. to 14. "get" and "set": accessors are not yet, `NotYet`. +/// 15. If desc has a [[Get]] or [[Set]] field and a [[Value]] or +/// [[Writable]] field, throw a TypeError (not reachable while accessors +/// are `NotYet`). +fn to_property_descriptor(vm: &mut Vm, obj: Value) -> Result { + if !vm.heap().is_object(obj) { + return Err(vm.type_error("Property description must be an object")); + } + let field = |vm: &mut Vm, name: &str| -> Result, Abrupt> { + let k = key(vm, name)?; + if has_property(vm, obj, k)? { + Ok(Some(vm.get_value(obj, k)?)) + } else { + Ok(None) + } + }; + let enumerable = field(vm, "enumerable")?; + let configurable = field(vm, "configurable")?; + let value = field(vm, "value")?; + let writable = field(vm, "writable")?; + let get = field(vm, "get")?; + let set = field(vm, "set")?; + if get.is_some() || set.is_some() { + return Err(Abrupt::NotYet("accessor property descriptors".into())); + } + Ok(Descriptor { + value, + writable: writable.map(|v| vm.to_boolean(v)), + enumerable: enumerable.map(|v| vm.to_boolean(v)), + configurable: configurable.map(|v| vm.to_boolean(v)), + }) +} + +/// `Object.defineProperty ( O, P, Attributes )`, §20.1.2.4†: +/// 1. If O is not an Object, throw a TypeError exception. +/// 2. Let key be ? ToPropertyKey(P). +/// 3. Let desc be ? ToPropertyDescriptor(Attributes). +/// 4. Perform ? DefinePropertyOrThrow(O, key, desc). +/// 5. Return O. +fn define_property(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let o = arg(args, 0); + if !vm.heap().is_object(o) { + return Err(vm.type_error("Object.defineProperty called on non-object")); + } + let k = vm.coerce_property_key(arg(args, 1))?; + let desc = to_property_descriptor(vm, arg(args, 2))?; + define_property_or_throw(vm, o, k, &desc)?; + Ok(o) +} + +/// DefinePropertyOrThrow, §7.3.8†, over `[[DefineOwnProperty]]` +/// (`Vm::define_own_property`: OrdinaryDefineOwnProperty §10.1.6.1† and the +/// Array exotic object's §10.4.2.1†); a false result is a TypeError. +/// +/// # Errors +/// A `TypeError` when the definition is refused, [`Abrupt::OutOfMemory`]. +pub fn define_property_or_throw( + vm: &mut Vm, + obj: Value, + k: clazel_js_heap::string::Key, + desc: &Descriptor, +) -> Result<(), Abrupt> { + let d = PropDesc { + value: desc.value, + writable: desc.writable, + enumerable: desc.enumerable, + configurable: desc.configurable, + }; + vm.define_property_or_throw(obj, k, &d) +} diff --git a/crates/clazel-js-builtins/src/string.rs b/crates/clazel-js-builtins/src/string.rs new file mode 100644 index 000000000..d0e45d61a --- /dev/null +++ b/crates/clazel-js-builtins/src/string.rs @@ -0,0 +1,267 @@ +//! `String` (§22.1†): the constructor, `String.fromCharCode`, and +//! `String.prototype.toString`, `valueOf`, `charAt`, `charCodeAt`, +//! `indexOf`, `slice`, `substring`. + +use crate::util::{ + CONSTANT, arg, constructor, define, js_string, method, number, require_object_coercible, + string, to_integer_or_infinity, +}; +use clazel_js_heap::string::JsString; +use clazel_js_heap::value::Value; +use clazel_js_vm::heap::ObjKind; +use clazel_js_vm::interp::{Abrupt, Intrinsic, Vm}; + +/// Installs `String` and `String.prototype`. +/// +/// # Errors +/// As [`crate::install`]. +pub fn install(vm: &mut Vm) -> Result<(), Abrupt> { + // §22.1.3†: the prototype is itself a String object whose [[StringData]] + // is the empty String. + let object_proto = vm.intrinsic(Intrinsic::ObjectPrototype); + let empty = string(vm, "")?; + let proto = vm.new_wrapper(empty, object_proto)?; + vm.set_intrinsic(Intrinsic::StringPrototype, proto); + let ctor = constructor(vm, "String", 1, string_constructor, proto)?; + method(vm, ctor, "fromCharCode", 1, from_char_code)?; + method(vm, proto, "charAt", 1, char_at)?; + method(vm, proto, "charCodeAt", 1, char_code_at)?; + method(vm, proto, "indexOf", 1, index_of)?; + method(vm, proto, "slice", 2, slice)?; + method(vm, proto, "substring", 2, substring)?; + method(vm, proto, "toString", 0, to_string)?; + method(vm, proto, "valueOf", 0, value_of)?; + crate::util::global(vm, "String", ctor) +} + +/// `String ( value )`, §22.1.1.1†: +/// 1. If value is not present, let s be the empty String. +/// 2. Else, +/// a. If NewTarget is undefined and value is a Symbol, return +/// SymbolDescriptiveString(value). (No Symbols yet.) +/// b. Let s be ? ToString(value). +/// 3. If NewTarget is undefined, return s. +/// 4. Return StringCreate(s, ? GetPrototypeFromConstructor(NewTarget, +/// "%String.prototype%")). +fn string_constructor(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let s = match args.first() { + Some(v) => { + let s = vm.to_js_string(*v)?; + js_string(vm, s)? + } + None => string(vm, "")?, + }; + let nt = vm.new_target(); + if nt == Value::UNDEFINED { + return Ok(s); + } + // StringCreate, §10.4.3.4†: the String exotic object, with `length`. + let o = vm.create_from_constructor(nt, Intrinsic::StringPrototype, ObjKind::Primitive(s))?; + let len = vm.heap().string(s).map_or(0, JsString::len); + let len = i32::try_from(len).map_err(|_| Abrupt::OutOfMemory)?; + define(vm, o, "length", Value::int32(len), CONSTANT)?; + Ok(o) +} + +/// `String.fromCharCode ( ...codeUnits )`, §22.1.2.1†: +/// 1. Let result be the empty String. +/// 2. For each element next of codeUnits, do +/// a. Let nextCU be the code unit whose numeric value is ℝ(? ToUint16(next)). +/// b. Set result to the string-concatenation of result and nextCU. +/// 3. Return result. +fn from_char_code(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let mut units = Vec::with_capacity(args.len()); + for a in args { + let n = vm.to_number(*a)?; + // ToUint16, §7.1.9†: truncate, then modulo 2^16. + let unit = if n.is_finite() { + (n.trunc().rem_euclid(65536.0)) as u16 + } else { + 0 + }; + units.push(unit); + } + let s = JsString::from_units(&units).map_err(|_| Abrupt::OutOfMemory)?; + js_string(vm, s) +} + +/// thisStringValue ( value ), §22.1.3.35.1†: +/// 1. If value is a String, return value. +/// 2. If value is an Object and value has a [[StringData]] internal slot, +/// then return value.[[StringData]]. +/// 3. Throw a TypeError exception. +fn this_string_value(vm: &mut Vm, v: Value) -> Result { + if vm.heap().is_string(v) { + return Ok(v); + } + if let Some(ObjKind::Primitive(p)) = vm.heap().obj(v).map(|o| o.kind) + && vm.heap().is_string(p) + { + return Ok(p); + } + Err(vm.type_error("not a String")) +} + +/// `String.prototype.toString ( )`, §22.1.3.28†: +/// 1. Return ? thisStringValue(this value). +fn to_string(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + this_string_value(vm, this) +} + +/// `String.prototype.valueOf ( )`, §22.1.3.35†: +/// 1. Return ? thisStringValue(this value). +fn value_of(vm: &mut Vm, this: Value, _args: &[Value]) -> Result { + this_string_value(vm, this) +} + +/// The first steps of every method below: +/// 1. Let O be ? RequireObjectCoercible(this value). +/// 2. Let S be ? ToString(O). +fn this_string(vm: &mut Vm, this: Value, method: &str) -> Result { + require_object_coercible(vm, this, &format!("String.prototype.{method}"))?; + vm.to_js_string(this) +} + +/// The length of a string as `f64`. +fn len_of(s: &JsString) -> f64 { + s.len() as f64 +} + +/// `String.prototype.charAt ( pos )`, §22.1.3.2†: +/// 1 and 2. As above. +/// 3. Let position be ? ToIntegerOrInfinity(pos). +/// 4. Let size be the length of S. +/// 5. If position < 0 or position ≥ size, return the empty String. +/// 6. Return the substring of S from position to position + 1. +fn char_at(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let s = this_string(vm, this, "charAt")?; + let position = to_integer_or_infinity(vm, arg(args, 0))?; + if position < 0.0 || position >= len_of(&s) { + return string(vm, ""); + } + let i = position as usize; + js_string(vm, s.substring(i, i.saturating_add(1))) +} + +/// `String.prototype.charCodeAt ( pos )`, §22.1.3.3†: +/// 1 and 2. As above. +/// 3. Let position be ? ToIntegerOrInfinity(pos). +/// 4. Let size be the length of S. +/// 5. If position < 0 or position ≥ size, return NaN. +/// 6. Return the Number value for the numeric value of the code unit at +/// index position within the String S. +fn char_code_at(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let s = this_string(vm, this, "charCodeAt")?; + let position = to_integer_or_infinity(vm, arg(args, 0))?; + if position < 0.0 || position >= len_of(&s) { + return Ok(Value::double(f64::NAN)); + } + let unit = s.unit(position as usize).unwrap_or(0); + Ok(Value::int32(i32::from(unit))) +} + +/// `String.prototype.indexOf ( searchString [ , position ] )`, §22.1.3.9†: +/// 1 and 2. As above. +/// 3. Let searchStr be ? ToString(searchString). +/// 4. Let pos be ? ToIntegerOrInfinity(position). +/// 5. Assert: If position is undefined, then pos is 0. +/// 6. Let len be the length of S. +/// 7. Let start be the result of clamping pos between 0 and len. +/// 8. Let result be StringIndexOf(S, searchStr, start). +/// 9. If result is not-found, return -1𝔽. +/// 10. Return 𝔽(result). +/// +/// StringIndexOf ( string, searchValue, fromIndex ), §22.1.3.?†: +/// 3. If searchValue is the empty String and fromIndex ≤ len, return +/// fromIndex. +/// 5 to 8. For each i from fromIndex to len - searchLen, if the substring of +/// string from i to i + searchLen is searchValue, return i. +/// 9. Return not-found. +fn index_of(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let s = this_string(vm, this, "indexOf")?; + let search = vm.to_js_string(arg(args, 0))?; + let pos = to_integer_or_infinity(vm, arg(args, 1))?; + let start = pos.clamp(0.0, len_of(&s)) as usize; + let hay: Vec = s.units().collect(); + let needle: Vec = search.units().collect(); + if needle.is_empty() { + return Ok(number(start as f64)); + } + let Some(last) = hay.len().checked_sub(needle.len()) else { + return Ok(Value::int32(-1)); + }; + for i in start..=last { + let end = i.saturating_add(needle.len()); + if hay.get(i..end) == Some(needle.as_slice()) { + return Ok(number(i as f64)); + } + } + Ok(Value::int32(-1)) +} + +/// `String.prototype.slice ( start, end )`, §22.1.3.24†: +/// 1 and 2. As above. +/// 3. Let len be the length of S. +/// 4. Let intStart be ? ToIntegerOrInfinity(start). +/// 5. If intStart = -∞, let from be 0. +/// 6. Else if intStart < 0, let from be max(len + intStart, 0). +/// 7. Else, let from be min(intStart, len). +/// 8. If end is undefined, let intEnd be len; else let intEnd be ? +/// ToIntegerOrInfinity(end). +/// 9. If intEnd = -∞, let to be 0. +/// 10. Else if intEnd < 0, let to be max(len + intEnd, 0). +/// 11. Else, let to be min(intEnd, len). +/// 12. If from ≥ to, return the empty String. +/// 13. Return the substring of S from from to to. +fn slice(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let s = this_string(vm, this, "slice")?; + let len = len_of(&s); + let relative = |n: f64| -> f64 { + if n == f64::NEG_INFINITY { + 0.0 + } else if n < 0.0 { + (len + n).max(0.0) + } else { + n.min(len) + } + }; + let int_start = to_integer_or_infinity(vm, arg(args, 0))?; + let from = relative(int_start); + let end = arg(args, 1); + let to = if end == Value::UNDEFINED { + len + } else { + relative(to_integer_or_infinity(vm, end)?) + }; + if from >= to { + return string(vm, ""); + } + js_string(vm, s.substring(from as usize, to as usize)) +} + +/// `String.prototype.substring ( start, end )`, §22.1.3.25†: +/// 1 and 2. As above. +/// 3. Let len be the length of S. +/// 4. Let intStart be ? ToIntegerOrInfinity(start). +/// 5. If end is undefined, let intEnd be len; else let intEnd be ? +/// ToIntegerOrInfinity(end). +/// 6. Let finalStart be the result of clamping intStart between 0 and len. +/// 7. Let finalEnd be the result of clamping intEnd between 0 and len. +/// 8. Let from be min(finalStart, finalEnd). +/// 9. Let to be max(finalStart, finalEnd). +/// 10. Return the substring of S from from to to. +fn substring(vm: &mut Vm, this: Value, args: &[Value]) -> Result { + let s = this_string(vm, this, "substring")?; + let len = len_of(&s); + let int_start = to_integer_or_infinity(vm, arg(args, 0))?; + let end = arg(args, 1); + let int_end = if end == Value::UNDEFINED { + len + } else { + to_integer_or_infinity(vm, end)? + }; + let final_start = int_start.clamp(0.0, len); + let final_end = int_end.clamp(0.0, len); + let (from, to) = (final_start.min(final_end), final_start.max(final_end)); + js_string(vm, s.substring(from as usize, to as usize)) +} diff --git a/crates/clazel-js-builtins/src/util.rs b/crates/clazel-js-builtins/src/util.rs new file mode 100644 index 000000000..b3b17a5d4 --- /dev/null +++ b/crates/clazel-js-builtins/src/util.rs @@ -0,0 +1,213 @@ +//! Helpers the built-ins share: arguments, defining properties, the +//! conversions of §7.1 that need no more than the VM's, and `SameValue`. + +use clazel_js_heap::shape::Attrs; +use clazel_js_heap::string::{JsString, Key}; +use clazel_js_heap::value::Value; +use clazel_js_vm::heap::ObjKind; +use clazel_js_vm::interp::{Abrupt, Intrinsic, NativeFn, Vm}; +use clazel_js_vm::numeric::{Num, narrow, num}; + +/// `[[Writable]]` and `[[Configurable]]`: the attributes of a built-in +/// function property (§17†: "Every other data property described in clauses +/// 19 through 28 ... has the attributes { [[Writable]]: true, +/// [[Enumerable]]: false, [[Configurable]]: true }"). +pub const METHOD: Attrs = Attrs(Attrs::WRITABLE | Attrs::CONFIGURABLE); +/// No attribute: a constant such as `Math.PI` or `Number.MAX_VALUE`. +pub const CONSTANT: Attrs = Attrs(0); +/// `[[Configurable]]` only: `length` and `name` of a function (§10.2.9†). +pub const CONFIGURABLE_ONLY: Attrs = Attrs(Attrs::CONFIGURABLE); + +/// Argument `i`, `undefined` if there are fewer (§10.4.1.1†'s convention for +/// built-ins: a missing argument is `undefined`). +#[must_use] +pub fn arg(args: &[Value], i: usize) -> Value { + args.get(i).copied().unwrap_or(Value::UNDEFINED) +} + +/// The interned key for a name. +/// +/// # Errors +/// [`Abrupt::OutOfMemory`] if the name is too long. +pub fn key(vm: &mut Vm, name: &str) -> Result { + Ok(vm.heap_mut().intern_str(name)?) +} + +/// A new string value. +/// +/// # Errors +/// [`Abrupt::OutOfMemory`]. +pub fn string(vm: &mut Vm, s: &str) -> Result { + vm.new_string(s) +} + +/// A new string value from a [`JsString`]. +/// +/// # Errors +/// [`Abrupt::OutOfMemory`]. +pub fn js_string(vm: &mut Vm, s: JsString) -> Result { + Ok(vm.heap_mut().new_string(s)?) +} + +/// A Number value, in the encoding the interpreter keeps (an int32 when the +/// Number is an integer in range other than `-0`). +#[must_use] +pub fn number(d: f64) -> Value { + match narrow(d) { + Num::I(i) => Value::int32(i), + Num::D(d) => Value::double(d), + } +} + +/// Defines the own data property `name` of `obj` with `attrs`. +/// +/// # Errors +/// [`Abrupt::OutOfMemory`], or [`Abrupt::Internal`] if `obj` is not an +/// object. +pub fn define(vm: &mut Vm, obj: Value, name: &str, v: Value, attrs: Attrs) -> Result<(), Abrupt> { + let k = key(vm, name)?; + Ok(vm.heap_mut().define(obj, k, v, attrs)?) +} + +/// Defines a built-in method of `obj`: a native function with `length` and +/// `name`, as a writable, non-enumerable, configurable property. +/// +/// # Errors +/// As [`define`]. +pub fn method( + vm: &mut Vm, + obj: Value, + name: &str, + length: i32, + f: NativeFn, +) -> Result { + let func = vm.new_native(name, length, f)?; + define(vm, obj, name, func, METHOD)?; + Ok(func) +} + +/// Makes a constructor: a native constructor `name` with `proto` as its +/// `prototype` (no attributes, §17†) and `proto.constructor` pointing back +/// (writable, configurable). +/// +/// # Errors +/// As [`define`]. +pub fn constructor( + vm: &mut Vm, + name: &str, + length: i32, + f: NativeFn, + proto: Value, +) -> Result { + let ctor = vm.new_native_constructor(name, length, f)?; + define(vm, ctor, "prototype", proto, CONSTANT)?; + define(vm, proto, "constructor", ctor, METHOD)?; + Ok(ctor) +} + +/// Defines `name` of the global object as a writable, non-enumerable, +/// configurable property (§19†, "Function Properties of the Global Object" +/// and §19.3†, "Constructor Properties"). +/// +/// # Errors +/// As [`define`]. +pub fn global(vm: &mut Vm, name: &str, v: Value) -> Result<(), Abrupt> { + let g = vm.global(); + define(vm, g, name, v, METHOD) +} + +/// A new ordinary object with the intrinsic `%Object.prototype%`. +/// +/// # Errors +/// [`Abrupt::OutOfMemory`]. +pub fn new_plain(vm: &mut Vm) -> Result { + vm.new_object() +} + +/// A new ordinary object with `proto` as its prototype and `kind`. +/// +/// # Errors +/// [`Abrupt::OutOfMemory`]. +pub fn new_object_with(vm: &mut Vm, proto: Value, kind: ObjKind) -> Result { + Ok(vm.heap_mut().new_object(proto, kind)?) +} + +/// The list of values as an array: CreateArrayFromList, §7.3.18†. +/// +/// # Errors +/// [`Abrupt::OutOfMemory`]. +pub fn new_array_like(vm: &mut Vm, items: &[Value]) -> Result { + vm.create_array_from_list(items) +} + +/// The Number a value is, if it is one. +#[must_use] +pub fn as_number(v: Value) -> Option { + num(v).map(Num::to_f64) +} + +/// RequireObjectCoercible, §7.2.1†: `undefined` and `null` are a TypeError. +/// +/// # Errors +/// A `TypeError`. +pub fn require_object_coercible(vm: &mut Vm, v: Value, what: &str) -> Result<(), Abrupt> { + if v == Value::UNDEFINED || v == Value::NULL { + return Err(vm.type_error(&format!("{what} called on null or undefined"))); + } + Ok(()) +} + +/// ToIntegerOrInfinity, §7.1.5†: NaN and the zeros are `+0`, the infinities +/// stay, any other Number is truncated. +/// +/// # Errors +/// The exceptions of ToNumber. +pub fn to_integer_or_infinity(vm: &mut Vm, v: Value) -> Result { + let n = vm.to_number(v)?; + if n.is_nan() { + return Ok(0.0); + } + // `+ 0.0` turns `-0` into `+0` (step 4: "return 0" for a zero). + Ok(n.trunc() + 0.0) +} + +/// SameValue, §7.2.10†: Numbers by value with NaN equal to itself and `+0` +/// distinct from `-0`, Strings by code units, everything else by identity. +#[must_use] +pub fn same_value(vm: &Vm, a: Value, b: Value) -> bool { + if let (Some(x), Some(y)) = (as_number(a), as_number(b)) { + return if x.is_nan() || y.is_nan() { + x.is_nan() && y.is_nan() + } else { + x.to_bits() == y.to_bits() + }; + } + if let (Some(x), Some(y)) = (vm.heap().string(a), vm.heap().string(b)) { + return x == y; + } + a == b +} + +/// HasProperty, §7.3.12†, for an object: an own or inherited property. +/// +/// # Errors +/// [`Abrupt::Internal`] if the prototype chain is corrupt. +pub fn has_property(vm: &Vm, obj: Value, k: Key) -> Result { + Ok(vm.heap().find(obj, k)?.is_some()) +} + +/// The own-property test of `[[GetOwnProperty]]` for the objects this slice +/// has (§10.1.5†). +/// +/// # Errors +/// [`Abrupt::Internal`] if the prototype chain is corrupt. +pub fn has_own_property(vm: &Vm, obj: Value, k: Key) -> Result { + Ok(vm.heap().find(obj, k)?.is_some_and(|f| f.own)) +} + +/// The prototype a native error, wrapper or other object gets: the intrinsic +/// if the realm has it installed. +#[must_use] +pub fn intrinsic(vm: &Vm, which: Intrinsic) -> Value { + vm.intrinsic(which) +} diff --git a/crates/clazel-js-builtins/tests/array.rs b/crates/clazel-js-builtins/tests/array.rs new file mode 100644 index 000000000..c6cf1c3ce --- /dev/null +++ b/crates/clazel-js-builtins/tests/array.rs @@ -0,0 +1,437 @@ +//! Tests of arrays: the Array exotic object (§10.4.2†) and the `Array` +//! built-in, as small scripts run with the built-ins installed. Each +//! expectation is what ECMA-262 says, written by hand; the result is the +//! global `r` as its ToString. No files are read, so these run under Miri +//! too (with every eighth case, as the other built-in tests do). + +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; +use clazel_js_vm::compile::compile_script; +use clazel_js_vm::interp::{Config, Vm}; + +fn cps(src: &str) -> Vec { + src.chars().map(u32::from).collect() +} + +fn config() -> Config { + if cfg!(miri) { + Config { + region_slots: 1 << 12, + heap_cells: 1 << 14, + } + } else { + Config::default() + } +} + +fn r_of(src: &str) -> Result { + let c = cps(src); + let ast = parse(&c, SourceGoal::Script).map_err(|_| format!("does not parse: {src}"))?; + let p = compile_script(&ast, &c).map_err(|e| format!("{src}: {e}"))?; + let mut vm = Vm::new(config()).map_err(|e| format!("{e:?}"))?; + clazel_js_builtins::install(&mut vm).map_err(|e| format!("install: {e:?}"))?; + let script = vm.load(&p).map_err(|e| format!("{e:?}"))?; + vm.set_budget(5_000_000); + vm.run(&script).map_err(|e| format!("{src}: {e:?}"))?; + let g = vm.global(); + let r = vm.get_named(g, "r").map_err(|e| format!("{e:?}"))?; + let s = vm.to_js_string(r).map_err(|e| format!("{src}: {e:?}"))?; + Ok(s.to_string_lossy()) +} + +fn check(cases: &[(&str, &str)]) -> Result<(), String> { + let stride = if cfg!(miri) { 8 } else { 1 }; + for (src, want) in cases.iter().step_by(stride) { + let got = r_of(src)?; + if got != *want { + return Err(format!("{src}: expected {want:?}, got {got:?}")); + } + } + Ok(()) +} + +#[test] +fn array_exotic_objects() -> Result<(), String> { + check(&[ + // §10.4.2.1 step 2.h: an index at or above `length` grows it. + ("var a = []; a[4] = 1; var r = a.length;", "5"), + ("var a = [1, 2, 3]; var r = a.length;", "3"), + ( + "var a = [1, , 3]; var r = a.length + ',' + (1 in a);", + "3,false", + ), + ("var a = [1, 2, ,]; var r = a.length;", "3"), + // A key that is not a canonical index does not. + ("var a = []; a['01'] = 1; a.x = 2; var r = a.length;", "0"), + ( + "var a = []; a[4294967294] = 1; var r = a.length;", + "4294967295", + ), + ("var a = []; a[4294967295] = 1; var r = a.length;", "0"), + // ArraySetLength truncates, §10.4.2.4. + ( + "var a = [1, 2, 3, 4]; a.length = 2; var r = a + ',' + a.length;", + "1,2,2", + ), + ( + "var a = [1, 2]; a.length = 5; var r = a.length + ',' + (3 in a);", + "5,false", + ), + ( + "var a = [1, 2, 3]; a.length = 0; var r = a.length + ',' + (0 in a);", + "0,false", + ), + // Step 5: a RangeError for a length that is not a Uint32. + ( + "var r; try { [].length = -1; } catch (e) { r = e.name; }", + "RangeError", + ), + ( + "var r; try { [].length = 1.5; } catch (e) { r = e.name; }", + "RangeError", + ), + ("var a = []; a.length = '3'; var r = a.length;", "3"), + // A non-configurable element stops the truncation (step 16.b). + ( + "var a = [1, 2]; Object.defineProperty(a, 2, { value: 5 }); a.length = 0; var r = a.length + ',' + a[0] + ',' + a[2];", + "3,1,5", + ), + // `length` is not enumerable, writable, not configurable. + ( + "var a = [1]; var r = Object.keys(a).join() + '|' + a.hasOwnProperty('length');", + "0|true", + ), + ( + "var a = [1, 2]; Object.defineProperty(a, 'length', { writable: false }); a[2] = 3; var r = a.length + ',' + (2 in a);", + "2,false", + ), + ( + "'use strict'; var a = [1]; Object.defineProperty(a, 'length', { writable: false }); var r; try { a.push(2); } catch (e) { r = e.name + a.length; }", + "TypeError1", + ), + // Array.prototype is an array, Object.prototype.toString says so. + ( + "var r = Array.isArray(Array.prototype) + ',' + Object.prototype.toString.call([]);", + "true,[object Array]", + ), + ( + "var r = Array.isArray({ length: 0 }) + ',' + Array.isArray([]);", + "false,true", + ), + ( + "var r = Object.getPrototypeOf([]) === Array.prototype;", + "true", + ), + // Object.keys returns an array now. + ("var r = Array.isArray(Object.keys({ a: 1 }));", "true"), + ( + "var a = [3, 4]; var r = delete a[0]; r = r + ',' + a.length + ',' + (0 in a);", + "true,2,false", + ), + ]) +} + +#[test] +fn array_constructor_and_statics() -> Result<(), String> { + check(&[ + ("var r = new Array().length;", "0"), + ( + "var r = new Array(3).length + ',' + (0 in new Array(3));", + "3,false", + ), + ("var r = Array(1, 2, 3).join();", "1,2,3"), + ("var r = Array('a').join() + new Array('a').length;", "a1"), + ( + "var r; try { new Array(-1); } catch (e) { r = e.name; }", + "RangeError", + ), + ( + "var r; try { new Array(1.5); } catch (e) { r = e.name; }", + "RangeError", + ), + ("var r = new Array(4294967295).length;", "4294967295"), + ( + "var r = Array.of(7, 8, 9).join('-') + Array.of(3).length;", + "7-8-91", + ), + ("var r = Array.of().length;", "0"), + ( + "var r = Array.length + Array.name + Array.prototype.constructor.name;", + "1ArrayArray", + ), + ( + "var r = Array.prototype.push.length + Array.prototype.splice.length;", + "3", + ), + ("var r = Array.isArray(new Array(2));", "true"), + ]) +} + +#[test] +fn array_mutators() -> Result<(), String> { + check(&[ + ( + "var a = [1]; var n = a.push(2, 3); var r = n + ',' + a;", + "3,1,2,3", + ), + ( + "var a = [1, 2, 3]; var x = a.pop(); var r = x + ',' + a;", + "3,1,2", + ), + ( + "var a = []; var r = a.pop() + ',' + a.length;", + "undefined,0", + ), + ( + "var a = [1, 2, 3]; var x = a.shift(); var r = x + ',' + a + ',' + a.length;", + "1,2,3,2", + ), + ( + "var a = [1, , 3]; a.shift(); var r = a.length + ',' + (0 in a) + ',' + a[1];", + "2,false,3", + ), + ( + "var a = [3]; var n = a.unshift(1, 2); var r = n + ',' + a;", + "3,1,2,3", + ), + ( + "var a = [1, 2, 3, 4, 5]; var d = a.splice(1, 2); var r = d + '|' + a;", + "2,3|1,4,5", + ), + ( + "var a = [1, 2, 3]; var d = a.splice(1, 0, 'x', 'y'); var r = d.length + '|' + a;", + "0|1,x,y,2,3", + ), + ( + "var a = [1, 2, 3]; var d = a.splice(1); var r = d + '|' + a;", + "2,3|1", + ), + ( + "var a = [1, 2, 3]; var d = a.splice(-1, 1, 9); var r = d + '|' + a;", + "3|1,2,9", + ), + ( + "var a = [1, 2, 3]; var d = a.splice(); var r = d.length + '|' + a;", + "0|1,2,3", + ), + ( + "var a = [1, 2, 3]; var d = a.splice(0, 3, 'a'); var r = d + '|' + a;", + "1,2,3|a", + ), + ("var a = [1, 2, 3]; a.reverse(); var r = a.join();", "3,2,1"), + ( + "var a = [1, 2, 3, 4]; a.reverse(); var r = a.join();", + "4,3,2,1", + ), + ( + "var a = [1, , 3]; a.reverse(); var r = a.join() + ',' + (1 in a);", + "3,,1,false", + ), + ( + "var a = [1, , 3, 4]; a.reverse(); var r = (2 in a) + ',' + a[3];", + "false,1", + ), + ( + "var a = [1, 2, 3, 4]; a.fill(0, 1, 3); var r = a.join();", + "1,0,0,4", + ), + ("var a = [1, 2, 3]; a.fill(7); var r = a.join();", "7,7,7"), + ( + "var a = [1, 2, 3]; a.fill(7, -1); var r = a.join();", + "1,2,7", + ), + // Generic: an array-like object. + ( + "var o = { length: 2, 0: 'a', 1: 'b' }; Array.prototype.push.call(o, 'c'); var r = o.length + o[2];", + "3c", + ), + ( + "var o = { length: 3, 0: 'a', 1: 'b', 2: 'c' }; Array.prototype.reverse.call(o); var r = o[0] + o[1] + o[2];", + "cba", + ), + ( + "var o = { length: 9007199254740991 }; var r; try { Array.prototype.push.call(o, 1); } catch (e) { r = e.name; }", + "TypeError", + ), + ]) +} + +#[test] +fn array_accessors() -> Result<(), String> { + check(&[ + ("var r = [1, 2, 3, 4].slice(1, 3).join();", "2,3"), + ("var r = [1, 2, 3, 4].slice(-2).join();", "3,4"), + ("var r = [1, 2, 3, 4].slice().join();", "1,2,3,4"), + ("var r = [1, 2, 3].slice(2, 1).length;", "0"), + ( + "var s = [1, , 3].slice(0); var r = s.length + ',' + (1 in s);", + "3,false", + ), + ("var r = [1, 2].concat([3, 4], 5, [[6]]).length;", "6"), + ("var r = [1].concat([2], [3, [4]]).join();", "1,2,3,4"), + ( + "var c = [1, , 3].concat([4]); var r = c.length + ',' + (1 in c);", + "4,false", + ), + ("var r = [1, 2, 3].join('-');", "1-2-3"), + ("var r = [1, null, undefined, 4].join();", "1,,,4"), + ("var r = [].join() + '|' + [5].join('x');", "|5"), + ("var r = [[1, 2], [3]].join(';');", "1,2;3"), + ("var r = String([1, [2, 3]]);", "1,2,3"), + ( + "var r = [1, 2, 3, 2].indexOf(2) + ',' + [1, 2, 3, 2].indexOf(2, 2) + ',' + [1].indexOf(5);", + "1,3,-1", + ), + ( + "var r = [1, 2, 3, 2].indexOf(2, -1) + ',' + [NaN].indexOf(NaN);", + "3,-1", + ), + ( + "var r = [1, 2, 3, 2].lastIndexOf(2) + ',' + [1, 2, 3, 2].lastIndexOf(2, 2) + ',' + [1].lastIndexOf(5);", + "3,1,-1", + ), + ("var r = [1, 2, 3].lastIndexOf(3, -2);", "-1"), + ( + "var r = [1, NaN].includes(NaN) + ',' + [1, 2].includes(3) + ',' + [1, 2].includes(1, 1);", + "true,false,false", + ), + ( + "var r = [, 1].includes(undefined) + ',' + [, 1].indexOf(undefined);", + "true,-1", + ), + ( + "var r = [0].includes(-0) + ',' + [-0].indexOf(0);", + "true,0", + ), + ]) +} + +#[test] +fn array_callbacks() -> Result<(), String> { + check(&[ + ( + "var s = 0; [1, 2, 3].forEach(function (v) { s += v; }); var r = s;", + "6", + ), + ( + "var s = 0; [1, , 3].forEach(function (v) { s += 1; }); var r = s;", + "2", + ), + ( + "var o = {}; [1].forEach(function (v, i, a) { o.x = this === o && v === 1 && i === 0 && a.length === 1; }, o); var r = o.x;", + "true", + ), + ( + "var r = [1, 2, 3].map(function (v) { return v * 2; }).join();", + "2,4,6", + ), + ( + "var m = [1, , 3].map(function (v) { return v; }); var r = m.length + ',' + (1 in m);", + "3,false", + ), + ( + "var r = [1, 2, 3, 4].filter(function (v) { return v % 2 === 0; }).join();", + "2,4", + ), + ( + "var r = [1, 2, 3].some(function (v) { return v > 2; }) + ',' + [1, 2].some(function (v) { return v > 2; });", + "true,false", + ), + ( + "var r = [1, 2, 3].every(function (v) { return v > 0; }) + ',' + [1, 2].every(function (v) { return v > 1; }) + ',' + [].every(function () { return false; });", + "true,false,true", + ), + ( + "var r = [1, 2, 3].reduce(function (a, v) { return a + v; }) + ',' + [1, 2, 3].reduce(function (a, v) { return a + v; }, 10);", + "6,16", + ), + ( + "var r = ['a', 'b', 'c'].reduceRight(function (a, v) { return a + v; }) + ',' + [1].reduceRight(function (a, v) { return a + v; }, 5);", + "cba,6", + ), + ( + "var r; try { [].reduce(function () {}); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "var r; try { [1].map(3); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "var r = [1, 2, 3].find(function (v) { return v > 1; }) + ',' + [1, 2, 3].findIndex(function (v) { return v > 1; }) + ',' + [1].find(function (v) { return v > 1; }) + ',' + [1].findIndex(function (v) { return v > 1; });", + "2,1,undefined,-1", + ), + // find visits holes as undefined (no HasProperty). + ( + "var n = 0; [, ,].find(function (v) { n++; return false; }); var r = n;", + "2", + ), + // The length is read once: an element pushed during forEach is not visited. + ( + "var a = [1, 2]; var n = 0; a.forEach(function (v) { n++; a.push(9); }); var r = n + ',' + a.length;", + "2,4", + ), + ]) +} + +#[test] +fn array_sort() -> Result<(), String> { + check(&[ + ("var r = [3, 1, 2].sort().join();", "1,2,3"), + // The default order is by string: "10" < "9". + ("var r = [9, 10, 1].sort().join();", "1,10,9"), + ( + "var r = [3, 1, 2].sort(function (a, b) { return b - a; }).join();", + "3,2,1", + ), + // undefined last, holes after them. + ( + "var a = [3, undefined, 1, , 2]; a.sort(); var r = a.length + ',' + a.join() + ',' + (4 in a) + ',' + (3 in a);", + "5,1,2,3,,,false,true", + ), + // Stable. + ( + "var a = [{ k: 1, v: 'a' }, { k: 0, v: 'b' }, { k: 1, v: 'c' }, { k: 0, v: 'd' }]; a.sort(function (x, y) { return x.k - y.k; }); var r = a.map(function (e) { return e.v; }).join('');", + "bdac", + ), + ( + "var r; try { [1].sort(1); } catch (e) { r = e.name; }", + "TypeError", + ), + // An exception from the comparator ends the sort and propagates. + ( + "var r; try { [2, 1].sort(function () { throw 7; }); } catch (e) { r = e; }", + "7", + ), + ( + "var a = []; for (var i = 0; i < 50; i++) a.push((i * 37) % 50); a.sort(function (x, y) { return x - y; }); var ok = true; for (var j = 0; j < 50; j++) if (a[j] !== j) ok = false; var r = ok;", + "true", + ), + // NaN from the comparator is +0. + ( + "var r = [2, 1].sort(function () { return NaN; }).join();", + "2,1", + ), + ]) +} + +#[test] +fn array_species_and_recursion_bounds() -> Result<(), String> { + check(&[ + // A cyclic join recurses through natives until the bound, a RangeError. + ( + "var a = []; a[0] = a; var r; try { a.join(); } catch (e) { r = e.name; }", + "RangeError", + ), + // A `constructor` that is not a constructor is a TypeError + // (§10.4.2.3 step 6). + ( + "var a = [1]; a.constructor = 1; var r; try { a.map(function (v) { return v; }); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "var a = [1]; a.constructor = undefined; var r = a.map(function (v) { return v + 1; }).join();", + "2", + ), + ]) +} diff --git a/crates/clazel-js-builtins/tests/builtins.rs b/crates/clazel-js-builtins/tests/builtins.rs new file mode 100644 index 000000000..d10c4b743 --- /dev/null +++ b/crates/clazel-js-builtins/tests/builtins.rs @@ -0,0 +1,762 @@ +//! Tests of the built-ins: small scripts compiled by `compile_script` and run +//! in a VM with the built-ins installed, the result read from the global `r` +//! as its ToString. Each expectation is what ECMA-262 says, written by hand. +//! +//! These tests read no files, so they also run under Miri. + +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; +use clazel_js_vm::compile::compile_script; +use clazel_js_vm::interp::{Abrupt, Config, Vm}; + +fn cps(src: &str) -> Vec { + src.chars().map(u32::from).collect() +} + +fn config() -> Config { + if cfg!(miri) { + Config { + region_slots: 1 << 12, + heap_cells: 1 << 14, + } + } else { + Config::default() + } +} + +/// Runs `src` with the built-ins; the VM and the result. +fn run(src: &str) -> Result<(Vm, Result), String> { + let c = cps(src); + let ast = parse(&c, SourceGoal::Script).map_err(|_| format!("does not parse: {src}"))?; + let p = compile_script(&ast, &c).map_err(|e| format!("{src}: {e}"))?; + let mut vm = Vm::new(config()).map_err(|e| format!("{e:?}"))?; + clazel_js_builtins::install(&mut vm).map_err(|e| format!("install: {e:?}"))?; + let script = vm.load(&p).map_err(|e| format!("{e:?}"))?; + let r = vm.run(&script); + Ok((vm, r)) +} + +/// The ToString of the global `r` after running `src`. +fn r_of(src: &str) -> Result { + let (mut vm, res) = run(src)?; + res.map_err(|e| format!("{src}: {e:?}"))?; + let g = vm.global(); + let r = vm.get_named(g, "r").map_err(|e| format!("{e:?}"))?; + let s = vm.to_js_string(r).map_err(|e| format!("{src}: {e:?}"))?; + Ok(s.to_string_lossy()) +} + +/// Runs each `(script, expected ToString of r)`. +fn check(cases: &[(&str, &str)]) -> Result<(), String> { + // Each case builds a VM and installs every built-in, which Miri runs + // slowly (object_constructor_and_prototype took 298 s there, at xtask's + // 300 s limit), so under Miri every third case runs. Natively, all do. + let stride = if cfg!(miri) { 3 } else { 1 }; + for (src, want) in cases.iter().step_by(stride) { + let got = r_of(src)?; + if got != *want { + return Err(format!("{src}: expected {want:?}, got {got:?}")); + } + } + Ok(()) +} + +#[test] +fn objects_reach_to_primitive() -> Result<(), String> { + check(&[ + // §7.1.1.1 OrdinaryToPrimitive: `valueOf` first for hint number and + // default, `toString` first for hint string. + ( + "var o = { valueOf: function () { return 6; } }; var r = o * 7;", + "42", + ), + ( + "var o = { valueOf: function () { return 6; } }; var r = o + 1;", + "7", + ), + ( + "var o = { toString: function () { return 'x'; } }; var r = o + 'y';", + "xy", + ), + ( + "var o = { toString: function () { return 'x'; }, valueOf: function () { return 1; } }; var r = '' + o;", + "1", + ), + ( + "var o = { toString: function () { return '5'; } }; var r = o * 2;", + "10", + ), + // A `valueOf` that returns an object is skipped for `toString`. + ( + "var o = { valueOf: function () { return {}; }, toString: function () { return '3'; } }; var r = +o;", + "3", + ), + ( + "var a = { valueOf: function () { return 1; } }, b = { valueOf: function () { return 2; } }; var r = a < b;", + "true", + ), + ( + "var o = { valueOf: function () { return 3; } }; var r = o == 3;", + "true", + ), + ("var r = {} + '';", "[object Object]"), + ("var r = String({});", "[object Object]"), + // Neither method gives a primitive: a TypeError. + ( + "var o = { valueOf: function () { return {}; }, toString: function () { return {}; } }; var r; try { o + 1; } catch (e) { r = e.constructor === TypeError; }", + "true", + ), + ]) +} + +#[test] +fn errors_are_constructed_by_the_constructors() -> Result<(), String> { + check(&[ + ("var e = new Error('boom'); var r = e.message;", "boom"), + ("var e = Error('boom'); var r = e.message;", "boom"), + ("var r = new Error().hasOwnProperty('message');", "false"), + ("var r = new Error('').hasOwnProperty('message');", "true"), + ("var r = new Error('m').toString();", "Error: m"), + ("var r = new TypeError('m').toString();", "TypeError: m"), + ("var r = new RangeError().toString();", "RangeError"), + ("var r = new SyntaxError('s').name;", "SyntaxError"), + ("var r = new ReferenceError('s').name;", "ReferenceError"), + ( + "var r = Object.getPrototypeOf(new TypeError()) === TypeError.prototype;", + "true", + ), + ( + "var r = Object.getPrototypeOf(TypeError.prototype) === Error.prototype;", + "true", + ), + ( + "var r = Object.getPrototypeOf(TypeError) === Error;", + "true", + ), + ( + "var r = TypeError.prototype.constructor === TypeError;", + "true", + ), + ( + "var r = Object.prototype.toString.call(new Error());", + "[object Error]", + ), + ( + "var r = Error.prototype.toString.call({ name: 'N', message: 'M' });", + "N: M", + ), + ( + "var r = Error.prototype.toString.call({ name: '', message: 'M' });", + "M", + ), + ("var r = Error.prototype.toString.call({});", "Error"), + ("var r = new Error('m', { cause: 7 }).cause;", "7"), + ( + "var r = new Error('m', {}).hasOwnProperty('cause');", + "false", + ), + ( + "var r = Error.length + ',' + TypeError.name;", + "1,TypeError", + ), + // The errors the VM throws are the same objects. + ( + "var r; try { null.x; } catch (e) { r = (e.constructor === TypeError) + ',' + (Object.getPrototypeOf(e) === TypeError.prototype) + ',' + e.hasOwnProperty('name'); }", + "true,true,false", + ), + ( + "var r; try { undefinedName; } catch (e) { r = e.constructor === ReferenceError; }", + "true", + ), + ( + "var r; try { new Error('a', 1); Error.prototype.toString.call(1); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "var r = Object.prototype.toString.call((function () { try { null.x; } catch (e) { return e; } })());", + "[object Error]", + ), + ]) +} + +#[test] +fn object_constructor_and_prototype() -> Result<(), String> { + check(&[ + ("var r = typeof Object();", "object"), + ("var o = {}; var r = Object(o) === o;", "true"), + ("var r = typeof Object(1) + (Object(1) + 1);", "object2"), + ("var r = Object('ab').length;", "2"), + ( + "var r = Object(null) !== null && typeof Object(undefined);", + "object", + ), + ("var r = typeof new Object(5);", "object"), + ("var r = new Object(true).valueOf();", "true"), + ( + "var r = Object.prototype.toString.call(undefined);", + "[object Undefined]", + ), + ( + "var r = Object.prototype.toString.call(null);", + "[object Null]", + ), + ( + "var r = Object.prototype.toString.call(1);", + "[object Number]", + ), + ( + "var r = Object.prototype.toString.call('s');", + "[object String]", + ), + ( + "var r = Object.prototype.toString.call(true);", + "[object Boolean]", + ), + ( + "var r = Object.prototype.toString.call(function () {});", + "[object Function]", + ), + ( + "var r = Object.prototype.toString.call({});", + "[object Object]", + ), + ("var r = ({}).hasOwnProperty('a');", "false"), + ("var r = ({ a: 1 }).hasOwnProperty('a');", "true"), + ("var r = ({ a: 1 }).hasOwnProperty('toString');", "false"), + // ToObject('abc') is a String object with an own `length`. + ( + "var r = Object.prototype.hasOwnProperty.call('abc', 'length');", + "true", + ), + ( + "var r = Object.prototype.hasOwnProperty.call(new String('abc'), 'length');", + "true", + ), + ("var o = {}; var r = o.valueOf() === o;", "true"), + ("var r = typeof Object.prototype.valueOf.call(2);", "object"), + ( + "var r; try { Object.prototype.valueOf.call(null); } catch (e) { r = e.name; }", + "TypeError", + ), + ("var r = Object.prototype.constructor === Object;", "true"), + ( + "var r = Object.getPrototypeOf({}) === Object.prototype;", + "true", + ), + ("var r = Object.getPrototypeOf(Object.prototype);", "null"), + ( + "var r = Object.getPrototypeOf(1) === Number.prototype;", + "true", + ), + ( + "var r = Object.getPrototypeOf('s') === String.prototype;", + "true", + ), + ( + "var r; try { Object.getPrototypeOf(null); } catch (e) { r = e.name; }", + "TypeError", + ), + ]) +} + +#[test] +fn object_keys_create_and_define_property() -> Result<(), String> { + check(&[ + ( + "var k = Object.keys({ b: 1, a: 2 }); var r = k.length + k[0] + k[1];", + "2ba", + ), + // Integer keys come first, ascending. + ( + "var k = Object.keys({ b: 1, 10: 2, a: 3, 2: 4 }); var r = k[0] + k[1] + k[2] + k[3];", + "210ba", + ), + ("var r = Object.keys('ab').length;", "0"), + ( + "var o = Object.create({ p: 1 }); o.q = 2; var k = Object.keys(o); var r = k.length + k[0];", + "1q", + ), + ( + "var p = {}; var o = Object.create(p); var r = Object.getPrototypeOf(o) === p;", + "true", + ), + ( + "var o = Object.create(null); var r = Object.getPrototypeOf(o);", + "null", + ), + ( + "var r; try { Object.create(1); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "var o = Object.create({}, { a: { value: 1, enumerable: true }, b: { value: 2 } }); var k = Object.keys(o); var r = o.a + o.b + k.length;", + "4", + ), + // A new property takes `false` for each absent attribute. + ( + "var o = {}; Object.defineProperty(o, 'x', { value: 5 }); o.x = 6; var r = o.x + ',' + Object.keys(o).length + ',' + o.hasOwnProperty('x');", + "5,0,true", + ), + ( + "var o = {}; var r = Object.defineProperty(o, 'x', { value: 1 }) === o;", + "true", + ), + ( + "'use strict'; var o = {}; Object.defineProperty(o, 'x', { value: 5 }); var r; try { o.x = 6; } catch (e) { r = e.name + o.x; }", + "TypeError5", + ), + ( + "var o = {}; Object.defineProperty(o, 'x', { value: 1, writable: true, configurable: true, enumerable: true }); Object.defineProperty(o, 'x', { writable: false }); o.x = 2; var r = o.x + ',' + Object.keys(o)[0];", + "1,x", + ), + // A configurable property may be redefined, even to a new value. + ( + "var o = { a: 1 }; Object.defineProperty(o, 'a', { value: 9, enumerable: false }); var r = o.a + ',' + Object.keys(o).length;", + "9,0", + ), + // A non-configurable one may not change. + ( + "var o = {}; Object.defineProperty(o, 'x', { value: 1 }); var r; try { Object.defineProperty(o, 'x', { value: 2 }); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "var o = {}; Object.defineProperty(o, 'x', { value: 1 }); var r; try { Object.defineProperty(o, 'x', { configurable: true }); } catch (e) { r = e.name; }", + "TypeError", + ), + // The same value is not a change. + ( + "var o = {}; Object.defineProperty(o, 'x', { value: 1 }); Object.defineProperty(o, 'x', { value: 1 }); var r = o.x;", + "1", + ), + ( + "var r; try { Object.defineProperty(1, 'x', {}); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "var r; try { Object.defineProperty({}, 'x', 1); } catch (e) { r = e.name; }", + "TypeError", + ), + ]) +} + +#[test] +fn function_prototype_call_apply_bind() -> Result<(), String> { + check(&[ + ( + "function f(a, b) { return this.k + a + b; } var r = f.call({ k: 1 }, 2, 3);", + "6", + ), + ( + "function f() { return typeof this; } var r = f.call(1);", + "object", + ), + ( + "function f() { 'use strict'; return typeof this; } var r = f.call(1);", + "number", + ), + ( + "function f() { 'use strict'; return this; } var r = f.call(undefined);", + "undefined", + ), + ( + "var r; try { Function.prototype.call.call({}); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "function f(a, b) { return a * b; } var r = f.apply(null, { length: 2, 0: 6, 1: 7 });", + "42", + ), + ( + "function f(a, b) { return a - b; } var r = f.apply(null, { length: 2, 0: 10, 1: 4 });", + "6", + ), + ( + "function f() { return 1; } var r = f.apply(null) + f.apply(null, undefined) + f.apply(null, null);", + "3", + ), + ( + "function f() { return 1; } var r; try { f.apply(null, 1); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "function f(a, b) { return this.k + a + b; } var g = f.bind({ k: 1 }, 2); var r = g(3);", + "6", + ), + ( + "function f(a, b, c) { return a + b + c; } var g = f.bind(null, 1).bind(null, 2); var r = g(3);", + "6", + ), + ( + "function f(a, b, c) {} var r = f.bind(null, 1).length;", + "2", + ), + ( + "function f(a) {} var r = f.bind(null, 1, 2, 3).length;", + "0", + ), + ("function foo() {} var r = foo.bind().name;", "bound foo"), + ("var r = (function () {}).bind().name;", "bound "), + ( + "function F(x) { this.x = x; } var B = F.bind(null, 5); var o = new B(); var r = o.x + ',' + (Object.getPrototypeOf(o) === F.prototype);", + "5,true", + ), + ( + "function f() { return this; } var r = f.bind(1)() == 1;", + "true", + ), + ( + "var r; try { Function.prototype.bind.call(1); } catch (e) { r = e.name; }", + "TypeError", + ), + ("var r = typeof Function;", "function"), + ( + "var r = Function.prototype.call.length + ',' + Function.prototype.apply.length + ',' + Function.prototype.bind.length;", + "1,2,1", + ), + ( + "function f() {} var r = Object.getPrototypeOf(f) === Function.prototype;", + "true", + ), + ]) +} + +#[test] +fn boolean_builtin() -> Result<(), String> { + check(&[ + ( + "var r = Boolean(0) + ',' + Boolean('x') + ',' + Boolean() + ',' + Boolean({});", + "false,true,false,true", + ), + ("var r = typeof new Boolean(false);", "object"), + ("var r = new Boolean(false) ? 'truthy' : 'falsy';", "truthy"), + ("var r = new Boolean(true).valueOf();", "true"), + ("var r = new Boolean(0).toString();", "false"), + ("var r = true.toString();", "true"), + ("var r = Boolean.prototype.valueOf.call(false);", "false"), + ( + "var r; try { Boolean.prototype.toString.call(1); } catch (e) { r = e.name; }", + "TypeError", + ), + ("var r = Boolean.prototype.valueOf();", "false"), + ( + "var r = Object.prototype.toString.call(new Boolean(true));", + "[object Boolean]", + ), + ("var r = new Boolean(1) + 1;", "2"), + ]) +} + +#[test] +fn number_builtin() -> Result<(), String> { + check(&[ + ( + "var r = Number('42') + ',' + Number('') + ',' + Number() + ',' + Number(null) + ',' + Number(undefined);", + "42,0,0,0,NaN", + ), + ( + "var r = Number({ valueOf: function () { return 4; } });", + "4", + ), + ("var r = typeof new Number(1) + new Number(2);", "object2"), + ("var r = new Number(3).valueOf();", "3"), + ( + "var r = (255).toString(16) + ',' + (255).toString(2) + ',' + (-255).toString(36) + ',' + (0.5).toString(2);", + "ff,11111111,-73,0.1", + ), + ( + "var r = (1e21).toString() + ',' + (123.456).toString() + ',' + (-0).toString();", + "1e+21,123.456,0", + ), + ("var r = (35).toString(36) + (10).toString(10);", "z10"), + ( + "var r; try { (1).toString(1); } catch (e) { r = e.name; }", + "RangeError", + ), + ( + "var r; try { (1).toString(37); } catch (e) { r = e.name; }", + "RangeError", + ), + ( + "var r = (1.005).toFixed(2) + ',' + (123.456).toFixed(1) + ',' + (0).toFixed(2) + ',' + (1e21).toFixed(2) + ',' + (2.5).toFixed();", + "1.00,123.5,0.00,1e+21,3", + ), + ( + "var r = NaN.toFixed(2) + ',' + (-1.5).toFixed(0);", + "NaN,-2", + ), + ( + "var r; try { (1).toFixed(101); } catch (e) { r = e.name; }", + "RangeError", + ), + ( + "var r; try { (1).toFixed(-1); } catch (e) { r = e.name; }", + "RangeError", + ), + ( + "var r; try { Number.prototype.toString.call('1'); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "var r = Number.MAX_SAFE_INTEGER + ',' + Number.MIN_SAFE_INTEGER + ',' + Number.EPSILON;", + "9007199254740991,-9007199254740991,2.220446049250313e-16", + ), + ( + "var r = Number.MAX_VALUE + ',' + Number.MIN_VALUE;", + "1.7976931348623157e+308,5e-324", + ), + ( + "var r = Number.POSITIVE_INFINITY + ',' + Number.NEGATIVE_INFINITY + ',' + Number.NaN;", + "Infinity,-Infinity,NaN", + ), + ( + "Number.MAX_VALUE = 1; var r = Number.MAX_VALUE > 1;", + "true", + ), + ( + "var r = Number.isInteger(5) + ',' + Number.isInteger(5.5) + ',' + Number.isInteger('5') + ',' + Number.isInteger(Infinity);", + "true,false,false,false", + ), + ( + "var r = Number.isNaN(NaN) + ',' + Number.isNaN('x') + ',' + Number.isFinite(1) + ',' + Number.isFinite('1');", + "true,false,true,false", + ), + ( + "var r = Number.isSafeInteger(9007199254740991) + ',' + Number.isSafeInteger(9007199254740992);", + "true,false", + ), + ("var r = Number.parseFloat === parseFloat;", "true"), + ("var r = Number.parseInt === parseInt;", "true"), + ("var r = (5).constructor === Number;", "true"), + ("var r = Number.prototype.valueOf();", "0"), + ]) +} + +#[test] +fn string_builtin() -> Result<(), String> { + check(&[ + ( + "var r = String(123) + String(true) + String(null) + String(undefined) + String();", + "123truenullundefined", + ), + ( + "var r = String({ toString: function () { return 'T'; } });", + "T", + ), + ( + "var r = typeof new String('a') + new String('bc').length;", + "object2", + ), + ("var r = new String('a') + 'b';", "ab"), + ( + "var r = new String('xy').valueOf() + new String('z').toString();", + "xyz", + ), + ( + "var r = 'abc'.charAt(1) + '|' + 'abc'.charAt(5) + '|' + 'abc'.charAt(-1) + '|' + 'abc'.charAt();", + "b|||a", + ), + ( + "var r = 'abc'.charCodeAt(1) + ',' + 'abc'.charCodeAt(3) + ',' + 'abc'.charCodeAt(-1);", + "98,NaN,NaN", + ), + ( + "var r = 'hello'.indexOf('l') + ',' + 'hello'.indexOf('l', 3) + ',' + 'hello'.indexOf('z') + ',' + 'hello'.indexOf('') + ',' + 'hello'.indexOf('', 9) + ',' + 'hello'.indexOf('lo');", + "2,3,-1,0,5,3", + ), + ( + "var r = 'hello'.indexOf('hello!') + ',' + 'hello'.indexOf('o', -5) + ',' + 'abc'.indexOf(undefined);", + "-1,4,-1", + ), + ( + "var r = 'hello'.slice(1, 3) + '|' + 'hello'.slice(-3) + '|' + 'hello'.slice(2, -1) + '|' + 'hello'.slice(3, 1) + '|' + 'hello'.slice();", + "el|llo|ll||hello", + ), + ( + "var r = 'hello'.substring(1, 3) + '|' + 'hello'.substring(3, 1) + '|' + 'hello'.substring(-2, 2) + '|' + 'hello'.substring(2) + '|' + 'hello'.substring(NaN, 2);", + "el|el|he|llo|he", + ), + ( + "var r = (123).toString().charAt(1) + true.toString().charAt(0);", + "2t", + ), + ( + "var r = String.fromCharCode(72, 105) + String.fromCharCode(65601).charCodeAt(0);", + "Hi65", + ), + ( + "var r; try { String.prototype.charAt.call(null, 0); } catch (e) { r = e.name; }", + "TypeError", + ), + ("var r = String.prototype.charAt.call(12, 1);", "2"), + ( + "var r; try { String.prototype.toString.call(1); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "var r = Object.prototype.toString.call(new String('a'));", + "[object String]", + ), + ("var r = 'abc'.length + 'abc'[1];", "3b"), + ("var r = ''.constructor === String;", "true"), + ("var r = 'a' < new String('b');", "true"), + ("var r = String.prototype.toString();", ""), + ]) +} + +#[test] +fn math_builtin() -> Result<(), String> { + check(&[ + ( + "var r = Math.abs(-3) + ',' + Math.abs('-2.5') + ',' + 1 / Math.abs(-0) + ',' + Math.abs(NaN);", + "3,2.5,Infinity,NaN", + ), + ( + "var r = Math.floor(1.5) + ',' + Math.floor(-1.5) + ',' + Math.ceil(1.2) + ',' + Math.ceil(-1.2) + ',' + 1 / Math.ceil(-0.5);", + "1,-2,2,-1,-Infinity", + ), + ( + "var r = Math.trunc(1.9) + ',' + Math.trunc(-1.9) + ',' + 1 / Math.trunc(-0.9);", + "1,-1,-Infinity", + ), + ( + "var r = Math.round(2.5) + ',' + Math.round(-2.5) + ',' + Math.round(2.4) + ',' + Math.round(-2.6) + ',' + Math.round(0.49999999999999994) + ',' + 1 / Math.round(-0.4) + ',' + 1 / Math.round(-0.5) + ',' + Math.round(1e308 * 10);", + "3,-2,2,-3,0,-Infinity,-Infinity,Infinity", + ), + ( + "var r = Math.round(4503599627370495.5) + ',' + Math.round(-4503599627370495.5);", + "4503599627370496,-4503599627370495", + ), + ( + "var r = Math.sign(-5) + ',' + Math.sign(5) + ',' + 1 / Math.sign(-0) + ',' + Math.sign(0) + ',' + Math.sign(NaN);", + "-1,1,-Infinity,0,NaN", + ), + ( + "var r = Math.min() + ',' + Math.max() + ',' + Math.min(3, 1, 2) + ',' + Math.max(3, 1, 2) + ',' + Math.max(1, NaN) + ',' + Math.min(NaN, 1);", + "Infinity,-Infinity,1,3,NaN,NaN", + ), + ( + "var r = 1 / Math.min(0, -0) + ',' + 1 / Math.min(-0, 0) + ',' + 1 / Math.max(0, -0) + ',' + 1 / Math.max(-0, 0) + ',' + 1 / Math.max(-0, -0);", + "-Infinity,-Infinity,Infinity,Infinity,-Infinity", + ), + ( + "var r = Math.max('7', 3) + ',' + Math.min({ valueOf: function () { return 2; } }, 5);", + "7,2", + ), + ( + "var r = Math.pow(NaN, 0) + ',' + Math.pow(1, Infinity) + ',' + Math.pow(-8, 1 / 3) + ',' + Math.pow(0, -1);", + "1,NaN,NaN,Infinity", + ), + ( + "var r = Math.sqrt(16) + ',' + Math.sqrt(2) + ',' + Math.sqrt(-1) + ',' + 1 / Math.sqrt(-0) + ',' + Math.sqrt(Infinity);", + "4,1.4142135623730951,NaN,-Infinity,Infinity", + ), + ( + "var r = Math.PI + ',' + Math.E + ',' + Math.SQRT2 + ',' + Math.LN2;", + "3.141592653589793,2.718281828459045,1.4142135623730951,0.6931471805599453", + ), + ("Math.PI = 3; var r = Math.PI > 3.14;", "true"), + ( + "var r = Object.prototype.toString.call(Math);", + "[object Object]", + ), + ( + "var r = Math.max.length + ',' + Math.pow.length + ',' + Math.abs.length;", + "2,2,1", + ), + ]) +} + +/// `Math.pow` is the C library's `pow`, whose last digit the standard leaves +/// to the implementation (§21.3.2.26†); Miri deliberately perturbs it, so +/// the exact-value cases do not run there. +#[test] +#[cfg_attr(miri, ignore = "Miri perturbs libm results such as pow")] +fn math_pow_exact_values() -> Result<(), String> { + check(&[( + "var r = Math.pow(2, 10) + ',' + Math.pow(2, -1) + ',' + Math.pow(10, 2);", + "1024,0.5,100", + )]) +} + +#[test] +fn global_functions() -> Result<(), String> { + check(&[ + ("var r = globalThis === this;", "true"), + ("var r = typeof globalThis.Math;", "object"), + ( + "var r = isNaN('x') + ',' + isNaN('1') + ',' + isNaN(undefined) + ',' + isNaN({}) + ',' + isNaN(null);", + "true,false,true,true,false", + ), + ( + "var r = isFinite('1') + ',' + isFinite(Infinity) + ',' + isFinite(NaN) + ',' + isFinite('x');", + "true,false,false,false", + ), + ( + "var r = parseFloat('3.14abc') + ',' + parseFloat(' -1e3x') + ',' + parseFloat('.5') + ',' + parseFloat('5.') + ',' + parseFloat('abc') + ',' + parseFloat('Infinityx') + ',' + parseFloat('-Infinity');", + "3.14,-1000,0.5,5,NaN,Infinity,-Infinity", + ), + ( + "var r = parseFloat('1e') + ',' + parseFloat('1e+') + ',' + parseFloat('1e+2') + ',' + 1 / parseFloat('-0') + ',' + parseFloat('0x10') + ',' + parseFloat('+.') + ',' + parseFloat('');", + "1,1,100,-Infinity,0,NaN,NaN", + ), + ( + "var r = parseInt('42px') + ',' + parseInt(' -7') + ',' + parseInt('+8') + ',' + parseInt('0x1F') + ',' + parseInt('12', 8) + ',' + parseInt('z', 36) + ',' + parseInt('') + ',' + parseInt('x');", + "42,-7,8,31,10,35,NaN,NaN", + ), + ( + "var r = parseInt('0x10', 16) + ',' + parseInt('0x10', 10) + ',' + parseInt('11', 2) + ',' + parseInt('11', 1) + ',' + parseInt('11', 37) + ',' + parseInt('10', 0) + ',' + parseInt('0X', 16);", + "16,0,3,NaN,NaN,10,NaN", + ), + ( + "var r = 1 / parseInt('-0') + ',' + parseInt('9007199254740993') + ',' + parseInt('123456789012345678901234567890') + ',' + parseInt(0.0000005) + ',' + parseInt('ff', 16.9);", + "-Infinity,9007199254740992,1.2345678901234568e+29,5,255", + ), + ( + "var r = parseInt('ffffffffffffffffffffffffffffffffff', 16) > 1e40;", + "true", + ), + ]) +} + +#[test] +fn wrapper_objects_and_sloppy_this() -> Result<(), String> { + check(&[ + ( + "function f() { return typeof this; } var r = f.call('s') + f.call(true) + f.call(1);", + "objectobjectobject", + ), + ( + "function f() { return this.length; } var r = f.call('abc');", + "3", + ), + ( + "String.prototype.twice = function () { return this + this; }; var r = 'ab'.twice();", + "abab", + ), + ( + "Number.prototype.inc = function () { return this + 1; }; var r = (4).inc();", + "5", + ), + ( + "Boolean.prototype.yes = function () { return typeof this; }; var r = true.yes();", + "object", + ), + ("var r = 'abc'.nothing;", "undefined"), + ("var r = (5).nothing + true.nothing;", "NaN"), + ]) +} + +#[test] +fn natives_work_without_the_builtins_installed() -> Result<(), String> { + // The VM alone still throws plain errors with `name` and `message`. + let c = cps("var r; try { null.x; } catch (e) { r = e.name + ':' + e.constructor; }"); + let ast = parse(&c, SourceGoal::Script).map_err(|_| "parse".to_string())?; + let p = compile_script(&ast, &c).map_err(|e| e.to_string())?; + let mut vm = Vm::new(config()).map_err(|e| format!("{e:?}"))?; + let script = vm.load(&p).map_err(|e| format!("{e:?}"))?; + vm.run(&script).map_err(|e| format!("{e:?}"))?; + let g = vm.global(); + let r = vm.get_named(g, "r").map_err(|e| format!("{e:?}"))?; + assert_eq!(vm.string_text(r).as_deref(), Some("TypeError:undefined")); + Ok(()) +} diff --git a/crates/clazel-js-builtins/tests/conformance_builtins.rs b/crates/clazel-js-builtins/tests/conformance_builtins.rs new file mode 100644 index 000000000..596b71060 --- /dev/null +++ b/crates/clazel-js-builtins/tests/conformance_builtins.rs @@ -0,0 +1,497 @@ +//! Test262 through the interpreter with the built-ins of `clazel-js-builtins` +//! installed: the scripts the compiler accepts, run in a fresh [`Vm`] each, +//! with a minimal native harness (item 7.9, first group). It is the runner of +//! `clazel-js-vm`'s `conformance_interp.rs` with `install` called first, so +//! the two counts compare; the difference is what the built-ins unlock. +//! +//! The suite is every row of the generated Test262 table (the same table the +//! compiler's test reads), and each test lands in exactly one class: +//! +//! - **pass**: it ran in every mode it asks for (sloppy and strict, or the one +//! its flags name) and finished without throwing, or threw the error type +//! its `negative:` block names; +//! - **xfail**, an expected failure with a named reason (printed, with the +//! count of each): the test needs something this slice does not have; or +//! - **fail**: it ran and went wrong (an assertion failed, an exception other +//! than the expected one, the VM reported an internal error or ran out of +//! its step budget). This must be zero, and `cargo xtask verify` fails the +//! run otherwise. +//! +//! The harness is native, as phase-7.md asks of the shell's `$262` ("built +//! from public calls only"): `assert`, `assert.sameValue`, +//! `assert.notSameValue`, `assert.throws` (which compares `thrown.constructor` +//! with the expected constructor, as `assert.js` does), and a `$262` object +//! with `global` and `gc`; its other members report "not yet" when called. A +//! test that includes any other harness file (`propertyHelper.js`, +//! `compareArray.js`, ...) is an expected failure: they need `Array`, +//! `Symbol` or accessor properties, later groups of 7.9. +//! +//! A script that throws an uncaught `ReferenceError` for a name that is not +//! defined is classed by the name ("global X is missing"): the built-ins +//! (`Object`, `Array`, `Symbol`, ...) are 7.9, and a test that reaches one is +//! waiting for it. A test that catches the error itself is judged on what it +//! does next. +//! +//! The counts are in units of tests, not runs. + +use clazel_js_heap::value::{Decoded, Value}; +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; +use clazel_js_vm::bytecode::Program; +use clazel_js_vm::compile::{CompileError, compile_script}; +use clazel_js_vm::interp::{Abrupt, Config, Vm}; +use std::collections::BTreeMap; +use std::path::PathBuf; +use std::sync::OnceLock; + +/// A test's row in the generated table. +struct Case { + path: &'static str, + flags: &'static [&'static str], + features: &'static [&'static str], + includes: &'static [&'static str], + negative: Option<(&'static str, &'static str)>, + expect: Expect, +} + +/// Whether a test is expected to pass, and if not why. +enum Expect { + Run, + Fail(&'static str), +} + +include!("../../clazel-js-lexer/tests/generated/test262.rs"); + +fn root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../clazel-js-lexer/tests/data/test262/test") +} + +/// The most steps (back-edges and calls) one test run may take. +const BUDGET: u64 = 2_000_000; + +// ----------------------------------------------------------------------- +// The native harness +// ----------------------------------------------------------------------- + +/// Throws what a failed assertion throws: an object with `name` +/// `Test262Error` and the message. +fn test262_error(vm: &mut Vm, msg: &str) -> Abrupt { + let make = |vm: &mut Vm| -> Result { + let o = vm.new_object()?; + let n = vm.new_string("Test262Error")?; + let m = vm.new_string(msg)?; + vm.define_property(o, "name", n)?; + vm.define_property(o, "message", m)?; + Ok(o) + }; + match make(vm) { + Ok(o) => Abrupt::Throw(o), + Err(e) => e, + } +} + +/// The message argument of an assertion, as text. +fn message(vm: &mut Vm, v: Option<&Value>) -> String { + match v { + Some(v) if *v != Value::UNDEFINED => vm.describe(*v), + _ => String::new(), + } +} + +/// SameValue, §7.2.10 (strings by content, Numbers by value with NaN equal to +/// NaN and `+0` distinct from `-0`). +fn same_value(vm: &Vm, a: Value, b: Value) -> bool { + use clazel_js_vm::numeric::num; + if let (Some(x), Some(y)) = (num(a), num(b)) { + let (x, y) = (x.to_f64(), y.to_f64()); + return if x.is_nan() || y.is_nan() { + x.is_nan() && y.is_nan() + } else { + x.to_bits() == y.to_bits() + }; + } + if let (Some(x), Some(y)) = (vm.heap().string(a), vm.heap().string(b)) { + return x == y; + } + a == b +} + +fn assert_true(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + if args.first() == Some(&Value::TRUE) { + return Ok(Value::UNDEFINED); + } + let m = message(vm, args.get(1)); + Err(test262_error( + vm, + &format!("Expected true but got something else. {m}"), + )) +} + +fn assert_same_value(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let (a, b) = ( + args.first().copied().unwrap_or(Value::UNDEFINED), + args.get(1).copied().unwrap_or(Value::UNDEFINED), + ); + if same_value(vm, a, b) { + return Ok(Value::UNDEFINED); + } + let (da, db) = (vm.describe(a), vm.describe(b)); + let m = message(vm, args.get(2)); + Err(test262_error( + vm, + &format!("Expected SameValue({da}, {db}) to be true. {m}"), + )) +} + +fn assert_not_same_value(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let (a, b) = ( + args.first().copied().unwrap_or(Value::UNDEFINED), + args.get(1).copied().unwrap_or(Value::UNDEFINED), + ); + if !same_value(vm, a, b) { + return Ok(Value::UNDEFINED); + } + let da = vm.describe(a); + let m = message(vm, args.get(2)); + Err(test262_error( + vm, + &format!("Expected SameValue({da}, {da}) to be false. {m}"), + )) +} + +fn assert_throws(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let ctor = args.first().copied().unwrap_or(Value::UNDEFINED); + let f = args.get(1).copied().unwrap_or(Value::UNDEFINED); + match vm.call(f, Value::UNDEFINED, &[]) { + Ok(_) => Err(test262_error( + vm, + "Expected an exception, but none was thrown", + )), + Err(Abrupt::Throw(e)) => { + // assert.js: `thrown.constructor !== expectedErrorConstructor`. + let same_ctor = vm.get_named(e, "constructor")? == ctor && ctor != Value::UNDEFINED; + if same_ctor { + Ok(Value::UNDEFINED) + } else { + let d = vm.describe(e); + Err(test262_error( + vm, + &format!("Expected a different error, got {d}"), + )) + } + } + Err(other) => Err(other), + } +} + +fn not_yet(_vm: &mut Vm, _this: Value, _args: &[Value]) -> Result { + Err(Abrupt::NotYet("$262 member".into())) +} + +fn ignore(_vm: &mut Vm, _this: Value, _args: &[Value]) -> Result { + Ok(Value::UNDEFINED) +} + +/// `harness/sta.js`, compiled once: it defines `Test262Error`. +fn sta_program() -> Option<&'static Program> { + static STA: OnceLock> = OnceLock::new(); + STA.get_or_init(|| { + let path = root().join("../harness/sta.js"); + let text = std::fs::read_to_string(path).ok()?; + let src: Vec = text.chars().map(u32::from).collect(); + let ast = parse(&src, SourceGoal::Script).ok()?; + compile_script(&ast, &src).ok() + }) + .as_ref() +} + +/// Installs `assert` (unless `raw`), `print` and `$262`, and runs the real +/// `sta.js` (unless `raw`). +fn install_harness(vm: &mut Vm, raw: bool) -> Result<(), Abrupt> { + if !raw { + let sta = sta_program().ok_or(Abrupt::Internal("sta.js does not compile"))?; + let script = vm.load(sta)?; + vm.run(&script)?; + let assert = vm.new_native("assert", 2, assert_true)?; + for (name, len, f) in [ + ( + "sameValue", + 3, + assert_same_value as clazel_js_vm::interp::NativeFn, + ), + ("notSameValue", 3, assert_not_same_value), + ("throws", 3, assert_throws), + ] { + let n = vm.new_native(name, len, f)?; + vm.define_property(assert, name, n)?; + } + vm.define_global("assert", assert)?; + } + let print = vm.new_native("print", 1, ignore)?; + vm.define_global("print", print)?; + let host = vm.new_object()?; + let g = vm.global(); + vm.define_property(host, "global", g)?; + for name in ["gc", "createRealm", "evalScript", "detachArrayBuffer"] { + let f = vm.new_native(name, 0, if name == "gc" { ignore } else { not_yet })?; + vm.define_property(host, name, f)?; + } + vm.define_global("$262", host) +} + +// ----------------------------------------------------------------------- +// Running and classifying +// ----------------------------------------------------------------------- + +/// What happened to one run of a test. +enum Outcome { + Pass, + /// An expected failure, with its category. + Xfail(String), + /// An unexpected failure, with what happened. + Fail(String), +} + +/// The text of a thrown error's `name` and `message`, if it is an object. +fn thrown(vm: &mut Vm, v: Value) -> (Option, Option) { + if !matches!(v.decode(), Decoded::Ref(_)) { + return (None, None); + } + let name = vm.get_named(v, "name").ok().and_then(|n| vm.string_text(n)); + // An error made by `new Test262Error` has no own `name`: its constructor's + // name stands for it. + let name = name.or_else(|| { + let c = vm.get_named(v, "constructor").ok()?; + let n = vm.get_named(c, "name").ok()?; + vm.string_text(n) + }); + let msg = vm + .get_named(v, "message") + .ok() + .and_then(|n| vm.string_text(n)); + (name, msg) +} + +/// Runs one source in a new VM and classifies the result. +fn run_one(case: &Case, src: &[u32], goal: SourceGoal) -> Outcome { + let Ok(ast) = parse(src, goal) else { + return Outcome::Fail("does not parse".into()); + }; + let program = match compile_script(&ast, src) { + Ok(p) => p, + Err(CompileError::NotYetCompiled { feature, .. }) => { + return Outcome::Xfail(format!("the compiler does not yet compile {feature}")); + } + Err(e) => return Outcome::Fail(format!("compile error: {e}")), + }; + let Ok(mut vm) = Vm::new(Config::default()) else { + return Outcome::Fail("could not make a VM".into()); + }; + if let Err(e) = clazel_js_builtins::install(&mut vm) { + return Outcome::Fail(format!("install: {e:?}")); + } + if let Err(e) = install_harness(&mut vm, case.flags.contains(&"raw")) { + return Outcome::Fail(format!("harness: {e:?}")); + } + let script = match vm.load(&program) { + Ok(s) => s, + Err(Abrupt::NotYet(what)) => { + return Outcome::Xfail(format!("the VM does not yet load {what}")); + } + Err(e) => return Outcome::Fail(format!("load: {e:?}")), + }; + vm.set_budget(BUDGET); + let result = vm.run(&script); + let negative = case.negative.filter(|(phase, _)| *phase == "runtime"); + match result { + Ok(_) => match negative { + None => Outcome::Pass, + Some((_, ty)) => Outcome::Fail(format!("expected {ty}, nothing was thrown")), + }, + Err(Abrupt::NotYet(what)) => { + Outcome::Xfail(format!("the VM does not yet implement {what}")) + } + Err(Abrupt::Throw(e)) => { + let (name, msg) = thrown(&mut vm, e); + if let Some((_, ty)) = negative + && name.as_deref() == Some(ty) + { + return Outcome::Pass; + } + if name.as_deref() == Some("ReferenceError") + && let Some(m) = &msg + && let Some(var) = m.strip_suffix(" is not defined") + { + return Outcome::Xfail(format!("global {var} is missing (a built-in, 7.9)")); + } + // `assert.throws` reports a missing global the same way, inside + // its own error: "Expected a different error, got ReferenceError: + // eval is not defined" (the compiler's third slice, #230, compiles + // the RegExp literal tests that reach `eval` this way). + if name.as_deref() == Some("Test262Error") + && let Some(m) = &msg + && let Some((_, got)) = m.split_once("got ReferenceError: ") + && let Some(var) = got.strip_suffix(" is not defined") + { + return Outcome::Xfail(format!("global {var} is missing (a built-in, 7.9)")); + } + if name.as_deref() == Some("TypeError") + && let Some(m) = &msg + && m == "undefined is not a function" + { + return Outcome::Xfail( + "calls a method that is not there (a built-in of 7.9: Function.prototype.call, Object.prototype.toString, ...)" + .into(), + ); + } + match negative { + Some((_, ty)) => Outcome::Fail(format!("expected {ty}, got {}", vm.describe(e))), + None => Outcome::Fail(vm.describe(e)), + } + } + Err(Abrupt::Terminated) => Outcome::Fail("step budget exhausted".into()), + Err(e) => Outcome::Fail(format!("{e:?}")), + } +} + +fn bump(m: &mut BTreeMap, k: String) { + let e = m.entry(k).or_insert(0); + *e = e.saturating_add(1); +} + +/// The runs of a test: its goal and whether to prefix a Use Strict Directive. +fn runs(case: &Case) -> Vec<(SourceGoal, bool)> { + let has = |f: &str| case.flags.contains(&f); + if has("raw") || has("noStrict") { + vec![(SourceGoal::Script, false)] + } else if has("onlyStrict") { + vec![(SourceGoal::Script, true)] + } else { + vec![(SourceGoal::Script, false), (SourceGoal::Script, true)] + } +} + +/// The harness files the native harness stands in for. +const NATIVE_INCLUDES: [&str; 2] = ["assert.js", "sta.js"]; + +/// Tests that run and give a different result for a known reason, each with +/// its reason: expected failures. +const KNOWN_GAPS: &[(&str, &str)] = &[ + ( + "language/identifier-resolution/assign-to-global-undefined.js", + "strict assignment to an unresolvable reference throws before the right-hand side runs (§13.15.2 step 1.a, ResolveBinding first); SetGlobal checks when it runs, after it, and the op list has no resolve-first op", + ), + ( + "language/comments/S7.4_A5.js", + "calls eval, which is not there yet (a built-in, 7.9); the test's own catch turns the ReferenceError into a bare Test262Error, so the runner cannot class it by name (reached since array literals run, #290)", + ), +]; + +/// Classifies a test that is not run, or `None` if it should be. +fn not_run(case: &Case) -> Option { + if let Some((_, why)) = KNOWN_GAPS.iter().find(|(p, _)| *p == case.path) { + return Some((*why).to_string()); + } + if let Expect::Fail(reason) = case.expect { + return Some(format!("expected failure of the table: {reason}")); + } + if matches!(case.negative, Some((phase, _)) if phase == "parse" || phase == "early" || phase == "resolution") + { + return Some("negative parse/early test (the parser's evidence, not run)".into()); + } + if case.flags.contains(&"module") { + return Some("module (the compiler's third slice, an open pull request)".into()); + } + if case.flags.contains(&"async") { + return Some("async test (Promises and the job queue, 7.9)".into()); + } + if case.path.starts_with("harness/") { + return Some("test of the real harness files (replaced by the native harness)".into()); + } + case.includes + .iter() + .find(|i| !NATIVE_INCLUDES.contains(i)) + .map(|i| format!("includes {i} (needs built-ins of later groups of 7.9)")) +} + +#[test] +#[cfg_attr(miri, ignore = "reads files; Miri runs the other tests")] +fn test262_through_the_interpreter() -> Result<(), String> { + assert_eq!(TREE_SHA256.len(), 64); + let (mut pass, mut xfail, mut fail) = (0usize, 0usize, 0usize); + let mut categories: BTreeMap = BTreeMap::new(); + let mut failures: Vec = Vec::new(); + for case in TEST262 { + if let Some(reason) = not_run(case) { + xfail = xfail.saturating_add(1); + bump(&mut categories, reason); + continue; + } + let text = std::fs::read_to_string(root().join(case.path)) + .map_err(|e| format!("{}: {e}", case.path))?; + let body: Vec = text.chars().map(u32::from).collect(); + let mut worst = Outcome::Pass; + for (goal, strict) in runs(case) { + let mut src: Vec = Vec::new(); + if strict { + src.extend("\"use strict\";\n".chars().map(u32::from)); + } + src.extend(body.iter().copied()); + let out = run_one(case, &src, goal); + let mode = if strict { "strict" } else { "sloppy" }; + worst = match (worst, out) { + (Outcome::Fail(m), _) => Outcome::Fail(m), + (_, Outcome::Fail(m)) => Outcome::Fail(format!("{mode}: {m}")), + (Outcome::Xfail(m), _) => Outcome::Xfail(m), + (_, Outcome::Xfail(m)) => Outcome::Xfail(m), + (Outcome::Pass, Outcome::Pass) => Outcome::Pass, + }; + } + match worst { + Outcome::Pass => pass = pass.saturating_add(1), + Outcome::Xfail(reason) => { + xfail = xfail.saturating_add(1); + bump(&mut categories, reason); + } + Outcome::Fail(m) => { + fail = fail.saturating_add(1); + failures.push(format!("{} {:?}: {m}", case.path, case.features)); + } + } + } + let total = TEST262.len(); + println!( + "clazel-conformance: suite=\"Test262 through the interpreter with the first built-ins\" pass={pass} xfail={xfail} fail={fail} total={total}" + ); + // Group the categories by their stem, so the print stays readable. + let mut stems: BTreeMap = BTreeMap::new(); + for (reason, n) in &categories { + let stem = if reason.starts_with("global ") { + "a global that is a built-in is missing (7.9)".to_string() + } else { + reason.clone() + }; + let e = stems.entry(stem).or_insert(0); + *e = e.saturating_add(*n); + } + let mut by_count: Vec<_> = stems.iter().collect(); + by_count.sort_by(|a, b| b.1.cmp(a.1).then(a.0.cmp(b.0))); + for (reason, n) in by_count { + println!(" expected failure: {n:6} {reason}"); + } + let mut globals: Vec<_> = categories + .iter() + .filter(|(r, _)| r.starts_with("global ")) + .collect(); + globals.sort_by(|a, b| b.1.cmp(a.1)); + for (reason, n) in globals.into_iter().take(15) { + println!(" {n:6} {reason}"); + } + for f in failures.iter().take(60) { + println!(" FAIL {f}"); + } + assert_eq!(pass.saturating_add(xfail).saturating_add(fail), total); + assert!(pass > 0); + assert_eq!(fail, 0, "{fail} tests failed unexpectedly"); + Ok(()) +} diff --git a/crates/clazel-js-builtins/tests/json.rs b/crates/clazel-js-builtins/tests/json.rs new file mode 100644 index 000000000..27448c5c3 --- /dev/null +++ b/crates/clazel-js-builtins/tests/json.rs @@ -0,0 +1,241 @@ +//! Tests of `JSON.parse` and `JSON.stringify`: scripts run with the built-ins +//! installed, each expectation what ECMA-262 §25.5† says, written by hand. +//! The result is the global `r` as its ToString. No files are read, so these +//! run under Miri too (every eighth case). + +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; +use clazel_js_vm::compile::compile_script; +use clazel_js_vm::interp::{Config, Vm}; + +fn r_of(src: &str) -> Result { + let c: Vec = src.chars().map(u32::from).collect(); + let ast = parse(&c, SourceGoal::Script).map_err(|_| format!("does not parse: {src}"))?; + let p = compile_script(&ast, &c).map_err(|e| format!("{src}: {e}"))?; + let cfg = if cfg!(miri) { + Config { + region_slots: 1 << 12, + heap_cells: 1 << 14, + } + } else { + Config::default() + }; + let mut vm = Vm::new(cfg).map_err(|e| format!("{e:?}"))?; + clazel_js_builtins::install(&mut vm).map_err(|e| format!("install: {e:?}"))?; + let script = vm.load(&p).map_err(|e| format!("{e:?}"))?; + vm.set_budget(5_000_000); + vm.run(&script).map_err(|e| format!("{src}: {e:?}"))?; + let g = vm.global(); + let r = vm.get_named(g, "r").map_err(|e| format!("{e:?}"))?; + let s = vm.to_js_string(r).map_err(|e| format!("{src}: {e:?}"))?; + Ok(s.to_string_lossy()) +} + +fn check(cases: &[(&str, &str)]) -> Result<(), String> { + let stride = if cfg!(miri) { 8 } else { 1 }; + for (src, want) in cases.iter().step_by(stride) { + let got = r_of(src)?; + if got != *want { + return Err(format!("{src}: expected {want:?}, got {got:?}")); + } + } + Ok(()) +} + +#[test] +fn json_parse() -> Result<(), String> { + check(&[ + ("var r = JSON.parse('1') + JSON.parse(' 2.5e1 ');", "26"), + ("var r = JSON.parse('null') === null;", "true"), + ( + "var r = JSON.parse('true') && !JSON.parse('false');", + "true", + ), + ("var r = JSON.parse('\"a\\\\nb\"').length;", "3"), + ("var r = JSON.parse('\"\\\\u0041\"');", "A"), + ("var r = JSON.parse('[1,2,[3]]').join();", "1,2,3"), + ("var r = Array.isArray(JSON.parse('[]'));", "true"), + ( + "var o = JSON.parse('{\"a\":1,\"b\":{\"c\":2}}'); var r = o.a + o.b.c;", + "3", + ), + // A duplicate name takes the later value. + ("var r = JSON.parse('{\"a\":1,\"a\":2}').a;", "2"), + // Numbers: -0 and big exponents. + ("var r = 1 / JSON.parse('-0');", "-Infinity"), + ("var r = JSON.parse('1e400');", "Infinity"), + // SyntaxErrors. + ( + "var r; try { JSON.parse('{a:1}'); } catch (e) { r = e.name; }", + "SyntaxError", + ), + ( + "var r; try { JSON.parse(''); } catch (e) { r = e.name; }", + "SyntaxError", + ), + ( + "var r; try { JSON.parse('[1,]'); } catch (e) { r = e.name; }", + "SyntaxError", + ), + ( + "var r; try { JSON.parse('01'); } catch (e) { r = e.name; }", + "SyntaxError", + ), + ( + "var r; try { JSON.parse(\"'a'\"); } catch (e) { r = e.name; }", + "SyntaxError", + ), + ( + "var r; try { JSON.parse(undefined); } catch (e) { r = e.name; }", + "SyntaxError", + ), + // The nesting bound: 600 levels is a SyntaxError, 100 is fine. + ( + "var s = ''; for (var i = 0; i < 600; i++) s += '['; for (var i = 0; i < 600; i++) s += ']'; var r; try { JSON.parse(s); } catch (e) { r = e.name; }", + "SyntaxError", + ), + ( + "var s = ''; for (var i = 0; i < 100; i++) s += '['; for (var i = 0; i < 100; i++) s += ']'; var r = Array.isArray(JSON.parse(s));", + "true", + ), + // The deepest text the parser takes, revived. + ( + "var s = ''; for (var i = 0; i < 500; i++) s += '['; for (var i = 0; i < 500; i++) s += ']'; var n = 0; JSON.parse(s, function (k, v) { n++; return v; }); var r = n;", + "500", + ), + // The reviver, §25.5.1.1: bottom-up, holder as this, deletes on undefined. + ( + "var log = ''; JSON.parse('{\"a\":[1,2],\"b\":3}', function (k, v) { log += k + ';'; return v; }); var r = log;", + "0;1;a;b;;", + ), + ( + "var r = JSON.parse('[1,2,3]', function (k, v) { return typeof v === 'number' ? v * 2 : v; }).join();", + "2,4,6", + ), + ( + "var o = JSON.parse('{\"a\":1,\"b\":2}', function (k, v) { return k === 'a' ? undefined : v; }); var r = ('a' in o) + ',' + o.b;", + "false,2", + ), + ( + "var holder; JSON.parse('{\"x\":1}', function (k, v) { if (k === 'x') holder = this; return v; }); var r = holder.x;", + "1", + ), + ( + "var r = JSON.parse('5', function (k, v) { return k === '' ? v + 1 : v; });", + "6", + ), + ("var r = JSON.parse('[1]', 5).length;", "1"), + ]) +} + +#[test] +fn json_stringify() -> Result<(), String> { + check(&[ + ( + "var r = JSON.stringify(1) + JSON.stringify('a\"b') + JSON.stringify(null);", + "1\"a\\\"b\"null", + ), + ( + "var r = JSON.stringify(true) + JSON.stringify(false);", + "truefalse", + ), + ( + "var r = JSON.stringify(NaN) + JSON.stringify(Infinity) + JSON.stringify(-0);", + "nullnull0", + ), + ("var r = JSON.stringify(undefined);", "undefined"), + ("var r = JSON.stringify(function () {});", "undefined"), + ( + "var r = JSON.stringify([1, 'a', null, undefined, function () {}]);", + "[1,\"a\",null,null,null]", + ), + ( + "var r = JSON.stringify({ a: 1, b: undefined, c: function () {}, d: [2] });", + "{\"a\":1,\"d\":[2]}", + ), + ("var r = JSON.stringify({});", "{}"), + ("var r = JSON.stringify([]);", "[]"), + ( + "var r = JSON.stringify({ 2: 'x', 1: 'y', b: 0 });", + "{\"1\":\"y\",\"2\":\"x\",\"b\":0}", + ), + // Wrapper objects unwrap. + ( + "var r = JSON.stringify([new Number(3), new String('s'), new Boolean(false)]);", + "[3,\"s\",false]", + ), + // Quoting: control characters and lone surrogates. + ( + "var r = JSON.stringify('\\u0001\\n\\ud800');", + "\"\\u0001\\n\\ud800\"", + ), + // toJSON, §25.5.2.2 step 2. + ( + "var r = JSON.stringify({ toJSON: function (k) { return 'k=' + k; } });", + "\"k=\"", + ), + ( + "var r = JSON.stringify({ a: { toJSON: function (k) { return k + '!'; } } });", + "{\"a\":\"a!\"}", + ), + // The replacer function: holder as this, key and value. + ( + "var r = JSON.stringify({ a: 1, b: 2 }, function (k, v) { return k === 'a' ? undefined : v; });", + "{\"b\":2}", + ), + ( + "var r = JSON.stringify(1, function (k, v) { return k === '' ? v + 1 : v; });", + "2", + ), + // The replacer array: a property list, in its order, no duplicates. + ( + "var r = JSON.stringify({ a: 1, b: 2, c: 3 }, ['c', 'a', 'c', 5]);", + "{\"c\":3,\"a\":1}", + ), + ( + "var r = JSON.stringify({ 1: 'x', a: 'y' }, [1, new String('a')]);", + "{\"1\":\"x\",\"a\":\"y\"}", + ), + // The gap: a number (at most 10), a string (first 10 units), wrappers. + ( + "var r = JSON.stringify({ a: [1, { b: 2 }] }, null, 2);", + "{\n \"a\": [\n 1,\n {\n \"b\": 2\n }\n ]\n}", + ), + ("var r = JSON.stringify([1], null, '--');", "[\n--1\n]"), + ( + "var r = JSON.stringify([1], null, 'abcdefghijklmnop');", + "[\nabcdefghij1\n]", + ), + ("var r = JSON.stringify([1], null, 20).length;", "15"), + ("var r = JSON.stringify([1], null, 0.9);", "[1]"), + ( + "var r = JSON.stringify({ a: 1 }, null, new Number(1));", + "{\n \"a\": 1\n}", + ), + ( + "var r = JSON.stringify([], null, 2) + JSON.stringify({}, null, 2);", + "[]{}", + ), + // Cycles are a TypeError, §25.5.2.4 step 1. + ( + "var a = {}; a.self = a; var r; try { JSON.stringify(a); } catch (e) { r = e.name; }", + "TypeError", + ), + ( + "var a = []; a[0] = [a]; var r; try { JSON.stringify(a); } catch (e) { r = e.name; }", + "TypeError", + ), + // The same object twice (not a cycle) is fine. + ("var x = {}; var r = JSON.stringify([x, x]);", "[{},{}]"), + // Depth past the bound is a RangeError. + ( + "var a = []; for (var i = 0; i < 200; i++) a = [a]; var r; try { JSON.stringify(a); } catch (e) { r = e.name; }", + "RangeError", + ), + // Round trip. + ( + "var s = '{\"a\":[1,2,{\"b\":null}],\"c\":\"x\"}'; var r = JSON.stringify(JSON.parse(s)) === s;", + "true", + ), + ]) +} diff --git a/crates/clazel-js-heap/Cargo.toml b/crates/clazel-js-heap/Cargo.toml index 18d2bf7d4..1f7ad5208 100644 --- a/crates/clazel-js-heap/Cargo.toml +++ b/crates/clazel-js-heap/Cargo.toml @@ -4,7 +4,7 @@ version = "0.0.0" edition.workspace = true license.workspace = true publish.workspace = true -description = "The JavaScript engine's heap for Clazel's Phase 7 browser (phase-7.md item 7.5): NaN-boxed values, a generational index arena, Latin-1/UTF-16 strings with interned property keys, and shapes." +description = "The JavaScript engine's heap for Clazel's Phase 7 browser (phase-7.md item 7.5): NaN-boxed values, a generational index arena, Latin-1/UTF-16 strings with interned property keys, shapes, elements, symbols, and (item 7.6) the mark-and-sweep collector with weak references." # No dependencies: the heap is ours end to end (CLAUDE.md rule 8), and it # depends on no browser crate (phase-7.md, "The engine as a library"). @@ -14,7 +14,7 @@ description = "The JavaScript engine's heap for Clazel's Phase 7 browser (phase- # have not run in CI's full verification on a pull request yet; raising it is # CI's call. `target-tier` is the plan's (phase-7.md, "Crates"): 1. [package.metadata.clazel] -tier = 0 +tier = 1 target-tier = 1 [lints] diff --git a/crates/clazel-js-heap/src/elements/contract.rs b/crates/clazel-js-heap/src/elements/contract.rs new file mode 100644 index 000000000..158ad4958 --- /dev/null +++ b/crates/clazel-js-heap/src/elements/contract.rs @@ -0,0 +1,301 @@ +//! The contract of element storage (phase-7.md item 7.5, "Elements"), stated +//! before the implementation (CLAUDE.md rule 3). +//! +//! # What is modelled +//! +//! The indexed properties of an Array exotic object, ECMA-262 §10.4.2: a +//! `length` (a `u32`, §10.4.2.1) and, for some array indices below it, a +//! value. This is [`Spec`]. The implementation ([`super::Elements`]) must +//! behave exactly as `Spec` does, whichever representation it holds. +//! +//! Modelling choices (SPEC-STYLE.md): +//! +//! - **Values are `u64` bit patterns.** A value is a NaN-boxed +//! [`crate::value::Value`]; this module does not look inside, except that +//! [`HOLE_BITS`] means "no element here". +//! - **Every element is a plain data property** with all three attributes +//! true. Elements with other attributes, accessors, and a non-writable +//! `length` are a later slice (they need a place for the attributes); an +//! array that needs one will leave this storage. +//! - **A hole is the absence of an element.** [`Spec`] stores no hole; +//! a [`Spec::set`] of the hole value is refused rather than treated as a +//! delete, so a caller cannot make an element that reads as absent while +//! being present. +//! - **Prototype lookups are not here.** `get` of a hole is `None`; reading +//! up the prototype chain is the caller's job (§10.1.8.1 OrdinaryGet). +//! - **Failure is a result, not a throw.** A `RangeError` for an invalid +//! length (§10.4.2.4 ArraySetLength) is the caller's job; here every +//! `u32` is a valid length. +//! - **Step numbers** below are those of ECMA-262 as the author remembers +//! them; no copy of the standard is vendored, so they have not been +//! checked against the text in this session. +//! +//! # The spec +//! +//! [`Spec`] is a length and a map from array index to value. +//! +//! - **S1.** Every key is below the length. +//! - **S2.** No stored value is [`HOLE_BITS`]. +//! - **[`Spec::set`]**, §10.4.2.1 \[\[DefineOwnProperty\]\] step 3 (P is an +//! array index), sub-step g: the element is stored, and if +//! `index >= length` the length becomes `index + 1`. +//! `u32::MAX` is not an array index (§6.1.7 "array index": an integer +//! index whose numeric value is below 2^32 - 1), so it is refused. +//! - **[`Spec::delete`]**, §10.1.10.1 OrdinaryDelete as reached from +//! §10.4.2: the element is removed; the length does not change. +//! - **[`Spec::set_length`]**, §10.4.2.4 ArraySetLength, the loop that +//! deletes elements from the end (step 17): every element at or above the +//! new length is gone, and the length is the new one. Growing the length +//! adds holes only. +//! - **[`Spec::get`]**: the element, or `None` for a hole or an index at or +//! above the length. +//! +//! # What the implementation must meet +//! +//! - **E1. Refinement.** After every operation, [`Repr::to_spec`] of the +//! storage equals the `Spec` that the same operations produce from +//! [`Spec::new`], and every operation returns what the `Spec`'s does. +//! - **E2. Well-formed representation** ([`repr_well_formed`]). Dense: the +//! vector is no longer than the length and no longer than [`MAX_DENSE`]. +//! Sparse: every key is below the length and no stored value is a hole. +//! - **E3. The fallback rule** ([`choose`]). A `set` at `index` on dense +//! storage of `n` slots stays dense unless `index >= n` and either +//! `index >= MAX_DENSE` or `index - n > MAX_GAP`, in which case the +//! storage becomes sparse. Nothing else changes the representation: +//! in particular sparse storage never becomes dense again (so an array +//! that went sparse once stays sparse, a stated and measurable cost), and +//! `set_length` and `delete` never switch it. +//! - **E4. Bounded dense memory.** Dense storage never holds more than +//! [`MAX_DENSE`] slots, and `set_length` never allocates: growing a +//! length costs nothing, so `new Array(4294967295)` is one `u32`. + +use std::collections::BTreeMap; + +/// The bit pattern of [`crate::value::Value::HOLE`]: no element. +pub const HOLE_BITS: u64 = 0xFFFA_0000_0000_0004; + +/// The most slots dense storage may hold: 2^24, which is 128 MiB of `u64`. +pub const MAX_DENSE: u32 = 1 << 24; + +/// The most holes a single `set` may add to dense storage by writing past +/// its end. A write further out makes the storage sparse. +pub const MAX_GAP: u32 = 1024; + +/// An array index is below this (§6.1.7). +pub const INDEX_LIMIT: u32 = u32::MAX; + +/// Why an element operation was refused. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Error { + /// `u32::MAX`, which is not an array index. + NotAnIndex, + /// The value is the hole; use `delete`. + HoleValue, + /// Memory for the storage could not be reserved. + OutOfMemory, +} + +/// The abstract array (see the module header). +#[derive(Clone, PartialEq, Eq, Debug, Default)] +pub struct Spec { + /// The array's `length`. + pub length: u32, + /// The elements that exist. + pub entries: BTreeMap, +} + +impl Spec { + /// An empty array with the given length: all holes. + #[must_use] + pub fn new(length: u32) -> Self { + Self { + length, + entries: BTreeMap::new(), + } + } + + /// S1 and S2. + #[must_use] + pub fn well_formed(&self) -> bool { + self.entries + .iter() + .all(|(&k, &v)| k < self.length && v != HOLE_BITS) + } + + /// The element at `index`, or `None` for a hole. + #[must_use] + pub fn get(&self, index: u32) -> Option { + self.entries.get(&index).copied() + } + + /// §10.4.2.1 \[\[DefineOwnProperty\]\], array-index case. + /// + /// # Errors + /// [`Error::NotAnIndex`] for `u32::MAX`; [`Error::HoleValue`] for the + /// hole. Nothing changes on error. + pub fn set(&mut self, index: u32, value: u64) -> Result<(), Error> { + if index == INDEX_LIMIT { + return Err(Error::NotAnIndex); + } + if value == HOLE_BITS { + return Err(Error::HoleValue); + } + self.entries.insert(index, value); + // 3.g "If index >= length, then ... Set lengthDesc.[[Value]] to + // index + 1" (no overflow: index < u32::MAX). + if index >= self.length { + self.length = index.saturating_add(1); + } + Ok(()) + } + + /// Remove the element at `index`; the length stays. Returns whether an + /// element was there. + pub fn delete(&mut self, index: u32) -> bool { + self.entries.remove(&index).is_some() + } + + /// §10.4.2.4 ArraySetLength, deleting from the end (step 17). + pub fn set_length(&mut self, new_length: u32) { + self.entries.retain(|&k, _| k < new_length); + self.length = new_length; + } +} + +/// Which representation an element store holds. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Mode { + /// A vector indexed by element index, holes stored as [`HOLE_BITS`]. + Dense, + /// An ordered map of the elements that exist. + Sparse, +} + +/// A representation's contents, as the contract sees them. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum Repr { + /// Dense: the slots. + Dense(Vec), + /// Sparse: the elements. + Sparse(BTreeMap), +} + +impl Repr { + /// The abstract array this representation stands for, with the given + /// `length` (E1). + #[must_use] + pub fn to_spec(&self, length: u32) -> Spec { + let entries = match self { + Self::Dense(v) => v + .iter() + .enumerate() + .filter(|&(_, &x)| x != HOLE_BITS) + .filter_map(|(i, &x)| u32::try_from(i).ok().map(|i| (i, x))) + .collect(), + Self::Sparse(m) => m.clone(), + }; + Spec { length, entries } + } +} + +/// E2. +#[must_use] +pub fn repr_well_formed(repr: &Repr, length: u32) -> bool { + match repr { + Repr::Dense(v) => { + let n = u32::try_from(v.len()).ok(); + n.is_some_and(|n| n <= length && n <= MAX_DENSE) + } + Repr::Sparse(m) => m.iter().all(|(&k, &v)| k < length && v != HOLE_BITS), + } +} + +/// E3: the representation after a `set` at `index`, given the one before +/// and, for dense storage, its slot count `slots`. +#[must_use] +pub fn choose(mode: Mode, slots: u32, index: u32) -> Mode { + match mode { + Mode::Sparse => Mode::Sparse, + Mode::Dense => { + if index < slots { + Mode::Dense + } else if index >= MAX_DENSE || index.saturating_sub(slots) > MAX_GAP { + Mode::Sparse + } else { + Mode::Dense + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn spec_set_grows_length() { + let mut s = Spec::new(0); + assert_eq!(s.set(5, 7), Ok(())); + assert_eq!(s.length, 6); + assert_eq!(s.get(5), Some(7)); + assert_eq!(s.get(0), None); + assert!(s.well_formed()); + assert_eq!(s.set(2, 1), Ok(())); + assert_eq!(s.length, 6); + } + + #[test] + fn spec_refuses() { + let mut s = Spec::new(3); + assert_eq!(s.set(u32::MAX, 1), Err(Error::NotAnIndex)); + assert_eq!(s.set(0, HOLE_BITS), Err(Error::HoleValue)); + assert_eq!(s, Spec::new(3)); + assert_eq!(s.set(u32::MAX - 1, 1), Ok(())); + assert_eq!(s.length, u32::MAX); + } + + #[test] + fn spec_set_length_truncates() { + let mut s = Spec::new(0); + for i in 0..10 { + let _ = s.set(i, u64::from(i)); + } + s.set_length(4); + assert_eq!(s.length, 4); + assert_eq!(s.get(3), Some(3)); + assert_eq!(s.get(4), None); + s.set_length(100); + assert_eq!(s.get(4), None, "growing adds holes only"); + assert!(s.well_formed()); + assert!(s.delete(1)); + assert!(!s.delete(1)); + assert_eq!(s.length, 100); + } + + #[test] + fn choose_rule() { + assert_eq!(choose(Mode::Dense, 0, 0), Mode::Dense); + assert_eq!(choose(Mode::Dense, 0, MAX_GAP), Mode::Dense); + assert_eq!(choose(Mode::Dense, 0, MAX_GAP + 1), Mode::Sparse); + assert_eq!(choose(Mode::Dense, 10, 3), Mode::Dense); + assert_eq!(choose(Mode::Dense, MAX_DENSE - 1, MAX_DENSE), Mode::Sparse); + assert_eq!( + choose(Mode::Dense, MAX_DENSE - 1, MAX_DENSE - 1), + Mode::Dense + ); + assert_eq!(choose(Mode::Sparse, 0, 0), Mode::Sparse); + } + + #[test] + fn repr_view() { + let r = Repr::Dense(vec![1, HOLE_BITS, 3]); + let s = r.to_spec(5); + assert_eq!(s.entries.len(), 2); + assert!(repr_well_formed(&r, 5)); + assert!(!repr_well_formed(&r, 2)); + assert!(!repr_well_formed( + &Repr::Sparse(BTreeMap::from([(7, 1)])), + 7 + )); + } +} diff --git a/crates/clazel-js-heap/src/elements/mod.rs b/crates/clazel-js-heap/src/elements/mod.rs new file mode 100644 index 000000000..18a59e241 --- /dev/null +++ b/crates/clazel-js-heap/src/elements/mod.rs @@ -0,0 +1,448 @@ +//! Elements: the indexed properties of an array (phase-7.md item 7.5). +//! +//! [`contract`] states what element storage must do (E1 to E4) and when it +//! falls back from dense to sparse (E3). [`Elements`] follows it: +//! +//! - **Dense:** a `Vec` of NaN-boxed values indexed by element index, +//! with [`Value::HOLE`] in the slots that have no element. Reads and +//! writes within it are one bounds-checked access, which is the shape the +//! interpreter's element ops and the JIT's templates want. +//! - **Sparse:** a `BTreeMap` of the elements that exist. +//! +//! The `length` is a separate `u32`, so growing it (`new Array(n)`) costs +//! nothing and a sparse array of length 2^32 - 1 is a few words. +//! +//! The collector (7.6) will need to enumerate the values: [`Elements::values`] +//! does, in index order, for either representation. + +pub mod contract; + +use crate::value::Value; +use contract::{Error, MAX_DENSE, Mode, Repr, Spec, choose, repr_well_formed}; +use std::collections::BTreeMap; + +pub use contract::{INDEX_LIMIT, MAX_GAP}; + +/// The dense representation's operations, free functions over the slots so +/// that the Kani harnesses can prove them without the sparse map (CBMC does +/// not finish with `BTreeMap` in the model). +pub(crate) mod dense { + use super::contract::{Error, HOLE_BITS}; + + /// The slot's bits, which may be [`HOLE_BITS`]; `None` past the slots. + pub(crate) fn get(v: &[u64], index: u32) -> Option { + let i = usize::try_from(index).ok()?; + v.get(i).copied() + } + + /// Write `bits` at `index`, padding with holes. The caller has checked + /// the fallback rule (contract `choose`), so the gap is small. + pub(crate) fn set(v: &mut Vec, index: u32, bits: u64) -> Result<(), Error> { + let i = usize::try_from(index).map_err(|_| Error::NotAnIndex)?; + if let Some(slot) = v.get_mut(i) { + *slot = bits; + } else { + let need = i.saturating_add(1).saturating_sub(v.len()); + v.try_reserve(need).map_err(|_| Error::OutOfMemory)?; + v.resize(i, HOLE_BITS); + v.push(bits); + } + Ok(()) + } + + /// Make the slot a hole; whether it held an element. + pub(crate) fn delete(v: &mut [u64], index: u32) -> bool { + let Some(slot) = usize::try_from(index).ok().and_then(|i| v.get_mut(i)) else { + return false; + }; + let was = *slot != HOLE_BITS; + *slot = HOLE_BITS; + was + } + + /// Drop the slots at or past `new_length`. + pub(crate) fn truncate(v: &mut Vec, new_length: u32) { + let keep = usize::try_from(new_length).unwrap_or(usize::MAX); + v.truncate(keep); + } +} + +/// The elements of one array. +#[derive(Clone, Debug)] +pub struct Elements { + length: u32, + repr: Repr, +} + +impl Default for Elements { + fn default() -> Self { + Self::new() + } +} + +impl Elements { + /// An empty array of length 0, dense. + #[must_use] + pub const fn new() -> Self { + Self { + length: 0, + repr: Repr::Dense(Vec::new()), + } + } + + /// An array of the given length with no elements (all holes), `new + /// Array(length)`. Dense and empty: nothing is allocated for the holes + /// (E4). + #[must_use] + pub const fn with_length(length: u32) -> Self { + Self { + length, + repr: Repr::Dense(Vec::new()), + } + } + + /// The array's `length`. + #[must_use] + pub const fn len(&self) -> u32 { + self.length + } + + /// Whether `length` is 0. + #[must_use] + pub const fn is_empty(&self) -> bool { + self.length == 0 + } + + /// Which representation is in use (E3). + #[must_use] + pub const fn mode(&self) -> Mode { + match self.repr { + Repr::Dense(_) => Mode::Dense, + Repr::Sparse(_) => Mode::Sparse, + } + } + + /// The element at `index`; `None` for a hole or an index at or past the + /// length (the caller then goes up the prototype chain). + #[must_use] + pub fn get(&self, index: u32) -> Option { + let bits = match &self.repr { + Repr::Dense(v) => dense::get(v, index)?, + Repr::Sparse(m) => *m.get(&index)?, + }; + if bits == contract::HOLE_BITS { + return None; + } + Value::from_bits(bits) + } + + /// Whether an element exists at `index`. + #[must_use] + pub fn has(&self, index: u32) -> bool { + self.get(index).is_some() + } + + /// Store `value` at `index`, growing the length if `index >= length` + /// (contract: `Spec::set`). May turn dense storage sparse (E3). + /// + /// # Errors + /// [`Error::NotAnIndex`] for `u32::MAX`; [`Error::HoleValue`] for + /// [`Value::HOLE`]; [`Error::OutOfMemory`] if dense storage could not + /// grow. Nothing changes on error. + pub fn set(&mut self, index: u32, value: Value) -> Result<(), Error> { + if index == INDEX_LIMIT { + return Err(Error::NotAnIndex); + } + if value == Value::HOLE { + return Err(Error::HoleValue); + } + let bits = value.bits(); + let slots = match &self.repr { + Repr::Dense(v) => u32::try_from(v.len()).unwrap_or(u32::MAX), + Repr::Sparse(_) => 0, + }; + let before = self.mode(); + if choose(before, slots, index) == Mode::Sparse && before == Mode::Dense { + self.make_sparse(); + } + match &mut self.repr { + // `choose` kept it dense: the gap is at most MAX_GAP and + // `index < MAX_DENSE`, so dense storage holds at most MAX_DENSE + // slots in all (E4). + Repr::Dense(v) => dense::set(v, index, bits)?, + Repr::Sparse(m) => { + m.insert(index, bits); + } + } + if index >= self.length { + self.length = index.saturating_add(1); + } + Ok(()) + } + + /// Remove the element at `index`, leaving a hole; the length stays. + /// Returns whether an element was there. + pub fn delete(&mut self, index: u32) -> bool { + match &mut self.repr { + Repr::Dense(v) => dense::delete(v, index), + Repr::Sparse(m) => m.remove(&index).is_some(), + } + } + + /// Set `length`, dropping every element at or past it (contract: + /// `Spec::set_length`). Never allocates, and never changes the + /// representation (E3, E4). + pub fn set_length(&mut self, new_length: u32) { + match &mut self.repr { + Repr::Dense(v) => dense::truncate(v, new_length), + Repr::Sparse(m) => { + // `split_off` keeps keys below the bound in `m`. + drop(m.split_off(&new_length)); + } + } + self.length = new_length; + } + + /// The existing elements as `(index, value)`, in index order. + pub fn entries(&self) -> Box + '_> { + match &self.repr { + Repr::Dense(v) => Box::new(v.iter().enumerate().filter_map(|(i, &b)| { + let i = u32::try_from(i).ok()?; + (b != contract::HOLE_BITS) + .then(|| Value::from_bits(b)) + .flatten() + .map(|x| (i, x)) + })), + Repr::Sparse(m) => Box::new( + m.iter() + .filter_map(|(&i, &b)| Value::from_bits(b).map(|x| (i, x))), + ), + } + } + + /// The existing values, in index order: what the collector scans. + pub fn values(&self) -> impl Iterator + '_ { + self.entries().map(|(_, v)| v) + } + + /// The abstract array this storage stands for (E1). + #[must_use] + pub fn to_spec(&self) -> Spec { + self.repr.to_spec(self.length) + } + + /// E2 for the current state. + #[must_use] + pub fn is_well_formed(&self) -> bool { + repr_well_formed(&self.repr, self.length) + } + + fn make_sparse(&mut self) { + if let Repr::Dense(v) = &self.repr { + let m: BTreeMap = v + .iter() + .enumerate() + .filter(|&(_, &b)| b != contract::HOLE_BITS) + .filter_map(|(i, &b)| u32::try_from(i).ok().map(|i| (i, b))) + .collect(); + self.repr = Repr::Sparse(m); + } + } + + /// The most slots dense storage may hold (E4). + pub const DENSE_LIMIT: u32 = MAX_DENSE; +} + +#[cfg(test)] +mod tests { + use super::*; + + fn n(i: i32) -> Value { + Value::int32(i) + } + + fn put(e: &mut Elements, index: u32, v: Value) { + assert_eq!(e.set(index, v), Ok(())); + } + + #[test] + fn push_get_length() { + let mut e = Elements::new(); + assert!(e.is_empty()); + for i in 0..5u32 { + assert_eq!(e.set(i, n(i32::try_from(i).unwrap_or(0))), Ok(())); + } + assert_eq!(e.len(), 5); + assert_eq!(e.get(3), Some(n(3))); + assert_eq!(e.get(5), None); + assert_eq!(e.mode(), Mode::Dense); + assert!(e.is_well_formed()); + } + + #[test] + fn holes_in_dense() { + let mut e = Elements::new(); + put(&mut e, 0, n(1)); + put(&mut e, 10, n(2)); + assert_eq!(e.mode(), Mode::Dense); + assert_eq!(e.len(), 11); + assert!(!e.has(5)); + assert!(e.has(10)); + assert!(e.delete(10)); + assert!(!e.delete(10)); + assert_eq!(e.len(), 11, "delete leaves the length"); + assert_eq!(e.entries().count(), 1); + } + + #[test] + fn falls_back_past_the_gap() { + let mut e = Elements::new(); + put(&mut e, 0, n(1)); + put(&mut e, MAX_GAP + 1, n(2)); + assert_eq!(e.mode(), Mode::Dense, "slots is 1, so the gap is MAX_GAP"); + put(&mut e, MAX_GAP + 1 + MAX_GAP + 2, n(3)); + assert_eq!(e.mode(), Mode::Sparse); + assert_eq!(e.get(0), Some(n(1))); + assert_eq!(e.get(MAX_GAP + 1), Some(n(2))); + assert_eq!(e.entries().count(), 3); + assert!(e.is_well_formed()); + } + + #[test] + fn sparse_stays_sparse() { + let mut e = Elements::new(); + put(&mut e, 1_000_000, n(1)); + assert_eq!(e.mode(), Mode::Sparse); + e.set_length(0); + assert_eq!(e.mode(), Mode::Sparse); + put(&mut e, 0, n(2)); + assert_eq!(e.mode(), Mode::Sparse); + assert_eq!(e.len(), 1); + } + + #[test] + fn huge_length_costs_nothing() { + let mut e = Elements::with_length(u32::MAX); + assert_eq!(e.len(), u32::MAX); + assert_eq!(e.mode(), Mode::Dense); + assert_eq!(e.get(7), None); + assert_eq!(e.set(u32::MAX, n(1)), Err(Error::NotAnIndex)); + assert_eq!(e.set(u32::MAX - 1, n(1)), Ok(())); + assert_eq!(e.mode(), Mode::Sparse); + assert_eq!(e.len(), u32::MAX); + assert!(e.is_well_formed()); + } + + #[test] + fn dense_limit_makes_sparse() { + let mut e = Elements::new(); + // Reaching MAX_DENSE by index, with every set in range of the gap + // rule, is not feasible to do slot by slot in a test; jump instead. + put(&mut e, MAX_DENSE, n(1)); + assert_eq!(e.mode(), Mode::Sparse); + } + + #[test] + fn refuses_hole_value_and_changes_nothing() { + let mut e = Elements::new(); + put(&mut e, 0, n(1)); + let before = e.to_spec(); + assert_eq!(e.set(1, Value::HOLE), Err(Error::HoleValue)); + assert_eq!(e.set(u32::MAX, n(1)), Err(Error::NotAnIndex)); + assert_eq!(e.to_spec(), before); + assert_eq!(e.len(), 1); + } + + #[test] + fn set_length_truncates_both() { + for sparse in [false, true] { + let mut e = Elements::new(); + if sparse { + put(&mut e, 100_000, n(9)); + e.delete(100_000); + } + for i in 0..10u32 { + put(&mut e, i, n(i32::try_from(i).unwrap_or(0))); + } + assert_eq!(e.mode() == Mode::Sparse, sparse); + e.set_length(4); + assert_eq!(e.len(), 4); + assert_eq!(e.get(3), Some(n(3))); + assert_eq!(e.get(4), None); + e.set_length(50); + assert_eq!(e.get(4), None); + assert_eq!(e.values().count(), 4); + assert!(e.is_well_formed()); + } + } + + /// A small deterministic generator, so the test needs no dependency. + struct Lcg(u64); + impl Lcg { + fn next(&mut self) -> u64 { + self.0 = self + .0 + .wrapping_mul(6_364_136_223_846_793_005) + .wrapping_add(1_442_695_040_888_963_407); + self.0 >> 33 + } + } + + /// E1, E2 and E3 over random operation sequences against `Spec`. + #[test] + fn matches_the_spec_on_random_sequences() { + let mut rng = Lcg(0x5EED); + // Miri interprets every step: 300 sequences took 3013 s there, over + // xtask's 300 s limit, so it runs the first 10 of the same sequence + // (about 100 s). The full count runs in the normal test job. + let sequences = if cfg!(miri) { 10 } else { 300 }; + for _ in 0..sequences { + let mut e = Elements::new(); + let mut s = Spec::new(0); + for _ in 0..200 { + let op = rng.next() % 8; + // Mostly small indices, some far, some at the edge. + let index = match rng.next() % 10 { + 0 => u32::try_from(rng.next() % 5000).unwrap_or(0) + 2000, + 1 => u32::MAX - u32::try_from(rng.next() % 3).unwrap_or(0), + _ => u32::try_from(rng.next() % 40).unwrap_or(0), + }; + let before = ( + e.mode(), + match &e.repr { + Repr::Dense(v) => u32::try_from(v.len()).unwrap_or(0), + Repr::Sparse(_) => 0, + }, + ); + match op { + 0..=3 => { + let v = if rng.next().is_multiple_of(9) { + Value::HOLE + } else { + n(i32::try_from(rng.next() % 1000).unwrap_or(0)) + }; + let got = e.set(index, v); + let want = s.set(index, v.bits()); + assert_eq!(got, want); + if got.is_ok() { + assert_eq!(e.mode(), choose(before.0, before.1, index)); + } + } + 4 | 5 => assert_eq!(e.delete(index), s.delete(index)), + 6 => { + let l = u32::try_from(rng.next() % 60).unwrap_or(0); + let mode = e.mode(); + e.set_length(l); + s.set_length(l); + assert_eq!(e.mode(), mode); + } + _ => assert_eq!(e.get(index).map(Value::bits), s.get(index)), + } + assert!(e.is_well_formed()); + assert!(s.well_formed()); + assert_eq!(e.to_spec(), s); + assert_eq!(e.len(), s.length); + } + } + } +} diff --git a/crates/clazel-js-heap/src/gc/contract.rs b/crates/clazel-js-heap/src/gc/contract.rs new file mode 100644 index 000000000..8166f8e8e --- /dev/null +++ b/crates/clazel-js-heap/src/gc/contract.rs @@ -0,0 +1,525 @@ +//! The collector's contract (phase-7.md items 7.0(f) and 7.6), stated before +//! the implementation (CLAUDE.md rule 3). This is the part a person reads. +//! +//! # Model +//! +//! 7.0(f) chooses G1: a stop-the-world, non-moving mark and sweep over the +//! arena. A collection starts from the **roots** and keeps what they reach. +//! The heap is abstracted here as a [`Node`] graph over arena slot indices: +//! node `i` is slot `i`. +//! +//! - A **strong edge** `i -> j` is a reference stored in object `i` that +//! keeps `j` alive: a property or element value, the prototype, a +//! `FinalizationRegistry`'s callback and held values. +//! - An **ephemeron** `(k, v)` of node `i` is one `WeakMap` entry: the +//! value `v` is kept alive only while both the map `i` and the key `k` +//! are alive (ECMA-262, "WeakMap Objects" and "Processing Model of +//! WeakRef and FinalizationRegistry Targets", whose liveness definition +//! makes a WeakMap value live only through its key). +//! - Everything else is **weak** and is not an edge at all: a `WeakSet` +//! member, a `WeakRef` target, a `FinalizationRegistry` cell's target +//! and unregister token, and the key of a `WeakMap` entry itself. +//! +//! # Reachability +//! +//! [`reachable`] is the specification: the least set of nodes that contains +//! every in-range root, contains the target of a strong edge of a member, +//! and contains the value `v` of an ephemeron `(k, v)` of a member when `k` +//! is a member. It is written as the plainest fixpoint, with no worklist +//! and no cleverness, so that reading it is checking it. +//! +//! # Invariants +//! +//! For one collection, with `roots` the references the embedder reports +//! (the value-stack region, the open handles, the objects kept alive for +//! the current job, the DOM wrappers, and the job and task queues): +//! +//! - **GC1 Safety.** A slot reachable from the roots is never freed. It +//! keeps its index and generation, so every reference the program holds +//! to it still resolves, to the same object. The Kani +//! harness proves the marker keeps every reachable node of every graph +//! up to its bound (see `src/proofs.rs` for the bound and for what is +//! not proven), and the property test checks the whole collector on +//! random heaps of any size ([`safe`]). +//! - **GC2 Precision.** A slot not reachable from the roots is freed in +//! that collection, so a collection leaves exactly the reachable slots +//! ([`exact`]). Garbage is not a safety matter; this keeps the collector +//! from being "correct" by keeping everything. +//! - **GC3 No dangling references.** After a collection no live object +//! holds a reference, strong or weak, to a slot that is not live. Weak +//! references to a freed slot are removed (GC4), so none is left to +//! alias a later occupant even before the generation check ([`closed`] +//! for the strong ones). +//! - **GC4 Weak clearing.** A `WeakRef` whose target was freed reads as +//! empty. A `WeakMap` entry or `WeakSet` member whose key was freed is +//! removed. A registry cell whose target was freed is removed, and so is +//! an unregister token that was freed. Weak references to survivors are +//! unchanged. +//! - **GC5 Ephemerons.** A `WeakMap` value is kept alive only through its +//! own key: a value that refers to its key does not keep the key alive, +//! and a chain of entries (the value of one is the key of the next) is +//! followed to its end, whatever the order of the entries. +//! - **GC6 Finalization.** Each cell of a *surviving* registry whose target +//! was freed produces exactly one cleanup, for the registry's callback and +//! that cell's held value, which the collection returns for the embedder +//! to run as a task, in that registry's cell order. A freed registry +//! produces none. Both the callback and the held values are kept alive +//! by the collection, and the embedder roots them until the task runs. +//! Nothing runs during a collection. +//! - **GC7 Safe points.** A collection runs only between operations: the +//! heap refuses (`Error::InOperation`) while an operation is in +//! progress, because a handler may hold a reference that no root names. +//! With exit-based JIT code (jit.md, "Exit") that means only from Rust, +//! at the interpreter's dispatch point. +//! - **GC8 Handles.** A handle scope is closed in the order it was opened, +//! and a mismatch is an error, not a silent unrooting. +//! - **GC9 `WeakRef` liveness.** A `WeakRef`'s target read during a job is +//! kept alive until the job ends (ECMA-262 abstract operations +//! `AddToKeptObjects` and `ClearKeptObjects`; section numbers differ +//! between editions, so they are cited by name), so a program never sees +//! a target vanish within one job. +//! +//! - **GC5 (work).** Marking is linear: for a graph of `n` nodes, `s` +//! strong edges and `e` ephemerons, it does at most `n + s + 2e` units +//! of work ([`work_bound`]), whatever the order of the entries. A unit is +//! one node taken off the work list, one strong edge examined, one +//! ephemeron examined when its map is processed, or one pending ephemeron +//! fired when its key is marked. (A pass over every ephemeron until +//! nothing changes is quadratic on a chain listed backwards; the +//! collector indexes the pending ephemerons by key instead.) This bounds +//! the work; it is not a pause-time claim. +//! - **GC10 Shapes.** After a collection, every shape of a live object, and +//! the parent of every live shape, is live, and so is the root shape +//! (`crate::shape::ROOT`): a lookup that walks a live shape's parent +//! chain never meets a freed shape. A shape the embedder names as a root +//! (an inline cache's, a class's initial shape) is live. A *transition* +//! (a shape's table of children) is weak: it does not keep the child +//! alive, and a transition to a freed shape is removed. +//! - **GC11 Keys.** After a collection, every interned string that a live +//! shape names (the key it added, or, for a dictionary shape, every key it +//! holds) is live, and so is every key the embedder names as a root (a +//! constant of live bytecode). Resolving a live key gives the string it +//! was interned as. +//! - **GC12 Precision for shapes and keys.** A shape that none of GC10's +//! rules keeps is freed, and so is a key that none of GC11's rules keeps +//! ([`live_shapes`] is the exact set; [`shapes_safe`] and +//! [`shapes_exact`] check against it). Shapes of dead objects keep +//! nothing, so a dictionary shape dies with its object. +//! - **GC13 Ids are never reused.** A freed shape id resolves to nothing +//! from then on, and a freed key too: a table never hands out an id twice, +//! so an inline cache that cached a freed shape id cannot hit another +//! shape, and a stale key is a defined error, not another property. +//! (The cost is a tombstone per freed entry; compacting the tables is a +//! later optimization that must flush the inline caches.) +//! +//! Symbols are not collected in this slice, and nothing outside the heap's +//! own tables (an embedder's own copies of a `Key`) is traced: the +//! embedder reports the keys and shapes it holds. +//! +//! The arena's own invariants (`crate::arena::contract`) hold throughout: +//! freeing goes through `Arena::free`, so a stale reference to a freed slot +//! is a defined error, whatever the collector does (7.0(f)). + +/// One slot of the abstract heap graph (see the module header). +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Node { + /// Slots this slot keeps alive. + pub strong: Vec, + /// `WeakMap` entries as `(key, value)` slot pairs. + pub ephemerons: Vec<(usize, usize)>, +} + +/// A heap graph: what [`reachable`] and the collector's `mark` read. The +/// provided implementations are for the slices of [`Node`]; the Kani +/// harnesses give it fixed-size arrays, which CBMC handles far faster than +/// vectors of vectors. +pub trait Graph { + /// The number of nodes. + fn len(&self) -> usize; + /// The strong edges of node `i` (none if there is no node `i`). + fn strong(&self, i: usize) -> &[usize]; + /// The ephemerons of node `i` (none if there is no node `i`). + fn ephemerons(&self, i: usize) -> &[(usize, usize)]; + /// Whether there are no nodes. + fn is_empty(&self) -> bool { + self.len() == 0 + } +} + +impl Graph for [Node] { + fn len(&self) -> usize { + <[Node]>::len(self) + } + fn strong(&self, i: usize) -> &[usize] { + self.get(i).map_or(&[], |n| &n.strong) + } + fn ephemerons(&self, i: usize) -> &[(usize, usize)] { + self.get(i).map_or(&[], |n| &n.ephemerons) + } +} + +impl Graph for Vec { + fn len(&self) -> usize { + Vec::len(self) + } + fn strong(&self, i: usize) -> &[usize] { + self.as_slice().strong(i) + } + fn ephemerons(&self, i: usize) -> &[(usize, usize)] { + self.as_slice().ephemerons(i) + } +} + +impl Graph for [Node; N] { + fn len(&self) -> usize { + N + } + fn strong(&self, i: usize) -> &[usize] { + self.as_slice().strong(i) + } + fn ephemerons(&self, i: usize) -> &[(usize, usize)] { + self.as_slice().ephemerons(i) + } +} + +/// The nodes reachable from `roots` (module header, "Reachability"), as one +/// flag per node. A root or edge naming no node (a stale or out-of-range +/// reference) reaches nothing. +#[must_use] +pub fn reachable(graph: &G, roots: &[usize]) -> Vec { + let mut marked = vec![false; graph.len()]; + for &r in roots { + mark(&mut marked, r); + } + loop { + let mut changed = false; + for i in 0..graph.len() { + if !marked.get(i).copied().unwrap_or(false) { + continue; + } + for &e in graph.strong(i) { + changed |= mark(&mut marked, e); + } + for &(k, v) in graph.ephemerons(i) { + if marked.get(k).copied().unwrap_or(false) { + changed |= mark(&mut marked, v); + } + } + } + if !changed { + return marked; + } + } +} + +/// Mark node `i`; whether that changed anything. +fn mark(marked: &mut [bool], i: usize) -> bool { + match marked.get_mut(i) { + Some(m) if !*m => { + *m = true; + true + } + _ => false, + } +} + +/// GC1: every reachable node survives. +#[must_use] +pub fn safe(graph: &G, roots: &[usize], survives: &[bool]) -> bool { + survives.len() == graph.len() + && reachable(graph, roots) + .iter() + .zip(survives) + .all(|(&r, &s)| !r || s) +} + +/// GC1 and GC2: exactly the reachable nodes survive. +#[must_use] +pub fn exact(graph: &G, roots: &[usize], survives: &[bool]) -> bool { + reachable(graph, roots) == survives +} + +/// GC3 for the survivors: every strong edge of a surviving node, and every +/// ephemeron value whose key survives, names a surviving node. An ephemeron +/// key of a surviving map may name a freed node only before GC4 clears the +/// entry, so keys are not checked here. +#[must_use] +pub fn closed(graph: &G, survives: &[bool]) -> bool { + let alive = |i: usize| survives.get(i).copied().unwrap_or(false); + (0..graph.len()).filter(|&i| alive(i)).all(|i| { + graph.strong(i).iter().all(|&e| alive(e)) + && graph + .ephemerons(i) + .iter() + .all(|&(k, v)| !alive(k) || alive(v)) + }) +} + +/// GC5's bound on the work of marking a graph of `nodes` nodes, `strong` +/// strong edges and `ephemerons` ephemerons: `nodes + strong + 2 * +/// ephemerons` (saturating). +#[must_use] +pub const fn work_bound(nodes: usize, strong: usize, ephemerons: usize) -> usize { + nodes + .saturating_add(strong) + .saturating_add(ephemerons.saturating_mul(2)) +} + +/// The totals of a graph, for [`work_bound`]: its nodes, strong edges and +/// ephemerons. +#[must_use] +pub fn totals(graph: &G) -> (usize, usize, usize) { + let (mut strong, mut eph) = (0usize, 0usize); + for i in 0..graph.len() { + strong = strong.saturating_add(graph.strong(i).len()); + eph = eph.saturating_add(graph.ephemerons(i).len()); + } + (graph.len(), strong, eph) +} + +/// The shape table, as GC10 to GC12 see it: entry `i` is the table's own +/// index (the shape with id `n` is entry `n - 1`), and an interned key is a +/// number below the key table's size. +pub trait ShapeGraph { + /// The number of table entries, freed ones included. + fn len(&self) -> usize; + /// Whether entry `i` is a live shape (a freed or missing one is not). + fn exists(&self, i: usize) -> bool; + /// The parent of shape `i`, if it has one. + fn parent(&self, i: usize) -> Option; + /// Calls `f` with each key shape `i` names: the one key a shared shape + /// added, or all the keys of a dictionary shape. + fn each_key(&self, i: usize, f: &mut dyn FnMut(usize)); + /// Whether there are no entries. + fn is_empty(&self) -> bool { + self.len() == 0 + } +} + +/// The live shapes and keys (GC10 to GC12), as two flag vectors (shapes by +/// entry, keys by number below `key_count`): the least sets such that a +/// root that is a live shape is in the first, the parent of a member is in +/// the first, and a root key and every key a member names is in the second. +/// Written as the plainest fixpoint. A root or key naming nothing is +/// ignored. +#[must_use] +pub fn live_shapes( + graph: &G, + roots: &[usize], + root_keys: &[usize], + key_count: usize, +) -> (Vec, Vec) { + let mut shapes = vec![false; graph.len()]; + let mut keys = vec![false; key_count]; + for &r in roots { + if graph.exists(r) { + mark(&mut shapes, r); + } + } + for &k in root_keys { + mark(&mut keys, k); + } + loop { + let mut changed = false; + for i in 0..graph.len() { + if !shapes.get(i).copied().unwrap_or(false) { + continue; + } + if let Some(p) = graph.parent(i).filter(|&p| graph.exists(p)) { + changed |= mark(&mut shapes, p); + } + graph.each_key(i, &mut |k| changed |= mark(&mut keys, k)); + } + if !changed { + return (shapes, keys); + } + } +} + +/// GC10 and GC11: every live shape and key survives. +#[must_use] +pub fn shapes_safe( + graph: &G, + roots: &[usize], + root_keys: &[usize], + shapes: &[bool], + keys: &[bool], +) -> bool { + let (want_s, want_k) = live_shapes(graph, roots, root_keys, keys.len()); + shapes.len() == want_s.len() + && want_s.iter().zip(shapes).all(|(&w, &s)| !w || s) + && want_k.iter().zip(keys).all(|(&w, &s)| !w || s) +} + +/// GC10 to GC12: exactly the live shapes and keys survive. +#[must_use] +pub fn shapes_exact( + graph: &G, + roots: &[usize], + root_keys: &[usize], + shapes: &[bool], + keys: &[bool], +) -> bool { + live_shapes(graph, roots, root_keys, keys.len()) == (shapes.to_vec(), keys.to_vec()) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Shape tables for the spec's tests: `(exists, parent, keys)`. + struct Table(Vec<(bool, Option, Vec)>); + + impl ShapeGraph for Table { + fn len(&self) -> usize { + self.0.len() + } + fn exists(&self, i: usize) -> bool { + self.0.get(i).is_some_and(|e| e.0) + } + fn parent(&self, i: usize) -> Option { + self.0.get(i).and_then(|e| e.1) + } + fn each_key(&self, i: usize, f: &mut dyn FnMut(usize)) { + for &k in self.0.get(i).map_or(&[][..], |e| &e.2) { + f(k); + } + } + } + + #[test] + fn a_live_shape_keeps_its_parents_and_its_keys() { + // 0 <- 1 <- 2 (keys 0, 1, 2), and 3 is a separate shape of key 3. + let t = Table(vec![ + (true, None, vec![]), + (true, Some(0), vec![0]), + (true, Some(1), vec![1]), + (true, None, vec![3]), + ]); + let (s, k) = live_shapes(&t, &[2], &[], 5); + assert_eq!(s, [true, true, true, false]); + assert_eq!(k, [true, true, false, false, false]); + // A root key alone keeps its string, and nothing else. + let (s, k) = live_shapes(&t, &[], &[4], 5); + assert_eq!(s, [false; 4]); + assert_eq!(k, [false, false, false, false, true]); + } + + #[test] + fn freed_and_missing_shapes_and_keys_reach_nothing() { + let t = Table(vec![(false, None, vec![]), (true, Some(0), vec![9])]); + // The parent was freed (it cannot be, in a heap; the spec does not + // follow it), a root names a missing shape and a missing key. + let (s, k) = live_shapes(&t, &[1, 7], &[8], 2); + assert_eq!(s, [false, true]); + assert_eq!(k, [false, false]); + } + + #[test] + fn safe_and_exact_tell_the_cases_apart() { + let t = Table(vec![(true, None, vec![]), (true, Some(0), vec![0])]); + assert!(shapes_exact(&t, &[1], &[], &[true, true], &[true])); + assert!(shapes_safe(&t, &[1], &[], &[true, true], &[true])); + // Keeping the key of nothing is safe, not exact. + assert!(shapes_safe(&t, &[0], &[], &[true, true], &[true])); + assert!(!shapes_exact(&t, &[0], &[], &[true, true], &[true])); + // Freeing a parent or a key is neither. + assert!(!shapes_safe(&t, &[1], &[], &[false, true], &[true])); + assert!(!shapes_safe(&t, &[1], &[], &[true, true], &[false])); + } + + #[test] + fn the_work_bound_counts_each_ephemeron_twice() { + assert_eq!(work_bound(10, 3, 4), 21); + assert_eq!(work_bound(usize::MAX, 1, 1), usize::MAX); + let g = [ + Node { + strong: vec![1], + ephemerons: vec![(1, 2)], + }, + Node::default(), + ]; + assert_eq!(totals(&g), (2, 1, 1)); + } + + fn node(strong: &[usize], eph: &[(usize, usize)]) -> Node { + Node { + strong: strong.to_vec(), + ephemerons: eph.to_vec(), + } + } + + #[test] + fn roots_and_strong_edges_are_followed() { + // 0 -> 1 -> 2; 3 is unreachable; 4 -> 0 does not make 4 reachable. + let g = [ + node(&[1], &[]), + node(&[2], &[]), + node(&[], &[]), + node(&[], &[]), + node(&[0], &[]), + ]; + assert_eq!(reachable(&g, &[0]), [true, true, true, false, false]); + assert_eq!(reachable(&g, &[]), [false; 5]); + } + + #[test] + fn cycles_terminate_and_are_not_kept_without_a_root() { + let g = [node(&[1], &[]), node(&[0], &[])]; + assert_eq!(reachable(&g, &[0]), [true, true]); + assert_eq!(reachable(&g, &[]), [false, false]); + } + + #[test] + fn an_ephemeron_value_needs_its_map_and_its_key() { + // Node 0 is a map with entry (1 -> 2). + let g = [node(&[], &[(1, 2)]), node(&[], &[]), node(&[], &[])]; + assert_eq!(reachable(&g, &[0]), [true, false, false]); + assert_eq!(reachable(&g, &[0, 1]), [true, true, true]); + // The key alone, without the map, keeps nothing. + assert_eq!(reachable(&g, &[1]), [false, true, false]); + } + + #[test] + fn a_value_that_refers_to_its_key_does_not_keep_it_alive() { + // Map 0 has entry (1 -> 2), and 2 -> 1. + let g = [node(&[], &[(1, 2)]), node(&[], &[]), node(&[1], &[])]; + assert_eq!(reachable(&g, &[0]), [true, false, false]); + } + + #[test] + fn ephemeron_chains_are_followed_in_any_order() { + // Map 0: (3 -> 2), (2 -> 1), (1 -> 4): listed against the chain. + let g = [ + node(&[], &[(3, 2), (2, 1), (1, 4)]), + node(&[], &[]), + node(&[], &[]), + node(&[], &[]), + node(&[], &[]), + ]; + assert_eq!(reachable(&g, &[0, 3]), [true, true, true, true, true]); + assert_eq!(reachable(&g, &[0]), [true, false, false, false, false]); + } + + #[test] + fn names_of_no_node_reach_nothing() { + let g = [node(&[9], &[(9, 8)])]; + assert_eq!(reachable(&g, &[7, 0]), [true]); + } + + #[test] + fn safe_exact_and_closed_tell_the_cases_apart() { + let g = [node(&[1], &[]), node(&[], &[]), node(&[], &[])]; + assert!(safe(&g, &[0], &[true, true, false])); + assert!(exact(&g, &[0], &[true, true, false])); + // Keeping garbage is safe, not exact. + assert!(safe(&g, &[0], &[true, true, true])); + assert!(!exact(&g, &[0], &[true, true, true])); + // Freeing something reachable is neither. + assert!(!safe(&g, &[0], &[true, false, false])); + assert!(!exact(&g, &[0], &[true, false, false])); + assert!(closed(&g, &[true, true, false])); + assert!(!closed(&g, &[true, false, false])); + assert!(!safe(&g, &[0], &[true])); + } +} diff --git a/crates/clazel-js-heap/src/gc/mod.rs b/crates/clazel-js-heap/src/gc/mod.rs new file mode 100644 index 000000000..49c2a5800 --- /dev/null +++ b/crates/clazel-js-heap/src/gc/mod.rs @@ -0,0 +1,752 @@ +//! The collector (phase-7.md item 7.6). [`contract`] states what it must +//! satisfy (GC1 to GC9); this is the implementation, and `src/proofs.rs` +//! holds the Kani harnesses for it. +//! +//! 7.0(f) G1: stop-the-world mark and sweep, non-moving. A collection: +//! +//! 1. refuses to run during an operation (GC7); +//! 2. builds the abstract graph of the live objects ([`Heap::graph`]) and +//! the root slots, from the caller's roots, the open handle scopes and +//! the objects kept alive for the job (GC9); +//! 3. marks ([`mark`], which is proven equal to [`contract::reachable`]); +//! 4. clears weak references to unmarked slots and takes the finalization +//! cleanups (GC4, GC6); +//! 5. frees every unmarked slot through [`Arena::free`], which bumps its +//! generation (GC2; the arena's invariant 3); +//! 6. marks and sweeps the shapes and interned keys from the survivors +//! (below). +//! +//! # What an embedder reports +//! +//! 7.0(f) names the roots: the value-stack region, the handles that +//! bindings and built-ins hold, every DOM node's wrapper, and the job and +//! task queues. The heap knows its own handle stack and kept-alive list; +//! the rest the embedder passes to [`Heap::collect`] as `roots` (the +//! engine's `Host` tracing method makes the DOM wrappers and task queues +//! part of that list). The cleanups a collection returns are tasks; their +//! callback and held values are alive after the collection, and the +//! embedder must report them as roots until it has run them. +//! +//! # Strings and shapes (GC10 to GC13) +//! +//! The heap owns the property-key [`Interner`] and the [`Shapes`] table, +//! and a collection also frees what no live thing reaches (step 6): +//! +//! - the shapes of the *surviving* objects, their parents, and the shapes the +//! embedder reports ([`RootSet::shapes`]) are kept, and so is +//! [`ROOT`]; +//! - the keys those shapes name, and the keys the embedder reports +//! ([`RootSet::keys`]), are kept; +//! - everything else is freed ([`Shapes::sweep`], [`Interner::sweep`]), and +//! its id is never issued again. +//! +//! Shapes and keys hold no reference into the arena, so they are marked +//! after the objects, from the survivors. [`mark_shapes`] is the marker, +//! proven closed over its roots by Kani (`src/proofs.rs`). +//! +//! # What is not here +//! +//! Symbols are not collected. A key or shape id the embedder keeps outside +//! the heap (a bytecode constant, an inline cache's shape) is the +//! embedder's to report; one it forgot to report is a defined miss after +//! the collection (GC13), not another property. Compacting the tombstones +//! of the two tables is later, and must flush the inline caches. + +pub mod contract; + +use crate::arena::{self, Arena, Ref}; +use crate::elements::Elements; +use crate::shape::{self, Attrs, ROOT, ShapeId, Shapes}; +use crate::string::{Interner, JsString, Key}; +use crate::value::Value; +use contract::{Graph, Node, ShapeGraph}; + +/// What a heap object is, beyond its ordinary properties. +#[derive(Clone, Debug)] +pub enum Kind { + /// An object with only strong properties. + Ordinary, + /// `WeakMap`: `(key, value)` entries; the key is weak, and the value is + /// kept alive by the key (an ephemeron). + WeakMap(Vec<(Ref, Value)>), + /// `WeakSet`: weakly held members. + WeakSet(Vec), + /// `WeakRef`: a weakly held target, `None` once cleared. + WeakRef(Option), + /// `FinalizationRegistry`. + Registry(Registry), +} + +/// A `FinalizationRegistry`'s state. +#[derive(Clone, Debug)] +pub struct Registry { + /// The cleanup callback, held strongly. + pub callback: Value, + /// Registered targets. + pub cells: Vec, +} + +/// One `FinalizationRegistry.prototype.register` call. +#[derive(Clone, Copy, Debug)] +pub struct Cell { + /// The weakly held target. + pub target: Ref, + /// The value passed to the callback, held strongly. + pub held: Value, + /// The weakly held unregister token, if any. + pub token: Option, +} + +/// A heap object: a prototype, property slots, indexed elements and a kind. +#[derive(Clone, Debug)] +pub struct Object { + /// The prototype, a strong reference. + pub proto: Option, + /// The shape: which keys the property slots below belong to. A live + /// object's shape is live (GC10). + pub shape: ShapeId, + /// Property values (a shape maps keys to these indices), all strong. + pub slots: Vec, + /// Indexed properties, all strong. + pub elements: Elements, + /// What kind of object this is. + pub kind: Kind, +} + +impl Object { + /// An ordinary object with no properties and no prototype. + #[must_use] + pub const fn ordinary() -> Self { + Self::of(Kind::Ordinary) + } + + /// An object of the given kind with no properties and no prototype. + #[must_use] + pub const fn of(kind: Kind) -> Self { + Self { + proto: None, + shape: ROOT, + slots: Vec::new(), + elements: Elements::new(), + kind, + } + } +} + +/// One finalization callback to run: the embedder queues it as a task +/// (GC6). Nothing runs during a collection. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Cleanup { + /// The registry's callback. + pub callback: Value, + /// The held value of the cell whose target was freed. + pub held: Value, +} + +/// What one collection did. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Collection { + /// Slots freed. + pub freed: usize, + /// Slots that survived. + pub survivors: usize, + /// Cleanups for the embedder to run as tasks. + pub cleanups: Vec, + /// Shapes freed (GC12). + pub freed_shapes: usize, + /// Interned keys freed (GC12). + pub freed_keys: usize, +} + +/// Why a heap operation failed. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Error { + /// The arena refused ([`arena::Error`]). + Arena(arena::Error), + /// A collection was requested during an operation (GC7). + InOperation, + /// `exit_op` without a matching `enter_op`. + NotInOperation, + /// A handle was made with no scope open. + NoScope, + /// A scope was closed out of order (GC8). + ScopeMismatch, + /// The reference does not name a live `WeakRef`. + NotAWeakRef, + /// A shape operation failed ([`shape::Error`]); `Unknown` also when an + /// object is allocated with a shape that is not live. + Shape(shape::Error), + /// The key is not live: never issued, or freed by a collection (GC13). + StaleKey, + /// The interner is full, or the string is too long. + Intern, +} + +impl From for Error { + fn from(e: arena::Error) -> Self { + Self::Arena(e) + } +} + +impl From for Error { + fn from(e: shape::Error) -> Self { + Self::Shape(e) + } +} + +/// What the embedder reports to a collection besides the heap's own roots +/// (the handle stack and the job's kept targets). +#[derive(Clone, Copy, Debug, Default)] +pub struct RootSet<'a> { + /// The value stack, DOM wrappers, task queues and pending cleanups. + pub values: &'a [Value], + /// Keys held outside the heap, such as bytecode constants. + pub keys: &'a [Key], + /// Shapes held outside the heap, such as an inline cache's. + pub shapes: &'a [ShapeId], +} + +/// An open handle scope; closing it unroots the handles made in it. +#[derive(Debug)] +pub struct Scope { + depth: usize, +} + +/// The heap: the arena of objects, the handle stack, the objects kept +/// alive for the current job and the operation guard. +#[derive(Debug)] +pub struct Heap { + arena: Arena, + handles: Vec, + scope_marks: Vec, + kept: Vec, + op_depth: u32, + shapes: Shapes, + keys: Interner, +} + +impl Default for Heap { + fn default() -> Self { + Self::new() + } +} + +/// Which slots are alive after marking: the generation of each live slot +/// (before the sweep) and its mark. Owned, so the sweep can mutate the +/// arena while asking it. +struct Liveness { + generation: Vec>, + marked: Vec, +} + +impl Liveness { + /// Whether `r` names an object that survives this collection. + fn survives(&self, r: Ref) -> bool { + let Some(i) = usize::try_from(r.index).ok() else { + return false; + }; + self.generation.get(i).copied().flatten() == Some(r.generation) + && self.marked.get(i).copied().unwrap_or(false) + } +} + +impl Heap { + /// An empty heap that may grow to [`arena::MAX_SLOTS`] objects. + #[must_use] + pub fn new() -> Self { + Self::with_limit(arena::MAX_SLOTS) + } + + /// An empty heap of at most `limit` objects: the memory limit, and the + /// harnesses' bound. + #[must_use] + pub fn with_limit(limit: usize) -> Self { + Self { + arena: Arena::with_limit(limit), + handles: Vec::new(), + scope_marks: Vec::new(), + kept: Vec::new(), + op_depth: 0, + shapes: Shapes::new(), + keys: Interner::new(), + } + } + + /// Store an object. Never collects: a full heap is reported, and the + /// embedder collects at its next safe point. + /// + /// # Errors + /// [`arena::Error::Full`] as [`Error::Arena`]; [`shape::Error::Unknown`] + /// as [`Error::Shape`] if the object's shape is not live. + pub fn alloc(&mut self, object: Object) -> Result { + if !self.shapes.is_live(object.shape) { + return Err(Error::Shape(shape::Error::Unknown)); + } + Ok(self.arena.alloc(object)?) + } + + /// The key for `s`, interning it if no live key has its string. + /// + /// # Errors + /// [`Error::Intern`] if the table is full. + pub fn intern(&mut self, s: &JsString) -> Result { + self.keys.intern(s).map_err(|_| Error::Intern) + } + + /// The string a live key stands for. + #[must_use] + pub fn key_string(&self, k: Key) -> Option<&JsString> { + self.keys.resolve(k) + } + + /// The interner, for its invariants' checks. + #[must_use] + pub const fn keys(&self) -> &Interner { + &self.keys + } + + /// The shape table, for lookups and its invariants' checks. + #[must_use] + pub const fn shapes(&self) -> &Shapes { + &self.shapes + } + + /// Give object `r` the property `key` with `attrs` and the value `v`: + /// its shape becomes the transition on `(key, attrs)` (shared with every + /// object that gains the same properties in the same order), and the + /// slot the shape assigns holds `v` (earlier slots missing from the + /// object read as `undefined`). Returns the slot. + /// + /// # Errors + /// [`Error::StaleKey`] if `key` is not live; [`Error::Arena`] for a bad + /// `r`; [`Error::Shape`] as [`Shapes::add_property`] (`Exists` if the + /// object's shape has the key already). + pub fn add_property(&mut self, r: Ref, key: Key, attrs: Attrs, v: Value) -> Result { + if self.keys.resolve(key).is_none() { + return Err(Error::StaleKey); + } + let from = self.arena.get(r)?.shape; + let (to, slot) = self.shapes.add_property(from, key, attrs)?; + let o = self.arena.get_mut(r)?; + let at = usize::try_from(slot).map_err(|_| Error::Shape(shape::Error::TooMany))?; + if o.slots.len() <= at { + o.slots.resize(at.saturating_add(1), Value::UNDEFINED); + } + if let Some(cell) = o.slots.get_mut(at) { + *cell = v; + } + o.shape = to; + Ok(slot) + } + + /// The object `r` names. + /// + /// # Errors + /// [`Error::Arena`]: out of range, or stale. + pub fn get(&self, r: Ref) -> Result<&Object, Error> { + Ok(self.arena.get(r)?) + } + + /// The object `r` names, mutably. + /// + /// # Errors + /// As [`Heap::get`]. + pub fn get_mut(&mut self, r: Ref) -> Result<&mut Object, Error> { + Ok(self.arena.get_mut(r)?) + } + + /// Whether `r` names a live object. + #[must_use] + pub fn is_live(&self, r: Ref) -> bool { + self.arena.is_live(r) + } + + /// The number of live objects. + #[must_use] + pub const fn live(&self) -> usize { + self.arena.live() + } + + /// The arena, for its invariants' checks. + #[must_use] + pub const fn arena(&self) -> &Arena { + &self.arena + } + + /// An operation begins: no collection may run until it ends (GC7). + pub fn enter_op(&mut self) { + self.op_depth = self.op_depth.saturating_add(1); + } + + /// The operation ends. + /// + /// # Errors + /// [`Error::NotInOperation`] if none was begun. + pub fn exit_op(&mut self) -> Result<(), Error> { + self.op_depth = self.op_depth.checked_sub(1).ok_or(Error::NotInOperation)?; + Ok(()) + } + + /// Open a handle scope (GC8). + pub fn open_scope(&mut self) -> Scope { + self.scope_marks.push(self.handles.len()); + Scope { + depth: self.scope_marks.len(), + } + } + + /// Root `v` until the innermost scope closes; returns it. + /// + /// # Errors + /// [`Error::NoScope`] if no scope is open. + pub fn root(&mut self, v: Value) -> Result { + if self.scope_marks.is_empty() { + return Err(Error::NoScope); + } + self.handles.push(v); + Ok(v) + } + + /// Close `scope`, unrooting its handles. + /// + /// # Errors + /// [`Error::ScopeMismatch`] if it is not the innermost open scope; the + /// handles stay rooted then. + pub fn close_scope(&mut self, scope: Scope) -> Result<(), Error> { + if scope.depth != self.scope_marks.len() { + return Err(Error::ScopeMismatch); + } + let to = self.scope_marks.pop().ok_or(Error::ScopeMismatch)?; + self.handles.truncate(to); + Ok(()) + } + + /// `WeakRef.prototype.deref` (ECMA-262 `AddToKeptObjects`): the target, + /// or `None` if it was collected. A live target is kept alive until + /// [`Heap::end_job`] (GC9). + /// + /// # Errors + /// [`Error::NotAWeakRef`] if `weak` is not a live `WeakRef`. + pub fn deref_weak(&mut self, weak: Ref) -> Result, Error> { + let Kind::WeakRef(target) = self.arena.get(weak)?.kind else { + return Err(Error::NotAWeakRef); + }; + let Some(t) = target.filter(|&t| self.arena.is_live(t)) else { + return Ok(None); + }; + self.kept.push(t); + Ok(Some(t)) + } + + /// The job ends (ECMA-262 `ClearKeptObjects`): the targets read by + /// `deref_weak` are no longer kept alive. + pub fn end_job(&mut self) { + self.kept.clear(); + } + + /// The abstract graph of the heap: one node per arena slot, the empty + /// node for a slot that is not live. A reference to a slot that is not + /// live, or is a different generation, is no edge. + #[must_use] + pub fn graph(&self) -> Vec { + let mut nodes = vec![Node::default(); self.arena.slots()]; + let idx = |r: Ref| -> Option { + self.arena + .is_live(r) + .then(|| usize::try_from(r.index).ok()) + .flatten() + }; + let val = |v: Value| v.as_ref().and_then(idx); + for (r, o) in self.arena.iter() { + let Some(node) = usize::try_from(r.index).ok().and_then(|i| nodes.get_mut(i)) else { + continue; + }; + node.strong.extend(o.proto.and_then(idx)); + node.strong.extend(o.slots.iter().copied().filter_map(val)); + node.strong.extend(o.elements.values().filter_map(val)); + match &o.kind { + Kind::Ordinary | Kind::WeakSet(_) | Kind::WeakRef(_) => {} + Kind::WeakMap(entries) => { + for &(k, v) in entries { + if let (Some(k), Some(v)) = (idx(k), val(v)) { + node.ephemerons.push((k, v)); + } + } + } + Kind::Registry(reg) => { + node.strong.extend(val(reg.callback)); + node.strong + .extend(reg.cells.iter().filter_map(|c| val(c.held))); + } + } + } + nodes + } + + /// The slot indices of the live objects among `roots`, the open + /// handles and the kept-alive targets. + #[must_use] + pub fn root_indices(&self, roots: &[Value]) -> Vec { + let from_refs = self.kept.iter().copied(); + let from_values = roots + .iter() + .chain(self.handles.iter()) + .filter_map(|v| v.as_ref()); + from_refs + .chain(from_values) + .filter(|&r| self.arena.is_live(r)) + .filter_map(|r| usize::try_from(r.index).ok()) + .collect() + } + + /// Collect garbage (module header). `roots` are the embedder's: + /// the value stack, DOM wrappers, task queues and pending cleanups. No + /// key or shape is reported (see [`Heap::collect_with`]). + /// + /// # Errors + /// As [`Heap::collect_with`]. + pub fn collect(&mut self, roots: &[Value]) -> Result { + self.collect_with(&RootSet { + values: roots, + ..RootSet::default() + }) + } + + /// Collect garbage (module header), with the keys and shapes the + /// embedder holds as well as its values. + /// + /// # Errors + /// [`Error::InOperation`] during an operation (GC7); nothing changes. + /// [`Error::Arena`] if the arena reports corruption while freeing + /// (unreachable by construction). + pub fn collect_with(&mut self, roots: &RootSet<'_>) -> Result { + if self.op_depth != 0 { + return Err(Error::InOperation); + } + let graph = self.graph(); + let marked = mark(&graph, &self.root_indices(roots.values)); + let mut generation = vec![None; self.arena.slots()]; + let live: Vec = self.arena.iter().map(|(r, _)| r).collect(); + for r in &live { + if let Some(g) = usize::try_from(r.index) + .ok() + .and_then(|i| generation.get_mut(i)) + { + *g = Some(r.generation); + } + } + let liveness = Liveness { generation, marked }; + let mut out = Collection::default(); + // The shapes of the survivors, and the embedder's, are the roots of + // the shape table (entry 0 is `ROOT`). + let mut shape_roots: Vec = vec![0]; + let entry = |id: ShapeId| usize::try_from(id.0).ok().and_then(|i| i.checked_sub(1)); + shape_roots.extend(roots.shapes.iter().copied().filter_map(entry)); + for &r in &live { + if liveness.survives(r) { + out.survivors = out.survivors.saturating_add(1); + shape_roots.extend(self.arena.get(r).ok().and_then(|o| entry(o.shape))); + if let Ok(o) = self.arena.get_mut(r) { + clear_weak(o, &liveness, &mut out.cleanups); + } + } else { + self.arena.free(r)?; + out.freed = out.freed.saturating_add(1); + } + } + let key_roots: Vec = roots + .keys + .iter() + .filter_map(|k| usize::try_from(k.0).ok()) + .collect(); + let mut shape_marks = vec![false; self.shapes.issued()]; + let mut key_marks = vec![false; self.keys.issued()]; + mark_shapes( + &self.shapes, + &shape_roots, + &key_roots, + &mut shape_marks, + &mut key_marks, + ); + out.freed_shapes = self.shapes.sweep(&shape_marks); + out.freed_keys = self.keys.sweep(&key_marks); + Ok(out) + } +} + +/// GC4 and GC6 for one surviving object: remove weak references to freed +/// slots, and take the cleanups of a registry's dead cells. +fn clear_weak(o: &mut Object, live: &Liveness, cleanups: &mut Vec) { + match &mut o.kind { + Kind::Ordinary => {} + Kind::WeakMap(entries) => entries.retain(|&(k, _)| live.survives(k)), + Kind::WeakSet(members) => members.retain(|&m| live.survives(m)), + Kind::WeakRef(target) => { + if target.is_some_and(|t| !live.survives(t)) { + *target = None; + } + } + Kind::Registry(reg) => { + let callback = reg.callback; + reg.cells.retain_mut(|cell| { + if live.survives(cell.target) { + if cell.token.is_some_and(|t| !live.survives(t)) { + cell.token = None; + } + true + } else { + cleanups.push(Cleanup { + callback, + held: cell.held, + }); + false + } + }); + } + } +} + +/// Mark the shapes and keys that `roots` (shape table entries) and +/// `root_keys` reach (GC10 to GC12): each root shape, its parents, and every +/// key any of them names. `shapes` (one flag per table entry) and `keys` +/// (one per key issued) are all false on entry. Each shape is marked once, +/// so the work is linear in the table. [`contract::live_shapes`] is its +/// specification. A root naming no live shape, or a key past `keys`, is +/// ignored; a parent that is not live ends a chain. +pub fn mark_shapes( + graph: &G, + roots: &[usize], + root_keys: &[usize], + shapes: &mut [bool], + keys: &mut [bool], +) { + for &root in roots { + let mut at = Some(root); + while let Some(i) = at { + if !graph.exists(i) { + break; + } + match shapes.get_mut(i) { + Some(m) if !*m => *m = true, + _ => break, + } + graph.each_key(i, &mut |k| { + if let Some(m) = keys.get_mut(k) { + *m = true; + } + }); + at = graph.parent(i); + } + } + for &k in root_keys { + if let Some(m) = keys.get_mut(k) { + *m = true; + } + } +} + +/// Marking's end of a list of pending ephemerons, and of an empty head. +const NONE: usize = usize::MAX; + +/// Mark the nodes reachable from `roots`: a worklist for the strong edges, +/// and a pending list per key for the ephemerons, so that every edge and +/// every ephemeron is looked at a bounded number of times (GC5's work +/// bound). [`contract::reachable`] is its specification, and the Kani +/// harnesses prove it closed over the roots and edges (so GC1) on every +/// graph of their bound. +#[must_use] +pub fn mark(graph: &G, roots: &[usize]) -> Vec { + mark_counted(graph, roots).0 +} + +/// [`mark`], and the units of work it did (GC5, [`contract::work_bound`]). +#[must_use] +pub fn mark_counted(graph: &G, roots: &[usize]) -> (Vec, usize) { + let n = graph.len(); + let (_, _, ephemerons) = contract::totals(graph); + let mut marked = vec![false; n]; + // Each node is queued at most once, so a stack of `n` never overflows. + let mut work = vec![0usize; n]; + let mut head = vec![NONE; n]; + // Each ephemeron is listed at most once: when its map is processed. + let mut link = vec![(0usize, NONE); ephemerons]; + let units = mark_into(graph, roots, &mut marked, &mut work, &mut head, &mut link); + (marked, units) +} + +/// [`mark`] over storage the caller provides, returning the units of work: +/// - `marked`: one flag per node, all false, filled in; +/// - `work`: a stack of at least one slot per node; +/// - `head`: one entry per node, all `usize::MAX`; +/// - `link`: at least one entry per ephemeron in the graph. +/// +/// The Kani harnesses call this with arrays, because CBMC does not finish +/// with vectors in the model. +/// +/// An ephemeron `(k, v)` of a marked node is examined once. If `k` is marked +/// then, `v` is marked; otherwise the entry goes on `k`'s pending list +/// (`head[k]`, then `link`, newest first), and marking `k` marks every `v` +/// on it. So a key marked later still reaches its values, and no pass over +/// the ephemerons repeats. +pub fn mark_into( + graph: &G, + roots: &[usize], + marked: &mut [bool], + work: &mut [usize], + head: &mut [usize], + link: &mut [(usize, usize)], +) -> usize { + let mut top = 0usize; + let mut used = 0usize; + let mut units = 0usize; + for &r in roots { + visit(marked, work, &mut top, r); + } + while top > 0 { + top = top.saturating_sub(1); + let Some(i) = work.get(top).copied() else { + break; + }; + units = units.saturating_add(1); + // `i` is marked, as a key too: fire what waited for it. + let mut at = head.get(i).copied().unwrap_or(NONE); + while let Some(&(v, next)) = link.get(at) { + units = units.saturating_add(1); + visit(marked, work, &mut top, v); + at = next; + } + for &e in graph.strong(i) { + units = units.saturating_add(1); + visit(marked, work, &mut top, e); + } + for &(k, v) in graph.ephemerons(i) { + units = units.saturating_add(1); + if marked.get(k).copied().unwrap_or(false) { + visit(marked, work, &mut top, v); + } else if let (Some(h), Some(slot)) = (head.get_mut(k), link.get_mut(used)) { + *slot = (v, *h); + *h = used; + used = used.saturating_add(1); + } + } + } + units +} + +/// Mark `i` and queue it, if it names a node not yet marked. +fn visit(marked: &mut [bool], work: &mut [usize], top: &mut usize, i: usize) -> bool { + match marked.get_mut(i) { + Some(m) if !*m => { + *m = true; + if let Some(slot) = work.get_mut(*top) { + *slot = i; + *top = top.saturating_add(1); + } + true + } + _ => false, + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/clazel-js-heap/src/gc/tests.rs b/crates/clazel-js-heap/src/gc/tests.rs new file mode 100644 index 000000000..c311fab87 --- /dev/null +++ b/crates/clazel-js-heap/src/gc/tests.rs @@ -0,0 +1,1318 @@ +//! Unit tests for each weak kind and for the roots, and the property test of +//! GC1 to GC6 over random heaps of any size, from a fixed-seed generator. + +use super::contract::{self, Node}; +use super::*; +use crate::arena::Error as ArenaError; +use crate::shape::MAX_FAST_PROPERTIES; +use std::collections::HashSet; + +type R = Result; + +fn obj(h: &mut Heap) -> R { + h.alloc(Object::ordinary()) +} + +fn val(r: Ref) -> Value { + Value::heap_ref(r) +} + +fn int(i: i32) -> Value { + Value::int32(i) +} + +fn kind_of(h: &mut Heap, kind: Kind) -> R { + h.alloc(Object::of(kind)) +} + +fn stale(h: &Heap, r: Ref) -> bool { + h.get(r).err() == Some(Error::Arena(ArenaError::Stale)) +} + +fn add_slot(h: &mut Heap, to: Ref, v: Value) -> R<()> { + h.get_mut(to)?.slots.push(v); + Ok(()) +} + +// ---- Roots and tracing ------------------------------------------------ + +#[test] +fn what_the_roots_reach_survives_and_the_rest_is_freed() -> R<()> { + let mut h = Heap::new(); + let a = obj(&mut h)?; + let b = obj(&mut h)?; + let c = obj(&mut h)?; + let garbage = obj(&mut h)?; + add_slot(&mut h, a, val(b))?; + add_slot(&mut h, b, val(c))?; + add_slot(&mut h, garbage, val(a))?; + // Prototype and element edges count too. + let d = obj(&mut h)?; + h.get_mut(c)?.proto = Some(d); + let e = obj(&mut h)?; + assert_eq!(h.get_mut(d)?.elements.set(3, val(e)), Ok(())); + let out = h.collect(&[val(a)])?; + assert_eq!((out.freed, out.survivors), (1, 5)); + for r in [a, b, c, d, e] { + assert!(h.is_live(r)); + } + assert!(stale(&h, garbage)); + assert!(h.arena().is_well_formed()); + Ok(()) +} + +#[test] +fn cycles_without_a_root_are_freed() -> R<()> { + let mut h = Heap::new(); + let a = obj(&mut h)?; + let b = obj(&mut h)?; + add_slot(&mut h, a, val(b))?; + add_slot(&mut h, b, val(a))?; + let out = h.collect(&[int(7), Value::UNDEFINED])?; + assert_eq!((out.freed, h.live()), (2, 0)); + Ok(()) +} + +#[test] +fn a_freed_slot_is_reused_with_a_new_generation_so_old_references_stay_stale() -> R<()> { + let mut h = Heap::new(); + let old = obj(&mut h)?; + h.collect(&[])?; + let new = obj(&mut h)?; + assert_eq!(new.index, old.index); + assert_ne!(new.generation, old.generation); + assert!(stale(&h, old)); + // A stale root reaches nothing, and does not keep the new occupant. + h.collect(&[val(old)])?; + assert!(stale(&h, new) || !h.is_live(new)); + Ok(()) +} + +#[test] +fn a_long_chain_does_not_recurse() -> R<()> { + let mut h = Heap::new(); + let head = obj(&mut h)?; + let mut tail = head; + // Miri interprets every step, so it builds a shorter chain; the normal + // test job builds the full one. + let length = if cfg!(miri) { 2_000 } else { 100_000 }; + for _ in 0..length { + let n = obj(&mut h)?; + add_slot(&mut h, tail, val(n))?; + tail = n; + } + let out = h.collect(&[val(head)])?; + assert_eq!((out.freed, out.survivors), (0, length + 1)); + let out = h.collect(&[])?; + assert_eq!(out.freed, length + 1); + Ok(()) +} + +#[test] +fn the_value_stack_handles_and_kept_objects_are_roots() -> R<()> { + let mut h = Heap::new(); + let on_stack = obj(&mut h)?; + let handled = obj(&mut h)?; + let nested = obj(&mut h)?; + let garbage = obj(&mut h)?; + let scope = h.open_scope(); + h.root(val(handled))?; + let inner = h.open_scope(); + h.root(val(nested))?; + h.collect(&[val(on_stack)])?; + assert!(h.is_live(on_stack) && h.is_live(handled) && h.is_live(nested)); + assert!(stale(&h, garbage)); + h.close_scope(inner)?; + h.collect(&[])?; + assert!(h.is_live(handled) && !h.is_live(nested) && !h.is_live(on_stack)); + h.close_scope(scope)?; + h.collect(&[])?; + assert_eq!(h.live(), 0); + Ok(()) +} + +#[test] +fn scopes_close_in_order_and_handles_need_a_scope() -> R<()> { + let mut h = Heap::new(); + let a = obj(&mut h)?; + assert_eq!(h.root(val(a)), Err(Error::NoScope)); + let outer = h.open_scope(); + let inner = h.open_scope(); + assert_eq!(h.close_scope(outer), Err(Error::ScopeMismatch)); + h.close_scope(inner)?; + h.close_scope(Scope { depth: 1 })?; + // The mismatch above did not unroot anything. + let s = h.open_scope(); + h.root(val(a))?; + let t = h.open_scope(); + assert_eq!(h.close_scope(s), Err(Error::ScopeMismatch)); + h.collect(&[])?; + assert!(h.is_live(a)); + h.close_scope(t)?; + Ok(()) +} + +#[test] +fn a_collection_is_refused_during_an_operation() -> R<()> { + let mut h = Heap::new(); + let a = obj(&mut h)?; + assert_eq!(h.exit_op(), Err(Error::NotInOperation)); + h.enter_op(); + h.enter_op(); + assert_eq!(h.collect(&[]).err(), Some(Error::InOperation)); + h.exit_op()?; + assert_eq!(h.collect(&[]).err(), Some(Error::InOperation)); + h.exit_op()?; + assert!(h.is_live(a), "the refused collection freed nothing"); + assert_eq!(h.collect(&[])?.freed, 1); + Ok(()) +} + +// ---- WeakMap and WeakSet ---------------------------------------------- + +fn weak_map(h: &mut Heap, entries: &[(Ref, Value)]) -> R { + kind_of(h, Kind::WeakMap(entries.to_vec())) +} + +fn map_entries(h: &Heap, m: Ref) -> R> { + match &h.get(m)?.kind { + Kind::WeakMap(e) => Ok(e.clone()), + _ => Err(Error::NotAWeakRef), + } +} + +#[test] +fn a_weakmap_entry_lives_while_its_key_does() -> R<()> { + let mut h = Heap::new(); + let key = obj(&mut h)?; + let dead_key = obj(&mut h)?; + let value = obj(&mut h)?; + let dead_value = obj(&mut h)?; + let m = weak_map( + &mut h, + &[ + (key, val(value)), + (dead_key, val(dead_value)), + (key, int(1)), + ], + )?; + let out = h.collect(&[val(m), val(key)])?; + assert_eq!(out.freed, 2); + assert!(h.is_live(value) && stale(&h, dead_value) && stale(&h, dead_key)); + assert_eq!(map_entries(&h, m)?, [(key, val(value)), (key, int(1))]); + // Drop the key: the entries go, and so does the value. + h.collect(&[val(m)])?; + assert_eq!(map_entries(&h, m)?, []); + assert!(stale(&h, value) && stale(&h, key)); + Ok(()) +} + +#[test] +fn a_weakmap_does_not_keep_its_keys_and_an_unreachable_map_is_freed() -> R<()> { + let mut h = Heap::new(); + let key = obj(&mut h)?; + let value = obj(&mut h)?; + // The value refers to its key: still not enough to keep the key. + add_slot(&mut h, value, val(key))?; + let m = weak_map(&mut h, &[(key, val(value))])?; + h.collect(&[val(m)])?; + assert_eq!(h.live(), 1); + // Without the map, a rooted key keeps nothing of the map's. + let k2 = obj(&mut h)?; + let v2 = obj(&mut h)?; + let m2 = weak_map(&mut h, &[(k2, val(v2))])?; + h.collect(&[val(k2)])?; + assert!(h.is_live(k2) && stale(&h, v2) && stale(&h, m2) && stale(&h, m)); + Ok(()) +} + +#[test] +fn ephemeron_chains_resolve_in_any_entry_order() -> R<()> { + // Entries (k[i] -> k[i+1]) listed from the end of the chain to the start. + let mut h = Heap::new(); + // Marking is linear (GC5): a chain listed backwards used to resolve one + // link per pass, and 2000 keys took 2999 s under Miri. + let length: usize = if cfg!(miri) { 400 } else { 2000 }; + let ks: Vec = (0..length).map(|_| obj(&mut h)).collect::>()?; + let entries: Vec<(Ref, Value)> = ks + .windows(2) + .rev() + .filter_map(|w| Some((*w.first()?, val(*w.get(1)?)))) + .collect(); + let m = weak_map(&mut h, &entries)?; + let first = *ks.first().ok_or(Error::NoScope)?; + h.collect(&[val(m), val(first)])?; + assert_eq!(h.live(), length + 1, "the whole chain, and the map"); + h.collect(&[val(m)])?; + assert_eq!(h.live(), 1, "the map alone"); + Ok(()) +} + +#[test] +fn a_weakset_drops_dead_members_and_keeps_none_alive() -> R<()> { + let mut h = Heap::new(); + let live = obj(&mut h)?; + let dead = obj(&mut h)?; + let s = kind_of(&mut h, Kind::WeakSet(vec![live, dead, live]))?; + h.collect(&[val(s), val(live)])?; + assert!(matches!(&h.get(s)?.kind, Kind::WeakSet(m) if *m == [live, live])); + assert!(stale(&h, dead)); + h.collect(&[val(s)])?; + assert!(matches!(&h.get(s)?.kind, Kind::WeakSet(m) if m.is_empty())); + Ok(()) +} + +// ---- WeakRef ---------------------------------------------------------- + +#[test] +fn a_weakref_is_cleared_when_its_target_is_freed() -> R<()> { + let mut h = Heap::new(); + let target = obj(&mut h)?; + let w = kind_of(&mut h, Kind::WeakRef(Some(target)))?; + let w2 = kind_of(&mut h, Kind::WeakRef(Some(target)))?; + // Rooted target: both stay set. + h.collect(&[val(w), val(w2), val(target)])?; + assert_eq!(h.deref_weak(w)?, Some(target)); + h.end_job(); + // Unrooted: cleared, the target freed, and a clear WeakRef stays clear. + h.collect(&[val(w)])?; + assert_eq!(h.deref_weak(w)?, None); + assert!(stale(&h, target) && stale(&h, w2)); + h.collect(&[val(w)])?; + assert_eq!(h.deref_weak(w)?, None); + Ok(()) +} + +#[test] +fn a_target_read_in_a_job_is_kept_until_the_job_ends() -> R<()> { + let mut h = Heap::new(); + let target = obj(&mut h)?; + let w = kind_of(&mut h, Kind::WeakRef(Some(target)))?; + assert_eq!(h.deref_weak(w)?, Some(target)); + h.collect(&[val(w)])?; + assert!(h.is_live(target), "GC9: kept for the job"); + assert_eq!(h.deref_weak(w)?, Some(target)); + h.end_job(); + h.collect(&[val(w)])?; + assert!(stale(&h, target)); + assert_eq!(h.deref_weak(w)?, None); + Ok(()) +} + +#[test] +fn deref_of_something_that_is_not_a_weakref_is_an_error() -> R<()> { + let mut h = Heap::new(); + let o = obj(&mut h)?; + assert_eq!(h.deref_weak(o), Err(Error::NotAWeakRef)); + h.collect(&[])?; + assert_eq!(h.deref_weak(o), Err(Error::Arena(ArenaError::Stale))); + Ok(()) +} + +// ---- FinalizationRegistry --------------------------------------------- + +fn registry(h: &mut Heap, callback: Value, cells: Vec) -> R { + kind_of(h, Kind::Registry(Registry { callback, cells })) +} + +fn cell(target: Ref, held: Value, token: Option) -> Cell { + Cell { + target, + held, + token, + } +} + +fn cells_of(h: &Heap, r: Ref) -> R> { + match &h.get(r)?.kind { + Kind::Registry(reg) => Ok(reg.cells.clone()), + _ => Err(Error::NotAWeakRef), + } +} + +#[test] +fn a_dead_target_queues_one_cleanup_and_its_cell_goes() -> R<()> { + let mut h = Heap::new(); + let callback = obj(&mut h)?; + let held = obj(&mut h)?; + let alive = obj(&mut h)?; + let dies = obj(&mut h)?; + let dies_too = obj(&mut h)?; + let reg = registry( + &mut h, + val(callback), + vec![ + cell(dies, val(held), None), + cell(alive, int(5), None), + cell(dies_too, int(6), None), + ], + )?; + let out = h.collect(&[val(reg), val(alive)])?; + assert_eq!( + out.cleanups, + [ + Cleanup { + callback: val(callback), + held: val(held) + }, + Cleanup { + callback: val(callback), + held: int(6) + }, + ] + ); + // The callback and the held value outlive the collection (GC6). + assert!(h.is_live(callback) && h.is_live(held)); + assert!(stale(&h, dies) && stale(&h, dies_too)); + let left = cells_of(&h, reg)?; + assert_eq!(left.len(), 1); + assert_eq!(left.first().map(|c| c.target), Some(alive)); + // No cleanup is repeated. + assert_eq!(h.collect(&[val(reg), val(alive)])?.cleanups, []); + // When the target goes later, it is queued then. + let out = h.collect(&[val(reg)])?; + assert_eq!(out.cleanups.len(), 1); + Ok(()) +} + +#[test] +fn an_unreachable_registry_queues_nothing() -> R<()> { + let mut h = Heap::new(); + let target = obj(&mut h)?; + let reg = registry(&mut h, int(0), vec![cell(target, int(1), None)])?; + let out = h.collect(&[])?; + assert_eq!((out.freed, out.cleanups.len()), (2, 0)); + assert!(stale(&h, reg)); + Ok(()) +} + +#[test] +fn a_dead_unregister_token_is_cleared_and_a_live_one_kept() -> R<()> { + let mut h = Heap::new(); + let target = obj(&mut h)?; + let tok_live = obj(&mut h)?; + let tok_dead = obj(&mut h)?; + let reg = registry( + &mut h, + int(0), + vec![ + cell(target, int(1), Some(tok_live)), + cell(target, int(2), Some(tok_dead)), + ], + )?; + let out = h.collect(&[val(reg), val(target), val(tok_live)])?; + assert!(out.cleanups.is_empty()); + let cells = cells_of(&h, reg)?; + assert_eq!( + cells.iter().map(|c| c.token).collect::>(), + [Some(tok_live), None] + ); + assert!(stale(&h, tok_dead)); + Ok(()) +} + +#[test] +fn held_values_are_strong_so_a_held_value_naming_its_target_keeps_it() -> R<()> { + // The standard says so: the registry holds `held` strongly. + let mut h = Heap::new(); + let target = obj(&mut h)?; + let reg = registry(&mut h, int(0), vec![cell(target, val(target), None)])?; + let out = h.collect(&[val(reg)])?; + assert!(out.cleanups.is_empty() && h.is_live(target)); + Ok(()) +} + +// ---- The property test ------------------------------------------------ + +/// A small deterministic generator, so the test needs no dependency. +struct Lcg(u64); + +impl Lcg { + fn next(&mut self) -> u64 { + self.0 = self + .0 + .wrapping_mul(6_364_136_223_846_793_005) + .wrapping_add(1_442_695_040_888_963_407); + self.0 >> 33 + } + + /// A number below `n` (which is not zero). + fn below(&mut self, n: usize) -> usize { + let n = u64::try_from(n.max(1)).unwrap_or(1); + usize::try_from(self.next().checked_rem(n).unwrap_or(0)).unwrap_or(0) + } + + fn chance(&mut self, percent: usize) -> bool { + self.below(100) < percent + } + + fn pick(&mut self, live: &[Ref]) -> Option { + live.get(self.below(live.len())).copied() + } + + /// A reference to a live object about nine times in ten, else an int. + fn value(&mut self, live: &[Ref]) -> Value { + match self.pick(live) { + Some(r) if !self.chance(10) => val(r), + _ => int(i32::try_from(self.next() % 1000).unwrap_or(0)), + } + } +} + +/// Everything a collection may change about an object, as plain data. +#[derive(Clone, Debug, PartialEq)] +struct Snap { + proto: Option, + shape: ShapeId, + slots: Vec, + elements: Vec, + entries: Vec<(Ref, Value)>, + members: Vec, + target: Option, + callback: Option, + cells: Vec<(Ref, Value, Option)>, +} + +fn snap(o: &Object) -> Snap { + let mut s = Snap { + proto: o.proto, + shape: o.shape, + slots: o.slots.clone(), + elements: o.elements.values().collect(), + entries: vec![], + members: vec![], + target: None, + callback: None, + cells: vec![], + }; + match &o.kind { + Kind::Ordinary => {} + Kind::WeakMap(e) => s.entries.clone_from(e), + Kind::WeakSet(m) => s.members.clone_from(m), + Kind::WeakRef(t) => s.target = *t, + Kind::Registry(r) => { + s.callback = Some(r.callback); + s.cells = r + .cells + .iter() + .map(|c| (c.target, c.held, c.token)) + .collect(); + } + } + s +} + +fn snapshot(h: &Heap) -> Vec<(Ref, Snap)> { + h.arena().iter().map(|(r, o)| (r, snap(o))).collect() +} + +fn random_object(rng: &mut Lcg) -> Object { + Object::of(match rng.below(10) { + 0 | 1 => Kind::WeakMap(vec![]), + 2 => Kind::WeakSet(vec![]), + 3 => Kind::WeakRef(None), + 4 => Kind::Registry(Registry { + callback: int(0), + cells: vec![], + }), + _ => Kind::Ordinary, + }) +} + +/// Add random edges, all to live objects, from the live objects. +fn wire(h: &mut Heap, rng: &mut Lcg, live: &[Ref], keys: &[Key], density: usize) -> R<()> { + for &r in live { + if !rng.chance(density) { + continue; + } + // Properties through the shape table: few distinct keys, so shapes + // are shared and have parents. + for _ in 0..rng.below(4) { + let (key, v) = (keys.get(rng.below(keys.len())).copied(), rng.value(live)); + if let Some(key) = key { + match h.add_property(r, key, Attrs::ALL, v) { + Ok(_) | Err(Error::Shape(shape::Error::Exists)) => {} + Err(e) => return Err(e), + } + } + } + let proto = if rng.chance(30) { rng.pick(live) } else { None }; + let slots: Vec = (0..rng.below(4)).map(|_| rng.value(live)).collect(); + let elems: Vec = (0..rng.below(3)).map(|_| rng.value(live)).collect(); + let (t, k, v) = (rng.pick(live), rng.pick(live), rng.value(live)); + let (t2, k2) = (rng.pick(live), rng.pick(live)); + let o = h.get_mut(r)?; + if proto.is_some() { + o.proto = proto; + } + o.slots.extend(slots); + for (i, e) in elems.into_iter().enumerate() { + let _ = o.elements.set(u32::try_from(i).unwrap_or(0), e); + } + match &mut o.kind { + Kind::Ordinary => {} + Kind::WeakMap(es) => es.extend(k.map(|k| (k, v))), + Kind::WeakSet(ms) => ms.extend(t), + Kind::WeakRef(target) => *target = t, + Kind::Registry(reg) => { + reg.callback = v; + reg.cells.extend(t.map(|t| cell(t, Value::int32(1), k))); + reg.cells.extend(t2.map(|t| cell(t, v, k2))); + } + } + } + Ok(()) +} + +fn survived(before: &Ref, h: &Heap) -> bool { + h.get(*before).is_ok() +} + +/// The objects reachable from `roots`, by a second, independent walk over +/// the snapshot of the heap's public data: it shares nothing with +/// `Heap::graph` (whose omissions it would otherwise share), so the test +/// does not trust the extraction it checks. +fn oracle(before: &[(Ref, Snap)], roots: &[Ref]) -> HashSet { + let all: HashSet = before.iter().map(|(r, _)| *r).collect(); + let mut seen: HashSet = roots.iter().copied().filter(|r| all.contains(r)).collect(); + loop { + let mut add: Vec = vec![]; + for (r, s) in before { + if !seen.contains(r) { + continue; + } + add.extend(s.proto); + add.extend(s.slots.iter().chain(&s.elements).filter_map(|v| v.as_ref())); + add.extend(s.callback.and_then(|v| v.as_ref())); + add.extend(s.cells.iter().filter_map(|c| c.1.as_ref())); + add.extend( + s.entries + .iter() + .filter(|(k, _)| seen.contains(k)) + .filter_map(|(_, v)| v.as_ref()), + ); + } + let n = seen.len(); + seen.extend(add.into_iter().filter(|r| all.contains(r))); + if seen.len() == n { + return seen; + } + } +} + +/// A shape's properties: key, slot, attributes. +type Props = Vec<(Key, u32, Attrs)>; + +/// The live shapes and keys, as plain data from the public accessors. +struct Tables { + shapes: Vec<(ShapeId, Props)>, + keys: Vec<(Key, JsString)>, + issued_shapes: usize, + issued_keys: usize, +} + +fn tables(h: &Heap) -> Tables { + let issued_shapes = h.shapes().issued(); + let issued_keys = h.keys().issued(); + let shapes = (1..=issued_shapes) + .filter_map(|i| u32::try_from(i).ok().map(ShapeId)) + .filter(|&id| h.shapes().is_live(id)) + .map(|id| (id, h.shapes().properties(id))) + .collect(); + let keys = (0..issued_keys) + .filter_map(|i| u32::try_from(i).ok().map(Key)) + .filter_map(|k| h.key_string(k).map(|s| (k, s.clone()))) + .collect(); + Tables { + shapes, + keys, + issued_shapes, + issued_keys, + } +} + +/// GC10 to GC12's oracle: the shapes and keys that must survive, by an +/// independent walk over the public accessors (`Shapes::parent`, +/// `Shapes::properties`) and the snapshot, not `mark_shapes` or the table's +/// `ShapeGraph`. `objects` are the shapes of the objects that survive. +fn table_oracle( + h: &Heap, + objects: &[ShapeId], + ekeys: &[Key], + eshapes: &[ShapeId], +) -> (HashSet, HashSet) { + let mut shapes: HashSet = HashSet::from([ROOT]); + let mut stack: Vec = objects.iter().chain(eshapes).copied().collect(); + while let Some(id) = stack.pop() { + if h.shapes().is_live(id) && shapes.insert(id) { + stack.extend(h.shapes().parent(id)); + } + } + let mut keys: HashSet = ekeys + .iter() + .copied() + .filter(|&k| h.key_string(k).is_some()) + .collect(); + for &id in &shapes { + keys.extend(h.shapes().properties(id).into_iter().map(|(k, _, _)| k)); + } + (shapes, keys) +} + +/// What a collection must do to the shape and key tables. +struct TableWant { + before: Tables, + shapes: HashSet, + keys: HashSet, + spec_shapes: Vec, + spec_keys: Vec, +} + +fn entry(id: &ShapeId) -> Option { + usize::try_from(id.0).ok().and_then(|i| i.checked_sub(1)) +} + +/// The oracle's and the specification's sets, computed before a collection. +fn table_want( + h: &Heap, + survivors_shapes: &[ShapeId], + ekeys: &[Key], + eshapes: &[ShapeId], +) -> TableWant { + let before = tables(h); + let (shapes, keys) = table_oracle(h, survivors_shapes, ekeys, eshapes); + // The specification's sets, over the same table (GC10 to GC12). + let spec_roots: Vec = std::iter::once(0) + .chain(survivors_shapes.iter().filter_map(entry)) + .chain(eshapes.iter().filter_map(entry)) + .collect(); + let spec_root_keys: Vec = ekeys + .iter() + .filter(|&&k| h.key_string(k).is_some()) + .filter_map(|k| usize::try_from(k.0).ok()) + .collect(); + let (spec_shapes, spec_keys) = + contract::live_shapes(h.shapes(), &spec_roots, &spec_root_keys, before.issued_keys); + TableWant { + before, + shapes, + keys, + spec_shapes, + spec_keys, + } +} + +/// GC10 to GC13 after a collection; returns the freed shape ids and keys. +fn check_tables(h: &Heap, want: &TableWant, out: &Collection) -> R<(Vec, Vec)> { + let mut freed_shapes = vec![]; + for (id, props) in &want.before.shapes { + let live = h.shapes().is_live(*id); + assert_eq!(live, want.shapes.contains(id), "GC10/GC12 shape {id:?}"); + let i = entry(id).ok_or(Error::NoScope)?; + assert_eq!(live, want.spec_shapes.get(i).copied().unwrap_or(false)); + if live { + assert_eq!( + &h.shapes().properties(*id), + props, + "a live shape is as it was" + ); + } else { + freed_shapes.push(*id); + assert_eq!(h.shapes().slot_count(*id), None, "GC13"); + } + } + let mut freed_keys = vec![]; + for (k, string) in &want.before.keys { + let live = h.key_string(*k).is_some(); + assert_eq!(live, want.keys.contains(k), "GC11/GC12 key {k:?}"); + let i = usize::try_from(k.0).map_err(|_| Error::NoScope)?; + assert_eq!(live, want.spec_keys.get(i).copied().unwrap_or(false)); + if live { + assert_eq!(h.key_string(*k), Some(string), "GC11: the same string"); + assert_eq!(h.keys().find(string), Some(*k)); + } else { + freed_keys.push(*k); + assert_eq!(h.keys().find(string), None); + } + } + assert_eq!(out.freed_shapes, freed_shapes.len()); + assert_eq!(out.freed_keys, freed_keys.len()); + let after = tables(h); + assert_eq!(h.shapes().len(), after.shapes.len()); + assert_eq!(h.keys().len(), after.keys.len()); + // GC13: nothing was issued or renumbered. + assert_eq!(after.issued_shapes, want.before.issued_shapes); + assert_eq!(after.issued_keys, want.before.issued_keys); + assert!(h.shapes().is_live(ROOT), "ROOT is never freed"); + // GC10: every live object's shape, and every parent, is live. + for (_, o) in h.arena().iter() { + let mut at = Some(o.shape); + while let Some(id) = at { + assert!(h.shapes().is_live(id), "GC10"); + at = h.shapes().parent(id); + } + } + Ok((freed_shapes, freed_keys)) +} + +/// GC1 to GC6 and GC10 to GC13, checked for one collection of `h` against +/// the world before. `held` are the references the caller made roots other +/// than through `roots`: its open handles and the targets it kept for the +/// job. `ekeys` and `eshapes` are the keys and shapes it reports. Returns +/// the shape ids and keys that were freed. +fn collect_and_check( + h: &mut Heap, + roots: &[Value], + held: &[Ref], + ekeys: &[Key], + eshapes: &[ShapeId], +) -> R<(Vec, Vec)> { + let graph = h.graph(); + let root_idx = h.root_indices(roots); + let before = snapshot(h); + let all_roots: Vec = roots + .iter() + .filter_map(|v| v.as_ref()) + .chain(held.iter().copied()) + .collect(); + let want = oracle(&before, &all_roots); + let survivors_shapes: Vec = before + .iter() + .filter(|(r, _)| want.contains(r)) + .map(|(_, s)| s.shape) + .collect(); + let table_want = table_want(h, &survivors_shapes, ekeys, eshapes); + let out = h.collect_with(&RootSet { + values: roots, + keys: ekeys, + shapes: eshapes, + })?; + let freed = check_tables(h, &table_want, &out)?; + for (r, _) in &before { + assert_eq!( + want.contains(r), + h.get(*r).is_ok(), + "the oracle's reachability" + ); + } + + // Which slots survived, by (index, generation) as before. + let mut survives = vec![false; graph.len()]; + for (r, _) in &before { + let slot = usize::try_from(r.index) + .ok() + .and_then(|i| survives.get_mut(i)); + if let (true, Some(s)) = (survived(r, h), slot) { + *s = true; + } + } + // GC1 and GC2, against the specification's reachability. + assert!(contract::safe(&graph, &root_idx, &survives), "GC1"); + assert!(contract::exact(&graph, &root_idx, &survives), "GC2"); + assert_eq!(out.survivors, survives.iter().filter(|&&s| s).count()); + assert_eq!(out.freed.checked_add(out.survivors), Some(before.len())); + assert_eq!(h.live(), out.survivors); + // Every freed slot's old reference is a defined error (arena). + for (r, _) in &before { + if !survived(r, h) { + assert!(stale(h, *r)); + } + } + // GC3: the survivors' graph is closed, and no weak reference dangles. + let after = h.graph(); + assert!(contract::closed(&after, &survives), "GC3"); + let alive = |r: &Ref| h.is_live(*r); + for (r, s) in snapshot(h) { + assert!(s.entries.iter().all(|(k, _)| alive(k)), "GC3 map key"); + assert!(s.members.iter().all(alive), "GC3 set member"); + assert!(s.target.iter().all(alive), "GC3 weakref"); + assert!( + s.cells + .iter() + .all(|(t, _, k)| alive(t) && k.iter().all(alive)) + ); + // GC4: weak references to survivors are unchanged, the rest removed, + // and nothing else about a survivor changed. + let was = before + .iter() + .find(|(b, _)| *b == r) + .map(|(_, s)| s.clone()) + .ok_or(Error::NoScope)?; + let keep = |x: &Ref| survived(x, h); + let want = Snap { + entries: was + .entries + .iter() + .copied() + .filter(|(k, _)| keep(k)) + .collect(), + members: was.members.iter().copied().filter(keep).collect(), + target: was.target.filter(keep), + cells: was + .cells + .iter() + .filter(|(t, _, _)| keep(t)) + .map(|&(t, hv, k)| (t, hv, k.filter(keep))) + .collect(), + ..was + }; + assert_eq!(s, want, "GC4"); + } + // GC6: one cleanup per dead cell of a surviving registry, in order. + let mut cleanups = vec![]; + for (r, s) in &before { + if let (true, Some(callback)) = (survived(r, h), s.callback) { + for (t, held, _) in &s.cells { + if !survived(t, h) { + cleanups.push(Cleanup { + callback, + held: *held, + }); + } + } + } + } + assert_eq!(out.cleanups, cleanups, "GC6"); + // Their callbacks and held values are alive. + for c in &out.cleanups { + for v in [c.callback, c.held] { + assert!(v.as_ref().is_none_or(|r| h.is_live(r))); + } + } + assert!(h.arena().is_well_formed()); + Ok(freed) +} + +fn live_refs(h: &Heap) -> Vec { + h.arena().iter().map(|(r, _)| r).collect() +} + +/// The names of the property keys of the property test: enough that one +/// object can pass the dictionary cutoff. Under Miri, 16: a chain of 128 +/// shapes made the specification's fixpoint and the oracle's walks too slow +/// (the first version of this test took 837 s), and the dictionary is +/// covered by its own unit test there. +fn pool_strings() -> R> { + let n = if cfg!(miri) { + 16 + } else { + MAX_FAST_PROPERTIES + 12 + }; + (0..n) + .map(|i| JsString::from_rust_str(&format!("key{i}")).map_err(|_| Error::Intern)) + .collect() +} + +/// The pool's keys, interned again (a freed one gets a new key, GC13). +fn intern_pool(h: &mut Heap, pool: &[JsString]) -> R> { + pool.iter().map(|s| h.intern(s)).collect() +} + +#[test] +fn random_heaps_of_any_size_keep_what_the_roots_reach() -> R<()> { + let mut rng = Lcg(0x005E_ED6C); + let mut cases = 0; + // Under Miri, the first 2 cases of the same sequence (at a fraction of + // their size); the normal test job runs all 240. + let count: usize = if cfg!(miri) { 2 } else { 240 }; + for case in 0..count { + // Mostly small heaps, a few large ones. + let n = match case % 20 { + // Under Miri, a thirtieth of the size, from the same draw. + 0 if cfg!(miri) => 50 + rng.below(50), + 0 => 1500 + rng.below(1500), + // Under Miri, a heap of 20 to 80: the checks are quadratic, and + // 300 objects took 570 s. + 1..=4 if cfg!(miri) => 20 + rng.below(60), + 1..=4 => 50 + rng.below(250), + _ => rng.below(30), + }; + let mut h = Heap::new(); + let mut live = Vec::new(); + for _ in 0..n { + live.push(h.alloc(random_object(&mut rng))?); + } + let density = 20 + rng.below(80); + let pool = pool_strings()?; + let mut keys = intern_pool(&mut h, &pool)?; + // Ids freed so far: they must stay freed, whatever is added later. + let mut dead_shapes: Vec = vec![]; + let mut dead_keys: Vec = vec![]; + wire( + &mut h, + &mut rng, + &live, + keys.get(..12).unwrap_or(&[]), + density, + )?; + // In two cases of the twenty, one object has so many properties that + // its shape is a dictionary of its own. + if let (true, Some(&big)) = (case % 20 < 2 && !cfg!(miri), live.first()) { + for &k in &keys { + match h.add_property(big, k, Attrs::ALL, Value::int32(1)) { + Ok(_) | Err(Error::Shape(shape::Error::Exists)) => {} + Err(e) => return Err(e), + } + } + assert!(h.shapes().is_dictionary(h.get(big)?.shape)); + } + // Several rounds: collect with random roots, then mutate the + // survivors, reuse freed slots, and collect again. + for _ in 0..4 { + let live = live_refs(&h); + let mut roots: Vec = (0..rng.below(1 + live.len() / 4)) + .map(|_| rng.value(&live)) + .collect(); + // The embedder reports some keys and shapes it holds. + let ekeys: Vec = keys.iter().copied().filter(|_| rng.chance(3)).collect(); + let eshapes: Vec = (1..=h.shapes().issued()) + .filter_map(|i| u32::try_from(i).ok().map(ShapeId)) + .filter(|&id| h.shapes().is_live(id) && rng.chance(2)) + .collect(); + // Some roots are handles; some targets are kept for the job. + if rng.chance(50) { + let scope = h.open_scope(); + let mut held: Vec = vec![]; + for v in roots.drain(..roots.len() / 2) { + h.root(v)?; + held.extend(v.as_ref()); + } + for &r in &live { + if rng.chance(3) { + held.extend(h.deref_weak(r).ok().flatten()); + } + } + let freed = collect_and_check(&mut h, &roots, &held, &ekeys, &eshapes)?; + dead_shapes.extend(freed.0); + dead_keys.extend(freed.1); + h.close_scope(scope)?; + } else { + let freed = collect_and_check(&mut h, &roots, &[], &ekeys, &eshapes)?; + dead_shapes.extend(freed.0); + dead_keys.extend(freed.1); + } + h.end_job(); + cases += 1; + let survivors = live_refs(&h); + for _ in 0..rng.below(20) { + h.alloc(random_object(&mut rng))?; + } + let all = live_refs(&h); + let some: Vec = survivors + .iter() + .copied() + .filter(|_| rng.chance(50)) + .collect(); + let fresh: Vec = all + .iter() + .copied() + .filter(|r| !survivors.contains(r)) + .collect(); + // The interned strings come back with new keys when freed, and a + // stale key is refused. + let old = keys; + keys = intern_pool(&mut h, &pool)?; + for (a, b) in old.iter().zip(&keys) { + assert_eq!(a == b, h.key_string(*a).is_some()); + } + if let (Some(&k), Some(&r)) = (dead_keys.first(), some.first()) { + let stale = h.add_property(r, k, Attrs::ALL, Value::int32(0)); + assert_eq!(stale, Err(Error::StaleKey)); + } + wire(&mut h, &mut rng, &some, keys.get(..12).unwrap_or(&[]), 60)?; + wire(&mut h, &mut rng, &fresh, keys.get(..12).unwrap_or(&[]), 100)?; + // GC13: nothing freed has come back, under its old id. + assert!(dead_shapes.iter().all(|&id| !h.shapes().is_live(id))); + assert!(dead_keys.iter().all(|&k| h.key_string(k).is_none())); + } + } + assert_eq!(cases, count * 4); + Ok(()) +} + +/// The marker agrees with the specification on random graphs, including +/// ones with wild edges (no such node) that a heap's graph never has. +#[test] +fn mark_equals_the_specification_on_random_graphs() { + let mut rng = Lcg(0xA11CE); + // Under Miri, the first 100 graphs of the same sequence. + let graphs = if cfg!(miri) { 100 } else { 2000 }; + for _ in 0..graphs { + let n = rng.below(12); + let name = |rng: &mut Lcg| rng.below(n + 2); + let graph: Vec = (0..n) + .map(|_| Node { + strong: (0..rng.below(3)).map(|_| name(&mut rng)).collect(), + ephemerons: (0..rng.below(3)) + .map(|_| (name(&mut rng), name(&mut rng))) + .collect(), + }) + .collect(); + let roots: Vec = (0..rng.below(4)).map(|_| name(&mut rng)).collect(); + let (marked, units) = mark_counted(&graph, &roots); + assert_eq!(marked, contract::reachable(&graph, &roots)); + let (nodes, strong, eph) = contract::totals(&graph); + assert!(units <= contract::work_bound(nodes, strong, eph), "GC5"); + } +} + +/// A map (node 0) of the chain `1 -> 2 -> ... -> n` (entry `(i, i + 1)`), +/// listed forwards or backwards, and the work of marking from the map and +/// the first key. +fn chain_work(n: usize, backwards: bool) -> (usize, usize) { + let mut entries: Vec<(usize, usize)> = (1..n).map(|i| (i, i.saturating_add(1))).collect(); + if backwards { + entries.reverse(); + } + let mut graph = vec![Node::default(); n.saturating_add(1)]; + if let Some(map) = graph.first_mut() { + map.ephemerons = entries; + } + let (marked, units) = mark_counted(&graph, &[0, 1]); + assert!(marked.iter().all(|&m| m), "the whole chain is marked"); + let (nodes, strong, eph) = contract::totals(&graph); + assert!(units <= contract::work_bound(nodes, strong, eph), "GC5"); + (units, nodes.saturating_add(strong).saturating_add(eph)) +} + +/// GC5, counted rather than timed: the work on a chain listed in either +/// order is a bounded number of units per element, and doubling the length +/// doubles it (a quadratic marker would quadruple it). +#[test] +fn ephemeron_marking_work_is_linear_in_either_order() { + let sizes: &[usize] = if cfg!(miri) { + &[50, 100, 200] + } else { + &[500, 1000, 2000, 4000] + }; + let mut last: Option = None; + for &n in sizes { + let (fwd, _) = chain_work(n, false); + let (back, size) = chain_work(n, true); + // Listed forwards, each key is marked before its entry is examined; + // backwards, each entry waits on its key and is fired: at most two + // units more per entry, in either order. + assert!(fwd <= back, "waiting costs, at most, a firing per entry"); + assert!(back <= 4 * size, "a bounded number of units per element"); + if let Some(prev) = last { + assert!(back <= 2 * prev + 8, "doubling the chain doubles the work"); + } + last = Some(back); + } +} + +/// Many entries waiting on one key, and one value kept by many keys: each +/// entry is still handled once. +#[test] +fn many_entries_on_one_key_and_many_keys_for_one_value_are_linear() { + let n = if cfg!(miri) { 100 } else { 3000 }; + // Node 0: the map. 1: the shared key. 2: the shared value. Nodes 3..: + // keys that are marked last, each with an entry to node 2, and an entry + // (1, i) from the shared key to each of them. + let mut graph = vec![Node::default(); n + 3]; + let mut entries: Vec<(usize, usize)> = (3..n + 3).map(|i| (1, i)).collect(); + entries.extend((3..n + 3).map(|i| (i, 2))); + if let Some(map) = graph.first_mut() { + map.ephemerons = entries; + } + let (marked, units) = mark_counted(&graph, &[0, 1]); + assert!(marked.iter().all(|&m| m)); + let (nodes, strong, eph) = contract::totals(&graph); + assert!(units <= contract::work_bound(nodes, strong, eph)); + // And without the shared key, nothing else is marked. + let (marked, _) = mark_counted(&graph, &[0]); + assert_eq!(marked.iter().filter(|&&m| m).count(), 1); +} + +// ---- Strings and shapes ---------------------------------------------------- + +fn key(h: &mut Heap, s: &str) -> R { + h.intern(&JsString::from_rust_str(s).map_err(|_| Error::Intern)?) +} + +fn shape_of(h: &Heap, r: Ref) -> R { + Ok(h.get(r)?.shape) +} + +#[test] +fn a_live_objects_shape_and_keys_survive_and_the_rest_are_freed() -> R<()> { + let mut h = Heap::new(); + let (a, b, c) = (key(&mut h, "a")?, key(&mut h, "b")?, key(&mut h, "c")?); + let keep = obj(&mut h)?; + let drop = obj(&mut h)?; + h.add_property(keep, a, Attrs::ALL, int(1))?; + h.add_property(drop, b, Attrs::ALL, int(2))?; + let kept_shape = shape_of(&h, keep)?; + let dropped_shape = shape_of(&h, drop)?; + // `c` is interned and used by nothing. + let out = h.collect(&[val(keep)])?; + assert_eq!((out.freed_shapes, out.freed_keys), (1, 2)); + assert!(h.shapes().is_live(kept_shape) && h.shapes().is_live(ROOT)); + assert!(!h.shapes().is_live(dropped_shape)); + assert_eq!(h.shapes().lookup(kept_shape, a), Some((0, Attrs::ALL))); + assert!(h.key_string(a).is_some()); + assert!(h.key_string(b).is_none() && h.key_string(c).is_none()); + assert_eq!(h.keys().len(), 1); + Ok(()) +} + +#[test] +fn a_shape_keeps_its_parents_but_a_parent_does_not_keep_its_children() -> R<()> { + let mut h = Heap::new(); + let (a, b) = (key(&mut h, "a")?, key(&mut h, "b")?); + let o = obj(&mut h)?; + h.add_property(o, a, Attrs::ALL, int(1))?; + let s1 = shape_of(&h, o)?; + h.add_property(o, b, Attrs::ALL, int(2))?; + let s2 = shape_of(&h, o)?; + // A second object stays at {a}, and the first dies: the child {a, b} + // goes, its parent {a} stays, and so does the key `a`, not `b`. + let other = obj(&mut h)?; + h.add_property(other, a, Attrs::ALL, int(1))?; + assert_eq!(shape_of(&h, other)?, s1, "shared"); + h.collect(&[val(other)])?; + assert!(h.shapes().is_live(s1) && !h.shapes().is_live(s2)); + assert!(h.key_string(a).is_some() && h.key_string(b).is_none()); + // The transition to the freed child is gone: the same property added + // again is a new shape, never the old id, and a stale key is refused. + assert_eq!( + h.add_property(other, b, Attrs::ALL, int(3)), + Err(Error::StaleKey) + ); + let b2 = key(&mut h, "b")?; + assert_ne!(b2, b, "a freed key id is not reused"); + h.add_property(other, b2, Attrs::ALL, int(3))?; + let s3 = shape_of(&h, other)?; + assert!(s3.0 > s2.0, "a freed shape id is not reused"); + assert_eq!(h.shapes().lookup(s3, a), Some((0, Attrs::ALL))); + // With the object gone, so is everything but the root. + h.collect(&[])?; + assert!(!h.shapes().is_live(s1) && !h.shapes().is_live(s3)); + assert_eq!(h.shapes().len(), 1); + assert_eq!(h.keys().len(), 0); + Ok(()) +} + +#[test] +fn a_dictionary_shape_dies_with_its_object_and_its_keys_with_it() -> R<()> { + let mut h = Heap::new(); + let o = obj(&mut h)?; + let mut keys = vec![]; + for i in 0..=MAX_FAST_PROPERTIES { + let k = key(&mut h, &format!("p{i}"))?; + keys.push(k); + h.add_property(o, k, Attrs::ALL, int(1))?; + } + let dict = shape_of(&h, o)?; + assert!(h.shapes().is_dictionary(dict)); + let out = h.collect(&[val(o)])?; + // A dictionary has no parent: the shared chain below it is garbage, but + // the dictionary holds every key. + assert_eq!(out.freed_shapes, MAX_FAST_PROPERTIES); + assert_eq!(out.freed_keys, 0); + assert!(keys.iter().all(|&k| h.key_string(k).is_some())); + let out = h.collect(&[])?; + assert!(!h.shapes().is_live(dict)); + assert_eq!(out.freed_keys, MAX_FAST_PROPERTIES + 1); + assert_eq!((h.shapes().len(), h.keys().len()), (1, 0)); + Ok(()) +} + +#[test] +fn the_embedder_keeps_keys_and_shapes_it_reports() -> R<()> { + let mut h = Heap::new(); + let (a, b) = (key(&mut h, "a")?, key(&mut h, "b")?); + let o = obj(&mut h)?; + h.add_property(o, a, Attrs::ALL, int(1))?; + let s1 = shape_of(&h, o)?; + // `o` dies, but its shape is an inline cache's, and `b` a constant's. + let out = h.collect_with(&RootSet { + values: &[], + keys: &[b], + shapes: &[s1], + })?; + assert_eq!(out.freed_shapes, 0); + assert!(h.shapes().is_live(s1)); + assert!(h.key_string(a).is_some(), "the reported shape names `a`"); + assert!(h.key_string(b).is_some()); + // A stale or made-up root is ignored. + let out = h.collect_with(&RootSet { + values: &[], + keys: &[Key(77)], + shapes: &[ShapeId(0), ShapeId(500)], + })?; + assert_eq!((out.freed_shapes, out.freed_keys), (1, 2)); + assert_eq!(h.shapes().len(), 1); + Ok(()) +} + +#[test] +fn an_object_needs_a_live_shape_and_a_refused_collection_frees_nothing() -> R<()> { + let mut h = Heap::new(); + let mut o = Object::ordinary(); + o.shape = ShapeId(40); + assert_eq!( + h.alloc(o).map(|_| ()), + Err(Error::Shape(shape::Error::Unknown)) + ); + let k = key(&mut h, "k")?; + h.enter_op(); + assert_eq!(h.collect(&[]).map(|_| ()), Err(Error::InOperation)); + assert!(h.key_string(k).is_some(), "nothing changed"); + h.exit_op()?; + Ok(()) +} + +/// A shape table for the marker's agreement with the specification: +/// `(exists, parent, keys)` per entry. +struct Table(Vec<(bool, Option, Vec)>); + +impl contract::ShapeGraph for Table { + fn len(&self) -> usize { + self.0.len() + } + fn exists(&self, i: usize) -> bool { + self.0.get(i).is_some_and(|e| e.0) + } + fn parent(&self, i: usize) -> Option { + self.0.get(i).and_then(|e| e.1) + } + fn each_key(&self, i: usize, f: &mut dyn FnMut(usize)) { + for &k in self.0.get(i).map_or(&[][..], |e| &e.2) { + f(k); + } + } +} + +/// `mark_shapes` equals the specification on random tables, including wild +/// ones a real table never has (cycles, parents that do not exist, keys out +/// of range, roots naming nothing). +#[test] +fn mark_shapes_equals_the_specification_on_random_tables() { + let mut rng = Lcg(0x5AA5E); + let tables = if cfg!(miri) { 100 } else { 3000 }; + for _ in 0..tables { + let n = rng.below(14); + let key_count = rng.below(8); + let table: Vec<(bool, Option, Vec)> = (0..n) + .map(|_| { + let parent = rng.chance(70).then(|| rng.below(n + 2)); + let keys = (0..rng.below(3)) + .map(|_| rng.below(key_count + 2)) + .collect(); + (rng.chance(85), parent, keys) + }) + .collect(); + let g = Table(table); + let roots: Vec = (0..rng.below(4)).map(|_| rng.below(n + 2)).collect(); + let root_keys: Vec = (0..rng.below(3)) + .map(|_| rng.below(key_count + 2)) + .collect(); + let mut shapes = vec![false; n]; + let mut keys = vec![false; key_count]; + mark_shapes(&g, &roots, &root_keys, &mut shapes, &mut keys); + assert!(contract::shapes_exact( + &g, &roots, &root_keys, &shapes, &keys + )); + } +} diff --git a/crates/clazel-js-heap/src/lib.rs b/crates/clazel-js-heap/src/lib.rs index 320530151..c76b66777 100644 --- a/crates/clazel-js-heap/src/lib.rs +++ b/crates/clazel-js-heap/src/lib.rs @@ -11,15 +11,24 @@ //! is under 256, and an interner for property keys. //! - [`shape`]: hidden classes with transitions and a dictionary-mode cutoff. //! -//! The collector (item 7.6) is not here. The arena leaves room for it: it -//! can enumerate live slots, and freeing is one call. +//! - [`elements`]: an array's indexed properties, dense with a stated +//! fallback to sparse; [`elements::contract`] is the spec. +//! - [`symbol`]: symbols (well-known ones at fixed ids) and the property key +//! that is a string or a symbol; [`symbol::contract`] is the spec. +//! +//! - [`gc`]: the collector (item 7.6): stop-the-world mark and sweep over +//! the arena, with ephemerons, `WeakRef` and `FinalizationRegistry`; +//! [`gc::contract`] is the spec. //! //! Tier: see `[package.metadata.clazel]` in `Cargo.toml` and //! `docs/VERIFICATION.md`. pub mod arena; +pub mod elements; +pub mod gc; pub mod shape; pub mod string; +pub mod symbol; pub mod value; #[cfg(kani)] diff --git a/crates/clazel-js-heap/src/proofs.rs b/crates/clazel-js-heap/src/proofs.rs index 1e04573b8..5ebdc294d 100644 --- a/crates/clazel-js-heap/src/proofs.rs +++ b/crates/clazel-js-heap/src/proofs.rs @@ -178,3 +178,380 @@ fn value_roundtrip_any() { None => assert!(p > 0xFFFF_FFFF_FFFF), } } + +// ---- Second slice: elements and symbols ------------------------------- +// +// Bounds, stated. The dense operations (`elements::dense`) are proven for +// one operation on a three-slot vector with indices below 5 (`dense_one_op`) +// and two sets from empty with indices below 6 (`dense_set_pair`). The fallback rule (E3) is +// proven for every `u32` index as the pure function `choose`. NOT proven: +// the sparse representation, the switch from dense to sparse, and +// `Elements` as a whole. Harnesses through `Elements::set` (which reaches +// `BTreeMap`) ran for over eight minutes without finishing and were +// dropped; those paths are covered by unit tests, including random +// sequences against the abstract array (`elements::contract::Spec`), which +// is a test, not a proof. Symbol harnesses cover the key encoding (Y4) for +// all `u32`; the table's construction (Y1 to Y3) formats strings and is +// covered by unit tests only. + +use crate::elements::Elements; +use crate::elements::contract::{HOLE_BITS, MAX_DENSE, MAX_GAP, Mode, choose}; +use crate::elements::dense; +use crate::string::Key; +use crate::symbol::contract::{PropertyKey, SYMBOL_BIT, SymbolId, decode, encode}; + +/// E3's rule is total and keeps dense storage within E4's bound. +#[kani::proof] +fn elements_choose_any() { + let slots: u32 = kani::any(); + let index: u32 = kani::any(); + kani::assume(slots <= MAX_DENSE); + let m = choose(Mode::Dense, slots, index); + if m == Mode::Dense { + // Either in range of the slots, or a write of at most MAX_GAP holes + // below MAX_DENSE: the slot count after is at most MAX_DENSE. + assert!(index < slots || (index < MAX_DENSE && index.saturating_sub(slots) <= MAX_GAP)); + assert!(index < MAX_DENSE); + } else { + assert!(index >= slots); + } + assert_eq!(choose(Mode::Sparse, slots, index), Mode::Sparse); +} + +/// One operation (set, delete or truncate) on a dense vector of three +/// slots holding any bit patterns, against a three-cell model: reads, the +/// slot count's bound by the length (E2) and by `MAX_DENSE` (E4). +#[kani::proof] +#[kani::unwind(6)] +fn dense_one_op() { + let a: u64 = kani::any(); + let b: u64 = kani::any(); + let c: u64 = kani::any(); + let mut v: Vec = vec![a, b, c]; + let mut cells = [a, b, c]; + let i: u32 = kani::any(); + kani::assume(i < 5); + let mut length: u32 = 3; + match kani::any::() % 3 { + 0 => { + let x: u64 = kani::any(); + kani::assume(x != HOLE_BITS); + assert!(dense::set(&mut v, i, x).is_ok()); + if let Some(cell) = usize::try_from(i).ok().and_then(|i| cells.get_mut(i)) { + *cell = x; + } + length = length.max(i.saturating_add(1)); + } + 1 => { + let idx = usize::try_from(i).ok(); + let had = idx + .and_then(|i| cells.get(i)) + .is_some_and(|&x| x != HOLE_BITS); + assert_eq!(dense::delete(&mut v, i), had); + if let Some(cell) = idx.and_then(|i| cells.get_mut(i)) { + *cell = HOLE_BITS; + } + } + _ => { + dense::truncate(&mut v, i); + length = i; + } + } + assert!(u32::try_from(v.len()).is_ok_and(|n| n <= length && n <= MAX_DENSE)); + // Slots the model has (those below the vector's length) read back. + for k in 0..3u32 { + let kept = usize::try_from(k).ok().is_some_and(|k| k < v.len()); + let want = cells.get(usize::try_from(k).unwrap_or(0)).copied(); + if kept { + assert_eq!(dense::get(&v, k), want); + } else { + assert_eq!(dense::get(&v, k), None); + } + } +} + +/// `dense::set` at any index of the small range, after one set at 0: +/// both elements read back, nothing else appears (E1 for the dense case). +#[kani::proof] +#[kani::unwind(8)] +fn dense_set_pair() { + let mut v: Vec = Vec::new(); + assert!(dense::set(&mut v, 0, 1).is_ok()); + let index: u32 = kani::any(); + kani::assume(index < 6); + assert!(dense::set(&mut v, index, 2).is_ok()); + assert_eq!(dense::get(&v, index), Some(2)); + if index != 0 { + assert_eq!(dense::get(&v, 0), Some(1)); + } + let want_len = index.saturating_add(1).max(1); + assert_eq!(u32::try_from(v.len()), Ok(want_len)); + assert_eq!(dense::get(&v, want_len), None); +} + +/// An empty array has no element at any index. +#[kani::proof] +fn elements_empty_get_any() { + let e = Elements::new(); + let i: u32 = kani::any(); + assert_eq!(e.get(i), None); + assert_eq!(e.len(), 0); +} + +/// Y4: for every `u32` key, decoding then encoding gives it back, and a +/// string and a symbol with the same number never share a key. +#[kani::proof] +fn symbol_key_encoding_any() { + let k: u32 = kani::any(); + assert_eq!(encode(decode(Key(k))), Some(Key(k))); + let n: u32 = kani::any(); + kani::assume(n < SYMBOL_BIT); + let s = encode(PropertyKey::String(Key(n))); + let y = encode(PropertyKey::Symbol(SymbolId(n))); + assert!(s.is_some() && y.is_some()); + assert_ne!(s, y); + assert_eq!(s.map(decode), Some(PropertyKey::String(Key(n)))); + assert_eq!(y.map(decode), Some(PropertyKey::Symbol(SymbolId(n)))); + // Numbers at or above 2^31 are refused, never wrapped (Y5). + let big: u32 = kani::any(); + kani::assume(big >= SYMBOL_BIT); + assert_eq!(encode(PropertyKey::String(Key(big))), None); + assert_eq!(encode(PropertyKey::Symbol(SymbolId(big))), None); +} + +// ---- Third slice: the collector ----------------------------------------- +// +// Bounds, stated. `gc_mark_closed_over_roots` proves the real marker +// (`gc::mark_into`, which `gc::mark` and so `Heap::collect` call) for every +// graph of `GRAPH` (3) nodes where each node has one strong edge and the +// first `EPH` (2) nodes have one ephemeron each, each edge naming any node +// or no node (a stale reference, or no edge), with any `GRAPH` roots: the +// marked set contains the roots and is closed under the rules that define +// reachability (a strong edge of a marked node; an ephemeron value whose +// map and key are marked). `gc::contract::reachable` is the least set with +// those two properties, so it is inside the marked set: every reachable +// node survives (GC1). That is the kernel of "a slot reachable from the +// roots is never freed". It takes about 5 minutes. +// +// This is a smaller bound than the quadratic marker's (4 nodes, an +// ephemeron on each, about 2 minutes): the marker now indexes pending +// ephemerons by key, in linked lists CBMC handles badly. These did not +// finish: 4 nodes with 4 ephemerons (over 13 CPU minutes), 4 nodes with 2 +// ephemerons (over 10), 3 nodes with 3 ephemerons and the work counter +// (over 9), so none was kept. Two ephemerons cover a chain of two entries, +// the key marked before and after its entry is examined. +// +// GC5's work bound (`gc::contract::work_bound`) is NOT proven: the +// harness with the counter did not finish (above). It is tested: +// `gc::tests` counts the work on chains of thousands of entries in either +// order and on random graphs, and the bound is read off the loops (no node or +// entry is handled twice), which is not a proof. +// +// NOT proven by Kani: +// - GC2 (nothing unreachable is kept), by tests only. +// - `Heap::collect` as a whole: graph extraction, weak clearing, the sweep +// (GC3, GC4, GC6) and the safe-point guard. A harness over a heap of two +// objects (one slot value each, a `WeakRef`, any roots) was written and +// ran past a 10-minute limit without finishing, as the elements harnesses +// through `Vec`s and `BTreeMap` did, and was dropped. A first version of +// the marker harness over `Vec`s ran out of memory; `mark_into` takes +// slices so that arrays stand in for the vectors. Freeing goes through +// `Arena::free`, whose invariants the arena harnesses prove. +// - Graphs past `GRAPH` nodes, and arbitrary operation sequences. The +// property test in `gc::tests` checks GC1 to GC6 on random heaps of up +// to 3,000 objects against an independent oracle, which is a test, not a +// proof. + +use crate::gc::contract::Graph; +use crate::gc::mark_into; + +/// Nodes in the graphs of `gc_mark_closed_over_roots`. +const GRAPH: usize = 3; + +/// How many of them (the first ones) have an ephemeron (see the header). +const EPH: usize = 2; + +/// A node name, or one that names no node. +fn any_name(n: usize) -> usize { + let i: usize = kani::any(); + kani::assume(i <= n); + i +} + +/// A graph of `GRAPH` nodes as fixed arrays (vectors of vectors made CBMC +/// run for over ten minutes): each node has one strong edge, the first +/// `EPH` have an ephemeron, and a name past the last node (`GRAPH`) is "no node", which +/// reaches nothing, so it stands for the absence of the edge as well as for +/// a stale reference. +struct Fixed { + strong: [usize; GRAPH], + eph: [(usize, usize); EPH], +} + +impl Graph for Fixed { + fn len(&self) -> usize { + GRAPH + } + fn strong(&self, i: usize) -> &[usize] { + self.strong.get(i..=i).unwrap_or(&[]) + } + fn ephemerons(&self, i: usize) -> &[(usize, usize)] { + self.eph.get(i..=i).unwrap_or(&[]) + } +} + +#[kani::proof] +#[kani::solver(cadical)] +#[kani::unwind(5)] +fn gc_mark_closed_over_roots() { + let mut g = Fixed { + strong: [0; GRAPH], + eph: [(0, 0); EPH], + }; + for s in &mut g.strong { + *s = any_name(GRAPH); + } + for e in &mut g.eph { + *e = (any_name(GRAPH), any_name(GRAPH)); + } + // Any `GRAPH` roots, each a node or none: any subset of the nodes. + let mut roots = [0usize; GRAPH]; + for r in &mut roots { + *r = any_name(GRAPH); + } + let mut marked = [false; GRAPH]; + let mut work = [0usize; GRAPH]; + let mut head = [usize::MAX; GRAPH]; + let mut link = [(0usize, usize::MAX); EPH]; + let _ = mark_into(&g, &roots, &mut marked, &mut work, &mut head, &mut link); + // GC1. `marked` contains the roots, and is closed under the rules that + // define reachability (a strong edge of a marked node, an ephemeron + // value whose key and map are marked). `contract::reachable` is the + // least set with those two properties, so it is inside `marked`: every + // reachable node survives. (Equality, GC2, is checked by the unit test + // `mark_equals_the_specification_on_random_graphs`, and on heaps by the + // property test.) + let is = |i: usize| marked.get(i).copied().unwrap_or(false); + for &r in &roots { + assert!(r >= GRAPH || is(r)); + } + for i in 0..GRAPH { + if is(i) { + let e = g.strong.get(i).copied().unwrap_or(GRAPH); + assert!(e >= GRAPH || is(e)); + let (k, v) = g.eph.get(i).copied().unwrap_or((GRAPH, GRAPH)); + assert!(k >= GRAPH || v >= GRAPH || !is(k) || is(v)); + } + } +} + +// ---- Fourth slice: strings and shapes -------------------------------------- +// +// `gc_mark_shapes_is_the_least_closed_set` proves the real marker +// (`gc::mark_shapes`, which `Heap::collect` calls) on every table of `TABLE` +// shape entries and `TABLE` keys: each entry may exist or not, have any +// parent (an entry, or none, cycles included) and name any key or none, with +// any `TABLE` root shapes and `TABLE` root keys (an entry or key, or none). +// It proves both halves of `gc::contract::live_shapes`: +// - safety (GC10, GC11): every root shape that exists, every parent of a +// marked shape that exists, every key of a marked shape and every root key +// is marked; +// - precision (GC12): a marked shape is a root or the parent of a marked +// shape, and a marked key is a root key or the key of a marked shape. +// A closed set that is also supported by the roots is the least one, so the +// marked sets are exactly `live_shapes`. +// +// NOT proven by Kani, for want of a harness that finishes: `Shapes::sweep` +// and `Interner::sweep` and the transition clean-up (they walk `HashMap`s, +// which CBMC models as paths, as the elements harnesses' `BTreeMap`s did); +// `Heap::collect_with` as a whole; and a table past `TABLE` entries. Those +// are tested (`gc::tests`, `shape::tests`, `string::tests`), against an +// independent oracle in the property test. + +use crate::gc::contract::ShapeGraph; +use crate::gc::mark_shapes; + +/// Entries and keys in the tables of `gc_mark_shapes_is_the_least_closed_set`. +const TABLE: usize = 4; + +/// A shape table as arrays: each entry has at most one parent and one key, +/// and a name past `TABLE` is none. +struct FixedShapes { + exists: [bool; TABLE], + parent: [usize; TABLE], + key: [usize; TABLE], +} + +impl ShapeGraph for FixedShapes { + fn len(&self) -> usize { + TABLE + } + fn exists(&self, i: usize) -> bool { + self.exists.get(i).copied().unwrap_or(false) + } + fn parent(&self, i: usize) -> Option { + self.parent.get(i).copied().filter(|&p| p < TABLE) + } + fn each_key(&self, i: usize, f: &mut dyn FnMut(usize)) { + if let Some(k) = self.key.get(i).copied().filter(|&k| k < TABLE) { + f(k); + } + } +} + +#[kani::proof] +#[kani::solver(cadical)] +#[kani::unwind(8)] +fn gc_mark_shapes_is_the_least_closed_set() { + let mut g = FixedShapes { + exists: [false; TABLE], + parent: [0; TABLE], + key: [0; TABLE], + }; + for i in 0..TABLE { + g.exists[i] = kani::any(); + g.parent[i] = any_name(TABLE); + g.key[i] = any_name(TABLE); + } + let mut roots = [0usize; TABLE]; + let mut root_keys = [0usize; TABLE]; + for i in 0..TABLE { + roots[i] = any_name(TABLE); + root_keys[i] = any_name(TABLE); + } + let mut shapes = [false; TABLE]; + let mut keys = [false; TABLE]; + mark_shapes(&g, &roots, &root_keys, &mut shapes, &mut keys); + let sh = |i: usize| shapes.get(i).copied().unwrap_or(false); + let ky = |i: usize| keys.get(i).copied().unwrap_or(false); + // Safety. + for &r in &roots { + assert!(!g.exists(r) || sh(r)); + } + for &k in &root_keys { + assert!(k >= TABLE || ky(k)); + } + for i in 0..TABLE { + if sh(i) { + assert!(g.exists(i)); + if let Some(p) = g.parent(i) { + assert!(!g.exists(p) || sh(p)); + } + g.each_key(i, &mut |k| assert!(ky(k))); + } + } + // Precision: every marked shape is a root, or the parent of a marked + // shape; every marked key is a root key, or a marked shape's. + for i in 0..TABLE { + if sh(i) { + let supported = + roots.contains(&i) || (0..TABLE).any(|j| sh(j) && g.parent(j) == Some(i)); + assert!(supported); + } + if ky(i) { + let supported = root_keys.contains(&i) + || (0..TABLE).any(|j| sh(j) && g.key.get(j).copied() == Some(i)); + assert!(supported); + } + } +} diff --git a/crates/clazel-js-heap/src/shape.rs b/crates/clazel-js-heap/src/shape.rs index 61c712090..86efffa99 100644 --- a/crates/clazel-js-heap/src/shape.rs +++ b/crates/clazel-js-heap/src/shape.rs @@ -20,7 +20,18 @@ //! this slice, since property deletion is a later slice. //! //! Lookups walk the parent chain, at most [`MAX_FAST_PROPERTIES`] long. +//! +//! # Collection (7.6, GC10, GC12, GC13) +//! +//! The collector frees shapes ([`Shapes::sweep`]). A shape keeps its parent +//! alive (a lookup walks the chain), but a parent's *transitions* are weak: +//! a child no object has is freed, and the transition to it is removed, so +//! the same property added again makes a new shape. **A shape id is never +//! reused**: the table keeps a tombstone, and a freed id resolves to nothing +//! (so an inline cache that cached it cannot hit another shape). [`ROOT`] is +//! never freed. +use crate::gc::contract::ShapeGraph; use crate::string::Key; use std::collections::HashMap; @@ -79,8 +90,10 @@ struct Shape { /// The table of shapes. #[derive(Debug)] pub struct Shapes { - /// The shape with id `i` is at index `i - 1`. - table: Vec, + /// The shape with id `i` is at index `i - 1`; `None` once freed. + table: Vec>, + /// How many entries are `Some`. + live: usize, } impl Default for Shapes { @@ -94,13 +107,14 @@ impl Shapes { #[must_use] pub fn new() -> Self { Self { - table: vec![Shape { + table: vec![Some(Shape { parent: None, added: None, slots: 0, transitions: HashMap::new(), dictionary: None, - }], + })], + live: 1, } } @@ -109,6 +123,7 @@ impl Shapes { .ok() .and_then(|i| i.checked_sub(1)) .and_then(|i| self.table.get(i)) + .and_then(Option::as_ref) } fn get_mut(&mut self, id: ShapeId) -> Option<&mut Shape> { @@ -116,6 +131,7 @@ impl Shapes { .ok() .and_then(|i| i.checked_sub(1)) .and_then(|i| self.table.get_mut(i)) + .and_then(Option::as_mut) } fn push(&mut self, shape: Shape) -> Result { @@ -123,20 +139,69 @@ impl Shapes { .ok() .and_then(|n| n.checked_add(1)) .ok_or(Error::TooMany)?; - self.table.push(shape); + self.table.push(Some(shape)); + self.live = self.live.saturating_add(1); Ok(ShapeId(id)) } - /// The number of shapes, [`ROOT`] included. + /// The number of live shapes, [`ROOT`] included. + #[must_use] + pub const fn len(&self) -> usize { + self.live + } + + /// Always false: [`ROOT`] is never freed. #[must_use] - pub fn len(&self) -> usize { + pub const fn is_empty(&self) -> bool { + self.live == 0 + } + + /// The number of shape ids ever issued, freed ones included: the size + /// of the table the collector indexes by shape (id `n` is entry `n - 1`). + #[must_use] + pub fn issued(&self) -> usize { self.table.len() } - /// Always false: [`ROOT`] is there from the start. + /// Whether `id` names a live shape. #[must_use] - pub fn is_empty(&self) -> bool { - self.table.is_empty() + pub fn is_live(&self, id: ShapeId) -> bool { + self.get(id).is_some() + } + + /// The shape `id` was reached from (a shared shape's parent; a + /// dictionary shape and [`ROOT`] have none). + #[must_use] + pub fn parent(&self, id: ShapeId) -> Option { + self.get(id).and_then(|s| s.parent) + } + + /// Free every shape but [`ROOT`] whose flag in `keep` is false (by table + /// entry, so shape id `n` is `keep[n - 1]`; one past the end of `keep` + /// counts as false), and remove the transitions to the freed shapes + /// from the survivors; returns how many were freed. The caller keeps + /// every parent of a kept shape (GC10); freed ids are never issued again + /// (GC13). + pub fn sweep(&mut self, keep: &[bool]) -> usize { + let mut freed = 0usize; + for (i, slot) in self.table.iter_mut().enumerate() { + if i != 0 && slot.is_some() && !keep.get(i).copied().unwrap_or(false) { + *slot = None; + freed = freed.saturating_add(1); + } + } + self.live = self.live.saturating_sub(freed); + let alive: Vec = self.table.iter().map(Option::is_some).collect(); + for shape in self.table.iter_mut().flatten() { + shape.transitions.retain(|_, to| { + usize::try_from(to.0) + .ok() + .and_then(|i| i.checked_sub(1)) + .and_then(|i| alive.get(i).copied()) + .unwrap_or(false) + }); + } + freed } /// Whether `id` is a dictionary shape: unshared, mutated in place, and @@ -265,6 +330,37 @@ impl Shapes { } } +impl ShapeGraph for Shapes { + fn len(&self) -> usize { + self.table.len() + } + + fn exists(&self, i: usize) -> bool { + self.table.get(i).is_some_and(Option::is_some) + } + + fn parent(&self, i: usize) -> Option { + let p = self.table.get(i)?.as_ref()?.parent?; + usize::try_from(p.0).ok()?.checked_sub(1) + } + + fn each_key(&self, i: usize, f: &mut dyn FnMut(usize)) { + let Some(Some(shape)) = self.table.get(i) else { + return; + }; + let mut key = |k: Key| { + if let Ok(k) = usize::try_from(k.0) { + f(k); + } + }; + if let Some(d) = &shape.dictionary { + d.keys().copied().for_each(&mut key); + } else if let Some((k, _)) = shape.added { + key(k); + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -387,4 +483,31 @@ mod tests { assert_eq!(t.add_property(d1, extra, Attrs::ALL), Err(Error::Exists)); Ok(()) } + + #[test] + fn a_swept_shape_is_gone_its_transition_too_and_the_root_stays() -> Result<(), Error> { + let mut t = Shapes::new(); + let (s1, _) = t.add_property(ROOT, A, Attrs::ALL)?; + let (s2, _) = t.add_property(s1, B, Attrs::ALL)?; + let (s3, _) = t.add_property(s1, C, Attrs::ALL)?; + assert_eq!((t.len(), t.issued()), (4, 4)); + assert_eq!(t.parent(s2), Some(s1)); + // Keep {a} and {a, c}; ask to free the root too, which is refused. + assert_eq!(t.sweep(&[false, true, false, true]), 1); + assert_eq!((t.len(), t.issued()), (3, 4)); + assert!(t.is_live(ROOT) && t.is_live(s1) && t.is_live(s3)); + assert!(!t.is_live(s2)); + assert_eq!(t.slot_count(s2), None); + assert_eq!(t.lookup(s3, A), Some((0, Attrs::ALL))); + // Adding `b` to {a} again makes a new shape with a new id. + let (again, slot) = t.add_property(s1, B, Attrs::ALL)?; + assert_ne!(again, s2); + assert_eq!(again, ShapeId(5)); + assert_eq!(slot, 1); + // The surviving transition is still shared. + assert_eq!(t.add_property(s1, C, Attrs::ALL)?.0, s3); + // A freed shape cannot gain properties. + assert_eq!(t.add_property(s2, C, Attrs::ALL), Err(Error::Unknown)); + Ok(()) + } } diff --git a/crates/clazel-js-heap/src/string.rs b/crates/clazel-js-heap/src/string.rs index a12b1109e..db8dbce53 100644 --- a/crates/clazel-js-heap/src/string.rs +++ b/crates/clazel-js-heap/src/string.rs @@ -175,11 +175,18 @@ impl JsString { #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] pub struct Key(pub u32); -/// A table of property-key strings: each distinct string once. +/// A table of property-key strings: each distinct live string once. +/// +/// A collection may free keys ([`Interner::sweep`]). **A key id is never +/// reused** (GC13): a freed key resolves to nothing from then on, and +/// interning its string again makes a new key, so a stale key is a defined +/// miss, not another property. The table keeps a tombstone per freed key. #[derive(Debug, Default)] pub struct Interner { keys: HashMap, - strings: Vec, + /// Indexed by key; `None` once freed. + strings: Vec>, + live: usize, } impl Interner { @@ -190,42 +197,70 @@ impl Interner { } /// The key for `s`, adding it if it is new. Equal strings, whatever - /// their origin, get one key. + /// their origin, get one key (while it is live). /// /// # Errors - /// [`TooLong`] if the table already holds `u32::MAX` strings. + /// [`TooLong`] if `u32::MAX` keys have been issued. pub fn intern(&mut self, s: &JsString) -> Result { if let Some(&k) = self.keys.get(s) { return Ok(k); } let key = Key(u32::try_from(self.strings.len()).map_err(|_| TooLong)?); self.keys.insert(s.clone(), key); - self.strings.push(s.clone()); + self.strings.push(Some(s.clone())); + self.live = self.live.saturating_add(1); Ok(key) } - /// The key for `s` if it was interned, without adding it. + /// The key for `s` if it was interned and is live, without adding it. #[must_use] pub fn find(&self, s: &JsString) -> Option { self.keys.get(s).copied() } - /// The string a key stands for. + /// The string a live key stands for. #[must_use] pub fn resolve(&self, k: Key) -> Option<&JsString> { - usize::try_from(k.0).ok().and_then(|i| self.strings.get(i)) + usize::try_from(k.0) + .ok() + .and_then(|i| self.strings.get(i)) + .and_then(Option::as_ref) } - /// The number of distinct keys. + /// The number of live keys. #[must_use] - pub fn len(&self) -> usize { - self.strings.len() + pub const fn len(&self) -> usize { + self.live } - /// Whether no key has been interned. + /// Whether no key is live. #[must_use] - pub fn is_empty(&self) -> bool { - self.strings.is_empty() + pub const fn is_empty(&self) -> bool { + self.live == 0 + } + + /// The number of keys ever issued, freed ones included: the size of the + /// table `sweep` and the collector index by key. + #[must_use] + pub fn issued(&self) -> usize { + self.strings.len() + } + + /// Free every live key whose flag in `keep` is false (a key past the end + /// of `keep` is freed too); returns how many were freed. Freed keys are + /// never issued again (GC13). + pub fn sweep(&mut self, keep: &[bool]) -> usize { + let mut freed = 0usize; + for (i, slot) in self.strings.iter_mut().enumerate() { + if slot.is_some() && !keep.get(i).copied().unwrap_or(false) { + if let Some(s) = slot.take() { + self.keys.remove(&s); + } + freed = freed.saturating_add(1); + } + } + self.live = self.live.saturating_sub(freed); + freed } } @@ -351,4 +386,35 @@ mod tests { assert_ne!(w, a); Ok(()) } + + #[test] + fn a_swept_key_is_gone_for_good_and_its_id_is_never_reused() -> Result<(), TooLong> { + let mut t = Interner::new(); + let (a, b, c) = ( + t.intern(&s("a")?)?, + t.intern(&s("b")?)?, + t.intern(&s("c")?)?, + ); + assert_eq!(t.sweep(&[true, false, true]), 1); + assert_eq!((t.len(), t.issued()), (2, 3)); + assert_eq!(t.resolve(b), None); + assert_eq!(t.find(&s("b")?), None); + assert_eq!(t.resolve(a), Some(&s("a")?)); + assert_eq!(t.find(&s("c")?), Some(c)); + // The string again is a new key, not the old one. + let b2 = t.intern(&s("b")?)?; + assert_ne!(b2, b); + assert_eq!(b2, Key(3)); + assert_eq!(t.resolve(b), None); + assert_eq!((t.len(), t.issued()), (3, 4)); + // A flag list that is short frees what it does not name; a second + // sweep of the freed key frees nothing. + assert_eq!(t.sweep(&[true]), 2); + assert_eq!(t.sweep(&[true]), 0); + assert_eq!((t.len(), t.issued()), (1, 4)); + assert!(!t.is_empty()); + assert_eq!(t.sweep(&[]), 1); + assert!(t.is_empty()); + Ok(()) + } } diff --git a/crates/clazel-js-heap/src/symbol/contract.rs b/crates/clazel-js-heap/src/symbol/contract.rs new file mode 100644 index 000000000..d58e782c9 --- /dev/null +++ b/crates/clazel-js-heap/src/symbol/contract.rs @@ -0,0 +1,187 @@ +//! The contract of symbols and property keys (phase-7.md item 7.5, "The +//! rest"), stated before the implementation (CLAUDE.md rule 3). +//! +//! # What is modelled +//! +//! ECMA-262 §6.1.5, The Symbol Type: a Symbol is a primitive value that is +//! unique and immutable, and may hold a *description* (a String or +//! `undefined`); and §6.1.7, property keys: a property key is a String or a +//! Symbol. +//! +//! Modelling choices (SPEC-STYLE.md): +//! +//! - **A symbol is its id.** [`SymbolId`] is a `u32` into a table; two +//! symbols are the same value exactly when their ids are equal, however +//! their descriptions compare (§6.1.5: "each Symbol value immutably holds +//! an associated value called [[Description]]"; identity is not the +//! description). +//! - **Symbols are never freed in this slice.** Reclaiming one needs the +//! collector (7.6); until then a table only grows, so an id is never +//! reused (Y2). +//! - **The global symbol registry** (`Symbol.for`, §20.4.2.2 and the +//! GlobalSymbolRegistry of §20.4.2) is a built-in's state, not the +//! heap's, and is absent here. +//! - **A symbol as a value** (its NaN-boxed form, a heap reference or a +//! tagged id) is not decided here; this slice is the table and the key. +//! - **Step numbers** are not cited: the clauses used have none to quote. +//! +//! # Well-known symbols (§6.1.5.1, Table 1) +//! +//! The standard's thirteen well-known symbols are the **fixed ids 0 to +//! 12**, in the order of [`WELL_KNOWN`], and exist in every table from the +//! start. Their description is `"Symbol."` followed by the name, as the +//! standard gives them (`@@iterator` is described `Symbol.iterator`). +//! +//! # Invariants +//! +//! - **Y1. Fixed ids.** In every table, the symbol with id `i < 13` is the +//! well-known symbol `WELL_KNOWN[i]`, with that description. Created +//! symbols have ids from 13 up, in creation order. +//! - **Y2. Unique and never reused.** Creating a symbol returns the id equal +//! to the table's length before, so no created symbol has the id of any +//! earlier one, and the table's length grows by exactly one. +//! - **Y3. Immutable description.** A symbol's description (a string, or +//! none) is what it was created with, in every later state. A symbol +//! created with the empty-string description is distinct from one with no +//! description: [`Description`] has two cases, not an `Option` collapsed +//! to empty. +//! - **Y4. Keys do not collide.** [`encode`] maps a [`PropertyKey`] to the +//! shapes' [`Key`], a `u32`, as follows: a string key `k` with `k < 2^31` +//! to itself; a symbol `s` with `s < 2^31` to `s | 2^31`. It is +//! injective, so a string and a symbol never share a key, and [`decode`] +//! is its inverse: `decode(encode(p)) == p` for every `p` it accepts, and +//! `encode(decode(k)) == Some(k)` for every key. +//! - **Y5. Capacity.** Strings interned as keys and symbols created are +//! each limited to `2^31` by Y4's encoding; one more is an error, never a +//! wrapped id. + +use crate::string::Key; + +/// A symbol: an index into a symbol table. +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] +pub struct SymbolId(pub u32); + +/// A property key: a String (interned) or a Symbol (§6.1.7). +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] +pub enum PropertyKey { + /// An interned string. + String(Key), + /// A symbol. + Symbol(SymbolId), +} + +/// A symbol's `[[Description]]` (§6.1.5): `undefined`, or a String. The +/// empty string and `undefined` differ (Y3). +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum Description { + /// `undefined`. + Undefined, + /// A String. + Text(S), +} + +/// The bit that marks a symbol's key (Y4); string keys are below it. +pub const SYMBOL_BIT: u32 = 1 << 31; + +/// §6.1.5.1 Table 1, in id order (Y1): the names of `Symbol.`. +pub const WELL_KNOWN: [&str; 13] = [ + "asyncIterator", + "hasInstance", + "isConcatSpreadable", + "iterator", + "match", + "matchAll", + "replace", + "search", + "species", + "split", + "toPrimitive", + "toStringTag", + "unscopables", +]; + +/// How many well-known symbols there are. +pub const WELL_KNOWN_COUNT: u32 = 13; + +/// Y4: the key for a property key, or `None` if its number is too large +/// for the encoding (Y5). +#[must_use] +pub const fn encode(p: PropertyKey) -> Option { + match p { + PropertyKey::String(k) => { + if k.0 < SYMBOL_BIT { + Some(k) + } else { + None + } + } + PropertyKey::Symbol(s) => { + if s.0 < SYMBOL_BIT { + Some(Key(s.0 | SYMBOL_BIT)) + } else { + None + } + } + } +} + +/// Y4: the property key a key stands for. +#[must_use] +pub const fn decode(k: Key) -> PropertyKey { + if k.0 & SYMBOL_BIT == 0 { + PropertyKey::String(k) + } else { + PropertyKey::Symbol(SymbolId(k.0 & !SYMBOL_BIT)) + } +} + +/// Y1: the description text of the well-known symbol `id`, or `None` if +/// `id` is not one. +#[must_use] +pub fn well_known_description(id: SymbolId) -> Option { + let i = usize::try_from(id.0).ok()?; + WELL_KNOWN.get(i).map(|n| format!("Symbol.{n}")) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn table_is_thirteen_distinct_names() { + assert_eq!(WELL_KNOWN.len(), 13); + assert_eq!(usize::try_from(WELL_KNOWN_COUNT), Ok(WELL_KNOWN.len())); + for (i, a) in WELL_KNOWN.iter().enumerate() { + for b in WELL_KNOWN.iter().skip(i.saturating_add(1)) { + assert_ne!(a, b); + } + } + } + + #[test] + fn descriptions() { + assert_eq!( + well_known_description(SymbolId(3)).as_deref(), + Some("Symbol.iterator") + ); + assert_eq!(well_known_description(SymbolId(13)), None); + } + + #[test] + fn encoding_edges() { + let s = PropertyKey::String(Key(SYMBOL_BIT - 1)); + let y = PropertyKey::Symbol(SymbolId(0)); + assert_eq!(encode(s), Some(Key(SYMBOL_BIT - 1))); + assert_eq!(encode(y), Some(Key(SYMBOL_BIT))); + assert_eq!(encode(PropertyKey::String(Key(SYMBOL_BIT))), None); + assert_eq!(encode(PropertyKey::Symbol(SymbolId(SYMBOL_BIT))), None); + assert_eq!(decode(Key(SYMBOL_BIT)), y); + assert_eq!( + decode(Key(u32::MAX)), + PropertyKey::Symbol(SymbolId(SYMBOL_BIT - 1)) + ); + for p in [s, y, PropertyKey::String(Key(0))] { + assert_eq!(encode(p).map(decode), Some(p)); + } + } +} diff --git a/crates/clazel-js-heap/src/symbol/mod.rs b/crates/clazel-js-heap/src/symbol/mod.rs new file mode 100644 index 000000000..733fe3e25 --- /dev/null +++ b/crates/clazel-js-heap/src/symbol/mod.rs @@ -0,0 +1,302 @@ +//! Symbols and property keys (phase-7.md item 7.5). +//! +//! [`contract`] states what the symbol table and the key encoding must do +//! (Y1 to Y5). [`Symbols`] is the table; [`PropertyKeys`] pairs it with the +//! string [`Interner`] so that strings and symbols are both property keys +//! with one [`Key`] type, which is what [`crate::shape::Shapes`] takes. + +pub mod contract; + +use crate::string::{Interner, JsString, Key, TooLong}; +use contract::{ + Description, PropertyKey, SYMBOL_BIT, WELL_KNOWN, WELL_KNOWN_COUNT, decode, encode, +}; + +pub use contract::SymbolId; + +/// Why a symbol or key operation failed. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Error { + /// A table is full (Y5): 2^31 strings or 2^31 symbols. + TooMany, + /// A string past the length limit. + TooLong, + /// The key or id names nothing in the table. + Unknown, +} + +impl From for Error { + fn from(_: TooLong) -> Self { + Self::TooLong + } +} + +macro_rules! well_known { + ($($name:ident = $id:expr, $js:expr;)*) => { + $( + #[doc = concat!("`Symbol.", $js, "` (§6.1.5.1, Table 1).")] + pub const $name: SymbolId = SymbolId($id); + )* + }; +} + +well_known! { + ASYNC_ITERATOR = 0, "asyncIterator"; + HAS_INSTANCE = 1, "hasInstance"; + IS_CONCAT_SPREADABLE = 2, "isConcatSpreadable"; + ITERATOR = 3, "iterator"; + MATCH = 4, "match"; + MATCH_ALL = 5, "matchAll"; + REPLACE = 6, "replace"; + SEARCH = 7, "search"; + SPECIES = 8, "species"; + SPLIT = 9, "split"; + TO_PRIMITIVE = 10, "toPrimitive"; + TO_STRING_TAG = 11, "toStringTag"; + UNSCOPABLES = 12, "unscopables"; +} + +/// The symbol table. +#[derive(Debug)] +pub struct Symbols { + descriptions: Vec>, +} + +impl Default for Symbols { + fn default() -> Self { + Self::new() + } +} + +impl Symbols { + /// A table holding the thirteen well-known symbols at ids 0 to 12 (Y1). + #[must_use] + pub fn new() -> Self { + let descriptions = WELL_KNOWN + .iter() + .map(|n| { + // The names are short ASCII: far under the string limit. + match JsString::from_rust_str(&format!("Symbol.{n}")) { + Ok(s) => Description::Text(s), + Err(_) => Description::Undefined, + } + }) + .collect(); + Self { descriptions } + } + + /// A new symbol, distinct from every other (Y2), with the description + /// it is given (Y3). + /// + /// # Errors + /// [`Error::TooMany`] when 2^31 symbols exist (Y5). + pub fn create(&mut self, description: Description) -> Result { + let id = u32::try_from(self.descriptions.len()).map_err(|_| Error::TooMany)?; + if id >= SYMBOL_BIT { + return Err(Error::TooMany); + } + self.descriptions.push(description); + Ok(SymbolId(id)) + } + + /// The symbol's description (`Symbol.prototype.description`'s source). + #[must_use] + pub fn description(&self, id: SymbolId) -> Option<&Description> { + usize::try_from(id.0) + .ok() + .and_then(|i| self.descriptions.get(i)) + } + + /// Whether `id` is one of the thirteen well-known symbols. + #[must_use] + pub const fn is_well_known(id: SymbolId) -> bool { + id.0 < WELL_KNOWN_COUNT + } + + /// The number of symbols, well-known ones included. + #[must_use] + pub fn len(&self) -> usize { + self.descriptions.len() + } + + /// Always false: the well-known symbols are there from the start. + #[must_use] + pub fn is_empty(&self) -> bool { + self.descriptions.is_empty() + } +} + +/// Strings and symbols as property keys. +#[derive(Debug, Default)] +pub struct PropertyKeys { + strings: Interner, + symbols: Symbols, +} + +impl PropertyKeys { + /// Empty string table; the symbol table has the well-known symbols. + #[must_use] + pub fn new() -> Self { + Self::default() + } + + /// The key for a string, interning it (equal strings, one key). + /// + /// # Errors + /// [`Error::TooMany`] when 2^31 strings are interned and `s` is new + /// (Y5); [`Error::TooLong`]. + pub fn string_key(&mut self, s: &JsString) -> Result { + if let Some(k) = self.strings.find(s) { + return Ok(k); + } + if self.strings.issued() >= usize::try_from(SYMBOL_BIT).unwrap_or(usize::MAX) { + return Err(Error::TooMany); + } + let k = self.strings.intern(s)?; + encode(PropertyKey::String(k)).ok_or(Error::TooMany) + } + + /// The key for a symbol. + /// + /// # Errors + /// [`Error::Unknown`] if the id names no symbol of this table. + pub fn symbol_key(&self, id: SymbolId) -> Result { + self.symbols.description(id).ok_or(Error::Unknown)?; + encode(PropertyKey::Symbol(id)).ok_or(Error::Unknown) + } + + /// A new symbol and its key. + /// + /// # Errors + /// As [`Symbols::create`]. + pub fn new_symbol( + &mut self, + description: Description, + ) -> Result<(SymbolId, Key), Error> { + let id = self.symbols.create(description)?; + let key = encode(PropertyKey::Symbol(id)).ok_or(Error::TooMany)?; + Ok((id, key)) + } + + /// What a key is: a string (resolved) or a symbol (Y4). + #[must_use] + pub fn classify(&self, key: Key) -> Option { + let p = decode(key); + match p { + PropertyKey::String(k) => self.strings.resolve(k).map(|_| p), + PropertyKey::Symbol(s) => self.symbols.description(s).map(|_| p), + } + } + + /// The string a key stands for, if it is a string key. + #[must_use] + pub fn string(&self, key: Key) -> Option<&JsString> { + match decode(key) { + PropertyKey::String(k) => self.strings.resolve(k), + PropertyKey::Symbol(_) => None, + } + } + + /// The symbol table. + #[must_use] + pub const fn symbols(&self) -> &Symbols { + &self.symbols + } + + /// The string table. + #[must_use] + pub const fn strings(&self) -> &Interner { + &self.strings + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::shape::{Attrs, ROOT, Shapes}; + + fn js(s: &str) -> JsString { + JsString::from_rust_str(s).unwrap_or_else(|_| JsString::empty()) + } + + #[test] + fn well_known_ids_are_fixed() { + let t = Symbols::new(); + assert_eq!(t.len(), 13); + assert_eq!(ITERATOR, SymbolId(3)); + assert_eq!(UNSCOPABLES, SymbolId(12)); + for (i, name) in WELL_KNOWN.iter().enumerate() { + let id = SymbolId(u32::try_from(i).unwrap_or(u32::MAX)); + assert!(Symbols::is_well_known(id)); + assert_eq!( + t.description(id), + Some(&Description::Text(js(&format!("Symbol.{name}")))) + ); + } + assert!(!Symbols::is_well_known(SymbolId(13))); + assert_eq!( + t.description(TO_PRIMITIVE), + Some(&Description::Text(js("Symbol.toPrimitive"))) + ); + } + + #[test] + fn created_symbols_are_unique() { + let mut t = Symbols::new(); + let a = t.create(Description::Text(js("x"))); + let b = t.create(Description::Text(js("x"))); + let c = t.create(Description::Undefined); + let d = t.create(Description::Text(JsString::empty())); + assert_eq!(a, Ok(SymbolId(13))); + assert_eq!(b, Ok(SymbolId(14))); + assert_eq!(c, Ok(SymbolId(15))); + assert_eq!(d, Ok(SymbolId(16))); + assert_ne!(a, b, "same description, different symbols"); + assert_eq!(t.description(SymbolId(15)), Some(&Description::Undefined)); + assert_ne!( + t.description(SymbolId(15)), + t.description(SymbolId(16)), + "undefined is not the empty string" + ); + assert_eq!(t.description(SymbolId(17)), None); + assert_eq!(t.len(), 17); + } + + #[test] + fn strings_and_symbols_never_share_a_key() { + let mut k = PropertyKeys::new(); + let s = k.string_key(&js("iterator")); + let again = k.string_key(&js("iterator")); + assert_eq!(s, again); + let sym = k.symbol_key(ITERATOR); + assert_ne!(s, sym, "the string \"iterator\" is not Symbol.iterator"); + let (id, key) = k + .new_symbol(Description::Text(js("iterator"))) + .unwrap_or((SymbolId(0), Key(0))); + assert_ne!(Ok(key), sym); + assert_ne!(Ok(key), s); + assert_eq!(k.classify(key), Some(PropertyKey::Symbol(id))); + assert_eq!(k.string(key), None); + let sk = s.unwrap_or(Key(0)); + assert_eq!(k.string(sk).map(JsString::len), Some(8)); + assert_eq!(k.classify(Key(sk.0 + 100)), None); + assert_eq!(k.symbol_key(SymbolId(99)), Err(Error::Unknown)); + } + + #[test] + fn symbol_keys_work_in_shapes() { + let mut k = PropertyKeys::new(); + let mut shapes = Shapes::new(); + let s = k.string_key(&js("a")).unwrap_or(Key(0)); + let y = k.symbol_key(ITERATOR).unwrap_or(Key(0)); + let s1 = shapes.add_property(ROOT, s, Attrs::ALL); + let s1 = s1.map(|(id, _)| id).unwrap_or(ROOT); + let s2 = shapes.add_property(s1, y, Attrs::ALL); + let s2 = s2.map(|(id, _)| id).unwrap_or(ROOT); + assert_eq!(shapes.lookup(s2, s), Some((0, Attrs::ALL))); + assert_eq!(shapes.lookup(s2, y), Some((1, Attrs::ALL))); + // The same order on another object reaches the same shape. + let t1 = shapes.add_property(ROOT, s, Attrs::ALL).map(|(i, _)| i); + assert_eq!(t1, Ok(s1)); + } +} diff --git a/crates/clazel-js-lexer/Cargo.toml b/crates/clazel-js-lexer/Cargo.toml index 02ff533b8..3aded2fe8 100644 --- a/crates/clazel-js-lexer/Cargo.toml +++ b/crates/clazel-js-lexer/Cargo.toml @@ -13,9 +13,18 @@ vstd = "=0.0.0-2026-10-04-0306" # shipped code today; `target-tier` is what the policy requires, since this # lexes scripts a page chose (phase-7.md 7.0(a)). Tier 0: the spec and the # contracts are written (item 7.2, first half, CLAUDE.md rule 3), and the -# implementation (src/exec.rs) is proven total, panic-free and to tile its -# input, but not to agree with the spec: it does not meet src/contract.rs -# yet, and there is no independent transcription or mutation set. +# implementation (src/exec.rs) is proven total, panic-free, to tile its +# input and to meet src/contract.rs for every source text, position, goal +# symbol and Annex B mode (`next_input_element` and `next_token`, which +# implement `JsLexer` as `ExecLexer`), by Verus with no escape hatch. The +# spec's ID_Start and ID_Continue above U+007F are the generated Unicode +# 18.0.0 table, with no axiom, and the lookups are proven to be them. The +# tier-2 evidence around the spec exists in part: an independent +# transcription of part of the grammar proven equal to the spec +# (src/independent.rs, src/equivalence.rs), 28 certified mutation kills +# (mutations.json) and Test262 conformance suites +# (tests/conformance_test262.rs). The tier stays 0 until CI's full +# verification passes on the pull request that raises it (CLAUDE.md rule 7). [package.metadata.clazel] tier = 0 target-tier = 2 diff --git a/crates/clazel-js-lexer/mutations.json b/crates/clazel-js-lexer/mutations.json index 4f0dd0478..0ad591626 100644 --- a/crates/clazel-js-lexer/mutations.json +++ b/crates/clazel-js-lexer/mutations.json @@ -6,6 +6,7 @@ "find": "c == 0x000A || c == 0x000D || c == 0x2028 || c == 0x2029", "replace": "c == 0x000A || c == 0x000D || c == 0x2029", "expect": "killed", + "module": "examples", "reason": "Drops from LineTerminator (§12.3): U+2028 would no longer end a line or trigger ASI, so example_line_terminators is false.", "certificate": { "file": "src/examples.rs", @@ -19,6 +20,7 @@ "find": "c == 0x000A || c == 0x000D || c == 0x2028 || c == 0x2029", "replace": "c == 0x000A || c == 0x000D || c == 0x0085 || c == 0x2028 || c == 0x2029", "expect": "killed", + "module": "equivalence", "reason": "Counts U+0085 NEL as a LineTerminator, which §12.3 does not: example_line_terminators is false.", "certificate": { "file": "src/examples.rs", @@ -32,6 +34,7 @@ "find": "c as int - 0x61 + 10", "replace": "c as int - 0x61", "expect": "killed", + "module": "examples", "reason": "Gives `b` the MV 1 instead of 11 (§12.9.3.1), so `\\x62`-style escapes and `0xb` would get the wrong value: example_hex_digit_values is false.", "certificate": { "file": "src/examples.rs", @@ -45,12 +48,338 @@ "find": " || (i + 2 < j && is_octal_digit(s[j - 1]) && legacy_octal_integer_literal(s, i, j - 1)))", "replace": " )", "expect": "killed", + "module": "examples", "reason": "Drops the recursive alternative `LegacyOctalIntegerLiteral OctalDigit` (§12.9.3, Annex B), so `010` is no longer a LegacyOctalIntegerLiteral: example_legacy_numeric_literals is false.", "certificate": { "file": "src/examples.rs", "lemma": "example_legacy_numeric_literals()", "witness": "!legacy_octal_integer_literal(seq![0x30u32, 0x31, 0x30], 0, 3)" } + }, + { + "id": "white-space-drops-zwnbsp", + "file": "src/spec.rs", + "find": "|| s[i] == 0xFEFF", + "replace": "|| s[i] == 0xFEFE", + "expect": "killed", + "reason": "Drops (U+FEFF) from WhiteSpace (§12.2): lemma_white_space_equiv, which equates white_space with the independent Annex A form, is false at U+FEFF.", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_white_space_equiv(seq![0xFEFFu32], 0, 1)", + "witness": "!white_space(seq![0xFEFFu32], 0, 1)" + } + }, + { + "id": "crlf-sequence-wrong-second-code-point", + "file": "src/spec.rs", + "find": "|| (j == i + 2 && s[i] == 0x000D && s[i + 1] == 0x000A))", + "replace": "|| (j == i + 2 && s[i] == 0x000D && s[i + 1] == 0x000B))", + "expect": "killed", + "reason": "LineTerminatorSequence (§12.3, alternative 5) wants then instead of , so CR LF would be two line terminators: lemma_line_terminator_sequence_equiv is false on CR LF over [0, 2).", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_line_terminator_sequence_equiv(seq![0x0Du32, 0x0Au32], 0, 2)", + "witness": "!line_terminator_sequence(seq![0x0Du32, 0x0Au32], 0, 2)" + } + }, + { + "id": "post-asterisk-allows-slash", + "file": "src/spec.rs", + "find": "(is_source_character(s[i]) && s[i] != 0x2F && s[i] != 0x2A", + "replace": "(is_source_character(s[i]) && s[i] != 0x2A", + "expect": "killed", + "reason": "PostAsteriskCommentChars (§12.4) stops excluding `/` after a `*`, so `*/` would no longer end a comment: lemma_comment_chars says a `/` right after `*` is not PostAsteriskCommentChars.", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_comment_chars(seq![0x2Fu32], 0, 1)", + "witness": "post_asterisk_comment_chars(seq![0x2Fu32], 0, 1)" + } + }, + { + "id": "digits-separator-needs-no-flag", + "file": "src/spec.rs", + "find": "|| (sep && i + 2 < j && s[j - 2] == 0x5F", + "replace": "|| (i + 2 < j && s[j - 2] == 0x5F", + "expect": "killed", + "reason": "Allows NumericLiteralSeparator in `Digits[~Sep]` (§12.9.3), so `1_1` would be plain DecimalDigits (and a CodePoint `\\u{1_1}`): lemma_plain_digits_equiv is false on 1_1.", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_plain_digits_equiv(10, seq![0x31u32, 0x5Fu32, 0x31u32], 0, 3)", + "witness": "digits(10, false, seq![0x31u32, 0x5Fu32, 0x31u32], 0, 3)" + } + }, + { + "id": "bigint-suffix-wrong-letter", + "file": "src/spec.rs", + "find": "nonempty(s, i, j) && i + 1 < j && s[j - 1] == 0x6E && (", + "replace": "nonempty(s, i, j) && i + 1 < j && s[j - 1] == 0x6D && (", + "expect": "killed", + "reason": "DecimalBigIntegerLiteral (§12.9.3) ends in `m` instead of `n` (BigIntLiteralSuffix): lemma_decimal_bigint_equiv is false on `1n`.", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_decimal_bigint_equiv(seq![0x31u32, 0x6Eu32], 0, 2)", + "witness": "!decimal_big_integer_literal(seq![0x31u32, 0x6Eu32], 0, 2)" + } + }, + { + "id": "exponent-indicator-drops-uppercase", + "file": "src/spec.rs", + "find": "nonempty(s, i, j) && (s[i] == 0x65 || s[i] == 0x45)", + "replace": "nonempty(s, i, j) && (s[i] == 0x65)", + "expect": "killed", + "reason": "ExponentIndicator (§12.9.3) loses `E`: lemma_exponent_equiv is false on `E1`.", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_exponent_equiv(seq![0x45u32, 0x31u32], 0, 2)", + "witness": "!exponent_part(true, seq![0x45u32, 0x31u32], 0, 2)" + } + }, + { + "id": "hex-prefix-drops-uppercase-x", + "file": "src/spec.rs", + "find": "((s[i + 1] == 0x78 || s[i + 1] == 0x58) && hex_digits(sep, s, i + 2, j))", + "replace": "((s[i + 1] == 0x78) && hex_digits(sep, s, i + 2, j))", + "expect": "killed", + "reason": "HexIntegerLiteral (§12.9.3) loses the `0X` prefix: lemma_non_decimal_equiv is false on `0X1`.", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_non_decimal_equiv(seq![0x30u32, 0x58u32, 0x31u32], 0, 3)", + "witness": "!non_decimal_integer_literal(true, seq![0x30u32, 0x58u32, 0x31u32], 0, 3)" + } + }, + { + "id": "identifier-start-drops-dollar", + "file": "src/spec.rs", + "find": "unicode_id_start(c) || c == 0x24 || c == 0x5F", + "replace": "unicode_id_start(c) || c == 0x5F", + "expect": "killed", + "reason": "IdentifierStartChar (§12.7) loses `$`: lemma_id_char_equiv, which equates it with UAX #31's Start, is false at U+0024.", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_id_char_equiv(0x24u32)", + "witness": "!is_identifier_start_char(0x24u32)" + } + }, + { + "id": "identifier-part-drops-digits", + "file": "src/spec.rs", + "find": "is_ascii_letter(c) || is_decimal_digit(c) || c == 0x5F", + "replace": "is_ascii_letter(c) || c == 0x5F", + "expect": "killed", + "reason": "UnicodeIDContinue (§12.7) loses the ASCII digits, so `a1` would end at `a`: lemma_id_char_equiv is false at U+0031.", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_id_char_equiv(0x31u32)", + "witness": "!is_identifier_part_char(0x31u32)" + } + }, + { + "id": "punctuators-drop-unsigned-right-shift-assign", + "file": "src/spec.rs", + "find": "seq!['>', '>', '>', '='], ", + "replace": "", + "expect": "killed", + "reason": "OtherPunctuator (§12.8) loses `>>>=`: the list has 52 entries, not the 53 that lemma_other_punctuators_len proves for the standard's list.", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_other_punctuators_len()", + "witness": "other_punctuators().len() == 52" + } + }, + { + "id": "reserved-words-drop-yield", + "file": "src/spec.rs", + "find": " seq!['y', 'i', 'e', 'l', 'd'],\n", + "replace": "", + "expect": "killed", + "reason": "ReservedWord (§12.7.2) loses `yield`: the list has 37 words, but lemma_reserved_words_equiv equates it with the independent list of 38.", + "certificate": { + "file": "src/equivalence.rs", + "lemma": "lemma_reserved_words_equiv()", + "witness": "reserved_words().len() == 37" + } + }, + { + "id": "string-escape-n-is-carriage-return", + "file": "src/spec.rs", + "find": " } else if c == 0x6E {\n 0x000A\n", + "replace": " } else if c == 0x6E {\n 0x000D\n", + "expect": "killed", + "module": "examples", + "reason": "The SV of `\\n` in a string (§12.9.4.2, Table 'String Single Character Escape Sequences') is instead of : example_single_escapes is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_single_escapes()", + "witness": "single_escape_code_unit(0x6Eu32) == 0x000Du16" + } + }, + { + "id": "template-trv-crlf-stays-cr", + "file": "src/spec.rs", + "find": " // : \"the code unit 0x000A (LINE FEED)\"\n seq![0x000Au16]", + "replace": " // : \"the code unit 0x000A (LINE FEED)\"\n seq![0x000Du16]", + "expect": "killed", + "module": "examples", + "reason": "The TRV of in a template (§12.9.6.2) is instead of : example_trv_crlf is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_trv_crlf()", + "witness": "line_terminator_sequence_trv(seq![0x0Du32, 0x0Au32], 0, 2)[0] == 0x000Du16" + } + }, + { + "id": "regexp-non-terminator-allows-line-feed", + "file": "src/spec.rs", + "find": "pub open spec fn is_regexp_non_terminator(c: u32) -> bool {\n is_not_line_terminator(c)", + "replace": "pub open spec fn is_regexp_non_terminator(c: u32) -> bool {\n is_source_character(c)", + "expect": "killed", + "module": "examples", + "reason": "RegularExpressionNonTerminator (§12.9.5) accepts , so a regular expression literal could span lines: example_regexp_non_terminator is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_regexp_non_terminator()", + "witness": "is_regexp_non_terminator(0x000Au32)" + } + }, + { + "id": "comments-are-not-trivia", + "file": "src/spec.rs", + "find": "|| kind == ElementKind::Comment || kind == ElementKind::HashbangComment", + "replace": "|| kind == ElementKind::HashbangComment", + "expect": "killed", + "module": "examples", + "reason": "A Comment would reach the syntactic grammar (§12: 'Comments behave like white space and are discarded'): example_trivia is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_trivia()", + "witness": "!is_trivia(ElementKind::Comment)" + } + }, + { + "id": "id-start-ascii-includes-underscore", + "file": "src/spec.rs", + "find": "is_ascii_letter(c)\n } else {\n unicode_id_start_non_ascii(c)", + "replace": "is_ascii_letter(c) || c == 0x5F\n } else {\n unicode_id_start_non_ascii(c)", + "expect": "killed", + "module": "examples", + "reason": "Puts `_` in ID_Start (§12.7: it is an IdentifierStartChar, not a UnicodeIDStart): example_unicode_id_anchors is false. The witness is ASCII because the table's predicate is an existential, which compute_only cannot evaluate; the table's own anchors are proven in the same lemma.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_unicode_id_anchors()", + "witness": "unicode_id_start(0x5Fu32)" + } + }, + { + "id": "id-continue-ascii-drops-underscore", + "file": "src/spec.rs", + "find": "is_ascii_letter(c) || is_decimal_digit(c) || c == 0x5F\n } else {\n unicode_id_continue_non_ascii(c)", + "replace": "is_ascii_letter(c) || is_decimal_digit(c)\n } else {\n unicode_id_continue_non_ascii(c)", + "expect": "killed", + "module": "examples", + "reason": "Drops `_` from ID_Continue (§12.7, UAX #31): example_unicode_id_anchors is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_unicode_id_anchors()", + "witness": "!unicode_id_continue(0x5Fu32)" + } + }, + { + "id": "regexp-backslash-takes-line-terminator", + "file": "src/spec.rs", + "find": "nonempty(s, i, j) && j == i + 2 && s[i] == 0x5C && is_regexp_non_terminator(s[i + 1])", + "replace": "nonempty(s, i, j) && j == i + 2 && s[i] == 0x5C", + "expect": "killed", + "module": "examples", + "reason": "A RegularExpressionBackslashSequence (§12.9.5) would take a line terminator, so an escaped newline would not end the literal's line: example_regexp_char_boundaries is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_regexp_char_boundaries()", + "witness": "regexp_backslash_sequence(seq![0x5Cu32, 0x0Au32], 0, 2)" + } + }, + { + "id": "regexp-class-char-allows-close-bracket", + "file": "src/spec.rs", + "find": "(j == i + 1 && is_regexp_non_terminator(s[i]) && s[i] != 0x5D\n && s[i] != 0x5C)", + "replace": "(j == i + 1 && is_regexp_non_terminator(s[i]) && s[i] != 0x5C)", + "expect": "killed", + "module": "examples", + "reason": "`]` would be a RegularExpressionClassChar (§12.9.5), so a class would have no unique end (the closing `]` through classes and escapes): example_regexp_char_boundaries is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_regexp_char_boundaries()", + "witness": "regexp_class_char(seq![0x5Du32], 0, 1)" + } + }, + { + "id": "regexp-char-allows-slash", + "file": "src/spec.rs", + "find": "(j == i + 1 && is_regexp_non_terminator(s[i]) && s[i] != 0x5C\n && s[i] != 0x2F && s[i] != 0x5B)", + "replace": "(j == i + 1 && is_regexp_non_terminator(s[i]) && s[i] != 0x5C\n && s[i] != 0x5B)", + "expect": "killed", + "module": "examples", + "reason": "`/` would be a RegularExpressionChar outside a class (§12.9.5), so a body would not end at its first unescaped `/`: example_regexp_char_boundaries is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_regexp_char_boundaries()", + "witness": "regexp_char(seq![0x2Fu32], 0, 1)" + } + }, + { + "id": "regexp-first-char-allows-star", + "file": "src/spec.rs", + "find": "is_regexp_non_terminator(s[i]) && s[i] != 0x2A\n && s[i] != 0x5C", + "replace": "is_regexp_non_terminator(s[i])\n && s[i] != 0x5C", + "expect": "killed", + "module": "examples", + "reason": "`*` would start a regular expression body (§12.9.5 RegularExpressionFirstChar), so `/*` would not be a comment: example_regexp_char_boundaries is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_regexp_char_boundaries()", + "witness": "regexp_first_char(seq![0x2Au32], 0, 1)" + } + }, + { + "id": "single-line-post-asterisk-allows-slash", + "file": "src/spec.rs", + "find": "(is_not_line_terminator(s[i]) && s[i] != 0x2F && s[i] != 0x2A\n && (j == i + 1 || single_line_delimited_comment_chars(s, i + 1, j)))", + "replace": "(is_not_line_terminator(s[i]) && s[i] != 0x2A\n && (j == i + 1 || single_line_delimited_comment_chars(s, i + 1, j)))", + "expect": "killed", + "module": "examples", + "reason": "SingleLinePostAsteriskCommentChars (Annex B.1.1) stops excluding `/` after a `*`, so `*/` would no longer end a delimited comment: example_html_comment_boundaries is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_html_comment_boundaries()", + "witness": "single_line_post_asterisk_comment_chars(seq![0x2Fu32], 0, 1)" + } + }, + { + "id": "single-line-delimited-chars-allow-line-terminator", + "file": "src/spec.rs", + "find": "(is_not_line_terminator(s[i]) && s[i] != 0x2A\n && (j == i + 1 || single_line_delimited_comment_chars(s, i + 1, j)))\n // 2. `*` SingleLinePostAsteriskCommentChars?", + "replace": "(is_source_character(s[i]) && s[i] != 0x2A\n && (j == i + 1 || single_line_delimited_comment_chars(s, i + 1, j)))\n // 2. `*` SingleLinePostAsteriskCommentChars?", + "expect": "killed", + "module": "examples", + "reason": "SingleLineDelimitedCommentChars (Annex B.1.1) would take a line terminator, so a delimited comment could span lines and a `/* */` before `-->` would no longer be on one line: example_html_comment_boundaries is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_html_comment_boundaries()", + "witness": "single_line_delimited_comment_chars(seq![0x0Au32], 0, 1)" + } + }, + { + "id": "white-space-sequence-takes-any-code-point", + "file": "src/spec.rs", + "find": " nonempty(s, i, j) && white_space(s, i, i + 1)\n && (j == i + 1 || white_space_sequence(s, i + 1, j))", + "replace": " nonempty(s, i, j)\n && (j == i + 1 || white_space_sequence(s, i + 1, j))", + "expect": "killed", + "module": "examples", + "reason": "WhiteSpaceSequence (Annex B.1.1) would take any code point, so the white space before the `-->` of an HTMLCloseComment would not be white space: example_html_comment_boundaries is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_html_comment_boundaries()", + "witness": "white_space_sequence(seq![0x41u32], 0, 1)" + } } ] } diff --git a/crates/clazel-js-lexer/src/equivalence.rs b/crates/clazel-js-lexer/src/equivalence.rs new file mode 100644 index 000000000..f9cffcd09 --- /dev/null +++ b/crates/clazel-js-lexer/src/equivalence.rs @@ -0,0 +1,1667 @@ +//! Proofs that [`super::independent`] and [`super::spec`] define the same +//! relation, at every source text and every range, for each production the +//! independent transcription covers (docs/VERIFICATION.md, item 2). +//! +//! The theorems, each `<==>` on all `(s, i, j)` (or on all `(s, k)`), with +//! no precondition: +//! +//! - `white_space(s,i,j) <==> ind_white_space(s,i,j)`, and the same for +//! `line_terminator` and `line_terminator_sequence`; +//! - `single_line_comment`, `multi_line_comment`, `hashbang_comment`; +//! - `digits(r, false, ..) <==> ind_plain_digits(r, ..)` and +//! `digits(r, true, ..) <==> ind_sep_digits(r, ..)` for `r` in 2, 8, 10, +//! 16; +//! - `numeric_literal(s,i,j) <==> ind_numeric_literal(s,i,j)`, and the +//! productions it is built from; +//! - `unicode_escape_sequence(s,i,j) <==> ind_unicode_escape(s,i,j)`; +//! - `other_punctuator`, `optional_chaining_punctuator`, `div_punctuator` +//! and `right_brace_punctuator` each `<==>` its independent form; +//! - `is_reserved_word(cps, false) <==> ind_is_reserved(cps)`. +//! +//! The identifier ranges are in the identifier part of this file when +//! present; see [`super::independent`] for what is not covered. The only +//! assumption shared by the two files is the generated table behind +//! `ID_Start` and `ID_Continue` of code points above U+007F. + +#[cfg(verus_only)] +use super::independent::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// --------------------------------------------------------------------- +// Terminal strings. +// --------------------------------------------------------------------- + +/// `is_lit` says, code point by code point, that the range is the literal. +pub proof fn lemma_is_lit(s: Seq, i: int, j: int, lit: Seq) + ensures + is_lit(s, i, j, lit) <==> (0 <= i && j <= s.len() && j - i == lit.len() && forall|k: int| + 0 <= k < lit.len() ==> s[i + k] == #[trigger] lit[k] as u32), +{ + if 0 <= i && j <= s.len() && j - i == lit.len() { + let m = lit.map_values(|c: char| c as u32); + if is_lit(s, i, j, lit) { + assert forall|k: int| 0 <= k < lit.len() implies s[i + k] == #[trigger] lit[k] as u32 by { + assert(s.subrange(i, j)[k] == m[k]); + } + } + if forall|k: int| 0 <= k < lit.len() ==> s[i + k] == #[trigger] lit[k] as u32 { + assert(s.subrange(i, j) =~= m) by { + assert forall|k: int| 0 <= k < lit.len() implies s.subrange(i, j)[k] == m[k] by { + assert(s[i + k] == lit[k] as u32); + } + } + } + } +} + +// --------------------------------------------------------------------- +// White space and line terminators. +// --------------------------------------------------------------------- + +/// `WhiteSpace` (§12.2) is Annex A's: one code point of the class. +pub proof fn lemma_white_space_equiv(s: Seq, i: int, j: int) + ensures + white_space(s, i, j) <==> ind_white_space(s, i, j), +{ +} + +/// `LineTerminator` (§12.3). +pub proof fn lemma_line_terminator_equiv(s: Seq, i: int, j: int) + ensures + line_terminator(s, i, j) <==> ind_line_terminator(s, i, j), +{ +} + +/// `LineTerminatorSequence` (§12.3). +pub proof fn lemma_line_terminator_sequence_equiv(s: Seq, i: int, j: int) + ensures + line_terminator_sequence(s, i, j) <==> ind_line_terminator_sequence(s, i, j), +{ +} + +// --------------------------------------------------------------------- +// Comments. +// --------------------------------------------------------------------- + +/// `SingleLineComment` (§12.4). +pub proof fn lemma_single_line_comment_equiv(s: Seq, i: int, j: int) + ensures + single_line_comment(s, i, j) <==> ind_single_line_comment(s, i, j), +{ + lemma_is_lit(s, i, i + 2, seq!['/', '/']); +} + +/// `HashbangComment` (§12.5). +pub proof fn lemma_hashbang_comment_equiv(s: Seq, i: int, j: int) + ensures + hashbang_comment(s, i, j) <==> ind_hashbang_comment(s, i, j), +{ + lemma_is_lit(s, i, i + 2, seq!['#', '!']); +} + +/// No `*/` begins inside `s[a..b)` (the pair may end at `b - 1` at most). +pub open spec fn no_star_slash(s: Seq, a: int, b: int) -> bool { + forall|k: int| a <= k < b - 1 ==> !(#[trigger] s[k] == 0x2A && s[k + 1] == 0x2F) +} + +/// Every code point of `s[a..b)` is a `SourceCharacter`. +pub open spec fn all_source(s: Seq, a: int, b: int) -> bool { + forall|k: int| a <= k < b ==> #[trigger] s[k] <= 0x10FFFF +} + +/// `MultiLineCommentChars` is "no `*/` inside", and `PostAsteriskCommentChars` +/// is the same after a `*`: the first code point is not `/`. Both on a +/// non-empty range, by induction on its length. +pub proof fn lemma_comment_chars(s: Seq, a: int, b: int) + ensures + multi_line_comment_chars(s, a, b) <==> (0 <= a && a < b && b <= s.len() && all_source( + s, + a, + b, + ) && no_star_slash(s, a, b)), + post_asterisk_comment_chars(s, a, b) <==> (0 <= a && a < b && b <= s.len() && all_source( + s, + a, + b, + ) && s[a] != 0x2F && no_star_slash(s, a, b)), + decreases b - a, +{ + if 0 <= a && a + 1 < b && b <= s.len() { + lemma_comment_chars(s, a + 1, b); + assert(no_star_slash(s, a, b) <==> (no_star_slash(s, a + 1, b) && !(s[a] == 0x2A && s[a + + 1] == 0x2F))) by { + if no_star_slash(s, a, b) { + assert forall|k: int| a + 1 <= k < b - 1 implies !(#[trigger] s[k] == 0x2A && s[k + + 1] == 0x2F) by { + assert(!(s[k] == 0x2A && s[k + 1] == 0x2F)); + } + } + if no_star_slash(s, a + 1, b) && !(s[a] == 0x2A && s[a + 1] == 0x2F) { + assert forall|k: int| a <= k < b - 1 implies !(#[trigger] s[k] == 0x2A && s[k + + 1] == 0x2F) by { + if k > a { + assert(!(s[k] == 0x2A && s[k + 1] == 0x2F)); + } + } + } + } + assert(all_source(s, a, b) <==> (s[a] <= 0x10FFFF && all_source(s, a + 1, b))) by { + if all_source(s, a + 1, b) && s[a] <= 0x10FFFF { + assert forall|k: int| a <= k < b implies #[trigger] s[k] <= 0x10FFFF by { + if k > a { + assert(s[k] <= 0x10FFFF); + } + } + } + } + } + if 0 <= a && a + 1 == b && b <= s.len() { + assert(no_star_slash(s, a, b)); + assert(all_source(s, a, b) <==> s[a] <= 0x10FFFF); + } +} + +/// `MultiLineComment` (§12.4). +pub proof fn lemma_multi_line_comment_equiv(s: Seq, i: int, j: int) + ensures + multi_line_comment(s, i, j) <==> ind_multi_line_comment(s, i, j), +{ + lemma_is_lit(s, i, i + 2, seq!['/', '*']); + lemma_is_lit(s, j - 2, j, seq!['*', '/']); + if 0 <= i && i + 4 <= j && j <= s.len() { + if i + 2 < j - 2 { + lemma_comment_chars(s, i + 2, j - 2); + } + // The range of the content ends with the closing `*` at j - 2, so + // the pair that would start at j - 3 is `?*`, never `*/`. + if multi_line_comment(s, i, j) { + assert forall|k: int| i + 2 <= k < j - 2 implies !(#[trigger] s[k] == 0x2A && s[k + + 1] == 0x2F) by { + if k < j - 3 { + assert(no_star_slash(s, i + 2, j - 2)); + } + } + assert forall|k: int| i + 2 <= k < j - 2 implies ind_src_cp(#[trigger] s[k]) by { + assert(all_source(s, i + 2, j - 2) || i + 2 == j - 2); + } + } + if ind_multi_line_comment(s, i, j) { + if i + 2 < j - 2 { + assert(no_star_slash(s, i + 2, j - 2)) by { + assert forall|k: int| i + 2 <= k < j - 3 implies !(#[trigger] s[k] == 0x2A + && s[k + 1] == 0x2F) by { + assert(!(s[k] == 0x2A && s[k + 1] == 0x2F)); + } + } + assert(all_source(s, i + 2, j - 2)); + } + } + } +} + +// --------------------------------------------------------------------- +// Digit classes and digit runs. +// --------------------------------------------------------------------- + +/// The digit classes of the two files agree for the four radices. +pub proof fn lemma_digit_class_equiv(radix: int, c: u32) + requires + radix == 2 || radix == 8 || radix == 10 || radix == 16, + ensures + is_radix_digit(radix, c) <==> ind_radix_digit(radix, c), +{ +} + +/// `Digits[~Sep]` is a run of one or more digits. +pub proof fn lemma_plain_digits_equiv(radix: int, s: Seq, i: int, j: int) + requires + radix == 2 || radix == 8 || radix == 10 || radix == 16, + ensures + digits(radix, false, s, i, j) <==> ind_plain_digits(radix, s, i, j), + decreases j - i, +{ + if 0 <= i && i + 1 < j && j <= s.len() { + lemma_plain_digits_equiv(radix, s, i, j - 1); + if digits(radix, false, s, i, j) { + assert forall|k: int| i <= k < j implies ind_radix_digit(radix, #[trigger] s[k]) by { + if k < j - 1 { + assert(ind_plain_digits(radix, s, i, j - 1)); + } + } + } + if ind_plain_digits(radix, s, i, j) { + assert(ind_plain_digits(radix, s, i, j - 1)) by { + assert forall|k: int| i <= k < j - 1 implies ind_radix_digit(radix, #[trigger] s[k]) by { + assert(ind_radix_digit(radix, s[k])); + } + } + } + } +} + +/// `Digits[+Sep]` is the closed form: digits and single `_`, ending and +/// starting with a digit. +pub proof fn lemma_sep_digits_equiv(radix: int, s: Seq, i: int, j: int) + requires + radix == 2 || radix == 8 || radix == 10 || radix == 16, + ensures + digits(radix, true, s, i, j) <==> ind_sep_digits(radix, s, i, j), + decreases j - i, +{ + if 0 <= i && i + 1 < j && j <= s.len() { + if i + 2 < j { + lemma_sep_digits_equiv(radix, s, i, j - 2); + } + lemma_sep_digits_equiv(radix, s, i, j - 1); + // (spec ==> independent) + if digits(radix, true, s, i, j) { + if i + 1 < j && ind_radix_digit(radix, s[j - 1]) && digits(radix, true, s, i, j - 1) { + assert(ind_sep_digits(radix, s, i, j - 1)); + assert forall|k: int| i <= k < j - 1 implies !(#[trigger] s[k] == 0x5F && s[k + + 1] == 0x5F) by { + if k < j - 2 { + assert(!(s[k] == 0x5F && s[k + 1] == 0x5F)); + } + } + assert forall|k: int| i <= k < j implies (ind_radix_digit(radix, #[trigger] s[k]) + || s[k] == 0x5F) by { + if k < j - 1 { + assert(ind_radix_digit(radix, s[k]) || s[k] == 0x5F); + } + } + } + if i + 2 < j && s[j - 2] == 0x5F && ind_radix_digit(radix, s[j - 1]) && digits( + radix, + true, + s, + i, + j - 2, + ) { + assert(ind_sep_digits(radix, s, i, j - 2)); + assert forall|k: int| i <= k < j - 1 implies !(#[trigger] s[k] == 0x5F && s[k + + 1] == 0x5F) by { + if k < j - 3 { + assert(!(s[k] == 0x5F && s[k + 1] == 0x5F)); + } + } + assert forall|k: int| i <= k < j implies (ind_radix_digit(radix, #[trigger] s[k]) + || s[k] == 0x5F) by { + if k < j - 2 { + assert(ind_radix_digit(radix, s[k]) || s[k] == 0x5F); + } + } + } + } + // (independent ==> spec) + if ind_sep_digits(radix, s, i, j) { + assert(ind_radix_digit(radix, s[j - 1])); + assert(ind_radix_digit(radix, s[j - 2]) || s[j - 2] == 0x5F); + if ind_radix_digit(radix, s[j - 2]) { + assert(ind_sep_digits(radix, s, i, j - 1)) by { + assert forall|k: int| i <= k < j - 1 implies (ind_radix_digit( + radix, + #[trigger] s[k], + ) || s[k] == 0x5F) by { + assert(ind_radix_digit(radix, s[k]) || s[k] == 0x5F); + } + assert forall|k: int| i <= k < j - 2 implies !(#[trigger] s[k] == 0x5F && s[k + + 1] == 0x5F) by { + assert(!(s[k] == 0x5F && s[k + 1] == 0x5F)); + } + } + } else { + // s[j-2] is `_`, so it is not s[i] (a digit): i + 2 < j. + assert(i + 2 < j); + assert(ind_radix_digit(radix, s[j - 3]) || s[j - 3] == 0x5F); + assert(!(s[j - 3] == 0x5F && s[j - 2] == 0x5F)); + assert(ind_sep_digits(radix, s, i, j - 2)) by { + assert forall|k: int| i <= k < j - 2 implies (ind_radix_digit( + radix, + #[trigger] s[k], + ) || s[k] == 0x5F) by { + assert(ind_radix_digit(radix, s[k]) || s[k] == 0x5F); + } + assert forall|k: int| i <= k < j - 3 implies !(#[trigger] s[k] == 0x5F && s[k + + 1] == 0x5F) by { + assert(!(s[k] == 0x5F && s[k + 1] == 0x5F)); + } + } + } + } + } +} + +/// The value of a run of hex digits is the same fold. +pub proof fn lemma_hex_value_equiv(s: Seq, a: int, b: int) + requires + 0 <= a, + b <= s.len(), + forall|k: int| a <= k < b ==> is_hex_digit(#[trigger] s[k]), + ensures + hex_digits_mv(s, a, b) == ind_hex_run_value(s, a, b), + decreases b - a, +{ + if a < b { + assert(is_hex_digit(s[b - 1])); + lemma_hex_value_equiv(s, a, b - 1); + } +} + +// --------------------------------------------------------------------- +// Numeric literals. +// --------------------------------------------------------------------- + +/// `LegacyOctalLikeDecimalIntegerLiteral` is `0` and one or more octal +/// digits. +pub proof fn lemma_legacy_octal_like_equiv(s: Seq, i: int, j: int) + ensures + legacy_octal_like_decimal_integer_literal(s, i, j) <==> ind_legacy_octal(s, i, j), + decreases j - i, +{ + if 0 <= i && i + 2 < j && j <= s.len() { + lemma_legacy_octal_like_equiv(s, i, j - 1); + lemma_plain_digits_equiv(8, s, i + 1, j); + lemma_plain_digits_equiv(8, s, i + 1, j - 1); + } + if 0 <= i && i + 2 == j && j <= s.len() { + lemma_plain_digits_equiv(8, s, i + 1, j); + } +} + +/// `LegacyOctalIntegerLiteral` is the same language. +pub proof fn lemma_legacy_octal_equiv(s: Seq, i: int, j: int) + ensures + legacy_octal_integer_literal(s, i, j) <==> ind_legacy_octal(s, i, j), + decreases j - i, +{ + if 0 <= i && i + 2 < j && j <= s.len() { + lemma_legacy_octal_equiv(s, i, j - 1); + lemma_plain_digits_equiv(8, s, i + 1, j); + lemma_plain_digits_equiv(8, s, i + 1, j - 1); + } + if 0 <= i && i + 2 == j && j <= s.len() { + lemma_plain_digits_equiv(8, s, i + 1, j); + } +} + +/// `NonOctalDecimalIntegerLiteral` is `0`, decimal digits, one of them 8 +/// or 9. +pub proof fn lemma_non_octal_decimal_equiv(s: Seq, i: int, j: int) + ensures + non_octal_decimal_integer_literal(s, i, j) <==> ind_non_octal_decimal_integer(s, i, j), + decreases j - i, +{ + if 0 <= i && i + 2 < j && j <= s.len() { + lemma_non_octal_decimal_equiv(s, i, j - 1); + lemma_legacy_octal_like_equiv(s, i, j - 1); + lemma_plain_digits_equiv(10, s, i + 1, j); + lemma_plain_digits_equiv(10, s, i + 1, j - 1); + lemma_plain_digits_equiv(8, s, i + 1, j - 1); + // (spec ==> independent) + if non_octal_decimal_integer_literal(s, i, j) { + if is_non_octal_digit(s[j - 1]) && legacy_octal_like_decimal_integer_literal( + s, + i, + j - 1, + ) { + assert(s[j - 1] == 0x38 || s[j - 1] == 0x39); + } + } + // (independent ==> spec) + if ind_non_octal_decimal_integer(s, i, j) { + let k = choose|k: int| i < k < j && (s[k] == 0x38 || s[k] == 0x39); + if exists|k2: int| i < k2 < j - 1 && (#[trigger] s[k2] == 0x38 || s[k2] == 0x39) { + let k2 = choose|k2: int| i < k2 < j - 1 && (s[k2] == 0x38 || s[k2] == 0x39); + assert(ind_non_octal_decimal_integer(s, i, j - 1)); + } else { + assert(k == j - 1); + assert(ind_legacy_octal(s, i, j - 1)) by { + assert forall|t: int| i + 1 <= t < j - 1 implies ind_oct(#[trigger] s[t]) by { + assert(ind_dec(s[t])); + assert(!(s[t] == 0x38 || s[t] == 0x39)); + } + } + } + } + } + if 0 <= i && i + 2 == j && j <= s.len() { + lemma_plain_digits_equiv(10, s, i + 1, j); + if ind_non_octal_decimal_integer(s, i, j) { + assert(s[i + 1] == 0x38 || s[i + 1] == 0x39); + } + } +} + +/// A separated digit run that does not start with `0` is exactly the +/// non-zero-led shapes of §12.9.3: a single `NonZeroDigit`, or one followed +/// by `DecimalDigits[+Sep]` with or without a separator. +pub proof fn lemma_nonzero_led_equiv(s: Seq, i: int, e: int) + requires + 0 <= i < e <= s.len(), + ensures + (ind_sep_digits(10, s, i, e) && s[i] != 0x30) <==> ((e == i + 1 && is_non_zero_digit(s[i])) + || (i + 1 < e && is_non_zero_digit(s[i]) && (decimal_digits(true, s, i + 1, e) || (s[i + 1] + == 0x5F && decimal_digits(true, s, i + 2, e))))), +{ + lemma_sep_digits_equiv(10, s, i + 1, e); + lemma_sep_digits_equiv(10, s, i + 2, e); + if ind_sep_digits(10, s, i, e) && s[i] != 0x30 { + assert(ind_dec(s[i])); + if e > i + 1 { + assert(ind_dec(s[i + 1]) || s[i + 1] == 0x5F); + if ind_dec(s[i + 1]) { + assert(ind_sep_digits(10, s, i + 1, e)) by { + assert forall|k: int| i + 1 <= k < e implies (ind_dec(#[trigger] s[k]) || s[k] + == 0x5F) by { + assert(ind_dec(s[k]) || s[k] == 0x5F); + } + assert forall|k: int| i + 1 <= k < e - 1 implies !(#[trigger] s[k] == 0x5F + && s[k + 1] == 0x5F) by { + assert(!(s[k] == 0x5F && s[k + 1] == 0x5F)); + } + } + } else { + assert(ind_dec(s[e - 1])); + assert(i + 2 < e); + assert(ind_dec(s[i + 2]) || s[i + 2] == 0x5F); + assert(!(s[i + 1] == 0x5F && s[i + 2] == 0x5F)); + assert(ind_sep_digits(10, s, i + 2, e)) by { + assert forall|k: int| i + 2 <= k < e implies (ind_dec(#[trigger] s[k]) || s[k] + == 0x5F) by { + assert(ind_dec(s[k]) || s[k] == 0x5F); + } + assert forall|k: int| i + 2 <= k < e - 1 implies !(#[trigger] s[k] == 0x5F + && s[k + 1] == 0x5F) by { + assert(!(s[k] == 0x5F && s[k + 1] == 0x5F)); + } + } + } + } + } + if e == i + 1 && is_non_zero_digit(s[i]) { + assert(ind_sep_digits(10, s, i, e)); + } + if i + 1 < e && is_non_zero_digit(s[i]) { + if decimal_digits(true, s, i + 1, e) { + assert(ind_sep_digits(10, s, i + 1, e)); + assert(ind_sep_digits(10, s, i, e)) by { + assert forall|k: int| i <= k < e implies (ind_dec(#[trigger] s[k]) || s[k] + == 0x5F) by { + if k > i { + assert(ind_dec(s[k]) || s[k] == 0x5F); + } + } + assert forall|k: int| i <= k < e - 1 implies !(#[trigger] s[k] == 0x5F && s[k + 1] + == 0x5F) by { + if k > i { + assert(!(s[k] == 0x5F && s[k + 1] == 0x5F)); + } + } + } + } + if s[i + 1] == 0x5F && decimal_digits(true, s, i + 2, e) { + assert(ind_sep_digits(10, s, i + 2, e)); + assert(ind_sep_digits(10, s, i, e)) by { + assert forall|k: int| i <= k < e implies (ind_dec(#[trigger] s[k]) || s[k] + == 0x5F) by { + if k > i + 1 { + assert(ind_dec(s[k]) || s[k] == 0x5F); + } + } + assert forall|k: int| i <= k < e - 1 implies !(#[trigger] s[k] == 0x5F && s[k + 1] + == 0x5F) by { + if k > i + 1 { + assert(!(s[k] == 0x5F && s[k + 1] == 0x5F)); + } + } + } + } + } +} + +/// `DecimalIntegerLiteral` (§12.9.3). +pub proof fn lemma_decimal_integer_equiv(s: Seq, i: int, j: int) + ensures + decimal_integer_literal(s, i, j) <==> ind_decimal_integer(s, i, j), +{ + lemma_non_octal_decimal_equiv(s, i, j); + if 0 <= i && i < j && j <= s.len() { + lemma_nonzero_led_equiv(s, i, j); + } +} + +/// `ExponentPart[+Sep]` (§12.9.3). +pub proof fn lemma_exponent_equiv(s: Seq, i: int, j: int) + ensures + exponent_part(true, s, i, j) <==> ind_exponent(s, i, j), +{ + lemma_sep_digits_equiv(10, s, i + 1, j); + lemma_sep_digits_equiv(10, s, i + 2, j); +} + +/// The first alternative's body is the independent one, at every split. +pub proof fn lemma_alt1_pointwise(s: Seq, i: int, j: int, a: int, b: int) + ensures + decimal_literal_1(s, i, a, b, j) <==> ind_dl_alt1(s, i, j, a, b), +{ + lemma_sep_digits_equiv(10, s, a + 1, b); + lemma_decimal_integer_equiv(s, i, a); + lemma_exponent_equiv(s, b, j); + if ind_dl_alt1(s, i, j, a, b) { + assert(j <= s.len()) by { + if b == j { + if b != a + 1 { + assert(ind_sep_digits(10, s, a + 1, b)); + } + } else { + assert(ind_exponent(s, b, j)); + assert(ind_sep_digits(10, s, b + 1, j) || ind_sep_digits(10, s, b + 2, j)); + } + } + } +} + +/// The body of the second alternative of `DecimalLiteral`, at the end `b` +/// of the digits. +pub open spec fn sp_alt2(s: Seq, i: int, j: int, b: int) -> bool { + i + 1 < b && b <= j && decimal_digits(true, s, i + 1, b) && opt( + b, + j, + exponent_part(true, s, b, j), + ) +} + +/// The body of the third alternative, at the end `a` of the integer. +pub open spec fn sp_alt3(s: Seq, i: int, j: int, a: int) -> bool { + i < a && a <= j && decimal_integer_literal(s, i, a) && opt(a, j, exponent_part(true, s, a, j)) +} + +/// The second and third alternatives' bodies are the independent ones. +pub proof fn lemma_alt23_pointwise(s: Seq, i: int, j: int, k: int) + ensures + (ind_is(s, i, 0x2E) && sp_alt2(s, i, j, k)) <==> ind_dl_alt2(s, i, j, k), + sp_alt3(s, i, j, k) <==> ind_dl_alt3(s, i, j, k), +{ + lemma_sep_digits_equiv(10, s, i + 1, k); + lemma_exponent_equiv(s, k, j); + lemma_decimal_integer_equiv(s, i, k); +} + +/// `DecimalLiteral` (§12.9.3). +pub proof fn lemma_decimal_literal_equiv(s: Seq, i: int, j: int) + ensures + decimal_literal(s, i, j) <==> ind_decimal_literal(s, i, j), +{ + // spec ==> independent + if decimal_literal(s, i, j) { + if exists|a: int, b: int| decimal_literal_1(s, i, a, b, j) { + let (a, b) = choose|a: int, b: int| decimal_literal_1(s, i, a, b, j); + lemma_alt1_pointwise(s, i, j, a, b); + assert(ind_dl_alt1(s, i, j, a, b)); + } else if s[i] == 0x2E && exists|b: int| #![auto] + i + 1 < b && b <= j && decimal_digits(true, s, i + 1, b) && opt( + b, + j, + exponent_part(true, s, b, j), + ) { + let b = choose|b: int| #![auto] + i + 1 < b && b <= j && decimal_digits(true, s, i + 1, b) && opt( + b, + j, + exponent_part(true, s, b, j), + ); + assert(sp_alt2(s, i, j, b)); + lemma_alt23_pointwise(s, i, j, b); + assert(ind_dl_alt2(s, i, j, b)); + } else if exists|a: int| #![auto] + i < a && a <= j && decimal_integer_literal(s, i, a) && opt( + a, + j, + exponent_part(true, s, a, j), + ) { + let a = choose|a: int| #![auto] + i < a && a <= j && decimal_integer_literal(s, i, a) && opt( + a, + j, + exponent_part(true, s, a, j), + ); + assert(sp_alt3(s, i, j, a)); + lemma_alt23_pointwise(s, i, j, a); + assert(ind_dl_alt3(s, i, j, a)); + } + } + // independent ==> spec + if ind_decimal_literal(s, i, j) { + if exists|a: int, b: int| ind_dl_alt1(s, i, j, a, b) { + let (a, b) = choose|a: int, b: int| ind_dl_alt1(s, i, j, a, b); + lemma_alt1_pointwise(s, i, j, a, b); + assert(decimal_literal_1(s, i, a, b, j)); + assert(decimal_literal(s, i, j)); + } else if exists|b: int| ind_dl_alt2(s, i, j, b) { + let b = choose|b: int| ind_dl_alt2(s, i, j, b); + lemma_alt23_pointwise(s, i, j, b); + assert(sp_alt2(s, i, j, b)); + assert(decimal_literal(s, i, j)); + } else if exists|a: int| ind_dl_alt3(s, i, j, a) { + let a = choose|a: int| ind_dl_alt3(s, i, j, a); + lemma_alt23_pointwise(s, i, j, a); + assert(sp_alt3(s, i, j, a)); + assert(decimal_literal(s, i, j)); + } + } +} + +/// `DecimalBigIntegerLiteral` (§12.9.3). +pub proof fn lemma_decimal_bigint_equiv(s: Seq, i: int, j: int) + ensures + decimal_big_integer_literal(s, i, j) <==> ind_decimal_bigint(s, i, j), +{ + if 0 <= i && i + 1 < j && j <= s.len() && s[j - 1] == 0x6E { + lemma_nonzero_led_equiv(s, i, j - 1); + lemma_sep_digits_equiv(10, s, i + 1, j - 1); + } +} + +/// `NonDecimalIntegerLiteral[+Sep]` (§12.9.3). +pub proof fn lemma_non_decimal_equiv(s: Seq, i: int, j: int) + ensures + non_decimal_integer_literal(true, s, i, j) <==> ind_non_decimal_integer(s, i, j), +{ + lemma_sep_digits_equiv(2, s, i + 2, j); + lemma_sep_digits_equiv(8, s, i + 2, j); + lemma_sep_digits_equiv(16, s, i + 2, j); +} + +/// `NumericLiteral` (§12.9.3): equal as relations on every range. +pub proof fn lemma_numeric_literal_equiv(s: Seq, i: int, j: int) + ensures + numeric_literal(s, i, j) <==> ind_numeric_literal(s, i, j), +{ + lemma_decimal_literal_equiv(s, i, j); + lemma_decimal_bigint_equiv(s, i, j); + lemma_non_decimal_equiv(s, i, j); + lemma_non_decimal_equiv(s, i, j - 1); + lemma_legacy_octal_equiv(s, i, j); +} + +// --------------------------------------------------------------------- +// Unicode escape sequences. +// --------------------------------------------------------------------- + +/// `ind_hex_run_end` is the first index at or after `k` that is not a hex +/// digit: everything before it is a hex digit, and it is not one (or is the +/// end). +pub proof fn lemma_hex_run_end(s: Seq, k: int) + requires + 0 <= k <= s.len(), + ensures + k <= ind_hex_run_end(s, k) <= s.len(), + forall|t: int| k <= t < ind_hex_run_end(s, k) ==> ind_hex(#[trigger] s[t]), + ind_hex_run_end(s, k) == s.len() || !ind_hex(s[ind_hex_run_end(s, k)]), + decreases s.len() - k, +{ + if k < s.len() && ind_hex(s[k]) { + lemma_hex_run_end(s, k + 1); + let m = ind_hex_run_end(s, k + 1); + assert forall|t: int| k <= t < m implies ind_hex(#[trigger] s[t]) by { + if t > k { + assert(ind_hex(s[t])); + } + } + } +} + +/// `UnicodeEscapeSequence` (§12.9.4): the deterministic reading ends at `j` +/// exactly when the grammar derives `s[i..j)`. +pub proof fn lemma_unicode_escape_equiv(s: Seq, i: int, j: int) + ensures + unicode_escape_sequence(s, i, j) <==> ind_unicode_escape(s, i, j), +{ + lemma_is_lit(s, i, i + 2, seq!['u', '{']); + if 0 <= i && i + 2 <= s.len() { + lemma_hex_run_end(s, i + 1); + lemma_hex_run_end(s, i + 2); + let m1 = ind_hex_run_end(s, i + 1); + let m = ind_hex_run_end(s, i + 2); + // (grammar ==> deterministic reading) + if unicode_escape_sequence(s, i, j) { + if s[i + 1] == 0x7B { + // u{ CodePoint } + lemma_plain_digits_equiv(16, s, i + 2, j - 1); + assert(ind_plain_digits(16, s, i + 2, j - 1)); + assert(forall|t: int| i + 2 <= t < j - 1 ==> is_hex_digit(s[t])); + lemma_hex_value_equiv(s, i + 2, j - 1); + assert(m == j - 1) by { + if m < j - 1 { + assert(ind_hex(s[m])); + } + if m > j - 1 { + assert(ind_hex(s[j - 1])); + } + } + } else { + // u Hex4Digits + assert(j == i + 5); + assert(m1 >= i + 5) by { + if m1 < i + 5 { + assert(ind_hex(s[m1])); + } + } + } + } + // (deterministic reading ==> grammar) + if ind_unicode_escape(s, i, j) { + if s[i + 1] == 0x7B { + assert(m > i + 2 && s[m] == 0x7D && j == m + 1); + lemma_hex_value_equiv(s, i + 2, m); + lemma_plain_digits_equiv(16, s, i + 2, m); + assert(ind_plain_digits(16, s, i + 2, m)); + } else { + assert(j == i + 5); + lemma_plain_digits_equiv(16, s, i + 1, i + 5); + assert(forall|t: int| i + 1 <= t < i + 5 ==> ind_hex(s[t])); + } + } + } +} + +// --------------------------------------------------------------------- +// Punctuators and reserved words. +// --------------------------------------------------------------------- + +/// The list of `OtherPunctuator` has 53 entries. +pub proof fn lemma_other_punctuators_len() + ensures + other_punctuators().len() == 53, +{ +} + +/// `OtherPunctuator` (§12.8) is the independent one-to-four-code-point +/// classification, at every range. The proof walks the 53 entries of the +/// standard's list one by one in each direction. +pub proof fn lemma_other_punctuator_equiv(s: Seq, i: int, j: int) + ensures + other_punctuator(s, i, j) <==> ind_other_punctuator(s, i, j), +{ + lemma_other_punctuators_len(); + // (spec ==> independent): every entry of the list has one of the forms. + if other_punctuator(s, i, j) { + let n = choose|n: int| + 0 <= n < other_punctuators().len() && is_lit(s, i, j, #[trigger] other_punctuators()[n]); + if n == 0 { + lemma_is_lit(s, i, j, other_punctuators()[0]); + assert(other_punctuators()[0][0] as u32 == 0x7B); + assert(s[i + 0] == 0x7B); + assert(ind_other_punctuator(s, i, j)); + } + if n == 1 { + lemma_is_lit(s, i, j, other_punctuators()[1]); + assert(other_punctuators()[1][0] as u32 == 0x28); + assert(s[i + 0] == 0x28); + assert(ind_other_punctuator(s, i, j)); + } + if n == 2 { + lemma_is_lit(s, i, j, other_punctuators()[2]); + assert(other_punctuators()[2][0] as u32 == 0x29); + assert(s[i + 0] == 0x29); + assert(ind_other_punctuator(s, i, j)); + } + if n == 3 { + lemma_is_lit(s, i, j, other_punctuators()[3]); + assert(other_punctuators()[3][0] as u32 == 0x5B); + assert(s[i + 0] == 0x5B); + assert(ind_other_punctuator(s, i, j)); + } + if n == 4 { + lemma_is_lit(s, i, j, other_punctuators()[4]); + assert(other_punctuators()[4][0] as u32 == 0x5D); + assert(s[i + 0] == 0x5D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 5 { + lemma_is_lit(s, i, j, other_punctuators()[5]); + assert(other_punctuators()[5][0] as u32 == 0x2E); + assert(s[i + 0] == 0x2E); + assert(ind_other_punctuator(s, i, j)); + } + if n == 6 { + lemma_is_lit(s, i, j, other_punctuators()[6]); + assert(other_punctuators()[6][0] as u32 == 0x2E); + assert(s[i + 0] == 0x2E); + assert(other_punctuators()[6][1] as u32 == 0x2E); + assert(s[i + 1] == 0x2E); + assert(other_punctuators()[6][2] as u32 == 0x2E); + assert(s[i + 2] == 0x2E); + assert(ind_other_punctuator(s, i, j)); + } + if n == 7 { + lemma_is_lit(s, i, j, other_punctuators()[7]); + assert(other_punctuators()[7][0] as u32 == 0x3B); + assert(s[i + 0] == 0x3B); + assert(ind_other_punctuator(s, i, j)); + } + if n == 8 { + lemma_is_lit(s, i, j, other_punctuators()[8]); + assert(other_punctuators()[8][0] as u32 == 0x2C); + assert(s[i + 0] == 0x2C); + assert(ind_other_punctuator(s, i, j)); + } + if n == 9 { + lemma_is_lit(s, i, j, other_punctuators()[9]); + assert(other_punctuators()[9][0] as u32 == 0x3C); + assert(s[i + 0] == 0x3C); + assert(ind_other_punctuator(s, i, j)); + } + if n == 10 { + lemma_is_lit(s, i, j, other_punctuators()[10]); + assert(other_punctuators()[10][0] as u32 == 0x3E); + assert(s[i + 0] == 0x3E); + assert(ind_other_punctuator(s, i, j)); + } + if n == 11 { + lemma_is_lit(s, i, j, other_punctuators()[11]); + assert(other_punctuators()[11][0] as u32 == 0x3C); + assert(s[i + 0] == 0x3C); + assert(other_punctuators()[11][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 12 { + lemma_is_lit(s, i, j, other_punctuators()[12]); + assert(other_punctuators()[12][0] as u32 == 0x3E); + assert(s[i + 0] == 0x3E); + assert(other_punctuators()[12][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 13 { + lemma_is_lit(s, i, j, other_punctuators()[13]); + assert(other_punctuators()[13][0] as u32 == 0x3D); + assert(s[i + 0] == 0x3D); + assert(other_punctuators()[13][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 14 { + lemma_is_lit(s, i, j, other_punctuators()[14]); + assert(other_punctuators()[14][0] as u32 == 0x21); + assert(s[i + 0] == 0x21); + assert(other_punctuators()[14][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 15 { + lemma_is_lit(s, i, j, other_punctuators()[15]); + assert(other_punctuators()[15][0] as u32 == 0x3D); + assert(s[i + 0] == 0x3D); + assert(other_punctuators()[15][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(other_punctuators()[15][2] as u32 == 0x3D); + assert(s[i + 2] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 16 { + lemma_is_lit(s, i, j, other_punctuators()[16]); + assert(other_punctuators()[16][0] as u32 == 0x21); + assert(s[i + 0] == 0x21); + assert(other_punctuators()[16][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(other_punctuators()[16][2] as u32 == 0x3D); + assert(s[i + 2] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 17 { + lemma_is_lit(s, i, j, other_punctuators()[17]); + assert(other_punctuators()[17][0] as u32 == 0x2B); + assert(s[i + 0] == 0x2B); + assert(ind_other_punctuator(s, i, j)); + } + if n == 18 { + lemma_is_lit(s, i, j, other_punctuators()[18]); + assert(other_punctuators()[18][0] as u32 == 0x2D); + assert(s[i + 0] == 0x2D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 19 { + lemma_is_lit(s, i, j, other_punctuators()[19]); + assert(other_punctuators()[19][0] as u32 == 0x2A); + assert(s[i + 0] == 0x2A); + assert(ind_other_punctuator(s, i, j)); + } + if n == 20 { + lemma_is_lit(s, i, j, other_punctuators()[20]); + assert(other_punctuators()[20][0] as u32 == 0x25); + assert(s[i + 0] == 0x25); + assert(ind_other_punctuator(s, i, j)); + } + if n == 21 { + lemma_is_lit(s, i, j, other_punctuators()[21]); + assert(other_punctuators()[21][0] as u32 == 0x2A); + assert(s[i + 0] == 0x2A); + assert(other_punctuators()[21][1] as u32 == 0x2A); + assert(s[i + 1] == 0x2A); + assert(ind_other_punctuator(s, i, j)); + } + if n == 22 { + lemma_is_lit(s, i, j, other_punctuators()[22]); + assert(other_punctuators()[22][0] as u32 == 0x2B); + assert(s[i + 0] == 0x2B); + assert(other_punctuators()[22][1] as u32 == 0x2B); + assert(s[i + 1] == 0x2B); + assert(ind_other_punctuator(s, i, j)); + } + if n == 23 { + lemma_is_lit(s, i, j, other_punctuators()[23]); + assert(other_punctuators()[23][0] as u32 == 0x2D); + assert(s[i + 0] == 0x2D); + assert(other_punctuators()[23][1] as u32 == 0x2D); + assert(s[i + 1] == 0x2D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 24 { + lemma_is_lit(s, i, j, other_punctuators()[24]); + assert(other_punctuators()[24][0] as u32 == 0x3C); + assert(s[i + 0] == 0x3C); + assert(other_punctuators()[24][1] as u32 == 0x3C); + assert(s[i + 1] == 0x3C); + assert(ind_other_punctuator(s, i, j)); + } + if n == 25 { + lemma_is_lit(s, i, j, other_punctuators()[25]); + assert(other_punctuators()[25][0] as u32 == 0x3E); + assert(s[i + 0] == 0x3E); + assert(other_punctuators()[25][1] as u32 == 0x3E); + assert(s[i + 1] == 0x3E); + assert(ind_other_punctuator(s, i, j)); + } + if n == 26 { + lemma_is_lit(s, i, j, other_punctuators()[26]); + assert(other_punctuators()[26][0] as u32 == 0x3E); + assert(s[i + 0] == 0x3E); + assert(other_punctuators()[26][1] as u32 == 0x3E); + assert(s[i + 1] == 0x3E); + assert(other_punctuators()[26][2] as u32 == 0x3E); + assert(s[i + 2] == 0x3E); + assert(ind_other_punctuator(s, i, j)); + } + if n == 27 { + lemma_is_lit(s, i, j, other_punctuators()[27]); + assert(other_punctuators()[27][0] as u32 == 0x26); + assert(s[i + 0] == 0x26); + assert(ind_other_punctuator(s, i, j)); + } + if n == 28 { + lemma_is_lit(s, i, j, other_punctuators()[28]); + assert(other_punctuators()[28][0] as u32 == 0x7C); + assert(s[i + 0] == 0x7C); + assert(ind_other_punctuator(s, i, j)); + } + if n == 29 { + lemma_is_lit(s, i, j, other_punctuators()[29]); + assert(other_punctuators()[29][0] as u32 == 0x5E); + assert(s[i + 0] == 0x5E); + assert(ind_other_punctuator(s, i, j)); + } + if n == 30 { + lemma_is_lit(s, i, j, other_punctuators()[30]); + assert(other_punctuators()[30][0] as u32 == 0x21); + assert(s[i + 0] == 0x21); + assert(ind_other_punctuator(s, i, j)); + } + if n == 31 { + lemma_is_lit(s, i, j, other_punctuators()[31]); + assert(other_punctuators()[31][0] as u32 == 0x7E); + assert(s[i + 0] == 0x7E); + assert(ind_other_punctuator(s, i, j)); + } + if n == 32 { + lemma_is_lit(s, i, j, other_punctuators()[32]); + assert(other_punctuators()[32][0] as u32 == 0x26); + assert(s[i + 0] == 0x26); + assert(other_punctuators()[32][1] as u32 == 0x26); + assert(s[i + 1] == 0x26); + assert(ind_other_punctuator(s, i, j)); + } + if n == 33 { + lemma_is_lit(s, i, j, other_punctuators()[33]); + assert(other_punctuators()[33][0] as u32 == 0x7C); + assert(s[i + 0] == 0x7C); + assert(other_punctuators()[33][1] as u32 == 0x7C); + assert(s[i + 1] == 0x7C); + assert(ind_other_punctuator(s, i, j)); + } + if n == 34 { + lemma_is_lit(s, i, j, other_punctuators()[34]); + assert(other_punctuators()[34][0] as u32 == 0x3F); + assert(s[i + 0] == 0x3F); + assert(other_punctuators()[34][1] as u32 == 0x3F); + assert(s[i + 1] == 0x3F); + assert(ind_other_punctuator(s, i, j)); + } + if n == 35 { + lemma_is_lit(s, i, j, other_punctuators()[35]); + assert(other_punctuators()[35][0] as u32 == 0x3F); + assert(s[i + 0] == 0x3F); + assert(ind_other_punctuator(s, i, j)); + } + if n == 36 { + lemma_is_lit(s, i, j, other_punctuators()[36]); + assert(other_punctuators()[36][0] as u32 == 0x3A); + assert(s[i + 0] == 0x3A); + assert(ind_other_punctuator(s, i, j)); + } + if n == 37 { + lemma_is_lit(s, i, j, other_punctuators()[37]); + assert(other_punctuators()[37][0] as u32 == 0x3D); + assert(s[i + 0] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 38 { + lemma_is_lit(s, i, j, other_punctuators()[38]); + assert(other_punctuators()[38][0] as u32 == 0x2B); + assert(s[i + 0] == 0x2B); + assert(other_punctuators()[38][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 39 { + lemma_is_lit(s, i, j, other_punctuators()[39]); + assert(other_punctuators()[39][0] as u32 == 0x2D); + assert(s[i + 0] == 0x2D); + assert(other_punctuators()[39][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 40 { + lemma_is_lit(s, i, j, other_punctuators()[40]); + assert(other_punctuators()[40][0] as u32 == 0x2A); + assert(s[i + 0] == 0x2A); + assert(other_punctuators()[40][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 41 { + lemma_is_lit(s, i, j, other_punctuators()[41]); + assert(other_punctuators()[41][0] as u32 == 0x25); + assert(s[i + 0] == 0x25); + assert(other_punctuators()[41][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 42 { + lemma_is_lit(s, i, j, other_punctuators()[42]); + assert(other_punctuators()[42][0] as u32 == 0x2A); + assert(s[i + 0] == 0x2A); + assert(other_punctuators()[42][1] as u32 == 0x2A); + assert(s[i + 1] == 0x2A); + assert(other_punctuators()[42][2] as u32 == 0x3D); + assert(s[i + 2] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 43 { + lemma_is_lit(s, i, j, other_punctuators()[43]); + assert(other_punctuators()[43][0] as u32 == 0x3C); + assert(s[i + 0] == 0x3C); + assert(other_punctuators()[43][1] as u32 == 0x3C); + assert(s[i + 1] == 0x3C); + assert(other_punctuators()[43][2] as u32 == 0x3D); + assert(s[i + 2] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 44 { + lemma_is_lit(s, i, j, other_punctuators()[44]); + assert(other_punctuators()[44][0] as u32 == 0x3E); + assert(s[i + 0] == 0x3E); + assert(other_punctuators()[44][1] as u32 == 0x3E); + assert(s[i + 1] == 0x3E); + assert(other_punctuators()[44][2] as u32 == 0x3D); + assert(s[i + 2] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 45 { + lemma_is_lit(s, i, j, other_punctuators()[45]); + assert(other_punctuators()[45][0] as u32 == 0x3E); + assert(s[i + 0] == 0x3E); + assert(other_punctuators()[45][1] as u32 == 0x3E); + assert(s[i + 1] == 0x3E); + assert(other_punctuators()[45][2] as u32 == 0x3E); + assert(s[i + 2] == 0x3E); + assert(other_punctuators()[45][3] as u32 == 0x3D); + assert(s[i + 3] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 46 { + lemma_is_lit(s, i, j, other_punctuators()[46]); + assert(other_punctuators()[46][0] as u32 == 0x26); + assert(s[i + 0] == 0x26); + assert(other_punctuators()[46][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 47 { + lemma_is_lit(s, i, j, other_punctuators()[47]); + assert(other_punctuators()[47][0] as u32 == 0x7C); + assert(s[i + 0] == 0x7C); + assert(other_punctuators()[47][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 48 { + lemma_is_lit(s, i, j, other_punctuators()[48]); + assert(other_punctuators()[48][0] as u32 == 0x5E); + assert(s[i + 0] == 0x5E); + assert(other_punctuators()[48][1] as u32 == 0x3D); + assert(s[i + 1] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 49 { + lemma_is_lit(s, i, j, other_punctuators()[49]); + assert(other_punctuators()[49][0] as u32 == 0x26); + assert(s[i + 0] == 0x26); + assert(other_punctuators()[49][1] as u32 == 0x26); + assert(s[i + 1] == 0x26); + assert(other_punctuators()[49][2] as u32 == 0x3D); + assert(s[i + 2] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 50 { + lemma_is_lit(s, i, j, other_punctuators()[50]); + assert(other_punctuators()[50][0] as u32 == 0x7C); + assert(s[i + 0] == 0x7C); + assert(other_punctuators()[50][1] as u32 == 0x7C); + assert(s[i + 1] == 0x7C); + assert(other_punctuators()[50][2] as u32 == 0x3D); + assert(s[i + 2] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 51 { + lemma_is_lit(s, i, j, other_punctuators()[51]); + assert(other_punctuators()[51][0] as u32 == 0x3F); + assert(s[i + 0] == 0x3F); + assert(other_punctuators()[51][1] as u32 == 0x3F); + assert(s[i + 1] == 0x3F); + assert(other_punctuators()[51][2] as u32 == 0x3D); + assert(s[i + 2] == 0x3D); + assert(ind_other_punctuator(s, i, j)); + } + if n == 52 { + lemma_is_lit(s, i, j, other_punctuators()[52]); + assert(other_punctuators()[52][0] as u32 == 0x3D); + assert(s[i + 0] == 0x3D); + assert(other_punctuators()[52][1] as u32 == 0x3E); + assert(s[i + 1] == 0x3E); + assert(ind_other_punctuator(s, i, j)); + } + } + // (independent ==> spec): each form is an entry of the list. + if ind_other_punctuator(s, i, j) { + if j == i + 1 && s[i + 0] == 0x7B { + lemma_is_lit(s, i, j, other_punctuators()[0]); + assert(is_lit(s, i, j, other_punctuators()[0])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x28 { + lemma_is_lit(s, i, j, other_punctuators()[1]); + assert(is_lit(s, i, j, other_punctuators()[1])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x29 { + lemma_is_lit(s, i, j, other_punctuators()[2]); + assert(is_lit(s, i, j, other_punctuators()[2])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x5B { + lemma_is_lit(s, i, j, other_punctuators()[3]); + assert(is_lit(s, i, j, other_punctuators()[3])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x5D { + lemma_is_lit(s, i, j, other_punctuators()[4]); + assert(is_lit(s, i, j, other_punctuators()[4])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x2E { + lemma_is_lit(s, i, j, other_punctuators()[5]); + assert(is_lit(s, i, j, other_punctuators()[5])); + assert(other_punctuator(s, i, j)); + } else if j == i + 3 && s[i + 0] == 0x2E && s[i + 1] == 0x2E && s[i + 2] == 0x2E { + lemma_is_lit(s, i, j, other_punctuators()[6]); + assert(is_lit(s, i, j, other_punctuators()[6])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x3B { + lemma_is_lit(s, i, j, other_punctuators()[7]); + assert(is_lit(s, i, j, other_punctuators()[7])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x2C { + lemma_is_lit(s, i, j, other_punctuators()[8]); + assert(is_lit(s, i, j, other_punctuators()[8])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x3C { + lemma_is_lit(s, i, j, other_punctuators()[9]); + assert(is_lit(s, i, j, other_punctuators()[9])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x3E { + lemma_is_lit(s, i, j, other_punctuators()[10]); + assert(is_lit(s, i, j, other_punctuators()[10])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x3C && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[11]); + assert(is_lit(s, i, j, other_punctuators()[11])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x3E && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[12]); + assert(is_lit(s, i, j, other_punctuators()[12])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x3D && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[13]); + assert(is_lit(s, i, j, other_punctuators()[13])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x21 && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[14]); + assert(is_lit(s, i, j, other_punctuators()[14])); + assert(other_punctuator(s, i, j)); + } else if j == i + 3 && s[i + 0] == 0x3D && s[i + 1] == 0x3D && s[i + 2] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[15]); + assert(is_lit(s, i, j, other_punctuators()[15])); + assert(other_punctuator(s, i, j)); + } else if j == i + 3 && s[i + 0] == 0x21 && s[i + 1] == 0x3D && s[i + 2] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[16]); + assert(is_lit(s, i, j, other_punctuators()[16])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x2B { + lemma_is_lit(s, i, j, other_punctuators()[17]); + assert(is_lit(s, i, j, other_punctuators()[17])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x2D { + lemma_is_lit(s, i, j, other_punctuators()[18]); + assert(is_lit(s, i, j, other_punctuators()[18])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x2A { + lemma_is_lit(s, i, j, other_punctuators()[19]); + assert(is_lit(s, i, j, other_punctuators()[19])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x25 { + lemma_is_lit(s, i, j, other_punctuators()[20]); + assert(is_lit(s, i, j, other_punctuators()[20])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x2A && s[i + 1] == 0x2A { + lemma_is_lit(s, i, j, other_punctuators()[21]); + assert(is_lit(s, i, j, other_punctuators()[21])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x2B && s[i + 1] == 0x2B { + lemma_is_lit(s, i, j, other_punctuators()[22]); + assert(is_lit(s, i, j, other_punctuators()[22])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x2D && s[i + 1] == 0x2D { + lemma_is_lit(s, i, j, other_punctuators()[23]); + assert(is_lit(s, i, j, other_punctuators()[23])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x3C && s[i + 1] == 0x3C { + lemma_is_lit(s, i, j, other_punctuators()[24]); + assert(is_lit(s, i, j, other_punctuators()[24])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x3E && s[i + 1] == 0x3E { + lemma_is_lit(s, i, j, other_punctuators()[25]); + assert(is_lit(s, i, j, other_punctuators()[25])); + assert(other_punctuator(s, i, j)); + } else if j == i + 3 && s[i + 0] == 0x3E && s[i + 1] == 0x3E && s[i + 2] == 0x3E { + lemma_is_lit(s, i, j, other_punctuators()[26]); + assert(is_lit(s, i, j, other_punctuators()[26])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x26 { + lemma_is_lit(s, i, j, other_punctuators()[27]); + assert(is_lit(s, i, j, other_punctuators()[27])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x7C { + lemma_is_lit(s, i, j, other_punctuators()[28]); + assert(is_lit(s, i, j, other_punctuators()[28])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x5E { + lemma_is_lit(s, i, j, other_punctuators()[29]); + assert(is_lit(s, i, j, other_punctuators()[29])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x21 { + lemma_is_lit(s, i, j, other_punctuators()[30]); + assert(is_lit(s, i, j, other_punctuators()[30])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x7E { + lemma_is_lit(s, i, j, other_punctuators()[31]); + assert(is_lit(s, i, j, other_punctuators()[31])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x26 && s[i + 1] == 0x26 { + lemma_is_lit(s, i, j, other_punctuators()[32]); + assert(is_lit(s, i, j, other_punctuators()[32])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x7C && s[i + 1] == 0x7C { + lemma_is_lit(s, i, j, other_punctuators()[33]); + assert(is_lit(s, i, j, other_punctuators()[33])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x3F && s[i + 1] == 0x3F { + lemma_is_lit(s, i, j, other_punctuators()[34]); + assert(is_lit(s, i, j, other_punctuators()[34])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x3F { + lemma_is_lit(s, i, j, other_punctuators()[35]); + assert(is_lit(s, i, j, other_punctuators()[35])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x3A { + lemma_is_lit(s, i, j, other_punctuators()[36]); + assert(is_lit(s, i, j, other_punctuators()[36])); + assert(other_punctuator(s, i, j)); + } else if j == i + 1 && s[i + 0] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[37]); + assert(is_lit(s, i, j, other_punctuators()[37])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x2B && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[38]); + assert(is_lit(s, i, j, other_punctuators()[38])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x2D && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[39]); + assert(is_lit(s, i, j, other_punctuators()[39])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x2A && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[40]); + assert(is_lit(s, i, j, other_punctuators()[40])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x25 && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[41]); + assert(is_lit(s, i, j, other_punctuators()[41])); + assert(other_punctuator(s, i, j)); + } else if j == i + 3 && s[i + 0] == 0x2A && s[i + 1] == 0x2A && s[i + 2] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[42]); + assert(is_lit(s, i, j, other_punctuators()[42])); + assert(other_punctuator(s, i, j)); + } else if j == i + 3 && s[i + 0] == 0x3C && s[i + 1] == 0x3C && s[i + 2] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[43]); + assert(is_lit(s, i, j, other_punctuators()[43])); + assert(other_punctuator(s, i, j)); + } else if j == i + 3 && s[i + 0] == 0x3E && s[i + 1] == 0x3E && s[i + 2] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[44]); + assert(is_lit(s, i, j, other_punctuators()[44])); + assert(other_punctuator(s, i, j)); + } else if j == i + 4 && s[i + 0] == 0x3E && s[i + 1] == 0x3E && s[i + 2] == 0x3E && s[i + 3] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[45]); + assert(is_lit(s, i, j, other_punctuators()[45])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x26 && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[46]); + assert(is_lit(s, i, j, other_punctuators()[46])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x7C && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[47]); + assert(is_lit(s, i, j, other_punctuators()[47])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x5E && s[i + 1] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[48]); + assert(is_lit(s, i, j, other_punctuators()[48])); + assert(other_punctuator(s, i, j)); + } else if j == i + 3 && s[i + 0] == 0x26 && s[i + 1] == 0x26 && s[i + 2] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[49]); + assert(is_lit(s, i, j, other_punctuators()[49])); + assert(other_punctuator(s, i, j)); + } else if j == i + 3 && s[i + 0] == 0x7C && s[i + 1] == 0x7C && s[i + 2] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[50]); + assert(is_lit(s, i, j, other_punctuators()[50])); + assert(other_punctuator(s, i, j)); + } else if j == i + 3 && s[i + 0] == 0x3F && s[i + 1] == 0x3F && s[i + 2] == 0x3D { + lemma_is_lit(s, i, j, other_punctuators()[51]); + assert(is_lit(s, i, j, other_punctuators()[51])); + assert(other_punctuator(s, i, j)); + } else if j == i + 2 && s[i + 0] == 0x3D && s[i + 1] == 0x3E { + lemma_is_lit(s, i, j, other_punctuators()[52]); + assert(is_lit(s, i, j, other_punctuators()[52])); + assert(other_punctuator(s, i, j)); + } else { + assert(false); + } + } +} + +/// `OptionalChainingPunctuator` (§12.8), with its lookahead. +pub proof fn lemma_optional_chaining_equiv(s: Seq, i: int, j: int) + ensures + optional_chaining_punctuator(s, i, j) <==> ind_optional_chaining(s, i, j), +{ + lemma_is_lit(s, i, j, seq!['?', '.']); +} + +/// `DivPunctuator` (§12.8). +pub proof fn lemma_div_punctuator_equiv(s: Seq, i: int, j: int) + ensures + div_punctuator(s, i, j) <==> ind_div_punctuator(s, i, j), +{ + lemma_is_lit(s, i, j, seq!['/']); + lemma_is_lit(s, i, j, seq!['/', '=']); + assert(seq!['/'][0] as u32 == 0x2F); + assert(seq!['/', '='][0] as u32 == 0x2F); + assert(seq!['/', '='][1] as u32 == 0x3D); +} + +/// `RightBracePunctuator` (§12.8). +pub proof fn lemma_right_brace_equiv(s: Seq, i: int, j: int) + ensures + right_brace_punctuator(s, i, j) <==> ind_right_brace(s, i, j), +{ + lemma_is_lit(s, i, j, seq!['}']); + assert(seq!['}'][0] as u32 == 0x7D); +} + +/// The two ways of writing "the code points of an ASCII word" agree. +pub proof fn lemma_word_equiv(w: Seq) + ensures + word(w) =~= ind_word(w), +{ +} + +/// The two lists of reserved words are the same sequence of words. +pub proof fn lemma_reserved_words_equiv() + ensures + reserved_words() =~~= ind_reserved_words(), +{ +} + +/// `ReservedWord` (§12.7.2): a name written without escapes is reserved +/// exactly when its code points spell one of the words. +pub proof fn lemma_reserved_word_equiv(cps: Seq) + ensures + is_reserved_word(cps, false) <==> ind_is_reserved(cps), +{ + lemma_reserved_words_equiv(); + assert(reserved_words() == ind_reserved_words()); + assert forall|n: int| 0 <= n < reserved_words().len() implies word( + #[trigger] reserved_words()[n], + ) =~= ind_word(reserved_words()[n]) by { + lemma_word_equiv(reserved_words()[n]); + } +} + +// --------------------------------------------------------------------- +// Identifier names. +// --------------------------------------------------------------------- + +/// A `UnicodeEscapeSequence` that the deterministic reading finds lies +/// inside the source text and is not empty. +pub proof fn lemma_escape_end_bounds(s: Seq, k: int) + requires + 0 <= k, + ind_escape_end(s, k) >= 0, + ensures + k < ind_escape_end(s, k) <= s.len(), +{ + if k < s.len() && s[k] == 0x75 { + lemma_hex_run_end(s, k + 1) ; + if k + 1 < s.len() && s[k + 1] == 0x7B { + lemma_hex_run_end(s, k + 2); + } + } +} + +/// A unit that the left-to-right reading finds lies inside the source text. +pub proof fn lemma_unit_end_bounds(s: Seq, k: int, start: bool) + requires + ind_unit_end(s, k, start) >= 0, + ensures + 0 <= k < s.len(), + k < ind_unit_end(s, k, start) <= s.len(), +{ + if 0 <= k < s.len() && s[k] == 0x5C { + lemma_escape_end_bounds(s, k + 1); + } +} + +/// `IdentifierStartChar` and `IdentifierPartChar` are UAX #31's `Start` and +/// `Continue`. +pub proof fn lemma_id_char_equiv(c: u32) + ensures + is_identifier_start_char(c) <==> ind_id_start(c), + is_identifier_part_char(c) <==> ind_id_continue(c), +{ +} + +/// `IdentifierStart` and `IdentifierPart` over `s[k..e)` are the one unit the +/// left-to-right reading finds at `k`, for the class selected by `start`. +pub proof fn lemma_unit_equiv(s: Seq, k: int, e: int) + ensures + identifier_start(s, k, e) <==> (0 <= k && k < e && e <= s.len() && ind_unit_end( + s, + k, + true, + ) == e), + identifier_part(s, k, e) <==> (0 <= k && k < e && e <= s.len() && ind_unit_end( + s, + k, + false, + ) == e), +{ + lemma_unicode_escape_equiv(s, k + 1, e); + if 0 <= k && k < s.len() && s[k] == 0x5C { + if ind_escape_end(s, k + 1) >= 0 { + lemma_escape_end_bounds(s, k + 1); + } + } + if 0 <= k { + lemma_id_char_equiv(s[k]); + } +} + +/// Appending one `Continue` unit to a name read left to right. +pub proof fn lemma_name_from_append(s: Seq, i: int, k: int, j: int, first: bool) + requires + ind_name_from(s, i, k, first), + k < j, + ind_unit_end(s, k, false) == j, + ensures + ind_name_from(s, i, j, first), + decreases k - i, +{ + if i < k { + let e = ind_unit_end(s, i, first); + assert(i < e && e <= k && ind_name_from(s, e, k, false)); + lemma_name_from_append(s, e, k, j, false); + assert(ind_name_from(s, e, j, false)); + assert(i < e && e <= j); + } else { + assert(i == k && !first); + assert(ind_name_from(s, j, j, false)); + } +} + +/// Extending an `IdentifierName` by an `IdentifierPart` (§12.7 alternative +/// 2). +pub proof fn lemma_name_extend(s: Seq, i: int, m: int, m2: int) + requires + identifier_name(s, i, m), + identifier_part(s, m, m2), + ensures + identifier_name(s, i, m2), +{ + let cps = choose|cps: Seq| identifier_name_cps(s, i, m, cps); + let ext = cps.push(identifier_code_point(s, m, m2)); + assert(ext.drop_last() =~= cps); + assert(identifier_name_cps(s, i, m2, ext)) by { + assert(ext.len() >= 2); + assert(i < m < m2); + assert(identifier_name_cps(s, i, m, ext.drop_last())); + assert(ext.last() == identifier_code_point(s, m, m2)); + } +} + +/// An `IdentifierName` on `[i, m)` followed by parts read left to right up +/// to `j` is an `IdentifierName` on `[i, j)`. +pub proof fn lemma_name_then_parts(s: Seq, i: int, m: int, j: int) + requires + identifier_name(s, i, m), + ind_name_from(s, m, j, false), + ensures + identifier_name(s, i, j), + decreases j - m, +{ + if m < j { + let e = ind_unit_end(s, m, false); + assert(m < e && e <= j && ind_name_from(s, e, j, false)); + lemma_unit_end_bounds(s, m, false); + lemma_unit_equiv(s, m, e); + lemma_name_extend(s, i, m, e); + lemma_name_then_parts(s, i, e, j); + } +} + +/// Every derivation of `IdentifierName` is a start unit and then parts, as +/// the left-to-right reading has it. +pub proof fn lemma_name_cps_to_ind(s: Seq, i: int, j: int, cps: Seq) + requires + identifier_name_cps(s, i, j, cps), + ensures + ind_name_from(s, i, j, true), + decreases j - i, +{ + lemma_unit_equiv(s, i, j); + if identifier_start(s, i, j) && cps =~= seq![identifier_code_point(s, i, j)] { + assert(ind_unit_end(s, i, true) == j); + assert(ind_name_from(s, j, j, false)); + } else { + assert(cps.len() >= 2 && exists|k: int| #![auto] + i < k < j && identifier_name_cps(s, i, k, cps.drop_last()) && identifier_part(s, k, j) + && cps.last() == identifier_code_point(s, k, j)); + let k = choose|k: int| #![auto] + i < k < j && identifier_name_cps(s, i, k, cps.drop_last()) && identifier_part(s, k, j) + && cps.last() == identifier_code_point(s, k, j); + lemma_name_cps_to_ind(s, i, k, cps.drop_last()); + lemma_unit_equiv(s, k, j); + lemma_name_from_append(s, i, k, j, true); + } +} + +/// `IdentifierName` (§12.7) is UAX #31's ` *`, with an +/// escape standing for a code point. +pub proof fn lemma_identifier_name_equiv(s: Seq, i: int, j: int) + ensures + identifier_name(s, i, j) <==> ind_identifier_name(s, i, j), +{ + if identifier_name(s, i, j) { + let cps = choose|cps: Seq| identifier_name_cps(s, i, j, cps); + assert(0 <= i); + lemma_name_cps_to_ind(s, i, j, cps); + } + if ind_identifier_name(s, i, j) { + let e = ind_unit_end(s, i, true); + assert(i < e && e <= j && ind_name_from(s, e, j, false)); + lemma_unit_end_bounds(s, i, true); + lemma_unit_equiv(s, i, e); + assert(identifier_start(s, i, e)); + assert(identifier_name_cps(s, i, e, seq![identifier_code_point(s, i, e)])); + assert(identifier_name(s, i, e)); + lemma_name_then_parts(s, i, e, j); + } +} + +/// `PrivateIdentifier` (§12.7). +pub proof fn lemma_private_identifier_equiv(s: Seq, i: int, j: int) + ensures + private_identifier(s, i, j) <==> ind_private_identifier(s, i, j), +{ + lemma_identifier_name_equiv(s, i + 1, j); +} + +} // verus! diff --git a/crates/clazel-js-lexer/src/examples.rs b/crates/clazel-js-lexer/src/examples.rs index a080c8e3f..43c0f36ae 100644 --- a/crates/clazel-js-lexer/src/examples.rs +++ b/crates/clazel-js-lexer/src/examples.rs @@ -8,6 +8,10 @@ #[allow(unused_imports)] use super::spec::*; +#[cfg(verus_only)] +use super::unicode::{lemma_id_continue_gap, lemma_id_start_gap}; +#[cfg(verus_only)] +use super::unicode_data::*; #[allow(unused_imports)] use vstd::prelude::*; @@ -265,4 +269,195 @@ pub proof fn example_regexp_literal() } } + +/// §12.9.4.2 Table "String Single Character Escape Sequences": `\b` is +/// U+0008, `\t` U+0009, `\n` U+000A, `\v` U+000B, `\f` U+000C, `\r` +/// U+000D, and `\"` is `"`. +pub proof fn example_single_escapes() + ensures + single_escape_code_unit(0x62u32) == 0x0008u16, + single_escape_code_unit(0x74u32) == 0x0009u16, + single_escape_code_unit(0x6Eu32) == 0x000Au16, + single_escape_code_unit(0x76u32) == 0x000Bu16, + single_escape_code_unit(0x66u32) == 0x000Cu16, + single_escape_code_unit(0x72u32) == 0x000Du16, + single_escape_code_unit(0x22u32) == 0x0022u16, +{ +} + +/// §12.9.6.2: the TRV of `` is the one code unit ``. +pub proof fn example_trv_crlf() + ensures + line_terminator_sequence_trv(seq![0x0Du32, 0x0Au32], 0, 2) =~= seq![0x000Au16], +{ +} + +/// §12.9.5: a `RegularExpressionNonTerminator` is a `SourceCharacter` but +/// not a `LineTerminator`: `a` is one, is not. +pub proof fn example_regexp_non_terminator() + ensures + is_regexp_non_terminator(0x61u32), + !is_regexp_non_terminator(0x000Au32), +{ +} + +/// §12.9.5: what ends or does not end a regular expression's characters. +/// Backslash takes any code point but a line terminator; `]` is not a class +/// character (it closes the class) but `/` is; `/` is not a body character +/// outside a class (it closes the body); `*` does not start a body. +pub proof fn example_regexp_char_boundaries() + ensures + !regexp_backslash_sequence(seq![0x5Cu32, 0x0Au32], 0, 2), + regexp_backslash_sequence(seq![0x5Cu32, 0x2Fu32], 0, 2), + !regexp_class_char(seq![0x5Du32], 0, 1), + regexp_class_char(seq![0x2Fu32], 0, 1), + !regexp_char(seq![0x2Fu32], 0, 1), + regexp_char(seq![0x61u32], 0, 1), + !regexp_first_char(seq![0x2Au32], 0, 1), + regexp_char(seq![0x2Au32], 0, 1), +{ +} + +/// Annex B.1.1: a `/` right after the `*` of a delimited comment is not a +/// comment character (it ends the comment), nor is a line terminator; the +/// `-->` of an `HTMLCloseComment` and the ``, a multi-line +//! comment with an `HTMLCloseComment`): `scan_html_close` ensures that the +//! end it returns is the longest `HTMLCloseComment` from where it starts and +//! `None` that there is none; its split into `WhiteSpaceSequence?`, +//! `SingleLineDelimitedCommentSequence?` and `-->` is deterministic +//! ([`super::html_proofs::lemma_hc_stuck`]: the code before the `-->` is +//! white space and delimited comments, each determined by the code point +//! that starts it, and a delimited comment ends at its first `*/`). +//! `lex_slash_comment` (`//`, `/* */` and Annex B's `MultiLineComment` with +//! its optional `HTMLCloseComment`, whose `*/` is the first one, +//! [`super::html_proofs::lemma_annex_b_ml`]), `lex_lt` (a +//! `SingleLineHTMLCloseComment`), `lex_html_open` (``, `#!`). +//! - **[`ExecLexer`] implements [`super::api::JsLexer`]**, with those two +//! functions: the executable lexer meets [`super::contract`]. //! -//! Non-ASCII `ID_Start` and `ID_Continue` come from [`super::unicode`], -//! which the spec leaves uninterpreted: nothing is claimed about them. +//! What is **not** proven, and is not part of the contract: +//! +//! - **`ReservedWord`**: there is nothing to prove of it in this module. The +//! executable lexer returns an identifier's code points and its escape +//! flag ([`Value::Name`]), and whether a name is a `ReservedWord` +//! ([`super::spec::is_reserved_word`], proven equal to a closed form in +//! [`super::equivalence`]) is the parser's (§12.7.2). +//! - **The spec itself**: that [`super::spec`] is the standard (checked by +//! the independent transcription of part of it, [`super::independent`], +//! proven equal to it, the conformance suites and the certified mutation +//! kills, not by a proof), and that the generated Unicode table is the +//! Unicode property. +//! +//! [`lemma_contract_implies_proven`] shows that what [`next_input_element`] +//! used to be proven to return is implied by the full contract. +//! +//! `ID_Start` and `ID_Continue` above U+007F are [`super::unicode`]'s +//! lookups, proven to be the spec's predicates, which are the generated +//! Unicode 18.0.0 table (no axiom). That the table is the Unicode property +//! is not proven: the spec's modelling choices list the evidence. +#[cfg(verus_only)] +use super::equivalence::*; +#[cfg(verus_only)] +use super::html_proofs::*; +#[cfg(verus_only)] +use super::ident_proofs::*; +#[cfg(verus_only)] +use super::independent::*; +#[cfg(verus_only)] +use super::longest_proofs::*; +#[cfg(verus_only)] +use super::regexp_proofs::*; +#[cfg(verus_only)] +use super::scan_proofs::*; #[allow(unused_imports)] use super::spec::*; +#[cfg(verus_only)] +use super::string_proofs::*; +#[cfg(verus_only)] +use super::template_proofs::*; +#[cfg(verus_only)] +use super::trivia_proofs::*; #[allow(unused_imports)] use vstd::prelude::*; -use super::api::{Element, Next, Step, Token, Value}; +use super::api::{Element, JsLexer, Next, Step, Token, Value}; verus! { @@ -85,22 +214,57 @@ pub open spec fn value_shape_agrees(el: Element) -> bool { } } -/// For the kinds whose derivation is proven, the kind derives the -/// element's range in the goal ([`element_matches`], one conjunct of -/// [`super::contract::next_input_element_ensures`]): `WhiteSpace`, -/// `LineTerminator`, `HashbangComment`, `DivPunctuator` and -/// `RightBracePunctuator`. For the other kinds nothing is claimed here. +/// The code points of a `Name` value (empty for any other). +pub open spec fn value_name_cps(v: Value) -> Seq { + match v { + Value::Name { code_points, .. } => code_points@, + _ => Seq::empty(), + } +} + +/// The kinds whose agreement with the spec is proven (see +/// [`kind_agrees_proven`]). +pub open spec fn kind_proven(kind: ElementKind, html: bool) -> bool { + kind == ElementKind::WhiteSpace || kind == ElementKind::LineTerminator || kind + == ElementKind::HashbangComment || kind == ElementKind::DivPunctuator || kind + == ElementKind::RightBracePunctuator || kind == ElementKind::Punctuator || (kind + == ElementKind::Comment && !html) || kind == ElementKind::NumericLiteral || kind + == ElementKind::StringLiteral || kind == ElementKind::IdentifierName || kind + == ElementKind::PrivateIdentifier || kind is Template || kind + == ElementKind::RegularExpressionLiteral +} + +/// The kinds whose value is proven to be the spec's +/// ([`super::contract::element_value_agrees`]). +pub open spec fn value_proven(kind: ElementKind) -> bool { + kind == ElementKind::NumericLiteral || kind == ElementKind::StringLiteral || kind + == ElementKind::IdentifierName || kind == ElementKind::PrivateIdentifier || kind is Template + || kind == ElementKind::RegularExpressionLiteral +} + +/// For the kinds of [`value_proven`], the value is the one the standard +/// defines for the range. +pub open spec fn value_agrees_proven(src: Seq, el: Element) -> bool { + value_proven(el.kind) ==> super::contract::element_value_agrees(src, el) +} + +/// For the kinds of [`kind_proven`], the kind derives the element's range +/// in the goal ([`element_matches`], one conjunct of +/// [`super::contract::next_input_element_ensures`]), and no longer range +/// inside the source text is derived by an element of that kind. (Not that +/// no element of another kind is longer: that is [`longest_len`], which is +/// not proven.) For the other kinds nothing is claimed here. pub open spec fn kind_agrees_proven(goal: Goal, html: bool, src: Seq, el: Element) -> bool { - (el.kind == ElementKind::WhiteSpace || el.kind == ElementKind::LineTerminator || el.kind - == ElementKind::HashbangComment || el.kind == ElementKind::DivPunctuator || el.kind - == ElementKind::RightBracePunctuator) ==> element_matches( + kind_proven(el.kind, html) ==> (element_matches( goal, html, el.kind, src, el.start as int, el.end as int, - ) + ) && forall|j: int| + j <= src.len() && #[trigger] element_matches(goal, html, el.kind, src, el.start as int, j) + ==> j <= el.end) } /// What [`next_input_element`] is proven to return: totality, span @@ -115,16 +279,90 @@ pub open spec fn next_input_element_proven( r: Result, ) -> bool { match r { - Err(_) => pos < src.len(), + Err(e) => pos < src.len() && (e == LexError::NoInputElement ==> proven_bounded( + goal, + html, + src, + pos as int, + pos as int, + ) && unproven_bounded(goal, html, src, pos as int, pos as int + 1)) && (e + == LexError::IdentifierAfterNumericLiteral ==> exists|end: int| + pos < end <= src.len() && #[trigger] element_matches( + goal, + html, + ElementKind::NumericLiteral, + src, + pos as int, + end, + ) && proven_bounded(goal, html, src, pos as int, end) && unproven_bounded( + goal, + html, + src, + pos as int, + end + 1, + ) && numeric_literal_follow_error(src, end)) && (e + == LexError::InvalidIdentifierEscape ==> exists|end: int| + pos < end <= src.len() && proven_bounded(goal, html, src, pos as int, end) + && unproven_bounded(goal, html, src, pos as int, end + 1) && numeric_absent( + src, + pos as int, + ) && #[trigger] invalid_identifier_at(goal, html, src, pos as int, end)), Ok(Step::Eof) => pos == src.len(), Ok(Step::Element(el)) => pos < src.len() && el.start == pos && pos < el.end && el.end - <= src.len() && value_shape_agrees(el) && kind_agrees_proven(goal, html, src, el), + <= src.len() && value_shape_agrees(el) && kind_agrees_proven(goal, html, src, el) + && value_agrees_proven(src, el) && (el.kind == ElementKind::Comment ==> element_matches( + goal, + html, + ElementKind::Comment, + src, + pos as int, + el.end as int, + )) && proven_bounded(goal, html, src, pos as int, el.end as int) && unproven_bounded( + goal, + html, + src, + pos as int, + el.end as int + 1, + ) && (el.kind != ElementKind::NumericLiteral ==> numeric_absent(src, pos as int)) && ( + el.kind == ElementKind::NumericLiteral ==> !numeric_literal_follow_error( + src, + el.end as int, + )) && (el.kind != ElementKind::IdentifierName ==> ident_absent(src, pos as int)) && ( + el.kind != ElementKind::PrivateIdentifier ==> private_absent(src, pos as int)) && (el.kind + == ElementKind::IdentifierName ==> identifier_name_valid( + src, + pos as int, + el.end as int, + )) && (el.kind == ElementKind::PrivateIdentifier ==> identifier_name_valid( + src, + pos as int + 1, + el.end as int, + )), } } +/// An identifier or private identifier over `pos..end` with an early error +/// (§12.7.1.1), and no other kind of that error possible at `pos`. +pub open spec fn invalid_identifier_at( + goal: Goal, + html: bool, + src: Seq, + pos: int, + end: int, +) -> bool { + (element_matches(goal, html, ElementKind::IdentifierName, src, pos, end) + && !identifier_name_valid(src, pos, end) && private_absent(src, pos)) + || (element_matches(goal, html, ElementKind::PrivateIdentifier, src, pos, end) + && !identifier_name_valid(src, pos + 1, end) && ident_absent(src, pos)) +} + /// What [`next_token`] is proven to return: the token is after `pos`, -/// inside the source text and not trivia, and the flag is set only by a -/// line terminator code point between `pos` and the token. +/// inside the source text and not trivia, and the flag is exactly whether a +/// line terminator code point is between `pos` and the token (which is +/// [`skip_trivia`]'s flag for the trivia that end there, by +/// [`super::lemmas::lemma_skip_trivia_flag`]). That the trivia end where +/// [`skip_trivia`] says is part of [`next_token`]'s contract +/// ([`super::contract::next_token_ensures`]), which it ensures as well. pub open spec fn next_token_proven( src: Seq, pos: usize, @@ -133,24 +371,40 @@ pub open spec fn next_token_proven( r: Result, ) -> bool { match r { - Err(_) => pos < src.len(), - Ok(Next::Eof { newline_before }) => newline_before ==> contains_line_terminator( + Err(e) => pos < src.len(), + Ok(Next::Eof { newline_before }) => newline_before == contains_line_terminator( src, pos as int, src.len() as int, ), Ok(Next::Token(tok)) => pos <= tok.element.start < tok.element.end <= src.len() && !is_trivia(tok.element.kind) && value_shape_agrees(tok.element) - && kind_agrees_proven(goal, html, src, tok.element) && (tok.newline_before - ==> contains_line_terminator(src, pos as int, tok.element.start as int)), + && kind_agrees_proven(goal, html, src, tok.element) && value_agrees_proven( + src, + tok.element, + ) && (kind_proven(tok.element.kind, html) ==> proven_bounded( + goal, + html, + src, + tok.element.start as int, + tok.element.end as int, + )) && tok.newline_before == contains_line_terminator( + src, + pos as int, + tok.element.start as int, + ), } } /// The full contract implies what is proven of [`next_input_element`] -/// (except the value's shape, which the full contract states value by -/// value): the proven part is a weakening, not a different claim. -/// ([`kind_agrees_proven`] is the contract's `element_matches` conjunct -/// itself, restricted to some kinds.) +/// about the spec (except the value's shape, which the full contract states +/// value by value, and the facts about the dispatch that +/// [`next_input_element_proven`] also carries to prove the above: +/// `numeric_absent` and the numeric follow check): the proven part is a +/// weakening, not a different claim. ([`kind_agrees_proven`] is the +/// contract's `element_matches` conjunct itself, restricted to some kinds; +/// [`proven_bounded`] is what the contract's longest length says of the +/// proven kinds.) pub proof fn lemma_contract_implies_proven( src: Seq, pos: usize, @@ -163,12 +417,273 @@ pub proof fn lemma_contract_implies_proven( super::contract::next_input_element_ensures(src, pos, goal, html, r), ensures match r { - Err(_) => pos < src.len(), + Err(e) => pos < src.len() && (e == LexError::NoInputElement ==> proven_bounded( + goal, + html, + src, + pos as int, + pos as int, + )), Ok(Step::Eof) => pos == src.len(), Ok(Step::Element(el)) => pos < src.len() && el.start == pos && pos < el.end && el.end - <= src.len() && kind_agrees_proven(goal, html, src, el), + <= src.len() && kind_agrees_proven(goal, html, src, el) && value_agrees_proven( + src, + el, + ) && (kind_proven(el.kind, html) ==> proven_bounded( + goal, + html, + src, + pos as int, + el.end as int, + )), }, { + super::lemmas::lemma_longest_len(goal, html, src, pos as int); + if let Err(LexError::NoInputElement) = r { + // The longest length is 0: no range, so no kind, derives anything. + assert forall|k: ElementKind, j: int| + pos < j <= src.len() && #[trigger] element_matches(goal, html, k, src, pos as int, j) + implies j <= pos by { + super::lemmas::lemma_kind_implies_any(goal, html, k, src, pos as int, j); + let n = j - pos as int; + assert(pos as int + n == j); + assert(any_element_matches(goal, html, src, pos as int, pos as int + n)); + } + } + if let Ok(Step::Element(el)) = r { + assert forall|k: ElementKind, j: int| + pos < j <= src.len() && #[trigger] element_matches(goal, html, k, src, pos as int, j) + implies j <= el.end by { + super::lemmas::lemma_kind_implies_any(goal, html, k, src, pos as int, j); + let n = j - pos as int; + assert(pos as int + n == j); + assert(any_element_matches(goal, html, src, pos as int, pos as int + n)); + } + assert forall|j: int| + j <= src.len() && #[trigger] element_matches( + goal, + html, + el.kind, + src, + el.start as int, + j, + ) implies j <= el.end by { + super::lemmas::lemma_kind_implies_any(goal, html, el.kind, src, pos as int, j); + super::lemmas::lemma_longest_len(goal, html, src, pos as int); + let n = j - pos as int; + assert(pos as int + n == j); + assert(any_element_matches(goal, html, src, pos as int, pos as int + n)); + } + } +} + +/// The full contract for an element that [`next_input_element`] returns: +/// the longest length (from [`proven_bounded`], and [`unproven_bounded`], +/// which in `html` mode is the bound on Annex B.1.1's `Comment`), the errors +/// of [`lex_error`], and the value. +pub proof fn lemma_element_meets_contract( + src: Seq, + pos: usize, + goal: Goal, + html: bool, + el: Element, +) + requires + next_input_element_proven(src, pos, goal, html, Ok(Step::Element(el))), + ensures + super::contract::next_input_element_ensures(src, pos, goal, html, Ok(Step::Element(el))), +{ + let i = pos as int; + let e = el.end as int; + assert(element_matches(goal, html, el.kind, src, i, e)) by { + if !kind_proven(el.kind, html) { + lemma_unproven_kind(el.kind, html); + } + } + super::lemmas::lemma_kind_implies_any(goal, html, el.kind, src, i, e); + lemma_longest_from_bounds(goal, html, src, i, e); + let n = longest_len(goal, html, src, i); + assert(n == e - i); + // No error: the numeric literal rule, and the identifier rules, do not fire. + assert(!(element_matches(goal, html, ElementKind::NumericLiteral, src, i, i + n) + && numeric_literal_follow_error(src, i + n))) by { + if el.kind == ElementKind::NumericLiteral { + assert(!numeric_literal_follow_error(src, e)); + } else { + reveal(numeric_absent); + assert(numeric_absent(src, i)); + } + } + assert(!(element_matches(goal, html, ElementKind::IdentifierName, src, i, i + n) + && !identifier_name_valid(src, i, i + n))) by { + if el.kind != ElementKind::IdentifierName { + reveal(ident_absent); + assert(ident_absent(src, i)); + } + } + assert(!(element_matches(goal, html, ElementKind::PrivateIdentifier, src, i, i + n) + && !identifier_name_valid(src, i + 1, i + n))) by { + if el.kind != ElementKind::PrivateIdentifier { + reveal(private_absent); + assert(private_absent(src, i)); + } + } + assert(lex_error(goal, html, src, i) is None); + // The value is the standard's: for the kinds whose value is proven, as + // proven; for the others there is none. + assert(super::contract::element_value_agrees(src, el)) by { + if value_proven(el.kind) { + assert(value_agrees_proven(src, el)); + } else { + assert(value_shape_agrees(el)); + } + } +} + +/// The contract for [`LexError::NoInputElement`]. +pub proof fn lemma_no_input_element_meets_contract( + src: Seq, + pos: usize, + goal: Goal, + html: bool, +) + requires + next_input_element_proven(src, pos, goal, html, Err(LexError::NoInputElement)), + ensures + super::contract::next_input_element_ensures( + src, + pos, + goal, + html, + Err(LexError::NoInputElement), + ), +{ + lemma_longest_zero(goal, html, src, pos as int); +} + +/// The contract for [`LexError::IdentifierAfterNumericLiteral`]. +pub proof fn lemma_numeric_error_meets_contract( + src: Seq, + pos: usize, + goal: Goal, + html: bool, + end: int, +) + requires + pos < end <= src.len(), + element_matches(goal, html, ElementKind::NumericLiteral, src, pos as int, end), + proven_bounded(goal, html, src, pos as int, end), + numeric_literal_follow_error(src, end), + unproven_bounded(goal, html, src, pos as int, end + 1), + ensures + super::contract::next_input_element_ensures( + src, + pos, + goal, + html, + Err(LexError::IdentifierAfterNumericLiteral), + ), +{ + super::lemmas::lemma_kind_implies_any( + goal, + html, + ElementKind::NumericLiteral, + src, + pos as int, + end, + ); + lemma_longest_from_bounds(goal, html, src, pos as int, end); +} + +/// The contract for [`LexError::InvalidIdentifierEscape`]. +pub proof fn lemma_invalid_identifier_meets_contract( + src: Seq, + pos: usize, + goal: Goal, + html: bool, + end: int, +) + requires + pos < end <= src.len(), + proven_bounded(goal, html, src, pos as int, end), + numeric_absent(src, pos as int), + invalid_identifier_at(goal, html, src, pos as int, end), + unproven_bounded(goal, html, src, pos as int, end + 1), + ensures + super::contract::next_input_element_ensures( + src, + pos, + goal, + html, + Err(LexError::InvalidIdentifierEscape), + ), +{ + let i = pos as int; + let kind = if element_matches(goal, html, ElementKind::IdentifierName, src, i, end) { + ElementKind::IdentifierName + } else { + ElementKind::PrivateIdentifier + }; + super::lemmas::lemma_kind_implies_any(goal, html, kind, src, i, end); + lemma_longest_from_bounds(goal, html, src, i, end); + let n = longest_len(goal, html, src, i); + assert(n == end - i); + reveal(numeric_absent); + assert(!element_matches(goal, html, ElementKind::NumericLiteral, src, i, i + n)); +} + +/// What [`next_input_element`] is proven to return meets the contract: the +/// whole of [`super::contract::next_input_element_ensures`], for every +/// source text, position, goal and `html`. +pub proof fn lemma_next_input_element_meets_contract( + src: Seq, + pos: usize, + goal: Goal, + html: bool, + r: Result, +) + requires + pos <= src.len(), + next_input_element_proven(src, pos, goal, html, r), + ensures + super::contract::next_input_element_ensures(src, pos, goal, html, r), +{ + match r { + Ok(Step::Eof) => {}, + Ok(Step::Element(el)) => { + lemma_element_meets_contract(src, pos, goal, html, el); + }, + Err(LexError::NoInputElement) => { + lemma_no_input_element_meets_contract(src, pos, goal, html); + }, + Err(LexError::IdentifierAfterNumericLiteral) => { + let end = choose|end: int| + pos < end <= src.len() && #[trigger] element_matches( + goal, + html, + ElementKind::NumericLiteral, + src, + pos as int, + end, + ) && proven_bounded(goal, html, src, pos as int, end) && unproven_bounded( + goal, + html, + src, + pos as int, + end + 1, + ) && numeric_literal_follow_error(src, end); + lemma_numeric_error_meets_contract(src, pos, goal, html, end); + }, + Err(LexError::InvalidIdentifierEscape) => { + let end = choose|end: int| + pos < end <= src.len() && proven_bounded(goal, html, src, pos as int, end) + && unproven_bounded(goal, html, src, pos as int, end + 1) && numeric_absent( + src, + pos as int, + ) && #[trigger] invalid_identifier_at(goal, html, src, pos as int, end); + lemma_invalid_identifier_meets_contract(src, pos, goal, html, end); + }, + } } // --------------------------------------------------------------------- @@ -295,10 +810,10 @@ fn hex_val(c: u32) -> (r: u32) } /// §12.7 `IdentifierStartChar :: UnicodeIDStart | $ | _`. Above U+007F, -/// [`super::unicode`]'s table, about which nothing is claimed. +/// [`super::unicode`]'s table, which the spec's predicate is. fn id_start_char(c: u32) -> (r: bool) ensures - c < 0x80 ==> r == is_identifier_start_char(c), + r == is_identifier_start_char(c), r ==> c <= 0x10FFFF, r ==> c != 0x5C, { @@ -312,7 +827,7 @@ fn id_start_char(c: u32) -> (r: bool) /// §12.7 `IdentifierPartChar :: UnicodeIDContinue | $`. fn id_part_char(c: u32) -> (r: bool) ensures - c < 0x80 ==> r == is_identifier_part_char(c), + r == is_identifier_part_char(c), r ==> c <= 0x10FFFF, r ==> c != 0x5C, { @@ -353,14 +868,22 @@ fn push_utf16(out: &mut Vec, cp: u32) fn push_range_utf16(out: &mut Vec, src: &[u32], i: usize, j: usize) requires i <= j <= src.len(), + forall|t: int| i <= t < j ==> src@[t] <= 0x10FFFF, + ensures + final(out)@ =~= old(out)@ + utf16_of(src@, i as int, j as int), { let mut k = i; while k < j invariant i <= k <= j <= src.len(), + forall|t: int| i <= t < j ==> src@[t] <= 0x10FFFF, + out@ =~= old(out)@ + utf16_of(src@, i as int, k as int), decreases j - k, { let c = peek(src, k); + proof { + lemma_utf16_of_push(src@, i as int, k as int); + } if c <= 0x10FFFF { push_utf16(out, c); } @@ -398,18 +921,35 @@ fn scan_line_chars(src: &[u32], k: usize) -> (r: usize) } } +/// The `*/` that ends at `e` closes a block comment whose body starts at +/// `k`: it is the first `*/` at or after `k`, and every code point before +/// it is a `SourceCharacter` (and, if `!multi_line`, not a +/// `LineTerminator`). +pub open spec fn block_close_ok(s: Seq, k: int, e: int, multi_line: bool) -> bool { + k + 2 <= e && e <= s.len() && s[e - 2] == 0x2A && s[e - 1] == 0x2F && forall|m: int| + k <= m < e - 2 ==> (#[trigger] s[m] <= 0x10FFFF && !(s[m] == 0x2A && s[m + 1] == 0x2F) && ( + multi_line || !is_line_terminator(s[m]))) +} + /// §12.4 `MultiLineComment` (or, with `!multi_line`, Annex B's /// `SingleLineDelimitedComment`) after its `/*` at `k`: the end of the /// first `*/` and whether a `LineTerminator` came before it. `None` if the /// source text ends, or a code point that is not a `SourceCharacter` (or, /// with `!multi_line`, a `LineTerminator`) comes, first. +/// +/// Proven: `Some((e, lt))` is the one end [`block_close_ok`] allows, and +/// `lt` is [`contains_line_terminator`] of the body; `None` means no end +/// is allowed. fn scan_block_comment(src: &[u32], k: usize, multi_line: bool) -> (r: Option<(usize, bool)>) requires k <= src.len(), ensures match r { - Some((e, _)) => k + 2 <= e <= src.len(), - None => true, + Some((e, lt)) => block_close_ok(src@, k as int, e as int, multi_line) + && lt == contains_line_terminator(src@, k as int, e as int - 2) && (forall| + e2: int, + | block_close_ok(src@, k as int, e2, multi_line) ==> e2 == e), + None => forall|e2: int| !block_close_ok(src@, k as int, e2, multi_line), }, { let mut j = k; @@ -417,33 +957,79 @@ fn scan_block_comment(src: &[u32], k: usize, multi_line: bool) -> (r: Option<(us loop invariant k <= j <= src.len(), + lt == contains_line_terminator(src@, k as int, j as int), + forall|m: int| + k <= m < j ==> (#[trigger] src@[m] <= 0x10FFFF && !(m + 1 < src.len() + && src@[m] == 0x2A && src@[m + 1] == 0x2F) && (multi_line + || !is_line_terminator(src@[m]))), decreases src.len() - j, { let c = peek(src, j); if c == 0x2A && peek(src, j.saturating_add(1)) == 0x2F { + proof { + let e = j + 2; + assert(block_close_ok(src@, k as int, e, multi_line)); + assert forall|e2: int| block_close_ok(src@, k as int, e2, multi_line) implies e2 + == e by { + if e2 < e { + assert(src@[e2 - 2] == 0x2A && src@[e2 - 1] == 0x2F); + assert(k <= e2 - 2 < j); + } else if e2 > e { + assert(k <= j < e2 - 2); + } + } + } return Some((j.saturating_add(2), lt)); } if !is_src(c) || (!multi_line && is_lt(c)) { + proof { + assert forall|e2: int| !block_close_ok(src@, k as int, e2, multi_line) by { + if block_close_ok(src@, k as int, e2, multi_line) { + if e2 - 2 < j { + assert(k <= e2 - 2 < j); + } else if e2 - 2 == j { + assert(src@[j as int] == 0x2A); + } else { + assert(k <= j < e2 - 2); + } + } + } + } return None; } if is_lt(c) { lt = true; } + proof { + assert(contains_line_terminator(src@, k as int, (j + 1) as int) == (lt || false) || true); + assert(lt == contains_line_terminator(src@, k as int, (j + 1) as int)) by { + if is_line_terminator(src@[j as int]) { + assert(contains_line_terminator(src@, k as int, (j + 1) as int)); + } else if contains_line_terminator(src@, k as int, (j + 1) as int) { + let m = choose|m: int| k <= m < j + 1 && is_line_terminator(src@[m]); + assert(m < j); + } + } + } j = j.saturating_add(1); } } -/// Annex B.1.1 `WhiteSpaceSequence?` from `k`. +/// Annex B.1.1 `WhiteSpaceSequence?` from `k`: the first index at or after +/// `k` that is not white space. fn scan_ws(src: &[u32], k: usize) -> (r: usize) requires k <= src.len(), ensures k <= r <= src.len(), + forall|m: int| k <= m < r ==> #[trigger] white_space(src@, m, m + 1), + r < src.len() ==> !white_space(src@, r as int, r as int + 1), { let mut j = k; loop invariant k <= j <= src.len(), + forall|m: int| k <= m < j ==> #[trigger] white_space(src@, m, m + 1), decreases src.len() - j, { if is_ws(peek(src, j)) { @@ -454,21 +1040,35 @@ fn scan_ws(src: &[u32], k: usize) -> (r: usize) } } -/// Annex B.1.1 the end of `-->` SingleLineCommentChars? at `k`. +/// Annex B.1.1 the end of `-->` SingleLineCommentChars? at `k`: if `-->` is +/// there, the end of the line (the first index at or after the `-->` that is +/// not a `SingleLineCommentChar`). fn scan_html_arrow(src: &[u32], k: usize) -> (r: Option) requires k <= src.len(), ensures match r { - Some(e) => k + 3 <= e <= src.len(), - None => true, + Some(e) => arrow_at(src@, k as int) && k + 3 <= e <= src.len() && no_lt_src( + src@, + k as int + 3, + e as int, + ) && (e < src.len() ==> !is_not_line_terminator(src@[e as int])), + None => !arrow_at(src@, k as int), }, { if peek(src, k) == 0x2D && peek(src, k.saturating_add(1)) == 0x2D && peek( src, k.saturating_add(2), ) == 0x3E { - Some(scan_line_chars(src, k.saturating_add(3))) + let e = scan_line_chars(src, k.saturating_add(3)); + proof { + assert(no_lt_src(src@, k as int + 3, e as int)) by { + assert forall|m: int| k as int + 3 <= m < e implies is_not_line_terminator( + #[trigger] src@[m], + ) by {} + } + } + Some(e) } else { None } @@ -479,33 +1079,115 @@ fn scan_html_arrow(src: &[u32], k: usize) -> (r: Option) /// Each part is determined by the code point that starts it (white space, /// `/*` or `-->`), so the parse is the only one, and it ends where the /// line does. +/// +/// Proven: `Some(e)` is the longest range `s[k..e)` that `HTMLCloseComment` +/// derives, and `None` means it derives none. fn scan_html_close(src: &[u32], k: usize) -> (r: Option) requires k <= src.len(), ensures match r { - Some(e) => k + 3 <= e <= src.len(), - None => true, + Some(e) => html_close_comment(src@, k as int, e as int) && forall|j: int| + html_close_comment(src@, k as int, j) ==> j <= e, + None => forall|j: int| !html_close_comment(src@, k as int, j), }, { let mut j = scan_ws(src, k); + proof { + lemma_hc_ws_run(src@, k as int, j as int); + } loop invariant k <= j <= src.len(), + hc_pre(src@, k as int, j as int), + j < src.len() ==> !white_space(src@, j as int, j as int + 1), decreases src.len() - j, { if peek(src, j) == 0x2F && peek(src, j.saturating_add(1)) == 0x2A { // SingleLineDelimitedComment WhiteSpaceSequence? match scan_block_comment(src, j.saturating_add(2), false) { Some((e, _)) => { - j = scan_ws(src, e); + proof { + lemma_sldc_equiv(src@, j as int, e as int); + lemma_hc_sldc(src@, j as int, e as int); + lemma_hc_cat(src@, k as int, j as int, e as int); + } + let j2 = scan_ws(src, e); + proof { + lemma_hc_ws_run(src@, e as int, j2 as int); + lemma_hc_cat(src@, k as int, e as int, j2 as int); + } + j = j2; }, None => { + proof { + // No delimited comment starts at `j`, so the `-->` of any + // derivation would have to be at `j`, where `/` is. + assert forall|a: int| !single_line_delimited_comment(src@, j as int, a) by { + lemma_sldc_equiv(src@, j as int, a); + } + assert forall|j2: int| !html_close_comment(src@, k as int, j2) by { + lemma_html_close_equiv(src@, k as int, j2); + if html_close_comment(src@, k as int, j2) { + let b = choose|b: int| + #![trigger arrow_at(src@, b)] + hc_pre(src@, k as int, b) && arrow_at(src@, b) && hc_tail( + src@, + b, + j2, + ); + lemma_hc_stuck(src@, k as int, j as int, b); + } + } + } return None; }, } } else { - return scan_html_arrow(src, j); + let r = scan_html_arrow(src, j); + proof { + assert forall|a: int| !single_line_delimited_comment(src@, j as int, a) by { + lemma_sldc_equiv(src@, j as int, a); + } + match r { + Some(e) => { + assert(hc_tail(src@, j as int, e as int)); + lemma_html_close_equiv(src@, k as int, e as int); + assert forall|j2: int| html_close_comment(src@, k as int, j2) implies j2 + <= e by { + lemma_html_close_equiv(src@, k as int, j2); + let b = choose|b: int| + #![trigger arrow_at(src@, b)] + hc_pre(src@, k as int, b) && arrow_at(src@, b) && hc_tail( + src@, + b, + j2, + ); + lemma_hc_stuck(src@, k as int, j as int, b); + if j2 > e { + assert(no_lt_src(src@, j as int + 3, j2 as int)); + assert(is_not_line_terminator(src@[e as int])); + } + } + }, + None => { + assert forall|j2: int| !html_close_comment(src@, k as int, j2) by { + lemma_html_close_equiv(src@, k as int, j2); + if html_close_comment(src@, k as int, j2) { + let b = choose|b: int| + #![trigger arrow_at(src@, b)] + hc_pre(src@, k as int, b) && arrow_at(src@, b) && hc_tail( + src@, + b, + j2, + ); + lemma_hc_stuck(src@, k as int, j as int, b); + } + } + }, + } + } + return r; } } } @@ -517,6 +1199,8 @@ fn line_terminator_sequence_end(src: &[u32], k: usize) -> (r: usize) is_line_terminator(src@[k as int]), ensures k < r <= src.len(), + line_terminator_sequence(src@, k as int, r as int), + forall|r2: int| line_terminator_sequence(src@, k as int, r2) ==> r2 == r, { if peek(src, k) == 0x0D && peek(src, k.saturating_add(1)) == 0x0A { k.saturating_add(2) @@ -531,12 +1215,20 @@ fn line_terminator_sequence_end(src: &[u32], k: usize) -> (r: usize) /// The MV of the `HexDigits[~Sep]` that start at `k` (all of them), capped /// at 0x110000 (any value above U+10FFFF is `NotCodePoint`), and the end. +/// +/// Proven: the end is where the run of hex digits ends, and the value is +/// the spec's [`hex_digits_mv`] of the run when that is at most U+10FFFF +/// (and above U+10FFFF otherwise). fn scan_hex_digits(src: &[u32], k: usize) -> (r: (u32, usize)) requires k <= src.len(), ensures r.0 <= 0x110000, k <= r.1 <= src.len(), + r.1 as int == ind_hex_run_end(src@, k as int), + r.0 <= 0x10FFFF ==> r.0 as int == hex_digits_mv(src@, k as int, r.1 as int), + r.0 > 0x10FFFF ==> hex_digits_mv(src@, k as int, r.1 as int) > 0x10FFFF, + forall|t: int| k <= t < r.1 ==> is_hex_digit(#[trigger] src@[t]), { let mut j = k; let mut v: u32 = 0; @@ -544,11 +1236,19 @@ fn scan_hex_digits(src: &[u32], k: usize) -> (r: (u32, usize)) invariant k <= j <= src.len(), v <= 0x110000, + ind_hex_run_end(src@, k as int) == ind_hex_run_end(src@, j as int), + forall|t: int| k <= t < j ==> is_hex_digit(#[trigger] src@[t]), + v <= 0x10FFFF ==> v as int == hex_digits_mv(src@, k as int, j as int), + v > 0x10FFFF ==> hex_digits_mv(src@, k as int, j as int) > 0x10FFFF, decreases src.len() - j, { let c = peek(src, j); if is_hex(c) { let d = hex_val(c); + proof { + reveal_with_fuel(hex_digits_mv, 2); + assert(ind_hex_run_end(src@, j as int) == ind_hex_run_end(src@, j as int + 1)); + } if v <= 0x10FFFF { let w = v.saturating_mul(16).saturating_add(d); v = if w > 0x10FFFF { @@ -565,25 +1265,48 @@ fn scan_hex_digits(src: &[u32], k: usize) -> (r: (u32, usize)) } /// §12.9.4 `Hex4Digits` at `k`: its MV. +/// +/// Proven: `Some(v)` when four hex digits start at `k`, and then `v` is +/// their [`hex_digits_mv`]; `None` when they do not. fn scan_hex4(src: &[u32], k: usize) -> (r: Option) requires k <= src.len(), ensures match r { - Some(v) => k + 4 <= src.len() && v <= 0xFFFF, - None => true, + Some(v) => k + 4 <= src.len() && v <= 0xFFFF && ind_hex_run_end(src@, k as int) >= k + + 4 && v as int == hex_digits_mv(src@, k as int, k as int + 4), + None => ind_hex_run_end(src@, k as int) < k + 4, }, { let a = peek(src, k); let b = peek(src, k.saturating_add(1)); let c = peek(src, k.saturating_add(2)); let d = peek(src, k.saturating_add(3)); + proof { + lemma_hex_run_end(src@, k as int); + } if is_hex(a) && is_hex(b) && is_hex(c) && is_hex(d) { let v = hex_val(a).saturating_mul(0x1000).saturating_add( hex_val(b).saturating_mul(0x100), ).saturating_add(hex_val(c).saturating_mul(0x10)).saturating_add(hex_val(d)); + proof { + reveal_with_fuel(hex_digits_mv, 5); + let end = ind_hex_run_end(src@, k as int); + if end < k + 4 { + assert(ind_hex(src@[end])); + } + } Some(v) } else { + proof { + let end = ind_hex_run_end(src@, k as int); + if end >= k + 4 { + assert(ind_hex(src@[k as int])); + assert(ind_hex(src@[k as int + 1])); + assert(ind_hex(src@[k as int + 2])); + assert(ind_hex(src@[k as int + 3])); + } + } None } } @@ -591,13 +1314,22 @@ fn scan_hex4(src: &[u32], k: usize) -> (r: Option) /// §12.9.4 `UnicodeEscapeSequence :: u Hex4Digits | u{ CodePoint }` at `k` /// (after the backslash): its code point (§12.7.1.3 IdentifierCodePoint) /// and its end. +/// +/// Proven: `Some((cp, e))` is the one `UnicodeEscapeSequence` the spec +/// derives at `k` (the deterministic reading [`ind_escape_end`] of +/// `independent.rs`, which `equivalence.rs` proves equal to the spec's) and +/// `cp` is its code point; `None` means the spec derives none. fn scan_unicode_escape(src: &[u32], k: usize) -> (r: Option<(u32, usize)>) requires k <= src.len(), ensures match r { - Some((cp, e)) => k < e <= src.len() && cp <= 0x10FFFF, - None => true, + Some((cp, e)) => k < e <= src.len() && cp <= 0x10FFFF && ind_escape_end( + src@, + k as int, + ) == e as int && cp as int == unicode_escape_code_point(src@, k as int, e as int) && (( + k + 1 < src.len() && src@[k as int + 1] != 0x7B) ==> cp <= 0xFFFF), + None => ind_escape_end(src@, k as int) == -1, }, { if peek(src, k) != 0x75 { @@ -607,8 +1339,16 @@ fn scan_unicode_escape(src: &[u32], k: usize) -> (r: Option<(u32, usize)>) // 2. `u{` CodePoint `}`, with CodePoint's MV at most 0x10FFFF. let (v, e) = scan_hex_digits(src, k.saturating_add(2)); if e > k.saturating_add(2) && v <= 0x10FFFF && peek(src, e) == 0x7D { + proof { + lemma_hex_value_equiv(src@, k as int + 2, e as int); + } Some((v, e.saturating_add(1))) } else { + proof { + if v > 0x10FFFF { + lemma_hex_value_equiv(src@, k as int + 2, e as int); + } + } None } } else { @@ -622,13 +1362,31 @@ fn scan_unicode_escape(src: &[u32], k: usize) -> (r: Option<(u32, usize)>) /// determined by its first code point, so the longest derivation is the /// greedy one): IdentifierCodePoints (§12.7.1.2), whether an escape was /// used, whether the §12.7.1.1 early errors pass, and the end. +/// +/// Proven: `Some` is the longest `IdentifierName` the spec derives at `k` +/// (it is read left to right as [`super::independent`]'s units, so no +/// derivation is longer: [`name_end`]), the code points are the spec's +/// IdentifierCodePoints of it, `escaped` is whether it has an escape, and +/// `valid` is whether it has no early error; `None` means the spec derives +/// none. fn scan_identifier(src: &[u32], k: usize) -> (r: Option<(Vec, bool, bool, usize)>) requires k <= src.len(), ensures match r { - Some((_, _, _, e)) => k < e <= src.len(), - None => true, + Some((cps, escaped, valid, e)) => k < e <= src.len() && ind_name_from( + src@, + k as int, + e as int, + true, + ) && name_end(src@, k as int, true) == e as int && identifier_name_cps( + src@, + k as int, + e as int, + cps@, + ) && escaped == identifier_has_escape(src@, k as int, e as int) && valid + == identifier_early_errors_ok(cps@), + None => ind_unit_end(src@, k as int, true) < 0, }, { let mut cps: Vec = Vec::new(); @@ -643,6 +1401,19 @@ fn scan_identifier(src: &[u32], k: usize) -> (r: Option<(Vec, bool, bool, u // lexical grammar production." match scan_unicode_escape(src, k.saturating_add(1)) { Some((cp, e)) => { + proof { + let s = src@; + assert(s[k as int] == 0x5C); + assert(ind_unit_end(s, k as int, true) == e as int); + lemma_unit_equiv(s, k as int, e as int); + assert(identifier_start(s, k as int, e as int)); + assert(identifier_code_point(s, k as int, e as int) == cp); + lemma_cps_start(s, k as int, e as int); + assert(ind_name_from(s, e as int, e as int, false)); + lemma_name_end_step(s, k as int, e as int, true); + lemma_early_one(cp); + lemma_has_escape_esc(s, k as int, k as int, e as int); + } cps.push(cp); escaped = true; valid = id_start_char(cp); @@ -654,14 +1425,38 @@ fn scan_identifier(src: &[u32], k: usize) -> (r: Option<(Vec, bool, bool, u } } else if id_start_char(c) { // IdentifierStart :: IdentifierStartChar + proof { + let s = src@; + assert(k < s.len() && c == s[k as int]); + lemma_id_char_equiv(c); + assert(ind_unit_end(s, k as int, true) == k as int + 1); + lemma_unit_equiv(s, k as int, k as int + 1); + assert(identifier_start(s, k as int, k as int + 1)); + lemma_cps_start(s, k as int, k as int + 1); + assert(ind_name_from(s, k as int + 1, k as int + 1, false)); + lemma_name_end_step(s, k as int, k as int + 1, true); + lemma_early_one(c); + lemma_has_escape_plain(s, k as int, k as int); + } cps.push(c); j = k.saturating_add(1); } else { + proof { + let s = src@; + if k < s.len() { + lemma_id_char_equiv(s[k as int]); + } + } return None; } loop invariant k < j <= src.len(), + ind_name_from(src@, k as int, j as int, true), + name_end(src@, k as int, true) == name_end(src@, j as int, false), + identifier_name_cps(src@, k as int, j as int, cps@), + escaped == identifier_has_escape(src@, k as int, j as int), + valid == identifier_early_errors_ok(cps@), decreases src.len() - j, { let c = peek(src, j); @@ -670,6 +1465,20 @@ fn scan_identifier(src: &[u32], k: usize) -> (r: Option<(Vec, bool, bool, u // rule for IdentifierPartChar. match scan_unicode_escape(src, j.saturating_add(1)) { Some((cp, e)) => { + proof { + let s = src@; + assert(s[j as int] == 0x5C); + assert(ind_unit_end(s, j as int, false) == e as int); + lemma_unit_equiv(s, j as int, e as int); + assert(identifier_part(s, j as int, e as int)); + assert(identifier_code_point(s, j as int, e as int) == cp); + lemma_cps_extend(s, k as int, j as int, e as int, cps@); + lemma_name_from_append(s, k as int, j as int, e as int, true); + lemma_name_end_step(s, j as int, e as int, false); + lemma_cps_nonempty(s, k as int, j as int, cps@); + lemma_early_push(cps@, cp); + lemma_has_escape_esc(s, k as int, j as int, e as int); + } cps.push(cp); escaped = true; if !id_part_char(cp) { @@ -678,14 +1487,45 @@ fn scan_identifier(src: &[u32], k: usize) -> (r: Option<(Vec, bool, bool, u j = e; }, None => { + proof { + let s = src@; + assert(s[j as int] == 0x5C); + assert(ind_unit_end(s, j as int, false) < 0); + lemma_name_end_stop(s, j as int); + lemma_name_end_derives(s, k as int, true); + } return Some((cps, escaped, valid, j)); }, } } else if id_part_char(c) { // IdentifierPart :: IdentifierPartChar + proof { + let s = src@; + assert(j < s.len() && c == s[j as int]); + lemma_id_char_equiv(c); + assert(ind_unit_end(s, j as int, false) == j as int + 1); + lemma_unit_equiv(s, j as int, j as int + 1); + assert(identifier_part(s, j as int, j as int + 1)); + assert(identifier_code_point(s, j as int, j as int + 1) == c); + lemma_cps_extend(s, k as int, j as int, j as int + 1, cps@); + lemma_name_from_append(s, k as int, j as int, j as int + 1, true); + lemma_name_end_step(s, j as int, j as int + 1, false); + lemma_cps_nonempty(s, k as int, j as int, cps@); + lemma_early_push(cps@, c); + lemma_has_escape_plain(s, k as int, j as int); + } cps.push(c); j = j.saturating_add(1); } else { + proof { + let s = src@; + if j < s.len() { + lemma_id_char_equiv(s[j as int]); + } + assert(ind_unit_end(s, j as int, false) < 0); + lemma_name_end_stop(s, j as int); + lemma_name_end_derives(s, k as int, true); + } return Some((cps, escaped, valid, j)); } } @@ -702,6 +1542,8 @@ fn scan_punctuator(src: &[u32], k: usize) -> (r: usize) k < src.len(), ensures k <= r <= src.len(), + r > k ==> punctuator(src@, k as int, r as int), + forall|j: int| punctuator(src@, k as int, j) ==> j <= r, { let c0 = peek(src, k); let c1 = peek(src, k.saturating_add(1)); @@ -801,6 +1643,17 @@ fn scan_punctuator(src: &[u32], k: usize) -> (r: usize) } else { 0 }; + proof { + // The punctuators are the closed forms of `independent.rs`. + assert forall|j: int| punctuator(src@, k as int, j) implies j <= k + n by { + lemma_other_punctuator_equiv(src@, k as int, j); + lemma_optional_chaining_equiv(src@, k as int, j); + } + if n > 0 { + lemma_other_punctuator_equiv(src@, k as int, (k + n) as int); + lemma_optional_chaining_equiv(src@, k as int, (k + n) as int); + } + } k.saturating_add(n) } @@ -815,22 +1668,52 @@ fn scan_digits_sep(src: &[u32], k: usize, radix: u32) -> (r: usize) requires k < src.len(), radix == 2 || radix == 8 || radix == 10 || radix == 16, + is_radix_digit(radix as int, src@[k as int]), ensures k < r <= src.len(), + ind_sep_digits(radix as int, src@, k as int, r as int), + forall|j: int| j > r ==> !ind_sep_digits(radix as int, src@, k as int, j), + !(r < src.len() && is_radix_digit(radix as int, src@[r as int])), + !(r + 1 < src.len() && src@[r as int] == 0x5F && is_radix_digit( + radix as int, + src@[r as int + 1], + )), { + proof { + lemma_digit_class_equiv(radix as int, src@[k as int]); + lemma_sep_base(radix as int, src@, k as int); + } let mut j = k.saturating_add(1); loop invariant k < j <= src.len(), radix == 2 || radix == 8 || radix == 10 || radix == 16, + ind_sep_digits(radix as int, src@, k as int, j as int), decreases src.len() - j, { let c = peek(src, j); if is_radix(radix, c) { + proof { + lemma_digit_class_equiv(radix as int, c); + lemma_sep_extend1(radix as int, src@, k as int, j as int); + } j = j.saturating_add(1); } else if c == 0x5F && is_radix(radix, peek(src, j.saturating_add(1))) { + proof { + lemma_digit_class_equiv(radix as int, src@[j as int + 1]); + lemma_sep_extend2(radix as int, src@, k as int, j as int); + } j = j.saturating_add(2); } else { + proof { + if j < src.len() { + lemma_digit_class_equiv(radix as int, src@[j as int]); + } + if j + 1 < src.len() { + lemma_digit_class_equiv(radix as int, src@[j as int + 1]); + } + lemma_sep_maximal(radix as int, src@, k as int, j as int); + } return j; } } @@ -842,6 +1725,8 @@ fn scan_exponent(src: &[u32], k: usize) -> (r: usize) k <= src.len(), ensures k <= r <= src.len(), + r > k ==> ind_exponent(src@, k as int, r as int), + forall|j: int| ind_exponent(src@, k as int, j) ==> j <= r, { let c = peek(src, k); if c == 0x65 || c == 0x45 { @@ -869,6 +1754,8 @@ fn scan_decimal_tail(src: &[u32], k: usize) -> (r: usize) k <= src.len(), ensures k <= r <= src.len(), + ind_tail(src@, k as int, r as int), + forall|j: int| ind_tail(src@, k as int, j) ==> j <= r, { if peek(src, k) == 0x2E { let m = k.saturating_add(1); @@ -877,112 +1764,477 @@ fn scan_decimal_tail(src: &[u32], k: usize) -> (r: usize) } else { m }; - scan_exponent(src, n) + let r = scan_exponent(src, n); + proof { + // The tail `.` digits? exponent?, on `[k, r)`. + assert(ind_dl_tail_mid(src@, k as int, n as int, r as int)); + assert forall|j: int| ind_tail(src@, k as int, j) implies j <= r by { + if ind_tail(src@, k as int, j) && j != k { + assert(!ind_exponent(src@, k as int, j)); + let b = choose|b: int| ind_dl_tail_mid(src@, k as int, b, j); + if m < src.len() && is_decimal_digit(src@[m as int]) { + if b > n { + assert(ind_sep_digits(10, src@, m as int, b)); + } + if b < n { + if b > m { + lemma_sep_chars(10, src@, m as int, n as int, b); + } + assert(b == j); + } + } + } + } + } + r } else { scan_exponent(src, k) } } -/// §12.9.3 `NumericLiteral` at `k`, the longest: its end, whether it is a -/// BigInt literal, and whether it is legacy (§12.9.3.1). -fn scan_numeric(src: &[u32], k: usize) -> (r: Option<(usize, bool, bool)>) +/// What [`scan_numeric`] returns, in the closed forms of `independent.rs`. +pub open spec fn numeric_ind_result(s: Seq, k: int, r: Option<(usize, bool, bool)>) -> bool { + match r { + Some((e, bigint, legacy)) => k < e <= s.len() && ind_numeric_literal(s, k, e as int) && ( + forall|j: int| ind_numeric_literal(s, k, j) ==> j <= e) && bigint == ind_is_bigint( + s, + k, + e as int, + ) && legacy == ind_is_legacy(s, k, e as int), + None => forall|j: int| !ind_numeric_literal(s, k, j), + } +} + +/// A `.` and a decimal digit. +fn num_dot(src: &[u32], k: usize) -> (r: Option<(usize, bool, bool)>) requires - k < src.len(), + k + 1 < src.len(), + src@[k as int] == 0x2E, + is_decimal_digit(src@[k as int + 1]), ensures - match r { - Some((e, _, _)) => k < e <= src.len(), - None => true, - }, + numeric_ind_result(src@, k as int, r), { - let c0 = peek(src, k); - let c1 = peek(src, k.saturating_add(1)); - if c0 == 0x2E { - // DecimalLiteral :: . DecimalDigits[+Sep] ExponentPart[+Sep]? - if is_dec(c1) { - let e = scan_digits_sep(src, k.saturating_add(1), 10); - Some((scan_exponent(src, e), false, false)) - } else { - None - } - } else if c0 == 0x30 { - let c2 = peek(src, k.saturating_add(2)); - let radix: u32 = if c1 == 0x62 || c1 == 0x42 { - 2 - } else if c1 == 0x6F || c1 == 0x4F { - 8 - } else if c1 == 0x78 || c1 == 0x58 { - 16 - } else { - 0 - }; - if radix != 0 && is_radix(radix, c2) { - // NonDecimalIntegerLiteral[+Sep] BigIntLiteralSuffix? - let e = scan_digits_sep(src, k.saturating_add(2), radix); - if peek(src, e) == 0x6E { - Some((e.saturating_add(1), true, false)) - } else { - Some((e, false, false)) + let e = scan_digits_sep(src, k.saturating_add(1), 10); + let r = scan_exponent(src, e); + proof { + let s = src@; + lemma_shape_dot(s, k as int); + assert forall|j: int| ind_numeric_literal(s, k as int, j) implies j <= r by { + let b = choose|b: int| #[trigger] ind_dl_alt2(s, k as int, j, b); + if b < e { + lemma_sep_chars(10, s, k as int + 1, e as int, b); + assert(j == b); } - } else if c1 == 0x6E { - // DecimalBigIntegerLiteral :: 0 BigIntLiteralSuffix - Some((k.saturating_add(2), true, false)) - } else if is_dec(c1) { - // `0` and decimal digits, with no separator: a - // LegacyOctalIntegerLiteral if they are all octal, else a - // NonOctalDecimalIntegerLiteral, which a DecimalLiteral may - // continue. - let mut j = k.saturating_add(1); - let mut octal = true; - loop - invariant - k < j <= src.len(), - decreases src.len() - j, - { - let c = peek(src, j); - if is_dec(c) { - if !is_oct(c) { - octal = false; + } + assert(ind_dl_alt2(s, k as int, r as int, e as int)); + assert(ind_decimal_literal(s, k as int, r as int)); + } + Some((r, false, false)) +} + +/// A `0`, a prefix letter and a digit of its radix. +fn num_prefix(src: &[u32], k: usize, radix: u32) -> (r: Option<(usize, bool, bool)>) + requires + k + 2 < src.len(), + prefix_digit(src@, k as int), + radix == 2 || radix == 8 || radix == 16, + radix == 2 ==> (src@[k as int + 1] == 0x62 || src@[k as int + 1] == 0x42), + radix == 8 ==> (src@[k as int + 1] == 0x6F || src@[k as int + 1] == 0x4F), + radix == 16 ==> (src@[k as int + 1] == 0x78 || src@[k as int + 1] == 0x58), + is_radix_digit(radix as int, src@[k as int + 2]), + ensures + numeric_ind_result(src@, k as int, r), +{ + // NonDecimalIntegerLiteral[+Sep] BigIntLiteralSuffix? + let e = scan_digits_sep(src, k.saturating_add(2), radix); + proof { + lemma_shape_prefix(src@, k as int, radix as int); + } + if peek(src, e) == 0x6E { + proof { + let s = src@; + assert forall|j: int| ind_numeric_literal(s, k as int, j) implies j <= e + 1 by { + if j > k + 3 && s[j - 1] == 0x6E && ind_sep_digits( + radix as int, + s, + k as int + 2, + j - 1, + ) { + if j - 1 < e { + lemma_sep_chars(radix as int, s, k as int + 2, e as int, j - 1); } - j = j.saturating_add(1); - } else { - break; } } - if octal { - Some((j, false, true)) - } else { - Some((scan_decimal_tail(src, j), false, true)) - } - } else { - // DecimalIntegerLiteral :: 0 - Some((scan_decimal_tail(src, k.saturating_add(1)), false, false)) - } - } else if is_dec(c0) { - // DecimalIntegerLiteral :: NonZeroDigit NumericLiteralSeparator? - // DecimalDigits[+Sep], or a DecimalBigIntegerLiteral. - let e = scan_digits_sep(src, k, 10); - if peek(src, e) == 0x6E { - Some((e.saturating_add(1), true, false)) - } else { - Some((scan_decimal_tail(src, e), false, false)) } + Some((e.saturating_add(1), true, false)) } else { - None + proof { + let s = src@; + assert forall|j: int| ind_numeric_literal(s, k as int, j) implies j <= e by { + if j > k + 3 && s[j - 1] == 0x6E && ind_sep_digits( + radix as int, + s, + k as int + 2, + j - 1, + ) { + if j - 1 < e { + lemma_sep_chars(radix as int, s, k as int + 2, e as int, j - 1); + } + assert(false); + } + } + } + Some((e, false, false)) } } -/// §12.9.3: "The SourceCharacter immediately following a NumericLiteral -/// must not be an IdentifierStart or DecimalDigit." -fn numeric_follow_error(src: &[u32], j: usize) -> (r: bool) +/// A `0` and a decimal digit: `0` and decimal digits, with no separator: a +/// LegacyOctalIntegerLiteral if they are all octal, else a +/// NonOctalDecimalIntegerLiteral, which a DecimalLiteral may continue. +fn num_zero_digit(src: &[u32], k: usize) -> (r: Option<(usize, bool, bool)>) requires - j <= src.len(), + k + 1 < src.len(), + src@[k as int] == 0x30, + is_decimal_digit(src@[k as int + 1]), + ensures + numeric_ind_result(src@, k as int, r), +{ + let mut j = k.saturating_add(1); + let mut octal = true; + while is_dec(peek(src, j)) + invariant + k < j <= src.len(), + all_dec(src@, k as int + 1, j as int), + octal == (forall|t: int| + k + 1 <= t < j ==> is_octal_digit(#[trigger] src@[t])), + !octal ==> exists|t: int| + k + 1 <= t < j && is_non_octal_digit(#[trigger] src@[t]), + decreases src.len() - j, + { + let c = peek(src, j); + if !is_oct(c) { + octal = false; + } + j = j.saturating_add(1); + } + proof { + lemma_shape_zero_digit(src@, k as int); + assert(j > k + 1) by { + if j == k + 1 { + assert(!is_decimal_digit(src@[k as int + 1])); + } + } + } + if octal { + proof { + let s = src@; + assert(ind_plain_digits(8, s, k as int + 1, j as int)); + assert(ind_legacy_octal(s, k as int, j as int)); + assert forall|jj: int| ind_numeric_literal(s, k as int, jj) implies jj <= j by { + if jj > j { + if ind_legacy_octal(s, k as int, jj) { + assert(ind_dec(s[j as int])); + } + } + if exists|a: int| #![trigger ind_non_octal_decimal_integer(s, k as int, a)] + ind_non_octal_decimal_integer(s, k as int, a) && ind_tail(s, a, jj) { + let a = choose|a: int| + #![trigger ind_non_octal_decimal_integer(s, k as int, a)] + ind_non_octal_decimal_integer(s, k as int, a) && ind_tail( + s, + a, + jj, + ); + let t = choose|t: int| k < t < a && (s[t] == 0x38 || s[t] == 0x39); + assert(a <= j) by { + if a > j { + assert(ind_dec(s[j as int])); + } + } + assert(is_octal_digit(s[t])); + } + } + } + Some((j, false, true)) + } else { + let r = scan_decimal_tail(src, j); + proof { + let s = src@; + let t0 = choose|t: int| k + 1 <= t < j && is_non_octal_digit(src@[t]); + assert(ind_non_octal_decimal_integer(s, k as int, j as int)) by { + assert(ind_plain_digits(10, s, k as int + 1, j as int)); + assert(k < t0 < j && (s[t0] == 0x38 || s[t0] == 0x39)); + } + assert forall|jj: int| ind_numeric_literal(s, k as int, jj) implies jj <= r by { + if ind_legacy_octal(s, k as int, jj) && jj > j { + assert(ind_dec(s[j as int])); + } + if exists|a: int| #![trigger ind_non_octal_decimal_integer(s, k as int, a)] + ind_non_octal_decimal_integer(s, k as int, a) && ind_tail(s, a, jj) { + let a = choose|a: int| + #![trigger ind_non_octal_decimal_integer(s, k as int, a)] + ind_non_octal_decimal_integer(s, k as int, a) && ind_tail( + s, + a, + jj, + ); + assert(a <= j) by { + if a > j { + assert(ind_dec(s[j as int])); + } + } + if a < j { + if jj > a { + lemma_tail_first(s, a, jj); + assert(ind_dec(s[a])); + } + } + } + } + } + Some((r, false, true)) + } +} + +/// A non-zero digit. +#[verifier::rlimit(400)] +fn num_nonzero(src: &[u32], k: usize) -> (r: Option<(usize, bool, bool)>) + requires + k < src.len(), + is_decimal_digit(src@[k as int]), + src@[k as int] != 0x30, + ensures + numeric_ind_result(src@, k as int, r), +{ + let e = scan_digits_sep(src, k, 10); + proof { + lemma_shape_nonzero(src@, k as int); + } + if peek(src, e) == 0x6E { + proof { + let s = src@; + assert forall|j: int| ind_numeric_literal(s, k as int, j) implies j <= e + 1 by { + if exists|a: int| #![trigger ind_sep_digits(10, s, k as int, a)] + ind_sep_digits(10, s, k as int, a) && ind_tail(s, a, j) { + let a = choose|a: int| #![trigger ind_sep_digits(10, s, k as int, a)] + ind_sep_digits(10, s, k as int, a) && ind_tail(s, a, j); + if j > a { + lemma_tail_first(s, a, j); + if a < e { + lemma_sep_chars(10, s, k as int, e as int, a); + } + } + } + if j > k + 1 && s[j - 1] == 0x6E && ind_sep_digits(10, s, k as int, j - 1) { + if j - 1 < e { + lemma_sep_chars(10, s, k as int, e as int, j - 1); + } + } + } + } + Some((e.saturating_add(1), true, false)) + } else { + let r = scan_decimal_tail(src, e); + proof { + let s = src@; + assert forall|j: int| ind_numeric_literal(s, k as int, j) implies j <= r by { + if exists|a: int| #![trigger ind_sep_digits(10, s, k as int, a)] + ind_sep_digits(10, s, k as int, a) && ind_tail(s, a, j) { + let a = choose|a: int| #![trigger ind_sep_digits(10, s, k as int, a)] + ind_sep_digits(10, s, k as int, a) && ind_tail(s, a, j); + if a < e { + if j > a { + lemma_tail_first(s, a, j); + lemma_sep_chars(10, s, k as int, e as int, a); + } + } + } + if j > k + 1 && s[j - 1] == 0x6E && ind_sep_digits(10, s, k as int, j - 1) { + if j - 1 < e { + lemma_sep_chars(10, s, k as int, e as int, j - 1); + } else { + assert(j - 1 == e); + } + } + } + } + Some((r, false, false)) + } +} + +/// [`scan_numeric`], stated in the closed forms of `independent.rs`. +fn scan_numeric_ind(src: &[u32], k: usize) -> (r: Option<(usize, bool, bool)>) + requires + k < src.len(), + ensures + numeric_ind_result(src@, k as int, r), +{ + let c0 = peek(src, k); + let c1 = peek(src, k.saturating_add(1)); + if c0 == 0x2E { + // DecimalLiteral :: . DecimalDigits[+Sep] ExponentPart[+Sep]? + if is_dec(c1) { + num_dot(src, k) + } else { + proof { + let s = src@; + assert forall|j: int| !ind_numeric_literal(s, k as int, j) by { + lemma_shape_dot(s, k as int); + if ind_numeric_literal(s, k as int, j) { + let b = choose|b: int| #[trigger] ind_dl_alt2(s, k as int, j, b); + } + } + } + None + } + } else if c0 == 0x30 { + let c2 = peek(src, k.saturating_add(2)); + let radix: u32 = if c1 == 0x62 || c1 == 0x42 { + 2 + } else if c1 == 0x6F || c1 == 0x4F { + 8 + } else if c1 == 0x78 || c1 == 0x58 { + 16 + } else { + 0 + }; + if radix != 0 && is_radix(radix, c2) { + num_prefix(src, k, radix) + } else if c1 == 0x6E { + // DecimalBigIntegerLiteral :: 0 BigIntLiteralSuffix + proof { + lemma_shape_zero_n(src@, k as int); + } + Some((k.saturating_add(2), true, false)) + } else if is_dec(c1) { + num_zero_digit(src, k) + } else { + // DecimalIntegerLiteral :: 0 + proof { + let s = src@; + assert(!prefix_digit(s, k as int)); + lemma_shape_zero_alone(s, k as int); + } + Some((scan_decimal_tail(src, k.saturating_add(1)), false, false)) + } + } else if is_dec(c0) { + // DecimalIntegerLiteral :: NonZeroDigit NumericLiteralSeparator? + // DecimalDigits[+Sep], or a DecimalBigIntegerLiteral. + num_nonzero(src, k) + } else { + proof { + let s = src@; + assert forall|j: int| !ind_numeric_literal(s, k as int, j) by { + lemma_decimal_literal_tail(s, k as int, j); + if ind_decimal_literal(s, k as int, j) { + if !(exists|b: int| #[trigger] ind_dl_alt2(s, k as int, j, b)) { + let a = choose|a: int| #![trigger ind_decimal_integer(s, k as int, a)] + k < a && ind_decimal_integer(s, k as int, a) && ind_tail(s, a, j); + } + } + } + } + None + } +} + +/// §12.9.3 `NumericLiteral` at `k`, the longest: its end, whether it is a +/// BigInt literal, and whether it is legacy (§12.9.3.1). +/// +/// Proven: the end is a range the spec derives as a `NumericLiteral` +/// ([`numeric_literal`]) and no longer range is; `None` means no range is; +/// the two flags are [`numeric_literal_is_bigint`] and +/// [`numeric_literal_is_legacy`] of the range. +fn scan_numeric(src: &[u32], k: usize) -> (r: Option<(usize, bool, bool)>) + requires + k < src.len(), + ensures + match r { + Some((e, bigint, legacy)) => k < e <= src.len() && numeric_literal( + src@, + k as int, + e as int, + ) && (forall|j: int| numeric_literal(src@, k as int, j) ==> j <= e) && bigint + == numeric_literal_is_bigint(src@, k as int, e as int) && legacy + == numeric_literal_is_legacy(src@, k as int, e as int), + None => forall|j: int| !numeric_literal(src@, k as int, j), + }, +{ + let r = scan_numeric_ind(src, k); + proof { + match r { + Some((e, bigint, legacy)) => { + lemma_numeric_literal_equiv(src@, k as int, e as int); + lemma_flags_equiv(src@, k as int, e as int); + assert forall|j: int| numeric_literal(src@, k as int, j) implies j <= e by { + lemma_numeric_literal_equiv(src@, k as int, j); + } + }, + None => { + assert forall|j: int| !numeric_literal(src@, k as int, j) by { + lemma_numeric_literal_equiv(src@, k as int, j); + } + }, + } + } + r +} + +/// §12.9.3: "The SourceCharacter immediately following a NumericLiteral +/// must not be an IdentifierStart or DecimalDigit." +/// +/// Proven: it is [`numeric_literal_follow_error`] (an `IdentifierStart` +/// that is a `UnicodeEscapeSequence` included; above U+007F, the spec's +/// `ID_Start` is the table of [`super::unicode`]). +fn numeric_follow_error(src: &[u32], j: usize) -> (r: bool) + requires + j <= src.len(), + ensures + r == numeric_literal_follow_error(src@, j as int), { let c = peek(src, j); + proof { + let s = src@; + if j < s.len() { + assert(c == s[j as int]); + if is_decimal_digit(c) { + assert(numeric_literal_follow_error(s, j as int)); + } + } + } if is_dec(c) || id_start_char(c) { + proof { + let s = src@; + if j < s.len() && !is_decimal_digit(c) { + assert(identifier_start(s, j as int, j as int + 1)); + } + } true } else if c == 0x5C { - scan_unicode_escape(src, j.saturating_add(1)).is_some() + let esc = scan_unicode_escape(src, j.saturating_add(1)); + proof { + let s = src@; + if esc is Some { + let e = esc.unwrap().1; + lemma_unicode_escape_equiv(s, j as int + 1, e as int); + assert(identifier_start(s, j as int, e as int)); + } else { + assert forall|k: int| !(j < k <= s.len() && identifier_start(s, j as int, k)) by { + if j < k <= s.len() && identifier_start(s, j as int, k) { + lemma_unicode_escape_equiv(s, j as int + 1, k); + } + } + } + } + esc.is_some() } else { + proof { + let s = src@; + assert forall|k: int| !(j < k <= s.len() && identifier_start(s, j as int, k)) by {} + if j < s.len() { + assert(!is_decimal_digit(s[j as int])); + } + } false } } @@ -995,12 +2247,23 @@ fn numeric_follow_error(src: &[u32], j: usize) -> (r: bool) /// octal digit: its MV, pushed as one code unit, and its end. The /// lookaheads make it the longest run its first digit allows: three digits /// after `0` to `3`, two after `4` to `7`. +/// +/// Proven: it is the `LegacyOctalEscapeSequence` the spec derives at `k` +/// (given that a leading `0` has an octal digit after it, the other cases +/// being `0` alone), and the code unit pushed is its MV. fn scan_legacy_octal_escape(src: &[u32], k: usize, units: &mut Vec) -> (r: usize) requires k < src.len(), is_octal_digit(src@[k as int]), + src@[k as int] != 0x30 || (k + 1 < src.len() && is_octal_digit(src@[k as int + 1])), ensures k < r <= src.len(), + legacy_octal_escape_sequence(src@, k as int, r as int), + final(units)@ =~= old(units)@ + seq![legacy_octal_escape_sequence_mv( + src@, + k as int, + r as int, + ) as u16], { let d = peek(src, k); let mut v: u32 = d.saturating_sub(0x30); @@ -1021,23 +2284,51 @@ fn scan_legacy_octal_escape(src: &[u32], k: usize, units: &mut Vec) -> (r: /// §12.9.4 `\ EscapeSequence` or `LineContinuation`, the backslash at /// `k`: appends the SV (§12.9.4.2) and returns the end. +/// +/// Proven: `Some(e)` is the one end of a `StringCharacter` (an escape) at +/// `k` that the spec derives, and what is appended is its SV; `None` means +/// the spec derives none. fn scan_string_escape(src: &[u32], k: usize, units: &mut Vec) -> (r: Option) requires k < src.len(), + src@[k as int] == 0x5C, ensures match r { - Some(e) => k + 1 < e <= src.len(), - None => true, + Some(e) => k + 1 < e <= src.len() && exists|v: Seq| + string_character_sv(src@, k as int, e as int, 0x22, v) && final(units)@ + =~= old(units)@ + v, + None => forall|e: int, v: Seq| !string_character_sv(src@, k as int, e, 0x22, v), }, { let m = k.saturating_add(1); let d = peek(src, m); + proof { + lemma_is_lit(src@, m as int, m as int + 2, seq!['u', '{']); + } if is_lt(d) { // LineContinuation: the empty String. - Some(line_terminator_sequence_end(src, m)) + let e = line_terminator_sequence_end(src, m); + proof { + assert(line_continuation(src@, k as int, e as int)); + assert(string_character_sv( + src@, + k as int, + e as int, + 0x22, + Seq::::empty(), + )); + assert(units@ =~= old(units)@ + Seq::::empty()); + } + Some(e) } else if d == 0x27 || d == 0x22 || d == 0x5C { // SingleEscapeCharacter ' " \: the character itself. units.push(d as u16); + proof { + let v = seq![d as u16]; + assert(character_escape_sequence_sv(src@, m as int, m as int + 1, v)); + assert(string_character_sv(src@, k as int, m as int + 1, 0x22, v)); + assert(units@ =~= old(units)@ + v); + } Some(m.saturating_add(1)) } else if d == 0x62 || d == 0x66 || d == 0x6E || d == 0x72 || d == 0x74 || d == 0x76 { // SingleEscapeCharacter b f n r t v (Table "String Single Character @@ -1056,21 +2347,55 @@ fn scan_string_escape(src: &[u32], k: usize, units: &mut Vec) -> (r: Option 0x0D }; units.push(u); + proof { + let v = seq![u]; + assert(u == single_escape_code_unit(d)); + assert(character_escape_sequence_sv(src@, m as int, m as int + 1, v)); + assert(string_character_sv(src@, k as int, m as int + 1, 0x22, v)); + assert(units@ =~= old(units)@ + v); + } Some(m.saturating_add(1)) } else if d == 0x30 && !is_dec(peek(src, m.saturating_add(1))) { // `0` [lookahead ∉ DecimalDigit]: 0x0000. units.push(0); + proof { + let v = seq![0u16]; + assert(template_escape_sequence_sv(src@, m as int, m as int + 1, v)); + assert(string_character_sv(src@, k as int, m as int + 1, 0x22, v)); + assert(units@ =~= old(units)@ + v); + } Some(m.saturating_add(1)) } else if d == 0x30 && !is_oct(peek(src, m.saturating_add(1))) { // LegacyOctalEscapeSequence :: `0` [lookahead ∈ { 8, 9 }]: 0. units.push(0); + proof { + let v = seq![0u16]; + assert(legacy_octal_escape_sequence(src@, m as int, m as int + 1)); + assert(legacy_octal_escape_sequence_mv(src@, m as int, m as int + 1) == 0); + assert(escape_sequence_sv(src@, m as int, m as int + 1, v)); + assert(string_character_sv(src@, k as int, m as int + 1, 0x22, v)); + assert(units@ =~= old(units)@ + v); + } Some(m.saturating_add(1)) } else if is_oct(d) { // LegacyOctalEscapeSequence, the other alternatives. - Some(scan_legacy_octal_escape(src, m, units)) + let e = scan_legacy_octal_escape(src, m, units); + proof { + let v = seq![legacy_octal_escape_sequence_mv(src@, m as int, e as int) as u16]; + assert(escape_sequence_sv(src@, m as int, e as int, v)); + assert(string_character_sv(src@, k as int, e as int, 0x22, v)); + assert(units@ =~= old(units)@ + v); + } + Some(e) } else if d == 0x38 || d == 0x39 { // NonOctalDecimalEscapeSequence: 0x0038 or 0x0039. units.push(d as u16); + proof { + let v = seq![d as u16]; + assert(escape_sequence_sv(src@, m as int, m as int + 1, v)); + assert(string_character_sv(src@, k as int, m as int + 1, 0x22, v)); + assert(units@ =~= old(units)@ + v); + } Some(m.saturating_add(1)) } else if d == 0x78 { // HexEscapeSequence :: x HexDigit HexDigit @@ -1078,8 +2403,25 @@ fn scan_string_escape(src: &[u32], k: usize, units: &mut Vec) -> (r: Option let b = peek(src, m.saturating_add(2)); if is_hex(a) && is_hex(b) { units.push(hex_val(a).saturating_mul(16).saturating_add(hex_val(b)) as u16); + proof { + let s = src@; + reveal_with_fuel(hex_digits_mv, 3); + let v = seq![hex_digits_mv(s, m as int + 1, m as int + 3) as u16]; + assert(hex_escape_sequence(s, m as int, m as int + 3)); + assert(escape_sequence_sv(s, m as int, m as int + 3, v)); + assert(string_character_sv(s, k as int, m as int + 3, 0x22, v)); + assert(units@ =~= old(units)@ + v); + } Some(m.saturating_add(3)) } else { + proof { + assert forall|e: int, v: Seq| !string_character_sv(src@, k as int, e, 0x22, v) by { + if string_character_sv(src@, k as int, e, 0x22, v) { + assert(!hex_escape_sequence(src@, m as int, e)); + assert(!line_continuation(src@, k as int, e)); + } + } + } None } } else if d == 0x75 { @@ -1087,61 +2429,163 @@ fn scan_string_escape(src: &[u32], k: usize, units: &mut Vec) -> (r: Option match scan_unicode_escape(src, m) { Some((cp, e)) => { push_utf16(units, cp); + proof { + let s = src@; + lemma_unicode_escape_equiv(s, m as int, e as int); + let v = utf16_encode_code_point(cp as int); + assert(unicode_escape_sequence(s, m as int, e as int)); + assert(v =~= unicode_escape_sv(s, m as int, e as int)) by { + if s[m as int + 1] != 0x7B { + assert(cp <= 0xFFFF); + } + } + assert(escape_sequence_sv(s, m as int, e as int, v)); + assert(string_character_sv(s, k as int, e as int, 0x22, v)); + assert(units@ =~= old(units)@ + v); + } Some(e) }, - None => None, + None => { + proof { + assert forall|e: int, v: Seq| + !string_character_sv(src@, k as int, e, 0x22, v) by { + lemma_unicode_escape_equiv(src@, m as int, e); + if string_character_sv(src@, k as int, e, 0x22, v) { + assert(!line_continuation(src@, k as int, e)); + } + } + } + None + }, } } else if is_src(d) { // NonEscapeCharacter: the code point. push_utf16(units, d); + proof { + let v = utf16_encode_code_point(d as int); + assert(character_escape_sequence_sv(src@, m as int, m as int + 1, v)); + assert(string_character_sv(src@, k as int, m as int + 1, 0x22, v)); + assert(units@ =~= old(units)@ + v); + } Some(m.saturating_add(1)) } else { + proof { + assert forall|e: int, v: Seq| !string_character_sv(src@, k as int, e, 0x22, v) by { + if string_character_sv(src@, k as int, e, 0x22, v) { + assert(!line_continuation(src@, k as int, e)); + } + } + } None } } /// §12.9.4 `StringLiteral` at `k` (a quote): its SV and its end. +/// +/// Proven: `Some((units, e))` is the one `StringLiteral` the spec derives at +/// `k`, and `units` is its SV ([`string_literal_sv`]); `None` means the +/// spec derives none. fn scan_string(src: &[u32], k: usize) -> (r: Option<(Vec, usize)>) requires k < src.len(), src@[k as int] == 0x22 || src@[k as int] == 0x27, ensures match r { - Some((_, e)) => k + 2 <= e <= src.len(), - None => true, + Some((units, e)) => k + 2 <= e <= src.len() && string_literal_sv( + src@, + k as int, + e as int, + units@, + ) && (forall|j: int| string_literal(src@, k as int, j) ==> j == e), + None => forall|j: int| !string_literal(src@, k as int, j), }, { let quote = peek(src, k); let mut units: Vec = Vec::new(); let mut j = k.saturating_add(1); + proof { + lemma_str_init(src@, k as int + 1, quote); + } loop invariant k < j <= src.len(), + quote == src@[k as int], quote == 0x22 || quote == 0x27, + str_inv(src@, k as int + 1, j as int, quote, units@), decreases src.len() - j, { let c = peek(src, j); if c == quote { + proof { + lemma_str_quote(src@, k as int + 1, j as int, quote, units@, k as int); + } return Some((units, j.saturating_add(1))); } else if c == 0x5C { + let ghost before = units@; match scan_string_escape(src, j, &mut units) { Some(e) => { + proof { + let v = choose|v: Seq| + string_character_sv(src@, j as int, e as int, 0x22, v) && units@ + =~= before + v; + lemma_esc_q_free(src@, j as int, e as int, 0x22, quote, v); + lemma_str_step(src@, k as int + 1, j as int, e as int, quote, before, v); + } j = e; }, None => { + proof { + assert forall|m: int, v: Seq| + !string_character_sv(src@, j as int, m, quote, v) by { + if string_character_sv(src@, j as int, m, quote, v) { + lemma_esc_q_free(src@, j as int, m, quote, 0x22, v); + } + } + lemma_str_no_char(src@, k as int + 1, j as int, quote, before, k as int); + } return None; }, } } else if c == 0x2028 || c == 0x2029 { // , : the code unit. + let ghost before = units@; units.push(c as u16); + proof { + assert(string_character_sv( + src@, + j as int, + j as int + 1, + quote, + seq![c as u16], + )); + assert(units@ =~= before + seq![c as u16]); + lemma_str_step( + src@, + k as int + 1, + j as int, + j as int + 1, + quote, + before, + seq![c as u16], + ); + } j = j.saturating_add(1); } else if is_line_char(c) { // SourceCharacter but not one of the quote or \ or // LineTerminator. + let ghost before = units@; push_utf16(&mut units, c); + proof { + let v = utf16_encode_code_point(c as int); + assert(string_character_sv(src@, j as int, j as int + 1, quote, v)); + assert(units@ =~= before + v); + lemma_str_step(src@, k as int + 1, j as int, j as int + 1, quote, before, v); + } j = j.saturating_add(1); } else { + proof { + lemma_str_no_char(src@, k as int + 1, j as int, quote, units@, k as int); + } return None; } } @@ -1152,32 +2596,57 @@ fn scan_string(src: &[u32], k: usize) -> (r: Option<(Vec, usize)>) // --------------------------------------------------------------------- /// §12.9.5 `RegularExpressionClass` at `k` (a `[`): its end. +/// +/// Proven: the end is a `RegularExpressionClass` of the spec, and it is the +/// only one that starts at `k` ([`class_scan`]); `None` means the spec +/// derives none. fn scan_regexp_class(src: &[u32], k: usize) -> (r: Option) requires k < src.len(), + src@[k as int] == 0x5B, ensures match r { - Some(e) => k < e <= src.len(), - None => true, + Some(e) => k < e <= src.len() && regexp_class(src@, k as int, e as int) && class_scan( + src@, + k as int + 1, + ) == Some(e as int), + None => class_scan(src@, k as int + 1) is None, }, { let mut j = k.saturating_add(1); loop invariant k < j <= src.len(), + src@[k as int] == 0x5B, + regexp_class_chars(src@, k as int + 1, j as int), + class_scan(src@, k as int + 1) == class_scan(src@, j as int), decreases src.len() - j, { let c = peek(src, j); if c == 0x5D { + // `]`: RegularExpressionClass :: `[` RegularExpressionClassChars `]` + proof { + assert(src@[j as int] == 0x5D); + assert(class_scan(src@, j as int) == Some(j as int + 1)); + assert(regexp_class(src@, k as int, j as int + 1)); + } return Some(j.saturating_add(1)); } else if c == 0x5C { // RegularExpressionBackslashSequence if is_line_char(peek(src, j.saturating_add(1))) { + proof { + assert(regexp_class_char(src@, j as int, j as int + 2)); + lemma_class_chars_ext(src@, k as int + 1, j as int, j as int + 2); + } j = j.saturating_add(2); } else { return None; } } else if is_line_char(c) { + proof { + assert(regexp_class_char(src@, j as int, j as int + 1)); + lemma_class_chars_ext(src@, k as int + 1, j as int, j as int + 1); + } j = j.saturating_add(1); } else { return None; @@ -1187,60 +2656,142 @@ fn scan_regexp_class(src: &[u32], k: usize) -> (r: Option) /// §12.9.5 `RegularExpressionLiteral` at `k` (a `/`): the index of the /// closing `/` and the end of the flags. +/// +/// Proven: the result is a `RegularExpressionLiteral` of the spec (the +/// closing `/` is the first one outside a class and an escape, the flags +/// are the longest run of `IdentifierPartChar`s), every literal the spec +/// derives from `k` has that closing `/` and ends at or before the result +/// ([`lemma_literal_unique`]), and `None` means the spec derives none. fn scan_regexp(src: &[u32], k: usize) -> (r: Option<(usize, usize)>) requires k < src.len(), + src@[k as int] == 0x2F, ensures match r { - Some((b, e)) => k + 1 < b < e <= src.len(), - None => true, + Some((b, e)) => k + 1 < b < e <= src.len() && regexp_literal( + src@, + k as int, + e as int, + b as int, + ) && forall|b2: int, j2: int| + #[trigger] regexp_literal(src@, k as int, j2, b2) ==> b2 == b && j2 <= e, + None => forall|b2: int, j2: int| !#[trigger] regexp_literal(src@, k as int, j2, b2), }, { let first = peek(src, k.saturating_add(1)); // RegularExpressionFirstChar is not `*` or `/`. if first == 0x2A || first == 0x2F { + proof { + assert forall|b2: int, j2: int| !#[trigger] regexp_literal(src@, k as int, j2, b2) by { + if regexp_literal(src@, k as int, j2, b2) { + lemma_literal_scan(src@, k as int, j2, b2); + } + } + } return None; } let mut j = k.saturating_add(1); loop invariant k < j <= src.len(), - j == k + 1 ==> (k + 1 >= src.len() || src@[(k + 1) as int] != 0x2F), + src@[k as int] == 0x2F, + j == k + 1 || regexp_body(src@, k as int + 1, j as int), + j == k + 1 ==> (k + 1 >= src.len() || (src@[(k + 1) as int] != 0x2F && src@[(k + + 1) as int] != 0x2A)), + body_scan(src@, k as int + 1) == body_scan(src@, j as int), decreases src.len() - j, { let c = peek(src, j); if c == 0x2F { // The closing `/`, then RegularExpressionFlags. let mut f = j.saturating_add(1); + proof { + assert(j > k + 1); + } loop invariant k + 1 < j < f <= src.len(), + src@[k as int] == 0x2F, + j < src.len(), + src@[j as int] == 0x2F, + regexp_body(src@, k as int + 1, j as int), + body_scan(src@, k as int + 1) == Some(j as int), + forall|m: int| j < m < f ==> is_identifier_part_char(src@[m]), decreases src.len() - f, { if id_part_char(peek(src, f)) { f = f.saturating_add(1); } else { + proof { + assert(regexp_flags(src@, j as int + 1, f as int)) by { + assert forall|m: int| j as int + 1 <= m < f implies #[trigger] + is_identifier_part_char(src@[m]) by {} + } + assert(nonempty(src@, k as int, f as int)); + assert(src@[k as int] == 0x2F); + assert(regexp_body(src@, k as int + 1, j as int)); + assert(regexp_literal(src@, k as int, f as int, j as int)); + assert forall|b2: int, j2: int| + #[trigger] regexp_literal(src@, k as int, j2, b2) implies b2 == j + as int && j2 <= f by { + lemma_literal_scan(src@, k as int, j2, b2); + if j2 > f { + assert(src@[f as int] == src@[f as int]); + assert(is_identifier_part_char(src@[f as int])); + } + } + } return Some((j, f)); } } } else if c == 0x5C { if is_line_char(peek(src, j.saturating_add(1))) { + proof { + assert(regexp_char(src@, j as int, j as int + 2)); + lemma_body_extend(src@, k as int + 1, j as int, j as int + 2); + assert(rx_next(src@, j as int) == Some(j as int + 2)); + lemma_body_scan_step(src@, j as int, j as int + 2); + } j = j.saturating_add(2); } else { + proof { + assert(rx_next(src@, j as int) is None); + lemma_no_literal(src@, k as int); + } return None; } } else if c == 0x5B { match scan_regexp_class(src, j) { Some(e) => { + proof { + assert(regexp_char(src@, j as int, e as int)); + lemma_body_extend(src@, k as int + 1, j as int, e as int); + assert(rx_next(src@, j as int) == Some(e as int)); + lemma_body_scan_step(src@, j as int, e as int); + } j = e; }, None => { + proof { + assert(rx_next(src@, j as int) is None); + lemma_no_literal(src@, k as int); + } return None; }, } } else if is_line_char(c) { + proof { + assert(regexp_char(src@, j as int, j as int + 1)); + lemma_body_extend(src@, k as int + 1, j as int, j as int + 1); + assert(rx_next(src@, j as int) == Some(j as int + 1)); + lemma_body_scan_step(src@, j as int, j as int + 1); + } j = j.saturating_add(1); } else { + proof { + assert(rx_next(src@, j as int) is None); + lemma_no_literal(src@, k as int); + } return None; } } @@ -1258,20 +2809,53 @@ struct TemplateValues { raw: Vec, } +/// The TV a [`TemplateValues`] stands for. +spec fn tv_of(v: TemplateValues) -> Tv { + if v.defined { + Some(v.cooked@) + } else { + None + } +} + +/// What a successful [`scan_template_escape`] from `k` to `e` establishes: a +/// `TemplateCharacter` of the spec on `k..e`, and the TV and TRV grew by its +/// TV and TRV. +spec fn tesc_ok(s: Seq, k: int, e: int, old_v: TemplateValues, new_v: TemplateValues) -> bool { + k + 1 < e <= s.len() && exists|tv: Tv, trv: Seq| + #![trigger template_character(s, k, e, tv, trv)] + template_character(s, k, e, tv, trv) && tv_of(new_v) =~= tv_concat(tv_of(old_v), tv) + && new_v.raw@ =~= old_v.raw@ + trv +} + /// §12.9.6 `\ TemplateEscapeSequence`, `\ NotEscapeSequence` or /// `LineContinuation`, the backslash at `k`: appends the TV and TRV /// (§12.9.6.1, §12.9.6.2) and returns the end. +/// +/// Proven: `Some(e)` is a `TemplateCharacter` of the spec at `k` (an escape +/// of one of the three kinds; it has one length and value, +/// [`lemma_tchar_det`]), and the TV and TRV grew by its TV and TRV; +/// `None` means the spec derives no `TemplateCharacter` at `k`. fn scan_template_escape(src: &[u32], k: usize, values: &mut TemplateValues) -> (r: Option) requires k < src.len(), + src@[k as int] == 0x5C, ensures - match r { - Some(e) => k + 1 < e <= src.len(), - None => true, - }, + r is Some ==> tesc_ok(src@, k as int, r.unwrap() as int, *old(values), *final(values)), + r is None ==> forall|e: int, tv: Tv, trv: Seq| + !template_character(src@, k as int, e, tv, trv), { let m = k.saturating_add(1); let d = peek(src, m); + let ghost s = src@; + let ghost old_tv = tv_of(*values); + let ghost old_raw = values.raw@; + let ghost old_cooked = values.cooked@; + let ghost old_defined = values.defined; + let ghost old_values = *values; + proof { + lemma_is_lit(s, m as int, m as int + 2, seq!['u', '{']); + } if is_lt(d) { // LineContinuation: TV is empty; TRV is \ and the TRV of the // LineTerminatorSequence ( and are ). @@ -1282,13 +2866,29 @@ fn scan_template_escape(src: &[u32], k: usize, values: &mut TemplateValues) -> ( } else { values.raw.push(0x0A); } + proof { + let trv = seq![0x005Cu16] + line_terminator_sequence_trv(s, m as int, e as int); + assert(line_continuation(s, k as int, e as int)); + assert(template_character(s, k as int, e as int, Some(Seq::::empty()), trv)); + assert(tv_of(*values) =~= tv_concat(old_tv, Some(Seq::::empty()))); + assert(values.raw@ =~= old_raw + trv); + assert(tesc_ok(s, k as int, e as int, old_values, *values)); + } return Some(e); } // In every other case the TRV is the UTF-16 of the matched code points. let e: usize; + let ghost tvg: Tv; if d == 0x27 || d == 0x22 || d == 0x5C { values.cooked.push(d as u16); e = m.saturating_add(1); + proof { + let v = seq![d as u16]; + assert(character_escape_sequence_sv(s, m as int, e as int, v)); + assert(template_escape_sequence_sv(s, m as int, e as int, v)); + tvg = Some(v); + assert(values.cooked@ =~= old_cooked + v && values.defined == old_defined); + } } else if d == 0x62 || d == 0x66 || d == 0x6E || d == 0x72 || d == 0x74 || d == 0x76 { let u: u16 = if d == 0x62 { 0x08 @@ -1305,20 +2905,44 @@ fn scan_template_escape(src: &[u32], k: usize, values: &mut TemplateValues) -> ( }; values.cooked.push(u); e = m.saturating_add(1); + proof { + let v = seq![u]; + assert(u == single_escape_code_unit(d)); + assert(character_escape_sequence_sv(s, m as int, e as int, v)); + assert(template_escape_sequence_sv(s, m as int, e as int, v)); + tvg = Some(v); + assert(values.cooked@ =~= old_cooked + v && values.defined == old_defined); + } } else if d == 0x30 { if is_dec(peek(src, m.saturating_add(1))) { // NotEscapeSequence :: 0 DecimalDigit values.defined = false; e = m.saturating_add(2); + proof { + assert(not_escape_sequence(s, m as int, e as int)); + tvg = None; + assert(!values.defined); + } } else { // TemplateEscapeSequence :: 0 [lookahead ∉ DecimalDigit] values.cooked.push(0); e = m.saturating_add(1); + proof { + let v = seq![0u16]; + assert(template_escape_sequence_sv(s, m as int, e as int, v)); + tvg = Some(v); + assert(values.cooked@ =~= old_cooked + v && values.defined == old_defined); + } } } else if is_dec(d) { // NotEscapeSequence :: DecimalDigit but not 0 values.defined = false; e = m.saturating_add(1); + proof { + assert(not_escape_sequence(s, m as int, e as int)); + tvg = None; + assert(!values.defined); + } } else if d == 0x78 { let a = peek(src, m.saturating_add(1)); let b = peek(src, m.saturating_add(2)); @@ -1326,14 +2950,34 @@ fn scan_template_escape(src: &[u32], k: usize, values: &mut TemplateValues) -> ( // HexEscapeSequence values.cooked.push(hex_val(a).saturating_mul(16).saturating_add(hex_val(b)) as u16); e = m.saturating_add(3); + proof { + reveal_with_fuel(hex_digits_mv, 3); + let v = seq![hex_digits_mv(s, m as int + 1, m as int + 3) as u16]; + assert(hex_escape_sequence(s, m as int, e as int)); + assert(template_escape_sequence_sv(s, m as int, e as int, v)); + tvg = Some(v); + assert(values.cooked@ =~= old_cooked + v && values.defined == old_defined); + } } else if is_hex(a) { // NotEscapeSequence :: x HexDigit [lookahead ∉ HexDigit] values.defined = false; e = m.saturating_add(2); + proof { + assert(!at_hex_digit(s, m as int + 2)); + assert(not_escape_sequence(s, m as int, e as int)); + tvg = None; + assert(!values.defined); + } } else { // NotEscapeSequence :: x [lookahead ∉ HexDigit] values.defined = false; e = m.saturating_add(1); + proof { + assert(!at_hex_digit(s, m as int + 1)); + assert(not_escape_sequence(s, m as int, e as int)); + tvg = None; + assert(!values.defined); + } } } else if d == 0x75 { match scan_unicode_escape(src, m) { @@ -1341,25 +2985,102 @@ fn scan_template_escape(src: &[u32], k: usize, values: &mut TemplateValues) -> ( // UnicodeEscapeSequence push_utf16(&mut values.cooked, cp); e = end; + proof { + lemma_unicode_escape_equiv(s, m as int, e as int); + let v = utf16_encode_code_point(cp as int); + assert(unicode_escape_sequence(s, m as int, e as int)); + assert(v =~= unicode_escape_sv(s, m as int, e as int)) by { + if s[m as int + 1] != 0x7B { + assert(cp <= 0xFFFF); + } + } + assert(template_escape_sequence_sv(s, m as int, e as int, v)); + tvg = Some(v); + assert(values.cooked@ =~= old_cooked + v && values.defined == old_defined); + } }, None => { // NotEscapeSequence, alternatives 5 to 11: `u` and the hex // digits that follow (up to three), or `u{` and all of // them; each ends where no further hex digit follows. values.defined = false; + proof { + lemma_unicode_escape_equiv(s, m as int, m as int + 1); + lemma_hex_run_end(s, m as int + 1); + } if peek(src, m.saturating_add(1)) == 0x7B { - let (_, h) = scan_hex_digits(src, m.saturating_add(2)); + let (hv, h) = scan_hex_digits(src, m.saturating_add(2)); + let _ = hv; e = h; + proof { + lemma_hex_run_end(s, m as int + 2); + assert(s[m as int] == 0x75 && s[m as int + 1] == 0x7B); + assert(ind_escape_end(s, m as int) == -1); + lemma_hex_value_equiv(s, m as int + 2, h as int); + assert(!at_hex_digit(s, h as int)) by { + lemma_hex_run_end(s, m as int + 2); + if h < s.len() { + lemma_digit_class_equiv(16, s[h as int]); + } + } + if h > m + 2 { + lemma_plain_digits_equiv(16, s, m as int + 2, h as int); + assert forall|t: int| m + 2 <= t < h implies ind_hex(#[trigger] s[t]) + by { + lemma_digit_class_equiv(16, s[t]); + } + assert(ind_plain_digits(16, s, m as int + 2, h as int)); + assert(hex_digits(false, s, m as int + 2, h as int)); + if hv > 0x10FFFF { + assert(not_code_point(s, m as int + 2, h as int)); + assert(not_escape_sequence(s, m as int, h as int)); + } else { + assert(!ind_is(s, h as int, 0x7D)); + assert(code_point(s, m as int + 2, h as int)); + assert(!at(s, h as int, '}')); + assert(not_escape_sequence(s, m as int, h as int)); + } + } else { + assert(h == m + 2); + assert(not_escape_sequence(s, m as int, h as int)); + } + tvg = None; + assert(!values.defined); + } } else { - let mut h = m.saturating_add(1); - while is_hex(peek(src, h)) && h < m.saturating_add(4) - invariant - m < h <= src.len(), - decreases src.len() - h, - { - h = h.saturating_add(1); + let a = peek(src, m.saturating_add(1)); + let b = peek(src, m.saturating_add(2)); + let c3 = peek(src, m.saturating_add(3)); + if !is_hex(a) { + e = m.saturating_add(1); + } else if !is_hex(b) { + e = m.saturating_add(2); + } else if !is_hex(c3) { + e = m.saturating_add(3); + } else { + e = m.saturating_add(4); + } + proof { + assert(!at(s, m as int + 1, '{')); + assert(ind_escape_end(s, m as int) == -1); + // The digits that were counted are hex digits, and the + // code point that stopped the count is not one. + if e == m + 4 { + lemma_digit_class_equiv(16, s[m as int + 1]); + lemma_digit_class_equiv(16, s[m as int + 2]); + lemma_digit_class_equiv(16, s[m as int + 3]); + assert(ind_hex_run_end(s, m as int + 1) >= m as int + 4); + assert(!at_hex_digit(s, m as int + 4)) by { + if m + 4 < s.len() && is_hex_digit(s[m as int + 4]) { + lemma_digit_class_equiv(16, s[m as int + 4]); + assert(ind_hex(s[m as int + 4])); + } + } + } + assert(not_escape_sequence(s, m as int, e as int)); + tvg = None; + assert(!values.defined); } - e = h; } }, } @@ -1367,10 +3088,59 @@ fn scan_template_escape(src: &[u32], k: usize, values: &mut TemplateValues) -> ( // CharacterEscapeSequence :: NonEscapeCharacter push_utf16(&mut values.cooked, d); e = m.saturating_add(1); + proof { + let v = utf16_encode_code_point(d as int); + assert(character_escape_sequence_sv(s, m as int, e as int, v)); + assert(template_escape_sequence_sv(s, m as int, e as int, v)); + tvg = Some(v); + assert(values.cooked@ =~= old_cooked + v && values.defined == old_defined); + } } else { + proof { + assert forall|e: int, tv: Tv, trv: Seq| + !template_character(s, k as int, e, tv, trv) by { + if template_character(s, k as int, e, tv, trv) { + assert(!line_continuation(s, k as int, e)); + } + } + } return None; } + proof { + let trv = utf16_of(s, k as int, e as int); + assert(k + 1 < e <= src.len()); + match tvg { + Some(v) => { + assert(template_escape_sequence_sv(s, m as int, e as int, v)); + assert(template_character(s, k as int, e as int, Some(v), trv)); + lemma_tchar_src(s, k as int, e as int, Some(v), trv); + }, + None => { + assert(not_escape_sequence(s, m as int, e as int)); + assert(template_character(s, k as int, e as int, None::>, trv)); + lemma_tchar_src(s, k as int, e as int, None::>, trv); + }, + } + } push_range_utf16(&mut values.raw, src, k, e); + proof { + let trv = utf16_of(s, k as int, e as int); + match tvg { + Some(v) => { + assert(template_character(s, k as int, e as int, Some(v), trv)); + assert(tv_of(*values) =~= tv_concat(old_tv, Some(v))); + }, + None => { + assert(template_character(s, k as int, e as int, None::>, trv)); + assert(tv_of(*values) =~= tv_concat(old_tv, None::>)); + }, + } + assert(template_character(s, k as int, e as int, tvg, trv)); + assert(tv_of(*values) =~= tv_concat(old_tv, tvg)); + assert(values.raw@ =~= old_raw + trv); + assert(k + 1 < e <= src.len()); + assert(tesc_ok(s, k as int, e as int, old_values, *values)); + } Some(e) } @@ -1381,13 +3151,35 @@ type TemplateScan = (bool, Option>, Vec, usize); /// §12.9.6 a template component at `k` (`` ` `` for `NoSubstitutionTemplate` /// and `TemplateHead`, `}` for `TemplateMiddle` and `TemplateTail`): whether /// it closes with `` ` `` (rather than `${`), its TV and TRV, and its end. +/// +/// Proven: `Some((closes, tv, trv, e))` is a template component of the spec +/// ([`template_part`]) of the kind its opening and `closes` give +/// ([`tpart_kind`]), with that TV and TRV, and it is the only one the spec +/// derives from `k` (every derivation has this end and kind: the characters +/// stop at the first `` ` `` or `${`, since no `TemplateCharacter` starts +/// at either, [`lemma_part_unique`]); `None` means the spec derives none +/// ([`lemma_part_none`]). fn scan_template(src: &[u32], k: usize) -> (r: Option) requires k < src.len(), + src@[k as int] == 0x60 || src@[k as int] == 0x7D, ensures match r { - Some((_, _, _, e)) => k + 2 <= e <= src.len(), - None => true, + Some((closes, cooked, raw, e)) => k + 2 <= e <= src.len() && template_part( + src@, + k as int, + e as int, + tpart_kind(src@, k as int, closes), + match cooked { + Some(c) => Some(c@), + None => None, + }, + raw@, + ) && (forall|j: int, kind2: TemplateKind, tv2: Tv, trv2: Seq| + #[trigger] template_part(src@, k as int, j, kind2, tv2, trv2) ==> (j == e as int + && kind2 == tpart_kind(src@, k as int, closes))), + None => forall|j: int, kind2: TemplateKind, tv2: Tv, trv2: Seq| + !#[trigger] template_part(src@, k as int, j, kind2, tv2, trv2), }, { let mut values = TemplateValues { cooked: Vec::new(), defined: true, raw: Vec::new() }; @@ -1395,6 +3187,15 @@ fn scan_template(src: &[u32], k: usize) -> (r: Option) loop invariant k < j <= src.len(), + src@[k as int] == 0x60 || src@[k as int] == 0x7D, + j == k + 1 ==> (tv_of(values) == Some(Seq::::empty()) && values.raw@.len() == 0), + j > k + 1 ==> template_characters( + src@, + k as int + 1, + j as int, + tv_of(values), + values.raw@, + ), decreases src.len() - j, { let c = peek(src, j); @@ -1405,23 +3206,75 @@ fn scan_template(src: &[u32], k: usize) -> (r: Option) } else { j.saturating_add(2) }; + let ghost tvv = tv_of(values); + let ghost rawv = values.raw@; + let ghost cooked_view = values.cooked@; let cooked = if values.defined { Some(values.cooked) } else { None }; + proof { + let s = src@; + let kind = tpart_kind(s, k as int, closes); + assert(delim_at(s, j as int)); + if !closes { + assert(s[j as int] == 0x24 && s[j as int + 1] == 0x7B); + lemma_is_lit(s, j as int, j as int + 2, seq!['$', '{']); + assert(is_lit(s, j as int, j as int + 2, seq!['$', '{'])); + } + assert(match cooked { + Some(c) => Some(c@), + None => None, + } == tvv); + assert(template_part(s, k as int, end as int, kind, tvv, rawv)); + assert forall|j2: int, kind2: TemplateKind, tv2: Tv, trv2: Seq| + #[trigger] template_part(s, k as int, j2, kind2, tv2, trv2) implies (j2 + == end as int && kind2 == kind) by { + lemma_part_unique(s, k as int, j as int, j2, kind2, tv2, trv2); + } + } return Some((closes, cooked, values.raw, end)); } else if c == 0x5C { + let ghost ov = values; + let ghost s = src@; match scan_template_escape(src, j, &mut values) { Some(e) => { + proof { + assert(tesc_ok(s, j as int, e as int, ov, values)); + let (tv, trv) = choose|tv: Tv, trv: Seq| + #![trigger template_character(s, j as int, e as int, tv, trv)] + template_character(s, j as int, e as int, tv, trv) && tv_of(values) + =~= tv_concat(tv_of(ov), tv) && values.raw@ =~= ov.raw@ + trv; + lemma_chars_step(s, k as int + 1, j as int, e as int, tv_of(ov), ov.raw@, tv, trv); + assert(tv_of(values) == tv_concat(tv_of(ov), tv)); + assert(values.raw@ == ov.raw@ + trv); + } j = e; }, None => { + proof { + let s = src@; + assert(no_char(s, j as int)); + assert(!delim_at(s, j as int)); + assert forall|j2: int, kind2: TemplateKind, tv2: Tv, trv2: Seq| + !#[trigger] template_part(s, k as int, j2, kind2, tv2, trv2) by { + if template_part(s, k as int, j2, kind2, tv2, trv2) { + assert(j as int == k as int + 1 || exists| + tv1: Tv, + trv1: Seq, + | #[trigger] template_characters(s, k as int + 1, j as int, tv1, trv1)); + lemma_part_none(s, k as int, j as int, j2, kind2, tv2, trv2); + } + } + } return None; }, } } else if is_lt(c) { // LineTerminatorSequence: TV and TRV are its TRV. + let ghost ov = values; + let ghost s = src@; let e = line_terminator_sequence_end(src, j); let u: u16 = if c == 0x2028 || c == 0x2029 { c as u16 @@ -1430,14 +3283,57 @@ fn scan_template(src: &[u32], k: usize) -> (r: Option) }; values.cooked.push(u); values.raw.push(u); + proof { + let trv = line_terminator_sequence_trv(s, j as int, e as int); + assert(u == trv[0]); + assert(trv.len() == 1); + assert(template_character(s, j as int, e as int, Some(trv), trv)); + lemma_chars_step(s, k as int + 1, j as int, e as int, tv_of(ov), ov.raw@, Some(trv), trv); + assert(tv_of(values) == tv_concat(tv_of(ov), Some(trv))); + assert(values.raw@ == ov.raw@ + trv); + } j = e; } else if is_src(c) { // `$` [lookahead ≠ {], or SourceCharacter but not one of ` \ $ // LineTerminator: UTF16EncodeCodePoint of it. + let ghost ov = values; + let ghost s = src@; push_utf16(&mut values.cooked, c); push_utf16(&mut values.raw, c); + proof { + let trv = utf16_encode_code_point(c as int); + assert(c == s[j as int]); + assert(template_character(s, j as int, j as int + 1, Some(trv), trv)); + lemma_chars_step( + s, + k as int + 1, + j as int, + j as int + 1, + tv_of(ov), + ov.raw@, + Some(trv), + trv, + ); + assert(tv_of(values) == tv_concat(tv_of(ov), Some(trv))); + assert(values.raw@ == ov.raw@ + trv); + } j = j.saturating_add(1); } else { + proof { + let s = src@; + assert(no_char(s, j as int)); + assert(!delim_at(s, j as int)); + assert forall|j2: int, kind2: TemplateKind, tv2: Tv, trv2: Seq| + !#[trigger] template_part(s, k as int, j2, kind2, tv2, trv2) by { + if template_part(s, k as int, j2, kind2, tv2, trv2) { + assert(j as int == k as int + 1 || exists| + tv1: Tv, + trv1: Seq, + | #[trigger] template_characters(s, k as int + 1, j as int, tv1, trv1)); + lemma_part_none(s, k as int, j as int, j2, kind2, tv2, trv2); + } + } + } return None; } } @@ -1510,7 +3406,25 @@ type Candidate = (Element, Option); /// The candidate's element is a non-empty range at `pos`. pub open spec fn candidate_ok(src: Seq, pos: usize, c: (Element, Option)) -> bool { - c.0.start == pos && pos < c.0.end <= src.len() && value_shape_agrees(c.0) + c.0.start == pos && pos < c.0.end <= src.len() && value_shape_agrees(c.0) && c.1 + != Some(LexError::NoInputElement) && (c.1 == Some(LexError::IdentifierAfterNumericLiteral) + ==> c.0.kind == ElementKind::NumericLiteral) && (c.0.kind == ElementKind::NumericLiteral + ==> ((c.1 == Some(LexError::IdentifierAfterNumericLiteral)) + == numeric_literal_follow_error(src, c.0.end as int))) && (c.1 == Some( + LexError::InvalidIdentifierEscape) ==> (c.0.kind == ElementKind::IdentifierName + || c.0.kind == ElementKind::PrivateIdentifier)) && (c.0.kind + == ElementKind::IdentifierName ==> ((c.1 == Some(LexError::InvalidIdentifierEscape)) + == !identifier_name_valid(src, pos as int, c.0.end as int))) && (c.0.kind + == ElementKind::PrivateIdentifier ==> ((c.1 == Some(LexError::InvalidIdentifierEscape)) + == !identifier_name_valid(src, pos as int + 1, c.0.end as int))) +} + +/// The end of a candidate, or `pos` if there is none. +pub open spec fn cand_end(pos: usize, r: Option) -> int { + match r { + Some(c) => c.0.end as int, + None => pos as int, + } } /// [`candidate_ok`], and [`kind_agrees_proven`] in the goal. @@ -1521,7 +3435,10 @@ pub open spec fn candidate_agrees( pos: usize, c: (Element, Option), ) -> bool { - candidate_ok(src, pos, c) && kind_agrees_proven(goal, html, src, c.0) + candidate_ok(src, pos, c) && kind_agrees_proven(goal, html, src, c.0) && value_agrees_proven( + src, + c.0, + ) } /// The longer of two candidates (the first on a tie). @@ -1534,6 +3451,8 @@ fn longer(a: Option, b: Option) -> (r: Option) }, None => r == b, }, + a is Some ==> r is Some && r.unwrap().0.end >= a.unwrap().0.end, + b is Some ==> r is Some && r.unwrap().0.end >= b.unwrap().0.end, { match (a, b) { (Some(x), Some(y)) => { @@ -1553,223 +3472,1294 @@ fn longer(a: Option, b: Option) -> (r: Option) /// Annex B.1.1's `SingleLineDelimitedComment`, or its `MultiLineComment` /// with an optional `HTMLCloseComment` after the `*/` when the comment /// holds a `LineTerminator`). +/// +/// Proven: the candidate is a `Comment` of the spec ([`comment`], with +/// `html`'s alternatives), the longest from `pos`; `None` means there is +/// none. fn lex_slash_comment(src: &[u32], pos: usize, html: bool) -> (r: Option) requires pos < src.len(), + src@[pos as int] == 0x2F, ensures match r { - Some(c) => candidate_ok(src@, pos, c) && c.0.kind == ElementKind::Comment, - None => true, + Some(c) => candidate_ok(src@, pos, c) && c.0.kind == ElementKind::Comment && comment( + src@, + pos as int, + c.0.end as int, + html, + ) && forall|j: int| + j <= src.len() && #[trigger] comment(src@, pos as int, j, html) ==> j + <= c.0.end, + None => forall|j: int| !#[trigger] comment(src@, pos as int, j, html), }, { let c1 = peek(src, pos.saturating_add(1)); if c1 == 0x2F { let e = scan_line_chars(src, pos.saturating_add(2)); + proof { + // `//` and no LineTerminator: `SingleLineComment`; and it cannot + // be longer, since `src[e]` is a LineTerminator or past the end. + assert forall|j: int| #[trigger] comment(src@, pos as int, j, html) <==> comment( + src@, + pos as int, + j, + false, + ) by { + lemma_html_cm_slash_slash(src@, pos as int, j, html); + } + lemma_single_line_comment_equiv(src@, pos as int, e as int); + lemma_comment_plain_equiv(src@, pos as int, e as int); + assert(ind_single_line_comment(src@, pos as int, e as int)); + assert forall|j: int| j <= src.len() && #[trigger] comment(src@, pos as int, j, html) + implies j <= e by { + lemma_comment_plain_equiv(src@, pos as int, j); + lemma_single_line_comment_equiv(src@, pos as int, j); + if j > e { + assert(ind_single_line_comment(src@, pos as int, j) || ind_multi_line_comment( + src@, + pos as int, + j, + )); + if ind_single_line_comment(src@, pos as int, j) { + assert(ind_src_cp(src@[e as int]) && !ind_lt_cp(src@[e as int])); + } else { + assert(src@[pos as int + 1] == 0x2A); + } + } + } + } Some((plain(ElementKind::Comment, pos, e), None)) } else if c1 == 0x2A { match scan_block_comment(src, pos.saturating_add(2), true) { Some((e, lt)) => { - let end = if html && lt { - match scan_html_close(src, e) { - Some(f) => f, - None => e, - } + let hc = if html && lt { + scan_html_close(src, e) } else { - e + None + }; + let end = match hc { + Some(f) => f, + None => e, }; + proof { + if !html { + assert(end == e); + lemma_multi_line_comment_equiv(src@, pos as int, e as int); + lemma_comment_plain_equiv(src@, pos as int, e as int); + assert(ind_multi_line_comment(src@, pos as int, e as int)); + assert forall|j: int| + j <= src.len() && #[trigger] comment(src@, pos as int, j, html) + implies j <= e by { + lemma_comment_plain_equiv(src@, pos as int, j); + lemma_multi_line_comment_equiv(src@, pos as int, j); + lemma_single_line_comment_equiv(src@, pos as int, j); + if ind_multi_line_comment(src@, pos as int, j) { + assert(block_close_ok(src@, pos as int + 2, j, true)); + } + } + } else { + // Annex B.1.1: a delimited comment ends at the `*/`; a + // multi-line one ends there, or after an `HTMLCloseComment`. + assert forall|j: int| + #[trigger] comment(src@, pos as int, j, true) <==> (if lt { + e <= j && j <= src.len() && opt( + e as int, + j, + html_close_comment(src@, e as int, j), + ) + } else { + j == e + }) by { + lemma_html_cm_slash_star(src@, pos as int, e as int, j); + } + if lt { + match hc { + Some(f) => { + assert(e < f); + assert(opt( + e as int, + f as int, + html_close_comment(src@, e as int, f as int), + )); + }, + None => { + assert(opt( + e as int, + e as int, + html_close_comment(src@, e as int, e as int), + )); + }, + } + } + assert forall|j: int| + j <= src.len() && #[trigger] comment(src@, pos as int, j, true) + implies j <= end by { + if lt && j != e { + assert(html_close_comment(src@, e as int, j)); + } + } + } + } Some((plain(ElementKind::Comment, pos, end), None)) }, - None => None, + None => { + proof { + if html { + assert forall|j: int| !#[trigger] comment(src@, pos as int, j, true) by { + lemma_html_cm_slash_star_none(src@, pos as int, j); + } + } else { + assert forall|j: int| !#[trigger] comment(src@, pos as int, j, false) by { + lemma_comment_plain_equiv(src@, pos as int, j); + lemma_multi_line_comment_equiv(src@, pos as int, j); + lemma_single_line_comment_equiv(src@, pos as int, j); + if ind_multi_line_comment(src@, pos as int, j) { + assert(block_close_ok(src@, pos as int + 2, j, true)); + } + } + } + } + None + }, } } else { + proof { + assert forall|j: int| !#[trigger] comment(src@, pos as int, j, html) by { + if html { + lemma_html_cm_slash_other(src@, pos as int, j); + } else { + lemma_comment_plain_equiv(src@, pos as int, j); + lemma_multi_line_comment_equiv(src@, pos as int, j); + lemma_single_line_comment_equiv(src@, pos as int, j); + } + } + } None } } -/// Every alternative of `goal` that can start with the code point at -/// `pos`, the longest of them. -fn lex_common(src: &[u32], pos: usize, goal: Goal, html: bool) -> (r: Option) +/// `Comment` of §12.4 (not Annex B's) is a `MultiLineComment` or a +/// `SingleLineComment`, in the closed forms of `independent.rs`. +proof fn lemma_comment_plain_equiv(s: Seq, i: int, j: int) + ensures + comment(s, i, j, false) <==> (ind_multi_line_comment(s, i, j) || ind_single_line_comment( + s, + i, + j, + )), +{ + lemma_multi_line_comment_equiv(s, i, j); + lemma_single_line_comment_equiv(s, i, j); +} + +/// What the dispatch of [`lex_common`] proves of each branch: the candidate +/// agrees with the spec for its kind, no derivation of another proven kind +/// is longer ([`pk_bounded`]), and no numeric literal starts at `pos` unless +/// the candidate is one. +pub open spec fn lex_result_ok( + src: Seq, + pos: usize, + goal: Goal, + html: bool, + r: Option, +) -> bool { + (match r { + Some(c) => candidate_agrees(goal, html, src, pos, c), + None => true, + }) && pk_bounded(goal, html, src, pos as int, cand_end(pos, r)) && ((r is None + || r.unwrap().0.kind != ElementKind::NumericLiteral) ==> numeric_absent(src, pos as int)) + && ((r is None || r.unwrap().0.kind != ElementKind::IdentifierName) ==> ident_absent( + src, + pos as int, + )) && ((r is None || r.unwrap().0.kind != ElementKind::PrivateIdentifier) ==> private_absent( + src, + pos as int, + )) +} + +/// What the dispatch proves of Annex B.1.1's `Comment`, which is not a +/// proven kind ([`kind_proven`]): with `html`, a candidate that is a +/// `Comment` is a [`comment`] of the spec (Annex B's), and no [`comment`] +/// from `pos` is longer than the candidate. (`Comment`s that only the start +/// of the source text has, `HTMLCloseComment`, are [`lex_start`]'s.) +pub open spec fn cm_ok(src: Seq, pos: usize, html: bool, r: Option) -> bool { + html ==> ((match r { + Some(c) => c.0.kind == ElementKind::Comment ==> comment( + src, + pos as int, + c.0.end as int, + true, + ), + None => true, + }) && forall|j: int| + cand_end(pos, r) < j <= src.len() ==> !#[trigger] comment(src, pos as int, j, true)) +} + +/// The code point at `pos` cannot start one of Annex B's `Comment`s: it is +/// not `/`, not a `LineTerminator`, and ` SingleLineCommentChars?`, and its +//! parts are existentially split. Here: +//! +//! - [`lemma_sl_chars`] and [`lemma_sldc_equiv`] give a `SingleLine +//! DelimitedComment` a closed form, [`block_close_ok`] with no line +//! terminator (so its end is the first `*/`, [`lemma_block_close_unique`]); +//! - [`hc_pre`] is the code before the `-->` of an `HTMLCloseComment`: white +//! space and delimited comments, one after the other. It is tied to the +//! spec's split by [`lemma_html_close_equiv`], and shown deterministic: +//! whatever the split, the `-->` is at one place ([`lemma_hc_stuck`], +//! which says where a scan that cannot go on is); +//! - [`lemma_annex_b_ml`] gives Annex B's `MultiLineComment` (which is +//! `/* FirstCommentLine? LineTerminator MultiLineCommentChars? */ +//! HTMLCloseComment?`) its closed form: it ends at the first `*/` and holds +//! a line terminator before it, and then an `HTMLCloseComment` may follow; +//! - the `lemma_html_cm_*` lemmas say which alternatives of Annex B's +//! `Comment` can start with which code points. +//! +//! Nothing here changes [`super::spec`]; these are facts about it. + +#[cfg(verus_only)] +use super::equivalence::{lemma_comment_chars, lemma_is_lit, no_star_slash}; +#[cfg(verus_only)] +use super::exec::block_close_ok; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every code point of `s[a..b)` is a `SourceCharacter` that is not a +/// `LineTerminator`. +pub open spec fn no_lt_src(s: Seq, a: int, b: int) -> bool { + forall|k: int| a <= k < b ==> is_not_line_terminator(#[trigger] s[k]) +} + +/// `-->` starts at `b` (and is inside the source text). +pub open spec fn arrow_at(s: Seq, b: int) -> bool { + 0 <= b && b + 3 <= s.len() && s[b] == 0x2D && s[b + 1] == 0x2D && s[b + 2] == 0x3E +} + +/// `SingleLineDelimitedCommentChars` is "no `*/` inside, no line terminator", +/// and `SingleLinePostAsteriskCommentChars` is the same after a `*`: the first +/// code point is not `/`. Both on a non-empty range, by induction on its +/// length (as for `MultiLineCommentChars`, [`lemma_comment_chars`]). +pub proof fn lemma_sl_chars(s: Seq, a: int, b: int) + ensures + single_line_delimited_comment_chars(s, a, b) <==> (0 <= a && a < b && b <= s.len() + && no_lt_src(s, a, b) && no_star_slash(s, a, b)), + single_line_post_asterisk_comment_chars(s, a, b) <==> (0 <= a && a < b && b <= s.len() + && no_lt_src(s, a, b) && s[a] != 0x2F && no_star_slash(s, a, b)), + decreases b - a, +{ + if 0 <= a && a + 1 < b && b <= s.len() { + lemma_sl_chars(s, a + 1, b); + assert(no_star_slash(s, a, b) <==> (no_star_slash(s, a + 1, b) && !(s[a] == 0x2A && s[a + + 1] == 0x2F))) by { + if no_star_slash(s, a, b) { + assert forall|k: int| a + 1 <= k < b - 1 implies !(#[trigger] s[k] == 0x2A && s[k + + 1] == 0x2F) by { + assert(!(s[k] == 0x2A && s[k + 1] == 0x2F)); + } + } + if no_star_slash(s, a + 1, b) && !(s[a] == 0x2A && s[a + 1] == 0x2F) { + assert forall|k: int| a <= k < b - 1 implies !(#[trigger] s[k] == 0x2A && s[k + + 1] == 0x2F) by { + if k > a { + assert(!(s[k] == 0x2A && s[k + 1] == 0x2F)); + } + } + } + } + assert(no_lt_src(s, a, b) <==> (is_not_line_terminator(s[a]) && no_lt_src(s, a + 1, b))) + by { + if no_lt_src(s, a + 1, b) && is_not_line_terminator(s[a]) { + assert forall|k: int| a <= k < b implies is_not_line_terminator(#[trigger] s[k]) by { + if k > a { + assert(is_not_line_terminator(s[k])); + } + } + } + } + } + if 0 <= a && a + 1 == b && b <= s.len() { + assert(no_star_slash(s, a, b)); + if is_not_line_terminator(s[a]) { + assert(no_lt_src(s, a, b)) by { + assert forall|k: int| a <= k < b implies is_not_line_terminator(#[trigger] s[k]) by { + assert(k == a); + } + } + } + if no_lt_src(s, a, b) { + assert(is_not_line_terminator(s[a])); + } + } +} + +/// Annex B's `SingleLineDelimitedComment` is `/*` and the first `*/` with no +/// `LineTerminator` and only `SourceCharacter`s between ([`block_close_ok`] +/// without `multi_line`). +pub proof fn lemma_sldc_equiv(s: Seq, i: int, j: int) + ensures + single_line_delimited_comment(s, i, j) <==> (0 <= i && i + 4 <= j && j <= s.len() && s[i] + == 0x2F && s[i + 1] == 0x2A && block_close_ok(s, i + 2, j, false)), +{ + lemma_is_lit(s, i, i + 2, seq!['/', '*']); + lemma_is_lit(s, j - 2, j, seq!['*', '/']); + if 0 <= i && i + 4 <= j && j <= s.len() { + if i + 2 < j - 2 { + lemma_sl_chars(s, i + 2, j - 2); + } + if single_line_delimited_comment(s, i, j) { + assert(block_close_ok(s, i + 2, j, false)) by { + assert forall|m: int| i + 2 <= m < j - 2 implies (#[trigger] s[m] <= 0x10FFFF + && !(s[m] == 0x2A && s[m + 1] == 0x2F) && !is_line_terminator(s[m])) by { + assert(no_lt_src(s, i + 2, j - 2)); + if m < j - 3 { + assert(no_star_slash(s, i + 2, j - 2)); + } else { + assert(s[j - 2] == 0x2A); + } + } + } + } + if s[i] == 0x2F && s[i + 1] == 0x2A && block_close_ok(s, i + 2, j, false) { + if i + 2 < j - 2 { + assert(no_lt_src(s, i + 2, j - 2)) by { + assert forall|k: int| i + 2 <= k < j - 2 implies is_not_line_terminator( + #[trigger] s[k], + ) by { + assert(s[k] <= 0x10FFFF && !is_line_terminator(s[k])); + } + } + assert(no_star_slash(s, i + 2, j - 2)) by { + assert forall|k: int| i + 2 <= k < j - 3 implies !(#[trigger] s[k] == 0x2A + && s[k + 1] == 0x2F) by { + assert(!(s[k] == 0x2A && s[k + 1] == 0x2F)); + } + } + } + } + } +} + +/// A block comment's closing `*/` is the first one. +pub proof fn lemma_block_close_unique(s: Seq, k: int, e1: int, e2: int, multi_line: bool) + requires + block_close_ok(s, k, e1, multi_line), + block_close_ok(s, k, e2, multi_line), + ensures + e1 == e2, +{ + if e1 < e2 { + assert(k <= e1 - 2 < e2 - 2); + assert(s[e1 - 2] == 0x2A && s[e1 - 1] == 0x2F); + } + if e2 < e1 { + assert(k <= e2 - 2 < e1 - 2); + assert(s[e2 - 2] == 0x2A && s[e2 - 1] == 0x2F); + } +} + +/// A closing `*/` that no line terminator is before is the closing `*/` of +/// the version that allows them. +pub proof fn lemma_block_close_single_to_multi(s: Seq, k: int, e: int) + requires + block_close_ok(s, k, e, false), + ensures + block_close_ok(s, k, e, true), +{ +} + +/// The `/*` of a delimited comment, and a delimited comment's end is unique. +pub proof fn lemma_sldc_start(s: Seq, i: int, a: int) + requires + single_line_delimited_comment(s, i, a), + ensures + 0 <= i, + i + 4 <= a, + a <= s.len(), + s[i] == 0x2F, + s[i + 1] == 0x2A, +{ + lemma_sldc_equiv(s, i, a); +} + +/// A delimited comment from `i` ends at one place. +pub proof fn lemma_sldc_unique(s: Seq, i: int, a1: int, a2: int) + requires + single_line_delimited_comment(s, i, a1), + single_line_delimited_comment(s, i, a2), + ensures + a1 == a2, +{ + lemma_sldc_equiv(s, i, a1); + lemma_sldc_equiv(s, i, a2); + lemma_block_close_unique(s, i + 2, a1, a2, false); +} + +// --------------------------------------------------------------------- +// The code before the `-->` of an HTMLCloseComment. +// --------------------------------------------------------------------- + +/// White space and delimited comments, one after the other, from `i` to `b`: +/// what an `HTMLCloseComment` has before its `-->`. +pub open spec fn hc_pre(s: Seq, i: int, b: int) -> bool + decreases b - i, +{ + 0 <= i && i <= b && b <= s.len() && (i == b || (white_space(s, i, i + 1) && hc_pre( + s, + i + 1, + b, + )) || exists|a: int| + #![trigger single_line_delimited_comment(s, i, a)] + i < a && a <= b && single_line_delimited_comment(s, i, a) && hc_pre(s, a, b)) +} + +/// What the first step of [`hc_pre`] is when it is not empty. +pub proof fn lemma_hc_first(s: Seq, i: int, b: int) + requires + hc_pre(s, i, b), + i < b, + ensures + (white_space(s, i, i + 1) && hc_pre(s, i + 1, b)) || (exists|a: int| + #![trigger single_line_delimited_comment(s, i, a)] + i < a && a <= b && single_line_delimited_comment(s, i, a) && hc_pre(s, a, b)), +{ +} + +/// A run of white space is [`hc_pre`]. +pub proof fn lemma_hc_ws_run(s: Seq, i: int, j: int) + requires + 0 <= i <= j <= s.len(), + forall|k: int| i <= k < j ==> #[trigger] white_space(s, k, k + 1), + ensures + hc_pre(s, i, j), + decreases j - i, +{ + if i < j { + assert(white_space(s, i, i + 1)); + lemma_hc_ws_run(s, i + 1, j); + } +} + +/// A delimited comment is [`hc_pre`]. +pub proof fn lemma_hc_sldc(s: Seq, i: int, a: int) + requires + single_line_delimited_comment(s, i, a), + ensures + hc_pre(s, i, a), +{ + lemma_sldc_start(s, i, a); + assert(hc_pre(s, a, a)); +} + +/// [`hc_pre`] is closed under concatenation. +pub proof fn lemma_hc_cat(s: Seq, i: int, m: int, j: int) + requires + hc_pre(s, i, m), + hc_pre(s, m, j), + ensures + hc_pre(s, i, j), + decreases m - i, +{ + if i < m { + if white_space(s, i, i + 1) { + lemma_hc_first_ws(s, i, m); + lemma_hc_cat(s, i + 1, m, j); + } else { + lemma_hc_first_sldc(s, i, m); + let a = choose|a: int| + i < a && a <= m && single_line_delimited_comment(s, i, a) && hc_pre(s, a, m); + lemma_hc_cat(s, a, m, j); + assert(i < a && a <= j && single_line_delimited_comment(s, i, a) && hc_pre(s, a, j)); + } + } +} + +/// A white space code point is not `/` or `-`. +pub proof fn lemma_ws_not_slash_dash(s: Seq, i: int) + requires + white_space(s, i, i + 1), + ensures + s[i] != 0x2F, + s[i] != 0x2D, +{ +} + +/// A non-empty [`hc_pre`] that starts with white space goes on after it. +pub proof fn lemma_hc_first_ws(s: Seq, i: int, b: int) + requires + hc_pre(s, i, b), + i < b, + white_space(s, i, i + 1), + ensures + hc_pre(s, i + 1, b), +{ + lemma_hc_first(s, i, b); + lemma_ws_not_slash_dash(s, i); + if !hc_pre(s, i + 1, b) { + let a = choose|a: int| + i < a && a <= b && single_line_delimited_comment(s, i, a) && hc_pre(s, a, b); + lemma_sldc_start(s, i, a); + assert(false); + } +} + +/// A non-empty [`hc_pre`] that does not start with white space starts with a +/// delimited comment. +pub proof fn lemma_hc_first_sldc(s: Seq, i: int, b: int) + requires + hc_pre(s, i, b), + i < b, + !white_space(s, i, i + 1), + ensures + exists|a: int| + #![trigger single_line_delimited_comment(s, i, a)] + i < a && a <= b && single_line_delimited_comment(s, i, a) && hc_pre(s, a, b), +{ + lemma_hc_first(s, i, b); +} + +/// The first step of a non-empty [`hc_pre`] starts with white space or `/*`. +pub proof fn lemma_hc_start(s: Seq, i: int, b: int) + requires + hc_pre(s, i, b), + i < b, + ensures + white_space(s, i, i + 1) || (s[i] == 0x2F && s[i + 1] == 0x2A), +{ + lemma_hc_first(s, i, b); + if !white_space(s, i, i + 1) { + let a = choose|a: int| + i < a && a <= b && single_line_delimited_comment(s, i, a) && hc_pre(s, a, b); + lemma_sldc_start(s, i, a); + } +} + +/// If `s[i..x)` and `s[i..y)` are both [`hc_pre`], so is `s[x..y)`: the +/// steps are determined by the code points that start them. +pub proof fn lemma_hc_prefix(s: Seq, i: int, x: int, y: int) + requires + hc_pre(s, i, x), + hc_pre(s, i, y), + x <= y, + ensures + hc_pre(s, x, y), + decreases x - i, +{ + if i < x { + if white_space(s, i, i + 1) { + lemma_hc_first_ws(s, i, x); + lemma_hc_first_ws(s, i, y); + lemma_hc_prefix(s, i + 1, x, y); + } else { + lemma_hc_first_sldc(s, i, x); + lemma_hc_first_sldc(s, i, y); + let a1 = choose|a: int| + i < a && a <= x && single_line_delimited_comment(s, i, a) && hc_pre(s, a, x); + let a2 = choose|a: int| + i < a && a <= y && single_line_delimited_comment(s, i, a) && hc_pre(s, a, y); + lemma_sldc_unique(s, i, a1, a2); + lemma_hc_prefix(s, a1, x, y); + } + } +} + +/// Where a scan of an `HTMLCloseComment` that is at `j` finds the `-->`. +/// The code before the `-->` is [`hc_pre`] to `j`, and the code point at `j` +/// is not white space and no delimited comment starts there (so the scan +/// cannot go on); then every `-->` that follows [`hc_pre`] code from `i` is +/// the one at `j` if there is one. +pub proof fn lemma_hc_stuck(s: Seq, i: int, j: int, b: int) + requires + hc_pre(s, i, j), + hc_pre(s, i, b), + arrow_at(s, b), + !white_space(s, j, j + 1), + forall|a: int| !single_line_delimited_comment(s, j, a), + ensures + b == j, +{ + if b < j { + lemma_hc_prefix(s, i, b, j); + lemma_hc_start(s, b, j); + if white_space(s, b, b + 1) { + lemma_ws_not_slash_dash(s, b); + } + } + if j < b { + lemma_hc_prefix(s, i, j, b); + lemma_hc_first_sldc(s, j, b); + let a = choose|a: int| + j < a && a <= b && single_line_delimited_comment(s, j, a) && hc_pre(s, a, b); + assert(false); + } +} + +/// The part of `HTMLCloseComment` after the `-->`: no line terminator up to +/// `j`. +pub open spec fn hc_tail(s: Seq, b: int, j: int) -> bool { + b + 3 <= j && j <= s.len() && no_lt_src(s, b + 3, j) +} + +/// A `WhiteSpaceSequence` is white space throughout. +pub proof fn lemma_wss_ws(s: Seq, i: int, j: int) + requires + white_space_sequence(s, i, j), + ensures + forall|k: int| i <= k < j ==> #[trigger] white_space(s, k, k + 1), + decreases j - i, +{ + if j > i + 1 { + lemma_wss_ws(s, i + 1, j); + } + assert forall|k: int| i <= k < j implies #[trigger] white_space(s, k, k + 1) by { + if k > i { + assert(white_space_sequence(s, i + 1, j)); + } + } +} + +/// A `SingleLineDelimitedCommentSequence` is [`hc_pre`]. +pub proof fn lemma_sldcs_hc(s: Seq, i: int, j: int) + requires + single_line_delimited_comment_sequence(s, i, j), + ensures + hc_pre(s, i, j), + decreases j - i, +{ + let (a, b) = choose|a: int, b: int| + i < a && a <= b && b <= j && single_line_delimited_comment(s, i, a) && opt( + a, + b, + white_space_sequence(s, a, b), + ) && opt(b, j, single_line_delimited_comment_sequence(s, b, j)); + lemma_hc_sldc(s, i, a); + if a < b { + lemma_wss_ws(s, a, b); + lemma_hc_ws_run(s, a, b); + } + assert(hc_pre(s, a, b)); + lemma_hc_cat(s, i, a, b); + if b < j { + lemma_sldcs_hc(s, b, j); + lemma_hc_cat(s, i, b, j); + } +} + +/// [`hc_pre`] is a split of the spec: `WhiteSpaceSequence?` then +/// `SingleLineDelimitedCommentSequence?`. +pub proof fn lemma_hc_split(s: Seq, i: int, b: int) + requires + hc_pre(s, i, b), + ensures + exists|a: int| + i <= a && a <= b && opt(i, a, white_space_sequence(s, i, a)) && opt( + a, + b, + single_line_delimited_comment_sequence(s, a, b), + ), + decreases b - i, +{ + if i == b { + assert(i <= b && opt(i, b, white_space_sequence(s, i, b)) && opt( + b, + b, + single_line_delimited_comment_sequence(s, b, b), + )); + } else { + if white_space(s, i, i + 1) { + lemma_hc_first_ws(s, i, b); + lemma_hc_split(s, i + 1, b); + let a = choose|a: int| + i + 1 <= a && a <= b && opt(i + 1, a, white_space_sequence(s, i + 1, a)) && opt( + a, + b, + single_line_delimited_comment_sequence(s, a, b), + ); + assert(nonempty(s, i, a)); + assert(opt(i, a, white_space_sequence(s, i, a))); + assert(i <= a && a <= b && opt(i, a, white_space_sequence(s, i, a)) && opt( + a, + b, + single_line_delimited_comment_sequence(s, a, b), + )); + } else { + lemma_hc_first_sldc(s, i, b); + let a1 = choose|a: int| + i < a && a <= b && single_line_delimited_comment(s, i, a) && hc_pre(s, a, b); + lemma_hc_split(s, a1, b); + let a2 = choose|a: int| + a1 <= a && a <= b && opt(a1, a, white_space_sequence(s, a1, a)) && opt( + a, + b, + single_line_delimited_comment_sequence(s, a, b), + ); + assert(nonempty(s, i, b)); + assert(i < a1 && a1 <= a2 && a2 <= b && single_line_delimited_comment(s, i, a1) && opt( + a1, + a2, + white_space_sequence(s, a1, a2), + ) && opt(a2, b, single_line_delimited_comment_sequence(s, a2, b))); + assert(single_line_delimited_comment_sequence(s, i, b)); + assert(opt(i, i, white_space_sequence(s, i, i))); + } + } +} + +/// `HTMLCloseComment` as a closed form: [`hc_pre`] code from `i` to a `-->` +/// at `b`, and then no line terminator to `j`. +pub proof fn lemma_html_close_equiv(s: Seq, i: int, j: int) + ensures + html_close_comment(s, i, j) <==> (0 <= i && i < j && j <= s.len() && exists|b: int| + #![trigger arrow_at(s, b)] + hc_pre(s, i, b) && arrow_at(s, b) && hc_tail(s, b, j)), +{ + if html_close_comment(s, i, j) { + let (a, b) = choose|a: int, b: int| html_close_comment_split(s, i, a, b, j); + lemma_is_lit(s, b, b + 3, seq!['-', '-', '>']); + if i < a { + lemma_wss_ws(s, i, a); + lemma_hc_ws_run(s, i, a); + } else { + assert(hc_pre(s, i, a)); + } + if a < b { + lemma_sldcs_hc(s, a, b); + } else { + assert(hc_pre(s, a, b)); + } + lemma_hc_cat(s, i, a, b); + assert(arrow_at(s, b)); + assert(hc_tail(s, b, j)) by { + if b + 3 < j { + assert(no_lt_src(s, b + 3, j)); + } + } + } + if 0 <= i && i < j && j <= s.len() { + if exists|b: int| #![trigger arrow_at(s, b)] hc_pre(s, i, b) && arrow_at(s, b) && hc_tail( + s, + b, + j, + ) { + let b = choose|b: int| + #![trigger arrow_at(s, b)] + hc_pre(s, i, b) && arrow_at(s, b) && hc_tail(s, b, j); + lemma_hc_split(s, i, b); + let a = choose|a: int| + i <= a && a <= b && opt(i, a, white_space_sequence(s, i, a)) && opt( + a, + b, + single_line_delimited_comment_sequence(s, a, b), + ); + lemma_is_lit(s, b, b + 3, seq!['-', '-', '>']); + assert(is_lit(s, b, b + 3, seq!['-', '-', '>'])); + if b + 3 < j { + assert(single_line_comment_chars(s, b + 3, j)); + } + assert(html_close_comment_split(s, i, a, b, j)); + } + } +} + +// --------------------------------------------------------------------- +// Annex B's MultiLineComment. +// --------------------------------------------------------------------- + +/// The first line terminator of a range that has one. +pub proof fn lemma_first_lt(s: Seq, lo: int, hi: int) + requires + contains_line_terminator(s, lo, hi), + ensures + exists|a: int| + lo <= a < hi && is_line_terminator(s[a]) && forall|k: int| + lo <= k < a ==> !is_line_terminator(#[trigger] s[k]), + decreases hi - lo, +{ + if is_line_terminator(s[lo]) { + assert(lo <= lo < hi && is_line_terminator(s[lo])); + } else { + let k = choose|k: int| lo <= k < hi && is_line_terminator(#[trigger] s[k]); + assert(lo + 1 <= k); + assert(contains_line_terminator(s, lo + 1, hi)); + lemma_first_lt(s, lo + 1, hi); + let a = choose|a: int| + lo + 1 <= a < hi && is_line_terminator(s[a]) && forall|k: int| + lo + 1 <= k < a ==> !is_line_terminator(#[trigger] s[k]); + assert forall|k: int| lo <= k < a implies !is_line_terminator(#[trigger] s[k]) by { + if k > lo { + assert(!is_line_terminator(s[k])); + } + } + } +} + +/// An Annex B `MultiLineComment` is `/*` to a closing `*/`, with a line +/// terminator before it. +pub proof fn lemma_annex_b_close(s: Seq, i: int, j: int) + requires + annex_b_multi_line_comment(s, i, j), + ensures + 0 <= i, + i + 1 < s.len(), + s[i] == 0x2F, + s[i + 1] == 0x2A, + exists|e: int| + #![trigger block_close_ok(s, i + 2, e, true)] + block_close_ok(s, i + 2, e, true) && contains_line_terminator(s, i + 2, e - 2) && e <= j + && j <= s.len() && opt(e, j, html_close_comment(s, e, j)), +{ + let (a, c) = choose|a: int, c: int| annex_b_multi_line_comment_split(s, i, a, c, j); + lemma_is_lit(s, i, i + 2, seq!['/', '*']); + lemma_is_lit(s, c, c + 2, seq!['*', '/']); + if i + 2 < a { + lemma_sl_chars(s, i + 2, a); + } + if a + 1 < c { + lemma_comment_chars(s, a + 1, c); + } + assert(block_close_ok(s, i + 2, c + 2, true)) by { + assert forall|m: int| i + 2 <= m < c implies (#[trigger] s[m] <= 0x10FFFF && !(s[m] == 0x2A + && s[m + 1] == 0x2F)) by { + if m < a { + assert(no_lt_src(s, i + 2, a)); + if m < a - 1 { + assert(no_star_slash(s, i + 2, a)); + } + } else if m > a { + assert(a + 1 < c); + if m < c - 1 { + assert(no_star_slash(s, a + 1, c)); + } + } + } + } + assert(contains_line_terminator(s, i + 2, c)) by { + assert(i + 2 <= a < c && is_line_terminator(s[a])); + } +} + +/// An Annex B `MultiLineComment` from a `/*` whose first closing `*/` ends +/// at `e` (the closing `*/` is the first one): it has a line terminator +/// before the `*/`, and then ends there or after an `HTMLCloseComment`. +pub proof fn lemma_annex_b_ml(s: Seq, i: int, e: int, j: int) + requires + 0 <= i, + i + 1 < s.len(), + s[i] == 0x2F, + s[i + 1] == 0x2A, + block_close_ok(s, i + 2, e, true), + ensures + annex_b_multi_line_comment(s, i, j) <==> (contains_line_terminator(s, i + 2, e - 2) && e + <= j && j <= s.len() && opt(e, j, html_close_comment(s, e, j))), +{ + if annex_b_multi_line_comment(s, i, j) { + lemma_annex_b_close(s, i, j); + let e2 = choose|e2: int| + #![trigger block_close_ok(s, i + 2, e2, true)] + block_close_ok(s, i + 2, e2, true) && contains_line_terminator(s, i + 2, e2 - 2) + && e2 <= j && j <= s.len() && opt(e2, j, html_close_comment(s, e2, j)); + lemma_block_close_unique(s, i + 2, e, e2, true); + } + if contains_line_terminator(s, i + 2, e - 2) && e <= j && j <= s.len() && opt( + e, + j, + html_close_comment(s, e, j), + ) { + lemma_first_lt(s, i + 2, e - 2); + let a = choose|a: int| + i + 2 <= a < e - 2 && is_line_terminator(s[a]) && forall|k: int| + i + 2 <= k < a ==> !is_line_terminator(#[trigger] s[k]); + let c = e - 2; + lemma_is_lit(s, i, i + 2, seq!['/', '*']); + lemma_is_lit(s, c, c + 2, seq!['*', '/']); + if i + 2 < a { + lemma_sl_chars(s, i + 2, a); + assert(no_lt_src(s, i + 2, a)) by { + assert forall|k: int| i + 2 <= k < a implies is_not_line_terminator( + #[trigger] s[k], + ) by { + assert(s[k] <= 0x10FFFF && !is_line_terminator(s[k])); + } + } + assert(no_star_slash(s, i + 2, a)) by { + assert forall|k: int| i + 2 <= k < a - 1 implies !(#[trigger] s[k] == 0x2A && s[k + + 1] == 0x2F) by { + assert(!(s[k] == 0x2A && s[k + 1] == 0x2F)); + } + } + } + if a + 1 < c { + lemma_comment_chars(s, a + 1, c); + assert(no_star_slash(s, a + 1, c)) by { + assert forall|k: int| a + 1 <= k < c - 1 implies !(#[trigger] s[k] == 0x2A && s[k + + 1] == 0x2F) by { + assert(!(s[k] == 0x2A && s[k + 1] == 0x2F)); + } + } + assert(crate::equivalence::all_source(s, a + 1, c)) by { + assert forall|k: int| a + 1 <= k < c implies #[trigger] s[k] <= 0x10FFFF by { + assert(s[k] <= 0x10FFFF); + } + } + } + assert(line_terminator(s, a, a + 1)); + assert(annex_b_multi_line_comment_split(s, i, a, c, j)); + assert(nonempty(s, i, j)); + } +} + +// --------------------------------------------------------------------- +// Which alternatives of Annex B's `Comment` start with which code point. +// --------------------------------------------------------------------- + +/// A `SingleLineHTMLCloseComment` starts with a line terminator, and is the +/// `LineTerminatorSequence`, then an `HTMLCloseComment`. +pub proof fn lemma_slhcc(s: Seq, i: int, a: int, j: int) + requires + line_terminator_sequence(s, i, a), + forall|a2: int| line_terminator_sequence(s, i, a2) ==> a2 == a, + ensures + single_line_html_close_comment(s, i, j) <==> (a < j && j <= s.len() && html_close_comment( + s, + a, + j, + )), +{ + if a < j && j <= s.len() && html_close_comment(s, a, j) { + assert(nonempty(s, i, j)); + assert(i < a && a < j && line_terminator_sequence(s, i, a) && html_close_comment(s, a, j)); + } +} + +/// A `SingleLineHTMLCloseComment` starts with a line terminator. +pub proof fn lemma_slhcc_start(s: Seq, i: int, j: int) + requires + single_line_html_close_comment(s, i, j), + ensures + 0 <= i < s.len(), + is_line_terminator(s[i]), +{ + let a = choose|a: int| + i < a && a < j && line_terminator_sequence(s, i, a) && html_close_comment(s, a, j); +} + +/// Annex B's `Comment` that starts with `//` is the `SingleLineComment`, as +/// in §12.4. +pub proof fn lemma_html_cm_slash_slash(s: Seq, i: int, j: int, html: bool) + requires + 0 <= i, + i + 1 < s.len(), + s[i] == 0x2F, + s[i + 1] == 0x2F, + ensures + comment(s, i, j, html) <==> comment(s, i, j, false), +{ + lemma_is_lit(s, i, i + 2, seq!['/', '*']); + lemma_sldc_equiv(s, i, j); + if html { + if annex_b_multi_line_comment(s, i, j) { + lemma_annex_b_close(s, i, j); + } + if single_line_html_close_comment(s, i, j) { + lemma_slhcc_start(s, i, j); + } + lemma_is_lit(s, i, i + 4, seq!['<', '!', '-', '-']); + } + crate::equivalence::lemma_multi_line_comment_equiv(s, i, j); +} + +/// Annex B's `Comment` that starts with `/*`, whose first closing `*/` ends +/// at `e`: a delimited comment (no line terminator), or a `MultiLineComment` +/// (a line terminator) and an optional `HTMLCloseComment`. +pub proof fn lemma_html_cm_slash_star(s: Seq, i: int, e: int, j: int) + requires + 0 <= i, + i + 1 < s.len(), + s[i] == 0x2F, + s[i + 1] == 0x2A, + block_close_ok(s, i + 2, e, true), + ensures + comment(s, i, j, true) <==> (if contains_line_terminator(s, i + 2, e - 2) { + e <= j && j <= s.len() && opt(e, j, html_close_comment(s, e, j)) + } else { + j == e + }), +{ + lemma_annex_b_ml(s, i, e, j); + lemma_sldc_equiv(s, i, j); + crate::equivalence::lemma_single_line_comment_equiv(s, i, j); + lemma_is_lit(s, i, i + 4, seq!['<', '!', '-', '-']); + if single_line_html_close_comment(s, i, j) { + lemma_slhcc_start(s, i, j); + } + if single_line_delimited_comment(s, i, j) { + lemma_block_close_single_to_multi(s, i + 2, j); + lemma_block_close_unique(s, i + 2, e, j, true); + assert(!contains_line_terminator(s, i + 2, e - 2)) by { + if contains_line_terminator(s, i + 2, e - 2) { + let k = choose|k: int| i + 2 <= k < e - 2 && is_line_terminator(#[trigger] s[k]); + } + } + } + if !contains_line_terminator(s, i + 2, e - 2) && j == e { + assert(block_close_ok(s, i + 2, e, false)) by { + assert forall|m: int| i + 2 <= m < e - 2 implies (#[trigger] s[m] <= 0x10FFFF && !(s[m] + == 0x2A && s[m + 1] == 0x2F) && !is_line_terminator(s[m])) by { + assert(!is_line_terminator(s[m])); + } + } + } +} + +/// Annex B's `Comment` that starts with `/*`, when no closing `*/` is +/// reachable, is none. +pub proof fn lemma_html_cm_slash_star_none(s: Seq, i: int, j: int) + requires + 0 <= i, + i + 1 < s.len(), + s[i] == 0x2F, + s[i + 1] == 0x2A, + forall|e: int| !#[trigger] block_close_ok(s, i + 2, e, true), + ensures + !comment(s, i, j, true), +{ + lemma_sldc_equiv(s, i, j); + if single_line_delimited_comment(s, i, j) { + lemma_block_close_single_to_multi(s, i + 2, j); + } + if annex_b_multi_line_comment(s, i, j) { + lemma_annex_b_close(s, i, j); + } + crate::equivalence::lemma_single_line_comment_equiv(s, i, j); + lemma_is_lit(s, i, i + 4, seq!['<', '!', '-', '-']); + if single_line_html_close_comment(s, i, j) { + lemma_slhcc_start(s, i, j); + } +} + +/// `SingleLineHTMLOpenComment`: ``). +pub proof fn lemma_html_close_start(s: Seq, i: int, j: int) + requires + html_close_comment(s, i, j), + ensures + 0 <= i < s.len(), + white_space(s, i, i + 1) || s[i] == 0x2F || s[i] == 0x2D, +{ + lemma_html_close_equiv(s, i, j); + let b = choose|b: int| + #![trigger arrow_at(s, b)] + hc_pre(s, i, b) && arrow_at(s, b) && hc_tail(s, b, j); + if i < b { + lemma_hc_start(s, i, b); + } +} + +/// An `HTMLCloseComment` starts with white space, with `/*`, or with its +/// `-->`, and is as long as that needs. +pub proof fn lemma_html_close_shape(s: Seq, i: int, j: int) + requires + html_close_comment(s, i, j), + ensures + 0 <= i < s.len(), + white_space(s, i, i + 1) || (i + 1 < s.len() && s[i] == 0x2F && s[i + 1] == 0x2A && i + 2 + <= j) || (arrow_at(s, i) && i + 3 <= j), +{ + lemma_html_close_equiv(s, i, j); + let b = choose|b: int| + #![trigger arrow_at(s, b)] + hc_pre(s, i, b) && arrow_at(s, b) && hc_tail(s, b, j); + if i < b { + lemma_hc_start(s, i, b); + } +} + +/// Annex B's `Comment` starts with `//` or `/*`, or with `` is a comment only at the start of a line (Annex B.1.1); here it is \ +the tokens `--` and `>`, which only the parser rejects"; +const DOT: &str = "`.` then an identifier: valid tokens, which only the parser rejects"; + +/// Negative tests of [`LEXICAL_DIRS`] that the lexer does not reject, and +/// why: their error is not a lexical one. +const NEGATIVE_XFAIL: &[(&str, &str)] = &[ + ("language/comments/multi-line-html-close-extra.js", HTML), + ( + "language/comments/single-line-html-close-without-lt.js", + HTML, + ), + ("language/line-terminators/S7.3_A3.2_T1.js", AFTER_LT), + ( + "language/line-terminators/invalid-comment-single-cr.js", + AFTER_LT, + ), + ( + "language/line-terminators/invalid-comment-single-lf.js", + AFTER_LT, + ), + ( + "language/line-terminators/invalid-comment-single-ls.js", + AFTER_LT, + ), + ( + "language/line-terminators/invalid-comment-single-ps.js", + AFTER_LT, + ), + ("language/literals/numeric/7.8.3-1gs.js", STRICT), + ("language/literals/numeric/7.8.3-2gs.js", STRICT), + ( + "language/literals/numeric/legacy-octal-integer-strict.js", + STRICT, + ), + ( + "language/literals/numeric/legacy-octal-integery-000-strict.js", + STRICT, + ), + ( + "language/literals/numeric/legacy-octal-integery-005-strict.js", + STRICT, + ), + ( + "language/literals/numeric/legacy-octal-integery-01-strict.js", + STRICT, + ), + ( + "language/literals/numeric/legacy-octal-integery-010-strict.js", + STRICT, + ), + ( + "language/literals/numeric/legacy-octal-integery-06-strict.js", + STRICT, + ), + ( + "language/literals/numeric/legacy-octal-integery-07-strict.js", + STRICT, + ), + ( + "language/literals/numeric/non-octal-decimal-integer-strict.js", + STRICT, + ), + ( + "language/literals/numeric/numeric-separators/numeric-separator-literal-dot-nsl-ep-err.js", + DOT, + ), + ( + "language/literals/numeric/numeric-separators/numeric-separator-literal-dot-nsl-err.js", + DOT, + ), + ("language/literals/string/S7.8.4_A4.3_T1.js", STRICT), + ("language/literals/string/S7.8.4_A4.3_T2.js", STRICT), + ( + "language/literals/string/legacy-non-octal-escape-sequence-1-strict-explicit-pragma.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-2-strict-explicit-pragma.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-3-strict-explicit-pragma.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-4-strict-explicit-pragma.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-5-strict-explicit-pragma.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-6-strict-explicit-pragma.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-7-strict-explicit-pragma.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-8-strict-explicit-pragma.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-8-strict.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-9-strict-explicit-pragma.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-9-strict.js", + STRICT, + ), + ( + "language/literals/string/legacy-non-octal-escape-sequence-strict.js", + STRICT, + ), + ( + "language/literals/string/legacy-octal-escape-sequence-prologue-strict.js", + STRICT, + ), + ( + "language/literals/string/legacy-octal-escape-sequence-strict.js", + STRICT, + ), + ("language/types/string/S8.4_A13_T3.js", ADJACENT), + ("language/types/string/S8.4_A14_T3.js", ADJACENT), +]; + +fn test_root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data/test262/test") +} + +fn harness_root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data/test262/harness") +} + +fn read_file(path: &Path) -> Result, String> { + let text = std::fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?; + Ok(text.chars().map(u32::from).collect()) +} + +/// Annex B.1.1 applies to scripts, not modules. +fn html(case: &Case) -> bool { + !case.flags.contains(&"module") +} + +/// Whether every element from 0 in `goal`, resuming one code point after an +/// error, starts where the last ended and the last ends at the end: the +/// elements and the skipped code points tile the source text. +fn tiles(src: &[u32], goal: Goal, html: bool) -> bool { + let mut pos: usize = 0; + loop { + match next_input_element(src, pos, goal, html) { + Ok(Step::Eof) => return pos == src.len(), + Ok(Step::Element(el)) => { + if !(el.start == pos && pos < el.end && el.end <= src.len()) { + return false; + } + pos = el.end; + } + Err(_) => { + if pos >= src.len() { + return false; + } + pos = pos.saturating_add(1); + } + } + } +} + +/// The names after which a `/` starts a regular expression. +const BEFORE_EXPRESSION: &[&str] = &[ + "return", + "typeof", + "instanceof", + "in", + "of", + "new", + "delete", + "void", + "throw", + "case", + "do", + "else", + "yield", + "await", + "extends", +]; + +/// Whether a `/` after `el` starts a regular expression, as a guess. +fn regexp_after(el: &Element, src: &[u32]) -> bool { + let text: String = src + .get(el.start..el.end) + .unwrap_or(&[]) + .iter() + .filter_map(|&c| char::from_u32(c)) + .collect(); + match el.kind { + ElementKind::Punctuator => !matches!(text.as_str(), ")" | "]"), + ElementKind::RightBracePunctuator + | ElementKind::DivPunctuator + | ElementKind::Template(TemplateKind::Head | TemplateKind::Middle) => true, + ElementKind::IdentifierName => match &el.value { + Value::Name { + escaped: false, + code_points, + } => { + let name: String = code_points + .iter() + .filter_map(|&c| char::from_u32(c)) + .collect(); + BEFORE_EXPRESSION.contains(&name.as_str()) + } + _ => false, + }, + _ => false, + } +} + +/// Lexes `src` to the end with `next_token`, guessing each goal symbol from +/// the token before (and a stack of open braces, to know when a `}` closes a +/// template substitution). `true` if no error occurs and every token is +/// non-empty and inside the source. +fn lexes(src: &[u32], html: bool) -> bool { + let mut pos = 0; + let mut regexp = true; + let mut braces: Vec = Vec::new(); + loop { + let in_template = braces.last() == Some(&true); + let goal = if pos == 0 { + Goal::InputElementHashbangOrRegExp + } else { + match (regexp, in_template) { + (true, true) => Goal::InputElementRegExpOrTemplateTail, + (false, true) => Goal::InputElementTemplateTail, + (true, false) => Goal::InputElementRegExp, + (false, false) => Goal::InputElementDiv, + } + }; + match next_token(src, pos, goal, html) { + Ok(Next::Eof { .. }) => return true, + Ok(Next::Token(tok)) => { + let el = &tok.element; + if !(pos <= el.start && el.start < el.end && el.end <= src.len()) { + return false; + } + match el.kind { + ElementKind::Punctuator if src.get(el.start) == Some(&u32::from('{')) => { + braces.push(false); + } + ElementKind::Template(TemplateKind::Head) => braces.push(true), + ElementKind::RightBracePunctuator + | ElementKind::Template(TemplateKind::Tail) => { + braces.pop(); + } + _ => {} + } + regexp = regexp_after(el, src); + pos = el.end; + } + Err(_) => return false, + } + } +} + +/// The `.js` files below `dir`, sorted. +fn js_files(dir: &Path, out: &mut Vec) -> Result<(), String> { + let mut entries: Vec = std::fs::read_dir(dir) + .map_err(|e| format!("{}: {e}", dir.display()))? + .filter_map(|e| e.ok().map(|e| e.path())) + .collect(); + entries.sort(); + for p in entries { + if p.is_dir() { + js_files(&p, out)?; + } else if p.extension().is_some_and(|e| e == "js") { + out.push(p); + } + } + Ok(()) +} + +/// One suite's tally. +#[derive(Default)] +struct Tally { + pass: u64, + xfail: u64, + fail: Vec, +} + +fn report(name: &str, t: &Tally, total: usize) { + println!( + "clazel-conformance: suite=\"{name}\" pass={} xfail={} fail={} total={total}", + t.pass, + t.xfail, + t.fail.len() + ); +} + +fn check(name: &str, t: &Tally, total: usize) { + report(name, t, total); + assert_eq!( + t.pass + .saturating_add(t.xfail) + .saturating_add(u64::try_from(t.fail.len()).unwrap_or(u64::MAX)), + u64::try_from(total).unwrap_or(u64::MAX), + "{name}: the counts do not add up" + ); + assert!( + t.fail.is_empty(), + "{name}: {} unexpected failures:\n{}", + t.fail.len(), + t.fail.join("\n") + ); +} + +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn positive_tests_lex() -> Result<(), String> { + assert_eq!(TREE_SHA256.len(), 64); + let cases: Vec<&Case> = TEST262.iter().filter(|c| c.negative.is_none()).collect(); + let mut t = Tally::default(); + let features: std::collections::BTreeSet<&str> = cases + .iter() + .flat_map(|c| c.features.iter().copied()) + .collect(); + let with_includes = cases.iter().filter(|c| !c.includes.is_empty()).count(); + println!( + "{} positive tests, {} features, {with_includes} with harness includes", + cases.len(), + features.len() + ); + for case in &cases { + if let Expect::Fail(reason) = case.expect { + assert!(!reason.is_empty()); + t.xfail += 1; + continue; + } + let src = read_file(&test_root().join(case.path))?; + if lexes(&src, html(case)) { + t.pass += 1; + } else { + t.fail.push(case.path.to_owned()); + } + } + check("Test262 language/ positive tests lex", &t, cases.len()); + Ok(()) +} + +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn lexical_negative_tests_rejected() -> Result<(), String> { + let cases: Vec<&Case> = TEST262 + .iter() + .filter(|c| { + matches!(c.negative, Some(("parse", _))) + && LEXICAL_DIRS.iter().any(|d| c.path.starts_with(d)) + }) + .collect(); + // Every listed expected failure is a test of the suite, once. + for (i, (path, reason)) in NEGATIVE_XFAIL.iter().enumerate() { + assert!( + cases.iter().any(|c| c.path == *path), + "{path} is not a lexical negative test" + ); + assert!(!reason.is_empty()); + assert!( + NEGATIVE_XFAIL.iter().take(i).all(|(p, _)| p != path), + "{path} is listed twice" + ); + } + let mut t = Tally::default(); + for case in &cases { + let listed = NEGATIVE_XFAIL.iter().any(|(p, _)| *p == case.path); + let marked = matches!(case.expect, Expect::Fail(_)); + let src = read_file(&test_root().join(case.path))?; + let rejected = !lexes(&src, html(case)); + match (rejected, listed || marked) { + (true, false) => t.pass += 1, + (false, true) => t.xfail += 1, + (true, true) => t.fail.push(format!( + "{}: listed as an expected failure, but rejected", + case.path + )), + (false, false) => t.fail.push(format!( + "{}: a lexical error the lexer does not report", + case.path + )), + } + } + check("Test262 lexical negative tests rejected", &t, cases.len()); + Ok(()) +} + +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn every_test_tiles() -> Result<(), String> { + let mut t = Tally::default(); + for case in TEST262 { + let src = read_file(&test_root().join(case.path))?; + let h = html(case); + if tiles(&src, Goal::InputElementDiv, h) + && tiles(&src, Goal::InputElementRegExp, h) + && tiles(&src, Goal::InputElementHashbangOrRegExp, h) + { + // `next_token` ends too (an error resumes where `lexes` stops). + let _ = lexes(&src, h); + t.pass += 1; + } else { + t.fail.push(case.path.to_owned()); + } + } + check("Test262 language/ every test tiles", &t, TEST262.len()); + Ok(()) +} + +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn harness_files_lex() -> Result<(), String> { + let mut files = Vec::new(); + js_files(&harness_root(), &mut files)?; + let mut t = Tally::default(); + for path in &files { + let src = read_file(path)?; + if lexes(&src, true) { + t.pass += 1; + } else { + t.fail.push(path.display().to_string()); + } + } + check("Test262 harness files lex", &t, files.len()); + Ok(()) +} diff --git a/crates/clazel-js-lexer/tests/unicode_tables.rs b/crates/clazel-js-lexer/tests/unicode_tables.rs index eae4018a9..8264595d2 100644 --- a/crates/clazel-js-lexer/tests/unicode_tables.rs +++ b/crates/clazel-js-lexer/tests/unicode_tables.rs @@ -1,10 +1,24 @@ //! The `ID_Start` and `ID_Continue` tables (`src/unicode_data.rs`, generated //! from Unicode 18.0.0 by `xtask/src/js_idents.rs`) and their lookups: the //! shape the bisection relies on, a property Unicode guarantees, and known -//! code points. Nothing about the tables is proven (`src/unicode.rs`). +//! code points, and a second derivation from the file. `src/unicode.rs` +//! proves the lookups are the spec's predicates; that the tables are the +//! Unicode properties is checked here, not proven. use clazel_js_lexer::unicode::{Range, id_continue_non_ascii, id_start_non_ascii}; -use clazel_js_lexer::unicode_data::{ID_CONTINUE, ID_START}; +use clazel_js_lexer::unicode_data::{ + ID_CONTINUE_LEN, ID_START_LEN, id_continue_at_exec, id_start_at_exec, +}; + +/// The rows of `ID_Start`, through the accessor the lookups use. +fn id_start_rows() -> Vec { + (0..ID_START_LEN).map(id_start_at_exec).collect() +} + +/// The rows of `ID_Continue`. +fn id_continue_rows() -> Vec { + (0..ID_CONTINUE_LEN).map(id_continue_at_exec).collect() +} /// Sorted, disjoint, non-empty rows, all within U+0080..U+10FFFF. fn well_formed(table: &[Range]) { @@ -21,8 +35,8 @@ fn well_formed(table: &[Range]) { #[test] fn tables_are_sorted_and_disjoint() { - well_formed(&ID_START); - well_formed(&ID_CONTINUE); + well_formed(&id_start_rows()); + well_formed(&id_continue_rows()); } /// Every lookup agrees with a linear scan of its table, and `ID_Start` is a @@ -36,9 +50,10 @@ fn lookups_agree_with_the_tables() { // both. let (stride, row_stride) = if cfg!(miri) { (40_009, 97) } else { (7, 1) }; let mut points: Vec = (0..=0x11_0000u32).step_by(stride).collect(); - for row in ID_START + let (id_start, id_continue) = (id_start_rows(), id_continue_rows()); + for row in id_start .iter() - .chain(ID_CONTINUE.iter()) + .chain(id_continue.iter()) .step_by(row_stride) { points.extend([ @@ -51,8 +66,8 @@ fn lookups_agree_with_the_tables() { for c in points { let start = id_start_non_ascii(c); let cont = id_continue_non_ascii(c); - assert_eq!(start, c >= 0x80 && in_table(&ID_START, c), "U+{c:04X}"); - assert_eq!(cont, c >= 0x80 && in_table(&ID_CONTINUE, c), "U+{c:04X}"); + assert_eq!(start, c >= 0x80 && in_table(&id_start, c), "U+{c:04X}"); + assert_eq!(cont, c >= 0x80 && in_table(&id_continue, c), "U+{c:04X}"); assert!(!start || cont, "U+{c:04X} is ID_Start but not ID_Continue"); } } @@ -76,3 +91,57 @@ fn known_code_points() { assert!(!id_start_non_ascii(c) && !id_continue_non_ascii(c)); } } + +/// A second derivation of the two properties from `DerivedCoreProperties.txt` +/// (the file the generator reads), with a parser of its own that shares no +/// code with `xtask`: a bitmap per property over every code point. Every +/// code point U+0080..U+10FFFF is compared with both lookups, each way: a +/// code point the file lists must be found, and one it does not list must +/// not be. +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn lookups_equal_the_file_for_every_code_point() { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../clazel-js-regexp/tests/data/ucd-regexp/DerivedCoreProperties.txt"); + let text = std::fs::read_to_string(&path).unwrap_or_default(); + assert!(!text.is_empty(), "cannot read {}", path.display()); + let mut start = vec![false; 0x11_0000]; + let mut cont = vec![false; 0x11_0000]; + for line in text.lines() { + // `[..] ; # comment` + let body = line.split('#').next().unwrap_or(""); + let Some((cps, prop)) = body.split_once(';') else { + continue; + }; + let (cps, prop) = (cps.trim(), prop.trim()); + let bitmap = match prop { + "ID_Start" => &mut start, + "ID_Continue" => &mut cont, + _ => continue, + }; + let (lo, hi) = match cps.split_once("..") { + Some((a, b)) => (a, b), + None => (cps, cps), + }; + let (Ok(lo), Ok(hi)) = (usize::from_str_radix(lo, 16), usize::from_str_radix(hi, 16)) + else { + assert!(line.is_empty(), "not a hex range: {line}"); + continue; + }; + let Some(slots) = bitmap.get_mut(lo..=hi) else { + assert!(line.is_empty(), "range outside U+10FFFF: {line}"); + continue; + }; + slots.fill(true); + } + // The file lists both properties (ASCII letters are the first rows). + let at = |bitmap: &[bool], c: usize| bitmap.get(c).copied().unwrap_or(false); + assert!(at(&start, 0x41) && at(&cont, 0x30) && at(&cont, 0x5F) && !at(&start, 0x5F)); + for (i, (&s, &c)) in (0u32..).zip(start.iter().zip(cont.iter())) { + if i < 0x80 { + continue; + } + assert_eq!(id_start_non_ascii(i), s, "ID_Start U+{i:04X}"); + assert_eq!(id_continue_non_ascii(i), c, "ID_Continue U+{i:04X}"); + } +} diff --git a/crates/clazel-js-number/README.md b/crates/clazel-js-number/README.md index e2c2a100d..fa36e1267 100644 --- a/crates/clazel-js-number/README.md +++ b/crates/clazel-js-number/README.md @@ -13,8 +13,10 @@ Part of [Clazel](../../README.md). ECMAScript's Number conversions | `bigint_literal_value(&str) -> Option` | The value of a lexer BigInt literal (`n` suffix, `_` separators, §12.9.3†). | | `BigInt::to_string_radix(u32)` | BigInt::toString (§6.1.6.2.23†), radix 2 to 36, lowercase; `RangeError` outside that. | -`BigInt` is a sign and a `Big` magnitude (zero is never negative), so the limb -arithmetic exists once. Conversion takes a word-sized chunk of digits at a +`BigInt` is a sign and a `Big` magnitude (zero is never negative, unbounded, +`Vec`-backed). The Number conversions use the fixed-capacity `FixedBig` +instead (64 limbs for StringToNumber's exact path, 32 for the formatters and +the tables), which Kani can reason about. Conversion takes a word-sized chunk of digits at a time (quadratic), with a bit-extraction path for radix 2, 4, 8, 16 and 32. † The section numbers are from `phase-7.md`; the spec text is not vendored, so @@ -46,7 +48,7 @@ Raising the tier to 1 waits for CI's full verification on the pull request. - Heap storage for BigInts (item 7.5): this crate parses and prints them. - Number::toString for a radix other than 10. -- The powers-of-five tables are computed on first use from `Big`, not stored. +- The powers-of-five tables are computed on first use from `FixedBig`, not stored. A later change can emit them as a checked-in constant, as `cargo xtask tables` does for IDNA. diff --git a/crates/clazel-js-number/src/bigint.rs b/crates/clazel-js-number/src/bigint.rs index d5990959b..d9b3711d8 100644 --- a/crates/clazel-js-number/src/bigint.rs +++ b/crates/clazel-js-number/src/bigint.rs @@ -1,12 +1,14 @@ -//! A small unsigned big integer: little-endian `u64` limbs, no trailing zero -//! limb. It holds the exact values the slow paths need (powers of ten and -//! five, scaled mantissas) and nothing more: add and multiply by a word, -//! shifts, comparison, subtraction, and long division. +//! The unbounded unsigned big integer of BigInt values: little-endian `u64` +//! limbs in a `Vec`, no trailing zero limb. It has what BigInt parsing and +//! printing need: multiply and add a word, divide by a word (with the +//! remainder), comparison, and bit access. //! -//! Every operation is total. Preconditions that cannot be stated in the type -//! (`sub_assign` needs `self >= other`; `divrem` needs a nonzero divisor) -//! have a defined answer when violated (`sub_assign` gives zero, `divrem` by -//! zero returns `(0, num)`), so no input panics. +//! The exact paths of the Number conversions use the fixed-capacity, +//! array-backed [`crate::fixed::FixedBig`] instead (Kani cannot reason about +//! `Vec` growth, and those paths are bounded). A BigInt has no bound, so it +//! stays `Vec`-backed. +//! +//! Every operation is total: division by zero leaves the value alone. use std::cmp::Ordering; @@ -17,11 +19,6 @@ pub struct Big { limbs: Vec, } -/// `10^19`, the largest power of ten in a `u64`. -const TEN_19: u64 = 10_000_000_000_000_000_000; -/// `5^27`, the largest power of five in a `u64`. -const FIVE_27: u64 = 7_450_580_596_923_828_125; - /// Append the decimal digits of `v`, padded with zeros on the left to at /// least `width` digits (`v = 0` prints one digit when `width` is 0). pub fn push_u64(s: &mut String, v: u64, width: usize) { @@ -45,12 +42,6 @@ pub fn push_u64(s: &mut String, v: u64, width: usize) { } } -/// A mask of the low `bits` bits. -fn low_mask(bits: u32) -> u64 { - 1u64.checked_shl(bits) - .map_or(u64::MAX, |b| b.wrapping_sub(1)) -} - impl Big { /// Zero. #[must_use] @@ -171,113 +162,6 @@ impl Big { rem as u64 } - /// `self *= 10^k`. - pub fn mul_pow10(&mut self, k: u32) { - let mut k = k; - while k >= 19 { - self.mul_small(TEN_19); - k = k.saturating_sub(19); - } - let mut p: u64 = 1; - for _ in 0..k { - p = p.saturating_mul(10); - } - self.mul_small(p); - } - - /// `self *= 5^k`. - pub fn mul_pow5(&mut self, k: u32) { - let mut k = k; - while k >= 27 { - self.mul_small(FIVE_27); - k = k.saturating_sub(27); - } - let mut p: u64 = 1; - for _ in 0..k { - p = p.saturating_mul(5); - } - self.mul_small(p); - } - - /// `self = floor(self / 5^k)`: floor division composes, so this divides - /// by `5^27` as often as it fits, then by the rest. - pub fn div_pow5(&mut self, k: u32) { - let mut k = k; - while k >= 27 { - self.div_small(FIVE_27); - k = k.saturating_sub(27); - } - let mut p: u64 = 1; - for _ in 0..k { - p = p.saturating_mul(5); - } - self.div_small(p); - } - - /// `self << bits`. - #[must_use] - pub fn shl(&self, bits: u64) -> Self { - if self.is_zero() { - return Self::zero(); - } - let limbs = usize::try_from(bits / 64).unwrap_or(usize::MAX); - let rem = (bits % 64) as u32; - let mut out = vec![0u64; limbs]; - let mut carry: u64 = 0; - for &l in &self.limbs { - if rem == 0 { - out.push(l); - } else { - out.push((l << rem) | carry); - carry = l.checked_shr(64u32.saturating_sub(rem)).unwrap_or(0); - } - } - if carry != 0 { - out.push(carry); - } - let mut b = Self { limbs: out }; - b.trim(); - b - } - - /// `self << 1 | bit`, in place. - fn shl1_or(&mut self, bit: bool) { - let mut carry = u64::from(bit); - for l in &mut self.limbs { - let next = *l >> 63; - *l = (*l << 1) | carry; - carry = next; - } - if carry != 0 { - self.limbs.push(carry); - } - } - - /// `self >> bits`, and whether any bit shifted out was set. - #[must_use] - pub fn shr(&self, bits: u64) -> (Self, bool) { - let limbs = usize::try_from(bits / 64).unwrap_or(usize::MAX); - let rem = (bits % 64) as u32; - let mut sticky = false; - for &l in self.limbs.iter().take(limbs) { - sticky |= l != 0; - } - let kept: Vec = self.limbs.iter().skip(limbs).copied().collect(); - let mut out = Vec::with_capacity(kept.len()); - if rem == 0 { - out.extend_from_slice(&kept); - } else { - sticky |= kept.first().is_some_and(|&l| l & low_mask(rem) != 0); - for (i, &l) in kept.iter().enumerate() { - let next = kept.get(i.saturating_add(1)).copied().unwrap_or(0); - out.push((l >> rem) | next.checked_shl(64u32.saturating_sub(rem)).unwrap_or(0)); - } - } - let mut b = Self { limbs: out }; - b.trim(); - (b, sticky) - } - /// Numeric comparison. #[must_use] pub fn cmp_big(&self, other: &Self) -> Ordering { @@ -294,101 +178,4 @@ impl Big { o => o, } } - - /// `self += other`. - pub fn add_assign(&mut self, other: &Self) { - let mut carry = false; - let n = self.limbs.len().max(other.limbs.len()); - self.limbs.resize(n, 0); - for (i, l) in self.limbs.iter_mut().enumerate() { - let o = other.limbs.get(i).copied().unwrap_or(0); - let (s1, c1) = l.overflowing_add(o); - let (s2, c2) = s1.overflowing_add(u64::from(carry)); - *l = s2; - carry = c1 | c2; - } - if carry { - self.limbs.push(1); - } - self.trim(); - } - - /// `self -= other`. If `other > self` the result is zero. - pub fn sub_assign(&mut self, other: &Self) { - if self.cmp_big(other) == Ordering::Less { - self.limbs.clear(); - return; - } - let mut borrow = false; - for (i, l) in self.limbs.iter_mut().enumerate() { - let o = other.limbs.get(i).copied().unwrap_or(0); - let (d1, b1) = l.overflowing_sub(o); - let (d2, b2) = d1.overflowing_sub(u64::from(borrow)); - *l = d2; - borrow = b1 | b2; - } - self.trim(); - } - - /// Quotient and remainder of `num / den`, by binary long division. - /// Division by zero returns `(0, num)`. - #[must_use] - pub fn divrem(num: &Self, den: &Self) -> (Self, Self) { - if den.is_zero() { - return (Self::zero(), num.clone()); - } - let nbits = num.bit_len(); - let qlimbs = usize::try_from(nbits.div_ceil(64)).unwrap_or(0); - let mut quo = vec![0u64; qlimbs]; - let mut rem = Self::zero(); - let mut i = nbits; - while i > 0 { - i = i.saturating_sub(1); - rem.shl1_or(num.bit(i)); - if rem.cmp_big(den) != Ordering::Less { - rem.sub_assign(den); - let limb = usize::try_from(i / 64).unwrap_or(usize::MAX); - if let Some(q) = quo.get_mut(limb) { - *q |= 1u64 << (i % 64); - } - } - } - let mut q = Self { limbs: quo }; - q.trim(); - (q, rem) - } - - /// The decimal digits, most significant first (`"0"` for zero). - #[must_use] - pub fn to_decimal(&self) -> String { - if self.is_zero() { - return "0".to_string(); - } - let mut t = self.clone(); - let mut chunks: Vec = Vec::new(); - while !t.is_zero() { - chunks.push(t.div_small(TEN_19)); - } - let mut s = String::new(); - for (i, c) in chunks.iter().rev().enumerate() { - push_u64(&mut s, *c, if i == 0 { 0 } else { 19 }); - } - s - } - - /// `5^k`. - #[must_use] - pub fn pow5(k: u32) -> Self { - let mut b = Self::from_u64(1); - b.mul_pow5(k); - b - } - - /// `10^k`. - #[must_use] - pub fn pow10(k: u32) -> Self { - let mut b = Self::from_u64(1); - b.mul_pow10(k); - b - } } diff --git a/crates/clazel-js-number/src/fixed.rs b/crates/clazel-js-number/src/fixed.rs new file mode 100644 index 000000000..ef10d79d3 --- /dev/null +++ b/crates/clazel-js-number/src/fixed.rs @@ -0,0 +1,505 @@ +//! A fixed-capacity unsigned big integer: `N` little-endian `u64` limbs in an +//! array, and the count of limbs in use. This is the arithmetic of the exact +//! paths of the Number conversions (StringToNumber's fallback, `toFixed`, +//! `toExponential`, `toPrecision` and the power-of-five tables). BigInt values +//! are unbounded and use the `Vec`-backed [`crate::bigint::Big`] instead. +//! +//! Why an array: Kani cannot reason about `Vec` growth. With an array every +//! operation is a loop over at most `N` limbs. +//! +//! Every operation is **total**, and total whatever `N` is, so a proof at a +//! small `N` is a proof about the same generic code that runs at the real `N`: +//! +//! - an operation whose true result needs more than `N` limbs drops the limbs +//! that do not fit and sets [`FixedBig::lost`] (a sticky flag). The value is +//! then wrong but defined, and `lost` says so. Callers size `N` so that it +//! never happens ([`BIG_LIMBS_PARSE`] and [`BIG_LIMBS_FORMAT`]); +//! - `sub_assign` needs `self >= other` and gives zero otherwise; `divrem` +//! by zero returns `(0, num)`; `divrem` is exact only if +//! `den.bit_len() < 64 * N` (the running remainder reaches `2 * den`). +//! +//! Representation invariant (every operation keeps it; the Kani harnesses +//! assert it): `len <= N`; `limbs[len..]` are all zero; if `len > 0` then +//! `limbs[len - 1] != 0`. So zero has `len == 0`, and the derived equality is +//! numeric equality (together with the `lost` flag). + +use std::cmp::Ordering; + +/// Limbs for StringToNumber's exact path (`crate::parse::decimal_exact`). +/// +/// At most 801 significant digits (800 and the sticky digit), then either +/// `* 10^e10` with the value below `10^310` (at most 1030 bits), or a +/// division by `10^-e10` with `-e10 <= 330 + 801 = 1131`: `10^1131` is 3757 +/// bits, the dividend is shifted to 3757 + 55 bits and the running +/// remainder has one more: 3813 bits, 60 limbs. 64 limbs hold 4096 bits. +/// (Measured over the whole test suite: at most 57 limbs.) +pub const BIG_LIMBS_PARSE: usize = 64; +/// Limbs for the formatters and the power-of-five tables. +/// +/// Formatters: the numerator is `m * 2^e2 * 10^scale` and the denominator +/// `2^-e2 * 10^-scale` (each factor only on the side of its sign) for a +/// 53-bit `m`, `e2` in `-1074..=971` and `scale` at most `100 + 324 + 8` +/// (`p <= 101`, a decimal exponent of at least -324, and the loop's eight +/// steps). The worst case is a tiny `x`: the numerator is `53 + 3.33 * 432` +/// bits, 1492 with the shift by one, 24 limbs; the denominator is at most +/// 1075 bits. Tables: `5^342` is 795 bits and the largest dividend is +/// `2^1718`, 27 limbs. 32 limbs hold 2048 bits. (Measured over the whole +/// test suite: at most 28 limbs, counting the pre-trim bound of `shl`, all +/// in the tables.) +pub const BIG_LIMBS_FORMAT: usize = 32; + +/// `10^19`, the largest power of ten in a `u64`. +const TEN_19: u64 = 10_000_000_000_000_000_000; +/// `5^27`, the largest power of five in a `u64`. +const FIVE_27: u64 = 7_450_580_596_923_828_125; + +/// A mask of the low `bits` bits. +fn low_mask(bits: u32) -> u64 { + 1u64.checked_shl(bits) + .map_or(u64::MAX, |b| b.wrapping_sub(1)) +} + +/// An unsigned integer of at most `64 * N` bits. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct FixedBig { + /// Little-endian limbs; the ones from `len` on are zero. + limbs: [u64; N], + /// The number of limbs in use: the last one is nonzero. + len: usize, + /// Whether an operation ever had to drop limbs that did not fit. + lost: bool, +} + +impl FixedBig { + /// Zero. + #[must_use] + pub fn zero() -> Self { + Self { + limbs: [0; N], + len: 0, + lost: false, + } + } + + /// The value `x`. + #[must_use] + pub fn from_u64(x: u64) -> Self { + let mut b = Self::zero(); + b.push(x); + b.trim(); + b + } + + /// The value `x`. + #[must_use] + pub fn from_u128(x: u128) -> Self { + let mut b = Self::zero(); + b.push(x as u64); + b.push((x >> 64) as u64); + b.trim(); + b + } + + /// The value with the limbs `limbs`, least significant first. + #[must_use] + pub fn from_limbs(limbs: [u64; N]) -> Self { + let mut b = Self { + limbs, + len: N, + lost: false, + }; + b.trim(); + b + } + + /// Append the limb `v` (a nonzero one that does not fit sets `lost`). + fn push(&mut self, v: u64) { + match self.limbs.get_mut(self.len) { + Some(slot) => { + *slot = v; + self.len = self.len.saturating_add(1); + } + None => self.lost |= v != 0, + } + } + + /// Drop zero limbs from the top. + fn trim(&mut self) { + while self.len > 0 && self.limbs.get(self.len.saturating_sub(1)) == Some(&0) { + self.len = self.len.saturating_sub(1); + } + } + + /// Whether the value is zero. + #[must_use] + pub fn is_zero(&self) -> bool { + self.len == 0 + } + + /// Whether an operation had to drop limbs that did not fit (the value is + /// then not the true result). + #[must_use] + pub fn lost(&self) -> bool { + self.lost + } + + /// The number of limbs in use. + #[must_use] + pub fn limbs_used(&self) -> usize { + self.len + } + + /// Whether the representation invariant holds (for the Kani harnesses). + #[must_use] + pub fn invariant(&self) -> bool { + self.len <= N + && self.limbs.iter().skip(self.len).all(|&l| l == 0) + && (self.len == 0 + || self + .limbs + .get(self.len.saturating_sub(1)) + .is_some_and(|&l| l != 0)) + } + + /// The number of significant bits (zero for zero). + #[must_use] + pub fn bit_len(&self) -> u64 { + if self.len == 0 { + return 0; + } + let top = self + .limbs + .get(self.len.saturating_sub(1)) + .copied() + .unwrap_or(0); + let below = (self.len as u64).saturating_sub(1).saturating_mul(64); + below.saturating_add(u64::from(64u32.saturating_sub(top.leading_zeros()))) + } + + /// Bit `i` (bit 0 is the least significant). + #[must_use] + pub fn bit(&self, i: u64) -> bool { + let limb = usize::try_from(i / 64).unwrap_or(usize::MAX); + match self.limbs.get(limb) { + Some(&l) => (l >> (i % 64)) & 1 == 1, + None => false, + } + } + + /// The low 128 bits. + #[must_use] + pub fn low_u128(&self) -> u128 { + let lo = self.limbs.first().copied().unwrap_or(0); + let hi = self.limbs.get(1).copied().unwrap_or(0); + u128::from(lo) | (u128::from(hi) << 64) + } + + /// `self *= m`. + pub fn mul_small(&mut self, m: u64) { + let mut carry: u128 = 0; + for l in self.limbs.iter_mut().take(self.len) { + // (2^64-1)^2 + (2^64-1) < 2^128, so this never wraps. + let t = u128::from(*l) + .wrapping_mul(u128::from(m)) + .wrapping_add(carry); + *l = t as u64; + carry = t >> 64; + } + if carry != 0 { + self.push(carry as u64); + } + self.trim(); + } + + /// `self += a`. + pub fn add_small(&mut self, a: u64) { + let mut carry = a; + for l in self.limbs.iter_mut().take(self.len) { + if carry == 0 { + break; + } + let (s, c) = l.overflowing_add(carry); + *l = s; + carry = u64::from(c); + } + if carry != 0 { + self.push(carry); + } + // A carry that did not fit leaves the top limb zero (found by Kani). + self.trim(); + } + + /// `self /= d` for a nonzero word `d`; returns the remainder. Division + /// by zero leaves `self` alone and returns 0. + pub fn div_small(&mut self, d: u64) -> u64 { + if d == 0 { + return 0; + } + let wide = u128::from(d); + let mut rem: u128 = 0; + if let Some(used) = self.limbs.get_mut(..self.len) { + for l in used.iter_mut().rev() { + // rem < d < 2^64, so the shift loses nothing. + let cur = (rem << 64) | u128::from(*l); + *l = cur.checked_div(wide).unwrap_or(0) as u64; + rem = cur.checked_rem(wide).unwrap_or(0); + } + } + self.trim(); + rem as u64 + } + + /// `self *= 10^k`. + pub fn mul_pow10(&mut self, k: u32) { + let mut k = k; + while k >= 19 { + self.mul_small(TEN_19); + k = k.saturating_sub(19); + } + let mut p: u64 = 1; + for _ in 0..k { + p = p.saturating_mul(10); + } + self.mul_small(p); + } + + /// `self *= 5^k`. + pub fn mul_pow5(&mut self, k: u32) { + let mut k = k; + while k >= 27 { + self.mul_small(FIVE_27); + k = k.saturating_sub(27); + } + let mut p: u64 = 1; + for _ in 0..k { + p = p.saturating_mul(5); + } + self.mul_small(p); + } + + /// `self = floor(self / 5^k)`: floor division composes, so this divides + /// by `5^27` as often as it fits, then by the rest. + pub fn div_pow5(&mut self, k: u32) { + let mut k = k; + while k >= 27 { + let _ = self.div_small(FIVE_27); + k = k.saturating_sub(27); + } + let mut p: u64 = 1; + for _ in 0..k { + p = p.saturating_mul(5); + } + let _ = self.div_small(p); + } + + /// Store `v` as limb `i` of a result under construction (`lost` if the + /// limb does not fit and is nonzero). + fn put(&mut self, i: usize, v: u64) { + match self.limbs.get_mut(i) { + Some(slot) => *slot = v, + None => self.lost |= v != 0, + } + } + + /// `self << bits`. + #[must_use] + pub fn shl(&self, bits: u64) -> Self { + let mut out = Self::zero(); + out.lost = self.lost; + if self.is_zero() { + return out; + } + let skip = usize::try_from(bits / 64).unwrap_or(usize::MAX); + let rem = (bits % 64) as u32; + let mut carry: u64 = 0; + let mut at = skip; + for &l in self.limbs.iter().take(self.len) { + if rem == 0 { + out.put(at, l); + } else { + out.put(at, (l << rem) | carry); + carry = l.checked_shr(64u32.saturating_sub(rem)).unwrap_or(0); + } + at = at.saturating_add(1); + } + if carry != 0 { + out.put(at, carry); + } + // Every limb written is at an index of `at` or less. + out.len = at.saturating_add(1).min(N); + out.trim(); + out + } + + /// `self << 1 | bit`, in place. + fn shl1_or(&mut self, bit: bool) { + let mut carry = u64::from(bit); + for l in self.limbs.iter_mut().take(self.len) { + let next = *l >> 63; + *l = (*l << 1) | carry; + carry = next; + } + if carry != 0 { + self.push(carry); + } + } + + /// `self >> bits`, and whether any bit shifted out was set. + #[must_use] + pub fn shr(&self, bits: u64) -> (Self, bool) { + let mut out = Self::zero(); + out.lost = self.lost; + let skip = usize::try_from(bits / 64).unwrap_or(usize::MAX); + let rem = (bits % 64) as u32; + let mut sticky = false; + for &l in self.limbs.iter().take(self.len).take(skip) { + sticky |= l != 0; + } + if rem != 0 { + sticky |= self + .limbs + .get(skip) + .is_some_and(|&l| l & low_mask(rem) != 0); + } + let kept = self.len.saturating_sub(skip); + for i in 0..kept { + let j = skip.saturating_add(i); + let l = self.limbs.get(j).copied().unwrap_or(0); + let w = if rem == 0 { + l + } else { + let next = self.limbs.get(j.saturating_add(1)).copied().unwrap_or(0); + (l >> rem) | next.checked_shl(64u32.saturating_sub(rem)).unwrap_or(0) + }; + out.put(i, w); + } + out.len = kept.min(N); + out.trim(); + (out, sticky) + } + + /// Numeric comparison. + #[must_use] + pub fn cmp_big(&self, other: &Self) -> Ordering { + match self.len.cmp(&other.len) { + Ordering::Equal => { + for (a, b) in self + .limbs + .iter() + .take(self.len) + .rev() + .zip(other.limbs.iter().take(other.len).rev()) + { + match a.cmp(b) { + Ordering::Equal => {} + o => return o, + } + } + Ordering::Equal + } + o => o, + } + } + + /// `self += other`. + pub fn add_assign(&mut self, other: &Self) { + let mut carry = false; + let n = self.len.max(other.len); + // Limbs from `len` on are zero, so the sum needs no special case. + self.len = n; + self.lost |= other.lost; + for (l, &o) in self.limbs.iter_mut().zip(other.limbs.iter()).take(n) { + let (s1, c1) = l.overflowing_add(o); + let (s2, c2) = s1.overflowing_add(u64::from(carry)); + *l = s2; + carry = c1 | c2; + } + if carry { + self.push(1); + } + self.trim(); + } + + /// `self -= other`. If `other > self` the result is zero. + pub fn sub_assign(&mut self, other: &Self) { + if self.cmp_big(other) == Ordering::Less { + self.limbs = [0; N]; + self.len = 0; + return; + } + let mut borrow = false; + for (l, &o) in self.limbs.iter_mut().zip(other.limbs.iter()).take(self.len) { + let (d1, b1) = l.overflowing_sub(o); + let (d2, b2) = d1.overflowing_sub(u64::from(borrow)); + *l = d2; + borrow = b1 | b2; + } + self.trim(); + } + + /// Quotient and remainder of `num / den`, by binary long division. + /// Division by zero returns `(0, num)`. + #[must_use] + pub fn divrem(num: &Self, den: &Self) -> (Self, Self) { + if den.is_zero() { + return (Self::zero(), num.clone()); + } + let nbits = num.bit_len(); + let mut quo = Self::zero(); + let mut rem = Self::zero(); + let mut i = nbits; + while i > 0 { + i = i.saturating_sub(1); + rem.shl1_or(num.bit(i)); + if rem.cmp_big(den) != Ordering::Less { + rem.sub_assign(den); + let limb = usize::try_from(i / 64).unwrap_or(usize::MAX); + if let Some(q) = quo.limbs.get_mut(limb) { + *q |= 1u64 << (i % 64); + } + } + } + // `nbits <= 64 * N`, so the quotient has at most `ceil(nbits / 64)` + // limbs, all of which exist. + quo.len = usize::try_from(nbits.div_ceil(64)).unwrap_or(0).min(N); + quo.trim(); + let lost = num.lost | den.lost | rem.lost; + quo.lost = lost; + rem.lost = lost; + (quo, rem) + } + + /// The decimal digits, most significant first (`"0"` for zero). + #[must_use] + pub fn to_decimal(&self) -> String { + if self.is_zero() { + return "0".to_string(); + } + let mut t = self.clone(); + let mut chunks: Vec = Vec::new(); + while !t.is_zero() { + chunks.push(t.div_small(TEN_19)); + } + let mut s = String::new(); + for (i, c) in chunks.iter().rev().enumerate() { + crate::bigint::push_u64(&mut s, *c, if i == 0 { 0 } else { 19 }); + } + s + } + + /// `5^k`. + #[must_use] + pub fn pow5(k: u32) -> Self { + let mut b = Self::from_u64(1); + b.mul_pow5(k); + b + } + + /// `10^k`. + #[must_use] + pub fn pow10(k: u32) -> Self { + let mut b = Self::from_u64(1); + b.mul_pow10(k); + b + } +} diff --git a/crates/clazel-js-number/src/format.rs b/crates/clazel-js-number/src/format.rs index 316395aba..b2c21b81d 100644 --- a/crates/clazel-js-number/src/format.rs +++ b/crates/clazel-js-number/src/format.rs @@ -21,7 +21,11 @@ //! `toPrecision` are slow paths, not the hot path of number printing.) //! - Number::toString's shortest digits come from [`crate::shortest`]. -use crate::bigint::{Big, push_u64}; +use crate::bigint::push_u64; +use crate::fixed::{BIG_LIMBS_FORMAT, FixedBig}; + +/// The big integer of the exact formatting arithmetic. +type Big = FixedBig; use crate::shortest::{log10_pow2, shortest_digits}; /// A `RangeError`: an argument outside the range the spec allows. @@ -120,10 +124,13 @@ fn decompose(x: f64) -> (u64, i64) { } /// The integer nearest to `x * 10^scale`, the larger one on a tie. -fn scaled_round(x: f64, scale: i64) -> Big { +/// +/// Generic in the limb count `N` so that the Kani harnesses run the same code +/// at a small `N` (too few limbs gives a wrong but defined answer). +pub(crate) fn scaled_round(x: f64, scale: i64) -> FixedBig { let (m, e2) = decompose(x); - let mut num = Big::from_u64(m); - let mut den = Big::from_u64(1); + let mut num = FixedBig::::from_u64(m); + let mut den = FixedBig::::from_u64(1); if e2 >= 0 { num = num.shl(e2.unsigned_abs()); } else { @@ -138,7 +145,7 @@ fn scaled_round(x: f64, scale: i64) -> Big { // floor((2 num + den) / (2 den)) is round-half-up of num / den. let mut twice = num.shl(1); twice.add_assign(&den); - Big::divrem(&twice, &den.shl(1)).0 + FixedBig::::divrem(&twice, &den.shl(1)).0 } /// The `p` digits `n` and the exponent `e` of the spec's "let e and n be @@ -195,7 +202,7 @@ pub fn to_fixed(x: f64, fraction_digits: i64) -> Result { } // 9.a.: "let n be an integer for which n / 10^f - x is as close to zero // as possible. If there are two such n, pick the larger n." - let n = scaled_round(x, f); + let n = scaled_round::(x, f); let mut m = n.to_decimal(); let f = usize::try_from(f).unwrap_or(0); // 10. "If f != 0, then let k be the length of m; if k <= f, then let z diff --git a/crates/clazel-js-number/src/lib.rs b/crates/clazel-js-number/src/lib.rs index 426aaaa42..d1aecd0ec 100644 --- a/crates/clazel-js-number/src/lib.rs +++ b/crates/clazel-js-number/src/lib.rs @@ -12,7 +12,7 @@ //! - [`number_to_string`]: the shortest digits that round-trip, by Ryū //! ([`shortest`]), laid out as the spec says ([`format`]). //! - [`to_fixed`], [`to_exponential`], [`to_precision`]: exact, by big -//! integers ([`format`], [`bigint`]). +//! integers ([`format`], [`fixed`]). //! //! - [`string_to_bigint`], [`bigint_literal_value`] and //! [`BigInt::to_string_radix`]: StringToBigInt §7.1.14†, the value of a @@ -27,6 +27,7 @@ //! Kani harnesses (compiled only under `cargo kani`). pub mod bigint; +pub mod fixed; pub mod format; pub mod js_bigint; pub mod parse; diff --git a/crates/clazel-js-number/src/parse.rs b/crates/clazel-js-number/src/parse.rs index 81502b5c1..ea276a1e5 100644 --- a/crates/clazel-js-number/src/parse.rs +++ b/crates/clazel-js-number/src/parse.rs @@ -35,7 +35,7 @@ //! - The numeric separator `_` is not accepted (the grammar uses //! `DecimalDigits[~Sep]`). -use crate::bigint::Big; +use crate::fixed::{BIG_LIMBS_PARSE, FixedBig}; use crate::tables::{EL_MAX_Q, EL_MIN_Q, Tables, tables}; /// The significant digits a decimal is cut to for the exact path. @@ -311,7 +311,15 @@ pub(crate) fn leading_digits(d: &Decimal<'_>) -> (u64, i64, bool) { /// The nearest `f64` to `d` (not negative), by exact big-integer arithmetic. #[must_use] pub fn decimal_exact(d: &Decimal<'_>) -> f64 { - let mut big = Big::zero(); + decimal_exact_in::(d) +} + +/// [`decimal_exact`] with `N` limbs of big integer. `N` is a parameter so +/// that the Kani harnesses run the same code at a small `N`; with fewer +/// limbs than the input needs the answer is wrong but defined. +#[must_use] +pub fn decimal_exact_in(d: &Decimal<'_>) -> f64 { + let mut big = FixedBig::::zero(); let mut taken: usize = 0; let mut dropped: i64 = 0; let mut sticky = false; @@ -360,7 +368,7 @@ pub fn decimal_exact(d: &Decimal<'_>) -> f64 { let (hi, st) = big.shr(bl.saturating_sub(64)); return round_to_f64(hi.low_u128() as u64, bl.saturating_sub(64) as i64, st); } - let p = Big::pow10(u32::try_from(e10.saturating_neg()).unwrap_or(0)); + let p = FixedBig::::pow10(u32::try_from(e10.saturating_neg()).unwrap_or(0)); // Scale so the quotient has 55 or 56 bits, more than a double needs. let s = (p.bit_len() as i64) .saturating_sub(big.bit_len() as i64) @@ -370,13 +378,20 @@ pub fn decimal_exact(d: &Decimal<'_>) -> f64 { } else { (big, p.shl(s.unsigned_abs())) }; - let (quo, rem) = Big::divrem(&num, &den); + let (quo, rem) = FixedBig::::divrem(&num, &den); round_to_f64(quo.low_u128() as u64, s.saturating_neg(), !rem.is_zero()) } /// The nearest `f64` to `d` (not negative), using `t` for Eisel–Lemire. #[must_use] pub fn decimal_to_f64(d: &Decimal<'_>, t: &Tables) -> f64 { + decimal_to_f64_in::(d, t) +} + +/// [`decimal_to_f64`] with `N` limbs for the exact path (see +/// [`decimal_exact_in`]). +#[must_use] +pub fn decimal_to_f64_in(d: &Decimal<'_>, t: &Tables) -> f64 { let (w, q, truncated) = leading_digits(d); if w == 0 { return 0.0; @@ -410,7 +425,19 @@ pub fn decimal_to_f64(d: &Decimal<'_>, t: &Tables) -> f64 { } } } - decimal_exact(d) + decimal_exact_in::(d) +} + +/// The value of the ASCII digit `c` (`0-9`, `a-f`, `A-F`) if it is below +/// `radix`: `char::to_digit` for the radixes 2 to 16, on bytes. +fn digit_in_radix(c: u8, radix: u32) -> Option { + let v = match c { + b'0'..=b'9' => u32::from(c.wrapping_sub(b'0')), + b'a'..=b'f' => u32::from(c.wrapping_sub(b'a')).saturating_add(10), + b'A'..=b'F' => u32::from(c.wrapping_sub(b'A')).saturating_add(10), + _ => return None, + }; + (v < radix).then_some(v) } /// The value of the digits of `b` in radix `1 << bits` (`bits` in 1..=4), @@ -425,7 +452,7 @@ pub(crate) fn radix_value(b: &[u8], bits: u32) -> Option { let mut sticky = false; let mut full = false; for &c in b { - let v = char::from(c).to_digit(radix)?; + let v = digit_in_radix(c, radix)?; if full { sticky |= v != 0; exp2 = exp2.saturating_add(i64::from(bits)); @@ -449,7 +476,69 @@ pub(crate) fn radix_value(b: &[u8], bits: u32) -> Option { /// StringToNumber (§7.1.4.1.1†): the Number value of `s`, or NaN. #[must_use] pub fn string_to_number(s: &str) -> f64 { - let t = s.trim_matches(is_str_whitespace).as_bytes(); + string_to_number_bytes::(s.as_bytes(), tables()) +} + +/// The length in bytes of the StrWhiteSpace character at the start of `b` +/// (UTF-8), or 0 if there is none: the code points of [`is_str_whitespace`]. +fn leading_whitespace(b: &[u8]) -> usize { + match b { + [0x09..=0x0D | 0x20, ..] => 1, + [0xC2, 0xA0, ..] => 2, + [0xE1, 0x9A, 0x80, ..] + | [0xE2, 0x80, 0x80..=0x8A | 0xA8 | 0xA9 | 0xAF, ..] + | [0xE2, 0x81, 0x9F, ..] + | [0xE3, 0x80, 0x80, ..] + | [0xEF, 0xBB, 0xBF, ..] => 3, + _ => 0, + } +} + +/// The length in bytes of the StrWhiteSpace character at the end of `b`. +fn trailing_whitespace(b: &[u8]) -> usize { + match b { + [.., 0x09..=0x0D | 0x20] => 1, + [.., 0xC2, 0xA0] => 2, + [.., 0xE1, 0x9A, 0x80] + | [.., 0xE2, 0x80, 0x80..=0x8A | 0xA8 | 0xA9 | 0xAF] + | [.., 0xE2, 0x81, 0x9F] + | [.., 0xE3, 0x80, 0x80] + | [.., 0xEF, 0xBB, 0xBF] => 3, + _ => 0, + } +} + +/// `b` (the UTF-8 of a string) without its leading and trailing +/// StrWhiteSpace: `str::trim_matches(is_str_whitespace)` on bytes, so that +/// the conversion below is a function of bytes and Kani never meets `char` +/// decoding. (The tests compare the two on every code point.) +#[must_use] +pub fn trim_str_whitespace(b: &[u8]) -> &[u8] { + let mut t = b; + loop { + let n = leading_whitespace(t); + if n == 0 { + break; + } + t = t.get(n..).unwrap_or(&[]); + } + loop { + let n = trailing_whitespace(t); + if n == 0 { + break; + } + t = t.get(..t.len().saturating_sub(n)).unwrap_or(&[]); + } + t +} + +/// StringToNumber on the UTF-8 bytes of the string, with `t` for +/// Eisel–Lemire and `N` limbs for the exact path ([`decimal_exact_in`]). +/// Bytes that are not UTF-8 are not whitespace and not part of a literal, +/// so they give NaN, as `string_to_number` would for the same text. +#[must_use] +pub fn string_to_number_bytes(s: &[u8], tbl: &Tables) -> f64 { + let t = trim_str_whitespace(s); if t.is_empty() { return 0.0; } @@ -474,7 +563,7 @@ pub fn string_to_number(s: &str) -> f64 { f64::INFINITY } else { match scan_decimal(body) { - Some(d) => decimal_to_f64(&d, tables()), + Some(d) => decimal_to_f64_in::(&d, tbl), None => return f64::NAN, } }; diff --git a/crates/clazel-js-number/src/proofs.rs b/crates/clazel-js-number/src/proofs.rs index b66a5d401..9a2141887 100644 --- a/crates/clazel-js-number/src/proofs.rs +++ b/crates/clazel-js-number/src/proofs.rs @@ -4,49 +4,126 @@ //! (symbolic, not sampled), that the functions named never panic, never //! overflow (the crate is built with overflow checks, so an overflowing //! operation is a failed assertion), and never index out of bounds; and, -//! where an oracle is cheap, that the answer is right: -//! -//! - `Big::add_small` against `u128` addition; -//! - `round_to_f64` for every `q`, `exp2` and `sticky`: total, and never -//! NaN or negative; -//! - `eisel_lemire` for every `w`, `q` and every table entry with its top -//! bit set (as every real entry has): total, never NaN or negative. That -//! no table value can make it overflow is this proof; that the real table -//! is right is the differential tests' job; -//! - `mul_shift` (every 57-bit `m`, 125-bit `mul` and shift), -//! `log10_pow2`, `log10_pow5` and `pow5bits`; -//! - `scan_decimal` on every byte string of up to 5 bytes, `leading_digits` -//! on every 3 + 3 digit literal with any exponent, and `radix_value` on -//! every 3-byte string in each radix. -//! -//! Harnesses that were written and did not finish within 100 s (on a loaded -//! laptop) were removed rather than left failing: `Big`'s `mul_small`, -//! `shl`/`shr`, `cmp_big`/`sub_assign`/`add_assign`, `divrem` and `div_small` -//! (vectors of limbs and 64-bit multiplication and division), Ryu's `d2d_with` -//! as a whole, and `layout_decimal` (string building). So these are **not** -//! proven panic-free by Kani: `decimal_exact`, `string_to_number`, -//! `number_to_string`, the three `to_*` formatters, `Big` beyond `add_small`, -//! and `d2d_with`. The differential tests (`tests/differential.rs`, with -//! overflow checks on) exercise them; that is evidence, not proof. -//! -//! BigInt parsing and printing (`js_bigint`): proven are `digit_value` and -//! `digit_char` (total, below 36, mutually inverse, exactly the ASCII -//! alphanumerics), `chunk_power` (the largest power of each radix 2 to 36 -//! in a `u64`), and one-limb `mul_small` then `add_small` against `u128` -//! arithmetic (31 s). Written and stopped after more than 3 minutes, so -//! removed: one-limb `div_small` against `u64` division (a 128-by-64-bit -//! division) and `push_chunk` (a 64-step loop of `u64` division). Not proven: +//! where an oracle is cheap, that the answer is right. +//! +//! # `FixedBig`, the array-backed big integer of the Number conversions +//! +//! `FixedBig` (`fixed.rs`) holds `N` limbs in an array and the count in +//! use; an operation whose result needs more limbs drops them and sets a +//! `lost` flag, so every operation is total at every `N`. The harnesses run +//! it at `N` = 1, 2 and 3, not at the real sizes (64 limbs for StringToNumber's +//! exact path, 32 for the formatters and tables: they were sized by argument +//! and by measurement, see `fixed.rs`). The code is the same generic code at +//! every `N`: every limb access is a `get`, `take` or `zip`, and `N` appears +//! only as the array length and in `min(N)`, so what is shown at `N` = 2 or 3 +//! for totality holds at `N` = 64. That is an argument, not a Kani result, and +//! the differential tests (`tests/fixed.rs` against `u128` with `N` = 2, +//! `tests/differential.rs` at the real sizes) are the evidence behind it. +//! +//! Proven on symbolic contents, each keeping the representation invariant +//! (`FixedBig::invariant`: `len <= N`, limbs from `len` on zero, top limb +//! nonzero): +//! +//! - `from_u128`, `low_u128`, `bit_len`, `bit` against `u128` (`N` = 2); +//! `cmp_big` is `u128` order; +//! - `add_small`, `add_assign` against `u128` addition, and `lost` exactly on +//! carry out; `sub_assign` against saturating `u128` subtraction; +//! - `mul_small`: one-limb value times a word equals the `u128` product; +//! total on any three limbs times any word; +//! - `div_small`: total on any three limbs and any word, zero included, with +//! the remainder below the divisor; +//! - `shl` (any shift of a `u64`, equal to the `u128` shift when it is below +//! 64) and `shr` (any shift of a `u128`, with the sticky bit); both total +//! on any three limbs and any shift. +//! +//! Kani found one real bug here: `add_small` with a carry out of the top limb +//! left a zero top limb (invariant broken, only when the value did not fit). +//! Fixed by trimming. +//! +//! **Not proven by Kani**: `divrem` (its loop runs once per bit of the +//! dividend; one limb with a free dividend did not finish in 12 minutes, and +//! one limb with a dividend under 2^12 did not in 8), `mul_pow10`, +//! `mul_pow5`, `div_pow5` (each is a loop whose counter falls by saturating +//! subtraction around `mul_small`/`div_small`, but at `N` = 2 with `k` up to +//! 40 the multiplications did not finish in 7 minutes), `to_decimal`. They are +//! checked by `tests/fixed.rs` (`divrem`, against `u128`) and the +//! differential tests. +//! +//! # StringToNumber +//! +//! `string_to_number(&str)` is `string_to_number_bytes(s.as_bytes(), tables())`, +//! on bytes: the old `&str` version crashed the Kani compiler in `char` +//! decoding (`trim_matches` over `char`s and `char::to_digit`), so trimming is +//! now `trim_str_whitespace` on UTF-8 bytes and radix digits are +//! `digit_in_radix` (`tests/trim.rs` compares the trimmer with +//! `trim_matches(is_str_whitespace)` on every Unicode scalar value). +//! +//! - `string_to_number_bytes::<2>` with a table that has no Eisel-Lemire +//! entry: total on every byte string of up to 3 bytes over a 12-byte +//! alphabet (digits 0, 1, 9; both signs; `.`; `e`; space; `x`; `I`; the +//! bytes `E2` and `80`). That covers whitespace trimming, signs, the radix +//! prefix, `Infinity`'s first letter, `scan_decimal` and Clinger's fast +//! path. A three-byte literal never reaches the exact path (`decimal_exact` +//! needs a decimal exponent beyond 22 or more than 15 significant digits), +//! so that path is not covered by this harness. +//! - Also: `round_to_f64` (every `q`, `exp2`, `sticky`), `eisel_lemire` (every +//! `w`, `q` and every table entry with its top bit set), `scan_decimal` on +//! every byte string of up to 5 bytes, `leading_digits` on every 3 + 3 digit +//! literal with any exponent, and `radix_value` on every 3-byte string in +//! each radix. +//! +//! **Not proven by Kani**: `decimal_exact` (the exact fallback): a harness for +//! one digit times `10^exp`, `exp` in 0..=400 on two limbs, did not finish in +//! 12 minutes; neither did 0..=40 nor 0..=18. Negative exponents also need +//! `divrem`. So `string_to_number` is **not** proven panic-free overall. +//! `tables()` (the powers-of-five tables, built once from `FixedBig` by +//! loops over about 700 entries) is not proven either. A CBMC timeout is +//! reported by Kani as FAILED, not as a counterexample; none of the harnesses +//! that were dropped produced a counterexample. +//! +//! # Number to string, `toFixed`, `toExponential`, `toPrecision` +//! +//! Proven: `mul_shift` (every 57-bit `m`, 125-bit `mul` and shift), +//! `log10_pow2`, `log10_pow5` and `pow5bits`. +//! +//! **Not proven by Kani**: `number_to_string`, `to_fixed`, `to_exponential`, +//! `to_precision`, `scaled_round`, `exponent_digits`, `layout_decimal` +//! (`String` building), and Ryu's `d2d_with` as a whole (written and stopped +//! after more than 400 s). A harness for `scaled_round` at two limbs with +//! `divrem` replaced by a stub that returns any value (Kani's unstable +//! stubbing, which `cargo kani -p` does not enable) did not finish in 6 +//! minutes. The differential tests (`tests/differential.rs`: every `f32` +//! widened, 300,000 random doubles, `toFixed`/`toExponential`/`toPrecision` +//! against Rust's `{:.*}` and `{:.*e}`, with overflow checks on) exercise them; +//! that is evidence, not proof. +//! +//! # BigInt (`js_bigint`, the `Vec`-backed `bigint::Big`) +//! +//! Proven: `digit_value` and `digit_char` (total, below 36, mutually inverse, +//! exactly the ASCII alphanumerics), `chunk_power` (the largest power of each +//! radix 2 to 36 in a `u64`), `Big`'s `mul_small` and `div_small` total on any +//! two limbs, `cmp_big` is `u128` order and `bit_len`/`bit` match `u128`, and +//! one-limb `mul_small` then `add_small` against `u128` arithmetic. +//! `Big::add_small` against `u128` addition. BigInt magnitudes are unbounded, +//! so `Big` stays `Vec`-backed, and Kani cannot grow a `Vec` symbolically: //! `parse_magnitude`, `magnitude_digits`, `string_to_bigint`, -//! `bigint_literal_value`, `BigInt::to_string_radix`; `tests/bigint.rs` -//! exercises them against an independent reference. +//! `bigint_literal_value` and `BigInt::to_string_radix` are **not** proven; +//! `tests/bigint.rs` exercises them against an independent reference. +//! One-limb `div_small` against `u64` division and `push_chunk` (a 64-step +//! loop of `u64` division) were stopped after more than 3 minutes. +//! +//! So `clazel-js-number` stays at tier 0: no public conversion is proven free +//! of panics and overflow. use crate::bigint::Big; +use crate::fixed::FixedBig; use crate::js_bigint::{chunk_power, digit_char, digit_value}; use crate::parse::{ Decimal, eisel_lemire, leading_digits, radix_value, round_to_f64, scan_decimal, + string_to_number_bytes, }; use crate::shortest::{log10_pow2, log10_pow5, mul_shift}; -use crate::tables::pow5bits; +use crate::tables::{Tables, pow5bits}; #[kani::proof] #[kani::unwind(4)] @@ -212,3 +289,238 @@ fn single_limb_mul_add_is_u128_arithmetic() { kani::assume(want <= u128::MAX - u128::from(a)); assert!(b.low_u128() == want + u128::from(a)); } + +// ---- `Big` on up to two limbs: the operations that finish ---- + +#[kani::proof] +#[kani::unwind(5)] +fn big_mul_small_two_limbs_is_total() { + // Any two-limb value times any word: no panic, no overflow, no + // out-of-bounds access. (The value is not checked: a 128-bit + // multiplication against `u128` does not finish.) + let x: u128 = kani::any(); + let m: u64 = kani::any(); + let mut b = Big::from_u128(x); + b.mul_small(m); +} + +#[kani::proof] +#[kani::unwind(5)] +fn big_div_small_two_limbs_is_total() { + // Likewise for division by any word, zero included. + let x: u128 = kani::any(); + let m: u64 = kani::any(); + let mut b = Big::from_u128(x); + let _ = b.div_small(m); +} + +#[kani::proof] +#[kani::unwind(5)] +fn big_cmp_is_u128_order() { + let x: u128 = kani::any(); + let y: u128 = kani::any(); + assert!(Big::from_u128(x).cmp_big(&Big::from_u128(y)) == x.cmp(&y)); +} + +#[kani::proof] +#[kani::unwind(5)] +fn big_bit_len_and_bit_match_u128() { + let x: u128 = kani::any(); + let i: u64 = kani::any(); + let b = Big::from_u128(x); + assert!(b.bit_len() == u64::from(128 - x.leading_zeros())); + let want = i < 128 && (x >> i) & 1 == 1; + assert!(b.bit(i) == want); +} + +// ---- `FixedBig` (array-backed): the same generic code at small `N` ---- + +type B1 = FixedBig<1>; +type B2 = FixedBig<2>; +type B3 = FixedBig<3>; + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_from_u128_is_u128() { + let x: u128 = kani::any(); + let i: u64 = kani::any(); + let b = B2::from_u128(x); + assert!(b.invariant() && !b.lost()); + assert!(b.low_u128() == x); + assert!(b.bit_len() == u64::from(128 - x.leading_zeros())); + assert!(b.bit(i) == (i < 128 && (x >> i) & 1 == 1)); + assert!(b.is_zero() == (x == 0)); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_cmp_is_u128_order() { + let x: u128 = kani::any(); + let y: u128 = kani::any(); + assert!(B2::from_u128(x).cmp_big(&B2::from_u128(y)) == x.cmp(&y)); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_add_small_is_u128_sum() { + let x: u128 = kani::any(); + let a: u64 = kani::any(); + let mut b = B2::from_u128(x); + b.add_small(a); + assert!(b.invariant()); + assert!(b.low_u128() == x.wrapping_add(u128::from(a))); + assert!(b.lost() == x.checked_add(u128::from(a)).is_none()); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_mul_small_is_total() { + // Three limbs of any value times any word: total, invariant kept. + let x: [u64; 3] = kani::any(); + let m: u64 = kani::any(); + let mut b = B3::from_limbs(x); + b.mul_small(m); + assert!(b.invariant()); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_mul_small_one_limb_is_u128_product() { + let x: u64 = kani::any(); + let m: u64 = kani::any(); + let mut b = B2::from_u64(x); + b.mul_small(m); + assert!(b.invariant() && !b.lost()); + assert!(b.low_u128() == u128::from(x) * u128::from(m)); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_div_small_is_total() { + let x: [u64; 3] = kani::any(); + let d: u64 = kani::any(); + let mut b = B3::from_limbs(x); + let r = b.div_small(d); + assert!(b.invariant()); + assert!(d == 0 || r < d); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_shl_is_total_and_u128_shift() { + let x: u64 = kani::any(); + let bits: u64 = kani::any(); + let b = B2::from_u64(x); + let s = b.shl(bits); + assert!(s.invariant()); + if bits < 64 { + assert!(s.low_u128() == u128::from(x) << bits); + assert!(!s.lost()); + } +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_shl_three_limbs_is_total() { + let x: [u64; 3] = kani::any(); + let bits: u64 = kani::any(); + let s = B3::from_limbs(x).shl(bits); + assert!(s.invariant()); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_shr_is_u128_shift() { + let x: u128 = kani::any(); + let bits: u64 = kani::any(); + let (s, sticky) = B2::from_u128(x).shr(bits); + assert!(s.invariant() && !s.lost()); + if bits < 128 { + assert!(s.low_u128() == x >> bits); + assert!(sticky == (x & ((1u128 << bits) - 1) != 0)); + } else { + assert!(s.is_zero() && sticky == (x != 0)); + } +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_shr_three_limbs_is_total() { + let x: [u64; 3] = kani::any(); + let bits: u64 = kani::any(); + let (s, _) = B3::from_limbs(x).shr(bits); + assert!(s.invariant()); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_add_assign_is_u128_sum() { + let x: u128 = kani::any(); + let y: u128 = kani::any(); + let mut b = B2::from_u128(x); + b.add_assign(&B2::from_u128(y)); + assert!(b.invariant()); + assert!(b.low_u128() == x.wrapping_add(y)); + assert!(b.lost() == x.checked_add(y).is_none()); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_add_assign_three_limbs_is_total() { + let x: [u64; 3] = kani::any(); + let y: [u64; 3] = kani::any(); + let mut b = B3::from_limbs(x); + b.add_assign(&B3::from_limbs(y)); + assert!(b.invariant()); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_sub_assign_is_u128_difference() { + let x: u128 = kani::any(); + let y: u128 = kani::any(); + let mut b = B2::from_u128(x); + b.sub_assign(&B2::from_u128(y)); + assert!(b.invariant() && !b.lost()); + assert!(b.low_u128() == x.saturating_sub(y)); +} + +#[kani::proof] +#[kani::unwind(5)] +fn fixed_sub_assign_three_limbs_is_total() { + let x: [u64; 3] = kani::any(); + let y: [u64; 3] = kani::any(); + let mut b = B3::from_limbs(x); + b.sub_assign(&B3::from_limbs(y)); + assert!(b.invariant()); +} + +// ---- StringToNumber on bytes ---- + +/// Tables with no Eisel-Lemire entry: every decimal that Clinger's fast path +/// does not take goes to the exact path. +fn no_tables() -> Tables { + Tables { + el: Vec::new(), + pow5_split: Vec::new(), + pow5_inv_split: Vec::new(), + } +} + +#[kani::proof] +#[kani::unwind(6)] +fn string_to_number_bytes_three_bytes_is_total() { + // Every byte string of up to 3 bytes over an alphabet that has a digit, + // each sign, the point, both exponent letters, the radix letter, a space, + // two bytes of a multi-byte whitespace character, and `I`. + let bytes: [u8; 3] = kani::any(); + let n: usize = kani::any(); + kani::assume(n <= 3); + for b in &bytes { + kani::assume(matches!( + *b, + b'0' | b'1' | b'9' | b'.' | b'e' | b'-' | b'+' | b' ' | b'x' | b'I' | 0xE2 | 0x80 + )); + } + let _ = string_to_number_bytes::<2>(&bytes[..n], &no_tables()); +} diff --git a/crates/clazel-js-number/src/tables.rs b/crates/clazel-js-number/src/tables.rs index de9637b99..934de29aa 100644 --- a/crates/clazel-js-number/src/tables.rs +++ b/crates/clazel-js-number/src/tables.rs @@ -19,7 +19,10 @@ //! (Ulf Adams, "Ryu: fast float-to-string conversion", PLDI 2018, section //! 3, `d2s_full_table.h`.) -use crate::bigint::Big; +use crate::fixed::{BIG_LIMBS_FORMAT, FixedBig}; + +/// The big integer the tables are computed with. +type Big = FixedBig; use std::sync::OnceLock; /// The smallest decimal exponent the Eisel–Lemire table covers. diff --git a/crates/clazel-js-number/tests/differential.rs b/crates/clazel-js-number/tests/differential.rs index 61ea628f0..09b8fb152 100644 --- a/crates/clazel-js-number/tests/differential.rs +++ b/crates/clazel-js-number/tests/differential.rs @@ -13,12 +13,15 @@ //! `tail`, `split`, `parse_f64`) stand in for slicing and `unwrap`: a failed //! lookup gives an empty string or NaN, and the assertion that follows fails. -use clazel_js_number::bigint::Big; +use clazel_js_number::fixed::FixedBig; use clazel_js_number::shortest::shortest_digits; use clazel_js_number::{ RangeError, number_to_string, string_to_number, to_exponential, to_fixed, to_precision, }; +/// Room for the exact decimal expansions of the midpoints (about 2,600 bits). +type Big = FixedBig<64>; + /// SplitMix64 (Steele, Lea and Flood 2014): a fixed-seed generator, so a /// failure reproduces. struct Rng(u64); diff --git a/crates/clazel-js-number/tests/fixed.rs b/crates/clazel-js-number/tests/fixed.rs new file mode 100644 index 000000000..1051cb8dd --- /dev/null +++ b/crates/clazel-js-number/tests/fixed.rs @@ -0,0 +1,122 @@ +//! `FixedBig<2>` against `u128` arithmetic, including the cases where the +//! result does not fit and `lost` must say so. + +use clazel_js_number::fixed::FixedBig; +use std::cmp::Ordering; + +type B = FixedBig<2>; + +/// SplitMix64: a fixed-seed generator, so a failure reproduces. +struct Rng(u64); + +impl Rng { + fn next(&mut self) -> u64 { + self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); + let mut z = self.0; + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) + } + /// A value with a random bit length, so short values are common. + fn wide(&mut self) -> u128 { + let v = (u128::from(self.next()) << 64) | u128::from(self.next()); + v.checked_shr((self.next() % 128) as u32).unwrap_or(0) + } +} + +#[test] +fn matches_u128() { + let mut rng = Rng(0x00F1_8ED0_0000_0001); + let n = if cfg!(miri) { 20 } else { 200_000 }; + for _ in 0..n { + let x = rng.wide(); + let y = rng.wide(); + let w = rng.next(); + let ww = u128::from(w); + let bx = B::from_u128(x); + let by = B::from_u128(y); + assert!(bx.invariant() && by.invariant()); + assert_eq!(bx.low_u128(), x); + assert_eq!(bx.bit_len(), u64::from(128 - x.leading_zeros())); + assert_eq!(bx.cmp_big(&by), x.cmp(&y)); + assert_eq!(bx.cmp_big(&bx), Ordering::Equal); + + let mut s = bx.clone(); + s.add_assign(&by); + assert!(s.invariant()); + assert_eq!(s.low_u128(), x.wrapping_add(y)); + assert_eq!(s.lost(), x.checked_add(y).is_none()); + + let mut d = bx.clone(); + d.sub_assign(&by); + assert!(d.invariant()); + assert_eq!(d.low_u128(), x.saturating_sub(y)); + assert!(!d.lost()); + + let mut m = bx.clone(); + m.mul_small(w); + assert!(m.invariant()); + assert_eq!(m.low_u128(), x.wrapping_mul(ww)); + assert_eq!(m.lost(), x.checked_mul(ww).is_none()); + + let mut a = bx.clone(); + a.add_small(w); + assert!(a.invariant()); + assert_eq!(a.low_u128(), x.wrapping_add(ww)); + assert_eq!(a.lost(), x.checked_add(ww).is_none()); + + let mut q = bx.clone(); + let r = q.div_small(w); + assert!(q.invariant()); + if w == 0 { + assert_eq!((q.low_u128(), r), (x, 0)); + } else { + assert_eq!(q.low_u128(), x.checked_div(ww).unwrap_or(0)); + assert_eq!(u128::from(r), x.checked_rem(ww).unwrap_or(0)); + } + + let sh = rng.next() % 140; + let shu = sh as u32; + let l = bx.shl(sh); + assert!(l.invariant()); + let shifted = x.checked_shl(shu).unwrap_or(0); + let fits = sh < 128 && shifted.checked_shr(shu) == Some(x); + assert_eq!(l.lost(), !fits && x != 0); + if sh < 128 { + assert_eq!(l.low_u128(), shifted); + } + let (rs, sticky) = bx.shr(sh); + assert!(rs.invariant()); + if sh < 128 { + assert_eq!(rs.low_u128(), x.checked_shr(shu).unwrap_or(0)); + let mask = 1u128.checked_shl(shu).unwrap_or(0).wrapping_sub(1); + assert_eq!(sticky, x & mask != 0); + } else { + assert_eq!(rs.low_u128(), 0); + assert_eq!(sticky, x != 0); + } + + // divrem is exact while the divisor leaves a spare bit. + if y != 0 && y < 1 << 127 { + let (qq, rr) = B::divrem(&bx, &by); + assert!(qq.invariant() && rr.invariant()); + assert_eq!(qq.low_u128(), x.checked_div(y).unwrap_or(0)); + assert_eq!(rr.low_u128(), x.checked_rem(y).unwrap_or(0)); + assert!(!qq.lost()); + } + let (z, same) = B::divrem(&bx, &B::zero()); + assert!(z.is_zero()); + assert_eq!(same, bx); + } +} + +#[test] +fn zero_and_decimal() { + assert!(B::zero().is_zero()); + assert_eq!(B::zero().to_decimal(), "0"); + assert_eq!(B::from_u128(u128::MAX).to_decimal(), u128::MAX.to_string()); + assert!(!B::from_u128(u128::MAX).lost()); + // 10^40 does not fit in 128 bits. + assert!(B::pow10(40).lost()); + assert!(!B::pow10(38).lost()); +} diff --git a/crates/clazel-js-number/tests/trim.rs b/crates/clazel-js-number/tests/trim.rs new file mode 100644 index 000000000..df2bc736f --- /dev/null +++ b/crates/clazel-js-number/tests/trim.rs @@ -0,0 +1,58 @@ +//! The byte-level `trim_str_whitespace` against `str::trim_matches` with +//! `is_str_whitespace`, for every Unicode scalar value at the start, at the +//! end, alone and doubled, and for the digit-radix helper's callers. + +use clazel_js_number::parse::{is_str_whitespace, string_to_number, trim_str_whitespace}; + +fn same(s: &str) { + let want = s.trim_matches(is_str_whitespace).as_bytes(); + assert_eq!(trim_str_whitespace(s.as_bytes()), want, "trim of {s:?}"); +} + +#[test] +fn trim_matches_str_trim_for_every_scalar() { + let step = if cfg!(miri) { 4099 } else { 1 }; + let mut cp: u32 = 0; + while cp <= 0x0010_FFFF { + if let Some(c) = char::from_u32(cp) { + same(&c.to_string()); + same(&format!("{c}{c}")); + same(&format!("{c}1")); + same(&format!("1{c}")); + same(&format!("{c}1{c}")); + same(&format!(" {c} 12.5{c}\t{c}")); + } + cp = cp.saturating_add(step); + } + same(""); + same("\u{2028}\u{2029}\u{FEFF}\u{A0}\u{3000}x\u{1680}\u{200A}"); +} + +#[test] +fn whitespace_set_is_what_the_spec_lists() { + for s in [ + "\u{9}7", + "\u{A}7", + "\u{B}7", + "\u{C}7", + "\u{D}7", + " 7", + "\u{A0}7", + "\u{1680}7", + "\u{2000}7", + "\u{200A}7", + "\u{2028}7", + "\u{2029}7", + "\u{202F}7", + "\u{205F}7", + "\u{3000}7", + "\u{FEFF}7", + "7\u{3000}", + ] { + assert!(string_to_number(s).to_bits() == 7f64.to_bits(), "{s:?}"); + } + // Not whitespace: ZWSP (U+200B), the Mongolian vowel separator, NEL. + for s in ["\u{200B}7", "\u{180E}7", "\u{85}7", "7\u{200B}"] { + assert!(string_to_number(s).is_nan(), "{s:?}"); + } +} diff --git a/crates/clazel-js-regexp/Cargo.toml b/crates/clazel-js-regexp/Cargo.toml index 71e3004d6..410574b52 100644 --- a/crates/clazel-js-regexp/Cargo.toml +++ b/crates/clazel-js-regexp/Cargo.toml @@ -4,7 +4,7 @@ version = "0.0.0" edition.workspace = true license.workspace = true publish.workspace = true -description = "ECMAScript regular expressions: the pattern parser and its early errors (phase-7.md item 7.10, first half). The matcher and the Unicode tables are not written; the crate holds the vendored Unicode Character Database files." +description = "ECMAScript regular expressions: the pattern parser and early errors, and the backtracking matcher with a bounded stack, case folding and property tables (phase-7.md item 7.10)." [dependencies] # ID_Start and ID_Continue above U+007F, for group names (RegExpIdentifierStart @@ -14,10 +14,10 @@ clazel-js-lexer = { path = "../clazel-js-lexer" } # Verification tier (docs/VERIFICATION.md). `tier` is what is proven about the # shipped code today; `target-tier` is what phase-7.md's "Crates" table sets. -# The parser is written and tested (tests/), and src/parser/proofs.rs holds Kani -# harnesses for it, but `tier` stays 0 until CI's full verification of them is -# green on a pull request (CLAUDE.md rule 7), and the matcher, which the -# target tier also covers, does not exist. +# The parser and the matcher are written and tested (tests/), and +# src/parser/proofs.rs and src/proofs.rs hold Kani harnesses for pieces of +# them, but `tier` stays 0 until CI's full verification of them is green on a +# pull request (CLAUDE.md rule 7) and the gaps the README lists are closed. [package.metadata.clazel] tier = 0 target-tier = 1 diff --git a/crates/clazel-js-regexp/README.md b/crates/clazel-js-regexp/README.md index 980433f46..3be8293e5 100644 --- a/crates/clazel-js-regexp/README.md +++ b/crates/clazel-js-regexp/README.md @@ -4,10 +4,13 @@ Part of [Clazel](../../README.md). The ECMAScript pattern parser, the matcher and the Unicode tables behind `\p{..}` and case folding ([phase-7.md](../../docs/phases/phase-7.md) item 7.10). -**Written: the pattern parser and its early errors. Not written: the matcher, -the case-folding tables and the property tables.** Tier 0 (target tier 1). +**Written: the pattern parser and its early errors; the backtracking matcher +with a bounded stack, a step budget and an interrupt hook; the case-folding +tables, and the General_Category, binary-property, Script, Script_Extensions +and properties-of-strings tables.** +Tier 0 (target tier 1): see "What is proven". -The Unicode 18.0.0 files the tables will be generated from are vendored in +The Unicode 18.0.0 files the tables are generated from are vendored in `tests/data/ucd-regexp/` (see `SOURCE.md` there). ## The parser @@ -52,12 +55,12 @@ choices at its top. **Not done** -- `\p{Script=..}` and `\p{Script_Extensions=..}` values are accepted by - syntax. A script table plugs in through `props::Names` (`parse_with`); the - generated tables of item 7.10's second half are where it comes from. - `General_Category` values, the binary properties and the properties of +- `General_Category` values, the binary properties and the properties of strings are checked, against lists in `src/props.rs` that - `tests/conformance_names.rs` compares with the vendored UCD. + `tests/conformance_names.rs` compares with the vendored UCD. `Script` and + `Script_Extensions` values are checked by `parse` against the generated + `tables::SCRIPTS` (`props::UcdScripts`); `parse_with` takes any other + `props::Names`. - The section numbers are not checked against the specification's text (†). - There is no `spec.rs`: the parser is the transcription, with the grammar's production names at each step. No Verus. @@ -66,6 +69,122 @@ choices at its top. only), so `new RegExp(..)` strings and the generated property-escape tests are not run. +## The matcher + +```rust +let pattern = parse(&pattern_units, flags)?; // §22.2.1† +let program = compile(&pattern, flags)?; // §22.2.2† as instructions +let m = program.exec(&input, start, sticky, &Limits::default(), None)?; +``` + +`Program::exec` matches UTF-16 input (`&[u16]`) from a code unit index +`start`: only there with `sticky` (`y`), else scanning forward. `lastIndex`, +`g` and what to do with the result are the caller's. A `Match` holds the +captures as code unit index pairs (index 0 the whole match; `None` for a group +that did not take part); `Program::named_capture` finds the group of a name, +duplicate names included. + +Spec: §22.2.2† (Pattern Semantics), section numbers marked † as everywhere. +`src/compile.rs` and `src/vm.rs` list the modelling choices at their tops. + +**Covered** + +- Alternation, groups (capturing, named, duplicate names, non-capturing), + greedy and lazy quantifiers with RepeatMatcher's empty check and the reset of + captures on each iteration, counted quantifiers (`{n,m}` is a counter, not a + copy), backreferences (numbered, named, backward ones in lookbehind), + `^ $ \b \B` with `m`, `.` with and without `s`, classes and the escapes + `\d \D \s \S \w \W`. +- Lookahead and lookbehind, positive and negative. A lookbehind matches + backwards (terms right to left, characters read before the position, groups + capture end first); bodies are atomic. +- `i`: `Canonicalize` without `u`/`v` is `toUppercase` of the code unit (never + into or out of ASCII, never to more than one unit), with `u`/`v` simple case + folding; `\w`/`\b` get U+017F and U+212A with `u`/`v` and `i`. Classes ask, + as the standard does, whether some member has the character's canonical form + (so `/\P{Lu}/iu` matches `A`, and `v` mode does not). +- `u` and `v` (code points): `.` and classes match a pair as one character, + a lone surrogate is itself, and a search steps by code point. +- Modifiers `(?ims-ims:..)`, as compile-time flag scopes. +- `v` class sets: union, `&&`, `--`, nested and negated classes, ranges, and + `\q{..}` with strings (matched longest first, then single characters, then + the empty string). +- `\p{..}` and `\P{..}` for `General_Category` (every value and alias) and + the binary properties of ECMA-262's table (`Any`, `ASCII`, `Assigned` and + the others from tables generated from the UCD). +- `\p{Script=..}`, `\p{sc=..}`, `\p{Script_Extensions=..}`, `\p{scx=..}` and + their `\P` forms, for every value of `PropertyValueAliases.txt` by long + name, short name or further alias (`Qaai` for `Inherited`), including + `Unknown` (`Zzzz`: every code point `Scripts.txt` does not list) and + `Katakana_Or_Hiragana` (`Hrkt`: no code points). Under `i` they follow the + same per-leaf rule as the other properties (`/\P{sc=Greek}/iu` matches + `\u03b9`, `v` mode does not). +- Properties of strings with `v`: `Basic_Emoji`, `Emoji_Keycap_Sequence`, + `RGI_Emoji_Modifier_Sequence`, `RGI_Emoji_Flag_Sequence`, + `RGI_Emoji_Tag_Sequence`, `RGI_Emoji_ZWJ_Sequence` and `RGI_Emoji` (their + union, UTS #51 ED-27), as an atom or in a class set, with `&&` and `--`: the + strings of two or more code points longest first, then the one-character + members (only `Basic_Emoji` has any), as for `\q{..}`. +- **An explicit backtrack stack, no recursion over the pattern or the input.** + Choice points and register changes are entries of a `Vec` capped at + `Limits::max_stack` entries (default a million, 16 to 24 bytes each). A + match that needs more is `MatchError::StackOverflow`, which the engine turns + into a `RangeError`: a listed deviation (the standard has no limit). A + greedy loop over one character is one entry for the whole run. +- **A step budget and an interrupt hook** for 7.0(g): `Limits::max_steps` + (`MatchError::StepLimit`), and a closure asked every 4096 steps + (`MatchError::Interrupted`). A catastrophic pattern such as `(a*)*b` runs + until one of these; the empty check and the one-entry runs keep its stack + shallow, so it is the step budget that ends it, while `(?:a|aa)*b` on a long + run of `a` fills the stack first and ends in `StackOverflow` + (`tests/limits.rs`). +- Compilation recurses over the pattern, bounded by the parser's `MAX_DEPTH` + (a deeper hand-built tree is `CompileError::TooDeep`); matching does not. + +**Not done** + +- A string property is matched as a flat list of alternatives, one + instruction run per string (about 2 800 strings for `RGI_Emoji`), not a + trie: correct, but a search for `\p{RGI_Emoji}` tries every string at each + start. `CompileError::UnsupportedProperty` is now only for a tree built by + hand with a name the tables do not hold. +- The `RegExp` object: `lastIndex`, `exec`'s result object, `d` indices, + `Symbol.replace` and the rest (7.10's other items). +- Test262's `built-ins/RegExp` is not vendored, and the + `language/literals/regexp` tests that run a match need a JavaScript engine + (they assert on the result of `.exec`), so no Test262 match test is run + here. The conformance tests are the standard's own worked examples + (`tests/matching.rs`). +- Two choices, listed in `src/vm.rs`: a match that starts inside a surrogate + pair with `u` starts at the pair, and the stack bound's allocation can be up + to twice `max_stack` entries (the `Vec` doubles). +- The section numbers are not checked against the specification's text (†). +- No Verus. No `spec.rs`: the compiler and the matcher are the transcription. + +## The tables + +`src/tables.rs` is generated by `tests/tables.rs` from `tests/data/ucd-regexp/` +(there is no `cargo xtask tables` step: the generator is a test, so no other +crate's check is affected). The test `tables_are_current` fails if the +committed file differs from what the generator writes; to rewrite it run +`CLAZEL_WRITE_TABLES=1 cargo test -p clazel-js-regexp --test tables` and +`cargo fmt`. Contents: Simple Case Folding (`CaseFolding.txt`, `C` and `S`), +`toUppercase` of the BMP (`SpecialCasing.txt` and `UnicodeData.txt`), +`General_Category` (`DerivedGeneralCategory.txt`), and the binary properties +(`PropList.txt`, `DerivedCoreProperties.txt`, `DerivedNormalizationProps.txt`, +`DerivedBinaryProperties.txt`, `emoji-data.txt`), `Script` and +`Script_Extensions` per script (`Scripts.txt`, `ScriptExtensions.txt`, names +from `PropertyValueAliases.txt`), and the six emoji sequence types +(`emoji-sequences.txt`, `emoji-zwj-sequences.txt`; `RGI_Emoji` is their +union). About 29 500 lines. +Independent checks in the same file: the categories against `UnicodeData.txt` +for every code point, the equivalence classes (`unicode::equivalents`) against +`canonicalize` for every code point, folding idempotent, the word-character +extras exactly U+017F and U+212A, every property name resolving, every script +name resolving, `Script` and `Script_Extensions` against `Scripts.txt` and +`ScriptExtensions.txt` for every code point (read again, not through the +generator), and the string properties against the emoji files. + ## Evidence - `tests/parser.rs`: about 240 hand-written accept and reject cases by mode @@ -80,11 +199,50 @@ choices at its top. and reason (lexical errors, which are `clazel-js-lexer`'s; patterns built by computing a string; two tests whose error is in the JavaScript parser). - `tests/conformance_names.rs`: the property names against the vendored UCD. +- `tests/matching.rs`: 234 hand-written matches by feature, with the + result the standard's algorithm gives (several are the standard's own + worked examples: `(z)((a+)?(b+)?(c))*`, `(?=(a+))a*b\1`, + `(.*?)a(?!(a+)b\2c)\2(.*)`, `(?<=(\d+)(\d+))$`), start indexes and `y`, + long inputs (100 000 characters, counts of 100 000 and 4 000 000 000). +- `tests/limits.rs`: catastrophic patterns end in `StepLimit` or + `StackOverflow`; the stack bound is exact; the hook is asked and stops. +- `tests/differential.rs`: a reference matcher (a recursive, exponential, + spec-literal transcription of §22.2.2† over characters, with closures for + continuations, sharing only the parser, the case-folding function and the + tables) against the matcher, over random patterns and inputs: 12 000 + patterns, about 87 000 comparisons (about 52 000 with a match), each at + every start with and without `y`, in all of `i m s u v`. The test is not + vacuous: injecting a bug into the empty check, the backward group capture, + `\b`, the word-character extras, lookahead negation, or subtraction + operand order each made it fail. A reduced count under Miri. A second test + runs 33 patterns for `Script`, `Script_Extensions` and strings (order of + the alternatives, `\P` under `iu` and `iv`, set operations, lookbehind) on + every input of up to two tokens and a sample of three, about 19 000 + comparisons; making the strings shortest-first made it fail. The reference + reads the same tables, so the tables are checked by `tests/tables.rs`. +- `tests/tables.rs`: the generated tables against the UCD, above. - `src/proofs.rs`: the Kani harnesses, below. ## Kani harnesses (compiled only under `cargo kani`) -`src/parser/proofs.rs`. Each runs one step of the parser on every input up to +**The matcher's** (`src/proofs.rs`): + +| Harness | Checks | +|---------|--------| +| `canonicalize_unit_any` | for every code unit, `Canonicalize` without `u` is a code unit and a non-ASCII unit never becomes ASCII | +| `simple_fold_any` | simple case folding of every code point is a code point | +| `stack_push_respects_the_bound` | any bound up to 4, six pushes: the length never exceeds the bound, a push is refused exactly when full | +| `stack_pop_returns_the_last_push` | a pop returns the last push, an empty stack nothing | +| `stack_commit_look_keeps_only_the_undos` | a positive lookaround's commit drops the mark and the choices above it, keeps the undo entries in order, returns the mark's position | +| `read_any_3` | reading a character at any position of any 3-unit input, either direction, either mode: the new position is inside and one or two units away, the character valid | + +Not here: the matcher loop and the compiler (they build `Vec`s in loops, which +CBMC does not finish on, as for the parser below), and that the inverse case +table agrees with the forward one (a bisection twice over a table, with a +growing `Vec`: over ten CPU-minutes). Those are tested: the differential, and +`tests/tables.rs` for every code point. + +**The parser's** (`src/parser/proofs.rs`). Each runs one step of the parser on every input up to a bound (a symbolic array of code units, a symbolic length, a symbolic start position), and checks that no panic, overflow or out-of-bounds read is reachable, with unwinding assertions that every loop finishes within the stated @@ -119,5 +277,11 @@ duplicate-name check (`names_in_disjunction`) is in no harness either. Group names are checked only below U+0080: the lexer's `ID_Start` table lookup, which Verus proves total there, is a bisection that costs CBMC minutes. -**The tier stays 0** until CI's full verification of the harnesses has passed -on a pull request (CLAUDE.md rule 7), and the whole-parse gap above is closed. +## What is proven + +Nothing beyond the harnesses above, and none of them has had CI's full +verification on a pull request. **The tier stays 0** until it has (CLAUDE.md +rule 7), and the whole-parse gap and the matcher gap above are closed. The +matcher is tested, not proven: that it implements §22.2.2† rests on the +differential against a reference written from the same reading of the +standard, and on the hand-written cases. diff --git a/crates/clazel-js-regexp/src/compile.rs b/crates/clazel-js-regexp/src/compile.rs new file mode 100644 index 000000000..f80da75dc --- /dev/null +++ b/crates/clazel-js-regexp/src/compile.rs @@ -0,0 +1,838 @@ +//! From the syntax tree to instructions: ECMA-262 §22.2.2† (Pattern +//! Semantics), one production at a time. Section numbers marked † are from +//! memory (the specification's text is not vendored). +//! +//! # Modelling choices +//! +//! - **Continuations become jumps.** The standard's Matcher is a closure from +//! a MatchState and a continuation to a MatchState or failure. Here a +//! Matcher is a run of instructions that falls through to the next one on +//! success, and a failure is a pop of the backtrack stack. Every place the +//! standard tries one continuation and then another (alternation, a greedy +//! or lazy quantifier, a class with strings) is a [`Inst::Split`] or a +//! stack entry, in the order the standard tries them. +//! - **Direction.** A lookbehind body is compiled with `direction` backward +//! (§22.2.2.3.1† Alternative: "the terms are matched right to left"): its +//! terms in reverse order, its characters read before the position, and its +//! groups closing with the end first. A lookahead inside a lookbehind is +//! forward again, and the other way round. +//! - **Modifiers.** `i`, `m` and `s` are compile-time state: `(?i-s:..)` +//! compiles its body with them changed, so no instruction asks for the flag +//! at match time. `u` and `v` are the pattern's, and cannot be modified. +//! - **Counting.** `{n,m}` is a counter register and a loop, never `n` copies +//! of the body. A greedy quantifier over one character with no captures is +//! one instruction ([`Inst::Run`]) and one stack entry for the whole run. +//! - **Classes.** A character class is a postfix program over its leaves (a +//! character, a range, an escape) and `not`, `or`, `and`, `minus`, so that +//! matching a character needs no recursion over the class's nesting. With +//! `i`, each leaf asks whether *some* character with the same `Canonicalize` +//! as the input's is in it (CharacterSetMatcher, §22.2.2.7.1†), which is the +//! standard's rule for classes without `v`; with `v` the standard folds each +//! leaf first and then combines them, which is the same formula applied leaf +//! by leaf, with `\P`, `\D`, `\S`, `\W` and `[^..]` as a `not` over the +//! positive leaf. (Without `v`, `\P{Lu}` with `iu` matches `a`: the complement +//! is taken before folding, as the standard does.) +//! - **Strings in classes (`v`).** A class that can match strings of more or +//! fewer than one character is an alternation: the strings, longest first +//! (ties in code point order), then the single characters, then the empty +//! string (§22.2.2.7† CompileAtom, "CharacterClass"). +//! - **Recursion** over the tree is bounded by [`MAX_DEPTH`]: the parser +//! never builds a deeper one, and a tree built by hand that is deeper is +//! [`CompileError::TooDeep`]. The matcher itself does not recurse. + +use std::collections::BTreeSet; + +use crate::ast::{ + Alternative, Assertion, Atom, CharacterClass, ClassContents, ClassEscape, ClassItem, + ClassSetExpr, ClassSetItem, ClassSetOperand, Disjunction, Group, Look, Pattern, Property, + Quantifier, Term, TermKind, +}; +use crate::flags::Flags; +use crate::parser::MAX_DEPTH; +use crate::program::{ClassOp, ClassProg, EscKind, Inst, One, OneKind, Program}; +use crate::unicode::{self, Prop, canonicalize, simple_fold}; + +/// Why a pattern cannot be compiled. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum CompileError { + /// The tree nests deeper than [`MAX_DEPTH`] (a tree from + /// [`crate::parse`] never does). + TooDeep, + /// A backreference to a group that does not exist (a tree from + /// [`crate::parse`] never has one). + BackReference(u32), + /// A `\p{..}` whose table is not generated: a name or value the tables do + /// not hold (a tree from [`crate::parse`] never has one). The text names + /// it. + UnsupportedProperty(String), + /// More than 2^32 instructions or registers. + TooLarge, +} + +impl core::fmt::Display for CompileError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + CompileError::TooDeep => write!(f, "pattern nests too deeply"), + CompileError::BackReference(n) => write!(f, "no group {n} to refer to"), + CompileError::UnsupportedProperty(p) => write!(f, "unsupported property {p}"), + CompileError::TooLarge => write!(f, "pattern too large"), + } + } +} + +type R = Result; + +/// Compiles `pattern`, parsed with `flags`, into a [`Program`]. +/// +/// # Errors +/// +/// [`CompileError`]: a `\p{..}` that has no table, or (for a tree not made by +/// [`crate::parse`]) too deep or with a dangling backreference. +pub fn compile(pattern: &Pattern, flags: Flags) -> Result { + let regs0 = pattern + .capture_count + .checked_add(1) + .and_then(|n| n.checked_mul(2)) + .ok_or(CompileError::TooLarge)?; + let mut c = Compiler { + unicode: flags.unicode_mode(), + v: flags.unicode_sets, + capture_count: pattern.capture_count, + group_names: &pattern.group_names, + insts: Vec::new(), + classes: Vec::new(), + backrefs: Vec::new(), + next_reg: regs0, + }; + let st = St { + ic: flags.ignore_case, + ml: flags.multiline, + dot_all: flags.dot_all, + back: false, + }; + c.disjunction(&pattern.disjunction, st, 1)?; + c.emit(Inst::Match)?; + Ok(Program { + insts: c.insts, + classes: c.classes, + backrefs: c.backrefs, + regs: usize::try_from(c.next_reg).map_err(|_| CompileError::TooLarge)?, + capture_count: pattern.capture_count, + unicode: c.unicode, + group_names: pattern.group_names.clone(), + }) +} + +/// The compile-time state: the flags in scope and the direction. +#[derive(Debug, Clone, Copy)] +struct St { + ic: bool, + ml: bool, + dot_all: bool, + back: bool, +} + +struct Compiler<'a> { + unicode: bool, + v: bool, + capture_count: u32, + group_names: &'a [(Vec, u32)], + insts: Vec, + classes: Vec, + backrefs: Vec>, + next_reg: u32, +} + +/// What a quantifier repeats. +enum Body<'t> { + One(One), + Kind(&'t TermKind), +} + +/// An alternative of a class with strings. +enum Alt { + Str(Vec), + One(One), + Empty, +} + +fn to_usize(n: u64) -> usize { + usize::try_from(n).unwrap_or(usize::MAX) +} + +impl Compiler<'_> { + fn pc(&self) -> R { + u32::try_from(self.insts.len()).map_err(|_| CompileError::TooLarge) + } + + fn emit(&mut self, i: Inst) -> R { + let at = self.pc()?; + self.insts.push(i); + Ok(at) + } + + fn set(&mut self, at: u32, i: Inst) { + if let Some(slot) = self.insts.get_mut(at as usize) { + *slot = i; + } + } + + fn reg(&mut self) -> R { + let r = self.next_reg; + self.next_reg = r.checked_add(1).ok_or(CompileError::TooLarge)?; + Ok(r) + } + + fn add_class(&mut self, c: ClassProg) -> R { + let id = u32::try_from(self.classes.len()).map_err(|_| CompileError::TooLarge)?; + self.classes.push(c); + Ok(id) + } + + /// Disjunction (§22.2.2.3.1†): `Alternative | Disjunction` tries the left + /// alternative, then the right one. + fn disjunction(&mut self, d: &Disjunction, st: St, depth: usize) -> R { + if depth > MAX_DEPTH.saturating_add(1) { + return Err(CompileError::TooDeep); + } + let n = d.alternatives.len(); + let mut jumps = Vec::new(); + for (i, alt) in d.alternatives.iter().enumerate() { + if i.saturating_add(1) >= n { + self.alternative(alt, st, depth)?; + } else { + let split = self.emit(Inst::Split { + first: 0, + second: 0, + })?; + let first = self.pc()?; + self.alternative(alt, st, depth)?; + jumps.push(self.emit(Inst::Jmp(0))?); + let second = self.pc()?; + self.set(split, Inst::Split { first, second }); + } + } + let end = self.pc()?; + for j in jumps { + self.set(j, Inst::Jmp(end)); + } + Ok(()) + } + + /// Alternative (§22.2.2.3.1†): the terms in order, or with `direction` + /// backward in reverse order. + fn alternative(&mut self, a: &Alternative, st: St, depth: usize) -> R { + if st.back { + for t in a.terms.iter().rev() { + self.term(t, st, depth)?; + } + } else { + for t in &a.terms { + self.term(t, st, depth)?; + } + } + Ok(()) + } + + fn term(&mut self, t: &Term, st: St, depth: usize) -> R { + match &t.quantifier { + None => self.kind(&t.kind, st, depth), + Some(q) => self.quantified(&t.kind, q, st, depth), + } + } + + fn kind(&mut self, k: &TermKind, st: St, depth: usize) -> R { + match k { + TermKind::Assertion(a) => { + let extras = self.unicode && st.ic; + let inst = match a { + Assertion::Start => Inst::LineStart { multiline: st.ml }, + Assertion::End => Inst::LineEnd { multiline: st.ml }, + Assertion::WordBoundary => Inst::WordBoundary { neg: false, extras }, + Assertion::NotWordBoundary => Inst::WordBoundary { neg: true, extras }, + }; + self.emit(inst).map(|_| ()) + } + TermKind::Look(l) => self.look(l, st, depth), + TermKind::Atom(a) => self.atom(a, st, depth), + } + } + + /// Assertion `(?=`, `(?!`, `(?<=`, `(? R { + let start = self.emit(Inst::LookStart { + neg: l.negative, + end: 0, + })?; + let inner = St { + back: l.behind, + ..st + }; + self.disjunction(&l.body, inner, depth.saturating_add(1))?; + self.emit(Inst::LookEnd { neg: l.negative })?; + let end = self.pc()?; + self.set( + start, + Inst::LookStart { + neg: l.negative, + end, + }, + ); + Ok(()) + } + + fn emit_body(&mut self, b: &Body<'_>, st: St, depth: usize) -> R { + match b { + Body::One(o) => self.emit(Inst::One(o.clone())).map(|_| ()), + Body::Kind(k) => self.kind(k, st, depth), + } + } + + /// Term :: Atom Quantifier (§22.2.2.3.1†, RepeatMatcher §22.2.2.3.2†). + fn quantified(&mut self, k: &TermKind, q: &Quantifier, st: St, depth: usize) -> R { + let min = to_usize(q.min); + let max = q.max.map_or(usize::MAX, to_usize); + // RepeatMatcher step 1: "If max = 0, return c(x)". + if max == 0 { + return Ok(()); + } + let one = match k { + TermKind::Atom(a) => self.one_of(a, st, depth)?, + _ => None, + }; + if q.greedy + && let Some(o) = &one + { + return self + .emit(Inst::Run { + one: o.clone(), + min, + max, + }) + .map(|_| ()); + } + let body = match one { + Some(o) => Body::One(o), + None => Body::Kind(k), + }; + if min == 1 && max == 1 { + return self.emit_body(&body, st, depth); + } + let counter = self.reg()?; + let pos_reg = self.reg()?; + self.emit(Inst::CounterInit { reg: counter })?; + let head = self.emit(Inst::LoopHead { + counter, + min, + max, + greedy: q.greedy, + body: 0, + exit: 0, + })?; + let body_pc = self.pc()?; + // Step 4: "For each integer k in the inclusive interval from + // parenIndex + 1 to parenIndex + parenCount, set cap[k] to undefined." + if let Some((first, last)) = cap_range(k) { + self.emit(Inst::ResetCaps { first, last })?; + } + self.emit(Inst::SavePos { reg: pos_reg })?; + self.emit_body(&body, st, depth)?; + // Step 2.b: "If min = 0 and y's endIndex = x's endIndex, return + // failure." + self.emit(Inst::LoopEnd { + counter, + pos_reg, + min, + head, + })?; + let exit = self.pc()?; + self.set( + head, + Inst::LoopHead { + counter, + min, + max, + greedy: q.greedy, + body: body_pc, + exit, + }, + ); + Ok(()) + } + + /// The matcher for an atom that is one character, with no captures, or + /// `None` for the others (groups, backreferences, a class with strings). + fn one_of(&mut self, a: &Atom, st: St, depth: usize) -> R> { + let kind = match a { + Atom::Char(c) => OneKind::Char { + c: if st.ic { + canonicalize(*c, self.unicode) + } else { + *c + }, + ic: st.ic, + }, + Atom::Dot => OneKind::Dot { all: st.dot_all }, + // `\p{RGI_Emoji}` and the like (`v`): strings, so an alternation + // (§22.2.2.7† CompileAtom, "CharacterClassEscape"); see `atom`. + Atom::Escape(e) if e.may_contain_strings() => return Ok(None), + Atom::Escape(e) => { + let mut ops = Vec::new(); + self.escape(e, &mut ops)?; + let id = self.add_class(ClassProg { + ops, + ic: st.ic, + invert: false, + word_extras: self.unicode && st.ic, + })?; + OneKind::Class(id) + } + Atom::Class(c) => { + if !self.class_strings_set(c, st, depth)?.is_empty() { + return Ok(None); + } + let prog = self.class_prog(c, st, depth)?; + OneKind::Class(self.add_class(prog)?) + } + _ => return Ok(None), + }; + Ok(Some(One { + kind, + back: st.back, + })) + } + + fn atom(&mut self, a: &Atom, st: St, depth: usize) -> R { + if let Some(one) = self.one_of(a, st, depth)? { + return self.emit(Inst::One(one)).map(|_| ()); + } + match a { + Atom::Group(g) => self.group(g, st, depth), + Atom::Modified(m) => { + // §22.2.1†, RegularExpressionModifiers: `add` turns a flag on + // and `remove` turns it off, for the body only. + let flag = |cur: bool, add: bool, remove: bool| (cur || add) && !remove; + let inner = St { + ic: flag(st.ic, m.add.ignore_case, m.remove.ignore_case), + ml: flag(st.ml, m.add.multiline, m.remove.multiline), + dot_all: flag(st.dot_all, m.add.dot_all, m.remove.dot_all), + back: st.back, + }; + self.disjunction(&m.body, inner, depth.saturating_add(1)) + } + Atom::BackReference(n) => { + if *n == 0 || *n > self.capture_count { + return Err(CompileError::BackReference(*n)); + } + self.backref(vec![*n], st) + } + Atom::NamedBackReference(name) => { + let groups: Vec = self + .group_names + .iter() + .filter(|(n, _)| n == name) + .map(|&(_, i)| i) + .collect(); + if groups.is_empty() { + return Err(CompileError::BackReference(0)); + } + self.backref(groups, st) + } + Atom::Class(c) => self.class_with_strings(c, st, depth), + // A property of strings outside a class is the class that holds + // only it. + Atom::Escape(e) => { + let class = CharacterClass { + negated: false, + contents: ClassContents::Set(ClassSetExpr::Union(vec![ClassSetItem::Operand( + ClassSetOperand::Escape(e.clone()), + )])), + }; + self.class_with_strings(&class, st, depth) + } + // One character, handled by `one_of`. + Atom::Char(_) | Atom::Dot => Ok(()), + } + } + + fn backref(&mut self, groups: Vec, st: St) -> R { + let id = u32::try_from(self.backrefs.len()).map_err(|_| CompileError::TooLarge)?; + self.backrefs.push(groups); + self.emit(Inst::BackRef { + id, + ic: st.ic, + back: st.back, + }) + .map(|_| ()) + } + + /// Atom :: `(` GroupSpecifier Disjunction `)` (§22.2.2.3.1†): the group + /// records where it began, and sets the capture when it ends. + fn group(&mut self, g: &Group, st: St, depth: usize) -> R { + let inner_depth = depth.saturating_add(1); + match g.index { + Some(cap) => { + let reg = self.reg()?; + self.emit(Inst::GroupOpen { reg })?; + self.disjunction(&g.body, st, inner_depth)?; + self.emit(Inst::GroupClose { + cap, + reg, + back: st.back, + }) + .map(|_| ()) + } + None => self.disjunction(&g.body, st, inner_depth), + } + } + + /// A `\d`-like escape as a leaf (or, with `v`, a leaf and a `not`). + fn escape(&self, e: &ClassEscape, ops: &mut Vec) -> R { + let (kind, neg) = match e { + ClassEscape::Digit => (EscKind::Digit, false), + ClassEscape::NotDigit => (EscKind::Digit, true), + ClassEscape::Space => (EscKind::Space, false), + ClassEscape::NotSpace => (EscKind::Space, true), + ClassEscape::Word => (EscKind::Word, false), + ClassEscape::NotWord => (EscKind::Word, true), + ClassEscape::Property { negated, property } => { + (EscKind::Prop(self.property(property)?), *negated) + } + }; + if self.v && neg { + ops.push(ClassOp::Esc { kind, neg: false }); + ops.push(ClassOp::Not); + } else { + ops.push(ClassOp::Esc { kind, neg }); + } + Ok(()) + } + + /// UnicodePropertyValueExpression (§22.2.2.9†). + fn property(&self, p: &Property) -> R { + let unsupported = |what: &str, v: &str| { + CompileError::UnsupportedProperty(format!("{what}={v}: no table is generated")) + }; + match p { + Property::GeneralCategory(v) => unicode::category_mask(v) + .map(Prop::Category) + .ok_or_else(|| unsupported("General_Category", v)), + Property::Binary(n) => { + unicode::binary_property(n).ok_or_else(|| unsupported("binary property", n)) + } + Property::Script(v) => { + unicode::script_property(v, false).ok_or_else(|| unsupported("Script", v)) + } + Property::ScriptExtensions(v) => { + unicode::script_property(v, true).ok_or_else(|| unsupported("Script_Extensions", v)) + } + // The one-character members; the longer strings are + // `operand_strings`'s. + Property::Strings(n) => unicode::string_property(n) + .map(|s| s.chars) + .ok_or_else(|| unsupported("property of strings", n)), + } + } + + /// CharacterClass (§22.2.2.7† CompileToCharSet) as a postfix program. + fn class_prog(&self, c: &CharacterClass, st: St, depth: usize) -> R { + let mut ops = Vec::new(); + let mut invert = false; + match &c.contents { + ClassContents::Ranges(items) => { + for it in items { + match it { + ClassItem::Char(x) => ops.push(ClassOp::Char(*x)), + ClassItem::Range(a, b) => ops.push(ClassOp::Range(*a, *b)), + ClassItem::Escape(e) => self.escape(e, &mut ops)?, + } + } + ops.push(ClassOp::Or( + u32::try_from(items.len()).map_err(|_| CompileError::TooLarge)?, + )); + invert = c.negated; + } + ClassContents::Set(expr) => { + self.set_expr(expr, &mut ops, depth.saturating_add(1))?; + if c.negated { + ops.push(ClassOp::Not); + } + } + } + Ok(ClassProg { + ops, + ic: st.ic, + invert, + word_extras: self.unicode && st.ic, + }) + } + + /// ClassSetExpression (`v`): union, intersection or subtraction of its + /// operands. + fn set_expr(&self, e: &ClassSetExpr, ops: &mut Vec, depth: usize) -> R { + if depth > MAX_DEPTH.saturating_add(2) { + return Err(CompileError::TooDeep); + } + let count = |n: usize| u32::try_from(n).map_err(|_| CompileError::TooLarge); + match e { + ClassSetExpr::Union(items) => { + for it in items { + match it { + ClassSetItem::Operand(o) => self.operand(o, ops, depth)?, + ClassSetItem::Range(a, b) => ops.push(ClassOp::Range(*a, *b)), + } + } + ops.push(ClassOp::Or(count(items.len())?)); + } + ClassSetExpr::Intersection(os) => { + for o in os { + self.operand(o, ops, depth)?; + } + ops.push(ClassOp::And(count(os.len())?)); + } + ClassSetExpr::Subtraction(os) => { + for o in os { + self.operand(o, ops, depth)?; + } + ops.push(ClassOp::Sub(count(os.len())?)); + } + } + Ok(()) + } + + fn operand(&self, o: &ClassSetOperand, ops: &mut Vec, depth: usize) -> R { + match o { + ClassSetOperand::Char(c) => ops.push(ClassOp::Char(*c)), + ClassSetOperand::Escape(e) => self.escape(e, ops)?, + ClassSetOperand::Nested { negated, expr } => { + self.set_expr(expr, ops, depth.saturating_add(1))?; + if *negated { + ops.push(ClassOp::Not); + } + } + ClassSetOperand::Strings(list) => { + // The one-character strings are characters of the set; the + // others are in `strings_of`. + let mut n = 0u32; + for s in list { + if let [c] = s.as_slice() { + ops.push(ClassOp::Char(*c)); + n = n.saturating_add(1); + } + } + ops.push(ClassOp::Or(n)); + } + } + Ok(()) + } + + /// The strings a `v` class can match that are not one character (the empty + /// string included), folded if the class ignores case; empty for a class + /// without `v` or a negated one. + fn class_strings_set(&self, c: &CharacterClass, st: St, depth: usize) -> R>> { + match &c.contents { + ClassContents::Set(e) if !c.negated => { + self.strings_of(e, self.v && st.ic, depth.saturating_add(1)) + } + _ => Ok(BTreeSet::new()), + } + } + + /// The strings of a ClassSetExpression that are not one character: + /// MayContainStrings's sets (§22.2.1.1†), computed. A union holds those of + /// its operands, an intersection those of all, a subtraction those of the + /// first that the others lack. + fn strings_of(&self, e: &ClassSetExpr, fold: bool, depth: usize) -> R>> { + if depth > MAX_DEPTH.saturating_add(2) { + return Err(CompileError::TooDeep); + } + let next = depth.saturating_add(1); + match e { + ClassSetExpr::Union(items) => { + let mut out = BTreeSet::new(); + for it in items { + if let ClassSetItem::Operand(o) = it { + out.extend(self.operand_strings(o, fold, next)?); + } + } + Ok(out) + } + ClassSetExpr::Intersection(os) => { + let mut it = os.iter(); + let mut out = match it.next() { + Some(o) => self.operand_strings(o, fold, next)?, + None => BTreeSet::new(), + }; + for o in it { + let other = self.operand_strings(o, fold, next)?; + out.retain(|s| other.contains(s)); + } + Ok(out) + } + ClassSetExpr::Subtraction(os) => { + let mut it = os.iter(); + let mut out = match it.next() { + Some(o) => self.operand_strings(o, fold, next)?, + None => BTreeSet::new(), + }; + for o in it { + let other = self.operand_strings(o, fold, next)?; + out.retain(|s| !other.contains(s)); + } + Ok(out) + } + } + } + + fn operand_strings( + &self, + o: &ClassSetOperand, + fold: bool, + depth: usize, + ) -> R>> { + match o { + ClassSetOperand::Char(_) => Ok(BTreeSet::new()), + ClassSetOperand::Escape(ClassEscape::Property { + property: Property::Strings(n), + .. + }) => { + // A property of strings: its strings of two or more + // characters (UTS #51 ED-27). + let set = unicode::string_property(n).ok_or_else(|| { + CompileError::UnsupportedProperty(format!( + "property of strings={n}: no table is generated" + )) + })?; + Ok(set + .strings + .into_iter() + .map(|s| { + if fold { + s.iter().map(|&c| simple_fold(c)).collect() + } else { + s + } + }) + .collect()) + } + ClassSetOperand::Escape(_) => Ok(BTreeSet::new()), + ClassSetOperand::Nested { negated, expr } => { + if *negated { + Ok(BTreeSet::new()) + } else { + self.strings_of(expr, fold, depth) + } + } + ClassSetOperand::Strings(list) => Ok(list + .iter() + .filter(|s| s.len() != 1) + .map(|s| { + if fold { + s.iter().map(|&c| simple_fold(c)).collect() + } else { + s.clone() + } + }) + .collect()), + } + } + + /// A `v` class with strings: the strings longest first, then the single + /// characters, then the empty string if it is in the set. + fn class_with_strings(&mut self, c: &CharacterClass, st: St, depth: usize) -> R { + let set = self.class_strings_set(c, st, depth)?; + let has_empty = set.contains(&Vec::new()); + let mut multi: Vec> = set.into_iter().filter(|s| !s.is_empty()).collect(); + multi.sort_by(|a, b| b.len().cmp(&a.len()).then_with(|| a.cmp(b))); + let prog = self.class_prog(c, st, depth)?; + let id = self.add_class(prog)?; + let mut alts: Vec = multi.into_iter().map(Alt::Str).collect(); + alts.push(Alt::One(One { + kind: OneKind::Class(id), + back: st.back, + })); + if has_empty { + alts.push(Alt::Empty); + } + let n = alts.len(); + let mut jumps = Vec::new(); + for (i, alt) in alts.iter().enumerate() { + let last = i.saturating_add(1) >= n; + let split = if last { + None + } else { + Some(self.emit(Inst::Split { + first: 0, + second: 0, + })?) + }; + let first = self.pc()?; + match alt { + Alt::Str(s) => { + let one = |c: u32| One { + kind: OneKind::Char { + c: if st.ic { simple_fold(c) } else { c }, + ic: st.ic, + }, + back: st.back, + }; + if st.back { + for &c in s.iter().rev() { + self.emit(Inst::One(one(c)))?; + } + } else { + for &c in s { + self.emit(Inst::One(one(c)))?; + } + } + } + Alt::One(o) => { + self.emit(Inst::One(o.clone()))?; + } + Alt::Empty => {} + } + if let Some(split) = split { + jumps.push(self.emit(Inst::Jmp(0))?); + let second = self.pc()?; + self.set(split, Inst::Split { first, second }); + } + } + let end = self.pc()?; + for j in jumps { + self.set(j, Inst::Jmp(end)); + } + Ok(()) + } +} + +/// The first and last capture number of the groups inside a term (with the +/// term itself, if it is a group), by an explicit walk: no recursion. +fn cap_range(k: &TermKind) -> Option<(u32, u32)> { + /// Records a group's number and queues the bodies a kind holds. + fn visit<'t>(k: &'t TermKind, work: &mut Vec<&'t Disjunction>, lo: &mut u32, hi: &mut u32) { + match k { + TermKind::Atom(Atom::Group(g)) => { + if let Some(i) = g.index { + *lo = (*lo).min(i); + *hi = (*hi).max(i); + } + work.push(&g.body); + } + TermKind::Atom(Atom::Modified(m)) => work.push(&m.body), + TermKind::Look(l) => work.push(&l.body), + _ => {} + } + } + let mut lo = u32::MAX; + let mut hi = 0u32; + let mut work: Vec<&Disjunction> = Vec::new(); + visit(k, &mut work, &mut lo, &mut hi); + while let Some(d) = work.pop() { + for alt in &d.alternatives { + for t in &alt.terms { + visit(&t.kind, &mut work, &mut lo, &mut hi); + } + } + } + (hi != 0).then_some((lo, hi)) +} diff --git a/crates/clazel-js-regexp/src/lib.rs b/crates/clazel-js-regexp/src/lib.rs index 1d44eb46a..152c64f3e 100644 --- a/crates/clazel-js-regexp/src/lib.rs +++ b/crates/clazel-js-regexp/src/lib.rs @@ -1,32 +1,49 @@ -//! ECMAScript regular expressions (phase-7.md item 7.10): this is the -//! pattern parser. The matcher and the Unicode tables behind `\p{..}` and -//! case folding are not written. +//! ECMAScript regular expressions (phase-7.md item 7.10): the pattern parser +//! and the backtracking matcher. //! //! Spec: ECMA-262, 2026 edition (`clazel-js-lexer/SPEC-PIN.md`): §22.2.1† //! (the pattern grammar), §22.2.1.1† (its early errors), Annex B §B.1.2† -//! (the web-compatibility grammar without `u` or `v`), and the flag rules of -//! §13.2.7.1† and §22.2.3.4†. Section numbers marked † are from memory (the -//! specification's text is not vendored); see [`parser`] for the modelling +//! (the web-compatibility grammar without `u` or `v`), §22.2.2† (Pattern +//! Semantics, the matcher), and the flag rules of §13.2.7.1† and §22.2.3.4†. +//! Section numbers marked † are from memory (the specification's text is not +//! vendored); see [`parser`], [`compile`] and [`vm`] for the modelling //! choices. //! //! - [`parse`] / [`parse_with`]: a pattern (UTF-16 code units) and //! [`Flags`] to a [`Pattern`] (the tree of [`ast`]), or an [`Error`]. //! - [`Flags::parse`]: the flag string. +//! - [`compile`]: a [`Pattern`] and its [`Flags`] to a [`Program`]. +//! - [`Program::exec`]: the program on UTF-16 input from a start index, with +//! [`Limits`] (a bounded backtrack stack, a step budget) and an interrupt +//! hook; the captures of the match as a [`Match`], or a [`MatchError`]. +//! - [`unicode`] and [`tables`]: case canonicalization and `\p{..}` sets, +//! from tables generated from the vendored Unicode 18.0.0 files. //! - [`props`]: the property names `\p{..}` accepts, and [`props::Names`], //! where a table of script names plugs in. //! -//! Tier 0 today. `src/parser/proofs.rs` holds Kani harnesses (Tier 1) that are -//! compiled only under `cargo kani`; see the README for the bounds and for -//! what they leave out. +//! Tier 0 today. `src/parser/proofs.rs` and `src/proofs.rs` hold Kani +//! harnesses (Tier 1) that are compiled only under `cargo kani`; see the +//! README for the bounds and for what they leave out. pub mod ast; +pub mod compile; pub mod error; pub mod flags; pub mod parser; +pub mod program; +#[cfg(kani)] +mod proofs; pub mod props; +pub(crate) mod stack; +pub mod tables; +pub mod unicode; +pub mod vm; pub use ast::Pattern; +pub use compile::{CompileError, compile}; pub use error::{Error, ErrorKind, FlagsError}; pub use flags::Flags; pub use parser::{MAX_DEPTH, parse, parse_with}; -pub use props::{Names, SyntaxOnly}; +pub use program::Program; +pub use props::{Names, SyntaxOnly, UcdScripts}; +pub use vm::{Limits, Match, MatchError}; diff --git a/crates/clazel-js-regexp/src/parser.rs b/crates/clazel-js-regexp/src/parser.rs index 61f3cfe0d..66cba5b6a 100644 --- a/crates/clazel-js-regexp/src/parser.rs +++ b/crates/clazel-js-regexp/src/parser.rs @@ -58,8 +58,8 @@ type R = Result; #[cfg(kani)] mod proofs; -/// Parses `pattern` (UTF-16 code units) with `flags`, accepting every -/// syntactically valid `Script` value in `\p{sc=..}`. +/// Parses `pattern` (UTF-16 code units) with `flags`, accepting the `Script` +/// values of the generated tables ([`props::UcdScripts`]) in `\p{sc=..}`. /// /// ECMA-262 §22.2.3.4† RegExpInitialize step 7 (ParsePattern) and §22.2.1†, /// with the early errors of §22.2.1.1†. @@ -68,7 +68,7 @@ mod proofs; /// /// [`Error`] if the pattern is a Syntax Error. pub fn parse(pattern: &[u16], flags: Flags) -> Result { - parse_with(pattern, flags, &props::SyntaxOnly) + parse_with(pattern, flags, &props::UcdScripts) } /// [`parse`], checking `Script` and `Script_Extensions` values with `names`. diff --git a/crates/clazel-js-regexp/src/program.rs b/crates/clazel-js-regexp/src/program.rs new file mode 100644 index 000000000..50d5f7844 --- /dev/null +++ b/crates/clazel-js-regexp/src/program.rs @@ -0,0 +1,260 @@ +//! The compiled form of a pattern: a list of instructions for the backtracking +//! matcher of [`crate::vm`]. +//! +//! The standard (ECMA-262 §22.2.2†) defines matching with closures: each +//! Matcher takes a state and a continuation. An instruction list is the same +//! thing with the continuation made explicit, so the matcher can be a loop +//! over a heap stack instead of recursion. [`crate::compile`] does that +//! translation, one production at a time, and cites the algorithm it takes +//! each instruction from. + +use crate::unicode::Prop; + +/// A matcher for one character (a code unit, or with `u`/`v` a code point). +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum OneKind { + /// A character; with `ic`, `c` is already canonicalized and the input's + /// character is too, before the comparison. + Char { + /// The character. + c: u32, + /// `i` is on. + ic: bool, + }, + /// `.`: any character, or any but a line terminator. + Dot { + /// `s` is on. + all: bool, + }, + /// A character class, by index into [`Program::classes`]. + Class(u32), +} + +/// [`OneKind`] and the direction it reads in. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct One { + pub(crate) kind: OneKind, + /// Reads the character before the position, and moves back (lookbehind). + pub(crate) back: bool, +} + +/// An instruction. `pc` values are indices into [`Program::insts`]; register +/// numbers index [`Program`]'s registers (the captures come first: capture +/// `n` is registers `2n` and `2n + 1`). +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum Inst { + /// Match one character and move past it. + One(One), + /// A greedy loop over one character, `min` to `max` times, as one + /// instruction and one stack entry (RepeatMatcher, §22.2.2.3.1†). + Run { + /// The character. + one: One, + /// The least number of repetitions. + min: usize, + /// The most. + max: usize, + }, + /// `^` (§22.2.2.6.1† Assertion, step for `^`). + LineStart { + /// `m` is on. + multiline: bool, + }, + /// `$`. + LineEnd { + /// `m` is on. + multiline: bool, + }, + /// `\b` and `\B`. + WordBoundary { + /// `\B`. + neg: bool, + /// `u` or `v` with `i`: U+017F and U+212A are word characters. + extras: bool, + }, + /// A backreference (BackreferenceMatcher, §22.2.2.7.2†): the first group + /// of the list `Program::backrefs[id]` that has a capture is the one used. + BackRef { + /// Index into the program's list of group lists. + id: u32, + /// `i` is on. + ic: bool, + /// Matches backwards. + back: bool, + }, + /// Try `first`, and on failure `second`. + Split { + /// Tried first. + first: u32, + /// Tried after `first` fails. + second: u32, + }, + /// Go to `target`. + Jmp(u32), + /// A capturing group begins: remember the position in `reg`. + GroupOpen { + /// The register. + reg: u32, + }, + /// A capturing group ends: capture `cap` becomes the text between the + /// remembered position and this one. + GroupClose { + /// The capture's number. + cap: u32, + /// The register `GroupOpen` wrote. + reg: u32, + /// The group was matched backwards, so the end comes first. + back: bool, + }, + /// Undefine captures `first` to `last` (RepeatMatcher step 4). + ResetCaps { + /// The first capture. + first: u32, + /// The last. + last: u32, + }, + /// Set a loop's counter to zero. + CounterInit { + /// The register. + reg: u32, + }, + /// The top of a general loop (RepeatMatcher steps 1, 5, 6 and 7). + LoopHead { + /// The counter's register. + counter: u32, + /// The least number of iterations. + min: usize, + /// The most. + max: usize, + /// No `?` after the quantifier. + greedy: bool, + /// The first instruction of the body. + body: u32, + /// The instruction after the loop. + exit: u32, + }, + /// Remember the position a loop iteration began at. + SavePos { + /// The register. + reg: u32, + }, + /// The end of a loop iteration (RepeatMatcher step 2.b): an iteration + /// beyond the least number that matched nothing fails; else count it. + LoopEnd { + /// The counter's register. + counter: u32, + /// The register `SavePos` wrote. + pos_reg: u32, + /// The least number of iterations. + min: usize, + /// The instruction of the loop's `LoopHead`. + head: u32, + }, + /// The start of a lookaround's body (§22.2.2.3.2† Assertion). + LookStart { + /// `(?!` and `(?, + /// `i` is on: a leaf holds if it holds for some character with the same + /// `Canonicalize` as the input's (CharacterSetMatcher, §22.2.2.7.1†). + pub(crate) ic: bool, + /// The class is `[^..]` without `v` (with `v`, a `Not` op does it). + pub(crate) invert: bool, + /// `\w` includes U+017F and U+212A (`u` or `v`, with `i`). + pub(crate) word_extras: bool, +} + +/// A compiled pattern: see [`crate::compile`] and [`Program::exec`]. +#[derive(Debug, Clone)] +pub struct Program { + pub(crate) insts: Vec, + pub(crate) classes: Vec, + pub(crate) backrefs: Vec>, + pub(crate) regs: usize, + pub(crate) capture_count: u32, + pub(crate) unicode: bool, + pub(crate) group_names: Vec<(Vec, u32)>, +} + +impl Program { + /// The number of capturing groups (not counting the whole match). + #[must_use] + pub fn capture_count(&self) -> u32 { + self.capture_count + } + + /// Every named group, as its name (code points) and its number: the + /// parser's [`crate::ast::Pattern::group_names`]. + #[must_use] + pub fn group_names(&self) -> &[(Vec, u32)] { + &self.group_names + } + + /// Whether the pattern reads code points (`u` or `v`) rather than code + /// units. + #[must_use] + pub fn unicode(&self) -> bool { + self.unicode + } + + /// The group named `name` that took part in `m` (§22.2.2.3.1†'s duplicate + /// named groups: at most one does): its capture, if any. + #[must_use] + pub fn named_capture(&self, m: &crate::vm::Match, name: &[u32]) -> Option<(usize, usize)> { + self.group_names + .iter() + .filter(|(n, _)| n.as_slice() == name) + .find_map(|&(_, i)| m.captures.get(usize::try_from(i).ok()?).copied().flatten()) + } +} diff --git a/crates/clazel-js-regexp/src/proofs.rs b/crates/clazel-js-regexp/src/proofs.rs new file mode 100644 index 000000000..ca0e60e21 --- /dev/null +++ b/crates/clazel-js-regexp/src/proofs.rs @@ -0,0 +1,183 @@ +//! Kani harnesses (Tier 1) for the matcher's small pieces. Compiled only under +//! `cargo kani`. +//! +//! - **The case tables' lookups** ([`crate::unicode`]): for every input, +//! `Canonicalize` stays in range and never turns a non-ASCII unit into an +//! ASCII one (§22.2.2.7.3†). That the inverse table agrees with the forward +//! one (`equivalents` of a character's canonical form contains it) is not +//! here: with the two bisections and a growing `Vec` it ran past ten CPU +//! minutes in CBMC. `tests/tables.rs` checks it for every code point. +//! - **The backtrack stack** ([`crate::stack::BtStack`]): a push never grows it +//! past its bound, says "full" exactly at the bound, and a pop returns what +//! was pushed last; `commit_look` keeps the undo entries and drops the rest. +//! - **Reading a character** ([`crate::vm::read`]): at any position of any +//! input of up to 3 units, forward or backward, in either mode, the new +//! position is inside the input, moves by one or two units, and the +//! character is a code unit or a valid code point. +//! +//! What is not here: the matcher loop itself and the compiler. Both build +//! `Vec`s in loops, and the parser's README records that CBMC does not finish +//! on those (`src/parser/proofs.rs`). They are tested instead, by the +//! differential against a reference matcher (`tests/differential.rs`). + +use crate::stack::{BtStack, Entry}; +use crate::unicode::{canonicalize_unit, simple_fold}; +use crate::vm::read; + +/// The non-unicode `Canonicalize` of any code unit is a code unit, and a +/// non-ASCII unit never becomes ASCII. +#[kani::proof] +#[kani::unwind(14)] +fn canonicalize_unit_any() { + let c: u16 = kani::any(); + let r = canonicalize_unit(u32::from(c)); + assert!(r <= 0xFFFF); + if c >= 128 { + assert!(r >= 128); + } +} + +/// Simple case folding of any code point is a code point. +#[kani::proof] +#[kani::unwind(14)] +fn simple_fold_any() { + let c: u32 = kani::any(); + kani::assume(c <= 0x10FFFF); + assert!(simple_fold(c) <= 0x10FFFF); +} + +fn any_entry() -> Entry { + match kani::any::() % 4 { + 0 => Entry::Branch { + pc: kani::any(), + pos: kani::any(), + }, + 1 => Entry::Restore { + reg: kani::any(), + old: kani::any(), + }, + 2 => Entry::Run { + pc: kani::any(), + back: kani::any(), + cur: kani::any(), + limit: kani::any(), + }, + _ => Entry::LookMark { + neg: kani::any(), + pc: kani::any(), + pos: kani::any(), + }, + } +} + +/// Pushing never takes the stack past its bound, and refuses exactly when it +/// is full, whatever the bound and the entries. +#[kani::proof] +#[kani::unwind(8)] +fn stack_push_respects_the_bound() { + let max: usize = kani::any(); + kani::assume(max <= 4); + let mut s = BtStack::new(max); + for i in 0..6usize { + let before = s.len(); + let r = s.push(any_entry()); + assert!(s.len() <= max); + if before < max { + assert!(r.is_ok() && s.len() == before + 1); + } else { + assert!(r.is_err() && s.len() == before); + } + let _ = i; + } +} + +/// A pop returns the entry pushed last, and an empty stack returns nothing. +#[kani::proof] +#[kani::unwind(4)] +fn stack_pop_returns_the_last_push() { + let mut s = BtStack::new(2); + assert!(s.pop().is_none()); + let a = any_entry(); + let b = any_entry(); + assert!(s.push(a).is_ok()); + assert!(s.push(b).is_ok()); + assert!(s.pop() == Some(b)); + assert!(s.pop() == Some(a)); + assert!(s.pop().is_none()); +} + +/// A positive lookaround's commit removes the mark and everything above it +/// but the undo entries, and gives back the position the mark holds. +#[kani::proof] +#[kani::unwind(8)] +fn stack_commit_look_keeps_only_the_undos() { + let mut s = BtStack::new(4); + let below = any_entry(); + kani::assume(!matches!(below, Entry::LookMark { .. })); + let mark_pos: usize = kani::any(); + assert!(s.push(below).is_ok()); + assert!( + s.push(Entry::LookMark { + neg: false, + pc: 0, + pos: mark_pos + }) + .is_ok() + ); + let a = any_entry(); + kani::assume(!matches!(a, Entry::LookMark { .. })); + let b = any_entry(); + kani::assume(!matches!(b, Entry::LookMark { .. })); + assert!(s.push(a).is_ok()); + assert!(s.push(b).is_ok()); + let Some((pos, _)) = s.commit_look() else { + panic!("a mark is on the stack"); + }; + assert!(pos == mark_pos); + // What remains above `below` is the undo entries among `a`, `b`, in order. + let mut kept = Vec::new(); + while let Some(e) = s.pop() { + kept.push(e); + } + let undos = [a, b] + .iter() + .filter(|e| matches!(e, Entry::Restore { .. })) + .count(); + assert!(kept.len() == undos + 1); + assert!(kept.last() == Some(&below)); +} + +/// Reading a character at any position of any input of up to 3 units. +#[kani::proof] +#[kani::unwind(6)] +fn read_any_3() { + let units: [u16; 3] = kani::any(); + let len: usize = kani::any(); + kani::assume(len <= 3); + let input = units.get(..len).unwrap_or_default(); + let pos: usize = kani::any(); + kani::assume(pos <= len); + let unicode: bool = kani::any(); + let back: bool = kani::any(); + match read(input, unicode, pos, back) { + Some((ch, np)) => { + assert!(np <= len); + if back { + assert!(np < pos && pos - np <= 2); + } else { + assert!(np > pos && np - pos <= 2); + } + assert!(ch <= 0x10FFFF); + if !unicode { + assert!(ch <= 0xFFFF && np.abs_diff(pos) == 1); + } + } + None => { + if back { + assert!(pos == 0); + } else { + assert!(pos == len); + } + } + } +} diff --git a/crates/clazel-js-regexp/src/props.rs b/crates/clazel-js-regexp/src/props.rs index a7108b93f..2d68c2147 100644 --- a/crates/clazel-js-regexp/src/props.rs +++ b/crates/clazel-js-regexp/src/props.rs @@ -13,9 +13,11 @@ //! kind for strings. //! //! `Script` / `Script_Extensions` values are not listed here: there are about -//! 170 and they change with every Unicode version, so they belong to the -//! generated tables of item 7.10's second half. [`Names::is_script`] is where -//! they plug in; [`SyntaxOnly`] accepts every syntactically valid value. +//! 170 and they change with every Unicode version, so they are in the +//! generated tables (`crate::tables::SCRIPTS`). [`Names::is_script`] is where +//! they plug in: [`UcdScripts`] accepts the values of those tables (what +//! [`crate::parse`] uses), [`SyntaxOnly`] accepts every syntactically valid +//! value. //! `tests/conformance_names.rs` checks the lists below against the vendored //! UCD files. @@ -37,6 +39,17 @@ impl Names for SyntaxOnly { } } +/// Accepts the `Script` values of `PropertyValueAliases.txt`: every name and +/// alias in [`crate::tables::SCRIPTS`], case-sensitively. +#[derive(Debug, Clone, Copy, Default)] +pub struct UcdScripts; + +impl Names for UcdScripts { + fn is_script(&self, value: &str) -> bool { + crate::unicode::script_property(value, false).is_some() + } +} + /// A list of ASCII names as a constant, and the function that tests /// membership as a `match` on the bytes: no loop, so a bounded model checker /// needs no unwinding bound for it. The one list is the source of both. diff --git a/crates/clazel-js-regexp/src/stack.rs b/crates/clazel-js-regexp/src/stack.rs new file mode 100644 index 000000000..e65c3b087 --- /dev/null +++ b/crates/clazel-js-regexp/src/stack.rs @@ -0,0 +1,126 @@ +//! The backtrack stack: a `Vec` that refuses to grow past a stated number of +//! entries. +//! +//! The matcher (ECMA-262 §22.2.2†, continuation-passing) is run as a loop over +//! this stack, never by recursion. Every choice point and every change to a +//! register that a failure must undo is an [`Entry`]. The bound is the whole +//! of the matcher's heap use that depends on the input; exceeding it is +//! [`crate::MatchError::StackOverflow`], which the engine turns into a +//! `RangeError` (a listed deviation: phase-7.md item 7.10). + +/// One entry of the backtrack stack. Three of the four kinds are 16 bytes; +/// [`Entry::Run`] makes the type 24. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Entry { + /// A choice: on failure, resume at `pc` with the input position `pos`. + Branch { + /// The instruction to resume at. + pc: u32, + /// The input position. + pos: usize, + }, + /// An undo: on failure, put `old` back in register `reg`. + Restore { + /// The register. + reg: u32, + /// Its earlier value. + old: usize, + }, + /// The choices of a greedy loop over one character: on failure, resume at + /// `pc` one character nearer to `limit` than `cur` (forward matching + /// moves down to `limit`, backward matching up), and keep this entry until + /// `limit` is reached. + Run { + /// The instruction to resume at. + pc: u32, + /// Whether the loop matches backwards (in a lookbehind). + back: bool, + /// The end of the longest run, or of the one tried last. + cur: usize, + /// The end of the shortest run. + limit: usize, + }, + /// The start of a lookaround's body. On failure it ends a negative + /// lookaround as a success (resume at `pc`, position `pos`), and a + /// positive one as a failure. + LookMark { + /// A negative lookaround. + neg: bool, + /// Where the instruction after the lookaround is. + pc: u32, + /// The input position the lookaround started at. + pos: usize, + }, +} + +/// The stack was full. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct Full; + +/// A stack of at most `max` entries. +#[derive(Debug)] +pub(crate) struct BtStack { + items: Vec, + max: usize, +} + +impl BtStack { + /// An empty stack that holds at most `max` entries. + pub(crate) fn new(max: usize) -> BtStack { + BtStack { + items: Vec::new(), + max, + } + } + + /// The number of entries (the harnesses' view of the bound). + #[cfg(kani)] + pub(crate) fn len(&self) -> usize { + self.items.len() + } + + /// Pushes `e`, or says the stack is full (and leaves it as it was). + pub(crate) fn push(&mut self, e: Entry) -> Result<(), Full> { + if self.items.len() >= self.max { + return Err(Full); + } + self.items.push(e); + Ok(()) + } + + /// Pops the top entry. + pub(crate) fn pop(&mut self) -> Option { + self.items.pop() + } + + /// Removes every entry. + pub(crate) fn clear(&mut self) { + self.items.clear(); + } + + /// What a positive lookaround does when its body has matched: removes the + /// topmost [`Entry::LookMark`] and every choice above it (the body is + /// atomic), keeping the [`Entry::Restore`]s above it, so that backtracking + /// past the lookaround still undoes the captures its body set. Returns the + /// position the lookaround started at and the number of entries above the + /// mark (the work done). + pub(crate) fn commit_look(&mut self) -> Option<(usize, usize)> { + let m = self + .items + .iter() + .rposition(|e| matches!(e, Entry::LookMark { .. }))?; + let Some(&Entry::LookMark { pos, .. }) = self.items.get(m) else { + return None; + }; + let len = self.items.len(); + let mut w = m; + for r in m.saturating_add(1)..len { + if matches!(self.items.get(r), Some(Entry::Restore { .. })) { + self.items.swap(w, r); + w = w.saturating_add(1); + } + } + self.items.truncate(w); + Some((pos, len.saturating_sub(m))) + } +} diff --git a/crates/clazel-js-regexp/src/tables.rs b/crates/clazel-js-regexp/src/tables.rs new file mode 100644 index 000000000..3dc74a963 --- /dev/null +++ b/crates/clazel-js-regexp/src/tables.rs @@ -0,0 +1,29494 @@ +//! Unicode 18.0.0 tables for case folding, `\p{..}` and `\w`: generated. +//! +//! @generated by `tests/tables.rs` (`CLAZEL_WRITE_TABLES=1 cargo test -p +//! clazel-js-regexp --test tables`) from `tests/data/ucd-regexp/` (source, +//! commit and license: `SOURCE.md` there). Do not edit by hand: the test +//! `tables_are_current` fails if this file differs from what the generator +//! writes. What each table holds and which file it comes from is in the +//! header of that test file. + +/// Simple Case Folding (CaseFolding.txt, statuses C and S): code point, folded, sorted by code point. +pub static SIMPLE_FOLD: &[(u32, u32)] = &[ + (0x41, 0x61), + (0x42, 0x62), + (0x43, 0x63), + (0x44, 0x64), + (0x45, 0x65), + (0x46, 0x66), + (0x47, 0x67), + (0x48, 0x68), + (0x49, 0x69), + (0x4A, 0x6A), + (0x4B, 0x6B), + (0x4C, 0x6C), + (0x4D, 0x6D), + (0x4E, 0x6E), + (0x4F, 0x6F), + (0x50, 0x70), + (0x51, 0x71), + (0x52, 0x72), + (0x53, 0x73), + (0x54, 0x74), + (0x55, 0x75), + (0x56, 0x76), + (0x57, 0x77), + (0x58, 0x78), + (0x59, 0x79), + (0x5A, 0x7A), + (0xB5, 0x3BC), + (0xC0, 0xE0), + (0xC1, 0xE1), + (0xC2, 0xE2), + (0xC3, 0xE3), + (0xC4, 0xE4), + (0xC5, 0xE5), + (0xC6, 0xE6), + (0xC7, 0xE7), + (0xC8, 0xE8), + (0xC9, 0xE9), + (0xCA, 0xEA), + (0xCB, 0xEB), + (0xCC, 0xEC), + (0xCD, 0xED), + (0xCE, 0xEE), + (0xCF, 0xEF), + (0xD0, 0xF0), + (0xD1, 0xF1), + (0xD2, 0xF2), + (0xD3, 0xF3), + (0xD4, 0xF4), + (0xD5, 0xF5), + (0xD6, 0xF6), + (0xD8, 0xF8), + (0xD9, 0xF9), + (0xDA, 0xFA), + (0xDB, 0xFB), + (0xDC, 0xFC), + (0xDD, 0xFD), + (0xDE, 0xFE), + (0x100, 0x101), + (0x102, 0x103), + (0x104, 0x105), + (0x106, 0x107), + (0x108, 0x109), + (0x10A, 0x10B), + (0x10C, 0x10D), + (0x10E, 0x10F), + (0x110, 0x111), + (0x112, 0x113), + (0x114, 0x115), + (0x116, 0x117), + (0x118, 0x119), + (0x11A, 0x11B), + (0x11C, 0x11D), + (0x11E, 0x11F), + (0x120, 0x121), + (0x122, 0x123), + (0x124, 0x125), + (0x126, 0x127), + (0x128, 0x129), + (0x12A, 0x12B), + (0x12C, 0x12D), + (0x12E, 0x12F), + (0x132, 0x133), + (0x134, 0x135), + (0x136, 0x137), + (0x139, 0x13A), + (0x13B, 0x13C), + (0x13D, 0x13E), + (0x13F, 0x140), + (0x141, 0x142), + (0x143, 0x144), + (0x145, 0x146), + (0x147, 0x148), + (0x14A, 0x14B), + (0x14C, 0x14D), + (0x14E, 0x14F), + (0x150, 0x151), + (0x152, 0x153), + (0x154, 0x155), + (0x156, 0x157), + (0x158, 0x159), + (0x15A, 0x15B), + (0x15C, 0x15D), + (0x15E, 0x15F), + (0x160, 0x161), + (0x162, 0x163), + (0x164, 0x165), + (0x166, 0x167), + (0x168, 0x169), + (0x16A, 0x16B), + (0x16C, 0x16D), + (0x16E, 0x16F), + (0x170, 0x171), + (0x172, 0x173), + (0x174, 0x175), + (0x176, 0x177), + (0x178, 0xFF), + (0x179, 0x17A), + (0x17B, 0x17C), + (0x17D, 0x17E), + (0x17F, 0x73), + (0x181, 0x253), + (0x182, 0x183), + (0x184, 0x185), + (0x186, 0x254), + (0x187, 0x188), + (0x189, 0x256), + (0x18A, 0x257), + (0x18B, 0x18C), + (0x18E, 0x1DD), + (0x18F, 0x259), + (0x190, 0x25B), + (0x191, 0x192), + (0x193, 0x260), + (0x194, 0x263), + (0x196, 0x269), + (0x197, 0x268), + (0x198, 0x199), + (0x19C, 0x26F), + (0x19D, 0x272), + (0x19F, 0x275), + (0x1A0, 0x1A1), + (0x1A2, 0x1A3), + (0x1A4, 0x1A5), + (0x1A6, 0x280), + (0x1A7, 0x1A8), + (0x1A9, 0x283), + (0x1AC, 0x1AD), + (0x1AE, 0x288), + (0x1AF, 0x1B0), + (0x1B1, 0x28A), + (0x1B2, 0x28B), + (0x1B3, 0x1B4), + (0x1B5, 0x1B6), + (0x1B7, 0x292), + (0x1B8, 0x1B9), + (0x1BC, 0x1BD), + (0x1C4, 0x1C6), + (0x1C5, 0x1C6), + (0x1C7, 0x1C9), + (0x1C8, 0x1C9), + (0x1CA, 0x1CC), + (0x1CB, 0x1CC), + (0x1CD, 0x1CE), + (0x1CF, 0x1D0), + (0x1D1, 0x1D2), + (0x1D3, 0x1D4), + (0x1D5, 0x1D6), + (0x1D7, 0x1D8), + (0x1D9, 0x1DA), + (0x1DB, 0x1DC), + (0x1DE, 0x1DF), + (0x1E0, 0x1E1), + (0x1E2, 0x1E3), + (0x1E4, 0x1E5), + (0x1E6, 0x1E7), + (0x1E8, 0x1E9), + (0x1EA, 0x1EB), + (0x1EC, 0x1ED), + (0x1EE, 0x1EF), + (0x1F1, 0x1F3), + (0x1F2, 0x1F3), + (0x1F4, 0x1F5), + (0x1F6, 0x195), + (0x1F7, 0x1BF), + (0x1F8, 0x1F9), + (0x1FA, 0x1FB), + (0x1FC, 0x1FD), + (0x1FE, 0x1FF), + (0x200, 0x201), + (0x202, 0x203), + (0x204, 0x205), + (0x206, 0x207), + (0x208, 0x209), + (0x20A, 0x20B), + (0x20C, 0x20D), + (0x20E, 0x20F), + (0x210, 0x211), + (0x212, 0x213), + (0x214, 0x215), + (0x216, 0x217), + (0x218, 0x219), + (0x21A, 0x21B), + (0x21C, 0x21D), + (0x21E, 0x21F), + (0x220, 0x19E), + (0x222, 0x223), + (0x224, 0x225), + (0x226, 0x227), + (0x228, 0x229), + (0x22A, 0x22B), + (0x22C, 0x22D), + (0x22E, 0x22F), + (0x230, 0x231), + (0x232, 0x233), + (0x23A, 0x2C65), + (0x23B, 0x23C), + (0x23D, 0x19A), + (0x23E, 0x2C66), + (0x241, 0x242), + (0x243, 0x180), + (0x244, 0x289), + (0x245, 0x28C), + (0x246, 0x247), + (0x248, 0x249), + (0x24A, 0x24B), + (0x24C, 0x24D), + (0x24E, 0x24F), + (0x345, 0x3B9), + (0x370, 0x371), + (0x372, 0x373), + (0x376, 0x377), + (0x37F, 0x3F3), + (0x386, 0x3AC), + (0x388, 0x3AD), + (0x389, 0x3AE), + (0x38A, 0x3AF), + (0x38C, 0x3CC), + (0x38E, 0x3CD), + (0x38F, 0x3CE), + (0x391, 0x3B1), + (0x392, 0x3B2), + (0x393, 0x3B3), + (0x394, 0x3B4), + (0x395, 0x3B5), + (0x396, 0x3B6), + (0x397, 0x3B7), + (0x398, 0x3B8), + (0x399, 0x3B9), + (0x39A, 0x3BA), + (0x39B, 0x3BB), + (0x39C, 0x3BC), + (0x39D, 0x3BD), + (0x39E, 0x3BE), + (0x39F, 0x3BF), + (0x3A0, 0x3C0), + (0x3A1, 0x3C1), + (0x3A3, 0x3C3), + (0x3A4, 0x3C4), + (0x3A5, 0x3C5), + (0x3A6, 0x3C6), + (0x3A7, 0x3C7), + (0x3A8, 0x3C8), + (0x3A9, 0x3C9), + (0x3AA, 0x3CA), + (0x3AB, 0x3CB), + (0x3C2, 0x3C3), + (0x3CF, 0x3D7), + (0x3D0, 0x3B2), + (0x3D1, 0x3B8), + (0x3D5, 0x3C6), + (0x3D6, 0x3C0), + (0x3D8, 0x3D9), + (0x3DA, 0x3DB), + (0x3DC, 0x3DD), + (0x3DE, 0x3DF), + (0x3E0, 0x3E1), + (0x3E2, 0x3E3), + (0x3E4, 0x3E5), + (0x3E6, 0x3E7), + (0x3E8, 0x3E9), + (0x3EA, 0x3EB), + (0x3EC, 0x3ED), + (0x3EE, 0x3EF), + (0x3F0, 0x3BA), + (0x3F1, 0x3C1), + (0x3F4, 0x3B8), + (0x3F5, 0x3B5), + (0x3F7, 0x3F8), + (0x3F9, 0x3F2), + (0x3FA, 0x3FB), + (0x3FD, 0x37B), + (0x3FE, 0x37C), + (0x3FF, 0x37D), + (0x400, 0x450), + (0x401, 0x451), + (0x402, 0x452), + (0x403, 0x453), + (0x404, 0x454), + (0x405, 0x455), + (0x406, 0x456), + (0x407, 0x457), + (0x408, 0x458), + (0x409, 0x459), + (0x40A, 0x45A), + (0x40B, 0x45B), + (0x40C, 0x45C), + (0x40D, 0x45D), + (0x40E, 0x45E), + (0x40F, 0x45F), + (0x410, 0x430), + (0x411, 0x431), + (0x412, 0x432), + (0x413, 0x433), + (0x414, 0x434), + (0x415, 0x435), + (0x416, 0x436), + (0x417, 0x437), + (0x418, 0x438), + (0x419, 0x439), + (0x41A, 0x43A), + (0x41B, 0x43B), + (0x41C, 0x43C), + (0x41D, 0x43D), + (0x41E, 0x43E), + (0x41F, 0x43F), + (0x420, 0x440), + (0x421, 0x441), + (0x422, 0x442), + (0x423, 0x443), + (0x424, 0x444), + (0x425, 0x445), + (0x426, 0x446), + (0x427, 0x447), + (0x428, 0x448), + (0x429, 0x449), + (0x42A, 0x44A), + (0x42B, 0x44B), + (0x42C, 0x44C), + (0x42D, 0x44D), + (0x42E, 0x44E), + (0x42F, 0x44F), + (0x460, 0x461), + (0x462, 0x463), + (0x464, 0x465), + (0x466, 0x467), + (0x468, 0x469), + (0x46A, 0x46B), + (0x46C, 0x46D), + (0x46E, 0x46F), + (0x470, 0x471), + (0x472, 0x473), + (0x474, 0x475), + (0x476, 0x477), + (0x478, 0x479), + (0x47A, 0x47B), + (0x47C, 0x47D), + (0x47E, 0x47F), + (0x480, 0x481), + (0x48A, 0x48B), + (0x48C, 0x48D), + (0x48E, 0x48F), + (0x490, 0x491), + (0x492, 0x493), + (0x494, 0x495), + (0x496, 0x497), + (0x498, 0x499), + (0x49A, 0x49B), + (0x49C, 0x49D), + (0x49E, 0x49F), + (0x4A0, 0x4A1), + (0x4A2, 0x4A3), + (0x4A4, 0x4A5), + (0x4A6, 0x4A7), + (0x4A8, 0x4A9), + (0x4AA, 0x4AB), + (0x4AC, 0x4AD), + (0x4AE, 0x4AF), + (0x4B0, 0x4B1), + (0x4B2, 0x4B3), + (0x4B4, 0x4B5), + (0x4B6, 0x4B7), + (0x4B8, 0x4B9), + (0x4BA, 0x4BB), + (0x4BC, 0x4BD), + (0x4BE, 0x4BF), + (0x4C0, 0x4CF), + (0x4C1, 0x4C2), + (0x4C3, 0x4C4), + (0x4C5, 0x4C6), + (0x4C7, 0x4C8), + (0x4C9, 0x4CA), + (0x4CB, 0x4CC), + (0x4CD, 0x4CE), + (0x4D0, 0x4D1), + (0x4D2, 0x4D3), + (0x4D4, 0x4D5), + (0x4D6, 0x4D7), + (0x4D8, 0x4D9), + (0x4DA, 0x4DB), + (0x4DC, 0x4DD), + (0x4DE, 0x4DF), + (0x4E0, 0x4E1), + (0x4E2, 0x4E3), + (0x4E4, 0x4E5), + (0x4E6, 0x4E7), + (0x4E8, 0x4E9), + (0x4EA, 0x4EB), + (0x4EC, 0x4ED), + (0x4EE, 0x4EF), + (0x4F0, 0x4F1), + (0x4F2, 0x4F3), + (0x4F4, 0x4F5), + (0x4F6, 0x4F7), + (0x4F8, 0x4F9), + (0x4FA, 0x4FB), + (0x4FC, 0x4FD), + (0x4FE, 0x4FF), + (0x500, 0x501), + (0x502, 0x503), + (0x504, 0x505), + (0x506, 0x507), + (0x508, 0x509), + (0x50A, 0x50B), + (0x50C, 0x50D), + (0x50E, 0x50F), + (0x510, 0x511), + (0x512, 0x513), + (0x514, 0x515), + (0x516, 0x517), + (0x518, 0x519), + (0x51A, 0x51B), + (0x51C, 0x51D), + (0x51E, 0x51F), + (0x520, 0x521), + (0x522, 0x523), + (0x524, 0x525), + (0x526, 0x527), + (0x528, 0x529), + (0x52A, 0x52B), + (0x52C, 0x52D), + (0x52E, 0x52F), + (0x531, 0x561), + (0x532, 0x562), + (0x533, 0x563), + (0x534, 0x564), + (0x535, 0x565), + (0x536, 0x566), + (0x537, 0x567), + (0x538, 0x568), + (0x539, 0x569), + (0x53A, 0x56A), + (0x53B, 0x56B), + (0x53C, 0x56C), + (0x53D, 0x56D), + (0x53E, 0x56E), + (0x53F, 0x56F), + (0x540, 0x570), + (0x541, 0x571), + (0x542, 0x572), + (0x543, 0x573), + (0x544, 0x574), + (0x545, 0x575), + (0x546, 0x576), + (0x547, 0x577), + (0x548, 0x578), + (0x549, 0x579), + (0x54A, 0x57A), + (0x54B, 0x57B), + (0x54C, 0x57C), + (0x54D, 0x57D), + (0x54E, 0x57E), + (0x54F, 0x57F), + (0x550, 0x580), + (0x551, 0x581), + (0x552, 0x582), + (0x553, 0x583), + (0x554, 0x584), + (0x555, 0x585), + (0x556, 0x586), + (0x10A0, 0x2D00), + (0x10A1, 0x2D01), + (0x10A2, 0x2D02), + (0x10A3, 0x2D03), + (0x10A4, 0x2D04), + (0x10A5, 0x2D05), + (0x10A6, 0x2D06), + (0x10A7, 0x2D07), + (0x10A8, 0x2D08), + (0x10A9, 0x2D09), + (0x10AA, 0x2D0A), + (0x10AB, 0x2D0B), + (0x10AC, 0x2D0C), + (0x10AD, 0x2D0D), + (0x10AE, 0x2D0E), + (0x10AF, 0x2D0F), + (0x10B0, 0x2D10), + (0x10B1, 0x2D11), + (0x10B2, 0x2D12), + (0x10B3, 0x2D13), + (0x10B4, 0x2D14), + (0x10B5, 0x2D15), + (0x10B6, 0x2D16), + (0x10B7, 0x2D17), + (0x10B8, 0x2D18), + (0x10B9, 0x2D19), + (0x10BA, 0x2D1A), + (0x10BB, 0x2D1B), + (0x10BC, 0x2D1C), + (0x10BD, 0x2D1D), + (0x10BE, 0x2D1E), + (0x10BF, 0x2D1F), + (0x10C0, 0x2D20), + (0x10C1, 0x2D21), + (0x10C2, 0x2D22), + (0x10C3, 0x2D23), + (0x10C4, 0x2D24), + (0x10C5, 0x2D25), + (0x10C7, 0x2D27), + (0x10CD, 0x2D2D), + (0x13F8, 0x13F0), + (0x13F9, 0x13F1), + (0x13FA, 0x13F2), + (0x13FB, 0x13F3), + (0x13FC, 0x13F4), + (0x13FD, 0x13F5), + (0x1C80, 0x432), + (0x1C81, 0x434), + (0x1C82, 0x43E), + (0x1C83, 0x441), + (0x1C84, 0x442), + (0x1C85, 0x442), + (0x1C86, 0x44A), + (0x1C87, 0x463), + (0x1C88, 0xA64B), + (0x1C89, 0x1C8A), + (0x1C90, 0x10D0), + (0x1C91, 0x10D1), + (0x1C92, 0x10D2), + (0x1C93, 0x10D3), + (0x1C94, 0x10D4), + (0x1C95, 0x10D5), + (0x1C96, 0x10D6), + (0x1C97, 0x10D7), + (0x1C98, 0x10D8), + (0x1C99, 0x10D9), + (0x1C9A, 0x10DA), + (0x1C9B, 0x10DB), + (0x1C9C, 0x10DC), + (0x1C9D, 0x10DD), + (0x1C9E, 0x10DE), + (0x1C9F, 0x10DF), + (0x1CA0, 0x10E0), + (0x1CA1, 0x10E1), + (0x1CA2, 0x10E2), + (0x1CA3, 0x10E3), + (0x1CA4, 0x10E4), + (0x1CA5, 0x10E5), + (0x1CA6, 0x10E6), + (0x1CA7, 0x10E7), + (0x1CA8, 0x10E8), + (0x1CA9, 0x10E9), + (0x1CAA, 0x10EA), + (0x1CAB, 0x10EB), + (0x1CAC, 0x10EC), + (0x1CAD, 0x10ED), + (0x1CAE, 0x10EE), + (0x1CAF, 0x10EF), + (0x1CB0, 0x10F0), + (0x1CB1, 0x10F1), + (0x1CB2, 0x10F2), + (0x1CB3, 0x10F3), + (0x1CB4, 0x10F4), + (0x1CB5, 0x10F5), + (0x1CB6, 0x10F6), + (0x1CB7, 0x10F7), + (0x1CB8, 0x10F8), + (0x1CB9, 0x10F9), + (0x1CBA, 0x10FA), + (0x1CBD, 0x10FD), + (0x1CBE, 0x10FE), + (0x1CBF, 0x10FF), + (0x1E00, 0x1E01), + (0x1E02, 0x1E03), + (0x1E04, 0x1E05), + (0x1E06, 0x1E07), + (0x1E08, 0x1E09), + (0x1E0A, 0x1E0B), + (0x1E0C, 0x1E0D), + (0x1E0E, 0x1E0F), + (0x1E10, 0x1E11), + (0x1E12, 0x1E13), + (0x1E14, 0x1E15), + (0x1E16, 0x1E17), + (0x1E18, 0x1E19), + (0x1E1A, 0x1E1B), + (0x1E1C, 0x1E1D), + (0x1E1E, 0x1E1F), + (0x1E20, 0x1E21), + (0x1E22, 0x1E23), + (0x1E24, 0x1E25), + (0x1E26, 0x1E27), + (0x1E28, 0x1E29), + (0x1E2A, 0x1E2B), + (0x1E2C, 0x1E2D), + (0x1E2E, 0x1E2F), + (0x1E30, 0x1E31), + (0x1E32, 0x1E33), + (0x1E34, 0x1E35), + (0x1E36, 0x1E37), + (0x1E38, 0x1E39), + (0x1E3A, 0x1E3B), + (0x1E3C, 0x1E3D), + (0x1E3E, 0x1E3F), + (0x1E40, 0x1E41), + (0x1E42, 0x1E43), + (0x1E44, 0x1E45), + (0x1E46, 0x1E47), + (0x1E48, 0x1E49), + (0x1E4A, 0x1E4B), + (0x1E4C, 0x1E4D), + (0x1E4E, 0x1E4F), + (0x1E50, 0x1E51), + (0x1E52, 0x1E53), + (0x1E54, 0x1E55), + (0x1E56, 0x1E57), + (0x1E58, 0x1E59), + (0x1E5A, 0x1E5B), + (0x1E5C, 0x1E5D), + (0x1E5E, 0x1E5F), + (0x1E60, 0x1E61), + (0x1E62, 0x1E63), + (0x1E64, 0x1E65), + (0x1E66, 0x1E67), + (0x1E68, 0x1E69), + (0x1E6A, 0x1E6B), + (0x1E6C, 0x1E6D), + (0x1E6E, 0x1E6F), + (0x1E70, 0x1E71), + (0x1E72, 0x1E73), + (0x1E74, 0x1E75), + (0x1E76, 0x1E77), + (0x1E78, 0x1E79), + (0x1E7A, 0x1E7B), + (0x1E7C, 0x1E7D), + (0x1E7E, 0x1E7F), + (0x1E80, 0x1E81), + (0x1E82, 0x1E83), + (0x1E84, 0x1E85), + (0x1E86, 0x1E87), + (0x1E88, 0x1E89), + (0x1E8A, 0x1E8B), + (0x1E8C, 0x1E8D), + (0x1E8E, 0x1E8F), + (0x1E90, 0x1E91), + (0x1E92, 0x1E93), + (0x1E94, 0x1E95), + (0x1E9B, 0x1E61), + (0x1E9E, 0xDF), + (0x1EA0, 0x1EA1), + (0x1EA2, 0x1EA3), + (0x1EA4, 0x1EA5), + (0x1EA6, 0x1EA7), + (0x1EA8, 0x1EA9), + (0x1EAA, 0x1EAB), + (0x1EAC, 0x1EAD), + (0x1EAE, 0x1EAF), + (0x1EB0, 0x1EB1), + (0x1EB2, 0x1EB3), + (0x1EB4, 0x1EB5), + (0x1EB6, 0x1EB7), + (0x1EB8, 0x1EB9), + (0x1EBA, 0x1EBB), + (0x1EBC, 0x1EBD), + (0x1EBE, 0x1EBF), + (0x1EC0, 0x1EC1), + (0x1EC2, 0x1EC3), + (0x1EC4, 0x1EC5), + (0x1EC6, 0x1EC7), + (0x1EC8, 0x1EC9), + (0x1ECA, 0x1ECB), + (0x1ECC, 0x1ECD), + (0x1ECE, 0x1ECF), + (0x1ED0, 0x1ED1), + (0x1ED2, 0x1ED3), + (0x1ED4, 0x1ED5), + (0x1ED6, 0x1ED7), + (0x1ED8, 0x1ED9), + (0x1EDA, 0x1EDB), + (0x1EDC, 0x1EDD), + (0x1EDE, 0x1EDF), + (0x1EE0, 0x1EE1), + (0x1EE2, 0x1EE3), + (0x1EE4, 0x1EE5), + (0x1EE6, 0x1EE7), + (0x1EE8, 0x1EE9), + (0x1EEA, 0x1EEB), + (0x1EEC, 0x1EED), + (0x1EEE, 0x1EEF), + (0x1EF0, 0x1EF1), + (0x1EF2, 0x1EF3), + (0x1EF4, 0x1EF5), + (0x1EF6, 0x1EF7), + (0x1EF8, 0x1EF9), + (0x1EFA, 0x1EFB), + (0x1EFC, 0x1EFD), + (0x1EFE, 0x1EFF), + (0x1F08, 0x1F00), + (0x1F09, 0x1F01), + (0x1F0A, 0x1F02), + (0x1F0B, 0x1F03), + (0x1F0C, 0x1F04), + (0x1F0D, 0x1F05), + (0x1F0E, 0x1F06), + (0x1F0F, 0x1F07), + (0x1F18, 0x1F10), + (0x1F19, 0x1F11), + (0x1F1A, 0x1F12), + (0x1F1B, 0x1F13), + (0x1F1C, 0x1F14), + (0x1F1D, 0x1F15), + (0x1F28, 0x1F20), + (0x1F29, 0x1F21), + (0x1F2A, 0x1F22), + (0x1F2B, 0x1F23), + (0x1F2C, 0x1F24), + (0x1F2D, 0x1F25), + (0x1F2E, 0x1F26), + (0x1F2F, 0x1F27), + (0x1F38, 0x1F30), + (0x1F39, 0x1F31), + (0x1F3A, 0x1F32), + (0x1F3B, 0x1F33), + (0x1F3C, 0x1F34), + (0x1F3D, 0x1F35), + (0x1F3E, 0x1F36), + (0x1F3F, 0x1F37), + (0x1F48, 0x1F40), + (0x1F49, 0x1F41), + (0x1F4A, 0x1F42), + (0x1F4B, 0x1F43), + (0x1F4C, 0x1F44), + (0x1F4D, 0x1F45), + (0x1F59, 0x1F51), + (0x1F5B, 0x1F53), + (0x1F5D, 0x1F55), + (0x1F5F, 0x1F57), + (0x1F68, 0x1F60), + (0x1F69, 0x1F61), + (0x1F6A, 0x1F62), + (0x1F6B, 0x1F63), + (0x1F6C, 0x1F64), + (0x1F6D, 0x1F65), + (0x1F6E, 0x1F66), + (0x1F6F, 0x1F67), + (0x1F88, 0x1F80), + (0x1F89, 0x1F81), + (0x1F8A, 0x1F82), + (0x1F8B, 0x1F83), + (0x1F8C, 0x1F84), + (0x1F8D, 0x1F85), + (0x1F8E, 0x1F86), + (0x1F8F, 0x1F87), + (0x1F98, 0x1F90), + (0x1F99, 0x1F91), + (0x1F9A, 0x1F92), + (0x1F9B, 0x1F93), + (0x1F9C, 0x1F94), + (0x1F9D, 0x1F95), + (0x1F9E, 0x1F96), + (0x1F9F, 0x1F97), + (0x1FA8, 0x1FA0), + (0x1FA9, 0x1FA1), + (0x1FAA, 0x1FA2), + (0x1FAB, 0x1FA3), + (0x1FAC, 0x1FA4), + (0x1FAD, 0x1FA5), + (0x1FAE, 0x1FA6), + (0x1FAF, 0x1FA7), + (0x1FB8, 0x1FB0), + (0x1FB9, 0x1FB1), + (0x1FBA, 0x1F70), + (0x1FBB, 0x1F71), + (0x1FBC, 0x1FB3), + (0x1FBE, 0x3B9), + (0x1FC8, 0x1F72), + (0x1FC9, 0x1F73), + (0x1FCA, 0x1F74), + (0x1FCB, 0x1F75), + (0x1FCC, 0x1FC3), + (0x1FD3, 0x390), + (0x1FD8, 0x1FD0), + (0x1FD9, 0x1FD1), + (0x1FDA, 0x1F76), + (0x1FDB, 0x1F77), + (0x1FE3, 0x3B0), + (0x1FE8, 0x1FE0), + (0x1FE9, 0x1FE1), + (0x1FEA, 0x1F7A), + (0x1FEB, 0x1F7B), + (0x1FEC, 0x1FE5), + (0x1FF8, 0x1F78), + (0x1FF9, 0x1F79), + (0x1FFA, 0x1F7C), + (0x1FFB, 0x1F7D), + (0x1FFC, 0x1FF3), + (0x2126, 0x3C9), + (0x212A, 0x6B), + (0x212B, 0xE5), + (0x2132, 0x214E), + (0x2160, 0x2170), + (0x2161, 0x2171), + (0x2162, 0x2172), + (0x2163, 0x2173), + (0x2164, 0x2174), + (0x2165, 0x2175), + (0x2166, 0x2176), + (0x2167, 0x2177), + (0x2168, 0x2178), + (0x2169, 0x2179), + (0x216A, 0x217A), + (0x216B, 0x217B), + (0x216C, 0x217C), + (0x216D, 0x217D), + (0x216E, 0x217E), + (0x216F, 0x217F), + (0x2183, 0x2184), + (0x24B6, 0x24D0), + (0x24B7, 0x24D1), + (0x24B8, 0x24D2), + (0x24B9, 0x24D3), + (0x24BA, 0x24D4), + (0x24BB, 0x24D5), + (0x24BC, 0x24D6), + (0x24BD, 0x24D7), + (0x24BE, 0x24D8), + (0x24BF, 0x24D9), + (0x24C0, 0x24DA), + (0x24C1, 0x24DB), + (0x24C2, 0x24DC), + (0x24C3, 0x24DD), + (0x24C4, 0x24DE), + (0x24C5, 0x24DF), + (0x24C6, 0x24E0), + (0x24C7, 0x24E1), + (0x24C8, 0x24E2), + (0x24C9, 0x24E3), + (0x24CA, 0x24E4), + (0x24CB, 0x24E5), + (0x24CC, 0x24E6), + (0x24CD, 0x24E7), + (0x24CE, 0x24E8), + (0x24CF, 0x24E9), + (0x2C00, 0x2C30), + (0x2C01, 0x2C31), + (0x2C02, 0x2C32), + (0x2C03, 0x2C33), + (0x2C04, 0x2C34), + (0x2C05, 0x2C35), + (0x2C06, 0x2C36), + (0x2C07, 0x2C37), + (0x2C08, 0x2C38), + (0x2C09, 0x2C39), + (0x2C0A, 0x2C3A), + (0x2C0B, 0x2C3B), + (0x2C0C, 0x2C3C), + (0x2C0D, 0x2C3D), + (0x2C0E, 0x2C3E), + (0x2C0F, 0x2C3F), + (0x2C10, 0x2C40), + (0x2C11, 0x2C41), + (0x2C12, 0x2C42), + (0x2C13, 0x2C43), + (0x2C14, 0x2C44), + (0x2C15, 0x2C45), + (0x2C16, 0x2C46), + (0x2C17, 0x2C47), + (0x2C18, 0x2C48), + (0x2C19, 0x2C49), + (0x2C1A, 0x2C4A), + (0x2C1B, 0x2C4B), + (0x2C1C, 0x2C4C), + (0x2C1D, 0x2C4D), + (0x2C1E, 0x2C4E), + (0x2C1F, 0x2C4F), + (0x2C20, 0x2C50), + (0x2C21, 0x2C51), + (0x2C22, 0x2C52), + (0x2C23, 0x2C53), + (0x2C24, 0x2C54), + (0x2C25, 0x2C55), + (0x2C26, 0x2C56), + (0x2C27, 0x2C57), + (0x2C28, 0x2C58), + (0x2C29, 0x2C59), + (0x2C2A, 0x2C5A), + (0x2C2B, 0x2C5B), + (0x2C2C, 0x2C5C), + (0x2C2D, 0x2C5D), + (0x2C2E, 0x2C5E), + (0x2C2F, 0x2C5F), + (0x2C60, 0x2C61), + (0x2C62, 0x26B), + (0x2C63, 0x1D7D), + (0x2C64, 0x27D), + (0x2C67, 0x2C68), + (0x2C69, 0x2C6A), + (0x2C6B, 0x2C6C), + (0x2C6D, 0x251), + (0x2C6E, 0x271), + (0x2C6F, 0x250), + (0x2C70, 0x252), + (0x2C72, 0x2C73), + (0x2C75, 0x2C76), + (0x2C7E, 0x23F), + (0x2C7F, 0x240), + (0x2C80, 0x2C81), + (0x2C82, 0x2C83), + (0x2C84, 0x2C85), + (0x2C86, 0x2C87), + (0x2C88, 0x2C89), + (0x2C8A, 0x2C8B), + (0x2C8C, 0x2C8D), + (0x2C8E, 0x2C8F), + (0x2C90, 0x2C91), + (0x2C92, 0x2C93), + (0x2C94, 0x2C95), + (0x2C96, 0x2C97), + (0x2C98, 0x2C99), + (0x2C9A, 0x2C9B), + (0x2C9C, 0x2C9D), + (0x2C9E, 0x2C9F), + (0x2CA0, 0x2CA1), + (0x2CA2, 0x2CA3), + (0x2CA4, 0x2CA5), + (0x2CA6, 0x2CA7), + (0x2CA8, 0x2CA9), + (0x2CAA, 0x2CAB), + (0x2CAC, 0x2CAD), + (0x2CAE, 0x2CAF), + (0x2CB0, 0x2CB1), + (0x2CB2, 0x2CB3), + (0x2CB4, 0x2CB5), + (0x2CB6, 0x2CB7), + (0x2CB8, 0x2CB9), + (0x2CBA, 0x2CBB), + (0x2CBC, 0x2CBD), + (0x2CBE, 0x2CBF), + (0x2CC0, 0x2CC1), + (0x2CC2, 0x2CC3), + (0x2CC4, 0x2CC5), + (0x2CC6, 0x2CC7), + (0x2CC8, 0x2CC9), + (0x2CCA, 0x2CCB), + (0x2CCC, 0x2CCD), + (0x2CCE, 0x2CCF), + (0x2CD0, 0x2CD1), + (0x2CD2, 0x2CD3), + (0x2CD4, 0x2CD5), + (0x2CD6, 0x2CD7), + (0x2CD8, 0x2CD9), + (0x2CDA, 0x2CDB), + (0x2CDC, 0x2CDD), + (0x2CDE, 0x2CDF), + (0x2CE0, 0x2CE1), + (0x2CE2, 0x2CE3), + (0x2CEB, 0x2CEC), + (0x2CED, 0x2CEE), + (0x2CF2, 0x2CF3), + (0xA640, 0xA641), + (0xA642, 0xA643), + (0xA644, 0xA645), + (0xA646, 0xA647), + (0xA648, 0xA649), + (0xA64A, 0xA64B), + (0xA64C, 0xA64D), + (0xA64E, 0xA64F), + (0xA650, 0xA651), + (0xA652, 0xA653), + (0xA654, 0xA655), + (0xA656, 0xA657), + (0xA658, 0xA659), + (0xA65A, 0xA65B), + (0xA65C, 0xA65D), + (0xA65E, 0xA65F), + (0xA660, 0xA661), + (0xA662, 0xA663), + (0xA664, 0xA665), + (0xA666, 0xA667), + (0xA668, 0xA669), + (0xA66A, 0xA66B), + (0xA66C, 0xA66D), + (0xA680, 0xA681), + (0xA682, 0xA683), + (0xA684, 0xA685), + (0xA686, 0xA687), + (0xA688, 0xA689), + (0xA68A, 0xA68B), + (0xA68C, 0xA68D), + (0xA68E, 0xA68F), + (0xA690, 0xA691), + (0xA692, 0xA693), + (0xA694, 0xA695), + (0xA696, 0xA697), + (0xA698, 0xA699), + (0xA69A, 0xA69B), + (0xA722, 0xA723), + (0xA724, 0xA725), + (0xA726, 0xA727), + (0xA728, 0xA729), + (0xA72A, 0xA72B), + (0xA72C, 0xA72D), + (0xA72E, 0xA72F), + (0xA732, 0xA733), + (0xA734, 0xA735), + (0xA736, 0xA737), + (0xA738, 0xA739), + (0xA73A, 0xA73B), + (0xA73C, 0xA73D), + (0xA73E, 0xA73F), + (0xA740, 0xA741), + (0xA742, 0xA743), + (0xA744, 0xA745), + (0xA746, 0xA747), + (0xA748, 0xA749), + (0xA74A, 0xA74B), + (0xA74C, 0xA74D), + (0xA74E, 0xA74F), + (0xA750, 0xA751), + (0xA752, 0xA753), + (0xA754, 0xA755), + (0xA756, 0xA757), + (0xA758, 0xA759), + (0xA75A, 0xA75B), + (0xA75C, 0xA75D), + (0xA75E, 0xA75F), + (0xA760, 0xA761), + (0xA762, 0xA763), + (0xA764, 0xA765), + (0xA766, 0xA767), + (0xA768, 0xA769), + (0xA76A, 0xA76B), + (0xA76C, 0xA76D), + (0xA76E, 0xA76F), + (0xA779, 0xA77A), + (0xA77B, 0xA77C), + (0xA77D, 0x1D79), + (0xA77E, 0xA77F), + (0xA780, 0xA781), + (0xA782, 0xA783), + (0xA784, 0xA785), + (0xA786, 0xA787), + (0xA78B, 0xA78C), + (0xA78D, 0x265), + (0xA790, 0xA791), + (0xA792, 0xA793), + (0xA796, 0xA797), + (0xA798, 0xA799), + (0xA79A, 0xA79B), + (0xA79C, 0xA79D), + (0xA79E, 0xA79F), + (0xA7A0, 0xA7A1), + (0xA7A2, 0xA7A3), + (0xA7A4, 0xA7A5), + (0xA7A6, 0xA7A7), + (0xA7A8, 0xA7A9), + (0xA7AA, 0x266), + (0xA7AB, 0x25C), + (0xA7AC, 0x261), + (0xA7AD, 0x26C), + (0xA7AE, 0x26A), + (0xA7B0, 0x29E), + (0xA7B1, 0x287), + (0xA7B2, 0x29D), + (0xA7B3, 0xAB53), + (0xA7B4, 0xA7B5), + (0xA7B6, 0xA7B7), + (0xA7B8, 0xA7B9), + (0xA7BA, 0xA7BB), + (0xA7BC, 0xA7BD), + (0xA7BE, 0xA7BF), + (0xA7C0, 0xA7C1), + (0xA7C2, 0xA7C3), + (0xA7C4, 0xA794), + (0xA7C5, 0x282), + (0xA7C6, 0x1D8E), + (0xA7C7, 0xA7C8), + (0xA7C9, 0xA7CA), + (0xA7CB, 0x264), + (0xA7CC, 0xA7CD), + (0xA7CE, 0xA7CF), + (0xA7D0, 0xA7D1), + (0xA7D2, 0xA7D3), + (0xA7D4, 0xA7D5), + (0xA7D6, 0xA7D7), + (0xA7D8, 0xA7D9), + (0xA7DA, 0xA7DB), + (0xA7DC, 0x19B), + (0xA7DD, 0x277), + (0xA7E2, 0x27C), + (0xA7F5, 0xA7F6), + (0xAB6C, 0xAB4B), + (0xAB6D, 0xAB4C), + (0xAB70, 0x13A0), + (0xAB71, 0x13A1), + (0xAB72, 0x13A2), + (0xAB73, 0x13A3), + (0xAB74, 0x13A4), + (0xAB75, 0x13A5), + (0xAB76, 0x13A6), + (0xAB77, 0x13A7), + (0xAB78, 0x13A8), + (0xAB79, 0x13A9), + (0xAB7A, 0x13AA), + (0xAB7B, 0x13AB), + (0xAB7C, 0x13AC), + (0xAB7D, 0x13AD), + (0xAB7E, 0x13AE), + (0xAB7F, 0x13AF), + (0xAB80, 0x13B0), + (0xAB81, 0x13B1), + (0xAB82, 0x13B2), + (0xAB83, 0x13B3), + (0xAB84, 0x13B4), + (0xAB85, 0x13B5), + (0xAB86, 0x13B6), + (0xAB87, 0x13B7), + (0xAB88, 0x13B8), + (0xAB89, 0x13B9), + (0xAB8A, 0x13BA), + (0xAB8B, 0x13BB), + (0xAB8C, 0x13BC), + (0xAB8D, 0x13BD), + (0xAB8E, 0x13BE), + (0xAB8F, 0x13BF), + (0xAB90, 0x13C0), + (0xAB91, 0x13C1), + (0xAB92, 0x13C2), + (0xAB93, 0x13C3), + (0xAB94, 0x13C4), + (0xAB95, 0x13C5), + (0xAB96, 0x13C6), + (0xAB97, 0x13C7), + (0xAB98, 0x13C8), + (0xAB99, 0x13C9), + (0xAB9A, 0x13CA), + (0xAB9B, 0x13CB), + (0xAB9C, 0x13CC), + (0xAB9D, 0x13CD), + (0xAB9E, 0x13CE), + (0xAB9F, 0x13CF), + (0xABA0, 0x13D0), + (0xABA1, 0x13D1), + (0xABA2, 0x13D2), + (0xABA3, 0x13D3), + (0xABA4, 0x13D4), + (0xABA5, 0x13D5), + (0xABA6, 0x13D6), + (0xABA7, 0x13D7), + (0xABA8, 0x13D8), + (0xABA9, 0x13D9), + (0xABAA, 0x13DA), + (0xABAB, 0x13DB), + (0xABAC, 0x13DC), + (0xABAD, 0x13DD), + (0xABAE, 0x13DE), + (0xABAF, 0x13DF), + (0xABB0, 0x13E0), + (0xABB1, 0x13E1), + (0xABB2, 0x13E2), + (0xABB3, 0x13E3), + (0xABB4, 0x13E4), + (0xABB5, 0x13E5), + (0xABB6, 0x13E6), + (0xABB7, 0x13E7), + (0xABB8, 0x13E8), + (0xABB9, 0x13E9), + (0xABBA, 0x13EA), + (0xABBB, 0x13EB), + (0xABBC, 0x13EC), + (0xABBD, 0x13ED), + (0xABBE, 0x13EE), + (0xABBF, 0x13EF), + (0xFB05, 0xFB06), + (0xFF21, 0xFF41), + (0xFF22, 0xFF42), + (0xFF23, 0xFF43), + (0xFF24, 0xFF44), + (0xFF25, 0xFF45), + (0xFF26, 0xFF46), + (0xFF27, 0xFF47), + (0xFF28, 0xFF48), + (0xFF29, 0xFF49), + (0xFF2A, 0xFF4A), + (0xFF2B, 0xFF4B), + (0xFF2C, 0xFF4C), + (0xFF2D, 0xFF4D), + (0xFF2E, 0xFF4E), + (0xFF2F, 0xFF4F), + (0xFF30, 0xFF50), + (0xFF31, 0xFF51), + (0xFF32, 0xFF52), + (0xFF33, 0xFF53), + (0xFF34, 0xFF54), + (0xFF35, 0xFF55), + (0xFF36, 0xFF56), + (0xFF37, 0xFF57), + (0xFF38, 0xFF58), + (0xFF39, 0xFF59), + (0xFF3A, 0xFF5A), + (0x10400, 0x10428), + (0x10401, 0x10429), + (0x10402, 0x1042A), + (0x10403, 0x1042B), + (0x10404, 0x1042C), + (0x10405, 0x1042D), + (0x10406, 0x1042E), + (0x10407, 0x1042F), + (0x10408, 0x10430), + (0x10409, 0x10431), + (0x1040A, 0x10432), + (0x1040B, 0x10433), + (0x1040C, 0x10434), + (0x1040D, 0x10435), + (0x1040E, 0x10436), + (0x1040F, 0x10437), + (0x10410, 0x10438), + (0x10411, 0x10439), + (0x10412, 0x1043A), + (0x10413, 0x1043B), + (0x10414, 0x1043C), + (0x10415, 0x1043D), + (0x10416, 0x1043E), + (0x10417, 0x1043F), + (0x10418, 0x10440), + (0x10419, 0x10441), + (0x1041A, 0x10442), + (0x1041B, 0x10443), + (0x1041C, 0x10444), + (0x1041D, 0x10445), + (0x1041E, 0x10446), + (0x1041F, 0x10447), + (0x10420, 0x10448), + (0x10421, 0x10449), + (0x10422, 0x1044A), + (0x10423, 0x1044B), + (0x10424, 0x1044C), + (0x10425, 0x1044D), + (0x10426, 0x1044E), + (0x10427, 0x1044F), + (0x104B0, 0x104D8), + (0x104B1, 0x104D9), + (0x104B2, 0x104DA), + (0x104B3, 0x104DB), + (0x104B4, 0x104DC), + (0x104B5, 0x104DD), + (0x104B6, 0x104DE), + (0x104B7, 0x104DF), + (0x104B8, 0x104E0), + (0x104B9, 0x104E1), + (0x104BA, 0x104E2), + (0x104BB, 0x104E3), + (0x104BC, 0x104E4), + (0x104BD, 0x104E5), + (0x104BE, 0x104E6), + (0x104BF, 0x104E7), + (0x104C0, 0x104E8), + (0x104C1, 0x104E9), + (0x104C2, 0x104EA), + (0x104C3, 0x104EB), + (0x104C4, 0x104EC), + (0x104C5, 0x104ED), + (0x104C6, 0x104EE), + (0x104C7, 0x104EF), + (0x104C8, 0x104F0), + (0x104C9, 0x104F1), + (0x104CA, 0x104F2), + (0x104CB, 0x104F3), + (0x104CC, 0x104F4), + (0x104CD, 0x104F5), + (0x104CE, 0x104F6), + (0x104CF, 0x104F7), + (0x104D0, 0x104F8), + (0x104D1, 0x104F9), + (0x104D2, 0x104FA), + (0x104D3, 0x104FB), + (0x10570, 0x10597), + (0x10571, 0x10598), + (0x10572, 0x10599), + (0x10573, 0x1059A), + (0x10574, 0x1059B), + (0x10575, 0x1059C), + (0x10576, 0x1059D), + (0x10577, 0x1059E), + (0x10578, 0x1059F), + (0x10579, 0x105A0), + (0x1057A, 0x105A1), + (0x1057C, 0x105A3), + (0x1057D, 0x105A4), + (0x1057E, 0x105A5), + (0x1057F, 0x105A6), + (0x10580, 0x105A7), + (0x10581, 0x105A8), + (0x10582, 0x105A9), + (0x10583, 0x105AA), + (0x10584, 0x105AB), + (0x10585, 0x105AC), + (0x10586, 0x105AD), + (0x10587, 0x105AE), + (0x10588, 0x105AF), + (0x10589, 0x105B0), + (0x1058A, 0x105B1), + (0x1058C, 0x105B3), + (0x1058D, 0x105B4), + (0x1058E, 0x105B5), + (0x1058F, 0x105B6), + (0x10590, 0x105B7), + (0x10591, 0x105B8), + (0x10592, 0x105B9), + (0x10594, 0x105BB), + (0x10595, 0x105BC), + (0x10C80, 0x10CC0), + (0x10C81, 0x10CC1), + (0x10C82, 0x10CC2), + (0x10C83, 0x10CC3), + (0x10C84, 0x10CC4), + (0x10C85, 0x10CC5), + (0x10C86, 0x10CC6), + (0x10C87, 0x10CC7), + (0x10C88, 0x10CC8), + (0x10C89, 0x10CC9), + (0x10C8A, 0x10CCA), + (0x10C8B, 0x10CCB), + (0x10C8C, 0x10CCC), + (0x10C8D, 0x10CCD), + (0x10C8E, 0x10CCE), + (0x10C8F, 0x10CCF), + (0x10C90, 0x10CD0), + (0x10C91, 0x10CD1), + (0x10C92, 0x10CD2), + (0x10C93, 0x10CD3), + (0x10C94, 0x10CD4), + (0x10C95, 0x10CD5), + (0x10C96, 0x10CD6), + (0x10C97, 0x10CD7), + (0x10C98, 0x10CD8), + (0x10C99, 0x10CD9), + (0x10C9A, 0x10CDA), + (0x10C9B, 0x10CDB), + (0x10C9C, 0x10CDC), + (0x10C9D, 0x10CDD), + (0x10C9E, 0x10CDE), + (0x10C9F, 0x10CDF), + (0x10CA0, 0x10CE0), + (0x10CA1, 0x10CE1), + (0x10CA2, 0x10CE2), + (0x10CA3, 0x10CE3), + (0x10CA4, 0x10CE4), + (0x10CA5, 0x10CE5), + (0x10CA6, 0x10CE6), + (0x10CA7, 0x10CE7), + (0x10CA8, 0x10CE8), + (0x10CA9, 0x10CE9), + (0x10CAA, 0x10CEA), + (0x10CAB, 0x10CEB), + (0x10CAC, 0x10CEC), + (0x10CAD, 0x10CED), + (0x10CAE, 0x10CEE), + (0x10CAF, 0x10CEF), + (0x10CB0, 0x10CF0), + (0x10CB1, 0x10CF1), + (0x10CB2, 0x10CF2), + (0x10D50, 0x10D70), + (0x10D51, 0x10D71), + (0x10D52, 0x10D72), + (0x10D53, 0x10D73), + (0x10D54, 0x10D74), + (0x10D55, 0x10D75), + (0x10D56, 0x10D76), + (0x10D57, 0x10D77), + (0x10D58, 0x10D78), + (0x10D59, 0x10D79), + (0x10D5A, 0x10D7A), + (0x10D5B, 0x10D7B), + (0x10D5C, 0x10D7C), + (0x10D5D, 0x10D7D), + (0x10D5E, 0x10D7E), + (0x10D5F, 0x10D7F), + (0x10D60, 0x10D80), + (0x10D61, 0x10D81), + (0x10D62, 0x10D82), + (0x10D63, 0x10D83), + (0x10D64, 0x10D84), + (0x10D65, 0x10D85), + (0x118A0, 0x118C0), + (0x118A1, 0x118C1), + (0x118A2, 0x118C2), + (0x118A3, 0x118C3), + (0x118A4, 0x118C4), + (0x118A5, 0x118C5), + (0x118A6, 0x118C6), + (0x118A7, 0x118C7), + (0x118A8, 0x118C8), + (0x118A9, 0x118C9), + (0x118AA, 0x118CA), + (0x118AB, 0x118CB), + (0x118AC, 0x118CC), + (0x118AD, 0x118CD), + (0x118AE, 0x118CE), + (0x118AF, 0x118CF), + (0x118B0, 0x118D0), + (0x118B1, 0x118D1), + (0x118B2, 0x118D2), + (0x118B3, 0x118D3), + (0x118B4, 0x118D4), + (0x118B5, 0x118D5), + (0x118B6, 0x118D6), + (0x118B7, 0x118D7), + (0x118B8, 0x118D8), + (0x118B9, 0x118D9), + (0x118BA, 0x118DA), + (0x118BB, 0x118DB), + (0x118BC, 0x118DC), + (0x118BD, 0x118DD), + (0x118BE, 0x118DE), + (0x118BF, 0x118DF), + (0x16E40, 0x16E60), + (0x16E41, 0x16E61), + (0x16E42, 0x16E62), + (0x16E43, 0x16E63), + (0x16E44, 0x16E64), + (0x16E45, 0x16E65), + (0x16E46, 0x16E66), + (0x16E47, 0x16E67), + (0x16E48, 0x16E68), + (0x16E49, 0x16E69), + (0x16E4A, 0x16E6A), + (0x16E4B, 0x16E6B), + (0x16E4C, 0x16E6C), + (0x16E4D, 0x16E6D), + (0x16E4E, 0x16E6E), + (0x16E4F, 0x16E6F), + (0x16E50, 0x16E70), + (0x16E51, 0x16E71), + (0x16E52, 0x16E72), + (0x16E53, 0x16E73), + (0x16E54, 0x16E74), + (0x16E55, 0x16E75), + (0x16E56, 0x16E76), + (0x16E57, 0x16E77), + (0x16E58, 0x16E78), + (0x16E59, 0x16E79), + (0x16E5A, 0x16E7A), + (0x16E5B, 0x16E7B), + (0x16E5C, 0x16E7C), + (0x16E5D, 0x16E7D), + (0x16E5E, 0x16E7E), + (0x16E5F, 0x16E7F), + (0x16EA0, 0x16EBB), + (0x16EA1, 0x16EBC), + (0x16EA2, 0x16EBD), + (0x16EA3, 0x16EBE), + (0x16EA4, 0x16EBF), + (0x16EA5, 0x16EC0), + (0x16EA6, 0x16EC1), + (0x16EA7, 0x16EC2), + (0x16EA8, 0x16EC3), + (0x16EA9, 0x16EC4), + (0x16EAA, 0x16EC5), + (0x16EAB, 0x16EC6), + (0x16EAC, 0x16EC7), + (0x16EAD, 0x16EC8), + (0x16EAE, 0x16EC9), + (0x16EAF, 0x16ECA), + (0x16EB0, 0x16ECB), + (0x16EB1, 0x16ECC), + (0x16EB2, 0x16ECD), + (0x16EB3, 0x16ECE), + (0x16EB4, 0x16ECF), + (0x16EB5, 0x16ED0), + (0x16EB6, 0x16ED1), + (0x16EB7, 0x16ED2), + (0x16EB8, 0x16ED3), + (0x1DF40, 0x1DF41), + (0x1DF48, 0x1DF49), + (0x1DF4A, 0x1DF4B), + (0x1DF4D, 0x1DF4E), + (0x1DF51, 0x1DF52), + (0x1DF68, 0x1DF69), + (0x1DF6A, 0x1DF6B), + (0x1DF6C, 0x1DF6D), + (0x1DF6E, 0x1DF6F), + (0x1DF72, 0x1DF73), + (0x1DF74, 0x1DF75), + (0x1DF76, 0x1DF77), + (0x1DF78, 0x1DF79), + (0x1DF7A, 0x1DF7B), + (0x1DF7C, 0x1DF7D), + (0x1DF7E, 0x1DF7F), + (0x1DF95, 0xDF), + (0x1E900, 0x1E922), + (0x1E901, 0x1E923), + (0x1E902, 0x1E924), + (0x1E903, 0x1E925), + (0x1E904, 0x1E926), + (0x1E905, 0x1E927), + (0x1E906, 0x1E928), + (0x1E907, 0x1E929), + (0x1E908, 0x1E92A), + (0x1E909, 0x1E92B), + (0x1E90A, 0x1E92C), + (0x1E90B, 0x1E92D), + (0x1E90C, 0x1E92E), + (0x1E90D, 0x1E92F), + (0x1E90E, 0x1E930), + (0x1E90F, 0x1E931), + (0x1E910, 0x1E932), + (0x1E911, 0x1E933), + (0x1E912, 0x1E934), + (0x1E913, 0x1E935), + (0x1E914, 0x1E936), + (0x1E915, 0x1E937), + (0x1E916, 0x1E938), + (0x1E917, 0x1E939), + (0x1E918, 0x1E93A), + (0x1E919, 0x1E93B), + (0x1E91A, 0x1E93C), + (0x1E91B, 0x1E93D), + (0x1E91C, 0x1E93E), + (0x1E91D, 0x1E93F), + (0x1E91E, 0x1E940), + (0x1E91F, 0x1E941), + (0x1E920, 0x1E942), + (0x1E921, 0x1E943), +]; + +/// `SIMPLE_FOLD` as (folded, code point), sorted. +pub static SIMPLE_FOLD_INVERSE: &[(u32, u32)] = &[ + (0x61, 0x41), + (0x62, 0x42), + (0x63, 0x43), + (0x64, 0x44), + (0x65, 0x45), + (0x66, 0x46), + (0x67, 0x47), + (0x68, 0x48), + (0x69, 0x49), + (0x6A, 0x4A), + (0x6B, 0x4B), + (0x6B, 0x212A), + (0x6C, 0x4C), + (0x6D, 0x4D), + (0x6E, 0x4E), + (0x6F, 0x4F), + (0x70, 0x50), + (0x71, 0x51), + (0x72, 0x52), + (0x73, 0x53), + (0x73, 0x17F), + (0x74, 0x54), + (0x75, 0x55), + (0x76, 0x56), + (0x77, 0x57), + (0x78, 0x58), + (0x79, 0x59), + (0x7A, 0x5A), + (0xDF, 0x1E9E), + (0xDF, 0x1DF95), + (0xE0, 0xC0), + (0xE1, 0xC1), + (0xE2, 0xC2), + (0xE3, 0xC3), + (0xE4, 0xC4), + (0xE5, 0xC5), + (0xE5, 0x212B), + (0xE6, 0xC6), + (0xE7, 0xC7), + (0xE8, 0xC8), + (0xE9, 0xC9), + (0xEA, 0xCA), + (0xEB, 0xCB), + (0xEC, 0xCC), + (0xED, 0xCD), + (0xEE, 0xCE), + (0xEF, 0xCF), + (0xF0, 0xD0), + (0xF1, 0xD1), + (0xF2, 0xD2), + (0xF3, 0xD3), + (0xF4, 0xD4), + (0xF5, 0xD5), + (0xF6, 0xD6), + (0xF8, 0xD8), + (0xF9, 0xD9), + (0xFA, 0xDA), + (0xFB, 0xDB), + (0xFC, 0xDC), + (0xFD, 0xDD), + (0xFE, 0xDE), + (0xFF, 0x178), + (0x101, 0x100), + (0x103, 0x102), + (0x105, 0x104), + (0x107, 0x106), + (0x109, 0x108), + (0x10B, 0x10A), + (0x10D, 0x10C), + (0x10F, 0x10E), + (0x111, 0x110), + (0x113, 0x112), + (0x115, 0x114), + (0x117, 0x116), + (0x119, 0x118), + (0x11B, 0x11A), + (0x11D, 0x11C), + (0x11F, 0x11E), + (0x121, 0x120), + (0x123, 0x122), + (0x125, 0x124), + (0x127, 0x126), + (0x129, 0x128), + (0x12B, 0x12A), + (0x12D, 0x12C), + (0x12F, 0x12E), + (0x133, 0x132), + (0x135, 0x134), + (0x137, 0x136), + (0x13A, 0x139), + (0x13C, 0x13B), + (0x13E, 0x13D), + (0x140, 0x13F), + (0x142, 0x141), + (0x144, 0x143), + (0x146, 0x145), + (0x148, 0x147), + (0x14B, 0x14A), + (0x14D, 0x14C), + (0x14F, 0x14E), + (0x151, 0x150), + (0x153, 0x152), + (0x155, 0x154), + (0x157, 0x156), + (0x159, 0x158), + (0x15B, 0x15A), + (0x15D, 0x15C), + (0x15F, 0x15E), + (0x161, 0x160), + (0x163, 0x162), + (0x165, 0x164), + (0x167, 0x166), + (0x169, 0x168), + (0x16B, 0x16A), + (0x16D, 0x16C), + (0x16F, 0x16E), + (0x171, 0x170), + (0x173, 0x172), + (0x175, 0x174), + (0x177, 0x176), + (0x17A, 0x179), + (0x17C, 0x17B), + (0x17E, 0x17D), + (0x180, 0x243), + (0x183, 0x182), + (0x185, 0x184), + (0x188, 0x187), + (0x18C, 0x18B), + (0x192, 0x191), + (0x195, 0x1F6), + (0x199, 0x198), + (0x19A, 0x23D), + (0x19B, 0xA7DC), + (0x19E, 0x220), + (0x1A1, 0x1A0), + (0x1A3, 0x1A2), + (0x1A5, 0x1A4), + (0x1A8, 0x1A7), + (0x1AD, 0x1AC), + (0x1B0, 0x1AF), + (0x1B4, 0x1B3), + (0x1B6, 0x1B5), + (0x1B9, 0x1B8), + (0x1BD, 0x1BC), + (0x1BF, 0x1F7), + (0x1C6, 0x1C4), + (0x1C6, 0x1C5), + (0x1C9, 0x1C7), + (0x1C9, 0x1C8), + (0x1CC, 0x1CA), + (0x1CC, 0x1CB), + (0x1CE, 0x1CD), + (0x1D0, 0x1CF), + (0x1D2, 0x1D1), + (0x1D4, 0x1D3), + (0x1D6, 0x1D5), + (0x1D8, 0x1D7), + (0x1DA, 0x1D9), + (0x1DC, 0x1DB), + (0x1DD, 0x18E), + (0x1DF, 0x1DE), + (0x1E1, 0x1E0), + (0x1E3, 0x1E2), + (0x1E5, 0x1E4), + (0x1E7, 0x1E6), + (0x1E9, 0x1E8), + (0x1EB, 0x1EA), + (0x1ED, 0x1EC), + (0x1EF, 0x1EE), + (0x1F3, 0x1F1), + (0x1F3, 0x1F2), + (0x1F5, 0x1F4), + (0x1F9, 0x1F8), + (0x1FB, 0x1FA), + (0x1FD, 0x1FC), + (0x1FF, 0x1FE), + (0x201, 0x200), + (0x203, 0x202), + (0x205, 0x204), + (0x207, 0x206), + (0x209, 0x208), + (0x20B, 0x20A), + (0x20D, 0x20C), + (0x20F, 0x20E), + (0x211, 0x210), + (0x213, 0x212), + (0x215, 0x214), + (0x217, 0x216), + (0x219, 0x218), + (0x21B, 0x21A), + (0x21D, 0x21C), + (0x21F, 0x21E), + (0x223, 0x222), + (0x225, 0x224), + (0x227, 0x226), + (0x229, 0x228), + (0x22B, 0x22A), + (0x22D, 0x22C), + (0x22F, 0x22E), + (0x231, 0x230), + (0x233, 0x232), + (0x23C, 0x23B), + (0x23F, 0x2C7E), + (0x240, 0x2C7F), + (0x242, 0x241), + (0x247, 0x246), + (0x249, 0x248), + (0x24B, 0x24A), + (0x24D, 0x24C), + (0x24F, 0x24E), + (0x250, 0x2C6F), + (0x251, 0x2C6D), + (0x252, 0x2C70), + (0x253, 0x181), + (0x254, 0x186), + (0x256, 0x189), + (0x257, 0x18A), + (0x259, 0x18F), + (0x25B, 0x190), + (0x25C, 0xA7AB), + (0x260, 0x193), + (0x261, 0xA7AC), + (0x263, 0x194), + (0x264, 0xA7CB), + (0x265, 0xA78D), + (0x266, 0xA7AA), + (0x268, 0x197), + (0x269, 0x196), + (0x26A, 0xA7AE), + (0x26B, 0x2C62), + (0x26C, 0xA7AD), + (0x26F, 0x19C), + (0x271, 0x2C6E), + (0x272, 0x19D), + (0x275, 0x19F), + (0x277, 0xA7DD), + (0x27C, 0xA7E2), + (0x27D, 0x2C64), + (0x280, 0x1A6), + (0x282, 0xA7C5), + (0x283, 0x1A9), + (0x287, 0xA7B1), + (0x288, 0x1AE), + (0x289, 0x244), + (0x28A, 0x1B1), + (0x28B, 0x1B2), + (0x28C, 0x245), + (0x292, 0x1B7), + (0x29D, 0xA7B2), + (0x29E, 0xA7B0), + (0x371, 0x370), + (0x373, 0x372), + (0x377, 0x376), + (0x37B, 0x3FD), + (0x37C, 0x3FE), + (0x37D, 0x3FF), + (0x390, 0x1FD3), + (0x3AC, 0x386), + (0x3AD, 0x388), + (0x3AE, 0x389), + (0x3AF, 0x38A), + (0x3B0, 0x1FE3), + (0x3B1, 0x391), + (0x3B2, 0x392), + (0x3B2, 0x3D0), + (0x3B3, 0x393), + (0x3B4, 0x394), + (0x3B5, 0x395), + (0x3B5, 0x3F5), + (0x3B6, 0x396), + (0x3B7, 0x397), + (0x3B8, 0x398), + (0x3B8, 0x3D1), + (0x3B8, 0x3F4), + (0x3B9, 0x345), + (0x3B9, 0x399), + (0x3B9, 0x1FBE), + (0x3BA, 0x39A), + (0x3BA, 0x3F0), + (0x3BB, 0x39B), + (0x3BC, 0xB5), + (0x3BC, 0x39C), + (0x3BD, 0x39D), + (0x3BE, 0x39E), + (0x3BF, 0x39F), + (0x3C0, 0x3A0), + (0x3C0, 0x3D6), + (0x3C1, 0x3A1), + (0x3C1, 0x3F1), + (0x3C3, 0x3A3), + (0x3C3, 0x3C2), + (0x3C4, 0x3A4), + (0x3C5, 0x3A5), + (0x3C6, 0x3A6), + (0x3C6, 0x3D5), + (0x3C7, 0x3A7), + (0x3C8, 0x3A8), + (0x3C9, 0x3A9), + (0x3C9, 0x2126), + (0x3CA, 0x3AA), + (0x3CB, 0x3AB), + (0x3CC, 0x38C), + (0x3CD, 0x38E), + (0x3CE, 0x38F), + (0x3D7, 0x3CF), + (0x3D9, 0x3D8), + (0x3DB, 0x3DA), + (0x3DD, 0x3DC), + (0x3DF, 0x3DE), + (0x3E1, 0x3E0), + (0x3E3, 0x3E2), + (0x3E5, 0x3E4), + (0x3E7, 0x3E6), + (0x3E9, 0x3E8), + (0x3EB, 0x3EA), + (0x3ED, 0x3EC), + (0x3EF, 0x3EE), + (0x3F2, 0x3F9), + (0x3F3, 0x37F), + (0x3F8, 0x3F7), + (0x3FB, 0x3FA), + (0x430, 0x410), + (0x431, 0x411), + (0x432, 0x412), + (0x432, 0x1C80), + (0x433, 0x413), + (0x434, 0x414), + (0x434, 0x1C81), + (0x435, 0x415), + (0x436, 0x416), + (0x437, 0x417), + (0x438, 0x418), + (0x439, 0x419), + (0x43A, 0x41A), + (0x43B, 0x41B), + (0x43C, 0x41C), + (0x43D, 0x41D), + (0x43E, 0x41E), + (0x43E, 0x1C82), + (0x43F, 0x41F), + (0x440, 0x420), + (0x441, 0x421), + (0x441, 0x1C83), + (0x442, 0x422), + (0x442, 0x1C84), + (0x442, 0x1C85), + (0x443, 0x423), + (0x444, 0x424), + (0x445, 0x425), + (0x446, 0x426), + (0x447, 0x427), + (0x448, 0x428), + (0x449, 0x429), + (0x44A, 0x42A), + (0x44A, 0x1C86), + (0x44B, 0x42B), + (0x44C, 0x42C), + (0x44D, 0x42D), + (0x44E, 0x42E), + (0x44F, 0x42F), + (0x450, 0x400), + (0x451, 0x401), + (0x452, 0x402), + (0x453, 0x403), + (0x454, 0x404), + (0x455, 0x405), + (0x456, 0x406), + (0x457, 0x407), + (0x458, 0x408), + (0x459, 0x409), + (0x45A, 0x40A), + (0x45B, 0x40B), + (0x45C, 0x40C), + (0x45D, 0x40D), + (0x45E, 0x40E), + (0x45F, 0x40F), + (0x461, 0x460), + (0x463, 0x462), + (0x463, 0x1C87), + (0x465, 0x464), + (0x467, 0x466), + (0x469, 0x468), + (0x46B, 0x46A), + (0x46D, 0x46C), + (0x46F, 0x46E), + (0x471, 0x470), + (0x473, 0x472), + (0x475, 0x474), + (0x477, 0x476), + (0x479, 0x478), + (0x47B, 0x47A), + (0x47D, 0x47C), + (0x47F, 0x47E), + (0x481, 0x480), + (0x48B, 0x48A), + (0x48D, 0x48C), + (0x48F, 0x48E), + (0x491, 0x490), + (0x493, 0x492), + (0x495, 0x494), + (0x497, 0x496), + (0x499, 0x498), + (0x49B, 0x49A), + (0x49D, 0x49C), + (0x49F, 0x49E), + (0x4A1, 0x4A0), + (0x4A3, 0x4A2), + (0x4A5, 0x4A4), + (0x4A7, 0x4A6), + (0x4A9, 0x4A8), + (0x4AB, 0x4AA), + (0x4AD, 0x4AC), + (0x4AF, 0x4AE), + (0x4B1, 0x4B0), + (0x4B3, 0x4B2), + (0x4B5, 0x4B4), + (0x4B7, 0x4B6), + (0x4B9, 0x4B8), + (0x4BB, 0x4BA), + (0x4BD, 0x4BC), + (0x4BF, 0x4BE), + (0x4C2, 0x4C1), + (0x4C4, 0x4C3), + (0x4C6, 0x4C5), + (0x4C8, 0x4C7), + (0x4CA, 0x4C9), + (0x4CC, 0x4CB), + (0x4CE, 0x4CD), + (0x4CF, 0x4C0), + (0x4D1, 0x4D0), + (0x4D3, 0x4D2), + (0x4D5, 0x4D4), + (0x4D7, 0x4D6), + (0x4D9, 0x4D8), + (0x4DB, 0x4DA), + (0x4DD, 0x4DC), + (0x4DF, 0x4DE), + (0x4E1, 0x4E0), + (0x4E3, 0x4E2), + (0x4E5, 0x4E4), + (0x4E7, 0x4E6), + (0x4E9, 0x4E8), + (0x4EB, 0x4EA), + (0x4ED, 0x4EC), + (0x4EF, 0x4EE), + (0x4F1, 0x4F0), + (0x4F3, 0x4F2), + (0x4F5, 0x4F4), + (0x4F7, 0x4F6), + (0x4F9, 0x4F8), + (0x4FB, 0x4FA), + (0x4FD, 0x4FC), + (0x4FF, 0x4FE), + (0x501, 0x500), + (0x503, 0x502), + (0x505, 0x504), + (0x507, 0x506), + (0x509, 0x508), + (0x50B, 0x50A), + (0x50D, 0x50C), + (0x50F, 0x50E), + (0x511, 0x510), + (0x513, 0x512), + (0x515, 0x514), + (0x517, 0x516), + (0x519, 0x518), + (0x51B, 0x51A), + (0x51D, 0x51C), + (0x51F, 0x51E), + (0x521, 0x520), + (0x523, 0x522), + (0x525, 0x524), + (0x527, 0x526), + (0x529, 0x528), + (0x52B, 0x52A), + (0x52D, 0x52C), + (0x52F, 0x52E), + (0x561, 0x531), + (0x562, 0x532), + (0x563, 0x533), + (0x564, 0x534), + (0x565, 0x535), + (0x566, 0x536), + (0x567, 0x537), + (0x568, 0x538), + (0x569, 0x539), + (0x56A, 0x53A), + (0x56B, 0x53B), + (0x56C, 0x53C), + (0x56D, 0x53D), + (0x56E, 0x53E), + (0x56F, 0x53F), + (0x570, 0x540), + (0x571, 0x541), + (0x572, 0x542), + (0x573, 0x543), + (0x574, 0x544), + (0x575, 0x545), + (0x576, 0x546), + (0x577, 0x547), + (0x578, 0x548), + (0x579, 0x549), + (0x57A, 0x54A), + (0x57B, 0x54B), + (0x57C, 0x54C), + (0x57D, 0x54D), + (0x57E, 0x54E), + (0x57F, 0x54F), + (0x580, 0x550), + (0x581, 0x551), + (0x582, 0x552), + (0x583, 0x553), + (0x584, 0x554), + (0x585, 0x555), + (0x586, 0x556), + (0x10D0, 0x1C90), + (0x10D1, 0x1C91), + (0x10D2, 0x1C92), + (0x10D3, 0x1C93), + (0x10D4, 0x1C94), + (0x10D5, 0x1C95), + (0x10D6, 0x1C96), + (0x10D7, 0x1C97), + (0x10D8, 0x1C98), + (0x10D9, 0x1C99), + (0x10DA, 0x1C9A), + (0x10DB, 0x1C9B), + (0x10DC, 0x1C9C), + (0x10DD, 0x1C9D), + (0x10DE, 0x1C9E), + (0x10DF, 0x1C9F), + (0x10E0, 0x1CA0), + (0x10E1, 0x1CA1), + (0x10E2, 0x1CA2), + (0x10E3, 0x1CA3), + (0x10E4, 0x1CA4), + (0x10E5, 0x1CA5), + (0x10E6, 0x1CA6), + (0x10E7, 0x1CA7), + (0x10E8, 0x1CA8), + (0x10E9, 0x1CA9), + (0x10EA, 0x1CAA), + (0x10EB, 0x1CAB), + (0x10EC, 0x1CAC), + (0x10ED, 0x1CAD), + (0x10EE, 0x1CAE), + (0x10EF, 0x1CAF), + (0x10F0, 0x1CB0), + (0x10F1, 0x1CB1), + (0x10F2, 0x1CB2), + (0x10F3, 0x1CB3), + (0x10F4, 0x1CB4), + (0x10F5, 0x1CB5), + (0x10F6, 0x1CB6), + (0x10F7, 0x1CB7), + (0x10F8, 0x1CB8), + (0x10F9, 0x1CB9), + (0x10FA, 0x1CBA), + (0x10FD, 0x1CBD), + (0x10FE, 0x1CBE), + (0x10FF, 0x1CBF), + (0x13A0, 0xAB70), + (0x13A1, 0xAB71), + (0x13A2, 0xAB72), + (0x13A3, 0xAB73), + (0x13A4, 0xAB74), + (0x13A5, 0xAB75), + (0x13A6, 0xAB76), + (0x13A7, 0xAB77), + (0x13A8, 0xAB78), + (0x13A9, 0xAB79), + (0x13AA, 0xAB7A), + (0x13AB, 0xAB7B), + (0x13AC, 0xAB7C), + (0x13AD, 0xAB7D), + (0x13AE, 0xAB7E), + (0x13AF, 0xAB7F), + (0x13B0, 0xAB80), + (0x13B1, 0xAB81), + (0x13B2, 0xAB82), + (0x13B3, 0xAB83), + (0x13B4, 0xAB84), + (0x13B5, 0xAB85), + (0x13B6, 0xAB86), + (0x13B7, 0xAB87), + (0x13B8, 0xAB88), + (0x13B9, 0xAB89), + (0x13BA, 0xAB8A), + (0x13BB, 0xAB8B), + (0x13BC, 0xAB8C), + (0x13BD, 0xAB8D), + (0x13BE, 0xAB8E), + (0x13BF, 0xAB8F), + (0x13C0, 0xAB90), + (0x13C1, 0xAB91), + (0x13C2, 0xAB92), + (0x13C3, 0xAB93), + (0x13C4, 0xAB94), + (0x13C5, 0xAB95), + (0x13C6, 0xAB96), + (0x13C7, 0xAB97), + (0x13C8, 0xAB98), + (0x13C9, 0xAB99), + (0x13CA, 0xAB9A), + (0x13CB, 0xAB9B), + (0x13CC, 0xAB9C), + (0x13CD, 0xAB9D), + (0x13CE, 0xAB9E), + (0x13CF, 0xAB9F), + (0x13D0, 0xABA0), + (0x13D1, 0xABA1), + (0x13D2, 0xABA2), + (0x13D3, 0xABA3), + (0x13D4, 0xABA4), + (0x13D5, 0xABA5), + (0x13D6, 0xABA6), + (0x13D7, 0xABA7), + (0x13D8, 0xABA8), + (0x13D9, 0xABA9), + (0x13DA, 0xABAA), + (0x13DB, 0xABAB), + (0x13DC, 0xABAC), + (0x13DD, 0xABAD), + (0x13DE, 0xABAE), + (0x13DF, 0xABAF), + (0x13E0, 0xABB0), + (0x13E1, 0xABB1), + (0x13E2, 0xABB2), + (0x13E3, 0xABB3), + (0x13E4, 0xABB4), + (0x13E5, 0xABB5), + (0x13E6, 0xABB6), + (0x13E7, 0xABB7), + (0x13E8, 0xABB8), + (0x13E9, 0xABB9), + (0x13EA, 0xABBA), + (0x13EB, 0xABBB), + (0x13EC, 0xABBC), + (0x13ED, 0xABBD), + (0x13EE, 0xABBE), + (0x13EF, 0xABBF), + (0x13F0, 0x13F8), + (0x13F1, 0x13F9), + (0x13F2, 0x13FA), + (0x13F3, 0x13FB), + (0x13F4, 0x13FC), + (0x13F5, 0x13FD), + (0x1C8A, 0x1C89), + (0x1D79, 0xA77D), + (0x1D7D, 0x2C63), + (0x1D8E, 0xA7C6), + (0x1E01, 0x1E00), + (0x1E03, 0x1E02), + (0x1E05, 0x1E04), + (0x1E07, 0x1E06), + (0x1E09, 0x1E08), + (0x1E0B, 0x1E0A), + (0x1E0D, 0x1E0C), + (0x1E0F, 0x1E0E), + (0x1E11, 0x1E10), + (0x1E13, 0x1E12), + (0x1E15, 0x1E14), + (0x1E17, 0x1E16), + (0x1E19, 0x1E18), + (0x1E1B, 0x1E1A), + (0x1E1D, 0x1E1C), + (0x1E1F, 0x1E1E), + (0x1E21, 0x1E20), + (0x1E23, 0x1E22), + (0x1E25, 0x1E24), + (0x1E27, 0x1E26), + (0x1E29, 0x1E28), + (0x1E2B, 0x1E2A), + (0x1E2D, 0x1E2C), + (0x1E2F, 0x1E2E), + (0x1E31, 0x1E30), + (0x1E33, 0x1E32), + (0x1E35, 0x1E34), + (0x1E37, 0x1E36), + (0x1E39, 0x1E38), + (0x1E3B, 0x1E3A), + (0x1E3D, 0x1E3C), + (0x1E3F, 0x1E3E), + (0x1E41, 0x1E40), + (0x1E43, 0x1E42), + (0x1E45, 0x1E44), + (0x1E47, 0x1E46), + (0x1E49, 0x1E48), + (0x1E4B, 0x1E4A), + (0x1E4D, 0x1E4C), + (0x1E4F, 0x1E4E), + (0x1E51, 0x1E50), + (0x1E53, 0x1E52), + (0x1E55, 0x1E54), + (0x1E57, 0x1E56), + (0x1E59, 0x1E58), + (0x1E5B, 0x1E5A), + (0x1E5D, 0x1E5C), + (0x1E5F, 0x1E5E), + (0x1E61, 0x1E60), + (0x1E61, 0x1E9B), + (0x1E63, 0x1E62), + (0x1E65, 0x1E64), + (0x1E67, 0x1E66), + (0x1E69, 0x1E68), + (0x1E6B, 0x1E6A), + (0x1E6D, 0x1E6C), + (0x1E6F, 0x1E6E), + (0x1E71, 0x1E70), + (0x1E73, 0x1E72), + (0x1E75, 0x1E74), + (0x1E77, 0x1E76), + (0x1E79, 0x1E78), + (0x1E7B, 0x1E7A), + (0x1E7D, 0x1E7C), + (0x1E7F, 0x1E7E), + (0x1E81, 0x1E80), + (0x1E83, 0x1E82), + (0x1E85, 0x1E84), + (0x1E87, 0x1E86), + (0x1E89, 0x1E88), + (0x1E8B, 0x1E8A), + (0x1E8D, 0x1E8C), + (0x1E8F, 0x1E8E), + (0x1E91, 0x1E90), + (0x1E93, 0x1E92), + (0x1E95, 0x1E94), + (0x1EA1, 0x1EA0), + (0x1EA3, 0x1EA2), + (0x1EA5, 0x1EA4), + (0x1EA7, 0x1EA6), + (0x1EA9, 0x1EA8), + (0x1EAB, 0x1EAA), + (0x1EAD, 0x1EAC), + (0x1EAF, 0x1EAE), + (0x1EB1, 0x1EB0), + (0x1EB3, 0x1EB2), + (0x1EB5, 0x1EB4), + (0x1EB7, 0x1EB6), + (0x1EB9, 0x1EB8), + (0x1EBB, 0x1EBA), + (0x1EBD, 0x1EBC), + (0x1EBF, 0x1EBE), + (0x1EC1, 0x1EC0), + (0x1EC3, 0x1EC2), + (0x1EC5, 0x1EC4), + (0x1EC7, 0x1EC6), + (0x1EC9, 0x1EC8), + (0x1ECB, 0x1ECA), + (0x1ECD, 0x1ECC), + (0x1ECF, 0x1ECE), + (0x1ED1, 0x1ED0), + (0x1ED3, 0x1ED2), + (0x1ED5, 0x1ED4), + (0x1ED7, 0x1ED6), + (0x1ED9, 0x1ED8), + (0x1EDB, 0x1EDA), + (0x1EDD, 0x1EDC), + (0x1EDF, 0x1EDE), + (0x1EE1, 0x1EE0), + (0x1EE3, 0x1EE2), + (0x1EE5, 0x1EE4), + (0x1EE7, 0x1EE6), + (0x1EE9, 0x1EE8), + (0x1EEB, 0x1EEA), + (0x1EED, 0x1EEC), + (0x1EEF, 0x1EEE), + (0x1EF1, 0x1EF0), + (0x1EF3, 0x1EF2), + (0x1EF5, 0x1EF4), + (0x1EF7, 0x1EF6), + (0x1EF9, 0x1EF8), + (0x1EFB, 0x1EFA), + (0x1EFD, 0x1EFC), + (0x1EFF, 0x1EFE), + (0x1F00, 0x1F08), + (0x1F01, 0x1F09), + (0x1F02, 0x1F0A), + (0x1F03, 0x1F0B), + (0x1F04, 0x1F0C), + (0x1F05, 0x1F0D), + (0x1F06, 0x1F0E), + (0x1F07, 0x1F0F), + (0x1F10, 0x1F18), + (0x1F11, 0x1F19), + (0x1F12, 0x1F1A), + (0x1F13, 0x1F1B), + (0x1F14, 0x1F1C), + (0x1F15, 0x1F1D), + (0x1F20, 0x1F28), + (0x1F21, 0x1F29), + (0x1F22, 0x1F2A), + (0x1F23, 0x1F2B), + (0x1F24, 0x1F2C), + (0x1F25, 0x1F2D), + (0x1F26, 0x1F2E), + (0x1F27, 0x1F2F), + (0x1F30, 0x1F38), + (0x1F31, 0x1F39), + (0x1F32, 0x1F3A), + (0x1F33, 0x1F3B), + (0x1F34, 0x1F3C), + (0x1F35, 0x1F3D), + (0x1F36, 0x1F3E), + (0x1F37, 0x1F3F), + (0x1F40, 0x1F48), + (0x1F41, 0x1F49), + (0x1F42, 0x1F4A), + (0x1F43, 0x1F4B), + (0x1F44, 0x1F4C), + (0x1F45, 0x1F4D), + (0x1F51, 0x1F59), + (0x1F53, 0x1F5B), + (0x1F55, 0x1F5D), + (0x1F57, 0x1F5F), + (0x1F60, 0x1F68), + (0x1F61, 0x1F69), + (0x1F62, 0x1F6A), + (0x1F63, 0x1F6B), + (0x1F64, 0x1F6C), + (0x1F65, 0x1F6D), + (0x1F66, 0x1F6E), + (0x1F67, 0x1F6F), + (0x1F70, 0x1FBA), + (0x1F71, 0x1FBB), + (0x1F72, 0x1FC8), + (0x1F73, 0x1FC9), + (0x1F74, 0x1FCA), + (0x1F75, 0x1FCB), + (0x1F76, 0x1FDA), + (0x1F77, 0x1FDB), + (0x1F78, 0x1FF8), + (0x1F79, 0x1FF9), + (0x1F7A, 0x1FEA), + (0x1F7B, 0x1FEB), + (0x1F7C, 0x1FFA), + (0x1F7D, 0x1FFB), + (0x1F80, 0x1F88), + (0x1F81, 0x1F89), + (0x1F82, 0x1F8A), + (0x1F83, 0x1F8B), + (0x1F84, 0x1F8C), + (0x1F85, 0x1F8D), + (0x1F86, 0x1F8E), + (0x1F87, 0x1F8F), + (0x1F90, 0x1F98), + (0x1F91, 0x1F99), + (0x1F92, 0x1F9A), + (0x1F93, 0x1F9B), + (0x1F94, 0x1F9C), + (0x1F95, 0x1F9D), + (0x1F96, 0x1F9E), + (0x1F97, 0x1F9F), + (0x1FA0, 0x1FA8), + (0x1FA1, 0x1FA9), + (0x1FA2, 0x1FAA), + (0x1FA3, 0x1FAB), + (0x1FA4, 0x1FAC), + (0x1FA5, 0x1FAD), + (0x1FA6, 0x1FAE), + (0x1FA7, 0x1FAF), + (0x1FB0, 0x1FB8), + (0x1FB1, 0x1FB9), + (0x1FB3, 0x1FBC), + (0x1FC3, 0x1FCC), + (0x1FD0, 0x1FD8), + (0x1FD1, 0x1FD9), + (0x1FE0, 0x1FE8), + (0x1FE1, 0x1FE9), + (0x1FE5, 0x1FEC), + (0x1FF3, 0x1FFC), + (0x214E, 0x2132), + (0x2170, 0x2160), + (0x2171, 0x2161), + (0x2172, 0x2162), + (0x2173, 0x2163), + (0x2174, 0x2164), + (0x2175, 0x2165), + (0x2176, 0x2166), + (0x2177, 0x2167), + (0x2178, 0x2168), + (0x2179, 0x2169), + (0x217A, 0x216A), + (0x217B, 0x216B), + (0x217C, 0x216C), + (0x217D, 0x216D), + (0x217E, 0x216E), + (0x217F, 0x216F), + (0x2184, 0x2183), + (0x24D0, 0x24B6), + (0x24D1, 0x24B7), + (0x24D2, 0x24B8), + (0x24D3, 0x24B9), + (0x24D4, 0x24BA), + (0x24D5, 0x24BB), + (0x24D6, 0x24BC), + (0x24D7, 0x24BD), + (0x24D8, 0x24BE), + (0x24D9, 0x24BF), + (0x24DA, 0x24C0), + (0x24DB, 0x24C1), + (0x24DC, 0x24C2), + (0x24DD, 0x24C3), + (0x24DE, 0x24C4), + (0x24DF, 0x24C5), + (0x24E0, 0x24C6), + (0x24E1, 0x24C7), + (0x24E2, 0x24C8), + (0x24E3, 0x24C9), + (0x24E4, 0x24CA), + (0x24E5, 0x24CB), + (0x24E6, 0x24CC), + (0x24E7, 0x24CD), + (0x24E8, 0x24CE), + (0x24E9, 0x24CF), + (0x2C30, 0x2C00), + (0x2C31, 0x2C01), + (0x2C32, 0x2C02), + (0x2C33, 0x2C03), + (0x2C34, 0x2C04), + (0x2C35, 0x2C05), + (0x2C36, 0x2C06), + (0x2C37, 0x2C07), + (0x2C38, 0x2C08), + (0x2C39, 0x2C09), + (0x2C3A, 0x2C0A), + (0x2C3B, 0x2C0B), + (0x2C3C, 0x2C0C), + (0x2C3D, 0x2C0D), + (0x2C3E, 0x2C0E), + (0x2C3F, 0x2C0F), + (0x2C40, 0x2C10), + (0x2C41, 0x2C11), + (0x2C42, 0x2C12), + (0x2C43, 0x2C13), + (0x2C44, 0x2C14), + (0x2C45, 0x2C15), + (0x2C46, 0x2C16), + (0x2C47, 0x2C17), + (0x2C48, 0x2C18), + (0x2C49, 0x2C19), + (0x2C4A, 0x2C1A), + (0x2C4B, 0x2C1B), + (0x2C4C, 0x2C1C), + (0x2C4D, 0x2C1D), + (0x2C4E, 0x2C1E), + (0x2C4F, 0x2C1F), + (0x2C50, 0x2C20), + (0x2C51, 0x2C21), + (0x2C52, 0x2C22), + (0x2C53, 0x2C23), + (0x2C54, 0x2C24), + (0x2C55, 0x2C25), + (0x2C56, 0x2C26), + (0x2C57, 0x2C27), + (0x2C58, 0x2C28), + (0x2C59, 0x2C29), + (0x2C5A, 0x2C2A), + (0x2C5B, 0x2C2B), + (0x2C5C, 0x2C2C), + (0x2C5D, 0x2C2D), + (0x2C5E, 0x2C2E), + (0x2C5F, 0x2C2F), + (0x2C61, 0x2C60), + (0x2C65, 0x23A), + (0x2C66, 0x23E), + (0x2C68, 0x2C67), + (0x2C6A, 0x2C69), + (0x2C6C, 0x2C6B), + (0x2C73, 0x2C72), + (0x2C76, 0x2C75), + (0x2C81, 0x2C80), + (0x2C83, 0x2C82), + (0x2C85, 0x2C84), + (0x2C87, 0x2C86), + (0x2C89, 0x2C88), + (0x2C8B, 0x2C8A), + (0x2C8D, 0x2C8C), + (0x2C8F, 0x2C8E), + (0x2C91, 0x2C90), + (0x2C93, 0x2C92), + (0x2C95, 0x2C94), + (0x2C97, 0x2C96), + (0x2C99, 0x2C98), + (0x2C9B, 0x2C9A), + (0x2C9D, 0x2C9C), + (0x2C9F, 0x2C9E), + (0x2CA1, 0x2CA0), + (0x2CA3, 0x2CA2), + (0x2CA5, 0x2CA4), + (0x2CA7, 0x2CA6), + (0x2CA9, 0x2CA8), + (0x2CAB, 0x2CAA), + (0x2CAD, 0x2CAC), + (0x2CAF, 0x2CAE), + (0x2CB1, 0x2CB0), + (0x2CB3, 0x2CB2), + (0x2CB5, 0x2CB4), + (0x2CB7, 0x2CB6), + (0x2CB9, 0x2CB8), + (0x2CBB, 0x2CBA), + (0x2CBD, 0x2CBC), + (0x2CBF, 0x2CBE), + (0x2CC1, 0x2CC0), + (0x2CC3, 0x2CC2), + (0x2CC5, 0x2CC4), + (0x2CC7, 0x2CC6), + (0x2CC9, 0x2CC8), + (0x2CCB, 0x2CCA), + (0x2CCD, 0x2CCC), + (0x2CCF, 0x2CCE), + (0x2CD1, 0x2CD0), + (0x2CD3, 0x2CD2), + (0x2CD5, 0x2CD4), + (0x2CD7, 0x2CD6), + (0x2CD9, 0x2CD8), + (0x2CDB, 0x2CDA), + (0x2CDD, 0x2CDC), + (0x2CDF, 0x2CDE), + (0x2CE1, 0x2CE0), + (0x2CE3, 0x2CE2), + (0x2CEC, 0x2CEB), + (0x2CEE, 0x2CED), + (0x2CF3, 0x2CF2), + (0x2D00, 0x10A0), + (0x2D01, 0x10A1), + (0x2D02, 0x10A2), + (0x2D03, 0x10A3), + (0x2D04, 0x10A4), + (0x2D05, 0x10A5), + (0x2D06, 0x10A6), + (0x2D07, 0x10A7), + (0x2D08, 0x10A8), + (0x2D09, 0x10A9), + (0x2D0A, 0x10AA), + (0x2D0B, 0x10AB), + (0x2D0C, 0x10AC), + (0x2D0D, 0x10AD), + (0x2D0E, 0x10AE), + (0x2D0F, 0x10AF), + (0x2D10, 0x10B0), + (0x2D11, 0x10B1), + (0x2D12, 0x10B2), + (0x2D13, 0x10B3), + (0x2D14, 0x10B4), + (0x2D15, 0x10B5), + (0x2D16, 0x10B6), + (0x2D17, 0x10B7), + (0x2D18, 0x10B8), + (0x2D19, 0x10B9), + (0x2D1A, 0x10BA), + (0x2D1B, 0x10BB), + (0x2D1C, 0x10BC), + (0x2D1D, 0x10BD), + (0x2D1E, 0x10BE), + (0x2D1F, 0x10BF), + (0x2D20, 0x10C0), + (0x2D21, 0x10C1), + (0x2D22, 0x10C2), + (0x2D23, 0x10C3), + (0x2D24, 0x10C4), + (0x2D25, 0x10C5), + (0x2D27, 0x10C7), + (0x2D2D, 0x10CD), + (0xA641, 0xA640), + (0xA643, 0xA642), + (0xA645, 0xA644), + (0xA647, 0xA646), + (0xA649, 0xA648), + (0xA64B, 0x1C88), + (0xA64B, 0xA64A), + (0xA64D, 0xA64C), + (0xA64F, 0xA64E), + (0xA651, 0xA650), + (0xA653, 0xA652), + (0xA655, 0xA654), + (0xA657, 0xA656), + (0xA659, 0xA658), + (0xA65B, 0xA65A), + (0xA65D, 0xA65C), + (0xA65F, 0xA65E), + (0xA661, 0xA660), + (0xA663, 0xA662), + (0xA665, 0xA664), + (0xA667, 0xA666), + (0xA669, 0xA668), + (0xA66B, 0xA66A), + (0xA66D, 0xA66C), + (0xA681, 0xA680), + (0xA683, 0xA682), + (0xA685, 0xA684), + (0xA687, 0xA686), + (0xA689, 0xA688), + (0xA68B, 0xA68A), + (0xA68D, 0xA68C), + (0xA68F, 0xA68E), + (0xA691, 0xA690), + (0xA693, 0xA692), + (0xA695, 0xA694), + (0xA697, 0xA696), + (0xA699, 0xA698), + (0xA69B, 0xA69A), + (0xA723, 0xA722), + (0xA725, 0xA724), + (0xA727, 0xA726), + (0xA729, 0xA728), + (0xA72B, 0xA72A), + (0xA72D, 0xA72C), + (0xA72F, 0xA72E), + (0xA733, 0xA732), + (0xA735, 0xA734), + (0xA737, 0xA736), + (0xA739, 0xA738), + (0xA73B, 0xA73A), + (0xA73D, 0xA73C), + (0xA73F, 0xA73E), + (0xA741, 0xA740), + (0xA743, 0xA742), + (0xA745, 0xA744), + (0xA747, 0xA746), + (0xA749, 0xA748), + (0xA74B, 0xA74A), + (0xA74D, 0xA74C), + (0xA74F, 0xA74E), + (0xA751, 0xA750), + (0xA753, 0xA752), + (0xA755, 0xA754), + (0xA757, 0xA756), + (0xA759, 0xA758), + (0xA75B, 0xA75A), + (0xA75D, 0xA75C), + (0xA75F, 0xA75E), + (0xA761, 0xA760), + (0xA763, 0xA762), + (0xA765, 0xA764), + (0xA767, 0xA766), + (0xA769, 0xA768), + (0xA76B, 0xA76A), + (0xA76D, 0xA76C), + (0xA76F, 0xA76E), + (0xA77A, 0xA779), + (0xA77C, 0xA77B), + (0xA77F, 0xA77E), + (0xA781, 0xA780), + (0xA783, 0xA782), + (0xA785, 0xA784), + (0xA787, 0xA786), + (0xA78C, 0xA78B), + (0xA791, 0xA790), + (0xA793, 0xA792), + (0xA794, 0xA7C4), + (0xA797, 0xA796), + (0xA799, 0xA798), + (0xA79B, 0xA79A), + (0xA79D, 0xA79C), + (0xA79F, 0xA79E), + (0xA7A1, 0xA7A0), + (0xA7A3, 0xA7A2), + (0xA7A5, 0xA7A4), + (0xA7A7, 0xA7A6), + (0xA7A9, 0xA7A8), + (0xA7B5, 0xA7B4), + (0xA7B7, 0xA7B6), + (0xA7B9, 0xA7B8), + (0xA7BB, 0xA7BA), + (0xA7BD, 0xA7BC), + (0xA7BF, 0xA7BE), + (0xA7C1, 0xA7C0), + (0xA7C3, 0xA7C2), + (0xA7C8, 0xA7C7), + (0xA7CA, 0xA7C9), + (0xA7CD, 0xA7CC), + (0xA7CF, 0xA7CE), + (0xA7D1, 0xA7D0), + (0xA7D3, 0xA7D2), + (0xA7D5, 0xA7D4), + (0xA7D7, 0xA7D6), + (0xA7D9, 0xA7D8), + (0xA7DB, 0xA7DA), + (0xA7F6, 0xA7F5), + (0xAB4B, 0xAB6C), + (0xAB4C, 0xAB6D), + (0xAB53, 0xA7B3), + (0xFB06, 0xFB05), + (0xFF41, 0xFF21), + (0xFF42, 0xFF22), + (0xFF43, 0xFF23), + (0xFF44, 0xFF24), + (0xFF45, 0xFF25), + (0xFF46, 0xFF26), + (0xFF47, 0xFF27), + (0xFF48, 0xFF28), + (0xFF49, 0xFF29), + (0xFF4A, 0xFF2A), + (0xFF4B, 0xFF2B), + (0xFF4C, 0xFF2C), + (0xFF4D, 0xFF2D), + (0xFF4E, 0xFF2E), + (0xFF4F, 0xFF2F), + (0xFF50, 0xFF30), + (0xFF51, 0xFF31), + (0xFF52, 0xFF32), + (0xFF53, 0xFF33), + (0xFF54, 0xFF34), + (0xFF55, 0xFF35), + (0xFF56, 0xFF36), + (0xFF57, 0xFF37), + (0xFF58, 0xFF38), + (0xFF59, 0xFF39), + (0xFF5A, 0xFF3A), + (0x10428, 0x10400), + (0x10429, 0x10401), + (0x1042A, 0x10402), + (0x1042B, 0x10403), + (0x1042C, 0x10404), + (0x1042D, 0x10405), + (0x1042E, 0x10406), + (0x1042F, 0x10407), + (0x10430, 0x10408), + (0x10431, 0x10409), + (0x10432, 0x1040A), + (0x10433, 0x1040B), + (0x10434, 0x1040C), + (0x10435, 0x1040D), + (0x10436, 0x1040E), + (0x10437, 0x1040F), + (0x10438, 0x10410), + (0x10439, 0x10411), + (0x1043A, 0x10412), + (0x1043B, 0x10413), + (0x1043C, 0x10414), + (0x1043D, 0x10415), + (0x1043E, 0x10416), + (0x1043F, 0x10417), + (0x10440, 0x10418), + (0x10441, 0x10419), + (0x10442, 0x1041A), + (0x10443, 0x1041B), + (0x10444, 0x1041C), + (0x10445, 0x1041D), + (0x10446, 0x1041E), + (0x10447, 0x1041F), + (0x10448, 0x10420), + (0x10449, 0x10421), + (0x1044A, 0x10422), + (0x1044B, 0x10423), + (0x1044C, 0x10424), + (0x1044D, 0x10425), + (0x1044E, 0x10426), + (0x1044F, 0x10427), + (0x104D8, 0x104B0), + (0x104D9, 0x104B1), + (0x104DA, 0x104B2), + (0x104DB, 0x104B3), + (0x104DC, 0x104B4), + (0x104DD, 0x104B5), + (0x104DE, 0x104B6), + (0x104DF, 0x104B7), + (0x104E0, 0x104B8), + (0x104E1, 0x104B9), + (0x104E2, 0x104BA), + (0x104E3, 0x104BB), + (0x104E4, 0x104BC), + (0x104E5, 0x104BD), + (0x104E6, 0x104BE), + (0x104E7, 0x104BF), + (0x104E8, 0x104C0), + (0x104E9, 0x104C1), + (0x104EA, 0x104C2), + (0x104EB, 0x104C3), + (0x104EC, 0x104C4), + (0x104ED, 0x104C5), + (0x104EE, 0x104C6), + (0x104EF, 0x104C7), + (0x104F0, 0x104C8), + (0x104F1, 0x104C9), + (0x104F2, 0x104CA), + (0x104F3, 0x104CB), + (0x104F4, 0x104CC), + (0x104F5, 0x104CD), + (0x104F6, 0x104CE), + (0x104F7, 0x104CF), + (0x104F8, 0x104D0), + (0x104F9, 0x104D1), + (0x104FA, 0x104D2), + (0x104FB, 0x104D3), + (0x10597, 0x10570), + (0x10598, 0x10571), + (0x10599, 0x10572), + (0x1059A, 0x10573), + (0x1059B, 0x10574), + (0x1059C, 0x10575), + (0x1059D, 0x10576), + (0x1059E, 0x10577), + (0x1059F, 0x10578), + (0x105A0, 0x10579), + (0x105A1, 0x1057A), + (0x105A3, 0x1057C), + (0x105A4, 0x1057D), + (0x105A5, 0x1057E), + (0x105A6, 0x1057F), + (0x105A7, 0x10580), + (0x105A8, 0x10581), + (0x105A9, 0x10582), + (0x105AA, 0x10583), + (0x105AB, 0x10584), + (0x105AC, 0x10585), + (0x105AD, 0x10586), + (0x105AE, 0x10587), + (0x105AF, 0x10588), + (0x105B0, 0x10589), + (0x105B1, 0x1058A), + (0x105B3, 0x1058C), + (0x105B4, 0x1058D), + (0x105B5, 0x1058E), + (0x105B6, 0x1058F), + (0x105B7, 0x10590), + (0x105B8, 0x10591), + (0x105B9, 0x10592), + (0x105BB, 0x10594), + (0x105BC, 0x10595), + (0x10CC0, 0x10C80), + (0x10CC1, 0x10C81), + (0x10CC2, 0x10C82), + (0x10CC3, 0x10C83), + (0x10CC4, 0x10C84), + (0x10CC5, 0x10C85), + (0x10CC6, 0x10C86), + (0x10CC7, 0x10C87), + (0x10CC8, 0x10C88), + (0x10CC9, 0x10C89), + (0x10CCA, 0x10C8A), + (0x10CCB, 0x10C8B), + (0x10CCC, 0x10C8C), + (0x10CCD, 0x10C8D), + (0x10CCE, 0x10C8E), + (0x10CCF, 0x10C8F), + (0x10CD0, 0x10C90), + (0x10CD1, 0x10C91), + (0x10CD2, 0x10C92), + (0x10CD3, 0x10C93), + (0x10CD4, 0x10C94), + (0x10CD5, 0x10C95), + (0x10CD6, 0x10C96), + (0x10CD7, 0x10C97), + (0x10CD8, 0x10C98), + (0x10CD9, 0x10C99), + (0x10CDA, 0x10C9A), + (0x10CDB, 0x10C9B), + (0x10CDC, 0x10C9C), + (0x10CDD, 0x10C9D), + (0x10CDE, 0x10C9E), + (0x10CDF, 0x10C9F), + (0x10CE0, 0x10CA0), + (0x10CE1, 0x10CA1), + (0x10CE2, 0x10CA2), + (0x10CE3, 0x10CA3), + (0x10CE4, 0x10CA4), + (0x10CE5, 0x10CA5), + (0x10CE6, 0x10CA6), + (0x10CE7, 0x10CA7), + (0x10CE8, 0x10CA8), + (0x10CE9, 0x10CA9), + (0x10CEA, 0x10CAA), + (0x10CEB, 0x10CAB), + (0x10CEC, 0x10CAC), + (0x10CED, 0x10CAD), + (0x10CEE, 0x10CAE), + (0x10CEF, 0x10CAF), + (0x10CF0, 0x10CB0), + (0x10CF1, 0x10CB1), + (0x10CF2, 0x10CB2), + (0x10D70, 0x10D50), + (0x10D71, 0x10D51), + (0x10D72, 0x10D52), + (0x10D73, 0x10D53), + (0x10D74, 0x10D54), + (0x10D75, 0x10D55), + (0x10D76, 0x10D56), + (0x10D77, 0x10D57), + (0x10D78, 0x10D58), + (0x10D79, 0x10D59), + (0x10D7A, 0x10D5A), + (0x10D7B, 0x10D5B), + (0x10D7C, 0x10D5C), + (0x10D7D, 0x10D5D), + (0x10D7E, 0x10D5E), + (0x10D7F, 0x10D5F), + (0x10D80, 0x10D60), + (0x10D81, 0x10D61), + (0x10D82, 0x10D62), + (0x10D83, 0x10D63), + (0x10D84, 0x10D64), + (0x10D85, 0x10D65), + (0x118C0, 0x118A0), + (0x118C1, 0x118A1), + (0x118C2, 0x118A2), + (0x118C3, 0x118A3), + (0x118C4, 0x118A4), + (0x118C5, 0x118A5), + (0x118C6, 0x118A6), + (0x118C7, 0x118A7), + (0x118C8, 0x118A8), + (0x118C9, 0x118A9), + (0x118CA, 0x118AA), + (0x118CB, 0x118AB), + (0x118CC, 0x118AC), + (0x118CD, 0x118AD), + (0x118CE, 0x118AE), + (0x118CF, 0x118AF), + (0x118D0, 0x118B0), + (0x118D1, 0x118B1), + (0x118D2, 0x118B2), + (0x118D3, 0x118B3), + (0x118D4, 0x118B4), + (0x118D5, 0x118B5), + (0x118D6, 0x118B6), + (0x118D7, 0x118B7), + (0x118D8, 0x118B8), + (0x118D9, 0x118B9), + (0x118DA, 0x118BA), + (0x118DB, 0x118BB), + (0x118DC, 0x118BC), + (0x118DD, 0x118BD), + (0x118DE, 0x118BE), + (0x118DF, 0x118BF), + (0x16E60, 0x16E40), + (0x16E61, 0x16E41), + (0x16E62, 0x16E42), + (0x16E63, 0x16E43), + (0x16E64, 0x16E44), + (0x16E65, 0x16E45), + (0x16E66, 0x16E46), + (0x16E67, 0x16E47), + (0x16E68, 0x16E48), + (0x16E69, 0x16E49), + (0x16E6A, 0x16E4A), + (0x16E6B, 0x16E4B), + (0x16E6C, 0x16E4C), + (0x16E6D, 0x16E4D), + (0x16E6E, 0x16E4E), + (0x16E6F, 0x16E4F), + (0x16E70, 0x16E50), + (0x16E71, 0x16E51), + (0x16E72, 0x16E52), + (0x16E73, 0x16E53), + (0x16E74, 0x16E54), + (0x16E75, 0x16E55), + (0x16E76, 0x16E56), + (0x16E77, 0x16E57), + (0x16E78, 0x16E58), + (0x16E79, 0x16E59), + (0x16E7A, 0x16E5A), + (0x16E7B, 0x16E5B), + (0x16E7C, 0x16E5C), + (0x16E7D, 0x16E5D), + (0x16E7E, 0x16E5E), + (0x16E7F, 0x16E5F), + (0x16EBB, 0x16EA0), + (0x16EBC, 0x16EA1), + (0x16EBD, 0x16EA2), + (0x16EBE, 0x16EA3), + (0x16EBF, 0x16EA4), + (0x16EC0, 0x16EA5), + (0x16EC1, 0x16EA6), + (0x16EC2, 0x16EA7), + (0x16EC3, 0x16EA8), + (0x16EC4, 0x16EA9), + (0x16EC5, 0x16EAA), + (0x16EC6, 0x16EAB), + (0x16EC7, 0x16EAC), + (0x16EC8, 0x16EAD), + (0x16EC9, 0x16EAE), + (0x16ECA, 0x16EAF), + (0x16ECB, 0x16EB0), + (0x16ECC, 0x16EB1), + (0x16ECD, 0x16EB2), + (0x16ECE, 0x16EB3), + (0x16ECF, 0x16EB4), + (0x16ED0, 0x16EB5), + (0x16ED1, 0x16EB6), + (0x16ED2, 0x16EB7), + (0x16ED3, 0x16EB8), + (0x1DF41, 0x1DF40), + (0x1DF49, 0x1DF48), + (0x1DF4B, 0x1DF4A), + (0x1DF4E, 0x1DF4D), + (0x1DF52, 0x1DF51), + (0x1DF69, 0x1DF68), + (0x1DF6B, 0x1DF6A), + (0x1DF6D, 0x1DF6C), + (0x1DF6F, 0x1DF6E), + (0x1DF73, 0x1DF72), + (0x1DF75, 0x1DF74), + (0x1DF77, 0x1DF76), + (0x1DF79, 0x1DF78), + (0x1DF7B, 0x1DF7A), + (0x1DF7D, 0x1DF7C), + (0x1DF7F, 0x1DF7E), + (0x1E922, 0x1E900), + (0x1E923, 0x1E901), + (0x1E924, 0x1E902), + (0x1E925, 0x1E903), + (0x1E926, 0x1E904), + (0x1E927, 0x1E905), + (0x1E928, 0x1E906), + (0x1E929, 0x1E907), + (0x1E92A, 0x1E908), + (0x1E92B, 0x1E909), + (0x1E92C, 0x1E90A), + (0x1E92D, 0x1E90B), + (0x1E92E, 0x1E90C), + (0x1E92F, 0x1E90D), + (0x1E930, 0x1E90E), + (0x1E931, 0x1E90F), + (0x1E932, 0x1E910), + (0x1E933, 0x1E911), + (0x1E934, 0x1E912), + (0x1E935, 0x1E913), + (0x1E936, 0x1E914), + (0x1E937, 0x1E915), + (0x1E938, 0x1E916), + (0x1E939, 0x1E917), + (0x1E93A, 0x1E918), + (0x1E93B, 0x1E919), + (0x1E93C, 0x1E91A), + (0x1E93D, 0x1E91B), + (0x1E93E, 0x1E91C), + (0x1E93F, 0x1E91D), + (0x1E940, 0x1E91E), + (0x1E941, 0x1E91F), + (0x1E942, 0x1E920), + (0x1E943, 0x1E921), +]; + +/// toUppercase of a BMP code point where it is one different BMP code point, and not ASCII for a non-ASCII argument: code point, upper case, sorted by code point. +pub static UPPER: &[(u32, u32)] = &[ + (0x61, 0x41), + (0x62, 0x42), + (0x63, 0x43), + (0x64, 0x44), + (0x65, 0x45), + (0x66, 0x46), + (0x67, 0x47), + (0x68, 0x48), + (0x69, 0x49), + (0x6A, 0x4A), + (0x6B, 0x4B), + (0x6C, 0x4C), + (0x6D, 0x4D), + (0x6E, 0x4E), + (0x6F, 0x4F), + (0x70, 0x50), + (0x71, 0x51), + (0x72, 0x52), + (0x73, 0x53), + (0x74, 0x54), + (0x75, 0x55), + (0x76, 0x56), + (0x77, 0x57), + (0x78, 0x58), + (0x79, 0x59), + (0x7A, 0x5A), + (0xB5, 0x39C), + (0xE0, 0xC0), + (0xE1, 0xC1), + (0xE2, 0xC2), + (0xE3, 0xC3), + (0xE4, 0xC4), + (0xE5, 0xC5), + (0xE6, 0xC6), + (0xE7, 0xC7), + (0xE8, 0xC8), + (0xE9, 0xC9), + (0xEA, 0xCA), + (0xEB, 0xCB), + (0xEC, 0xCC), + (0xED, 0xCD), + (0xEE, 0xCE), + (0xEF, 0xCF), + (0xF0, 0xD0), + (0xF1, 0xD1), + (0xF2, 0xD2), + (0xF3, 0xD3), + (0xF4, 0xD4), + (0xF5, 0xD5), + (0xF6, 0xD6), + (0xF8, 0xD8), + (0xF9, 0xD9), + (0xFA, 0xDA), + (0xFB, 0xDB), + (0xFC, 0xDC), + (0xFD, 0xDD), + (0xFE, 0xDE), + (0xFF, 0x178), + (0x101, 0x100), + (0x103, 0x102), + (0x105, 0x104), + (0x107, 0x106), + (0x109, 0x108), + (0x10B, 0x10A), + (0x10D, 0x10C), + (0x10F, 0x10E), + (0x111, 0x110), + (0x113, 0x112), + (0x115, 0x114), + (0x117, 0x116), + (0x119, 0x118), + (0x11B, 0x11A), + (0x11D, 0x11C), + (0x11F, 0x11E), + (0x121, 0x120), + (0x123, 0x122), + (0x125, 0x124), + (0x127, 0x126), + (0x129, 0x128), + (0x12B, 0x12A), + (0x12D, 0x12C), + (0x12F, 0x12E), + (0x133, 0x132), + (0x135, 0x134), + (0x137, 0x136), + (0x13A, 0x139), + (0x13C, 0x13B), + (0x13E, 0x13D), + (0x140, 0x13F), + (0x142, 0x141), + (0x144, 0x143), + (0x146, 0x145), + (0x148, 0x147), + (0x14B, 0x14A), + (0x14D, 0x14C), + (0x14F, 0x14E), + (0x151, 0x150), + (0x153, 0x152), + (0x155, 0x154), + (0x157, 0x156), + (0x159, 0x158), + (0x15B, 0x15A), + (0x15D, 0x15C), + (0x15F, 0x15E), + (0x161, 0x160), + (0x163, 0x162), + (0x165, 0x164), + (0x167, 0x166), + (0x169, 0x168), + (0x16B, 0x16A), + (0x16D, 0x16C), + (0x16F, 0x16E), + (0x171, 0x170), + (0x173, 0x172), + (0x175, 0x174), + (0x177, 0x176), + (0x17A, 0x179), + (0x17C, 0x17B), + (0x17E, 0x17D), + (0x180, 0x243), + (0x183, 0x182), + (0x185, 0x184), + (0x188, 0x187), + (0x18C, 0x18B), + (0x192, 0x191), + (0x195, 0x1F6), + (0x199, 0x198), + (0x19A, 0x23D), + (0x19B, 0xA7DC), + (0x19E, 0x220), + (0x1A1, 0x1A0), + (0x1A3, 0x1A2), + (0x1A5, 0x1A4), + (0x1A8, 0x1A7), + (0x1AD, 0x1AC), + (0x1B0, 0x1AF), + (0x1B4, 0x1B3), + (0x1B6, 0x1B5), + (0x1B9, 0x1B8), + (0x1BD, 0x1BC), + (0x1BF, 0x1F7), + (0x1C5, 0x1C4), + (0x1C6, 0x1C4), + (0x1C8, 0x1C7), + (0x1C9, 0x1C7), + (0x1CB, 0x1CA), + (0x1CC, 0x1CA), + (0x1CE, 0x1CD), + (0x1D0, 0x1CF), + (0x1D2, 0x1D1), + (0x1D4, 0x1D3), + (0x1D6, 0x1D5), + (0x1D8, 0x1D7), + (0x1DA, 0x1D9), + (0x1DC, 0x1DB), + (0x1DD, 0x18E), + (0x1DF, 0x1DE), + (0x1E1, 0x1E0), + (0x1E3, 0x1E2), + (0x1E5, 0x1E4), + (0x1E7, 0x1E6), + (0x1E9, 0x1E8), + (0x1EB, 0x1EA), + (0x1ED, 0x1EC), + (0x1EF, 0x1EE), + (0x1F2, 0x1F1), + (0x1F3, 0x1F1), + (0x1F5, 0x1F4), + (0x1F9, 0x1F8), + (0x1FB, 0x1FA), + (0x1FD, 0x1FC), + (0x1FF, 0x1FE), + (0x201, 0x200), + (0x203, 0x202), + (0x205, 0x204), + (0x207, 0x206), + (0x209, 0x208), + (0x20B, 0x20A), + (0x20D, 0x20C), + (0x20F, 0x20E), + (0x211, 0x210), + (0x213, 0x212), + (0x215, 0x214), + (0x217, 0x216), + (0x219, 0x218), + (0x21B, 0x21A), + (0x21D, 0x21C), + (0x21F, 0x21E), + (0x223, 0x222), + (0x225, 0x224), + (0x227, 0x226), + (0x229, 0x228), + (0x22B, 0x22A), + (0x22D, 0x22C), + (0x22F, 0x22E), + (0x231, 0x230), + (0x233, 0x232), + (0x23C, 0x23B), + (0x23F, 0x2C7E), + (0x240, 0x2C7F), + (0x242, 0x241), + (0x247, 0x246), + (0x249, 0x248), + (0x24B, 0x24A), + (0x24D, 0x24C), + (0x24F, 0x24E), + (0x250, 0x2C6F), + (0x251, 0x2C6D), + (0x252, 0x2C70), + (0x253, 0x181), + (0x254, 0x186), + (0x256, 0x189), + (0x257, 0x18A), + (0x259, 0x18F), + (0x25B, 0x190), + (0x25C, 0xA7AB), + (0x260, 0x193), + (0x261, 0xA7AC), + (0x263, 0x194), + (0x264, 0xA7CB), + (0x265, 0xA78D), + (0x266, 0xA7AA), + (0x268, 0x197), + (0x269, 0x196), + (0x26A, 0xA7AE), + (0x26B, 0x2C62), + (0x26C, 0xA7AD), + (0x26F, 0x19C), + (0x271, 0x2C6E), + (0x272, 0x19D), + (0x275, 0x19F), + (0x277, 0xA7DD), + (0x27C, 0xA7E2), + (0x27D, 0x2C64), + (0x280, 0x1A6), + (0x282, 0xA7C5), + (0x283, 0x1A9), + (0x287, 0xA7B1), + (0x288, 0x1AE), + (0x289, 0x244), + (0x28A, 0x1B1), + (0x28B, 0x1B2), + (0x28C, 0x245), + (0x292, 0x1B7), + (0x29D, 0xA7B2), + (0x29E, 0xA7B0), + (0x345, 0x399), + (0x371, 0x370), + (0x373, 0x372), + (0x377, 0x376), + (0x37B, 0x3FD), + (0x37C, 0x3FE), + (0x37D, 0x3FF), + (0x3AC, 0x386), + (0x3AD, 0x388), + (0x3AE, 0x389), + (0x3AF, 0x38A), + (0x3B1, 0x391), + (0x3B2, 0x392), + (0x3B3, 0x393), + (0x3B4, 0x394), + (0x3B5, 0x395), + (0x3B6, 0x396), + (0x3B7, 0x397), + (0x3B8, 0x398), + (0x3B9, 0x399), + (0x3BA, 0x39A), + (0x3BB, 0x39B), + (0x3BC, 0x39C), + (0x3BD, 0x39D), + (0x3BE, 0x39E), + (0x3BF, 0x39F), + (0x3C0, 0x3A0), + (0x3C1, 0x3A1), + (0x3C2, 0x3A3), + (0x3C3, 0x3A3), + (0x3C4, 0x3A4), + (0x3C5, 0x3A5), + (0x3C6, 0x3A6), + (0x3C7, 0x3A7), + (0x3C8, 0x3A8), + (0x3C9, 0x3A9), + (0x3CA, 0x3AA), + (0x3CB, 0x3AB), + (0x3CC, 0x38C), + (0x3CD, 0x38E), + (0x3CE, 0x38F), + (0x3D0, 0x392), + (0x3D1, 0x398), + (0x3D5, 0x3A6), + (0x3D6, 0x3A0), + (0x3D7, 0x3CF), + (0x3D9, 0x3D8), + (0x3DB, 0x3DA), + (0x3DD, 0x3DC), + (0x3DF, 0x3DE), + (0x3E1, 0x3E0), + (0x3E3, 0x3E2), + (0x3E5, 0x3E4), + (0x3E7, 0x3E6), + (0x3E9, 0x3E8), + (0x3EB, 0x3EA), + (0x3ED, 0x3EC), + (0x3EF, 0x3EE), + (0x3F0, 0x39A), + (0x3F1, 0x3A1), + (0x3F2, 0x3F9), + (0x3F3, 0x37F), + (0x3F5, 0x395), + (0x3F8, 0x3F7), + (0x3FB, 0x3FA), + (0x430, 0x410), + (0x431, 0x411), + (0x432, 0x412), + (0x433, 0x413), + (0x434, 0x414), + (0x435, 0x415), + (0x436, 0x416), + (0x437, 0x417), + (0x438, 0x418), + (0x439, 0x419), + (0x43A, 0x41A), + (0x43B, 0x41B), + (0x43C, 0x41C), + (0x43D, 0x41D), + (0x43E, 0x41E), + (0x43F, 0x41F), + (0x440, 0x420), + (0x441, 0x421), + (0x442, 0x422), + (0x443, 0x423), + (0x444, 0x424), + (0x445, 0x425), + (0x446, 0x426), + (0x447, 0x427), + (0x448, 0x428), + (0x449, 0x429), + (0x44A, 0x42A), + (0x44B, 0x42B), + (0x44C, 0x42C), + (0x44D, 0x42D), + (0x44E, 0x42E), + (0x44F, 0x42F), + (0x450, 0x400), + (0x451, 0x401), + (0x452, 0x402), + (0x453, 0x403), + (0x454, 0x404), + (0x455, 0x405), + (0x456, 0x406), + (0x457, 0x407), + (0x458, 0x408), + (0x459, 0x409), + (0x45A, 0x40A), + (0x45B, 0x40B), + (0x45C, 0x40C), + (0x45D, 0x40D), + (0x45E, 0x40E), + (0x45F, 0x40F), + (0x461, 0x460), + (0x463, 0x462), + (0x465, 0x464), + (0x467, 0x466), + (0x469, 0x468), + (0x46B, 0x46A), + (0x46D, 0x46C), + (0x46F, 0x46E), + (0x471, 0x470), + (0x473, 0x472), + (0x475, 0x474), + (0x477, 0x476), + (0x479, 0x478), + (0x47B, 0x47A), + (0x47D, 0x47C), + (0x47F, 0x47E), + (0x481, 0x480), + (0x48B, 0x48A), + (0x48D, 0x48C), + (0x48F, 0x48E), + (0x491, 0x490), + (0x493, 0x492), + (0x495, 0x494), + (0x497, 0x496), + (0x499, 0x498), + (0x49B, 0x49A), + (0x49D, 0x49C), + (0x49F, 0x49E), + (0x4A1, 0x4A0), + (0x4A3, 0x4A2), + (0x4A5, 0x4A4), + (0x4A7, 0x4A6), + (0x4A9, 0x4A8), + (0x4AB, 0x4AA), + (0x4AD, 0x4AC), + (0x4AF, 0x4AE), + (0x4B1, 0x4B0), + (0x4B3, 0x4B2), + (0x4B5, 0x4B4), + (0x4B7, 0x4B6), + (0x4B9, 0x4B8), + (0x4BB, 0x4BA), + (0x4BD, 0x4BC), + (0x4BF, 0x4BE), + (0x4C2, 0x4C1), + (0x4C4, 0x4C3), + (0x4C6, 0x4C5), + (0x4C8, 0x4C7), + (0x4CA, 0x4C9), + (0x4CC, 0x4CB), + (0x4CE, 0x4CD), + (0x4CF, 0x4C0), + (0x4D1, 0x4D0), + (0x4D3, 0x4D2), + (0x4D5, 0x4D4), + (0x4D7, 0x4D6), + (0x4D9, 0x4D8), + (0x4DB, 0x4DA), + (0x4DD, 0x4DC), + (0x4DF, 0x4DE), + (0x4E1, 0x4E0), + (0x4E3, 0x4E2), + (0x4E5, 0x4E4), + (0x4E7, 0x4E6), + (0x4E9, 0x4E8), + (0x4EB, 0x4EA), + (0x4ED, 0x4EC), + (0x4EF, 0x4EE), + (0x4F1, 0x4F0), + (0x4F3, 0x4F2), + (0x4F5, 0x4F4), + (0x4F7, 0x4F6), + (0x4F9, 0x4F8), + (0x4FB, 0x4FA), + (0x4FD, 0x4FC), + (0x4FF, 0x4FE), + (0x501, 0x500), + (0x503, 0x502), + (0x505, 0x504), + (0x507, 0x506), + (0x509, 0x508), + (0x50B, 0x50A), + (0x50D, 0x50C), + (0x50F, 0x50E), + (0x511, 0x510), + (0x513, 0x512), + (0x515, 0x514), + (0x517, 0x516), + (0x519, 0x518), + (0x51B, 0x51A), + (0x51D, 0x51C), + (0x51F, 0x51E), + (0x521, 0x520), + (0x523, 0x522), + (0x525, 0x524), + (0x527, 0x526), + (0x529, 0x528), + (0x52B, 0x52A), + (0x52D, 0x52C), + (0x52F, 0x52E), + (0x561, 0x531), + (0x562, 0x532), + (0x563, 0x533), + (0x564, 0x534), + (0x565, 0x535), + (0x566, 0x536), + (0x567, 0x537), + (0x568, 0x538), + (0x569, 0x539), + (0x56A, 0x53A), + (0x56B, 0x53B), + (0x56C, 0x53C), + (0x56D, 0x53D), + (0x56E, 0x53E), + (0x56F, 0x53F), + (0x570, 0x540), + (0x571, 0x541), + (0x572, 0x542), + (0x573, 0x543), + (0x574, 0x544), + (0x575, 0x545), + (0x576, 0x546), + (0x577, 0x547), + (0x578, 0x548), + (0x579, 0x549), + (0x57A, 0x54A), + (0x57B, 0x54B), + (0x57C, 0x54C), + (0x57D, 0x54D), + (0x57E, 0x54E), + (0x57F, 0x54F), + (0x580, 0x550), + (0x581, 0x551), + (0x582, 0x552), + (0x583, 0x553), + (0x584, 0x554), + (0x585, 0x555), + (0x586, 0x556), + (0x10D0, 0x1C90), + (0x10D1, 0x1C91), + (0x10D2, 0x1C92), + (0x10D3, 0x1C93), + (0x10D4, 0x1C94), + (0x10D5, 0x1C95), + (0x10D6, 0x1C96), + (0x10D7, 0x1C97), + (0x10D8, 0x1C98), + (0x10D9, 0x1C99), + (0x10DA, 0x1C9A), + (0x10DB, 0x1C9B), + (0x10DC, 0x1C9C), + (0x10DD, 0x1C9D), + (0x10DE, 0x1C9E), + (0x10DF, 0x1C9F), + (0x10E0, 0x1CA0), + (0x10E1, 0x1CA1), + (0x10E2, 0x1CA2), + (0x10E3, 0x1CA3), + (0x10E4, 0x1CA4), + (0x10E5, 0x1CA5), + (0x10E6, 0x1CA6), + (0x10E7, 0x1CA7), + (0x10E8, 0x1CA8), + (0x10E9, 0x1CA9), + (0x10EA, 0x1CAA), + (0x10EB, 0x1CAB), + (0x10EC, 0x1CAC), + (0x10ED, 0x1CAD), + (0x10EE, 0x1CAE), + (0x10EF, 0x1CAF), + (0x10F0, 0x1CB0), + (0x10F1, 0x1CB1), + (0x10F2, 0x1CB2), + (0x10F3, 0x1CB3), + (0x10F4, 0x1CB4), + (0x10F5, 0x1CB5), + (0x10F6, 0x1CB6), + (0x10F7, 0x1CB7), + (0x10F8, 0x1CB8), + (0x10F9, 0x1CB9), + (0x10FA, 0x1CBA), + (0x10FD, 0x1CBD), + (0x10FE, 0x1CBE), + (0x10FF, 0x1CBF), + (0x13F8, 0x13F0), + (0x13F9, 0x13F1), + (0x13FA, 0x13F2), + (0x13FB, 0x13F3), + (0x13FC, 0x13F4), + (0x13FD, 0x13F5), + (0x1C80, 0x412), + (0x1C81, 0x414), + (0x1C82, 0x41E), + (0x1C83, 0x421), + (0x1C84, 0x422), + (0x1C85, 0x422), + (0x1C86, 0x42A), + (0x1C87, 0x462), + (0x1C88, 0xA64A), + (0x1C8A, 0x1C89), + (0x1D79, 0xA77D), + (0x1D7D, 0x2C63), + (0x1D8E, 0xA7C6), + (0x1E01, 0x1E00), + (0x1E03, 0x1E02), + (0x1E05, 0x1E04), + (0x1E07, 0x1E06), + (0x1E09, 0x1E08), + (0x1E0B, 0x1E0A), + (0x1E0D, 0x1E0C), + (0x1E0F, 0x1E0E), + (0x1E11, 0x1E10), + (0x1E13, 0x1E12), + (0x1E15, 0x1E14), + (0x1E17, 0x1E16), + (0x1E19, 0x1E18), + (0x1E1B, 0x1E1A), + (0x1E1D, 0x1E1C), + (0x1E1F, 0x1E1E), + (0x1E21, 0x1E20), + (0x1E23, 0x1E22), + (0x1E25, 0x1E24), + (0x1E27, 0x1E26), + (0x1E29, 0x1E28), + (0x1E2B, 0x1E2A), + (0x1E2D, 0x1E2C), + (0x1E2F, 0x1E2E), + (0x1E31, 0x1E30), + (0x1E33, 0x1E32), + (0x1E35, 0x1E34), + (0x1E37, 0x1E36), + (0x1E39, 0x1E38), + (0x1E3B, 0x1E3A), + (0x1E3D, 0x1E3C), + (0x1E3F, 0x1E3E), + (0x1E41, 0x1E40), + (0x1E43, 0x1E42), + (0x1E45, 0x1E44), + (0x1E47, 0x1E46), + (0x1E49, 0x1E48), + (0x1E4B, 0x1E4A), + (0x1E4D, 0x1E4C), + (0x1E4F, 0x1E4E), + (0x1E51, 0x1E50), + (0x1E53, 0x1E52), + (0x1E55, 0x1E54), + (0x1E57, 0x1E56), + (0x1E59, 0x1E58), + (0x1E5B, 0x1E5A), + (0x1E5D, 0x1E5C), + (0x1E5F, 0x1E5E), + (0x1E61, 0x1E60), + (0x1E63, 0x1E62), + (0x1E65, 0x1E64), + (0x1E67, 0x1E66), + (0x1E69, 0x1E68), + (0x1E6B, 0x1E6A), + (0x1E6D, 0x1E6C), + (0x1E6F, 0x1E6E), + (0x1E71, 0x1E70), + (0x1E73, 0x1E72), + (0x1E75, 0x1E74), + (0x1E77, 0x1E76), + (0x1E79, 0x1E78), + (0x1E7B, 0x1E7A), + (0x1E7D, 0x1E7C), + (0x1E7F, 0x1E7E), + (0x1E81, 0x1E80), + (0x1E83, 0x1E82), + (0x1E85, 0x1E84), + (0x1E87, 0x1E86), + (0x1E89, 0x1E88), + (0x1E8B, 0x1E8A), + (0x1E8D, 0x1E8C), + (0x1E8F, 0x1E8E), + (0x1E91, 0x1E90), + (0x1E93, 0x1E92), + (0x1E95, 0x1E94), + (0x1E9B, 0x1E60), + (0x1EA1, 0x1EA0), + (0x1EA3, 0x1EA2), + (0x1EA5, 0x1EA4), + (0x1EA7, 0x1EA6), + (0x1EA9, 0x1EA8), + (0x1EAB, 0x1EAA), + (0x1EAD, 0x1EAC), + (0x1EAF, 0x1EAE), + (0x1EB1, 0x1EB0), + (0x1EB3, 0x1EB2), + (0x1EB5, 0x1EB4), + (0x1EB7, 0x1EB6), + (0x1EB9, 0x1EB8), + (0x1EBB, 0x1EBA), + (0x1EBD, 0x1EBC), + (0x1EBF, 0x1EBE), + (0x1EC1, 0x1EC0), + (0x1EC3, 0x1EC2), + (0x1EC5, 0x1EC4), + (0x1EC7, 0x1EC6), + (0x1EC9, 0x1EC8), + (0x1ECB, 0x1ECA), + (0x1ECD, 0x1ECC), + (0x1ECF, 0x1ECE), + (0x1ED1, 0x1ED0), + (0x1ED3, 0x1ED2), + (0x1ED5, 0x1ED4), + (0x1ED7, 0x1ED6), + (0x1ED9, 0x1ED8), + (0x1EDB, 0x1EDA), + (0x1EDD, 0x1EDC), + (0x1EDF, 0x1EDE), + (0x1EE1, 0x1EE0), + (0x1EE3, 0x1EE2), + (0x1EE5, 0x1EE4), + (0x1EE7, 0x1EE6), + (0x1EE9, 0x1EE8), + (0x1EEB, 0x1EEA), + (0x1EED, 0x1EEC), + (0x1EEF, 0x1EEE), + (0x1EF1, 0x1EF0), + (0x1EF3, 0x1EF2), + (0x1EF5, 0x1EF4), + (0x1EF7, 0x1EF6), + (0x1EF9, 0x1EF8), + (0x1EFB, 0x1EFA), + (0x1EFD, 0x1EFC), + (0x1EFF, 0x1EFE), + (0x1F00, 0x1F08), + (0x1F01, 0x1F09), + (0x1F02, 0x1F0A), + (0x1F03, 0x1F0B), + (0x1F04, 0x1F0C), + (0x1F05, 0x1F0D), + (0x1F06, 0x1F0E), + (0x1F07, 0x1F0F), + (0x1F10, 0x1F18), + (0x1F11, 0x1F19), + (0x1F12, 0x1F1A), + (0x1F13, 0x1F1B), + (0x1F14, 0x1F1C), + (0x1F15, 0x1F1D), + (0x1F20, 0x1F28), + (0x1F21, 0x1F29), + (0x1F22, 0x1F2A), + (0x1F23, 0x1F2B), + (0x1F24, 0x1F2C), + (0x1F25, 0x1F2D), + (0x1F26, 0x1F2E), + (0x1F27, 0x1F2F), + (0x1F30, 0x1F38), + (0x1F31, 0x1F39), + (0x1F32, 0x1F3A), + (0x1F33, 0x1F3B), + (0x1F34, 0x1F3C), + (0x1F35, 0x1F3D), + (0x1F36, 0x1F3E), + (0x1F37, 0x1F3F), + (0x1F40, 0x1F48), + (0x1F41, 0x1F49), + (0x1F42, 0x1F4A), + (0x1F43, 0x1F4B), + (0x1F44, 0x1F4C), + (0x1F45, 0x1F4D), + (0x1F51, 0x1F59), + (0x1F53, 0x1F5B), + (0x1F55, 0x1F5D), + (0x1F57, 0x1F5F), + (0x1F60, 0x1F68), + (0x1F61, 0x1F69), + (0x1F62, 0x1F6A), + (0x1F63, 0x1F6B), + (0x1F64, 0x1F6C), + (0x1F65, 0x1F6D), + (0x1F66, 0x1F6E), + (0x1F67, 0x1F6F), + (0x1F70, 0x1FBA), + (0x1F71, 0x1FBB), + (0x1F72, 0x1FC8), + (0x1F73, 0x1FC9), + (0x1F74, 0x1FCA), + (0x1F75, 0x1FCB), + (0x1F76, 0x1FDA), + (0x1F77, 0x1FDB), + (0x1F78, 0x1FF8), + (0x1F79, 0x1FF9), + (0x1F7A, 0x1FEA), + (0x1F7B, 0x1FEB), + (0x1F7C, 0x1FFA), + (0x1F7D, 0x1FFB), + (0x1FB0, 0x1FB8), + (0x1FB1, 0x1FB9), + (0x1FBE, 0x399), + (0x1FD0, 0x1FD8), + (0x1FD1, 0x1FD9), + (0x1FE0, 0x1FE8), + (0x1FE1, 0x1FE9), + (0x1FE5, 0x1FEC), + (0x214E, 0x2132), + (0x2170, 0x2160), + (0x2171, 0x2161), + (0x2172, 0x2162), + (0x2173, 0x2163), + (0x2174, 0x2164), + (0x2175, 0x2165), + (0x2176, 0x2166), + (0x2177, 0x2167), + (0x2178, 0x2168), + (0x2179, 0x2169), + (0x217A, 0x216A), + (0x217B, 0x216B), + (0x217C, 0x216C), + (0x217D, 0x216D), + (0x217E, 0x216E), + (0x217F, 0x216F), + (0x2184, 0x2183), + (0x24D0, 0x24B6), + (0x24D1, 0x24B7), + (0x24D2, 0x24B8), + (0x24D3, 0x24B9), + (0x24D4, 0x24BA), + (0x24D5, 0x24BB), + (0x24D6, 0x24BC), + (0x24D7, 0x24BD), + (0x24D8, 0x24BE), + (0x24D9, 0x24BF), + (0x24DA, 0x24C0), + (0x24DB, 0x24C1), + (0x24DC, 0x24C2), + (0x24DD, 0x24C3), + (0x24DE, 0x24C4), + (0x24DF, 0x24C5), + (0x24E0, 0x24C6), + (0x24E1, 0x24C7), + (0x24E2, 0x24C8), + (0x24E3, 0x24C9), + (0x24E4, 0x24CA), + (0x24E5, 0x24CB), + (0x24E6, 0x24CC), + (0x24E7, 0x24CD), + (0x24E8, 0x24CE), + (0x24E9, 0x24CF), + (0x2C30, 0x2C00), + (0x2C31, 0x2C01), + (0x2C32, 0x2C02), + (0x2C33, 0x2C03), + (0x2C34, 0x2C04), + (0x2C35, 0x2C05), + (0x2C36, 0x2C06), + (0x2C37, 0x2C07), + (0x2C38, 0x2C08), + (0x2C39, 0x2C09), + (0x2C3A, 0x2C0A), + (0x2C3B, 0x2C0B), + (0x2C3C, 0x2C0C), + (0x2C3D, 0x2C0D), + (0x2C3E, 0x2C0E), + (0x2C3F, 0x2C0F), + (0x2C40, 0x2C10), + (0x2C41, 0x2C11), + (0x2C42, 0x2C12), + (0x2C43, 0x2C13), + (0x2C44, 0x2C14), + (0x2C45, 0x2C15), + (0x2C46, 0x2C16), + (0x2C47, 0x2C17), + (0x2C48, 0x2C18), + (0x2C49, 0x2C19), + (0x2C4A, 0x2C1A), + (0x2C4B, 0x2C1B), + (0x2C4C, 0x2C1C), + (0x2C4D, 0x2C1D), + (0x2C4E, 0x2C1E), + (0x2C4F, 0x2C1F), + (0x2C50, 0x2C20), + (0x2C51, 0x2C21), + (0x2C52, 0x2C22), + (0x2C53, 0x2C23), + (0x2C54, 0x2C24), + (0x2C55, 0x2C25), + (0x2C56, 0x2C26), + (0x2C57, 0x2C27), + (0x2C58, 0x2C28), + (0x2C59, 0x2C29), + (0x2C5A, 0x2C2A), + (0x2C5B, 0x2C2B), + (0x2C5C, 0x2C2C), + (0x2C5D, 0x2C2D), + (0x2C5E, 0x2C2E), + (0x2C5F, 0x2C2F), + (0x2C61, 0x2C60), + (0x2C65, 0x23A), + (0x2C66, 0x23E), + (0x2C68, 0x2C67), + (0x2C6A, 0x2C69), + (0x2C6C, 0x2C6B), + (0x2C73, 0x2C72), + (0x2C76, 0x2C75), + (0x2C81, 0x2C80), + (0x2C83, 0x2C82), + (0x2C85, 0x2C84), + (0x2C87, 0x2C86), + (0x2C89, 0x2C88), + (0x2C8B, 0x2C8A), + (0x2C8D, 0x2C8C), + (0x2C8F, 0x2C8E), + (0x2C91, 0x2C90), + (0x2C93, 0x2C92), + (0x2C95, 0x2C94), + (0x2C97, 0x2C96), + (0x2C99, 0x2C98), + (0x2C9B, 0x2C9A), + (0x2C9D, 0x2C9C), + (0x2C9F, 0x2C9E), + (0x2CA1, 0x2CA0), + (0x2CA3, 0x2CA2), + (0x2CA5, 0x2CA4), + (0x2CA7, 0x2CA6), + (0x2CA9, 0x2CA8), + (0x2CAB, 0x2CAA), + (0x2CAD, 0x2CAC), + (0x2CAF, 0x2CAE), + (0x2CB1, 0x2CB0), + (0x2CB3, 0x2CB2), + (0x2CB5, 0x2CB4), + (0x2CB7, 0x2CB6), + (0x2CB9, 0x2CB8), + (0x2CBB, 0x2CBA), + (0x2CBD, 0x2CBC), + (0x2CBF, 0x2CBE), + (0x2CC1, 0x2CC0), + (0x2CC3, 0x2CC2), + (0x2CC5, 0x2CC4), + (0x2CC7, 0x2CC6), + (0x2CC9, 0x2CC8), + (0x2CCB, 0x2CCA), + (0x2CCD, 0x2CCC), + (0x2CCF, 0x2CCE), + (0x2CD1, 0x2CD0), + (0x2CD3, 0x2CD2), + (0x2CD5, 0x2CD4), + (0x2CD7, 0x2CD6), + (0x2CD9, 0x2CD8), + (0x2CDB, 0x2CDA), + (0x2CDD, 0x2CDC), + (0x2CDF, 0x2CDE), + (0x2CE1, 0x2CE0), + (0x2CE3, 0x2CE2), + (0x2CEC, 0x2CEB), + (0x2CEE, 0x2CED), + (0x2CF3, 0x2CF2), + (0x2D00, 0x10A0), + (0x2D01, 0x10A1), + (0x2D02, 0x10A2), + (0x2D03, 0x10A3), + (0x2D04, 0x10A4), + (0x2D05, 0x10A5), + (0x2D06, 0x10A6), + (0x2D07, 0x10A7), + (0x2D08, 0x10A8), + (0x2D09, 0x10A9), + (0x2D0A, 0x10AA), + (0x2D0B, 0x10AB), + (0x2D0C, 0x10AC), + (0x2D0D, 0x10AD), + (0x2D0E, 0x10AE), + (0x2D0F, 0x10AF), + (0x2D10, 0x10B0), + (0x2D11, 0x10B1), + (0x2D12, 0x10B2), + (0x2D13, 0x10B3), + (0x2D14, 0x10B4), + (0x2D15, 0x10B5), + (0x2D16, 0x10B6), + (0x2D17, 0x10B7), + (0x2D18, 0x10B8), + (0x2D19, 0x10B9), + (0x2D1A, 0x10BA), + (0x2D1B, 0x10BB), + (0x2D1C, 0x10BC), + (0x2D1D, 0x10BD), + (0x2D1E, 0x10BE), + (0x2D1F, 0x10BF), + (0x2D20, 0x10C0), + (0x2D21, 0x10C1), + (0x2D22, 0x10C2), + (0x2D23, 0x10C3), + (0x2D24, 0x10C4), + (0x2D25, 0x10C5), + (0x2D27, 0x10C7), + (0x2D2D, 0x10CD), + (0xA641, 0xA640), + (0xA643, 0xA642), + (0xA645, 0xA644), + (0xA647, 0xA646), + (0xA649, 0xA648), + (0xA64B, 0xA64A), + (0xA64D, 0xA64C), + (0xA64F, 0xA64E), + (0xA651, 0xA650), + (0xA653, 0xA652), + (0xA655, 0xA654), + (0xA657, 0xA656), + (0xA659, 0xA658), + (0xA65B, 0xA65A), + (0xA65D, 0xA65C), + (0xA65F, 0xA65E), + (0xA661, 0xA660), + (0xA663, 0xA662), + (0xA665, 0xA664), + (0xA667, 0xA666), + (0xA669, 0xA668), + (0xA66B, 0xA66A), + (0xA66D, 0xA66C), + (0xA681, 0xA680), + (0xA683, 0xA682), + (0xA685, 0xA684), + (0xA687, 0xA686), + (0xA689, 0xA688), + (0xA68B, 0xA68A), + (0xA68D, 0xA68C), + (0xA68F, 0xA68E), + (0xA691, 0xA690), + (0xA693, 0xA692), + (0xA695, 0xA694), + (0xA697, 0xA696), + (0xA699, 0xA698), + (0xA69B, 0xA69A), + (0xA723, 0xA722), + (0xA725, 0xA724), + (0xA727, 0xA726), + (0xA729, 0xA728), + (0xA72B, 0xA72A), + (0xA72D, 0xA72C), + (0xA72F, 0xA72E), + (0xA733, 0xA732), + (0xA735, 0xA734), + (0xA737, 0xA736), + (0xA739, 0xA738), + (0xA73B, 0xA73A), + (0xA73D, 0xA73C), + (0xA73F, 0xA73E), + (0xA741, 0xA740), + (0xA743, 0xA742), + (0xA745, 0xA744), + (0xA747, 0xA746), + (0xA749, 0xA748), + (0xA74B, 0xA74A), + (0xA74D, 0xA74C), + (0xA74F, 0xA74E), + (0xA751, 0xA750), + (0xA753, 0xA752), + (0xA755, 0xA754), + (0xA757, 0xA756), + (0xA759, 0xA758), + (0xA75B, 0xA75A), + (0xA75D, 0xA75C), + (0xA75F, 0xA75E), + (0xA761, 0xA760), + (0xA763, 0xA762), + (0xA765, 0xA764), + (0xA767, 0xA766), + (0xA769, 0xA768), + (0xA76B, 0xA76A), + (0xA76D, 0xA76C), + (0xA76F, 0xA76E), + (0xA77A, 0xA779), + (0xA77C, 0xA77B), + (0xA77F, 0xA77E), + (0xA781, 0xA780), + (0xA783, 0xA782), + (0xA785, 0xA784), + (0xA787, 0xA786), + (0xA78C, 0xA78B), + (0xA791, 0xA790), + (0xA793, 0xA792), + (0xA794, 0xA7C4), + (0xA797, 0xA796), + (0xA799, 0xA798), + (0xA79B, 0xA79A), + (0xA79D, 0xA79C), + (0xA79F, 0xA79E), + (0xA7A1, 0xA7A0), + (0xA7A3, 0xA7A2), + (0xA7A5, 0xA7A4), + (0xA7A7, 0xA7A6), + (0xA7A9, 0xA7A8), + (0xA7B5, 0xA7B4), + (0xA7B7, 0xA7B6), + (0xA7B9, 0xA7B8), + (0xA7BB, 0xA7BA), + (0xA7BD, 0xA7BC), + (0xA7BF, 0xA7BE), + (0xA7C1, 0xA7C0), + (0xA7C3, 0xA7C2), + (0xA7C8, 0xA7C7), + (0xA7CA, 0xA7C9), + (0xA7CD, 0xA7CC), + (0xA7CF, 0xA7CE), + (0xA7D1, 0xA7D0), + (0xA7D3, 0xA7D2), + (0xA7D5, 0xA7D4), + (0xA7D7, 0xA7D6), + (0xA7D9, 0xA7D8), + (0xA7DB, 0xA7DA), + (0xA7F6, 0xA7F5), + (0xAB4B, 0xAB6C), + (0xAB4C, 0xAB6D), + (0xAB53, 0xA7B3), + (0xAB70, 0x13A0), + (0xAB71, 0x13A1), + (0xAB72, 0x13A2), + (0xAB73, 0x13A3), + (0xAB74, 0x13A4), + (0xAB75, 0x13A5), + (0xAB76, 0x13A6), + (0xAB77, 0x13A7), + (0xAB78, 0x13A8), + (0xAB79, 0x13A9), + (0xAB7A, 0x13AA), + (0xAB7B, 0x13AB), + (0xAB7C, 0x13AC), + (0xAB7D, 0x13AD), + (0xAB7E, 0x13AE), + (0xAB7F, 0x13AF), + (0xAB80, 0x13B0), + (0xAB81, 0x13B1), + (0xAB82, 0x13B2), + (0xAB83, 0x13B3), + (0xAB84, 0x13B4), + (0xAB85, 0x13B5), + (0xAB86, 0x13B6), + (0xAB87, 0x13B7), + (0xAB88, 0x13B8), + (0xAB89, 0x13B9), + (0xAB8A, 0x13BA), + (0xAB8B, 0x13BB), + (0xAB8C, 0x13BC), + (0xAB8D, 0x13BD), + (0xAB8E, 0x13BE), + (0xAB8F, 0x13BF), + (0xAB90, 0x13C0), + (0xAB91, 0x13C1), + (0xAB92, 0x13C2), + (0xAB93, 0x13C3), + (0xAB94, 0x13C4), + (0xAB95, 0x13C5), + (0xAB96, 0x13C6), + (0xAB97, 0x13C7), + (0xAB98, 0x13C8), + (0xAB99, 0x13C9), + (0xAB9A, 0x13CA), + (0xAB9B, 0x13CB), + (0xAB9C, 0x13CC), + (0xAB9D, 0x13CD), + (0xAB9E, 0x13CE), + (0xAB9F, 0x13CF), + (0xABA0, 0x13D0), + (0xABA1, 0x13D1), + (0xABA2, 0x13D2), + (0xABA3, 0x13D3), + (0xABA4, 0x13D4), + (0xABA5, 0x13D5), + (0xABA6, 0x13D6), + (0xABA7, 0x13D7), + (0xABA8, 0x13D8), + (0xABA9, 0x13D9), + (0xABAA, 0x13DA), + (0xABAB, 0x13DB), + (0xABAC, 0x13DC), + (0xABAD, 0x13DD), + (0xABAE, 0x13DE), + (0xABAF, 0x13DF), + (0xABB0, 0x13E0), + (0xABB1, 0x13E1), + (0xABB2, 0x13E2), + (0xABB3, 0x13E3), + (0xABB4, 0x13E4), + (0xABB5, 0x13E5), + (0xABB6, 0x13E6), + (0xABB7, 0x13E7), + (0xABB8, 0x13E8), + (0xABB9, 0x13E9), + (0xABBA, 0x13EA), + (0xABBB, 0x13EB), + (0xABBC, 0x13EC), + (0xABBD, 0x13ED), + (0xABBE, 0x13EE), + (0xABBF, 0x13EF), + (0xFF41, 0xFF21), + (0xFF42, 0xFF22), + (0xFF43, 0xFF23), + (0xFF44, 0xFF24), + (0xFF45, 0xFF25), + (0xFF46, 0xFF26), + (0xFF47, 0xFF27), + (0xFF48, 0xFF28), + (0xFF49, 0xFF29), + (0xFF4A, 0xFF2A), + (0xFF4B, 0xFF2B), + (0xFF4C, 0xFF2C), + (0xFF4D, 0xFF2D), + (0xFF4E, 0xFF2E), + (0xFF4F, 0xFF2F), + (0xFF50, 0xFF30), + (0xFF51, 0xFF31), + (0xFF52, 0xFF32), + (0xFF53, 0xFF33), + (0xFF54, 0xFF34), + (0xFF55, 0xFF35), + (0xFF56, 0xFF36), + (0xFF57, 0xFF37), + (0xFF58, 0xFF38), + (0xFF59, 0xFF39), + (0xFF5A, 0xFF3A), +]; + +/// `UPPER` as (upper case, code point), sorted. +pub static UPPER_INVERSE: &[(u32, u32)] = &[ + (0x41, 0x61), + (0x42, 0x62), + (0x43, 0x63), + (0x44, 0x64), + (0x45, 0x65), + (0x46, 0x66), + (0x47, 0x67), + (0x48, 0x68), + (0x49, 0x69), + (0x4A, 0x6A), + (0x4B, 0x6B), + (0x4C, 0x6C), + (0x4D, 0x6D), + (0x4E, 0x6E), + (0x4F, 0x6F), + (0x50, 0x70), + (0x51, 0x71), + (0x52, 0x72), + (0x53, 0x73), + (0x54, 0x74), + (0x55, 0x75), + (0x56, 0x76), + (0x57, 0x77), + (0x58, 0x78), + (0x59, 0x79), + (0x5A, 0x7A), + (0xC0, 0xE0), + (0xC1, 0xE1), + (0xC2, 0xE2), + (0xC3, 0xE3), + (0xC4, 0xE4), + (0xC5, 0xE5), + (0xC6, 0xE6), + (0xC7, 0xE7), + (0xC8, 0xE8), + (0xC9, 0xE9), + (0xCA, 0xEA), + (0xCB, 0xEB), + (0xCC, 0xEC), + (0xCD, 0xED), + (0xCE, 0xEE), + (0xCF, 0xEF), + (0xD0, 0xF0), + (0xD1, 0xF1), + (0xD2, 0xF2), + (0xD3, 0xF3), + (0xD4, 0xF4), + (0xD5, 0xF5), + (0xD6, 0xF6), + (0xD8, 0xF8), + (0xD9, 0xF9), + (0xDA, 0xFA), + (0xDB, 0xFB), + (0xDC, 0xFC), + (0xDD, 0xFD), + (0xDE, 0xFE), + (0x100, 0x101), + (0x102, 0x103), + (0x104, 0x105), + (0x106, 0x107), + (0x108, 0x109), + (0x10A, 0x10B), + (0x10C, 0x10D), + (0x10E, 0x10F), + (0x110, 0x111), + (0x112, 0x113), + (0x114, 0x115), + (0x116, 0x117), + (0x118, 0x119), + (0x11A, 0x11B), + (0x11C, 0x11D), + (0x11E, 0x11F), + (0x120, 0x121), + (0x122, 0x123), + (0x124, 0x125), + (0x126, 0x127), + (0x128, 0x129), + (0x12A, 0x12B), + (0x12C, 0x12D), + (0x12E, 0x12F), + (0x132, 0x133), + (0x134, 0x135), + (0x136, 0x137), + (0x139, 0x13A), + (0x13B, 0x13C), + (0x13D, 0x13E), + (0x13F, 0x140), + (0x141, 0x142), + (0x143, 0x144), + (0x145, 0x146), + (0x147, 0x148), + (0x14A, 0x14B), + (0x14C, 0x14D), + (0x14E, 0x14F), + (0x150, 0x151), + (0x152, 0x153), + (0x154, 0x155), + (0x156, 0x157), + (0x158, 0x159), + (0x15A, 0x15B), + (0x15C, 0x15D), + (0x15E, 0x15F), + (0x160, 0x161), + (0x162, 0x163), + (0x164, 0x165), + (0x166, 0x167), + (0x168, 0x169), + (0x16A, 0x16B), + (0x16C, 0x16D), + (0x16E, 0x16F), + (0x170, 0x171), + (0x172, 0x173), + (0x174, 0x175), + (0x176, 0x177), + (0x178, 0xFF), + (0x179, 0x17A), + (0x17B, 0x17C), + (0x17D, 0x17E), + (0x181, 0x253), + (0x182, 0x183), + (0x184, 0x185), + (0x186, 0x254), + (0x187, 0x188), + (0x189, 0x256), + (0x18A, 0x257), + (0x18B, 0x18C), + (0x18E, 0x1DD), + (0x18F, 0x259), + (0x190, 0x25B), + (0x191, 0x192), + (0x193, 0x260), + (0x194, 0x263), + (0x196, 0x269), + (0x197, 0x268), + (0x198, 0x199), + (0x19C, 0x26F), + (0x19D, 0x272), + (0x19F, 0x275), + (0x1A0, 0x1A1), + (0x1A2, 0x1A3), + (0x1A4, 0x1A5), + (0x1A6, 0x280), + (0x1A7, 0x1A8), + (0x1A9, 0x283), + (0x1AC, 0x1AD), + (0x1AE, 0x288), + (0x1AF, 0x1B0), + (0x1B1, 0x28A), + (0x1B2, 0x28B), + (0x1B3, 0x1B4), + (0x1B5, 0x1B6), + (0x1B7, 0x292), + (0x1B8, 0x1B9), + (0x1BC, 0x1BD), + (0x1C4, 0x1C5), + (0x1C4, 0x1C6), + (0x1C7, 0x1C8), + (0x1C7, 0x1C9), + (0x1CA, 0x1CB), + (0x1CA, 0x1CC), + (0x1CD, 0x1CE), + (0x1CF, 0x1D0), + (0x1D1, 0x1D2), + (0x1D3, 0x1D4), + (0x1D5, 0x1D6), + (0x1D7, 0x1D8), + (0x1D9, 0x1DA), + (0x1DB, 0x1DC), + (0x1DE, 0x1DF), + (0x1E0, 0x1E1), + (0x1E2, 0x1E3), + (0x1E4, 0x1E5), + (0x1E6, 0x1E7), + (0x1E8, 0x1E9), + (0x1EA, 0x1EB), + (0x1EC, 0x1ED), + (0x1EE, 0x1EF), + (0x1F1, 0x1F2), + (0x1F1, 0x1F3), + (0x1F4, 0x1F5), + (0x1F6, 0x195), + (0x1F7, 0x1BF), + (0x1F8, 0x1F9), + (0x1FA, 0x1FB), + (0x1FC, 0x1FD), + (0x1FE, 0x1FF), + (0x200, 0x201), + (0x202, 0x203), + (0x204, 0x205), + (0x206, 0x207), + (0x208, 0x209), + (0x20A, 0x20B), + (0x20C, 0x20D), + (0x20E, 0x20F), + (0x210, 0x211), + (0x212, 0x213), + (0x214, 0x215), + (0x216, 0x217), + (0x218, 0x219), + (0x21A, 0x21B), + (0x21C, 0x21D), + (0x21E, 0x21F), + (0x220, 0x19E), + (0x222, 0x223), + (0x224, 0x225), + (0x226, 0x227), + (0x228, 0x229), + (0x22A, 0x22B), + (0x22C, 0x22D), + (0x22E, 0x22F), + (0x230, 0x231), + (0x232, 0x233), + (0x23A, 0x2C65), + (0x23B, 0x23C), + (0x23D, 0x19A), + (0x23E, 0x2C66), + (0x241, 0x242), + (0x243, 0x180), + (0x244, 0x289), + (0x245, 0x28C), + (0x246, 0x247), + (0x248, 0x249), + (0x24A, 0x24B), + (0x24C, 0x24D), + (0x24E, 0x24F), + (0x370, 0x371), + (0x372, 0x373), + (0x376, 0x377), + (0x37F, 0x3F3), + (0x386, 0x3AC), + (0x388, 0x3AD), + (0x389, 0x3AE), + (0x38A, 0x3AF), + (0x38C, 0x3CC), + (0x38E, 0x3CD), + (0x38F, 0x3CE), + (0x391, 0x3B1), + (0x392, 0x3B2), + (0x392, 0x3D0), + (0x393, 0x3B3), + (0x394, 0x3B4), + (0x395, 0x3B5), + (0x395, 0x3F5), + (0x396, 0x3B6), + (0x397, 0x3B7), + (0x398, 0x3B8), + (0x398, 0x3D1), + (0x399, 0x345), + (0x399, 0x3B9), + (0x399, 0x1FBE), + (0x39A, 0x3BA), + (0x39A, 0x3F0), + (0x39B, 0x3BB), + (0x39C, 0xB5), + (0x39C, 0x3BC), + (0x39D, 0x3BD), + (0x39E, 0x3BE), + (0x39F, 0x3BF), + (0x3A0, 0x3C0), + (0x3A0, 0x3D6), + (0x3A1, 0x3C1), + (0x3A1, 0x3F1), + (0x3A3, 0x3C2), + (0x3A3, 0x3C3), + (0x3A4, 0x3C4), + (0x3A5, 0x3C5), + (0x3A6, 0x3C6), + (0x3A6, 0x3D5), + (0x3A7, 0x3C7), + (0x3A8, 0x3C8), + (0x3A9, 0x3C9), + (0x3AA, 0x3CA), + (0x3AB, 0x3CB), + (0x3CF, 0x3D7), + (0x3D8, 0x3D9), + (0x3DA, 0x3DB), + (0x3DC, 0x3DD), + (0x3DE, 0x3DF), + (0x3E0, 0x3E1), + (0x3E2, 0x3E3), + (0x3E4, 0x3E5), + (0x3E6, 0x3E7), + (0x3E8, 0x3E9), + (0x3EA, 0x3EB), + (0x3EC, 0x3ED), + (0x3EE, 0x3EF), + (0x3F7, 0x3F8), + (0x3F9, 0x3F2), + (0x3FA, 0x3FB), + (0x3FD, 0x37B), + (0x3FE, 0x37C), + (0x3FF, 0x37D), + (0x400, 0x450), + (0x401, 0x451), + (0x402, 0x452), + (0x403, 0x453), + (0x404, 0x454), + (0x405, 0x455), + (0x406, 0x456), + (0x407, 0x457), + (0x408, 0x458), + (0x409, 0x459), + (0x40A, 0x45A), + (0x40B, 0x45B), + (0x40C, 0x45C), + (0x40D, 0x45D), + (0x40E, 0x45E), + (0x40F, 0x45F), + (0x410, 0x430), + (0x411, 0x431), + (0x412, 0x432), + (0x412, 0x1C80), + (0x413, 0x433), + (0x414, 0x434), + (0x414, 0x1C81), + (0x415, 0x435), + (0x416, 0x436), + (0x417, 0x437), + (0x418, 0x438), + (0x419, 0x439), + (0x41A, 0x43A), + (0x41B, 0x43B), + (0x41C, 0x43C), + (0x41D, 0x43D), + (0x41E, 0x43E), + (0x41E, 0x1C82), + (0x41F, 0x43F), + (0x420, 0x440), + (0x421, 0x441), + (0x421, 0x1C83), + (0x422, 0x442), + (0x422, 0x1C84), + (0x422, 0x1C85), + (0x423, 0x443), + (0x424, 0x444), + (0x425, 0x445), + (0x426, 0x446), + (0x427, 0x447), + (0x428, 0x448), + (0x429, 0x449), + (0x42A, 0x44A), + (0x42A, 0x1C86), + (0x42B, 0x44B), + (0x42C, 0x44C), + (0x42D, 0x44D), + (0x42E, 0x44E), + (0x42F, 0x44F), + (0x460, 0x461), + (0x462, 0x463), + (0x462, 0x1C87), + (0x464, 0x465), + (0x466, 0x467), + (0x468, 0x469), + (0x46A, 0x46B), + (0x46C, 0x46D), + (0x46E, 0x46F), + (0x470, 0x471), + (0x472, 0x473), + (0x474, 0x475), + (0x476, 0x477), + (0x478, 0x479), + (0x47A, 0x47B), + (0x47C, 0x47D), + (0x47E, 0x47F), + (0x480, 0x481), + (0x48A, 0x48B), + (0x48C, 0x48D), + (0x48E, 0x48F), + (0x490, 0x491), + (0x492, 0x493), + (0x494, 0x495), + (0x496, 0x497), + (0x498, 0x499), + (0x49A, 0x49B), + (0x49C, 0x49D), + (0x49E, 0x49F), + (0x4A0, 0x4A1), + (0x4A2, 0x4A3), + (0x4A4, 0x4A5), + (0x4A6, 0x4A7), + (0x4A8, 0x4A9), + (0x4AA, 0x4AB), + (0x4AC, 0x4AD), + (0x4AE, 0x4AF), + (0x4B0, 0x4B1), + (0x4B2, 0x4B3), + (0x4B4, 0x4B5), + (0x4B6, 0x4B7), + (0x4B8, 0x4B9), + (0x4BA, 0x4BB), + (0x4BC, 0x4BD), + (0x4BE, 0x4BF), + (0x4C0, 0x4CF), + (0x4C1, 0x4C2), + (0x4C3, 0x4C4), + (0x4C5, 0x4C6), + (0x4C7, 0x4C8), + (0x4C9, 0x4CA), + (0x4CB, 0x4CC), + (0x4CD, 0x4CE), + (0x4D0, 0x4D1), + (0x4D2, 0x4D3), + (0x4D4, 0x4D5), + (0x4D6, 0x4D7), + (0x4D8, 0x4D9), + (0x4DA, 0x4DB), + (0x4DC, 0x4DD), + (0x4DE, 0x4DF), + (0x4E0, 0x4E1), + (0x4E2, 0x4E3), + (0x4E4, 0x4E5), + (0x4E6, 0x4E7), + (0x4E8, 0x4E9), + (0x4EA, 0x4EB), + (0x4EC, 0x4ED), + (0x4EE, 0x4EF), + (0x4F0, 0x4F1), + (0x4F2, 0x4F3), + (0x4F4, 0x4F5), + (0x4F6, 0x4F7), + (0x4F8, 0x4F9), + (0x4FA, 0x4FB), + (0x4FC, 0x4FD), + (0x4FE, 0x4FF), + (0x500, 0x501), + (0x502, 0x503), + (0x504, 0x505), + (0x506, 0x507), + (0x508, 0x509), + (0x50A, 0x50B), + (0x50C, 0x50D), + (0x50E, 0x50F), + (0x510, 0x511), + (0x512, 0x513), + (0x514, 0x515), + (0x516, 0x517), + (0x518, 0x519), + (0x51A, 0x51B), + (0x51C, 0x51D), + (0x51E, 0x51F), + (0x520, 0x521), + (0x522, 0x523), + (0x524, 0x525), + (0x526, 0x527), + (0x528, 0x529), + (0x52A, 0x52B), + (0x52C, 0x52D), + (0x52E, 0x52F), + (0x531, 0x561), + (0x532, 0x562), + (0x533, 0x563), + (0x534, 0x564), + (0x535, 0x565), + (0x536, 0x566), + (0x537, 0x567), + (0x538, 0x568), + (0x539, 0x569), + (0x53A, 0x56A), + (0x53B, 0x56B), + (0x53C, 0x56C), + (0x53D, 0x56D), + (0x53E, 0x56E), + (0x53F, 0x56F), + (0x540, 0x570), + (0x541, 0x571), + (0x542, 0x572), + (0x543, 0x573), + (0x544, 0x574), + (0x545, 0x575), + (0x546, 0x576), + (0x547, 0x577), + (0x548, 0x578), + (0x549, 0x579), + (0x54A, 0x57A), + (0x54B, 0x57B), + (0x54C, 0x57C), + (0x54D, 0x57D), + (0x54E, 0x57E), + (0x54F, 0x57F), + (0x550, 0x580), + (0x551, 0x581), + (0x552, 0x582), + (0x553, 0x583), + (0x554, 0x584), + (0x555, 0x585), + (0x556, 0x586), + (0x10A0, 0x2D00), + (0x10A1, 0x2D01), + (0x10A2, 0x2D02), + (0x10A3, 0x2D03), + (0x10A4, 0x2D04), + (0x10A5, 0x2D05), + (0x10A6, 0x2D06), + (0x10A7, 0x2D07), + (0x10A8, 0x2D08), + (0x10A9, 0x2D09), + (0x10AA, 0x2D0A), + (0x10AB, 0x2D0B), + (0x10AC, 0x2D0C), + (0x10AD, 0x2D0D), + (0x10AE, 0x2D0E), + (0x10AF, 0x2D0F), + (0x10B0, 0x2D10), + (0x10B1, 0x2D11), + (0x10B2, 0x2D12), + (0x10B3, 0x2D13), + (0x10B4, 0x2D14), + (0x10B5, 0x2D15), + (0x10B6, 0x2D16), + (0x10B7, 0x2D17), + (0x10B8, 0x2D18), + (0x10B9, 0x2D19), + (0x10BA, 0x2D1A), + (0x10BB, 0x2D1B), + (0x10BC, 0x2D1C), + (0x10BD, 0x2D1D), + (0x10BE, 0x2D1E), + (0x10BF, 0x2D1F), + (0x10C0, 0x2D20), + (0x10C1, 0x2D21), + (0x10C2, 0x2D22), + (0x10C3, 0x2D23), + (0x10C4, 0x2D24), + (0x10C5, 0x2D25), + (0x10C7, 0x2D27), + (0x10CD, 0x2D2D), + (0x13A0, 0xAB70), + (0x13A1, 0xAB71), + (0x13A2, 0xAB72), + (0x13A3, 0xAB73), + (0x13A4, 0xAB74), + (0x13A5, 0xAB75), + (0x13A6, 0xAB76), + (0x13A7, 0xAB77), + (0x13A8, 0xAB78), + (0x13A9, 0xAB79), + (0x13AA, 0xAB7A), + (0x13AB, 0xAB7B), + (0x13AC, 0xAB7C), + (0x13AD, 0xAB7D), + (0x13AE, 0xAB7E), + (0x13AF, 0xAB7F), + (0x13B0, 0xAB80), + (0x13B1, 0xAB81), + (0x13B2, 0xAB82), + (0x13B3, 0xAB83), + (0x13B4, 0xAB84), + (0x13B5, 0xAB85), + (0x13B6, 0xAB86), + (0x13B7, 0xAB87), + (0x13B8, 0xAB88), + (0x13B9, 0xAB89), + (0x13BA, 0xAB8A), + (0x13BB, 0xAB8B), + (0x13BC, 0xAB8C), + (0x13BD, 0xAB8D), + (0x13BE, 0xAB8E), + (0x13BF, 0xAB8F), + (0x13C0, 0xAB90), + (0x13C1, 0xAB91), + (0x13C2, 0xAB92), + (0x13C3, 0xAB93), + (0x13C4, 0xAB94), + (0x13C5, 0xAB95), + (0x13C6, 0xAB96), + (0x13C7, 0xAB97), + (0x13C8, 0xAB98), + (0x13C9, 0xAB99), + (0x13CA, 0xAB9A), + (0x13CB, 0xAB9B), + (0x13CC, 0xAB9C), + (0x13CD, 0xAB9D), + (0x13CE, 0xAB9E), + (0x13CF, 0xAB9F), + (0x13D0, 0xABA0), + (0x13D1, 0xABA1), + (0x13D2, 0xABA2), + (0x13D3, 0xABA3), + (0x13D4, 0xABA4), + (0x13D5, 0xABA5), + (0x13D6, 0xABA6), + (0x13D7, 0xABA7), + (0x13D8, 0xABA8), + (0x13D9, 0xABA9), + (0x13DA, 0xABAA), + (0x13DB, 0xABAB), + (0x13DC, 0xABAC), + (0x13DD, 0xABAD), + (0x13DE, 0xABAE), + (0x13DF, 0xABAF), + (0x13E0, 0xABB0), + (0x13E1, 0xABB1), + (0x13E2, 0xABB2), + (0x13E3, 0xABB3), + (0x13E4, 0xABB4), + (0x13E5, 0xABB5), + (0x13E6, 0xABB6), + (0x13E7, 0xABB7), + (0x13E8, 0xABB8), + (0x13E9, 0xABB9), + (0x13EA, 0xABBA), + (0x13EB, 0xABBB), + (0x13EC, 0xABBC), + (0x13ED, 0xABBD), + (0x13EE, 0xABBE), + (0x13EF, 0xABBF), + (0x13F0, 0x13F8), + (0x13F1, 0x13F9), + (0x13F2, 0x13FA), + (0x13F3, 0x13FB), + (0x13F4, 0x13FC), + (0x13F5, 0x13FD), + (0x1C89, 0x1C8A), + (0x1C90, 0x10D0), + (0x1C91, 0x10D1), + (0x1C92, 0x10D2), + (0x1C93, 0x10D3), + (0x1C94, 0x10D4), + (0x1C95, 0x10D5), + (0x1C96, 0x10D6), + (0x1C97, 0x10D7), + (0x1C98, 0x10D8), + (0x1C99, 0x10D9), + (0x1C9A, 0x10DA), + (0x1C9B, 0x10DB), + (0x1C9C, 0x10DC), + (0x1C9D, 0x10DD), + (0x1C9E, 0x10DE), + (0x1C9F, 0x10DF), + (0x1CA0, 0x10E0), + (0x1CA1, 0x10E1), + (0x1CA2, 0x10E2), + (0x1CA3, 0x10E3), + (0x1CA4, 0x10E4), + (0x1CA5, 0x10E5), + (0x1CA6, 0x10E6), + (0x1CA7, 0x10E7), + (0x1CA8, 0x10E8), + (0x1CA9, 0x10E9), + (0x1CAA, 0x10EA), + (0x1CAB, 0x10EB), + (0x1CAC, 0x10EC), + (0x1CAD, 0x10ED), + (0x1CAE, 0x10EE), + (0x1CAF, 0x10EF), + (0x1CB0, 0x10F0), + (0x1CB1, 0x10F1), + (0x1CB2, 0x10F2), + (0x1CB3, 0x10F3), + (0x1CB4, 0x10F4), + (0x1CB5, 0x10F5), + (0x1CB6, 0x10F6), + (0x1CB7, 0x10F7), + (0x1CB8, 0x10F8), + (0x1CB9, 0x10F9), + (0x1CBA, 0x10FA), + (0x1CBD, 0x10FD), + (0x1CBE, 0x10FE), + (0x1CBF, 0x10FF), + (0x1E00, 0x1E01), + (0x1E02, 0x1E03), + (0x1E04, 0x1E05), + (0x1E06, 0x1E07), + (0x1E08, 0x1E09), + (0x1E0A, 0x1E0B), + (0x1E0C, 0x1E0D), + (0x1E0E, 0x1E0F), + (0x1E10, 0x1E11), + (0x1E12, 0x1E13), + (0x1E14, 0x1E15), + (0x1E16, 0x1E17), + (0x1E18, 0x1E19), + (0x1E1A, 0x1E1B), + (0x1E1C, 0x1E1D), + (0x1E1E, 0x1E1F), + (0x1E20, 0x1E21), + (0x1E22, 0x1E23), + (0x1E24, 0x1E25), + (0x1E26, 0x1E27), + (0x1E28, 0x1E29), + (0x1E2A, 0x1E2B), + (0x1E2C, 0x1E2D), + (0x1E2E, 0x1E2F), + (0x1E30, 0x1E31), + (0x1E32, 0x1E33), + (0x1E34, 0x1E35), + (0x1E36, 0x1E37), + (0x1E38, 0x1E39), + (0x1E3A, 0x1E3B), + (0x1E3C, 0x1E3D), + (0x1E3E, 0x1E3F), + (0x1E40, 0x1E41), + (0x1E42, 0x1E43), + (0x1E44, 0x1E45), + (0x1E46, 0x1E47), + (0x1E48, 0x1E49), + (0x1E4A, 0x1E4B), + (0x1E4C, 0x1E4D), + (0x1E4E, 0x1E4F), + (0x1E50, 0x1E51), + (0x1E52, 0x1E53), + (0x1E54, 0x1E55), + (0x1E56, 0x1E57), + (0x1E58, 0x1E59), + (0x1E5A, 0x1E5B), + (0x1E5C, 0x1E5D), + (0x1E5E, 0x1E5F), + (0x1E60, 0x1E61), + (0x1E60, 0x1E9B), + (0x1E62, 0x1E63), + (0x1E64, 0x1E65), + (0x1E66, 0x1E67), + (0x1E68, 0x1E69), + (0x1E6A, 0x1E6B), + (0x1E6C, 0x1E6D), + (0x1E6E, 0x1E6F), + (0x1E70, 0x1E71), + (0x1E72, 0x1E73), + (0x1E74, 0x1E75), + (0x1E76, 0x1E77), + (0x1E78, 0x1E79), + (0x1E7A, 0x1E7B), + (0x1E7C, 0x1E7D), + (0x1E7E, 0x1E7F), + (0x1E80, 0x1E81), + (0x1E82, 0x1E83), + (0x1E84, 0x1E85), + (0x1E86, 0x1E87), + (0x1E88, 0x1E89), + (0x1E8A, 0x1E8B), + (0x1E8C, 0x1E8D), + (0x1E8E, 0x1E8F), + (0x1E90, 0x1E91), + (0x1E92, 0x1E93), + (0x1E94, 0x1E95), + (0x1EA0, 0x1EA1), + (0x1EA2, 0x1EA3), + (0x1EA4, 0x1EA5), + (0x1EA6, 0x1EA7), + (0x1EA8, 0x1EA9), + (0x1EAA, 0x1EAB), + (0x1EAC, 0x1EAD), + (0x1EAE, 0x1EAF), + (0x1EB0, 0x1EB1), + (0x1EB2, 0x1EB3), + (0x1EB4, 0x1EB5), + (0x1EB6, 0x1EB7), + (0x1EB8, 0x1EB9), + (0x1EBA, 0x1EBB), + (0x1EBC, 0x1EBD), + (0x1EBE, 0x1EBF), + (0x1EC0, 0x1EC1), + (0x1EC2, 0x1EC3), + (0x1EC4, 0x1EC5), + (0x1EC6, 0x1EC7), + (0x1EC8, 0x1EC9), + (0x1ECA, 0x1ECB), + (0x1ECC, 0x1ECD), + (0x1ECE, 0x1ECF), + (0x1ED0, 0x1ED1), + (0x1ED2, 0x1ED3), + (0x1ED4, 0x1ED5), + (0x1ED6, 0x1ED7), + (0x1ED8, 0x1ED9), + (0x1EDA, 0x1EDB), + (0x1EDC, 0x1EDD), + (0x1EDE, 0x1EDF), + (0x1EE0, 0x1EE1), + (0x1EE2, 0x1EE3), + (0x1EE4, 0x1EE5), + (0x1EE6, 0x1EE7), + (0x1EE8, 0x1EE9), + (0x1EEA, 0x1EEB), + (0x1EEC, 0x1EED), + (0x1EEE, 0x1EEF), + (0x1EF0, 0x1EF1), + (0x1EF2, 0x1EF3), + (0x1EF4, 0x1EF5), + (0x1EF6, 0x1EF7), + (0x1EF8, 0x1EF9), + (0x1EFA, 0x1EFB), + (0x1EFC, 0x1EFD), + (0x1EFE, 0x1EFF), + (0x1F08, 0x1F00), + (0x1F09, 0x1F01), + (0x1F0A, 0x1F02), + (0x1F0B, 0x1F03), + (0x1F0C, 0x1F04), + (0x1F0D, 0x1F05), + (0x1F0E, 0x1F06), + (0x1F0F, 0x1F07), + (0x1F18, 0x1F10), + (0x1F19, 0x1F11), + (0x1F1A, 0x1F12), + (0x1F1B, 0x1F13), + (0x1F1C, 0x1F14), + (0x1F1D, 0x1F15), + (0x1F28, 0x1F20), + (0x1F29, 0x1F21), + (0x1F2A, 0x1F22), + (0x1F2B, 0x1F23), + (0x1F2C, 0x1F24), + (0x1F2D, 0x1F25), + (0x1F2E, 0x1F26), + (0x1F2F, 0x1F27), + (0x1F38, 0x1F30), + (0x1F39, 0x1F31), + (0x1F3A, 0x1F32), + (0x1F3B, 0x1F33), + (0x1F3C, 0x1F34), + (0x1F3D, 0x1F35), + (0x1F3E, 0x1F36), + (0x1F3F, 0x1F37), + (0x1F48, 0x1F40), + (0x1F49, 0x1F41), + (0x1F4A, 0x1F42), + (0x1F4B, 0x1F43), + (0x1F4C, 0x1F44), + (0x1F4D, 0x1F45), + (0x1F59, 0x1F51), + (0x1F5B, 0x1F53), + (0x1F5D, 0x1F55), + (0x1F5F, 0x1F57), + (0x1F68, 0x1F60), + (0x1F69, 0x1F61), + (0x1F6A, 0x1F62), + (0x1F6B, 0x1F63), + (0x1F6C, 0x1F64), + (0x1F6D, 0x1F65), + (0x1F6E, 0x1F66), + (0x1F6F, 0x1F67), + (0x1FB8, 0x1FB0), + (0x1FB9, 0x1FB1), + (0x1FBA, 0x1F70), + (0x1FBB, 0x1F71), + (0x1FC8, 0x1F72), + (0x1FC9, 0x1F73), + (0x1FCA, 0x1F74), + (0x1FCB, 0x1F75), + (0x1FD8, 0x1FD0), + (0x1FD9, 0x1FD1), + (0x1FDA, 0x1F76), + (0x1FDB, 0x1F77), + (0x1FE8, 0x1FE0), + (0x1FE9, 0x1FE1), + (0x1FEA, 0x1F7A), + (0x1FEB, 0x1F7B), + (0x1FEC, 0x1FE5), + (0x1FF8, 0x1F78), + (0x1FF9, 0x1F79), + (0x1FFA, 0x1F7C), + (0x1FFB, 0x1F7D), + (0x2132, 0x214E), + (0x2160, 0x2170), + (0x2161, 0x2171), + (0x2162, 0x2172), + (0x2163, 0x2173), + (0x2164, 0x2174), + (0x2165, 0x2175), + (0x2166, 0x2176), + (0x2167, 0x2177), + (0x2168, 0x2178), + (0x2169, 0x2179), + (0x216A, 0x217A), + (0x216B, 0x217B), + (0x216C, 0x217C), + (0x216D, 0x217D), + (0x216E, 0x217E), + (0x216F, 0x217F), + (0x2183, 0x2184), + (0x24B6, 0x24D0), + (0x24B7, 0x24D1), + (0x24B8, 0x24D2), + (0x24B9, 0x24D3), + (0x24BA, 0x24D4), + (0x24BB, 0x24D5), + (0x24BC, 0x24D6), + (0x24BD, 0x24D7), + (0x24BE, 0x24D8), + (0x24BF, 0x24D9), + (0x24C0, 0x24DA), + (0x24C1, 0x24DB), + (0x24C2, 0x24DC), + (0x24C3, 0x24DD), + (0x24C4, 0x24DE), + (0x24C5, 0x24DF), + (0x24C6, 0x24E0), + (0x24C7, 0x24E1), + (0x24C8, 0x24E2), + (0x24C9, 0x24E3), + (0x24CA, 0x24E4), + (0x24CB, 0x24E5), + (0x24CC, 0x24E6), + (0x24CD, 0x24E7), + (0x24CE, 0x24E8), + (0x24CF, 0x24E9), + (0x2C00, 0x2C30), + (0x2C01, 0x2C31), + (0x2C02, 0x2C32), + (0x2C03, 0x2C33), + (0x2C04, 0x2C34), + (0x2C05, 0x2C35), + (0x2C06, 0x2C36), + (0x2C07, 0x2C37), + (0x2C08, 0x2C38), + (0x2C09, 0x2C39), + (0x2C0A, 0x2C3A), + (0x2C0B, 0x2C3B), + (0x2C0C, 0x2C3C), + (0x2C0D, 0x2C3D), + (0x2C0E, 0x2C3E), + (0x2C0F, 0x2C3F), + (0x2C10, 0x2C40), + (0x2C11, 0x2C41), + (0x2C12, 0x2C42), + (0x2C13, 0x2C43), + (0x2C14, 0x2C44), + (0x2C15, 0x2C45), + (0x2C16, 0x2C46), + (0x2C17, 0x2C47), + (0x2C18, 0x2C48), + (0x2C19, 0x2C49), + (0x2C1A, 0x2C4A), + (0x2C1B, 0x2C4B), + (0x2C1C, 0x2C4C), + (0x2C1D, 0x2C4D), + (0x2C1E, 0x2C4E), + (0x2C1F, 0x2C4F), + (0x2C20, 0x2C50), + (0x2C21, 0x2C51), + (0x2C22, 0x2C52), + (0x2C23, 0x2C53), + (0x2C24, 0x2C54), + (0x2C25, 0x2C55), + (0x2C26, 0x2C56), + (0x2C27, 0x2C57), + (0x2C28, 0x2C58), + (0x2C29, 0x2C59), + (0x2C2A, 0x2C5A), + (0x2C2B, 0x2C5B), + (0x2C2C, 0x2C5C), + (0x2C2D, 0x2C5D), + (0x2C2E, 0x2C5E), + (0x2C2F, 0x2C5F), + (0x2C60, 0x2C61), + (0x2C62, 0x26B), + (0x2C63, 0x1D7D), + (0x2C64, 0x27D), + (0x2C67, 0x2C68), + (0x2C69, 0x2C6A), + (0x2C6B, 0x2C6C), + (0x2C6D, 0x251), + (0x2C6E, 0x271), + (0x2C6F, 0x250), + (0x2C70, 0x252), + (0x2C72, 0x2C73), + (0x2C75, 0x2C76), + (0x2C7E, 0x23F), + (0x2C7F, 0x240), + (0x2C80, 0x2C81), + (0x2C82, 0x2C83), + (0x2C84, 0x2C85), + (0x2C86, 0x2C87), + (0x2C88, 0x2C89), + (0x2C8A, 0x2C8B), + (0x2C8C, 0x2C8D), + (0x2C8E, 0x2C8F), + (0x2C90, 0x2C91), + (0x2C92, 0x2C93), + (0x2C94, 0x2C95), + (0x2C96, 0x2C97), + (0x2C98, 0x2C99), + (0x2C9A, 0x2C9B), + (0x2C9C, 0x2C9D), + (0x2C9E, 0x2C9F), + (0x2CA0, 0x2CA1), + (0x2CA2, 0x2CA3), + (0x2CA4, 0x2CA5), + (0x2CA6, 0x2CA7), + (0x2CA8, 0x2CA9), + (0x2CAA, 0x2CAB), + (0x2CAC, 0x2CAD), + (0x2CAE, 0x2CAF), + (0x2CB0, 0x2CB1), + (0x2CB2, 0x2CB3), + (0x2CB4, 0x2CB5), + (0x2CB6, 0x2CB7), + (0x2CB8, 0x2CB9), + (0x2CBA, 0x2CBB), + (0x2CBC, 0x2CBD), + (0x2CBE, 0x2CBF), + (0x2CC0, 0x2CC1), + (0x2CC2, 0x2CC3), + (0x2CC4, 0x2CC5), + (0x2CC6, 0x2CC7), + (0x2CC8, 0x2CC9), + (0x2CCA, 0x2CCB), + (0x2CCC, 0x2CCD), + (0x2CCE, 0x2CCF), + (0x2CD0, 0x2CD1), + (0x2CD2, 0x2CD3), + (0x2CD4, 0x2CD5), + (0x2CD6, 0x2CD7), + (0x2CD8, 0x2CD9), + (0x2CDA, 0x2CDB), + (0x2CDC, 0x2CDD), + (0x2CDE, 0x2CDF), + (0x2CE0, 0x2CE1), + (0x2CE2, 0x2CE3), + (0x2CEB, 0x2CEC), + (0x2CED, 0x2CEE), + (0x2CF2, 0x2CF3), + (0xA640, 0xA641), + (0xA642, 0xA643), + (0xA644, 0xA645), + (0xA646, 0xA647), + (0xA648, 0xA649), + (0xA64A, 0x1C88), + (0xA64A, 0xA64B), + (0xA64C, 0xA64D), + (0xA64E, 0xA64F), + (0xA650, 0xA651), + (0xA652, 0xA653), + (0xA654, 0xA655), + (0xA656, 0xA657), + (0xA658, 0xA659), + (0xA65A, 0xA65B), + (0xA65C, 0xA65D), + (0xA65E, 0xA65F), + (0xA660, 0xA661), + (0xA662, 0xA663), + (0xA664, 0xA665), + (0xA666, 0xA667), + (0xA668, 0xA669), + (0xA66A, 0xA66B), + (0xA66C, 0xA66D), + (0xA680, 0xA681), + (0xA682, 0xA683), + (0xA684, 0xA685), + (0xA686, 0xA687), + (0xA688, 0xA689), + (0xA68A, 0xA68B), + (0xA68C, 0xA68D), + (0xA68E, 0xA68F), + (0xA690, 0xA691), + (0xA692, 0xA693), + (0xA694, 0xA695), + (0xA696, 0xA697), + (0xA698, 0xA699), + (0xA69A, 0xA69B), + (0xA722, 0xA723), + (0xA724, 0xA725), + (0xA726, 0xA727), + (0xA728, 0xA729), + (0xA72A, 0xA72B), + (0xA72C, 0xA72D), + (0xA72E, 0xA72F), + (0xA732, 0xA733), + (0xA734, 0xA735), + (0xA736, 0xA737), + (0xA738, 0xA739), + (0xA73A, 0xA73B), + (0xA73C, 0xA73D), + (0xA73E, 0xA73F), + (0xA740, 0xA741), + (0xA742, 0xA743), + (0xA744, 0xA745), + (0xA746, 0xA747), + (0xA748, 0xA749), + (0xA74A, 0xA74B), + (0xA74C, 0xA74D), + (0xA74E, 0xA74F), + (0xA750, 0xA751), + (0xA752, 0xA753), + (0xA754, 0xA755), + (0xA756, 0xA757), + (0xA758, 0xA759), + (0xA75A, 0xA75B), + (0xA75C, 0xA75D), + (0xA75E, 0xA75F), + (0xA760, 0xA761), + (0xA762, 0xA763), + (0xA764, 0xA765), + (0xA766, 0xA767), + (0xA768, 0xA769), + (0xA76A, 0xA76B), + (0xA76C, 0xA76D), + (0xA76E, 0xA76F), + (0xA779, 0xA77A), + (0xA77B, 0xA77C), + (0xA77D, 0x1D79), + (0xA77E, 0xA77F), + (0xA780, 0xA781), + (0xA782, 0xA783), + (0xA784, 0xA785), + (0xA786, 0xA787), + (0xA78B, 0xA78C), + (0xA78D, 0x265), + (0xA790, 0xA791), + (0xA792, 0xA793), + (0xA796, 0xA797), + (0xA798, 0xA799), + (0xA79A, 0xA79B), + (0xA79C, 0xA79D), + (0xA79E, 0xA79F), + (0xA7A0, 0xA7A1), + (0xA7A2, 0xA7A3), + (0xA7A4, 0xA7A5), + (0xA7A6, 0xA7A7), + (0xA7A8, 0xA7A9), + (0xA7AA, 0x266), + (0xA7AB, 0x25C), + (0xA7AC, 0x261), + (0xA7AD, 0x26C), + (0xA7AE, 0x26A), + (0xA7B0, 0x29E), + (0xA7B1, 0x287), + (0xA7B2, 0x29D), + (0xA7B3, 0xAB53), + (0xA7B4, 0xA7B5), + (0xA7B6, 0xA7B7), + (0xA7B8, 0xA7B9), + (0xA7BA, 0xA7BB), + (0xA7BC, 0xA7BD), + (0xA7BE, 0xA7BF), + (0xA7C0, 0xA7C1), + (0xA7C2, 0xA7C3), + (0xA7C4, 0xA794), + (0xA7C5, 0x282), + (0xA7C6, 0x1D8E), + (0xA7C7, 0xA7C8), + (0xA7C9, 0xA7CA), + (0xA7CB, 0x264), + (0xA7CC, 0xA7CD), + (0xA7CE, 0xA7CF), + (0xA7D0, 0xA7D1), + (0xA7D2, 0xA7D3), + (0xA7D4, 0xA7D5), + (0xA7D6, 0xA7D7), + (0xA7D8, 0xA7D9), + (0xA7DA, 0xA7DB), + (0xA7DC, 0x19B), + (0xA7DD, 0x277), + (0xA7E2, 0x27C), + (0xA7F5, 0xA7F6), + (0xAB6C, 0xAB4B), + (0xAB6D, 0xAB4C), + (0xFF21, 0xFF41), + (0xFF22, 0xFF42), + (0xFF23, 0xFF43), + (0xFF24, 0xFF44), + (0xFF25, 0xFF45), + (0xFF26, 0xFF46), + (0xFF27, 0xFF47), + (0xFF28, 0xFF48), + (0xFF29, 0xFF49), + (0xFF2A, 0xFF4A), + (0xFF2B, 0xFF4B), + (0xFF2C, 0xFF4C), + (0xFF2D, 0xFF4D), + (0xFF2E, 0xFF4E), + (0xFF2F, 0xFF4F), + (0xFF30, 0xFF50), + (0xFF31, 0xFF51), + (0xFF32, 0xFF52), + (0xFF33, 0xFF53), + (0xFF34, 0xFF54), + (0xFF35, 0xFF55), + (0xFF36, 0xFF56), + (0xFF37, 0xFF57), + (0xFF38, 0xFF58), + (0xFF39, 0xFF59), + (0xFF3A, 0xFF5A), +]; + +/// The general categories, in the order of the index in `GENERAL_CATEGORY`. +pub static CATEGORY_NAMES: [&str; 30] = [ + "Lu", "Ll", "Lt", "Lm", "Lo", "Mn", "Mc", "Me", "Nd", "Nl", "No", "Pc", "Pd", "Ps", "Pe", "Pi", + "Pf", "Po", "Sm", "Sc", "Sk", "So", "Zs", "Zl", "Zp", "Cc", "Cf", "Cs", "Co", "Cn", +]; + +/// General_Category (DerivedGeneralCategory.txt): first, last, index into `CATEGORY_NAMES`. Sorted, disjoint, covers U+0000 to U+10FFFF. +pub static GENERAL_CATEGORY: &[(u32, u32, u8)] = &[ + (0x0, 0x1F, 25), + (0x20, 0x20, 22), + (0x21, 0x23, 17), + (0x24, 0x24, 19), + (0x25, 0x27, 17), + (0x28, 0x28, 13), + (0x29, 0x29, 14), + (0x2A, 0x2A, 17), + (0x2B, 0x2B, 18), + (0x2C, 0x2C, 17), + (0x2D, 0x2D, 12), + (0x2E, 0x2F, 17), + (0x30, 0x39, 8), + (0x3A, 0x3B, 17), + (0x3C, 0x3E, 18), + (0x3F, 0x40, 17), + (0x41, 0x5A, 0), + (0x5B, 0x5B, 13), + (0x5C, 0x5C, 17), + (0x5D, 0x5D, 14), + (0x5E, 0x5E, 20), + (0x5F, 0x5F, 11), + (0x60, 0x60, 20), + (0x61, 0x7A, 1), + (0x7B, 0x7B, 13), + (0x7C, 0x7C, 18), + (0x7D, 0x7D, 14), + (0x7E, 0x7E, 18), + (0x7F, 0x9F, 25), + (0xA0, 0xA0, 22), + (0xA1, 0xA1, 17), + (0xA2, 0xA5, 19), + (0xA6, 0xA6, 21), + (0xA7, 0xA7, 17), + (0xA8, 0xA8, 20), + (0xA9, 0xA9, 21), + (0xAA, 0xAA, 4), + (0xAB, 0xAB, 15), + (0xAC, 0xAC, 18), + (0xAD, 0xAD, 26), + (0xAE, 0xAE, 21), + (0xAF, 0xAF, 20), + (0xB0, 0xB0, 21), + (0xB1, 0xB1, 18), + (0xB2, 0xB3, 10), + (0xB4, 0xB4, 20), + (0xB5, 0xB5, 1), + (0xB6, 0xB7, 17), + (0xB8, 0xB8, 20), + (0xB9, 0xB9, 10), + (0xBA, 0xBA, 4), + (0xBB, 0xBB, 16), + (0xBC, 0xBE, 10), + (0xBF, 0xBF, 17), + (0xC0, 0xD6, 0), + (0xD7, 0xD7, 18), + (0xD8, 0xDE, 0), + (0xDF, 0xF6, 1), + (0xF7, 0xF7, 18), + (0xF8, 0xFF, 1), + (0x100, 0x100, 0), + (0x101, 0x101, 1), + (0x102, 0x102, 0), + (0x103, 0x103, 1), + (0x104, 0x104, 0), + (0x105, 0x105, 1), + (0x106, 0x106, 0), + (0x107, 0x107, 1), + (0x108, 0x108, 0), + (0x109, 0x109, 1), + (0x10A, 0x10A, 0), + (0x10B, 0x10B, 1), + (0x10C, 0x10C, 0), + (0x10D, 0x10D, 1), + (0x10E, 0x10E, 0), + (0x10F, 0x10F, 1), + (0x110, 0x110, 0), + (0x111, 0x111, 1), + (0x112, 0x112, 0), + (0x113, 0x113, 1), + (0x114, 0x114, 0), + (0x115, 0x115, 1), + (0x116, 0x116, 0), + (0x117, 0x117, 1), + (0x118, 0x118, 0), + (0x119, 0x119, 1), + (0x11A, 0x11A, 0), + (0x11B, 0x11B, 1), + (0x11C, 0x11C, 0), + (0x11D, 0x11D, 1), + (0x11E, 0x11E, 0), + (0x11F, 0x11F, 1), + (0x120, 0x120, 0), + (0x121, 0x121, 1), + (0x122, 0x122, 0), + (0x123, 0x123, 1), + (0x124, 0x124, 0), + (0x125, 0x125, 1), + (0x126, 0x126, 0), + (0x127, 0x127, 1), + (0x128, 0x128, 0), + (0x129, 0x129, 1), + (0x12A, 0x12A, 0), + (0x12B, 0x12B, 1), + (0x12C, 0x12C, 0), + (0x12D, 0x12D, 1), + (0x12E, 0x12E, 0), + (0x12F, 0x12F, 1), + (0x130, 0x130, 0), + (0x131, 0x131, 1), + (0x132, 0x132, 0), + (0x133, 0x133, 1), + (0x134, 0x134, 0), + (0x135, 0x135, 1), + (0x136, 0x136, 0), + (0x137, 0x138, 1), + (0x139, 0x139, 0), + (0x13A, 0x13A, 1), + (0x13B, 0x13B, 0), + (0x13C, 0x13C, 1), + (0x13D, 0x13D, 0), + (0x13E, 0x13E, 1), + (0x13F, 0x13F, 0), + (0x140, 0x140, 1), + (0x141, 0x141, 0), + (0x142, 0x142, 1), + (0x143, 0x143, 0), + (0x144, 0x144, 1), + (0x145, 0x145, 0), + (0x146, 0x146, 1), + (0x147, 0x147, 0), + (0x148, 0x149, 1), + (0x14A, 0x14A, 0), + (0x14B, 0x14B, 1), + (0x14C, 0x14C, 0), + (0x14D, 0x14D, 1), + (0x14E, 0x14E, 0), + (0x14F, 0x14F, 1), + (0x150, 0x150, 0), + (0x151, 0x151, 1), + (0x152, 0x152, 0), + (0x153, 0x153, 1), + (0x154, 0x154, 0), + (0x155, 0x155, 1), + (0x156, 0x156, 0), + (0x157, 0x157, 1), + (0x158, 0x158, 0), + (0x159, 0x159, 1), + (0x15A, 0x15A, 0), + (0x15B, 0x15B, 1), + (0x15C, 0x15C, 0), + (0x15D, 0x15D, 1), + (0x15E, 0x15E, 0), + (0x15F, 0x15F, 1), + (0x160, 0x160, 0), + (0x161, 0x161, 1), + (0x162, 0x162, 0), + (0x163, 0x163, 1), + (0x164, 0x164, 0), + (0x165, 0x165, 1), + (0x166, 0x166, 0), + (0x167, 0x167, 1), + (0x168, 0x168, 0), + (0x169, 0x169, 1), + (0x16A, 0x16A, 0), + (0x16B, 0x16B, 1), + (0x16C, 0x16C, 0), + (0x16D, 0x16D, 1), + (0x16E, 0x16E, 0), + (0x16F, 0x16F, 1), + (0x170, 0x170, 0), + (0x171, 0x171, 1), + (0x172, 0x172, 0), + (0x173, 0x173, 1), + (0x174, 0x174, 0), + (0x175, 0x175, 1), + (0x176, 0x176, 0), + (0x177, 0x177, 1), + (0x178, 0x179, 0), + (0x17A, 0x17A, 1), + (0x17B, 0x17B, 0), + (0x17C, 0x17C, 1), + (0x17D, 0x17D, 0), + (0x17E, 0x180, 1), + (0x181, 0x182, 0), + (0x183, 0x183, 1), + (0x184, 0x184, 0), + (0x185, 0x185, 1), + (0x186, 0x187, 0), + (0x188, 0x188, 1), + (0x189, 0x18B, 0), + (0x18C, 0x18D, 1), + (0x18E, 0x191, 0), + (0x192, 0x192, 1), + (0x193, 0x194, 0), + (0x195, 0x195, 1), + (0x196, 0x198, 0), + (0x199, 0x19B, 1), + (0x19C, 0x19D, 0), + (0x19E, 0x19E, 1), + (0x19F, 0x1A0, 0), + (0x1A1, 0x1A1, 1), + (0x1A2, 0x1A2, 0), + (0x1A3, 0x1A3, 1), + (0x1A4, 0x1A4, 0), + (0x1A5, 0x1A5, 1), + (0x1A6, 0x1A7, 0), + (0x1A8, 0x1A8, 1), + (0x1A9, 0x1A9, 0), + (0x1AA, 0x1AB, 1), + (0x1AC, 0x1AC, 0), + (0x1AD, 0x1AD, 1), + (0x1AE, 0x1AF, 0), + (0x1B0, 0x1B0, 1), + (0x1B1, 0x1B3, 0), + (0x1B4, 0x1B4, 1), + (0x1B5, 0x1B5, 0), + (0x1B6, 0x1B6, 1), + (0x1B7, 0x1B8, 0), + (0x1B9, 0x1BA, 1), + (0x1BB, 0x1BB, 4), + (0x1BC, 0x1BC, 0), + (0x1BD, 0x1BF, 1), + (0x1C0, 0x1C3, 4), + (0x1C4, 0x1C4, 0), + (0x1C5, 0x1C5, 2), + (0x1C6, 0x1C6, 1), + (0x1C7, 0x1C7, 0), + (0x1C8, 0x1C8, 2), + (0x1C9, 0x1C9, 1), + (0x1CA, 0x1CA, 0), + (0x1CB, 0x1CB, 2), + (0x1CC, 0x1CC, 1), + (0x1CD, 0x1CD, 0), + (0x1CE, 0x1CE, 1), + (0x1CF, 0x1CF, 0), + (0x1D0, 0x1D0, 1), + (0x1D1, 0x1D1, 0), + (0x1D2, 0x1D2, 1), + (0x1D3, 0x1D3, 0), + (0x1D4, 0x1D4, 1), + (0x1D5, 0x1D5, 0), + (0x1D6, 0x1D6, 1), + (0x1D7, 0x1D7, 0), + (0x1D8, 0x1D8, 1), + (0x1D9, 0x1D9, 0), + (0x1DA, 0x1DA, 1), + (0x1DB, 0x1DB, 0), + (0x1DC, 0x1DD, 1), + (0x1DE, 0x1DE, 0), + (0x1DF, 0x1DF, 1), + (0x1E0, 0x1E0, 0), + (0x1E1, 0x1E1, 1), + (0x1E2, 0x1E2, 0), + (0x1E3, 0x1E3, 1), + (0x1E4, 0x1E4, 0), + (0x1E5, 0x1E5, 1), + (0x1E6, 0x1E6, 0), + (0x1E7, 0x1E7, 1), + (0x1E8, 0x1E8, 0), + (0x1E9, 0x1E9, 1), + (0x1EA, 0x1EA, 0), + (0x1EB, 0x1EB, 1), + (0x1EC, 0x1EC, 0), + (0x1ED, 0x1ED, 1), + (0x1EE, 0x1EE, 0), + (0x1EF, 0x1F0, 1), + (0x1F1, 0x1F1, 0), + (0x1F2, 0x1F2, 2), + (0x1F3, 0x1F3, 1), + (0x1F4, 0x1F4, 0), + (0x1F5, 0x1F5, 1), + (0x1F6, 0x1F8, 0), + (0x1F9, 0x1F9, 1), + (0x1FA, 0x1FA, 0), + (0x1FB, 0x1FB, 1), + (0x1FC, 0x1FC, 0), + (0x1FD, 0x1FD, 1), + (0x1FE, 0x1FE, 0), + (0x1FF, 0x1FF, 1), + (0x200, 0x200, 0), + (0x201, 0x201, 1), + (0x202, 0x202, 0), + (0x203, 0x203, 1), + (0x204, 0x204, 0), + (0x205, 0x205, 1), + (0x206, 0x206, 0), + (0x207, 0x207, 1), + (0x208, 0x208, 0), + (0x209, 0x209, 1), + (0x20A, 0x20A, 0), + (0x20B, 0x20B, 1), + (0x20C, 0x20C, 0), + (0x20D, 0x20D, 1), + (0x20E, 0x20E, 0), + (0x20F, 0x20F, 1), + (0x210, 0x210, 0), + (0x211, 0x211, 1), + (0x212, 0x212, 0), + (0x213, 0x213, 1), + (0x214, 0x214, 0), + (0x215, 0x215, 1), + (0x216, 0x216, 0), + (0x217, 0x217, 1), + (0x218, 0x218, 0), + (0x219, 0x219, 1), + (0x21A, 0x21A, 0), + (0x21B, 0x21B, 1), + (0x21C, 0x21C, 0), + (0x21D, 0x21D, 1), + (0x21E, 0x21E, 0), + (0x21F, 0x21F, 1), + (0x220, 0x220, 0), + (0x221, 0x221, 1), + (0x222, 0x222, 0), + (0x223, 0x223, 1), + (0x224, 0x224, 0), + (0x225, 0x225, 1), + (0x226, 0x226, 0), + (0x227, 0x227, 1), + (0x228, 0x228, 0), + (0x229, 0x229, 1), + (0x22A, 0x22A, 0), + (0x22B, 0x22B, 1), + (0x22C, 0x22C, 0), + (0x22D, 0x22D, 1), + (0x22E, 0x22E, 0), + (0x22F, 0x22F, 1), + (0x230, 0x230, 0), + (0x231, 0x231, 1), + (0x232, 0x232, 0), + (0x233, 0x239, 1), + (0x23A, 0x23B, 0), + (0x23C, 0x23C, 1), + (0x23D, 0x23E, 0), + (0x23F, 0x240, 1), + (0x241, 0x241, 0), + (0x242, 0x242, 1), + (0x243, 0x246, 0), + (0x247, 0x247, 1), + (0x248, 0x248, 0), + (0x249, 0x249, 1), + (0x24A, 0x24A, 0), + (0x24B, 0x24B, 1), + (0x24C, 0x24C, 0), + (0x24D, 0x24D, 1), + (0x24E, 0x24E, 0), + (0x24F, 0x293, 1), + (0x294, 0x295, 4), + (0x296, 0x2AF, 1), + (0x2B0, 0x2C1, 3), + (0x2C2, 0x2C5, 20), + (0x2C6, 0x2D1, 3), + (0x2D2, 0x2DF, 20), + (0x2E0, 0x2E4, 3), + (0x2E5, 0x2EB, 20), + (0x2EC, 0x2EC, 3), + (0x2ED, 0x2ED, 20), + (0x2EE, 0x2EE, 3), + (0x2EF, 0x2FF, 20), + (0x300, 0x36F, 5), + (0x370, 0x370, 0), + (0x371, 0x371, 1), + (0x372, 0x372, 0), + (0x373, 0x373, 1), + (0x374, 0x374, 3), + (0x375, 0x375, 20), + (0x376, 0x376, 0), + (0x377, 0x377, 1), + (0x378, 0x379, 29), + (0x37A, 0x37A, 3), + (0x37B, 0x37D, 1), + (0x37E, 0x37E, 17), + (0x37F, 0x37F, 0), + (0x380, 0x383, 29), + (0x384, 0x385, 20), + (0x386, 0x386, 0), + (0x387, 0x387, 17), + (0x388, 0x38A, 0), + (0x38B, 0x38B, 29), + (0x38C, 0x38C, 0), + (0x38D, 0x38D, 29), + (0x38E, 0x38F, 0), + (0x390, 0x390, 1), + (0x391, 0x3A1, 0), + (0x3A2, 0x3A2, 29), + (0x3A3, 0x3AB, 0), + (0x3AC, 0x3CE, 1), + (0x3CF, 0x3CF, 0), + (0x3D0, 0x3D1, 1), + (0x3D2, 0x3D4, 0), + (0x3D5, 0x3D7, 1), + (0x3D8, 0x3D8, 0), + (0x3D9, 0x3D9, 1), + (0x3DA, 0x3DA, 0), + (0x3DB, 0x3DB, 1), + (0x3DC, 0x3DC, 0), + (0x3DD, 0x3DD, 1), + (0x3DE, 0x3DE, 0), + (0x3DF, 0x3DF, 1), + (0x3E0, 0x3E0, 0), + (0x3E1, 0x3E1, 1), + (0x3E2, 0x3E2, 0), + (0x3E3, 0x3E3, 1), + (0x3E4, 0x3E4, 0), + (0x3E5, 0x3E5, 1), + (0x3E6, 0x3E6, 0), + (0x3E7, 0x3E7, 1), + (0x3E8, 0x3E8, 0), + (0x3E9, 0x3E9, 1), + (0x3EA, 0x3EA, 0), + (0x3EB, 0x3EB, 1), + (0x3EC, 0x3EC, 0), + (0x3ED, 0x3ED, 1), + (0x3EE, 0x3EE, 0), + (0x3EF, 0x3F3, 1), + (0x3F4, 0x3F4, 0), + (0x3F5, 0x3F5, 1), + (0x3F6, 0x3F6, 18), + (0x3F7, 0x3F7, 0), + (0x3F8, 0x3F8, 1), + (0x3F9, 0x3FA, 0), + (0x3FB, 0x3FC, 1), + (0x3FD, 0x42F, 0), + (0x430, 0x45F, 1), + (0x460, 0x460, 0), + (0x461, 0x461, 1), + (0x462, 0x462, 0), + (0x463, 0x463, 1), + (0x464, 0x464, 0), + (0x465, 0x465, 1), + (0x466, 0x466, 0), + (0x467, 0x467, 1), + (0x468, 0x468, 0), + (0x469, 0x469, 1), + (0x46A, 0x46A, 0), + (0x46B, 0x46B, 1), + (0x46C, 0x46C, 0), + (0x46D, 0x46D, 1), + (0x46E, 0x46E, 0), + (0x46F, 0x46F, 1), + (0x470, 0x470, 0), + (0x471, 0x471, 1), + (0x472, 0x472, 0), + (0x473, 0x473, 1), + (0x474, 0x474, 0), + (0x475, 0x475, 1), + (0x476, 0x476, 0), + (0x477, 0x477, 1), + (0x478, 0x478, 0), + (0x479, 0x479, 1), + (0x47A, 0x47A, 0), + (0x47B, 0x47B, 1), + (0x47C, 0x47C, 0), + (0x47D, 0x47D, 1), + (0x47E, 0x47E, 0), + (0x47F, 0x47F, 1), + (0x480, 0x480, 0), + (0x481, 0x481, 1), + (0x482, 0x482, 21), + (0x483, 0x487, 5), + (0x488, 0x489, 7), + (0x48A, 0x48A, 0), + (0x48B, 0x48B, 1), + (0x48C, 0x48C, 0), + (0x48D, 0x48D, 1), + (0x48E, 0x48E, 0), + (0x48F, 0x48F, 1), + (0x490, 0x490, 0), + (0x491, 0x491, 1), + (0x492, 0x492, 0), + (0x493, 0x493, 1), + (0x494, 0x494, 0), + (0x495, 0x495, 1), + (0x496, 0x496, 0), + (0x497, 0x497, 1), + (0x498, 0x498, 0), + (0x499, 0x499, 1), + (0x49A, 0x49A, 0), + (0x49B, 0x49B, 1), + (0x49C, 0x49C, 0), + (0x49D, 0x49D, 1), + (0x49E, 0x49E, 0), + (0x49F, 0x49F, 1), + (0x4A0, 0x4A0, 0), + (0x4A1, 0x4A1, 1), + (0x4A2, 0x4A2, 0), + (0x4A3, 0x4A3, 1), + (0x4A4, 0x4A4, 0), + (0x4A5, 0x4A5, 1), + (0x4A6, 0x4A6, 0), + (0x4A7, 0x4A7, 1), + (0x4A8, 0x4A8, 0), + (0x4A9, 0x4A9, 1), + (0x4AA, 0x4AA, 0), + (0x4AB, 0x4AB, 1), + (0x4AC, 0x4AC, 0), + (0x4AD, 0x4AD, 1), + (0x4AE, 0x4AE, 0), + (0x4AF, 0x4AF, 1), + (0x4B0, 0x4B0, 0), + (0x4B1, 0x4B1, 1), + (0x4B2, 0x4B2, 0), + (0x4B3, 0x4B3, 1), + (0x4B4, 0x4B4, 0), + (0x4B5, 0x4B5, 1), + (0x4B6, 0x4B6, 0), + (0x4B7, 0x4B7, 1), + (0x4B8, 0x4B8, 0), + (0x4B9, 0x4B9, 1), + (0x4BA, 0x4BA, 0), + (0x4BB, 0x4BB, 1), + (0x4BC, 0x4BC, 0), + (0x4BD, 0x4BD, 1), + (0x4BE, 0x4BE, 0), + (0x4BF, 0x4BF, 1), + (0x4C0, 0x4C1, 0), + (0x4C2, 0x4C2, 1), + (0x4C3, 0x4C3, 0), + (0x4C4, 0x4C4, 1), + (0x4C5, 0x4C5, 0), + (0x4C6, 0x4C6, 1), + (0x4C7, 0x4C7, 0), + (0x4C8, 0x4C8, 1), + (0x4C9, 0x4C9, 0), + (0x4CA, 0x4CA, 1), + (0x4CB, 0x4CB, 0), + (0x4CC, 0x4CC, 1), + (0x4CD, 0x4CD, 0), + (0x4CE, 0x4CF, 1), + (0x4D0, 0x4D0, 0), + (0x4D1, 0x4D1, 1), + (0x4D2, 0x4D2, 0), + (0x4D3, 0x4D3, 1), + (0x4D4, 0x4D4, 0), + (0x4D5, 0x4D5, 1), + (0x4D6, 0x4D6, 0), + (0x4D7, 0x4D7, 1), + (0x4D8, 0x4D8, 0), + (0x4D9, 0x4D9, 1), + (0x4DA, 0x4DA, 0), + (0x4DB, 0x4DB, 1), + (0x4DC, 0x4DC, 0), + (0x4DD, 0x4DD, 1), + (0x4DE, 0x4DE, 0), + (0x4DF, 0x4DF, 1), + (0x4E0, 0x4E0, 0), + (0x4E1, 0x4E1, 1), + (0x4E2, 0x4E2, 0), + (0x4E3, 0x4E3, 1), + (0x4E4, 0x4E4, 0), + (0x4E5, 0x4E5, 1), + (0x4E6, 0x4E6, 0), + (0x4E7, 0x4E7, 1), + (0x4E8, 0x4E8, 0), + (0x4E9, 0x4E9, 1), + (0x4EA, 0x4EA, 0), + (0x4EB, 0x4EB, 1), + (0x4EC, 0x4EC, 0), + (0x4ED, 0x4ED, 1), + (0x4EE, 0x4EE, 0), + (0x4EF, 0x4EF, 1), + (0x4F0, 0x4F0, 0), + (0x4F1, 0x4F1, 1), + (0x4F2, 0x4F2, 0), + (0x4F3, 0x4F3, 1), + (0x4F4, 0x4F4, 0), + (0x4F5, 0x4F5, 1), + (0x4F6, 0x4F6, 0), + (0x4F7, 0x4F7, 1), + (0x4F8, 0x4F8, 0), + (0x4F9, 0x4F9, 1), + (0x4FA, 0x4FA, 0), + (0x4FB, 0x4FB, 1), + (0x4FC, 0x4FC, 0), + (0x4FD, 0x4FD, 1), + (0x4FE, 0x4FE, 0), + (0x4FF, 0x4FF, 1), + (0x500, 0x500, 0), + (0x501, 0x501, 1), + (0x502, 0x502, 0), + (0x503, 0x503, 1), + (0x504, 0x504, 0), + (0x505, 0x505, 1), + (0x506, 0x506, 0), + (0x507, 0x507, 1), + (0x508, 0x508, 0), + (0x509, 0x509, 1), + (0x50A, 0x50A, 0), + (0x50B, 0x50B, 1), + (0x50C, 0x50C, 0), + (0x50D, 0x50D, 1), + (0x50E, 0x50E, 0), + (0x50F, 0x50F, 1), + (0x510, 0x510, 0), + (0x511, 0x511, 1), + (0x512, 0x512, 0), + (0x513, 0x513, 1), + (0x514, 0x514, 0), + (0x515, 0x515, 1), + (0x516, 0x516, 0), + (0x517, 0x517, 1), + (0x518, 0x518, 0), + (0x519, 0x519, 1), + (0x51A, 0x51A, 0), + (0x51B, 0x51B, 1), + (0x51C, 0x51C, 0), + (0x51D, 0x51D, 1), + (0x51E, 0x51E, 0), + (0x51F, 0x51F, 1), + (0x520, 0x520, 0), + (0x521, 0x521, 1), + (0x522, 0x522, 0), + (0x523, 0x523, 1), + (0x524, 0x524, 0), + (0x525, 0x525, 1), + (0x526, 0x526, 0), + (0x527, 0x527, 1), + (0x528, 0x528, 0), + (0x529, 0x529, 1), + (0x52A, 0x52A, 0), + (0x52B, 0x52B, 1), + (0x52C, 0x52C, 0), + (0x52D, 0x52D, 1), + (0x52E, 0x52E, 0), + (0x52F, 0x52F, 1), + (0x530, 0x530, 29), + (0x531, 0x556, 0), + (0x557, 0x557, 29), + (0x558, 0x559, 3), + (0x55A, 0x55F, 17), + (0x560, 0x588, 1), + (0x589, 0x589, 17), + (0x58A, 0x58A, 12), + (0x58B, 0x58C, 3), + (0x58D, 0x58E, 21), + (0x58F, 0x58F, 19), + (0x590, 0x590, 29), + (0x591, 0x5BD, 5), + (0x5BE, 0x5BE, 12), + (0x5BF, 0x5BF, 5), + (0x5C0, 0x5C0, 17), + (0x5C1, 0x5C2, 5), + (0x5C3, 0x5C3, 17), + (0x5C4, 0x5C5, 5), + (0x5C6, 0x5C6, 17), + (0x5C7, 0x5C9, 5), + (0x5CA, 0x5CF, 29), + (0x5D0, 0x5EA, 4), + (0x5EB, 0x5EE, 29), + (0x5EF, 0x5F2, 4), + (0x5F3, 0x5F4, 17), + (0x5F5, 0x5FF, 29), + (0x600, 0x605, 26), + (0x606, 0x608, 18), + (0x609, 0x60A, 17), + (0x60B, 0x60B, 19), + (0x60C, 0x60D, 17), + (0x60E, 0x60F, 21), + (0x610, 0x61A, 5), + (0x61B, 0x61B, 17), + (0x61C, 0x61C, 26), + (0x61D, 0x61F, 17), + (0x620, 0x63F, 4), + (0x640, 0x640, 3), + (0x641, 0x64A, 4), + (0x64B, 0x65F, 5), + (0x660, 0x669, 8), + (0x66A, 0x66D, 17), + (0x66E, 0x66F, 4), + (0x670, 0x670, 5), + (0x671, 0x6D3, 4), + (0x6D4, 0x6D4, 17), + (0x6D5, 0x6D5, 4), + (0x6D6, 0x6DC, 5), + (0x6DD, 0x6DD, 26), + (0x6DE, 0x6DE, 21), + (0x6DF, 0x6E4, 5), + (0x6E5, 0x6E6, 3), + (0x6E7, 0x6E8, 5), + (0x6E9, 0x6E9, 21), + (0x6EA, 0x6ED, 5), + (0x6EE, 0x6EF, 4), + (0x6F0, 0x6F9, 8), + (0x6FA, 0x6FC, 4), + (0x6FD, 0x6FE, 21), + (0x6FF, 0x6FF, 4), + (0x700, 0x70D, 17), + (0x70E, 0x70E, 29), + (0x70F, 0x70F, 26), + (0x710, 0x710, 4), + (0x711, 0x711, 5), + (0x712, 0x72F, 4), + (0x730, 0x74A, 5), + (0x74B, 0x74C, 29), + (0x74D, 0x7A5, 4), + (0x7A6, 0x7B0, 5), + (0x7B1, 0x7B1, 4), + (0x7B2, 0x7BF, 29), + (0x7C0, 0x7C9, 8), + (0x7CA, 0x7EA, 4), + (0x7EB, 0x7F3, 5), + (0x7F4, 0x7F5, 3), + (0x7F6, 0x7F6, 21), + (0x7F7, 0x7F9, 17), + (0x7FA, 0x7FA, 3), + (0x7FB, 0x7FC, 29), + (0x7FD, 0x7FD, 5), + (0x7FE, 0x7FF, 19), + (0x800, 0x815, 4), + (0x816, 0x819, 5), + (0x81A, 0x81A, 3), + (0x81B, 0x823, 5), + (0x824, 0x824, 3), + (0x825, 0x827, 5), + (0x828, 0x828, 3), + (0x829, 0x82D, 5), + (0x82E, 0x82F, 29), + (0x830, 0x83E, 17), + (0x83F, 0x83F, 29), + (0x840, 0x858, 4), + (0x859, 0x85B, 5), + (0x85C, 0x85D, 29), + (0x85E, 0x85E, 17), + (0x85F, 0x85F, 29), + (0x860, 0x86A, 4), + (0x86B, 0x86F, 29), + (0x870, 0x887, 4), + (0x888, 0x888, 20), + (0x889, 0x88F, 4), + (0x890, 0x891, 26), + (0x892, 0x896, 29), + (0x897, 0x89F, 5), + (0x8A0, 0x8C8, 4), + (0x8C9, 0x8C9, 3), + (0x8CA, 0x8E1, 5), + (0x8E2, 0x8E2, 26), + (0x8E3, 0x902, 5), + (0x903, 0x903, 6), + (0x904, 0x939, 4), + (0x93A, 0x93A, 5), + (0x93B, 0x93B, 6), + (0x93C, 0x93C, 5), + (0x93D, 0x93D, 4), + (0x93E, 0x940, 6), + (0x941, 0x948, 5), + (0x949, 0x94C, 6), + (0x94D, 0x94D, 5), + (0x94E, 0x94F, 6), + (0x950, 0x950, 4), + (0x951, 0x957, 5), + (0x958, 0x961, 4), + (0x962, 0x963, 5), + (0x964, 0x965, 17), + (0x966, 0x96F, 8), + (0x970, 0x970, 17), + (0x971, 0x971, 3), + (0x972, 0x980, 4), + (0x981, 0x981, 5), + (0x982, 0x983, 6), + (0x984, 0x984, 29), + (0x985, 0x98C, 4), + (0x98D, 0x98E, 29), + (0x98F, 0x990, 4), + (0x991, 0x992, 29), + (0x993, 0x9A8, 4), + (0x9A9, 0x9A9, 29), + (0x9AA, 0x9B0, 4), + (0x9B1, 0x9B1, 29), + (0x9B2, 0x9B2, 4), + (0x9B3, 0x9B5, 29), + (0x9B6, 0x9B9, 4), + (0x9BA, 0x9BB, 29), + (0x9BC, 0x9BC, 5), + (0x9BD, 0x9BD, 4), + (0x9BE, 0x9C0, 6), + (0x9C1, 0x9C4, 5), + (0x9C5, 0x9C6, 29), + (0x9C7, 0x9C8, 6), + (0x9C9, 0x9CA, 29), + (0x9CB, 0x9CC, 6), + (0x9CD, 0x9CD, 5), + (0x9CE, 0x9CE, 4), + (0x9CF, 0x9D6, 29), + (0x9D7, 0x9D7, 6), + (0x9D8, 0x9DB, 29), + (0x9DC, 0x9DD, 4), + (0x9DE, 0x9DE, 29), + (0x9DF, 0x9E1, 4), + (0x9E2, 0x9E3, 5), + (0x9E4, 0x9E5, 29), + (0x9E6, 0x9EF, 8), + (0x9F0, 0x9F1, 4), + (0x9F2, 0x9F3, 19), + (0x9F4, 0x9F9, 10), + (0x9FA, 0x9FA, 21), + (0x9FB, 0x9FB, 19), + (0x9FC, 0x9FC, 4), + (0x9FD, 0x9FD, 17), + (0x9FE, 0x9FE, 5), + (0x9FF, 0xA00, 29), + (0xA01, 0xA02, 5), + (0xA03, 0xA03, 6), + (0xA04, 0xA04, 29), + (0xA05, 0xA0A, 4), + (0xA0B, 0xA0E, 29), + (0xA0F, 0xA10, 4), + (0xA11, 0xA12, 29), + (0xA13, 0xA28, 4), + (0xA29, 0xA29, 29), + (0xA2A, 0xA30, 4), + (0xA31, 0xA31, 29), + (0xA32, 0xA33, 4), + (0xA34, 0xA34, 29), + (0xA35, 0xA36, 4), + (0xA37, 0xA37, 29), + (0xA38, 0xA39, 4), + (0xA3A, 0xA3B, 29), + (0xA3C, 0xA3C, 5), + (0xA3D, 0xA3D, 29), + (0xA3E, 0xA40, 6), + (0xA41, 0xA42, 5), + (0xA43, 0xA46, 29), + (0xA47, 0xA48, 5), + (0xA49, 0xA4A, 29), + (0xA4B, 0xA4D, 5), + (0xA4E, 0xA50, 29), + (0xA51, 0xA51, 5), + (0xA52, 0xA58, 29), + (0xA59, 0xA5C, 4), + (0xA5D, 0xA5D, 29), + (0xA5E, 0xA5E, 4), + (0xA5F, 0xA65, 29), + (0xA66, 0xA6F, 8), + (0xA70, 0xA71, 5), + (0xA72, 0xA74, 4), + (0xA75, 0xA75, 5), + (0xA76, 0xA76, 17), + (0xA77, 0xA80, 29), + (0xA81, 0xA82, 5), + (0xA83, 0xA83, 6), + (0xA84, 0xA84, 29), + (0xA85, 0xA8D, 4), + (0xA8E, 0xA8E, 29), + (0xA8F, 0xA91, 4), + (0xA92, 0xA92, 29), + (0xA93, 0xAA8, 4), + (0xAA9, 0xAA9, 29), + (0xAAA, 0xAB0, 4), + (0xAB1, 0xAB1, 29), + (0xAB2, 0xAB3, 4), + (0xAB4, 0xAB4, 29), + (0xAB5, 0xAB9, 4), + (0xABA, 0xABB, 29), + (0xABC, 0xABC, 5), + (0xABD, 0xABD, 4), + (0xABE, 0xAC0, 6), + (0xAC1, 0xAC5, 5), + (0xAC6, 0xAC6, 29), + (0xAC7, 0xAC8, 5), + (0xAC9, 0xAC9, 6), + (0xACA, 0xACA, 29), + (0xACB, 0xACC, 6), + (0xACD, 0xACD, 5), + (0xACE, 0xACF, 29), + (0xAD0, 0xAD0, 4), + (0xAD1, 0xADF, 29), + (0xAE0, 0xAE1, 4), + (0xAE2, 0xAE3, 5), + (0xAE4, 0xAE5, 29), + (0xAE6, 0xAEF, 8), + (0xAF0, 0xAF0, 17), + (0xAF1, 0xAF1, 19), + (0xAF2, 0xAF8, 29), + (0xAF9, 0xAF9, 4), + (0xAFA, 0xAFF, 5), + (0xB00, 0xB00, 29), + (0xB01, 0xB01, 5), + (0xB02, 0xB03, 6), + (0xB04, 0xB04, 29), + (0xB05, 0xB0C, 4), + (0xB0D, 0xB0E, 29), + (0xB0F, 0xB10, 4), + (0xB11, 0xB12, 29), + (0xB13, 0xB28, 4), + (0xB29, 0xB29, 29), + (0xB2A, 0xB30, 4), + (0xB31, 0xB31, 29), + (0xB32, 0xB33, 4), + (0xB34, 0xB34, 29), + (0xB35, 0xB39, 4), + (0xB3A, 0xB3B, 29), + (0xB3C, 0xB3C, 5), + (0xB3D, 0xB3D, 4), + (0xB3E, 0xB3E, 6), + (0xB3F, 0xB3F, 5), + (0xB40, 0xB40, 6), + (0xB41, 0xB44, 5), + (0xB45, 0xB46, 29), + (0xB47, 0xB48, 6), + (0xB49, 0xB4A, 29), + (0xB4B, 0xB4C, 6), + (0xB4D, 0xB4D, 5), + (0xB4E, 0xB52, 29), + (0xB53, 0xB56, 5), + (0xB57, 0xB57, 6), + (0xB58, 0xB5B, 29), + (0xB5C, 0xB5D, 4), + (0xB5E, 0xB5E, 29), + (0xB5F, 0xB61, 4), + (0xB62, 0xB63, 5), + (0xB64, 0xB65, 29), + (0xB66, 0xB6F, 8), + (0xB70, 0xB70, 21), + (0xB71, 0xB71, 4), + (0xB72, 0xB77, 10), + (0xB78, 0xB81, 29), + (0xB82, 0xB82, 5), + (0xB83, 0xB83, 4), + (0xB84, 0xB84, 29), + (0xB85, 0xB8A, 4), + (0xB8B, 0xB8D, 29), + (0xB8E, 0xB90, 4), + (0xB91, 0xB91, 29), + (0xB92, 0xB95, 4), + (0xB96, 0xB98, 29), + (0xB99, 0xB9A, 4), + (0xB9B, 0xB9B, 29), + (0xB9C, 0xB9C, 4), + (0xB9D, 0xB9D, 29), + (0xB9E, 0xB9F, 4), + (0xBA0, 0xBA2, 29), + (0xBA3, 0xBA4, 4), + (0xBA5, 0xBA7, 29), + (0xBA8, 0xBAA, 4), + (0xBAB, 0xBAD, 29), + (0xBAE, 0xBB9, 4), + (0xBBA, 0xBBD, 29), + (0xBBE, 0xBBF, 6), + (0xBC0, 0xBC0, 5), + (0xBC1, 0xBC2, 6), + (0xBC3, 0xBC5, 29), + (0xBC6, 0xBC8, 6), + (0xBC9, 0xBC9, 29), + (0xBCA, 0xBCC, 6), + (0xBCD, 0xBCD, 5), + (0xBCE, 0xBCF, 29), + (0xBD0, 0xBD0, 4), + (0xBD1, 0xBD6, 29), + (0xBD7, 0xBD7, 6), + (0xBD8, 0xBE5, 29), + (0xBE6, 0xBEF, 8), + (0xBF0, 0xBF2, 10), + (0xBF3, 0xBF8, 21), + (0xBF9, 0xBF9, 19), + (0xBFA, 0xBFA, 21), + (0xBFB, 0xBFF, 29), + (0xC00, 0xC00, 5), + (0xC01, 0xC03, 6), + (0xC04, 0xC04, 5), + (0xC05, 0xC0C, 4), + (0xC0D, 0xC0D, 29), + (0xC0E, 0xC10, 4), + (0xC11, 0xC11, 29), + (0xC12, 0xC28, 4), + (0xC29, 0xC29, 29), + (0xC2A, 0xC39, 4), + (0xC3A, 0xC3B, 29), + (0xC3C, 0xC3C, 5), + (0xC3D, 0xC3D, 4), + (0xC3E, 0xC40, 5), + (0xC41, 0xC44, 6), + (0xC45, 0xC45, 29), + (0xC46, 0xC48, 5), + (0xC49, 0xC49, 29), + (0xC4A, 0xC4D, 5), + (0xC4E, 0xC54, 29), + (0xC55, 0xC56, 5), + (0xC57, 0xC57, 29), + (0xC58, 0xC5A, 4), + (0xC5B, 0xC5B, 29), + (0xC5C, 0xC5D, 4), + (0xC5E, 0xC5F, 29), + (0xC60, 0xC61, 4), + (0xC62, 0xC63, 5), + (0xC64, 0xC65, 29), + (0xC66, 0xC6F, 8), + (0xC70, 0xC76, 29), + (0xC77, 0xC77, 17), + (0xC78, 0xC7E, 10), + (0xC7F, 0xC7F, 21), + (0xC80, 0xC80, 4), + (0xC81, 0xC81, 5), + (0xC82, 0xC83, 6), + (0xC84, 0xC84, 17), + (0xC85, 0xC8C, 4), + (0xC8D, 0xC8D, 29), + (0xC8E, 0xC90, 4), + (0xC91, 0xC91, 29), + (0xC92, 0xCA8, 4), + (0xCA9, 0xCA9, 29), + (0xCAA, 0xCB3, 4), + (0xCB4, 0xCB4, 29), + (0xCB5, 0xCB9, 4), + (0xCBA, 0xCBB, 29), + (0xCBC, 0xCBC, 5), + (0xCBD, 0xCBD, 4), + (0xCBE, 0xCBE, 6), + (0xCBF, 0xCBF, 5), + (0xCC0, 0xCC4, 6), + (0xCC5, 0xCC5, 29), + (0xCC6, 0xCC6, 5), + (0xCC7, 0xCC8, 6), + (0xCC9, 0xCC9, 29), + (0xCCA, 0xCCB, 6), + (0xCCC, 0xCCD, 5), + (0xCCE, 0xCD4, 29), + (0xCD5, 0xCD6, 6), + (0xCD7, 0xCDB, 29), + (0xCDC, 0xCDE, 4), + (0xCDF, 0xCDF, 29), + (0xCE0, 0xCE1, 4), + (0xCE2, 0xCE3, 5), + (0xCE4, 0xCE5, 29), + (0xCE6, 0xCEF, 8), + (0xCF0, 0xCF0, 29), + (0xCF1, 0xCF2, 4), + (0xCF3, 0xCF3, 6), + (0xCF4, 0xCFF, 29), + (0xD00, 0xD01, 5), + (0xD02, 0xD03, 6), + (0xD04, 0xD0C, 4), + (0xD0D, 0xD0D, 29), + (0xD0E, 0xD10, 4), + (0xD11, 0xD11, 29), + (0xD12, 0xD3A, 4), + (0xD3B, 0xD3C, 5), + (0xD3D, 0xD3D, 4), + (0xD3E, 0xD40, 6), + (0xD41, 0xD44, 5), + (0xD45, 0xD45, 29), + (0xD46, 0xD48, 6), + (0xD49, 0xD49, 29), + (0xD4A, 0xD4C, 6), + (0xD4D, 0xD4D, 5), + (0xD4E, 0xD4E, 4), + (0xD4F, 0xD4F, 21), + (0xD50, 0xD53, 29), + (0xD54, 0xD56, 4), + (0xD57, 0xD57, 6), + (0xD58, 0xD5E, 10), + (0xD5F, 0xD61, 4), + (0xD62, 0xD63, 5), + (0xD64, 0xD65, 29), + (0xD66, 0xD6F, 8), + (0xD70, 0xD78, 10), + (0xD79, 0xD79, 21), + (0xD7A, 0xD7F, 4), + (0xD80, 0xD80, 29), + (0xD81, 0xD81, 5), + (0xD82, 0xD83, 6), + (0xD84, 0xD84, 29), + (0xD85, 0xD96, 4), + (0xD97, 0xD99, 29), + (0xD9A, 0xDB1, 4), + (0xDB2, 0xDB2, 29), + (0xDB3, 0xDBB, 4), + (0xDBC, 0xDBC, 29), + (0xDBD, 0xDBD, 4), + (0xDBE, 0xDBF, 29), + (0xDC0, 0xDC6, 4), + (0xDC7, 0xDC9, 29), + (0xDCA, 0xDCA, 5), + (0xDCB, 0xDCE, 29), + (0xDCF, 0xDD1, 6), + (0xDD2, 0xDD4, 5), + (0xDD5, 0xDD5, 29), + (0xDD6, 0xDD6, 5), + (0xDD7, 0xDD7, 29), + (0xDD8, 0xDDF, 6), + (0xDE0, 0xDE5, 29), + (0xDE6, 0xDEF, 8), + (0xDF0, 0xDF1, 29), + (0xDF2, 0xDF3, 6), + (0xDF4, 0xDF4, 17), + (0xDF5, 0xE00, 29), + (0xE01, 0xE30, 4), + (0xE31, 0xE31, 5), + (0xE32, 0xE33, 4), + (0xE34, 0xE3A, 5), + (0xE3B, 0xE3E, 29), + (0xE3F, 0xE3F, 19), + (0xE40, 0xE45, 4), + (0xE46, 0xE46, 3), + (0xE47, 0xE4E, 5), + (0xE4F, 0xE4F, 17), + (0xE50, 0xE59, 8), + (0xE5A, 0xE5B, 17), + (0xE5C, 0xE80, 29), + (0xE81, 0xE82, 4), + (0xE83, 0xE83, 29), + (0xE84, 0xE84, 4), + (0xE85, 0xE85, 29), + (0xE86, 0xE8A, 4), + (0xE8B, 0xE8B, 29), + (0xE8C, 0xEA3, 4), + (0xEA4, 0xEA4, 29), + (0xEA5, 0xEA5, 4), + (0xEA6, 0xEA6, 29), + (0xEA7, 0xEB0, 4), + (0xEB1, 0xEB1, 5), + (0xEB2, 0xEB3, 4), + (0xEB4, 0xEBC, 5), + (0xEBD, 0xEBD, 4), + (0xEBE, 0xEBF, 29), + (0xEC0, 0xEC4, 4), + (0xEC5, 0xEC5, 29), + (0xEC6, 0xEC6, 3), + (0xEC7, 0xEC7, 29), + (0xEC8, 0xECE, 5), + (0xECF, 0xECF, 29), + (0xED0, 0xED9, 8), + (0xEDA, 0xEDB, 29), + (0xEDC, 0xEDF, 4), + (0xEE0, 0xEFF, 29), + (0xF00, 0xF00, 4), + (0xF01, 0xF03, 21), + (0xF04, 0xF12, 17), + (0xF13, 0xF13, 21), + (0xF14, 0xF14, 17), + (0xF15, 0xF17, 21), + (0xF18, 0xF19, 5), + (0xF1A, 0xF1F, 21), + (0xF20, 0xF29, 8), + (0xF2A, 0xF33, 10), + (0xF34, 0xF34, 21), + (0xF35, 0xF35, 5), + (0xF36, 0xF36, 21), + (0xF37, 0xF37, 5), + (0xF38, 0xF38, 21), + (0xF39, 0xF39, 5), + (0xF3A, 0xF3A, 13), + (0xF3B, 0xF3B, 14), + (0xF3C, 0xF3C, 13), + (0xF3D, 0xF3D, 14), + (0xF3E, 0xF3F, 6), + (0xF40, 0xF47, 4), + (0xF48, 0xF48, 29), + (0xF49, 0xF6C, 4), + (0xF6D, 0xF70, 29), + (0xF71, 0xF7E, 5), + (0xF7F, 0xF7F, 6), + (0xF80, 0xF84, 5), + (0xF85, 0xF85, 17), + (0xF86, 0xF87, 5), + (0xF88, 0xF8C, 4), + (0xF8D, 0xF97, 5), + (0xF98, 0xF98, 29), + (0xF99, 0xFBC, 5), + (0xFBD, 0xFBD, 29), + (0xFBE, 0xFC5, 21), + (0xFC6, 0xFC6, 5), + (0xFC7, 0xFCC, 21), + (0xFCD, 0xFCD, 29), + (0xFCE, 0xFCF, 21), + (0xFD0, 0xFD4, 17), + (0xFD5, 0xFD8, 21), + (0xFD9, 0xFDA, 17), + (0xFDB, 0xFFF, 29), + (0x1000, 0x102A, 4), + (0x102B, 0x102C, 6), + (0x102D, 0x1030, 5), + (0x1031, 0x1031, 6), + (0x1032, 0x1037, 5), + (0x1038, 0x1038, 6), + (0x1039, 0x103A, 5), + (0x103B, 0x103C, 6), + (0x103D, 0x103E, 5), + (0x103F, 0x103F, 4), + (0x1040, 0x1049, 8), + (0x104A, 0x104F, 17), + (0x1050, 0x1055, 4), + (0x1056, 0x1057, 6), + (0x1058, 0x1059, 5), + (0x105A, 0x105D, 4), + (0x105E, 0x1060, 5), + (0x1061, 0x1061, 4), + (0x1062, 0x1064, 6), + (0x1065, 0x1066, 4), + (0x1067, 0x106D, 6), + (0x106E, 0x1070, 4), + (0x1071, 0x1074, 5), + (0x1075, 0x1081, 4), + (0x1082, 0x1082, 5), + (0x1083, 0x1084, 6), + (0x1085, 0x1086, 5), + (0x1087, 0x108C, 6), + (0x108D, 0x108D, 5), + (0x108E, 0x108E, 4), + (0x108F, 0x108F, 6), + (0x1090, 0x1099, 8), + (0x109A, 0x109C, 6), + (0x109D, 0x109D, 5), + (0x109E, 0x109F, 21), + (0x10A0, 0x10C5, 0), + (0x10C6, 0x10C6, 29), + (0x10C7, 0x10C7, 0), + (0x10C8, 0x10CC, 29), + (0x10CD, 0x10CD, 0), + (0x10CE, 0x10CF, 29), + (0x10D0, 0x10FA, 1), + (0x10FB, 0x10FB, 17), + (0x10FC, 0x10FC, 3), + (0x10FD, 0x10FF, 1), + (0x1100, 0x1248, 4), + (0x1249, 0x1249, 29), + (0x124A, 0x124D, 4), + (0x124E, 0x124F, 29), + (0x1250, 0x1256, 4), + (0x1257, 0x1257, 29), + (0x1258, 0x1258, 4), + (0x1259, 0x1259, 29), + (0x125A, 0x125D, 4), + (0x125E, 0x125F, 29), + (0x1260, 0x1288, 4), + (0x1289, 0x1289, 29), + (0x128A, 0x128D, 4), + (0x128E, 0x128F, 29), + (0x1290, 0x12B0, 4), + (0x12B1, 0x12B1, 29), + (0x12B2, 0x12B5, 4), + (0x12B6, 0x12B7, 29), + (0x12B8, 0x12BE, 4), + (0x12BF, 0x12BF, 29), + (0x12C0, 0x12C0, 4), + (0x12C1, 0x12C1, 29), + (0x12C2, 0x12C5, 4), + (0x12C6, 0x12C7, 29), + (0x12C8, 0x12D6, 4), + (0x12D7, 0x12D7, 29), + (0x12D8, 0x1310, 4), + (0x1311, 0x1311, 29), + (0x1312, 0x1315, 4), + (0x1316, 0x1317, 29), + (0x1318, 0x135A, 4), + (0x135B, 0x135C, 29), + (0x135D, 0x135F, 5), + (0x1360, 0x1368, 17), + (0x1369, 0x137C, 10), + (0x137D, 0x137F, 29), + (0x1380, 0x138F, 4), + (0x1390, 0x1399, 21), + (0x139A, 0x139F, 29), + (0x13A0, 0x13F5, 0), + (0x13F6, 0x13F7, 29), + (0x13F8, 0x13FD, 1), + (0x13FE, 0x13FF, 29), + (0x1400, 0x1400, 12), + (0x1401, 0x166C, 4), + (0x166D, 0x166D, 21), + (0x166E, 0x166E, 17), + (0x166F, 0x167F, 4), + (0x1680, 0x1680, 22), + (0x1681, 0x169A, 4), + (0x169B, 0x169B, 13), + (0x169C, 0x169C, 14), + (0x169D, 0x169F, 29), + (0x16A0, 0x16EA, 4), + (0x16EB, 0x16ED, 17), + (0x16EE, 0x16F0, 9), + (0x16F1, 0x16F8, 4), + (0x16F9, 0x16FF, 29), + (0x1700, 0x1711, 4), + (0x1712, 0x1714, 5), + (0x1715, 0x1715, 6), + (0x1716, 0x171E, 29), + (0x171F, 0x1731, 4), + (0x1732, 0x1733, 5), + (0x1734, 0x1734, 6), + (0x1735, 0x1736, 17), + (0x1737, 0x173F, 29), + (0x1740, 0x1751, 4), + (0x1752, 0x1753, 5), + (0x1754, 0x175F, 29), + (0x1760, 0x176C, 4), + (0x176D, 0x176D, 29), + (0x176E, 0x1770, 4), + (0x1771, 0x1771, 29), + (0x1772, 0x1773, 5), + (0x1774, 0x177F, 29), + (0x1780, 0x17B3, 4), + (0x17B4, 0x17B5, 5), + (0x17B6, 0x17B6, 6), + (0x17B7, 0x17BD, 5), + (0x17BE, 0x17C5, 6), + (0x17C6, 0x17C6, 5), + (0x17C7, 0x17C8, 6), + (0x17C9, 0x17D3, 5), + (0x17D4, 0x17D6, 17), + (0x17D7, 0x17D7, 3), + (0x17D8, 0x17DA, 17), + (0x17DB, 0x17DB, 19), + (0x17DC, 0x17DC, 4), + (0x17DD, 0x17DD, 5), + (0x17DE, 0x17DF, 29), + (0x17E0, 0x17E9, 8), + (0x17EA, 0x17EF, 29), + (0x17F0, 0x17F9, 10), + (0x17FA, 0x17FF, 29), + (0x1800, 0x1805, 17), + (0x1806, 0x1806, 12), + (0x1807, 0x180A, 17), + (0x180B, 0x180D, 5), + (0x180E, 0x180E, 26), + (0x180F, 0x180F, 5), + (0x1810, 0x1819, 8), + (0x181A, 0x181F, 29), + (0x1820, 0x1842, 4), + (0x1843, 0x1843, 3), + (0x1844, 0x1878, 4), + (0x1879, 0x187F, 29), + (0x1880, 0x1884, 4), + (0x1885, 0x1886, 5), + (0x1887, 0x18A8, 4), + (0x18A9, 0x18A9, 5), + (0x18AA, 0x18AA, 4), + (0x18AB, 0x18AF, 29), + (0x18B0, 0x18F5, 4), + (0x18F6, 0x18FF, 29), + (0x1900, 0x191E, 4), + (0x191F, 0x191F, 29), + (0x1920, 0x1922, 5), + (0x1923, 0x1926, 6), + (0x1927, 0x1928, 5), + (0x1929, 0x192B, 6), + (0x192C, 0x192F, 29), + (0x1930, 0x1931, 6), + (0x1932, 0x1932, 5), + (0x1933, 0x1938, 6), + (0x1939, 0x193B, 5), + (0x193C, 0x193F, 29), + (0x1940, 0x1940, 21), + (0x1941, 0x1943, 29), + (0x1944, 0x1945, 17), + (0x1946, 0x194F, 8), + (0x1950, 0x196D, 4), + (0x196E, 0x196F, 29), + (0x1970, 0x1974, 4), + (0x1975, 0x197F, 29), + (0x1980, 0x19AB, 4), + (0x19AC, 0x19AF, 29), + (0x19B0, 0x19C9, 4), + (0x19CA, 0x19CF, 29), + (0x19D0, 0x19D9, 8), + (0x19DA, 0x19DA, 10), + (0x19DB, 0x19DD, 29), + (0x19DE, 0x19FF, 21), + (0x1A00, 0x1A16, 4), + (0x1A17, 0x1A18, 5), + (0x1A19, 0x1A1A, 6), + (0x1A1B, 0x1A1B, 5), + (0x1A1C, 0x1A1D, 29), + (0x1A1E, 0x1A1F, 17), + (0x1A20, 0x1A54, 4), + (0x1A55, 0x1A55, 6), + (0x1A56, 0x1A56, 5), + (0x1A57, 0x1A57, 6), + (0x1A58, 0x1A5E, 5), + (0x1A5F, 0x1A5F, 29), + (0x1A60, 0x1A60, 5), + (0x1A61, 0x1A61, 6), + (0x1A62, 0x1A62, 5), + (0x1A63, 0x1A64, 6), + (0x1A65, 0x1A6C, 5), + (0x1A6D, 0x1A72, 6), + (0x1A73, 0x1A7C, 5), + (0x1A7D, 0x1A7E, 29), + (0x1A7F, 0x1A7F, 5), + (0x1A80, 0x1A89, 8), + (0x1A8A, 0x1A8F, 29), + (0x1A90, 0x1A99, 8), + (0x1A9A, 0x1A9F, 29), + (0x1AA0, 0x1AA6, 17), + (0x1AA7, 0x1AA7, 3), + (0x1AA8, 0x1AAD, 17), + (0x1AAE, 0x1AAF, 29), + (0x1AB0, 0x1ABD, 5), + (0x1ABE, 0x1ABE, 7), + (0x1ABF, 0x1AF0, 5), + (0x1AF1, 0x1AFF, 29), + (0x1B00, 0x1B03, 5), + (0x1B04, 0x1B04, 6), + (0x1B05, 0x1B33, 4), + (0x1B34, 0x1B34, 5), + (0x1B35, 0x1B35, 6), + (0x1B36, 0x1B3A, 5), + (0x1B3B, 0x1B3B, 6), + (0x1B3C, 0x1B3C, 5), + (0x1B3D, 0x1B41, 6), + (0x1B42, 0x1B42, 5), + (0x1B43, 0x1B44, 6), + (0x1B45, 0x1B4C, 4), + (0x1B4D, 0x1B4D, 29), + (0x1B4E, 0x1B4F, 17), + (0x1B50, 0x1B59, 8), + (0x1B5A, 0x1B60, 17), + (0x1B61, 0x1B6A, 21), + (0x1B6B, 0x1B73, 5), + (0x1B74, 0x1B7C, 21), + (0x1B7D, 0x1B7F, 17), + (0x1B80, 0x1B81, 5), + (0x1B82, 0x1B82, 6), + (0x1B83, 0x1BA0, 4), + (0x1BA1, 0x1BA1, 6), + (0x1BA2, 0x1BA5, 5), + (0x1BA6, 0x1BA7, 6), + (0x1BA8, 0x1BA9, 5), + (0x1BAA, 0x1BAA, 6), + (0x1BAB, 0x1BAD, 5), + (0x1BAE, 0x1BAF, 4), + (0x1BB0, 0x1BB9, 8), + (0x1BBA, 0x1BE5, 4), + (0x1BE6, 0x1BE6, 5), + (0x1BE7, 0x1BE7, 6), + (0x1BE8, 0x1BE9, 5), + (0x1BEA, 0x1BEC, 6), + (0x1BED, 0x1BED, 5), + (0x1BEE, 0x1BEE, 6), + (0x1BEF, 0x1BF1, 5), + (0x1BF2, 0x1BF3, 6), + (0x1BF4, 0x1BFB, 29), + (0x1BFC, 0x1BFF, 17), + (0x1C00, 0x1C23, 4), + (0x1C24, 0x1C2B, 6), + (0x1C2C, 0x1C33, 5), + (0x1C34, 0x1C35, 6), + (0x1C36, 0x1C37, 5), + (0x1C38, 0x1C3A, 29), + (0x1C3B, 0x1C3F, 17), + (0x1C40, 0x1C49, 8), + (0x1C4A, 0x1C4C, 29), + (0x1C4D, 0x1C4F, 4), + (0x1C50, 0x1C59, 8), + (0x1C5A, 0x1C77, 4), + (0x1C78, 0x1C7D, 3), + (0x1C7E, 0x1C7F, 17), + (0x1C80, 0x1C88, 1), + (0x1C89, 0x1C89, 0), + (0x1C8A, 0x1C8A, 1), + (0x1C8B, 0x1C8F, 29), + (0x1C90, 0x1CBA, 0), + (0x1CBB, 0x1CBC, 29), + (0x1CBD, 0x1CBF, 0), + (0x1CC0, 0x1CC7, 17), + (0x1CC8, 0x1CCF, 29), + (0x1CD0, 0x1CD2, 5), + (0x1CD3, 0x1CD3, 17), + (0x1CD4, 0x1CE0, 5), + (0x1CE1, 0x1CE1, 6), + (0x1CE2, 0x1CE8, 5), + (0x1CE9, 0x1CEC, 4), + (0x1CED, 0x1CED, 5), + (0x1CEE, 0x1CF3, 4), + (0x1CF4, 0x1CF4, 5), + (0x1CF5, 0x1CF6, 4), + (0x1CF7, 0x1CF7, 6), + (0x1CF8, 0x1CF9, 5), + (0x1CFA, 0x1CFA, 4), + (0x1CFB, 0x1CFF, 29), + (0x1D00, 0x1D2B, 1), + (0x1D2C, 0x1D6A, 3), + (0x1D6B, 0x1D77, 1), + (0x1D78, 0x1D78, 3), + (0x1D79, 0x1D9A, 1), + (0x1D9B, 0x1DBF, 3), + (0x1DC0, 0x1DFF, 5), + (0x1E00, 0x1E00, 0), + (0x1E01, 0x1E01, 1), + (0x1E02, 0x1E02, 0), + (0x1E03, 0x1E03, 1), + (0x1E04, 0x1E04, 0), + (0x1E05, 0x1E05, 1), + (0x1E06, 0x1E06, 0), + (0x1E07, 0x1E07, 1), + (0x1E08, 0x1E08, 0), + (0x1E09, 0x1E09, 1), + (0x1E0A, 0x1E0A, 0), + (0x1E0B, 0x1E0B, 1), + (0x1E0C, 0x1E0C, 0), + (0x1E0D, 0x1E0D, 1), + (0x1E0E, 0x1E0E, 0), + (0x1E0F, 0x1E0F, 1), + (0x1E10, 0x1E10, 0), + (0x1E11, 0x1E11, 1), + (0x1E12, 0x1E12, 0), + (0x1E13, 0x1E13, 1), + (0x1E14, 0x1E14, 0), + (0x1E15, 0x1E15, 1), + (0x1E16, 0x1E16, 0), + (0x1E17, 0x1E17, 1), + (0x1E18, 0x1E18, 0), + (0x1E19, 0x1E19, 1), + (0x1E1A, 0x1E1A, 0), + (0x1E1B, 0x1E1B, 1), + (0x1E1C, 0x1E1C, 0), + (0x1E1D, 0x1E1D, 1), + (0x1E1E, 0x1E1E, 0), + (0x1E1F, 0x1E1F, 1), + (0x1E20, 0x1E20, 0), + (0x1E21, 0x1E21, 1), + (0x1E22, 0x1E22, 0), + (0x1E23, 0x1E23, 1), + (0x1E24, 0x1E24, 0), + (0x1E25, 0x1E25, 1), + (0x1E26, 0x1E26, 0), + (0x1E27, 0x1E27, 1), + (0x1E28, 0x1E28, 0), + (0x1E29, 0x1E29, 1), + (0x1E2A, 0x1E2A, 0), + (0x1E2B, 0x1E2B, 1), + (0x1E2C, 0x1E2C, 0), + (0x1E2D, 0x1E2D, 1), + (0x1E2E, 0x1E2E, 0), + (0x1E2F, 0x1E2F, 1), + (0x1E30, 0x1E30, 0), + (0x1E31, 0x1E31, 1), + (0x1E32, 0x1E32, 0), + (0x1E33, 0x1E33, 1), + (0x1E34, 0x1E34, 0), + (0x1E35, 0x1E35, 1), + (0x1E36, 0x1E36, 0), + (0x1E37, 0x1E37, 1), + (0x1E38, 0x1E38, 0), + (0x1E39, 0x1E39, 1), + (0x1E3A, 0x1E3A, 0), + (0x1E3B, 0x1E3B, 1), + (0x1E3C, 0x1E3C, 0), + (0x1E3D, 0x1E3D, 1), + (0x1E3E, 0x1E3E, 0), + (0x1E3F, 0x1E3F, 1), + (0x1E40, 0x1E40, 0), + (0x1E41, 0x1E41, 1), + (0x1E42, 0x1E42, 0), + (0x1E43, 0x1E43, 1), + (0x1E44, 0x1E44, 0), + (0x1E45, 0x1E45, 1), + (0x1E46, 0x1E46, 0), + (0x1E47, 0x1E47, 1), + (0x1E48, 0x1E48, 0), + (0x1E49, 0x1E49, 1), + (0x1E4A, 0x1E4A, 0), + (0x1E4B, 0x1E4B, 1), + (0x1E4C, 0x1E4C, 0), + (0x1E4D, 0x1E4D, 1), + (0x1E4E, 0x1E4E, 0), + (0x1E4F, 0x1E4F, 1), + (0x1E50, 0x1E50, 0), + (0x1E51, 0x1E51, 1), + (0x1E52, 0x1E52, 0), + (0x1E53, 0x1E53, 1), + (0x1E54, 0x1E54, 0), + (0x1E55, 0x1E55, 1), + (0x1E56, 0x1E56, 0), + (0x1E57, 0x1E57, 1), + (0x1E58, 0x1E58, 0), + (0x1E59, 0x1E59, 1), + (0x1E5A, 0x1E5A, 0), + (0x1E5B, 0x1E5B, 1), + (0x1E5C, 0x1E5C, 0), + (0x1E5D, 0x1E5D, 1), + (0x1E5E, 0x1E5E, 0), + (0x1E5F, 0x1E5F, 1), + (0x1E60, 0x1E60, 0), + (0x1E61, 0x1E61, 1), + (0x1E62, 0x1E62, 0), + (0x1E63, 0x1E63, 1), + (0x1E64, 0x1E64, 0), + (0x1E65, 0x1E65, 1), + (0x1E66, 0x1E66, 0), + (0x1E67, 0x1E67, 1), + (0x1E68, 0x1E68, 0), + (0x1E69, 0x1E69, 1), + (0x1E6A, 0x1E6A, 0), + (0x1E6B, 0x1E6B, 1), + (0x1E6C, 0x1E6C, 0), + (0x1E6D, 0x1E6D, 1), + (0x1E6E, 0x1E6E, 0), + (0x1E6F, 0x1E6F, 1), + (0x1E70, 0x1E70, 0), + (0x1E71, 0x1E71, 1), + (0x1E72, 0x1E72, 0), + (0x1E73, 0x1E73, 1), + (0x1E74, 0x1E74, 0), + (0x1E75, 0x1E75, 1), + (0x1E76, 0x1E76, 0), + (0x1E77, 0x1E77, 1), + (0x1E78, 0x1E78, 0), + (0x1E79, 0x1E79, 1), + (0x1E7A, 0x1E7A, 0), + (0x1E7B, 0x1E7B, 1), + (0x1E7C, 0x1E7C, 0), + (0x1E7D, 0x1E7D, 1), + (0x1E7E, 0x1E7E, 0), + (0x1E7F, 0x1E7F, 1), + (0x1E80, 0x1E80, 0), + (0x1E81, 0x1E81, 1), + (0x1E82, 0x1E82, 0), + (0x1E83, 0x1E83, 1), + (0x1E84, 0x1E84, 0), + (0x1E85, 0x1E85, 1), + (0x1E86, 0x1E86, 0), + (0x1E87, 0x1E87, 1), + (0x1E88, 0x1E88, 0), + (0x1E89, 0x1E89, 1), + (0x1E8A, 0x1E8A, 0), + (0x1E8B, 0x1E8B, 1), + (0x1E8C, 0x1E8C, 0), + (0x1E8D, 0x1E8D, 1), + (0x1E8E, 0x1E8E, 0), + (0x1E8F, 0x1E8F, 1), + (0x1E90, 0x1E90, 0), + (0x1E91, 0x1E91, 1), + (0x1E92, 0x1E92, 0), + (0x1E93, 0x1E93, 1), + (0x1E94, 0x1E94, 0), + (0x1E95, 0x1E9D, 1), + (0x1E9E, 0x1E9E, 0), + (0x1E9F, 0x1E9F, 1), + (0x1EA0, 0x1EA0, 0), + (0x1EA1, 0x1EA1, 1), + (0x1EA2, 0x1EA2, 0), + (0x1EA3, 0x1EA3, 1), + (0x1EA4, 0x1EA4, 0), + (0x1EA5, 0x1EA5, 1), + (0x1EA6, 0x1EA6, 0), + (0x1EA7, 0x1EA7, 1), + (0x1EA8, 0x1EA8, 0), + (0x1EA9, 0x1EA9, 1), + (0x1EAA, 0x1EAA, 0), + (0x1EAB, 0x1EAB, 1), + (0x1EAC, 0x1EAC, 0), + (0x1EAD, 0x1EAD, 1), + (0x1EAE, 0x1EAE, 0), + (0x1EAF, 0x1EAF, 1), + (0x1EB0, 0x1EB0, 0), + (0x1EB1, 0x1EB1, 1), + (0x1EB2, 0x1EB2, 0), + (0x1EB3, 0x1EB3, 1), + (0x1EB4, 0x1EB4, 0), + (0x1EB5, 0x1EB5, 1), + (0x1EB6, 0x1EB6, 0), + (0x1EB7, 0x1EB7, 1), + (0x1EB8, 0x1EB8, 0), + (0x1EB9, 0x1EB9, 1), + (0x1EBA, 0x1EBA, 0), + (0x1EBB, 0x1EBB, 1), + (0x1EBC, 0x1EBC, 0), + (0x1EBD, 0x1EBD, 1), + (0x1EBE, 0x1EBE, 0), + (0x1EBF, 0x1EBF, 1), + (0x1EC0, 0x1EC0, 0), + (0x1EC1, 0x1EC1, 1), + (0x1EC2, 0x1EC2, 0), + (0x1EC3, 0x1EC3, 1), + (0x1EC4, 0x1EC4, 0), + (0x1EC5, 0x1EC5, 1), + (0x1EC6, 0x1EC6, 0), + (0x1EC7, 0x1EC7, 1), + (0x1EC8, 0x1EC8, 0), + (0x1EC9, 0x1EC9, 1), + (0x1ECA, 0x1ECA, 0), + (0x1ECB, 0x1ECB, 1), + (0x1ECC, 0x1ECC, 0), + (0x1ECD, 0x1ECD, 1), + (0x1ECE, 0x1ECE, 0), + (0x1ECF, 0x1ECF, 1), + (0x1ED0, 0x1ED0, 0), + (0x1ED1, 0x1ED1, 1), + (0x1ED2, 0x1ED2, 0), + (0x1ED3, 0x1ED3, 1), + (0x1ED4, 0x1ED4, 0), + (0x1ED5, 0x1ED5, 1), + (0x1ED6, 0x1ED6, 0), + (0x1ED7, 0x1ED7, 1), + (0x1ED8, 0x1ED8, 0), + (0x1ED9, 0x1ED9, 1), + (0x1EDA, 0x1EDA, 0), + (0x1EDB, 0x1EDB, 1), + (0x1EDC, 0x1EDC, 0), + (0x1EDD, 0x1EDD, 1), + (0x1EDE, 0x1EDE, 0), + (0x1EDF, 0x1EDF, 1), + (0x1EE0, 0x1EE0, 0), + (0x1EE1, 0x1EE1, 1), + (0x1EE2, 0x1EE2, 0), + (0x1EE3, 0x1EE3, 1), + (0x1EE4, 0x1EE4, 0), + (0x1EE5, 0x1EE5, 1), + (0x1EE6, 0x1EE6, 0), + (0x1EE7, 0x1EE7, 1), + (0x1EE8, 0x1EE8, 0), + (0x1EE9, 0x1EE9, 1), + (0x1EEA, 0x1EEA, 0), + (0x1EEB, 0x1EEB, 1), + (0x1EEC, 0x1EEC, 0), + (0x1EED, 0x1EED, 1), + (0x1EEE, 0x1EEE, 0), + (0x1EEF, 0x1EEF, 1), + (0x1EF0, 0x1EF0, 0), + (0x1EF1, 0x1EF1, 1), + (0x1EF2, 0x1EF2, 0), + (0x1EF3, 0x1EF3, 1), + (0x1EF4, 0x1EF4, 0), + (0x1EF5, 0x1EF5, 1), + (0x1EF6, 0x1EF6, 0), + (0x1EF7, 0x1EF7, 1), + (0x1EF8, 0x1EF8, 0), + (0x1EF9, 0x1EF9, 1), + (0x1EFA, 0x1EFA, 0), + (0x1EFB, 0x1EFB, 1), + (0x1EFC, 0x1EFC, 0), + (0x1EFD, 0x1EFD, 1), + (0x1EFE, 0x1EFE, 0), + (0x1EFF, 0x1F07, 1), + (0x1F08, 0x1F0F, 0), + (0x1F10, 0x1F15, 1), + (0x1F16, 0x1F17, 29), + (0x1F18, 0x1F1D, 0), + (0x1F1E, 0x1F1F, 29), + (0x1F20, 0x1F27, 1), + (0x1F28, 0x1F2F, 0), + (0x1F30, 0x1F37, 1), + (0x1F38, 0x1F3F, 0), + (0x1F40, 0x1F45, 1), + (0x1F46, 0x1F47, 29), + (0x1F48, 0x1F4D, 0), + (0x1F4E, 0x1F4F, 29), + (0x1F50, 0x1F57, 1), + (0x1F58, 0x1F58, 29), + (0x1F59, 0x1F59, 0), + (0x1F5A, 0x1F5A, 29), + (0x1F5B, 0x1F5B, 0), + (0x1F5C, 0x1F5C, 29), + (0x1F5D, 0x1F5D, 0), + (0x1F5E, 0x1F5E, 29), + (0x1F5F, 0x1F5F, 0), + (0x1F60, 0x1F67, 1), + (0x1F68, 0x1F6F, 0), + (0x1F70, 0x1F7D, 1), + (0x1F7E, 0x1F7F, 29), + (0x1F80, 0x1F87, 1), + (0x1F88, 0x1F8F, 2), + (0x1F90, 0x1F97, 1), + (0x1F98, 0x1F9F, 2), + (0x1FA0, 0x1FA7, 1), + (0x1FA8, 0x1FAF, 2), + (0x1FB0, 0x1FB4, 1), + (0x1FB5, 0x1FB5, 29), + (0x1FB6, 0x1FB7, 1), + (0x1FB8, 0x1FBB, 0), + (0x1FBC, 0x1FBC, 2), + (0x1FBD, 0x1FBD, 20), + (0x1FBE, 0x1FBE, 1), + (0x1FBF, 0x1FC1, 20), + (0x1FC2, 0x1FC4, 1), + (0x1FC5, 0x1FC5, 29), + (0x1FC6, 0x1FC7, 1), + (0x1FC8, 0x1FCB, 0), + (0x1FCC, 0x1FCC, 2), + (0x1FCD, 0x1FCF, 20), + (0x1FD0, 0x1FD3, 1), + (0x1FD4, 0x1FD5, 29), + (0x1FD6, 0x1FD7, 1), + (0x1FD8, 0x1FDB, 0), + (0x1FDC, 0x1FDC, 29), + (0x1FDD, 0x1FDF, 20), + (0x1FE0, 0x1FE7, 1), + (0x1FE8, 0x1FEC, 0), + (0x1FED, 0x1FEF, 20), + (0x1FF0, 0x1FF1, 29), + (0x1FF2, 0x1FF4, 1), + (0x1FF5, 0x1FF5, 29), + (0x1FF6, 0x1FF7, 1), + (0x1FF8, 0x1FFB, 0), + (0x1FFC, 0x1FFC, 2), + (0x1FFD, 0x1FFE, 20), + (0x1FFF, 0x1FFF, 29), + (0x2000, 0x200A, 22), + (0x200B, 0x200F, 26), + (0x2010, 0x2015, 12), + (0x2016, 0x2017, 17), + (0x2018, 0x2018, 15), + (0x2019, 0x2019, 16), + (0x201A, 0x201A, 13), + (0x201B, 0x201C, 15), + (0x201D, 0x201D, 16), + (0x201E, 0x201E, 13), + (0x201F, 0x201F, 15), + (0x2020, 0x2027, 17), + (0x2028, 0x2028, 23), + (0x2029, 0x2029, 24), + (0x202A, 0x202E, 26), + (0x202F, 0x202F, 22), + (0x2030, 0x2038, 17), + (0x2039, 0x2039, 15), + (0x203A, 0x203A, 16), + (0x203B, 0x203E, 17), + (0x203F, 0x2040, 11), + (0x2041, 0x2043, 17), + (0x2044, 0x2044, 18), + (0x2045, 0x2045, 13), + (0x2046, 0x2046, 14), + (0x2047, 0x2051, 17), + (0x2052, 0x2052, 18), + (0x2053, 0x2053, 17), + (0x2054, 0x2054, 11), + (0x2055, 0x205E, 17), + (0x205F, 0x205F, 22), + (0x2060, 0x2064, 26), + (0x2065, 0x2065, 29), + (0x2066, 0x206F, 26), + (0x2070, 0x2070, 10), + (0x2071, 0x2071, 3), + (0x2072, 0x2073, 29), + (0x2074, 0x2079, 10), + (0x207A, 0x207C, 18), + (0x207D, 0x207D, 13), + (0x207E, 0x207E, 14), + (0x207F, 0x207F, 3), + (0x2080, 0x2089, 10), + (0x208A, 0x208C, 18), + (0x208D, 0x208D, 13), + (0x208E, 0x208E, 14), + (0x208F, 0x209F, 3), + (0x20A0, 0x20C4, 19), + (0x20C5, 0x20CF, 29), + (0x20D0, 0x20DC, 5), + (0x20DD, 0x20E0, 7), + (0x20E1, 0x20E1, 5), + (0x20E2, 0x20E4, 7), + (0x20E5, 0x20F0, 5), + (0x20F1, 0x20FF, 29), + (0x2100, 0x2101, 21), + (0x2102, 0x2102, 0), + (0x2103, 0x2106, 21), + (0x2107, 0x2107, 0), + (0x2108, 0x2109, 21), + (0x210A, 0x210A, 1), + (0x210B, 0x210D, 0), + (0x210E, 0x210F, 1), + (0x2110, 0x2112, 0), + (0x2113, 0x2113, 1), + (0x2114, 0x2114, 21), + (0x2115, 0x2115, 0), + (0x2116, 0x2117, 21), + (0x2118, 0x2118, 18), + (0x2119, 0x211D, 0), + (0x211E, 0x2123, 21), + (0x2124, 0x2124, 0), + (0x2125, 0x2125, 21), + (0x2126, 0x2126, 0), + (0x2127, 0x2127, 21), + (0x2128, 0x2128, 0), + (0x2129, 0x2129, 21), + (0x212A, 0x212D, 0), + (0x212E, 0x212E, 21), + (0x212F, 0x212F, 1), + (0x2130, 0x2133, 0), + (0x2134, 0x2134, 1), + (0x2135, 0x2138, 4), + (0x2139, 0x2139, 1), + (0x213A, 0x213B, 21), + (0x213C, 0x213D, 1), + (0x213E, 0x213F, 0), + (0x2140, 0x2144, 18), + (0x2145, 0x2145, 0), + (0x2146, 0x2149, 1), + (0x214A, 0x214A, 21), + (0x214B, 0x214B, 18), + (0x214C, 0x214D, 21), + (0x214E, 0x214E, 1), + (0x214F, 0x214F, 21), + (0x2150, 0x215F, 10), + (0x2160, 0x2182, 9), + (0x2183, 0x2183, 0), + (0x2184, 0x2184, 1), + (0x2185, 0x2188, 9), + (0x2189, 0x2189, 10), + (0x218A, 0x218B, 21), + (0x218C, 0x218F, 29), + (0x2190, 0x2194, 18), + (0x2195, 0x2199, 21), + (0x219A, 0x219B, 18), + (0x219C, 0x219F, 21), + (0x21A0, 0x21A0, 18), + (0x21A1, 0x21A2, 21), + (0x21A3, 0x21A3, 18), + (0x21A4, 0x21A5, 21), + (0x21A6, 0x21A6, 18), + (0x21A7, 0x21AD, 21), + (0x21AE, 0x21AE, 18), + (0x21AF, 0x21CD, 21), + (0x21CE, 0x21CF, 18), + (0x21D0, 0x21D1, 21), + (0x21D2, 0x21D2, 18), + (0x21D3, 0x21D3, 21), + (0x21D4, 0x21D4, 18), + (0x21D5, 0x21F3, 21), + (0x21F4, 0x22FF, 18), + (0x2300, 0x2307, 21), + (0x2308, 0x2308, 13), + (0x2309, 0x2309, 14), + (0x230A, 0x230A, 13), + (0x230B, 0x230B, 14), + (0x230C, 0x231F, 21), + (0x2320, 0x2321, 18), + (0x2322, 0x2328, 21), + (0x2329, 0x2329, 13), + (0x232A, 0x232A, 14), + (0x232B, 0x237B, 21), + (0x237C, 0x237C, 18), + (0x237D, 0x239A, 21), + (0x239B, 0x23B3, 18), + (0x23B4, 0x23DB, 21), + (0x23DC, 0x23E1, 18), + (0x23E2, 0x2429, 21), + (0x242A, 0x243F, 29), + (0x2440, 0x244A, 21), + (0x244B, 0x245F, 29), + (0x2460, 0x249B, 10), + (0x249C, 0x24E9, 21), + (0x24EA, 0x24FF, 10), + (0x2500, 0x25B6, 21), + (0x25B7, 0x25B7, 18), + (0x25B8, 0x25C0, 21), + (0x25C1, 0x25C1, 18), + (0x25C2, 0x25F7, 21), + (0x25F8, 0x25FF, 18), + (0x2600, 0x266E, 21), + (0x266F, 0x266F, 18), + (0x2670, 0x2767, 21), + (0x2768, 0x2768, 13), + (0x2769, 0x2769, 14), + (0x276A, 0x276A, 13), + (0x276B, 0x276B, 14), + (0x276C, 0x276C, 13), + (0x276D, 0x276D, 14), + (0x276E, 0x276E, 13), + (0x276F, 0x276F, 14), + (0x2770, 0x2770, 13), + (0x2771, 0x2771, 14), + (0x2772, 0x2772, 13), + (0x2773, 0x2773, 14), + (0x2774, 0x2774, 13), + (0x2775, 0x2775, 14), + (0x2776, 0x2793, 10), + (0x2794, 0x27BF, 21), + (0x27C0, 0x27C4, 18), + (0x27C5, 0x27C5, 13), + (0x27C6, 0x27C6, 14), + (0x27C7, 0x27E5, 18), + (0x27E6, 0x27E6, 13), + (0x27E7, 0x27E7, 14), + (0x27E8, 0x27E8, 13), + (0x27E9, 0x27E9, 14), + (0x27EA, 0x27EA, 13), + (0x27EB, 0x27EB, 14), + (0x27EC, 0x27EC, 13), + (0x27ED, 0x27ED, 14), + (0x27EE, 0x27EE, 13), + (0x27EF, 0x27EF, 14), + (0x27F0, 0x27FF, 18), + (0x2800, 0x28FF, 21), + (0x2900, 0x2982, 18), + (0x2983, 0x2983, 13), + (0x2984, 0x2984, 14), + (0x2985, 0x2985, 13), + (0x2986, 0x2986, 14), + (0x2987, 0x2987, 13), + (0x2988, 0x2988, 14), + (0x2989, 0x2989, 13), + (0x298A, 0x298A, 14), + (0x298B, 0x298B, 13), + (0x298C, 0x298C, 14), + (0x298D, 0x298D, 13), + (0x298E, 0x298E, 14), + (0x298F, 0x298F, 13), + (0x2990, 0x2990, 14), + (0x2991, 0x2991, 13), + (0x2992, 0x2992, 14), + (0x2993, 0x2993, 13), + (0x2994, 0x2994, 14), + (0x2995, 0x2995, 13), + (0x2996, 0x2996, 14), + (0x2997, 0x2997, 13), + (0x2998, 0x2998, 14), + (0x2999, 0x29D7, 18), + (0x29D8, 0x29D8, 13), + (0x29D9, 0x29D9, 14), + (0x29DA, 0x29DA, 13), + (0x29DB, 0x29DB, 14), + (0x29DC, 0x29FB, 18), + (0x29FC, 0x29FC, 13), + (0x29FD, 0x29FD, 14), + (0x29FE, 0x2AFF, 18), + (0x2B00, 0x2B2F, 21), + (0x2B30, 0x2B44, 18), + (0x2B45, 0x2B46, 21), + (0x2B47, 0x2B4C, 18), + (0x2B4D, 0x2B73, 21), + (0x2B74, 0x2B75, 29), + (0x2B76, 0x2BFF, 21), + (0x2C00, 0x2C2F, 0), + (0x2C30, 0x2C5F, 1), + (0x2C60, 0x2C60, 0), + (0x2C61, 0x2C61, 1), + (0x2C62, 0x2C64, 0), + (0x2C65, 0x2C66, 1), + (0x2C67, 0x2C67, 0), + (0x2C68, 0x2C68, 1), + (0x2C69, 0x2C69, 0), + (0x2C6A, 0x2C6A, 1), + (0x2C6B, 0x2C6B, 0), + (0x2C6C, 0x2C6C, 1), + (0x2C6D, 0x2C70, 0), + (0x2C71, 0x2C71, 1), + (0x2C72, 0x2C72, 0), + (0x2C73, 0x2C74, 1), + (0x2C75, 0x2C75, 0), + (0x2C76, 0x2C7B, 1), + (0x2C7C, 0x2C7D, 3), + (0x2C7E, 0x2C80, 0), + (0x2C81, 0x2C81, 1), + (0x2C82, 0x2C82, 0), + (0x2C83, 0x2C83, 1), + (0x2C84, 0x2C84, 0), + (0x2C85, 0x2C85, 1), + (0x2C86, 0x2C86, 0), + (0x2C87, 0x2C87, 1), + (0x2C88, 0x2C88, 0), + (0x2C89, 0x2C89, 1), + (0x2C8A, 0x2C8A, 0), + (0x2C8B, 0x2C8B, 1), + (0x2C8C, 0x2C8C, 0), + (0x2C8D, 0x2C8D, 1), + (0x2C8E, 0x2C8E, 0), + (0x2C8F, 0x2C8F, 1), + (0x2C90, 0x2C90, 0), + (0x2C91, 0x2C91, 1), + (0x2C92, 0x2C92, 0), + (0x2C93, 0x2C93, 1), + (0x2C94, 0x2C94, 0), + (0x2C95, 0x2C95, 1), + (0x2C96, 0x2C96, 0), + (0x2C97, 0x2C97, 1), + (0x2C98, 0x2C98, 0), + (0x2C99, 0x2C99, 1), + (0x2C9A, 0x2C9A, 0), + (0x2C9B, 0x2C9B, 1), + (0x2C9C, 0x2C9C, 0), + (0x2C9D, 0x2C9D, 1), + (0x2C9E, 0x2C9E, 0), + (0x2C9F, 0x2C9F, 1), + (0x2CA0, 0x2CA0, 0), + (0x2CA1, 0x2CA1, 1), + (0x2CA2, 0x2CA2, 0), + (0x2CA3, 0x2CA3, 1), + (0x2CA4, 0x2CA4, 0), + (0x2CA5, 0x2CA5, 1), + (0x2CA6, 0x2CA6, 0), + (0x2CA7, 0x2CA7, 1), + (0x2CA8, 0x2CA8, 0), + (0x2CA9, 0x2CA9, 1), + (0x2CAA, 0x2CAA, 0), + (0x2CAB, 0x2CAB, 1), + (0x2CAC, 0x2CAC, 0), + (0x2CAD, 0x2CAD, 1), + (0x2CAE, 0x2CAE, 0), + (0x2CAF, 0x2CAF, 1), + (0x2CB0, 0x2CB0, 0), + (0x2CB1, 0x2CB1, 1), + (0x2CB2, 0x2CB2, 0), + (0x2CB3, 0x2CB3, 1), + (0x2CB4, 0x2CB4, 0), + (0x2CB5, 0x2CB5, 1), + (0x2CB6, 0x2CB6, 0), + (0x2CB7, 0x2CB7, 1), + (0x2CB8, 0x2CB8, 0), + (0x2CB9, 0x2CB9, 1), + (0x2CBA, 0x2CBA, 0), + (0x2CBB, 0x2CBB, 1), + (0x2CBC, 0x2CBC, 0), + (0x2CBD, 0x2CBD, 1), + (0x2CBE, 0x2CBE, 0), + (0x2CBF, 0x2CBF, 1), + (0x2CC0, 0x2CC0, 0), + (0x2CC1, 0x2CC1, 1), + (0x2CC2, 0x2CC2, 0), + (0x2CC3, 0x2CC3, 1), + (0x2CC4, 0x2CC4, 0), + (0x2CC5, 0x2CC5, 1), + (0x2CC6, 0x2CC6, 0), + (0x2CC7, 0x2CC7, 1), + (0x2CC8, 0x2CC8, 0), + (0x2CC9, 0x2CC9, 1), + (0x2CCA, 0x2CCA, 0), + (0x2CCB, 0x2CCB, 1), + (0x2CCC, 0x2CCC, 0), + (0x2CCD, 0x2CCD, 1), + (0x2CCE, 0x2CCE, 0), + (0x2CCF, 0x2CCF, 1), + (0x2CD0, 0x2CD0, 0), + (0x2CD1, 0x2CD1, 1), + (0x2CD2, 0x2CD2, 0), + (0x2CD3, 0x2CD3, 1), + (0x2CD4, 0x2CD4, 0), + (0x2CD5, 0x2CD5, 1), + (0x2CD6, 0x2CD6, 0), + (0x2CD7, 0x2CD7, 1), + (0x2CD8, 0x2CD8, 0), + (0x2CD9, 0x2CD9, 1), + (0x2CDA, 0x2CDA, 0), + (0x2CDB, 0x2CDB, 1), + (0x2CDC, 0x2CDC, 0), + (0x2CDD, 0x2CDD, 1), + (0x2CDE, 0x2CDE, 0), + (0x2CDF, 0x2CDF, 1), + (0x2CE0, 0x2CE0, 0), + (0x2CE1, 0x2CE1, 1), + (0x2CE2, 0x2CE2, 0), + (0x2CE3, 0x2CE4, 1), + (0x2CE5, 0x2CEA, 21), + (0x2CEB, 0x2CEB, 0), + (0x2CEC, 0x2CEC, 1), + (0x2CED, 0x2CED, 0), + (0x2CEE, 0x2CEE, 1), + (0x2CEF, 0x2CF1, 5), + (0x2CF2, 0x2CF2, 0), + (0x2CF3, 0x2CF3, 1), + (0x2CF4, 0x2CF8, 29), + (0x2CF9, 0x2CFC, 17), + (0x2CFD, 0x2CFD, 10), + (0x2CFE, 0x2CFF, 17), + (0x2D00, 0x2D25, 1), + (0x2D26, 0x2D26, 29), + (0x2D27, 0x2D27, 1), + (0x2D28, 0x2D2C, 29), + (0x2D2D, 0x2D2D, 1), + (0x2D2E, 0x2D2F, 29), + (0x2D30, 0x2D67, 4), + (0x2D68, 0x2D6E, 29), + (0x2D6F, 0x2D6F, 3), + (0x2D70, 0x2D70, 17), + (0x2D71, 0x2D7E, 29), + (0x2D7F, 0x2D7F, 5), + (0x2D80, 0x2D96, 4), + (0x2D97, 0x2D9F, 29), + (0x2DA0, 0x2DA6, 4), + (0x2DA7, 0x2DA7, 29), + (0x2DA8, 0x2DAE, 4), + (0x2DAF, 0x2DAF, 29), + (0x2DB0, 0x2DB6, 4), + (0x2DB7, 0x2DB7, 29), + (0x2DB8, 0x2DBE, 4), + (0x2DBF, 0x2DBF, 29), + (0x2DC0, 0x2DC6, 4), + (0x2DC7, 0x2DC7, 29), + (0x2DC8, 0x2DCE, 4), + (0x2DCF, 0x2DCF, 29), + (0x2DD0, 0x2DD6, 4), + (0x2DD7, 0x2DD7, 29), + (0x2DD8, 0x2DDE, 4), + (0x2DDF, 0x2DDF, 29), + (0x2DE0, 0x2DFF, 5), + (0x2E00, 0x2E01, 17), + (0x2E02, 0x2E02, 15), + (0x2E03, 0x2E03, 16), + (0x2E04, 0x2E04, 15), + (0x2E05, 0x2E05, 16), + (0x2E06, 0x2E08, 17), + (0x2E09, 0x2E09, 15), + (0x2E0A, 0x2E0A, 16), + (0x2E0B, 0x2E0B, 17), + (0x2E0C, 0x2E0C, 15), + (0x2E0D, 0x2E0D, 16), + (0x2E0E, 0x2E16, 17), + (0x2E17, 0x2E17, 12), + (0x2E18, 0x2E19, 17), + (0x2E1A, 0x2E1A, 12), + (0x2E1B, 0x2E1B, 17), + (0x2E1C, 0x2E1C, 15), + (0x2E1D, 0x2E1D, 16), + (0x2E1E, 0x2E1F, 17), + (0x2E20, 0x2E20, 15), + (0x2E21, 0x2E21, 16), + (0x2E22, 0x2E22, 13), + (0x2E23, 0x2E23, 14), + (0x2E24, 0x2E24, 13), + (0x2E25, 0x2E25, 14), + (0x2E26, 0x2E26, 13), + (0x2E27, 0x2E27, 14), + (0x2E28, 0x2E28, 13), + (0x2E29, 0x2E29, 14), + (0x2E2A, 0x2E2E, 17), + (0x2E2F, 0x2E2F, 3), + (0x2E30, 0x2E39, 17), + (0x2E3A, 0x2E3B, 12), + (0x2E3C, 0x2E3F, 17), + (0x2E40, 0x2E40, 12), + (0x2E41, 0x2E41, 17), + (0x2E42, 0x2E42, 13), + (0x2E43, 0x2E4F, 17), + (0x2E50, 0x2E51, 21), + (0x2E52, 0x2E54, 17), + (0x2E55, 0x2E55, 13), + (0x2E56, 0x2E56, 14), + (0x2E57, 0x2E57, 13), + (0x2E58, 0x2E58, 14), + (0x2E59, 0x2E59, 13), + (0x2E5A, 0x2E5A, 14), + (0x2E5B, 0x2E5B, 13), + (0x2E5C, 0x2E5C, 14), + (0x2E5D, 0x2E5D, 12), + (0x2E5E, 0x2E5F, 29), + (0x2E60, 0x2E61, 17), + (0x2E62, 0x2E62, 13), + (0x2E63, 0x2E63, 14), + (0x2E64, 0x2E7F, 29), + (0x2E80, 0x2E99, 21), + (0x2E9A, 0x2E9A, 29), + (0x2E9B, 0x2EF3, 21), + (0x2EF4, 0x2EFF, 29), + (0x2F00, 0x2FD5, 21), + (0x2FD6, 0x2FEF, 29), + (0x2FF0, 0x2FFF, 21), + (0x3000, 0x3000, 22), + (0x3001, 0x3003, 17), + (0x3004, 0x3004, 21), + (0x3005, 0x3005, 3), + (0x3006, 0x3006, 4), + (0x3007, 0x3007, 9), + (0x3008, 0x3008, 13), + (0x3009, 0x3009, 14), + (0x300A, 0x300A, 13), + (0x300B, 0x300B, 14), + (0x300C, 0x300C, 13), + (0x300D, 0x300D, 14), + (0x300E, 0x300E, 13), + (0x300F, 0x300F, 14), + (0x3010, 0x3010, 13), + (0x3011, 0x3011, 14), + (0x3012, 0x3013, 21), + (0x3014, 0x3014, 13), + (0x3015, 0x3015, 14), + (0x3016, 0x3016, 13), + (0x3017, 0x3017, 14), + (0x3018, 0x3018, 13), + (0x3019, 0x3019, 14), + (0x301A, 0x301A, 13), + (0x301B, 0x301B, 14), + (0x301C, 0x301C, 12), + (0x301D, 0x301D, 13), + (0x301E, 0x301F, 14), + (0x3020, 0x3020, 21), + (0x3021, 0x3029, 9), + (0x302A, 0x302D, 5), + (0x302E, 0x302F, 6), + (0x3030, 0x3030, 12), + (0x3031, 0x3035, 3), + (0x3036, 0x3037, 21), + (0x3038, 0x303A, 9), + (0x303B, 0x303B, 3), + (0x303C, 0x303C, 4), + (0x303D, 0x303D, 17), + (0x303E, 0x303F, 21), + (0x3040, 0x3040, 29), + (0x3041, 0x3096, 4), + (0x3097, 0x3098, 29), + (0x3099, 0x309A, 5), + (0x309B, 0x309C, 20), + (0x309D, 0x309E, 3), + (0x309F, 0x309F, 4), + (0x30A0, 0x30A0, 12), + (0x30A1, 0x30FA, 4), + (0x30FB, 0x30FB, 17), + (0x30FC, 0x30FE, 3), + (0x30FF, 0x30FF, 4), + (0x3100, 0x3104, 29), + (0x3105, 0x312F, 4), + (0x3130, 0x3130, 29), + (0x3131, 0x318E, 4), + (0x318F, 0x318F, 29), + (0x3190, 0x3191, 21), + (0x3192, 0x3195, 10), + (0x3196, 0x319F, 21), + (0x31A0, 0x31BF, 4), + (0x31C0, 0x31E5, 21), + (0x31E6, 0x31EE, 29), + (0x31EF, 0x31EF, 21), + (0x31F0, 0x31FF, 4), + (0x3200, 0x321E, 21), + (0x321F, 0x321F, 29), + (0x3220, 0x3229, 10), + (0x322A, 0x3247, 21), + (0x3248, 0x324F, 10), + (0x3250, 0x3250, 21), + (0x3251, 0x325F, 10), + (0x3260, 0x327F, 21), + (0x3280, 0x3289, 10), + (0x328A, 0x32B0, 21), + (0x32B1, 0x32BF, 10), + (0x32C0, 0x33FF, 21), + (0x3400, 0x4DBF, 4), + (0x4DC0, 0x4DFF, 21), + (0x4E00, 0xA014, 4), + (0xA015, 0xA015, 3), + (0xA016, 0xA48C, 4), + (0xA48D, 0xA48F, 29), + (0xA490, 0xA4C6, 21), + (0xA4C7, 0xA4CF, 29), + (0xA4D0, 0xA4F7, 4), + (0xA4F8, 0xA4FD, 3), + (0xA4FE, 0xA4FF, 17), + (0xA500, 0xA60B, 4), + (0xA60C, 0xA60C, 3), + (0xA60D, 0xA60F, 17), + (0xA610, 0xA61F, 4), + (0xA620, 0xA629, 8), + (0xA62A, 0xA62B, 4), + (0xA62C, 0xA63F, 29), + (0xA640, 0xA640, 0), + (0xA641, 0xA641, 1), + (0xA642, 0xA642, 0), + (0xA643, 0xA643, 1), + (0xA644, 0xA644, 0), + (0xA645, 0xA645, 1), + (0xA646, 0xA646, 0), + (0xA647, 0xA647, 1), + (0xA648, 0xA648, 0), + (0xA649, 0xA649, 1), + (0xA64A, 0xA64A, 0), + (0xA64B, 0xA64B, 1), + (0xA64C, 0xA64C, 0), + (0xA64D, 0xA64D, 1), + (0xA64E, 0xA64E, 0), + (0xA64F, 0xA64F, 1), + (0xA650, 0xA650, 0), + (0xA651, 0xA651, 1), + (0xA652, 0xA652, 0), + (0xA653, 0xA653, 1), + (0xA654, 0xA654, 0), + (0xA655, 0xA655, 1), + (0xA656, 0xA656, 0), + (0xA657, 0xA657, 1), + (0xA658, 0xA658, 0), + (0xA659, 0xA659, 1), + (0xA65A, 0xA65A, 0), + (0xA65B, 0xA65B, 1), + (0xA65C, 0xA65C, 0), + (0xA65D, 0xA65D, 1), + (0xA65E, 0xA65E, 0), + (0xA65F, 0xA65F, 1), + (0xA660, 0xA660, 0), + (0xA661, 0xA661, 1), + (0xA662, 0xA662, 0), + (0xA663, 0xA663, 1), + (0xA664, 0xA664, 0), + (0xA665, 0xA665, 1), + (0xA666, 0xA666, 0), + (0xA667, 0xA667, 1), + (0xA668, 0xA668, 0), + (0xA669, 0xA669, 1), + (0xA66A, 0xA66A, 0), + (0xA66B, 0xA66B, 1), + (0xA66C, 0xA66C, 0), + (0xA66D, 0xA66D, 1), + (0xA66E, 0xA66E, 4), + (0xA66F, 0xA66F, 5), + (0xA670, 0xA672, 7), + (0xA673, 0xA673, 17), + (0xA674, 0xA67D, 5), + (0xA67E, 0xA67E, 17), + (0xA67F, 0xA67F, 3), + (0xA680, 0xA680, 0), + (0xA681, 0xA681, 1), + (0xA682, 0xA682, 0), + (0xA683, 0xA683, 1), + (0xA684, 0xA684, 0), + (0xA685, 0xA685, 1), + (0xA686, 0xA686, 0), + (0xA687, 0xA687, 1), + (0xA688, 0xA688, 0), + (0xA689, 0xA689, 1), + (0xA68A, 0xA68A, 0), + (0xA68B, 0xA68B, 1), + (0xA68C, 0xA68C, 0), + (0xA68D, 0xA68D, 1), + (0xA68E, 0xA68E, 0), + (0xA68F, 0xA68F, 1), + (0xA690, 0xA690, 0), + (0xA691, 0xA691, 1), + (0xA692, 0xA692, 0), + (0xA693, 0xA693, 1), + (0xA694, 0xA694, 0), + (0xA695, 0xA695, 1), + (0xA696, 0xA696, 0), + (0xA697, 0xA697, 1), + (0xA698, 0xA698, 0), + (0xA699, 0xA699, 1), + (0xA69A, 0xA69A, 0), + (0xA69B, 0xA69B, 1), + (0xA69C, 0xA69D, 3), + (0xA69E, 0xA69F, 5), + (0xA6A0, 0xA6E5, 4), + (0xA6E6, 0xA6EF, 9), + (0xA6F0, 0xA6F1, 5), + (0xA6F2, 0xA6F7, 17), + (0xA6F8, 0xA6FF, 29), + (0xA700, 0xA716, 20), + (0xA717, 0xA71F, 3), + (0xA720, 0xA721, 20), + (0xA722, 0xA722, 0), + (0xA723, 0xA723, 1), + (0xA724, 0xA724, 0), + (0xA725, 0xA725, 1), + (0xA726, 0xA726, 0), + (0xA727, 0xA727, 1), + (0xA728, 0xA728, 0), + (0xA729, 0xA729, 1), + (0xA72A, 0xA72A, 0), + (0xA72B, 0xA72B, 1), + (0xA72C, 0xA72C, 0), + (0xA72D, 0xA72D, 1), + (0xA72E, 0xA72E, 0), + (0xA72F, 0xA731, 1), + (0xA732, 0xA732, 0), + (0xA733, 0xA733, 1), + (0xA734, 0xA734, 0), + (0xA735, 0xA735, 1), + (0xA736, 0xA736, 0), + (0xA737, 0xA737, 1), + (0xA738, 0xA738, 0), + (0xA739, 0xA739, 1), + (0xA73A, 0xA73A, 0), + (0xA73B, 0xA73B, 1), + (0xA73C, 0xA73C, 0), + (0xA73D, 0xA73D, 1), + (0xA73E, 0xA73E, 0), + (0xA73F, 0xA73F, 1), + (0xA740, 0xA740, 0), + (0xA741, 0xA741, 1), + (0xA742, 0xA742, 0), + (0xA743, 0xA743, 1), + (0xA744, 0xA744, 0), + (0xA745, 0xA745, 1), + (0xA746, 0xA746, 0), + (0xA747, 0xA747, 1), + (0xA748, 0xA748, 0), + (0xA749, 0xA749, 1), + (0xA74A, 0xA74A, 0), + (0xA74B, 0xA74B, 1), + (0xA74C, 0xA74C, 0), + (0xA74D, 0xA74D, 1), + (0xA74E, 0xA74E, 0), + (0xA74F, 0xA74F, 1), + (0xA750, 0xA750, 0), + (0xA751, 0xA751, 1), + (0xA752, 0xA752, 0), + (0xA753, 0xA753, 1), + (0xA754, 0xA754, 0), + (0xA755, 0xA755, 1), + (0xA756, 0xA756, 0), + (0xA757, 0xA757, 1), + (0xA758, 0xA758, 0), + (0xA759, 0xA759, 1), + (0xA75A, 0xA75A, 0), + (0xA75B, 0xA75B, 1), + (0xA75C, 0xA75C, 0), + (0xA75D, 0xA75D, 1), + (0xA75E, 0xA75E, 0), + (0xA75F, 0xA75F, 1), + (0xA760, 0xA760, 0), + (0xA761, 0xA761, 1), + (0xA762, 0xA762, 0), + (0xA763, 0xA763, 1), + (0xA764, 0xA764, 0), + (0xA765, 0xA765, 1), + (0xA766, 0xA766, 0), + (0xA767, 0xA767, 1), + (0xA768, 0xA768, 0), + (0xA769, 0xA769, 1), + (0xA76A, 0xA76A, 0), + (0xA76B, 0xA76B, 1), + (0xA76C, 0xA76C, 0), + (0xA76D, 0xA76D, 1), + (0xA76E, 0xA76E, 0), + (0xA76F, 0xA76F, 1), + (0xA770, 0xA770, 3), + (0xA771, 0xA778, 1), + (0xA779, 0xA779, 0), + (0xA77A, 0xA77A, 1), + (0xA77B, 0xA77B, 0), + (0xA77C, 0xA77C, 1), + (0xA77D, 0xA77E, 0), + (0xA77F, 0xA77F, 1), + (0xA780, 0xA780, 0), + (0xA781, 0xA781, 1), + (0xA782, 0xA782, 0), + (0xA783, 0xA783, 1), + (0xA784, 0xA784, 0), + (0xA785, 0xA785, 1), + (0xA786, 0xA786, 0), + (0xA787, 0xA787, 1), + (0xA788, 0xA788, 3), + (0xA789, 0xA78A, 20), + (0xA78B, 0xA78B, 0), + (0xA78C, 0xA78C, 1), + (0xA78D, 0xA78D, 0), + (0xA78E, 0xA78E, 1), + (0xA78F, 0xA78F, 4), + (0xA790, 0xA790, 0), + (0xA791, 0xA791, 1), + (0xA792, 0xA792, 0), + (0xA793, 0xA795, 1), + (0xA796, 0xA796, 0), + (0xA797, 0xA797, 1), + (0xA798, 0xA798, 0), + (0xA799, 0xA799, 1), + (0xA79A, 0xA79A, 0), + (0xA79B, 0xA79B, 1), + (0xA79C, 0xA79C, 0), + (0xA79D, 0xA79D, 1), + (0xA79E, 0xA79E, 0), + (0xA79F, 0xA79F, 1), + (0xA7A0, 0xA7A0, 0), + (0xA7A1, 0xA7A1, 1), + (0xA7A2, 0xA7A2, 0), + (0xA7A3, 0xA7A3, 1), + (0xA7A4, 0xA7A4, 0), + (0xA7A5, 0xA7A5, 1), + (0xA7A6, 0xA7A6, 0), + (0xA7A7, 0xA7A7, 1), + (0xA7A8, 0xA7A8, 0), + (0xA7A9, 0xA7A9, 1), + (0xA7AA, 0xA7AE, 0), + (0xA7AF, 0xA7AF, 1), + (0xA7B0, 0xA7B4, 0), + (0xA7B5, 0xA7B5, 1), + (0xA7B6, 0xA7B6, 0), + (0xA7B7, 0xA7B7, 1), + (0xA7B8, 0xA7B8, 0), + (0xA7B9, 0xA7B9, 1), + (0xA7BA, 0xA7BA, 0), + (0xA7BB, 0xA7BB, 1), + (0xA7BC, 0xA7BC, 0), + (0xA7BD, 0xA7BD, 1), + (0xA7BE, 0xA7BE, 0), + (0xA7BF, 0xA7BF, 1), + (0xA7C0, 0xA7C0, 0), + (0xA7C1, 0xA7C1, 1), + (0xA7C2, 0xA7C2, 0), + (0xA7C3, 0xA7C3, 1), + (0xA7C4, 0xA7C7, 0), + (0xA7C8, 0xA7C8, 1), + (0xA7C9, 0xA7C9, 0), + (0xA7CA, 0xA7CA, 1), + (0xA7CB, 0xA7CC, 0), + (0xA7CD, 0xA7CD, 1), + (0xA7CE, 0xA7CE, 0), + (0xA7CF, 0xA7CF, 1), + (0xA7D0, 0xA7D0, 0), + (0xA7D1, 0xA7D1, 1), + (0xA7D2, 0xA7D2, 0), + (0xA7D3, 0xA7D3, 1), + (0xA7D4, 0xA7D4, 0), + (0xA7D5, 0xA7D5, 1), + (0xA7D6, 0xA7D6, 0), + (0xA7D7, 0xA7D7, 1), + (0xA7D8, 0xA7D8, 0), + (0xA7D9, 0xA7D9, 1), + (0xA7DA, 0xA7DA, 0), + (0xA7DB, 0xA7DB, 1), + (0xA7DC, 0xA7DD, 0), + (0xA7DE, 0xA7E1, 29), + (0xA7E2, 0xA7E2, 0), + (0xA7E3, 0xA7F0, 29), + (0xA7F1, 0xA7F4, 3), + (0xA7F5, 0xA7F5, 0), + (0xA7F6, 0xA7F6, 1), + (0xA7F7, 0xA7F7, 4), + (0xA7F8, 0xA7F9, 3), + (0xA7FA, 0xA7FA, 1), + (0xA7FB, 0xA801, 4), + (0xA802, 0xA802, 5), + (0xA803, 0xA805, 4), + (0xA806, 0xA806, 5), + (0xA807, 0xA80A, 4), + (0xA80B, 0xA80B, 5), + (0xA80C, 0xA822, 4), + (0xA823, 0xA824, 6), + (0xA825, 0xA826, 5), + (0xA827, 0xA827, 6), + (0xA828, 0xA82B, 21), + (0xA82C, 0xA82C, 5), + (0xA82D, 0xA82F, 29), + (0xA830, 0xA835, 10), + (0xA836, 0xA837, 21), + (0xA838, 0xA838, 19), + (0xA839, 0xA839, 21), + (0xA83A, 0xA83F, 29), + (0xA840, 0xA873, 4), + (0xA874, 0xA877, 17), + (0xA878, 0xA87F, 29), + (0xA880, 0xA881, 6), + (0xA882, 0xA8B3, 4), + (0xA8B4, 0xA8C3, 6), + (0xA8C4, 0xA8C5, 5), + (0xA8C6, 0xA8CD, 29), + (0xA8CE, 0xA8CF, 17), + (0xA8D0, 0xA8D9, 8), + (0xA8DA, 0xA8DF, 29), + (0xA8E0, 0xA8F1, 5), + (0xA8F2, 0xA8F7, 4), + (0xA8F8, 0xA8FA, 17), + (0xA8FB, 0xA8FB, 4), + (0xA8FC, 0xA8FC, 17), + (0xA8FD, 0xA8FE, 4), + (0xA8FF, 0xA8FF, 5), + (0xA900, 0xA909, 8), + (0xA90A, 0xA925, 4), + (0xA926, 0xA92D, 5), + (0xA92E, 0xA92F, 17), + (0xA930, 0xA946, 4), + (0xA947, 0xA951, 5), + (0xA952, 0xA953, 6), + (0xA954, 0xA95E, 29), + (0xA95F, 0xA95F, 17), + (0xA960, 0xA97C, 4), + (0xA97D, 0xA97F, 29), + (0xA980, 0xA982, 5), + (0xA983, 0xA983, 6), + (0xA984, 0xA9B2, 4), + (0xA9B3, 0xA9B3, 5), + (0xA9B4, 0xA9B5, 6), + (0xA9B6, 0xA9B9, 5), + (0xA9BA, 0xA9BB, 6), + (0xA9BC, 0xA9BD, 5), + (0xA9BE, 0xA9C0, 6), + (0xA9C1, 0xA9CD, 17), + (0xA9CE, 0xA9CE, 29), + (0xA9CF, 0xA9CF, 3), + (0xA9D0, 0xA9D9, 8), + (0xA9DA, 0xA9DD, 29), + (0xA9DE, 0xA9DF, 17), + (0xA9E0, 0xA9E4, 4), + (0xA9E5, 0xA9E5, 5), + (0xA9E6, 0xA9E6, 3), + (0xA9E7, 0xA9EF, 4), + (0xA9F0, 0xA9F9, 8), + (0xA9FA, 0xA9FE, 4), + (0xA9FF, 0xA9FF, 29), + (0xAA00, 0xAA28, 4), + (0xAA29, 0xAA2E, 5), + (0xAA2F, 0xAA30, 6), + (0xAA31, 0xAA32, 5), + (0xAA33, 0xAA34, 6), + (0xAA35, 0xAA36, 5), + (0xAA37, 0xAA3F, 29), + (0xAA40, 0xAA42, 4), + (0xAA43, 0xAA43, 5), + (0xAA44, 0xAA4B, 4), + (0xAA4C, 0xAA4C, 5), + (0xAA4D, 0xAA4D, 6), + (0xAA4E, 0xAA4F, 29), + (0xAA50, 0xAA59, 8), + (0xAA5A, 0xAA5B, 29), + (0xAA5C, 0xAA5F, 17), + (0xAA60, 0xAA6F, 4), + (0xAA70, 0xAA70, 3), + (0xAA71, 0xAA76, 4), + (0xAA77, 0xAA79, 21), + (0xAA7A, 0xAA7A, 4), + (0xAA7B, 0xAA7B, 6), + (0xAA7C, 0xAA7C, 5), + (0xAA7D, 0xAA7D, 6), + (0xAA7E, 0xAAAF, 4), + (0xAAB0, 0xAAB0, 5), + (0xAAB1, 0xAAB1, 4), + (0xAAB2, 0xAAB4, 5), + (0xAAB5, 0xAAB6, 4), + (0xAAB7, 0xAAB8, 5), + (0xAAB9, 0xAABD, 4), + (0xAABE, 0xAABF, 5), + (0xAAC0, 0xAAC0, 4), + (0xAAC1, 0xAAC1, 5), + (0xAAC2, 0xAAC2, 4), + (0xAAC3, 0xAADA, 29), + (0xAADB, 0xAADC, 4), + (0xAADD, 0xAADD, 3), + (0xAADE, 0xAADF, 17), + (0xAAE0, 0xAAEA, 4), + (0xAAEB, 0xAAEB, 6), + (0xAAEC, 0xAAED, 5), + (0xAAEE, 0xAAEF, 6), + (0xAAF0, 0xAAF1, 17), + (0xAAF2, 0xAAF2, 4), + (0xAAF3, 0xAAF4, 3), + (0xAAF5, 0xAAF5, 6), + (0xAAF6, 0xAAF6, 5), + (0xAAF7, 0xAB00, 29), + (0xAB01, 0xAB06, 4), + (0xAB07, 0xAB08, 29), + (0xAB09, 0xAB0E, 4), + (0xAB0F, 0xAB10, 29), + (0xAB11, 0xAB16, 4), + (0xAB17, 0xAB1F, 29), + (0xAB20, 0xAB26, 4), + (0xAB27, 0xAB27, 29), + (0xAB28, 0xAB2E, 4), + (0xAB2F, 0xAB2F, 29), + (0xAB30, 0xAB5A, 1), + (0xAB5B, 0xAB5B, 20), + (0xAB5C, 0xAB5F, 3), + (0xAB60, 0xAB68, 1), + (0xAB69, 0xAB69, 3), + (0xAB6A, 0xAB6B, 20), + (0xAB6C, 0xAB6D, 0), + (0xAB6E, 0xAB6F, 29), + (0xAB70, 0xABBF, 1), + (0xABC0, 0xABE2, 4), + (0xABE3, 0xABE4, 6), + (0xABE5, 0xABE5, 5), + (0xABE6, 0xABE7, 6), + (0xABE8, 0xABE8, 5), + (0xABE9, 0xABEA, 6), + (0xABEB, 0xABEB, 17), + (0xABEC, 0xABEC, 6), + (0xABED, 0xABED, 5), + (0xABEE, 0xABEF, 29), + (0xABF0, 0xABF9, 8), + (0xABFA, 0xABFF, 29), + (0xAC00, 0xD7A3, 4), + (0xD7A4, 0xD7AF, 29), + (0xD7B0, 0xD7C6, 4), + (0xD7C7, 0xD7CA, 29), + (0xD7CB, 0xD7FB, 4), + (0xD7FC, 0xD7FF, 29), + (0xD800, 0xDFFF, 27), + (0xE000, 0xF8FF, 28), + (0xF900, 0xFA6D, 4), + (0xFA6E, 0xFA6F, 29), + (0xFA70, 0xFAD9, 4), + (0xFADA, 0xFAFF, 29), + (0xFB00, 0xFB06, 1), + (0xFB07, 0xFB12, 29), + (0xFB13, 0xFB17, 1), + (0xFB18, 0xFB1C, 29), + (0xFB1D, 0xFB1D, 4), + (0xFB1E, 0xFB1E, 5), + (0xFB1F, 0xFB28, 4), + (0xFB29, 0xFB29, 18), + (0xFB2A, 0xFB36, 4), + (0xFB37, 0xFB37, 29), + (0xFB38, 0xFB3C, 4), + (0xFB3D, 0xFB3D, 29), + (0xFB3E, 0xFB3E, 4), + (0xFB3F, 0xFB3F, 29), + (0xFB40, 0xFB41, 4), + (0xFB42, 0xFB42, 29), + (0xFB43, 0xFB44, 4), + (0xFB45, 0xFB45, 29), + (0xFB46, 0xFBB1, 4), + (0xFBB2, 0xFBC2, 20), + (0xFBC3, 0xFBD2, 21), + (0xFBD3, 0xFD3D, 4), + (0xFD3E, 0xFD3E, 14), + (0xFD3F, 0xFD3F, 13), + (0xFD40, 0xFD4F, 21), + (0xFD50, 0xFD8F, 4), + (0xFD90, 0xFD91, 21), + (0xFD92, 0xFDC7, 4), + (0xFDC8, 0xFDCF, 21), + (0xFDD0, 0xFDEF, 29), + (0xFDF0, 0xFDFB, 4), + (0xFDFC, 0xFDFC, 19), + (0xFDFD, 0xFDFF, 21), + (0xFE00, 0xFE0F, 5), + (0xFE10, 0xFE16, 17), + (0xFE17, 0xFE17, 13), + (0xFE18, 0xFE18, 14), + (0xFE19, 0xFE19, 17), + (0xFE1A, 0xFE1F, 29), + (0xFE20, 0xFE2F, 5), + (0xFE30, 0xFE30, 17), + (0xFE31, 0xFE32, 12), + (0xFE33, 0xFE34, 11), + (0xFE35, 0xFE35, 13), + (0xFE36, 0xFE36, 14), + (0xFE37, 0xFE37, 13), + (0xFE38, 0xFE38, 14), + (0xFE39, 0xFE39, 13), + (0xFE3A, 0xFE3A, 14), + (0xFE3B, 0xFE3B, 13), + (0xFE3C, 0xFE3C, 14), + (0xFE3D, 0xFE3D, 13), + (0xFE3E, 0xFE3E, 14), + (0xFE3F, 0xFE3F, 13), + (0xFE40, 0xFE40, 14), + (0xFE41, 0xFE41, 13), + (0xFE42, 0xFE42, 14), + (0xFE43, 0xFE43, 13), + (0xFE44, 0xFE44, 14), + (0xFE45, 0xFE46, 17), + (0xFE47, 0xFE47, 13), + (0xFE48, 0xFE48, 14), + (0xFE49, 0xFE4C, 17), + (0xFE4D, 0xFE4F, 11), + (0xFE50, 0xFE52, 17), + (0xFE53, 0xFE53, 29), + (0xFE54, 0xFE57, 17), + (0xFE58, 0xFE58, 12), + (0xFE59, 0xFE59, 13), + (0xFE5A, 0xFE5A, 14), + (0xFE5B, 0xFE5B, 13), + (0xFE5C, 0xFE5C, 14), + (0xFE5D, 0xFE5D, 13), + (0xFE5E, 0xFE5E, 14), + (0xFE5F, 0xFE61, 17), + (0xFE62, 0xFE62, 18), + (0xFE63, 0xFE63, 12), + (0xFE64, 0xFE66, 18), + (0xFE67, 0xFE67, 29), + (0xFE68, 0xFE68, 17), + (0xFE69, 0xFE69, 19), + (0xFE6A, 0xFE6B, 17), + (0xFE6C, 0xFE6F, 29), + (0xFE70, 0xFE74, 4), + (0xFE75, 0xFE75, 29), + (0xFE76, 0xFEFC, 4), + (0xFEFD, 0xFEFE, 29), + (0xFEFF, 0xFEFF, 26), + (0xFF00, 0xFF00, 29), + (0xFF01, 0xFF03, 17), + (0xFF04, 0xFF04, 19), + (0xFF05, 0xFF07, 17), + (0xFF08, 0xFF08, 13), + (0xFF09, 0xFF09, 14), + (0xFF0A, 0xFF0A, 17), + (0xFF0B, 0xFF0B, 18), + (0xFF0C, 0xFF0C, 17), + (0xFF0D, 0xFF0D, 12), + (0xFF0E, 0xFF0F, 17), + (0xFF10, 0xFF19, 8), + (0xFF1A, 0xFF1B, 17), + (0xFF1C, 0xFF1E, 18), + (0xFF1F, 0xFF20, 17), + (0xFF21, 0xFF3A, 0), + (0xFF3B, 0xFF3B, 13), + (0xFF3C, 0xFF3C, 17), + (0xFF3D, 0xFF3D, 14), + (0xFF3E, 0xFF3E, 20), + (0xFF3F, 0xFF3F, 11), + (0xFF40, 0xFF40, 20), + (0xFF41, 0xFF5A, 1), + (0xFF5B, 0xFF5B, 13), + (0xFF5C, 0xFF5C, 18), + (0xFF5D, 0xFF5D, 14), + (0xFF5E, 0xFF5E, 18), + (0xFF5F, 0xFF5F, 13), + (0xFF60, 0xFF60, 14), + (0xFF61, 0xFF61, 17), + (0xFF62, 0xFF62, 13), + (0xFF63, 0xFF63, 14), + (0xFF64, 0xFF65, 17), + (0xFF66, 0xFF6F, 4), + (0xFF70, 0xFF70, 3), + (0xFF71, 0xFF9D, 4), + (0xFF9E, 0xFF9F, 3), + (0xFFA0, 0xFFBE, 4), + (0xFFBF, 0xFFC1, 29), + (0xFFC2, 0xFFC7, 4), + (0xFFC8, 0xFFC9, 29), + (0xFFCA, 0xFFCF, 4), + (0xFFD0, 0xFFD1, 29), + (0xFFD2, 0xFFD7, 4), + (0xFFD8, 0xFFD9, 29), + (0xFFDA, 0xFFDC, 4), + (0xFFDD, 0xFFDF, 29), + (0xFFE0, 0xFFE1, 19), + (0xFFE2, 0xFFE2, 18), + (0xFFE3, 0xFFE3, 20), + (0xFFE4, 0xFFE4, 21), + (0xFFE5, 0xFFE6, 19), + (0xFFE7, 0xFFE7, 29), + (0xFFE8, 0xFFE8, 21), + (0xFFE9, 0xFFEC, 18), + (0xFFED, 0xFFEE, 21), + (0xFFEF, 0xFFF8, 29), + (0xFFF9, 0xFFFB, 26), + (0xFFFC, 0xFFFD, 21), + (0xFFFE, 0xFFFF, 29), + (0x10000, 0x1000B, 4), + (0x1000C, 0x1000C, 29), + (0x1000D, 0x10026, 4), + (0x10027, 0x10027, 29), + (0x10028, 0x1003A, 4), + (0x1003B, 0x1003B, 29), + (0x1003C, 0x1003D, 4), + (0x1003E, 0x1003E, 29), + (0x1003F, 0x1004D, 4), + (0x1004E, 0x1004F, 29), + (0x10050, 0x1005D, 4), + (0x1005E, 0x1007F, 29), + (0x10080, 0x100FA, 4), + (0x100FB, 0x100FF, 29), + (0x10100, 0x10102, 17), + (0x10103, 0x10106, 29), + (0x10107, 0x10133, 10), + (0x10134, 0x10136, 29), + (0x10137, 0x1013F, 21), + (0x10140, 0x10174, 9), + (0x10175, 0x10178, 10), + (0x10179, 0x10189, 21), + (0x1018A, 0x1018B, 10), + (0x1018C, 0x1018E, 21), + (0x1018F, 0x1018F, 29), + (0x10190, 0x1019C, 21), + (0x1019D, 0x1019F, 29), + (0x101A0, 0x101A0, 21), + (0x101A1, 0x101CF, 29), + (0x101D0, 0x101FC, 21), + (0x101FD, 0x101FD, 5), + (0x101FE, 0x1027F, 29), + (0x10280, 0x1029C, 4), + (0x1029D, 0x1029F, 29), + (0x102A0, 0x102D0, 4), + (0x102D1, 0x102DF, 29), + (0x102E0, 0x102E0, 5), + (0x102E1, 0x102FB, 10), + (0x102FC, 0x102FF, 29), + (0x10300, 0x1031F, 4), + (0x10320, 0x10323, 10), + (0x10324, 0x1032C, 29), + (0x1032D, 0x10340, 4), + (0x10341, 0x10341, 9), + (0x10342, 0x10349, 4), + (0x1034A, 0x1034A, 9), + (0x1034B, 0x1034F, 29), + (0x10350, 0x10375, 4), + (0x10376, 0x1037A, 5), + (0x1037B, 0x1037F, 29), + (0x10380, 0x1039D, 4), + (0x1039E, 0x1039E, 29), + (0x1039F, 0x1039F, 17), + (0x103A0, 0x103C3, 4), + (0x103C4, 0x103C7, 29), + (0x103C8, 0x103CF, 4), + (0x103D0, 0x103D0, 17), + (0x103D1, 0x103D5, 9), + (0x103D6, 0x103FF, 29), + (0x10400, 0x10427, 0), + (0x10428, 0x1044F, 1), + (0x10450, 0x1049D, 4), + (0x1049E, 0x1049F, 29), + (0x104A0, 0x104A9, 8), + (0x104AA, 0x104AF, 29), + (0x104B0, 0x104D3, 0), + (0x104D4, 0x104D7, 29), + (0x104D8, 0x104FB, 1), + (0x104FC, 0x104FF, 29), + (0x10500, 0x10527, 4), + (0x10528, 0x1052F, 29), + (0x10530, 0x10563, 4), + (0x10564, 0x1056E, 29), + (0x1056F, 0x1056F, 17), + (0x10570, 0x1057A, 0), + (0x1057B, 0x1057B, 29), + (0x1057C, 0x1058A, 0), + (0x1058B, 0x1058B, 29), + (0x1058C, 0x10592, 0), + (0x10593, 0x10593, 29), + (0x10594, 0x10595, 0), + (0x10596, 0x10596, 29), + (0x10597, 0x105A1, 1), + (0x105A2, 0x105A2, 29), + (0x105A3, 0x105B1, 1), + (0x105B2, 0x105B2, 29), + (0x105B3, 0x105B9, 1), + (0x105BA, 0x105BA, 29), + (0x105BB, 0x105BC, 1), + (0x105BD, 0x105BF, 29), + (0x105C0, 0x105F3, 4), + (0x105F4, 0x105FF, 29), + (0x10600, 0x10736, 4), + (0x10737, 0x1073F, 29), + (0x10740, 0x10755, 4), + (0x10756, 0x1075F, 29), + (0x10760, 0x10767, 4), + (0x10768, 0x1077F, 29), + (0x10780, 0x10785, 3), + (0x10786, 0x10786, 29), + (0x10787, 0x107B0, 3), + (0x107B1, 0x107B1, 29), + (0x107B2, 0x107BF, 3), + (0x107C0, 0x107FF, 29), + (0x10800, 0x10805, 4), + (0x10806, 0x10807, 29), + (0x10808, 0x10808, 4), + (0x10809, 0x10809, 29), + (0x1080A, 0x10835, 4), + (0x10836, 0x10836, 29), + (0x10837, 0x10838, 4), + (0x10839, 0x1083B, 29), + (0x1083C, 0x1083C, 4), + (0x1083D, 0x1083E, 29), + (0x1083F, 0x10855, 4), + (0x10856, 0x10856, 29), + (0x10857, 0x10857, 17), + (0x10858, 0x1085F, 10), + (0x10860, 0x10876, 4), + (0x10877, 0x10878, 21), + (0x10879, 0x1087F, 10), + (0x10880, 0x1089E, 4), + (0x1089F, 0x108A6, 29), + (0x108A7, 0x108AF, 10), + (0x108B0, 0x108DF, 29), + (0x108E0, 0x108F2, 4), + (0x108F3, 0x108F3, 29), + (0x108F4, 0x108F5, 4), + (0x108F6, 0x108FA, 29), + (0x108FB, 0x108FF, 10), + (0x10900, 0x10915, 4), + (0x10916, 0x1091B, 10), + (0x1091C, 0x1091E, 29), + (0x1091F, 0x1091F, 17), + (0x10920, 0x10939, 4), + (0x1093A, 0x1093E, 29), + (0x1093F, 0x1093F, 17), + (0x10940, 0x10959, 4), + (0x1095A, 0x1097F, 29), + (0x10980, 0x109B7, 4), + (0x109B8, 0x109BB, 29), + (0x109BC, 0x109BD, 10), + (0x109BE, 0x109BF, 4), + (0x109C0, 0x109CF, 10), + (0x109D0, 0x109D1, 29), + (0x109D2, 0x109FF, 10), + (0x10A00, 0x10A00, 4), + (0x10A01, 0x10A03, 5), + (0x10A04, 0x10A04, 29), + (0x10A05, 0x10A06, 5), + (0x10A07, 0x10A0B, 29), + (0x10A0C, 0x10A0F, 5), + (0x10A10, 0x10A13, 4), + (0x10A14, 0x10A14, 29), + (0x10A15, 0x10A17, 4), + (0x10A18, 0x10A18, 29), + (0x10A19, 0x10A35, 4), + (0x10A36, 0x10A37, 29), + (0x10A38, 0x10A3A, 5), + (0x10A3B, 0x10A3E, 29), + (0x10A3F, 0x10A3F, 5), + (0x10A40, 0x10A48, 10), + (0x10A49, 0x10A4F, 29), + (0x10A50, 0x10A58, 17), + (0x10A59, 0x10A5F, 29), + (0x10A60, 0x10A7C, 4), + (0x10A7D, 0x10A7E, 10), + (0x10A7F, 0x10A7F, 17), + (0x10A80, 0x10A9C, 4), + (0x10A9D, 0x10A9F, 10), + (0x10AA0, 0x10ABF, 29), + (0x10AC0, 0x10AC7, 4), + (0x10AC8, 0x10AC8, 21), + (0x10AC9, 0x10AE4, 4), + (0x10AE5, 0x10AE6, 5), + (0x10AE7, 0x10AEA, 29), + (0x10AEB, 0x10AEF, 10), + (0x10AF0, 0x10AF6, 17), + (0x10AF7, 0x10AFF, 29), + (0x10B00, 0x10B35, 4), + (0x10B36, 0x10B38, 29), + (0x10B39, 0x10B3F, 17), + (0x10B40, 0x10B55, 4), + (0x10B56, 0x10B57, 29), + (0x10B58, 0x10B5F, 10), + (0x10B60, 0x10B72, 4), + (0x10B73, 0x10B77, 29), + (0x10B78, 0x10B7F, 10), + (0x10B80, 0x10B91, 4), + (0x10B92, 0x10B98, 29), + (0x10B99, 0x10B9C, 17), + (0x10B9D, 0x10BA8, 29), + (0x10BA9, 0x10BAF, 10), + (0x10BB0, 0x10BFF, 29), + (0x10C00, 0x10C48, 4), + (0x10C49, 0x10C7F, 29), + (0x10C80, 0x10CB2, 0), + (0x10CB3, 0x10CBF, 29), + (0x10CC0, 0x10CF2, 1), + (0x10CF3, 0x10CF9, 29), + (0x10CFA, 0x10CFF, 10), + (0x10D00, 0x10D23, 4), + (0x10D24, 0x10D27, 5), + (0x10D28, 0x10D2F, 29), + (0x10D30, 0x10D39, 8), + (0x10D3A, 0x10D3F, 29), + (0x10D40, 0x10D49, 8), + (0x10D4A, 0x10D4D, 4), + (0x10D4E, 0x10D4E, 3), + (0x10D4F, 0x10D4F, 4), + (0x10D50, 0x10D65, 0), + (0x10D66, 0x10D68, 29), + (0x10D69, 0x10D6D, 5), + (0x10D6E, 0x10D6E, 12), + (0x10D6F, 0x10D6F, 3), + (0x10D70, 0x10D85, 1), + (0x10D86, 0x10D8D, 29), + (0x10D8E, 0x10D8F, 18), + (0x10D90, 0x10E5F, 29), + (0x10E60, 0x10E7E, 10), + (0x10E7F, 0x10E7F, 29), + (0x10E80, 0x10EA9, 4), + (0x10EAA, 0x10EAA, 29), + (0x10EAB, 0x10EAC, 5), + (0x10EAD, 0x10EAD, 12), + (0x10EAE, 0x10EAF, 29), + (0x10EB0, 0x10EB1, 4), + (0x10EB2, 0x10EC1, 29), + (0x10EC2, 0x10EC4, 4), + (0x10EC5, 0x10EC5, 3), + (0x10EC6, 0x10EC7, 4), + (0x10EC8, 0x10EC8, 29), + (0x10EC9, 0x10ECA, 20), + (0x10ECB, 0x10ECF, 5), + (0x10ED0, 0x10ED0, 17), + (0x10ED1, 0x10ED8, 21), + (0x10ED9, 0x10EEE, 4), + (0x10EEF, 0x10EEF, 29), + (0x10EF0, 0x10EFF, 5), + (0x10F00, 0x10F1C, 4), + (0x10F1D, 0x10F26, 10), + (0x10F27, 0x10F27, 4), + (0x10F28, 0x10F2F, 29), + (0x10F30, 0x10F45, 4), + (0x10F46, 0x10F50, 5), + (0x10F51, 0x10F54, 10), + (0x10F55, 0x10F59, 17), + (0x10F5A, 0x10F6F, 29), + (0x10F70, 0x10F81, 4), + (0x10F82, 0x10F85, 5), + (0x10F86, 0x10F89, 17), + (0x10F8A, 0x10FAF, 29), + (0x10FB0, 0x10FC4, 4), + (0x10FC5, 0x10FCB, 10), + (0x10FCC, 0x10FDF, 29), + (0x10FE0, 0x10FF6, 4), + (0x10FF7, 0x10FFF, 29), + (0x11000, 0x11000, 6), + (0x11001, 0x11001, 5), + (0x11002, 0x11002, 6), + (0x11003, 0x11037, 4), + (0x11038, 0x11046, 5), + (0x11047, 0x1104D, 17), + (0x1104E, 0x11051, 29), + (0x11052, 0x11065, 10), + (0x11066, 0x1106F, 8), + (0x11070, 0x11070, 5), + (0x11071, 0x11072, 4), + (0x11073, 0x11074, 5), + (0x11075, 0x11075, 4), + (0x11076, 0x1107E, 29), + (0x1107F, 0x11081, 5), + (0x11082, 0x11082, 6), + (0x11083, 0x110AF, 4), + (0x110B0, 0x110B2, 6), + (0x110B3, 0x110B6, 5), + (0x110B7, 0x110B8, 6), + (0x110B9, 0x110BA, 5), + (0x110BB, 0x110BC, 17), + (0x110BD, 0x110BD, 26), + (0x110BE, 0x110C1, 17), + (0x110C2, 0x110C2, 5), + (0x110C3, 0x110CC, 29), + (0x110CD, 0x110CD, 26), + (0x110CE, 0x110CF, 29), + (0x110D0, 0x110E8, 4), + (0x110E9, 0x110EF, 29), + (0x110F0, 0x110F9, 8), + (0x110FA, 0x110FF, 29), + (0x11100, 0x11102, 5), + (0x11103, 0x11126, 4), + (0x11127, 0x1112B, 5), + (0x1112C, 0x1112C, 6), + (0x1112D, 0x11134, 5), + (0x11135, 0x11135, 29), + (0x11136, 0x1113F, 8), + (0x11140, 0x11143, 17), + (0x11144, 0x11144, 4), + (0x11145, 0x11146, 6), + (0x11147, 0x11147, 4), + (0x11148, 0x1114F, 29), + (0x11150, 0x11172, 4), + (0x11173, 0x11173, 5), + (0x11174, 0x11175, 17), + (0x11176, 0x11176, 4), + (0x11177, 0x1117F, 29), + (0x11180, 0x11181, 5), + (0x11182, 0x11182, 6), + (0x11183, 0x111B2, 4), + (0x111B3, 0x111B5, 6), + (0x111B6, 0x111BE, 5), + (0x111BF, 0x111C0, 6), + (0x111C1, 0x111C4, 4), + (0x111C5, 0x111C8, 17), + (0x111C9, 0x111CC, 5), + (0x111CD, 0x111CD, 17), + (0x111CE, 0x111CE, 6), + (0x111CF, 0x111CF, 5), + (0x111D0, 0x111D9, 8), + (0x111DA, 0x111DA, 4), + (0x111DB, 0x111DB, 17), + (0x111DC, 0x111DC, 4), + (0x111DD, 0x111DF, 17), + (0x111E0, 0x111E0, 29), + (0x111E1, 0x111F4, 10), + (0x111F5, 0x111FF, 29), + (0x11200, 0x11211, 4), + (0x11212, 0x11212, 29), + (0x11213, 0x1122B, 4), + (0x1122C, 0x1122E, 6), + (0x1122F, 0x11231, 5), + (0x11232, 0x11233, 6), + (0x11234, 0x11234, 5), + (0x11235, 0x11235, 6), + (0x11236, 0x11237, 5), + (0x11238, 0x1123D, 17), + (0x1123E, 0x1123E, 5), + (0x1123F, 0x11240, 4), + (0x11241, 0x11241, 5), + (0x11242, 0x1127F, 29), + (0x11280, 0x11286, 4), + (0x11287, 0x11287, 29), + (0x11288, 0x11288, 4), + (0x11289, 0x11289, 29), + (0x1128A, 0x1128D, 4), + (0x1128E, 0x1128E, 29), + (0x1128F, 0x1129D, 4), + (0x1129E, 0x1129E, 29), + (0x1129F, 0x112A8, 4), + (0x112A9, 0x112A9, 17), + (0x112AA, 0x112AF, 29), + (0x112B0, 0x112DE, 4), + (0x112DF, 0x112DF, 5), + (0x112E0, 0x112E2, 6), + (0x112E3, 0x112EA, 5), + (0x112EB, 0x112EF, 29), + (0x112F0, 0x112F9, 8), + (0x112FA, 0x112FF, 29), + (0x11300, 0x11301, 5), + (0x11302, 0x11303, 6), + (0x11304, 0x11304, 29), + (0x11305, 0x1130C, 4), + (0x1130D, 0x1130E, 29), + (0x1130F, 0x11310, 4), + (0x11311, 0x11312, 29), + (0x11313, 0x11328, 4), + (0x11329, 0x11329, 29), + (0x1132A, 0x11330, 4), + (0x11331, 0x11331, 29), + (0x11332, 0x11333, 4), + (0x11334, 0x11334, 29), + (0x11335, 0x11339, 4), + (0x1133A, 0x1133A, 29), + (0x1133B, 0x1133C, 5), + (0x1133D, 0x1133D, 4), + (0x1133E, 0x1133F, 6), + (0x11340, 0x11340, 5), + (0x11341, 0x11344, 6), + (0x11345, 0x11346, 29), + (0x11347, 0x11348, 6), + (0x11349, 0x1134A, 29), + (0x1134B, 0x1134D, 6), + (0x1134E, 0x1134F, 29), + (0x11350, 0x11350, 4), + (0x11351, 0x11356, 29), + (0x11357, 0x11357, 6), + (0x11358, 0x1135C, 29), + (0x1135D, 0x11361, 4), + (0x11362, 0x11363, 6), + (0x11364, 0x11365, 29), + (0x11366, 0x1136C, 5), + (0x1136D, 0x1136F, 29), + (0x11370, 0x11374, 5), + (0x11375, 0x1137F, 29), + (0x11380, 0x11389, 4), + (0x1138A, 0x1138A, 29), + (0x1138B, 0x1138B, 4), + (0x1138C, 0x1138D, 29), + (0x1138E, 0x1138E, 4), + (0x1138F, 0x1138F, 29), + (0x11390, 0x113B5, 4), + (0x113B6, 0x113B6, 29), + (0x113B7, 0x113B7, 4), + (0x113B8, 0x113BA, 6), + (0x113BB, 0x113C0, 5), + (0x113C1, 0x113C1, 29), + (0x113C2, 0x113C2, 6), + (0x113C3, 0x113C4, 29), + (0x113C5, 0x113C5, 6), + (0x113C6, 0x113C6, 29), + (0x113C7, 0x113CA, 6), + (0x113CB, 0x113CB, 29), + (0x113CC, 0x113CD, 6), + (0x113CE, 0x113CE, 5), + (0x113CF, 0x113CF, 6), + (0x113D0, 0x113D0, 5), + (0x113D1, 0x113D1, 4), + (0x113D2, 0x113D2, 5), + (0x113D3, 0x113D3, 4), + (0x113D4, 0x113D5, 17), + (0x113D6, 0x113D6, 29), + (0x113D7, 0x113D8, 17), + (0x113D9, 0x113E0, 29), + (0x113E1, 0x113E2, 5), + (0x113E3, 0x113FF, 29), + (0x11400, 0x11434, 4), + (0x11435, 0x11437, 6), + (0x11438, 0x1143F, 5), + (0x11440, 0x11441, 6), + (0x11442, 0x11444, 5), + (0x11445, 0x11445, 6), + (0x11446, 0x11446, 5), + (0x11447, 0x1144A, 4), + (0x1144B, 0x1144F, 17), + (0x11450, 0x11459, 8), + (0x1145A, 0x1145B, 17), + (0x1145C, 0x1145C, 29), + (0x1145D, 0x1145D, 17), + (0x1145E, 0x1145E, 5), + (0x1145F, 0x11461, 4), + (0x11462, 0x1147F, 29), + (0x11480, 0x114AF, 4), + (0x114B0, 0x114B2, 6), + (0x114B3, 0x114B8, 5), + (0x114B9, 0x114B9, 6), + (0x114BA, 0x114BA, 5), + (0x114BB, 0x114BE, 6), + (0x114BF, 0x114C0, 5), + (0x114C1, 0x114C1, 6), + (0x114C2, 0x114C3, 5), + (0x114C4, 0x114C5, 4), + (0x114C6, 0x114C6, 17), + (0x114C7, 0x114C7, 4), + (0x114C8, 0x114CF, 29), + (0x114D0, 0x114D9, 8), + (0x114DA, 0x1157F, 29), + (0x11580, 0x115AE, 4), + (0x115AF, 0x115B1, 6), + (0x115B2, 0x115B5, 5), + (0x115B6, 0x115B7, 29), + (0x115B8, 0x115BB, 6), + (0x115BC, 0x115BD, 5), + (0x115BE, 0x115BE, 6), + (0x115BF, 0x115C0, 5), + (0x115C1, 0x115D7, 17), + (0x115D8, 0x115DB, 4), + (0x115DC, 0x115DD, 5), + (0x115DE, 0x115FF, 29), + (0x11600, 0x1162F, 4), + (0x11630, 0x11632, 6), + (0x11633, 0x1163A, 5), + (0x1163B, 0x1163C, 6), + (0x1163D, 0x1163D, 5), + (0x1163E, 0x1163E, 6), + (0x1163F, 0x11640, 5), + (0x11641, 0x11643, 17), + (0x11644, 0x11644, 4), + (0x11645, 0x1164F, 29), + (0x11650, 0x11659, 8), + (0x1165A, 0x1165F, 29), + (0x11660, 0x1166C, 17), + (0x1166D, 0x1167F, 29), + (0x11680, 0x116AA, 4), + (0x116AB, 0x116AB, 5), + (0x116AC, 0x116AC, 6), + (0x116AD, 0x116AD, 5), + (0x116AE, 0x116AF, 6), + (0x116B0, 0x116B5, 5), + (0x116B6, 0x116B6, 6), + (0x116B7, 0x116B7, 5), + (0x116B8, 0x116B8, 4), + (0x116B9, 0x116B9, 17), + (0x116BA, 0x116BF, 29), + (0x116C0, 0x116C9, 8), + (0x116CA, 0x116CF, 29), + (0x116D0, 0x116E3, 8), + (0x116E4, 0x116FF, 29), + (0x11700, 0x1171A, 4), + (0x1171B, 0x1171C, 29), + (0x1171D, 0x1171D, 5), + (0x1171E, 0x1171E, 6), + (0x1171F, 0x1171F, 5), + (0x11720, 0x11721, 6), + (0x11722, 0x11725, 5), + (0x11726, 0x11726, 6), + (0x11727, 0x1172B, 5), + (0x1172C, 0x1172F, 29), + (0x11730, 0x11739, 8), + (0x1173A, 0x1173B, 10), + (0x1173C, 0x1173E, 17), + (0x1173F, 0x1173F, 21), + (0x11740, 0x11746, 4), + (0x11747, 0x117FF, 29), + (0x11800, 0x1182B, 4), + (0x1182C, 0x1182E, 6), + (0x1182F, 0x11837, 5), + (0x11838, 0x11838, 6), + (0x11839, 0x1183A, 5), + (0x1183B, 0x1183B, 17), + (0x1183C, 0x1189F, 29), + (0x118A0, 0x118BF, 0), + (0x118C0, 0x118DF, 1), + (0x118E0, 0x118E9, 8), + (0x118EA, 0x118F2, 10), + (0x118F3, 0x118FE, 29), + (0x118FF, 0x11906, 4), + (0x11907, 0x11908, 29), + (0x11909, 0x11909, 4), + (0x1190A, 0x1190B, 29), + (0x1190C, 0x11913, 4), + (0x11914, 0x11914, 29), + (0x11915, 0x11916, 4), + (0x11917, 0x11917, 29), + (0x11918, 0x1192F, 4), + (0x11930, 0x11935, 6), + (0x11936, 0x11936, 29), + (0x11937, 0x11938, 6), + (0x11939, 0x1193A, 29), + (0x1193B, 0x1193C, 5), + (0x1193D, 0x1193D, 6), + (0x1193E, 0x1193E, 5), + (0x1193F, 0x1193F, 4), + (0x11940, 0x11940, 6), + (0x11941, 0x11941, 4), + (0x11942, 0x11942, 6), + (0x11943, 0x11943, 5), + (0x11944, 0x11946, 17), + (0x11947, 0x1194F, 29), + (0x11950, 0x11959, 8), + (0x1195A, 0x1199F, 29), + (0x119A0, 0x119A7, 4), + (0x119A8, 0x119A9, 29), + (0x119AA, 0x119D0, 4), + (0x119D1, 0x119D3, 6), + (0x119D4, 0x119D7, 5), + (0x119D8, 0x119D9, 29), + (0x119DA, 0x119DB, 5), + (0x119DC, 0x119DF, 6), + (0x119E0, 0x119E0, 5), + (0x119E1, 0x119E1, 4), + (0x119E2, 0x119E2, 17), + (0x119E3, 0x119E3, 4), + (0x119E4, 0x119E4, 6), + (0x119E5, 0x119FF, 29), + (0x11A00, 0x11A00, 4), + (0x11A01, 0x11A0A, 5), + (0x11A0B, 0x11A32, 4), + (0x11A33, 0x11A38, 5), + (0x11A39, 0x11A39, 6), + (0x11A3A, 0x11A3A, 4), + (0x11A3B, 0x11A3E, 5), + (0x11A3F, 0x11A46, 17), + (0x11A47, 0x11A47, 5), + (0x11A48, 0x11A4F, 29), + (0x11A50, 0x11A50, 4), + (0x11A51, 0x11A56, 5), + (0x11A57, 0x11A58, 6), + (0x11A59, 0x11A5B, 5), + (0x11A5C, 0x11A89, 4), + (0x11A8A, 0x11A96, 5), + (0x11A97, 0x11A97, 6), + (0x11A98, 0x11A99, 5), + (0x11A9A, 0x11A9C, 17), + (0x11A9D, 0x11A9D, 4), + (0x11A9E, 0x11AA2, 17), + (0x11AA3, 0x11AAF, 29), + (0x11AB0, 0x11AF8, 4), + (0x11AF9, 0x11AFF, 29), + (0x11B00, 0x11B09, 17), + (0x11B0A, 0x11B0A, 4), + (0x11B0B, 0x11B5F, 29), + (0x11B60, 0x11B60, 5), + (0x11B61, 0x11B61, 6), + (0x11B62, 0x11B64, 5), + (0x11B65, 0x11B65, 6), + (0x11B66, 0x11B66, 5), + (0x11B67, 0x11B67, 6), + (0x11B68, 0x11BBF, 29), + (0x11BC0, 0x11BE0, 4), + (0x11BE1, 0x11BE1, 17), + (0x11BE2, 0x11BEF, 29), + (0x11BF0, 0x11BF9, 8), + (0x11BFA, 0x11BFF, 29), + (0x11C00, 0x11C08, 4), + (0x11C09, 0x11C09, 29), + (0x11C0A, 0x11C2E, 4), + (0x11C2F, 0x11C2F, 6), + (0x11C30, 0x11C36, 5), + (0x11C37, 0x11C37, 29), + (0x11C38, 0x11C3D, 5), + (0x11C3E, 0x11C3E, 6), + (0x11C3F, 0x11C3F, 5), + (0x11C40, 0x11C40, 4), + (0x11C41, 0x11C45, 17), + (0x11C46, 0x11C4F, 29), + (0x11C50, 0x11C59, 8), + (0x11C5A, 0x11C6C, 10), + (0x11C6D, 0x11C6F, 29), + (0x11C70, 0x11C71, 17), + (0x11C72, 0x11C8F, 4), + (0x11C90, 0x11C91, 29), + (0x11C92, 0x11CA7, 5), + (0x11CA8, 0x11CA8, 29), + (0x11CA9, 0x11CA9, 6), + (0x11CAA, 0x11CB0, 5), + (0x11CB1, 0x11CB1, 6), + (0x11CB2, 0x11CB3, 5), + (0x11CB4, 0x11CB4, 6), + (0x11CB5, 0x11CB6, 5), + (0x11CB7, 0x11CFF, 29), + (0x11D00, 0x11D06, 4), + (0x11D07, 0x11D07, 29), + (0x11D08, 0x11D09, 4), + (0x11D0A, 0x11D0A, 29), + (0x11D0B, 0x11D30, 4), + (0x11D31, 0x11D36, 5), + (0x11D37, 0x11D39, 29), + (0x11D3A, 0x11D3A, 5), + (0x11D3B, 0x11D3B, 29), + (0x11D3C, 0x11D3D, 5), + (0x11D3E, 0x11D3E, 29), + (0x11D3F, 0x11D45, 5), + (0x11D46, 0x11D46, 4), + (0x11D47, 0x11D47, 5), + (0x11D48, 0x11D4F, 29), + (0x11D50, 0x11D59, 8), + (0x11D5A, 0x11D5F, 29), + (0x11D60, 0x11D65, 4), + (0x11D66, 0x11D66, 29), + (0x11D67, 0x11D68, 4), + (0x11D69, 0x11D69, 29), + (0x11D6A, 0x11D89, 4), + (0x11D8A, 0x11D8E, 6), + (0x11D8F, 0x11D8F, 29), + (0x11D90, 0x11D91, 5), + (0x11D92, 0x11D92, 29), + (0x11D93, 0x11D94, 6), + (0x11D95, 0x11D95, 5), + (0x11D96, 0x11D96, 6), + (0x11D97, 0x11D97, 5), + (0x11D98, 0x11D98, 4), + (0x11D99, 0x11D9F, 29), + (0x11DA0, 0x11DA9, 8), + (0x11DAA, 0x11DAF, 29), + (0x11DB0, 0x11DD8, 4), + (0x11DD9, 0x11DD9, 3), + (0x11DDA, 0x11DDB, 4), + (0x11DDC, 0x11DDF, 29), + (0x11DE0, 0x11DE9, 8), + (0x11DEA, 0x11DEF, 29), + (0x11DF0, 0x11DF0, 5), + (0x11DF1, 0x11DF1, 4), + (0x11DF2, 0x11EDF, 29), + (0x11EE0, 0x11EF2, 4), + (0x11EF3, 0x11EF4, 5), + (0x11EF5, 0x11EF6, 6), + (0x11EF7, 0x11EF8, 17), + (0x11EF9, 0x11EFF, 29), + (0x11F00, 0x11F01, 5), + (0x11F02, 0x11F02, 4), + (0x11F03, 0x11F03, 6), + (0x11F04, 0x11F10, 4), + (0x11F11, 0x11F11, 29), + (0x11F12, 0x11F33, 4), + (0x11F34, 0x11F35, 6), + (0x11F36, 0x11F3A, 5), + (0x11F3B, 0x11F3D, 29), + (0x11F3E, 0x11F3F, 6), + (0x11F40, 0x11F40, 5), + (0x11F41, 0x11F41, 6), + (0x11F42, 0x11F42, 5), + (0x11F43, 0x11F4F, 17), + (0x11F50, 0x11F59, 8), + (0x11F5A, 0x11F5A, 5), + (0x11F5B, 0x11FAF, 29), + (0x11FB0, 0x11FB0, 4), + (0x11FB1, 0x11FBF, 29), + (0x11FC0, 0x11FD4, 10), + (0x11FD5, 0x11FDC, 21), + (0x11FDD, 0x11FE0, 19), + (0x11FE1, 0x11FF1, 21), + (0x11FF2, 0x11FFE, 29), + (0x11FFF, 0x11FFF, 17), + (0x12000, 0x12399, 4), + (0x1239A, 0x123FF, 29), + (0x12400, 0x1246F, 9), + (0x12470, 0x12474, 17), + (0x12475, 0x1247F, 9), + (0x12480, 0x12543, 4), + (0x12544, 0x1254F, 29), + (0x12550, 0x12686, 9), + (0x12687, 0x12F8F, 29), + (0x12F90, 0x12FF0, 4), + (0x12FF1, 0x12FF2, 17), + (0x12FF3, 0x12FFF, 29), + (0x13000, 0x1342F, 4), + (0x13430, 0x1343F, 26), + (0x13440, 0x13440, 5), + (0x13441, 0x13446, 4), + (0x13447, 0x13455, 5), + (0x13456, 0x1345F, 29), + (0x13460, 0x143FA, 4), + (0x143FB, 0x143FF, 29), + (0x14400, 0x14646, 4), + (0x14647, 0x160FF, 29), + (0x16100, 0x1611D, 4), + (0x1611E, 0x16129, 5), + (0x1612A, 0x1612C, 6), + (0x1612D, 0x1612F, 5), + (0x16130, 0x16139, 8), + (0x1613A, 0x167FF, 29), + (0x16800, 0x16A38, 4), + (0x16A39, 0x16A3F, 29), + (0x16A40, 0x16A5E, 4), + (0x16A5F, 0x16A5F, 29), + (0x16A60, 0x16A69, 8), + (0x16A6A, 0x16A6D, 29), + (0x16A6E, 0x16A6F, 17), + (0x16A70, 0x16ABE, 4), + (0x16ABF, 0x16ABF, 29), + (0x16AC0, 0x16AC9, 8), + (0x16ACA, 0x16ACF, 29), + (0x16AD0, 0x16AED, 4), + (0x16AEE, 0x16AEF, 29), + (0x16AF0, 0x16AF4, 5), + (0x16AF5, 0x16AF5, 17), + (0x16AF6, 0x16AFF, 29), + (0x16B00, 0x16B2F, 4), + (0x16B30, 0x16B36, 5), + (0x16B37, 0x16B3B, 17), + (0x16B3C, 0x16B3F, 21), + (0x16B40, 0x16B43, 3), + (0x16B44, 0x16B44, 17), + (0x16B45, 0x16B45, 21), + (0x16B46, 0x16B4F, 29), + (0x16B50, 0x16B59, 8), + (0x16B5A, 0x16B5A, 29), + (0x16B5B, 0x16B61, 10), + (0x16B62, 0x16B62, 29), + (0x16B63, 0x16B77, 4), + (0x16B78, 0x16B7C, 29), + (0x16B7D, 0x16B8F, 4), + (0x16B90, 0x16D3F, 29), + (0x16D40, 0x16D42, 3), + (0x16D43, 0x16D6A, 4), + (0x16D6B, 0x16D6C, 3), + (0x16D6D, 0x16D6F, 17), + (0x16D70, 0x16D79, 8), + (0x16D7A, 0x16E3F, 29), + (0x16E40, 0x16E5F, 0), + (0x16E60, 0x16E7F, 1), + (0x16E80, 0x16E96, 10), + (0x16E97, 0x16E9A, 17), + (0x16E9B, 0x16E9F, 29), + (0x16EA0, 0x16EB8, 0), + (0x16EB9, 0x16EBA, 29), + (0x16EBB, 0x16ED3, 1), + (0x16ED4, 0x16EFF, 29), + (0x16F00, 0x16F4A, 4), + (0x16F4B, 0x16F4E, 29), + (0x16F4F, 0x16F4F, 5), + (0x16F50, 0x16F50, 4), + (0x16F51, 0x16F87, 6), + (0x16F88, 0x16F8E, 29), + (0x16F8F, 0x16F92, 5), + (0x16F93, 0x16F9F, 3), + (0x16FA0, 0x16FDF, 29), + (0x16FE0, 0x16FE1, 3), + (0x16FE2, 0x16FE2, 17), + (0x16FE3, 0x16FE3, 3), + (0x16FE4, 0x16FE4, 5), + (0x16FE5, 0x16FEF, 29), + (0x16FF0, 0x16FF1, 6), + (0x16FF2, 0x16FF3, 3), + (0x16FF4, 0x16FF6, 9), + (0x16FF7, 0x16FFF, 29), + (0x17000, 0x18CDA, 4), + (0x18CDB, 0x18CFE, 29), + (0x18CFF, 0x18D20, 4), + (0x18D21, 0x18D7F, 29), + (0x18D80, 0x18DF2, 4), + (0x18DF3, 0x18DFF, 29), + (0x18E00, 0x19191, 4), + (0x19192, 0x1919F, 29), + (0x191A0, 0x191D2, 4), + (0x191D3, 0x1AFEF, 29), + (0x1AFF0, 0x1AFF3, 3), + (0x1AFF4, 0x1AFF4, 29), + (0x1AFF5, 0x1AFFB, 3), + (0x1AFFC, 0x1AFFC, 29), + (0x1AFFD, 0x1AFFE, 3), + (0x1AFFF, 0x1AFFF, 29), + (0x1B000, 0x1B128, 4), + (0x1B129, 0x1B131, 29), + (0x1B132, 0x1B132, 4), + (0x1B133, 0x1B14F, 29), + (0x1B150, 0x1B152, 4), + (0x1B153, 0x1B154, 29), + (0x1B155, 0x1B155, 4), + (0x1B156, 0x1B163, 29), + (0x1B164, 0x1B168, 4), + (0x1B169, 0x1B16F, 29), + (0x1B170, 0x1B2FB, 4), + (0x1B2FC, 0x1BBFF, 29), + (0x1BC00, 0x1BC6A, 4), + (0x1BC6B, 0x1BC6F, 29), + (0x1BC70, 0x1BC7C, 4), + (0x1BC7D, 0x1BC7F, 29), + (0x1BC80, 0x1BC88, 4), + (0x1BC89, 0x1BC8F, 29), + (0x1BC90, 0x1BC99, 4), + (0x1BC9A, 0x1BC9B, 29), + (0x1BC9C, 0x1BC9C, 21), + (0x1BC9D, 0x1BC9E, 5), + (0x1BC9F, 0x1BC9F, 17), + (0x1BCA0, 0x1BCA3, 26), + (0x1BCA4, 0x1CBFF, 29), + (0x1CC00, 0x1CCEF, 21), + (0x1CCF0, 0x1CCF9, 8), + (0x1CCFA, 0x1CCFC, 21), + (0x1CCFD, 0x1CCFF, 29), + (0x1CD00, 0x1CEB3, 21), + (0x1CEB4, 0x1CEB9, 29), + (0x1CEBA, 0x1CED0, 21), + (0x1CED1, 0x1CED1, 29), + (0x1CED2, 0x1CED4, 21), + (0x1CED5, 0x1CEDC, 29), + (0x1CEDD, 0x1CEDF, 18), + (0x1CEE0, 0x1CEEF, 21), + (0x1CEF0, 0x1CEFD, 18), + (0x1CEFE, 0x1CEFF, 29), + (0x1CF00, 0x1CF2D, 5), + (0x1CF2E, 0x1CF2F, 29), + (0x1CF30, 0x1CF46, 5), + (0x1CF47, 0x1CF4F, 29), + (0x1CF50, 0x1CFC3, 21), + (0x1CFC4, 0x1CFFF, 29), + (0x1D000, 0x1D0F5, 21), + (0x1D0F6, 0x1D0FF, 29), + (0x1D100, 0x1D126, 21), + (0x1D127, 0x1D128, 5), + (0x1D129, 0x1D164, 21), + (0x1D165, 0x1D166, 6), + (0x1D167, 0x1D169, 5), + (0x1D16A, 0x1D16C, 21), + (0x1D16D, 0x1D172, 6), + (0x1D173, 0x1D17A, 26), + (0x1D17B, 0x1D182, 5), + (0x1D183, 0x1D184, 21), + (0x1D185, 0x1D18B, 5), + (0x1D18C, 0x1D1A9, 21), + (0x1D1AA, 0x1D1AD, 5), + (0x1D1AE, 0x1D241, 21), + (0x1D242, 0x1D244, 5), + (0x1D245, 0x1D245, 21), + (0x1D246, 0x1D24F, 29), + (0x1D250, 0x1D252, 6), + (0x1D253, 0x1D25A, 21), + (0x1D25B, 0x1D25C, 5), + (0x1D25D, 0x1D25E, 21), + (0x1D25F, 0x1D25F, 6), + (0x1D260, 0x1D27F, 21), + (0x1D280, 0x1D281, 6), + (0x1D282, 0x1D2BF, 29), + (0x1D2C0, 0x1D2D3, 10), + (0x1D2D4, 0x1D2DF, 29), + (0x1D2E0, 0x1D2F3, 10), + (0x1D2F4, 0x1D2FF, 29), + (0x1D300, 0x1D356, 21), + (0x1D357, 0x1D35F, 29), + (0x1D360, 0x1D378, 10), + (0x1D379, 0x1D3FF, 29), + (0x1D400, 0x1D419, 0), + (0x1D41A, 0x1D433, 1), + (0x1D434, 0x1D44D, 0), + (0x1D44E, 0x1D454, 1), + (0x1D455, 0x1D455, 29), + (0x1D456, 0x1D467, 1), + (0x1D468, 0x1D481, 0), + (0x1D482, 0x1D49B, 1), + (0x1D49C, 0x1D49C, 0), + (0x1D49D, 0x1D49D, 29), + (0x1D49E, 0x1D49F, 0), + (0x1D4A0, 0x1D4A1, 29), + (0x1D4A2, 0x1D4A2, 0), + (0x1D4A3, 0x1D4A4, 29), + (0x1D4A5, 0x1D4A6, 0), + (0x1D4A7, 0x1D4A8, 29), + (0x1D4A9, 0x1D4AC, 0), + (0x1D4AD, 0x1D4AD, 29), + (0x1D4AE, 0x1D4B5, 0), + (0x1D4B6, 0x1D4B9, 1), + (0x1D4BA, 0x1D4BA, 29), + (0x1D4BB, 0x1D4BB, 1), + (0x1D4BC, 0x1D4BC, 29), + (0x1D4BD, 0x1D4C3, 1), + (0x1D4C4, 0x1D4C4, 29), + (0x1D4C5, 0x1D4CF, 1), + (0x1D4D0, 0x1D4E9, 0), + (0x1D4EA, 0x1D503, 1), + (0x1D504, 0x1D505, 0), + (0x1D506, 0x1D506, 29), + (0x1D507, 0x1D50A, 0), + (0x1D50B, 0x1D50C, 29), + (0x1D50D, 0x1D514, 0), + (0x1D515, 0x1D515, 29), + (0x1D516, 0x1D51C, 0), + (0x1D51D, 0x1D51D, 29), + (0x1D51E, 0x1D537, 1), + (0x1D538, 0x1D539, 0), + (0x1D53A, 0x1D53A, 29), + (0x1D53B, 0x1D53E, 0), + (0x1D53F, 0x1D53F, 29), + (0x1D540, 0x1D544, 0), + (0x1D545, 0x1D545, 29), + (0x1D546, 0x1D546, 0), + (0x1D547, 0x1D549, 29), + (0x1D54A, 0x1D550, 0), + (0x1D551, 0x1D551, 29), + (0x1D552, 0x1D56B, 1), + (0x1D56C, 0x1D585, 0), + (0x1D586, 0x1D59F, 1), + (0x1D5A0, 0x1D5B9, 0), + (0x1D5BA, 0x1D5D3, 1), + (0x1D5D4, 0x1D5ED, 0), + (0x1D5EE, 0x1D607, 1), + (0x1D608, 0x1D621, 0), + (0x1D622, 0x1D63B, 1), + (0x1D63C, 0x1D655, 0), + (0x1D656, 0x1D66F, 1), + (0x1D670, 0x1D689, 0), + (0x1D68A, 0x1D6A6, 1), + (0x1D6A7, 0x1D6A7, 29), + (0x1D6A8, 0x1D6C0, 0), + (0x1D6C1, 0x1D6C1, 18), + (0x1D6C2, 0x1D6DA, 1), + (0x1D6DB, 0x1D6DB, 18), + (0x1D6DC, 0x1D6E1, 1), + (0x1D6E2, 0x1D6FA, 0), + (0x1D6FB, 0x1D6FB, 18), + (0x1D6FC, 0x1D714, 1), + (0x1D715, 0x1D715, 18), + (0x1D716, 0x1D71B, 1), + (0x1D71C, 0x1D734, 0), + (0x1D735, 0x1D735, 18), + (0x1D736, 0x1D74E, 1), + (0x1D74F, 0x1D74F, 18), + (0x1D750, 0x1D755, 1), + (0x1D756, 0x1D76E, 0), + (0x1D76F, 0x1D76F, 18), + (0x1D770, 0x1D788, 1), + (0x1D789, 0x1D789, 18), + (0x1D78A, 0x1D78F, 1), + (0x1D790, 0x1D7A8, 0), + (0x1D7A9, 0x1D7A9, 18), + (0x1D7AA, 0x1D7C2, 1), + (0x1D7C3, 0x1D7C3, 18), + (0x1D7C4, 0x1D7C9, 1), + (0x1D7CA, 0x1D7CA, 0), + (0x1D7CB, 0x1D7CB, 1), + (0x1D7CC, 0x1D7CD, 29), + (0x1D7CE, 0x1D7FF, 8), + (0x1D800, 0x1D9FF, 21), + (0x1DA00, 0x1DA36, 5), + (0x1DA37, 0x1DA3A, 21), + (0x1DA3B, 0x1DA6C, 5), + (0x1DA6D, 0x1DA74, 21), + (0x1DA75, 0x1DA75, 5), + (0x1DA76, 0x1DA83, 21), + (0x1DA84, 0x1DA84, 5), + (0x1DA85, 0x1DA86, 21), + (0x1DA87, 0x1DA8B, 17), + (0x1DA8C, 0x1DA9A, 29), + (0x1DA9B, 0x1DA9F, 5), + (0x1DAA0, 0x1DAA0, 29), + (0x1DAA1, 0x1DAAF, 5), + (0x1DAB0, 0x1DAFF, 29), + (0x1DB00, 0x1DB1C, 18), + (0x1DB1D, 0x1DEFF, 29), + (0x1DF00, 0x1DF09, 1), + (0x1DF0A, 0x1DF0A, 4), + (0x1DF0B, 0x1DF3F, 1), + (0x1DF40, 0x1DF40, 0), + (0x1DF41, 0x1DF47, 1), + (0x1DF48, 0x1DF48, 0), + (0x1DF49, 0x1DF49, 1), + (0x1DF4A, 0x1DF4A, 0), + (0x1DF4B, 0x1DF4C, 1), + (0x1DF4D, 0x1DF4D, 0), + (0x1DF4E, 0x1DF50, 1), + (0x1DF51, 0x1DF51, 0), + (0x1DF52, 0x1DF67, 1), + (0x1DF68, 0x1DF68, 0), + (0x1DF69, 0x1DF69, 1), + (0x1DF6A, 0x1DF6A, 0), + (0x1DF6B, 0x1DF6B, 1), + (0x1DF6C, 0x1DF6C, 0), + (0x1DF6D, 0x1DF6D, 1), + (0x1DF6E, 0x1DF6E, 0), + (0x1DF6F, 0x1DF71, 1), + (0x1DF72, 0x1DF72, 0), + (0x1DF73, 0x1DF73, 1), + (0x1DF74, 0x1DF74, 0), + (0x1DF75, 0x1DF75, 1), + (0x1DF76, 0x1DF76, 0), + (0x1DF77, 0x1DF77, 1), + (0x1DF78, 0x1DF78, 0), + (0x1DF79, 0x1DF79, 1), + (0x1DF7A, 0x1DF7A, 0), + (0x1DF7B, 0x1DF7B, 1), + (0x1DF7C, 0x1DF7C, 0), + (0x1DF7D, 0x1DF7D, 1), + (0x1DF7E, 0x1DF7E, 0), + (0x1DF7F, 0x1DF7F, 1), + (0x1DF80, 0x1DF81, 4), + (0x1DF82, 0x1DF8F, 29), + (0x1DF90, 0x1DF96, 1), + (0x1DF97, 0x1DFCC, 29), + (0x1DFCD, 0x1DFFF, 3), + (0x1E000, 0x1E006, 5), + (0x1E007, 0x1E007, 29), + (0x1E008, 0x1E018, 5), + (0x1E019, 0x1E01A, 29), + (0x1E01B, 0x1E021, 5), + (0x1E022, 0x1E022, 29), + (0x1E023, 0x1E024, 5), + (0x1E025, 0x1E025, 29), + (0x1E026, 0x1E02A, 5), + (0x1E02B, 0x1E02F, 29), + (0x1E030, 0x1E06D, 3), + (0x1E06E, 0x1E08E, 29), + (0x1E08F, 0x1E08F, 5), + (0x1E090, 0x1E0FF, 29), + (0x1E100, 0x1E12C, 4), + (0x1E12D, 0x1E12F, 29), + (0x1E130, 0x1E136, 5), + (0x1E137, 0x1E13D, 3), + (0x1E13E, 0x1E13F, 29), + (0x1E140, 0x1E149, 8), + (0x1E14A, 0x1E14D, 29), + (0x1E14E, 0x1E14E, 4), + (0x1E14F, 0x1E14F, 21), + (0x1E150, 0x1E28F, 29), + (0x1E290, 0x1E2AD, 4), + (0x1E2AE, 0x1E2AE, 5), + (0x1E2AF, 0x1E2BF, 29), + (0x1E2C0, 0x1E2EB, 4), + (0x1E2EC, 0x1E2EF, 5), + (0x1E2F0, 0x1E2F9, 8), + (0x1E2FA, 0x1E2FE, 29), + (0x1E2FF, 0x1E2FF, 19), + (0x1E300, 0x1E4CF, 29), + (0x1E4D0, 0x1E4EA, 4), + (0x1E4EB, 0x1E4EB, 3), + (0x1E4EC, 0x1E4EF, 5), + (0x1E4F0, 0x1E4F9, 8), + (0x1E4FA, 0x1E5CF, 29), + (0x1E5D0, 0x1E5ED, 4), + (0x1E5EE, 0x1E5EF, 5), + (0x1E5F0, 0x1E5F0, 4), + (0x1E5F1, 0x1E5FA, 8), + (0x1E5FB, 0x1E5FE, 29), + (0x1E5FF, 0x1E5FF, 17), + (0x1E600, 0x1E6BF, 29), + (0x1E6C0, 0x1E6DE, 4), + (0x1E6DF, 0x1E6DF, 29), + (0x1E6E0, 0x1E6E2, 4), + (0x1E6E3, 0x1E6E3, 5), + (0x1E6E4, 0x1E6E5, 4), + (0x1E6E6, 0x1E6E6, 5), + (0x1E6E7, 0x1E6ED, 4), + (0x1E6EE, 0x1E6EF, 5), + (0x1E6F0, 0x1E6F4, 4), + (0x1E6F5, 0x1E6F5, 5), + (0x1E6F6, 0x1E6FD, 29), + (0x1E6FE, 0x1E6FE, 4), + (0x1E6FF, 0x1E6FF, 3), + (0x1E700, 0x1E7DF, 29), + (0x1E7E0, 0x1E7E6, 4), + (0x1E7E7, 0x1E7E7, 29), + (0x1E7E8, 0x1E7EB, 4), + (0x1E7EC, 0x1E7EC, 29), + (0x1E7ED, 0x1E7EE, 4), + (0x1E7EF, 0x1E7EF, 29), + (0x1E7F0, 0x1E7FE, 4), + (0x1E7FF, 0x1E7FF, 29), + (0x1E800, 0x1E8C4, 4), + (0x1E8C5, 0x1E8C6, 29), + (0x1E8C7, 0x1E8CF, 10), + (0x1E8D0, 0x1E8D6, 5), + (0x1E8D7, 0x1E8FF, 29), + (0x1E900, 0x1E921, 0), + (0x1E922, 0x1E943, 1), + (0x1E944, 0x1E94A, 5), + (0x1E94B, 0x1E94B, 3), + (0x1E94C, 0x1E94F, 29), + (0x1E950, 0x1E959, 8), + (0x1E95A, 0x1E95D, 29), + (0x1E95E, 0x1E95F, 17), + (0x1E960, 0x1EC70, 29), + (0x1EC71, 0x1ECAB, 10), + (0x1ECAC, 0x1ECAC, 21), + (0x1ECAD, 0x1ECAF, 10), + (0x1ECB0, 0x1ECB0, 19), + (0x1ECB1, 0x1ECB4, 10), + (0x1ECB5, 0x1ED00, 29), + (0x1ED01, 0x1ED2D, 10), + (0x1ED2E, 0x1ED2E, 21), + (0x1ED2F, 0x1ED3D, 10), + (0x1ED3E, 0x1EDFF, 29), + (0x1EE00, 0x1EE03, 4), + (0x1EE04, 0x1EE04, 29), + (0x1EE05, 0x1EE1F, 4), + (0x1EE20, 0x1EE20, 29), + (0x1EE21, 0x1EE22, 4), + (0x1EE23, 0x1EE23, 29), + (0x1EE24, 0x1EE24, 4), + (0x1EE25, 0x1EE26, 29), + (0x1EE27, 0x1EE27, 4), + (0x1EE28, 0x1EE28, 29), + (0x1EE29, 0x1EE32, 4), + (0x1EE33, 0x1EE33, 29), + (0x1EE34, 0x1EE37, 4), + (0x1EE38, 0x1EE38, 29), + (0x1EE39, 0x1EE39, 4), + (0x1EE3A, 0x1EE3A, 29), + (0x1EE3B, 0x1EE3B, 4), + (0x1EE3C, 0x1EE41, 29), + (0x1EE42, 0x1EE42, 4), + (0x1EE43, 0x1EE46, 29), + (0x1EE47, 0x1EE47, 4), + (0x1EE48, 0x1EE48, 29), + (0x1EE49, 0x1EE49, 4), + (0x1EE4A, 0x1EE4A, 29), + (0x1EE4B, 0x1EE4B, 4), + (0x1EE4C, 0x1EE4C, 29), + (0x1EE4D, 0x1EE4F, 4), + (0x1EE50, 0x1EE50, 29), + (0x1EE51, 0x1EE52, 4), + (0x1EE53, 0x1EE53, 29), + (0x1EE54, 0x1EE54, 4), + (0x1EE55, 0x1EE56, 29), + (0x1EE57, 0x1EE57, 4), + (0x1EE58, 0x1EE58, 29), + (0x1EE59, 0x1EE59, 4), + (0x1EE5A, 0x1EE5A, 29), + (0x1EE5B, 0x1EE5B, 4), + (0x1EE5C, 0x1EE5C, 29), + (0x1EE5D, 0x1EE5D, 4), + (0x1EE5E, 0x1EE5E, 29), + (0x1EE5F, 0x1EE5F, 4), + (0x1EE60, 0x1EE60, 29), + (0x1EE61, 0x1EE62, 4), + (0x1EE63, 0x1EE63, 29), + (0x1EE64, 0x1EE64, 4), + (0x1EE65, 0x1EE66, 29), + (0x1EE67, 0x1EE6A, 4), + (0x1EE6B, 0x1EE6B, 29), + (0x1EE6C, 0x1EE72, 4), + (0x1EE73, 0x1EE73, 29), + (0x1EE74, 0x1EE77, 4), + (0x1EE78, 0x1EE78, 29), + (0x1EE79, 0x1EE7C, 4), + (0x1EE7D, 0x1EE7D, 29), + (0x1EE7E, 0x1EE7E, 4), + (0x1EE7F, 0x1EE7F, 29), + (0x1EE80, 0x1EE89, 4), + (0x1EE8A, 0x1EE8A, 29), + (0x1EE8B, 0x1EE9B, 4), + (0x1EE9C, 0x1EEA0, 29), + (0x1EEA1, 0x1EEA3, 4), + (0x1EEA4, 0x1EEA4, 29), + (0x1EEA5, 0x1EEA9, 4), + (0x1EEAA, 0x1EEAA, 29), + (0x1EEAB, 0x1EEBB, 4), + (0x1EEBC, 0x1EEEF, 29), + (0x1EEF0, 0x1EEF1, 18), + (0x1EEF2, 0x1EFFF, 29), + (0x1F000, 0x1F02B, 21), + (0x1F02C, 0x1F02F, 29), + (0x1F030, 0x1F093, 21), + (0x1F094, 0x1F09F, 29), + (0x1F0A0, 0x1F0AE, 21), + (0x1F0AF, 0x1F0B0, 29), + (0x1F0B1, 0x1F0BF, 21), + (0x1F0C0, 0x1F0C0, 29), + (0x1F0C1, 0x1F0CF, 21), + (0x1F0D0, 0x1F0D0, 29), + (0x1F0D1, 0x1F0F5, 21), + (0x1F0F6, 0x1F0FF, 29), + (0x1F100, 0x1F10C, 10), + (0x1F10D, 0x1F1AE, 21), + (0x1F1AF, 0x1F1E5, 29), + (0x1F1E6, 0x1F202, 21), + (0x1F203, 0x1F20F, 29), + (0x1F210, 0x1F23B, 21), + (0x1F23C, 0x1F23F, 29), + (0x1F240, 0x1F248, 21), + (0x1F249, 0x1F24F, 29), + (0x1F250, 0x1F251, 21), + (0x1F252, 0x1F25F, 29), + (0x1F260, 0x1F265, 21), + (0x1F266, 0x1F2FF, 29), + (0x1F300, 0x1F3FA, 21), + (0x1F3FB, 0x1F3FF, 20), + (0x1F400, 0x1F6D9, 21), + (0x1F6DA, 0x1F6DB, 29), + (0x1F6DC, 0x1F6EC, 21), + (0x1F6ED, 0x1F6EF, 29), + (0x1F6F0, 0x1F6FC, 21), + (0x1F6FD, 0x1F6FF, 29), + (0x1F700, 0x1F7DB, 21), + (0x1F7DC, 0x1F7DF, 29), + (0x1F7E0, 0x1F7EB, 21), + (0x1F7EC, 0x1F7EF, 29), + (0x1F7F0, 0x1F80B, 21), + (0x1F80C, 0x1F80F, 29), + (0x1F810, 0x1F847, 21), + (0x1F848, 0x1F84F, 29), + (0x1F850, 0x1F859, 21), + (0x1F85A, 0x1F85F, 29), + (0x1F860, 0x1F887, 21), + (0x1F888, 0x1F88F, 29), + (0x1F890, 0x1F8AD, 21), + (0x1F8AE, 0x1F8AF, 29), + (0x1F8B0, 0x1F8BB, 21), + (0x1F8BC, 0x1F8BF, 29), + (0x1F8C0, 0x1F8C1, 21), + (0x1F8C2, 0x1F8CF, 29), + (0x1F8D0, 0x1F8D8, 18), + (0x1F8D9, 0x1F8FF, 29), + (0x1F900, 0x1FA57, 21), + (0x1FA58, 0x1FA5F, 29), + (0x1FA60, 0x1FA6D, 21), + (0x1FA6E, 0x1FA6F, 29), + (0x1FA70, 0x1FA7C, 21), + (0x1FA7D, 0x1FA7F, 29), + (0x1FA80, 0x1FAC6, 21), + (0x1FAC7, 0x1FAC7, 29), + (0x1FAC8, 0x1FAC8, 21), + (0x1FAC9, 0x1FACB, 29), + (0x1FACC, 0x1FADD, 21), + (0x1FADE, 0x1FADE, 29), + (0x1FADF, 0x1FAEB, 21), + (0x1FAEC, 0x1FAEE, 29), + (0x1FAEF, 0x1FAFA, 21), + (0x1FAFB, 0x1FAFF, 29), + (0x1FB00, 0x1FB92, 21), + (0x1FB93, 0x1FB93, 29), + (0x1FB94, 0x1FBEF, 21), + (0x1FBF0, 0x1FBF9, 8), + (0x1FBFA, 0x1FBFA, 21), + (0x1FBFB, 0x1FFFF, 29), + (0x20000, 0x2A6DF, 4), + (0x2A6E0, 0x2A6FF, 29), + (0x2A700, 0x2B81E, 4), + (0x2B81F, 0x2B81F, 29), + (0x2B820, 0x2CEAD, 4), + (0x2CEAE, 0x2CEAF, 29), + (0x2CEB0, 0x2EBE0, 4), + (0x2EBE1, 0x2EBEF, 29), + (0x2EBF0, 0x2EE5D, 4), + (0x2EE5E, 0x2F7FF, 29), + (0x2F800, 0x2FA1D, 4), + (0x2FA1E, 0x2FFFF, 29), + (0x30000, 0x3134A, 4), + (0x3134B, 0x3134F, 29), + (0x31350, 0x33479, 4), + (0x3347A, 0x3CFFF, 29), + (0x3D000, 0x3FC3F, 4), + (0x3FC40, 0xE0000, 29), + (0xE0001, 0xE0001, 26), + (0xE0002, 0xE001F, 29), + (0xE0020, 0xE007F, 26), + (0xE0080, 0xE00FF, 29), + (0xE0100, 0xE01EF, 5), + (0xE01F0, 0xEFFFF, 29), + (0xF0000, 0xFFFFD, 28), + (0xFFFFE, 0xFFFFF, 29), + (0x100000, 0x10FFFD, 28), + (0x10FFFE, 0x10FFFF, 29), +]; + +/// The code points with the binary property ASCII_Hex_Digit, as ranges. +pub static BINARY_ASCII_HEX_DIGIT: &[(u32, u32)] = &[(0x30, 0x39), (0x41, 0x46), (0x61, 0x66)]; + +/// The code points with the binary property Alphabetic, as ranges. +pub static BINARY_ALPHABETIC: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0x61, 0x7A), + (0xAA, 0xAA), + (0xB5, 0xB5), + (0xBA, 0xBA), + (0xC0, 0xD6), + (0xD8, 0xF6), + (0xF8, 0x2C1), + (0x2C6, 0x2D1), + (0x2E0, 0x2E4), + (0x2EC, 0x2EC), + (0x2EE, 0x2EE), + (0x345, 0x345), + (0x363, 0x374), + (0x376, 0x377), + (0x37A, 0x37D), + (0x37F, 0x37F), + (0x386, 0x386), + (0x388, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x3A1), + (0x3A3, 0x3F5), + (0x3F7, 0x481), + (0x48A, 0x52F), + (0x531, 0x556), + (0x558, 0x559), + (0x560, 0x588), + (0x58B, 0x58C), + (0x5B0, 0x5BD), + (0x5BF, 0x5BF), + (0x5C1, 0x5C2), + (0x5C4, 0x5C5), + (0x5C7, 0x5C9), + (0x5D0, 0x5EA), + (0x5EF, 0x5F2), + (0x610, 0x61A), + (0x620, 0x657), + (0x659, 0x65F), + (0x66E, 0x6D3), + (0x6D5, 0x6DC), + (0x6E1, 0x6E8), + (0x6ED, 0x6EF), + (0x6FA, 0x6FC), + (0x6FF, 0x6FF), + (0x710, 0x73F), + (0x74D, 0x7B1), + (0x7CA, 0x7EA), + (0x7F4, 0x7F5), + (0x7FA, 0x7FA), + (0x800, 0x817), + (0x81A, 0x82C), + (0x840, 0x858), + (0x860, 0x86A), + (0x870, 0x887), + (0x889, 0x88F), + (0x897, 0x897), + (0x8A0, 0x8C9), + (0x8D4, 0x8DF), + (0x8E3, 0x8E9), + (0x8F0, 0x93B), + (0x93D, 0x94C), + (0x94E, 0x950), + (0x955, 0x963), + (0x971, 0x983), + (0x985, 0x98C), + (0x98F, 0x990), + (0x993, 0x9A8), + (0x9AA, 0x9B0), + (0x9B2, 0x9B2), + (0x9B6, 0x9B9), + (0x9BD, 0x9C4), + (0x9C7, 0x9C8), + (0x9CB, 0x9CC), + (0x9CE, 0x9CE), + (0x9D7, 0x9D7), + (0x9DC, 0x9DD), + (0x9DF, 0x9E3), + (0x9F0, 0x9F1), + (0x9FC, 0x9FC), + (0xA01, 0xA03), + (0xA05, 0xA0A), + (0xA0F, 0xA10), + (0xA13, 0xA28), + (0xA2A, 0xA30), + (0xA32, 0xA33), + (0xA35, 0xA36), + (0xA38, 0xA39), + (0xA3E, 0xA42), + (0xA47, 0xA48), + (0xA4B, 0xA4C), + (0xA51, 0xA51), + (0xA59, 0xA5C), + (0xA5E, 0xA5E), + (0xA70, 0xA75), + (0xA81, 0xA83), + (0xA85, 0xA8D), + (0xA8F, 0xA91), + (0xA93, 0xAA8), + (0xAAA, 0xAB0), + (0xAB2, 0xAB3), + (0xAB5, 0xAB9), + (0xABD, 0xAC5), + (0xAC7, 0xAC9), + (0xACB, 0xACC), + (0xAD0, 0xAD0), + (0xAE0, 0xAE3), + (0xAF9, 0xAFC), + (0xB01, 0xB03), + (0xB05, 0xB0C), + (0xB0F, 0xB10), + (0xB13, 0xB28), + (0xB2A, 0xB30), + (0xB32, 0xB33), + (0xB35, 0xB39), + (0xB3D, 0xB44), + (0xB47, 0xB48), + (0xB4B, 0xB4C), + (0xB56, 0xB57), + (0xB5C, 0xB5D), + (0xB5F, 0xB63), + (0xB71, 0xB71), + (0xB82, 0xB83), + (0xB85, 0xB8A), + (0xB8E, 0xB90), + (0xB92, 0xB95), + (0xB99, 0xB9A), + (0xB9C, 0xB9C), + (0xB9E, 0xB9F), + (0xBA3, 0xBA4), + (0xBA8, 0xBAA), + (0xBAE, 0xBB9), + (0xBBE, 0xBC2), + (0xBC6, 0xBC8), + (0xBCA, 0xBCC), + (0xBD0, 0xBD0), + (0xBD7, 0xBD7), + (0xC00, 0xC0C), + (0xC0E, 0xC10), + (0xC12, 0xC28), + (0xC2A, 0xC39), + (0xC3D, 0xC44), + (0xC46, 0xC48), + (0xC4A, 0xC4C), + (0xC55, 0xC56), + (0xC58, 0xC5A), + (0xC5C, 0xC5D), + (0xC60, 0xC63), + (0xC80, 0xC83), + (0xC85, 0xC8C), + (0xC8E, 0xC90), + (0xC92, 0xCA8), + (0xCAA, 0xCB3), + (0xCB5, 0xCB9), + (0xCBD, 0xCC4), + (0xCC6, 0xCC8), + (0xCCA, 0xCCC), + (0xCD5, 0xCD6), + (0xCDC, 0xCDE), + (0xCE0, 0xCE3), + (0xCF1, 0xCF3), + (0xD00, 0xD0C), + (0xD0E, 0xD10), + (0xD12, 0xD3A), + (0xD3D, 0xD44), + (0xD46, 0xD48), + (0xD4A, 0xD4C), + (0xD4E, 0xD4E), + (0xD54, 0xD57), + (0xD5F, 0xD63), + (0xD7A, 0xD7F), + (0xD81, 0xD83), + (0xD85, 0xD96), + (0xD9A, 0xDB1), + (0xDB3, 0xDBB), + (0xDBD, 0xDBD), + (0xDC0, 0xDC6), + (0xDCF, 0xDD4), + (0xDD6, 0xDD6), + (0xDD8, 0xDDF), + (0xDF2, 0xDF3), + (0xE01, 0xE3A), + (0xE40, 0xE46), + (0xE4D, 0xE4D), + (0xE81, 0xE82), + (0xE84, 0xE84), + (0xE86, 0xE8A), + (0xE8C, 0xEA3), + (0xEA5, 0xEA5), + (0xEA7, 0xEB9), + (0xEBB, 0xEBD), + (0xEC0, 0xEC4), + (0xEC6, 0xEC6), + (0xECD, 0xECD), + (0xEDC, 0xEDF), + (0xF00, 0xF00), + (0xF40, 0xF47), + (0xF49, 0xF6C), + (0xF71, 0xF83), + (0xF88, 0xF97), + (0xF99, 0xFBC), + (0x1000, 0x1036), + (0x1038, 0x1038), + (0x103B, 0x103F), + (0x1050, 0x108F), + (0x109A, 0x109D), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10D0, 0x10FA), + (0x10FC, 0x1248), + (0x124A, 0x124D), + (0x1250, 0x1256), + (0x1258, 0x1258), + (0x125A, 0x125D), + (0x1260, 0x1288), + (0x128A, 0x128D), + (0x1290, 0x12B0), + (0x12B2, 0x12B5), + (0x12B8, 0x12BE), + (0x12C0, 0x12C0), + (0x12C2, 0x12C5), + (0x12C8, 0x12D6), + (0x12D8, 0x1310), + (0x1312, 0x1315), + (0x1318, 0x135A), + (0x1380, 0x138F), + (0x13A0, 0x13F5), + (0x13F8, 0x13FD), + (0x1401, 0x166C), + (0x166F, 0x167F), + (0x1681, 0x169A), + (0x16A0, 0x16EA), + (0x16EE, 0x16F8), + (0x1700, 0x1713), + (0x171F, 0x1733), + (0x1740, 0x1753), + (0x1760, 0x176C), + (0x176E, 0x1770), + (0x1772, 0x1773), + (0x1780, 0x17B3), + (0x17B6, 0x17C8), + (0x17D7, 0x17D7), + (0x17DC, 0x17DC), + (0x1820, 0x1878), + (0x1880, 0x18AA), + (0x18B0, 0x18F5), + (0x1900, 0x191E), + (0x1920, 0x192B), + (0x1930, 0x1938), + (0x1950, 0x196D), + (0x1970, 0x1974), + (0x1980, 0x19AB), + (0x19B0, 0x19C9), + (0x1A00, 0x1A1B), + (0x1A20, 0x1A5E), + (0x1A61, 0x1A74), + (0x1AA7, 0x1AA7), + (0x1ABF, 0x1AC0), + (0x1ACC, 0x1ACE), + (0x1B00, 0x1B33), + (0x1B35, 0x1B43), + (0x1B45, 0x1B4C), + (0x1B80, 0x1BA9), + (0x1BAC, 0x1BAF), + (0x1BBA, 0x1BE5), + (0x1BE7, 0x1BF1), + (0x1C00, 0x1C36), + (0x1C4D, 0x1C4F), + (0x1C5A, 0x1C7D), + (0x1C80, 0x1C8A), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1CE9, 0x1CEC), + (0x1CEE, 0x1CF3), + (0x1CF5, 0x1CF6), + (0x1CFA, 0x1CFA), + (0x1D00, 0x1DBF), + (0x1DD3, 0x1DF4), + (0x1E00, 0x1F15), + (0x1F18, 0x1F1D), + (0x1F20, 0x1F45), + (0x1F48, 0x1F4D), + (0x1F50, 0x1F57), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FBC), + (0x1FBE, 0x1FBE), + (0x1FC2, 0x1FC4), + (0x1FC6, 0x1FCC), + (0x1FD0, 0x1FD3), + (0x1FD6, 0x1FDB), + (0x1FE0, 0x1FEC), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FFC), + (0x2071, 0x2071), + (0x207F, 0x207F), + (0x208F, 0x209F), + (0x2102, 0x2102), + (0x2107, 0x2107), + (0x210A, 0x2113), + (0x2115, 0x2115), + (0x2119, 0x211D), + (0x2124, 0x2124), + (0x2126, 0x2126), + (0x2128, 0x2128), + (0x212A, 0x212D), + (0x212F, 0x2139), + (0x213C, 0x213F), + (0x2145, 0x2149), + (0x214E, 0x214E), + (0x2160, 0x2188), + (0x24B6, 0x24E9), + (0x2C00, 0x2CE4), + (0x2CEB, 0x2CEE), + (0x2CF2, 0x2CF3), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0x2D30, 0x2D67), + (0x2D6F, 0x2D6F), + (0x2D80, 0x2D96), + (0x2DA0, 0x2DA6), + (0x2DA8, 0x2DAE), + (0x2DB0, 0x2DB6), + (0x2DB8, 0x2DBE), + (0x2DC0, 0x2DC6), + (0x2DC8, 0x2DCE), + (0x2DD0, 0x2DD6), + (0x2DD8, 0x2DDE), + (0x2DE0, 0x2DFF), + (0x2E2F, 0x2E2F), + (0x3005, 0x3007), + (0x3021, 0x3029), + (0x3031, 0x3035), + (0x3038, 0x303C), + (0x3041, 0x3096), + (0x309D, 0x309F), + (0x30A1, 0x30FA), + (0x30FC, 0x30FF), + (0x3105, 0x312F), + (0x3131, 0x318E), + (0x31A0, 0x31BF), + (0x31F0, 0x31FF), + (0x3400, 0x4DBF), + (0x4E00, 0xA48C), + (0xA4D0, 0xA4FD), + (0xA500, 0xA60C), + (0xA610, 0xA61F), + (0xA62A, 0xA62B), + (0xA640, 0xA66E), + (0xA674, 0xA67B), + (0xA67F, 0xA6EF), + (0xA717, 0xA71F), + (0xA722, 0xA788), + (0xA78B, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F1, 0xA805), + (0xA807, 0xA827), + (0xA840, 0xA873), + (0xA880, 0xA8C3), + (0xA8C5, 0xA8C5), + (0xA8F2, 0xA8F7), + (0xA8FB, 0xA8FB), + (0xA8FD, 0xA8FF), + (0xA90A, 0xA92A), + (0xA930, 0xA952), + (0xA960, 0xA97C), + (0xA980, 0xA9B2), + (0xA9B4, 0xA9BF), + (0xA9CF, 0xA9CF), + (0xA9E0, 0xA9EF), + (0xA9FA, 0xA9FE), + (0xAA00, 0xAA36), + (0xAA40, 0xAA4D), + (0xAA60, 0xAA76), + (0xAA7A, 0xAABE), + (0xAAC0, 0xAAC0), + (0xAAC2, 0xAAC2), + (0xAADB, 0xAADD), + (0xAAE0, 0xAAEF), + (0xAAF2, 0xAAF5), + (0xAB01, 0xAB06), + (0xAB09, 0xAB0E), + (0xAB11, 0xAB16), + (0xAB20, 0xAB26), + (0xAB28, 0xAB2E), + (0xAB30, 0xAB5A), + (0xAB5C, 0xAB69), + (0xAB6C, 0xAB6D), + (0xAB70, 0xABEA), + (0xAC00, 0xD7A3), + (0xD7B0, 0xD7C6), + (0xD7CB, 0xD7FB), + (0xF900, 0xFA6D), + (0xFA70, 0xFAD9), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFB1D, 0xFB28), + (0xFB2A, 0xFB36), + (0xFB38, 0xFB3C), + (0xFB3E, 0xFB3E), + (0xFB40, 0xFB41), + (0xFB43, 0xFB44), + (0xFB46, 0xFBB1), + (0xFBD3, 0xFD3D), + (0xFD50, 0xFD8F), + (0xFD92, 0xFDC7), + (0xFDF0, 0xFDFB), + (0xFE70, 0xFE74), + (0xFE76, 0xFEFC), + (0xFF21, 0xFF3A), + (0xFF41, 0xFF5A), + (0xFF66, 0xFFBE), + (0xFFC2, 0xFFC7), + (0xFFCA, 0xFFCF), + (0xFFD2, 0xFFD7), + (0xFFDA, 0xFFDC), + (0x10000, 0x1000B), + (0x1000D, 0x10026), + (0x10028, 0x1003A), + (0x1003C, 0x1003D), + (0x1003F, 0x1004D), + (0x10050, 0x1005D), + (0x10080, 0x100FA), + (0x10140, 0x10174), + (0x10280, 0x1029C), + (0x102A0, 0x102D0), + (0x10300, 0x1031F), + (0x1032D, 0x1034A), + (0x10350, 0x1037A), + (0x10380, 0x1039D), + (0x103A0, 0x103C3), + (0x103C8, 0x103CF), + (0x103D1, 0x103D5), + (0x10400, 0x1049D), + (0x104B0, 0x104D3), + (0x104D8, 0x104FB), + (0x10500, 0x10527), + (0x10530, 0x10563), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x105C0, 0x105F3), + (0x10600, 0x10736), + (0x10740, 0x10755), + (0x10760, 0x10767), + (0x10780, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x10800, 0x10805), + (0x10808, 0x10808), + (0x1080A, 0x10835), + (0x10837, 0x10838), + (0x1083C, 0x1083C), + (0x1083F, 0x10855), + (0x10860, 0x10876), + (0x10880, 0x1089E), + (0x108E0, 0x108F2), + (0x108F4, 0x108F5), + (0x10900, 0x10915), + (0x10920, 0x10939), + (0x10940, 0x10959), + (0x10980, 0x109B7), + (0x109BE, 0x109BF), + (0x10A00, 0x10A03), + (0x10A05, 0x10A06), + (0x10A0C, 0x10A13), + (0x10A15, 0x10A17), + (0x10A19, 0x10A35), + (0x10A60, 0x10A7C), + (0x10A80, 0x10A9C), + (0x10AC0, 0x10AC7), + (0x10AC9, 0x10AE4), + (0x10B00, 0x10B35), + (0x10B40, 0x10B55), + (0x10B60, 0x10B72), + (0x10B80, 0x10B91), + (0x10C00, 0x10C48), + (0x10C80, 0x10CB2), + (0x10CC0, 0x10CF2), + (0x10D00, 0x10D27), + (0x10D4A, 0x10D65), + (0x10D69, 0x10D69), + (0x10D6F, 0x10D85), + (0x10E80, 0x10EA9), + (0x10EAB, 0x10EAC), + (0x10EB0, 0x10EB1), + (0x10EC2, 0x10EC7), + (0x10ECB, 0x10ECD), + (0x10ED9, 0x10EEE), + (0x10EF3, 0x10EF3), + (0x10EF5, 0x10EF5), + (0x10EF7, 0x10EF8), + (0x10EFA, 0x10EFC), + (0x10F00, 0x10F1C), + (0x10F27, 0x10F27), + (0x10F30, 0x10F45), + (0x10F70, 0x10F81), + (0x10FB0, 0x10FC4), + (0x10FE0, 0x10FF6), + (0x11000, 0x11045), + (0x11071, 0x11075), + (0x11080, 0x110B8), + (0x110C2, 0x110C2), + (0x110D0, 0x110E8), + (0x11100, 0x11132), + (0x11144, 0x11147), + (0x11150, 0x11172), + (0x11176, 0x11176), + (0x11180, 0x111BF), + (0x111C1, 0x111C4), + (0x111CE, 0x111CF), + (0x111DA, 0x111DA), + (0x111DC, 0x111DC), + (0x11200, 0x11211), + (0x11213, 0x11234), + (0x11237, 0x11237), + (0x1123E, 0x11241), + (0x11280, 0x11286), + (0x11288, 0x11288), + (0x1128A, 0x1128D), + (0x1128F, 0x1129D), + (0x1129F, 0x112A8), + (0x112B0, 0x112E8), + (0x11300, 0x11303), + (0x11305, 0x1130C), + (0x1130F, 0x11310), + (0x11313, 0x11328), + (0x1132A, 0x11330), + (0x11332, 0x11333), + (0x11335, 0x11339), + (0x1133D, 0x11344), + (0x11347, 0x11348), + (0x1134B, 0x1134C), + (0x11350, 0x11350), + (0x11357, 0x11357), + (0x1135D, 0x11363), + (0x11380, 0x11389), + (0x1138B, 0x1138B), + (0x1138E, 0x1138E), + (0x11390, 0x113B5), + (0x113B7, 0x113C0), + (0x113C2, 0x113C2), + (0x113C5, 0x113C5), + (0x113C7, 0x113CA), + (0x113CC, 0x113CD), + (0x113D1, 0x113D1), + (0x113D3, 0x113D3), + (0x11400, 0x11441), + (0x11443, 0x11445), + (0x11447, 0x1144A), + (0x1145F, 0x11461), + (0x11480, 0x114C1), + (0x114C4, 0x114C5), + (0x114C7, 0x114C7), + (0x11580, 0x115B5), + (0x115B8, 0x115BE), + (0x115D8, 0x115DD), + (0x11600, 0x1163E), + (0x11640, 0x11640), + (0x11644, 0x11644), + (0x11680, 0x116B5), + (0x116B8, 0x116B8), + (0x11700, 0x1171A), + (0x1171D, 0x1172A), + (0x11740, 0x11746), + (0x11800, 0x11838), + (0x118A0, 0x118DF), + (0x118FF, 0x11906), + (0x11909, 0x11909), + (0x1190C, 0x11913), + (0x11915, 0x11916), + (0x11918, 0x11935), + (0x11937, 0x11938), + (0x1193B, 0x1193C), + (0x1193F, 0x11942), + (0x119A0, 0x119A7), + (0x119AA, 0x119D7), + (0x119DA, 0x119DF), + (0x119E1, 0x119E1), + (0x119E3, 0x119E4), + (0x11A00, 0x11A32), + (0x11A35, 0x11A3E), + (0x11A50, 0x11A97), + (0x11A9D, 0x11A9D), + (0x11AB0, 0x11AF8), + (0x11B0A, 0x11B0A), + (0x11B60, 0x11B67), + (0x11BC0, 0x11BE0), + (0x11C00, 0x11C08), + (0x11C0A, 0x11C36), + (0x11C38, 0x11C3E), + (0x11C40, 0x11C40), + (0x11C72, 0x11C8F), + (0x11C92, 0x11CA7), + (0x11CA9, 0x11CB6), + (0x11D00, 0x11D06), + (0x11D08, 0x11D09), + (0x11D0B, 0x11D36), + (0x11D3A, 0x11D3A), + (0x11D3C, 0x11D3D), + (0x11D3F, 0x11D41), + (0x11D43, 0x11D43), + (0x11D46, 0x11D47), + (0x11D60, 0x11D65), + (0x11D67, 0x11D68), + (0x11D6A, 0x11D8E), + (0x11D90, 0x11D91), + (0x11D93, 0x11D96), + (0x11D98, 0x11D98), + (0x11DB0, 0x11DDB), + (0x11DF0, 0x11DF1), + (0x11EE0, 0x11EF6), + (0x11F00, 0x11F10), + (0x11F12, 0x11F3A), + (0x11F3E, 0x11F40), + (0x11FB0, 0x11FB0), + (0x12000, 0x12399), + (0x12400, 0x1246F), + (0x12475, 0x12543), + (0x12550, 0x12686), + (0x12F90, 0x12FF0), + (0x13000, 0x1342F), + (0x13441, 0x13446), + (0x13460, 0x143FA), + (0x14400, 0x14646), + (0x16100, 0x1612E), + (0x16800, 0x16A38), + (0x16A40, 0x16A5E), + (0x16A70, 0x16ABE), + (0x16AD0, 0x16AED), + (0x16B00, 0x16B2F), + (0x16B40, 0x16B43), + (0x16B63, 0x16B77), + (0x16B7D, 0x16B8F), + (0x16D40, 0x16D6C), + (0x16E40, 0x16E7F), + (0x16EA0, 0x16EB8), + (0x16EBB, 0x16ED3), + (0x16F00, 0x16F4A), + (0x16F4F, 0x16F87), + (0x16F8F, 0x16F9F), + (0x16FE0, 0x16FE1), + (0x16FE3, 0x16FE3), + (0x16FF0, 0x16FF6), + (0x17000, 0x18CDA), + (0x18CFF, 0x18D20), + (0x18D80, 0x18DF2), + (0x18E00, 0x19191), + (0x191A0, 0x191D2), + (0x1AFF0, 0x1AFF3), + (0x1AFF5, 0x1AFFB), + (0x1AFFD, 0x1AFFE), + (0x1B000, 0x1B128), + (0x1B132, 0x1B132), + (0x1B150, 0x1B152), + (0x1B155, 0x1B155), + (0x1B164, 0x1B168), + (0x1B170, 0x1B2FB), + (0x1BC00, 0x1BC6A), + (0x1BC70, 0x1BC7C), + (0x1BC80, 0x1BC88), + (0x1BC90, 0x1BC99), + (0x1BC9E, 0x1BC9E), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D6C0), + (0x1D6C2, 0x1D6DA), + (0x1D6DC, 0x1D6FA), + (0x1D6FC, 0x1D714), + (0x1D716, 0x1D734), + (0x1D736, 0x1D74E), + (0x1D750, 0x1D76E), + (0x1D770, 0x1D788), + (0x1D78A, 0x1D7A8), + (0x1D7AA, 0x1D7C2), + (0x1D7C4, 0x1D7CB), + (0x1DF00, 0x1DF81), + (0x1DF90, 0x1DF96), + (0x1DFCD, 0x1E006), + (0x1E008, 0x1E018), + (0x1E01B, 0x1E021), + (0x1E023, 0x1E024), + (0x1E026, 0x1E02A), + (0x1E030, 0x1E06D), + (0x1E08F, 0x1E08F), + (0x1E100, 0x1E12C), + (0x1E137, 0x1E13D), + (0x1E14E, 0x1E14E), + (0x1E290, 0x1E2AD), + (0x1E2C0, 0x1E2EB), + (0x1E4D0, 0x1E4EB), + (0x1E5D0, 0x1E5ED), + (0x1E5F0, 0x1E5F0), + (0x1E6C0, 0x1E6DE), + (0x1E6E0, 0x1E6F5), + (0x1E6FE, 0x1E6FF), + (0x1E7E0, 0x1E7E6), + (0x1E7E8, 0x1E7EB), + (0x1E7ED, 0x1E7EE), + (0x1E7F0, 0x1E7FE), + (0x1E800, 0x1E8C4), + (0x1E900, 0x1E943), + (0x1E947, 0x1E947), + (0x1E94B, 0x1E94B), + (0x1EE00, 0x1EE03), + (0x1EE05, 0x1EE1F), + (0x1EE21, 0x1EE22), + (0x1EE24, 0x1EE24), + (0x1EE27, 0x1EE27), + (0x1EE29, 0x1EE32), + (0x1EE34, 0x1EE37), + (0x1EE39, 0x1EE39), + (0x1EE3B, 0x1EE3B), + (0x1EE42, 0x1EE42), + (0x1EE47, 0x1EE47), + (0x1EE49, 0x1EE49), + (0x1EE4B, 0x1EE4B), + (0x1EE4D, 0x1EE4F), + (0x1EE51, 0x1EE52), + (0x1EE54, 0x1EE54), + (0x1EE57, 0x1EE57), + (0x1EE59, 0x1EE59), + (0x1EE5B, 0x1EE5B), + (0x1EE5D, 0x1EE5D), + (0x1EE5F, 0x1EE5F), + (0x1EE61, 0x1EE62), + (0x1EE64, 0x1EE64), + (0x1EE67, 0x1EE6A), + (0x1EE6C, 0x1EE72), + (0x1EE74, 0x1EE77), + (0x1EE79, 0x1EE7C), + (0x1EE7E, 0x1EE7E), + (0x1EE80, 0x1EE89), + (0x1EE8B, 0x1EE9B), + (0x1EEA1, 0x1EEA3), + (0x1EEA5, 0x1EEA9), + (0x1EEAB, 0x1EEBB), + (0x1F130, 0x1F149), + (0x1F150, 0x1F169), + (0x1F170, 0x1F189), + (0x20000, 0x2A6DF), + (0x2A700, 0x2B81E), + (0x2B820, 0x2CEAD), + (0x2CEB0, 0x2EBE0), + (0x2EBF0, 0x2EE5D), + (0x2F800, 0x2FA1D), + (0x30000, 0x3134A), + (0x31350, 0x33479), + (0x3D000, 0x3FC3F), +]; + +/// The code points with the binary property Bidi_Control, as ranges. +pub static BINARY_BIDI_CONTROL: &[(u32, u32)] = &[ + (0x61C, 0x61C), + (0x200E, 0x200F), + (0x202A, 0x202E), + (0x2066, 0x2069), +]; + +/// The code points with the binary property Bidi_Mirrored, as ranges. +pub static BINARY_BIDI_MIRRORED: &[(u32, u32)] = &[ + (0x28, 0x29), + (0x3C, 0x3C), + (0x3E, 0x3E), + (0x5B, 0x5B), + (0x5D, 0x5D), + (0x7B, 0x7B), + (0x7D, 0x7D), + (0xAB, 0xAB), + (0xBB, 0xBB), + (0xF3A, 0xF3D), + (0x169B, 0x169C), + (0x2039, 0x203A), + (0x2045, 0x2046), + (0x207D, 0x207E), + (0x208D, 0x208E), + (0x2140, 0x2140), + (0x2201, 0x2204), + (0x2208, 0x220D), + (0x2211, 0x2211), + (0x2215, 0x2216), + (0x221A, 0x221D), + (0x221F, 0x2222), + (0x2224, 0x2224), + (0x2226, 0x2226), + (0x222B, 0x2233), + (0x2239, 0x2239), + (0x223B, 0x224C), + (0x2252, 0x2255), + (0x225F, 0x2260), + (0x2262, 0x2262), + (0x2264, 0x226B), + (0x226D, 0x228C), + (0x228F, 0x2292), + (0x2298, 0x2298), + (0x22A2, 0x22A3), + (0x22A6, 0x22B8), + (0x22BE, 0x22BF), + (0x22C9, 0x22CD), + (0x22D0, 0x22D1), + (0x22D6, 0x22ED), + (0x22F0, 0x22FF), + (0x2308, 0x230B), + (0x2320, 0x2321), + (0x2329, 0x232A), + (0x2768, 0x2775), + (0x27C0, 0x27C0), + (0x27C3, 0x27C6), + (0x27C8, 0x27C9), + (0x27CB, 0x27CD), + (0x27D3, 0x27D6), + (0x27DC, 0x27DE), + (0x27E2, 0x27EF), + (0x2983, 0x2998), + (0x299B, 0x29A0), + (0x29A2, 0x29AF), + (0x29B8, 0x29B8), + (0x29C0, 0x29C5), + (0x29C9, 0x29C9), + (0x29CE, 0x29D2), + (0x29D4, 0x29D5), + (0x29D8, 0x29DC), + (0x29E1, 0x29E1), + (0x29E3, 0x29E5), + (0x29E8, 0x29E9), + (0x29F4, 0x29F9), + (0x29FC, 0x29FD), + (0x2A0A, 0x2A1C), + (0x2A1E, 0x2A21), + (0x2A24, 0x2A24), + (0x2A26, 0x2A26), + (0x2A29, 0x2A29), + (0x2A2B, 0x2A2E), + (0x2A34, 0x2A35), + (0x2A3C, 0x2A3E), + (0x2A57, 0x2A58), + (0x2A64, 0x2A65), + (0x2A6A, 0x2A6D), + (0x2A6F, 0x2A70), + (0x2A73, 0x2A74), + (0x2A79, 0x2AA3), + (0x2AA6, 0x2AAD), + (0x2AAF, 0x2AD6), + (0x2ADC, 0x2ADC), + (0x2ADE, 0x2ADE), + (0x2AE2, 0x2AE6), + (0x2AEC, 0x2AEE), + (0x2AF3, 0x2AF3), + (0x2AF7, 0x2AFB), + (0x2AFD, 0x2AFD), + (0x2BFE, 0x2BFE), + (0x2E02, 0x2E05), + (0x2E09, 0x2E0A), + (0x2E0C, 0x2E0D), + (0x2E1C, 0x2E1D), + (0x2E20, 0x2E29), + (0x2E55, 0x2E5C), + (0x2E62, 0x2E63), + (0x3008, 0x3011), + (0x3014, 0x301B), + (0xFE59, 0xFE5E), + (0xFE64, 0xFE65), + (0xFF08, 0xFF09), + (0xFF1C, 0xFF1C), + (0xFF1E, 0xFF1E), + (0xFF3B, 0xFF3B), + (0xFF3D, 0xFF3D), + (0xFF5B, 0xFF5B), + (0xFF5D, 0xFF5D), + (0xFF5F, 0xFF60), + (0xFF62, 0xFF63), + (0x1CEDD, 0x1CEDF), + (0x1D6DB, 0x1D6DB), + (0x1D715, 0x1D715), + (0x1D74F, 0x1D74F), + (0x1D789, 0x1D789), + (0x1D7C3, 0x1D7C3), + (0x1DB03, 0x1DB09), + (0x1DB10, 0x1DB10), + (0x1DB17, 0x1DB18), + (0x1DB1B, 0x1DB1B), +]; + +/// The code points with the binary property Case_Ignorable, as ranges. +pub static BINARY_CASE_IGNORABLE: &[(u32, u32)] = &[ + (0x27, 0x27), + (0x2E, 0x2E), + (0x3A, 0x3A), + (0x5E, 0x5E), + (0x60, 0x60), + (0xA8, 0xA8), + (0xAD, 0xAD), + (0xAF, 0xAF), + (0xB4, 0xB4), + (0xB7, 0xB8), + (0x2B0, 0x36F), + (0x374, 0x375), + (0x37A, 0x37A), + (0x384, 0x385), + (0x387, 0x387), + (0x483, 0x489), + (0x558, 0x559), + (0x55F, 0x55F), + (0x58B, 0x58C), + (0x591, 0x5BD), + (0x5BF, 0x5BF), + (0x5C1, 0x5C2), + (0x5C4, 0x5C5), + (0x5C7, 0x5C9), + (0x5F4, 0x5F4), + (0x600, 0x605), + (0x610, 0x61A), + (0x61C, 0x61C), + (0x640, 0x640), + (0x64B, 0x65F), + (0x670, 0x670), + (0x6D6, 0x6DD), + (0x6DF, 0x6E8), + (0x6EA, 0x6ED), + (0x70F, 0x70F), + (0x711, 0x711), + (0x730, 0x74A), + (0x7A6, 0x7B0), + (0x7EB, 0x7F5), + (0x7FA, 0x7FA), + (0x7FD, 0x7FD), + (0x816, 0x82D), + (0x859, 0x85B), + (0x888, 0x888), + (0x890, 0x891), + (0x897, 0x89F), + (0x8C9, 0x902), + (0x93A, 0x93A), + (0x93C, 0x93C), + (0x941, 0x948), + (0x94D, 0x94D), + (0x951, 0x957), + (0x962, 0x963), + (0x971, 0x971), + (0x981, 0x981), + (0x9BC, 0x9BC), + (0x9C1, 0x9C4), + (0x9CD, 0x9CD), + (0x9E2, 0x9E3), + (0x9FE, 0x9FE), + (0xA01, 0xA02), + (0xA3C, 0xA3C), + (0xA41, 0xA42), + (0xA47, 0xA48), + (0xA4B, 0xA4D), + (0xA51, 0xA51), + (0xA70, 0xA71), + (0xA75, 0xA75), + (0xA81, 0xA82), + (0xABC, 0xABC), + (0xAC1, 0xAC5), + (0xAC7, 0xAC8), + (0xACD, 0xACD), + (0xAE2, 0xAE3), + (0xAFA, 0xAFF), + (0xB01, 0xB01), + (0xB3C, 0xB3C), + (0xB3F, 0xB3F), + (0xB41, 0xB44), + (0xB4D, 0xB4D), + (0xB53, 0xB56), + (0xB62, 0xB63), + (0xB82, 0xB82), + (0xBC0, 0xBC0), + (0xBCD, 0xBCD), + (0xC00, 0xC00), + (0xC04, 0xC04), + (0xC3C, 0xC3C), + (0xC3E, 0xC40), + (0xC46, 0xC48), + (0xC4A, 0xC4D), + (0xC55, 0xC56), + (0xC62, 0xC63), + (0xC81, 0xC81), + (0xCBC, 0xCBC), + (0xCBF, 0xCBF), + (0xCC6, 0xCC6), + (0xCCC, 0xCCD), + (0xCE2, 0xCE3), + (0xD00, 0xD01), + (0xD3B, 0xD3C), + (0xD41, 0xD44), + (0xD4D, 0xD4D), + (0xD62, 0xD63), + (0xD81, 0xD81), + (0xDCA, 0xDCA), + (0xDD2, 0xDD4), + (0xDD6, 0xDD6), + (0xE31, 0xE31), + (0xE34, 0xE3A), + (0xE46, 0xE4E), + (0xEB1, 0xEB1), + (0xEB4, 0xEBC), + (0xEC6, 0xEC6), + (0xEC8, 0xECE), + (0xF18, 0xF19), + (0xF35, 0xF35), + (0xF37, 0xF37), + (0xF39, 0xF39), + (0xF71, 0xF7E), + (0xF80, 0xF84), + (0xF86, 0xF87), + (0xF8D, 0xF97), + (0xF99, 0xFBC), + (0xFC6, 0xFC6), + (0x102D, 0x1030), + (0x1032, 0x1037), + (0x1039, 0x103A), + (0x103D, 0x103E), + (0x1058, 0x1059), + (0x105E, 0x1060), + (0x1071, 0x1074), + (0x1082, 0x1082), + (0x1085, 0x1086), + (0x108D, 0x108D), + (0x109D, 0x109D), + (0x10FC, 0x10FC), + (0x135D, 0x135F), + (0x1712, 0x1714), + (0x1732, 0x1733), + (0x1752, 0x1753), + (0x1772, 0x1773), + (0x17B4, 0x17B5), + (0x17B7, 0x17BD), + (0x17C6, 0x17C6), + (0x17C9, 0x17D3), + (0x17D7, 0x17D7), + (0x17DD, 0x17DD), + (0x180B, 0x180F), + (0x1843, 0x1843), + (0x1885, 0x1886), + (0x18A9, 0x18A9), + (0x1920, 0x1922), + (0x1927, 0x1928), + (0x1932, 0x1932), + (0x1939, 0x193B), + (0x1A17, 0x1A18), + (0x1A1B, 0x1A1B), + (0x1A56, 0x1A56), + (0x1A58, 0x1A5E), + (0x1A60, 0x1A60), + (0x1A62, 0x1A62), + (0x1A65, 0x1A6C), + (0x1A73, 0x1A7C), + (0x1A7F, 0x1A7F), + (0x1AA7, 0x1AA7), + (0x1AB0, 0x1AF0), + (0x1B00, 0x1B03), + (0x1B34, 0x1B34), + (0x1B36, 0x1B3A), + (0x1B3C, 0x1B3C), + (0x1B42, 0x1B42), + (0x1B6B, 0x1B73), + (0x1B80, 0x1B81), + (0x1BA2, 0x1BA5), + (0x1BA8, 0x1BA9), + (0x1BAB, 0x1BAD), + (0x1BE6, 0x1BE6), + (0x1BE8, 0x1BE9), + (0x1BED, 0x1BED), + (0x1BEF, 0x1BF1), + (0x1C2C, 0x1C33), + (0x1C36, 0x1C37), + (0x1C78, 0x1C7D), + (0x1CD0, 0x1CD2), + (0x1CD4, 0x1CE0), + (0x1CE2, 0x1CE8), + (0x1CED, 0x1CED), + (0x1CF4, 0x1CF4), + (0x1CF8, 0x1CF9), + (0x1D2C, 0x1D6A), + (0x1D78, 0x1D78), + (0x1D9B, 0x1DFF), + (0x1FBD, 0x1FBD), + (0x1FBF, 0x1FC1), + (0x1FCD, 0x1FCF), + (0x1FDD, 0x1FDF), + (0x1FED, 0x1FEF), + (0x1FFD, 0x1FFE), + (0x200B, 0x200F), + (0x2018, 0x2019), + (0x2024, 0x2024), + (0x2027, 0x2027), + (0x202A, 0x202E), + (0x2060, 0x2064), + (0x2066, 0x206F), + (0x2071, 0x2071), + (0x207F, 0x207F), + (0x208F, 0x209F), + (0x20D0, 0x20F0), + (0x2C7C, 0x2C7D), + (0x2CEF, 0x2CF1), + (0x2D6F, 0x2D6F), + (0x2D7F, 0x2D7F), + (0x2DE0, 0x2DFF), + (0x2E2F, 0x2E2F), + (0x3005, 0x3005), + (0x302A, 0x302D), + (0x3031, 0x3035), + (0x303B, 0x303B), + (0x3099, 0x309E), + (0x30FC, 0x30FE), + (0xA015, 0xA015), + (0xA4F8, 0xA4FD), + (0xA60C, 0xA60C), + (0xA66F, 0xA672), + (0xA674, 0xA67D), + (0xA67F, 0xA67F), + (0xA69C, 0xA69F), + (0xA6F0, 0xA6F1), + (0xA700, 0xA721), + (0xA770, 0xA770), + (0xA788, 0xA78A), + (0xA7F1, 0xA7F4), + (0xA7F8, 0xA7F9), + (0xA802, 0xA802), + (0xA806, 0xA806), + (0xA80B, 0xA80B), + (0xA825, 0xA826), + (0xA82C, 0xA82C), + (0xA8C4, 0xA8C5), + (0xA8E0, 0xA8F1), + (0xA8FF, 0xA8FF), + (0xA926, 0xA92D), + (0xA947, 0xA951), + (0xA980, 0xA982), + (0xA9B3, 0xA9B3), + (0xA9B6, 0xA9B9), + (0xA9BC, 0xA9BD), + (0xA9CF, 0xA9CF), + (0xA9E5, 0xA9E6), + (0xAA29, 0xAA2E), + (0xAA31, 0xAA32), + (0xAA35, 0xAA36), + (0xAA43, 0xAA43), + (0xAA4C, 0xAA4C), + (0xAA70, 0xAA70), + (0xAA7C, 0xAA7C), + (0xAAB0, 0xAAB0), + (0xAAB2, 0xAAB4), + (0xAAB7, 0xAAB8), + (0xAABE, 0xAABF), + (0xAAC1, 0xAAC1), + (0xAADD, 0xAADD), + (0xAAEC, 0xAAED), + (0xAAF3, 0xAAF4), + (0xAAF6, 0xAAF6), + (0xAB5B, 0xAB5F), + (0xAB69, 0xAB6B), + (0xABE5, 0xABE5), + (0xABE8, 0xABE8), + (0xABED, 0xABED), + (0xFB1E, 0xFB1E), + (0xFBB2, 0xFBC2), + (0xFE00, 0xFE0F), + (0xFE13, 0xFE13), + (0xFE20, 0xFE2F), + (0xFE52, 0xFE52), + (0xFE55, 0xFE55), + (0xFEFF, 0xFEFF), + (0xFF07, 0xFF07), + (0xFF0E, 0xFF0E), + (0xFF1A, 0xFF1A), + (0xFF3E, 0xFF3E), + (0xFF40, 0xFF40), + (0xFF70, 0xFF70), + (0xFF9E, 0xFF9F), + (0xFFE3, 0xFFE3), + (0xFFF9, 0xFFFB), + (0x101FD, 0x101FD), + (0x102E0, 0x102E0), + (0x10376, 0x1037A), + (0x10780, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x10A01, 0x10A03), + (0x10A05, 0x10A06), + (0x10A0C, 0x10A0F), + (0x10A38, 0x10A3A), + (0x10A3F, 0x10A3F), + (0x10AE5, 0x10AE6), + (0x10D24, 0x10D27), + (0x10D4E, 0x10D4E), + (0x10D69, 0x10D6D), + (0x10D6F, 0x10D6F), + (0x10EAB, 0x10EAC), + (0x10EC5, 0x10EC5), + (0x10EC9, 0x10ECF), + (0x10EF0, 0x10EFF), + (0x10F46, 0x10F50), + (0x10F82, 0x10F85), + (0x11001, 0x11001), + (0x11038, 0x11046), + (0x11070, 0x11070), + (0x11073, 0x11074), + (0x1107F, 0x11081), + (0x110B3, 0x110B6), + (0x110B9, 0x110BA), + (0x110BD, 0x110BD), + (0x110C2, 0x110C2), + (0x110CD, 0x110CD), + (0x11100, 0x11102), + (0x11127, 0x1112B), + (0x1112D, 0x11134), + (0x11173, 0x11173), + (0x11180, 0x11181), + (0x111B6, 0x111BE), + (0x111C9, 0x111CC), + (0x111CF, 0x111CF), + (0x1122F, 0x11231), + (0x11234, 0x11234), + (0x11236, 0x11237), + (0x1123E, 0x1123E), + (0x11241, 0x11241), + (0x112DF, 0x112DF), + (0x112E3, 0x112EA), + (0x11300, 0x11301), + (0x1133B, 0x1133C), + (0x11340, 0x11340), + (0x11366, 0x1136C), + (0x11370, 0x11374), + (0x113BB, 0x113C0), + (0x113CE, 0x113CE), + (0x113D0, 0x113D0), + (0x113D2, 0x113D2), + (0x113E1, 0x113E2), + (0x11438, 0x1143F), + (0x11442, 0x11444), + (0x11446, 0x11446), + (0x1145E, 0x1145E), + (0x114B3, 0x114B8), + (0x114BA, 0x114BA), + (0x114BF, 0x114C0), + (0x114C2, 0x114C3), + (0x115B2, 0x115B5), + (0x115BC, 0x115BD), + (0x115BF, 0x115C0), + (0x115DC, 0x115DD), + (0x11633, 0x1163A), + (0x1163D, 0x1163D), + (0x1163F, 0x11640), + (0x116AB, 0x116AB), + (0x116AD, 0x116AD), + (0x116B0, 0x116B5), + (0x116B7, 0x116B7), + (0x1171D, 0x1171D), + (0x1171F, 0x1171F), + (0x11722, 0x11725), + (0x11727, 0x1172B), + (0x1182F, 0x11837), + (0x11839, 0x1183A), + (0x1193B, 0x1193C), + (0x1193E, 0x1193E), + (0x11943, 0x11943), + (0x119D4, 0x119D7), + (0x119DA, 0x119DB), + (0x119E0, 0x119E0), + (0x11A01, 0x11A0A), + (0x11A33, 0x11A38), + (0x11A3B, 0x11A3E), + (0x11A47, 0x11A47), + (0x11A51, 0x11A56), + (0x11A59, 0x11A5B), + (0x11A8A, 0x11A96), + (0x11A98, 0x11A99), + (0x11B60, 0x11B60), + (0x11B62, 0x11B64), + (0x11B66, 0x11B66), + (0x11C30, 0x11C36), + (0x11C38, 0x11C3D), + (0x11C3F, 0x11C3F), + (0x11C92, 0x11CA7), + (0x11CAA, 0x11CB0), + (0x11CB2, 0x11CB3), + (0x11CB5, 0x11CB6), + (0x11D31, 0x11D36), + (0x11D3A, 0x11D3A), + (0x11D3C, 0x11D3D), + (0x11D3F, 0x11D45), + (0x11D47, 0x11D47), + (0x11D90, 0x11D91), + (0x11D95, 0x11D95), + (0x11D97, 0x11D97), + (0x11DD9, 0x11DD9), + (0x11DF0, 0x11DF0), + (0x11EF3, 0x11EF4), + (0x11F00, 0x11F01), + (0x11F36, 0x11F3A), + (0x11F40, 0x11F40), + (0x11F42, 0x11F42), + (0x11F5A, 0x11F5A), + (0x13430, 0x13440), + (0x13447, 0x13455), + (0x1611E, 0x16129), + (0x1612D, 0x1612F), + (0x16AF0, 0x16AF4), + (0x16B30, 0x16B36), + (0x16B40, 0x16B43), + (0x16D40, 0x16D42), + (0x16D6B, 0x16D6C), + (0x16F4F, 0x16F4F), + (0x16F8F, 0x16F9F), + (0x16FE0, 0x16FE1), + (0x16FE3, 0x16FE4), + (0x16FF2, 0x16FF3), + (0x1AFF0, 0x1AFF3), + (0x1AFF5, 0x1AFFB), + (0x1AFFD, 0x1AFFE), + (0x1BC9D, 0x1BC9E), + (0x1BCA0, 0x1BCA3), + (0x1CF00, 0x1CF2D), + (0x1CF30, 0x1CF46), + (0x1D127, 0x1D128), + (0x1D167, 0x1D169), + (0x1D173, 0x1D182), + (0x1D185, 0x1D18B), + (0x1D1AA, 0x1D1AD), + (0x1D242, 0x1D244), + (0x1D25B, 0x1D25C), + (0x1DA00, 0x1DA36), + (0x1DA3B, 0x1DA6C), + (0x1DA75, 0x1DA75), + (0x1DA84, 0x1DA84), + (0x1DA9B, 0x1DA9F), + (0x1DAA1, 0x1DAAF), + (0x1DFCD, 0x1E006), + (0x1E008, 0x1E018), + (0x1E01B, 0x1E021), + (0x1E023, 0x1E024), + (0x1E026, 0x1E02A), + (0x1E030, 0x1E06D), + (0x1E08F, 0x1E08F), + (0x1E130, 0x1E13D), + (0x1E2AE, 0x1E2AE), + (0x1E2EC, 0x1E2EF), + (0x1E4EB, 0x1E4EF), + (0x1E5EE, 0x1E5EF), + (0x1E6E3, 0x1E6E3), + (0x1E6E6, 0x1E6E6), + (0x1E6EE, 0x1E6EF), + (0x1E6F5, 0x1E6F5), + (0x1E6FF, 0x1E6FF), + (0x1E8D0, 0x1E8D6), + (0x1E944, 0x1E94B), + (0x1F3FB, 0x1F3FF), + (0xE0001, 0xE0001), + (0xE0020, 0xE007F), + (0xE0100, 0xE01EF), +]; + +/// The code points with the binary property Cased, as ranges. +pub static BINARY_CASED: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0x61, 0x7A), + (0xAA, 0xAA), + (0xB5, 0xB5), + (0xBA, 0xBA), + (0xC0, 0xD6), + (0xD8, 0xF6), + (0xF8, 0x1BA), + (0x1BC, 0x1BF), + (0x1C4, 0x293), + (0x296, 0x2B8), + (0x2C0, 0x2C1), + (0x2E0, 0x2E4), + (0x345, 0x345), + (0x370, 0x373), + (0x376, 0x377), + (0x37A, 0x37D), + (0x37F, 0x37F), + (0x386, 0x386), + (0x388, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x3A1), + (0x3A3, 0x3F5), + (0x3F7, 0x481), + (0x48A, 0x52F), + (0x531, 0x556), + (0x558, 0x558), + (0x560, 0x588), + (0x58B, 0x58C), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10D0, 0x10FA), + (0x10FC, 0x10FF), + (0x13A0, 0x13F5), + (0x13F8, 0x13FD), + (0x1C80, 0x1C8A), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1D00, 0x1DBF), + (0x1E00, 0x1F15), + (0x1F18, 0x1F1D), + (0x1F20, 0x1F45), + (0x1F48, 0x1F4D), + (0x1F50, 0x1F57), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FBC), + (0x1FBE, 0x1FBE), + (0x1FC2, 0x1FC4), + (0x1FC6, 0x1FCC), + (0x1FD0, 0x1FD3), + (0x1FD6, 0x1FDB), + (0x1FE0, 0x1FEC), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FFC), + (0x2071, 0x2071), + (0x207F, 0x207F), + (0x2090, 0x209F), + (0x2102, 0x2102), + (0x2107, 0x2107), + (0x210A, 0x2113), + (0x2115, 0x2115), + (0x2119, 0x211D), + (0x2124, 0x2124), + (0x2126, 0x2126), + (0x2128, 0x2128), + (0x212A, 0x212D), + (0x212F, 0x2134), + (0x2139, 0x2139), + (0x213C, 0x213F), + (0x2145, 0x2149), + (0x214E, 0x214E), + (0x2160, 0x217F), + (0x2183, 0x2184), + (0x24B6, 0x24E9), + (0x2C00, 0x2CE4), + (0x2CEB, 0x2CEE), + (0x2CF2, 0x2CF3), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0xA640, 0xA66D), + (0xA680, 0xA69D), + (0xA722, 0xA787), + (0xA78B, 0xA78E), + (0xA790, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F1, 0xA7F6), + (0xA7F8, 0xA7FA), + (0xAB30, 0xAB5A), + (0xAB5C, 0xAB69), + (0xAB6C, 0xAB6D), + (0xAB70, 0xABBF), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFF21, 0xFF3A), + (0xFF41, 0xFF5A), + (0x10400, 0x1044F), + (0x104B0, 0x104D3), + (0x104D8, 0x104FB), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x10780, 0x10780), + (0x10783, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x10C80, 0x10CB2), + (0x10CC0, 0x10CF2), + (0x10D50, 0x10D65), + (0x10D70, 0x10D85), + (0x118A0, 0x118DF), + (0x16E40, 0x16E7F), + (0x16EA0, 0x16EB8), + (0x16EBB, 0x16ED3), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D6C0), + (0x1D6C2, 0x1D6DA), + (0x1D6DC, 0x1D6FA), + (0x1D6FC, 0x1D714), + (0x1D716, 0x1D734), + (0x1D736, 0x1D74E), + (0x1D750, 0x1D76E), + (0x1D770, 0x1D788), + (0x1D78A, 0x1D7A8), + (0x1D7AA, 0x1D7C2), + (0x1D7C4, 0x1D7CB), + (0x1DF00, 0x1DF09), + (0x1DF0B, 0x1DF7F), + (0x1DF90, 0x1DF96), + (0x1DFCD, 0x1DFFF), + (0x1E030, 0x1E06D), + (0x1E900, 0x1E943), + (0x1F130, 0x1F149), + (0x1F150, 0x1F169), + (0x1F170, 0x1F189), +]; + +/// The code points with the binary property Changes_When_Casefolded, as ranges. +pub static BINARY_CHANGES_WHEN_CASEFOLDED: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0xB5, 0xB5), + (0xC0, 0xD6), + (0xD8, 0xDF), + (0x100, 0x100), + (0x102, 0x102), + (0x104, 0x104), + (0x106, 0x106), + (0x108, 0x108), + (0x10A, 0x10A), + (0x10C, 0x10C), + (0x10E, 0x10E), + (0x110, 0x110), + (0x112, 0x112), + (0x114, 0x114), + (0x116, 0x116), + (0x118, 0x118), + (0x11A, 0x11A), + (0x11C, 0x11C), + (0x11E, 0x11E), + (0x120, 0x120), + (0x122, 0x122), + (0x124, 0x124), + (0x126, 0x126), + (0x128, 0x128), + (0x12A, 0x12A), + (0x12C, 0x12C), + (0x12E, 0x12E), + (0x130, 0x130), + (0x132, 0x132), + (0x134, 0x134), + (0x136, 0x136), + (0x139, 0x139), + (0x13B, 0x13B), + (0x13D, 0x13D), + (0x13F, 0x13F), + (0x141, 0x141), + (0x143, 0x143), + (0x145, 0x145), + (0x147, 0x147), + (0x149, 0x14A), + (0x14C, 0x14C), + (0x14E, 0x14E), + (0x150, 0x150), + (0x152, 0x152), + (0x154, 0x154), + (0x156, 0x156), + (0x158, 0x158), + (0x15A, 0x15A), + (0x15C, 0x15C), + (0x15E, 0x15E), + (0x160, 0x160), + (0x162, 0x162), + (0x164, 0x164), + (0x166, 0x166), + (0x168, 0x168), + (0x16A, 0x16A), + (0x16C, 0x16C), + (0x16E, 0x16E), + (0x170, 0x170), + (0x172, 0x172), + (0x174, 0x174), + (0x176, 0x176), + (0x178, 0x179), + (0x17B, 0x17B), + (0x17D, 0x17D), + (0x17F, 0x17F), + (0x181, 0x182), + (0x184, 0x184), + (0x186, 0x187), + (0x189, 0x18B), + (0x18E, 0x191), + (0x193, 0x194), + (0x196, 0x198), + (0x19C, 0x19D), + (0x19F, 0x1A0), + (0x1A2, 0x1A2), + (0x1A4, 0x1A4), + (0x1A6, 0x1A7), + (0x1A9, 0x1A9), + (0x1AC, 0x1AC), + (0x1AE, 0x1AF), + (0x1B1, 0x1B3), + (0x1B5, 0x1B5), + (0x1B7, 0x1B8), + (0x1BC, 0x1BC), + (0x1C4, 0x1C5), + (0x1C7, 0x1C8), + (0x1CA, 0x1CB), + (0x1CD, 0x1CD), + (0x1CF, 0x1CF), + (0x1D1, 0x1D1), + (0x1D3, 0x1D3), + (0x1D5, 0x1D5), + (0x1D7, 0x1D7), + (0x1D9, 0x1D9), + (0x1DB, 0x1DB), + (0x1DE, 0x1DE), + (0x1E0, 0x1E0), + (0x1E2, 0x1E2), + (0x1E4, 0x1E4), + (0x1E6, 0x1E6), + (0x1E8, 0x1E8), + (0x1EA, 0x1EA), + (0x1EC, 0x1EC), + (0x1EE, 0x1EE), + (0x1F1, 0x1F2), + (0x1F4, 0x1F4), + (0x1F6, 0x1F8), + (0x1FA, 0x1FA), + (0x1FC, 0x1FC), + (0x1FE, 0x1FE), + (0x200, 0x200), + (0x202, 0x202), + (0x204, 0x204), + (0x206, 0x206), + (0x208, 0x208), + (0x20A, 0x20A), + (0x20C, 0x20C), + (0x20E, 0x20E), + (0x210, 0x210), + (0x212, 0x212), + (0x214, 0x214), + (0x216, 0x216), + (0x218, 0x218), + (0x21A, 0x21A), + (0x21C, 0x21C), + (0x21E, 0x21E), + (0x220, 0x220), + (0x222, 0x222), + (0x224, 0x224), + (0x226, 0x226), + (0x228, 0x228), + (0x22A, 0x22A), + (0x22C, 0x22C), + (0x22E, 0x22E), + (0x230, 0x230), + (0x232, 0x232), + (0x23A, 0x23B), + (0x23D, 0x23E), + (0x241, 0x241), + (0x243, 0x246), + (0x248, 0x248), + (0x24A, 0x24A), + (0x24C, 0x24C), + (0x24E, 0x24E), + (0x345, 0x345), + (0x370, 0x370), + (0x372, 0x372), + (0x376, 0x376), + (0x37F, 0x37F), + (0x386, 0x386), + (0x388, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x38F), + (0x391, 0x3A1), + (0x3A3, 0x3AB), + (0x3C2, 0x3C2), + (0x3CF, 0x3D1), + (0x3D5, 0x3D6), + (0x3D8, 0x3D8), + (0x3DA, 0x3DA), + (0x3DC, 0x3DC), + (0x3DE, 0x3DE), + (0x3E0, 0x3E0), + (0x3E2, 0x3E2), + (0x3E4, 0x3E4), + (0x3E6, 0x3E6), + (0x3E8, 0x3E8), + (0x3EA, 0x3EA), + (0x3EC, 0x3EC), + (0x3EE, 0x3EE), + (0x3F0, 0x3F1), + (0x3F4, 0x3F5), + (0x3F7, 0x3F7), + (0x3F9, 0x3FA), + (0x3FD, 0x42F), + (0x460, 0x460), + (0x462, 0x462), + (0x464, 0x464), + (0x466, 0x466), + (0x468, 0x468), + (0x46A, 0x46A), + (0x46C, 0x46C), + (0x46E, 0x46E), + (0x470, 0x470), + (0x472, 0x472), + (0x474, 0x474), + (0x476, 0x476), + (0x478, 0x478), + (0x47A, 0x47A), + (0x47C, 0x47C), + (0x47E, 0x47E), + (0x480, 0x480), + (0x48A, 0x48A), + (0x48C, 0x48C), + (0x48E, 0x48E), + (0x490, 0x490), + (0x492, 0x492), + (0x494, 0x494), + (0x496, 0x496), + (0x498, 0x498), + (0x49A, 0x49A), + (0x49C, 0x49C), + (0x49E, 0x49E), + (0x4A0, 0x4A0), + (0x4A2, 0x4A2), + (0x4A4, 0x4A4), + (0x4A6, 0x4A6), + (0x4A8, 0x4A8), + (0x4AA, 0x4AA), + (0x4AC, 0x4AC), + (0x4AE, 0x4AE), + (0x4B0, 0x4B0), + (0x4B2, 0x4B2), + (0x4B4, 0x4B4), + (0x4B6, 0x4B6), + (0x4B8, 0x4B8), + (0x4BA, 0x4BA), + (0x4BC, 0x4BC), + (0x4BE, 0x4BE), + (0x4C0, 0x4C1), + (0x4C3, 0x4C3), + (0x4C5, 0x4C5), + (0x4C7, 0x4C7), + (0x4C9, 0x4C9), + (0x4CB, 0x4CB), + (0x4CD, 0x4CD), + (0x4D0, 0x4D0), + (0x4D2, 0x4D2), + (0x4D4, 0x4D4), + (0x4D6, 0x4D6), + (0x4D8, 0x4D8), + (0x4DA, 0x4DA), + (0x4DC, 0x4DC), + (0x4DE, 0x4DE), + (0x4E0, 0x4E0), + (0x4E2, 0x4E2), + (0x4E4, 0x4E4), + (0x4E6, 0x4E6), + (0x4E8, 0x4E8), + (0x4EA, 0x4EA), + (0x4EC, 0x4EC), + (0x4EE, 0x4EE), + (0x4F0, 0x4F0), + (0x4F2, 0x4F2), + (0x4F4, 0x4F4), + (0x4F6, 0x4F6), + (0x4F8, 0x4F8), + (0x4FA, 0x4FA), + (0x4FC, 0x4FC), + (0x4FE, 0x4FE), + (0x500, 0x500), + (0x502, 0x502), + (0x504, 0x504), + (0x506, 0x506), + (0x508, 0x508), + (0x50A, 0x50A), + (0x50C, 0x50C), + (0x50E, 0x50E), + (0x510, 0x510), + (0x512, 0x512), + (0x514, 0x514), + (0x516, 0x516), + (0x518, 0x518), + (0x51A, 0x51A), + (0x51C, 0x51C), + (0x51E, 0x51E), + (0x520, 0x520), + (0x522, 0x522), + (0x524, 0x524), + (0x526, 0x526), + (0x528, 0x528), + (0x52A, 0x52A), + (0x52C, 0x52C), + (0x52E, 0x52E), + (0x531, 0x556), + (0x587, 0x587), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x13F8, 0x13FD), + (0x1C80, 0x1C89), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1E00, 0x1E00), + (0x1E02, 0x1E02), + (0x1E04, 0x1E04), + (0x1E06, 0x1E06), + (0x1E08, 0x1E08), + (0x1E0A, 0x1E0A), + (0x1E0C, 0x1E0C), + (0x1E0E, 0x1E0E), + (0x1E10, 0x1E10), + (0x1E12, 0x1E12), + (0x1E14, 0x1E14), + (0x1E16, 0x1E16), + (0x1E18, 0x1E18), + (0x1E1A, 0x1E1A), + (0x1E1C, 0x1E1C), + (0x1E1E, 0x1E1E), + (0x1E20, 0x1E20), + (0x1E22, 0x1E22), + (0x1E24, 0x1E24), + (0x1E26, 0x1E26), + (0x1E28, 0x1E28), + (0x1E2A, 0x1E2A), + (0x1E2C, 0x1E2C), + (0x1E2E, 0x1E2E), + (0x1E30, 0x1E30), + (0x1E32, 0x1E32), + (0x1E34, 0x1E34), + (0x1E36, 0x1E36), + (0x1E38, 0x1E38), + (0x1E3A, 0x1E3A), + (0x1E3C, 0x1E3C), + (0x1E3E, 0x1E3E), + (0x1E40, 0x1E40), + (0x1E42, 0x1E42), + (0x1E44, 0x1E44), + (0x1E46, 0x1E46), + (0x1E48, 0x1E48), + (0x1E4A, 0x1E4A), + (0x1E4C, 0x1E4C), + (0x1E4E, 0x1E4E), + (0x1E50, 0x1E50), + (0x1E52, 0x1E52), + (0x1E54, 0x1E54), + (0x1E56, 0x1E56), + (0x1E58, 0x1E58), + (0x1E5A, 0x1E5A), + (0x1E5C, 0x1E5C), + (0x1E5E, 0x1E5E), + (0x1E60, 0x1E60), + (0x1E62, 0x1E62), + (0x1E64, 0x1E64), + (0x1E66, 0x1E66), + (0x1E68, 0x1E68), + (0x1E6A, 0x1E6A), + (0x1E6C, 0x1E6C), + (0x1E6E, 0x1E6E), + (0x1E70, 0x1E70), + (0x1E72, 0x1E72), + (0x1E74, 0x1E74), + (0x1E76, 0x1E76), + (0x1E78, 0x1E78), + (0x1E7A, 0x1E7A), + (0x1E7C, 0x1E7C), + (0x1E7E, 0x1E7E), + (0x1E80, 0x1E80), + (0x1E82, 0x1E82), + (0x1E84, 0x1E84), + (0x1E86, 0x1E86), + (0x1E88, 0x1E88), + (0x1E8A, 0x1E8A), + (0x1E8C, 0x1E8C), + (0x1E8E, 0x1E8E), + (0x1E90, 0x1E90), + (0x1E92, 0x1E92), + (0x1E94, 0x1E94), + (0x1E9A, 0x1E9B), + (0x1E9E, 0x1E9E), + (0x1EA0, 0x1EA0), + (0x1EA2, 0x1EA2), + (0x1EA4, 0x1EA4), + (0x1EA6, 0x1EA6), + (0x1EA8, 0x1EA8), + (0x1EAA, 0x1EAA), + (0x1EAC, 0x1EAC), + (0x1EAE, 0x1EAE), + (0x1EB0, 0x1EB0), + (0x1EB2, 0x1EB2), + (0x1EB4, 0x1EB4), + (0x1EB6, 0x1EB6), + (0x1EB8, 0x1EB8), + (0x1EBA, 0x1EBA), + (0x1EBC, 0x1EBC), + (0x1EBE, 0x1EBE), + (0x1EC0, 0x1EC0), + (0x1EC2, 0x1EC2), + (0x1EC4, 0x1EC4), + (0x1EC6, 0x1EC6), + (0x1EC8, 0x1EC8), + (0x1ECA, 0x1ECA), + (0x1ECC, 0x1ECC), + (0x1ECE, 0x1ECE), + (0x1ED0, 0x1ED0), + (0x1ED2, 0x1ED2), + (0x1ED4, 0x1ED4), + (0x1ED6, 0x1ED6), + (0x1ED8, 0x1ED8), + (0x1EDA, 0x1EDA), + (0x1EDC, 0x1EDC), + (0x1EDE, 0x1EDE), + (0x1EE0, 0x1EE0), + (0x1EE2, 0x1EE2), + (0x1EE4, 0x1EE4), + (0x1EE6, 0x1EE6), + (0x1EE8, 0x1EE8), + (0x1EEA, 0x1EEA), + (0x1EEC, 0x1EEC), + (0x1EEE, 0x1EEE), + (0x1EF0, 0x1EF0), + (0x1EF2, 0x1EF2), + (0x1EF4, 0x1EF4), + (0x1EF6, 0x1EF6), + (0x1EF8, 0x1EF8), + (0x1EFA, 0x1EFA), + (0x1EFC, 0x1EFC), + (0x1EFE, 0x1EFE), + (0x1F08, 0x1F0F), + (0x1F18, 0x1F1D), + (0x1F28, 0x1F2F), + (0x1F38, 0x1F3F), + (0x1F48, 0x1F4D), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F5F), + (0x1F68, 0x1F6F), + (0x1F80, 0x1FAF), + (0x1FB2, 0x1FB4), + (0x1FB7, 0x1FBC), + (0x1FC2, 0x1FC4), + (0x1FC7, 0x1FCC), + (0x1FD8, 0x1FDB), + (0x1FE8, 0x1FEC), + (0x1FF2, 0x1FF4), + (0x1FF7, 0x1FFC), + (0x2126, 0x2126), + (0x212A, 0x212B), + (0x2132, 0x2132), + (0x2160, 0x216F), + (0x2183, 0x2183), + (0x24B6, 0x24CF), + (0x2C00, 0x2C2F), + (0x2C60, 0x2C60), + (0x2C62, 0x2C64), + (0x2C67, 0x2C67), + (0x2C69, 0x2C69), + (0x2C6B, 0x2C6B), + (0x2C6D, 0x2C70), + (0x2C72, 0x2C72), + (0x2C75, 0x2C75), + (0x2C7E, 0x2C80), + (0x2C82, 0x2C82), + (0x2C84, 0x2C84), + (0x2C86, 0x2C86), + (0x2C88, 0x2C88), + (0x2C8A, 0x2C8A), + (0x2C8C, 0x2C8C), + (0x2C8E, 0x2C8E), + (0x2C90, 0x2C90), + (0x2C92, 0x2C92), + (0x2C94, 0x2C94), + (0x2C96, 0x2C96), + (0x2C98, 0x2C98), + (0x2C9A, 0x2C9A), + (0x2C9C, 0x2C9C), + (0x2C9E, 0x2C9E), + (0x2CA0, 0x2CA0), + (0x2CA2, 0x2CA2), + (0x2CA4, 0x2CA4), + (0x2CA6, 0x2CA6), + (0x2CA8, 0x2CA8), + (0x2CAA, 0x2CAA), + (0x2CAC, 0x2CAC), + (0x2CAE, 0x2CAE), + (0x2CB0, 0x2CB0), + (0x2CB2, 0x2CB2), + (0x2CB4, 0x2CB4), + (0x2CB6, 0x2CB6), + (0x2CB8, 0x2CB8), + (0x2CBA, 0x2CBA), + (0x2CBC, 0x2CBC), + (0x2CBE, 0x2CBE), + (0x2CC0, 0x2CC0), + (0x2CC2, 0x2CC2), + (0x2CC4, 0x2CC4), + (0x2CC6, 0x2CC6), + (0x2CC8, 0x2CC8), + (0x2CCA, 0x2CCA), + (0x2CCC, 0x2CCC), + (0x2CCE, 0x2CCE), + (0x2CD0, 0x2CD0), + (0x2CD2, 0x2CD2), + (0x2CD4, 0x2CD4), + (0x2CD6, 0x2CD6), + (0x2CD8, 0x2CD8), + (0x2CDA, 0x2CDA), + (0x2CDC, 0x2CDC), + (0x2CDE, 0x2CDE), + (0x2CE0, 0x2CE0), + (0x2CE2, 0x2CE2), + (0x2CEB, 0x2CEB), + (0x2CED, 0x2CED), + (0x2CF2, 0x2CF2), + (0xA640, 0xA640), + (0xA642, 0xA642), + (0xA644, 0xA644), + (0xA646, 0xA646), + (0xA648, 0xA648), + (0xA64A, 0xA64A), + (0xA64C, 0xA64C), + (0xA64E, 0xA64E), + (0xA650, 0xA650), + (0xA652, 0xA652), + (0xA654, 0xA654), + (0xA656, 0xA656), + (0xA658, 0xA658), + (0xA65A, 0xA65A), + (0xA65C, 0xA65C), + (0xA65E, 0xA65E), + (0xA660, 0xA660), + (0xA662, 0xA662), + (0xA664, 0xA664), + (0xA666, 0xA666), + (0xA668, 0xA668), + (0xA66A, 0xA66A), + (0xA66C, 0xA66C), + (0xA680, 0xA680), + (0xA682, 0xA682), + (0xA684, 0xA684), + (0xA686, 0xA686), + (0xA688, 0xA688), + (0xA68A, 0xA68A), + (0xA68C, 0xA68C), + (0xA68E, 0xA68E), + (0xA690, 0xA690), + (0xA692, 0xA692), + (0xA694, 0xA694), + (0xA696, 0xA696), + (0xA698, 0xA698), + (0xA69A, 0xA69A), + (0xA722, 0xA722), + (0xA724, 0xA724), + (0xA726, 0xA726), + (0xA728, 0xA728), + (0xA72A, 0xA72A), + (0xA72C, 0xA72C), + (0xA72E, 0xA72E), + (0xA732, 0xA732), + (0xA734, 0xA734), + (0xA736, 0xA736), + (0xA738, 0xA738), + (0xA73A, 0xA73A), + (0xA73C, 0xA73C), + (0xA73E, 0xA73E), + (0xA740, 0xA740), + (0xA742, 0xA742), + (0xA744, 0xA744), + (0xA746, 0xA746), + (0xA748, 0xA748), + (0xA74A, 0xA74A), + (0xA74C, 0xA74C), + (0xA74E, 0xA74E), + (0xA750, 0xA750), + (0xA752, 0xA752), + (0xA754, 0xA754), + (0xA756, 0xA756), + (0xA758, 0xA758), + (0xA75A, 0xA75A), + (0xA75C, 0xA75C), + (0xA75E, 0xA75E), + (0xA760, 0xA760), + (0xA762, 0xA762), + (0xA764, 0xA764), + (0xA766, 0xA766), + (0xA768, 0xA768), + (0xA76A, 0xA76A), + (0xA76C, 0xA76C), + (0xA76E, 0xA76E), + (0xA779, 0xA779), + (0xA77B, 0xA77B), + (0xA77D, 0xA77E), + (0xA780, 0xA780), + (0xA782, 0xA782), + (0xA784, 0xA784), + (0xA786, 0xA786), + (0xA78B, 0xA78B), + (0xA78D, 0xA78D), + (0xA790, 0xA790), + (0xA792, 0xA792), + (0xA796, 0xA796), + (0xA798, 0xA798), + (0xA79A, 0xA79A), + (0xA79C, 0xA79C), + (0xA79E, 0xA79E), + (0xA7A0, 0xA7A0), + (0xA7A2, 0xA7A2), + (0xA7A4, 0xA7A4), + (0xA7A6, 0xA7A6), + (0xA7A8, 0xA7A8), + (0xA7AA, 0xA7AE), + (0xA7B0, 0xA7B4), + (0xA7B6, 0xA7B6), + (0xA7B8, 0xA7B8), + (0xA7BA, 0xA7BA), + (0xA7BC, 0xA7BC), + (0xA7BE, 0xA7BE), + (0xA7C0, 0xA7C0), + (0xA7C2, 0xA7C2), + (0xA7C4, 0xA7C7), + (0xA7C9, 0xA7C9), + (0xA7CB, 0xA7CC), + (0xA7CE, 0xA7CE), + (0xA7D0, 0xA7D0), + (0xA7D2, 0xA7D2), + (0xA7D4, 0xA7D4), + (0xA7D6, 0xA7D6), + (0xA7D8, 0xA7D8), + (0xA7DA, 0xA7DA), + (0xA7DC, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F5, 0xA7F5), + (0xAB6C, 0xAB6D), + (0xAB70, 0xABBF), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFF21, 0xFF3A), + (0x10400, 0x10427), + (0x104B0, 0x104D3), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10C80, 0x10CB2), + (0x10D50, 0x10D65), + (0x118A0, 0x118BF), + (0x16E40, 0x16E5F), + (0x16EA0, 0x16EB8), + (0x1DF40, 0x1DF40), + (0x1DF48, 0x1DF48), + (0x1DF4A, 0x1DF4A), + (0x1DF4D, 0x1DF4D), + (0x1DF51, 0x1DF51), + (0x1DF68, 0x1DF68), + (0x1DF6A, 0x1DF6A), + (0x1DF6C, 0x1DF6C), + (0x1DF6E, 0x1DF6E), + (0x1DF72, 0x1DF72), + (0x1DF74, 0x1DF74), + (0x1DF76, 0x1DF76), + (0x1DF78, 0x1DF78), + (0x1DF7A, 0x1DF7A), + (0x1DF7C, 0x1DF7C), + (0x1DF7E, 0x1DF7E), + (0x1DF95, 0x1DF95), + (0x1E900, 0x1E921), +]; + +/// The code points with the binary property Changes_When_Casemapped, as ranges. +pub static BINARY_CHANGES_WHEN_CASEMAPPED: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0x61, 0x7A), + (0xB5, 0xB5), + (0xC0, 0xD6), + (0xD8, 0xF6), + (0xF8, 0x137), + (0x139, 0x18C), + (0x18E, 0x1A9), + (0x1AC, 0x1B9), + (0x1BC, 0x1BD), + (0x1BF, 0x1BF), + (0x1C4, 0x220), + (0x222, 0x233), + (0x23A, 0x254), + (0x256, 0x257), + (0x259, 0x259), + (0x25B, 0x25C), + (0x260, 0x261), + (0x263, 0x266), + (0x268, 0x26C), + (0x26F, 0x26F), + (0x271, 0x272), + (0x275, 0x275), + (0x277, 0x277), + (0x27C, 0x27D), + (0x280, 0x280), + (0x282, 0x283), + (0x287, 0x28C), + (0x292, 0x292), + (0x29D, 0x29E), + (0x345, 0x345), + (0x370, 0x373), + (0x376, 0x377), + (0x37B, 0x37D), + (0x37F, 0x37F), + (0x386, 0x386), + (0x388, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x3A1), + (0x3A3, 0x3D1), + (0x3D5, 0x3F5), + (0x3F7, 0x3FB), + (0x3FD, 0x481), + (0x48A, 0x52F), + (0x531, 0x556), + (0x561, 0x587), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10D0, 0x10FA), + (0x10FD, 0x10FF), + (0x13A0, 0x13F5), + (0x13F8, 0x13FD), + (0x1C80, 0x1C8A), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1D79, 0x1D79), + (0x1D7D, 0x1D7D), + (0x1D8E, 0x1D8E), + (0x1E00, 0x1E9B), + (0x1E9E, 0x1E9E), + (0x1EA0, 0x1F15), + (0x1F18, 0x1F1D), + (0x1F20, 0x1F45), + (0x1F48, 0x1F4D), + (0x1F50, 0x1F57), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FBC), + (0x1FBE, 0x1FBE), + (0x1FC2, 0x1FC4), + (0x1FC6, 0x1FCC), + (0x1FD0, 0x1FD3), + (0x1FD6, 0x1FDB), + (0x1FE0, 0x1FEC), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FFC), + (0x2126, 0x2126), + (0x212A, 0x212B), + (0x2132, 0x2132), + (0x214E, 0x214E), + (0x2160, 0x217F), + (0x2183, 0x2184), + (0x24B6, 0x24E9), + (0x2C00, 0x2C70), + (0x2C72, 0x2C73), + (0x2C75, 0x2C76), + (0x2C7E, 0x2CE3), + (0x2CEB, 0x2CEE), + (0x2CF2, 0x2CF3), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0xA640, 0xA66D), + (0xA680, 0xA69B), + (0xA722, 0xA72F), + (0xA732, 0xA76F), + (0xA779, 0xA787), + (0xA78B, 0xA78D), + (0xA790, 0xA794), + (0xA796, 0xA7AE), + (0xA7B0, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F5, 0xA7F6), + (0xAB4B, 0xAB4C), + (0xAB53, 0xAB53), + (0xAB6C, 0xAB6D), + (0xAB70, 0xABBF), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFF21, 0xFF3A), + (0xFF41, 0xFF5A), + (0x10400, 0x1044F), + (0x104B0, 0x104D3), + (0x104D8, 0x104FB), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x10C80, 0x10CB2), + (0x10CC0, 0x10CF2), + (0x10D50, 0x10D65), + (0x10D70, 0x10D85), + (0x118A0, 0x118DF), + (0x16E40, 0x16E7F), + (0x16EA0, 0x16EB8), + (0x16EBB, 0x16ED3), + (0x1DF40, 0x1DF41), + (0x1DF48, 0x1DF4B), + (0x1DF4D, 0x1DF4E), + (0x1DF51, 0x1DF52), + (0x1DF68, 0x1DF6F), + (0x1DF72, 0x1DF7F), + (0x1DF95, 0x1DF95), + (0x1E900, 0x1E943), +]; + +/// The code points with the binary property Changes_When_Lowercased, as ranges. +pub static BINARY_CHANGES_WHEN_LOWERCASED: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0xC0, 0xD6), + (0xD8, 0xDE), + (0x100, 0x100), + (0x102, 0x102), + (0x104, 0x104), + (0x106, 0x106), + (0x108, 0x108), + (0x10A, 0x10A), + (0x10C, 0x10C), + (0x10E, 0x10E), + (0x110, 0x110), + (0x112, 0x112), + (0x114, 0x114), + (0x116, 0x116), + (0x118, 0x118), + (0x11A, 0x11A), + (0x11C, 0x11C), + (0x11E, 0x11E), + (0x120, 0x120), + (0x122, 0x122), + (0x124, 0x124), + (0x126, 0x126), + (0x128, 0x128), + (0x12A, 0x12A), + (0x12C, 0x12C), + (0x12E, 0x12E), + (0x130, 0x130), + (0x132, 0x132), + (0x134, 0x134), + (0x136, 0x136), + (0x139, 0x139), + (0x13B, 0x13B), + (0x13D, 0x13D), + (0x13F, 0x13F), + (0x141, 0x141), + (0x143, 0x143), + (0x145, 0x145), + (0x147, 0x147), + (0x14A, 0x14A), + (0x14C, 0x14C), + (0x14E, 0x14E), + (0x150, 0x150), + (0x152, 0x152), + (0x154, 0x154), + (0x156, 0x156), + (0x158, 0x158), + (0x15A, 0x15A), + (0x15C, 0x15C), + (0x15E, 0x15E), + (0x160, 0x160), + (0x162, 0x162), + (0x164, 0x164), + (0x166, 0x166), + (0x168, 0x168), + (0x16A, 0x16A), + (0x16C, 0x16C), + (0x16E, 0x16E), + (0x170, 0x170), + (0x172, 0x172), + (0x174, 0x174), + (0x176, 0x176), + (0x178, 0x179), + (0x17B, 0x17B), + (0x17D, 0x17D), + (0x181, 0x182), + (0x184, 0x184), + (0x186, 0x187), + (0x189, 0x18B), + (0x18E, 0x191), + (0x193, 0x194), + (0x196, 0x198), + (0x19C, 0x19D), + (0x19F, 0x1A0), + (0x1A2, 0x1A2), + (0x1A4, 0x1A4), + (0x1A6, 0x1A7), + (0x1A9, 0x1A9), + (0x1AC, 0x1AC), + (0x1AE, 0x1AF), + (0x1B1, 0x1B3), + (0x1B5, 0x1B5), + (0x1B7, 0x1B8), + (0x1BC, 0x1BC), + (0x1C4, 0x1C5), + (0x1C7, 0x1C8), + (0x1CA, 0x1CB), + (0x1CD, 0x1CD), + (0x1CF, 0x1CF), + (0x1D1, 0x1D1), + (0x1D3, 0x1D3), + (0x1D5, 0x1D5), + (0x1D7, 0x1D7), + (0x1D9, 0x1D9), + (0x1DB, 0x1DB), + (0x1DE, 0x1DE), + (0x1E0, 0x1E0), + (0x1E2, 0x1E2), + (0x1E4, 0x1E4), + (0x1E6, 0x1E6), + (0x1E8, 0x1E8), + (0x1EA, 0x1EA), + (0x1EC, 0x1EC), + (0x1EE, 0x1EE), + (0x1F1, 0x1F2), + (0x1F4, 0x1F4), + (0x1F6, 0x1F8), + (0x1FA, 0x1FA), + (0x1FC, 0x1FC), + (0x1FE, 0x1FE), + (0x200, 0x200), + (0x202, 0x202), + (0x204, 0x204), + (0x206, 0x206), + (0x208, 0x208), + (0x20A, 0x20A), + (0x20C, 0x20C), + (0x20E, 0x20E), + (0x210, 0x210), + (0x212, 0x212), + (0x214, 0x214), + (0x216, 0x216), + (0x218, 0x218), + (0x21A, 0x21A), + (0x21C, 0x21C), + (0x21E, 0x21E), + (0x220, 0x220), + (0x222, 0x222), + (0x224, 0x224), + (0x226, 0x226), + (0x228, 0x228), + (0x22A, 0x22A), + (0x22C, 0x22C), + (0x22E, 0x22E), + (0x230, 0x230), + (0x232, 0x232), + (0x23A, 0x23B), + (0x23D, 0x23E), + (0x241, 0x241), + (0x243, 0x246), + (0x248, 0x248), + (0x24A, 0x24A), + (0x24C, 0x24C), + (0x24E, 0x24E), + (0x370, 0x370), + (0x372, 0x372), + (0x376, 0x376), + (0x37F, 0x37F), + (0x386, 0x386), + (0x388, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x38F), + (0x391, 0x3A1), + (0x3A3, 0x3AB), + (0x3CF, 0x3CF), + (0x3D8, 0x3D8), + (0x3DA, 0x3DA), + (0x3DC, 0x3DC), + (0x3DE, 0x3DE), + (0x3E0, 0x3E0), + (0x3E2, 0x3E2), + (0x3E4, 0x3E4), + (0x3E6, 0x3E6), + (0x3E8, 0x3E8), + (0x3EA, 0x3EA), + (0x3EC, 0x3EC), + (0x3EE, 0x3EE), + (0x3F4, 0x3F4), + (0x3F7, 0x3F7), + (0x3F9, 0x3FA), + (0x3FD, 0x42F), + (0x460, 0x460), + (0x462, 0x462), + (0x464, 0x464), + (0x466, 0x466), + (0x468, 0x468), + (0x46A, 0x46A), + (0x46C, 0x46C), + (0x46E, 0x46E), + (0x470, 0x470), + (0x472, 0x472), + (0x474, 0x474), + (0x476, 0x476), + (0x478, 0x478), + (0x47A, 0x47A), + (0x47C, 0x47C), + (0x47E, 0x47E), + (0x480, 0x480), + (0x48A, 0x48A), + (0x48C, 0x48C), + (0x48E, 0x48E), + (0x490, 0x490), + (0x492, 0x492), + (0x494, 0x494), + (0x496, 0x496), + (0x498, 0x498), + (0x49A, 0x49A), + (0x49C, 0x49C), + (0x49E, 0x49E), + (0x4A0, 0x4A0), + (0x4A2, 0x4A2), + (0x4A4, 0x4A4), + (0x4A6, 0x4A6), + (0x4A8, 0x4A8), + (0x4AA, 0x4AA), + (0x4AC, 0x4AC), + (0x4AE, 0x4AE), + (0x4B0, 0x4B0), + (0x4B2, 0x4B2), + (0x4B4, 0x4B4), + (0x4B6, 0x4B6), + (0x4B8, 0x4B8), + (0x4BA, 0x4BA), + (0x4BC, 0x4BC), + (0x4BE, 0x4BE), + (0x4C0, 0x4C1), + (0x4C3, 0x4C3), + (0x4C5, 0x4C5), + (0x4C7, 0x4C7), + (0x4C9, 0x4C9), + (0x4CB, 0x4CB), + (0x4CD, 0x4CD), + (0x4D0, 0x4D0), + (0x4D2, 0x4D2), + (0x4D4, 0x4D4), + (0x4D6, 0x4D6), + (0x4D8, 0x4D8), + (0x4DA, 0x4DA), + (0x4DC, 0x4DC), + (0x4DE, 0x4DE), + (0x4E0, 0x4E0), + (0x4E2, 0x4E2), + (0x4E4, 0x4E4), + (0x4E6, 0x4E6), + (0x4E8, 0x4E8), + (0x4EA, 0x4EA), + (0x4EC, 0x4EC), + (0x4EE, 0x4EE), + (0x4F0, 0x4F0), + (0x4F2, 0x4F2), + (0x4F4, 0x4F4), + (0x4F6, 0x4F6), + (0x4F8, 0x4F8), + (0x4FA, 0x4FA), + (0x4FC, 0x4FC), + (0x4FE, 0x4FE), + (0x500, 0x500), + (0x502, 0x502), + (0x504, 0x504), + (0x506, 0x506), + (0x508, 0x508), + (0x50A, 0x50A), + (0x50C, 0x50C), + (0x50E, 0x50E), + (0x510, 0x510), + (0x512, 0x512), + (0x514, 0x514), + (0x516, 0x516), + (0x518, 0x518), + (0x51A, 0x51A), + (0x51C, 0x51C), + (0x51E, 0x51E), + (0x520, 0x520), + (0x522, 0x522), + (0x524, 0x524), + (0x526, 0x526), + (0x528, 0x528), + (0x52A, 0x52A), + (0x52C, 0x52C), + (0x52E, 0x52E), + (0x531, 0x556), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x13A0, 0x13F5), + (0x1C89, 0x1C89), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1E00, 0x1E00), + (0x1E02, 0x1E02), + (0x1E04, 0x1E04), + (0x1E06, 0x1E06), + (0x1E08, 0x1E08), + (0x1E0A, 0x1E0A), + (0x1E0C, 0x1E0C), + (0x1E0E, 0x1E0E), + (0x1E10, 0x1E10), + (0x1E12, 0x1E12), + (0x1E14, 0x1E14), + (0x1E16, 0x1E16), + (0x1E18, 0x1E18), + (0x1E1A, 0x1E1A), + (0x1E1C, 0x1E1C), + (0x1E1E, 0x1E1E), + (0x1E20, 0x1E20), + (0x1E22, 0x1E22), + (0x1E24, 0x1E24), + (0x1E26, 0x1E26), + (0x1E28, 0x1E28), + (0x1E2A, 0x1E2A), + (0x1E2C, 0x1E2C), + (0x1E2E, 0x1E2E), + (0x1E30, 0x1E30), + (0x1E32, 0x1E32), + (0x1E34, 0x1E34), + (0x1E36, 0x1E36), + (0x1E38, 0x1E38), + (0x1E3A, 0x1E3A), + (0x1E3C, 0x1E3C), + (0x1E3E, 0x1E3E), + (0x1E40, 0x1E40), + (0x1E42, 0x1E42), + (0x1E44, 0x1E44), + (0x1E46, 0x1E46), + (0x1E48, 0x1E48), + (0x1E4A, 0x1E4A), + (0x1E4C, 0x1E4C), + (0x1E4E, 0x1E4E), + (0x1E50, 0x1E50), + (0x1E52, 0x1E52), + (0x1E54, 0x1E54), + (0x1E56, 0x1E56), + (0x1E58, 0x1E58), + (0x1E5A, 0x1E5A), + (0x1E5C, 0x1E5C), + (0x1E5E, 0x1E5E), + (0x1E60, 0x1E60), + (0x1E62, 0x1E62), + (0x1E64, 0x1E64), + (0x1E66, 0x1E66), + (0x1E68, 0x1E68), + (0x1E6A, 0x1E6A), + (0x1E6C, 0x1E6C), + (0x1E6E, 0x1E6E), + (0x1E70, 0x1E70), + (0x1E72, 0x1E72), + (0x1E74, 0x1E74), + (0x1E76, 0x1E76), + (0x1E78, 0x1E78), + (0x1E7A, 0x1E7A), + (0x1E7C, 0x1E7C), + (0x1E7E, 0x1E7E), + (0x1E80, 0x1E80), + (0x1E82, 0x1E82), + (0x1E84, 0x1E84), + (0x1E86, 0x1E86), + (0x1E88, 0x1E88), + (0x1E8A, 0x1E8A), + (0x1E8C, 0x1E8C), + (0x1E8E, 0x1E8E), + (0x1E90, 0x1E90), + (0x1E92, 0x1E92), + (0x1E94, 0x1E94), + (0x1E9E, 0x1E9E), + (0x1EA0, 0x1EA0), + (0x1EA2, 0x1EA2), + (0x1EA4, 0x1EA4), + (0x1EA6, 0x1EA6), + (0x1EA8, 0x1EA8), + (0x1EAA, 0x1EAA), + (0x1EAC, 0x1EAC), + (0x1EAE, 0x1EAE), + (0x1EB0, 0x1EB0), + (0x1EB2, 0x1EB2), + (0x1EB4, 0x1EB4), + (0x1EB6, 0x1EB6), + (0x1EB8, 0x1EB8), + (0x1EBA, 0x1EBA), + (0x1EBC, 0x1EBC), + (0x1EBE, 0x1EBE), + (0x1EC0, 0x1EC0), + (0x1EC2, 0x1EC2), + (0x1EC4, 0x1EC4), + (0x1EC6, 0x1EC6), + (0x1EC8, 0x1EC8), + (0x1ECA, 0x1ECA), + (0x1ECC, 0x1ECC), + (0x1ECE, 0x1ECE), + (0x1ED0, 0x1ED0), + (0x1ED2, 0x1ED2), + (0x1ED4, 0x1ED4), + (0x1ED6, 0x1ED6), + (0x1ED8, 0x1ED8), + (0x1EDA, 0x1EDA), + (0x1EDC, 0x1EDC), + (0x1EDE, 0x1EDE), + (0x1EE0, 0x1EE0), + (0x1EE2, 0x1EE2), + (0x1EE4, 0x1EE4), + (0x1EE6, 0x1EE6), + (0x1EE8, 0x1EE8), + (0x1EEA, 0x1EEA), + (0x1EEC, 0x1EEC), + (0x1EEE, 0x1EEE), + (0x1EF0, 0x1EF0), + (0x1EF2, 0x1EF2), + (0x1EF4, 0x1EF4), + (0x1EF6, 0x1EF6), + (0x1EF8, 0x1EF8), + (0x1EFA, 0x1EFA), + (0x1EFC, 0x1EFC), + (0x1EFE, 0x1EFE), + (0x1F08, 0x1F0F), + (0x1F18, 0x1F1D), + (0x1F28, 0x1F2F), + (0x1F38, 0x1F3F), + (0x1F48, 0x1F4D), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F5F), + (0x1F68, 0x1F6F), + (0x1F88, 0x1F8F), + (0x1F98, 0x1F9F), + (0x1FA8, 0x1FAF), + (0x1FB8, 0x1FBC), + (0x1FC8, 0x1FCC), + (0x1FD8, 0x1FDB), + (0x1FE8, 0x1FEC), + (0x1FF8, 0x1FFC), + (0x2126, 0x2126), + (0x212A, 0x212B), + (0x2132, 0x2132), + (0x2160, 0x216F), + (0x2183, 0x2183), + (0x24B6, 0x24CF), + (0x2C00, 0x2C2F), + (0x2C60, 0x2C60), + (0x2C62, 0x2C64), + (0x2C67, 0x2C67), + (0x2C69, 0x2C69), + (0x2C6B, 0x2C6B), + (0x2C6D, 0x2C70), + (0x2C72, 0x2C72), + (0x2C75, 0x2C75), + (0x2C7E, 0x2C80), + (0x2C82, 0x2C82), + (0x2C84, 0x2C84), + (0x2C86, 0x2C86), + (0x2C88, 0x2C88), + (0x2C8A, 0x2C8A), + (0x2C8C, 0x2C8C), + (0x2C8E, 0x2C8E), + (0x2C90, 0x2C90), + (0x2C92, 0x2C92), + (0x2C94, 0x2C94), + (0x2C96, 0x2C96), + (0x2C98, 0x2C98), + (0x2C9A, 0x2C9A), + (0x2C9C, 0x2C9C), + (0x2C9E, 0x2C9E), + (0x2CA0, 0x2CA0), + (0x2CA2, 0x2CA2), + (0x2CA4, 0x2CA4), + (0x2CA6, 0x2CA6), + (0x2CA8, 0x2CA8), + (0x2CAA, 0x2CAA), + (0x2CAC, 0x2CAC), + (0x2CAE, 0x2CAE), + (0x2CB0, 0x2CB0), + (0x2CB2, 0x2CB2), + (0x2CB4, 0x2CB4), + (0x2CB6, 0x2CB6), + (0x2CB8, 0x2CB8), + (0x2CBA, 0x2CBA), + (0x2CBC, 0x2CBC), + (0x2CBE, 0x2CBE), + (0x2CC0, 0x2CC0), + (0x2CC2, 0x2CC2), + (0x2CC4, 0x2CC4), + (0x2CC6, 0x2CC6), + (0x2CC8, 0x2CC8), + (0x2CCA, 0x2CCA), + (0x2CCC, 0x2CCC), + (0x2CCE, 0x2CCE), + (0x2CD0, 0x2CD0), + (0x2CD2, 0x2CD2), + (0x2CD4, 0x2CD4), + (0x2CD6, 0x2CD6), + (0x2CD8, 0x2CD8), + (0x2CDA, 0x2CDA), + (0x2CDC, 0x2CDC), + (0x2CDE, 0x2CDE), + (0x2CE0, 0x2CE0), + (0x2CE2, 0x2CE2), + (0x2CEB, 0x2CEB), + (0x2CED, 0x2CED), + (0x2CF2, 0x2CF2), + (0xA640, 0xA640), + (0xA642, 0xA642), + (0xA644, 0xA644), + (0xA646, 0xA646), + (0xA648, 0xA648), + (0xA64A, 0xA64A), + (0xA64C, 0xA64C), + (0xA64E, 0xA64E), + (0xA650, 0xA650), + (0xA652, 0xA652), + (0xA654, 0xA654), + (0xA656, 0xA656), + (0xA658, 0xA658), + (0xA65A, 0xA65A), + (0xA65C, 0xA65C), + (0xA65E, 0xA65E), + (0xA660, 0xA660), + (0xA662, 0xA662), + (0xA664, 0xA664), + (0xA666, 0xA666), + (0xA668, 0xA668), + (0xA66A, 0xA66A), + (0xA66C, 0xA66C), + (0xA680, 0xA680), + (0xA682, 0xA682), + (0xA684, 0xA684), + (0xA686, 0xA686), + (0xA688, 0xA688), + (0xA68A, 0xA68A), + (0xA68C, 0xA68C), + (0xA68E, 0xA68E), + (0xA690, 0xA690), + (0xA692, 0xA692), + (0xA694, 0xA694), + (0xA696, 0xA696), + (0xA698, 0xA698), + (0xA69A, 0xA69A), + (0xA722, 0xA722), + (0xA724, 0xA724), + (0xA726, 0xA726), + (0xA728, 0xA728), + (0xA72A, 0xA72A), + (0xA72C, 0xA72C), + (0xA72E, 0xA72E), + (0xA732, 0xA732), + (0xA734, 0xA734), + (0xA736, 0xA736), + (0xA738, 0xA738), + (0xA73A, 0xA73A), + (0xA73C, 0xA73C), + (0xA73E, 0xA73E), + (0xA740, 0xA740), + (0xA742, 0xA742), + (0xA744, 0xA744), + (0xA746, 0xA746), + (0xA748, 0xA748), + (0xA74A, 0xA74A), + (0xA74C, 0xA74C), + (0xA74E, 0xA74E), + (0xA750, 0xA750), + (0xA752, 0xA752), + (0xA754, 0xA754), + (0xA756, 0xA756), + (0xA758, 0xA758), + (0xA75A, 0xA75A), + (0xA75C, 0xA75C), + (0xA75E, 0xA75E), + (0xA760, 0xA760), + (0xA762, 0xA762), + (0xA764, 0xA764), + (0xA766, 0xA766), + (0xA768, 0xA768), + (0xA76A, 0xA76A), + (0xA76C, 0xA76C), + (0xA76E, 0xA76E), + (0xA779, 0xA779), + (0xA77B, 0xA77B), + (0xA77D, 0xA77E), + (0xA780, 0xA780), + (0xA782, 0xA782), + (0xA784, 0xA784), + (0xA786, 0xA786), + (0xA78B, 0xA78B), + (0xA78D, 0xA78D), + (0xA790, 0xA790), + (0xA792, 0xA792), + (0xA796, 0xA796), + (0xA798, 0xA798), + (0xA79A, 0xA79A), + (0xA79C, 0xA79C), + (0xA79E, 0xA79E), + (0xA7A0, 0xA7A0), + (0xA7A2, 0xA7A2), + (0xA7A4, 0xA7A4), + (0xA7A6, 0xA7A6), + (0xA7A8, 0xA7A8), + (0xA7AA, 0xA7AE), + (0xA7B0, 0xA7B4), + (0xA7B6, 0xA7B6), + (0xA7B8, 0xA7B8), + (0xA7BA, 0xA7BA), + (0xA7BC, 0xA7BC), + (0xA7BE, 0xA7BE), + (0xA7C0, 0xA7C0), + (0xA7C2, 0xA7C2), + (0xA7C4, 0xA7C7), + (0xA7C9, 0xA7C9), + (0xA7CB, 0xA7CC), + (0xA7CE, 0xA7CE), + (0xA7D0, 0xA7D0), + (0xA7D2, 0xA7D2), + (0xA7D4, 0xA7D4), + (0xA7D6, 0xA7D6), + (0xA7D8, 0xA7D8), + (0xA7DA, 0xA7DA), + (0xA7DC, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F5, 0xA7F5), + (0xAB6C, 0xAB6D), + (0xFF21, 0xFF3A), + (0x10400, 0x10427), + (0x104B0, 0x104D3), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10C80, 0x10CB2), + (0x10D50, 0x10D65), + (0x118A0, 0x118BF), + (0x16E40, 0x16E5F), + (0x16EA0, 0x16EB8), + (0x1DF40, 0x1DF40), + (0x1DF48, 0x1DF48), + (0x1DF4A, 0x1DF4A), + (0x1DF4D, 0x1DF4D), + (0x1DF51, 0x1DF51), + (0x1DF68, 0x1DF68), + (0x1DF6A, 0x1DF6A), + (0x1DF6C, 0x1DF6C), + (0x1DF6E, 0x1DF6E), + (0x1DF72, 0x1DF72), + (0x1DF74, 0x1DF74), + (0x1DF76, 0x1DF76), + (0x1DF78, 0x1DF78), + (0x1DF7A, 0x1DF7A), + (0x1DF7C, 0x1DF7C), + (0x1DF7E, 0x1DF7E), + (0x1E900, 0x1E921), +]; + +/// The code points with the binary property Changes_When_NFKC_Casefolded, as ranges. +pub static BINARY_CHANGES_WHEN_NFKC_CASEFOLDED: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0xA0, 0xA0), + (0xA8, 0xA8), + (0xAA, 0xAA), + (0xAD, 0xAD), + (0xAF, 0xAF), + (0xB2, 0xB5), + (0xB8, 0xBA), + (0xBC, 0xBE), + (0xC0, 0xD6), + (0xD8, 0xDF), + (0x100, 0x100), + (0x102, 0x102), + (0x104, 0x104), + (0x106, 0x106), + (0x108, 0x108), + (0x10A, 0x10A), + (0x10C, 0x10C), + (0x10E, 0x10E), + (0x110, 0x110), + (0x112, 0x112), + (0x114, 0x114), + (0x116, 0x116), + (0x118, 0x118), + (0x11A, 0x11A), + (0x11C, 0x11C), + (0x11E, 0x11E), + (0x120, 0x120), + (0x122, 0x122), + (0x124, 0x124), + (0x126, 0x126), + (0x128, 0x128), + (0x12A, 0x12A), + (0x12C, 0x12C), + (0x12E, 0x12E), + (0x130, 0x130), + (0x132, 0x134), + (0x136, 0x136), + (0x139, 0x139), + (0x13B, 0x13B), + (0x13D, 0x13D), + (0x13F, 0x141), + (0x143, 0x143), + (0x145, 0x145), + (0x147, 0x147), + (0x149, 0x14A), + (0x14C, 0x14C), + (0x14E, 0x14E), + (0x150, 0x150), + (0x152, 0x152), + (0x154, 0x154), + (0x156, 0x156), + (0x158, 0x158), + (0x15A, 0x15A), + (0x15C, 0x15C), + (0x15E, 0x15E), + (0x160, 0x160), + (0x162, 0x162), + (0x164, 0x164), + (0x166, 0x166), + (0x168, 0x168), + (0x16A, 0x16A), + (0x16C, 0x16C), + (0x16E, 0x16E), + (0x170, 0x170), + (0x172, 0x172), + (0x174, 0x174), + (0x176, 0x176), + (0x178, 0x179), + (0x17B, 0x17B), + (0x17D, 0x17D), + (0x17F, 0x17F), + (0x181, 0x182), + (0x184, 0x184), + (0x186, 0x187), + (0x189, 0x18B), + (0x18E, 0x191), + (0x193, 0x194), + (0x196, 0x198), + (0x19C, 0x19D), + (0x19F, 0x1A0), + (0x1A2, 0x1A2), + (0x1A4, 0x1A4), + (0x1A6, 0x1A7), + (0x1A9, 0x1A9), + (0x1AC, 0x1AC), + (0x1AE, 0x1AF), + (0x1B1, 0x1B3), + (0x1B5, 0x1B5), + (0x1B7, 0x1B8), + (0x1BC, 0x1BC), + (0x1C4, 0x1CD), + (0x1CF, 0x1CF), + (0x1D1, 0x1D1), + (0x1D3, 0x1D3), + (0x1D5, 0x1D5), + (0x1D7, 0x1D7), + (0x1D9, 0x1D9), + (0x1DB, 0x1DB), + (0x1DE, 0x1DE), + (0x1E0, 0x1E0), + (0x1E2, 0x1E2), + (0x1E4, 0x1E4), + (0x1E6, 0x1E6), + (0x1E8, 0x1E8), + (0x1EA, 0x1EA), + (0x1EC, 0x1EC), + (0x1EE, 0x1EE), + (0x1F1, 0x1F4), + (0x1F6, 0x1F8), + (0x1FA, 0x1FA), + (0x1FC, 0x1FC), + (0x1FE, 0x1FE), + (0x200, 0x200), + (0x202, 0x202), + (0x204, 0x204), + (0x206, 0x206), + (0x208, 0x208), + (0x20A, 0x20A), + (0x20C, 0x20C), + (0x20E, 0x20E), + (0x210, 0x210), + (0x212, 0x212), + (0x214, 0x214), + (0x216, 0x216), + (0x218, 0x218), + (0x21A, 0x21A), + (0x21C, 0x21C), + (0x21E, 0x21E), + (0x220, 0x220), + (0x222, 0x222), + (0x224, 0x224), + (0x226, 0x226), + (0x228, 0x228), + (0x22A, 0x22A), + (0x22C, 0x22C), + (0x22E, 0x22E), + (0x230, 0x230), + (0x232, 0x232), + (0x23A, 0x23B), + (0x23D, 0x23E), + (0x241, 0x241), + (0x243, 0x246), + (0x248, 0x248), + (0x24A, 0x24A), + (0x24C, 0x24C), + (0x24E, 0x24E), + (0x2B0, 0x2B8), + (0x2D8, 0x2DD), + (0x2E0, 0x2E4), + (0x340, 0x341), + (0x343, 0x345), + (0x34F, 0x34F), + (0x370, 0x370), + (0x372, 0x372), + (0x374, 0x374), + (0x376, 0x376), + (0x37A, 0x37A), + (0x37E, 0x37F), + (0x384, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x38F), + (0x391, 0x3A1), + (0x3A3, 0x3AB), + (0x3C2, 0x3C2), + (0x3CF, 0x3D6), + (0x3D8, 0x3D8), + (0x3DA, 0x3DA), + (0x3DC, 0x3DC), + (0x3DE, 0x3DE), + (0x3E0, 0x3E0), + (0x3E2, 0x3E2), + (0x3E4, 0x3E4), + (0x3E6, 0x3E6), + (0x3E8, 0x3E8), + (0x3EA, 0x3EA), + (0x3EC, 0x3EC), + (0x3EE, 0x3EE), + (0x3F0, 0x3F2), + (0x3F4, 0x3F5), + (0x3F7, 0x3F7), + (0x3F9, 0x3FA), + (0x3FD, 0x42F), + (0x460, 0x460), + (0x462, 0x462), + (0x464, 0x464), + (0x466, 0x466), + (0x468, 0x468), + (0x46A, 0x46A), + (0x46C, 0x46C), + (0x46E, 0x46E), + (0x470, 0x470), + (0x472, 0x472), + (0x474, 0x474), + (0x476, 0x476), + (0x478, 0x478), + (0x47A, 0x47A), + (0x47C, 0x47C), + (0x47E, 0x47E), + (0x480, 0x480), + (0x48A, 0x48A), + (0x48C, 0x48C), + (0x48E, 0x48E), + (0x490, 0x490), + (0x492, 0x492), + (0x494, 0x494), + (0x496, 0x496), + (0x498, 0x498), + (0x49A, 0x49A), + (0x49C, 0x49C), + (0x49E, 0x49E), + (0x4A0, 0x4A0), + (0x4A2, 0x4A2), + (0x4A4, 0x4A4), + (0x4A6, 0x4A6), + (0x4A8, 0x4A8), + (0x4AA, 0x4AA), + (0x4AC, 0x4AC), + (0x4AE, 0x4AE), + (0x4B0, 0x4B0), + (0x4B2, 0x4B2), + (0x4B4, 0x4B4), + (0x4B6, 0x4B6), + (0x4B8, 0x4B8), + (0x4BA, 0x4BA), + (0x4BC, 0x4BC), + (0x4BE, 0x4BE), + (0x4C0, 0x4C1), + (0x4C3, 0x4C3), + (0x4C5, 0x4C5), + (0x4C7, 0x4C7), + (0x4C9, 0x4C9), + (0x4CB, 0x4CB), + (0x4CD, 0x4CD), + (0x4D0, 0x4D0), + (0x4D2, 0x4D2), + (0x4D4, 0x4D4), + (0x4D6, 0x4D6), + (0x4D8, 0x4D8), + (0x4DA, 0x4DA), + (0x4DC, 0x4DC), + (0x4DE, 0x4DE), + (0x4E0, 0x4E0), + (0x4E2, 0x4E2), + (0x4E4, 0x4E4), + (0x4E6, 0x4E6), + (0x4E8, 0x4E8), + (0x4EA, 0x4EA), + (0x4EC, 0x4EC), + (0x4EE, 0x4EE), + (0x4F0, 0x4F0), + (0x4F2, 0x4F2), + (0x4F4, 0x4F4), + (0x4F6, 0x4F6), + (0x4F8, 0x4F8), + (0x4FA, 0x4FA), + (0x4FC, 0x4FC), + (0x4FE, 0x4FE), + (0x500, 0x500), + (0x502, 0x502), + (0x504, 0x504), + (0x506, 0x506), + (0x508, 0x508), + (0x50A, 0x50A), + (0x50C, 0x50C), + (0x50E, 0x50E), + (0x510, 0x510), + (0x512, 0x512), + (0x514, 0x514), + (0x516, 0x516), + (0x518, 0x518), + (0x51A, 0x51A), + (0x51C, 0x51C), + (0x51E, 0x51E), + (0x520, 0x520), + (0x522, 0x522), + (0x524, 0x524), + (0x526, 0x526), + (0x528, 0x528), + (0x52A, 0x52A), + (0x52C, 0x52C), + (0x52E, 0x52E), + (0x531, 0x556), + (0x558, 0x558), + (0x587, 0x587), + (0x58B, 0x58C), + (0x61C, 0x61C), + (0x675, 0x678), + (0x958, 0x95F), + (0x9DC, 0x9DD), + (0x9DF, 0x9DF), + (0xA33, 0xA33), + (0xA36, 0xA36), + (0xA59, 0xA5B), + (0xA5E, 0xA5E), + (0xB5C, 0xB5D), + (0xE33, 0xE33), + (0xEB3, 0xEB3), + (0xEDC, 0xEDD), + (0xF0C, 0xF0C), + (0xF43, 0xF43), + (0xF4D, 0xF4D), + (0xF52, 0xF52), + (0xF57, 0xF57), + (0xF5C, 0xF5C), + (0xF69, 0xF69), + (0xF73, 0xF73), + (0xF75, 0xF79), + (0xF81, 0xF81), + (0xF93, 0xF93), + (0xF9D, 0xF9D), + (0xFA2, 0xFA2), + (0xFA7, 0xFA7), + (0xFAC, 0xFAC), + (0xFB9, 0xFB9), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10FC, 0x10FC), + (0x115F, 0x1160), + (0x13F8, 0x13FD), + (0x17B4, 0x17B5), + (0x180B, 0x180F), + (0x1C80, 0x1C89), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1D2C, 0x1D2E), + (0x1D30, 0x1D3A), + (0x1D3C, 0x1D4D), + (0x1D4F, 0x1D6A), + (0x1D78, 0x1D78), + (0x1D9B, 0x1DBF), + (0x1E00, 0x1E00), + (0x1E02, 0x1E02), + (0x1E04, 0x1E04), + (0x1E06, 0x1E06), + (0x1E08, 0x1E08), + (0x1E0A, 0x1E0A), + (0x1E0C, 0x1E0C), + (0x1E0E, 0x1E0E), + (0x1E10, 0x1E10), + (0x1E12, 0x1E12), + (0x1E14, 0x1E14), + (0x1E16, 0x1E16), + (0x1E18, 0x1E18), + (0x1E1A, 0x1E1A), + (0x1E1C, 0x1E1C), + (0x1E1E, 0x1E1E), + (0x1E20, 0x1E20), + (0x1E22, 0x1E22), + (0x1E24, 0x1E24), + (0x1E26, 0x1E26), + (0x1E28, 0x1E28), + (0x1E2A, 0x1E2A), + (0x1E2C, 0x1E2C), + (0x1E2E, 0x1E2E), + (0x1E30, 0x1E30), + (0x1E32, 0x1E32), + (0x1E34, 0x1E34), + (0x1E36, 0x1E36), + (0x1E38, 0x1E38), + (0x1E3A, 0x1E3A), + (0x1E3C, 0x1E3C), + (0x1E3E, 0x1E3E), + (0x1E40, 0x1E40), + (0x1E42, 0x1E42), + (0x1E44, 0x1E44), + (0x1E46, 0x1E46), + (0x1E48, 0x1E48), + (0x1E4A, 0x1E4A), + (0x1E4C, 0x1E4C), + (0x1E4E, 0x1E4E), + (0x1E50, 0x1E50), + (0x1E52, 0x1E52), + (0x1E54, 0x1E54), + (0x1E56, 0x1E56), + (0x1E58, 0x1E58), + (0x1E5A, 0x1E5A), + (0x1E5C, 0x1E5C), + (0x1E5E, 0x1E5E), + (0x1E60, 0x1E60), + (0x1E62, 0x1E62), + (0x1E64, 0x1E64), + (0x1E66, 0x1E66), + (0x1E68, 0x1E68), + (0x1E6A, 0x1E6A), + (0x1E6C, 0x1E6C), + (0x1E6E, 0x1E6E), + (0x1E70, 0x1E70), + (0x1E72, 0x1E72), + (0x1E74, 0x1E74), + (0x1E76, 0x1E76), + (0x1E78, 0x1E78), + (0x1E7A, 0x1E7A), + (0x1E7C, 0x1E7C), + (0x1E7E, 0x1E7E), + (0x1E80, 0x1E80), + (0x1E82, 0x1E82), + (0x1E84, 0x1E84), + (0x1E86, 0x1E86), + (0x1E88, 0x1E88), + (0x1E8A, 0x1E8A), + (0x1E8C, 0x1E8C), + (0x1E8E, 0x1E8E), + (0x1E90, 0x1E90), + (0x1E92, 0x1E92), + (0x1E94, 0x1E94), + (0x1E9A, 0x1E9B), + (0x1E9E, 0x1E9E), + (0x1EA0, 0x1EA0), + (0x1EA2, 0x1EA2), + (0x1EA4, 0x1EA4), + (0x1EA6, 0x1EA6), + (0x1EA8, 0x1EA8), + (0x1EAA, 0x1EAA), + (0x1EAC, 0x1EAC), + (0x1EAE, 0x1EAE), + (0x1EB0, 0x1EB0), + (0x1EB2, 0x1EB2), + (0x1EB4, 0x1EB4), + (0x1EB6, 0x1EB6), + (0x1EB8, 0x1EB8), + (0x1EBA, 0x1EBA), + (0x1EBC, 0x1EBC), + (0x1EBE, 0x1EBE), + (0x1EC0, 0x1EC0), + (0x1EC2, 0x1EC2), + (0x1EC4, 0x1EC4), + (0x1EC6, 0x1EC6), + (0x1EC8, 0x1EC8), + (0x1ECA, 0x1ECA), + (0x1ECC, 0x1ECC), + (0x1ECE, 0x1ECE), + (0x1ED0, 0x1ED0), + (0x1ED2, 0x1ED2), + (0x1ED4, 0x1ED4), + (0x1ED6, 0x1ED6), + (0x1ED8, 0x1ED8), + (0x1EDA, 0x1EDA), + (0x1EDC, 0x1EDC), + (0x1EDE, 0x1EDE), + (0x1EE0, 0x1EE0), + (0x1EE2, 0x1EE2), + (0x1EE4, 0x1EE4), + (0x1EE6, 0x1EE6), + (0x1EE8, 0x1EE8), + (0x1EEA, 0x1EEA), + (0x1EEC, 0x1EEC), + (0x1EEE, 0x1EEE), + (0x1EF0, 0x1EF0), + (0x1EF2, 0x1EF2), + (0x1EF4, 0x1EF4), + (0x1EF6, 0x1EF6), + (0x1EF8, 0x1EF8), + (0x1EFA, 0x1EFA), + (0x1EFC, 0x1EFC), + (0x1EFE, 0x1EFE), + (0x1F08, 0x1F0F), + (0x1F18, 0x1F1D), + (0x1F28, 0x1F2F), + (0x1F38, 0x1F3F), + (0x1F48, 0x1F4D), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F5F), + (0x1F68, 0x1F6F), + (0x1F71, 0x1F71), + (0x1F73, 0x1F73), + (0x1F75, 0x1F75), + (0x1F77, 0x1F77), + (0x1F79, 0x1F79), + (0x1F7B, 0x1F7B), + (0x1F7D, 0x1F7D), + (0x1F80, 0x1FAF), + (0x1FB2, 0x1FB4), + (0x1FB7, 0x1FC4), + (0x1FC7, 0x1FCF), + (0x1FD3, 0x1FD3), + (0x1FD8, 0x1FDB), + (0x1FDD, 0x1FDF), + (0x1FE3, 0x1FE3), + (0x1FE8, 0x1FEF), + (0x1FF2, 0x1FF4), + (0x1FF7, 0x1FFE), + (0x2000, 0x200F), + (0x2011, 0x2011), + (0x2017, 0x2017), + (0x2024, 0x2026), + (0x202A, 0x202F), + (0x2033, 0x2034), + (0x2036, 0x2037), + (0x203C, 0x203C), + (0x203E, 0x203E), + (0x2047, 0x2049), + (0x2057, 0x2057), + (0x205F, 0x2071), + (0x2074, 0x208E), + (0x2090, 0x209F), + (0x20A8, 0x20A8), + (0x2100, 0x2103), + (0x2105, 0x2107), + (0x2109, 0x2113), + (0x2115, 0x2116), + (0x2119, 0x211D), + (0x2120, 0x2122), + (0x2124, 0x2124), + (0x2126, 0x2126), + (0x2128, 0x2128), + (0x212A, 0x212D), + (0x212F, 0x2139), + (0x213B, 0x2140), + (0x2145, 0x2149), + (0x2150, 0x217F), + (0x2183, 0x2183), + (0x2189, 0x2189), + (0x222C, 0x222D), + (0x222F, 0x2230), + (0x2329, 0x232A), + (0x2460, 0x24EA), + (0x2A0C, 0x2A0C), + (0x2A74, 0x2A76), + (0x2ADC, 0x2ADC), + (0x2C00, 0x2C2F), + (0x2C60, 0x2C60), + (0x2C62, 0x2C64), + (0x2C67, 0x2C67), + (0x2C69, 0x2C69), + (0x2C6B, 0x2C6B), + (0x2C6D, 0x2C70), + (0x2C72, 0x2C72), + (0x2C75, 0x2C75), + (0x2C7C, 0x2C80), + (0x2C82, 0x2C82), + (0x2C84, 0x2C84), + (0x2C86, 0x2C86), + (0x2C88, 0x2C88), + (0x2C8A, 0x2C8A), + (0x2C8C, 0x2C8C), + (0x2C8E, 0x2C8E), + (0x2C90, 0x2C90), + (0x2C92, 0x2C92), + (0x2C94, 0x2C94), + (0x2C96, 0x2C96), + (0x2C98, 0x2C98), + (0x2C9A, 0x2C9A), + (0x2C9C, 0x2C9C), + (0x2C9E, 0x2C9E), + (0x2CA0, 0x2CA0), + (0x2CA2, 0x2CA2), + (0x2CA4, 0x2CA4), + (0x2CA6, 0x2CA6), + (0x2CA8, 0x2CA8), + (0x2CAA, 0x2CAA), + (0x2CAC, 0x2CAC), + (0x2CAE, 0x2CAE), + (0x2CB0, 0x2CB0), + (0x2CB2, 0x2CB2), + (0x2CB4, 0x2CB4), + (0x2CB6, 0x2CB6), + (0x2CB8, 0x2CB8), + (0x2CBA, 0x2CBA), + (0x2CBC, 0x2CBC), + (0x2CBE, 0x2CBE), + (0x2CC0, 0x2CC0), + (0x2CC2, 0x2CC2), + (0x2CC4, 0x2CC4), + (0x2CC6, 0x2CC6), + (0x2CC8, 0x2CC8), + (0x2CCA, 0x2CCA), + (0x2CCC, 0x2CCC), + (0x2CCE, 0x2CCE), + (0x2CD0, 0x2CD0), + (0x2CD2, 0x2CD2), + (0x2CD4, 0x2CD4), + (0x2CD6, 0x2CD6), + (0x2CD8, 0x2CD8), + (0x2CDA, 0x2CDA), + (0x2CDC, 0x2CDC), + (0x2CDE, 0x2CDE), + (0x2CE0, 0x2CE0), + (0x2CE2, 0x2CE2), + (0x2CEB, 0x2CEB), + (0x2CED, 0x2CED), + (0x2CF2, 0x2CF2), + (0x2D6F, 0x2D6F), + (0x2E9F, 0x2E9F), + (0x2EF3, 0x2EF3), + (0x2F00, 0x2FD5), + (0x3000, 0x3000), + (0x3036, 0x3036), + (0x3038, 0x303A), + (0x309B, 0x309C), + (0x309F, 0x309F), + (0x30FF, 0x30FF), + (0x3131, 0x318E), + (0x3192, 0x319F), + (0x3200, 0x321E), + (0x3220, 0x3247), + (0x3250, 0x327E), + (0x3280, 0x33FF), + (0xA640, 0xA640), + (0xA642, 0xA642), + (0xA644, 0xA644), + (0xA646, 0xA646), + (0xA648, 0xA648), + (0xA64A, 0xA64A), + (0xA64C, 0xA64C), + (0xA64E, 0xA64E), + (0xA650, 0xA650), + (0xA652, 0xA652), + (0xA654, 0xA654), + (0xA656, 0xA656), + (0xA658, 0xA658), + (0xA65A, 0xA65A), + (0xA65C, 0xA65C), + (0xA65E, 0xA65E), + (0xA660, 0xA660), + (0xA662, 0xA662), + (0xA664, 0xA664), + (0xA666, 0xA666), + (0xA668, 0xA668), + (0xA66A, 0xA66A), + (0xA66C, 0xA66C), + (0xA680, 0xA680), + (0xA682, 0xA682), + (0xA684, 0xA684), + (0xA686, 0xA686), + (0xA688, 0xA688), + (0xA68A, 0xA68A), + (0xA68C, 0xA68C), + (0xA68E, 0xA68E), + (0xA690, 0xA690), + (0xA692, 0xA692), + (0xA694, 0xA694), + (0xA696, 0xA696), + (0xA698, 0xA698), + (0xA69A, 0xA69A), + (0xA69C, 0xA69D), + (0xA722, 0xA722), + (0xA724, 0xA724), + (0xA726, 0xA726), + (0xA728, 0xA728), + (0xA72A, 0xA72A), + (0xA72C, 0xA72C), + (0xA72E, 0xA72E), + (0xA732, 0xA732), + (0xA734, 0xA734), + (0xA736, 0xA736), + (0xA738, 0xA738), + (0xA73A, 0xA73A), + (0xA73C, 0xA73C), + (0xA73E, 0xA73E), + (0xA740, 0xA740), + (0xA742, 0xA742), + (0xA744, 0xA744), + (0xA746, 0xA746), + (0xA748, 0xA748), + (0xA74A, 0xA74A), + (0xA74C, 0xA74C), + (0xA74E, 0xA74E), + (0xA750, 0xA750), + (0xA752, 0xA752), + (0xA754, 0xA754), + (0xA756, 0xA756), + (0xA758, 0xA758), + (0xA75A, 0xA75A), + (0xA75C, 0xA75C), + (0xA75E, 0xA75E), + (0xA760, 0xA760), + (0xA762, 0xA762), + (0xA764, 0xA764), + (0xA766, 0xA766), + (0xA768, 0xA768), + (0xA76A, 0xA76A), + (0xA76C, 0xA76C), + (0xA76E, 0xA76E), + (0xA770, 0xA770), + (0xA779, 0xA779), + (0xA77B, 0xA77B), + (0xA77D, 0xA77E), + (0xA780, 0xA780), + (0xA782, 0xA782), + (0xA784, 0xA784), + (0xA786, 0xA786), + (0xA78B, 0xA78B), + (0xA78D, 0xA78D), + (0xA790, 0xA790), + (0xA792, 0xA792), + (0xA796, 0xA796), + (0xA798, 0xA798), + (0xA79A, 0xA79A), + (0xA79C, 0xA79C), + (0xA79E, 0xA79E), + (0xA7A0, 0xA7A0), + (0xA7A2, 0xA7A2), + (0xA7A4, 0xA7A4), + (0xA7A6, 0xA7A6), + (0xA7A8, 0xA7A8), + (0xA7AA, 0xA7AE), + (0xA7B0, 0xA7B4), + (0xA7B6, 0xA7B6), + (0xA7B8, 0xA7B8), + (0xA7BA, 0xA7BA), + (0xA7BC, 0xA7BC), + (0xA7BE, 0xA7BE), + (0xA7C0, 0xA7C0), + (0xA7C2, 0xA7C2), + (0xA7C4, 0xA7C7), + (0xA7C9, 0xA7C9), + (0xA7CB, 0xA7CC), + (0xA7CE, 0xA7CE), + (0xA7D0, 0xA7D0), + (0xA7D2, 0xA7D2), + (0xA7D4, 0xA7D4), + (0xA7D6, 0xA7D6), + (0xA7D8, 0xA7D8), + (0xA7DA, 0xA7DA), + (0xA7DC, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F1, 0xA7F5), + (0xA7F8, 0xA7F9), + (0xAB5C, 0xAB5F), + (0xAB69, 0xAB69), + (0xAB6C, 0xAB6D), + (0xAB70, 0xABBF), + (0xF900, 0xFA0D), + (0xFA10, 0xFA10), + (0xFA12, 0xFA12), + (0xFA15, 0xFA1E), + (0xFA20, 0xFA20), + (0xFA22, 0xFA22), + (0xFA25, 0xFA26), + (0xFA2A, 0xFA6D), + (0xFA70, 0xFAD9), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFB1D, 0xFB1D), + (0xFB1F, 0xFB36), + (0xFB38, 0xFB3C), + (0xFB3E, 0xFB3E), + (0xFB40, 0xFB41), + (0xFB43, 0xFB44), + (0xFB46, 0xFBB1), + (0xFBD3, 0xFD3D), + (0xFD50, 0xFD8F), + (0xFD92, 0xFDC7), + (0xFDF0, 0xFDFC), + (0xFE00, 0xFE19), + (0xFE30, 0xFE44), + (0xFE47, 0xFE52), + (0xFE54, 0xFE66), + (0xFE68, 0xFE6B), + (0xFE70, 0xFE72), + (0xFE74, 0xFE74), + (0xFE76, 0xFEFC), + (0xFEFF, 0xFEFF), + (0xFF01, 0xFFBE), + (0xFFC2, 0xFFC7), + (0xFFCA, 0xFFCF), + (0xFFD2, 0xFFD7), + (0xFFDA, 0xFFDC), + (0xFFE0, 0xFFE6), + (0xFFE8, 0xFFEE), + (0xFFF0, 0xFFF8), + (0x10400, 0x10427), + (0x104B0, 0x104D3), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10781, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x10C80, 0x10CB2), + (0x10D50, 0x10D65), + (0x118A0, 0x118BF), + (0x16E40, 0x16E5F), + (0x16EA0, 0x16EB8), + (0x1B123, 0x1B126), + (0x1BCA0, 0x1BCA3), + (0x1CCD6, 0x1CCF9), + (0x1D15E, 0x1D164), + (0x1D173, 0x1D17A), + (0x1D1BB, 0x1D1C0), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D7CB), + (0x1D7CE, 0x1D7FF), + (0x1DF40, 0x1DF40), + (0x1DF48, 0x1DF48), + (0x1DF4A, 0x1DF4A), + (0x1DF4D, 0x1DF4D), + (0x1DF51, 0x1DF51), + (0x1DF68, 0x1DF68), + (0x1DF6A, 0x1DF6A), + (0x1DF6C, 0x1DF6C), + (0x1DF6E, 0x1DF6E), + (0x1DF72, 0x1DF72), + (0x1DF74, 0x1DF74), + (0x1DF76, 0x1DF76), + (0x1DF78, 0x1DF78), + (0x1DF7A, 0x1DF7A), + (0x1DF7C, 0x1DF7C), + (0x1DF7E, 0x1DF7E), + (0x1DF95, 0x1DF95), + (0x1DFCD, 0x1DFFF), + (0x1E030, 0x1E06D), + (0x1E900, 0x1E921), + (0x1EE00, 0x1EE03), + (0x1EE05, 0x1EE1F), + (0x1EE21, 0x1EE22), + (0x1EE24, 0x1EE24), + (0x1EE27, 0x1EE27), + (0x1EE29, 0x1EE32), + (0x1EE34, 0x1EE37), + (0x1EE39, 0x1EE39), + (0x1EE3B, 0x1EE3B), + (0x1EE42, 0x1EE42), + (0x1EE47, 0x1EE47), + (0x1EE49, 0x1EE49), + (0x1EE4B, 0x1EE4B), + (0x1EE4D, 0x1EE4F), + (0x1EE51, 0x1EE52), + (0x1EE54, 0x1EE54), + (0x1EE57, 0x1EE57), + (0x1EE59, 0x1EE59), + (0x1EE5B, 0x1EE5B), + (0x1EE5D, 0x1EE5D), + (0x1EE5F, 0x1EE5F), + (0x1EE61, 0x1EE62), + (0x1EE64, 0x1EE64), + (0x1EE67, 0x1EE6A), + (0x1EE6C, 0x1EE72), + (0x1EE74, 0x1EE77), + (0x1EE79, 0x1EE7C), + (0x1EE7E, 0x1EE7E), + (0x1EE80, 0x1EE89), + (0x1EE8B, 0x1EE9B), + (0x1EEA1, 0x1EEA3), + (0x1EEA5, 0x1EEA9), + (0x1EEAB, 0x1EEBB), + (0x1F100, 0x1F10A), + (0x1F110, 0x1F12E), + (0x1F130, 0x1F14F), + (0x1F16A, 0x1F16C), + (0x1F190, 0x1F190), + (0x1F200, 0x1F202), + (0x1F210, 0x1F23B), + (0x1F240, 0x1F248), + (0x1F250, 0x1F251), + (0x1FBF0, 0x1FBF9), + (0x2F800, 0x2FA1D), + (0xE0000, 0xE0FFF), +]; + +/// The code points with the binary property Changes_When_Titlecased, as ranges. +pub static BINARY_CHANGES_WHEN_TITLECASED: &[(u32, u32)] = &[ + (0x61, 0x7A), + (0xB5, 0xB5), + (0xDF, 0xF6), + (0xF8, 0xFF), + (0x101, 0x101), + (0x103, 0x103), + (0x105, 0x105), + (0x107, 0x107), + (0x109, 0x109), + (0x10B, 0x10B), + (0x10D, 0x10D), + (0x10F, 0x10F), + (0x111, 0x111), + (0x113, 0x113), + (0x115, 0x115), + (0x117, 0x117), + (0x119, 0x119), + (0x11B, 0x11B), + (0x11D, 0x11D), + (0x11F, 0x11F), + (0x121, 0x121), + (0x123, 0x123), + (0x125, 0x125), + (0x127, 0x127), + (0x129, 0x129), + (0x12B, 0x12B), + (0x12D, 0x12D), + (0x12F, 0x12F), + (0x131, 0x131), + (0x133, 0x133), + (0x135, 0x135), + (0x137, 0x137), + (0x13A, 0x13A), + (0x13C, 0x13C), + (0x13E, 0x13E), + (0x140, 0x140), + (0x142, 0x142), + (0x144, 0x144), + (0x146, 0x146), + (0x148, 0x149), + (0x14B, 0x14B), + (0x14D, 0x14D), + (0x14F, 0x14F), + (0x151, 0x151), + (0x153, 0x153), + (0x155, 0x155), + (0x157, 0x157), + (0x159, 0x159), + (0x15B, 0x15B), + (0x15D, 0x15D), + (0x15F, 0x15F), + (0x161, 0x161), + (0x163, 0x163), + (0x165, 0x165), + (0x167, 0x167), + (0x169, 0x169), + (0x16B, 0x16B), + (0x16D, 0x16D), + (0x16F, 0x16F), + (0x171, 0x171), + (0x173, 0x173), + (0x175, 0x175), + (0x177, 0x177), + (0x17A, 0x17A), + (0x17C, 0x17C), + (0x17E, 0x180), + (0x183, 0x183), + (0x185, 0x185), + (0x188, 0x188), + (0x18C, 0x18C), + (0x192, 0x192), + (0x195, 0x195), + (0x199, 0x19B), + (0x19E, 0x19E), + (0x1A1, 0x1A1), + (0x1A3, 0x1A3), + (0x1A5, 0x1A5), + (0x1A8, 0x1A8), + (0x1AD, 0x1AD), + (0x1B0, 0x1B0), + (0x1B4, 0x1B4), + (0x1B6, 0x1B6), + (0x1B9, 0x1B9), + (0x1BD, 0x1BD), + (0x1BF, 0x1BF), + (0x1C4, 0x1C4), + (0x1C6, 0x1C7), + (0x1C9, 0x1CA), + (0x1CC, 0x1CC), + (0x1CE, 0x1CE), + (0x1D0, 0x1D0), + (0x1D2, 0x1D2), + (0x1D4, 0x1D4), + (0x1D6, 0x1D6), + (0x1D8, 0x1D8), + (0x1DA, 0x1DA), + (0x1DC, 0x1DD), + (0x1DF, 0x1DF), + (0x1E1, 0x1E1), + (0x1E3, 0x1E3), + (0x1E5, 0x1E5), + (0x1E7, 0x1E7), + (0x1E9, 0x1E9), + (0x1EB, 0x1EB), + (0x1ED, 0x1ED), + (0x1EF, 0x1F1), + (0x1F3, 0x1F3), + (0x1F5, 0x1F5), + (0x1F9, 0x1F9), + (0x1FB, 0x1FB), + (0x1FD, 0x1FD), + (0x1FF, 0x1FF), + (0x201, 0x201), + (0x203, 0x203), + (0x205, 0x205), + (0x207, 0x207), + (0x209, 0x209), + (0x20B, 0x20B), + (0x20D, 0x20D), + (0x20F, 0x20F), + (0x211, 0x211), + (0x213, 0x213), + (0x215, 0x215), + (0x217, 0x217), + (0x219, 0x219), + (0x21B, 0x21B), + (0x21D, 0x21D), + (0x21F, 0x21F), + (0x223, 0x223), + (0x225, 0x225), + (0x227, 0x227), + (0x229, 0x229), + (0x22B, 0x22B), + (0x22D, 0x22D), + (0x22F, 0x22F), + (0x231, 0x231), + (0x233, 0x233), + (0x23C, 0x23C), + (0x23F, 0x240), + (0x242, 0x242), + (0x247, 0x247), + (0x249, 0x249), + (0x24B, 0x24B), + (0x24D, 0x24D), + (0x24F, 0x254), + (0x256, 0x257), + (0x259, 0x259), + (0x25B, 0x25C), + (0x260, 0x261), + (0x263, 0x266), + (0x268, 0x26C), + (0x26F, 0x26F), + (0x271, 0x272), + (0x275, 0x275), + (0x277, 0x277), + (0x27C, 0x27D), + (0x280, 0x280), + (0x282, 0x283), + (0x287, 0x28C), + (0x292, 0x292), + (0x29D, 0x29E), + (0x345, 0x345), + (0x371, 0x371), + (0x373, 0x373), + (0x377, 0x377), + (0x37B, 0x37D), + (0x390, 0x390), + (0x3AC, 0x3CE), + (0x3D0, 0x3D1), + (0x3D5, 0x3D7), + (0x3D9, 0x3D9), + (0x3DB, 0x3DB), + (0x3DD, 0x3DD), + (0x3DF, 0x3DF), + (0x3E1, 0x3E1), + (0x3E3, 0x3E3), + (0x3E5, 0x3E5), + (0x3E7, 0x3E7), + (0x3E9, 0x3E9), + (0x3EB, 0x3EB), + (0x3ED, 0x3ED), + (0x3EF, 0x3F3), + (0x3F5, 0x3F5), + (0x3F8, 0x3F8), + (0x3FB, 0x3FB), + (0x430, 0x45F), + (0x461, 0x461), + (0x463, 0x463), + (0x465, 0x465), + (0x467, 0x467), + (0x469, 0x469), + (0x46B, 0x46B), + (0x46D, 0x46D), + (0x46F, 0x46F), + (0x471, 0x471), + (0x473, 0x473), + (0x475, 0x475), + (0x477, 0x477), + (0x479, 0x479), + (0x47B, 0x47B), + (0x47D, 0x47D), + (0x47F, 0x47F), + (0x481, 0x481), + (0x48B, 0x48B), + (0x48D, 0x48D), + (0x48F, 0x48F), + (0x491, 0x491), + (0x493, 0x493), + (0x495, 0x495), + (0x497, 0x497), + (0x499, 0x499), + (0x49B, 0x49B), + (0x49D, 0x49D), + (0x49F, 0x49F), + (0x4A1, 0x4A1), + (0x4A3, 0x4A3), + (0x4A5, 0x4A5), + (0x4A7, 0x4A7), + (0x4A9, 0x4A9), + (0x4AB, 0x4AB), + (0x4AD, 0x4AD), + (0x4AF, 0x4AF), + (0x4B1, 0x4B1), + (0x4B3, 0x4B3), + (0x4B5, 0x4B5), + (0x4B7, 0x4B7), + (0x4B9, 0x4B9), + (0x4BB, 0x4BB), + (0x4BD, 0x4BD), + (0x4BF, 0x4BF), + (0x4C2, 0x4C2), + (0x4C4, 0x4C4), + (0x4C6, 0x4C6), + (0x4C8, 0x4C8), + (0x4CA, 0x4CA), + (0x4CC, 0x4CC), + (0x4CE, 0x4CF), + (0x4D1, 0x4D1), + (0x4D3, 0x4D3), + (0x4D5, 0x4D5), + (0x4D7, 0x4D7), + (0x4D9, 0x4D9), + (0x4DB, 0x4DB), + (0x4DD, 0x4DD), + (0x4DF, 0x4DF), + (0x4E1, 0x4E1), + (0x4E3, 0x4E3), + (0x4E5, 0x4E5), + (0x4E7, 0x4E7), + (0x4E9, 0x4E9), + (0x4EB, 0x4EB), + (0x4ED, 0x4ED), + (0x4EF, 0x4EF), + (0x4F1, 0x4F1), + (0x4F3, 0x4F3), + (0x4F5, 0x4F5), + (0x4F7, 0x4F7), + (0x4F9, 0x4F9), + (0x4FB, 0x4FB), + (0x4FD, 0x4FD), + (0x4FF, 0x4FF), + (0x501, 0x501), + (0x503, 0x503), + (0x505, 0x505), + (0x507, 0x507), + (0x509, 0x509), + (0x50B, 0x50B), + (0x50D, 0x50D), + (0x50F, 0x50F), + (0x511, 0x511), + (0x513, 0x513), + (0x515, 0x515), + (0x517, 0x517), + (0x519, 0x519), + (0x51B, 0x51B), + (0x51D, 0x51D), + (0x51F, 0x51F), + (0x521, 0x521), + (0x523, 0x523), + (0x525, 0x525), + (0x527, 0x527), + (0x529, 0x529), + (0x52B, 0x52B), + (0x52D, 0x52D), + (0x52F, 0x52F), + (0x561, 0x587), + (0x13F8, 0x13FD), + (0x1C80, 0x1C88), + (0x1C8A, 0x1C8A), + (0x1D79, 0x1D79), + (0x1D7D, 0x1D7D), + (0x1D8E, 0x1D8E), + (0x1E01, 0x1E01), + (0x1E03, 0x1E03), + (0x1E05, 0x1E05), + (0x1E07, 0x1E07), + (0x1E09, 0x1E09), + (0x1E0B, 0x1E0B), + (0x1E0D, 0x1E0D), + (0x1E0F, 0x1E0F), + (0x1E11, 0x1E11), + (0x1E13, 0x1E13), + (0x1E15, 0x1E15), + (0x1E17, 0x1E17), + (0x1E19, 0x1E19), + (0x1E1B, 0x1E1B), + (0x1E1D, 0x1E1D), + (0x1E1F, 0x1E1F), + (0x1E21, 0x1E21), + (0x1E23, 0x1E23), + (0x1E25, 0x1E25), + (0x1E27, 0x1E27), + (0x1E29, 0x1E29), + (0x1E2B, 0x1E2B), + (0x1E2D, 0x1E2D), + (0x1E2F, 0x1E2F), + (0x1E31, 0x1E31), + (0x1E33, 0x1E33), + (0x1E35, 0x1E35), + (0x1E37, 0x1E37), + (0x1E39, 0x1E39), + (0x1E3B, 0x1E3B), + (0x1E3D, 0x1E3D), + (0x1E3F, 0x1E3F), + (0x1E41, 0x1E41), + (0x1E43, 0x1E43), + (0x1E45, 0x1E45), + (0x1E47, 0x1E47), + (0x1E49, 0x1E49), + (0x1E4B, 0x1E4B), + (0x1E4D, 0x1E4D), + (0x1E4F, 0x1E4F), + (0x1E51, 0x1E51), + (0x1E53, 0x1E53), + (0x1E55, 0x1E55), + (0x1E57, 0x1E57), + (0x1E59, 0x1E59), + (0x1E5B, 0x1E5B), + (0x1E5D, 0x1E5D), + (0x1E5F, 0x1E5F), + (0x1E61, 0x1E61), + (0x1E63, 0x1E63), + (0x1E65, 0x1E65), + (0x1E67, 0x1E67), + (0x1E69, 0x1E69), + (0x1E6B, 0x1E6B), + (0x1E6D, 0x1E6D), + (0x1E6F, 0x1E6F), + (0x1E71, 0x1E71), + (0x1E73, 0x1E73), + (0x1E75, 0x1E75), + (0x1E77, 0x1E77), + (0x1E79, 0x1E79), + (0x1E7B, 0x1E7B), + (0x1E7D, 0x1E7D), + (0x1E7F, 0x1E7F), + (0x1E81, 0x1E81), + (0x1E83, 0x1E83), + (0x1E85, 0x1E85), + (0x1E87, 0x1E87), + (0x1E89, 0x1E89), + (0x1E8B, 0x1E8B), + (0x1E8D, 0x1E8D), + (0x1E8F, 0x1E8F), + (0x1E91, 0x1E91), + (0x1E93, 0x1E93), + (0x1E95, 0x1E9B), + (0x1EA1, 0x1EA1), + (0x1EA3, 0x1EA3), + (0x1EA5, 0x1EA5), + (0x1EA7, 0x1EA7), + (0x1EA9, 0x1EA9), + (0x1EAB, 0x1EAB), + (0x1EAD, 0x1EAD), + (0x1EAF, 0x1EAF), + (0x1EB1, 0x1EB1), + (0x1EB3, 0x1EB3), + (0x1EB5, 0x1EB5), + (0x1EB7, 0x1EB7), + (0x1EB9, 0x1EB9), + (0x1EBB, 0x1EBB), + (0x1EBD, 0x1EBD), + (0x1EBF, 0x1EBF), + (0x1EC1, 0x1EC1), + (0x1EC3, 0x1EC3), + (0x1EC5, 0x1EC5), + (0x1EC7, 0x1EC7), + (0x1EC9, 0x1EC9), + (0x1ECB, 0x1ECB), + (0x1ECD, 0x1ECD), + (0x1ECF, 0x1ECF), + (0x1ED1, 0x1ED1), + (0x1ED3, 0x1ED3), + (0x1ED5, 0x1ED5), + (0x1ED7, 0x1ED7), + (0x1ED9, 0x1ED9), + (0x1EDB, 0x1EDB), + (0x1EDD, 0x1EDD), + (0x1EDF, 0x1EDF), + (0x1EE1, 0x1EE1), + (0x1EE3, 0x1EE3), + (0x1EE5, 0x1EE5), + (0x1EE7, 0x1EE7), + (0x1EE9, 0x1EE9), + (0x1EEB, 0x1EEB), + (0x1EED, 0x1EED), + (0x1EEF, 0x1EEF), + (0x1EF1, 0x1EF1), + (0x1EF3, 0x1EF3), + (0x1EF5, 0x1EF5), + (0x1EF7, 0x1EF7), + (0x1EF9, 0x1EF9), + (0x1EFB, 0x1EFB), + (0x1EFD, 0x1EFD), + (0x1EFF, 0x1F07), + (0x1F10, 0x1F15), + (0x1F20, 0x1F27), + (0x1F30, 0x1F37), + (0x1F40, 0x1F45), + (0x1F50, 0x1F57), + (0x1F60, 0x1F67), + (0x1F70, 0x1F7D), + (0x1F80, 0x1F87), + (0x1F90, 0x1F97), + (0x1FA0, 0x1FA7), + (0x1FB0, 0x1FB4), + (0x1FB6, 0x1FB7), + (0x1FBE, 0x1FBE), + (0x1FC2, 0x1FC4), + (0x1FC6, 0x1FC7), + (0x1FD0, 0x1FD3), + (0x1FD6, 0x1FD7), + (0x1FE0, 0x1FE7), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FF7), + (0x214E, 0x214E), + (0x2170, 0x217F), + (0x2184, 0x2184), + (0x24D0, 0x24E9), + (0x2C30, 0x2C5F), + (0x2C61, 0x2C61), + (0x2C65, 0x2C66), + (0x2C68, 0x2C68), + (0x2C6A, 0x2C6A), + (0x2C6C, 0x2C6C), + (0x2C73, 0x2C73), + (0x2C76, 0x2C76), + (0x2C81, 0x2C81), + (0x2C83, 0x2C83), + (0x2C85, 0x2C85), + (0x2C87, 0x2C87), + (0x2C89, 0x2C89), + (0x2C8B, 0x2C8B), + (0x2C8D, 0x2C8D), + (0x2C8F, 0x2C8F), + (0x2C91, 0x2C91), + (0x2C93, 0x2C93), + (0x2C95, 0x2C95), + (0x2C97, 0x2C97), + (0x2C99, 0x2C99), + (0x2C9B, 0x2C9B), + (0x2C9D, 0x2C9D), + (0x2C9F, 0x2C9F), + (0x2CA1, 0x2CA1), + (0x2CA3, 0x2CA3), + (0x2CA5, 0x2CA5), + (0x2CA7, 0x2CA7), + (0x2CA9, 0x2CA9), + (0x2CAB, 0x2CAB), + (0x2CAD, 0x2CAD), + (0x2CAF, 0x2CAF), + (0x2CB1, 0x2CB1), + (0x2CB3, 0x2CB3), + (0x2CB5, 0x2CB5), + (0x2CB7, 0x2CB7), + (0x2CB9, 0x2CB9), + (0x2CBB, 0x2CBB), + (0x2CBD, 0x2CBD), + (0x2CBF, 0x2CBF), + (0x2CC1, 0x2CC1), + (0x2CC3, 0x2CC3), + (0x2CC5, 0x2CC5), + (0x2CC7, 0x2CC7), + (0x2CC9, 0x2CC9), + (0x2CCB, 0x2CCB), + (0x2CCD, 0x2CCD), + (0x2CCF, 0x2CCF), + (0x2CD1, 0x2CD1), + (0x2CD3, 0x2CD3), + (0x2CD5, 0x2CD5), + (0x2CD7, 0x2CD7), + (0x2CD9, 0x2CD9), + (0x2CDB, 0x2CDB), + (0x2CDD, 0x2CDD), + (0x2CDF, 0x2CDF), + (0x2CE1, 0x2CE1), + (0x2CE3, 0x2CE3), + (0x2CEC, 0x2CEC), + (0x2CEE, 0x2CEE), + (0x2CF3, 0x2CF3), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0xA641, 0xA641), + (0xA643, 0xA643), + (0xA645, 0xA645), + (0xA647, 0xA647), + (0xA649, 0xA649), + (0xA64B, 0xA64B), + (0xA64D, 0xA64D), + (0xA64F, 0xA64F), + (0xA651, 0xA651), + (0xA653, 0xA653), + (0xA655, 0xA655), + (0xA657, 0xA657), + (0xA659, 0xA659), + (0xA65B, 0xA65B), + (0xA65D, 0xA65D), + (0xA65F, 0xA65F), + (0xA661, 0xA661), + (0xA663, 0xA663), + (0xA665, 0xA665), + (0xA667, 0xA667), + (0xA669, 0xA669), + (0xA66B, 0xA66B), + (0xA66D, 0xA66D), + (0xA681, 0xA681), + (0xA683, 0xA683), + (0xA685, 0xA685), + (0xA687, 0xA687), + (0xA689, 0xA689), + (0xA68B, 0xA68B), + (0xA68D, 0xA68D), + (0xA68F, 0xA68F), + (0xA691, 0xA691), + (0xA693, 0xA693), + (0xA695, 0xA695), + (0xA697, 0xA697), + (0xA699, 0xA699), + (0xA69B, 0xA69B), + (0xA723, 0xA723), + (0xA725, 0xA725), + (0xA727, 0xA727), + (0xA729, 0xA729), + (0xA72B, 0xA72B), + (0xA72D, 0xA72D), + (0xA72F, 0xA72F), + (0xA733, 0xA733), + (0xA735, 0xA735), + (0xA737, 0xA737), + (0xA739, 0xA739), + (0xA73B, 0xA73B), + (0xA73D, 0xA73D), + (0xA73F, 0xA73F), + (0xA741, 0xA741), + (0xA743, 0xA743), + (0xA745, 0xA745), + (0xA747, 0xA747), + (0xA749, 0xA749), + (0xA74B, 0xA74B), + (0xA74D, 0xA74D), + (0xA74F, 0xA74F), + (0xA751, 0xA751), + (0xA753, 0xA753), + (0xA755, 0xA755), + (0xA757, 0xA757), + (0xA759, 0xA759), + (0xA75B, 0xA75B), + (0xA75D, 0xA75D), + (0xA75F, 0xA75F), + (0xA761, 0xA761), + (0xA763, 0xA763), + (0xA765, 0xA765), + (0xA767, 0xA767), + (0xA769, 0xA769), + (0xA76B, 0xA76B), + (0xA76D, 0xA76D), + (0xA76F, 0xA76F), + (0xA77A, 0xA77A), + (0xA77C, 0xA77C), + (0xA77F, 0xA77F), + (0xA781, 0xA781), + (0xA783, 0xA783), + (0xA785, 0xA785), + (0xA787, 0xA787), + (0xA78C, 0xA78C), + (0xA791, 0xA791), + (0xA793, 0xA794), + (0xA797, 0xA797), + (0xA799, 0xA799), + (0xA79B, 0xA79B), + (0xA79D, 0xA79D), + (0xA79F, 0xA79F), + (0xA7A1, 0xA7A1), + (0xA7A3, 0xA7A3), + (0xA7A5, 0xA7A5), + (0xA7A7, 0xA7A7), + (0xA7A9, 0xA7A9), + (0xA7B5, 0xA7B5), + (0xA7B7, 0xA7B7), + (0xA7B9, 0xA7B9), + (0xA7BB, 0xA7BB), + (0xA7BD, 0xA7BD), + (0xA7BF, 0xA7BF), + (0xA7C1, 0xA7C1), + (0xA7C3, 0xA7C3), + (0xA7C8, 0xA7C8), + (0xA7CA, 0xA7CA), + (0xA7CD, 0xA7CD), + (0xA7CF, 0xA7CF), + (0xA7D1, 0xA7D1), + (0xA7D3, 0xA7D3), + (0xA7D5, 0xA7D5), + (0xA7D7, 0xA7D7), + (0xA7D9, 0xA7D9), + (0xA7DB, 0xA7DB), + (0xA7F6, 0xA7F6), + (0xAB4B, 0xAB4C), + (0xAB53, 0xAB53), + (0xAB70, 0xABBF), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFF41, 0xFF5A), + (0x10428, 0x1044F), + (0x104D8, 0x104FB), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x10CC0, 0x10CF2), + (0x10D70, 0x10D85), + (0x118C0, 0x118DF), + (0x16E60, 0x16E7F), + (0x16EBB, 0x16ED3), + (0x1DF41, 0x1DF41), + (0x1DF49, 0x1DF49), + (0x1DF4B, 0x1DF4B), + (0x1DF4E, 0x1DF4E), + (0x1DF52, 0x1DF52), + (0x1DF69, 0x1DF69), + (0x1DF6B, 0x1DF6B), + (0x1DF6D, 0x1DF6D), + (0x1DF6F, 0x1DF6F), + (0x1DF73, 0x1DF73), + (0x1DF75, 0x1DF75), + (0x1DF77, 0x1DF77), + (0x1DF79, 0x1DF79), + (0x1DF7B, 0x1DF7B), + (0x1DF7D, 0x1DF7D), + (0x1DF7F, 0x1DF7F), + (0x1DF95, 0x1DF95), + (0x1E922, 0x1E943), +]; + +/// The code points with the binary property Changes_When_Uppercased, as ranges. +pub static BINARY_CHANGES_WHEN_UPPERCASED: &[(u32, u32)] = &[ + (0x61, 0x7A), + (0xB5, 0xB5), + (0xDF, 0xF6), + (0xF8, 0xFF), + (0x101, 0x101), + (0x103, 0x103), + (0x105, 0x105), + (0x107, 0x107), + (0x109, 0x109), + (0x10B, 0x10B), + (0x10D, 0x10D), + (0x10F, 0x10F), + (0x111, 0x111), + (0x113, 0x113), + (0x115, 0x115), + (0x117, 0x117), + (0x119, 0x119), + (0x11B, 0x11B), + (0x11D, 0x11D), + (0x11F, 0x11F), + (0x121, 0x121), + (0x123, 0x123), + (0x125, 0x125), + (0x127, 0x127), + (0x129, 0x129), + (0x12B, 0x12B), + (0x12D, 0x12D), + (0x12F, 0x12F), + (0x131, 0x131), + (0x133, 0x133), + (0x135, 0x135), + (0x137, 0x137), + (0x13A, 0x13A), + (0x13C, 0x13C), + (0x13E, 0x13E), + (0x140, 0x140), + (0x142, 0x142), + (0x144, 0x144), + (0x146, 0x146), + (0x148, 0x149), + (0x14B, 0x14B), + (0x14D, 0x14D), + (0x14F, 0x14F), + (0x151, 0x151), + (0x153, 0x153), + (0x155, 0x155), + (0x157, 0x157), + (0x159, 0x159), + (0x15B, 0x15B), + (0x15D, 0x15D), + (0x15F, 0x15F), + (0x161, 0x161), + (0x163, 0x163), + (0x165, 0x165), + (0x167, 0x167), + (0x169, 0x169), + (0x16B, 0x16B), + (0x16D, 0x16D), + (0x16F, 0x16F), + (0x171, 0x171), + (0x173, 0x173), + (0x175, 0x175), + (0x177, 0x177), + (0x17A, 0x17A), + (0x17C, 0x17C), + (0x17E, 0x180), + (0x183, 0x183), + (0x185, 0x185), + (0x188, 0x188), + (0x18C, 0x18C), + (0x192, 0x192), + (0x195, 0x195), + (0x199, 0x19B), + (0x19E, 0x19E), + (0x1A1, 0x1A1), + (0x1A3, 0x1A3), + (0x1A5, 0x1A5), + (0x1A8, 0x1A8), + (0x1AD, 0x1AD), + (0x1B0, 0x1B0), + (0x1B4, 0x1B4), + (0x1B6, 0x1B6), + (0x1B9, 0x1B9), + (0x1BD, 0x1BD), + (0x1BF, 0x1BF), + (0x1C5, 0x1C6), + (0x1C8, 0x1C9), + (0x1CB, 0x1CC), + (0x1CE, 0x1CE), + (0x1D0, 0x1D0), + (0x1D2, 0x1D2), + (0x1D4, 0x1D4), + (0x1D6, 0x1D6), + (0x1D8, 0x1D8), + (0x1DA, 0x1DA), + (0x1DC, 0x1DD), + (0x1DF, 0x1DF), + (0x1E1, 0x1E1), + (0x1E3, 0x1E3), + (0x1E5, 0x1E5), + (0x1E7, 0x1E7), + (0x1E9, 0x1E9), + (0x1EB, 0x1EB), + (0x1ED, 0x1ED), + (0x1EF, 0x1F0), + (0x1F2, 0x1F3), + (0x1F5, 0x1F5), + (0x1F9, 0x1F9), + (0x1FB, 0x1FB), + (0x1FD, 0x1FD), + (0x1FF, 0x1FF), + (0x201, 0x201), + (0x203, 0x203), + (0x205, 0x205), + (0x207, 0x207), + (0x209, 0x209), + (0x20B, 0x20B), + (0x20D, 0x20D), + (0x20F, 0x20F), + (0x211, 0x211), + (0x213, 0x213), + (0x215, 0x215), + (0x217, 0x217), + (0x219, 0x219), + (0x21B, 0x21B), + (0x21D, 0x21D), + (0x21F, 0x21F), + (0x223, 0x223), + (0x225, 0x225), + (0x227, 0x227), + (0x229, 0x229), + (0x22B, 0x22B), + (0x22D, 0x22D), + (0x22F, 0x22F), + (0x231, 0x231), + (0x233, 0x233), + (0x23C, 0x23C), + (0x23F, 0x240), + (0x242, 0x242), + (0x247, 0x247), + (0x249, 0x249), + (0x24B, 0x24B), + (0x24D, 0x24D), + (0x24F, 0x254), + (0x256, 0x257), + (0x259, 0x259), + (0x25B, 0x25C), + (0x260, 0x261), + (0x263, 0x266), + (0x268, 0x26C), + (0x26F, 0x26F), + (0x271, 0x272), + (0x275, 0x275), + (0x277, 0x277), + (0x27C, 0x27D), + (0x280, 0x280), + (0x282, 0x283), + (0x287, 0x28C), + (0x292, 0x292), + (0x29D, 0x29E), + (0x345, 0x345), + (0x371, 0x371), + (0x373, 0x373), + (0x377, 0x377), + (0x37B, 0x37D), + (0x390, 0x390), + (0x3AC, 0x3CE), + (0x3D0, 0x3D1), + (0x3D5, 0x3D7), + (0x3D9, 0x3D9), + (0x3DB, 0x3DB), + (0x3DD, 0x3DD), + (0x3DF, 0x3DF), + (0x3E1, 0x3E1), + (0x3E3, 0x3E3), + (0x3E5, 0x3E5), + (0x3E7, 0x3E7), + (0x3E9, 0x3E9), + (0x3EB, 0x3EB), + (0x3ED, 0x3ED), + (0x3EF, 0x3F3), + (0x3F5, 0x3F5), + (0x3F8, 0x3F8), + (0x3FB, 0x3FB), + (0x430, 0x45F), + (0x461, 0x461), + (0x463, 0x463), + (0x465, 0x465), + (0x467, 0x467), + (0x469, 0x469), + (0x46B, 0x46B), + (0x46D, 0x46D), + (0x46F, 0x46F), + (0x471, 0x471), + (0x473, 0x473), + (0x475, 0x475), + (0x477, 0x477), + (0x479, 0x479), + (0x47B, 0x47B), + (0x47D, 0x47D), + (0x47F, 0x47F), + (0x481, 0x481), + (0x48B, 0x48B), + (0x48D, 0x48D), + (0x48F, 0x48F), + (0x491, 0x491), + (0x493, 0x493), + (0x495, 0x495), + (0x497, 0x497), + (0x499, 0x499), + (0x49B, 0x49B), + (0x49D, 0x49D), + (0x49F, 0x49F), + (0x4A1, 0x4A1), + (0x4A3, 0x4A3), + (0x4A5, 0x4A5), + (0x4A7, 0x4A7), + (0x4A9, 0x4A9), + (0x4AB, 0x4AB), + (0x4AD, 0x4AD), + (0x4AF, 0x4AF), + (0x4B1, 0x4B1), + (0x4B3, 0x4B3), + (0x4B5, 0x4B5), + (0x4B7, 0x4B7), + (0x4B9, 0x4B9), + (0x4BB, 0x4BB), + (0x4BD, 0x4BD), + (0x4BF, 0x4BF), + (0x4C2, 0x4C2), + (0x4C4, 0x4C4), + (0x4C6, 0x4C6), + (0x4C8, 0x4C8), + (0x4CA, 0x4CA), + (0x4CC, 0x4CC), + (0x4CE, 0x4CF), + (0x4D1, 0x4D1), + (0x4D3, 0x4D3), + (0x4D5, 0x4D5), + (0x4D7, 0x4D7), + (0x4D9, 0x4D9), + (0x4DB, 0x4DB), + (0x4DD, 0x4DD), + (0x4DF, 0x4DF), + (0x4E1, 0x4E1), + (0x4E3, 0x4E3), + (0x4E5, 0x4E5), + (0x4E7, 0x4E7), + (0x4E9, 0x4E9), + (0x4EB, 0x4EB), + (0x4ED, 0x4ED), + (0x4EF, 0x4EF), + (0x4F1, 0x4F1), + (0x4F3, 0x4F3), + (0x4F5, 0x4F5), + (0x4F7, 0x4F7), + (0x4F9, 0x4F9), + (0x4FB, 0x4FB), + (0x4FD, 0x4FD), + (0x4FF, 0x4FF), + (0x501, 0x501), + (0x503, 0x503), + (0x505, 0x505), + (0x507, 0x507), + (0x509, 0x509), + (0x50B, 0x50B), + (0x50D, 0x50D), + (0x50F, 0x50F), + (0x511, 0x511), + (0x513, 0x513), + (0x515, 0x515), + (0x517, 0x517), + (0x519, 0x519), + (0x51B, 0x51B), + (0x51D, 0x51D), + (0x51F, 0x51F), + (0x521, 0x521), + (0x523, 0x523), + (0x525, 0x525), + (0x527, 0x527), + (0x529, 0x529), + (0x52B, 0x52B), + (0x52D, 0x52D), + (0x52F, 0x52F), + (0x561, 0x587), + (0x10D0, 0x10FA), + (0x10FD, 0x10FF), + (0x13F8, 0x13FD), + (0x1C80, 0x1C88), + (0x1C8A, 0x1C8A), + (0x1D79, 0x1D79), + (0x1D7D, 0x1D7D), + (0x1D8E, 0x1D8E), + (0x1E01, 0x1E01), + (0x1E03, 0x1E03), + (0x1E05, 0x1E05), + (0x1E07, 0x1E07), + (0x1E09, 0x1E09), + (0x1E0B, 0x1E0B), + (0x1E0D, 0x1E0D), + (0x1E0F, 0x1E0F), + (0x1E11, 0x1E11), + (0x1E13, 0x1E13), + (0x1E15, 0x1E15), + (0x1E17, 0x1E17), + (0x1E19, 0x1E19), + (0x1E1B, 0x1E1B), + (0x1E1D, 0x1E1D), + (0x1E1F, 0x1E1F), + (0x1E21, 0x1E21), + (0x1E23, 0x1E23), + (0x1E25, 0x1E25), + (0x1E27, 0x1E27), + (0x1E29, 0x1E29), + (0x1E2B, 0x1E2B), + (0x1E2D, 0x1E2D), + (0x1E2F, 0x1E2F), + (0x1E31, 0x1E31), + (0x1E33, 0x1E33), + (0x1E35, 0x1E35), + (0x1E37, 0x1E37), + (0x1E39, 0x1E39), + (0x1E3B, 0x1E3B), + (0x1E3D, 0x1E3D), + (0x1E3F, 0x1E3F), + (0x1E41, 0x1E41), + (0x1E43, 0x1E43), + (0x1E45, 0x1E45), + (0x1E47, 0x1E47), + (0x1E49, 0x1E49), + (0x1E4B, 0x1E4B), + (0x1E4D, 0x1E4D), + (0x1E4F, 0x1E4F), + (0x1E51, 0x1E51), + (0x1E53, 0x1E53), + (0x1E55, 0x1E55), + (0x1E57, 0x1E57), + (0x1E59, 0x1E59), + (0x1E5B, 0x1E5B), + (0x1E5D, 0x1E5D), + (0x1E5F, 0x1E5F), + (0x1E61, 0x1E61), + (0x1E63, 0x1E63), + (0x1E65, 0x1E65), + (0x1E67, 0x1E67), + (0x1E69, 0x1E69), + (0x1E6B, 0x1E6B), + (0x1E6D, 0x1E6D), + (0x1E6F, 0x1E6F), + (0x1E71, 0x1E71), + (0x1E73, 0x1E73), + (0x1E75, 0x1E75), + (0x1E77, 0x1E77), + (0x1E79, 0x1E79), + (0x1E7B, 0x1E7B), + (0x1E7D, 0x1E7D), + (0x1E7F, 0x1E7F), + (0x1E81, 0x1E81), + (0x1E83, 0x1E83), + (0x1E85, 0x1E85), + (0x1E87, 0x1E87), + (0x1E89, 0x1E89), + (0x1E8B, 0x1E8B), + (0x1E8D, 0x1E8D), + (0x1E8F, 0x1E8F), + (0x1E91, 0x1E91), + (0x1E93, 0x1E93), + (0x1E95, 0x1E9B), + (0x1EA1, 0x1EA1), + (0x1EA3, 0x1EA3), + (0x1EA5, 0x1EA5), + (0x1EA7, 0x1EA7), + (0x1EA9, 0x1EA9), + (0x1EAB, 0x1EAB), + (0x1EAD, 0x1EAD), + (0x1EAF, 0x1EAF), + (0x1EB1, 0x1EB1), + (0x1EB3, 0x1EB3), + (0x1EB5, 0x1EB5), + (0x1EB7, 0x1EB7), + (0x1EB9, 0x1EB9), + (0x1EBB, 0x1EBB), + (0x1EBD, 0x1EBD), + (0x1EBF, 0x1EBF), + (0x1EC1, 0x1EC1), + (0x1EC3, 0x1EC3), + (0x1EC5, 0x1EC5), + (0x1EC7, 0x1EC7), + (0x1EC9, 0x1EC9), + (0x1ECB, 0x1ECB), + (0x1ECD, 0x1ECD), + (0x1ECF, 0x1ECF), + (0x1ED1, 0x1ED1), + (0x1ED3, 0x1ED3), + (0x1ED5, 0x1ED5), + (0x1ED7, 0x1ED7), + (0x1ED9, 0x1ED9), + (0x1EDB, 0x1EDB), + (0x1EDD, 0x1EDD), + (0x1EDF, 0x1EDF), + (0x1EE1, 0x1EE1), + (0x1EE3, 0x1EE3), + (0x1EE5, 0x1EE5), + (0x1EE7, 0x1EE7), + (0x1EE9, 0x1EE9), + (0x1EEB, 0x1EEB), + (0x1EED, 0x1EED), + (0x1EEF, 0x1EEF), + (0x1EF1, 0x1EF1), + (0x1EF3, 0x1EF3), + (0x1EF5, 0x1EF5), + (0x1EF7, 0x1EF7), + (0x1EF9, 0x1EF9), + (0x1EFB, 0x1EFB), + (0x1EFD, 0x1EFD), + (0x1EFF, 0x1F07), + (0x1F10, 0x1F15), + (0x1F20, 0x1F27), + (0x1F30, 0x1F37), + (0x1F40, 0x1F45), + (0x1F50, 0x1F57), + (0x1F60, 0x1F67), + (0x1F70, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FB7), + (0x1FBC, 0x1FBC), + (0x1FBE, 0x1FBE), + (0x1FC2, 0x1FC4), + (0x1FC6, 0x1FC7), + (0x1FCC, 0x1FCC), + (0x1FD0, 0x1FD3), + (0x1FD6, 0x1FD7), + (0x1FE0, 0x1FE7), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FF7), + (0x1FFC, 0x1FFC), + (0x214E, 0x214E), + (0x2170, 0x217F), + (0x2184, 0x2184), + (0x24D0, 0x24E9), + (0x2C30, 0x2C5F), + (0x2C61, 0x2C61), + (0x2C65, 0x2C66), + (0x2C68, 0x2C68), + (0x2C6A, 0x2C6A), + (0x2C6C, 0x2C6C), + (0x2C73, 0x2C73), + (0x2C76, 0x2C76), + (0x2C81, 0x2C81), + (0x2C83, 0x2C83), + (0x2C85, 0x2C85), + (0x2C87, 0x2C87), + (0x2C89, 0x2C89), + (0x2C8B, 0x2C8B), + (0x2C8D, 0x2C8D), + (0x2C8F, 0x2C8F), + (0x2C91, 0x2C91), + (0x2C93, 0x2C93), + (0x2C95, 0x2C95), + (0x2C97, 0x2C97), + (0x2C99, 0x2C99), + (0x2C9B, 0x2C9B), + (0x2C9D, 0x2C9D), + (0x2C9F, 0x2C9F), + (0x2CA1, 0x2CA1), + (0x2CA3, 0x2CA3), + (0x2CA5, 0x2CA5), + (0x2CA7, 0x2CA7), + (0x2CA9, 0x2CA9), + (0x2CAB, 0x2CAB), + (0x2CAD, 0x2CAD), + (0x2CAF, 0x2CAF), + (0x2CB1, 0x2CB1), + (0x2CB3, 0x2CB3), + (0x2CB5, 0x2CB5), + (0x2CB7, 0x2CB7), + (0x2CB9, 0x2CB9), + (0x2CBB, 0x2CBB), + (0x2CBD, 0x2CBD), + (0x2CBF, 0x2CBF), + (0x2CC1, 0x2CC1), + (0x2CC3, 0x2CC3), + (0x2CC5, 0x2CC5), + (0x2CC7, 0x2CC7), + (0x2CC9, 0x2CC9), + (0x2CCB, 0x2CCB), + (0x2CCD, 0x2CCD), + (0x2CCF, 0x2CCF), + (0x2CD1, 0x2CD1), + (0x2CD3, 0x2CD3), + (0x2CD5, 0x2CD5), + (0x2CD7, 0x2CD7), + (0x2CD9, 0x2CD9), + (0x2CDB, 0x2CDB), + (0x2CDD, 0x2CDD), + (0x2CDF, 0x2CDF), + (0x2CE1, 0x2CE1), + (0x2CE3, 0x2CE3), + (0x2CEC, 0x2CEC), + (0x2CEE, 0x2CEE), + (0x2CF3, 0x2CF3), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0xA641, 0xA641), + (0xA643, 0xA643), + (0xA645, 0xA645), + (0xA647, 0xA647), + (0xA649, 0xA649), + (0xA64B, 0xA64B), + (0xA64D, 0xA64D), + (0xA64F, 0xA64F), + (0xA651, 0xA651), + (0xA653, 0xA653), + (0xA655, 0xA655), + (0xA657, 0xA657), + (0xA659, 0xA659), + (0xA65B, 0xA65B), + (0xA65D, 0xA65D), + (0xA65F, 0xA65F), + (0xA661, 0xA661), + (0xA663, 0xA663), + (0xA665, 0xA665), + (0xA667, 0xA667), + (0xA669, 0xA669), + (0xA66B, 0xA66B), + (0xA66D, 0xA66D), + (0xA681, 0xA681), + (0xA683, 0xA683), + (0xA685, 0xA685), + (0xA687, 0xA687), + (0xA689, 0xA689), + (0xA68B, 0xA68B), + (0xA68D, 0xA68D), + (0xA68F, 0xA68F), + (0xA691, 0xA691), + (0xA693, 0xA693), + (0xA695, 0xA695), + (0xA697, 0xA697), + (0xA699, 0xA699), + (0xA69B, 0xA69B), + (0xA723, 0xA723), + (0xA725, 0xA725), + (0xA727, 0xA727), + (0xA729, 0xA729), + (0xA72B, 0xA72B), + (0xA72D, 0xA72D), + (0xA72F, 0xA72F), + (0xA733, 0xA733), + (0xA735, 0xA735), + (0xA737, 0xA737), + (0xA739, 0xA739), + (0xA73B, 0xA73B), + (0xA73D, 0xA73D), + (0xA73F, 0xA73F), + (0xA741, 0xA741), + (0xA743, 0xA743), + (0xA745, 0xA745), + (0xA747, 0xA747), + (0xA749, 0xA749), + (0xA74B, 0xA74B), + (0xA74D, 0xA74D), + (0xA74F, 0xA74F), + (0xA751, 0xA751), + (0xA753, 0xA753), + (0xA755, 0xA755), + (0xA757, 0xA757), + (0xA759, 0xA759), + (0xA75B, 0xA75B), + (0xA75D, 0xA75D), + (0xA75F, 0xA75F), + (0xA761, 0xA761), + (0xA763, 0xA763), + (0xA765, 0xA765), + (0xA767, 0xA767), + (0xA769, 0xA769), + (0xA76B, 0xA76B), + (0xA76D, 0xA76D), + (0xA76F, 0xA76F), + (0xA77A, 0xA77A), + (0xA77C, 0xA77C), + (0xA77F, 0xA77F), + (0xA781, 0xA781), + (0xA783, 0xA783), + (0xA785, 0xA785), + (0xA787, 0xA787), + (0xA78C, 0xA78C), + (0xA791, 0xA791), + (0xA793, 0xA794), + (0xA797, 0xA797), + (0xA799, 0xA799), + (0xA79B, 0xA79B), + (0xA79D, 0xA79D), + (0xA79F, 0xA79F), + (0xA7A1, 0xA7A1), + (0xA7A3, 0xA7A3), + (0xA7A5, 0xA7A5), + (0xA7A7, 0xA7A7), + (0xA7A9, 0xA7A9), + (0xA7B5, 0xA7B5), + (0xA7B7, 0xA7B7), + (0xA7B9, 0xA7B9), + (0xA7BB, 0xA7BB), + (0xA7BD, 0xA7BD), + (0xA7BF, 0xA7BF), + (0xA7C1, 0xA7C1), + (0xA7C3, 0xA7C3), + (0xA7C8, 0xA7C8), + (0xA7CA, 0xA7CA), + (0xA7CD, 0xA7CD), + (0xA7CF, 0xA7CF), + (0xA7D1, 0xA7D1), + (0xA7D3, 0xA7D3), + (0xA7D5, 0xA7D5), + (0xA7D7, 0xA7D7), + (0xA7D9, 0xA7D9), + (0xA7DB, 0xA7DB), + (0xA7F6, 0xA7F6), + (0xAB4B, 0xAB4C), + (0xAB53, 0xAB53), + (0xAB70, 0xABBF), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFF41, 0xFF5A), + (0x10428, 0x1044F), + (0x104D8, 0x104FB), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x10CC0, 0x10CF2), + (0x10D70, 0x10D85), + (0x118C0, 0x118DF), + (0x16E60, 0x16E7F), + (0x16EBB, 0x16ED3), + (0x1DF41, 0x1DF41), + (0x1DF49, 0x1DF49), + (0x1DF4B, 0x1DF4B), + (0x1DF4E, 0x1DF4E), + (0x1DF52, 0x1DF52), + (0x1DF69, 0x1DF69), + (0x1DF6B, 0x1DF6B), + (0x1DF6D, 0x1DF6D), + (0x1DF6F, 0x1DF6F), + (0x1DF73, 0x1DF73), + (0x1DF75, 0x1DF75), + (0x1DF77, 0x1DF77), + (0x1DF79, 0x1DF79), + (0x1DF7B, 0x1DF7B), + (0x1DF7D, 0x1DF7D), + (0x1DF7F, 0x1DF7F), + (0x1DF95, 0x1DF95), + (0x1E922, 0x1E943), +]; + +/// The code points with the binary property Dash, as ranges. +pub static BINARY_DASH: &[(u32, u32)] = &[ + (0x2D, 0x2D), + (0x58A, 0x58A), + (0x5BE, 0x5BE), + (0x1400, 0x1400), + (0x1806, 0x1806), + (0x2010, 0x2015), + (0x2053, 0x2053), + (0x207B, 0x207B), + (0x208B, 0x208B), + (0x2212, 0x2212), + (0x2E17, 0x2E17), + (0x2E1A, 0x2E1A), + (0x2E3A, 0x2E3B), + (0x2E40, 0x2E40), + (0x2E5D, 0x2E5D), + (0x301C, 0x301C), + (0x3030, 0x3030), + (0x30A0, 0x30A0), + (0xFE31, 0xFE32), + (0xFE58, 0xFE58), + (0xFE63, 0xFE63), + (0xFF0D, 0xFF0D), + (0x10D6E, 0x10D6E), + (0x10EAD, 0x10EAD), +]; + +/// The code points with the binary property Default_Ignorable_Code_Point, as ranges. +pub static BINARY_DEFAULT_IGNORABLE_CODE_POINT: &[(u32, u32)] = &[ + (0xAD, 0xAD), + (0x34F, 0x34F), + (0x61C, 0x61C), + (0x115F, 0x1160), + (0x17B4, 0x17B5), + (0x180B, 0x180F), + (0x200B, 0x200F), + (0x202A, 0x202E), + (0x2060, 0x206F), + (0x3164, 0x3164), + (0xFE00, 0xFE0F), + (0xFEFF, 0xFEFF), + (0xFFA0, 0xFFA0), + (0xFFF0, 0xFFF8), + (0x1BCA0, 0x1BCA3), + (0x1D173, 0x1D17A), + (0xE0000, 0xE0FFF), +]; + +/// The code points with the binary property Deprecated, as ranges. +pub static BINARY_DEPRECATED: &[(u32, u32)] = &[ + (0x149, 0x149), + (0x673, 0x673), + (0xF77, 0xF77), + (0xF79, 0xF79), + (0x17A3, 0x17A4), + (0x206A, 0x206F), + (0x2329, 0x232A), + (0xE0001, 0xE0001), +]; + +/// The code points with the binary property Diacritic, as ranges. +pub static BINARY_DIACRITIC: &[(u32, u32)] = &[ + (0x5E, 0x5E), + (0x60, 0x60), + (0xA8, 0xA8), + (0xAF, 0xAF), + (0xB4, 0xB4), + (0xB7, 0xB8), + (0x2B0, 0x34E), + (0x350, 0x357), + (0x35D, 0x362), + (0x374, 0x375), + (0x37A, 0x37A), + (0x384, 0x385), + (0x483, 0x487), + (0x558, 0x559), + (0x58B, 0x58C), + (0x591, 0x5BD), + (0x5BF, 0x5BF), + (0x5C1, 0x5C2), + (0x5C4, 0x5C5), + (0x5C7, 0x5C9), + (0x64B, 0x652), + (0x656, 0x658), + (0x6DF, 0x6E0), + (0x6E2, 0x6E2), + (0x6E5, 0x6E6), + (0x6EA, 0x6EC), + (0x730, 0x74A), + (0x7A6, 0x7B0), + (0x7EB, 0x7F5), + (0x818, 0x819), + (0x898, 0x89F), + (0x8C9, 0x8D3), + (0x8E3, 0x8FE), + (0x93C, 0x93C), + (0x94D, 0x94D), + (0x951, 0x954), + (0x971, 0x971), + (0x9BC, 0x9BC), + (0x9CD, 0x9CD), + (0xA3C, 0xA3C), + (0xA4D, 0xA4D), + (0xABC, 0xABC), + (0xACD, 0xACD), + (0xAFD, 0xAFF), + (0xB3C, 0xB3C), + (0xB4D, 0xB4D), + (0xB53, 0xB55), + (0xBCD, 0xBCD), + (0xC3C, 0xC3C), + (0xC4D, 0xC4D), + (0xCBC, 0xCBC), + (0xCCD, 0xCCD), + (0xD3B, 0xD3C), + (0xD4D, 0xD4D), + (0xDCA, 0xDCA), + (0xE3A, 0xE3A), + (0xE47, 0xE4C), + (0xE4E, 0xE4E), + (0xEBA, 0xEBA), + (0xEC8, 0xECC), + (0xF18, 0xF19), + (0xF35, 0xF35), + (0xF37, 0xF37), + (0xF39, 0xF39), + (0xF3E, 0xF3F), + (0xF82, 0xF84), + (0xF86, 0xF87), + (0xFC6, 0xFC6), + (0x1037, 0x1037), + (0x1039, 0x103A), + (0x1063, 0x1064), + (0x1069, 0x106D), + (0x1087, 0x108D), + (0x108F, 0x108F), + (0x109A, 0x109B), + (0x135D, 0x135F), + (0x1714, 0x1715), + (0x1734, 0x1734), + (0x17C9, 0x17D3), + (0x17DD, 0x17DD), + (0x1939, 0x193B), + (0x1A60, 0x1A60), + (0x1A75, 0x1A7C), + (0x1A7F, 0x1A7F), + (0x1AB0, 0x1ABE), + (0x1AC1, 0x1ACB), + (0x1ACF, 0x1AF0), + (0x1B34, 0x1B34), + (0x1B44, 0x1B44), + (0x1B6B, 0x1B73), + (0x1BAA, 0x1BAB), + (0x1BE6, 0x1BE6), + (0x1BF2, 0x1BF3), + (0x1C36, 0x1C37), + (0x1C78, 0x1C7D), + (0x1CD0, 0x1CE8), + (0x1CED, 0x1CED), + (0x1CF4, 0x1CF4), + (0x1CF7, 0x1CF9), + (0x1D2C, 0x1D6A), + (0x1D9B, 0x1DBE), + (0x1DC4, 0x1DCF), + (0x1DF5, 0x1DFF), + (0x1FBD, 0x1FBD), + (0x1FBF, 0x1FC1), + (0x1FCD, 0x1FCF), + (0x1FDD, 0x1FDF), + (0x1FED, 0x1FEF), + (0x1FFD, 0x1FFE), + (0x208F, 0x208F), + (0x2CEF, 0x2CF1), + (0x2E2F, 0x2E2F), + (0x302A, 0x302F), + (0x3099, 0x309C), + (0x30FC, 0x30FC), + (0xA66F, 0xA66F), + (0xA67C, 0xA67D), + (0xA67F, 0xA67F), + (0xA69C, 0xA69D), + (0xA6F0, 0xA6F1), + (0xA700, 0xA721), + (0xA788, 0xA78A), + (0xA7F1, 0xA7F1), + (0xA7F8, 0xA7F9), + (0xA806, 0xA806), + (0xA82C, 0xA82C), + (0xA8C4, 0xA8C4), + (0xA8E0, 0xA8F1), + (0xA92B, 0xA92E), + (0xA953, 0xA953), + (0xA9B3, 0xA9B3), + (0xA9C0, 0xA9C0), + (0xA9E5, 0xA9E5), + (0xAA7B, 0xAA7D), + (0xAABF, 0xAAC2), + (0xAAF6, 0xAAF6), + (0xAB5B, 0xAB5F), + (0xAB69, 0xAB6B), + (0xABEC, 0xABED), + (0xFB1E, 0xFB1E), + (0xFE20, 0xFE2F), + (0xFF3E, 0xFF3E), + (0xFF40, 0xFF40), + (0xFF70, 0xFF70), + (0xFF9E, 0xFF9F), + (0xFFE3, 0xFFE3), + (0x102E0, 0x102E0), + (0x10780, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BA), + (0x10A38, 0x10A3A), + (0x10A3F, 0x10A3F), + (0x10AE5, 0x10AE6), + (0x10D22, 0x10D27), + (0x10D4E, 0x10D4E), + (0x10D69, 0x10D6D), + (0x10ECB, 0x10ECF), + (0x10EF0, 0x10EF7), + (0x10EF9, 0x10EFA), + (0x10EFD, 0x10EFF), + (0x10F46, 0x10F50), + (0x10F82, 0x10F85), + (0x11046, 0x11046), + (0x11070, 0x11070), + (0x110B9, 0x110BA), + (0x11133, 0x11134), + (0x11173, 0x11173), + (0x111C0, 0x111C0), + (0x111CA, 0x111CC), + (0x11235, 0x11236), + (0x112E9, 0x112EA), + (0x1133B, 0x1133C), + (0x1134D, 0x1134D), + (0x11366, 0x1136C), + (0x11370, 0x11374), + (0x113CE, 0x113D0), + (0x113D2, 0x113D3), + (0x113E1, 0x113E2), + (0x11442, 0x11442), + (0x11446, 0x11446), + (0x114C2, 0x114C3), + (0x115BF, 0x115C0), + (0x1163F, 0x1163F), + (0x116B6, 0x116B7), + (0x1172B, 0x1172B), + (0x11839, 0x1183A), + (0x1193D, 0x1193E), + (0x11943, 0x11943), + (0x119E0, 0x119E0), + (0x11A34, 0x11A34), + (0x11A47, 0x11A47), + (0x11A99, 0x11A99), + (0x11C3F, 0x11C3F), + (0x11D42, 0x11D42), + (0x11D44, 0x11D45), + (0x11D97, 0x11D97), + (0x11DD9, 0x11DD9), + (0x11F41, 0x11F42), + (0x11F5A, 0x11F5A), + (0x13447, 0x13455), + (0x1612F, 0x1612F), + (0x16AF0, 0x16AF4), + (0x16B30, 0x16B36), + (0x16D6B, 0x16D6C), + (0x16F8F, 0x16F9F), + (0x16FF0, 0x16FF1), + (0x1AFF0, 0x1AFF3), + (0x1AFF5, 0x1AFFB), + (0x1AFFD, 0x1AFFE), + (0x1CF00, 0x1CF2D), + (0x1CF30, 0x1CF46), + (0x1D167, 0x1D169), + (0x1D16D, 0x1D172), + (0x1D17B, 0x1D182), + (0x1D185, 0x1D18B), + (0x1D1AA, 0x1D1AD), + (0x1D250, 0x1D252), + (0x1D25B, 0x1D25C), + (0x1DFCD, 0x1DFCF), + (0x1DFD1, 0x1DFFF), + (0x1E030, 0x1E06D), + (0x1E130, 0x1E136), + (0x1E2AE, 0x1E2AE), + (0x1E2EC, 0x1E2EF), + (0x1E5EE, 0x1E5EF), + (0x1E8D0, 0x1E8D6), + (0x1E944, 0x1E946), + (0x1E948, 0x1E94A), +]; + +/// The code points with the binary property Emoji, as ranges. +pub static BINARY_EMOJI: &[(u32, u32)] = &[ + (0x23, 0x23), + (0x2A, 0x2A), + (0x30, 0x39), + (0xA9, 0xA9), + (0xAE, 0xAE), + (0x203C, 0x203C), + (0x2049, 0x2049), + (0x2122, 0x2122), + (0x2139, 0x2139), + (0x2194, 0x2199), + (0x21A9, 0x21AA), + (0x231A, 0x231B), + (0x2328, 0x2328), + (0x23CF, 0x23CF), + (0x23E9, 0x23F3), + (0x23F8, 0x23FA), + (0x24C2, 0x24C2), + (0x25AA, 0x25AB), + (0x25B6, 0x25B6), + (0x25C0, 0x25C0), + (0x25FB, 0x25FE), + (0x2600, 0x2604), + (0x260E, 0x260E), + (0x2611, 0x2611), + (0x2614, 0x2615), + (0x2618, 0x2618), + (0x261D, 0x261D), + (0x2620, 0x2620), + (0x2622, 0x2623), + (0x2626, 0x2626), + (0x262A, 0x262A), + (0x262E, 0x262F), + (0x2638, 0x263A), + (0x2640, 0x2640), + (0x2642, 0x2642), + (0x2648, 0x2653), + (0x265F, 0x2660), + (0x2663, 0x2663), + (0x2665, 0x2666), + (0x2668, 0x2668), + (0x267B, 0x267B), + (0x267E, 0x267F), + (0x2692, 0x2697), + (0x2699, 0x2699), + (0x269B, 0x269C), + (0x26A0, 0x26A1), + (0x26A7, 0x26A7), + (0x26AA, 0x26AB), + (0x26B0, 0x26B1), + (0x26BD, 0x26BE), + (0x26C4, 0x26C5), + (0x26C8, 0x26C8), + (0x26CE, 0x26CF), + (0x26D1, 0x26D1), + (0x26D3, 0x26D4), + (0x26E9, 0x26EA), + (0x26F0, 0x26F5), + (0x26F7, 0x26FA), + (0x26FD, 0x26FD), + (0x2702, 0x2702), + (0x2705, 0x2705), + (0x2708, 0x270D), + (0x270F, 0x270F), + (0x2712, 0x2712), + (0x2714, 0x2714), + (0x2716, 0x2716), + (0x271D, 0x271D), + (0x2721, 0x2721), + (0x2728, 0x2728), + (0x2733, 0x2734), + (0x2744, 0x2744), + (0x2747, 0x2747), + (0x274C, 0x274C), + (0x274E, 0x274E), + (0x2753, 0x2755), + (0x2757, 0x2757), + (0x2763, 0x2764), + (0x2795, 0x2797), + (0x27A1, 0x27A1), + (0x27B0, 0x27B0), + (0x27BF, 0x27BF), + (0x2934, 0x2935), + (0x2B05, 0x2B07), + (0x2B1B, 0x2B1C), + (0x2B50, 0x2B50), + (0x2B55, 0x2B55), + (0x3030, 0x3030), + (0x303D, 0x303D), + (0x3297, 0x3297), + (0x3299, 0x3299), + (0x1F004, 0x1F004), + (0x1F0CF, 0x1F0CF), + (0x1F170, 0x1F171), + (0x1F17E, 0x1F17F), + (0x1F18E, 0x1F18E), + (0x1F191, 0x1F19A), + (0x1F1E6, 0x1F1FF), + (0x1F201, 0x1F202), + (0x1F21A, 0x1F21A), + (0x1F22F, 0x1F22F), + (0x1F232, 0x1F23A), + (0x1F250, 0x1F251), + (0x1F300, 0x1F321), + (0x1F324, 0x1F393), + (0x1F396, 0x1F397), + (0x1F399, 0x1F39B), + (0x1F39E, 0x1F3F0), + (0x1F3F3, 0x1F3F5), + (0x1F3F7, 0x1F4FD), + (0x1F4FF, 0x1F53D), + (0x1F549, 0x1F54E), + (0x1F550, 0x1F567), + (0x1F56F, 0x1F570), + (0x1F573, 0x1F57A), + (0x1F587, 0x1F587), + (0x1F58A, 0x1F58D), + (0x1F590, 0x1F590), + (0x1F595, 0x1F596), + (0x1F5A4, 0x1F5A5), + (0x1F5A8, 0x1F5A8), + (0x1F5B1, 0x1F5B2), + (0x1F5BC, 0x1F5BC), + (0x1F5C2, 0x1F5C4), + (0x1F5D1, 0x1F5D3), + (0x1F5DC, 0x1F5DE), + (0x1F5E1, 0x1F5E1), + (0x1F5E3, 0x1F5E3), + (0x1F5E8, 0x1F5E8), + (0x1F5EF, 0x1F5EF), + (0x1F5F3, 0x1F5F3), + (0x1F5FA, 0x1F64F), + (0x1F680, 0x1F6C5), + (0x1F6CB, 0x1F6D2), + (0x1F6D5, 0x1F6D9), + (0x1F6DC, 0x1F6E5), + (0x1F6E9, 0x1F6E9), + (0x1F6EB, 0x1F6EC), + (0x1F6F0, 0x1F6F0), + (0x1F6F3, 0x1F6FC), + (0x1F7E0, 0x1F7EB), + (0x1F7F0, 0x1F7F0), + (0x1F90C, 0x1F93A), + (0x1F93C, 0x1F945), + (0x1F947, 0x1F9FF), + (0x1FA70, 0x1FA7C), + (0x1FA80, 0x1FAC6), + (0x1FAC8, 0x1FAC8), + (0x1FACC, 0x1FADD), + (0x1FADF, 0x1FAEB), + (0x1FAEF, 0x1FAFA), +]; + +/// The code points with the binary property Emoji_Component, as ranges. +pub static BINARY_EMOJI_COMPONENT: &[(u32, u32)] = &[ + (0x23, 0x23), + (0x2A, 0x2A), + (0x30, 0x39), + (0x200D, 0x200D), + (0x20E3, 0x20E3), + (0xFE0F, 0xFE0F), + (0x1F1E6, 0x1F1FF), + (0x1F3FB, 0x1F3FF), + (0x1F9B0, 0x1F9B3), + (0xE0020, 0xE007F), +]; + +/// The code points with the binary property Emoji_Modifier, as ranges. +pub static BINARY_EMOJI_MODIFIER: &[(u32, u32)] = &[(0x1F3FB, 0x1F3FF)]; + +/// The code points with the binary property Emoji_Modifier_Base, as ranges. +pub static BINARY_EMOJI_MODIFIER_BASE: &[(u32, u32)] = &[ + (0x261D, 0x261D), + (0x26F9, 0x26F9), + (0x270A, 0x270D), + (0x1F385, 0x1F385), + (0x1F3C2, 0x1F3C4), + (0x1F3C7, 0x1F3C7), + (0x1F3CA, 0x1F3CC), + (0x1F442, 0x1F443), + (0x1F446, 0x1F450), + (0x1F466, 0x1F478), + (0x1F47C, 0x1F47C), + (0x1F481, 0x1F483), + (0x1F485, 0x1F487), + (0x1F48F, 0x1F48F), + (0x1F491, 0x1F491), + (0x1F4AA, 0x1F4AA), + (0x1F574, 0x1F575), + (0x1F57A, 0x1F57A), + (0x1F590, 0x1F590), + (0x1F595, 0x1F596), + (0x1F645, 0x1F647), + (0x1F64B, 0x1F64F), + (0x1F6A3, 0x1F6A3), + (0x1F6B4, 0x1F6B6), + (0x1F6C0, 0x1F6C0), + (0x1F6CC, 0x1F6CC), + (0x1F90C, 0x1F90C), + (0x1F90F, 0x1F90F), + (0x1F918, 0x1F91F), + (0x1F926, 0x1F926), + (0x1F930, 0x1F939), + (0x1F93C, 0x1F93E), + (0x1F977, 0x1F977), + (0x1F9B5, 0x1F9B6), + (0x1F9B8, 0x1F9B9), + (0x1F9BB, 0x1F9BB), + (0x1F9CD, 0x1F9CF), + (0x1F9D1, 0x1F9DD), + (0x1FAC3, 0x1FAC5), + (0x1FAF0, 0x1FAFA), +]; + +/// The code points with the binary property Emoji_Presentation, as ranges. +pub static BINARY_EMOJI_PRESENTATION: &[(u32, u32)] = &[ + (0x231A, 0x231B), + (0x23E9, 0x23EC), + (0x23F0, 0x23F0), + (0x23F3, 0x23F3), + (0x25FD, 0x25FE), + (0x2614, 0x2615), + (0x2648, 0x2653), + (0x267F, 0x267F), + (0x2693, 0x2693), + (0x26A1, 0x26A1), + (0x26AA, 0x26AB), + (0x26BD, 0x26BE), + (0x26C4, 0x26C5), + (0x26CE, 0x26CE), + (0x26D4, 0x26D4), + (0x26EA, 0x26EA), + (0x26F2, 0x26F3), + (0x26F5, 0x26F5), + (0x26FA, 0x26FA), + (0x26FD, 0x26FD), + (0x2705, 0x2705), + (0x270A, 0x270B), + (0x2728, 0x2728), + (0x274C, 0x274C), + (0x274E, 0x274E), + (0x2753, 0x2755), + (0x2757, 0x2757), + (0x2795, 0x2797), + (0x27B0, 0x27B0), + (0x27BF, 0x27BF), + (0x2B1B, 0x2B1C), + (0x2B50, 0x2B50), + (0x2B55, 0x2B55), + (0x1F004, 0x1F004), + (0x1F0CF, 0x1F0CF), + (0x1F18E, 0x1F18E), + (0x1F191, 0x1F19A), + (0x1F1E6, 0x1F1FF), + (0x1F201, 0x1F201), + (0x1F21A, 0x1F21A), + (0x1F22F, 0x1F22F), + (0x1F232, 0x1F236), + (0x1F238, 0x1F23A), + (0x1F250, 0x1F251), + (0x1F300, 0x1F320), + (0x1F32D, 0x1F335), + (0x1F337, 0x1F37C), + (0x1F37E, 0x1F393), + (0x1F3A0, 0x1F3CA), + (0x1F3CF, 0x1F3D3), + (0x1F3E0, 0x1F3F0), + (0x1F3F4, 0x1F3F4), + (0x1F3F8, 0x1F43E), + (0x1F440, 0x1F440), + (0x1F442, 0x1F4FC), + (0x1F4FF, 0x1F53D), + (0x1F54B, 0x1F54E), + (0x1F550, 0x1F567), + (0x1F57A, 0x1F57A), + (0x1F595, 0x1F596), + (0x1F5A4, 0x1F5A4), + (0x1F5FB, 0x1F64F), + (0x1F680, 0x1F6C5), + (0x1F6CC, 0x1F6CC), + (0x1F6D0, 0x1F6D2), + (0x1F6D5, 0x1F6D9), + (0x1F6DC, 0x1F6DF), + (0x1F6EB, 0x1F6EC), + (0x1F6F4, 0x1F6FC), + (0x1F7E0, 0x1F7EB), + (0x1F7F0, 0x1F7F0), + (0x1F90C, 0x1F93A), + (0x1F93C, 0x1F945), + (0x1F947, 0x1F9FF), + (0x1FA70, 0x1FA7C), + (0x1FA80, 0x1FAC6), + (0x1FAC8, 0x1FAC8), + (0x1FACC, 0x1FADD), + (0x1FADF, 0x1FAEB), + (0x1FAEF, 0x1FAFA), +]; + +/// The code points with the binary property Extended_Pictographic, as ranges. +pub static BINARY_EXTENDED_PICTOGRAPHIC: &[(u32, u32)] = &[ + (0xA9, 0xA9), + (0xAE, 0xAE), + (0x203C, 0x203C), + (0x2049, 0x2049), + (0x2122, 0x2122), + (0x2139, 0x2139), + (0x2194, 0x2199), + (0x21A9, 0x21AA), + (0x231A, 0x231B), + (0x2328, 0x2328), + (0x23CF, 0x23CF), + (0x23E9, 0x23F3), + (0x23F8, 0x23FA), + (0x24C2, 0x24C2), + (0x25AA, 0x25AB), + (0x25B6, 0x25B6), + (0x25C0, 0x25C0), + (0x25FB, 0x25FE), + (0x2600, 0x2604), + (0x260E, 0x260E), + (0x2611, 0x2611), + (0x2614, 0x2615), + (0x2618, 0x2618), + (0x261D, 0x261D), + (0x2620, 0x2620), + (0x2622, 0x2623), + (0x2626, 0x2626), + (0x262A, 0x262A), + (0x262E, 0x262F), + (0x2638, 0x263A), + (0x2640, 0x2640), + (0x2642, 0x2642), + (0x2648, 0x2653), + (0x265F, 0x2660), + (0x2663, 0x2663), + (0x2665, 0x2666), + (0x2668, 0x2668), + (0x267B, 0x267B), + (0x267E, 0x267F), + (0x2692, 0x2697), + (0x2699, 0x2699), + (0x269B, 0x269C), + (0x26A0, 0x26A1), + (0x26A7, 0x26A7), + (0x26AA, 0x26AB), + (0x26B0, 0x26B1), + (0x26BD, 0x26BE), + (0x26C4, 0x26C5), + (0x26C8, 0x26C8), + (0x26CE, 0x26CF), + (0x26D1, 0x26D1), + (0x26D3, 0x26D4), + (0x26E9, 0x26EA), + (0x26F0, 0x26F5), + (0x26F7, 0x26FA), + (0x26FD, 0x26FD), + (0x2702, 0x2702), + (0x2705, 0x2705), + (0x2708, 0x270D), + (0x270F, 0x270F), + (0x2712, 0x2712), + (0x2714, 0x2714), + (0x2716, 0x2716), + (0x271D, 0x271D), + (0x2721, 0x2721), + (0x2728, 0x2728), + (0x2733, 0x2734), + (0x2744, 0x2744), + (0x2747, 0x2747), + (0x274C, 0x274C), + (0x274E, 0x274E), + (0x2753, 0x2755), + (0x2757, 0x2757), + (0x2763, 0x2764), + (0x2795, 0x2797), + (0x27A1, 0x27A1), + (0x27B0, 0x27B0), + (0x27BF, 0x27BF), + (0x2934, 0x2935), + (0x2B05, 0x2B07), + (0x2B1B, 0x2B1C), + (0x2B50, 0x2B50), + (0x2B55, 0x2B55), + (0x3030, 0x3030), + (0x303D, 0x303D), + (0x3297, 0x3297), + (0x3299, 0x3299), + (0x1F004, 0x1F004), + (0x1F02C, 0x1F02F), + (0x1F094, 0x1F09F), + (0x1F0AF, 0x1F0B0), + (0x1F0C0, 0x1F0C0), + (0x1F0CF, 0x1F0D0), + (0x1F0F6, 0x1F0FF), + (0x1F170, 0x1F171), + (0x1F17E, 0x1F17F), + (0x1F18E, 0x1F18E), + (0x1F191, 0x1F19A), + (0x1F1AF, 0x1F1E5), + (0x1F201, 0x1F20F), + (0x1F21A, 0x1F21A), + (0x1F22F, 0x1F22F), + (0x1F232, 0x1F23A), + (0x1F23C, 0x1F23F), + (0x1F249, 0x1F25F), + (0x1F266, 0x1F321), + (0x1F324, 0x1F393), + (0x1F396, 0x1F397), + (0x1F399, 0x1F39B), + (0x1F39E, 0x1F3F0), + (0x1F3F3, 0x1F3F5), + (0x1F3F7, 0x1F3FA), + (0x1F400, 0x1F4FD), + (0x1F4FF, 0x1F53D), + (0x1F549, 0x1F54E), + (0x1F550, 0x1F567), + (0x1F56F, 0x1F570), + (0x1F573, 0x1F57A), + (0x1F587, 0x1F587), + (0x1F58A, 0x1F58D), + (0x1F590, 0x1F590), + (0x1F595, 0x1F596), + (0x1F5A4, 0x1F5A5), + (0x1F5A8, 0x1F5A8), + (0x1F5B1, 0x1F5B2), + (0x1F5BC, 0x1F5BC), + (0x1F5C2, 0x1F5C4), + (0x1F5D1, 0x1F5D3), + (0x1F5DC, 0x1F5DE), + (0x1F5E1, 0x1F5E1), + (0x1F5E3, 0x1F5E3), + (0x1F5E8, 0x1F5E8), + (0x1F5EF, 0x1F5EF), + (0x1F5F3, 0x1F5F3), + (0x1F5FA, 0x1F64F), + (0x1F680, 0x1F6C5), + (0x1F6CB, 0x1F6D2), + (0x1F6D5, 0x1F6E5), + (0x1F6E9, 0x1F6E9), + (0x1F6EB, 0x1F6F0), + (0x1F6F3, 0x1F6FF), + (0x1F7DC, 0x1F7F0), + (0x1F80C, 0x1F80F), + (0x1F848, 0x1F84F), + (0x1F85A, 0x1F85F), + (0x1F888, 0x1F88F), + (0x1F8AE, 0x1F8AF), + (0x1F8BC, 0x1F8BF), + (0x1F8C2, 0x1F8CF), + (0x1F8D9, 0x1F8FF), + (0x1F90C, 0x1F93A), + (0x1F93C, 0x1F945), + (0x1F947, 0x1F9FF), + (0x1FA58, 0x1FA5F), + (0x1FA6E, 0x1FAFF), + (0x1FC00, 0x1FFFD), +]; + +/// The code points with the binary property Extender, as ranges. +pub static BINARY_EXTENDER: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x2D0, 0x2D1), + (0x640, 0x640), + (0x7FA, 0x7FA), + (0xA71, 0xA71), + (0xAFB, 0xAFB), + (0xB54, 0xB55), + (0xE46, 0xE46), + (0xEC6, 0xEC6), + (0x180A, 0x180A), + (0x1843, 0x1843), + (0x1AA7, 0x1AA7), + (0x1C36, 0x1C36), + (0x1C7B, 0x1C7B), + (0x3005, 0x3005), + (0x3031, 0x3035), + (0x309D, 0x309E), + (0x30FC, 0x30FE), + (0xA015, 0xA015), + (0xA60C, 0xA60C), + (0xA9CF, 0xA9CF), + (0xA9E6, 0xA9E6), + (0xAA70, 0xAA70), + (0xAADD, 0xAADD), + (0xAAF3, 0xAAF4), + (0xFF70, 0xFF70), + (0x10781, 0x10782), + (0x10D4E, 0x10D4E), + (0x10D6A, 0x10D6A), + (0x10D6F, 0x10D6F), + (0x11237, 0x11237), + (0x1135D, 0x1135D), + (0x113D2, 0x113D3), + (0x115C6, 0x115C8), + (0x11A98, 0x11A98), + (0x11DD9, 0x11DD9), + (0x16B42, 0x16B43), + (0x16FE0, 0x16FE1), + (0x16FE3, 0x16FE3), + (0x16FF2, 0x16FF3), + (0x1E13C, 0x1E13D), + (0x1E5EF, 0x1E5EF), + (0x1E944, 0x1E946), +]; + +/// The code points with the binary property Grapheme_Base, as ranges. +pub static BINARY_GRAPHEME_BASE: &[(u32, u32)] = &[ + (0x20, 0x7E), + (0xA0, 0xAC), + (0xAE, 0x2FF), + (0x370, 0x377), + (0x37A, 0x37F), + (0x384, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x3A1), + (0x3A3, 0x482), + (0x48A, 0x52F), + (0x531, 0x556), + (0x558, 0x58F), + (0x5BE, 0x5BE), + (0x5C0, 0x5C0), + (0x5C3, 0x5C3), + (0x5C6, 0x5C6), + (0x5D0, 0x5EA), + (0x5EF, 0x5F4), + (0x606, 0x60F), + (0x61B, 0x61B), + (0x61D, 0x64A), + (0x660, 0x66F), + (0x671, 0x6D5), + (0x6DE, 0x6DE), + (0x6E5, 0x6E6), + (0x6E9, 0x6E9), + (0x6EE, 0x70D), + (0x710, 0x710), + (0x712, 0x72F), + (0x74D, 0x7A5), + (0x7B1, 0x7B1), + (0x7C0, 0x7EA), + (0x7F4, 0x7FA), + (0x7FE, 0x815), + (0x81A, 0x81A), + (0x824, 0x824), + (0x828, 0x828), + (0x830, 0x83E), + (0x840, 0x858), + (0x85E, 0x85E), + (0x860, 0x86A), + (0x870, 0x88F), + (0x8A0, 0x8C9), + (0x903, 0x939), + (0x93B, 0x93B), + (0x93D, 0x940), + (0x949, 0x94C), + (0x94E, 0x950), + (0x958, 0x961), + (0x964, 0x980), + (0x982, 0x983), + (0x985, 0x98C), + (0x98F, 0x990), + (0x993, 0x9A8), + (0x9AA, 0x9B0), + (0x9B2, 0x9B2), + (0x9B6, 0x9B9), + (0x9BD, 0x9BD), + (0x9BF, 0x9C0), + (0x9C7, 0x9C8), + (0x9CB, 0x9CC), + (0x9CE, 0x9CE), + (0x9DC, 0x9DD), + (0x9DF, 0x9E1), + (0x9E6, 0x9FD), + (0xA03, 0xA03), + (0xA05, 0xA0A), + (0xA0F, 0xA10), + (0xA13, 0xA28), + (0xA2A, 0xA30), + (0xA32, 0xA33), + (0xA35, 0xA36), + (0xA38, 0xA39), + (0xA3E, 0xA40), + (0xA59, 0xA5C), + (0xA5E, 0xA5E), + (0xA66, 0xA6F), + (0xA72, 0xA74), + (0xA76, 0xA76), + (0xA83, 0xA83), + (0xA85, 0xA8D), + (0xA8F, 0xA91), + (0xA93, 0xAA8), + (0xAAA, 0xAB0), + (0xAB2, 0xAB3), + (0xAB5, 0xAB9), + (0xABD, 0xAC0), + (0xAC9, 0xAC9), + (0xACB, 0xACC), + (0xAD0, 0xAD0), + (0xAE0, 0xAE1), + (0xAE6, 0xAF1), + (0xAF9, 0xAF9), + (0xB02, 0xB03), + (0xB05, 0xB0C), + (0xB0F, 0xB10), + (0xB13, 0xB28), + (0xB2A, 0xB30), + (0xB32, 0xB33), + (0xB35, 0xB39), + (0xB3D, 0xB3D), + (0xB40, 0xB40), + (0xB47, 0xB48), + (0xB4B, 0xB4C), + (0xB5C, 0xB5D), + (0xB5F, 0xB61), + (0xB66, 0xB77), + (0xB83, 0xB83), + (0xB85, 0xB8A), + (0xB8E, 0xB90), + (0xB92, 0xB95), + (0xB99, 0xB9A), + (0xB9C, 0xB9C), + (0xB9E, 0xB9F), + (0xBA3, 0xBA4), + (0xBA8, 0xBAA), + (0xBAE, 0xBB9), + (0xBBF, 0xBBF), + (0xBC1, 0xBC2), + (0xBC6, 0xBC8), + (0xBCA, 0xBCC), + (0xBD0, 0xBD0), + (0xBE6, 0xBFA), + (0xC01, 0xC03), + (0xC05, 0xC0C), + (0xC0E, 0xC10), + (0xC12, 0xC28), + (0xC2A, 0xC39), + (0xC3D, 0xC3D), + (0xC41, 0xC44), + (0xC58, 0xC5A), + (0xC5C, 0xC5D), + (0xC60, 0xC61), + (0xC66, 0xC6F), + (0xC77, 0xC80), + (0xC82, 0xC8C), + (0xC8E, 0xC90), + (0xC92, 0xCA8), + (0xCAA, 0xCB3), + (0xCB5, 0xCB9), + (0xCBD, 0xCBE), + (0xCC1, 0xCC1), + (0xCC3, 0xCC4), + (0xCDC, 0xCDE), + (0xCE0, 0xCE1), + (0xCE6, 0xCEF), + (0xCF1, 0xCF3), + (0xD02, 0xD0C), + (0xD0E, 0xD10), + (0xD12, 0xD3A), + (0xD3D, 0xD3D), + (0xD3F, 0xD40), + (0xD46, 0xD48), + (0xD4A, 0xD4C), + (0xD4E, 0xD4F), + (0xD54, 0xD56), + (0xD58, 0xD61), + (0xD66, 0xD7F), + (0xD82, 0xD83), + (0xD85, 0xD96), + (0xD9A, 0xDB1), + (0xDB3, 0xDBB), + (0xDBD, 0xDBD), + (0xDC0, 0xDC6), + (0xDD0, 0xDD1), + (0xDD8, 0xDDE), + (0xDE6, 0xDEF), + (0xDF2, 0xDF4), + (0xE01, 0xE30), + (0xE32, 0xE33), + (0xE3F, 0xE46), + (0xE4F, 0xE5B), + (0xE81, 0xE82), + (0xE84, 0xE84), + (0xE86, 0xE8A), + (0xE8C, 0xEA3), + (0xEA5, 0xEA5), + (0xEA7, 0xEB0), + (0xEB2, 0xEB3), + (0xEBD, 0xEBD), + (0xEC0, 0xEC4), + (0xEC6, 0xEC6), + (0xED0, 0xED9), + (0xEDC, 0xEDF), + (0xF00, 0xF17), + (0xF1A, 0xF34), + (0xF36, 0xF36), + (0xF38, 0xF38), + (0xF3A, 0xF47), + (0xF49, 0xF6C), + (0xF7F, 0xF7F), + (0xF85, 0xF85), + (0xF88, 0xF8C), + (0xFBE, 0xFC5), + (0xFC7, 0xFCC), + (0xFCE, 0xFDA), + (0x1000, 0x102C), + (0x1031, 0x1031), + (0x1038, 0x1038), + (0x103B, 0x103C), + (0x103F, 0x1057), + (0x105A, 0x105D), + (0x1061, 0x1070), + (0x1075, 0x1081), + (0x1083, 0x1084), + (0x1087, 0x108C), + (0x108E, 0x109C), + (0x109E, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10D0, 0x1248), + (0x124A, 0x124D), + (0x1250, 0x1256), + (0x1258, 0x1258), + (0x125A, 0x125D), + (0x1260, 0x1288), + (0x128A, 0x128D), + (0x1290, 0x12B0), + (0x12B2, 0x12B5), + (0x12B8, 0x12BE), + (0x12C0, 0x12C0), + (0x12C2, 0x12C5), + (0x12C8, 0x12D6), + (0x12D8, 0x1310), + (0x1312, 0x1315), + (0x1318, 0x135A), + (0x1360, 0x137C), + (0x1380, 0x1399), + (0x13A0, 0x13F5), + (0x13F8, 0x13FD), + (0x1400, 0x169C), + (0x16A0, 0x16F8), + (0x1700, 0x1711), + (0x171F, 0x1731), + (0x1735, 0x1736), + (0x1740, 0x1751), + (0x1760, 0x176C), + (0x176E, 0x1770), + (0x1780, 0x17B3), + (0x17B6, 0x17B6), + (0x17BE, 0x17C5), + (0x17C7, 0x17C8), + (0x17D4, 0x17DC), + (0x17E0, 0x17E9), + (0x17F0, 0x17F9), + (0x1800, 0x180A), + (0x1810, 0x1819), + (0x1820, 0x1878), + (0x1880, 0x1884), + (0x1887, 0x18A8), + (0x18AA, 0x18AA), + (0x18B0, 0x18F5), + (0x1900, 0x191E), + (0x1923, 0x1926), + (0x1929, 0x192B), + (0x1930, 0x1931), + (0x1933, 0x1938), + (0x1940, 0x1940), + (0x1944, 0x196D), + (0x1970, 0x1974), + (0x1980, 0x19AB), + (0x19B0, 0x19C9), + (0x19D0, 0x19DA), + (0x19DE, 0x1A16), + (0x1A19, 0x1A1A), + (0x1A1E, 0x1A55), + (0x1A57, 0x1A57), + (0x1A61, 0x1A61), + (0x1A63, 0x1A64), + (0x1A6D, 0x1A72), + (0x1A80, 0x1A89), + (0x1A90, 0x1A99), + (0x1AA0, 0x1AAD), + (0x1B04, 0x1B33), + (0x1B3E, 0x1B41), + (0x1B45, 0x1B4C), + (0x1B4E, 0x1B6A), + (0x1B74, 0x1B7F), + (0x1B82, 0x1BA1), + (0x1BA6, 0x1BA7), + (0x1BAE, 0x1BE5), + (0x1BE7, 0x1BE7), + (0x1BEA, 0x1BEC), + (0x1BEE, 0x1BEE), + (0x1BFC, 0x1C2B), + (0x1C34, 0x1C35), + (0x1C3B, 0x1C49), + (0x1C4D, 0x1C8A), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CC7), + (0x1CD3, 0x1CD3), + (0x1CE1, 0x1CE1), + (0x1CE9, 0x1CEC), + (0x1CEE, 0x1CF3), + (0x1CF5, 0x1CF7), + (0x1CFA, 0x1CFA), + (0x1D00, 0x1DBF), + (0x1E00, 0x1F15), + (0x1F18, 0x1F1D), + (0x1F20, 0x1F45), + (0x1F48, 0x1F4D), + (0x1F50, 0x1F57), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FC4), + (0x1FC6, 0x1FD3), + (0x1FD6, 0x1FDB), + (0x1FDD, 0x1FEF), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FFE), + (0x2000, 0x200A), + (0x2010, 0x2027), + (0x202F, 0x205F), + (0x2070, 0x2071), + (0x2074, 0x20C4), + (0x2100, 0x218B), + (0x2190, 0x2429), + (0x2440, 0x244A), + (0x2460, 0x2B73), + (0x2B76, 0x2CEE), + (0x2CF2, 0x2CF3), + (0x2CF9, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0x2D30, 0x2D67), + (0x2D6F, 0x2D70), + (0x2D80, 0x2D96), + (0x2DA0, 0x2DA6), + (0x2DA8, 0x2DAE), + (0x2DB0, 0x2DB6), + (0x2DB8, 0x2DBE), + (0x2DC0, 0x2DC6), + (0x2DC8, 0x2DCE), + (0x2DD0, 0x2DD6), + (0x2DD8, 0x2DDE), + (0x2E00, 0x2E5D), + (0x2E60, 0x2E63), + (0x2E80, 0x2E99), + (0x2E9B, 0x2EF3), + (0x2F00, 0x2FD5), + (0x2FF0, 0x3029), + (0x3030, 0x303F), + (0x3041, 0x3096), + (0x309B, 0x30FF), + (0x3105, 0x312F), + (0x3131, 0x318E), + (0x3190, 0x31E5), + (0x31EF, 0x321E), + (0x3220, 0xA48C), + (0xA490, 0xA4C6), + (0xA4D0, 0xA62B), + (0xA640, 0xA66E), + (0xA673, 0xA673), + (0xA67E, 0xA69D), + (0xA6A0, 0xA6EF), + (0xA6F2, 0xA6F7), + (0xA700, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F1, 0xA801), + (0xA803, 0xA805), + (0xA807, 0xA80A), + (0xA80C, 0xA824), + (0xA827, 0xA82B), + (0xA830, 0xA839), + (0xA840, 0xA877), + (0xA880, 0xA8C3), + (0xA8CE, 0xA8D9), + (0xA8F2, 0xA8FE), + (0xA900, 0xA925), + (0xA92E, 0xA946), + (0xA952, 0xA952), + (0xA95F, 0xA97C), + (0xA983, 0xA9B2), + (0xA9B4, 0xA9B5), + (0xA9BA, 0xA9BB), + (0xA9BE, 0xA9BF), + (0xA9C1, 0xA9CD), + (0xA9CF, 0xA9D9), + (0xA9DE, 0xA9E4), + (0xA9E6, 0xA9FE), + (0xAA00, 0xAA28), + (0xAA2F, 0xAA30), + (0xAA33, 0xAA34), + (0xAA40, 0xAA42), + (0xAA44, 0xAA4B), + (0xAA4D, 0xAA4D), + (0xAA50, 0xAA59), + (0xAA5C, 0xAA7B), + (0xAA7D, 0xAAAF), + (0xAAB1, 0xAAB1), + (0xAAB5, 0xAAB6), + (0xAAB9, 0xAABD), + (0xAAC0, 0xAAC0), + (0xAAC2, 0xAAC2), + (0xAADB, 0xAAEB), + (0xAAEE, 0xAAF5), + (0xAB01, 0xAB06), + (0xAB09, 0xAB0E), + (0xAB11, 0xAB16), + (0xAB20, 0xAB26), + (0xAB28, 0xAB2E), + (0xAB30, 0xAB6D), + (0xAB70, 0xABE4), + (0xABE6, 0xABE7), + (0xABE9, 0xABEC), + (0xABF0, 0xABF9), + (0xAC00, 0xD7A3), + (0xD7B0, 0xD7C6), + (0xD7CB, 0xD7FB), + (0xF900, 0xFA6D), + (0xFA70, 0xFAD9), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFB1D, 0xFB1D), + (0xFB1F, 0xFB36), + (0xFB38, 0xFB3C), + (0xFB3E, 0xFB3E), + (0xFB40, 0xFB41), + (0xFB43, 0xFB44), + (0xFB46, 0xFDCF), + (0xFDF0, 0xFDFF), + (0xFE10, 0xFE19), + (0xFE30, 0xFE52), + (0xFE54, 0xFE66), + (0xFE68, 0xFE6B), + (0xFE70, 0xFE74), + (0xFE76, 0xFEFC), + (0xFF01, 0xFF9D), + (0xFFA0, 0xFFBE), + (0xFFC2, 0xFFC7), + (0xFFCA, 0xFFCF), + (0xFFD2, 0xFFD7), + (0xFFDA, 0xFFDC), + (0xFFE0, 0xFFE6), + (0xFFE8, 0xFFEE), + (0xFFFC, 0xFFFD), + (0x10000, 0x1000B), + (0x1000D, 0x10026), + (0x10028, 0x1003A), + (0x1003C, 0x1003D), + (0x1003F, 0x1004D), + (0x10050, 0x1005D), + (0x10080, 0x100FA), + (0x10100, 0x10102), + (0x10107, 0x10133), + (0x10137, 0x1018E), + (0x10190, 0x1019C), + (0x101A0, 0x101A0), + (0x101D0, 0x101FC), + (0x10280, 0x1029C), + (0x102A0, 0x102D0), + (0x102E1, 0x102FB), + (0x10300, 0x10323), + (0x1032D, 0x1034A), + (0x10350, 0x10375), + (0x10380, 0x1039D), + (0x1039F, 0x103C3), + (0x103C8, 0x103D5), + (0x10400, 0x1049D), + (0x104A0, 0x104A9), + (0x104B0, 0x104D3), + (0x104D8, 0x104FB), + (0x10500, 0x10527), + (0x10530, 0x10563), + (0x1056F, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x105C0, 0x105F3), + (0x10600, 0x10736), + (0x10740, 0x10755), + (0x10760, 0x10767), + (0x10780, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x10800, 0x10805), + (0x10808, 0x10808), + (0x1080A, 0x10835), + (0x10837, 0x10838), + (0x1083C, 0x1083C), + (0x1083F, 0x10855), + (0x10857, 0x1089E), + (0x108A7, 0x108AF), + (0x108E0, 0x108F2), + (0x108F4, 0x108F5), + (0x108FB, 0x1091B), + (0x1091F, 0x10939), + (0x1093F, 0x10959), + (0x10980, 0x109B7), + (0x109BC, 0x109CF), + (0x109D2, 0x10A00), + (0x10A10, 0x10A13), + (0x10A15, 0x10A17), + (0x10A19, 0x10A35), + (0x10A40, 0x10A48), + (0x10A50, 0x10A58), + (0x10A60, 0x10A9F), + (0x10AC0, 0x10AE4), + (0x10AEB, 0x10AF6), + (0x10B00, 0x10B35), + (0x10B39, 0x10B55), + (0x10B58, 0x10B72), + (0x10B78, 0x10B91), + (0x10B99, 0x10B9C), + (0x10BA9, 0x10BAF), + (0x10C00, 0x10C48), + (0x10C80, 0x10CB2), + (0x10CC0, 0x10CF2), + (0x10CFA, 0x10D23), + (0x10D30, 0x10D39), + (0x10D40, 0x10D65), + (0x10D6E, 0x10D85), + (0x10D8E, 0x10D8F), + (0x10E60, 0x10E7E), + (0x10E80, 0x10EA9), + (0x10EAD, 0x10EAD), + (0x10EB0, 0x10EB1), + (0x10EC2, 0x10EC7), + (0x10EC9, 0x10ECA), + (0x10ED0, 0x10EEE), + (0x10F00, 0x10F27), + (0x10F30, 0x10F45), + (0x10F51, 0x10F59), + (0x10F70, 0x10F81), + (0x10F86, 0x10F89), + (0x10FB0, 0x10FCB), + (0x10FE0, 0x10FF6), + (0x11000, 0x11000), + (0x11002, 0x11037), + (0x11047, 0x1104D), + (0x11052, 0x1106F), + (0x11071, 0x11072), + (0x11075, 0x11075), + (0x11082, 0x110B2), + (0x110B7, 0x110B8), + (0x110BB, 0x110BC), + (0x110BE, 0x110C1), + (0x110D0, 0x110E8), + (0x110F0, 0x110F9), + (0x11103, 0x11126), + (0x1112C, 0x1112C), + (0x11136, 0x11147), + (0x11150, 0x11172), + (0x11174, 0x11176), + (0x11182, 0x111B5), + (0x111BF, 0x111BF), + (0x111C1, 0x111C8), + (0x111CD, 0x111CE), + (0x111D0, 0x111DF), + (0x111E1, 0x111F4), + (0x11200, 0x11211), + (0x11213, 0x1122E), + (0x11232, 0x11233), + (0x11238, 0x1123D), + (0x1123F, 0x11240), + (0x11280, 0x11286), + (0x11288, 0x11288), + (0x1128A, 0x1128D), + (0x1128F, 0x1129D), + (0x1129F, 0x112A9), + (0x112B0, 0x112DE), + (0x112E0, 0x112E2), + (0x112F0, 0x112F9), + (0x11302, 0x11303), + (0x11305, 0x1130C), + (0x1130F, 0x11310), + (0x11313, 0x11328), + (0x1132A, 0x11330), + (0x11332, 0x11333), + (0x11335, 0x11339), + (0x1133D, 0x1133D), + (0x1133F, 0x1133F), + (0x11341, 0x11344), + (0x11347, 0x11348), + (0x1134B, 0x1134C), + (0x11350, 0x11350), + (0x1135D, 0x11363), + (0x11380, 0x11389), + (0x1138B, 0x1138B), + (0x1138E, 0x1138E), + (0x11390, 0x113B5), + (0x113B7, 0x113B7), + (0x113B9, 0x113BA), + (0x113CA, 0x113CA), + (0x113CC, 0x113CD), + (0x113D1, 0x113D1), + (0x113D3, 0x113D5), + (0x113D7, 0x113D8), + (0x11400, 0x11437), + (0x11440, 0x11441), + (0x11445, 0x11445), + (0x11447, 0x1145B), + (0x1145D, 0x1145D), + (0x1145F, 0x11461), + (0x11480, 0x114AF), + (0x114B1, 0x114B2), + (0x114B9, 0x114B9), + (0x114BB, 0x114BC), + (0x114BE, 0x114BE), + (0x114C1, 0x114C1), + (0x114C4, 0x114C7), + (0x114D0, 0x114D9), + (0x11580, 0x115AE), + (0x115B0, 0x115B1), + (0x115B8, 0x115BB), + (0x115BE, 0x115BE), + (0x115C1, 0x115DB), + (0x11600, 0x11632), + (0x1163B, 0x1163C), + (0x1163E, 0x1163E), + (0x11641, 0x11644), + (0x11650, 0x11659), + (0x11660, 0x1166C), + (0x11680, 0x116AA), + (0x116AC, 0x116AC), + (0x116AE, 0x116AF), + (0x116B8, 0x116B9), + (0x116C0, 0x116C9), + (0x116D0, 0x116E3), + (0x11700, 0x1171A), + (0x1171E, 0x1171E), + (0x11720, 0x11721), + (0x11726, 0x11726), + (0x11730, 0x11746), + (0x11800, 0x1182E), + (0x11838, 0x11838), + (0x1183B, 0x1183B), + (0x118A0, 0x118F2), + (0x118FF, 0x11906), + (0x11909, 0x11909), + (0x1190C, 0x11913), + (0x11915, 0x11916), + (0x11918, 0x1192F), + (0x11931, 0x11935), + (0x11937, 0x11938), + (0x1193F, 0x11942), + (0x11944, 0x11946), + (0x11950, 0x11959), + (0x119A0, 0x119A7), + (0x119AA, 0x119D3), + (0x119DC, 0x119DF), + (0x119E1, 0x119E4), + (0x11A00, 0x11A00), + (0x11A0B, 0x11A32), + (0x11A39, 0x11A3A), + (0x11A3F, 0x11A46), + (0x11A50, 0x11A50), + (0x11A57, 0x11A58), + (0x11A5C, 0x11A89), + (0x11A97, 0x11A97), + (0x11A9A, 0x11AA2), + (0x11AB0, 0x11AF8), + (0x11B00, 0x11B0A), + (0x11B61, 0x11B61), + (0x11B65, 0x11B65), + (0x11B67, 0x11B67), + (0x11BC0, 0x11BE1), + (0x11BF0, 0x11BF9), + (0x11C00, 0x11C08), + (0x11C0A, 0x11C2F), + (0x11C3E, 0x11C3E), + (0x11C40, 0x11C45), + (0x11C50, 0x11C6C), + (0x11C70, 0x11C8F), + (0x11CA9, 0x11CA9), + (0x11CB1, 0x11CB1), + (0x11CB4, 0x11CB4), + (0x11D00, 0x11D06), + (0x11D08, 0x11D09), + (0x11D0B, 0x11D30), + (0x11D46, 0x11D46), + (0x11D50, 0x11D59), + (0x11D60, 0x11D65), + (0x11D67, 0x11D68), + (0x11D6A, 0x11D8E), + (0x11D93, 0x11D94), + (0x11D96, 0x11D96), + (0x11D98, 0x11D98), + (0x11DA0, 0x11DA9), + (0x11DB0, 0x11DDB), + (0x11DE0, 0x11DE9), + (0x11DF1, 0x11DF1), + (0x11EE0, 0x11EF2), + (0x11EF5, 0x11EF8), + (0x11F02, 0x11F10), + (0x11F12, 0x11F35), + (0x11F3E, 0x11F3F), + (0x11F43, 0x11F59), + (0x11FB0, 0x11FB0), + (0x11FC0, 0x11FF1), + (0x11FFF, 0x12399), + (0x12400, 0x12543), + (0x12550, 0x12686), + (0x12F90, 0x12FF2), + (0x13000, 0x1342F), + (0x13441, 0x13446), + (0x13460, 0x143FA), + (0x14400, 0x14646), + (0x16100, 0x1611D), + (0x1612A, 0x1612C), + (0x16130, 0x16139), + (0x16800, 0x16A38), + (0x16A40, 0x16A5E), + (0x16A60, 0x16A69), + (0x16A6E, 0x16ABE), + (0x16AC0, 0x16AC9), + (0x16AD0, 0x16AED), + (0x16AF5, 0x16AF5), + (0x16B00, 0x16B2F), + (0x16B37, 0x16B45), + (0x16B50, 0x16B59), + (0x16B5B, 0x16B61), + (0x16B63, 0x16B77), + (0x16B7D, 0x16B8F), + (0x16D40, 0x16D79), + (0x16E40, 0x16E9A), + (0x16EA0, 0x16EB8), + (0x16EBB, 0x16ED3), + (0x16F00, 0x16F4A), + (0x16F50, 0x16F87), + (0x16F93, 0x16F9F), + (0x16FE0, 0x16FE3), + (0x16FF2, 0x16FF6), + (0x17000, 0x18CDA), + (0x18CFF, 0x18D20), + (0x18D80, 0x18DF2), + (0x18E00, 0x19191), + (0x191A0, 0x191D2), + (0x1AFF0, 0x1AFF3), + (0x1AFF5, 0x1AFFB), + (0x1AFFD, 0x1AFFE), + (0x1B000, 0x1B128), + (0x1B132, 0x1B132), + (0x1B150, 0x1B152), + (0x1B155, 0x1B155), + (0x1B164, 0x1B168), + (0x1B170, 0x1B2FB), + (0x1BC00, 0x1BC6A), + (0x1BC70, 0x1BC7C), + (0x1BC80, 0x1BC88), + (0x1BC90, 0x1BC99), + (0x1BC9C, 0x1BC9C), + (0x1BC9F, 0x1BC9F), + (0x1CC00, 0x1CCFC), + (0x1CD00, 0x1CEB3), + (0x1CEBA, 0x1CED0), + (0x1CED2, 0x1CED4), + (0x1CEDD, 0x1CEFD), + (0x1CF50, 0x1CFC3), + (0x1D000, 0x1D0F5), + (0x1D100, 0x1D126), + (0x1D129, 0x1D164), + (0x1D16A, 0x1D16C), + (0x1D183, 0x1D184), + (0x1D18C, 0x1D1A9), + (0x1D1AE, 0x1D241), + (0x1D245, 0x1D245), + (0x1D253, 0x1D25A), + (0x1D25D, 0x1D25E), + (0x1D260, 0x1D27F), + (0x1D2C0, 0x1D2D3), + (0x1D2E0, 0x1D2F3), + (0x1D300, 0x1D356), + (0x1D360, 0x1D378), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D7CB), + (0x1D7CE, 0x1D9FF), + (0x1DA37, 0x1DA3A), + (0x1DA6D, 0x1DA74), + (0x1DA76, 0x1DA83), + (0x1DA85, 0x1DA8B), + (0x1DB00, 0x1DB1C), + (0x1DF00, 0x1DF81), + (0x1DF90, 0x1DF96), + (0x1DFCD, 0x1DFFF), + (0x1E030, 0x1E06D), + (0x1E100, 0x1E12C), + (0x1E137, 0x1E13D), + (0x1E140, 0x1E149), + (0x1E14E, 0x1E14F), + (0x1E290, 0x1E2AD), + (0x1E2C0, 0x1E2EB), + (0x1E2F0, 0x1E2F9), + (0x1E2FF, 0x1E2FF), + (0x1E4D0, 0x1E4EB), + (0x1E4F0, 0x1E4F9), + (0x1E5D0, 0x1E5ED), + (0x1E5F0, 0x1E5FA), + (0x1E5FF, 0x1E5FF), + (0x1E6C0, 0x1E6DE), + (0x1E6E0, 0x1E6E2), + (0x1E6E4, 0x1E6E5), + (0x1E6E7, 0x1E6ED), + (0x1E6F0, 0x1E6F4), + (0x1E6FE, 0x1E6FF), + (0x1E7E0, 0x1E7E6), + (0x1E7E8, 0x1E7EB), + (0x1E7ED, 0x1E7EE), + (0x1E7F0, 0x1E7FE), + (0x1E800, 0x1E8C4), + (0x1E8C7, 0x1E8CF), + (0x1E900, 0x1E943), + (0x1E94B, 0x1E94B), + (0x1E950, 0x1E959), + (0x1E95E, 0x1E95F), + (0x1EC71, 0x1ECB4), + (0x1ED01, 0x1ED3D), + (0x1EE00, 0x1EE03), + (0x1EE05, 0x1EE1F), + (0x1EE21, 0x1EE22), + (0x1EE24, 0x1EE24), + (0x1EE27, 0x1EE27), + (0x1EE29, 0x1EE32), + (0x1EE34, 0x1EE37), + (0x1EE39, 0x1EE39), + (0x1EE3B, 0x1EE3B), + (0x1EE42, 0x1EE42), + (0x1EE47, 0x1EE47), + (0x1EE49, 0x1EE49), + (0x1EE4B, 0x1EE4B), + (0x1EE4D, 0x1EE4F), + (0x1EE51, 0x1EE52), + (0x1EE54, 0x1EE54), + (0x1EE57, 0x1EE57), + (0x1EE59, 0x1EE59), + (0x1EE5B, 0x1EE5B), + (0x1EE5D, 0x1EE5D), + (0x1EE5F, 0x1EE5F), + (0x1EE61, 0x1EE62), + (0x1EE64, 0x1EE64), + (0x1EE67, 0x1EE6A), + (0x1EE6C, 0x1EE72), + (0x1EE74, 0x1EE77), + (0x1EE79, 0x1EE7C), + (0x1EE7E, 0x1EE7E), + (0x1EE80, 0x1EE89), + (0x1EE8B, 0x1EE9B), + (0x1EEA1, 0x1EEA3), + (0x1EEA5, 0x1EEA9), + (0x1EEAB, 0x1EEBB), + (0x1EEF0, 0x1EEF1), + (0x1F000, 0x1F02B), + (0x1F030, 0x1F093), + (0x1F0A0, 0x1F0AE), + (0x1F0B1, 0x1F0BF), + (0x1F0C1, 0x1F0CF), + (0x1F0D1, 0x1F0F5), + (0x1F100, 0x1F1AE), + (0x1F1E6, 0x1F202), + (0x1F210, 0x1F23B), + (0x1F240, 0x1F248), + (0x1F250, 0x1F251), + (0x1F260, 0x1F265), + (0x1F300, 0x1F6D9), + (0x1F6DC, 0x1F6EC), + (0x1F6F0, 0x1F6FC), + (0x1F700, 0x1F7DB), + (0x1F7E0, 0x1F7EB), + (0x1F7F0, 0x1F80B), + (0x1F810, 0x1F847), + (0x1F850, 0x1F859), + (0x1F860, 0x1F887), + (0x1F890, 0x1F8AD), + (0x1F8B0, 0x1F8BB), + (0x1F8C0, 0x1F8C1), + (0x1F8D0, 0x1F8D8), + (0x1F900, 0x1FA57), + (0x1FA60, 0x1FA6D), + (0x1FA70, 0x1FA7C), + (0x1FA80, 0x1FAC6), + (0x1FAC8, 0x1FAC8), + (0x1FACC, 0x1FADD), + (0x1FADF, 0x1FAEB), + (0x1FAEF, 0x1FAFA), + (0x1FB00, 0x1FB92), + (0x1FB94, 0x1FBFA), + (0x20000, 0x2A6DF), + (0x2A700, 0x2B81E), + (0x2B820, 0x2CEAD), + (0x2CEB0, 0x2EBE0), + (0x2EBF0, 0x2EE5D), + (0x2F800, 0x2FA1D), + (0x30000, 0x3134A), + (0x31350, 0x33479), + (0x3D000, 0x3FC3F), +]; + +/// The code points with the binary property Grapheme_Extend, as ranges. +pub static BINARY_GRAPHEME_EXTEND: &[(u32, u32)] = &[ + (0x300, 0x36F), + (0x483, 0x489), + (0x591, 0x5BD), + (0x5BF, 0x5BF), + (0x5C1, 0x5C2), + (0x5C4, 0x5C5), + (0x5C7, 0x5C9), + (0x610, 0x61A), + (0x64B, 0x65F), + (0x670, 0x670), + (0x6D6, 0x6DC), + (0x6DF, 0x6E4), + (0x6E7, 0x6E8), + (0x6EA, 0x6ED), + (0x711, 0x711), + (0x730, 0x74A), + (0x7A6, 0x7B0), + (0x7EB, 0x7F3), + (0x7FD, 0x7FD), + (0x816, 0x819), + (0x81B, 0x823), + (0x825, 0x827), + (0x829, 0x82D), + (0x859, 0x85B), + (0x897, 0x89F), + (0x8CA, 0x8E1), + (0x8E3, 0x902), + (0x93A, 0x93A), + (0x93C, 0x93C), + (0x941, 0x948), + (0x94D, 0x94D), + (0x951, 0x957), + (0x962, 0x963), + (0x981, 0x981), + (0x9BC, 0x9BC), + (0x9BE, 0x9BE), + (0x9C1, 0x9C4), + (0x9CD, 0x9CD), + (0x9D7, 0x9D7), + (0x9E2, 0x9E3), + (0x9FE, 0x9FE), + (0xA01, 0xA02), + (0xA3C, 0xA3C), + (0xA41, 0xA42), + (0xA47, 0xA48), + (0xA4B, 0xA4D), + (0xA51, 0xA51), + (0xA70, 0xA71), + (0xA75, 0xA75), + (0xA81, 0xA82), + (0xABC, 0xABC), + (0xAC1, 0xAC5), + (0xAC7, 0xAC8), + (0xACD, 0xACD), + (0xAE2, 0xAE3), + (0xAFA, 0xAFF), + (0xB01, 0xB01), + (0xB3C, 0xB3C), + (0xB3E, 0xB3F), + (0xB41, 0xB44), + (0xB4D, 0xB4D), + (0xB53, 0xB57), + (0xB62, 0xB63), + (0xB82, 0xB82), + (0xBBE, 0xBBE), + (0xBC0, 0xBC0), + (0xBCD, 0xBCD), + (0xBD7, 0xBD7), + (0xC00, 0xC00), + (0xC04, 0xC04), + (0xC3C, 0xC3C), + (0xC3E, 0xC40), + (0xC46, 0xC48), + (0xC4A, 0xC4D), + (0xC55, 0xC56), + (0xC62, 0xC63), + (0xC81, 0xC81), + (0xCBC, 0xCBC), + (0xCBF, 0xCC0), + (0xCC2, 0xCC2), + (0xCC6, 0xCC8), + (0xCCA, 0xCCD), + (0xCD5, 0xCD6), + (0xCE2, 0xCE3), + (0xD00, 0xD01), + (0xD3B, 0xD3C), + (0xD3E, 0xD3E), + (0xD41, 0xD44), + (0xD4D, 0xD4D), + (0xD57, 0xD57), + (0xD62, 0xD63), + (0xD81, 0xD81), + (0xDCA, 0xDCA), + (0xDCF, 0xDCF), + (0xDD2, 0xDD4), + (0xDD6, 0xDD6), + (0xDDF, 0xDDF), + (0xE31, 0xE31), + (0xE34, 0xE3A), + (0xE47, 0xE4E), + (0xEB1, 0xEB1), + (0xEB4, 0xEBC), + (0xEC8, 0xECE), + (0xF18, 0xF19), + (0xF35, 0xF35), + (0xF37, 0xF37), + (0xF39, 0xF39), + (0xF71, 0xF7E), + (0xF80, 0xF84), + (0xF86, 0xF87), + (0xF8D, 0xF97), + (0xF99, 0xFBC), + (0xFC6, 0xFC6), + (0x102D, 0x1030), + (0x1032, 0x1037), + (0x1039, 0x103A), + (0x103D, 0x103E), + (0x1058, 0x1059), + (0x105E, 0x1060), + (0x1071, 0x1074), + (0x1082, 0x1082), + (0x1085, 0x1086), + (0x108D, 0x108D), + (0x109D, 0x109D), + (0x135D, 0x135F), + (0x1712, 0x1715), + (0x1732, 0x1734), + (0x1752, 0x1753), + (0x1772, 0x1773), + (0x17B4, 0x17B5), + (0x17B7, 0x17BD), + (0x17C6, 0x17C6), + (0x17C9, 0x17D3), + (0x17DD, 0x17DD), + (0x180B, 0x180D), + (0x180F, 0x180F), + (0x1885, 0x1886), + (0x18A9, 0x18A9), + (0x1920, 0x1922), + (0x1927, 0x1928), + (0x1932, 0x1932), + (0x1939, 0x193B), + (0x1A17, 0x1A18), + (0x1A1B, 0x1A1B), + (0x1A56, 0x1A56), + (0x1A58, 0x1A5E), + (0x1A60, 0x1A60), + (0x1A62, 0x1A62), + (0x1A65, 0x1A6C), + (0x1A73, 0x1A7C), + (0x1A7F, 0x1A7F), + (0x1AB0, 0x1AF0), + (0x1B00, 0x1B03), + (0x1B34, 0x1B3D), + (0x1B42, 0x1B44), + (0x1B6B, 0x1B73), + (0x1B80, 0x1B81), + (0x1BA2, 0x1BA5), + (0x1BA8, 0x1BAD), + (0x1BE6, 0x1BE6), + (0x1BE8, 0x1BE9), + (0x1BED, 0x1BED), + (0x1BEF, 0x1BF3), + (0x1C2C, 0x1C33), + (0x1C36, 0x1C37), + (0x1CD0, 0x1CD2), + (0x1CD4, 0x1CE0), + (0x1CE2, 0x1CE8), + (0x1CED, 0x1CED), + (0x1CF4, 0x1CF4), + (0x1CF8, 0x1CF9), + (0x1DC0, 0x1DFF), + (0x200C, 0x200C), + (0x20D0, 0x20F0), + (0x2CEF, 0x2CF1), + (0x2D7F, 0x2D7F), + (0x2DE0, 0x2DFF), + (0x302A, 0x302F), + (0x3099, 0x309A), + (0xA66F, 0xA672), + (0xA674, 0xA67D), + (0xA69E, 0xA69F), + (0xA6F0, 0xA6F1), + (0xA802, 0xA802), + (0xA806, 0xA806), + (0xA80B, 0xA80B), + (0xA825, 0xA826), + (0xA82C, 0xA82C), + (0xA8C4, 0xA8C5), + (0xA8E0, 0xA8F1), + (0xA8FF, 0xA8FF), + (0xA926, 0xA92D), + (0xA947, 0xA951), + (0xA953, 0xA953), + (0xA980, 0xA982), + (0xA9B3, 0xA9B3), + (0xA9B6, 0xA9B9), + (0xA9BC, 0xA9BD), + (0xA9C0, 0xA9C0), + (0xA9E5, 0xA9E5), + (0xAA29, 0xAA2E), + (0xAA31, 0xAA32), + (0xAA35, 0xAA36), + (0xAA43, 0xAA43), + (0xAA4C, 0xAA4C), + (0xAA7C, 0xAA7C), + (0xAAB0, 0xAAB0), + (0xAAB2, 0xAAB4), + (0xAAB7, 0xAAB8), + (0xAABE, 0xAABF), + (0xAAC1, 0xAAC1), + (0xAAEC, 0xAAED), + (0xAAF6, 0xAAF6), + (0xABE5, 0xABE5), + (0xABE8, 0xABE8), + (0xABED, 0xABED), + (0xFB1E, 0xFB1E), + (0xFE00, 0xFE0F), + (0xFE20, 0xFE2F), + (0xFF9E, 0xFF9F), + (0x101FD, 0x101FD), + (0x102E0, 0x102E0), + (0x10376, 0x1037A), + (0x10A01, 0x10A03), + (0x10A05, 0x10A06), + (0x10A0C, 0x10A0F), + (0x10A38, 0x10A3A), + (0x10A3F, 0x10A3F), + (0x10AE5, 0x10AE6), + (0x10D24, 0x10D27), + (0x10D69, 0x10D6D), + (0x10EAB, 0x10EAC), + (0x10ECB, 0x10ECF), + (0x10EF0, 0x10EFF), + (0x10F46, 0x10F50), + (0x10F82, 0x10F85), + (0x11001, 0x11001), + (0x11038, 0x11046), + (0x11070, 0x11070), + (0x11073, 0x11074), + (0x1107F, 0x11081), + (0x110B3, 0x110B6), + (0x110B9, 0x110BA), + (0x110C2, 0x110C2), + (0x11100, 0x11102), + (0x11127, 0x1112B), + (0x1112D, 0x11134), + (0x11173, 0x11173), + (0x11180, 0x11181), + (0x111B6, 0x111BE), + (0x111C0, 0x111C0), + (0x111C9, 0x111CC), + (0x111CF, 0x111CF), + (0x1122F, 0x11231), + (0x11234, 0x11237), + (0x1123E, 0x1123E), + (0x11241, 0x11241), + (0x112DF, 0x112DF), + (0x112E3, 0x112EA), + (0x11300, 0x11301), + (0x1133B, 0x1133C), + (0x1133E, 0x1133E), + (0x11340, 0x11340), + (0x1134D, 0x1134D), + (0x11357, 0x11357), + (0x11366, 0x1136C), + (0x11370, 0x11374), + (0x113B8, 0x113B8), + (0x113BB, 0x113C0), + (0x113C2, 0x113C2), + (0x113C5, 0x113C5), + (0x113C7, 0x113C9), + (0x113CE, 0x113D0), + (0x113D2, 0x113D2), + (0x113E1, 0x113E2), + (0x11438, 0x1143F), + (0x11442, 0x11444), + (0x11446, 0x11446), + (0x1145E, 0x1145E), + (0x114B0, 0x114B0), + (0x114B3, 0x114B8), + (0x114BA, 0x114BA), + (0x114BD, 0x114BD), + (0x114BF, 0x114C0), + (0x114C2, 0x114C3), + (0x115AF, 0x115AF), + (0x115B2, 0x115B5), + (0x115BC, 0x115BD), + (0x115BF, 0x115C0), + (0x115DC, 0x115DD), + (0x11633, 0x1163A), + (0x1163D, 0x1163D), + (0x1163F, 0x11640), + (0x116AB, 0x116AB), + (0x116AD, 0x116AD), + (0x116B0, 0x116B7), + (0x1171D, 0x1171D), + (0x1171F, 0x1171F), + (0x11722, 0x11725), + (0x11727, 0x1172B), + (0x1182F, 0x11837), + (0x11839, 0x1183A), + (0x11930, 0x11930), + (0x1193B, 0x1193E), + (0x11943, 0x11943), + (0x119D4, 0x119D7), + (0x119DA, 0x119DB), + (0x119E0, 0x119E0), + (0x11A01, 0x11A0A), + (0x11A33, 0x11A38), + (0x11A3B, 0x11A3E), + (0x11A47, 0x11A47), + (0x11A51, 0x11A56), + (0x11A59, 0x11A5B), + (0x11A8A, 0x11A96), + (0x11A98, 0x11A99), + (0x11B60, 0x11B60), + (0x11B62, 0x11B64), + (0x11B66, 0x11B66), + (0x11C30, 0x11C36), + (0x11C38, 0x11C3D), + (0x11C3F, 0x11C3F), + (0x11C92, 0x11CA7), + (0x11CAA, 0x11CB0), + (0x11CB2, 0x11CB3), + (0x11CB5, 0x11CB6), + (0x11D31, 0x11D36), + (0x11D3A, 0x11D3A), + (0x11D3C, 0x11D3D), + (0x11D3F, 0x11D45), + (0x11D47, 0x11D47), + (0x11D90, 0x11D91), + (0x11D95, 0x11D95), + (0x11D97, 0x11D97), + (0x11DF0, 0x11DF0), + (0x11EF3, 0x11EF4), + (0x11F00, 0x11F01), + (0x11F36, 0x11F3A), + (0x11F40, 0x11F42), + (0x11F5A, 0x11F5A), + (0x13440, 0x13440), + (0x13447, 0x13455), + (0x1611E, 0x16129), + (0x1612D, 0x1612F), + (0x16AF0, 0x16AF4), + (0x16B30, 0x16B36), + (0x16F4F, 0x16F4F), + (0x16F8F, 0x16F92), + (0x16FE4, 0x16FE4), + (0x16FF0, 0x16FF1), + (0x1BC9D, 0x1BC9E), + (0x1CF00, 0x1CF2D), + (0x1CF30, 0x1CF46), + (0x1D127, 0x1D128), + (0x1D165, 0x1D169), + (0x1D16D, 0x1D172), + (0x1D17B, 0x1D182), + (0x1D185, 0x1D18B), + (0x1D1AA, 0x1D1AD), + (0x1D242, 0x1D244), + (0x1D250, 0x1D252), + (0x1D25B, 0x1D25C), + (0x1D25F, 0x1D25F), + (0x1D280, 0x1D281), + (0x1DA00, 0x1DA36), + (0x1DA3B, 0x1DA6C), + (0x1DA75, 0x1DA75), + (0x1DA84, 0x1DA84), + (0x1DA9B, 0x1DA9F), + (0x1DAA1, 0x1DAAF), + (0x1E000, 0x1E006), + (0x1E008, 0x1E018), + (0x1E01B, 0x1E021), + (0x1E023, 0x1E024), + (0x1E026, 0x1E02A), + (0x1E08F, 0x1E08F), + (0x1E130, 0x1E136), + (0x1E2AE, 0x1E2AE), + (0x1E2EC, 0x1E2EF), + (0x1E4EC, 0x1E4EF), + (0x1E5EE, 0x1E5EF), + (0x1E6E3, 0x1E6E3), + (0x1E6E6, 0x1E6E6), + (0x1E6EE, 0x1E6EF), + (0x1E6F5, 0x1E6F5), + (0x1E8D0, 0x1E8D6), + (0x1E944, 0x1E94A), + (0xE0020, 0xE007F), + (0xE0100, 0xE01EF), +]; + +/// The code points with the binary property Hex_Digit, as ranges. +pub static BINARY_HEX_DIGIT: &[(u32, u32)] = &[ + (0x30, 0x39), + (0x41, 0x46), + (0x61, 0x66), + (0xFF10, 0xFF19), + (0xFF21, 0xFF26), + (0xFF41, 0xFF46), +]; + +/// The code points with the binary property IDS_Binary_Operator, as ranges. +pub static BINARY_IDS_BINARY_OPERATOR: &[(u32, u32)] = + &[(0x2FF0, 0x2FF1), (0x2FF4, 0x2FFD), (0x31EF, 0x31EF)]; + +/// The code points with the binary property IDS_Trinary_Operator, as ranges. +pub static BINARY_IDS_TRINARY_OPERATOR: &[(u32, u32)] = &[(0x2FF2, 0x2FF3)]; + +/// The code points with the binary property ID_Continue, as ranges. +pub static BINARY_ID_CONTINUE: &[(u32, u32)] = &[ + (0x30, 0x39), + (0x41, 0x5A), + (0x5F, 0x5F), + (0x61, 0x7A), + (0xAA, 0xAA), + (0xB5, 0xB5), + (0xB7, 0xB7), + (0xBA, 0xBA), + (0xC0, 0xD6), + (0xD8, 0xF6), + (0xF8, 0x2C1), + (0x2C6, 0x2D1), + (0x2E0, 0x2E4), + (0x2EC, 0x2EC), + (0x2EE, 0x2EE), + (0x300, 0x374), + (0x376, 0x377), + (0x37A, 0x37D), + (0x37F, 0x37F), + (0x386, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x3A1), + (0x3A3, 0x3F5), + (0x3F7, 0x481), + (0x483, 0x487), + (0x48A, 0x52F), + (0x531, 0x556), + (0x558, 0x559), + (0x560, 0x588), + (0x58B, 0x58C), + (0x591, 0x5BD), + (0x5BF, 0x5BF), + (0x5C1, 0x5C2), + (0x5C4, 0x5C5), + (0x5C7, 0x5C9), + (0x5D0, 0x5EA), + (0x5EF, 0x5F2), + (0x610, 0x61A), + (0x620, 0x669), + (0x66E, 0x6D3), + (0x6D5, 0x6DC), + (0x6DF, 0x6E8), + (0x6EA, 0x6FC), + (0x6FF, 0x6FF), + (0x710, 0x74A), + (0x74D, 0x7B1), + (0x7C0, 0x7F5), + (0x7FA, 0x7FA), + (0x7FD, 0x7FD), + (0x800, 0x82D), + (0x840, 0x85B), + (0x860, 0x86A), + (0x870, 0x887), + (0x889, 0x88F), + (0x897, 0x8E1), + (0x8E3, 0x963), + (0x966, 0x96F), + (0x971, 0x983), + (0x985, 0x98C), + (0x98F, 0x990), + (0x993, 0x9A8), + (0x9AA, 0x9B0), + (0x9B2, 0x9B2), + (0x9B6, 0x9B9), + (0x9BC, 0x9C4), + (0x9C7, 0x9C8), + (0x9CB, 0x9CE), + (0x9D7, 0x9D7), + (0x9DC, 0x9DD), + (0x9DF, 0x9E3), + (0x9E6, 0x9F1), + (0x9FC, 0x9FC), + (0x9FE, 0x9FE), + (0xA01, 0xA03), + (0xA05, 0xA0A), + (0xA0F, 0xA10), + (0xA13, 0xA28), + (0xA2A, 0xA30), + (0xA32, 0xA33), + (0xA35, 0xA36), + (0xA38, 0xA39), + (0xA3C, 0xA3C), + (0xA3E, 0xA42), + (0xA47, 0xA48), + (0xA4B, 0xA4D), + (0xA51, 0xA51), + (0xA59, 0xA5C), + (0xA5E, 0xA5E), + (0xA66, 0xA75), + (0xA81, 0xA83), + (0xA85, 0xA8D), + (0xA8F, 0xA91), + (0xA93, 0xAA8), + (0xAAA, 0xAB0), + (0xAB2, 0xAB3), + (0xAB5, 0xAB9), + (0xABC, 0xAC5), + (0xAC7, 0xAC9), + (0xACB, 0xACD), + (0xAD0, 0xAD0), + (0xAE0, 0xAE3), + (0xAE6, 0xAEF), + (0xAF9, 0xAFF), + (0xB01, 0xB03), + (0xB05, 0xB0C), + (0xB0F, 0xB10), + (0xB13, 0xB28), + (0xB2A, 0xB30), + (0xB32, 0xB33), + (0xB35, 0xB39), + (0xB3C, 0xB44), + (0xB47, 0xB48), + (0xB4B, 0xB4D), + (0xB53, 0xB57), + (0xB5C, 0xB5D), + (0xB5F, 0xB63), + (0xB66, 0xB6F), + (0xB71, 0xB71), + (0xB82, 0xB83), + (0xB85, 0xB8A), + (0xB8E, 0xB90), + (0xB92, 0xB95), + (0xB99, 0xB9A), + (0xB9C, 0xB9C), + (0xB9E, 0xB9F), + (0xBA3, 0xBA4), + (0xBA8, 0xBAA), + (0xBAE, 0xBB9), + (0xBBE, 0xBC2), + (0xBC6, 0xBC8), + (0xBCA, 0xBCD), + (0xBD0, 0xBD0), + (0xBD7, 0xBD7), + (0xBE6, 0xBEF), + (0xC00, 0xC0C), + (0xC0E, 0xC10), + (0xC12, 0xC28), + (0xC2A, 0xC39), + (0xC3C, 0xC44), + (0xC46, 0xC48), + (0xC4A, 0xC4D), + (0xC55, 0xC56), + (0xC58, 0xC5A), + (0xC5C, 0xC5D), + (0xC60, 0xC63), + (0xC66, 0xC6F), + (0xC80, 0xC83), + (0xC85, 0xC8C), + (0xC8E, 0xC90), + (0xC92, 0xCA8), + (0xCAA, 0xCB3), + (0xCB5, 0xCB9), + (0xCBC, 0xCC4), + (0xCC6, 0xCC8), + (0xCCA, 0xCCD), + (0xCD5, 0xCD6), + (0xCDC, 0xCDE), + (0xCE0, 0xCE3), + (0xCE6, 0xCEF), + (0xCF1, 0xCF3), + (0xD00, 0xD0C), + (0xD0E, 0xD10), + (0xD12, 0xD44), + (0xD46, 0xD48), + (0xD4A, 0xD4E), + (0xD54, 0xD57), + (0xD5F, 0xD63), + (0xD66, 0xD6F), + (0xD7A, 0xD7F), + (0xD81, 0xD83), + (0xD85, 0xD96), + (0xD9A, 0xDB1), + (0xDB3, 0xDBB), + (0xDBD, 0xDBD), + (0xDC0, 0xDC6), + (0xDCA, 0xDCA), + (0xDCF, 0xDD4), + (0xDD6, 0xDD6), + (0xDD8, 0xDDF), + (0xDE6, 0xDEF), + (0xDF2, 0xDF3), + (0xE01, 0xE3A), + (0xE40, 0xE4E), + (0xE50, 0xE59), + (0xE81, 0xE82), + (0xE84, 0xE84), + (0xE86, 0xE8A), + (0xE8C, 0xEA3), + (0xEA5, 0xEA5), + (0xEA7, 0xEBD), + (0xEC0, 0xEC4), + (0xEC6, 0xEC6), + (0xEC8, 0xECE), + (0xED0, 0xED9), + (0xEDC, 0xEDF), + (0xF00, 0xF00), + (0xF18, 0xF19), + (0xF20, 0xF29), + (0xF35, 0xF35), + (0xF37, 0xF37), + (0xF39, 0xF39), + (0xF3E, 0xF47), + (0xF49, 0xF6C), + (0xF71, 0xF84), + (0xF86, 0xF97), + (0xF99, 0xFBC), + (0xFC6, 0xFC6), + (0x1000, 0x1049), + (0x1050, 0x109D), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10D0, 0x10FA), + (0x10FC, 0x1248), + (0x124A, 0x124D), + (0x1250, 0x1256), + (0x1258, 0x1258), + (0x125A, 0x125D), + (0x1260, 0x1288), + (0x128A, 0x128D), + (0x1290, 0x12B0), + (0x12B2, 0x12B5), + (0x12B8, 0x12BE), + (0x12C0, 0x12C0), + (0x12C2, 0x12C5), + (0x12C8, 0x12D6), + (0x12D8, 0x1310), + (0x1312, 0x1315), + (0x1318, 0x135A), + (0x135D, 0x135F), + (0x1369, 0x1371), + (0x1380, 0x138F), + (0x13A0, 0x13F5), + (0x13F8, 0x13FD), + (0x1401, 0x166C), + (0x166F, 0x167F), + (0x1681, 0x169A), + (0x16A0, 0x16EA), + (0x16EE, 0x16F8), + (0x1700, 0x1715), + (0x171F, 0x1734), + (0x1740, 0x1753), + (0x1760, 0x176C), + (0x176E, 0x1770), + (0x1772, 0x1773), + (0x1780, 0x17D3), + (0x17D7, 0x17D7), + (0x17DC, 0x17DD), + (0x17E0, 0x17E9), + (0x180B, 0x180D), + (0x180F, 0x1819), + (0x1820, 0x1878), + (0x1880, 0x18AA), + (0x18B0, 0x18F5), + (0x1900, 0x191E), + (0x1920, 0x192B), + (0x1930, 0x193B), + (0x1946, 0x196D), + (0x1970, 0x1974), + (0x1980, 0x19AB), + (0x19B0, 0x19C9), + (0x19D0, 0x19DA), + (0x1A00, 0x1A1B), + (0x1A20, 0x1A5E), + (0x1A60, 0x1A7C), + (0x1A7F, 0x1A89), + (0x1A90, 0x1A99), + (0x1AA7, 0x1AA7), + (0x1AB0, 0x1ABD), + (0x1ABF, 0x1AF0), + (0x1B00, 0x1B4C), + (0x1B50, 0x1B59), + (0x1B6B, 0x1B73), + (0x1B80, 0x1BF3), + (0x1C00, 0x1C37), + (0x1C40, 0x1C49), + (0x1C4D, 0x1C7D), + (0x1C80, 0x1C8A), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1CD0, 0x1CD2), + (0x1CD4, 0x1CFA), + (0x1D00, 0x1F15), + (0x1F18, 0x1F1D), + (0x1F20, 0x1F45), + (0x1F48, 0x1F4D), + (0x1F50, 0x1F57), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FBC), + (0x1FBE, 0x1FBE), + (0x1FC2, 0x1FC4), + (0x1FC6, 0x1FCC), + (0x1FD0, 0x1FD3), + (0x1FD6, 0x1FDB), + (0x1FE0, 0x1FEC), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FFC), + (0x200C, 0x200D), + (0x203F, 0x2040), + (0x2054, 0x2054), + (0x2071, 0x2071), + (0x207F, 0x207F), + (0x208F, 0x209F), + (0x20D0, 0x20DC), + (0x20E1, 0x20E1), + (0x20E5, 0x20F0), + (0x2102, 0x2102), + (0x2107, 0x2107), + (0x210A, 0x2113), + (0x2115, 0x2115), + (0x2118, 0x211D), + (0x2124, 0x2124), + (0x2126, 0x2126), + (0x2128, 0x2128), + (0x212A, 0x2139), + (0x213C, 0x213F), + (0x2145, 0x2149), + (0x214E, 0x214E), + (0x2160, 0x2188), + (0x2C00, 0x2CE4), + (0x2CEB, 0x2CF3), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0x2D30, 0x2D67), + (0x2D6F, 0x2D6F), + (0x2D7F, 0x2D96), + (0x2DA0, 0x2DA6), + (0x2DA8, 0x2DAE), + (0x2DB0, 0x2DB6), + (0x2DB8, 0x2DBE), + (0x2DC0, 0x2DC6), + (0x2DC8, 0x2DCE), + (0x2DD0, 0x2DD6), + (0x2DD8, 0x2DDE), + (0x2DE0, 0x2DFF), + (0x3005, 0x3007), + (0x3021, 0x302F), + (0x3031, 0x3035), + (0x3038, 0x303C), + (0x3041, 0x3096), + (0x3099, 0x309F), + (0x30A1, 0x30FF), + (0x3105, 0x312F), + (0x3131, 0x318E), + (0x31A0, 0x31BF), + (0x31F0, 0x31FF), + (0x3400, 0x4DBF), + (0x4E00, 0xA48C), + (0xA4D0, 0xA4FD), + (0xA500, 0xA60C), + (0xA610, 0xA62B), + (0xA640, 0xA66F), + (0xA674, 0xA67D), + (0xA67F, 0xA6F1), + (0xA717, 0xA71F), + (0xA722, 0xA788), + (0xA78B, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F1, 0xA827), + (0xA82C, 0xA82C), + (0xA840, 0xA873), + (0xA880, 0xA8C5), + (0xA8D0, 0xA8D9), + (0xA8E0, 0xA8F7), + (0xA8FB, 0xA8FB), + (0xA8FD, 0xA92D), + (0xA930, 0xA953), + (0xA960, 0xA97C), + (0xA980, 0xA9C0), + (0xA9CF, 0xA9D9), + (0xA9E0, 0xA9FE), + (0xAA00, 0xAA36), + (0xAA40, 0xAA4D), + (0xAA50, 0xAA59), + (0xAA60, 0xAA76), + (0xAA7A, 0xAAC2), + (0xAADB, 0xAADD), + (0xAAE0, 0xAAEF), + (0xAAF2, 0xAAF6), + (0xAB01, 0xAB06), + (0xAB09, 0xAB0E), + (0xAB11, 0xAB16), + (0xAB20, 0xAB26), + (0xAB28, 0xAB2E), + (0xAB30, 0xAB5A), + (0xAB5C, 0xAB69), + (0xAB6C, 0xAB6D), + (0xAB70, 0xABEA), + (0xABEC, 0xABED), + (0xABF0, 0xABF9), + (0xAC00, 0xD7A3), + (0xD7B0, 0xD7C6), + (0xD7CB, 0xD7FB), + (0xF900, 0xFA6D), + (0xFA70, 0xFAD9), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFB1D, 0xFB28), + (0xFB2A, 0xFB36), + (0xFB38, 0xFB3C), + (0xFB3E, 0xFB3E), + (0xFB40, 0xFB41), + (0xFB43, 0xFB44), + (0xFB46, 0xFBB1), + (0xFBD3, 0xFD3D), + (0xFD50, 0xFD8F), + (0xFD92, 0xFDC7), + (0xFDF0, 0xFDFB), + (0xFE00, 0xFE0F), + (0xFE20, 0xFE2F), + (0xFE33, 0xFE34), + (0xFE4D, 0xFE4F), + (0xFE70, 0xFE74), + (0xFE76, 0xFEFC), + (0xFF10, 0xFF19), + (0xFF21, 0xFF3A), + (0xFF3F, 0xFF3F), + (0xFF41, 0xFF5A), + (0xFF65, 0xFFBE), + (0xFFC2, 0xFFC7), + (0xFFCA, 0xFFCF), + (0xFFD2, 0xFFD7), + (0xFFDA, 0xFFDC), + (0x10000, 0x1000B), + (0x1000D, 0x10026), + (0x10028, 0x1003A), + (0x1003C, 0x1003D), + (0x1003F, 0x1004D), + (0x10050, 0x1005D), + (0x10080, 0x100FA), + (0x10140, 0x10174), + (0x101FD, 0x101FD), + (0x10280, 0x1029C), + (0x102A0, 0x102D0), + (0x102E0, 0x102E0), + (0x10300, 0x1031F), + (0x1032D, 0x1034A), + (0x10350, 0x1037A), + (0x10380, 0x1039D), + (0x103A0, 0x103C3), + (0x103C8, 0x103CF), + (0x103D1, 0x103D5), + (0x10400, 0x1049D), + (0x104A0, 0x104A9), + (0x104B0, 0x104D3), + (0x104D8, 0x104FB), + (0x10500, 0x10527), + (0x10530, 0x10563), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x105C0, 0x105F3), + (0x10600, 0x10736), + (0x10740, 0x10755), + (0x10760, 0x10767), + (0x10780, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x10800, 0x10805), + (0x10808, 0x10808), + (0x1080A, 0x10835), + (0x10837, 0x10838), + (0x1083C, 0x1083C), + (0x1083F, 0x10855), + (0x10860, 0x10876), + (0x10880, 0x1089E), + (0x108E0, 0x108F2), + (0x108F4, 0x108F5), + (0x10900, 0x10915), + (0x10920, 0x10939), + (0x10940, 0x10959), + (0x10980, 0x109B7), + (0x109BE, 0x109BF), + (0x10A00, 0x10A03), + (0x10A05, 0x10A06), + (0x10A0C, 0x10A13), + (0x10A15, 0x10A17), + (0x10A19, 0x10A35), + (0x10A38, 0x10A3A), + (0x10A3F, 0x10A3F), + (0x10A60, 0x10A7C), + (0x10A80, 0x10A9C), + (0x10AC0, 0x10AC7), + (0x10AC9, 0x10AE6), + (0x10B00, 0x10B35), + (0x10B40, 0x10B55), + (0x10B60, 0x10B72), + (0x10B80, 0x10B91), + (0x10C00, 0x10C48), + (0x10C80, 0x10CB2), + (0x10CC0, 0x10CF2), + (0x10D00, 0x10D27), + (0x10D30, 0x10D39), + (0x10D40, 0x10D65), + (0x10D69, 0x10D6D), + (0x10D6F, 0x10D85), + (0x10E80, 0x10EA9), + (0x10EAB, 0x10EAC), + (0x10EB0, 0x10EB1), + (0x10EC2, 0x10EC7), + (0x10ECB, 0x10ECF), + (0x10ED9, 0x10EEE), + (0x10EF0, 0x10F1C), + (0x10F27, 0x10F27), + (0x10F30, 0x10F50), + (0x10F70, 0x10F85), + (0x10FB0, 0x10FC4), + (0x10FE0, 0x10FF6), + (0x11000, 0x11046), + (0x11066, 0x11075), + (0x1107F, 0x110BA), + (0x110C2, 0x110C2), + (0x110D0, 0x110E8), + (0x110F0, 0x110F9), + (0x11100, 0x11134), + (0x11136, 0x1113F), + (0x11144, 0x11147), + (0x11150, 0x11173), + (0x11176, 0x11176), + (0x11180, 0x111C4), + (0x111C9, 0x111CC), + (0x111CE, 0x111DA), + (0x111DC, 0x111DC), + (0x11200, 0x11211), + (0x11213, 0x11237), + (0x1123E, 0x11241), + (0x11280, 0x11286), + (0x11288, 0x11288), + (0x1128A, 0x1128D), + (0x1128F, 0x1129D), + (0x1129F, 0x112A8), + (0x112B0, 0x112EA), + (0x112F0, 0x112F9), + (0x11300, 0x11303), + (0x11305, 0x1130C), + (0x1130F, 0x11310), + (0x11313, 0x11328), + (0x1132A, 0x11330), + (0x11332, 0x11333), + (0x11335, 0x11339), + (0x1133B, 0x11344), + (0x11347, 0x11348), + (0x1134B, 0x1134D), + (0x11350, 0x11350), + (0x11357, 0x11357), + (0x1135D, 0x11363), + (0x11366, 0x1136C), + (0x11370, 0x11374), + (0x11380, 0x11389), + (0x1138B, 0x1138B), + (0x1138E, 0x1138E), + (0x11390, 0x113B5), + (0x113B7, 0x113C0), + (0x113C2, 0x113C2), + (0x113C5, 0x113C5), + (0x113C7, 0x113CA), + (0x113CC, 0x113D3), + (0x113E1, 0x113E2), + (0x11400, 0x1144A), + (0x11450, 0x11459), + (0x1145E, 0x11461), + (0x11480, 0x114C5), + (0x114C7, 0x114C7), + (0x114D0, 0x114D9), + (0x11580, 0x115B5), + (0x115B8, 0x115C0), + (0x115D8, 0x115DD), + (0x11600, 0x11640), + (0x11644, 0x11644), + (0x11650, 0x11659), + (0x11680, 0x116B8), + (0x116C0, 0x116C9), + (0x116D0, 0x116E3), + (0x11700, 0x1171A), + (0x1171D, 0x1172B), + (0x11730, 0x11739), + (0x11740, 0x11746), + (0x11800, 0x1183A), + (0x118A0, 0x118E9), + (0x118FF, 0x11906), + (0x11909, 0x11909), + (0x1190C, 0x11913), + (0x11915, 0x11916), + (0x11918, 0x11935), + (0x11937, 0x11938), + (0x1193B, 0x11943), + (0x11950, 0x11959), + (0x119A0, 0x119A7), + (0x119AA, 0x119D7), + (0x119DA, 0x119E1), + (0x119E3, 0x119E4), + (0x11A00, 0x11A3E), + (0x11A47, 0x11A47), + (0x11A50, 0x11A99), + (0x11A9D, 0x11A9D), + (0x11AB0, 0x11AF8), + (0x11B0A, 0x11B0A), + (0x11B60, 0x11B67), + (0x11BC0, 0x11BE0), + (0x11BF0, 0x11BF9), + (0x11C00, 0x11C08), + (0x11C0A, 0x11C36), + (0x11C38, 0x11C40), + (0x11C50, 0x11C59), + (0x11C72, 0x11C8F), + (0x11C92, 0x11CA7), + (0x11CA9, 0x11CB6), + (0x11D00, 0x11D06), + (0x11D08, 0x11D09), + (0x11D0B, 0x11D36), + (0x11D3A, 0x11D3A), + (0x11D3C, 0x11D3D), + (0x11D3F, 0x11D47), + (0x11D50, 0x11D59), + (0x11D60, 0x11D65), + (0x11D67, 0x11D68), + (0x11D6A, 0x11D8E), + (0x11D90, 0x11D91), + (0x11D93, 0x11D98), + (0x11DA0, 0x11DA9), + (0x11DB0, 0x11DDB), + (0x11DE0, 0x11DE9), + (0x11DF0, 0x11DF1), + (0x11EE0, 0x11EF6), + (0x11F00, 0x11F10), + (0x11F12, 0x11F3A), + (0x11F3E, 0x11F42), + (0x11F50, 0x11F5A), + (0x11FB0, 0x11FB0), + (0x12000, 0x12399), + (0x12400, 0x1246F), + (0x12475, 0x12543), + (0x12550, 0x12686), + (0x12F90, 0x12FF0), + (0x13000, 0x1342F), + (0x13440, 0x13455), + (0x13460, 0x143FA), + (0x14400, 0x14646), + (0x16100, 0x16139), + (0x16800, 0x16A38), + (0x16A40, 0x16A5E), + (0x16A60, 0x16A69), + (0x16A70, 0x16ABE), + (0x16AC0, 0x16AC9), + (0x16AD0, 0x16AED), + (0x16AF0, 0x16AF4), + (0x16B00, 0x16B36), + (0x16B40, 0x16B43), + (0x16B50, 0x16B59), + (0x16B63, 0x16B77), + (0x16B7D, 0x16B8F), + (0x16D40, 0x16D6C), + (0x16D70, 0x16D79), + (0x16E40, 0x16E7F), + (0x16EA0, 0x16EB8), + (0x16EBB, 0x16ED3), + (0x16F00, 0x16F4A), + (0x16F4F, 0x16F87), + (0x16F8F, 0x16F9F), + (0x16FE0, 0x16FE1), + (0x16FE3, 0x16FE4), + (0x16FF0, 0x16FF6), + (0x17000, 0x18CDA), + (0x18CFF, 0x18D20), + (0x18D80, 0x18DF2), + (0x18E00, 0x19191), + (0x191A0, 0x191D2), + (0x1AFF0, 0x1AFF3), + (0x1AFF5, 0x1AFFB), + (0x1AFFD, 0x1AFFE), + (0x1B000, 0x1B128), + (0x1B132, 0x1B132), + (0x1B150, 0x1B152), + (0x1B155, 0x1B155), + (0x1B164, 0x1B168), + (0x1B170, 0x1B2FB), + (0x1BC00, 0x1BC6A), + (0x1BC70, 0x1BC7C), + (0x1BC80, 0x1BC88), + (0x1BC90, 0x1BC99), + (0x1BC9D, 0x1BC9E), + (0x1CCF0, 0x1CCF9), + (0x1CF00, 0x1CF2D), + (0x1CF30, 0x1CF46), + (0x1D127, 0x1D128), + (0x1D165, 0x1D169), + (0x1D16D, 0x1D172), + (0x1D17B, 0x1D182), + (0x1D185, 0x1D18B), + (0x1D1AA, 0x1D1AD), + (0x1D242, 0x1D244), + (0x1D250, 0x1D252), + (0x1D25B, 0x1D25C), + (0x1D25F, 0x1D25F), + (0x1D280, 0x1D281), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D6C0), + (0x1D6C2, 0x1D6DA), + (0x1D6DC, 0x1D6FA), + (0x1D6FC, 0x1D714), + (0x1D716, 0x1D734), + (0x1D736, 0x1D74E), + (0x1D750, 0x1D76E), + (0x1D770, 0x1D788), + (0x1D78A, 0x1D7A8), + (0x1D7AA, 0x1D7C2), + (0x1D7C4, 0x1D7CB), + (0x1D7CE, 0x1D7FF), + (0x1DA00, 0x1DA36), + (0x1DA3B, 0x1DA6C), + (0x1DA75, 0x1DA75), + (0x1DA84, 0x1DA84), + (0x1DA9B, 0x1DA9F), + (0x1DAA1, 0x1DAAF), + (0x1DF00, 0x1DF81), + (0x1DF90, 0x1DF96), + (0x1DFCD, 0x1E006), + (0x1E008, 0x1E018), + (0x1E01B, 0x1E021), + (0x1E023, 0x1E024), + (0x1E026, 0x1E02A), + (0x1E030, 0x1E06D), + (0x1E08F, 0x1E08F), + (0x1E100, 0x1E12C), + (0x1E130, 0x1E13D), + (0x1E140, 0x1E149), + (0x1E14E, 0x1E14E), + (0x1E290, 0x1E2AE), + (0x1E2C0, 0x1E2F9), + (0x1E4D0, 0x1E4F9), + (0x1E5D0, 0x1E5FA), + (0x1E6C0, 0x1E6DE), + (0x1E6E0, 0x1E6F5), + (0x1E6FE, 0x1E6FF), + (0x1E7E0, 0x1E7E6), + (0x1E7E8, 0x1E7EB), + (0x1E7ED, 0x1E7EE), + (0x1E7F0, 0x1E7FE), + (0x1E800, 0x1E8C4), + (0x1E8D0, 0x1E8D6), + (0x1E900, 0x1E94B), + (0x1E950, 0x1E959), + (0x1EE00, 0x1EE03), + (0x1EE05, 0x1EE1F), + (0x1EE21, 0x1EE22), + (0x1EE24, 0x1EE24), + (0x1EE27, 0x1EE27), + (0x1EE29, 0x1EE32), + (0x1EE34, 0x1EE37), + (0x1EE39, 0x1EE39), + (0x1EE3B, 0x1EE3B), + (0x1EE42, 0x1EE42), + (0x1EE47, 0x1EE47), + (0x1EE49, 0x1EE49), + (0x1EE4B, 0x1EE4B), + (0x1EE4D, 0x1EE4F), + (0x1EE51, 0x1EE52), + (0x1EE54, 0x1EE54), + (0x1EE57, 0x1EE57), + (0x1EE59, 0x1EE59), + (0x1EE5B, 0x1EE5B), + (0x1EE5D, 0x1EE5D), + (0x1EE5F, 0x1EE5F), + (0x1EE61, 0x1EE62), + (0x1EE64, 0x1EE64), + (0x1EE67, 0x1EE6A), + (0x1EE6C, 0x1EE72), + (0x1EE74, 0x1EE77), + (0x1EE79, 0x1EE7C), + (0x1EE7E, 0x1EE7E), + (0x1EE80, 0x1EE89), + (0x1EE8B, 0x1EE9B), + (0x1EEA1, 0x1EEA3), + (0x1EEA5, 0x1EEA9), + (0x1EEAB, 0x1EEBB), + (0x1FBF0, 0x1FBF9), + (0x20000, 0x2A6DF), + (0x2A700, 0x2B81E), + (0x2B820, 0x2CEAD), + (0x2CEB0, 0x2EBE0), + (0x2EBF0, 0x2EE5D), + (0x2F800, 0x2FA1D), + (0x30000, 0x3134A), + (0x31350, 0x33479), + (0x3D000, 0x3FC3F), + (0xE0100, 0xE01EF), +]; + +/// The code points with the binary property ID_Start, as ranges. +pub static BINARY_ID_START: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0x61, 0x7A), + (0xAA, 0xAA), + (0xB5, 0xB5), + (0xBA, 0xBA), + (0xC0, 0xD6), + (0xD8, 0xF6), + (0xF8, 0x2C1), + (0x2C6, 0x2D1), + (0x2E0, 0x2E4), + (0x2EC, 0x2EC), + (0x2EE, 0x2EE), + (0x370, 0x374), + (0x376, 0x377), + (0x37A, 0x37D), + (0x37F, 0x37F), + (0x386, 0x386), + (0x388, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x3A1), + (0x3A3, 0x3F5), + (0x3F7, 0x481), + (0x48A, 0x52F), + (0x531, 0x556), + (0x558, 0x559), + (0x560, 0x588), + (0x58B, 0x58C), + (0x5D0, 0x5EA), + (0x5EF, 0x5F2), + (0x620, 0x64A), + (0x66E, 0x66F), + (0x671, 0x6D3), + (0x6D5, 0x6D5), + (0x6E5, 0x6E6), + (0x6EE, 0x6EF), + (0x6FA, 0x6FC), + (0x6FF, 0x6FF), + (0x710, 0x710), + (0x712, 0x72F), + (0x74D, 0x7A5), + (0x7B1, 0x7B1), + (0x7CA, 0x7EA), + (0x7F4, 0x7F5), + (0x7FA, 0x7FA), + (0x800, 0x815), + (0x81A, 0x81A), + (0x824, 0x824), + (0x828, 0x828), + (0x840, 0x858), + (0x860, 0x86A), + (0x870, 0x887), + (0x889, 0x88F), + (0x8A0, 0x8C9), + (0x904, 0x939), + (0x93D, 0x93D), + (0x950, 0x950), + (0x958, 0x961), + (0x971, 0x980), + (0x985, 0x98C), + (0x98F, 0x990), + (0x993, 0x9A8), + (0x9AA, 0x9B0), + (0x9B2, 0x9B2), + (0x9B6, 0x9B9), + (0x9BD, 0x9BD), + (0x9CE, 0x9CE), + (0x9DC, 0x9DD), + (0x9DF, 0x9E1), + (0x9F0, 0x9F1), + (0x9FC, 0x9FC), + (0xA05, 0xA0A), + (0xA0F, 0xA10), + (0xA13, 0xA28), + (0xA2A, 0xA30), + (0xA32, 0xA33), + (0xA35, 0xA36), + (0xA38, 0xA39), + (0xA59, 0xA5C), + (0xA5E, 0xA5E), + (0xA72, 0xA74), + (0xA85, 0xA8D), + (0xA8F, 0xA91), + (0xA93, 0xAA8), + (0xAAA, 0xAB0), + (0xAB2, 0xAB3), + (0xAB5, 0xAB9), + (0xABD, 0xABD), + (0xAD0, 0xAD0), + (0xAE0, 0xAE1), + (0xAF9, 0xAF9), + (0xB05, 0xB0C), + (0xB0F, 0xB10), + (0xB13, 0xB28), + (0xB2A, 0xB30), + (0xB32, 0xB33), + (0xB35, 0xB39), + (0xB3D, 0xB3D), + (0xB5C, 0xB5D), + (0xB5F, 0xB61), + (0xB71, 0xB71), + (0xB83, 0xB83), + (0xB85, 0xB8A), + (0xB8E, 0xB90), + (0xB92, 0xB95), + (0xB99, 0xB9A), + (0xB9C, 0xB9C), + (0xB9E, 0xB9F), + (0xBA3, 0xBA4), + (0xBA8, 0xBAA), + (0xBAE, 0xBB9), + (0xBD0, 0xBD0), + (0xC05, 0xC0C), + (0xC0E, 0xC10), + (0xC12, 0xC28), + (0xC2A, 0xC39), + (0xC3D, 0xC3D), + (0xC58, 0xC5A), + (0xC5C, 0xC5D), + (0xC60, 0xC61), + (0xC80, 0xC80), + (0xC85, 0xC8C), + (0xC8E, 0xC90), + (0xC92, 0xCA8), + (0xCAA, 0xCB3), + (0xCB5, 0xCB9), + (0xCBD, 0xCBD), + (0xCDC, 0xCDE), + (0xCE0, 0xCE1), + (0xCF1, 0xCF2), + (0xD04, 0xD0C), + (0xD0E, 0xD10), + (0xD12, 0xD3A), + (0xD3D, 0xD3D), + (0xD4E, 0xD4E), + (0xD54, 0xD56), + (0xD5F, 0xD61), + (0xD7A, 0xD7F), + (0xD85, 0xD96), + (0xD9A, 0xDB1), + (0xDB3, 0xDBB), + (0xDBD, 0xDBD), + (0xDC0, 0xDC6), + (0xE01, 0xE30), + (0xE32, 0xE33), + (0xE40, 0xE46), + (0xE81, 0xE82), + (0xE84, 0xE84), + (0xE86, 0xE8A), + (0xE8C, 0xEA3), + (0xEA5, 0xEA5), + (0xEA7, 0xEB0), + (0xEB2, 0xEB3), + (0xEBD, 0xEBD), + (0xEC0, 0xEC4), + (0xEC6, 0xEC6), + (0xEDC, 0xEDF), + (0xF00, 0xF00), + (0xF40, 0xF47), + (0xF49, 0xF6C), + (0xF88, 0xF8C), + (0x1000, 0x102A), + (0x103F, 0x103F), + (0x1050, 0x1055), + (0x105A, 0x105D), + (0x1061, 0x1061), + (0x1065, 0x1066), + (0x106E, 0x1070), + (0x1075, 0x1081), + (0x108E, 0x108E), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10D0, 0x10FA), + (0x10FC, 0x1248), + (0x124A, 0x124D), + (0x1250, 0x1256), + (0x1258, 0x1258), + (0x125A, 0x125D), + (0x1260, 0x1288), + (0x128A, 0x128D), + (0x1290, 0x12B0), + (0x12B2, 0x12B5), + (0x12B8, 0x12BE), + (0x12C0, 0x12C0), + (0x12C2, 0x12C5), + (0x12C8, 0x12D6), + (0x12D8, 0x1310), + (0x1312, 0x1315), + (0x1318, 0x135A), + (0x1380, 0x138F), + (0x13A0, 0x13F5), + (0x13F8, 0x13FD), + (0x1401, 0x166C), + (0x166F, 0x167F), + (0x1681, 0x169A), + (0x16A0, 0x16EA), + (0x16EE, 0x16F8), + (0x1700, 0x1711), + (0x171F, 0x1731), + (0x1740, 0x1751), + (0x1760, 0x176C), + (0x176E, 0x1770), + (0x1780, 0x17B3), + (0x17D7, 0x17D7), + (0x17DC, 0x17DC), + (0x1820, 0x1878), + (0x1880, 0x18A8), + (0x18AA, 0x18AA), + (0x18B0, 0x18F5), + (0x1900, 0x191E), + (0x1950, 0x196D), + (0x1970, 0x1974), + (0x1980, 0x19AB), + (0x19B0, 0x19C9), + (0x1A00, 0x1A16), + (0x1A20, 0x1A54), + (0x1AA7, 0x1AA7), + (0x1B05, 0x1B33), + (0x1B45, 0x1B4C), + (0x1B83, 0x1BA0), + (0x1BAE, 0x1BAF), + (0x1BBA, 0x1BE5), + (0x1C00, 0x1C23), + (0x1C4D, 0x1C4F), + (0x1C5A, 0x1C7D), + (0x1C80, 0x1C8A), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1CE9, 0x1CEC), + (0x1CEE, 0x1CF3), + (0x1CF5, 0x1CF6), + (0x1CFA, 0x1CFA), + (0x1D00, 0x1DBF), + (0x1E00, 0x1F15), + (0x1F18, 0x1F1D), + (0x1F20, 0x1F45), + (0x1F48, 0x1F4D), + (0x1F50, 0x1F57), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FBC), + (0x1FBE, 0x1FBE), + (0x1FC2, 0x1FC4), + (0x1FC6, 0x1FCC), + (0x1FD0, 0x1FD3), + (0x1FD6, 0x1FDB), + (0x1FE0, 0x1FEC), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FFC), + (0x2071, 0x2071), + (0x207F, 0x207F), + (0x208F, 0x209F), + (0x2102, 0x2102), + (0x2107, 0x2107), + (0x210A, 0x2113), + (0x2115, 0x2115), + (0x2118, 0x211D), + (0x2124, 0x2124), + (0x2126, 0x2126), + (0x2128, 0x2128), + (0x212A, 0x2139), + (0x213C, 0x213F), + (0x2145, 0x2149), + (0x214E, 0x214E), + (0x2160, 0x2188), + (0x2C00, 0x2CE4), + (0x2CEB, 0x2CEE), + (0x2CF2, 0x2CF3), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0x2D30, 0x2D67), + (0x2D6F, 0x2D6F), + (0x2D80, 0x2D96), + (0x2DA0, 0x2DA6), + (0x2DA8, 0x2DAE), + (0x2DB0, 0x2DB6), + (0x2DB8, 0x2DBE), + (0x2DC0, 0x2DC6), + (0x2DC8, 0x2DCE), + (0x2DD0, 0x2DD6), + (0x2DD8, 0x2DDE), + (0x3005, 0x3007), + (0x3021, 0x3029), + (0x3031, 0x3035), + (0x3038, 0x303C), + (0x3041, 0x3096), + (0x309B, 0x309F), + (0x30A1, 0x30FA), + (0x30FC, 0x30FF), + (0x3105, 0x312F), + (0x3131, 0x318E), + (0x31A0, 0x31BF), + (0x31F0, 0x31FF), + (0x3400, 0x4DBF), + (0x4E00, 0xA48C), + (0xA4D0, 0xA4FD), + (0xA500, 0xA60C), + (0xA610, 0xA61F), + (0xA62A, 0xA62B), + (0xA640, 0xA66E), + (0xA67F, 0xA69D), + (0xA6A0, 0xA6EF), + (0xA717, 0xA71F), + (0xA722, 0xA788), + (0xA78B, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F1, 0xA801), + (0xA803, 0xA805), + (0xA807, 0xA80A), + (0xA80C, 0xA822), + (0xA840, 0xA873), + (0xA882, 0xA8B3), + (0xA8F2, 0xA8F7), + (0xA8FB, 0xA8FB), + (0xA8FD, 0xA8FE), + (0xA90A, 0xA925), + (0xA930, 0xA946), + (0xA960, 0xA97C), + (0xA984, 0xA9B2), + (0xA9CF, 0xA9CF), + (0xA9E0, 0xA9E4), + (0xA9E6, 0xA9EF), + (0xA9FA, 0xA9FE), + (0xAA00, 0xAA28), + (0xAA40, 0xAA42), + (0xAA44, 0xAA4B), + (0xAA60, 0xAA76), + (0xAA7A, 0xAA7A), + (0xAA7E, 0xAAAF), + (0xAAB1, 0xAAB1), + (0xAAB5, 0xAAB6), + (0xAAB9, 0xAABD), + (0xAAC0, 0xAAC0), + (0xAAC2, 0xAAC2), + (0xAADB, 0xAADD), + (0xAAE0, 0xAAEA), + (0xAAF2, 0xAAF4), + (0xAB01, 0xAB06), + (0xAB09, 0xAB0E), + (0xAB11, 0xAB16), + (0xAB20, 0xAB26), + (0xAB28, 0xAB2E), + (0xAB30, 0xAB5A), + (0xAB5C, 0xAB69), + (0xAB6C, 0xAB6D), + (0xAB70, 0xABE2), + (0xAC00, 0xD7A3), + (0xD7B0, 0xD7C6), + (0xD7CB, 0xD7FB), + (0xF900, 0xFA6D), + (0xFA70, 0xFAD9), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFB1D, 0xFB1D), + (0xFB1F, 0xFB28), + (0xFB2A, 0xFB36), + (0xFB38, 0xFB3C), + (0xFB3E, 0xFB3E), + (0xFB40, 0xFB41), + (0xFB43, 0xFB44), + (0xFB46, 0xFBB1), + (0xFBD3, 0xFD3D), + (0xFD50, 0xFD8F), + (0xFD92, 0xFDC7), + (0xFDF0, 0xFDFB), + (0xFE70, 0xFE74), + (0xFE76, 0xFEFC), + (0xFF21, 0xFF3A), + (0xFF41, 0xFF5A), + (0xFF66, 0xFFBE), + (0xFFC2, 0xFFC7), + (0xFFCA, 0xFFCF), + (0xFFD2, 0xFFD7), + (0xFFDA, 0xFFDC), + (0x10000, 0x1000B), + (0x1000D, 0x10026), + (0x10028, 0x1003A), + (0x1003C, 0x1003D), + (0x1003F, 0x1004D), + (0x10050, 0x1005D), + (0x10080, 0x100FA), + (0x10140, 0x10174), + (0x10280, 0x1029C), + (0x102A0, 0x102D0), + (0x10300, 0x1031F), + (0x1032D, 0x1034A), + (0x10350, 0x10375), + (0x10380, 0x1039D), + (0x103A0, 0x103C3), + (0x103C8, 0x103CF), + (0x103D1, 0x103D5), + (0x10400, 0x1049D), + (0x104B0, 0x104D3), + (0x104D8, 0x104FB), + (0x10500, 0x10527), + (0x10530, 0x10563), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x105C0, 0x105F3), + (0x10600, 0x10736), + (0x10740, 0x10755), + (0x10760, 0x10767), + (0x10780, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x10800, 0x10805), + (0x10808, 0x10808), + (0x1080A, 0x10835), + (0x10837, 0x10838), + (0x1083C, 0x1083C), + (0x1083F, 0x10855), + (0x10860, 0x10876), + (0x10880, 0x1089E), + (0x108E0, 0x108F2), + (0x108F4, 0x108F5), + (0x10900, 0x10915), + (0x10920, 0x10939), + (0x10940, 0x10959), + (0x10980, 0x109B7), + (0x109BE, 0x109BF), + (0x10A00, 0x10A00), + (0x10A10, 0x10A13), + (0x10A15, 0x10A17), + (0x10A19, 0x10A35), + (0x10A60, 0x10A7C), + (0x10A80, 0x10A9C), + (0x10AC0, 0x10AC7), + (0x10AC9, 0x10AE4), + (0x10B00, 0x10B35), + (0x10B40, 0x10B55), + (0x10B60, 0x10B72), + (0x10B80, 0x10B91), + (0x10C00, 0x10C48), + (0x10C80, 0x10CB2), + (0x10CC0, 0x10CF2), + (0x10D00, 0x10D23), + (0x10D4A, 0x10D65), + (0x10D6F, 0x10D85), + (0x10E80, 0x10EA9), + (0x10EB0, 0x10EB1), + (0x10EC2, 0x10EC7), + (0x10ED9, 0x10EEE), + (0x10F00, 0x10F1C), + (0x10F27, 0x10F27), + (0x10F30, 0x10F45), + (0x10F70, 0x10F81), + (0x10FB0, 0x10FC4), + (0x10FE0, 0x10FF6), + (0x11003, 0x11037), + (0x11071, 0x11072), + (0x11075, 0x11075), + (0x11083, 0x110AF), + (0x110D0, 0x110E8), + (0x11103, 0x11126), + (0x11144, 0x11144), + (0x11147, 0x11147), + (0x11150, 0x11172), + (0x11176, 0x11176), + (0x11183, 0x111B2), + (0x111C1, 0x111C4), + (0x111DA, 0x111DA), + (0x111DC, 0x111DC), + (0x11200, 0x11211), + (0x11213, 0x1122B), + (0x1123F, 0x11240), + (0x11280, 0x11286), + (0x11288, 0x11288), + (0x1128A, 0x1128D), + (0x1128F, 0x1129D), + (0x1129F, 0x112A8), + (0x112B0, 0x112DE), + (0x11305, 0x1130C), + (0x1130F, 0x11310), + (0x11313, 0x11328), + (0x1132A, 0x11330), + (0x11332, 0x11333), + (0x11335, 0x11339), + (0x1133D, 0x1133D), + (0x11350, 0x11350), + (0x1135D, 0x11361), + (0x11380, 0x11389), + (0x1138B, 0x1138B), + (0x1138E, 0x1138E), + (0x11390, 0x113B5), + (0x113B7, 0x113B7), + (0x113D1, 0x113D1), + (0x113D3, 0x113D3), + (0x11400, 0x11434), + (0x11447, 0x1144A), + (0x1145F, 0x11461), + (0x11480, 0x114AF), + (0x114C4, 0x114C5), + (0x114C7, 0x114C7), + (0x11580, 0x115AE), + (0x115D8, 0x115DB), + (0x11600, 0x1162F), + (0x11644, 0x11644), + (0x11680, 0x116AA), + (0x116B8, 0x116B8), + (0x11700, 0x1171A), + (0x11740, 0x11746), + (0x11800, 0x1182B), + (0x118A0, 0x118DF), + (0x118FF, 0x11906), + (0x11909, 0x11909), + (0x1190C, 0x11913), + (0x11915, 0x11916), + (0x11918, 0x1192F), + (0x1193F, 0x1193F), + (0x11941, 0x11941), + (0x119A0, 0x119A7), + (0x119AA, 0x119D0), + (0x119E1, 0x119E1), + (0x119E3, 0x119E3), + (0x11A00, 0x11A00), + (0x11A0B, 0x11A32), + (0x11A3A, 0x11A3A), + (0x11A50, 0x11A50), + (0x11A5C, 0x11A89), + (0x11A9D, 0x11A9D), + (0x11AB0, 0x11AF8), + (0x11B0A, 0x11B0A), + (0x11BC0, 0x11BE0), + (0x11C00, 0x11C08), + (0x11C0A, 0x11C2E), + (0x11C40, 0x11C40), + (0x11C72, 0x11C8F), + (0x11D00, 0x11D06), + (0x11D08, 0x11D09), + (0x11D0B, 0x11D30), + (0x11D46, 0x11D46), + (0x11D60, 0x11D65), + (0x11D67, 0x11D68), + (0x11D6A, 0x11D89), + (0x11D98, 0x11D98), + (0x11DB0, 0x11DDB), + (0x11DF1, 0x11DF1), + (0x11EE0, 0x11EF2), + (0x11F02, 0x11F02), + (0x11F04, 0x11F10), + (0x11F12, 0x11F33), + (0x11FB0, 0x11FB0), + (0x12000, 0x12399), + (0x12400, 0x1246F), + (0x12475, 0x12543), + (0x12550, 0x12686), + (0x12F90, 0x12FF0), + (0x13000, 0x1342F), + (0x13441, 0x13446), + (0x13460, 0x143FA), + (0x14400, 0x14646), + (0x16100, 0x1611D), + (0x16800, 0x16A38), + (0x16A40, 0x16A5E), + (0x16A70, 0x16ABE), + (0x16AD0, 0x16AED), + (0x16B00, 0x16B2F), + (0x16B40, 0x16B43), + (0x16B63, 0x16B77), + (0x16B7D, 0x16B8F), + (0x16D40, 0x16D6C), + (0x16E40, 0x16E7F), + (0x16EA0, 0x16EB8), + (0x16EBB, 0x16ED3), + (0x16F00, 0x16F4A), + (0x16F50, 0x16F50), + (0x16F93, 0x16F9F), + (0x16FE0, 0x16FE1), + (0x16FE3, 0x16FE3), + (0x16FF2, 0x16FF6), + (0x17000, 0x18CDA), + (0x18CFF, 0x18D20), + (0x18D80, 0x18DF2), + (0x18E00, 0x19191), + (0x191A0, 0x191D2), + (0x1AFF0, 0x1AFF3), + (0x1AFF5, 0x1AFFB), + (0x1AFFD, 0x1AFFE), + (0x1B000, 0x1B128), + (0x1B132, 0x1B132), + (0x1B150, 0x1B152), + (0x1B155, 0x1B155), + (0x1B164, 0x1B168), + (0x1B170, 0x1B2FB), + (0x1BC00, 0x1BC6A), + (0x1BC70, 0x1BC7C), + (0x1BC80, 0x1BC88), + (0x1BC90, 0x1BC99), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D6C0), + (0x1D6C2, 0x1D6DA), + (0x1D6DC, 0x1D6FA), + (0x1D6FC, 0x1D714), + (0x1D716, 0x1D734), + (0x1D736, 0x1D74E), + (0x1D750, 0x1D76E), + (0x1D770, 0x1D788), + (0x1D78A, 0x1D7A8), + (0x1D7AA, 0x1D7C2), + (0x1D7C4, 0x1D7CB), + (0x1DF00, 0x1DF81), + (0x1DF90, 0x1DF96), + (0x1DFCD, 0x1DFFF), + (0x1E030, 0x1E06D), + (0x1E100, 0x1E12C), + (0x1E137, 0x1E13D), + (0x1E14E, 0x1E14E), + (0x1E290, 0x1E2AD), + (0x1E2C0, 0x1E2EB), + (0x1E4D0, 0x1E4EB), + (0x1E5D0, 0x1E5ED), + (0x1E5F0, 0x1E5F0), + (0x1E6C0, 0x1E6DE), + (0x1E6E0, 0x1E6E2), + (0x1E6E4, 0x1E6E5), + (0x1E6E7, 0x1E6ED), + (0x1E6F0, 0x1E6F4), + (0x1E6FE, 0x1E6FF), + (0x1E7E0, 0x1E7E6), + (0x1E7E8, 0x1E7EB), + (0x1E7ED, 0x1E7EE), + (0x1E7F0, 0x1E7FE), + (0x1E800, 0x1E8C4), + (0x1E900, 0x1E943), + (0x1E94B, 0x1E94B), + (0x1EE00, 0x1EE03), + (0x1EE05, 0x1EE1F), + (0x1EE21, 0x1EE22), + (0x1EE24, 0x1EE24), + (0x1EE27, 0x1EE27), + (0x1EE29, 0x1EE32), + (0x1EE34, 0x1EE37), + (0x1EE39, 0x1EE39), + (0x1EE3B, 0x1EE3B), + (0x1EE42, 0x1EE42), + (0x1EE47, 0x1EE47), + (0x1EE49, 0x1EE49), + (0x1EE4B, 0x1EE4B), + (0x1EE4D, 0x1EE4F), + (0x1EE51, 0x1EE52), + (0x1EE54, 0x1EE54), + (0x1EE57, 0x1EE57), + (0x1EE59, 0x1EE59), + (0x1EE5B, 0x1EE5B), + (0x1EE5D, 0x1EE5D), + (0x1EE5F, 0x1EE5F), + (0x1EE61, 0x1EE62), + (0x1EE64, 0x1EE64), + (0x1EE67, 0x1EE6A), + (0x1EE6C, 0x1EE72), + (0x1EE74, 0x1EE77), + (0x1EE79, 0x1EE7C), + (0x1EE7E, 0x1EE7E), + (0x1EE80, 0x1EE89), + (0x1EE8B, 0x1EE9B), + (0x1EEA1, 0x1EEA3), + (0x1EEA5, 0x1EEA9), + (0x1EEAB, 0x1EEBB), + (0x20000, 0x2A6DF), + (0x2A700, 0x2B81E), + (0x2B820, 0x2CEAD), + (0x2CEB0, 0x2EBE0), + (0x2EBF0, 0x2EE5D), + (0x2F800, 0x2FA1D), + (0x30000, 0x3134A), + (0x31350, 0x33479), + (0x3D000, 0x3FC3F), +]; + +/// The code points with the binary property Ideographic, as ranges. +pub static BINARY_IDEOGRAPHIC: &[(u32, u32)] = &[ + (0x3006, 0x3007), + (0x3021, 0x3029), + (0x3038, 0x303A), + (0x3400, 0x4DBF), + (0x4E00, 0x9FFF), + (0xF900, 0xFA6D), + (0xFA70, 0xFAD9), + (0x16FE4, 0x16FE4), + (0x16FF2, 0x16FF6), + (0x17000, 0x18CDA), + (0x18CFF, 0x18D20), + (0x18D80, 0x18DF2), + (0x18E00, 0x19191), + (0x191A0, 0x191D2), + (0x1B170, 0x1B2FB), + (0x20000, 0x2A6DF), + (0x2A700, 0x2B81E), + (0x2B820, 0x2CEAD), + (0x2CEB0, 0x2EBE0), + (0x2EBF0, 0x2EE5D), + (0x2F800, 0x2FA1D), + (0x30000, 0x3134A), + (0x31350, 0x33479), + (0x3D000, 0x3FC3F), +]; + +/// The code points with the binary property Join_Control, as ranges. +pub static BINARY_JOIN_CONTROL: &[(u32, u32)] = &[(0x200C, 0x200D)]; + +/// The code points with the binary property Logical_Order_Exception, as ranges. +pub static BINARY_LOGICAL_ORDER_EXCEPTION: &[(u32, u32)] = &[ + (0xE40, 0xE44), + (0xEC0, 0xEC4), + (0x19B5, 0x19B7), + (0x19BA, 0x19BA), + (0xAAB5, 0xAAB6), + (0xAAB9, 0xAAB9), + (0xAABB, 0xAABC), +]; + +/// The code points with the binary property Lowercase, as ranges. +pub static BINARY_LOWERCASE: &[(u32, u32)] = &[ + (0x61, 0x7A), + (0xAA, 0xAA), + (0xB5, 0xB5), + (0xBA, 0xBA), + (0xDF, 0xF6), + (0xF8, 0xFF), + (0x101, 0x101), + (0x103, 0x103), + (0x105, 0x105), + (0x107, 0x107), + (0x109, 0x109), + (0x10B, 0x10B), + (0x10D, 0x10D), + (0x10F, 0x10F), + (0x111, 0x111), + (0x113, 0x113), + (0x115, 0x115), + (0x117, 0x117), + (0x119, 0x119), + (0x11B, 0x11B), + (0x11D, 0x11D), + (0x11F, 0x11F), + (0x121, 0x121), + (0x123, 0x123), + (0x125, 0x125), + (0x127, 0x127), + (0x129, 0x129), + (0x12B, 0x12B), + (0x12D, 0x12D), + (0x12F, 0x12F), + (0x131, 0x131), + (0x133, 0x133), + (0x135, 0x135), + (0x137, 0x138), + (0x13A, 0x13A), + (0x13C, 0x13C), + (0x13E, 0x13E), + (0x140, 0x140), + (0x142, 0x142), + (0x144, 0x144), + (0x146, 0x146), + (0x148, 0x149), + (0x14B, 0x14B), + (0x14D, 0x14D), + (0x14F, 0x14F), + (0x151, 0x151), + (0x153, 0x153), + (0x155, 0x155), + (0x157, 0x157), + (0x159, 0x159), + (0x15B, 0x15B), + (0x15D, 0x15D), + (0x15F, 0x15F), + (0x161, 0x161), + (0x163, 0x163), + (0x165, 0x165), + (0x167, 0x167), + (0x169, 0x169), + (0x16B, 0x16B), + (0x16D, 0x16D), + (0x16F, 0x16F), + (0x171, 0x171), + (0x173, 0x173), + (0x175, 0x175), + (0x177, 0x177), + (0x17A, 0x17A), + (0x17C, 0x17C), + (0x17E, 0x180), + (0x183, 0x183), + (0x185, 0x185), + (0x188, 0x188), + (0x18C, 0x18D), + (0x192, 0x192), + (0x195, 0x195), + (0x199, 0x19B), + (0x19E, 0x19E), + (0x1A1, 0x1A1), + (0x1A3, 0x1A3), + (0x1A5, 0x1A5), + (0x1A8, 0x1A8), + (0x1AA, 0x1AB), + (0x1AD, 0x1AD), + (0x1B0, 0x1B0), + (0x1B4, 0x1B4), + (0x1B6, 0x1B6), + (0x1B9, 0x1BA), + (0x1BD, 0x1BF), + (0x1C6, 0x1C6), + (0x1C9, 0x1C9), + (0x1CC, 0x1CC), + (0x1CE, 0x1CE), + (0x1D0, 0x1D0), + (0x1D2, 0x1D2), + (0x1D4, 0x1D4), + (0x1D6, 0x1D6), + (0x1D8, 0x1D8), + (0x1DA, 0x1DA), + (0x1DC, 0x1DD), + (0x1DF, 0x1DF), + (0x1E1, 0x1E1), + (0x1E3, 0x1E3), + (0x1E5, 0x1E5), + (0x1E7, 0x1E7), + (0x1E9, 0x1E9), + (0x1EB, 0x1EB), + (0x1ED, 0x1ED), + (0x1EF, 0x1F0), + (0x1F3, 0x1F3), + (0x1F5, 0x1F5), + (0x1F9, 0x1F9), + (0x1FB, 0x1FB), + (0x1FD, 0x1FD), + (0x1FF, 0x1FF), + (0x201, 0x201), + (0x203, 0x203), + (0x205, 0x205), + (0x207, 0x207), + (0x209, 0x209), + (0x20B, 0x20B), + (0x20D, 0x20D), + (0x20F, 0x20F), + (0x211, 0x211), + (0x213, 0x213), + (0x215, 0x215), + (0x217, 0x217), + (0x219, 0x219), + (0x21B, 0x21B), + (0x21D, 0x21D), + (0x21F, 0x21F), + (0x221, 0x221), + (0x223, 0x223), + (0x225, 0x225), + (0x227, 0x227), + (0x229, 0x229), + (0x22B, 0x22B), + (0x22D, 0x22D), + (0x22F, 0x22F), + (0x231, 0x231), + (0x233, 0x239), + (0x23C, 0x23C), + (0x23F, 0x240), + (0x242, 0x242), + (0x247, 0x247), + (0x249, 0x249), + (0x24B, 0x24B), + (0x24D, 0x24D), + (0x24F, 0x293), + (0x296, 0x2B8), + (0x2C0, 0x2C1), + (0x2E0, 0x2E4), + (0x345, 0x345), + (0x371, 0x371), + (0x373, 0x373), + (0x377, 0x377), + (0x37A, 0x37D), + (0x390, 0x390), + (0x3AC, 0x3CE), + (0x3D0, 0x3D1), + (0x3D5, 0x3D7), + (0x3D9, 0x3D9), + (0x3DB, 0x3DB), + (0x3DD, 0x3DD), + (0x3DF, 0x3DF), + (0x3E1, 0x3E1), + (0x3E3, 0x3E3), + (0x3E5, 0x3E5), + (0x3E7, 0x3E7), + (0x3E9, 0x3E9), + (0x3EB, 0x3EB), + (0x3ED, 0x3ED), + (0x3EF, 0x3F3), + (0x3F5, 0x3F5), + (0x3F8, 0x3F8), + (0x3FB, 0x3FC), + (0x430, 0x45F), + (0x461, 0x461), + (0x463, 0x463), + (0x465, 0x465), + (0x467, 0x467), + (0x469, 0x469), + (0x46B, 0x46B), + (0x46D, 0x46D), + (0x46F, 0x46F), + (0x471, 0x471), + (0x473, 0x473), + (0x475, 0x475), + (0x477, 0x477), + (0x479, 0x479), + (0x47B, 0x47B), + (0x47D, 0x47D), + (0x47F, 0x47F), + (0x481, 0x481), + (0x48B, 0x48B), + (0x48D, 0x48D), + (0x48F, 0x48F), + (0x491, 0x491), + (0x493, 0x493), + (0x495, 0x495), + (0x497, 0x497), + (0x499, 0x499), + (0x49B, 0x49B), + (0x49D, 0x49D), + (0x49F, 0x49F), + (0x4A1, 0x4A1), + (0x4A3, 0x4A3), + (0x4A5, 0x4A5), + (0x4A7, 0x4A7), + (0x4A9, 0x4A9), + (0x4AB, 0x4AB), + (0x4AD, 0x4AD), + (0x4AF, 0x4AF), + (0x4B1, 0x4B1), + (0x4B3, 0x4B3), + (0x4B5, 0x4B5), + (0x4B7, 0x4B7), + (0x4B9, 0x4B9), + (0x4BB, 0x4BB), + (0x4BD, 0x4BD), + (0x4BF, 0x4BF), + (0x4C2, 0x4C2), + (0x4C4, 0x4C4), + (0x4C6, 0x4C6), + (0x4C8, 0x4C8), + (0x4CA, 0x4CA), + (0x4CC, 0x4CC), + (0x4CE, 0x4CF), + (0x4D1, 0x4D1), + (0x4D3, 0x4D3), + (0x4D5, 0x4D5), + (0x4D7, 0x4D7), + (0x4D9, 0x4D9), + (0x4DB, 0x4DB), + (0x4DD, 0x4DD), + (0x4DF, 0x4DF), + (0x4E1, 0x4E1), + (0x4E3, 0x4E3), + (0x4E5, 0x4E5), + (0x4E7, 0x4E7), + (0x4E9, 0x4E9), + (0x4EB, 0x4EB), + (0x4ED, 0x4ED), + (0x4EF, 0x4EF), + (0x4F1, 0x4F1), + (0x4F3, 0x4F3), + (0x4F5, 0x4F5), + (0x4F7, 0x4F7), + (0x4F9, 0x4F9), + (0x4FB, 0x4FB), + (0x4FD, 0x4FD), + (0x4FF, 0x4FF), + (0x501, 0x501), + (0x503, 0x503), + (0x505, 0x505), + (0x507, 0x507), + (0x509, 0x509), + (0x50B, 0x50B), + (0x50D, 0x50D), + (0x50F, 0x50F), + (0x511, 0x511), + (0x513, 0x513), + (0x515, 0x515), + (0x517, 0x517), + (0x519, 0x519), + (0x51B, 0x51B), + (0x51D, 0x51D), + (0x51F, 0x51F), + (0x521, 0x521), + (0x523, 0x523), + (0x525, 0x525), + (0x527, 0x527), + (0x529, 0x529), + (0x52B, 0x52B), + (0x52D, 0x52D), + (0x52F, 0x52F), + (0x558, 0x558), + (0x560, 0x588), + (0x58B, 0x58C), + (0x10D0, 0x10FA), + (0x10FC, 0x10FF), + (0x13F8, 0x13FD), + (0x1C80, 0x1C88), + (0x1C8A, 0x1C8A), + (0x1D00, 0x1DBF), + (0x1E01, 0x1E01), + (0x1E03, 0x1E03), + (0x1E05, 0x1E05), + (0x1E07, 0x1E07), + (0x1E09, 0x1E09), + (0x1E0B, 0x1E0B), + (0x1E0D, 0x1E0D), + (0x1E0F, 0x1E0F), + (0x1E11, 0x1E11), + (0x1E13, 0x1E13), + (0x1E15, 0x1E15), + (0x1E17, 0x1E17), + (0x1E19, 0x1E19), + (0x1E1B, 0x1E1B), + (0x1E1D, 0x1E1D), + (0x1E1F, 0x1E1F), + (0x1E21, 0x1E21), + (0x1E23, 0x1E23), + (0x1E25, 0x1E25), + (0x1E27, 0x1E27), + (0x1E29, 0x1E29), + (0x1E2B, 0x1E2B), + (0x1E2D, 0x1E2D), + (0x1E2F, 0x1E2F), + (0x1E31, 0x1E31), + (0x1E33, 0x1E33), + (0x1E35, 0x1E35), + (0x1E37, 0x1E37), + (0x1E39, 0x1E39), + (0x1E3B, 0x1E3B), + (0x1E3D, 0x1E3D), + (0x1E3F, 0x1E3F), + (0x1E41, 0x1E41), + (0x1E43, 0x1E43), + (0x1E45, 0x1E45), + (0x1E47, 0x1E47), + (0x1E49, 0x1E49), + (0x1E4B, 0x1E4B), + (0x1E4D, 0x1E4D), + (0x1E4F, 0x1E4F), + (0x1E51, 0x1E51), + (0x1E53, 0x1E53), + (0x1E55, 0x1E55), + (0x1E57, 0x1E57), + (0x1E59, 0x1E59), + (0x1E5B, 0x1E5B), + (0x1E5D, 0x1E5D), + (0x1E5F, 0x1E5F), + (0x1E61, 0x1E61), + (0x1E63, 0x1E63), + (0x1E65, 0x1E65), + (0x1E67, 0x1E67), + (0x1E69, 0x1E69), + (0x1E6B, 0x1E6B), + (0x1E6D, 0x1E6D), + (0x1E6F, 0x1E6F), + (0x1E71, 0x1E71), + (0x1E73, 0x1E73), + (0x1E75, 0x1E75), + (0x1E77, 0x1E77), + (0x1E79, 0x1E79), + (0x1E7B, 0x1E7B), + (0x1E7D, 0x1E7D), + (0x1E7F, 0x1E7F), + (0x1E81, 0x1E81), + (0x1E83, 0x1E83), + (0x1E85, 0x1E85), + (0x1E87, 0x1E87), + (0x1E89, 0x1E89), + (0x1E8B, 0x1E8B), + (0x1E8D, 0x1E8D), + (0x1E8F, 0x1E8F), + (0x1E91, 0x1E91), + (0x1E93, 0x1E93), + (0x1E95, 0x1E9D), + (0x1E9F, 0x1E9F), + (0x1EA1, 0x1EA1), + (0x1EA3, 0x1EA3), + (0x1EA5, 0x1EA5), + (0x1EA7, 0x1EA7), + (0x1EA9, 0x1EA9), + (0x1EAB, 0x1EAB), + (0x1EAD, 0x1EAD), + (0x1EAF, 0x1EAF), + (0x1EB1, 0x1EB1), + (0x1EB3, 0x1EB3), + (0x1EB5, 0x1EB5), + (0x1EB7, 0x1EB7), + (0x1EB9, 0x1EB9), + (0x1EBB, 0x1EBB), + (0x1EBD, 0x1EBD), + (0x1EBF, 0x1EBF), + (0x1EC1, 0x1EC1), + (0x1EC3, 0x1EC3), + (0x1EC5, 0x1EC5), + (0x1EC7, 0x1EC7), + (0x1EC9, 0x1EC9), + (0x1ECB, 0x1ECB), + (0x1ECD, 0x1ECD), + (0x1ECF, 0x1ECF), + (0x1ED1, 0x1ED1), + (0x1ED3, 0x1ED3), + (0x1ED5, 0x1ED5), + (0x1ED7, 0x1ED7), + (0x1ED9, 0x1ED9), + (0x1EDB, 0x1EDB), + (0x1EDD, 0x1EDD), + (0x1EDF, 0x1EDF), + (0x1EE1, 0x1EE1), + (0x1EE3, 0x1EE3), + (0x1EE5, 0x1EE5), + (0x1EE7, 0x1EE7), + (0x1EE9, 0x1EE9), + (0x1EEB, 0x1EEB), + (0x1EED, 0x1EED), + (0x1EEF, 0x1EEF), + (0x1EF1, 0x1EF1), + (0x1EF3, 0x1EF3), + (0x1EF5, 0x1EF5), + (0x1EF7, 0x1EF7), + (0x1EF9, 0x1EF9), + (0x1EFB, 0x1EFB), + (0x1EFD, 0x1EFD), + (0x1EFF, 0x1F07), + (0x1F10, 0x1F15), + (0x1F20, 0x1F27), + (0x1F30, 0x1F37), + (0x1F40, 0x1F45), + (0x1F50, 0x1F57), + (0x1F60, 0x1F67), + (0x1F70, 0x1F7D), + (0x1F80, 0x1F87), + (0x1F90, 0x1F97), + (0x1FA0, 0x1FA7), + (0x1FB0, 0x1FB4), + (0x1FB6, 0x1FB7), + (0x1FBE, 0x1FBE), + (0x1FC2, 0x1FC4), + (0x1FC6, 0x1FC7), + (0x1FD0, 0x1FD3), + (0x1FD6, 0x1FD7), + (0x1FE0, 0x1FE7), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FF7), + (0x2071, 0x2071), + (0x207F, 0x207F), + (0x2090, 0x209F), + (0x210A, 0x210A), + (0x210E, 0x210F), + (0x2113, 0x2113), + (0x212F, 0x212F), + (0x2134, 0x2134), + (0x2139, 0x2139), + (0x213C, 0x213D), + (0x2146, 0x2149), + (0x214E, 0x214E), + (0x2170, 0x217F), + (0x2184, 0x2184), + (0x24D0, 0x24E9), + (0x2C30, 0x2C5F), + (0x2C61, 0x2C61), + (0x2C65, 0x2C66), + (0x2C68, 0x2C68), + (0x2C6A, 0x2C6A), + (0x2C6C, 0x2C6C), + (0x2C71, 0x2C71), + (0x2C73, 0x2C74), + (0x2C76, 0x2C7D), + (0x2C81, 0x2C81), + (0x2C83, 0x2C83), + (0x2C85, 0x2C85), + (0x2C87, 0x2C87), + (0x2C89, 0x2C89), + (0x2C8B, 0x2C8B), + (0x2C8D, 0x2C8D), + (0x2C8F, 0x2C8F), + (0x2C91, 0x2C91), + (0x2C93, 0x2C93), + (0x2C95, 0x2C95), + (0x2C97, 0x2C97), + (0x2C99, 0x2C99), + (0x2C9B, 0x2C9B), + (0x2C9D, 0x2C9D), + (0x2C9F, 0x2C9F), + (0x2CA1, 0x2CA1), + (0x2CA3, 0x2CA3), + (0x2CA5, 0x2CA5), + (0x2CA7, 0x2CA7), + (0x2CA9, 0x2CA9), + (0x2CAB, 0x2CAB), + (0x2CAD, 0x2CAD), + (0x2CAF, 0x2CAF), + (0x2CB1, 0x2CB1), + (0x2CB3, 0x2CB3), + (0x2CB5, 0x2CB5), + (0x2CB7, 0x2CB7), + (0x2CB9, 0x2CB9), + (0x2CBB, 0x2CBB), + (0x2CBD, 0x2CBD), + (0x2CBF, 0x2CBF), + (0x2CC1, 0x2CC1), + (0x2CC3, 0x2CC3), + (0x2CC5, 0x2CC5), + (0x2CC7, 0x2CC7), + (0x2CC9, 0x2CC9), + (0x2CCB, 0x2CCB), + (0x2CCD, 0x2CCD), + (0x2CCF, 0x2CCF), + (0x2CD1, 0x2CD1), + (0x2CD3, 0x2CD3), + (0x2CD5, 0x2CD5), + (0x2CD7, 0x2CD7), + (0x2CD9, 0x2CD9), + (0x2CDB, 0x2CDB), + (0x2CDD, 0x2CDD), + (0x2CDF, 0x2CDF), + (0x2CE1, 0x2CE1), + (0x2CE3, 0x2CE4), + (0x2CEC, 0x2CEC), + (0x2CEE, 0x2CEE), + (0x2CF3, 0x2CF3), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0xA641, 0xA641), + (0xA643, 0xA643), + (0xA645, 0xA645), + (0xA647, 0xA647), + (0xA649, 0xA649), + (0xA64B, 0xA64B), + (0xA64D, 0xA64D), + (0xA64F, 0xA64F), + (0xA651, 0xA651), + (0xA653, 0xA653), + (0xA655, 0xA655), + (0xA657, 0xA657), + (0xA659, 0xA659), + (0xA65B, 0xA65B), + (0xA65D, 0xA65D), + (0xA65F, 0xA65F), + (0xA661, 0xA661), + (0xA663, 0xA663), + (0xA665, 0xA665), + (0xA667, 0xA667), + (0xA669, 0xA669), + (0xA66B, 0xA66B), + (0xA66D, 0xA66D), + (0xA681, 0xA681), + (0xA683, 0xA683), + (0xA685, 0xA685), + (0xA687, 0xA687), + (0xA689, 0xA689), + (0xA68B, 0xA68B), + (0xA68D, 0xA68D), + (0xA68F, 0xA68F), + (0xA691, 0xA691), + (0xA693, 0xA693), + (0xA695, 0xA695), + (0xA697, 0xA697), + (0xA699, 0xA699), + (0xA69B, 0xA69D), + (0xA723, 0xA723), + (0xA725, 0xA725), + (0xA727, 0xA727), + (0xA729, 0xA729), + (0xA72B, 0xA72B), + (0xA72D, 0xA72D), + (0xA72F, 0xA731), + (0xA733, 0xA733), + (0xA735, 0xA735), + (0xA737, 0xA737), + (0xA739, 0xA739), + (0xA73B, 0xA73B), + (0xA73D, 0xA73D), + (0xA73F, 0xA73F), + (0xA741, 0xA741), + (0xA743, 0xA743), + (0xA745, 0xA745), + (0xA747, 0xA747), + (0xA749, 0xA749), + (0xA74B, 0xA74B), + (0xA74D, 0xA74D), + (0xA74F, 0xA74F), + (0xA751, 0xA751), + (0xA753, 0xA753), + (0xA755, 0xA755), + (0xA757, 0xA757), + (0xA759, 0xA759), + (0xA75B, 0xA75B), + (0xA75D, 0xA75D), + (0xA75F, 0xA75F), + (0xA761, 0xA761), + (0xA763, 0xA763), + (0xA765, 0xA765), + (0xA767, 0xA767), + (0xA769, 0xA769), + (0xA76B, 0xA76B), + (0xA76D, 0xA76D), + (0xA76F, 0xA778), + (0xA77A, 0xA77A), + (0xA77C, 0xA77C), + (0xA77F, 0xA77F), + (0xA781, 0xA781), + (0xA783, 0xA783), + (0xA785, 0xA785), + (0xA787, 0xA787), + (0xA78C, 0xA78C), + (0xA78E, 0xA78E), + (0xA791, 0xA791), + (0xA793, 0xA795), + (0xA797, 0xA797), + (0xA799, 0xA799), + (0xA79B, 0xA79B), + (0xA79D, 0xA79D), + (0xA79F, 0xA79F), + (0xA7A1, 0xA7A1), + (0xA7A3, 0xA7A3), + (0xA7A5, 0xA7A5), + (0xA7A7, 0xA7A7), + (0xA7A9, 0xA7A9), + (0xA7AF, 0xA7AF), + (0xA7B5, 0xA7B5), + (0xA7B7, 0xA7B7), + (0xA7B9, 0xA7B9), + (0xA7BB, 0xA7BB), + (0xA7BD, 0xA7BD), + (0xA7BF, 0xA7BF), + (0xA7C1, 0xA7C1), + (0xA7C3, 0xA7C3), + (0xA7C8, 0xA7C8), + (0xA7CA, 0xA7CA), + (0xA7CD, 0xA7CD), + (0xA7CF, 0xA7CF), + (0xA7D1, 0xA7D1), + (0xA7D3, 0xA7D3), + (0xA7D5, 0xA7D5), + (0xA7D7, 0xA7D7), + (0xA7D9, 0xA7D9), + (0xA7DB, 0xA7DB), + (0xA7F1, 0xA7F4), + (0xA7F6, 0xA7F6), + (0xA7F8, 0xA7FA), + (0xAB30, 0xAB5A), + (0xAB5C, 0xAB69), + (0xAB70, 0xABBF), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFF41, 0xFF5A), + (0x10428, 0x1044F), + (0x104D8, 0x104FB), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x10780, 0x10780), + (0x10783, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x10CC0, 0x10CF2), + (0x10D70, 0x10D85), + (0x118C0, 0x118DF), + (0x16E60, 0x16E7F), + (0x16EBB, 0x16ED3), + (0x1D41A, 0x1D433), + (0x1D44E, 0x1D454), + (0x1D456, 0x1D467), + (0x1D482, 0x1D49B), + (0x1D4B6, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D4CF), + (0x1D4EA, 0x1D503), + (0x1D51E, 0x1D537), + (0x1D552, 0x1D56B), + (0x1D586, 0x1D59F), + (0x1D5BA, 0x1D5D3), + (0x1D5EE, 0x1D607), + (0x1D622, 0x1D63B), + (0x1D656, 0x1D66F), + (0x1D68A, 0x1D6A6), + (0x1D6C2, 0x1D6DA), + (0x1D6DC, 0x1D6E1), + (0x1D6FC, 0x1D714), + (0x1D716, 0x1D71B), + (0x1D736, 0x1D74E), + (0x1D750, 0x1D755), + (0x1D770, 0x1D788), + (0x1D78A, 0x1D78F), + (0x1D7AA, 0x1D7C2), + (0x1D7C4, 0x1D7C9), + (0x1D7CB, 0x1D7CB), + (0x1DF00, 0x1DF09), + (0x1DF0B, 0x1DF3F), + (0x1DF41, 0x1DF47), + (0x1DF49, 0x1DF49), + (0x1DF4B, 0x1DF4C), + (0x1DF4E, 0x1DF50), + (0x1DF52, 0x1DF67), + (0x1DF69, 0x1DF69), + (0x1DF6B, 0x1DF6B), + (0x1DF6D, 0x1DF6D), + (0x1DF6F, 0x1DF71), + (0x1DF73, 0x1DF73), + (0x1DF75, 0x1DF75), + (0x1DF77, 0x1DF77), + (0x1DF79, 0x1DF79), + (0x1DF7B, 0x1DF7B), + (0x1DF7D, 0x1DF7D), + (0x1DF7F, 0x1DF7F), + (0x1DF90, 0x1DF96), + (0x1DFCD, 0x1DFFF), + (0x1E030, 0x1E06D), + (0x1E922, 0x1E943), +]; + +/// The code points with the binary property Math, as ranges. +pub static BINARY_MATH: &[(u32, u32)] = &[ + (0x2B, 0x2B), + (0x3C, 0x3E), + (0x5E, 0x5E), + (0x7C, 0x7C), + (0x7E, 0x7E), + (0xAC, 0xAC), + (0xB1, 0xB1), + (0xD7, 0xD7), + (0xF7, 0xF7), + (0x3D0, 0x3D2), + (0x3D5, 0x3D5), + (0x3F0, 0x3F1), + (0x3F4, 0x3F6), + (0x606, 0x608), + (0x2016, 0x2016), + (0x2032, 0x2034), + (0x2040, 0x2040), + (0x2044, 0x2044), + (0x2052, 0x2052), + (0x2061, 0x2064), + (0x207A, 0x207E), + (0x208A, 0x208E), + (0x20D0, 0x20DC), + (0x20E1, 0x20E1), + (0x20E5, 0x20E6), + (0x20EB, 0x20EF), + (0x2102, 0x2102), + (0x2107, 0x2107), + (0x210A, 0x2113), + (0x2115, 0x2115), + (0x2118, 0x211D), + (0x2124, 0x2124), + (0x2128, 0x2129), + (0x212C, 0x212D), + (0x212F, 0x2131), + (0x2133, 0x2138), + (0x213C, 0x2149), + (0x214B, 0x214B), + (0x2190, 0x21A7), + (0x21A9, 0x21AE), + (0x21B0, 0x21B1), + (0x21B6, 0x21B7), + (0x21BC, 0x21DB), + (0x21DD, 0x21DD), + (0x21E4, 0x21E5), + (0x21F4, 0x22FF), + (0x2308, 0x230B), + (0x2320, 0x2321), + (0x237C, 0x237C), + (0x239B, 0x23B5), + (0x23B7, 0x23B7), + (0x23D0, 0x23D0), + (0x23DC, 0x23E2), + (0x25A0, 0x25A1), + (0x25AE, 0x25B7), + (0x25BC, 0x25C1), + (0x25C6, 0x25C7), + (0x25CA, 0x25CB), + (0x25CF, 0x25D3), + (0x25E2, 0x25E2), + (0x25E4, 0x25E4), + (0x25E7, 0x25EC), + (0x25F8, 0x25FF), + (0x2605, 0x2606), + (0x2640, 0x2640), + (0x2642, 0x2642), + (0x2660, 0x2663), + (0x266D, 0x266F), + (0x27C0, 0x27FF), + (0x2900, 0x2AFF), + (0x2B30, 0x2B44), + (0x2B47, 0x2B4C), + (0xFB29, 0xFB29), + (0xFE61, 0xFE66), + (0xFE68, 0xFE68), + (0xFF0B, 0xFF0B), + (0xFF1C, 0xFF1E), + (0xFF3C, 0xFF3C), + (0xFF3E, 0xFF3E), + (0xFF5C, 0xFF5C), + (0xFF5E, 0xFF5E), + (0xFFE2, 0xFFE2), + (0xFFE9, 0xFFEC), + (0x10D8E, 0x10D8F), + (0x1CEDD, 0x1CEDF), + (0x1CEF0, 0x1CEFD), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D7CB), + (0x1D7CE, 0x1D7FF), + (0x1DB00, 0x1DB1C), + (0x1EE00, 0x1EE03), + (0x1EE05, 0x1EE1F), + (0x1EE21, 0x1EE22), + (0x1EE24, 0x1EE24), + (0x1EE27, 0x1EE27), + (0x1EE29, 0x1EE32), + (0x1EE34, 0x1EE37), + (0x1EE39, 0x1EE39), + (0x1EE3B, 0x1EE3B), + (0x1EE42, 0x1EE42), + (0x1EE47, 0x1EE47), + (0x1EE49, 0x1EE49), + (0x1EE4B, 0x1EE4B), + (0x1EE4D, 0x1EE4F), + (0x1EE51, 0x1EE52), + (0x1EE54, 0x1EE54), + (0x1EE57, 0x1EE57), + (0x1EE59, 0x1EE59), + (0x1EE5B, 0x1EE5B), + (0x1EE5D, 0x1EE5D), + (0x1EE5F, 0x1EE5F), + (0x1EE61, 0x1EE62), + (0x1EE64, 0x1EE64), + (0x1EE67, 0x1EE6A), + (0x1EE6C, 0x1EE72), + (0x1EE74, 0x1EE77), + (0x1EE79, 0x1EE7C), + (0x1EE7E, 0x1EE7E), + (0x1EE80, 0x1EE89), + (0x1EE8B, 0x1EE9B), + (0x1EEA1, 0x1EEA3), + (0x1EEA5, 0x1EEA9), + (0x1EEAB, 0x1EEBB), + (0x1EEF0, 0x1EEF1), + (0x1F7DB, 0x1F7DB), + (0x1F7F1, 0x1F7FF), + (0x1F8D0, 0x1F8D8), +]; + +/// The code points with the binary property Noncharacter_Code_Point, as ranges. +pub static BINARY_NONCHARACTER_CODE_POINT: &[(u32, u32)] = &[ + (0xFDD0, 0xFDEF), + (0xFFFE, 0xFFFF), + (0x1FFFE, 0x1FFFF), + (0x2FFFE, 0x2FFFF), + (0x3FFFE, 0x3FFFF), + (0x4FFFE, 0x4FFFF), + (0x5FFFE, 0x5FFFF), + (0x6FFFE, 0x6FFFF), + (0x7FFFE, 0x7FFFF), + (0x8FFFE, 0x8FFFF), + (0x9FFFE, 0x9FFFF), + (0xAFFFE, 0xAFFFF), + (0xBFFFE, 0xBFFFF), + (0xCFFFE, 0xCFFFF), + (0xDFFFE, 0xDFFFF), + (0xEFFFE, 0xEFFFF), + (0xFFFFE, 0xFFFFF), + (0x10FFFE, 0x10FFFF), +]; + +/// The code points with the binary property Pattern_Syntax, as ranges. +pub static BINARY_PATTERN_SYNTAX: &[(u32, u32)] = &[ + (0x21, 0x2F), + (0x3A, 0x40), + (0x5B, 0x5E), + (0x60, 0x60), + (0x7B, 0x7E), + (0xA1, 0xA7), + (0xA9, 0xA9), + (0xAB, 0xAC), + (0xAE, 0xAE), + (0xB0, 0xB1), + (0xB6, 0xB6), + (0xBB, 0xBB), + (0xBF, 0xBF), + (0xD7, 0xD7), + (0xF7, 0xF7), + (0x2010, 0x2027), + (0x2030, 0x203E), + (0x2041, 0x2053), + (0x2055, 0x205E), + (0x2190, 0x245F), + (0x2500, 0x2775), + (0x2794, 0x2BFF), + (0x2E00, 0x2E7F), + (0x3001, 0x3003), + (0x3008, 0x3020), + (0x3030, 0x3030), + (0xFD3E, 0xFD3F), + (0xFE45, 0xFE46), +]; + +/// The code points with the binary property Pattern_White_Space, as ranges. +pub static BINARY_PATTERN_WHITE_SPACE: &[(u32, u32)] = &[ + (0x9, 0xD), + (0x20, 0x20), + (0x85, 0x85), + (0x200E, 0x200F), + (0x2028, 0x2029), +]; + +/// The code points with the binary property Quotation_Mark, as ranges. +pub static BINARY_QUOTATION_MARK: &[(u32, u32)] = &[ + (0x22, 0x22), + (0x27, 0x27), + (0xAB, 0xAB), + (0xBB, 0xBB), + (0x2018, 0x201F), + (0x2039, 0x203A), + (0x2E42, 0x2E42), + (0x300C, 0x300F), + (0x301D, 0x301F), + (0xFE41, 0xFE44), + (0xFF02, 0xFF02), + (0xFF07, 0xFF07), + (0xFF62, 0xFF63), +]; + +/// The code points with the binary property Radical, as ranges. +pub static BINARY_RADICAL: &[(u32, u32)] = &[(0x2E80, 0x2E99), (0x2E9B, 0x2EF3), (0x2F00, 0x2FD5)]; + +/// The code points with the binary property Regional_Indicator, as ranges. +pub static BINARY_REGIONAL_INDICATOR: &[(u32, u32)] = &[(0x1F1E6, 0x1F1FF)]; + +/// The code points with the binary property Sentence_Terminal, as ranges. +pub static BINARY_SENTENCE_TERMINAL: &[(u32, u32)] = &[ + (0x21, 0x21), + (0x2E, 0x2E), + (0x3F, 0x3F), + (0x589, 0x589), + (0x61D, 0x61F), + (0x6D4, 0x6D4), + (0x700, 0x702), + (0x7F9, 0x7F9), + (0x837, 0x837), + (0x839, 0x839), + (0x83D, 0x83E), + (0x964, 0x965), + (0x104A, 0x104B), + (0x1362, 0x1362), + (0x1367, 0x1368), + (0x166E, 0x166E), + (0x1735, 0x1736), + (0x17D4, 0x17D5), + (0x1803, 0x1803), + (0x1809, 0x1809), + (0x1944, 0x1945), + (0x1AA8, 0x1AAB), + (0x1B4E, 0x1B4F), + (0x1B5A, 0x1B5B), + (0x1B5E, 0x1B5F), + (0x1B7D, 0x1B7F), + (0x1C3B, 0x1C3C), + (0x1C7E, 0x1C7F), + (0x2024, 0x2024), + (0x203C, 0x203D), + (0x2047, 0x2049), + (0x2CF9, 0x2CFB), + (0x2E2E, 0x2E2E), + (0x2E3C, 0x2E3C), + (0x2E53, 0x2E54), + (0x2E60, 0x2E61), + (0x3002, 0x3002), + (0xA4FF, 0xA4FF), + (0xA60E, 0xA60F), + (0xA6F3, 0xA6F3), + (0xA6F7, 0xA6F7), + (0xA876, 0xA877), + (0xA8CE, 0xA8CF), + (0xA92F, 0xA92F), + (0xA9C8, 0xA9C9), + (0xAA5D, 0xAA5F), + (0xAAF0, 0xAAF1), + (0xABEB, 0xABEB), + (0xFE12, 0xFE12), + (0xFE15, 0xFE16), + (0xFE52, 0xFE52), + (0xFE56, 0xFE57), + (0xFF01, 0xFF01), + (0xFF0E, 0xFF0E), + (0xFF1F, 0xFF1F), + (0xFF61, 0xFF61), + (0x10A56, 0x10A57), + (0x10F55, 0x10F59), + (0x10F86, 0x10F89), + (0x11047, 0x11048), + (0x110BE, 0x110C1), + (0x11141, 0x11143), + (0x111C5, 0x111C6), + (0x111CD, 0x111CD), + (0x111DE, 0x111DF), + (0x11238, 0x11239), + (0x1123B, 0x1123C), + (0x112A9, 0x112A9), + (0x113D4, 0x113D5), + (0x1144B, 0x1144C), + (0x115C2, 0x115C3), + (0x115C9, 0x115D7), + (0x11641, 0x11642), + (0x1173C, 0x1173E), + (0x11944, 0x11944), + (0x11946, 0x11946), + (0x11A42, 0x11A43), + (0x11A9B, 0x11A9C), + (0x11C41, 0x11C42), + (0x11EF7, 0x11EF8), + (0x11F43, 0x11F44), + (0x16A6E, 0x16A6F), + (0x16AF5, 0x16AF5), + (0x16B37, 0x16B38), + (0x16B44, 0x16B44), + (0x16D6E, 0x16D6F), + (0x16E98, 0x16E98), + (0x1BC9F, 0x1BC9F), + (0x1DA88, 0x1DA88), +]; + +/// The code points with the binary property Soft_Dotted, as ranges. +pub static BINARY_SOFT_DOTTED: &[(u32, u32)] = &[ + (0x69, 0x6A), + (0x12F, 0x12F), + (0x249, 0x249), + (0x268, 0x268), + (0x29D, 0x29D), + (0x2B2, 0x2B2), + (0x3F3, 0x3F3), + (0x456, 0x456), + (0x458, 0x458), + (0x1D62, 0x1D62), + (0x1D96, 0x1D96), + (0x1DA4, 0x1DA4), + (0x1DA8, 0x1DA8), + (0x1E2D, 0x1E2D), + (0x1ECB, 0x1ECB), + (0x2071, 0x2071), + (0x2148, 0x2149), + (0x2C7C, 0x2C7C), + (0x1D422, 0x1D423), + (0x1D456, 0x1D457), + (0x1D48A, 0x1D48B), + (0x1D4BE, 0x1D4BF), + (0x1D4F2, 0x1D4F3), + (0x1D526, 0x1D527), + (0x1D55A, 0x1D55B), + (0x1D58E, 0x1D58F), + (0x1D5C2, 0x1D5C3), + (0x1D5F6, 0x1D5F7), + (0x1D62A, 0x1D62B), + (0x1D65E, 0x1D65F), + (0x1D692, 0x1D693), + (0x1DF1A, 0x1DF1A), + (0x1DF6F, 0x1DF71), + (0x1DFD9, 0x1DFD9), + (0x1E04C, 0x1E04D), + (0x1E068, 0x1E068), +]; + +/// The code points with the binary property Terminal_Punctuation, as ranges. +pub static BINARY_TERMINAL_PUNCTUATION: &[(u32, u32)] = &[ + (0x21, 0x21), + (0x2C, 0x2C), + (0x2E, 0x2E), + (0x3A, 0x3B), + (0x3F, 0x3F), + (0x37E, 0x37E), + (0x387, 0x387), + (0x589, 0x589), + (0x5C3, 0x5C3), + (0x60C, 0x60C), + (0x61B, 0x61B), + (0x61D, 0x61F), + (0x6D4, 0x6D4), + (0x700, 0x70A), + (0x70C, 0x70C), + (0x7F8, 0x7F9), + (0x830, 0x835), + (0x837, 0x83E), + (0x85E, 0x85E), + (0x964, 0x965), + (0xE5A, 0xE5B), + (0xF08, 0xF08), + (0xF0D, 0xF12), + (0x104A, 0x104B), + (0x1361, 0x1368), + (0x166E, 0x166E), + (0x16EB, 0x16ED), + (0x1735, 0x1736), + (0x17D4, 0x17D6), + (0x17DA, 0x17DA), + (0x1802, 0x1805), + (0x1808, 0x1809), + (0x1944, 0x1945), + (0x1AA8, 0x1AAB), + (0x1B4E, 0x1B4F), + (0x1B5A, 0x1B5B), + (0x1B5D, 0x1B5F), + (0x1B7D, 0x1B7F), + (0x1C3B, 0x1C3F), + (0x1C7E, 0x1C7F), + (0x2024, 0x2024), + (0x203C, 0x203D), + (0x2047, 0x2049), + (0x2CF9, 0x2CFB), + (0x2E2E, 0x2E2E), + (0x2E3C, 0x2E3C), + (0x2E41, 0x2E41), + (0x2E4C, 0x2E4C), + (0x2E4E, 0x2E4F), + (0x2E53, 0x2E54), + (0x2E60, 0x2E61), + (0x3001, 0x3002), + (0xA4FE, 0xA4FF), + (0xA60D, 0xA60F), + (0xA6F3, 0xA6F7), + (0xA876, 0xA877), + (0xA8CE, 0xA8CF), + (0xA92F, 0xA92F), + (0xA9C7, 0xA9C9), + (0xAA5D, 0xAA5F), + (0xAADF, 0xAADF), + (0xAAF0, 0xAAF1), + (0xABEB, 0xABEB), + (0xFE12, 0xFE12), + (0xFE15, 0xFE16), + (0xFE50, 0xFE52), + (0xFE54, 0xFE57), + (0xFF01, 0xFF01), + (0xFF0C, 0xFF0C), + (0xFF0E, 0xFF0E), + (0xFF1A, 0xFF1B), + (0xFF1F, 0xFF1F), + (0xFF61, 0xFF61), + (0xFF64, 0xFF64), + (0x1039F, 0x1039F), + (0x103D0, 0x103D0), + (0x10857, 0x10857), + (0x1091F, 0x1091F), + (0x10A56, 0x10A57), + (0x10AF0, 0x10AF5), + (0x10B3A, 0x10B3F), + (0x10B99, 0x10B9C), + (0x10F55, 0x10F59), + (0x10F86, 0x10F89), + (0x11047, 0x1104D), + (0x110BE, 0x110C1), + (0x11141, 0x11143), + (0x111C5, 0x111C6), + (0x111CD, 0x111CD), + (0x111DE, 0x111DF), + (0x11238, 0x1123C), + (0x112A9, 0x112A9), + (0x113D4, 0x113D5), + (0x1144B, 0x1144D), + (0x1145A, 0x1145B), + (0x115C2, 0x115C5), + (0x115C9, 0x115D7), + (0x11641, 0x11642), + (0x1173C, 0x1173E), + (0x11944, 0x11944), + (0x11946, 0x11946), + (0x11A42, 0x11A43), + (0x11A9B, 0x11A9C), + (0x11AA1, 0x11AA2), + (0x11C41, 0x11C43), + (0x11C71, 0x11C71), + (0x11EF7, 0x11EF8), + (0x11F43, 0x11F44), + (0x12470, 0x12474), + (0x16A6E, 0x16A6F), + (0x16AF5, 0x16AF5), + (0x16B37, 0x16B39), + (0x16B44, 0x16B44), + (0x16D6E, 0x16D6F), + (0x16E97, 0x16E98), + (0x1BC9F, 0x1BC9F), + (0x1DA87, 0x1DA8A), +]; + +/// The code points with the binary property Unified_Ideograph, as ranges. +pub static BINARY_UNIFIED_IDEOGRAPH: &[(u32, u32)] = &[ + (0x3400, 0x4DBF), + (0x4E00, 0x9FFF), + (0xFA0E, 0xFA0F), + (0xFA11, 0xFA11), + (0xFA13, 0xFA14), + (0xFA1F, 0xFA1F), + (0xFA21, 0xFA21), + (0xFA23, 0xFA24), + (0xFA27, 0xFA29), + (0x20000, 0x2A6DF), + (0x2A700, 0x2B81E), + (0x2B820, 0x2CEAD), + (0x2CEB0, 0x2EBE0), + (0x2EBF0, 0x2EE5D), + (0x30000, 0x3134A), + (0x31350, 0x33479), +]; + +/// The code points with the binary property Uppercase, as ranges. +pub static BINARY_UPPERCASE: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0xC0, 0xD6), + (0xD8, 0xDE), + (0x100, 0x100), + (0x102, 0x102), + (0x104, 0x104), + (0x106, 0x106), + (0x108, 0x108), + (0x10A, 0x10A), + (0x10C, 0x10C), + (0x10E, 0x10E), + (0x110, 0x110), + (0x112, 0x112), + (0x114, 0x114), + (0x116, 0x116), + (0x118, 0x118), + (0x11A, 0x11A), + (0x11C, 0x11C), + (0x11E, 0x11E), + (0x120, 0x120), + (0x122, 0x122), + (0x124, 0x124), + (0x126, 0x126), + (0x128, 0x128), + (0x12A, 0x12A), + (0x12C, 0x12C), + (0x12E, 0x12E), + (0x130, 0x130), + (0x132, 0x132), + (0x134, 0x134), + (0x136, 0x136), + (0x139, 0x139), + (0x13B, 0x13B), + (0x13D, 0x13D), + (0x13F, 0x13F), + (0x141, 0x141), + (0x143, 0x143), + (0x145, 0x145), + (0x147, 0x147), + (0x14A, 0x14A), + (0x14C, 0x14C), + (0x14E, 0x14E), + (0x150, 0x150), + (0x152, 0x152), + (0x154, 0x154), + (0x156, 0x156), + (0x158, 0x158), + (0x15A, 0x15A), + (0x15C, 0x15C), + (0x15E, 0x15E), + (0x160, 0x160), + (0x162, 0x162), + (0x164, 0x164), + (0x166, 0x166), + (0x168, 0x168), + (0x16A, 0x16A), + (0x16C, 0x16C), + (0x16E, 0x16E), + (0x170, 0x170), + (0x172, 0x172), + (0x174, 0x174), + (0x176, 0x176), + (0x178, 0x179), + (0x17B, 0x17B), + (0x17D, 0x17D), + (0x181, 0x182), + (0x184, 0x184), + (0x186, 0x187), + (0x189, 0x18B), + (0x18E, 0x191), + (0x193, 0x194), + (0x196, 0x198), + (0x19C, 0x19D), + (0x19F, 0x1A0), + (0x1A2, 0x1A2), + (0x1A4, 0x1A4), + (0x1A6, 0x1A7), + (0x1A9, 0x1A9), + (0x1AC, 0x1AC), + (0x1AE, 0x1AF), + (0x1B1, 0x1B3), + (0x1B5, 0x1B5), + (0x1B7, 0x1B8), + (0x1BC, 0x1BC), + (0x1C4, 0x1C4), + (0x1C7, 0x1C7), + (0x1CA, 0x1CA), + (0x1CD, 0x1CD), + (0x1CF, 0x1CF), + (0x1D1, 0x1D1), + (0x1D3, 0x1D3), + (0x1D5, 0x1D5), + (0x1D7, 0x1D7), + (0x1D9, 0x1D9), + (0x1DB, 0x1DB), + (0x1DE, 0x1DE), + (0x1E0, 0x1E0), + (0x1E2, 0x1E2), + (0x1E4, 0x1E4), + (0x1E6, 0x1E6), + (0x1E8, 0x1E8), + (0x1EA, 0x1EA), + (0x1EC, 0x1EC), + (0x1EE, 0x1EE), + (0x1F1, 0x1F1), + (0x1F4, 0x1F4), + (0x1F6, 0x1F8), + (0x1FA, 0x1FA), + (0x1FC, 0x1FC), + (0x1FE, 0x1FE), + (0x200, 0x200), + (0x202, 0x202), + (0x204, 0x204), + (0x206, 0x206), + (0x208, 0x208), + (0x20A, 0x20A), + (0x20C, 0x20C), + (0x20E, 0x20E), + (0x210, 0x210), + (0x212, 0x212), + (0x214, 0x214), + (0x216, 0x216), + (0x218, 0x218), + (0x21A, 0x21A), + (0x21C, 0x21C), + (0x21E, 0x21E), + (0x220, 0x220), + (0x222, 0x222), + (0x224, 0x224), + (0x226, 0x226), + (0x228, 0x228), + (0x22A, 0x22A), + (0x22C, 0x22C), + (0x22E, 0x22E), + (0x230, 0x230), + (0x232, 0x232), + (0x23A, 0x23B), + (0x23D, 0x23E), + (0x241, 0x241), + (0x243, 0x246), + (0x248, 0x248), + (0x24A, 0x24A), + (0x24C, 0x24C), + (0x24E, 0x24E), + (0x370, 0x370), + (0x372, 0x372), + (0x376, 0x376), + (0x37F, 0x37F), + (0x386, 0x386), + (0x388, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x38F), + (0x391, 0x3A1), + (0x3A3, 0x3AB), + (0x3CF, 0x3CF), + (0x3D2, 0x3D4), + (0x3D8, 0x3D8), + (0x3DA, 0x3DA), + (0x3DC, 0x3DC), + (0x3DE, 0x3DE), + (0x3E0, 0x3E0), + (0x3E2, 0x3E2), + (0x3E4, 0x3E4), + (0x3E6, 0x3E6), + (0x3E8, 0x3E8), + (0x3EA, 0x3EA), + (0x3EC, 0x3EC), + (0x3EE, 0x3EE), + (0x3F4, 0x3F4), + (0x3F7, 0x3F7), + (0x3F9, 0x3FA), + (0x3FD, 0x42F), + (0x460, 0x460), + (0x462, 0x462), + (0x464, 0x464), + (0x466, 0x466), + (0x468, 0x468), + (0x46A, 0x46A), + (0x46C, 0x46C), + (0x46E, 0x46E), + (0x470, 0x470), + (0x472, 0x472), + (0x474, 0x474), + (0x476, 0x476), + (0x478, 0x478), + (0x47A, 0x47A), + (0x47C, 0x47C), + (0x47E, 0x47E), + (0x480, 0x480), + (0x48A, 0x48A), + (0x48C, 0x48C), + (0x48E, 0x48E), + (0x490, 0x490), + (0x492, 0x492), + (0x494, 0x494), + (0x496, 0x496), + (0x498, 0x498), + (0x49A, 0x49A), + (0x49C, 0x49C), + (0x49E, 0x49E), + (0x4A0, 0x4A0), + (0x4A2, 0x4A2), + (0x4A4, 0x4A4), + (0x4A6, 0x4A6), + (0x4A8, 0x4A8), + (0x4AA, 0x4AA), + (0x4AC, 0x4AC), + (0x4AE, 0x4AE), + (0x4B0, 0x4B0), + (0x4B2, 0x4B2), + (0x4B4, 0x4B4), + (0x4B6, 0x4B6), + (0x4B8, 0x4B8), + (0x4BA, 0x4BA), + (0x4BC, 0x4BC), + (0x4BE, 0x4BE), + (0x4C0, 0x4C1), + (0x4C3, 0x4C3), + (0x4C5, 0x4C5), + (0x4C7, 0x4C7), + (0x4C9, 0x4C9), + (0x4CB, 0x4CB), + (0x4CD, 0x4CD), + (0x4D0, 0x4D0), + (0x4D2, 0x4D2), + (0x4D4, 0x4D4), + (0x4D6, 0x4D6), + (0x4D8, 0x4D8), + (0x4DA, 0x4DA), + (0x4DC, 0x4DC), + (0x4DE, 0x4DE), + (0x4E0, 0x4E0), + (0x4E2, 0x4E2), + (0x4E4, 0x4E4), + (0x4E6, 0x4E6), + (0x4E8, 0x4E8), + (0x4EA, 0x4EA), + (0x4EC, 0x4EC), + (0x4EE, 0x4EE), + (0x4F0, 0x4F0), + (0x4F2, 0x4F2), + (0x4F4, 0x4F4), + (0x4F6, 0x4F6), + (0x4F8, 0x4F8), + (0x4FA, 0x4FA), + (0x4FC, 0x4FC), + (0x4FE, 0x4FE), + (0x500, 0x500), + (0x502, 0x502), + (0x504, 0x504), + (0x506, 0x506), + (0x508, 0x508), + (0x50A, 0x50A), + (0x50C, 0x50C), + (0x50E, 0x50E), + (0x510, 0x510), + (0x512, 0x512), + (0x514, 0x514), + (0x516, 0x516), + (0x518, 0x518), + (0x51A, 0x51A), + (0x51C, 0x51C), + (0x51E, 0x51E), + (0x520, 0x520), + (0x522, 0x522), + (0x524, 0x524), + (0x526, 0x526), + (0x528, 0x528), + (0x52A, 0x52A), + (0x52C, 0x52C), + (0x52E, 0x52E), + (0x531, 0x556), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x13A0, 0x13F5), + (0x1C89, 0x1C89), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1E00, 0x1E00), + (0x1E02, 0x1E02), + (0x1E04, 0x1E04), + (0x1E06, 0x1E06), + (0x1E08, 0x1E08), + (0x1E0A, 0x1E0A), + (0x1E0C, 0x1E0C), + (0x1E0E, 0x1E0E), + (0x1E10, 0x1E10), + (0x1E12, 0x1E12), + (0x1E14, 0x1E14), + (0x1E16, 0x1E16), + (0x1E18, 0x1E18), + (0x1E1A, 0x1E1A), + (0x1E1C, 0x1E1C), + (0x1E1E, 0x1E1E), + (0x1E20, 0x1E20), + (0x1E22, 0x1E22), + (0x1E24, 0x1E24), + (0x1E26, 0x1E26), + (0x1E28, 0x1E28), + (0x1E2A, 0x1E2A), + (0x1E2C, 0x1E2C), + (0x1E2E, 0x1E2E), + (0x1E30, 0x1E30), + (0x1E32, 0x1E32), + (0x1E34, 0x1E34), + (0x1E36, 0x1E36), + (0x1E38, 0x1E38), + (0x1E3A, 0x1E3A), + (0x1E3C, 0x1E3C), + (0x1E3E, 0x1E3E), + (0x1E40, 0x1E40), + (0x1E42, 0x1E42), + (0x1E44, 0x1E44), + (0x1E46, 0x1E46), + (0x1E48, 0x1E48), + (0x1E4A, 0x1E4A), + (0x1E4C, 0x1E4C), + (0x1E4E, 0x1E4E), + (0x1E50, 0x1E50), + (0x1E52, 0x1E52), + (0x1E54, 0x1E54), + (0x1E56, 0x1E56), + (0x1E58, 0x1E58), + (0x1E5A, 0x1E5A), + (0x1E5C, 0x1E5C), + (0x1E5E, 0x1E5E), + (0x1E60, 0x1E60), + (0x1E62, 0x1E62), + (0x1E64, 0x1E64), + (0x1E66, 0x1E66), + (0x1E68, 0x1E68), + (0x1E6A, 0x1E6A), + (0x1E6C, 0x1E6C), + (0x1E6E, 0x1E6E), + (0x1E70, 0x1E70), + (0x1E72, 0x1E72), + (0x1E74, 0x1E74), + (0x1E76, 0x1E76), + (0x1E78, 0x1E78), + (0x1E7A, 0x1E7A), + (0x1E7C, 0x1E7C), + (0x1E7E, 0x1E7E), + (0x1E80, 0x1E80), + (0x1E82, 0x1E82), + (0x1E84, 0x1E84), + (0x1E86, 0x1E86), + (0x1E88, 0x1E88), + (0x1E8A, 0x1E8A), + (0x1E8C, 0x1E8C), + (0x1E8E, 0x1E8E), + (0x1E90, 0x1E90), + (0x1E92, 0x1E92), + (0x1E94, 0x1E94), + (0x1E9E, 0x1E9E), + (0x1EA0, 0x1EA0), + (0x1EA2, 0x1EA2), + (0x1EA4, 0x1EA4), + (0x1EA6, 0x1EA6), + (0x1EA8, 0x1EA8), + (0x1EAA, 0x1EAA), + (0x1EAC, 0x1EAC), + (0x1EAE, 0x1EAE), + (0x1EB0, 0x1EB0), + (0x1EB2, 0x1EB2), + (0x1EB4, 0x1EB4), + (0x1EB6, 0x1EB6), + (0x1EB8, 0x1EB8), + (0x1EBA, 0x1EBA), + (0x1EBC, 0x1EBC), + (0x1EBE, 0x1EBE), + (0x1EC0, 0x1EC0), + (0x1EC2, 0x1EC2), + (0x1EC4, 0x1EC4), + (0x1EC6, 0x1EC6), + (0x1EC8, 0x1EC8), + (0x1ECA, 0x1ECA), + (0x1ECC, 0x1ECC), + (0x1ECE, 0x1ECE), + (0x1ED0, 0x1ED0), + (0x1ED2, 0x1ED2), + (0x1ED4, 0x1ED4), + (0x1ED6, 0x1ED6), + (0x1ED8, 0x1ED8), + (0x1EDA, 0x1EDA), + (0x1EDC, 0x1EDC), + (0x1EDE, 0x1EDE), + (0x1EE0, 0x1EE0), + (0x1EE2, 0x1EE2), + (0x1EE4, 0x1EE4), + (0x1EE6, 0x1EE6), + (0x1EE8, 0x1EE8), + (0x1EEA, 0x1EEA), + (0x1EEC, 0x1EEC), + (0x1EEE, 0x1EEE), + (0x1EF0, 0x1EF0), + (0x1EF2, 0x1EF2), + (0x1EF4, 0x1EF4), + (0x1EF6, 0x1EF6), + (0x1EF8, 0x1EF8), + (0x1EFA, 0x1EFA), + (0x1EFC, 0x1EFC), + (0x1EFE, 0x1EFE), + (0x1F08, 0x1F0F), + (0x1F18, 0x1F1D), + (0x1F28, 0x1F2F), + (0x1F38, 0x1F3F), + (0x1F48, 0x1F4D), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F5F), + (0x1F68, 0x1F6F), + (0x1FB8, 0x1FBB), + (0x1FC8, 0x1FCB), + (0x1FD8, 0x1FDB), + (0x1FE8, 0x1FEC), + (0x1FF8, 0x1FFB), + (0x2102, 0x2102), + (0x2107, 0x2107), + (0x210B, 0x210D), + (0x2110, 0x2112), + (0x2115, 0x2115), + (0x2119, 0x211D), + (0x2124, 0x2124), + (0x2126, 0x2126), + (0x2128, 0x2128), + (0x212A, 0x212D), + (0x2130, 0x2133), + (0x213E, 0x213F), + (0x2145, 0x2145), + (0x2160, 0x216F), + (0x2183, 0x2183), + (0x24B6, 0x24CF), + (0x2C00, 0x2C2F), + (0x2C60, 0x2C60), + (0x2C62, 0x2C64), + (0x2C67, 0x2C67), + (0x2C69, 0x2C69), + (0x2C6B, 0x2C6B), + (0x2C6D, 0x2C70), + (0x2C72, 0x2C72), + (0x2C75, 0x2C75), + (0x2C7E, 0x2C80), + (0x2C82, 0x2C82), + (0x2C84, 0x2C84), + (0x2C86, 0x2C86), + (0x2C88, 0x2C88), + (0x2C8A, 0x2C8A), + (0x2C8C, 0x2C8C), + (0x2C8E, 0x2C8E), + (0x2C90, 0x2C90), + (0x2C92, 0x2C92), + (0x2C94, 0x2C94), + (0x2C96, 0x2C96), + (0x2C98, 0x2C98), + (0x2C9A, 0x2C9A), + (0x2C9C, 0x2C9C), + (0x2C9E, 0x2C9E), + (0x2CA0, 0x2CA0), + (0x2CA2, 0x2CA2), + (0x2CA4, 0x2CA4), + (0x2CA6, 0x2CA6), + (0x2CA8, 0x2CA8), + (0x2CAA, 0x2CAA), + (0x2CAC, 0x2CAC), + (0x2CAE, 0x2CAE), + (0x2CB0, 0x2CB0), + (0x2CB2, 0x2CB2), + (0x2CB4, 0x2CB4), + (0x2CB6, 0x2CB6), + (0x2CB8, 0x2CB8), + (0x2CBA, 0x2CBA), + (0x2CBC, 0x2CBC), + (0x2CBE, 0x2CBE), + (0x2CC0, 0x2CC0), + (0x2CC2, 0x2CC2), + (0x2CC4, 0x2CC4), + (0x2CC6, 0x2CC6), + (0x2CC8, 0x2CC8), + (0x2CCA, 0x2CCA), + (0x2CCC, 0x2CCC), + (0x2CCE, 0x2CCE), + (0x2CD0, 0x2CD0), + (0x2CD2, 0x2CD2), + (0x2CD4, 0x2CD4), + (0x2CD6, 0x2CD6), + (0x2CD8, 0x2CD8), + (0x2CDA, 0x2CDA), + (0x2CDC, 0x2CDC), + (0x2CDE, 0x2CDE), + (0x2CE0, 0x2CE0), + (0x2CE2, 0x2CE2), + (0x2CEB, 0x2CEB), + (0x2CED, 0x2CED), + (0x2CF2, 0x2CF2), + (0xA640, 0xA640), + (0xA642, 0xA642), + (0xA644, 0xA644), + (0xA646, 0xA646), + (0xA648, 0xA648), + (0xA64A, 0xA64A), + (0xA64C, 0xA64C), + (0xA64E, 0xA64E), + (0xA650, 0xA650), + (0xA652, 0xA652), + (0xA654, 0xA654), + (0xA656, 0xA656), + (0xA658, 0xA658), + (0xA65A, 0xA65A), + (0xA65C, 0xA65C), + (0xA65E, 0xA65E), + (0xA660, 0xA660), + (0xA662, 0xA662), + (0xA664, 0xA664), + (0xA666, 0xA666), + (0xA668, 0xA668), + (0xA66A, 0xA66A), + (0xA66C, 0xA66C), + (0xA680, 0xA680), + (0xA682, 0xA682), + (0xA684, 0xA684), + (0xA686, 0xA686), + (0xA688, 0xA688), + (0xA68A, 0xA68A), + (0xA68C, 0xA68C), + (0xA68E, 0xA68E), + (0xA690, 0xA690), + (0xA692, 0xA692), + (0xA694, 0xA694), + (0xA696, 0xA696), + (0xA698, 0xA698), + (0xA69A, 0xA69A), + (0xA722, 0xA722), + (0xA724, 0xA724), + (0xA726, 0xA726), + (0xA728, 0xA728), + (0xA72A, 0xA72A), + (0xA72C, 0xA72C), + (0xA72E, 0xA72E), + (0xA732, 0xA732), + (0xA734, 0xA734), + (0xA736, 0xA736), + (0xA738, 0xA738), + (0xA73A, 0xA73A), + (0xA73C, 0xA73C), + (0xA73E, 0xA73E), + (0xA740, 0xA740), + (0xA742, 0xA742), + (0xA744, 0xA744), + (0xA746, 0xA746), + (0xA748, 0xA748), + (0xA74A, 0xA74A), + (0xA74C, 0xA74C), + (0xA74E, 0xA74E), + (0xA750, 0xA750), + (0xA752, 0xA752), + (0xA754, 0xA754), + (0xA756, 0xA756), + (0xA758, 0xA758), + (0xA75A, 0xA75A), + (0xA75C, 0xA75C), + (0xA75E, 0xA75E), + (0xA760, 0xA760), + (0xA762, 0xA762), + (0xA764, 0xA764), + (0xA766, 0xA766), + (0xA768, 0xA768), + (0xA76A, 0xA76A), + (0xA76C, 0xA76C), + (0xA76E, 0xA76E), + (0xA779, 0xA779), + (0xA77B, 0xA77B), + (0xA77D, 0xA77E), + (0xA780, 0xA780), + (0xA782, 0xA782), + (0xA784, 0xA784), + (0xA786, 0xA786), + (0xA78B, 0xA78B), + (0xA78D, 0xA78D), + (0xA790, 0xA790), + (0xA792, 0xA792), + (0xA796, 0xA796), + (0xA798, 0xA798), + (0xA79A, 0xA79A), + (0xA79C, 0xA79C), + (0xA79E, 0xA79E), + (0xA7A0, 0xA7A0), + (0xA7A2, 0xA7A2), + (0xA7A4, 0xA7A4), + (0xA7A6, 0xA7A6), + (0xA7A8, 0xA7A8), + (0xA7AA, 0xA7AE), + (0xA7B0, 0xA7B4), + (0xA7B6, 0xA7B6), + (0xA7B8, 0xA7B8), + (0xA7BA, 0xA7BA), + (0xA7BC, 0xA7BC), + (0xA7BE, 0xA7BE), + (0xA7C0, 0xA7C0), + (0xA7C2, 0xA7C2), + (0xA7C4, 0xA7C7), + (0xA7C9, 0xA7C9), + (0xA7CB, 0xA7CC), + (0xA7CE, 0xA7CE), + (0xA7D0, 0xA7D0), + (0xA7D2, 0xA7D2), + (0xA7D4, 0xA7D4), + (0xA7D6, 0xA7D6), + (0xA7D8, 0xA7D8), + (0xA7DA, 0xA7DA), + (0xA7DC, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F5, 0xA7F5), + (0xAB6C, 0xAB6D), + (0xFF21, 0xFF3A), + (0x10400, 0x10427), + (0x104B0, 0x104D3), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10C80, 0x10CB2), + (0x10D50, 0x10D65), + (0x118A0, 0x118BF), + (0x16E40, 0x16E5F), + (0x16EA0, 0x16EB8), + (0x1D400, 0x1D419), + (0x1D434, 0x1D44D), + (0x1D468, 0x1D481), + (0x1D49C, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B5), + (0x1D4D0, 0x1D4E9), + (0x1D504, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D538, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D56C, 0x1D585), + (0x1D5A0, 0x1D5B9), + (0x1D5D4, 0x1D5ED), + (0x1D608, 0x1D621), + (0x1D63C, 0x1D655), + (0x1D670, 0x1D689), + (0x1D6A8, 0x1D6C0), + (0x1D6E2, 0x1D6FA), + (0x1D71C, 0x1D734), + (0x1D756, 0x1D76E), + (0x1D790, 0x1D7A8), + (0x1D7CA, 0x1D7CA), + (0x1DF40, 0x1DF40), + (0x1DF48, 0x1DF48), + (0x1DF4A, 0x1DF4A), + (0x1DF4D, 0x1DF4D), + (0x1DF51, 0x1DF51), + (0x1DF68, 0x1DF68), + (0x1DF6A, 0x1DF6A), + (0x1DF6C, 0x1DF6C), + (0x1DF6E, 0x1DF6E), + (0x1DF72, 0x1DF72), + (0x1DF74, 0x1DF74), + (0x1DF76, 0x1DF76), + (0x1DF78, 0x1DF78), + (0x1DF7A, 0x1DF7A), + (0x1DF7C, 0x1DF7C), + (0x1DF7E, 0x1DF7E), + (0x1E900, 0x1E921), + (0x1F130, 0x1F149), + (0x1F150, 0x1F169), + (0x1F170, 0x1F189), +]; + +/// The code points with the binary property Variation_Selector, as ranges. +pub static BINARY_VARIATION_SELECTOR: &[(u32, u32)] = &[ + (0x180B, 0x180D), + (0x180F, 0x180F), + (0xFE00, 0xFE0F), + (0xE0100, 0xE01EF), +]; + +/// The code points with the binary property White_Space, as ranges. +pub static BINARY_WHITE_SPACE: &[(u32, u32)] = &[ + (0x9, 0xD), + (0x20, 0x20), + (0x85, 0x85), + (0xA0, 0xA0), + (0x1680, 0x1680), + (0x2000, 0x200A), + (0x2028, 0x2029), + (0x202F, 0x202F), + (0x205F, 0x205F), + (0x3000, 0x3000), +]; + +/// The code points with the binary property XID_Continue, as ranges. +pub static BINARY_XID_CONTINUE: &[(u32, u32)] = &[ + (0x30, 0x39), + (0x41, 0x5A), + (0x5F, 0x5F), + (0x61, 0x7A), + (0xAA, 0xAA), + (0xB5, 0xB5), + (0xB7, 0xB7), + (0xBA, 0xBA), + (0xC0, 0xD6), + (0xD8, 0xF6), + (0xF8, 0x2C1), + (0x2C6, 0x2D1), + (0x2E0, 0x2E4), + (0x2EC, 0x2EC), + (0x2EE, 0x2EE), + (0x300, 0x374), + (0x376, 0x377), + (0x37B, 0x37D), + (0x37F, 0x37F), + (0x386, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x3A1), + (0x3A3, 0x3F5), + (0x3F7, 0x481), + (0x483, 0x487), + (0x48A, 0x52F), + (0x531, 0x556), + (0x558, 0x559), + (0x560, 0x588), + (0x58B, 0x58C), + (0x591, 0x5BD), + (0x5BF, 0x5BF), + (0x5C1, 0x5C2), + (0x5C4, 0x5C5), + (0x5C7, 0x5C9), + (0x5D0, 0x5EA), + (0x5EF, 0x5F2), + (0x610, 0x61A), + (0x620, 0x669), + (0x66E, 0x6D3), + (0x6D5, 0x6DC), + (0x6DF, 0x6E8), + (0x6EA, 0x6FC), + (0x6FF, 0x6FF), + (0x710, 0x74A), + (0x74D, 0x7B1), + (0x7C0, 0x7F5), + (0x7FA, 0x7FA), + (0x7FD, 0x7FD), + (0x800, 0x82D), + (0x840, 0x85B), + (0x860, 0x86A), + (0x870, 0x887), + (0x889, 0x88F), + (0x897, 0x8E1), + (0x8E3, 0x963), + (0x966, 0x96F), + (0x971, 0x983), + (0x985, 0x98C), + (0x98F, 0x990), + (0x993, 0x9A8), + (0x9AA, 0x9B0), + (0x9B2, 0x9B2), + (0x9B6, 0x9B9), + (0x9BC, 0x9C4), + (0x9C7, 0x9C8), + (0x9CB, 0x9CE), + (0x9D7, 0x9D7), + (0x9DC, 0x9DD), + (0x9DF, 0x9E3), + (0x9E6, 0x9F1), + (0x9FC, 0x9FC), + (0x9FE, 0x9FE), + (0xA01, 0xA03), + (0xA05, 0xA0A), + (0xA0F, 0xA10), + (0xA13, 0xA28), + (0xA2A, 0xA30), + (0xA32, 0xA33), + (0xA35, 0xA36), + (0xA38, 0xA39), + (0xA3C, 0xA3C), + (0xA3E, 0xA42), + (0xA47, 0xA48), + (0xA4B, 0xA4D), + (0xA51, 0xA51), + (0xA59, 0xA5C), + (0xA5E, 0xA5E), + (0xA66, 0xA75), + (0xA81, 0xA83), + (0xA85, 0xA8D), + (0xA8F, 0xA91), + (0xA93, 0xAA8), + (0xAAA, 0xAB0), + (0xAB2, 0xAB3), + (0xAB5, 0xAB9), + (0xABC, 0xAC5), + (0xAC7, 0xAC9), + (0xACB, 0xACD), + (0xAD0, 0xAD0), + (0xAE0, 0xAE3), + (0xAE6, 0xAEF), + (0xAF9, 0xAFF), + (0xB01, 0xB03), + (0xB05, 0xB0C), + (0xB0F, 0xB10), + (0xB13, 0xB28), + (0xB2A, 0xB30), + (0xB32, 0xB33), + (0xB35, 0xB39), + (0xB3C, 0xB44), + (0xB47, 0xB48), + (0xB4B, 0xB4D), + (0xB53, 0xB57), + (0xB5C, 0xB5D), + (0xB5F, 0xB63), + (0xB66, 0xB6F), + (0xB71, 0xB71), + (0xB82, 0xB83), + (0xB85, 0xB8A), + (0xB8E, 0xB90), + (0xB92, 0xB95), + (0xB99, 0xB9A), + (0xB9C, 0xB9C), + (0xB9E, 0xB9F), + (0xBA3, 0xBA4), + (0xBA8, 0xBAA), + (0xBAE, 0xBB9), + (0xBBE, 0xBC2), + (0xBC6, 0xBC8), + (0xBCA, 0xBCD), + (0xBD0, 0xBD0), + (0xBD7, 0xBD7), + (0xBE6, 0xBEF), + (0xC00, 0xC0C), + (0xC0E, 0xC10), + (0xC12, 0xC28), + (0xC2A, 0xC39), + (0xC3C, 0xC44), + (0xC46, 0xC48), + (0xC4A, 0xC4D), + (0xC55, 0xC56), + (0xC58, 0xC5A), + (0xC5C, 0xC5D), + (0xC60, 0xC63), + (0xC66, 0xC6F), + (0xC80, 0xC83), + (0xC85, 0xC8C), + (0xC8E, 0xC90), + (0xC92, 0xCA8), + (0xCAA, 0xCB3), + (0xCB5, 0xCB9), + (0xCBC, 0xCC4), + (0xCC6, 0xCC8), + (0xCCA, 0xCCD), + (0xCD5, 0xCD6), + (0xCDC, 0xCDE), + (0xCE0, 0xCE3), + (0xCE6, 0xCEF), + (0xCF1, 0xCF3), + (0xD00, 0xD0C), + (0xD0E, 0xD10), + (0xD12, 0xD44), + (0xD46, 0xD48), + (0xD4A, 0xD4E), + (0xD54, 0xD57), + (0xD5F, 0xD63), + (0xD66, 0xD6F), + (0xD7A, 0xD7F), + (0xD81, 0xD83), + (0xD85, 0xD96), + (0xD9A, 0xDB1), + (0xDB3, 0xDBB), + (0xDBD, 0xDBD), + (0xDC0, 0xDC6), + (0xDCA, 0xDCA), + (0xDCF, 0xDD4), + (0xDD6, 0xDD6), + (0xDD8, 0xDDF), + (0xDE6, 0xDEF), + (0xDF2, 0xDF3), + (0xE01, 0xE3A), + (0xE40, 0xE4E), + (0xE50, 0xE59), + (0xE81, 0xE82), + (0xE84, 0xE84), + (0xE86, 0xE8A), + (0xE8C, 0xEA3), + (0xEA5, 0xEA5), + (0xEA7, 0xEBD), + (0xEC0, 0xEC4), + (0xEC6, 0xEC6), + (0xEC8, 0xECE), + (0xED0, 0xED9), + (0xEDC, 0xEDF), + (0xF00, 0xF00), + (0xF18, 0xF19), + (0xF20, 0xF29), + (0xF35, 0xF35), + (0xF37, 0xF37), + (0xF39, 0xF39), + (0xF3E, 0xF47), + (0xF49, 0xF6C), + (0xF71, 0xF84), + (0xF86, 0xF97), + (0xF99, 0xFBC), + (0xFC6, 0xFC6), + (0x1000, 0x1049), + (0x1050, 0x109D), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10D0, 0x10FA), + (0x10FC, 0x1248), + (0x124A, 0x124D), + (0x1250, 0x1256), + (0x1258, 0x1258), + (0x125A, 0x125D), + (0x1260, 0x1288), + (0x128A, 0x128D), + (0x1290, 0x12B0), + (0x12B2, 0x12B5), + (0x12B8, 0x12BE), + (0x12C0, 0x12C0), + (0x12C2, 0x12C5), + (0x12C8, 0x12D6), + (0x12D8, 0x1310), + (0x1312, 0x1315), + (0x1318, 0x135A), + (0x135D, 0x135F), + (0x1369, 0x1371), + (0x1380, 0x138F), + (0x13A0, 0x13F5), + (0x13F8, 0x13FD), + (0x1401, 0x166C), + (0x166F, 0x167F), + (0x1681, 0x169A), + (0x16A0, 0x16EA), + (0x16EE, 0x16F8), + (0x1700, 0x1715), + (0x171F, 0x1734), + (0x1740, 0x1753), + (0x1760, 0x176C), + (0x176E, 0x1770), + (0x1772, 0x1773), + (0x1780, 0x17D3), + (0x17D7, 0x17D7), + (0x17DC, 0x17DD), + (0x17E0, 0x17E9), + (0x180B, 0x180D), + (0x180F, 0x1819), + (0x1820, 0x1878), + (0x1880, 0x18AA), + (0x18B0, 0x18F5), + (0x1900, 0x191E), + (0x1920, 0x192B), + (0x1930, 0x193B), + (0x1946, 0x196D), + (0x1970, 0x1974), + (0x1980, 0x19AB), + (0x19B0, 0x19C9), + (0x19D0, 0x19DA), + (0x1A00, 0x1A1B), + (0x1A20, 0x1A5E), + (0x1A60, 0x1A7C), + (0x1A7F, 0x1A89), + (0x1A90, 0x1A99), + (0x1AA7, 0x1AA7), + (0x1AB0, 0x1ABD), + (0x1ABF, 0x1AF0), + (0x1B00, 0x1B4C), + (0x1B50, 0x1B59), + (0x1B6B, 0x1B73), + (0x1B80, 0x1BF3), + (0x1C00, 0x1C37), + (0x1C40, 0x1C49), + (0x1C4D, 0x1C7D), + (0x1C80, 0x1C8A), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1CD0, 0x1CD2), + (0x1CD4, 0x1CFA), + (0x1D00, 0x1F15), + (0x1F18, 0x1F1D), + (0x1F20, 0x1F45), + (0x1F48, 0x1F4D), + (0x1F50, 0x1F57), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FBC), + (0x1FBE, 0x1FBE), + (0x1FC2, 0x1FC4), + (0x1FC6, 0x1FCC), + (0x1FD0, 0x1FD3), + (0x1FD6, 0x1FDB), + (0x1FE0, 0x1FEC), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FFC), + (0x200C, 0x200D), + (0x203F, 0x2040), + (0x2054, 0x2054), + (0x2071, 0x2071), + (0x207F, 0x207F), + (0x208F, 0x209F), + (0x20D0, 0x20DC), + (0x20E1, 0x20E1), + (0x20E5, 0x20F0), + (0x2102, 0x2102), + (0x2107, 0x2107), + (0x210A, 0x2113), + (0x2115, 0x2115), + (0x2118, 0x211D), + (0x2124, 0x2124), + (0x2126, 0x2126), + (0x2128, 0x2128), + (0x212A, 0x2139), + (0x213C, 0x213F), + (0x2145, 0x2149), + (0x214E, 0x214E), + (0x2160, 0x2188), + (0x2C00, 0x2CE4), + (0x2CEB, 0x2CF3), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0x2D30, 0x2D67), + (0x2D6F, 0x2D6F), + (0x2D7F, 0x2D96), + (0x2DA0, 0x2DA6), + (0x2DA8, 0x2DAE), + (0x2DB0, 0x2DB6), + (0x2DB8, 0x2DBE), + (0x2DC0, 0x2DC6), + (0x2DC8, 0x2DCE), + (0x2DD0, 0x2DD6), + (0x2DD8, 0x2DDE), + (0x2DE0, 0x2DFF), + (0x3005, 0x3007), + (0x3021, 0x302F), + (0x3031, 0x3035), + (0x3038, 0x303C), + (0x3041, 0x3096), + (0x3099, 0x309A), + (0x309D, 0x309F), + (0x30A1, 0x30FF), + (0x3105, 0x312F), + (0x3131, 0x318E), + (0x31A0, 0x31BF), + (0x31F0, 0x31FF), + (0x3400, 0x4DBF), + (0x4E00, 0xA48C), + (0xA4D0, 0xA4FD), + (0xA500, 0xA60C), + (0xA610, 0xA62B), + (0xA640, 0xA66F), + (0xA674, 0xA67D), + (0xA67F, 0xA6F1), + (0xA717, 0xA71F), + (0xA722, 0xA788), + (0xA78B, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F1, 0xA827), + (0xA82C, 0xA82C), + (0xA840, 0xA873), + (0xA880, 0xA8C5), + (0xA8D0, 0xA8D9), + (0xA8E0, 0xA8F7), + (0xA8FB, 0xA8FB), + (0xA8FD, 0xA92D), + (0xA930, 0xA953), + (0xA960, 0xA97C), + (0xA980, 0xA9C0), + (0xA9CF, 0xA9D9), + (0xA9E0, 0xA9FE), + (0xAA00, 0xAA36), + (0xAA40, 0xAA4D), + (0xAA50, 0xAA59), + (0xAA60, 0xAA76), + (0xAA7A, 0xAAC2), + (0xAADB, 0xAADD), + (0xAAE0, 0xAAEF), + (0xAAF2, 0xAAF6), + (0xAB01, 0xAB06), + (0xAB09, 0xAB0E), + (0xAB11, 0xAB16), + (0xAB20, 0xAB26), + (0xAB28, 0xAB2E), + (0xAB30, 0xAB5A), + (0xAB5C, 0xAB69), + (0xAB6C, 0xAB6D), + (0xAB70, 0xABEA), + (0xABEC, 0xABED), + (0xABF0, 0xABF9), + (0xAC00, 0xD7A3), + (0xD7B0, 0xD7C6), + (0xD7CB, 0xD7FB), + (0xF900, 0xFA6D), + (0xFA70, 0xFAD9), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFB1D, 0xFB28), + (0xFB2A, 0xFB36), + (0xFB38, 0xFB3C), + (0xFB3E, 0xFB3E), + (0xFB40, 0xFB41), + (0xFB43, 0xFB44), + (0xFB46, 0xFBB1), + (0xFBD3, 0xFC5D), + (0xFC64, 0xFD3D), + (0xFD50, 0xFD8F), + (0xFD92, 0xFDC7), + (0xFDF0, 0xFDF9), + (0xFE00, 0xFE0F), + (0xFE20, 0xFE2F), + (0xFE33, 0xFE34), + (0xFE4D, 0xFE4F), + (0xFE71, 0xFE71), + (0xFE73, 0xFE73), + (0xFE77, 0xFE77), + (0xFE79, 0xFE79), + (0xFE7B, 0xFE7B), + (0xFE7D, 0xFE7D), + (0xFE7F, 0xFEFC), + (0xFF10, 0xFF19), + (0xFF21, 0xFF3A), + (0xFF3F, 0xFF3F), + (0xFF41, 0xFF5A), + (0xFF65, 0xFFBE), + (0xFFC2, 0xFFC7), + (0xFFCA, 0xFFCF), + (0xFFD2, 0xFFD7), + (0xFFDA, 0xFFDC), + (0x10000, 0x1000B), + (0x1000D, 0x10026), + (0x10028, 0x1003A), + (0x1003C, 0x1003D), + (0x1003F, 0x1004D), + (0x10050, 0x1005D), + (0x10080, 0x100FA), + (0x10140, 0x10174), + (0x101FD, 0x101FD), + (0x10280, 0x1029C), + (0x102A0, 0x102D0), + (0x102E0, 0x102E0), + (0x10300, 0x1031F), + (0x1032D, 0x1034A), + (0x10350, 0x1037A), + (0x10380, 0x1039D), + (0x103A0, 0x103C3), + (0x103C8, 0x103CF), + (0x103D1, 0x103D5), + (0x10400, 0x1049D), + (0x104A0, 0x104A9), + (0x104B0, 0x104D3), + (0x104D8, 0x104FB), + (0x10500, 0x10527), + (0x10530, 0x10563), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x105C0, 0x105F3), + (0x10600, 0x10736), + (0x10740, 0x10755), + (0x10760, 0x10767), + (0x10780, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x10800, 0x10805), + (0x10808, 0x10808), + (0x1080A, 0x10835), + (0x10837, 0x10838), + (0x1083C, 0x1083C), + (0x1083F, 0x10855), + (0x10860, 0x10876), + (0x10880, 0x1089E), + (0x108E0, 0x108F2), + (0x108F4, 0x108F5), + (0x10900, 0x10915), + (0x10920, 0x10939), + (0x10940, 0x10959), + (0x10980, 0x109B7), + (0x109BE, 0x109BF), + (0x10A00, 0x10A03), + (0x10A05, 0x10A06), + (0x10A0C, 0x10A13), + (0x10A15, 0x10A17), + (0x10A19, 0x10A35), + (0x10A38, 0x10A3A), + (0x10A3F, 0x10A3F), + (0x10A60, 0x10A7C), + (0x10A80, 0x10A9C), + (0x10AC0, 0x10AC7), + (0x10AC9, 0x10AE6), + (0x10B00, 0x10B35), + (0x10B40, 0x10B55), + (0x10B60, 0x10B72), + (0x10B80, 0x10B91), + (0x10C00, 0x10C48), + (0x10C80, 0x10CB2), + (0x10CC0, 0x10CF2), + (0x10D00, 0x10D27), + (0x10D30, 0x10D39), + (0x10D40, 0x10D65), + (0x10D69, 0x10D6D), + (0x10D6F, 0x10D85), + (0x10E80, 0x10EA9), + (0x10EAB, 0x10EAC), + (0x10EB0, 0x10EB1), + (0x10EC2, 0x10EC7), + (0x10ECB, 0x10ECF), + (0x10ED9, 0x10EEE), + (0x10EF0, 0x10F1C), + (0x10F27, 0x10F27), + (0x10F30, 0x10F50), + (0x10F70, 0x10F85), + (0x10FB0, 0x10FC4), + (0x10FE0, 0x10FF6), + (0x11000, 0x11046), + (0x11066, 0x11075), + (0x1107F, 0x110BA), + (0x110C2, 0x110C2), + (0x110D0, 0x110E8), + (0x110F0, 0x110F9), + (0x11100, 0x11134), + (0x11136, 0x1113F), + (0x11144, 0x11147), + (0x11150, 0x11173), + (0x11176, 0x11176), + (0x11180, 0x111C4), + (0x111C9, 0x111CC), + (0x111CE, 0x111DA), + (0x111DC, 0x111DC), + (0x11200, 0x11211), + (0x11213, 0x11237), + (0x1123E, 0x11241), + (0x11280, 0x11286), + (0x11288, 0x11288), + (0x1128A, 0x1128D), + (0x1128F, 0x1129D), + (0x1129F, 0x112A8), + (0x112B0, 0x112EA), + (0x112F0, 0x112F9), + (0x11300, 0x11303), + (0x11305, 0x1130C), + (0x1130F, 0x11310), + (0x11313, 0x11328), + (0x1132A, 0x11330), + (0x11332, 0x11333), + (0x11335, 0x11339), + (0x1133B, 0x11344), + (0x11347, 0x11348), + (0x1134B, 0x1134D), + (0x11350, 0x11350), + (0x11357, 0x11357), + (0x1135D, 0x11363), + (0x11366, 0x1136C), + (0x11370, 0x11374), + (0x11380, 0x11389), + (0x1138B, 0x1138B), + (0x1138E, 0x1138E), + (0x11390, 0x113B5), + (0x113B7, 0x113C0), + (0x113C2, 0x113C2), + (0x113C5, 0x113C5), + (0x113C7, 0x113CA), + (0x113CC, 0x113D3), + (0x113E1, 0x113E2), + (0x11400, 0x1144A), + (0x11450, 0x11459), + (0x1145E, 0x11461), + (0x11480, 0x114C5), + (0x114C7, 0x114C7), + (0x114D0, 0x114D9), + (0x11580, 0x115B5), + (0x115B8, 0x115C0), + (0x115D8, 0x115DD), + (0x11600, 0x11640), + (0x11644, 0x11644), + (0x11650, 0x11659), + (0x11680, 0x116B8), + (0x116C0, 0x116C9), + (0x116D0, 0x116E3), + (0x11700, 0x1171A), + (0x1171D, 0x1172B), + (0x11730, 0x11739), + (0x11740, 0x11746), + (0x11800, 0x1183A), + (0x118A0, 0x118E9), + (0x118FF, 0x11906), + (0x11909, 0x11909), + (0x1190C, 0x11913), + (0x11915, 0x11916), + (0x11918, 0x11935), + (0x11937, 0x11938), + (0x1193B, 0x11943), + (0x11950, 0x11959), + (0x119A0, 0x119A7), + (0x119AA, 0x119D7), + (0x119DA, 0x119E1), + (0x119E3, 0x119E4), + (0x11A00, 0x11A3E), + (0x11A47, 0x11A47), + (0x11A50, 0x11A99), + (0x11A9D, 0x11A9D), + (0x11AB0, 0x11AF8), + (0x11B0A, 0x11B0A), + (0x11B60, 0x11B67), + (0x11BC0, 0x11BE0), + (0x11BF0, 0x11BF9), + (0x11C00, 0x11C08), + (0x11C0A, 0x11C36), + (0x11C38, 0x11C40), + (0x11C50, 0x11C59), + (0x11C72, 0x11C8F), + (0x11C92, 0x11CA7), + (0x11CA9, 0x11CB6), + (0x11D00, 0x11D06), + (0x11D08, 0x11D09), + (0x11D0B, 0x11D36), + (0x11D3A, 0x11D3A), + (0x11D3C, 0x11D3D), + (0x11D3F, 0x11D47), + (0x11D50, 0x11D59), + (0x11D60, 0x11D65), + (0x11D67, 0x11D68), + (0x11D6A, 0x11D8E), + (0x11D90, 0x11D91), + (0x11D93, 0x11D98), + (0x11DA0, 0x11DA9), + (0x11DB0, 0x11DDB), + (0x11DE0, 0x11DE9), + (0x11DF0, 0x11DF1), + (0x11EE0, 0x11EF6), + (0x11F00, 0x11F10), + (0x11F12, 0x11F3A), + (0x11F3E, 0x11F42), + (0x11F50, 0x11F5A), + (0x11FB0, 0x11FB0), + (0x12000, 0x12399), + (0x12400, 0x1246F), + (0x12475, 0x12543), + (0x12550, 0x12686), + (0x12F90, 0x12FF0), + (0x13000, 0x1342F), + (0x13440, 0x13455), + (0x13460, 0x143FA), + (0x14400, 0x14646), + (0x16100, 0x16139), + (0x16800, 0x16A38), + (0x16A40, 0x16A5E), + (0x16A60, 0x16A69), + (0x16A70, 0x16ABE), + (0x16AC0, 0x16AC9), + (0x16AD0, 0x16AED), + (0x16AF0, 0x16AF4), + (0x16B00, 0x16B36), + (0x16B40, 0x16B43), + (0x16B50, 0x16B59), + (0x16B63, 0x16B77), + (0x16B7D, 0x16B8F), + (0x16D40, 0x16D6C), + (0x16D70, 0x16D79), + (0x16E40, 0x16E7F), + (0x16EA0, 0x16EB8), + (0x16EBB, 0x16ED3), + (0x16F00, 0x16F4A), + (0x16F4F, 0x16F87), + (0x16F8F, 0x16F9F), + (0x16FE0, 0x16FE1), + (0x16FE3, 0x16FE4), + (0x16FF0, 0x16FF6), + (0x17000, 0x18CDA), + (0x18CFF, 0x18D20), + (0x18D80, 0x18DF2), + (0x18E00, 0x19191), + (0x191A0, 0x191D2), + (0x1AFF0, 0x1AFF3), + (0x1AFF5, 0x1AFFB), + (0x1AFFD, 0x1AFFE), + (0x1B000, 0x1B128), + (0x1B132, 0x1B132), + (0x1B150, 0x1B152), + (0x1B155, 0x1B155), + (0x1B164, 0x1B168), + (0x1B170, 0x1B2FB), + (0x1BC00, 0x1BC6A), + (0x1BC70, 0x1BC7C), + (0x1BC80, 0x1BC88), + (0x1BC90, 0x1BC99), + (0x1BC9D, 0x1BC9E), + (0x1CCF0, 0x1CCF9), + (0x1CF00, 0x1CF2D), + (0x1CF30, 0x1CF46), + (0x1D127, 0x1D128), + (0x1D165, 0x1D169), + (0x1D16D, 0x1D172), + (0x1D17B, 0x1D182), + (0x1D185, 0x1D18B), + (0x1D1AA, 0x1D1AD), + (0x1D242, 0x1D244), + (0x1D250, 0x1D252), + (0x1D25B, 0x1D25C), + (0x1D25F, 0x1D25F), + (0x1D280, 0x1D281), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D6C0), + (0x1D6C2, 0x1D6DA), + (0x1D6DC, 0x1D6FA), + (0x1D6FC, 0x1D714), + (0x1D716, 0x1D734), + (0x1D736, 0x1D74E), + (0x1D750, 0x1D76E), + (0x1D770, 0x1D788), + (0x1D78A, 0x1D7A8), + (0x1D7AA, 0x1D7C2), + (0x1D7C4, 0x1D7CB), + (0x1D7CE, 0x1D7FF), + (0x1DA00, 0x1DA36), + (0x1DA3B, 0x1DA6C), + (0x1DA75, 0x1DA75), + (0x1DA84, 0x1DA84), + (0x1DA9B, 0x1DA9F), + (0x1DAA1, 0x1DAAF), + (0x1DF00, 0x1DF81), + (0x1DF90, 0x1DF96), + (0x1DFCD, 0x1E006), + (0x1E008, 0x1E018), + (0x1E01B, 0x1E021), + (0x1E023, 0x1E024), + (0x1E026, 0x1E02A), + (0x1E030, 0x1E06D), + (0x1E08F, 0x1E08F), + (0x1E100, 0x1E12C), + (0x1E130, 0x1E13D), + (0x1E140, 0x1E149), + (0x1E14E, 0x1E14E), + (0x1E290, 0x1E2AE), + (0x1E2C0, 0x1E2F9), + (0x1E4D0, 0x1E4F9), + (0x1E5D0, 0x1E5FA), + (0x1E6C0, 0x1E6DE), + (0x1E6E0, 0x1E6F5), + (0x1E6FE, 0x1E6FF), + (0x1E7E0, 0x1E7E6), + (0x1E7E8, 0x1E7EB), + (0x1E7ED, 0x1E7EE), + (0x1E7F0, 0x1E7FE), + (0x1E800, 0x1E8C4), + (0x1E8D0, 0x1E8D6), + (0x1E900, 0x1E94B), + (0x1E950, 0x1E959), + (0x1EE00, 0x1EE03), + (0x1EE05, 0x1EE1F), + (0x1EE21, 0x1EE22), + (0x1EE24, 0x1EE24), + (0x1EE27, 0x1EE27), + (0x1EE29, 0x1EE32), + (0x1EE34, 0x1EE37), + (0x1EE39, 0x1EE39), + (0x1EE3B, 0x1EE3B), + (0x1EE42, 0x1EE42), + (0x1EE47, 0x1EE47), + (0x1EE49, 0x1EE49), + (0x1EE4B, 0x1EE4B), + (0x1EE4D, 0x1EE4F), + (0x1EE51, 0x1EE52), + (0x1EE54, 0x1EE54), + (0x1EE57, 0x1EE57), + (0x1EE59, 0x1EE59), + (0x1EE5B, 0x1EE5B), + (0x1EE5D, 0x1EE5D), + (0x1EE5F, 0x1EE5F), + (0x1EE61, 0x1EE62), + (0x1EE64, 0x1EE64), + (0x1EE67, 0x1EE6A), + (0x1EE6C, 0x1EE72), + (0x1EE74, 0x1EE77), + (0x1EE79, 0x1EE7C), + (0x1EE7E, 0x1EE7E), + (0x1EE80, 0x1EE89), + (0x1EE8B, 0x1EE9B), + (0x1EEA1, 0x1EEA3), + (0x1EEA5, 0x1EEA9), + (0x1EEAB, 0x1EEBB), + (0x1FBF0, 0x1FBF9), + (0x20000, 0x2A6DF), + (0x2A700, 0x2B81E), + (0x2B820, 0x2CEAD), + (0x2CEB0, 0x2EBE0), + (0x2EBF0, 0x2EE5D), + (0x2F800, 0x2FA1D), + (0x30000, 0x3134A), + (0x31350, 0x33479), + (0x3D000, 0x3FC3F), + (0xE0100, 0xE01EF), +]; + +/// The code points with the binary property XID_Start, as ranges. +pub static BINARY_XID_START: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0x61, 0x7A), + (0xAA, 0xAA), + (0xB5, 0xB5), + (0xBA, 0xBA), + (0xC0, 0xD6), + (0xD8, 0xF6), + (0xF8, 0x2C1), + (0x2C6, 0x2D1), + (0x2E0, 0x2E4), + (0x2EC, 0x2EC), + (0x2EE, 0x2EE), + (0x370, 0x374), + (0x376, 0x377), + (0x37B, 0x37D), + (0x37F, 0x37F), + (0x386, 0x386), + (0x388, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x3A1), + (0x3A3, 0x3F5), + (0x3F7, 0x481), + (0x48A, 0x52F), + (0x531, 0x556), + (0x558, 0x559), + (0x560, 0x588), + (0x58B, 0x58C), + (0x5D0, 0x5EA), + (0x5EF, 0x5F2), + (0x620, 0x64A), + (0x66E, 0x66F), + (0x671, 0x6D3), + (0x6D5, 0x6D5), + (0x6E5, 0x6E6), + (0x6EE, 0x6EF), + (0x6FA, 0x6FC), + (0x6FF, 0x6FF), + (0x710, 0x710), + (0x712, 0x72F), + (0x74D, 0x7A5), + (0x7B1, 0x7B1), + (0x7CA, 0x7EA), + (0x7F4, 0x7F5), + (0x7FA, 0x7FA), + (0x800, 0x815), + (0x81A, 0x81A), + (0x824, 0x824), + (0x828, 0x828), + (0x840, 0x858), + (0x860, 0x86A), + (0x870, 0x887), + (0x889, 0x88F), + (0x8A0, 0x8C9), + (0x904, 0x939), + (0x93D, 0x93D), + (0x950, 0x950), + (0x958, 0x961), + (0x971, 0x980), + (0x985, 0x98C), + (0x98F, 0x990), + (0x993, 0x9A8), + (0x9AA, 0x9B0), + (0x9B2, 0x9B2), + (0x9B6, 0x9B9), + (0x9BD, 0x9BD), + (0x9CE, 0x9CE), + (0x9DC, 0x9DD), + (0x9DF, 0x9E1), + (0x9F0, 0x9F1), + (0x9FC, 0x9FC), + (0xA05, 0xA0A), + (0xA0F, 0xA10), + (0xA13, 0xA28), + (0xA2A, 0xA30), + (0xA32, 0xA33), + (0xA35, 0xA36), + (0xA38, 0xA39), + (0xA59, 0xA5C), + (0xA5E, 0xA5E), + (0xA72, 0xA74), + (0xA85, 0xA8D), + (0xA8F, 0xA91), + (0xA93, 0xAA8), + (0xAAA, 0xAB0), + (0xAB2, 0xAB3), + (0xAB5, 0xAB9), + (0xABD, 0xABD), + (0xAD0, 0xAD0), + (0xAE0, 0xAE1), + (0xAF9, 0xAF9), + (0xB05, 0xB0C), + (0xB0F, 0xB10), + (0xB13, 0xB28), + (0xB2A, 0xB30), + (0xB32, 0xB33), + (0xB35, 0xB39), + (0xB3D, 0xB3D), + (0xB5C, 0xB5D), + (0xB5F, 0xB61), + (0xB71, 0xB71), + (0xB83, 0xB83), + (0xB85, 0xB8A), + (0xB8E, 0xB90), + (0xB92, 0xB95), + (0xB99, 0xB9A), + (0xB9C, 0xB9C), + (0xB9E, 0xB9F), + (0xBA3, 0xBA4), + (0xBA8, 0xBAA), + (0xBAE, 0xBB9), + (0xBD0, 0xBD0), + (0xC05, 0xC0C), + (0xC0E, 0xC10), + (0xC12, 0xC28), + (0xC2A, 0xC39), + (0xC3D, 0xC3D), + (0xC58, 0xC5A), + (0xC5C, 0xC5D), + (0xC60, 0xC61), + (0xC80, 0xC80), + (0xC85, 0xC8C), + (0xC8E, 0xC90), + (0xC92, 0xCA8), + (0xCAA, 0xCB3), + (0xCB5, 0xCB9), + (0xCBD, 0xCBD), + (0xCDC, 0xCDE), + (0xCE0, 0xCE1), + (0xCF1, 0xCF2), + (0xD04, 0xD0C), + (0xD0E, 0xD10), + (0xD12, 0xD3A), + (0xD3D, 0xD3D), + (0xD4E, 0xD4E), + (0xD54, 0xD56), + (0xD5F, 0xD61), + (0xD7A, 0xD7F), + (0xD85, 0xD96), + (0xD9A, 0xDB1), + (0xDB3, 0xDBB), + (0xDBD, 0xDBD), + (0xDC0, 0xDC6), + (0xE01, 0xE30), + (0xE32, 0xE32), + (0xE40, 0xE46), + (0xE81, 0xE82), + (0xE84, 0xE84), + (0xE86, 0xE8A), + (0xE8C, 0xEA3), + (0xEA5, 0xEA5), + (0xEA7, 0xEB0), + (0xEB2, 0xEB2), + (0xEBD, 0xEBD), + (0xEC0, 0xEC4), + (0xEC6, 0xEC6), + (0xEDC, 0xEDF), + (0xF00, 0xF00), + (0xF40, 0xF47), + (0xF49, 0xF6C), + (0xF88, 0xF8C), + (0x1000, 0x102A), + (0x103F, 0x103F), + (0x1050, 0x1055), + (0x105A, 0x105D), + (0x1061, 0x1061), + (0x1065, 0x1066), + (0x106E, 0x1070), + (0x1075, 0x1081), + (0x108E, 0x108E), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10D0, 0x10FA), + (0x10FC, 0x1248), + (0x124A, 0x124D), + (0x1250, 0x1256), + (0x1258, 0x1258), + (0x125A, 0x125D), + (0x1260, 0x1288), + (0x128A, 0x128D), + (0x1290, 0x12B0), + (0x12B2, 0x12B5), + (0x12B8, 0x12BE), + (0x12C0, 0x12C0), + (0x12C2, 0x12C5), + (0x12C8, 0x12D6), + (0x12D8, 0x1310), + (0x1312, 0x1315), + (0x1318, 0x135A), + (0x1380, 0x138F), + (0x13A0, 0x13F5), + (0x13F8, 0x13FD), + (0x1401, 0x166C), + (0x166F, 0x167F), + (0x1681, 0x169A), + (0x16A0, 0x16EA), + (0x16EE, 0x16F8), + (0x1700, 0x1711), + (0x171F, 0x1731), + (0x1740, 0x1751), + (0x1760, 0x176C), + (0x176E, 0x1770), + (0x1780, 0x17B3), + (0x17D7, 0x17D7), + (0x17DC, 0x17DC), + (0x1820, 0x1878), + (0x1880, 0x18A8), + (0x18AA, 0x18AA), + (0x18B0, 0x18F5), + (0x1900, 0x191E), + (0x1950, 0x196D), + (0x1970, 0x1974), + (0x1980, 0x19AB), + (0x19B0, 0x19C9), + (0x1A00, 0x1A16), + (0x1A20, 0x1A54), + (0x1AA7, 0x1AA7), + (0x1B05, 0x1B33), + (0x1B45, 0x1B4C), + (0x1B83, 0x1BA0), + (0x1BAE, 0x1BAF), + (0x1BBA, 0x1BE5), + (0x1C00, 0x1C23), + (0x1C4D, 0x1C4F), + (0x1C5A, 0x1C7D), + (0x1C80, 0x1C8A), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x1CE9, 0x1CEC), + (0x1CEE, 0x1CF3), + (0x1CF5, 0x1CF6), + (0x1CFA, 0x1CFA), + (0x1D00, 0x1DBF), + (0x1E00, 0x1F15), + (0x1F18, 0x1F1D), + (0x1F20, 0x1F45), + (0x1F48, 0x1F4D), + (0x1F50, 0x1F57), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FBC), + (0x1FBE, 0x1FBE), + (0x1FC2, 0x1FC4), + (0x1FC6, 0x1FCC), + (0x1FD0, 0x1FD3), + (0x1FD6, 0x1FDB), + (0x1FE0, 0x1FEC), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FFC), + (0x2071, 0x2071), + (0x207F, 0x207F), + (0x208F, 0x209F), + (0x2102, 0x2102), + (0x2107, 0x2107), + (0x210A, 0x2113), + (0x2115, 0x2115), + (0x2118, 0x211D), + (0x2124, 0x2124), + (0x2126, 0x2126), + (0x2128, 0x2128), + (0x212A, 0x2139), + (0x213C, 0x213F), + (0x2145, 0x2149), + (0x214E, 0x214E), + (0x2160, 0x2188), + (0x2C00, 0x2CE4), + (0x2CEB, 0x2CEE), + (0x2CF2, 0x2CF3), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0x2D30, 0x2D67), + (0x2D6F, 0x2D6F), + (0x2D80, 0x2D96), + (0x2DA0, 0x2DA6), + (0x2DA8, 0x2DAE), + (0x2DB0, 0x2DB6), + (0x2DB8, 0x2DBE), + (0x2DC0, 0x2DC6), + (0x2DC8, 0x2DCE), + (0x2DD0, 0x2DD6), + (0x2DD8, 0x2DDE), + (0x3005, 0x3007), + (0x3021, 0x3029), + (0x3031, 0x3035), + (0x3038, 0x303C), + (0x3041, 0x3096), + (0x309D, 0x309F), + (0x30A1, 0x30FA), + (0x30FC, 0x30FF), + (0x3105, 0x312F), + (0x3131, 0x318E), + (0x31A0, 0x31BF), + (0x31F0, 0x31FF), + (0x3400, 0x4DBF), + (0x4E00, 0xA48C), + (0xA4D0, 0xA4FD), + (0xA500, 0xA60C), + (0xA610, 0xA61F), + (0xA62A, 0xA62B), + (0xA640, 0xA66E), + (0xA67F, 0xA69D), + (0xA6A0, 0xA6EF), + (0xA717, 0xA71F), + (0xA722, 0xA788), + (0xA78B, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F1, 0xA801), + (0xA803, 0xA805), + (0xA807, 0xA80A), + (0xA80C, 0xA822), + (0xA840, 0xA873), + (0xA882, 0xA8B3), + (0xA8F2, 0xA8F7), + (0xA8FB, 0xA8FB), + (0xA8FD, 0xA8FE), + (0xA90A, 0xA925), + (0xA930, 0xA946), + (0xA960, 0xA97C), + (0xA984, 0xA9B2), + (0xA9CF, 0xA9CF), + (0xA9E0, 0xA9E4), + (0xA9E6, 0xA9EF), + (0xA9FA, 0xA9FE), + (0xAA00, 0xAA28), + (0xAA40, 0xAA42), + (0xAA44, 0xAA4B), + (0xAA60, 0xAA76), + (0xAA7A, 0xAA7A), + (0xAA7E, 0xAAAF), + (0xAAB1, 0xAAB1), + (0xAAB5, 0xAAB6), + (0xAAB9, 0xAABD), + (0xAAC0, 0xAAC0), + (0xAAC2, 0xAAC2), + (0xAADB, 0xAADD), + (0xAAE0, 0xAAEA), + (0xAAF2, 0xAAF4), + (0xAB01, 0xAB06), + (0xAB09, 0xAB0E), + (0xAB11, 0xAB16), + (0xAB20, 0xAB26), + (0xAB28, 0xAB2E), + (0xAB30, 0xAB5A), + (0xAB5C, 0xAB69), + (0xAB6C, 0xAB6D), + (0xAB70, 0xABE2), + (0xAC00, 0xD7A3), + (0xD7B0, 0xD7C6), + (0xD7CB, 0xD7FB), + (0xF900, 0xFA6D), + (0xFA70, 0xFAD9), + (0xFB00, 0xFB06), + (0xFB13, 0xFB17), + (0xFB1D, 0xFB1D), + (0xFB1F, 0xFB28), + (0xFB2A, 0xFB36), + (0xFB38, 0xFB3C), + (0xFB3E, 0xFB3E), + (0xFB40, 0xFB41), + (0xFB43, 0xFB44), + (0xFB46, 0xFBB1), + (0xFBD3, 0xFC5D), + (0xFC64, 0xFD3D), + (0xFD50, 0xFD8F), + (0xFD92, 0xFDC7), + (0xFDF0, 0xFDF9), + (0xFE71, 0xFE71), + (0xFE73, 0xFE73), + (0xFE77, 0xFE77), + (0xFE79, 0xFE79), + (0xFE7B, 0xFE7B), + (0xFE7D, 0xFE7D), + (0xFE7F, 0xFEFC), + (0xFF21, 0xFF3A), + (0xFF41, 0xFF5A), + (0xFF66, 0xFF9D), + (0xFFA0, 0xFFBE), + (0xFFC2, 0xFFC7), + (0xFFCA, 0xFFCF), + (0xFFD2, 0xFFD7), + (0xFFDA, 0xFFDC), + (0x10000, 0x1000B), + (0x1000D, 0x10026), + (0x10028, 0x1003A), + (0x1003C, 0x1003D), + (0x1003F, 0x1004D), + (0x10050, 0x1005D), + (0x10080, 0x100FA), + (0x10140, 0x10174), + (0x10280, 0x1029C), + (0x102A0, 0x102D0), + (0x10300, 0x1031F), + (0x1032D, 0x1034A), + (0x10350, 0x10375), + (0x10380, 0x1039D), + (0x103A0, 0x103C3), + (0x103C8, 0x103CF), + (0x103D1, 0x103D5), + (0x10400, 0x1049D), + (0x104B0, 0x104D3), + (0x104D8, 0x104FB), + (0x10500, 0x10527), + (0x10530, 0x10563), + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), + (0x105C0, 0x105F3), + (0x10600, 0x10736), + (0x10740, 0x10755), + (0x10760, 0x10767), + (0x10780, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x10800, 0x10805), + (0x10808, 0x10808), + (0x1080A, 0x10835), + (0x10837, 0x10838), + (0x1083C, 0x1083C), + (0x1083F, 0x10855), + (0x10860, 0x10876), + (0x10880, 0x1089E), + (0x108E0, 0x108F2), + (0x108F4, 0x108F5), + (0x10900, 0x10915), + (0x10920, 0x10939), + (0x10940, 0x10959), + (0x10980, 0x109B7), + (0x109BE, 0x109BF), + (0x10A00, 0x10A00), + (0x10A10, 0x10A13), + (0x10A15, 0x10A17), + (0x10A19, 0x10A35), + (0x10A60, 0x10A7C), + (0x10A80, 0x10A9C), + (0x10AC0, 0x10AC7), + (0x10AC9, 0x10AE4), + (0x10B00, 0x10B35), + (0x10B40, 0x10B55), + (0x10B60, 0x10B72), + (0x10B80, 0x10B91), + (0x10C00, 0x10C48), + (0x10C80, 0x10CB2), + (0x10CC0, 0x10CF2), + (0x10D00, 0x10D23), + (0x10D4A, 0x10D65), + (0x10D6F, 0x10D85), + (0x10E80, 0x10EA9), + (0x10EB0, 0x10EB1), + (0x10EC2, 0x10EC7), + (0x10ED9, 0x10EEE), + (0x10F00, 0x10F1C), + (0x10F27, 0x10F27), + (0x10F30, 0x10F45), + (0x10F70, 0x10F81), + (0x10FB0, 0x10FC4), + (0x10FE0, 0x10FF6), + (0x11003, 0x11037), + (0x11071, 0x11072), + (0x11075, 0x11075), + (0x11083, 0x110AF), + (0x110D0, 0x110E8), + (0x11103, 0x11126), + (0x11144, 0x11144), + (0x11147, 0x11147), + (0x11150, 0x11172), + (0x11176, 0x11176), + (0x11183, 0x111B2), + (0x111C1, 0x111C4), + (0x111DA, 0x111DA), + (0x111DC, 0x111DC), + (0x11200, 0x11211), + (0x11213, 0x1122B), + (0x1123F, 0x11240), + (0x11280, 0x11286), + (0x11288, 0x11288), + (0x1128A, 0x1128D), + (0x1128F, 0x1129D), + (0x1129F, 0x112A8), + (0x112B0, 0x112DE), + (0x11305, 0x1130C), + (0x1130F, 0x11310), + (0x11313, 0x11328), + (0x1132A, 0x11330), + (0x11332, 0x11333), + (0x11335, 0x11339), + (0x1133D, 0x1133D), + (0x11350, 0x11350), + (0x1135D, 0x11361), + (0x11380, 0x11389), + (0x1138B, 0x1138B), + (0x1138E, 0x1138E), + (0x11390, 0x113B5), + (0x113B7, 0x113B7), + (0x113D1, 0x113D1), + (0x113D3, 0x113D3), + (0x11400, 0x11434), + (0x11447, 0x1144A), + (0x1145F, 0x11461), + (0x11480, 0x114AF), + (0x114C4, 0x114C5), + (0x114C7, 0x114C7), + (0x11580, 0x115AE), + (0x115D8, 0x115DB), + (0x11600, 0x1162F), + (0x11644, 0x11644), + (0x11680, 0x116AA), + (0x116B8, 0x116B8), + (0x11700, 0x1171A), + (0x11740, 0x11746), + (0x11800, 0x1182B), + (0x118A0, 0x118DF), + (0x118FF, 0x11906), + (0x11909, 0x11909), + (0x1190C, 0x11913), + (0x11915, 0x11916), + (0x11918, 0x1192F), + (0x1193F, 0x1193F), + (0x11941, 0x11941), + (0x119A0, 0x119A7), + (0x119AA, 0x119D0), + (0x119E1, 0x119E1), + (0x119E3, 0x119E3), + (0x11A00, 0x11A00), + (0x11A0B, 0x11A32), + (0x11A3A, 0x11A3A), + (0x11A50, 0x11A50), + (0x11A5C, 0x11A89), + (0x11A9D, 0x11A9D), + (0x11AB0, 0x11AF8), + (0x11B0A, 0x11B0A), + (0x11BC0, 0x11BE0), + (0x11C00, 0x11C08), + (0x11C0A, 0x11C2E), + (0x11C40, 0x11C40), + (0x11C72, 0x11C8F), + (0x11D00, 0x11D06), + (0x11D08, 0x11D09), + (0x11D0B, 0x11D30), + (0x11D46, 0x11D46), + (0x11D60, 0x11D65), + (0x11D67, 0x11D68), + (0x11D6A, 0x11D89), + (0x11D98, 0x11D98), + (0x11DB0, 0x11DDB), + (0x11DF1, 0x11DF1), + (0x11EE0, 0x11EF2), + (0x11F02, 0x11F02), + (0x11F04, 0x11F10), + (0x11F12, 0x11F33), + (0x11FB0, 0x11FB0), + (0x12000, 0x12399), + (0x12400, 0x1246F), + (0x12475, 0x12543), + (0x12550, 0x12686), + (0x12F90, 0x12FF0), + (0x13000, 0x1342F), + (0x13441, 0x13446), + (0x13460, 0x143FA), + (0x14400, 0x14646), + (0x16100, 0x1611D), + (0x16800, 0x16A38), + (0x16A40, 0x16A5E), + (0x16A70, 0x16ABE), + (0x16AD0, 0x16AED), + (0x16B00, 0x16B2F), + (0x16B40, 0x16B43), + (0x16B63, 0x16B77), + (0x16B7D, 0x16B8F), + (0x16D40, 0x16D6C), + (0x16E40, 0x16E7F), + (0x16EA0, 0x16EB8), + (0x16EBB, 0x16ED3), + (0x16F00, 0x16F4A), + (0x16F50, 0x16F50), + (0x16F93, 0x16F9F), + (0x16FE0, 0x16FE1), + (0x16FE3, 0x16FE3), + (0x16FF2, 0x16FF6), + (0x17000, 0x18CDA), + (0x18CFF, 0x18D20), + (0x18D80, 0x18DF2), + (0x18E00, 0x19191), + (0x191A0, 0x191D2), + (0x1AFF0, 0x1AFF3), + (0x1AFF5, 0x1AFFB), + (0x1AFFD, 0x1AFFE), + (0x1B000, 0x1B128), + (0x1B132, 0x1B132), + (0x1B150, 0x1B152), + (0x1B155, 0x1B155), + (0x1B164, 0x1B168), + (0x1B170, 0x1B2FB), + (0x1BC00, 0x1BC6A), + (0x1BC70, 0x1BC7C), + (0x1BC80, 0x1BC88), + (0x1BC90, 0x1BC99), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D6C0), + (0x1D6C2, 0x1D6DA), + (0x1D6DC, 0x1D6FA), + (0x1D6FC, 0x1D714), + (0x1D716, 0x1D734), + (0x1D736, 0x1D74E), + (0x1D750, 0x1D76E), + (0x1D770, 0x1D788), + (0x1D78A, 0x1D7A8), + (0x1D7AA, 0x1D7C2), + (0x1D7C4, 0x1D7CB), + (0x1DF00, 0x1DF81), + (0x1DF90, 0x1DF96), + (0x1DFCD, 0x1DFFF), + (0x1E030, 0x1E06D), + (0x1E100, 0x1E12C), + (0x1E137, 0x1E13D), + (0x1E14E, 0x1E14E), + (0x1E290, 0x1E2AD), + (0x1E2C0, 0x1E2EB), + (0x1E4D0, 0x1E4EB), + (0x1E5D0, 0x1E5ED), + (0x1E5F0, 0x1E5F0), + (0x1E6C0, 0x1E6DE), + (0x1E6E0, 0x1E6E2), + (0x1E6E4, 0x1E6E5), + (0x1E6E7, 0x1E6ED), + (0x1E6F0, 0x1E6F4), + (0x1E6FE, 0x1E6FF), + (0x1E7E0, 0x1E7E6), + (0x1E7E8, 0x1E7EB), + (0x1E7ED, 0x1E7EE), + (0x1E7F0, 0x1E7FE), + (0x1E800, 0x1E8C4), + (0x1E900, 0x1E943), + (0x1E94B, 0x1E94B), + (0x1EE00, 0x1EE03), + (0x1EE05, 0x1EE1F), + (0x1EE21, 0x1EE22), + (0x1EE24, 0x1EE24), + (0x1EE27, 0x1EE27), + (0x1EE29, 0x1EE32), + (0x1EE34, 0x1EE37), + (0x1EE39, 0x1EE39), + (0x1EE3B, 0x1EE3B), + (0x1EE42, 0x1EE42), + (0x1EE47, 0x1EE47), + (0x1EE49, 0x1EE49), + (0x1EE4B, 0x1EE4B), + (0x1EE4D, 0x1EE4F), + (0x1EE51, 0x1EE52), + (0x1EE54, 0x1EE54), + (0x1EE57, 0x1EE57), + (0x1EE59, 0x1EE59), + (0x1EE5B, 0x1EE5B), + (0x1EE5D, 0x1EE5D), + (0x1EE5F, 0x1EE5F), + (0x1EE61, 0x1EE62), + (0x1EE64, 0x1EE64), + (0x1EE67, 0x1EE6A), + (0x1EE6C, 0x1EE72), + (0x1EE74, 0x1EE77), + (0x1EE79, 0x1EE7C), + (0x1EE7E, 0x1EE7E), + (0x1EE80, 0x1EE89), + (0x1EE8B, 0x1EE9B), + (0x1EEA1, 0x1EEA3), + (0x1EEA5, 0x1EEA9), + (0x1EEAB, 0x1EEBB), + (0x20000, 0x2A6DF), + (0x2A700, 0x2B81E), + (0x2B820, 0x2CEAD), + (0x2CEB0, 0x2EBE0), + (0x2EBF0, 0x2EE5D), + (0x2F800, 0x2FA1D), + (0x30000, 0x3134A), + (0x31350, 0x33479), + (0x3D000, 0x3FC3F), +]; + +/// The binary properties of ECMA-262's list that need a table, by canonical name (`Any`, `ASCII` and `Assigned` need none). +pub static BINARY: &[(&str, &[(u32, u32)])] = &[ + ("ASCII_Hex_Digit", BINARY_ASCII_HEX_DIGIT), + ("Alphabetic", BINARY_ALPHABETIC), + ("Bidi_Control", BINARY_BIDI_CONTROL), + ("Bidi_Mirrored", BINARY_BIDI_MIRRORED), + ("Case_Ignorable", BINARY_CASE_IGNORABLE), + ("Cased", BINARY_CASED), + ("Changes_When_Casefolded", BINARY_CHANGES_WHEN_CASEFOLDED), + ("Changes_When_Casemapped", BINARY_CHANGES_WHEN_CASEMAPPED), + ("Changes_When_Lowercased", BINARY_CHANGES_WHEN_LOWERCASED), + ( + "Changes_When_NFKC_Casefolded", + BINARY_CHANGES_WHEN_NFKC_CASEFOLDED, + ), + ("Changes_When_Titlecased", BINARY_CHANGES_WHEN_TITLECASED), + ("Changes_When_Uppercased", BINARY_CHANGES_WHEN_UPPERCASED), + ("Dash", BINARY_DASH), + ( + "Default_Ignorable_Code_Point", + BINARY_DEFAULT_IGNORABLE_CODE_POINT, + ), + ("Deprecated", BINARY_DEPRECATED), + ("Diacritic", BINARY_DIACRITIC), + ("Emoji", BINARY_EMOJI), + ("Emoji_Component", BINARY_EMOJI_COMPONENT), + ("Emoji_Modifier", BINARY_EMOJI_MODIFIER), + ("Emoji_Modifier_Base", BINARY_EMOJI_MODIFIER_BASE), + ("Emoji_Presentation", BINARY_EMOJI_PRESENTATION), + ("Extended_Pictographic", BINARY_EXTENDED_PICTOGRAPHIC), + ("Extender", BINARY_EXTENDER), + ("Grapheme_Base", BINARY_GRAPHEME_BASE), + ("Grapheme_Extend", BINARY_GRAPHEME_EXTEND), + ("Hex_Digit", BINARY_HEX_DIGIT), + ("IDS_Binary_Operator", BINARY_IDS_BINARY_OPERATOR), + ("IDS_Trinary_Operator", BINARY_IDS_TRINARY_OPERATOR), + ("ID_Continue", BINARY_ID_CONTINUE), + ("ID_Start", BINARY_ID_START), + ("Ideographic", BINARY_IDEOGRAPHIC), + ("Join_Control", BINARY_JOIN_CONTROL), + ("Logical_Order_Exception", BINARY_LOGICAL_ORDER_EXCEPTION), + ("Lowercase", BINARY_LOWERCASE), + ("Math", BINARY_MATH), + ("Noncharacter_Code_Point", BINARY_NONCHARACTER_CODE_POINT), + ("Pattern_Syntax", BINARY_PATTERN_SYNTAX), + ("Pattern_White_Space", BINARY_PATTERN_WHITE_SPACE), + ("Quotation_Mark", BINARY_QUOTATION_MARK), + ("Radical", BINARY_RADICAL), + ("Regional_Indicator", BINARY_REGIONAL_INDICATOR), + ("Sentence_Terminal", BINARY_SENTENCE_TERMINAL), + ("Soft_Dotted", BINARY_SOFT_DOTTED), + ("Terminal_Punctuation", BINARY_TERMINAL_PUNCTUATION), + ("Unified_Ideograph", BINARY_UNIFIED_IDEOGRAPH), + ("Uppercase", BINARY_UPPERCASE), + ("Variation_Selector", BINARY_VARIATION_SELECTOR), + ("White_Space", BINARY_WHITE_SPACE), + ("XID_Continue", BINARY_XID_CONTINUE), + ("XID_Start", BINARY_XID_START), +]; + +/// The code points whose Script is Adlam. +pub static SCRIPT_ADLAM: &[(u32, u32)] = + &[(0x1E900, 0x1E94B), (0x1E950, 0x1E959), (0x1E95E, 0x1E95F)]; + +/// The code points whose Script_Extensions include Adlam. +pub static SCRIPT_EXT_ADLAM: &[(u32, u32)] = &[ + (0x61F, 0x61F), + (0x640, 0x640), + (0x204F, 0x204F), + (0x2E41, 0x2E41), + (0x1E900, 0x1E94B), + (0x1E950, 0x1E959), + (0x1E95E, 0x1E95F), +]; + +/// The code points whose Script is Ahom. +pub static SCRIPT_AHOM: &[(u32, u32)] = + &[(0x11700, 0x1171A), (0x1171D, 0x1172B), (0x11730, 0x11746)]; + +/// The code points whose Script is Anatolian_Hieroglyphs. +pub static SCRIPT_ANATOLIAN_HIEROGLYPHS: &[(u32, u32)] = &[(0x14400, 0x14646)]; + +/// The code points whose Script is Arabic. +pub static SCRIPT_ARABIC: &[(u32, u32)] = &[ + (0x600, 0x604), + (0x606, 0x60B), + (0x60D, 0x61A), + (0x61C, 0x61E), + (0x620, 0x63F), + (0x641, 0x64A), + (0x656, 0x66F), + (0x671, 0x6DC), + (0x6DE, 0x6FF), + (0x750, 0x77F), + (0x870, 0x891), + (0x897, 0x8E1), + (0x8E3, 0x8FF), + (0xFB50, 0xFD3D), + (0xFD40, 0xFDCF), + (0xFDF0, 0xFDFF), + (0xFE70, 0xFE74), + (0xFE76, 0xFEFC), + (0x10E60, 0x10E7E), + (0x10EC2, 0x10EC7), + (0x10EC9, 0x10EEE), + (0x10EF0, 0x10EFF), + (0x1EE00, 0x1EE03), + (0x1EE05, 0x1EE1F), + (0x1EE21, 0x1EE22), + (0x1EE24, 0x1EE24), + (0x1EE27, 0x1EE27), + (0x1EE29, 0x1EE32), + (0x1EE34, 0x1EE37), + (0x1EE39, 0x1EE39), + (0x1EE3B, 0x1EE3B), + (0x1EE42, 0x1EE42), + (0x1EE47, 0x1EE47), + (0x1EE49, 0x1EE49), + (0x1EE4B, 0x1EE4B), + (0x1EE4D, 0x1EE4F), + (0x1EE51, 0x1EE52), + (0x1EE54, 0x1EE54), + (0x1EE57, 0x1EE57), + (0x1EE59, 0x1EE59), + (0x1EE5B, 0x1EE5B), + (0x1EE5D, 0x1EE5D), + (0x1EE5F, 0x1EE5F), + (0x1EE61, 0x1EE62), + (0x1EE64, 0x1EE64), + (0x1EE67, 0x1EE6A), + (0x1EE6C, 0x1EE72), + (0x1EE74, 0x1EE77), + (0x1EE79, 0x1EE7C), + (0x1EE7E, 0x1EE7E), + (0x1EE80, 0x1EE89), + (0x1EE8B, 0x1EE9B), + (0x1EEA1, 0x1EEA3), + (0x1EEA5, 0x1EEA9), + (0x1EEAB, 0x1EEBB), + (0x1EEF0, 0x1EEF1), +]; + +/// The code points whose Script_Extensions include Arabic. +pub static SCRIPT_EXT_ARABIC: &[(u32, u32)] = &[ + (0x600, 0x604), + (0x606, 0x6DC), + (0x6DE, 0x6FF), + (0x750, 0x77F), + (0x870, 0x891), + (0x897, 0x8E1), + (0x8E3, 0x8FF), + (0x204F, 0x204F), + (0x2E41, 0x2E41), + (0xFB50, 0xFDCF), + (0xFDF0, 0xFDFF), + (0xFE70, 0xFE74), + (0xFE76, 0xFEFC), + (0x102E0, 0x102FB), + (0x10E60, 0x10E7E), + (0x10EC2, 0x10EC7), + (0x10EC9, 0x10EEE), + (0x10EF0, 0x10EFF), + (0x1EE00, 0x1EE03), + (0x1EE05, 0x1EE1F), + (0x1EE21, 0x1EE22), + (0x1EE24, 0x1EE24), + (0x1EE27, 0x1EE27), + (0x1EE29, 0x1EE32), + (0x1EE34, 0x1EE37), + (0x1EE39, 0x1EE39), + (0x1EE3B, 0x1EE3B), + (0x1EE42, 0x1EE42), + (0x1EE47, 0x1EE47), + (0x1EE49, 0x1EE49), + (0x1EE4B, 0x1EE4B), + (0x1EE4D, 0x1EE4F), + (0x1EE51, 0x1EE52), + (0x1EE54, 0x1EE54), + (0x1EE57, 0x1EE57), + (0x1EE59, 0x1EE59), + (0x1EE5B, 0x1EE5B), + (0x1EE5D, 0x1EE5D), + (0x1EE5F, 0x1EE5F), + (0x1EE61, 0x1EE62), + (0x1EE64, 0x1EE64), + (0x1EE67, 0x1EE6A), + (0x1EE6C, 0x1EE72), + (0x1EE74, 0x1EE77), + (0x1EE79, 0x1EE7C), + (0x1EE7E, 0x1EE7E), + (0x1EE80, 0x1EE89), + (0x1EE8B, 0x1EE9B), + (0x1EEA1, 0x1EEA3), + (0x1EEA5, 0x1EEA9), + (0x1EEAB, 0x1EEBB), + (0x1EEF0, 0x1EEF1), +]; + +/// The code points whose Script is Armenian. +pub static SCRIPT_ARMENIAN: &[(u32, u32)] = &[(0x531, 0x556), (0x558, 0x58F), (0xFB13, 0xFB17)]; + +/// The code points whose Script_Extensions include Armenian. +pub static SCRIPT_EXT_ARMENIAN: &[(u32, u32)] = &[ + (0x308, 0x308), + (0x531, 0x556), + (0x558, 0x58F), + (0xFB13, 0xFB17), +]; + +/// The code points whose Script is Avestan. +pub static SCRIPT_AVESTAN: &[(u32, u32)] = &[(0x10B00, 0x10B35), (0x10B39, 0x10B3F)]; + +/// The code points whose Script_Extensions include Avestan. +pub static SCRIPT_EXT_AVESTAN: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x2E30, 0x2E31), + (0x10B00, 0x10B35), + (0x10B39, 0x10B3F), +]; + +/// The code points whose Script is Balinese. +pub static SCRIPT_BALINESE: &[(u32, u32)] = &[(0x1B00, 0x1B4C), (0x1B4E, 0x1B7F)]; + +/// The code points whose Script is Bamum. +pub static SCRIPT_BAMUM: &[(u32, u32)] = &[(0xA6A0, 0xA6F7), (0x16800, 0x16A38)]; + +/// The code points whose Script is Bassa_Vah. +pub static SCRIPT_BASSA_VAH: &[(u32, u32)] = &[(0x16AD0, 0x16AED), (0x16AF0, 0x16AF5)]; + +/// The code points whose Script is Batak. +pub static SCRIPT_BATAK: &[(u32, u32)] = &[(0x1BC0, 0x1BF3), (0x1BFC, 0x1BFF)]; + +/// The code points whose Script is Bengali. +pub static SCRIPT_BENGALI: &[(u32, u32)] = &[ + (0x980, 0x983), + (0x985, 0x98C), + (0x98F, 0x990), + (0x993, 0x9A8), + (0x9AA, 0x9B0), + (0x9B2, 0x9B2), + (0x9B6, 0x9B9), + (0x9BC, 0x9C4), + (0x9C7, 0x9C8), + (0x9CB, 0x9CE), + (0x9D7, 0x9D7), + (0x9DC, 0x9DD), + (0x9DF, 0x9E3), + (0x9E6, 0x9FE), + (0x11DF0, 0x11DF1), +]; + +/// The code points whose Script_Extensions include Bengali. +pub static SCRIPT_EXT_BENGALI: &[(u32, u32)] = &[ + (0x2BC, 0x2BC), + (0x951, 0x952), + (0x964, 0x965), + (0x980, 0x983), + (0x985, 0x98C), + (0x98F, 0x990), + (0x993, 0x9A8), + (0x9AA, 0x9B0), + (0x9B2, 0x9B2), + (0x9B6, 0x9B9), + (0x9BC, 0x9C4), + (0x9C7, 0x9C8), + (0x9CB, 0x9CE), + (0x9D7, 0x9D7), + (0x9DC, 0x9DD), + (0x9DF, 0x9E3), + (0x9E6, 0x9FE), + (0x1CD0, 0x1CD0), + (0x1CD2, 0x1CD2), + (0x1CD5, 0x1CD6), + (0x1CD8, 0x1CD8), + (0x1CE1, 0x1CE1), + (0x1CEA, 0x1CEA), + (0x1CED, 0x1CED), + (0x1CF2, 0x1CF2), + (0x1CF5, 0x1CF7), + (0xA8F1, 0xA8F1), + (0x11DF0, 0x11DF1), +]; + +/// The code points whose Script is Beria_Erfe. +pub static SCRIPT_BERIA_ERFE: &[(u32, u32)] = &[(0x16EA0, 0x16EB8), (0x16EBB, 0x16ED3)]; + +/// The code points whose Script is Bhaiksuki. +pub static SCRIPT_BHAIKSUKI: &[(u32, u32)] = &[ + (0x11C00, 0x11C08), + (0x11C0A, 0x11C36), + (0x11C38, 0x11C45), + (0x11C50, 0x11C6C), +]; + +/// The code points whose Script is Bopomofo. +pub static SCRIPT_BOPOMOFO: &[(u32, u32)] = &[(0x2EA, 0x2EB), (0x3105, 0x312F), (0x31A0, 0x31BF)]; + +/// The code points whose Script_Extensions include Bopomofo. +pub static SCRIPT_EXT_BOPOMOFO: &[(u32, u32)] = &[ + (0x2C7, 0x2C7), + (0x2C9, 0x2CB), + (0x2D9, 0x2D9), + (0x2EA, 0x2EB), + (0x3001, 0x3003), + (0x3008, 0x3011), + (0x3013, 0x301F), + (0x302A, 0x302D), + (0x3030, 0x3030), + (0x3037, 0x3037), + (0x30FB, 0x30FB), + (0x3105, 0x312F), + (0x31A0, 0x31BF), + (0xFE45, 0xFE46), + (0xFF61, 0xFF65), +]; + +/// The code points whose Script is Brahmi. +pub static SCRIPT_BRAHMI: &[(u32, u32)] = + &[(0x11000, 0x1104D), (0x11052, 0x11075), (0x1107F, 0x1107F)]; + +/// The code points whose Script is Braille. +pub static SCRIPT_BRAILLE: &[(u32, u32)] = &[(0x2800, 0x28FF)]; + +/// The code points whose Script is Buginese. +pub static SCRIPT_BUGINESE: &[(u32, u32)] = &[(0x1A00, 0x1A1B), (0x1A1E, 0x1A1F)]; + +/// The code points whose Script_Extensions include Buginese. +pub static SCRIPT_EXT_BUGINESE: &[(u32, u32)] = + &[(0x1A00, 0x1A1B), (0x1A1E, 0x1A1F), (0xA9CF, 0xA9CF)]; + +/// The code points whose Script is Buhid. +pub static SCRIPT_BUHID: &[(u32, u32)] = &[(0x1740, 0x1753)]; + +/// The code points whose Script_Extensions include Buhid. +pub static SCRIPT_EXT_BUHID: &[(u32, u32)] = &[(0x1735, 0x1736), (0x1740, 0x1753)]; + +/// The code points whose Script is Canadian_Aboriginal. +pub static SCRIPT_CANADIAN_ABORIGINAL: &[(u32, u32)] = + &[(0x1400, 0x167F), (0x18B0, 0x18F5), (0x11AB0, 0x11ABF)]; + +/// The code points whose Script is Carian. +pub static SCRIPT_CARIAN: &[(u32, u32)] = &[(0x102A0, 0x102D0)]; + +/// The code points whose Script_Extensions include Carian. +pub static SCRIPT_EXT_CARIAN: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x205A, 0x205A), + (0x205D, 0x205D), + (0x2E31, 0x2E31), + (0x102A0, 0x102D0), +]; + +/// The code points whose Script is Caucasian_Albanian. +pub static SCRIPT_CAUCASIAN_ALBANIAN: &[(u32, u32)] = &[(0x10530, 0x10563), (0x1056F, 0x1056F)]; + +/// The code points whose Script_Extensions include Caucasian_Albanian. +pub static SCRIPT_EXT_CAUCASIAN_ALBANIAN: &[(u32, u32)] = &[ + (0x304, 0x304), + (0x331, 0x331), + (0x35E, 0x35E), + (0x10530, 0x10563), + (0x1056F, 0x1056F), +]; + +/// The code points whose Script is Chakma. +pub static SCRIPT_CHAKMA: &[(u32, u32)] = &[(0x11100, 0x11134), (0x11136, 0x11147)]; + +/// The code points whose Script_Extensions include Chakma. +pub static SCRIPT_EXT_CHAKMA: &[(u32, u32)] = &[ + (0x9E6, 0x9EF), + (0x1040, 0x1049), + (0x11100, 0x11134), + (0x11136, 0x11147), +]; + +/// The code points whose Script is Cham. +pub static SCRIPT_CHAM: &[(u32, u32)] = &[ + (0xAA00, 0xAA36), + (0xAA40, 0xAA4D), + (0xAA50, 0xAA59), + (0xAA5C, 0xAA5F), +]; + +/// The code points whose Script is Cherokee. +pub static SCRIPT_CHEROKEE: &[(u32, u32)] = &[(0x13A0, 0x13F5), (0x13F8, 0x13FD), (0xAB70, 0xABBF)]; + +/// The code points whose Script_Extensions include Cherokee. +pub static SCRIPT_EXT_CHEROKEE: &[(u32, u32)] = &[ + (0x300, 0x302), + (0x304, 0x304), + (0x30B, 0x30C), + (0x323, 0x324), + (0x330, 0x331), + (0x13A0, 0x13F5), + (0x13F8, 0x13FD), + (0xAB70, 0xABBF), +]; + +/// The code points whose Script is Chorasmian. +pub static SCRIPT_CHORASMIAN: &[(u32, u32)] = &[(0x10FB0, 0x10FCB)]; + +/// The code points whose Script is Common. +pub static SCRIPT_COMMON: &[(u32, u32)] = &[ + (0x0, 0x40), + (0x5B, 0x60), + (0x7B, 0xA9), + (0xAB, 0xB9), + (0xBB, 0xBF), + (0xD7, 0xD7), + (0xF7, 0xF7), + (0x2B9, 0x2DF), + (0x2E5, 0x2E9), + (0x2EC, 0x2FF), + (0x374, 0x374), + (0x37E, 0x37E), + (0x385, 0x385), + (0x387, 0x387), + (0x605, 0x605), + (0x60C, 0x60C), + (0x61B, 0x61B), + (0x61F, 0x61F), + (0x640, 0x640), + (0x6DD, 0x6DD), + (0x8E2, 0x8E2), + (0x964, 0x965), + (0xE3F, 0xE3F), + (0xFD5, 0xFD8), + (0x10FB, 0x10FB), + (0x16EB, 0x16ED), + (0x1735, 0x1736), + (0x1802, 0x1803), + (0x1805, 0x1805), + (0x1CD3, 0x1CD3), + (0x1CE1, 0x1CE1), + (0x1CE9, 0x1CEC), + (0x1CEE, 0x1CF3), + (0x1CF5, 0x1CF7), + (0x1CFA, 0x1CFA), + (0x2000, 0x200B), + (0x200E, 0x2064), + (0x2066, 0x2070), + (0x2074, 0x207E), + (0x2080, 0x208F), + (0x20A0, 0x20C4), + (0x2100, 0x2125), + (0x2127, 0x2129), + (0x212C, 0x2131), + (0x2133, 0x214D), + (0x214F, 0x215F), + (0x2189, 0x218B), + (0x2190, 0x2429), + (0x2440, 0x244A), + (0x2460, 0x27FF), + (0x2900, 0x2B73), + (0x2B76, 0x2BFF), + (0x2E00, 0x2E5D), + (0x2E60, 0x2E63), + (0x2FF0, 0x3004), + (0x3006, 0x3006), + (0x3008, 0x3020), + (0x3030, 0x3037), + (0x303C, 0x303F), + (0x309B, 0x309C), + (0x30A0, 0x30A0), + (0x30FB, 0x30FC), + (0x3190, 0x319F), + (0x31C0, 0x31E5), + (0x31EF, 0x31EF), + (0x3220, 0x325F), + (0x327F, 0x32CF), + (0x32FF, 0x32FF), + (0x3358, 0x33FF), + (0x4DC0, 0x4DFF), + (0xA700, 0xA721), + (0xA788, 0xA78A), + (0xA830, 0xA839), + (0xA92E, 0xA92E), + (0xA9CF, 0xA9CF), + (0xAB5B, 0xAB5B), + (0xAB6A, 0xAB6B), + (0xFD3E, 0xFD3F), + (0xFE10, 0xFE19), + (0xFE30, 0xFE52), + (0xFE54, 0xFE66), + (0xFE68, 0xFE6B), + (0xFEFF, 0xFEFF), + (0xFF01, 0xFF20), + (0xFF3B, 0xFF40), + (0xFF5B, 0xFF65), + (0xFF70, 0xFF70), + (0xFF9E, 0xFF9F), + (0xFFE0, 0xFFE6), + (0xFFE8, 0xFFEE), + (0xFFF9, 0xFFFD), + (0x10100, 0x10102), + (0x10107, 0x10133), + (0x10137, 0x1013F), + (0x10190, 0x1019C), + (0x101D0, 0x101FC), + (0x102E1, 0x102FB), + (0x1BCA0, 0x1BCA3), + (0x1CC00, 0x1CCFC), + (0x1CD00, 0x1CEB3), + (0x1CEBA, 0x1CED0), + (0x1CED2, 0x1CED4), + (0x1CEDD, 0x1CEFD), + (0x1CF50, 0x1CFC3), + (0x1D000, 0x1D0F5), + (0x1D100, 0x1D126), + (0x1D129, 0x1D166), + (0x1D16A, 0x1D17A), + (0x1D183, 0x1D184), + (0x1D18C, 0x1D1A9), + (0x1D1AE, 0x1D1FF), + (0x1D250, 0x1D25A), + (0x1D25D, 0x1D281), + (0x1D2C0, 0x1D2D3), + (0x1D2E0, 0x1D2F3), + (0x1D300, 0x1D356), + (0x1D360, 0x1D378), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D7CB), + (0x1D7CE, 0x1D7FF), + (0x1DB00, 0x1DB1C), + (0x1EC71, 0x1ECB4), + (0x1ED01, 0x1ED3D), + (0x1F000, 0x1F02B), + (0x1F030, 0x1F093), + (0x1F0A0, 0x1F0AE), + (0x1F0B1, 0x1F0BF), + (0x1F0C1, 0x1F0CF), + (0x1F0D1, 0x1F0F5), + (0x1F100, 0x1F1AE), + (0x1F1E6, 0x1F1FF), + (0x1F201, 0x1F202), + (0x1F210, 0x1F23B), + (0x1F240, 0x1F248), + (0x1F250, 0x1F251), + (0x1F260, 0x1F265), + (0x1F300, 0x1F6D9), + (0x1F6DC, 0x1F6EC), + (0x1F6F0, 0x1F6FC), + (0x1F700, 0x1F7DB), + (0x1F7E0, 0x1F7EB), + (0x1F7F0, 0x1F80B), + (0x1F810, 0x1F847), + (0x1F850, 0x1F859), + (0x1F860, 0x1F887), + (0x1F890, 0x1F8AD), + (0x1F8B0, 0x1F8BB), + (0x1F8C0, 0x1F8C1), + (0x1F8D0, 0x1F8D8), + (0x1F900, 0x1FA57), + (0x1FA60, 0x1FA6D), + (0x1FA70, 0x1FA7C), + (0x1FA80, 0x1FAC6), + (0x1FAC8, 0x1FAC8), + (0x1FACC, 0x1FADD), + (0x1FADF, 0x1FAEB), + (0x1FAEF, 0x1FAFA), + (0x1FB00, 0x1FB92), + (0x1FB94, 0x1FBFA), + (0xE0001, 0xE0001), + (0xE0020, 0xE007F), +]; + +/// The code points whose Script_Extensions include Common. +pub static SCRIPT_EXT_COMMON: &[(u32, u32)] = &[ + (0x0, 0x40), + (0x5B, 0x60), + (0x7B, 0xA9), + (0xAB, 0xB6), + (0xB8, 0xB9), + (0xBB, 0xBF), + (0xD7, 0xD7), + (0xF7, 0xF7), + (0x2B9, 0x2BB), + (0x2BD, 0x2C6), + (0x2C8, 0x2C8), + (0x2CC, 0x2CC), + (0x2CE, 0x2D6), + (0x2D8, 0x2D8), + (0x2DA, 0x2DF), + (0x2E5, 0x2E9), + (0x2EC, 0x2FF), + (0x37E, 0x37E), + (0x385, 0x385), + (0x387, 0x387), + (0x605, 0x605), + (0x6DD, 0x6DD), + (0x8E2, 0x8E2), + (0xE3F, 0xE3F), + (0xFD5, 0xFD8), + (0x2000, 0x200B), + (0x200E, 0x202E), + (0x2030, 0x204E), + (0x2050, 0x2059), + (0x205B, 0x205C), + (0x205E, 0x2064), + (0x2066, 0x2070), + (0x2074, 0x207E), + (0x2080, 0x208F), + (0x20A0, 0x20C4), + (0x2100, 0x2125), + (0x2127, 0x2129), + (0x212C, 0x2131), + (0x2133, 0x214D), + (0x214F, 0x215F), + (0x2189, 0x218B), + (0x2190, 0x2429), + (0x2440, 0x244A), + (0x2460, 0x27FF), + (0x2900, 0x2B73), + (0x2B76, 0x2BFF), + (0x2E00, 0x2E16), + (0x2E18, 0x2E2F), + (0x2E32, 0x2E3B), + (0x2E3D, 0x2E40), + (0x2E42, 0x2E42), + (0x2E44, 0x2E5D), + (0x2E60, 0x2E63), + (0x3000, 0x3000), + (0x3004, 0x3004), + (0x3012, 0x3012), + (0x3020, 0x3020), + (0x3036, 0x3036), + (0x3248, 0x325F), + (0x327F, 0x327F), + (0x32B1, 0x32BF), + (0x32CC, 0x32CF), + (0x3371, 0x337A), + (0x3380, 0x33DF), + (0x33FF, 0x33FF), + (0x4DC0, 0x4DFF), + (0xA708, 0xA721), + (0xA788, 0xA78A), + (0xAB5B, 0xAB5B), + (0xAB6A, 0xAB6B), + (0xFE10, 0xFE19), + (0xFE30, 0xFE44), + (0xFE47, 0xFE52), + (0xFE54, 0xFE66), + (0xFE68, 0xFE6B), + (0xFEFF, 0xFEFF), + (0xFF01, 0xFF20), + (0xFF3B, 0xFF40), + (0xFF5B, 0xFF60), + (0xFFE0, 0xFFE6), + (0xFFE8, 0xFFEE), + (0xFFF9, 0xFFFD), + (0x10190, 0x1019C), + (0x101D0, 0x101FC), + (0x1CC00, 0x1CCFC), + (0x1CD00, 0x1CEB3), + (0x1CEBA, 0x1CED0), + (0x1CED2, 0x1CED4), + (0x1CEDD, 0x1CEFD), + (0x1CF50, 0x1CFC3), + (0x1D000, 0x1D0F5), + (0x1D100, 0x1D126), + (0x1D129, 0x1D166), + (0x1D16A, 0x1D17A), + (0x1D183, 0x1D184), + (0x1D18C, 0x1D1A9), + (0x1D1AE, 0x1D1FF), + (0x1D250, 0x1D25A), + (0x1D25D, 0x1D281), + (0x1D2C0, 0x1D2D3), + (0x1D2E0, 0x1D2F3), + (0x1D300, 0x1D356), + (0x1D372, 0x1D378), + (0x1D400, 0x1D454), + (0x1D456, 0x1D49C), + (0x1D49E, 0x1D49F), + (0x1D4A2, 0x1D4A2), + (0x1D4A5, 0x1D4A6), + (0x1D4A9, 0x1D4AC), + (0x1D4AE, 0x1D4B9), + (0x1D4BB, 0x1D4BB), + (0x1D4BD, 0x1D4C3), + (0x1D4C5, 0x1D505), + (0x1D507, 0x1D50A), + (0x1D50D, 0x1D514), + (0x1D516, 0x1D51C), + (0x1D51E, 0x1D539), + (0x1D53B, 0x1D53E), + (0x1D540, 0x1D544), + (0x1D546, 0x1D546), + (0x1D54A, 0x1D550), + (0x1D552, 0x1D6A6), + (0x1D6A8, 0x1D7CB), + (0x1D7CE, 0x1D7FF), + (0x1DB00, 0x1DB1C), + (0x1EC71, 0x1ECB4), + (0x1ED01, 0x1ED3D), + (0x1F000, 0x1F02B), + (0x1F030, 0x1F093), + (0x1F0A0, 0x1F0AE), + (0x1F0B1, 0x1F0BF), + (0x1F0C1, 0x1F0CF), + (0x1F0D1, 0x1F0F5), + (0x1F100, 0x1F1AE), + (0x1F1E6, 0x1F1FF), + (0x1F201, 0x1F202), + (0x1F210, 0x1F23B), + (0x1F240, 0x1F248), + (0x1F260, 0x1F265), + (0x1F300, 0x1F6D9), + (0x1F6DC, 0x1F6EC), + (0x1F6F0, 0x1F6FC), + (0x1F700, 0x1F7DB), + (0x1F7E0, 0x1F7EB), + (0x1F7F0, 0x1F80B), + (0x1F810, 0x1F847), + (0x1F850, 0x1F859), + (0x1F860, 0x1F887), + (0x1F890, 0x1F8AD), + (0x1F8B0, 0x1F8BB), + (0x1F8C0, 0x1F8C1), + (0x1F8D0, 0x1F8D8), + (0x1F900, 0x1FA57), + (0x1FA60, 0x1FA6D), + (0x1FA70, 0x1FA7C), + (0x1FA80, 0x1FAC6), + (0x1FAC8, 0x1FAC8), + (0x1FACC, 0x1FADD), + (0x1FADF, 0x1FAEB), + (0x1FAEF, 0x1FAFA), + (0x1FB00, 0x1FB92), + (0x1FB94, 0x1FBFA), + (0xE0001, 0xE0001), + (0xE0020, 0xE007F), +]; + +/// The code points whose Script is Coptic. +pub static SCRIPT_COPTIC: &[(u32, u32)] = &[(0x3E2, 0x3EF), (0x2C80, 0x2CF3), (0x2CF9, 0x2CFF)]; + +/// The code points whose Script_Extensions include Coptic. +pub static SCRIPT_EXT_COPTIC: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x300, 0x300), + (0x304, 0x305), + (0x307, 0x307), + (0x374, 0x375), + (0x3E2, 0x3EF), + (0x2C80, 0x2CF3), + (0x2CF9, 0x2CFF), + (0x2E17, 0x2E17), + (0x102E0, 0x102FB), +]; + +/// The code points whose Script is Cuneiform. +pub static SCRIPT_CUNEIFORM: &[(u32, u32)] = &[ + (0x12000, 0x12399), + (0x12400, 0x12543), + (0x12550, 0x125A7), + (0x1264C, 0x12686), +]; + +/// The code points whose Script is Cypriot. +pub static SCRIPT_CYPRIOT: &[(u32, u32)] = &[ + (0x10800, 0x10805), + (0x10808, 0x10808), + (0x1080A, 0x10835), + (0x10837, 0x10838), + (0x1083C, 0x1083C), + (0x1083F, 0x1083F), +]; + +/// The code points whose Script_Extensions include Cypriot. +pub static SCRIPT_EXT_CYPRIOT: &[(u32, u32)] = &[ + (0x10100, 0x10102), + (0x10107, 0x10133), + (0x10137, 0x1013F), + (0x10800, 0x10805), + (0x10808, 0x10808), + (0x1080A, 0x10835), + (0x10837, 0x10838), + (0x1083C, 0x1083C), + (0x1083F, 0x1083F), +]; + +/// The code points whose Script is Cypro_Minoan. +pub static SCRIPT_CYPRO_MINOAN: &[(u32, u32)] = &[(0x12F90, 0x12FF2)]; + +/// The code points whose Script_Extensions include Cypro_Minoan. +pub static SCRIPT_EXT_CYPRO_MINOAN: &[(u32, u32)] = &[(0x10100, 0x10101), (0x12F90, 0x12FF2)]; + +/// The code points whose Script is Cyrillic. +pub static SCRIPT_CYRILLIC: &[(u32, u32)] = &[ + (0x400, 0x484), + (0x487, 0x52F), + (0x1C80, 0x1C8A), + (0x1D2B, 0x1D2B), + (0x1D78, 0x1D78), + (0x2DE0, 0x2DFF), + (0xA640, 0xA69F), + (0xFE2E, 0xFE2F), + (0x1E030, 0x1E06D), + (0x1E08F, 0x1E08F), +]; + +/// The code points whose Script_Extensions include Cyrillic. +pub static SCRIPT_EXT_CYRILLIC: &[(u32, u32)] = &[ + (0x2BC, 0x2BC), + (0x300, 0x302), + (0x304, 0x304), + (0x306, 0x306), + (0x308, 0x308), + (0x30B, 0x30B), + (0x311, 0x311), + (0x400, 0x52F), + (0x1C80, 0x1C8A), + (0x1D2B, 0x1D2B), + (0x1D78, 0x1D78), + (0x1DF8, 0x1DF8), + (0x2DE0, 0x2DFF), + (0x2E43, 0x2E43), + (0xA640, 0xA69F), + (0xFE2E, 0xFE2F), + (0x1E030, 0x1E06D), + (0x1E08F, 0x1E08F), +]; + +/// The code points whose Script is Deseret. +pub static SCRIPT_DESERET: &[(u32, u32)] = &[(0x10400, 0x1044F)]; + +/// The code points whose Script is Devanagari. +pub static SCRIPT_DEVANAGARI: &[(u32, u32)] = &[ + (0x900, 0x950), + (0x955, 0x963), + (0x966, 0x97F), + (0xA8E0, 0xA8FF), + (0x11B00, 0x11B0A), +]; + +/// The code points whose Script_Extensions include Devanagari. +pub static SCRIPT_EXT_DEVANAGARI: &[(u32, u32)] = &[ + (0x2BC, 0x2BC), + (0x900, 0x952), + (0x955, 0x97F), + (0x1CD0, 0x1CF4), + (0x1CF8, 0x1CF9), + (0x20F0, 0x20F0), + (0xA830, 0xA839), + (0xA8E0, 0xA8FF), + (0x11B00, 0x11B0A), +]; + +/// The code points whose Script is Dives_Akuru. +pub static SCRIPT_DIVES_AKURU: &[(u32, u32)] = &[ + (0x11900, 0x11906), + (0x11909, 0x11909), + (0x1190C, 0x11913), + (0x11915, 0x11916), + (0x11918, 0x11935), + (0x11937, 0x11938), + (0x1193B, 0x11946), + (0x11950, 0x11959), +]; + +/// The code points whose Script is Dogra. +pub static SCRIPT_DOGRA: &[(u32, u32)] = &[(0x11800, 0x1183B)]; + +/// The code points whose Script_Extensions include Dogra. +pub static SCRIPT_EXT_DOGRA: &[(u32, u32)] = + &[(0x964, 0x96F), (0xA830, 0xA839), (0x11800, 0x1183B)]; + +/// The code points whose Script is Duployan. +pub static SCRIPT_DUPLOYAN: &[(u32, u32)] = &[ + (0x1BC00, 0x1BC6A), + (0x1BC70, 0x1BC7C), + (0x1BC80, 0x1BC88), + (0x1BC90, 0x1BC99), + (0x1BC9C, 0x1BC9F), +]; + +/// The code points whose Script_Extensions include Duployan. +pub static SCRIPT_EXT_DUPLOYAN: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x307, 0x308), + (0x30A, 0x30A), + (0x323, 0x324), + (0x2E3C, 0x2E3C), + (0x1BC00, 0x1BC6A), + (0x1BC70, 0x1BC7C), + (0x1BC80, 0x1BC88), + (0x1BC90, 0x1BC99), + (0x1BC9C, 0x1BCA3), +]; + +/// The code points whose Script is Egyptian_Hieroglyphs. +pub static SCRIPT_EGYPTIAN_HIEROGLYPHS: &[(u32, u32)] = &[(0x13000, 0x13455), (0x13460, 0x143FA)]; + +/// The code points whose Script is Elbasan. +pub static SCRIPT_ELBASAN: &[(u32, u32)] = &[(0x10500, 0x10527)]; + +/// The code points whose Script_Extensions include Elbasan. +pub static SCRIPT_EXT_ELBASAN: &[(u32, u32)] = &[(0xB7, 0xB7), (0x305, 0x305), (0x10500, 0x10527)]; + +/// The code points whose Script is Elymaic. +pub static SCRIPT_ELYMAIC: &[(u32, u32)] = &[(0x10FE0, 0x10FF6)]; + +/// The code points whose Script is Ethiopic. +pub static SCRIPT_ETHIOPIC: &[(u32, u32)] = &[ + (0x1200, 0x1248), + (0x124A, 0x124D), + (0x1250, 0x1256), + (0x1258, 0x1258), + (0x125A, 0x125D), + (0x1260, 0x1288), + (0x128A, 0x128D), + (0x1290, 0x12B0), + (0x12B2, 0x12B5), + (0x12B8, 0x12BE), + (0x12C0, 0x12C0), + (0x12C2, 0x12C5), + (0x12C8, 0x12D6), + (0x12D8, 0x1310), + (0x1312, 0x1315), + (0x1318, 0x135A), + (0x135D, 0x137C), + (0x1380, 0x1399), + (0x2D80, 0x2D96), + (0x2DA0, 0x2DA6), + (0x2DA8, 0x2DAE), + (0x2DB0, 0x2DB6), + (0x2DB8, 0x2DBE), + (0x2DC0, 0x2DC6), + (0x2DC8, 0x2DCE), + (0x2DD0, 0x2DD6), + (0x2DD8, 0x2DDE), + (0xAB01, 0xAB06), + (0xAB09, 0xAB0E), + (0xAB11, 0xAB16), + (0xAB20, 0xAB26), + (0xAB28, 0xAB2E), + (0x1E7E0, 0x1E7E6), + (0x1E7E8, 0x1E7EB), + (0x1E7ED, 0x1E7EE), + (0x1E7F0, 0x1E7FE), +]; + +/// The code points whose Script_Extensions include Ethiopic. +pub static SCRIPT_EXT_ETHIOPIC: &[(u32, u32)] = &[ + (0x30E, 0x30E), + (0x1200, 0x1248), + (0x124A, 0x124D), + (0x1250, 0x1256), + (0x1258, 0x1258), + (0x125A, 0x125D), + (0x1260, 0x1288), + (0x128A, 0x128D), + (0x1290, 0x12B0), + (0x12B2, 0x12B5), + (0x12B8, 0x12BE), + (0x12C0, 0x12C0), + (0x12C2, 0x12C5), + (0x12C8, 0x12D6), + (0x12D8, 0x1310), + (0x1312, 0x1315), + (0x1318, 0x135A), + (0x135D, 0x137C), + (0x1380, 0x1399), + (0x2D80, 0x2D96), + (0x2DA0, 0x2DA6), + (0x2DA8, 0x2DAE), + (0x2DB0, 0x2DB6), + (0x2DB8, 0x2DBE), + (0x2DC0, 0x2DC6), + (0x2DC8, 0x2DCE), + (0x2DD0, 0x2DD6), + (0x2DD8, 0x2DDE), + (0xAB01, 0xAB06), + (0xAB09, 0xAB0E), + (0xAB11, 0xAB16), + (0xAB20, 0xAB26), + (0xAB28, 0xAB2E), + (0x1E7E0, 0x1E7E6), + (0x1E7E8, 0x1E7EB), + (0x1E7ED, 0x1E7EE), + (0x1E7F0, 0x1E7FE), +]; + +/// The code points whose Script is Garay. +pub static SCRIPT_GARAY: &[(u32, u32)] = + &[(0x10D40, 0x10D65), (0x10D69, 0x10D85), (0x10D8E, 0x10D8F)]; + +/// The code points whose Script_Extensions include Garay. +pub static SCRIPT_EXT_GARAY: &[(u32, u32)] = &[ + (0x60C, 0x60C), + (0x61B, 0x61B), + (0x61F, 0x61F), + (0x10D40, 0x10D65), + (0x10D69, 0x10D85), + (0x10D8E, 0x10D8F), +]; + +/// The code points whose Script is Georgian. +pub static SCRIPT_GEORGIAN: &[(u32, u32)] = &[ + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10D0, 0x10FA), + (0x10FC, 0x10FF), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), +]; + +/// The code points whose Script_Extensions include Georgian. +pub static SCRIPT_EXT_GEORGIAN: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x589, 0x589), + (0x10A0, 0x10C5), + (0x10C7, 0x10C7), + (0x10CD, 0x10CD), + (0x10D0, 0x10FF), + (0x1C90, 0x1CBA), + (0x1CBD, 0x1CBF), + (0x205A, 0x205A), + (0x2D00, 0x2D25), + (0x2D27, 0x2D27), + (0x2D2D, 0x2D2D), + (0x2E31, 0x2E31), +]; + +/// The code points whose Script is Glagolitic. +pub static SCRIPT_GLAGOLITIC: &[(u32, u32)] = &[ + (0x2C00, 0x2C5F), + (0x1E000, 0x1E006), + (0x1E008, 0x1E018), + (0x1E01B, 0x1E021), + (0x1E023, 0x1E024), + (0x1E026, 0x1E02A), +]; + +/// The code points whose Script_Extensions include Glagolitic. +pub static SCRIPT_EXT_GLAGOLITIC: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x303, 0x303), + (0x305, 0x305), + (0x484, 0x484), + (0x487, 0x487), + (0x589, 0x589), + (0x10FB, 0x10FB), + (0x205A, 0x205A), + (0x2C00, 0x2C5F), + (0x2E43, 0x2E43), + (0xA66F, 0xA66F), + (0x1E000, 0x1E006), + (0x1E008, 0x1E018), + (0x1E01B, 0x1E021), + (0x1E023, 0x1E024), + (0x1E026, 0x1E02A), +]; + +/// The code points whose Script is Gothic. +pub static SCRIPT_GOTHIC: &[(u32, u32)] = &[(0x10330, 0x1034A)]; + +/// The code points whose Script_Extensions include Gothic. +pub static SCRIPT_EXT_GOTHIC: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x304, 0x305), + (0x308, 0x308), + (0x331, 0x331), + (0x10330, 0x1034A), +]; + +/// The code points whose Script is Grantha. +pub static SCRIPT_GRANTHA: &[(u32, u32)] = &[ + (0x11300, 0x11303), + (0x11305, 0x1130C), + (0x1130F, 0x11310), + (0x11313, 0x11328), + (0x1132A, 0x11330), + (0x11332, 0x11333), + (0x11335, 0x11339), + (0x1133C, 0x11344), + (0x11347, 0x11348), + (0x1134B, 0x1134D), + (0x11350, 0x11350), + (0x11357, 0x11357), + (0x1135D, 0x11363), + (0x11366, 0x1136C), + (0x11370, 0x11374), +]; + +/// The code points whose Script_Extensions include Grantha. +pub static SCRIPT_EXT_GRANTHA: &[(u32, u32)] = &[ + (0x951, 0x952), + (0x964, 0x965), + (0xBE6, 0xBF3), + (0x1CD0, 0x1CD0), + (0x1CD2, 0x1CD3), + (0x1CF2, 0x1CF4), + (0x1CF8, 0x1CF9), + (0x20F0, 0x20F0), + (0x11300, 0x11303), + (0x11305, 0x1130C), + (0x1130F, 0x11310), + (0x11313, 0x11328), + (0x1132A, 0x11330), + (0x11332, 0x11333), + (0x11335, 0x11339), + (0x1133B, 0x11344), + (0x11347, 0x11348), + (0x1134B, 0x1134D), + (0x11350, 0x11350), + (0x11357, 0x11357), + (0x1135D, 0x11363), + (0x11366, 0x1136C), + (0x11370, 0x11374), + (0x11FD0, 0x11FD1), + (0x11FD3, 0x11FD3), +]; + +/// The code points whose Script is Greek. +pub static SCRIPT_GREEK: &[(u32, u32)] = &[ + (0x370, 0x373), + (0x375, 0x377), + (0x37A, 0x37D), + (0x37F, 0x37F), + (0x384, 0x384), + (0x386, 0x386), + (0x388, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x3A1), + (0x3A3, 0x3E1), + (0x3F0, 0x3FF), + (0x1D26, 0x1D2A), + (0x1D5D, 0x1D61), + (0x1D66, 0x1D6A), + (0x1DBF, 0x1DBF), + (0x1F00, 0x1F15), + (0x1F18, 0x1F1D), + (0x1F20, 0x1F45), + (0x1F48, 0x1F4D), + (0x1F50, 0x1F57), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FC4), + (0x1FC6, 0x1FD3), + (0x1FD6, 0x1FDB), + (0x1FDD, 0x1FEF), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FFE), + (0x2126, 0x2126), + (0xAB65, 0xAB65), + (0x10140, 0x1018E), + (0x101A0, 0x101A0), + (0x1D200, 0x1D245), + (0x1DFF3, 0x1DFF4), +]; + +/// The code points whose Script_Extensions include Greek. +pub static SCRIPT_EXT_GREEK: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x300, 0x301), + (0x304, 0x304), + (0x306, 0x306), + (0x308, 0x308), + (0x313, 0x313), + (0x342, 0x342), + (0x345, 0x345), + (0x370, 0x377), + (0x37A, 0x37D), + (0x37F, 0x37F), + (0x384, 0x384), + (0x386, 0x386), + (0x388, 0x38A), + (0x38C, 0x38C), + (0x38E, 0x3A1), + (0x3A3, 0x3E1), + (0x3F0, 0x3FF), + (0x1D26, 0x1D2A), + (0x1D5D, 0x1D61), + (0x1D66, 0x1D6A), + (0x1DBF, 0x1DC1), + (0x1F00, 0x1F15), + (0x1F18, 0x1F1D), + (0x1F20, 0x1F45), + (0x1F48, 0x1F4D), + (0x1F50, 0x1F57), + (0x1F59, 0x1F59), + (0x1F5B, 0x1F5B), + (0x1F5D, 0x1F5D), + (0x1F5F, 0x1F7D), + (0x1F80, 0x1FB4), + (0x1FB6, 0x1FC4), + (0x1FC6, 0x1FD3), + (0x1FD6, 0x1FDB), + (0x1FDD, 0x1FEF), + (0x1FF2, 0x1FF4), + (0x1FF6, 0x1FFE), + (0x205D, 0x205D), + (0x2126, 0x2126), + (0xAB65, 0xAB65), + (0x10140, 0x1018E), + (0x101A0, 0x101A0), + (0x1D200, 0x1D245), + (0x1DFF3, 0x1DFF4), +]; + +/// The code points whose Script is Gujarati. +pub static SCRIPT_GUJARATI: &[(u32, u32)] = &[ + (0xA81, 0xA83), + (0xA85, 0xA8D), + (0xA8F, 0xA91), + (0xA93, 0xAA8), + (0xAAA, 0xAB0), + (0xAB2, 0xAB3), + (0xAB5, 0xAB9), + (0xABC, 0xAC5), + (0xAC7, 0xAC9), + (0xACB, 0xACD), + (0xAD0, 0xAD0), + (0xAE0, 0xAE3), + (0xAE6, 0xAF1), + (0xAF9, 0xAFF), +]; + +/// The code points whose Script_Extensions include Gujarati. +pub static SCRIPT_EXT_GUJARATI: &[(u32, u32)] = &[ + (0x951, 0x952), + (0x964, 0x965), + (0xA81, 0xA83), + (0xA85, 0xA8D), + (0xA8F, 0xA91), + (0xA93, 0xAA8), + (0xAAA, 0xAB0), + (0xAB2, 0xAB3), + (0xAB5, 0xAB9), + (0xABC, 0xAC5), + (0xAC7, 0xAC9), + (0xACB, 0xACD), + (0xAD0, 0xAD0), + (0xAE0, 0xAE3), + (0xAE6, 0xAF1), + (0xAF9, 0xAFF), + (0xA830, 0xA839), +]; + +/// The code points whose Script is Gunjala_Gondi. +pub static SCRIPT_GUNJALA_GONDI: &[(u32, u32)] = &[ + (0x11D60, 0x11D65), + (0x11D67, 0x11D68), + (0x11D6A, 0x11D8E), + (0x11D90, 0x11D91), + (0x11D93, 0x11D98), + (0x11DA0, 0x11DA9), +]; + +/// The code points whose Script_Extensions include Gunjala_Gondi. +pub static SCRIPT_EXT_GUNJALA_GONDI: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x964, 0x965), + (0x11D60, 0x11D65), + (0x11D67, 0x11D68), + (0x11D6A, 0x11D8E), + (0x11D90, 0x11D91), + (0x11D93, 0x11D98), + (0x11DA0, 0x11DA9), +]; + +/// The code points whose Script is Gurmukhi. +pub static SCRIPT_GURMUKHI: &[(u32, u32)] = &[ + (0xA01, 0xA03), + (0xA05, 0xA0A), + (0xA0F, 0xA10), + (0xA13, 0xA28), + (0xA2A, 0xA30), + (0xA32, 0xA33), + (0xA35, 0xA36), + (0xA38, 0xA39), + (0xA3C, 0xA3C), + (0xA3E, 0xA42), + (0xA47, 0xA48), + (0xA4B, 0xA4D), + (0xA51, 0xA51), + (0xA59, 0xA5C), + (0xA5E, 0xA5E), + (0xA66, 0xA76), +]; + +/// The code points whose Script_Extensions include Gurmukhi. +pub static SCRIPT_EXT_GURMUKHI: &[(u32, u32)] = &[ + (0x951, 0x952), + (0x964, 0x965), + (0xA01, 0xA03), + (0xA05, 0xA0A), + (0xA0F, 0xA10), + (0xA13, 0xA28), + (0xA2A, 0xA30), + (0xA32, 0xA33), + (0xA35, 0xA36), + (0xA38, 0xA39), + (0xA3C, 0xA3C), + (0xA3E, 0xA42), + (0xA47, 0xA48), + (0xA4B, 0xA4D), + (0xA51, 0xA51), + (0xA59, 0xA5C), + (0xA5E, 0xA5E), + (0xA66, 0xA76), + (0xA830, 0xA839), +]; + +/// The code points whose Script is Gurung_Khema. +pub static SCRIPT_GURUNG_KHEMA: &[(u32, u32)] = &[(0x16100, 0x16139)]; + +/// The code points whose Script_Extensions include Gurung_Khema. +pub static SCRIPT_EXT_GURUNG_KHEMA: &[(u32, u32)] = &[(0x965, 0x965), (0x16100, 0x16139)]; + +/// The code points whose Script is Han. +pub static SCRIPT_HAN: &[(u32, u32)] = &[ + (0x2E80, 0x2E99), + (0x2E9B, 0x2EF3), + (0x2F00, 0x2FD5), + (0x3005, 0x3005), + (0x3007, 0x3007), + (0x3021, 0x3029), + (0x3038, 0x303B), + (0x3400, 0x4DBF), + (0x4E00, 0x9FFF), + (0xF900, 0xFA6D), + (0xFA70, 0xFAD9), + (0x16FE2, 0x16FE3), + (0x16FF0, 0x16FF6), + (0x20000, 0x2A6DF), + (0x2A700, 0x2B81E), + (0x2B820, 0x2CEAD), + (0x2CEB0, 0x2EBE0), + (0x2EBF0, 0x2EE5D), + (0x2F800, 0x2FA1D), + (0x30000, 0x3134A), + (0x31350, 0x33479), +]; + +/// The code points whose Script_Extensions include Han. +pub static SCRIPT_EXT_HAN: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x2E80, 0x2E99), + (0x2E9B, 0x2EF3), + (0x2F00, 0x2FD5), + (0x2FF0, 0x2FFF), + (0x3001, 0x3003), + (0x3005, 0x3011), + (0x3013, 0x301F), + (0x3021, 0x302D), + (0x3030, 0x3030), + (0x3037, 0x303F), + (0x30FB, 0x30FB), + (0x3190, 0x319F), + (0x31C0, 0x31E5), + (0x31EF, 0x31EF), + (0x3220, 0x3247), + (0x3280, 0x32B0), + (0x32C0, 0x32CB), + (0x32FF, 0x32FF), + (0x3358, 0x3370), + (0x337B, 0x337F), + (0x33E0, 0x33FE), + (0x3400, 0x4DBF), + (0x4E00, 0x9FFF), + (0xA700, 0xA707), + (0xF900, 0xFA6D), + (0xFA70, 0xFAD9), + (0xFE45, 0xFE46), + (0xFF61, 0xFF65), + (0x16FE2, 0x16FE3), + (0x16FF0, 0x16FF6), + (0x1D360, 0x1D371), + (0x1F250, 0x1F251), + (0x20000, 0x2A6DF), + (0x2A700, 0x2B81E), + (0x2B820, 0x2CEAD), + (0x2CEB0, 0x2EBE0), + (0x2EBF0, 0x2EE5D), + (0x2F800, 0x2FA1D), + (0x30000, 0x3134A), + (0x31350, 0x33479), +]; + +/// The code points whose Script is Hangul. +pub static SCRIPT_HANGUL: &[(u32, u32)] = &[ + (0x1100, 0x11FF), + (0x302E, 0x302F), + (0x3131, 0x318E), + (0x3200, 0x321E), + (0x3260, 0x327E), + (0xA960, 0xA97C), + (0xAC00, 0xD7A3), + (0xD7B0, 0xD7C6), + (0xD7CB, 0xD7FB), + (0xFFA0, 0xFFBE), + (0xFFC2, 0xFFC7), + (0xFFCA, 0xFFCF), + (0xFFD2, 0xFFD7), + (0xFFDA, 0xFFDC), +]; + +/// The code points whose Script_Extensions include Hangul. +pub static SCRIPT_EXT_HANGUL: &[(u32, u32)] = &[ + (0x1100, 0x11FF), + (0x3001, 0x3003), + (0x3008, 0x3011), + (0x3013, 0x301F), + (0x302E, 0x3030), + (0x3037, 0x3037), + (0x30FB, 0x30FB), + (0x3131, 0x318E), + (0x3200, 0x321E), + (0x3260, 0x327E), + (0xA960, 0xA97C), + (0xAC00, 0xD7A3), + (0xD7B0, 0xD7C6), + (0xD7CB, 0xD7FB), + (0xFE45, 0xFE46), + (0xFF61, 0xFF65), + (0xFFA0, 0xFFBE), + (0xFFC2, 0xFFC7), + (0xFFCA, 0xFFCF), + (0xFFD2, 0xFFD7), + (0xFFDA, 0xFFDC), +]; + +/// The code points whose Script is Hanifi_Rohingya. +pub static SCRIPT_HANIFI_ROHINGYA: &[(u32, u32)] = &[(0x10D00, 0x10D27), (0x10D30, 0x10D39)]; + +/// The code points whose Script_Extensions include Hanifi_Rohingya. +pub static SCRIPT_EXT_HANIFI_ROHINGYA: &[(u32, u32)] = &[ + (0x60C, 0x60C), + (0x61B, 0x61B), + (0x61F, 0x61F), + (0x640, 0x640), + (0x6D4, 0x6D4), + (0x10D00, 0x10D27), + (0x10D30, 0x10D39), +]; + +/// The code points whose Script is Hanunoo. +pub static SCRIPT_HANUNOO: &[(u32, u32)] = &[(0x1720, 0x1734)]; + +/// The code points whose Script_Extensions include Hanunoo. +pub static SCRIPT_EXT_HANUNOO: &[(u32, u32)] = &[(0x1720, 0x1736)]; + +/// The code points whose Script is Hatran. +pub static SCRIPT_HATRAN: &[(u32, u32)] = + &[(0x108E0, 0x108F2), (0x108F4, 0x108F5), (0x108FB, 0x108FF)]; + +/// The code points whose Script is Hebrew. +pub static SCRIPT_HEBREW: &[(u32, u32)] = &[ + (0x591, 0x5C9), + (0x5D0, 0x5EA), + (0x5EF, 0x5F4), + (0xFB1D, 0xFB36), + (0xFB38, 0xFB3C), + (0xFB3E, 0xFB3E), + (0xFB40, 0xFB41), + (0xFB43, 0xFB44), + (0xFB46, 0xFB4F), +]; + +/// The code points whose Script_Extensions include Hebrew. +pub static SCRIPT_EXT_HEBREW: &[(u32, u32)] = &[ + (0x307, 0x308), + (0x591, 0x5C9), + (0x5D0, 0x5EA), + (0x5EF, 0x5F4), + (0xFB1D, 0xFB36), + (0xFB38, 0xFB3C), + (0xFB3E, 0xFB3E), + (0xFB40, 0xFB41), + (0xFB43, 0xFB44), + (0xFB46, 0xFB4F), +]; + +/// The code points whose Script is Hiragana. +pub static SCRIPT_HIRAGANA: &[(u32, u32)] = &[ + (0x3041, 0x3096), + (0x309D, 0x309F), + (0x1B001, 0x1B11F), + (0x1B123, 0x1B123), + (0x1B132, 0x1B132), + (0x1B150, 0x1B152), + (0x1F200, 0x1F200), +]; + +/// The code points whose Script_Extensions include Hiragana. +pub static SCRIPT_EXT_HIRAGANA: &[(u32, u32)] = &[ + (0x3001, 0x3003), + (0x3008, 0x3011), + (0x3013, 0x301F), + (0x3030, 0x3035), + (0x3037, 0x3037), + (0x303C, 0x303D), + (0x3041, 0x3096), + (0x3099, 0x30A0), + (0x30FB, 0x30FC), + (0xFE45, 0xFE46), + (0xFF61, 0xFF65), + (0xFF70, 0xFF70), + (0xFF9E, 0xFF9F), + (0x1B001, 0x1B11F), + (0x1B123, 0x1B123), + (0x1B132, 0x1B132), + (0x1B150, 0x1B152), + (0x1F200, 0x1F200), +]; + +/// The code points whose Script is Imperial_Aramaic. +pub static SCRIPT_IMPERIAL_ARAMAIC: &[(u32, u32)] = &[(0x10840, 0x10855), (0x10857, 0x1085F)]; + +/// The code points whose Script is Inherited. +pub static SCRIPT_INHERITED: &[(u32, u32)] = &[ + (0x300, 0x36F), + (0x485, 0x486), + (0x64B, 0x655), + (0x670, 0x670), + (0x951, 0x954), + (0x1AB0, 0x1AF0), + (0x1CD0, 0x1CD2), + (0x1CD4, 0x1CE0), + (0x1CE2, 0x1CE8), + (0x1CED, 0x1CED), + (0x1CF4, 0x1CF4), + (0x1CF8, 0x1CF9), + (0x1DC0, 0x1DFF), + (0x200C, 0x200D), + (0x20D0, 0x20F0), + (0x302A, 0x302D), + (0x3099, 0x309A), + (0xFE00, 0xFE0F), + (0xFE20, 0xFE2D), + (0x101FD, 0x101FD), + (0x102E0, 0x102E0), + (0x1133B, 0x1133B), + (0x1CF00, 0x1CF2D), + (0x1CF30, 0x1CF46), + (0x1D127, 0x1D128), + (0x1D167, 0x1D169), + (0x1D17B, 0x1D182), + (0x1D185, 0x1D18B), + (0x1D1AA, 0x1D1AD), + (0x1D25B, 0x1D25C), + (0xE0100, 0xE01EF), +]; + +/// The code points whose Script_Extensions include Inherited. +pub static SCRIPT_EXT_INHERITED: &[(u32, u32)] = &[ + (0x30F, 0x30F), + (0x312, 0x312), + (0x314, 0x322), + (0x326, 0x32C), + (0x32F, 0x32F), + (0x332, 0x341), + (0x343, 0x344), + (0x346, 0x357), + (0x359, 0x35D), + (0x35F, 0x362), + (0x953, 0x954), + (0x1AB0, 0x1AF0), + (0x1DC2, 0x1DF7), + (0x1DF9, 0x1DF9), + (0x1DFB, 0x1DFF), + (0x200C, 0x200D), + (0x20D0, 0x20EF), + (0xFE00, 0xFE0F), + (0xFE20, 0xFE2D), + (0x101FD, 0x101FD), + (0x1CF00, 0x1CF2D), + (0x1CF30, 0x1CF46), + (0x1D127, 0x1D128), + (0x1D167, 0x1D169), + (0x1D17B, 0x1D182), + (0x1D185, 0x1D18B), + (0x1D1AA, 0x1D1AD), + (0x1D25B, 0x1D25C), + (0xE0100, 0xE01EF), +]; + +/// The code points whose Script is Inscriptional_Pahlavi. +pub static SCRIPT_INSCRIPTIONAL_PAHLAVI: &[(u32, u32)] = &[(0x10B60, 0x10B72), (0x10B78, 0x10B7F)]; + +/// The code points whose Script is Inscriptional_Parthian. +pub static SCRIPT_INSCRIPTIONAL_PARTHIAN: &[(u32, u32)] = &[(0x10B40, 0x10B55), (0x10B58, 0x10B5F)]; + +/// The code points whose Script is Javanese. +pub static SCRIPT_JAVANESE: &[(u32, u32)] = &[(0xA980, 0xA9CD), (0xA9D0, 0xA9D9), (0xA9DE, 0xA9DF)]; + +/// The code points whose Script_Extensions include Javanese. +pub static SCRIPT_EXT_JAVANESE: &[(u32, u32)] = + &[(0xA980, 0xA9CD), (0xA9CF, 0xA9D9), (0xA9DE, 0xA9DF)]; + +/// The code points whose Script is Jurchen. +pub static SCRIPT_JURCHEN: &[(u32, u32)] = &[(0x18E00, 0x19191), (0x191A0, 0x191D2)]; + +/// The code points whose Script is Kaithi. +pub static SCRIPT_KAITHI: &[(u32, u32)] = &[(0x11080, 0x110C2), (0x110CD, 0x110CD)]; + +/// The code points whose Script_Extensions include Kaithi. +pub static SCRIPT_EXT_KAITHI: &[(u32, u32)] = &[ + (0x966, 0x96F), + (0x2E31, 0x2E31), + (0xA830, 0xA839), + (0x11080, 0x110C2), + (0x110CD, 0x110CD), +]; + +/// The code points whose Script is Kannada. +pub static SCRIPT_KANNADA: &[(u32, u32)] = &[ + (0xC80, 0xC8C), + (0xC8E, 0xC90), + (0xC92, 0xCA8), + (0xCAA, 0xCB3), + (0xCB5, 0xCB9), + (0xCBC, 0xCC4), + (0xCC6, 0xCC8), + (0xCCA, 0xCCD), + (0xCD5, 0xCD6), + (0xCDC, 0xCDE), + (0xCE0, 0xCE3), + (0xCE6, 0xCEF), + (0xCF1, 0xCF3), +]; + +/// The code points whose Script_Extensions include Kannada. +pub static SCRIPT_EXT_KANNADA: &[(u32, u32)] = &[ + (0x951, 0x952), + (0x964, 0x965), + (0xB83, 0xB83), + (0xC80, 0xC8C), + (0xC8E, 0xC90), + (0xC92, 0xCA8), + (0xCAA, 0xCB3), + (0xCB5, 0xCB9), + (0xCBC, 0xCC4), + (0xCC6, 0xCC8), + (0xCCA, 0xCCD), + (0xCD5, 0xCD6), + (0xCDC, 0xCDE), + (0xCE0, 0xCE3), + (0xCE6, 0xCEF), + (0xCF1, 0xCF3), + (0x1CD0, 0x1CD0), + (0x1CD2, 0x1CD3), + (0x1CDA, 0x1CDA), + (0x1CF2, 0x1CF2), + (0x1CF4, 0x1CF4), + (0xA830, 0xA835), +]; + +/// The code points whose Script is Katakana. +pub static SCRIPT_KATAKANA: &[(u32, u32)] = &[ + (0x30A1, 0x30FA), + (0x30FD, 0x30FF), + (0x31F0, 0x31FF), + (0x32D0, 0x32FE), + (0x3300, 0x3357), + (0xFF66, 0xFF6F), + (0xFF71, 0xFF9D), + (0x1AFF0, 0x1AFF3), + (0x1AFF5, 0x1AFFB), + (0x1AFFD, 0x1AFFE), + (0x1B000, 0x1B000), + (0x1B120, 0x1B122), + (0x1B124, 0x1B128), + (0x1B155, 0x1B155), + (0x1B164, 0x1B168), +]; + +/// The code points whose Script_Extensions include Katakana. +pub static SCRIPT_EXT_KATAKANA: &[(u32, u32)] = &[ + (0x305, 0x305), + (0x323, 0x323), + (0x3001, 0x3003), + (0x3008, 0x3011), + (0x3013, 0x301F), + (0x3030, 0x3035), + (0x3037, 0x3037), + (0x303C, 0x303D), + (0x3099, 0x309C), + (0x30A0, 0x30FF), + (0x31F0, 0x31FF), + (0x32D0, 0x32FE), + (0x3300, 0x3357), + (0xFE45, 0xFE46), + (0xFF61, 0xFF9F), + (0x1AFF0, 0x1AFF3), + (0x1AFF5, 0x1AFFB), + (0x1AFFD, 0x1AFFE), + (0x1B000, 0x1B000), + (0x1B120, 0x1B122), + (0x1B124, 0x1B128), + (0x1B155, 0x1B155), + (0x1B164, 0x1B168), +]; + +/// The code points whose Script is Katakana_Or_Hiragana. +pub static SCRIPT_KATAKANA_OR_HIRAGANA: &[(u32, u32)] = &[]; + +/// The code points whose Script is Kawi. +pub static SCRIPT_KAWI: &[(u32, u32)] = + &[(0x11F00, 0x11F10), (0x11F12, 0x11F3A), (0x11F3E, 0x11F5A)]; + +/// The code points whose Script is Kayah_Li. +pub static SCRIPT_KAYAH_LI: &[(u32, u32)] = &[(0xA900, 0xA92D), (0xA92F, 0xA92F)]; + +/// The code points whose Script_Extensions include Kayah_Li. +pub static SCRIPT_EXT_KAYAH_LI: &[(u32, u32)] = &[(0xA900, 0xA92F)]; + +/// The code points whose Script is Kharoshthi. +pub static SCRIPT_KHAROSHTHI: &[(u32, u32)] = &[ + (0x10A00, 0x10A03), + (0x10A05, 0x10A06), + (0x10A0C, 0x10A13), + (0x10A15, 0x10A17), + (0x10A19, 0x10A35), + (0x10A38, 0x10A3A), + (0x10A3F, 0x10A48), + (0x10A50, 0x10A58), +]; + +/// The code points whose Script is Khitan_Small_Script. +pub static SCRIPT_KHITAN_SMALL_SCRIPT: &[(u32, u32)] = + &[(0x16FE4, 0x16FE4), (0x18B00, 0x18CDA), (0x18CFF, 0x18CFF)]; + +/// The code points whose Script is Khmer. +pub static SCRIPT_KHMER: &[(u32, u32)] = &[ + (0x1780, 0x17DD), + (0x17E0, 0x17E9), + (0x17F0, 0x17F9), + (0x19E0, 0x19FF), +]; + +/// The code points whose Script is Khojki. +pub static SCRIPT_KHOJKI: &[(u32, u32)] = &[(0x11200, 0x11211), (0x11213, 0x11241)]; + +/// The code points whose Script_Extensions include Khojki. +pub static SCRIPT_EXT_KHOJKI: &[(u32, u32)] = &[ + (0xAE6, 0xAEF), + (0xA830, 0xA839), + (0x11200, 0x11211), + (0x11213, 0x11241), +]; + +/// The code points whose Script is Khudawadi. +pub static SCRIPT_KHUDAWADI: &[(u32, u32)] = &[(0x112B0, 0x112EA), (0x112F0, 0x112F9)]; + +/// The code points whose Script_Extensions include Khudawadi. +pub static SCRIPT_EXT_KHUDAWADI: &[(u32, u32)] = &[ + (0x964, 0x965), + (0xA830, 0xA839), + (0x112B0, 0x112EA), + (0x112F0, 0x112F9), +]; + +/// The code points whose Script is Kirat_Rai. +pub static SCRIPT_KIRAT_RAI: &[(u32, u32)] = &[(0x16D40, 0x16D79)]; + +/// The code points whose Script is Lao. +pub static SCRIPT_LAO: &[(u32, u32)] = &[ + (0xE81, 0xE82), + (0xE84, 0xE84), + (0xE86, 0xE8A), + (0xE8C, 0xEA3), + (0xEA5, 0xEA5), + (0xEA7, 0xEBD), + (0xEC0, 0xEC4), + (0xEC6, 0xEC6), + (0xEC8, 0xECE), + (0xED0, 0xED9), + (0xEDC, 0xEDF), +]; + +/// The code points whose Script is Latin. +pub static SCRIPT_LATIN: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0x61, 0x7A), + (0xAA, 0xAA), + (0xBA, 0xBA), + (0xC0, 0xD6), + (0xD8, 0xF6), + (0xF8, 0x2B8), + (0x2E0, 0x2E4), + (0x1D00, 0x1D25), + (0x1D2C, 0x1D5C), + (0x1D62, 0x1D65), + (0x1D6B, 0x1D77), + (0x1D79, 0x1DBE), + (0x1E00, 0x1EFF), + (0x2071, 0x2071), + (0x207F, 0x207F), + (0x2090, 0x209F), + (0x212A, 0x212B), + (0x2132, 0x2132), + (0x214E, 0x214E), + (0x2160, 0x2188), + (0x2C60, 0x2C7F), + (0xA722, 0xA787), + (0xA78B, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F1, 0xA7FF), + (0xAB30, 0xAB5A), + (0xAB5C, 0xAB64), + (0xAB66, 0xAB69), + (0xAB6C, 0xAB6D), + (0xFB00, 0xFB06), + (0xFF21, 0xFF3A), + (0xFF41, 0xFF5A), + (0x10780, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x1DF00, 0x1DF81), + (0x1DF90, 0x1DF96), + (0x1DFCD, 0x1DFF2), + (0x1DFF5, 0x1DFFF), +]; + +/// The code points whose Script_Extensions include Latin. +pub static SCRIPT_EXT_LATIN: &[(u32, u32)] = &[ + (0x41, 0x5A), + (0x61, 0x7A), + (0xAA, 0xAA), + (0xB7, 0xB7), + (0xBA, 0xBA), + (0xC0, 0xD6), + (0xD8, 0xF6), + (0xF8, 0x2B8), + (0x2BC, 0x2BC), + (0x2C7, 0x2C7), + (0x2C9, 0x2CB), + (0x2CD, 0x2CD), + (0x2D7, 0x2D7), + (0x2D9, 0x2D9), + (0x2E0, 0x2E4), + (0x300, 0x30E), + (0x310, 0x311), + (0x313, 0x313), + (0x323, 0x325), + (0x32D, 0x32E), + (0x330, 0x331), + (0x358, 0x358), + (0x35E, 0x35E), + (0x363, 0x36F), + (0x485, 0x486), + (0x951, 0x952), + (0x10FB, 0x10FB), + (0x1D00, 0x1D25), + (0x1D2C, 0x1D5C), + (0x1D62, 0x1D65), + (0x1D6B, 0x1D77), + (0x1D79, 0x1DBE), + (0x1DF8, 0x1DF8), + (0x1E00, 0x1EFF), + (0x202F, 0x202F), + (0x2071, 0x2071), + (0x207F, 0x207F), + (0x2090, 0x209F), + (0x20F0, 0x20F0), + (0x212A, 0x212B), + (0x2132, 0x2132), + (0x214E, 0x214E), + (0x2160, 0x2188), + (0x2C60, 0x2C7F), + (0x2E17, 0x2E17), + (0xA700, 0xA707), + (0xA722, 0xA787), + (0xA78B, 0xA7DD), + (0xA7E2, 0xA7E2), + (0xA7F1, 0xA7FF), + (0xA92E, 0xA92E), + (0xAB30, 0xAB5A), + (0xAB5C, 0xAB64), + (0xAB66, 0xAB69), + (0xAB6C, 0xAB6D), + (0xFB00, 0xFB06), + (0xFF21, 0xFF3A), + (0xFF41, 0xFF5A), + (0x10780, 0x10785), + (0x10787, 0x107B0), + (0x107B2, 0x107BF), + (0x1DF00, 0x1DF81), + (0x1DF90, 0x1DF96), + (0x1DFCD, 0x1DFF2), + (0x1DFF5, 0x1DFFF), +]; + +/// The code points whose Script is Lepcha. +pub static SCRIPT_LEPCHA: &[(u32, u32)] = &[(0x1C00, 0x1C37), (0x1C3B, 0x1C49), (0x1C4D, 0x1C4F)]; + +/// The code points whose Script is Limbu. +pub static SCRIPT_LIMBU: &[(u32, u32)] = &[ + (0x1900, 0x191E), + (0x1920, 0x192B), + (0x1930, 0x193B), + (0x1940, 0x1940), + (0x1944, 0x194F), +]; + +/// The code points whose Script_Extensions include Limbu. +pub static SCRIPT_EXT_LIMBU: &[(u32, u32)] = &[ + (0x965, 0x965), + (0x1900, 0x191E), + (0x1920, 0x192B), + (0x1930, 0x193B), + (0x1940, 0x1940), + (0x1944, 0x194F), +]; + +/// The code points whose Script is Linear_A. +pub static SCRIPT_LINEAR_A: &[(u32, u32)] = + &[(0x10600, 0x10736), (0x10740, 0x10755), (0x10760, 0x10767)]; + +/// The code points whose Script_Extensions include Linear_A. +pub static SCRIPT_EXT_LINEAR_A: &[(u32, u32)] = &[ + (0x10107, 0x10133), + (0x10600, 0x10736), + (0x10740, 0x10755), + (0x10760, 0x10767), +]; + +/// The code points whose Script is Linear_B. +pub static SCRIPT_LINEAR_B: &[(u32, u32)] = &[ + (0x10000, 0x1000B), + (0x1000D, 0x10026), + (0x10028, 0x1003A), + (0x1003C, 0x1003D), + (0x1003F, 0x1004D), + (0x10050, 0x1005D), + (0x10080, 0x100FA), +]; + +/// The code points whose Script_Extensions include Linear_B. +pub static SCRIPT_EXT_LINEAR_B: &[(u32, u32)] = &[ + (0x10000, 0x1000B), + (0x1000D, 0x10026), + (0x10028, 0x1003A), + (0x1003C, 0x1003D), + (0x1003F, 0x1004D), + (0x10050, 0x1005D), + (0x10080, 0x100FA), + (0x10100, 0x10102), + (0x10107, 0x10133), + (0x10137, 0x1013F), +]; + +/// The code points whose Script is Lisu. +pub static SCRIPT_LISU: &[(u32, u32)] = &[(0xA4D0, 0xA4FF), (0x11FB0, 0x11FB0)]; + +/// The code points whose Script_Extensions include Lisu. +pub static SCRIPT_EXT_LISU: &[(u32, u32)] = &[ + (0x2BC, 0x2BC), + (0x2CD, 0x2CD), + (0x300A, 0x300B), + (0xA4D0, 0xA4FF), + (0x11FB0, 0x11FB0), +]; + +/// The code points whose Script is Lycian. +pub static SCRIPT_LYCIAN: &[(u32, u32)] = &[(0x10280, 0x1029C)]; + +/// The code points whose Script_Extensions include Lycian. +pub static SCRIPT_EXT_LYCIAN: &[(u32, u32)] = &[(0x205A, 0x205A), (0x10280, 0x1029C)]; + +/// The code points whose Script is Lydian. +pub static SCRIPT_LYDIAN: &[(u32, u32)] = &[(0x10920, 0x10939), (0x1093F, 0x1093F)]; + +/// The code points whose Script_Extensions include Lydian. +pub static SCRIPT_EXT_LYDIAN: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x2E31, 0x2E31), + (0x10920, 0x10939), + (0x1093F, 0x1093F), +]; + +/// The code points whose Script is Mahajani. +pub static SCRIPT_MAHAJANI: &[(u32, u32)] = &[(0x11150, 0x11176)]; + +/// The code points whose Script_Extensions include Mahajani. +pub static SCRIPT_EXT_MAHAJANI: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x964, 0x96F), + (0xA830, 0xA839), + (0x11150, 0x11176), +]; + +/// The code points whose Script is Makasar. +pub static SCRIPT_MAKASAR: &[(u32, u32)] = &[(0x11EE0, 0x11EF8)]; + +/// The code points whose Script is Malayalam. +pub static SCRIPT_MALAYALAM: &[(u32, u32)] = &[ + (0xD00, 0xD0C), + (0xD0E, 0xD10), + (0xD12, 0xD44), + (0xD46, 0xD48), + (0xD4A, 0xD4F), + (0xD54, 0xD63), + (0xD66, 0xD7F), +]; + +/// The code points whose Script_Extensions include Malayalam. +pub static SCRIPT_EXT_MALAYALAM: &[(u32, u32)] = &[ + (0x951, 0x952), + (0x964, 0x965), + (0xB83, 0xB83), + (0xD00, 0xD0C), + (0xD0E, 0xD10), + (0xD12, 0xD44), + (0xD46, 0xD48), + (0xD4A, 0xD4F), + (0xD54, 0xD63), + (0xD66, 0xD7F), + (0x1CDA, 0x1CDA), + (0x1CF2, 0x1CF2), + (0xA830, 0xA832), +]; + +/// The code points whose Script is Mandaic. +pub static SCRIPT_MANDAIC: &[(u32, u32)] = &[(0x840, 0x85B), (0x85E, 0x85E)]; + +/// The code points whose Script_Extensions include Mandaic. +pub static SCRIPT_EXT_MANDAIC: &[(u32, u32)] = &[(0x640, 0x640), (0x840, 0x85B), (0x85E, 0x85E)]; + +/// The code points whose Script is Manichaean. +pub static SCRIPT_MANICHAEAN: &[(u32, u32)] = &[(0x10AC0, 0x10AE6), (0x10AEB, 0x10AF6)]; + +/// The code points whose Script_Extensions include Manichaean. +pub static SCRIPT_EXT_MANICHAEAN: &[(u32, u32)] = + &[(0x640, 0x640), (0x10AC0, 0x10AE6), (0x10AEB, 0x10AF6)]; + +/// The code points whose Script is Marchen. +pub static SCRIPT_MARCHEN: &[(u32, u32)] = + &[(0x11C70, 0x11C8F), (0x11C92, 0x11CA7), (0x11CA9, 0x11CB6)]; + +/// The code points whose Script is Masaram_Gondi. +pub static SCRIPT_MASARAM_GONDI: &[(u32, u32)] = &[ + (0x11D00, 0x11D06), + (0x11D08, 0x11D09), + (0x11D0B, 0x11D36), + (0x11D3A, 0x11D3A), + (0x11D3C, 0x11D3D), + (0x11D3F, 0x11D47), + (0x11D50, 0x11D59), +]; + +/// The code points whose Script_Extensions include Masaram_Gondi. +pub static SCRIPT_EXT_MASARAM_GONDI: &[(u32, u32)] = &[ + (0x964, 0x965), + (0x11D00, 0x11D06), + (0x11D08, 0x11D09), + (0x11D0B, 0x11D36), + (0x11D3A, 0x11D3A), + (0x11D3C, 0x11D3D), + (0x11D3F, 0x11D47), + (0x11D50, 0x11D59), +]; + +/// The code points whose Script is Medefaidrin. +pub static SCRIPT_MEDEFAIDRIN: &[(u32, u32)] = &[(0x16E40, 0x16E9A)]; + +/// The code points whose Script is Meetei_Mayek. +pub static SCRIPT_MEETEI_MAYEK: &[(u32, u32)] = + &[(0xAAE0, 0xAAF6), (0xABC0, 0xABED), (0xABF0, 0xABF9)]; + +/// The code points whose Script is Mende_Kikakui. +pub static SCRIPT_MENDE_KIKAKUI: &[(u32, u32)] = &[(0x1E800, 0x1E8C4), (0x1E8C7, 0x1E8D6)]; + +/// The code points whose Script is Meroitic_Cursive. +pub static SCRIPT_MEROITIC_CURSIVE: &[(u32, u32)] = + &[(0x109A0, 0x109B7), (0x109BC, 0x109CF), (0x109D2, 0x109FF)]; + +/// The code points whose Script is Meroitic_Hieroglyphs. +pub static SCRIPT_MEROITIC_HIEROGLYPHS: &[(u32, u32)] = &[(0x10980, 0x1099F)]; + +/// The code points whose Script_Extensions include Meroitic_Hieroglyphs. +pub static SCRIPT_EXT_MEROITIC_HIEROGLYPHS: &[(u32, u32)] = &[(0x205D, 0x205D), (0x10980, 0x1099F)]; + +/// The code points whose Script is Miao. +pub static SCRIPT_MIAO: &[(u32, u32)] = + &[(0x16F00, 0x16F4A), (0x16F4F, 0x16F87), (0x16F8F, 0x16F9F)]; + +/// The code points whose Script is Modi. +pub static SCRIPT_MODI: &[(u32, u32)] = &[(0x11600, 0x11644), (0x11650, 0x11659)]; + +/// The code points whose Script_Extensions include Modi. +pub static SCRIPT_EXT_MODI: &[(u32, u32)] = + &[(0xA830, 0xA839), (0x11600, 0x11644), (0x11650, 0x11659)]; + +/// The code points whose Script is Mongolian. +pub static SCRIPT_MONGOLIAN: &[(u32, u32)] = &[ + (0x1800, 0x1801), + (0x1804, 0x1804), + (0x1806, 0x1819), + (0x1820, 0x1878), + (0x1880, 0x18AA), + (0x11660, 0x1166C), +]; + +/// The code points whose Script_Extensions include Mongolian. +pub static SCRIPT_EXT_MONGOLIAN: &[(u32, u32)] = &[ + (0x1800, 0x1819), + (0x1820, 0x1878), + (0x1880, 0x18AA), + (0x202F, 0x202F), + (0x3001, 0x3002), + (0x3008, 0x300B), + (0x11660, 0x1166C), +]; + +/// The code points whose Script is Mro. +pub static SCRIPT_MRO: &[(u32, u32)] = + &[(0x16A40, 0x16A5E), (0x16A60, 0x16A69), (0x16A6E, 0x16A6F)]; + +/// The code points whose Script is Multani. +pub static SCRIPT_MULTANI: &[(u32, u32)] = &[ + (0x11280, 0x11286), + (0x11288, 0x11288), + (0x1128A, 0x1128D), + (0x1128F, 0x1129D), + (0x1129F, 0x112A9), +]; + +/// The code points whose Script_Extensions include Multani. +pub static SCRIPT_EXT_MULTANI: &[(u32, u32)] = &[ + (0xA66, 0xA6F), + (0x11280, 0x11286), + (0x11288, 0x11288), + (0x1128A, 0x1128D), + (0x1128F, 0x1129D), + (0x1129F, 0x112A9), +]; + +/// The code points whose Script is Myanmar. +pub static SCRIPT_MYANMAR: &[(u32, u32)] = &[ + (0x1000, 0x109F), + (0xA9E0, 0xA9FE), + (0xAA60, 0xAA7F), + (0x116D0, 0x116E3), +]; + +/// The code points whose Script_Extensions include Myanmar. +pub static SCRIPT_EXT_MYANMAR: &[(u32, u32)] = &[ + (0x1000, 0x109F), + (0xA92E, 0xA92E), + (0xA9E0, 0xA9FE), + (0xAA60, 0xAA7F), + (0x116D0, 0x116E3), +]; + +/// The code points whose Script is Nabataean. +pub static SCRIPT_NABATAEAN: &[(u32, u32)] = &[(0x10880, 0x1089E), (0x108A7, 0x108AF)]; + +/// The code points whose Script is Nag_Mundari. +pub static SCRIPT_NAG_MUNDARI: &[(u32, u32)] = &[(0x1E4D0, 0x1E4F9)]; + +/// The code points whose Script is Nandinagari. +pub static SCRIPT_NANDINAGARI: &[(u32, u32)] = + &[(0x119A0, 0x119A7), (0x119AA, 0x119D7), (0x119DA, 0x119E4)]; + +/// The code points whose Script_Extensions include Nandinagari. +pub static SCRIPT_EXT_NANDINAGARI: &[(u32, u32)] = &[ + (0x951, 0x951), + (0x964, 0x965), + (0xCE6, 0xCEF), + (0x1CE9, 0x1CE9), + (0x1CF2, 0x1CF2), + (0x1CFA, 0x1CFA), + (0xA830, 0xA835), + (0x119A0, 0x119A7), + (0x119AA, 0x119D7), + (0x119DA, 0x119E4), +]; + +/// The code points whose Script is New_Tai_Lue. +pub static SCRIPT_NEW_TAI_LUE: &[(u32, u32)] = &[ + (0x1980, 0x19AB), + (0x19B0, 0x19C9), + (0x19D0, 0x19DA), + (0x19DE, 0x19DF), +]; + +/// The code points whose Script is Newa. +pub static SCRIPT_NEWA: &[(u32, u32)] = &[(0x11400, 0x1145B), (0x1145D, 0x11461)]; + +/// The code points whose Script_Extensions include Newa. +pub static SCRIPT_EXT_NEWA: &[(u32, u32)] = &[ + (0x951, 0x952), + (0x1CD5, 0x1CD5), + (0x1CD7, 0x1CD8), + (0x1CE2, 0x1CE2), + (0x1CE9, 0x1CE9), + (0x1CEB, 0x1CEB), + (0x1CED, 0x1CED), + (0x11400, 0x1145B), + (0x1145D, 0x11461), +]; + +/// The code points whose Script is Nko. +pub static SCRIPT_NKO: &[(u32, u32)] = &[(0x7C0, 0x7FA), (0x7FD, 0x7FF)]; + +/// The code points whose Script_Extensions include Nko. +pub static SCRIPT_EXT_NKO: &[(u32, u32)] = &[ + (0x60C, 0x60C), + (0x61B, 0x61B), + (0x61F, 0x61F), + (0x7C0, 0x7FA), + (0x7FD, 0x7FF), + (0xFD3E, 0xFD3F), +]; + +/// The code points whose Script is Nushu. +pub static SCRIPT_NUSHU: &[(u32, u32)] = &[(0x16FE1, 0x16FE1), (0x1B170, 0x1B2FB)]; + +/// The code points whose Script is Nyiakeng_Puachue_Hmong. +pub static SCRIPT_NYIAKENG_PUACHUE_HMONG: &[(u32, u32)] = &[ + (0x1E100, 0x1E12C), + (0x1E130, 0x1E13D), + (0x1E140, 0x1E149), + (0x1E14E, 0x1E14F), +]; + +/// The code points whose Script is Ogham. +pub static SCRIPT_OGHAM: &[(u32, u32)] = &[(0x1680, 0x169C)]; + +/// The code points whose Script is Ol_Chiki. +pub static SCRIPT_OL_CHIKI: &[(u32, u32)] = &[(0x1C50, 0x1C7F)]; + +/// The code points whose Script is Ol_Onal. +pub static SCRIPT_OL_ONAL: &[(u32, u32)] = &[(0x1E5D0, 0x1E5FA), (0x1E5FF, 0x1E5FF)]; + +/// The code points whose Script_Extensions include Ol_Onal. +pub static SCRIPT_EXT_OL_ONAL: &[(u32, u32)] = + &[(0x964, 0x965), (0x1E5D0, 0x1E5FA), (0x1E5FF, 0x1E5FF)]; + +/// The code points whose Script is Old_Hungarian. +pub static SCRIPT_OLD_HUNGARIAN: &[(u32, u32)] = + &[(0x10C80, 0x10CB2), (0x10CC0, 0x10CF2), (0x10CFA, 0x10CFF)]; + +/// The code points whose Script_Extensions include Old_Hungarian. +pub static SCRIPT_EXT_OLD_HUNGARIAN: &[(u32, u32)] = &[ + (0x205A, 0x205A), + (0x205D, 0x205D), + (0x2E31, 0x2E31), + (0x2E41, 0x2E41), + (0x10C80, 0x10CB2), + (0x10CC0, 0x10CF2), + (0x10CFA, 0x10CFF), +]; + +/// The code points whose Script is Old_Italic. +pub static SCRIPT_OLD_ITALIC: &[(u32, u32)] = &[(0x10300, 0x10323), (0x1032D, 0x1032F)]; + +/// The code points whose Script is Old_North_Arabian. +pub static SCRIPT_OLD_NORTH_ARABIAN: &[(u32, u32)] = &[(0x10A80, 0x10A9F)]; + +/// The code points whose Script is Old_Permic. +pub static SCRIPT_OLD_PERMIC: &[(u32, u32)] = &[(0x10350, 0x1037A)]; + +/// The code points whose Script_Extensions include Old_Permic. +pub static SCRIPT_EXT_OLD_PERMIC: &[(u32, u32)] = &[ + (0xB7, 0xB7), + (0x300, 0x300), + (0x306, 0x308), + (0x313, 0x313), + (0x483, 0x483), + (0x10350, 0x1037A), +]; + +/// The code points whose Script is Old_Persian. +pub static SCRIPT_OLD_PERSIAN: &[(u32, u32)] = &[(0x103A0, 0x103C3), (0x103C8, 0x103D5)]; + +/// The code points whose Script is Old_Sogdian. +pub static SCRIPT_OLD_SOGDIAN: &[(u32, u32)] = &[(0x10F00, 0x10F27)]; + +/// The code points whose Script is Old_South_Arabian. +pub static SCRIPT_OLD_SOUTH_ARABIAN: &[(u32, u32)] = &[(0x10A60, 0x10A7F)]; + +/// The code points whose Script is Old_Turkic. +pub static SCRIPT_OLD_TURKIC: &[(u32, u32)] = &[(0x10C00, 0x10C48)]; + +/// The code points whose Script_Extensions include Old_Turkic. +pub static SCRIPT_EXT_OLD_TURKIC: &[(u32, u32)] = + &[(0x205A, 0x205A), (0x2E30, 0x2E30), (0x10C00, 0x10C48)]; + +/// The code points whose Script is Old_Uyghur. +pub static SCRIPT_OLD_UYGHUR: &[(u32, u32)] = &[(0x10F70, 0x10F89)]; + +/// The code points whose Script_Extensions include Old_Uyghur. +pub static SCRIPT_EXT_OLD_UYGHUR: &[(u32, u32)] = + &[(0x640, 0x640), (0x10AF2, 0x10AF2), (0x10F70, 0x10F89)]; + +/// The code points whose Script is Oriya. +pub static SCRIPT_ORIYA: &[(u32, u32)] = &[ + (0xB01, 0xB03), + (0xB05, 0xB0C), + (0xB0F, 0xB10), + (0xB13, 0xB28), + (0xB2A, 0xB30), + (0xB32, 0xB33), + (0xB35, 0xB39), + (0xB3C, 0xB44), + (0xB47, 0xB48), + (0xB4B, 0xB4D), + (0xB53, 0xB57), + (0xB5C, 0xB5D), + (0xB5F, 0xB63), + (0xB66, 0xB77), +]; + +/// The code points whose Script_Extensions include Oriya. +pub static SCRIPT_EXT_ORIYA: &[(u32, u32)] = &[ + (0x951, 0x952), + (0x964, 0x965), + (0xB01, 0xB03), + (0xB05, 0xB0C), + (0xB0F, 0xB10), + (0xB13, 0xB28), + (0xB2A, 0xB30), + (0xB32, 0xB33), + (0xB35, 0xB39), + (0xB3C, 0xB44), + (0xB47, 0xB48), + (0xB4B, 0xB4D), + (0xB53, 0xB57), + (0xB5C, 0xB5D), + (0xB5F, 0xB63), + (0xB66, 0xB77), + (0x1CDA, 0x1CDA), + (0x1CF2, 0x1CF2), +]; + +/// The code points whose Script is Osage. +pub static SCRIPT_OSAGE: &[(u32, u32)] = &[(0x104B0, 0x104D3), (0x104D8, 0x104FB)]; + +/// The code points whose Script_Extensions include Osage. +pub static SCRIPT_EXT_OSAGE: &[(u32, u32)] = &[ + (0x301, 0x301), + (0x304, 0x304), + (0x30B, 0x30B), + (0x358, 0x358), + (0x104B0, 0x104D3), + (0x104D8, 0x104FB), +]; + +/// The code points whose Script is Osmanya. +pub static SCRIPT_OSMANYA: &[(u32, u32)] = &[(0x10480, 0x1049D), (0x104A0, 0x104A9)]; + +/// The code points whose Script is Pahawh_Hmong. +pub static SCRIPT_PAHAWH_HMONG: &[(u32, u32)] = &[ + (0x16B00, 0x16B45), + (0x16B50, 0x16B59), + (0x16B5B, 0x16B61), + (0x16B63, 0x16B77), + (0x16B7D, 0x16B8F), +]; + +/// The code points whose Script is Palmyrene. +pub static SCRIPT_PALMYRENE: &[(u32, u32)] = &[(0x10860, 0x1087F)]; + +/// The code points whose Script is Pau_Cin_Hau. +pub static SCRIPT_PAU_CIN_HAU: &[(u32, u32)] = &[(0x11AC0, 0x11AF8)]; + +/// The code points whose Script is Phags_Pa. +pub static SCRIPT_PHAGS_PA: &[(u32, u32)] = &[(0xA840, 0xA877)]; + +/// The code points whose Script_Extensions include Phags_Pa. +pub static SCRIPT_EXT_PHAGS_PA: &[(u32, u32)] = &[ + (0x1802, 0x1803), + (0x1805, 0x1805), + (0x202F, 0x202F), + (0x3002, 0x3002), + (0xA840, 0xA877), +]; + +/// The code points whose Script is Phoenician. +pub static SCRIPT_PHOENICIAN: &[(u32, u32)] = &[(0x10900, 0x1091B), (0x1091F, 0x1091F)]; + +/// The code points whose Script is Proto_Cuneiform. +pub static SCRIPT_PROTO_CUNEIFORM: &[(u32, u32)] = &[(0x125A8, 0x1264B)]; + +/// The code points whose Script_Extensions include Proto_Cuneiform. +pub static SCRIPT_EXT_PROTO_CUNEIFORM: &[(u32, u32)] = + &[(0x12550, 0x12586), (0x1258C, 0x1258D), (0x1259A, 0x1264B)]; + +/// The code points whose Script is Psalter_Pahlavi. +pub static SCRIPT_PSALTER_PAHLAVI: &[(u32, u32)] = + &[(0x10B80, 0x10B91), (0x10B99, 0x10B9C), (0x10BA9, 0x10BAF)]; + +/// The code points whose Script_Extensions include Psalter_Pahlavi. +pub static SCRIPT_EXT_PSALTER_PAHLAVI: &[(u32, u32)] = &[ + (0x640, 0x640), + (0x10B80, 0x10B91), + (0x10B99, 0x10B9C), + (0x10BA9, 0x10BAF), +]; + +/// The code points whose Script is Rejang. +pub static SCRIPT_REJANG: &[(u32, u32)] = &[(0xA930, 0xA953), (0xA95F, 0xA95F)]; + +/// The code points whose Script is Runic. +pub static SCRIPT_RUNIC: &[(u32, u32)] = &[(0x16A0, 0x16EA), (0x16EE, 0x16F8)]; + +/// The code points whose Script_Extensions include Runic. +pub static SCRIPT_EXT_RUNIC: &[(u32, u32)] = &[(0x16A0, 0x16F8)]; + +/// The code points whose Script is Samaritan. +pub static SCRIPT_SAMARITAN: &[(u32, u32)] = &[(0x800, 0x82D), (0x830, 0x83E)]; + +/// The code points whose Script_Extensions include Samaritan. +pub static SCRIPT_EXT_SAMARITAN: &[(u32, u32)] = + &[(0x800, 0x82D), (0x830, 0x83E), (0x2E31, 0x2E31)]; + +/// The code points whose Script is Saurashtra. +pub static SCRIPT_SAURASHTRA: &[(u32, u32)] = &[(0xA880, 0xA8C5), (0xA8CE, 0xA8D9)]; + +/// The code points whose Script is Seal. +pub static SCRIPT_SEAL: &[(u32, u32)] = &[(0x3D000, 0x3FC3F)]; + +/// The code points whose Script is Sharada. +pub static SCRIPT_SHARADA: &[(u32, u32)] = &[(0x11180, 0x111DF), (0x11B60, 0x11B67)]; + +/// The code points whose Script_Extensions include Sharada. +pub static SCRIPT_EXT_SHARADA: &[(u32, u32)] = &[ + (0x951, 0x951), + (0x1CD7, 0x1CD7), + (0x1CD9, 0x1CD9), + (0x1CDC, 0x1CDD), + (0x1CE0, 0x1CE0), + (0x1CEA, 0x1CEA), + (0x1CED, 0x1CED), + (0xA830, 0xA835), + (0xA838, 0xA838), + (0x11180, 0x111DF), + (0x11B60, 0x11B67), +]; + +/// The code points whose Script is Shavian. +pub static SCRIPT_SHAVIAN: &[(u32, u32)] = &[(0x10450, 0x1047F)]; + +/// The code points whose Script_Extensions include Shavian. +pub static SCRIPT_EXT_SHAVIAN: &[(u32, u32)] = &[(0xB7, 0xB7), (0x10450, 0x1047F)]; + +/// The code points whose Script is Siddham. +pub static SCRIPT_SIDDHAM: &[(u32, u32)] = &[(0x11580, 0x115B5), (0x115B8, 0x115DD)]; + +/// The code points whose Script is Sidetic. +pub static SCRIPT_SIDETIC: &[(u32, u32)] = &[(0x10940, 0x10959)]; + +/// The code points whose Script is SignWriting. +pub static SCRIPT_SIGNWRITING: &[(u32, u32)] = + &[(0x1D800, 0x1DA8B), (0x1DA9B, 0x1DA9F), (0x1DAA1, 0x1DAAF)]; + +/// The code points whose Script is Sinhala. +pub static SCRIPT_SINHALA: &[(u32, u32)] = &[ + (0xD81, 0xD83), + (0xD85, 0xD96), + (0xD9A, 0xDB1), + (0xDB3, 0xDBB), + (0xDBD, 0xDBD), + (0xDC0, 0xDC6), + (0xDCA, 0xDCA), + (0xDCF, 0xDD4), + (0xDD6, 0xDD6), + (0xDD8, 0xDDF), + (0xDE6, 0xDEF), + (0xDF2, 0xDF4), + (0x111E1, 0x111F4), +]; + +/// The code points whose Script_Extensions include Sinhala. +pub static SCRIPT_EXT_SINHALA: &[(u32, u32)] = &[ + (0x964, 0x965), + (0xD81, 0xD83), + (0xD85, 0xD96), + (0xD9A, 0xDB1), + (0xDB3, 0xDBB), + (0xDBD, 0xDBD), + (0xDC0, 0xDC6), + (0xDCA, 0xDCA), + (0xDCF, 0xDD4), + (0xDD6, 0xDD6), + (0xDD8, 0xDDF), + (0xDE6, 0xDEF), + (0xDF2, 0xDF4), + (0x1CF2, 0x1CF2), + (0x111E1, 0x111F4), +]; + +/// The code points whose Script is Sogdian. +pub static SCRIPT_SOGDIAN: &[(u32, u32)] = &[(0x10F30, 0x10F59)]; + +/// The code points whose Script_Extensions include Sogdian. +pub static SCRIPT_EXT_SOGDIAN: &[(u32, u32)] = &[(0x640, 0x640), (0x10F30, 0x10F59)]; + +/// The code points whose Script is Sora_Sompeng. +pub static SCRIPT_SORA_SOMPENG: &[(u32, u32)] = &[(0x110D0, 0x110E8), (0x110F0, 0x110F9)]; + +/// The code points whose Script is Soyombo. +pub static SCRIPT_SOYOMBO: &[(u32, u32)] = &[(0x11A50, 0x11AA2)]; + +/// The code points whose Script is Sundanese. +pub static SCRIPT_SUNDANESE: &[(u32, u32)] = &[(0x1B80, 0x1BBF), (0x1CC0, 0x1CC7)]; + +/// The code points whose Script is Sunuwar. +pub static SCRIPT_SUNUWAR: &[(u32, u32)] = &[(0x11BC0, 0x11BE1), (0x11BF0, 0x11BF9)]; + +/// The code points whose Script_Extensions include Sunuwar. +pub static SCRIPT_EXT_SUNUWAR: &[(u32, u32)] = &[ + (0x300, 0x301), + (0x303, 0x303), + (0x30D, 0x30D), + (0x310, 0x310), + (0x32D, 0x32D), + (0x331, 0x331), + (0x11BC0, 0x11BE1), + (0x11BF0, 0x11BF9), +]; + +/// The code points whose Script is Syloti_Nagri. +pub static SCRIPT_SYLOTI_NAGRI: &[(u32, u32)] = &[(0xA800, 0xA82C)]; + +/// The code points whose Script_Extensions include Syloti_Nagri. +pub static SCRIPT_EXT_SYLOTI_NAGRI: &[(u32, u32)] = + &[(0x964, 0x965), (0x9E6, 0x9EF), (0xA800, 0xA82C)]; + +/// The code points whose Script is Syriac. +pub static SCRIPT_SYRIAC: &[(u32, u32)] = &[ + (0x700, 0x70D), + (0x70F, 0x74A), + (0x74D, 0x74F), + (0x860, 0x86A), +]; + +/// The code points whose Script_Extensions include Syriac. +pub static SCRIPT_EXT_SYRIAC: &[(u32, u32)] = &[ + (0x303, 0x304), + (0x307, 0x308), + (0x30A, 0x30A), + (0x323, 0x325), + (0x32D, 0x32E), + (0x330, 0x331), + (0x60C, 0x60C), + (0x61B, 0x61C), + (0x61F, 0x61F), + (0x640, 0x640), + (0x64B, 0x655), + (0x670, 0x670), + (0x700, 0x70D), + (0x70F, 0x74A), + (0x74D, 0x74F), + (0x860, 0x86A), + (0x1DF8, 0x1DF8), + (0x1DFA, 0x1DFA), +]; + +/// The code points whose Script is Tagalog. +pub static SCRIPT_TAGALOG: &[(u32, u32)] = &[(0x1700, 0x1715), (0x171F, 0x171F)]; + +/// The code points whose Script_Extensions include Tagalog. +pub static SCRIPT_EXT_TAGALOG: &[(u32, u32)] = + &[(0x1700, 0x1715), (0x171F, 0x171F), (0x1735, 0x1736)]; + +/// The code points whose Script is Tagbanwa. +pub static SCRIPT_TAGBANWA: &[(u32, u32)] = &[(0x1760, 0x176C), (0x176E, 0x1770), (0x1772, 0x1773)]; + +/// The code points whose Script_Extensions include Tagbanwa. +pub static SCRIPT_EXT_TAGBANWA: &[(u32, u32)] = &[ + (0x1735, 0x1736), + (0x1760, 0x176C), + (0x176E, 0x1770), + (0x1772, 0x1773), +]; + +/// The code points whose Script is Tai_Le. +pub static SCRIPT_TAI_LE: &[(u32, u32)] = &[(0x1950, 0x196D), (0x1970, 0x1974)]; + +/// The code points whose Script_Extensions include Tai_Le. +pub static SCRIPT_EXT_TAI_LE: &[(u32, u32)] = &[ + (0x300, 0x301), + (0x307, 0x308), + (0x30C, 0x30C), + (0x1040, 0x1049), + (0x1950, 0x196D), + (0x1970, 0x1974), +]; + +/// The code points whose Script is Tai_Tham. +pub static SCRIPT_TAI_THAM: &[(u32, u32)] = &[ + (0x1A20, 0x1A5E), + (0x1A60, 0x1A7C), + (0x1A7F, 0x1A89), + (0x1A90, 0x1A99), + (0x1AA0, 0x1AAD), +]; + +/// The code points whose Script is Tai_Viet. +pub static SCRIPT_TAI_VIET: &[(u32, u32)] = &[(0xAA80, 0xAAC2), (0xAADB, 0xAADF)]; + +/// The code points whose Script is Tai_Yo. +pub static SCRIPT_TAI_YO: &[(u32, u32)] = + &[(0x1E6C0, 0x1E6DE), (0x1E6E0, 0x1E6F5), (0x1E6FE, 0x1E6FF)]; + +/// The code points whose Script is Takri. +pub static SCRIPT_TAKRI: &[(u32, u32)] = &[(0x11680, 0x116B9), (0x116C0, 0x116C9)]; + +/// The code points whose Script_Extensions include Takri. +pub static SCRIPT_EXT_TAKRI: &[(u32, u32)] = &[ + (0x964, 0x965), + (0xA830, 0xA839), + (0x11680, 0x116B9), + (0x116C0, 0x116C9), +]; + +/// The code points whose Script is Tamil. +pub static SCRIPT_TAMIL: &[(u32, u32)] = &[ + (0xB82, 0xB83), + (0xB85, 0xB8A), + (0xB8E, 0xB90), + (0xB92, 0xB95), + (0xB99, 0xB9A), + (0xB9C, 0xB9C), + (0xB9E, 0xB9F), + (0xBA3, 0xBA4), + (0xBA8, 0xBAA), + (0xBAE, 0xBB9), + (0xBBE, 0xBC2), + (0xBC6, 0xBC8), + (0xBCA, 0xBCD), + (0xBD0, 0xBD0), + (0xBD7, 0xBD7), + (0xBE6, 0xBFA), + (0x11FC0, 0x11FF1), + (0x11FFF, 0x11FFF), +]; + +/// The code points whose Script_Extensions include Tamil. +pub static SCRIPT_EXT_TAMIL: &[(u32, u32)] = &[ + (0x951, 0x952), + (0x964, 0x965), + (0xB82, 0xB83), + (0xB85, 0xB8A), + (0xB8E, 0xB90), + (0xB92, 0xB95), + (0xB99, 0xB9A), + (0xB9C, 0xB9C), + (0xB9E, 0xB9F), + (0xBA3, 0xBA4), + (0xBA8, 0xBAA), + (0xBAE, 0xBB9), + (0xBBE, 0xBC2), + (0xBC6, 0xBC8), + (0xBCA, 0xBCD), + (0xBD0, 0xBD0), + (0xBD7, 0xBD7), + (0xBE6, 0xBFA), + (0x1CDA, 0x1CDA), + (0xA8F3, 0xA8F3), + (0x11301, 0x11301), + (0x11303, 0x11303), + (0x1133B, 0x1133C), + (0x11FC0, 0x11FF1), + (0x11FFF, 0x11FFF), +]; + +/// The code points whose Script is Tangsa. +pub static SCRIPT_TANGSA: &[(u32, u32)] = &[(0x16A70, 0x16ABE), (0x16AC0, 0x16AC9)]; + +/// The code points whose Script is Tangut. +pub static SCRIPT_TANGUT: &[(u32, u32)] = &[ + (0x16FE0, 0x16FE0), + (0x17000, 0x18AFF), + (0x18D00, 0x18D20), + (0x18D80, 0x18DF2), +]; + +/// The code points whose Script_Extensions include Tangut. +pub static SCRIPT_EXT_TANGUT: &[(u32, u32)] = &[ + (0x2FF0, 0x2FFF), + (0x31EF, 0x31EF), + (0x16FE0, 0x16FE0), + (0x17000, 0x18AFF), + (0x18D00, 0x18D20), + (0x18D80, 0x18DF2), +]; + +/// The code points whose Script is Telugu. +pub static SCRIPT_TELUGU: &[(u32, u32)] = &[ + (0xC00, 0xC0C), + (0xC0E, 0xC10), + (0xC12, 0xC28), + (0xC2A, 0xC39), + (0xC3C, 0xC44), + (0xC46, 0xC48), + (0xC4A, 0xC4D), + (0xC55, 0xC56), + (0xC58, 0xC5A), + (0xC5C, 0xC5D), + (0xC60, 0xC63), + (0xC66, 0xC6F), + (0xC77, 0xC7F), +]; + +/// The code points whose Script_Extensions include Telugu. +pub static SCRIPT_EXT_TELUGU: &[(u32, u32)] = &[ + (0x951, 0x952), + (0x964, 0x965), + (0xB83, 0xB83), + (0xC00, 0xC0C), + (0xC0E, 0xC10), + (0xC12, 0xC28), + (0xC2A, 0xC39), + (0xC3C, 0xC44), + (0xC46, 0xC48), + (0xC4A, 0xC4D), + (0xC55, 0xC56), + (0xC58, 0xC5A), + (0xC5C, 0xC5D), + (0xC60, 0xC63), + (0xC66, 0xC6F), + (0xC77, 0xC7F), + (0x1CD5, 0x1CD6), + (0x1CD8, 0x1CD8), + (0x1CDA, 0x1CDA), + (0x1CF2, 0x1CF2), +]; + +/// The code points whose Script is Thaana. +pub static SCRIPT_THAANA: &[(u32, u32)] = &[(0x780, 0x7B1)]; + +/// The code points whose Script_Extensions include Thaana. +pub static SCRIPT_EXT_THAANA: &[(u32, u32)] = &[ + (0x60C, 0x60C), + (0x61B, 0x61C), + (0x61F, 0x61F), + (0x660, 0x669), + (0x780, 0x7B1), + (0xFDF2, 0xFDF2), + (0xFDFD, 0xFDFD), +]; + +/// The code points whose Script is Thai. +pub static SCRIPT_THAI: &[(u32, u32)] = &[(0xE01, 0xE3A), (0xE40, 0xE5B)]; + +/// The code points whose Script_Extensions include Thai. +pub static SCRIPT_EXT_THAI: &[(u32, u32)] = &[ + (0x2BC, 0x2BC), + (0x2D7, 0x2D7), + (0x303, 0x303), + (0x331, 0x331), + (0xE01, 0xE3A), + (0xE40, 0xE5B), +]; + +/// The code points whose Script is Tibetan. +pub static SCRIPT_TIBETAN: &[(u32, u32)] = &[ + (0xF00, 0xF47), + (0xF49, 0xF6C), + (0xF71, 0xF97), + (0xF99, 0xFBC), + (0xFBE, 0xFCC), + (0xFCE, 0xFD4), + (0xFD9, 0xFDA), +]; + +/// The code points whose Script_Extensions include Tibetan. +pub static SCRIPT_EXT_TIBETAN: &[(u32, u32)] = &[ + (0xF00, 0xF47), + (0xF49, 0xF6C), + (0xF71, 0xF97), + (0xF99, 0xFBC), + (0xFBE, 0xFCC), + (0xFCE, 0xFD4), + (0xFD9, 0xFDA), + (0x3008, 0x300B), +]; + +/// The code points whose Script is Tifinagh. +pub static SCRIPT_TIFINAGH: &[(u32, u32)] = &[(0x2D30, 0x2D67), (0x2D6F, 0x2D70), (0x2D7F, 0x2D7F)]; + +/// The code points whose Script_Extensions include Tifinagh. +pub static SCRIPT_EXT_TIFINAGH: &[(u32, u32)] = &[ + (0x302, 0x302), + (0x304, 0x304), + (0x306, 0x309), + (0x323, 0x323), + (0x2D30, 0x2D67), + (0x2D6F, 0x2D70), + (0x2D7F, 0x2D7F), +]; + +/// The code points whose Script is Tirhuta. +pub static SCRIPT_TIRHUTA: &[(u32, u32)] = &[(0x11480, 0x114C7), (0x114D0, 0x114D9)]; + +/// The code points whose Script_Extensions include Tirhuta. +pub static SCRIPT_EXT_TIRHUTA: &[(u32, u32)] = &[ + (0x951, 0x952), + (0x964, 0x965), + (0x1CD5, 0x1CD5), + (0x1CE2, 0x1CE2), + (0x1CF2, 0x1CF2), + (0xA830, 0xA839), + (0x11480, 0x114C7), + (0x114D0, 0x114D9), +]; + +/// The code points whose Script is Todhri. +pub static SCRIPT_TODHRI: &[(u32, u32)] = &[(0x105C0, 0x105F3)]; + +/// The code points whose Script_Extensions include Todhri. +pub static SCRIPT_EXT_TODHRI: &[(u32, u32)] = &[ + (0x301, 0x301), + (0x304, 0x304), + (0x307, 0x307), + (0x311, 0x311), + (0x313, 0x313), + (0x35E, 0x35E), + (0x105C0, 0x105F3), +]; + +/// The code points whose Script is Tolong_Siki. +pub static SCRIPT_TOLONG_SIKI: &[(u32, u32)] = &[(0x11DB0, 0x11DDB), (0x11DE0, 0x11DE9)]; + +/// The code points whose Script is Toto. +pub static SCRIPT_TOTO: &[(u32, u32)] = &[(0x1E290, 0x1E2AE)]; + +/// The code points whose Script_Extensions include Toto. +pub static SCRIPT_EXT_TOTO: &[(u32, u32)] = &[(0x2BC, 0x2BC), (0x1E290, 0x1E2AE)]; + +/// The code points whose Script is Tulu_Tigalari. +pub static SCRIPT_TULU_TIGALARI: &[(u32, u32)] = &[ + (0x11380, 0x11389), + (0x1138B, 0x1138B), + (0x1138E, 0x1138E), + (0x11390, 0x113B5), + (0x113B7, 0x113C0), + (0x113C2, 0x113C2), + (0x113C5, 0x113C5), + (0x113C7, 0x113CA), + (0x113CC, 0x113D5), + (0x113D7, 0x113D8), + (0x113E1, 0x113E2), +]; + +/// The code points whose Script_Extensions include Tulu_Tigalari. +pub static SCRIPT_EXT_TULU_TIGALARI: &[(u32, u32)] = &[ + (0xCE6, 0xCEF), + (0x1CF2, 0x1CF2), + (0x1CF4, 0x1CF4), + (0xA830, 0xA835), + (0xA8F1, 0xA8F1), + (0x11380, 0x11389), + (0x1138B, 0x1138B), + (0x1138E, 0x1138E), + (0x11390, 0x113B5), + (0x113B7, 0x113C0), + (0x113C2, 0x113C2), + (0x113C5, 0x113C5), + (0x113C7, 0x113CA), + (0x113CC, 0x113D5), + (0x113D7, 0x113D8), + (0x113E1, 0x113E2), +]; + +/// The code points whose Script is Ugaritic. +pub static SCRIPT_UGARITIC: &[(u32, u32)] = &[(0x10380, 0x1039D), (0x1039F, 0x1039F)]; + +/// The code points whose Script is Unknown. +pub static SCRIPT_UNKNOWN: &[(u32, u32)] = &[ + (0x378, 0x379), + (0x380, 0x383), + (0x38B, 0x38B), + (0x38D, 0x38D), + (0x3A2, 0x3A2), + (0x530, 0x530), + (0x557, 0x557), + (0x590, 0x590), + (0x5CA, 0x5CF), + (0x5EB, 0x5EE), + (0x5F5, 0x5FF), + (0x70E, 0x70E), + (0x74B, 0x74C), + (0x7B2, 0x7BF), + (0x7FB, 0x7FC), + (0x82E, 0x82F), + (0x83F, 0x83F), + (0x85C, 0x85D), + (0x85F, 0x85F), + (0x86B, 0x86F), + (0x892, 0x896), + (0x984, 0x984), + (0x98D, 0x98E), + (0x991, 0x992), + (0x9A9, 0x9A9), + (0x9B1, 0x9B1), + (0x9B3, 0x9B5), + (0x9BA, 0x9BB), + (0x9C5, 0x9C6), + (0x9C9, 0x9CA), + (0x9CF, 0x9D6), + (0x9D8, 0x9DB), + (0x9DE, 0x9DE), + (0x9E4, 0x9E5), + (0x9FF, 0xA00), + (0xA04, 0xA04), + (0xA0B, 0xA0E), + (0xA11, 0xA12), + (0xA29, 0xA29), + (0xA31, 0xA31), + (0xA34, 0xA34), + (0xA37, 0xA37), + (0xA3A, 0xA3B), + (0xA3D, 0xA3D), + (0xA43, 0xA46), + (0xA49, 0xA4A), + (0xA4E, 0xA50), + (0xA52, 0xA58), + (0xA5D, 0xA5D), + (0xA5F, 0xA65), + (0xA77, 0xA80), + (0xA84, 0xA84), + (0xA8E, 0xA8E), + (0xA92, 0xA92), + (0xAA9, 0xAA9), + (0xAB1, 0xAB1), + (0xAB4, 0xAB4), + (0xABA, 0xABB), + (0xAC6, 0xAC6), + (0xACA, 0xACA), + (0xACE, 0xACF), + (0xAD1, 0xADF), + (0xAE4, 0xAE5), + (0xAF2, 0xAF8), + (0xB00, 0xB00), + (0xB04, 0xB04), + (0xB0D, 0xB0E), + (0xB11, 0xB12), + (0xB29, 0xB29), + (0xB31, 0xB31), + (0xB34, 0xB34), + (0xB3A, 0xB3B), + (0xB45, 0xB46), + (0xB49, 0xB4A), + (0xB4E, 0xB52), + (0xB58, 0xB5B), + (0xB5E, 0xB5E), + (0xB64, 0xB65), + (0xB78, 0xB81), + (0xB84, 0xB84), + (0xB8B, 0xB8D), + (0xB91, 0xB91), + (0xB96, 0xB98), + (0xB9B, 0xB9B), + (0xB9D, 0xB9D), + (0xBA0, 0xBA2), + (0xBA5, 0xBA7), + (0xBAB, 0xBAD), + (0xBBA, 0xBBD), + (0xBC3, 0xBC5), + (0xBC9, 0xBC9), + (0xBCE, 0xBCF), + (0xBD1, 0xBD6), + (0xBD8, 0xBE5), + (0xBFB, 0xBFF), + (0xC0D, 0xC0D), + (0xC11, 0xC11), + (0xC29, 0xC29), + (0xC3A, 0xC3B), + (0xC45, 0xC45), + (0xC49, 0xC49), + (0xC4E, 0xC54), + (0xC57, 0xC57), + (0xC5B, 0xC5B), + (0xC5E, 0xC5F), + (0xC64, 0xC65), + (0xC70, 0xC76), + (0xC8D, 0xC8D), + (0xC91, 0xC91), + (0xCA9, 0xCA9), + (0xCB4, 0xCB4), + (0xCBA, 0xCBB), + (0xCC5, 0xCC5), + (0xCC9, 0xCC9), + (0xCCE, 0xCD4), + (0xCD7, 0xCDB), + (0xCDF, 0xCDF), + (0xCE4, 0xCE5), + (0xCF0, 0xCF0), + (0xCF4, 0xCFF), + (0xD0D, 0xD0D), + (0xD11, 0xD11), + (0xD45, 0xD45), + (0xD49, 0xD49), + (0xD50, 0xD53), + (0xD64, 0xD65), + (0xD80, 0xD80), + (0xD84, 0xD84), + (0xD97, 0xD99), + (0xDB2, 0xDB2), + (0xDBC, 0xDBC), + (0xDBE, 0xDBF), + (0xDC7, 0xDC9), + (0xDCB, 0xDCE), + (0xDD5, 0xDD5), + (0xDD7, 0xDD7), + (0xDE0, 0xDE5), + (0xDF0, 0xDF1), + (0xDF5, 0xE00), + (0xE3B, 0xE3E), + (0xE5C, 0xE80), + (0xE83, 0xE83), + (0xE85, 0xE85), + (0xE8B, 0xE8B), + (0xEA4, 0xEA4), + (0xEA6, 0xEA6), + (0xEBE, 0xEBF), + (0xEC5, 0xEC5), + (0xEC7, 0xEC7), + (0xECF, 0xECF), + (0xEDA, 0xEDB), + (0xEE0, 0xEFF), + (0xF48, 0xF48), + (0xF6D, 0xF70), + (0xF98, 0xF98), + (0xFBD, 0xFBD), + (0xFCD, 0xFCD), + (0xFDB, 0xFFF), + (0x10C6, 0x10C6), + (0x10C8, 0x10CC), + (0x10CE, 0x10CF), + (0x1249, 0x1249), + (0x124E, 0x124F), + (0x1257, 0x1257), + (0x1259, 0x1259), + (0x125E, 0x125F), + (0x1289, 0x1289), + (0x128E, 0x128F), + (0x12B1, 0x12B1), + (0x12B6, 0x12B7), + (0x12BF, 0x12BF), + (0x12C1, 0x12C1), + (0x12C6, 0x12C7), + (0x12D7, 0x12D7), + (0x1311, 0x1311), + (0x1316, 0x1317), + (0x135B, 0x135C), + (0x137D, 0x137F), + (0x139A, 0x139F), + (0x13F6, 0x13F7), + (0x13FE, 0x13FF), + (0x169D, 0x169F), + (0x16F9, 0x16FF), + (0x1716, 0x171E), + (0x1737, 0x173F), + (0x1754, 0x175F), + (0x176D, 0x176D), + (0x1771, 0x1771), + (0x1774, 0x177F), + (0x17DE, 0x17DF), + (0x17EA, 0x17EF), + (0x17FA, 0x17FF), + (0x181A, 0x181F), + (0x1879, 0x187F), + (0x18AB, 0x18AF), + (0x18F6, 0x18FF), + (0x191F, 0x191F), + (0x192C, 0x192F), + (0x193C, 0x193F), + (0x1941, 0x1943), + (0x196E, 0x196F), + (0x1975, 0x197F), + (0x19AC, 0x19AF), + (0x19CA, 0x19CF), + (0x19DB, 0x19DD), + (0x1A1C, 0x1A1D), + (0x1A5F, 0x1A5F), + (0x1A7D, 0x1A7E), + (0x1A8A, 0x1A8F), + (0x1A9A, 0x1A9F), + (0x1AAE, 0x1AAF), + (0x1AF1, 0x1AFF), + (0x1B4D, 0x1B4D), + (0x1BF4, 0x1BFB), + (0x1C38, 0x1C3A), + (0x1C4A, 0x1C4C), + (0x1C8B, 0x1C8F), + (0x1CBB, 0x1CBC), + (0x1CC8, 0x1CCF), + (0x1CFB, 0x1CFF), + (0x1F16, 0x1F17), + (0x1F1E, 0x1F1F), + (0x1F46, 0x1F47), + (0x1F4E, 0x1F4F), + (0x1F58, 0x1F58), + (0x1F5A, 0x1F5A), + (0x1F5C, 0x1F5C), + (0x1F5E, 0x1F5E), + (0x1F7E, 0x1F7F), + (0x1FB5, 0x1FB5), + (0x1FC5, 0x1FC5), + (0x1FD4, 0x1FD5), + (0x1FDC, 0x1FDC), + (0x1FF0, 0x1FF1), + (0x1FF5, 0x1FF5), + (0x1FFF, 0x1FFF), + (0x2065, 0x2065), + (0x2072, 0x2073), + (0x20C5, 0x20CF), + (0x20F1, 0x20FF), + (0x218C, 0x218F), + (0x242A, 0x243F), + (0x244B, 0x245F), + (0x2B74, 0x2B75), + (0x2CF4, 0x2CF8), + (0x2D26, 0x2D26), + (0x2D28, 0x2D2C), + (0x2D2E, 0x2D2F), + (0x2D68, 0x2D6E), + (0x2D71, 0x2D7E), + (0x2D97, 0x2D9F), + (0x2DA7, 0x2DA7), + (0x2DAF, 0x2DAF), + (0x2DB7, 0x2DB7), + (0x2DBF, 0x2DBF), + (0x2DC7, 0x2DC7), + (0x2DCF, 0x2DCF), + (0x2DD7, 0x2DD7), + (0x2DDF, 0x2DDF), + (0x2E5E, 0x2E5F), + (0x2E64, 0x2E7F), + (0x2E9A, 0x2E9A), + (0x2EF4, 0x2EFF), + (0x2FD6, 0x2FEF), + (0x3040, 0x3040), + (0x3097, 0x3098), + (0x3100, 0x3104), + (0x3130, 0x3130), + (0x318F, 0x318F), + (0x31E6, 0x31EE), + (0x321F, 0x321F), + (0xA48D, 0xA48F), + (0xA4C7, 0xA4CF), + (0xA62C, 0xA63F), + (0xA6F8, 0xA6FF), + (0xA7DE, 0xA7E1), + (0xA7E3, 0xA7F0), + (0xA82D, 0xA82F), + (0xA83A, 0xA83F), + (0xA878, 0xA87F), + (0xA8C6, 0xA8CD), + (0xA8DA, 0xA8DF), + (0xA954, 0xA95E), + (0xA97D, 0xA97F), + (0xA9CE, 0xA9CE), + (0xA9DA, 0xA9DD), + (0xA9FF, 0xA9FF), + (0xAA37, 0xAA3F), + (0xAA4E, 0xAA4F), + (0xAA5A, 0xAA5B), + (0xAAC3, 0xAADA), + (0xAAF7, 0xAB00), + (0xAB07, 0xAB08), + (0xAB0F, 0xAB10), + (0xAB17, 0xAB1F), + (0xAB27, 0xAB27), + (0xAB2F, 0xAB2F), + (0xAB6E, 0xAB6F), + (0xABEE, 0xABEF), + (0xABFA, 0xABFF), + (0xD7A4, 0xD7AF), + (0xD7C7, 0xD7CA), + (0xD7FC, 0xF8FF), + (0xFA6E, 0xFA6F), + (0xFADA, 0xFAFF), + (0xFB07, 0xFB12), + (0xFB18, 0xFB1C), + (0xFB37, 0xFB37), + (0xFB3D, 0xFB3D), + (0xFB3F, 0xFB3F), + (0xFB42, 0xFB42), + (0xFB45, 0xFB45), + (0xFDD0, 0xFDEF), + (0xFE1A, 0xFE1F), + (0xFE53, 0xFE53), + (0xFE67, 0xFE67), + (0xFE6C, 0xFE6F), + (0xFE75, 0xFE75), + (0xFEFD, 0xFEFE), + (0xFF00, 0xFF00), + (0xFFBF, 0xFFC1), + (0xFFC8, 0xFFC9), + (0xFFD0, 0xFFD1), + (0xFFD8, 0xFFD9), + (0xFFDD, 0xFFDF), + (0xFFE7, 0xFFE7), + (0xFFEF, 0xFFF8), + (0xFFFE, 0xFFFF), + (0x1000C, 0x1000C), + (0x10027, 0x10027), + (0x1003B, 0x1003B), + (0x1003E, 0x1003E), + (0x1004E, 0x1004F), + (0x1005E, 0x1007F), + (0x100FB, 0x100FF), + (0x10103, 0x10106), + (0x10134, 0x10136), + (0x1018F, 0x1018F), + (0x1019D, 0x1019F), + (0x101A1, 0x101CF), + (0x101FE, 0x1027F), + (0x1029D, 0x1029F), + (0x102D1, 0x102DF), + (0x102FC, 0x102FF), + (0x10324, 0x1032C), + (0x1034B, 0x1034F), + (0x1037B, 0x1037F), + (0x1039E, 0x1039E), + (0x103C4, 0x103C7), + (0x103D6, 0x103FF), + (0x1049E, 0x1049F), + (0x104AA, 0x104AF), + (0x104D4, 0x104D7), + (0x104FC, 0x104FF), + (0x10528, 0x1052F), + (0x10564, 0x1056E), + (0x1057B, 0x1057B), + (0x1058B, 0x1058B), + (0x10593, 0x10593), + (0x10596, 0x10596), + (0x105A2, 0x105A2), + (0x105B2, 0x105B2), + (0x105BA, 0x105BA), + (0x105BD, 0x105BF), + (0x105F4, 0x105FF), + (0x10737, 0x1073F), + (0x10756, 0x1075F), + (0x10768, 0x1077F), + (0x10786, 0x10786), + (0x107B1, 0x107B1), + (0x107C0, 0x107FF), + (0x10806, 0x10807), + (0x10809, 0x10809), + (0x10836, 0x10836), + (0x10839, 0x1083B), + (0x1083D, 0x1083E), + (0x10856, 0x10856), + (0x1089F, 0x108A6), + (0x108B0, 0x108DF), + (0x108F3, 0x108F3), + (0x108F6, 0x108FA), + (0x1091C, 0x1091E), + (0x1093A, 0x1093E), + (0x1095A, 0x1097F), + (0x109B8, 0x109BB), + (0x109D0, 0x109D1), + (0x10A04, 0x10A04), + (0x10A07, 0x10A0B), + (0x10A14, 0x10A14), + (0x10A18, 0x10A18), + (0x10A36, 0x10A37), + (0x10A3B, 0x10A3E), + (0x10A49, 0x10A4F), + (0x10A59, 0x10A5F), + (0x10AA0, 0x10ABF), + (0x10AE7, 0x10AEA), + (0x10AF7, 0x10AFF), + (0x10B36, 0x10B38), + (0x10B56, 0x10B57), + (0x10B73, 0x10B77), + (0x10B92, 0x10B98), + (0x10B9D, 0x10BA8), + (0x10BB0, 0x10BFF), + (0x10C49, 0x10C7F), + (0x10CB3, 0x10CBF), + (0x10CF3, 0x10CF9), + (0x10D28, 0x10D2F), + (0x10D3A, 0x10D3F), + (0x10D66, 0x10D68), + (0x10D86, 0x10D8D), + (0x10D90, 0x10E5F), + (0x10E7F, 0x10E7F), + (0x10EAA, 0x10EAA), + (0x10EAE, 0x10EAF), + (0x10EB2, 0x10EC1), + (0x10EC8, 0x10EC8), + (0x10EEF, 0x10EEF), + (0x10F28, 0x10F2F), + (0x10F5A, 0x10F6F), + (0x10F8A, 0x10FAF), + (0x10FCC, 0x10FDF), + (0x10FF7, 0x10FFF), + (0x1104E, 0x11051), + (0x11076, 0x1107E), + (0x110C3, 0x110CC), + (0x110CE, 0x110CF), + (0x110E9, 0x110EF), + (0x110FA, 0x110FF), + (0x11135, 0x11135), + (0x11148, 0x1114F), + (0x11177, 0x1117F), + (0x111E0, 0x111E0), + (0x111F5, 0x111FF), + (0x11212, 0x11212), + (0x11242, 0x1127F), + (0x11287, 0x11287), + (0x11289, 0x11289), + (0x1128E, 0x1128E), + (0x1129E, 0x1129E), + (0x112AA, 0x112AF), + (0x112EB, 0x112EF), + (0x112FA, 0x112FF), + (0x11304, 0x11304), + (0x1130D, 0x1130E), + (0x11311, 0x11312), + (0x11329, 0x11329), + (0x11331, 0x11331), + (0x11334, 0x11334), + (0x1133A, 0x1133A), + (0x11345, 0x11346), + (0x11349, 0x1134A), + (0x1134E, 0x1134F), + (0x11351, 0x11356), + (0x11358, 0x1135C), + (0x11364, 0x11365), + (0x1136D, 0x1136F), + (0x11375, 0x1137F), + (0x1138A, 0x1138A), + (0x1138C, 0x1138D), + (0x1138F, 0x1138F), + (0x113B6, 0x113B6), + (0x113C1, 0x113C1), + (0x113C3, 0x113C4), + (0x113C6, 0x113C6), + (0x113CB, 0x113CB), + (0x113D6, 0x113D6), + (0x113D9, 0x113E0), + (0x113E3, 0x113FF), + (0x1145C, 0x1145C), + (0x11462, 0x1147F), + (0x114C8, 0x114CF), + (0x114DA, 0x1157F), + (0x115B6, 0x115B7), + (0x115DE, 0x115FF), + (0x11645, 0x1164F), + (0x1165A, 0x1165F), + (0x1166D, 0x1167F), + (0x116BA, 0x116BF), + (0x116CA, 0x116CF), + (0x116E4, 0x116FF), + (0x1171B, 0x1171C), + (0x1172C, 0x1172F), + (0x11747, 0x117FF), + (0x1183C, 0x1189F), + (0x118F3, 0x118FE), + (0x11907, 0x11908), + (0x1190A, 0x1190B), + (0x11914, 0x11914), + (0x11917, 0x11917), + (0x11936, 0x11936), + (0x11939, 0x1193A), + (0x11947, 0x1194F), + (0x1195A, 0x1199F), + (0x119A8, 0x119A9), + (0x119D8, 0x119D9), + (0x119E5, 0x119FF), + (0x11A48, 0x11A4F), + (0x11AA3, 0x11AAF), + (0x11AF9, 0x11AFF), + (0x11B0B, 0x11B5F), + (0x11B68, 0x11BBF), + (0x11BE2, 0x11BEF), + (0x11BFA, 0x11BFF), + (0x11C09, 0x11C09), + (0x11C37, 0x11C37), + (0x11C46, 0x11C4F), + (0x11C6D, 0x11C6F), + (0x11C90, 0x11C91), + (0x11CA8, 0x11CA8), + (0x11CB7, 0x11CFF), + (0x11D07, 0x11D07), + (0x11D0A, 0x11D0A), + (0x11D37, 0x11D39), + (0x11D3B, 0x11D3B), + (0x11D3E, 0x11D3E), + (0x11D48, 0x11D4F), + (0x11D5A, 0x11D5F), + (0x11D66, 0x11D66), + (0x11D69, 0x11D69), + (0x11D8F, 0x11D8F), + (0x11D92, 0x11D92), + (0x11D99, 0x11D9F), + (0x11DAA, 0x11DAF), + (0x11DDC, 0x11DDF), + (0x11DEA, 0x11DEF), + (0x11DF2, 0x11EDF), + (0x11EF9, 0x11EFF), + (0x11F11, 0x11F11), + (0x11F3B, 0x11F3D), + (0x11F5B, 0x11FAF), + (0x11FB1, 0x11FBF), + (0x11FF2, 0x11FFE), + (0x1239A, 0x123FF), + (0x12544, 0x1254F), + (0x12687, 0x12F8F), + (0x12FF3, 0x12FFF), + (0x13456, 0x1345F), + (0x143FB, 0x143FF), + (0x14647, 0x160FF), + (0x1613A, 0x167FF), + (0x16A39, 0x16A3F), + (0x16A5F, 0x16A5F), + (0x16A6A, 0x16A6D), + (0x16ABF, 0x16ABF), + (0x16ACA, 0x16ACF), + (0x16AEE, 0x16AEF), + (0x16AF6, 0x16AFF), + (0x16B46, 0x16B4F), + (0x16B5A, 0x16B5A), + (0x16B62, 0x16B62), + (0x16B78, 0x16B7C), + (0x16B90, 0x16D3F), + (0x16D7A, 0x16E3F), + (0x16E9B, 0x16E9F), + (0x16EB9, 0x16EBA), + (0x16ED4, 0x16EFF), + (0x16F4B, 0x16F4E), + (0x16F88, 0x16F8E), + (0x16FA0, 0x16FDF), + (0x16FE5, 0x16FEF), + (0x16FF7, 0x16FFF), + (0x18CDB, 0x18CFE), + (0x18D21, 0x18D7F), + (0x18DF3, 0x18DFF), + (0x19192, 0x1919F), + (0x191D3, 0x1AFEF), + (0x1AFF4, 0x1AFF4), + (0x1AFFC, 0x1AFFC), + (0x1AFFF, 0x1AFFF), + (0x1B129, 0x1B131), + (0x1B133, 0x1B14F), + (0x1B153, 0x1B154), + (0x1B156, 0x1B163), + (0x1B169, 0x1B16F), + (0x1B2FC, 0x1BBFF), + (0x1BC6B, 0x1BC6F), + (0x1BC7D, 0x1BC7F), + (0x1BC89, 0x1BC8F), + (0x1BC9A, 0x1BC9B), + (0x1BCA4, 0x1CBFF), + (0x1CCFD, 0x1CCFF), + (0x1CEB4, 0x1CEB9), + (0x1CED1, 0x1CED1), + (0x1CED5, 0x1CEDC), + (0x1CEFE, 0x1CEFF), + (0x1CF2E, 0x1CF2F), + (0x1CF47, 0x1CF4F), + (0x1CFC4, 0x1CFFF), + (0x1D0F6, 0x1D0FF), + (0x1D246, 0x1D24F), + (0x1D282, 0x1D2BF), + (0x1D2D4, 0x1D2DF), + (0x1D2F4, 0x1D2FF), + (0x1D357, 0x1D35F), + (0x1D379, 0x1D3FF), + (0x1D455, 0x1D455), + (0x1D49D, 0x1D49D), + (0x1D4A0, 0x1D4A1), + (0x1D4A3, 0x1D4A4), + (0x1D4A7, 0x1D4A8), + (0x1D4AD, 0x1D4AD), + (0x1D4BA, 0x1D4BA), + (0x1D4BC, 0x1D4BC), + (0x1D4C4, 0x1D4C4), + (0x1D506, 0x1D506), + (0x1D50B, 0x1D50C), + (0x1D515, 0x1D515), + (0x1D51D, 0x1D51D), + (0x1D53A, 0x1D53A), + (0x1D53F, 0x1D53F), + (0x1D545, 0x1D545), + (0x1D547, 0x1D549), + (0x1D551, 0x1D551), + (0x1D6A7, 0x1D6A7), + (0x1D7CC, 0x1D7CD), + (0x1DA8C, 0x1DA9A), + (0x1DAA0, 0x1DAA0), + (0x1DAB0, 0x1DAFF), + (0x1DB1D, 0x1DEFF), + (0x1DF82, 0x1DF8F), + (0x1DF97, 0x1DFCC), + (0x1E007, 0x1E007), + (0x1E019, 0x1E01A), + (0x1E022, 0x1E022), + (0x1E025, 0x1E025), + (0x1E02B, 0x1E02F), + (0x1E06E, 0x1E08E), + (0x1E090, 0x1E0FF), + (0x1E12D, 0x1E12F), + (0x1E13E, 0x1E13F), + (0x1E14A, 0x1E14D), + (0x1E150, 0x1E28F), + (0x1E2AF, 0x1E2BF), + (0x1E2FA, 0x1E2FE), + (0x1E300, 0x1E4CF), + (0x1E4FA, 0x1E5CF), + (0x1E5FB, 0x1E5FE), + (0x1E600, 0x1E6BF), + (0x1E6DF, 0x1E6DF), + (0x1E6F6, 0x1E6FD), + (0x1E700, 0x1E7DF), + (0x1E7E7, 0x1E7E7), + (0x1E7EC, 0x1E7EC), + (0x1E7EF, 0x1E7EF), + (0x1E7FF, 0x1E7FF), + (0x1E8C5, 0x1E8C6), + (0x1E8D7, 0x1E8FF), + (0x1E94C, 0x1E94F), + (0x1E95A, 0x1E95D), + (0x1E960, 0x1EC70), + (0x1ECB5, 0x1ED00), + (0x1ED3E, 0x1EDFF), + (0x1EE04, 0x1EE04), + (0x1EE20, 0x1EE20), + (0x1EE23, 0x1EE23), + (0x1EE25, 0x1EE26), + (0x1EE28, 0x1EE28), + (0x1EE33, 0x1EE33), + (0x1EE38, 0x1EE38), + (0x1EE3A, 0x1EE3A), + (0x1EE3C, 0x1EE41), + (0x1EE43, 0x1EE46), + (0x1EE48, 0x1EE48), + (0x1EE4A, 0x1EE4A), + (0x1EE4C, 0x1EE4C), + (0x1EE50, 0x1EE50), + (0x1EE53, 0x1EE53), + (0x1EE55, 0x1EE56), + (0x1EE58, 0x1EE58), + (0x1EE5A, 0x1EE5A), + (0x1EE5C, 0x1EE5C), + (0x1EE5E, 0x1EE5E), + (0x1EE60, 0x1EE60), + (0x1EE63, 0x1EE63), + (0x1EE65, 0x1EE66), + (0x1EE6B, 0x1EE6B), + (0x1EE73, 0x1EE73), + (0x1EE78, 0x1EE78), + (0x1EE7D, 0x1EE7D), + (0x1EE7F, 0x1EE7F), + (0x1EE8A, 0x1EE8A), + (0x1EE9C, 0x1EEA0), + (0x1EEA4, 0x1EEA4), + (0x1EEAA, 0x1EEAA), + (0x1EEBC, 0x1EEEF), + (0x1EEF2, 0x1EFFF), + (0x1F02C, 0x1F02F), + (0x1F094, 0x1F09F), + (0x1F0AF, 0x1F0B0), + (0x1F0C0, 0x1F0C0), + (0x1F0D0, 0x1F0D0), + (0x1F0F6, 0x1F0FF), + (0x1F1AF, 0x1F1E5), + (0x1F203, 0x1F20F), + (0x1F23C, 0x1F23F), + (0x1F249, 0x1F24F), + (0x1F252, 0x1F25F), + (0x1F266, 0x1F2FF), + (0x1F6DA, 0x1F6DB), + (0x1F6ED, 0x1F6EF), + (0x1F6FD, 0x1F6FF), + (0x1F7DC, 0x1F7DF), + (0x1F7EC, 0x1F7EF), + (0x1F80C, 0x1F80F), + (0x1F848, 0x1F84F), + (0x1F85A, 0x1F85F), + (0x1F888, 0x1F88F), + (0x1F8AE, 0x1F8AF), + (0x1F8BC, 0x1F8BF), + (0x1F8C2, 0x1F8CF), + (0x1F8D9, 0x1F8FF), + (0x1FA58, 0x1FA5F), + (0x1FA6E, 0x1FA6F), + (0x1FA7D, 0x1FA7F), + (0x1FAC7, 0x1FAC7), + (0x1FAC9, 0x1FACB), + (0x1FADE, 0x1FADE), + (0x1FAEC, 0x1FAEE), + (0x1FAFB, 0x1FAFF), + (0x1FB93, 0x1FB93), + (0x1FBFB, 0x1FFFF), + (0x2A6E0, 0x2A6FF), + (0x2B81F, 0x2B81F), + (0x2CEAE, 0x2CEAF), + (0x2EBE1, 0x2EBEF), + (0x2EE5E, 0x2F7FF), + (0x2FA1E, 0x2FFFF), + (0x3134B, 0x3134F), + (0x3347A, 0x3CFFF), + (0x3FC40, 0xE0000), + (0xE0002, 0xE001F), + (0xE0080, 0xE00FF), + (0xE01F0, 0x10FFFF), +]; + +/// The code points whose Script is Vai. +pub static SCRIPT_VAI: &[(u32, u32)] = &[(0xA500, 0xA62B)]; + +/// The code points whose Script is Vithkuqi. +pub static SCRIPT_VITHKUQI: &[(u32, u32)] = &[ + (0x10570, 0x1057A), + (0x1057C, 0x1058A), + (0x1058C, 0x10592), + (0x10594, 0x10595), + (0x10597, 0x105A1), + (0x105A3, 0x105B1), + (0x105B3, 0x105B9), + (0x105BB, 0x105BC), +]; + +/// The code points whose Script is Wancho. +pub static SCRIPT_WANCHO: &[(u32, u32)] = &[(0x1E2C0, 0x1E2F9), (0x1E2FF, 0x1E2FF)]; + +/// The code points whose Script is Warang_Citi. +pub static SCRIPT_WARANG_CITI: &[(u32, u32)] = &[(0x118A0, 0x118F2), (0x118FF, 0x118FF)]; + +/// The code points whose Script is Yezidi. +pub static SCRIPT_YEZIDI: &[(u32, u32)] = + &[(0x10E80, 0x10EA9), (0x10EAB, 0x10EAD), (0x10EB0, 0x10EB1)]; + +/// The code points whose Script_Extensions include Yezidi. +pub static SCRIPT_EXT_YEZIDI: &[(u32, u32)] = &[ + (0x60C, 0x60C), + (0x61B, 0x61B), + (0x61F, 0x61F), + (0x660, 0x669), + (0x10E80, 0x10EA9), + (0x10EAB, 0x10EAD), + (0x10EB0, 0x10EB1), +]; + +/// The code points whose Script is Yi. +pub static SCRIPT_YI: &[(u32, u32)] = &[(0xA000, 0xA48C), (0xA490, 0xA4C6)]; + +/// The code points whose Script_Extensions include Yi. +pub static SCRIPT_EXT_YI: &[(u32, u32)] = &[ + (0x3001, 0x3002), + (0x3008, 0x3011), + (0x3014, 0x301B), + (0x30FB, 0x30FB), + (0xA000, 0xA48C), + (0xA490, 0xA4C6), + (0xFF61, 0xFF65), +]; + +/// The code points whose Script is Zanabazar_Square. +pub static SCRIPT_ZANABAZAR_SQUARE: &[(u32, u32)] = &[(0x11A00, 0x11A47)]; + +/// A value of the `Script` property. +#[derive(Debug, Clone, Copy)] +pub struct Script { + /// Every name of the value: short name, long name, further aliases. + pub names: &'static [&'static str], + /// The code points with `Script` this value. + pub sc: &'static [(u32, u32)], + /// The code points whose `Script_Extensions` include this value. + pub scx: &'static [(u32, u32)], +} + +/// The `Script` values of `PropertyValueAliases.txt`, by long name. +pub static SCRIPTS: &[Script] = &[ + Script { + names: &["Adlm", "Adlam"], + sc: SCRIPT_ADLAM, + scx: SCRIPT_EXT_ADLAM, + }, + Script { + names: &["Ahom"], + sc: SCRIPT_AHOM, + scx: SCRIPT_AHOM, + }, + Script { + names: &["Hluw", "Anatolian_Hieroglyphs"], + sc: SCRIPT_ANATOLIAN_HIEROGLYPHS, + scx: SCRIPT_ANATOLIAN_HIEROGLYPHS, + }, + Script { + names: &["Arab", "Arabic"], + sc: SCRIPT_ARABIC, + scx: SCRIPT_EXT_ARABIC, + }, + Script { + names: &["Armn", "Armenian"], + sc: SCRIPT_ARMENIAN, + scx: SCRIPT_EXT_ARMENIAN, + }, + Script { + names: &["Avst", "Avestan"], + sc: SCRIPT_AVESTAN, + scx: SCRIPT_EXT_AVESTAN, + }, + Script { + names: &["Bali", "Balinese"], + sc: SCRIPT_BALINESE, + scx: SCRIPT_BALINESE, + }, + Script { + names: &["Bamu", "Bamum"], + sc: SCRIPT_BAMUM, + scx: SCRIPT_BAMUM, + }, + Script { + names: &["Bass", "Bassa_Vah"], + sc: SCRIPT_BASSA_VAH, + scx: SCRIPT_BASSA_VAH, + }, + Script { + names: &["Batk", "Batak"], + sc: SCRIPT_BATAK, + scx: SCRIPT_BATAK, + }, + Script { + names: &["Beng", "Bengali"], + sc: SCRIPT_BENGALI, + scx: SCRIPT_EXT_BENGALI, + }, + Script { + names: &["Berf", "Beria_Erfe"], + sc: SCRIPT_BERIA_ERFE, + scx: SCRIPT_BERIA_ERFE, + }, + Script { + names: &["Bhks", "Bhaiksuki"], + sc: SCRIPT_BHAIKSUKI, + scx: SCRIPT_BHAIKSUKI, + }, + Script { + names: &["Bopo", "Bopomofo"], + sc: SCRIPT_BOPOMOFO, + scx: SCRIPT_EXT_BOPOMOFO, + }, + Script { + names: &["Brah", "Brahmi"], + sc: SCRIPT_BRAHMI, + scx: SCRIPT_BRAHMI, + }, + Script { + names: &["Brai", "Braille"], + sc: SCRIPT_BRAILLE, + scx: SCRIPT_BRAILLE, + }, + Script { + names: &["Bugi", "Buginese"], + sc: SCRIPT_BUGINESE, + scx: SCRIPT_EXT_BUGINESE, + }, + Script { + names: &["Buhd", "Buhid"], + sc: SCRIPT_BUHID, + scx: SCRIPT_EXT_BUHID, + }, + Script { + names: &["Cans", "Canadian_Aboriginal"], + sc: SCRIPT_CANADIAN_ABORIGINAL, + scx: SCRIPT_CANADIAN_ABORIGINAL, + }, + Script { + names: &["Cari", "Carian"], + sc: SCRIPT_CARIAN, + scx: SCRIPT_EXT_CARIAN, + }, + Script { + names: &["Aghb", "Caucasian_Albanian"], + sc: SCRIPT_CAUCASIAN_ALBANIAN, + scx: SCRIPT_EXT_CAUCASIAN_ALBANIAN, + }, + Script { + names: &["Cakm", "Chakma"], + sc: SCRIPT_CHAKMA, + scx: SCRIPT_EXT_CHAKMA, + }, + Script { + names: &["Cham"], + sc: SCRIPT_CHAM, + scx: SCRIPT_CHAM, + }, + Script { + names: &["Cher", "Cherokee"], + sc: SCRIPT_CHEROKEE, + scx: SCRIPT_EXT_CHEROKEE, + }, + Script { + names: &["Chrs", "Chorasmian"], + sc: SCRIPT_CHORASMIAN, + scx: SCRIPT_CHORASMIAN, + }, + Script { + names: &["Zyyy", "Common"], + sc: SCRIPT_COMMON, + scx: SCRIPT_EXT_COMMON, + }, + Script { + names: &["Copt", "Coptic", "Qaac"], + sc: SCRIPT_COPTIC, + scx: SCRIPT_EXT_COPTIC, + }, + Script { + names: &["Xsux", "Cuneiform"], + sc: SCRIPT_CUNEIFORM, + scx: SCRIPT_CUNEIFORM, + }, + Script { + names: &["Cprt", "Cypriot"], + sc: SCRIPT_CYPRIOT, + scx: SCRIPT_EXT_CYPRIOT, + }, + Script { + names: &["Cpmn", "Cypro_Minoan"], + sc: SCRIPT_CYPRO_MINOAN, + scx: SCRIPT_EXT_CYPRO_MINOAN, + }, + Script { + names: &["Cyrl", "Cyrillic"], + sc: SCRIPT_CYRILLIC, + scx: SCRIPT_EXT_CYRILLIC, + }, + Script { + names: &["Dsrt", "Deseret"], + sc: SCRIPT_DESERET, + scx: SCRIPT_DESERET, + }, + Script { + names: &["Deva", "Devanagari"], + sc: SCRIPT_DEVANAGARI, + scx: SCRIPT_EXT_DEVANAGARI, + }, + Script { + names: &["Diak", "Dives_Akuru"], + sc: SCRIPT_DIVES_AKURU, + scx: SCRIPT_DIVES_AKURU, + }, + Script { + names: &["Dogr", "Dogra"], + sc: SCRIPT_DOGRA, + scx: SCRIPT_EXT_DOGRA, + }, + Script { + names: &["Dupl", "Duployan"], + sc: SCRIPT_DUPLOYAN, + scx: SCRIPT_EXT_DUPLOYAN, + }, + Script { + names: &["Egyp", "Egyptian_Hieroglyphs"], + sc: SCRIPT_EGYPTIAN_HIEROGLYPHS, + scx: SCRIPT_EGYPTIAN_HIEROGLYPHS, + }, + Script { + names: &["Elba", "Elbasan"], + sc: SCRIPT_ELBASAN, + scx: SCRIPT_EXT_ELBASAN, + }, + Script { + names: &["Elym", "Elymaic"], + sc: SCRIPT_ELYMAIC, + scx: SCRIPT_ELYMAIC, + }, + Script { + names: &["Ethi", "Ethiopic"], + sc: SCRIPT_ETHIOPIC, + scx: SCRIPT_EXT_ETHIOPIC, + }, + Script { + names: &["Gara", "Garay"], + sc: SCRIPT_GARAY, + scx: SCRIPT_EXT_GARAY, + }, + Script { + names: &["Geor", "Georgian"], + sc: SCRIPT_GEORGIAN, + scx: SCRIPT_EXT_GEORGIAN, + }, + Script { + names: &["Glag", "Glagolitic"], + sc: SCRIPT_GLAGOLITIC, + scx: SCRIPT_EXT_GLAGOLITIC, + }, + Script { + names: &["Goth", "Gothic"], + sc: SCRIPT_GOTHIC, + scx: SCRIPT_EXT_GOTHIC, + }, + Script { + names: &["Gran", "Grantha"], + sc: SCRIPT_GRANTHA, + scx: SCRIPT_EXT_GRANTHA, + }, + Script { + names: &["Grek", "Greek"], + sc: SCRIPT_GREEK, + scx: SCRIPT_EXT_GREEK, + }, + Script { + names: &["Gujr", "Gujarati"], + sc: SCRIPT_GUJARATI, + scx: SCRIPT_EXT_GUJARATI, + }, + Script { + names: &["Gong", "Gunjala_Gondi"], + sc: SCRIPT_GUNJALA_GONDI, + scx: SCRIPT_EXT_GUNJALA_GONDI, + }, + Script { + names: &["Guru", "Gurmukhi"], + sc: SCRIPT_GURMUKHI, + scx: SCRIPT_EXT_GURMUKHI, + }, + Script { + names: &["Gukh", "Gurung_Khema"], + sc: SCRIPT_GURUNG_KHEMA, + scx: SCRIPT_EXT_GURUNG_KHEMA, + }, + Script { + names: &["Hani", "Han"], + sc: SCRIPT_HAN, + scx: SCRIPT_EXT_HAN, + }, + Script { + names: &["Hang", "Hangul"], + sc: SCRIPT_HANGUL, + scx: SCRIPT_EXT_HANGUL, + }, + Script { + names: &["Rohg", "Hanifi_Rohingya"], + sc: SCRIPT_HANIFI_ROHINGYA, + scx: SCRIPT_EXT_HANIFI_ROHINGYA, + }, + Script { + names: &["Hano", "Hanunoo"], + sc: SCRIPT_HANUNOO, + scx: SCRIPT_EXT_HANUNOO, + }, + Script { + names: &["Hatr", "Hatran"], + sc: SCRIPT_HATRAN, + scx: SCRIPT_HATRAN, + }, + Script { + names: &["Hebr", "Hebrew"], + sc: SCRIPT_HEBREW, + scx: SCRIPT_EXT_HEBREW, + }, + Script { + names: &["Hira", "Hiragana"], + sc: SCRIPT_HIRAGANA, + scx: SCRIPT_EXT_HIRAGANA, + }, + Script { + names: &["Armi", "Imperial_Aramaic"], + sc: SCRIPT_IMPERIAL_ARAMAIC, + scx: SCRIPT_IMPERIAL_ARAMAIC, + }, + Script { + names: &["Zinh", "Inherited", "Qaai"], + sc: SCRIPT_INHERITED, + scx: SCRIPT_EXT_INHERITED, + }, + Script { + names: &["Phli", "Inscriptional_Pahlavi"], + sc: SCRIPT_INSCRIPTIONAL_PAHLAVI, + scx: SCRIPT_INSCRIPTIONAL_PAHLAVI, + }, + Script { + names: &["Prti", "Inscriptional_Parthian"], + sc: SCRIPT_INSCRIPTIONAL_PARTHIAN, + scx: SCRIPT_INSCRIPTIONAL_PARTHIAN, + }, + Script { + names: &["Java", "Javanese"], + sc: SCRIPT_JAVANESE, + scx: SCRIPT_EXT_JAVANESE, + }, + Script { + names: &["Jurc", "Jurchen"], + sc: SCRIPT_JURCHEN, + scx: SCRIPT_JURCHEN, + }, + Script { + names: &["Kthi", "Kaithi"], + sc: SCRIPT_KAITHI, + scx: SCRIPT_EXT_KAITHI, + }, + Script { + names: &["Knda", "Kannada"], + sc: SCRIPT_KANNADA, + scx: SCRIPT_EXT_KANNADA, + }, + Script { + names: &["Kana", "Katakana"], + sc: SCRIPT_KATAKANA, + scx: SCRIPT_EXT_KATAKANA, + }, + Script { + names: &["Hrkt", "Katakana_Or_Hiragana"], + sc: SCRIPT_KATAKANA_OR_HIRAGANA, + scx: SCRIPT_KATAKANA_OR_HIRAGANA, + }, + Script { + names: &["Kawi"], + sc: SCRIPT_KAWI, + scx: SCRIPT_KAWI, + }, + Script { + names: &["Kali", "Kayah_Li"], + sc: SCRIPT_KAYAH_LI, + scx: SCRIPT_EXT_KAYAH_LI, + }, + Script { + names: &["Khar", "Kharoshthi"], + sc: SCRIPT_KHAROSHTHI, + scx: SCRIPT_KHAROSHTHI, + }, + Script { + names: &["Kits", "Khitan_Small_Script"], + sc: SCRIPT_KHITAN_SMALL_SCRIPT, + scx: SCRIPT_KHITAN_SMALL_SCRIPT, + }, + Script { + names: &["Khmr", "Khmer"], + sc: SCRIPT_KHMER, + scx: SCRIPT_KHMER, + }, + Script { + names: &["Khoj", "Khojki"], + sc: SCRIPT_KHOJKI, + scx: SCRIPT_EXT_KHOJKI, + }, + Script { + names: &["Sind", "Khudawadi"], + sc: SCRIPT_KHUDAWADI, + scx: SCRIPT_EXT_KHUDAWADI, + }, + Script { + names: &["Krai", "Kirat_Rai"], + sc: SCRIPT_KIRAT_RAI, + scx: SCRIPT_KIRAT_RAI, + }, + Script { + names: &["Laoo", "Lao"], + sc: SCRIPT_LAO, + scx: SCRIPT_LAO, + }, + Script { + names: &["Latn", "Latin"], + sc: SCRIPT_LATIN, + scx: SCRIPT_EXT_LATIN, + }, + Script { + names: &["Lepc", "Lepcha"], + sc: SCRIPT_LEPCHA, + scx: SCRIPT_LEPCHA, + }, + Script { + names: &["Limb", "Limbu"], + sc: SCRIPT_LIMBU, + scx: SCRIPT_EXT_LIMBU, + }, + Script { + names: &["Lina", "Linear_A"], + sc: SCRIPT_LINEAR_A, + scx: SCRIPT_EXT_LINEAR_A, + }, + Script { + names: &["Linb", "Linear_B"], + sc: SCRIPT_LINEAR_B, + scx: SCRIPT_EXT_LINEAR_B, + }, + Script { + names: &["Lisu"], + sc: SCRIPT_LISU, + scx: SCRIPT_EXT_LISU, + }, + Script { + names: &["Lyci", "Lycian"], + sc: SCRIPT_LYCIAN, + scx: SCRIPT_EXT_LYCIAN, + }, + Script { + names: &["Lydi", "Lydian"], + sc: SCRIPT_LYDIAN, + scx: SCRIPT_EXT_LYDIAN, + }, + Script { + names: &["Mahj", "Mahajani"], + sc: SCRIPT_MAHAJANI, + scx: SCRIPT_EXT_MAHAJANI, + }, + Script { + names: &["Maka", "Makasar"], + sc: SCRIPT_MAKASAR, + scx: SCRIPT_MAKASAR, + }, + Script { + names: &["Mlym", "Malayalam"], + sc: SCRIPT_MALAYALAM, + scx: SCRIPT_EXT_MALAYALAM, + }, + Script { + names: &["Mand", "Mandaic"], + sc: SCRIPT_MANDAIC, + scx: SCRIPT_EXT_MANDAIC, + }, + Script { + names: &["Mani", "Manichaean"], + sc: SCRIPT_MANICHAEAN, + scx: SCRIPT_EXT_MANICHAEAN, + }, + Script { + names: &["Marc", "Marchen"], + sc: SCRIPT_MARCHEN, + scx: SCRIPT_MARCHEN, + }, + Script { + names: &["Gonm", "Masaram_Gondi"], + sc: SCRIPT_MASARAM_GONDI, + scx: SCRIPT_EXT_MASARAM_GONDI, + }, + Script { + names: &["Medf", "Medefaidrin"], + sc: SCRIPT_MEDEFAIDRIN, + scx: SCRIPT_MEDEFAIDRIN, + }, + Script { + names: &["Mtei", "Meetei_Mayek"], + sc: SCRIPT_MEETEI_MAYEK, + scx: SCRIPT_MEETEI_MAYEK, + }, + Script { + names: &["Mend", "Mende_Kikakui"], + sc: SCRIPT_MENDE_KIKAKUI, + scx: SCRIPT_MENDE_KIKAKUI, + }, + Script { + names: &["Merc", "Meroitic_Cursive"], + sc: SCRIPT_MEROITIC_CURSIVE, + scx: SCRIPT_MEROITIC_CURSIVE, + }, + Script { + names: &["Mero", "Meroitic_Hieroglyphs"], + sc: SCRIPT_MEROITIC_HIEROGLYPHS, + scx: SCRIPT_EXT_MEROITIC_HIEROGLYPHS, + }, + Script { + names: &["Plrd", "Miao"], + sc: SCRIPT_MIAO, + scx: SCRIPT_MIAO, + }, + Script { + names: &["Modi"], + sc: SCRIPT_MODI, + scx: SCRIPT_EXT_MODI, + }, + Script { + names: &["Mong", "Mongolian"], + sc: SCRIPT_MONGOLIAN, + scx: SCRIPT_EXT_MONGOLIAN, + }, + Script { + names: &["Mroo", "Mro"], + sc: SCRIPT_MRO, + scx: SCRIPT_MRO, + }, + Script { + names: &["Mult", "Multani"], + sc: SCRIPT_MULTANI, + scx: SCRIPT_EXT_MULTANI, + }, + Script { + names: &["Mymr", "Myanmar"], + sc: SCRIPT_MYANMAR, + scx: SCRIPT_EXT_MYANMAR, + }, + Script { + names: &["Nbat", "Nabataean"], + sc: SCRIPT_NABATAEAN, + scx: SCRIPT_NABATAEAN, + }, + Script { + names: &["Nagm", "Nag_Mundari"], + sc: SCRIPT_NAG_MUNDARI, + scx: SCRIPT_NAG_MUNDARI, + }, + Script { + names: &["Nand", "Nandinagari"], + sc: SCRIPT_NANDINAGARI, + scx: SCRIPT_EXT_NANDINAGARI, + }, + Script { + names: &["Talu", "New_Tai_Lue"], + sc: SCRIPT_NEW_TAI_LUE, + scx: SCRIPT_NEW_TAI_LUE, + }, + Script { + names: &["Newa"], + sc: SCRIPT_NEWA, + scx: SCRIPT_EXT_NEWA, + }, + Script { + names: &["Nkoo", "Nko"], + sc: SCRIPT_NKO, + scx: SCRIPT_EXT_NKO, + }, + Script { + names: &["Nshu", "Nushu"], + sc: SCRIPT_NUSHU, + scx: SCRIPT_NUSHU, + }, + Script { + names: &["Hmnp", "Nyiakeng_Puachue_Hmong"], + sc: SCRIPT_NYIAKENG_PUACHUE_HMONG, + scx: SCRIPT_NYIAKENG_PUACHUE_HMONG, + }, + Script { + names: &["Ogam", "Ogham"], + sc: SCRIPT_OGHAM, + scx: SCRIPT_OGHAM, + }, + Script { + names: &["Olck", "Ol_Chiki"], + sc: SCRIPT_OL_CHIKI, + scx: SCRIPT_OL_CHIKI, + }, + Script { + names: &["Onao", "Ol_Onal"], + sc: SCRIPT_OL_ONAL, + scx: SCRIPT_EXT_OL_ONAL, + }, + Script { + names: &["Hung", "Old_Hungarian"], + sc: SCRIPT_OLD_HUNGARIAN, + scx: SCRIPT_EXT_OLD_HUNGARIAN, + }, + Script { + names: &["Ital", "Old_Italic"], + sc: SCRIPT_OLD_ITALIC, + scx: SCRIPT_OLD_ITALIC, + }, + Script { + names: &["Narb", "Old_North_Arabian"], + sc: SCRIPT_OLD_NORTH_ARABIAN, + scx: SCRIPT_OLD_NORTH_ARABIAN, + }, + Script { + names: &["Perm", "Old_Permic"], + sc: SCRIPT_OLD_PERMIC, + scx: SCRIPT_EXT_OLD_PERMIC, + }, + Script { + names: &["Xpeo", "Old_Persian"], + sc: SCRIPT_OLD_PERSIAN, + scx: SCRIPT_OLD_PERSIAN, + }, + Script { + names: &["Sogo", "Old_Sogdian"], + sc: SCRIPT_OLD_SOGDIAN, + scx: SCRIPT_OLD_SOGDIAN, + }, + Script { + names: &["Sarb", "Old_South_Arabian"], + sc: SCRIPT_OLD_SOUTH_ARABIAN, + scx: SCRIPT_OLD_SOUTH_ARABIAN, + }, + Script { + names: &["Orkh", "Old_Turkic"], + sc: SCRIPT_OLD_TURKIC, + scx: SCRIPT_EXT_OLD_TURKIC, + }, + Script { + names: &["Ougr", "Old_Uyghur"], + sc: SCRIPT_OLD_UYGHUR, + scx: SCRIPT_EXT_OLD_UYGHUR, + }, + Script { + names: &["Orya", "Oriya"], + sc: SCRIPT_ORIYA, + scx: SCRIPT_EXT_ORIYA, + }, + Script { + names: &["Osge", "Osage"], + sc: SCRIPT_OSAGE, + scx: SCRIPT_EXT_OSAGE, + }, + Script { + names: &["Osma", "Osmanya"], + sc: SCRIPT_OSMANYA, + scx: SCRIPT_OSMANYA, + }, + Script { + names: &["Hmng", "Pahawh_Hmong"], + sc: SCRIPT_PAHAWH_HMONG, + scx: SCRIPT_PAHAWH_HMONG, + }, + Script { + names: &["Palm", "Palmyrene"], + sc: SCRIPT_PALMYRENE, + scx: SCRIPT_PALMYRENE, + }, + Script { + names: &["Pauc", "Pau_Cin_Hau"], + sc: SCRIPT_PAU_CIN_HAU, + scx: SCRIPT_PAU_CIN_HAU, + }, + Script { + names: &["Phag", "Phags_Pa"], + sc: SCRIPT_PHAGS_PA, + scx: SCRIPT_EXT_PHAGS_PA, + }, + Script { + names: &["Phnx", "Phoenician"], + sc: SCRIPT_PHOENICIAN, + scx: SCRIPT_PHOENICIAN, + }, + Script { + names: &["Pcun", "Proto_Cuneiform"], + sc: SCRIPT_PROTO_CUNEIFORM, + scx: SCRIPT_EXT_PROTO_CUNEIFORM, + }, + Script { + names: &["Phlp", "Psalter_Pahlavi"], + sc: SCRIPT_PSALTER_PAHLAVI, + scx: SCRIPT_EXT_PSALTER_PAHLAVI, + }, + Script { + names: &["Rjng", "Rejang"], + sc: SCRIPT_REJANG, + scx: SCRIPT_REJANG, + }, + Script { + names: &["Runr", "Runic"], + sc: SCRIPT_RUNIC, + scx: SCRIPT_EXT_RUNIC, + }, + Script { + names: &["Samr", "Samaritan"], + sc: SCRIPT_SAMARITAN, + scx: SCRIPT_EXT_SAMARITAN, + }, + Script { + names: &["Saur", "Saurashtra"], + sc: SCRIPT_SAURASHTRA, + scx: SCRIPT_SAURASHTRA, + }, + Script { + names: &["Seal"], + sc: SCRIPT_SEAL, + scx: SCRIPT_SEAL, + }, + Script { + names: &["Shrd", "Sharada"], + sc: SCRIPT_SHARADA, + scx: SCRIPT_EXT_SHARADA, + }, + Script { + names: &["Shaw", "Shavian"], + sc: SCRIPT_SHAVIAN, + scx: SCRIPT_EXT_SHAVIAN, + }, + Script { + names: &["Sidd", "Siddham"], + sc: SCRIPT_SIDDHAM, + scx: SCRIPT_SIDDHAM, + }, + Script { + names: &["Sidt", "Sidetic"], + sc: SCRIPT_SIDETIC, + scx: SCRIPT_SIDETIC, + }, + Script { + names: &["Sgnw", "SignWriting"], + sc: SCRIPT_SIGNWRITING, + scx: SCRIPT_SIGNWRITING, + }, + Script { + names: &["Sinh", "Sinhala"], + sc: SCRIPT_SINHALA, + scx: SCRIPT_EXT_SINHALA, + }, + Script { + names: &["Sogd", "Sogdian"], + sc: SCRIPT_SOGDIAN, + scx: SCRIPT_EXT_SOGDIAN, + }, + Script { + names: &["Sora", "Sora_Sompeng"], + sc: SCRIPT_SORA_SOMPENG, + scx: SCRIPT_SORA_SOMPENG, + }, + Script { + names: &["Soyo", "Soyombo"], + sc: SCRIPT_SOYOMBO, + scx: SCRIPT_SOYOMBO, + }, + Script { + names: &["Sund", "Sundanese"], + sc: SCRIPT_SUNDANESE, + scx: SCRIPT_SUNDANESE, + }, + Script { + names: &["Sunu", "Sunuwar"], + sc: SCRIPT_SUNUWAR, + scx: SCRIPT_EXT_SUNUWAR, + }, + Script { + names: &["Sylo", "Syloti_Nagri"], + sc: SCRIPT_SYLOTI_NAGRI, + scx: SCRIPT_EXT_SYLOTI_NAGRI, + }, + Script { + names: &["Syrc", "Syriac"], + sc: SCRIPT_SYRIAC, + scx: SCRIPT_EXT_SYRIAC, + }, + Script { + names: &["Tglg", "Tagalog"], + sc: SCRIPT_TAGALOG, + scx: SCRIPT_EXT_TAGALOG, + }, + Script { + names: &["Tagb", "Tagbanwa"], + sc: SCRIPT_TAGBANWA, + scx: SCRIPT_EXT_TAGBANWA, + }, + Script { + names: &["Tale", "Tai_Le"], + sc: SCRIPT_TAI_LE, + scx: SCRIPT_EXT_TAI_LE, + }, + Script { + names: &["Lana", "Tai_Tham"], + sc: SCRIPT_TAI_THAM, + scx: SCRIPT_TAI_THAM, + }, + Script { + names: &["Tavt", "Tai_Viet"], + sc: SCRIPT_TAI_VIET, + scx: SCRIPT_TAI_VIET, + }, + Script { + names: &["Tayo", "Tai_Yo"], + sc: SCRIPT_TAI_YO, + scx: SCRIPT_TAI_YO, + }, + Script { + names: &["Takr", "Takri"], + sc: SCRIPT_TAKRI, + scx: SCRIPT_EXT_TAKRI, + }, + Script { + names: &["Taml", "Tamil"], + sc: SCRIPT_TAMIL, + scx: SCRIPT_EXT_TAMIL, + }, + Script { + names: &["Tnsa", "Tangsa"], + sc: SCRIPT_TANGSA, + scx: SCRIPT_TANGSA, + }, + Script { + names: &["Tang", "Tangut"], + sc: SCRIPT_TANGUT, + scx: SCRIPT_EXT_TANGUT, + }, + Script { + names: &["Telu", "Telugu"], + sc: SCRIPT_TELUGU, + scx: SCRIPT_EXT_TELUGU, + }, + Script { + names: &["Thaa", "Thaana"], + sc: SCRIPT_THAANA, + scx: SCRIPT_EXT_THAANA, + }, + Script { + names: &["Thai"], + sc: SCRIPT_THAI, + scx: SCRIPT_EXT_THAI, + }, + Script { + names: &["Tibt", "Tibetan"], + sc: SCRIPT_TIBETAN, + scx: SCRIPT_EXT_TIBETAN, + }, + Script { + names: &["Tfng", "Tifinagh"], + sc: SCRIPT_TIFINAGH, + scx: SCRIPT_EXT_TIFINAGH, + }, + Script { + names: &["Tirh", "Tirhuta"], + sc: SCRIPT_TIRHUTA, + scx: SCRIPT_EXT_TIRHUTA, + }, + Script { + names: &["Todr", "Todhri"], + sc: SCRIPT_TODHRI, + scx: SCRIPT_EXT_TODHRI, + }, + Script { + names: &["Tols", "Tolong_Siki"], + sc: SCRIPT_TOLONG_SIKI, + scx: SCRIPT_TOLONG_SIKI, + }, + Script { + names: &["Toto"], + sc: SCRIPT_TOTO, + scx: SCRIPT_EXT_TOTO, + }, + Script { + names: &["Tutg", "Tulu_Tigalari"], + sc: SCRIPT_TULU_TIGALARI, + scx: SCRIPT_EXT_TULU_TIGALARI, + }, + Script { + names: &["Ugar", "Ugaritic"], + sc: SCRIPT_UGARITIC, + scx: SCRIPT_UGARITIC, + }, + Script { + names: &["Zzzz", "Unknown"], + sc: SCRIPT_UNKNOWN, + scx: SCRIPT_UNKNOWN, + }, + Script { + names: &["Vaii", "Vai"], + sc: SCRIPT_VAI, + scx: SCRIPT_VAI, + }, + Script { + names: &["Vith", "Vithkuqi"], + sc: SCRIPT_VITHKUQI, + scx: SCRIPT_VITHKUQI, + }, + Script { + names: &["Wcho", "Wancho"], + sc: SCRIPT_WANCHO, + scx: SCRIPT_WANCHO, + }, + Script { + names: &["Wara", "Warang_Citi"], + sc: SCRIPT_WARANG_CITI, + scx: SCRIPT_WARANG_CITI, + }, + Script { + names: &["Yezi", "Yezidi"], + sc: SCRIPT_YEZIDI, + scx: SCRIPT_EXT_YEZIDI, + }, + Script { + names: &["Yiii", "Yi"], + sc: SCRIPT_YI, + scx: SCRIPT_EXT_YI, + }, + Script { + names: &["Zanb", "Zanabazar_Square"], + sc: SCRIPT_ZANABAZAR_SQUARE, + scx: SCRIPT_ZANABAZAR_SQUARE, + }, +]; + +/// The one-character members of Basic_Emoji, as ranges. +pub static STRING_CHARS_BASIC_EMOJI: &[(u32, u32)] = &[ + (0x231A, 0x231B), + (0x23E9, 0x23EC), + (0x23F0, 0x23F0), + (0x23F3, 0x23F3), + (0x25FD, 0x25FE), + (0x2614, 0x2615), + (0x2648, 0x2653), + (0x267F, 0x267F), + (0x2693, 0x2693), + (0x26A1, 0x26A1), + (0x26AA, 0x26AB), + (0x26BD, 0x26BE), + (0x26C4, 0x26C5), + (0x26CE, 0x26CE), + (0x26D4, 0x26D4), + (0x26EA, 0x26EA), + (0x26F2, 0x26F3), + (0x26F5, 0x26F5), + (0x26FA, 0x26FA), + (0x26FD, 0x26FD), + (0x2705, 0x2705), + (0x270A, 0x270B), + (0x2728, 0x2728), + (0x274C, 0x274C), + (0x274E, 0x274E), + (0x2753, 0x2755), + (0x2757, 0x2757), + (0x2795, 0x2797), + (0x27B0, 0x27B0), + (0x27BF, 0x27BF), + (0x2B1B, 0x2B1C), + (0x2B50, 0x2B50), + (0x2B55, 0x2B55), + (0x1F004, 0x1F004), + (0x1F0CF, 0x1F0CF), + (0x1F18E, 0x1F18E), + (0x1F191, 0x1F19A), + (0x1F201, 0x1F201), + (0x1F21A, 0x1F21A), + (0x1F22F, 0x1F22F), + (0x1F232, 0x1F236), + (0x1F238, 0x1F23A), + (0x1F250, 0x1F251), + (0x1F300, 0x1F320), + (0x1F32D, 0x1F335), + (0x1F337, 0x1F37C), + (0x1F37E, 0x1F393), + (0x1F3A0, 0x1F3CA), + (0x1F3CF, 0x1F3D3), + (0x1F3E0, 0x1F3F0), + (0x1F3F4, 0x1F3F4), + (0x1F3F8, 0x1F43E), + (0x1F440, 0x1F440), + (0x1F442, 0x1F4FC), + (0x1F4FF, 0x1F53D), + (0x1F54B, 0x1F54E), + (0x1F550, 0x1F567), + (0x1F57A, 0x1F57A), + (0x1F595, 0x1F596), + (0x1F5A4, 0x1F5A4), + (0x1F5FB, 0x1F64F), + (0x1F680, 0x1F6C5), + (0x1F6CC, 0x1F6CC), + (0x1F6D0, 0x1F6D2), + (0x1F6D5, 0x1F6D9), + (0x1F6DC, 0x1F6DF), + (0x1F6EB, 0x1F6EC), + (0x1F6F4, 0x1F6FC), + (0x1F7E0, 0x1F7EB), + (0x1F7F0, 0x1F7F0), + (0x1F90C, 0x1F93A), + (0x1F93C, 0x1F945), + (0x1F947, 0x1F9FF), + (0x1FA70, 0x1FA7C), + (0x1FA80, 0x1FAC6), + (0x1FAC8, 0x1FAC8), + (0x1FACC, 0x1FADD), + (0x1FADF, 0x1FAEB), + (0x1FAEF, 0x1FAFA), +]; + +/// The members of Basic_Emoji of two or more code points, in the order of the file. +pub static STRINGS_BASIC_EMOJI: &[&str] = &[ + "\u{A9}\u{FE0F}", + "\u{AE}\u{FE0F}", + "\u{203C}\u{FE0F}", + "\u{2049}\u{FE0F}", + "\u{2122}\u{FE0F}", + "\u{2139}\u{FE0F}", + "\u{2194}\u{FE0F}", + "\u{2195}\u{FE0F}", + "\u{2196}\u{FE0F}", + "\u{2197}\u{FE0F}", + "\u{2198}\u{FE0F}", + "\u{2199}\u{FE0F}", + "\u{21A9}\u{FE0F}", + "\u{21AA}\u{FE0F}", + "\u{2328}\u{FE0F}", + "\u{23CF}\u{FE0F}", + "\u{23ED}\u{FE0F}", + "\u{23EE}\u{FE0F}", + "\u{23EF}\u{FE0F}", + "\u{23F1}\u{FE0F}", + "\u{23F2}\u{FE0F}", + "\u{23F8}\u{FE0F}", + "\u{23F9}\u{FE0F}", + "\u{23FA}\u{FE0F}", + "\u{24C2}\u{FE0F}", + "\u{25AA}\u{FE0F}", + "\u{25AB}\u{FE0F}", + "\u{25B6}\u{FE0F}", + "\u{25C0}\u{FE0F}", + "\u{25FB}\u{FE0F}", + "\u{25FC}\u{FE0F}", + "\u{2600}\u{FE0F}", + "\u{2601}\u{FE0F}", + "\u{2602}\u{FE0F}", + "\u{2603}\u{FE0F}", + "\u{2604}\u{FE0F}", + "\u{260E}\u{FE0F}", + "\u{2611}\u{FE0F}", + "\u{2618}\u{FE0F}", + "\u{261D}\u{FE0F}", + "\u{2620}\u{FE0F}", + "\u{2622}\u{FE0F}", + "\u{2623}\u{FE0F}", + "\u{2626}\u{FE0F}", + "\u{262A}\u{FE0F}", + "\u{262E}\u{FE0F}", + "\u{262F}\u{FE0F}", + "\u{2638}\u{FE0F}", + "\u{2639}\u{FE0F}", + "\u{263A}\u{FE0F}", + "\u{2640}\u{FE0F}", + "\u{2642}\u{FE0F}", + "\u{265F}\u{FE0F}", + "\u{2660}\u{FE0F}", + "\u{2663}\u{FE0F}", + "\u{2665}\u{FE0F}", + "\u{2666}\u{FE0F}", + "\u{2668}\u{FE0F}", + "\u{267B}\u{FE0F}", + "\u{267E}\u{FE0F}", + "\u{2692}\u{FE0F}", + "\u{2694}\u{FE0F}", + "\u{2695}\u{FE0F}", + "\u{2696}\u{FE0F}", + "\u{2697}\u{FE0F}", + "\u{2699}\u{FE0F}", + "\u{269B}\u{FE0F}", + "\u{269C}\u{FE0F}", + "\u{26A0}\u{FE0F}", + "\u{26A7}\u{FE0F}", + "\u{26B0}\u{FE0F}", + "\u{26B1}\u{FE0F}", + "\u{26C8}\u{FE0F}", + "\u{26CF}\u{FE0F}", + "\u{26D1}\u{FE0F}", + "\u{26D3}\u{FE0F}", + "\u{26E9}\u{FE0F}", + "\u{26F0}\u{FE0F}", + "\u{26F1}\u{FE0F}", + "\u{26F4}\u{FE0F}", + "\u{26F7}\u{FE0F}", + "\u{26F8}\u{FE0F}", + "\u{26F9}\u{FE0F}", + "\u{2702}\u{FE0F}", + "\u{2708}\u{FE0F}", + "\u{2709}\u{FE0F}", + "\u{270C}\u{FE0F}", + "\u{270D}\u{FE0F}", + "\u{270F}\u{FE0F}", + "\u{2712}\u{FE0F}", + "\u{2714}\u{FE0F}", + "\u{2716}\u{FE0F}", + "\u{271D}\u{FE0F}", + "\u{2721}\u{FE0F}", + "\u{2733}\u{FE0F}", + "\u{2734}\u{FE0F}", + "\u{2744}\u{FE0F}", + "\u{2747}\u{FE0F}", + "\u{2763}\u{FE0F}", + "\u{2764}\u{FE0F}", + "\u{27A1}\u{FE0F}", + "\u{2934}\u{FE0F}", + "\u{2935}\u{FE0F}", + "\u{2B05}\u{FE0F}", + "\u{2B06}\u{FE0F}", + "\u{2B07}\u{FE0F}", + "\u{3030}\u{FE0F}", + "\u{303D}\u{FE0F}", + "\u{3297}\u{FE0F}", + "\u{3299}\u{FE0F}", + "\u{1F170}\u{FE0F}", + "\u{1F171}\u{FE0F}", + "\u{1F17E}\u{FE0F}", + "\u{1F17F}\u{FE0F}", + "\u{1F202}\u{FE0F}", + "\u{1F237}\u{FE0F}", + "\u{1F321}\u{FE0F}", + "\u{1F324}\u{FE0F}", + "\u{1F325}\u{FE0F}", + "\u{1F326}\u{FE0F}", + "\u{1F327}\u{FE0F}", + "\u{1F328}\u{FE0F}", + "\u{1F329}\u{FE0F}", + "\u{1F32A}\u{FE0F}", + "\u{1F32B}\u{FE0F}", + "\u{1F32C}\u{FE0F}", + "\u{1F336}\u{FE0F}", + "\u{1F37D}\u{FE0F}", + "\u{1F396}\u{FE0F}", + "\u{1F397}\u{FE0F}", + "\u{1F399}\u{FE0F}", + "\u{1F39A}\u{FE0F}", + "\u{1F39B}\u{FE0F}", + "\u{1F39E}\u{FE0F}", + "\u{1F39F}\u{FE0F}", + "\u{1F3CB}\u{FE0F}", + "\u{1F3CC}\u{FE0F}", + "\u{1F3CD}\u{FE0F}", + "\u{1F3CE}\u{FE0F}", + "\u{1F3D4}\u{FE0F}", + "\u{1F3D5}\u{FE0F}", + "\u{1F3D6}\u{FE0F}", + "\u{1F3D7}\u{FE0F}", + "\u{1F3D8}\u{FE0F}", + "\u{1F3D9}\u{FE0F}", + "\u{1F3DA}\u{FE0F}", + "\u{1F3DB}\u{FE0F}", + "\u{1F3DC}\u{FE0F}", + "\u{1F3DD}\u{FE0F}", + "\u{1F3DE}\u{FE0F}", + "\u{1F3DF}\u{FE0F}", + "\u{1F3F3}\u{FE0F}", + "\u{1F3F5}\u{FE0F}", + "\u{1F3F7}\u{FE0F}", + "\u{1F43F}\u{FE0F}", + "\u{1F441}\u{FE0F}", + "\u{1F4FD}\u{FE0F}", + "\u{1F549}\u{FE0F}", + "\u{1F54A}\u{FE0F}", + "\u{1F56F}\u{FE0F}", + "\u{1F570}\u{FE0F}", + "\u{1F573}\u{FE0F}", + "\u{1F574}\u{FE0F}", + "\u{1F575}\u{FE0F}", + "\u{1F576}\u{FE0F}", + "\u{1F577}\u{FE0F}", + "\u{1F578}\u{FE0F}", + "\u{1F579}\u{FE0F}", + "\u{1F587}\u{FE0F}", + "\u{1F58A}\u{FE0F}", + "\u{1F58B}\u{FE0F}", + "\u{1F58C}\u{FE0F}", + "\u{1F58D}\u{FE0F}", + "\u{1F590}\u{FE0F}", + "\u{1F5A5}\u{FE0F}", + "\u{1F5A8}\u{FE0F}", + "\u{1F5B1}\u{FE0F}", + "\u{1F5B2}\u{FE0F}", + "\u{1F5BC}\u{FE0F}", + "\u{1F5C2}\u{FE0F}", + "\u{1F5C3}\u{FE0F}", + "\u{1F5C4}\u{FE0F}", + "\u{1F5D1}\u{FE0F}", + "\u{1F5D2}\u{FE0F}", + "\u{1F5D3}\u{FE0F}", + "\u{1F5DC}\u{FE0F}", + "\u{1F5DD}\u{FE0F}", + "\u{1F5DE}\u{FE0F}", + "\u{1F5E1}\u{FE0F}", + "\u{1F5E3}\u{FE0F}", + "\u{1F5E8}\u{FE0F}", + "\u{1F5EF}\u{FE0F}", + "\u{1F5F3}\u{FE0F}", + "\u{1F5FA}\u{FE0F}", + "\u{1F6CB}\u{FE0F}", + "\u{1F6CD}\u{FE0F}", + "\u{1F6CE}\u{FE0F}", + "\u{1F6CF}\u{FE0F}", + "\u{1F6E0}\u{FE0F}", + "\u{1F6E1}\u{FE0F}", + "\u{1F6E2}\u{FE0F}", + "\u{1F6E3}\u{FE0F}", + "\u{1F6E4}\u{FE0F}", + "\u{1F6E5}\u{FE0F}", + "\u{1F6E9}\u{FE0F}", + "\u{1F6F0}\u{FE0F}", + "\u{1F6F3}\u{FE0F}", +]; + +/// The one-character members of Emoji_Keycap_Sequence, as ranges. +pub static STRING_CHARS_EMOJI_KEYCAP_SEQUENCE: &[(u32, u32)] = &[]; + +/// The members of Emoji_Keycap_Sequence of two or more code points, in the order of the file. +pub static STRINGS_EMOJI_KEYCAP_SEQUENCE: &[&str] = &[ + "\u{23}\u{FE0F}\u{20E3}", + "\u{2A}\u{FE0F}\u{20E3}", + "\u{30}\u{FE0F}\u{20E3}", + "\u{31}\u{FE0F}\u{20E3}", + "\u{32}\u{FE0F}\u{20E3}", + "\u{33}\u{FE0F}\u{20E3}", + "\u{34}\u{FE0F}\u{20E3}", + "\u{35}\u{FE0F}\u{20E3}", + "\u{36}\u{FE0F}\u{20E3}", + "\u{37}\u{FE0F}\u{20E3}", + "\u{38}\u{FE0F}\u{20E3}", + "\u{39}\u{FE0F}\u{20E3}", +]; + +/// The one-character members of RGI_Emoji_Flag_Sequence, as ranges. +pub static STRING_CHARS_RGI_EMOJI_FLAG_SEQUENCE: &[(u32, u32)] = &[]; + +/// The members of RGI_Emoji_Flag_Sequence of two or more code points, in the order of the file. +pub static STRINGS_RGI_EMOJI_FLAG_SEQUENCE: &[&str] = &[ + "\u{1F1E6}\u{1F1E8}", + "\u{1F1E6}\u{1F1E9}", + "\u{1F1E6}\u{1F1EA}", + "\u{1F1E6}\u{1F1EB}", + "\u{1F1E6}\u{1F1EC}", + "\u{1F1E6}\u{1F1EE}", + "\u{1F1E6}\u{1F1F1}", + "\u{1F1E6}\u{1F1F2}", + "\u{1F1E6}\u{1F1F4}", + "\u{1F1E6}\u{1F1F6}", + "\u{1F1E6}\u{1F1F7}", + "\u{1F1E6}\u{1F1F8}", + "\u{1F1E6}\u{1F1F9}", + "\u{1F1E6}\u{1F1FA}", + "\u{1F1E6}\u{1F1FC}", + "\u{1F1E6}\u{1F1FD}", + "\u{1F1E6}\u{1F1FF}", + "\u{1F1E7}\u{1F1E6}", + "\u{1F1E7}\u{1F1E7}", + "\u{1F1E7}\u{1F1E9}", + "\u{1F1E7}\u{1F1EA}", + "\u{1F1E7}\u{1F1EB}", + "\u{1F1E7}\u{1F1EC}", + "\u{1F1E7}\u{1F1ED}", + "\u{1F1E7}\u{1F1EE}", + "\u{1F1E7}\u{1F1EF}", + "\u{1F1E7}\u{1F1F1}", + "\u{1F1E7}\u{1F1F2}", + "\u{1F1E7}\u{1F1F3}", + "\u{1F1E7}\u{1F1F4}", + "\u{1F1E7}\u{1F1F6}", + "\u{1F1E7}\u{1F1F7}", + "\u{1F1E7}\u{1F1F8}", + "\u{1F1E7}\u{1F1F9}", + "\u{1F1E7}\u{1F1FB}", + "\u{1F1E7}\u{1F1FC}", + "\u{1F1E7}\u{1F1FE}", + "\u{1F1E7}\u{1F1FF}", + "\u{1F1E8}\u{1F1E6}", + "\u{1F1E8}\u{1F1E8}", + "\u{1F1E8}\u{1F1E9}", + "\u{1F1E8}\u{1F1EB}", + "\u{1F1E8}\u{1F1EC}", + "\u{1F1E8}\u{1F1ED}", + "\u{1F1E8}\u{1F1EE}", + "\u{1F1E8}\u{1F1F0}", + "\u{1F1E8}\u{1F1F1}", + "\u{1F1E8}\u{1F1F2}", + "\u{1F1E8}\u{1F1F3}", + "\u{1F1E8}\u{1F1F4}", + "\u{1F1E8}\u{1F1F5}", + "\u{1F1E8}\u{1F1F6}", + "\u{1F1E8}\u{1F1F7}", + "\u{1F1E8}\u{1F1FA}", + "\u{1F1E8}\u{1F1FB}", + "\u{1F1E8}\u{1F1FC}", + "\u{1F1E8}\u{1F1FD}", + "\u{1F1E8}\u{1F1FE}", + "\u{1F1E8}\u{1F1FF}", + "\u{1F1E9}\u{1F1EA}", + "\u{1F1E9}\u{1F1EC}", + "\u{1F1E9}\u{1F1EF}", + "\u{1F1E9}\u{1F1F0}", + "\u{1F1E9}\u{1F1F2}", + "\u{1F1E9}\u{1F1F4}", + "\u{1F1E9}\u{1F1FF}", + "\u{1F1EA}\u{1F1E6}", + "\u{1F1EA}\u{1F1E8}", + "\u{1F1EA}\u{1F1EA}", + "\u{1F1EA}\u{1F1EC}", + "\u{1F1EA}\u{1F1ED}", + "\u{1F1EA}\u{1F1F7}", + "\u{1F1EA}\u{1F1F8}", + "\u{1F1EA}\u{1F1F9}", + "\u{1F1EA}\u{1F1FA}", + "\u{1F1EB}\u{1F1EE}", + "\u{1F1EB}\u{1F1EF}", + "\u{1F1EB}\u{1F1F0}", + "\u{1F1EB}\u{1F1F2}", + "\u{1F1EB}\u{1F1F4}", + "\u{1F1EB}\u{1F1F7}", + "\u{1F1EC}\u{1F1E6}", + "\u{1F1EC}\u{1F1E7}", + "\u{1F1EC}\u{1F1E9}", + "\u{1F1EC}\u{1F1EA}", + "\u{1F1EC}\u{1F1EB}", + "\u{1F1EC}\u{1F1EC}", + "\u{1F1EC}\u{1F1ED}", + "\u{1F1EC}\u{1F1EE}", + "\u{1F1EC}\u{1F1F1}", + "\u{1F1EC}\u{1F1F2}", + "\u{1F1EC}\u{1F1F3}", + "\u{1F1EC}\u{1F1F5}", + "\u{1F1EC}\u{1F1F6}", + "\u{1F1EC}\u{1F1F7}", + "\u{1F1EC}\u{1F1F8}", + "\u{1F1EC}\u{1F1F9}", + "\u{1F1EC}\u{1F1FA}", + "\u{1F1EC}\u{1F1FC}", + "\u{1F1EC}\u{1F1FE}", + "\u{1F1ED}\u{1F1F0}", + "\u{1F1ED}\u{1F1F2}", + "\u{1F1ED}\u{1F1F3}", + "\u{1F1ED}\u{1F1F7}", + "\u{1F1ED}\u{1F1F9}", + "\u{1F1ED}\u{1F1FA}", + "\u{1F1EE}\u{1F1E8}", + "\u{1F1EE}\u{1F1E9}", + "\u{1F1EE}\u{1F1EA}", + "\u{1F1EE}\u{1F1F1}", + "\u{1F1EE}\u{1F1F2}", + "\u{1F1EE}\u{1F1F3}", + "\u{1F1EE}\u{1F1F4}", + "\u{1F1EE}\u{1F1F6}", + "\u{1F1EE}\u{1F1F7}", + "\u{1F1EE}\u{1F1F8}", + "\u{1F1EE}\u{1F1F9}", + "\u{1F1EF}\u{1F1EA}", + "\u{1F1EF}\u{1F1F2}", + "\u{1F1EF}\u{1F1F4}", + "\u{1F1EF}\u{1F1F5}", + "\u{1F1F0}\u{1F1EA}", + "\u{1F1F0}\u{1F1EC}", + "\u{1F1F0}\u{1F1ED}", + "\u{1F1F0}\u{1F1EE}", + "\u{1F1F0}\u{1F1F2}", + "\u{1F1F0}\u{1F1F3}", + "\u{1F1F0}\u{1F1F5}", + "\u{1F1F0}\u{1F1F7}", + "\u{1F1F0}\u{1F1FC}", + "\u{1F1F0}\u{1F1FE}", + "\u{1F1F0}\u{1F1FF}", + "\u{1F1F1}\u{1F1E6}", + "\u{1F1F1}\u{1F1E7}", + "\u{1F1F1}\u{1F1E8}", + "\u{1F1F1}\u{1F1EE}", + "\u{1F1F1}\u{1F1F0}", + "\u{1F1F1}\u{1F1F7}", + "\u{1F1F1}\u{1F1F8}", + "\u{1F1F1}\u{1F1F9}", + "\u{1F1F1}\u{1F1FA}", + "\u{1F1F1}\u{1F1FB}", + "\u{1F1F1}\u{1F1FE}", + "\u{1F1F2}\u{1F1E6}", + "\u{1F1F2}\u{1F1E8}", + "\u{1F1F2}\u{1F1E9}", + "\u{1F1F2}\u{1F1EA}", + "\u{1F1F2}\u{1F1EB}", + "\u{1F1F2}\u{1F1EC}", + "\u{1F1F2}\u{1F1ED}", + "\u{1F1F2}\u{1F1F0}", + "\u{1F1F2}\u{1F1F1}", + "\u{1F1F2}\u{1F1F2}", + "\u{1F1F2}\u{1F1F3}", + "\u{1F1F2}\u{1F1F4}", + "\u{1F1F2}\u{1F1F5}", + "\u{1F1F2}\u{1F1F6}", + "\u{1F1F2}\u{1F1F7}", + "\u{1F1F2}\u{1F1F8}", + "\u{1F1F2}\u{1F1F9}", + "\u{1F1F2}\u{1F1FA}", + "\u{1F1F2}\u{1F1FB}", + "\u{1F1F2}\u{1F1FC}", + "\u{1F1F2}\u{1F1FD}", + "\u{1F1F2}\u{1F1FE}", + "\u{1F1F2}\u{1F1FF}", + "\u{1F1F3}\u{1F1E6}", + "\u{1F1F3}\u{1F1E8}", + "\u{1F1F3}\u{1F1EA}", + "\u{1F1F3}\u{1F1EB}", + "\u{1F1F3}\u{1F1EC}", + "\u{1F1F3}\u{1F1EE}", + "\u{1F1F3}\u{1F1F1}", + "\u{1F1F3}\u{1F1F4}", + "\u{1F1F3}\u{1F1F5}", + "\u{1F1F3}\u{1F1F7}", + "\u{1F1F3}\u{1F1FA}", + "\u{1F1F3}\u{1F1FF}", + "\u{1F1F4}\u{1F1F2}", + "\u{1F1F5}\u{1F1E6}", + "\u{1F1F5}\u{1F1EA}", + "\u{1F1F5}\u{1F1EB}", + "\u{1F1F5}\u{1F1EC}", + "\u{1F1F5}\u{1F1ED}", + "\u{1F1F5}\u{1F1F0}", + "\u{1F1F5}\u{1F1F1}", + "\u{1F1F5}\u{1F1F2}", + "\u{1F1F5}\u{1F1F3}", + "\u{1F1F5}\u{1F1F7}", + "\u{1F1F5}\u{1F1F8}", + "\u{1F1F5}\u{1F1F9}", + "\u{1F1F5}\u{1F1FC}", + "\u{1F1F5}\u{1F1FE}", + "\u{1F1F6}\u{1F1E6}", + "\u{1F1F7}\u{1F1EA}", + "\u{1F1F7}\u{1F1F4}", + "\u{1F1F7}\u{1F1F8}", + "\u{1F1F7}\u{1F1FA}", + "\u{1F1F7}\u{1F1FC}", + "\u{1F1F8}\u{1F1E6}", + "\u{1F1F8}\u{1F1E7}", + "\u{1F1F8}\u{1F1E8}", + "\u{1F1F8}\u{1F1E9}", + "\u{1F1F8}\u{1F1EA}", + "\u{1F1F8}\u{1F1EC}", + "\u{1F1F8}\u{1F1ED}", + "\u{1F1F8}\u{1F1EE}", + "\u{1F1F8}\u{1F1EF}", + "\u{1F1F8}\u{1F1F0}", + "\u{1F1F8}\u{1F1F1}", + "\u{1F1F8}\u{1F1F2}", + "\u{1F1F8}\u{1F1F3}", + "\u{1F1F8}\u{1F1F4}", + "\u{1F1F8}\u{1F1F7}", + "\u{1F1F8}\u{1F1F8}", + "\u{1F1F8}\u{1F1F9}", + "\u{1F1F8}\u{1F1FB}", + "\u{1F1F8}\u{1F1FD}", + "\u{1F1F8}\u{1F1FE}", + "\u{1F1F8}\u{1F1FF}", + "\u{1F1F9}\u{1F1E6}", + "\u{1F1F9}\u{1F1E8}", + "\u{1F1F9}\u{1F1E9}", + "\u{1F1F9}\u{1F1EB}", + "\u{1F1F9}\u{1F1EC}", + "\u{1F1F9}\u{1F1ED}", + "\u{1F1F9}\u{1F1EF}", + "\u{1F1F9}\u{1F1F0}", + "\u{1F1F9}\u{1F1F1}", + "\u{1F1F9}\u{1F1F2}", + "\u{1F1F9}\u{1F1F3}", + "\u{1F1F9}\u{1F1F4}", + "\u{1F1F9}\u{1F1F7}", + "\u{1F1F9}\u{1F1F9}", + "\u{1F1F9}\u{1F1FB}", + "\u{1F1F9}\u{1F1FC}", + "\u{1F1F9}\u{1F1FF}", + "\u{1F1FA}\u{1F1E6}", + "\u{1F1FA}\u{1F1EC}", + "\u{1F1FA}\u{1F1F2}", + "\u{1F1FA}\u{1F1F3}", + "\u{1F1FA}\u{1F1F8}", + "\u{1F1FA}\u{1F1FE}", + "\u{1F1FA}\u{1F1FF}", + "\u{1F1FB}\u{1F1E6}", + "\u{1F1FB}\u{1F1E8}", + "\u{1F1FB}\u{1F1EA}", + "\u{1F1FB}\u{1F1EC}", + "\u{1F1FB}\u{1F1EE}", + "\u{1F1FB}\u{1F1F3}", + "\u{1F1FB}\u{1F1FA}", + "\u{1F1FC}\u{1F1EB}", + "\u{1F1FC}\u{1F1F8}", + "\u{1F1FD}\u{1F1F0}", + "\u{1F1FE}\u{1F1EA}", + "\u{1F1FE}\u{1F1F9}", + "\u{1F1FF}\u{1F1E6}", + "\u{1F1FF}\u{1F1F2}", + "\u{1F1FF}\u{1F1FC}", +]; + +/// The one-character members of RGI_Emoji_Modifier_Sequence, as ranges. +pub static STRING_CHARS_RGI_EMOJI_MODIFIER_SEQUENCE: &[(u32, u32)] = &[]; + +/// The members of RGI_Emoji_Modifier_Sequence of two or more code points, in the order of the file. +pub static STRINGS_RGI_EMOJI_MODIFIER_SEQUENCE: &[&str] = &[ + "\u{261D}\u{1F3FB}", + "\u{261D}\u{1F3FC}", + "\u{261D}\u{1F3FD}", + "\u{261D}\u{1F3FE}", + "\u{261D}\u{1F3FF}", + "\u{26F9}\u{1F3FB}", + "\u{26F9}\u{1F3FC}", + "\u{26F9}\u{1F3FD}", + "\u{26F9}\u{1F3FE}", + "\u{26F9}\u{1F3FF}", + "\u{270A}\u{1F3FB}", + "\u{270A}\u{1F3FC}", + "\u{270A}\u{1F3FD}", + "\u{270A}\u{1F3FE}", + "\u{270A}\u{1F3FF}", + "\u{270B}\u{1F3FB}", + "\u{270B}\u{1F3FC}", + "\u{270B}\u{1F3FD}", + "\u{270B}\u{1F3FE}", + "\u{270B}\u{1F3FF}", + "\u{270C}\u{1F3FB}", + "\u{270C}\u{1F3FC}", + "\u{270C}\u{1F3FD}", + "\u{270C}\u{1F3FE}", + "\u{270C}\u{1F3FF}", + "\u{270D}\u{1F3FB}", + "\u{270D}\u{1F3FC}", + "\u{270D}\u{1F3FD}", + "\u{270D}\u{1F3FE}", + "\u{270D}\u{1F3FF}", + "\u{1F385}\u{1F3FB}", + "\u{1F385}\u{1F3FC}", + "\u{1F385}\u{1F3FD}", + "\u{1F385}\u{1F3FE}", + "\u{1F385}\u{1F3FF}", + "\u{1F3C2}\u{1F3FB}", + "\u{1F3C2}\u{1F3FC}", + "\u{1F3C2}\u{1F3FD}", + "\u{1F3C2}\u{1F3FE}", + "\u{1F3C2}\u{1F3FF}", + "\u{1F3C3}\u{1F3FB}", + "\u{1F3C3}\u{1F3FC}", + "\u{1F3C3}\u{1F3FD}", + "\u{1F3C3}\u{1F3FE}", + "\u{1F3C3}\u{1F3FF}", + "\u{1F3C4}\u{1F3FB}", + "\u{1F3C4}\u{1F3FC}", + "\u{1F3C4}\u{1F3FD}", + "\u{1F3C4}\u{1F3FE}", + "\u{1F3C4}\u{1F3FF}", + "\u{1F3C7}\u{1F3FB}", + "\u{1F3C7}\u{1F3FC}", + "\u{1F3C7}\u{1F3FD}", + "\u{1F3C7}\u{1F3FE}", + "\u{1F3C7}\u{1F3FF}", + "\u{1F3CA}\u{1F3FB}", + "\u{1F3CA}\u{1F3FC}", + "\u{1F3CA}\u{1F3FD}", + "\u{1F3CA}\u{1F3FE}", + "\u{1F3CA}\u{1F3FF}", + "\u{1F3CB}\u{1F3FB}", + "\u{1F3CB}\u{1F3FC}", + "\u{1F3CB}\u{1F3FD}", + "\u{1F3CB}\u{1F3FE}", + "\u{1F3CB}\u{1F3FF}", + "\u{1F3CC}\u{1F3FB}", + "\u{1F3CC}\u{1F3FC}", + "\u{1F3CC}\u{1F3FD}", + "\u{1F3CC}\u{1F3FE}", + "\u{1F3CC}\u{1F3FF}", + "\u{1F442}\u{1F3FB}", + "\u{1F442}\u{1F3FC}", + "\u{1F442}\u{1F3FD}", + "\u{1F442}\u{1F3FE}", + "\u{1F442}\u{1F3FF}", + "\u{1F443}\u{1F3FB}", + "\u{1F443}\u{1F3FC}", + "\u{1F443}\u{1F3FD}", + "\u{1F443}\u{1F3FE}", + "\u{1F443}\u{1F3FF}", + "\u{1F446}\u{1F3FB}", + "\u{1F446}\u{1F3FC}", + "\u{1F446}\u{1F3FD}", + "\u{1F446}\u{1F3FE}", + "\u{1F446}\u{1F3FF}", + "\u{1F447}\u{1F3FB}", + "\u{1F447}\u{1F3FC}", + "\u{1F447}\u{1F3FD}", + "\u{1F447}\u{1F3FE}", + "\u{1F447}\u{1F3FF}", + "\u{1F448}\u{1F3FB}", + "\u{1F448}\u{1F3FC}", + "\u{1F448}\u{1F3FD}", + "\u{1F448}\u{1F3FE}", + "\u{1F448}\u{1F3FF}", + "\u{1F449}\u{1F3FB}", + "\u{1F449}\u{1F3FC}", + "\u{1F449}\u{1F3FD}", + "\u{1F449}\u{1F3FE}", + "\u{1F449}\u{1F3FF}", + "\u{1F44A}\u{1F3FB}", + "\u{1F44A}\u{1F3FC}", + "\u{1F44A}\u{1F3FD}", + "\u{1F44A}\u{1F3FE}", + "\u{1F44A}\u{1F3FF}", + "\u{1F44B}\u{1F3FB}", + "\u{1F44B}\u{1F3FC}", + "\u{1F44B}\u{1F3FD}", + "\u{1F44B}\u{1F3FE}", + "\u{1F44B}\u{1F3FF}", + "\u{1F44C}\u{1F3FB}", + "\u{1F44C}\u{1F3FC}", + "\u{1F44C}\u{1F3FD}", + "\u{1F44C}\u{1F3FE}", + "\u{1F44C}\u{1F3FF}", + "\u{1F44D}\u{1F3FB}", + "\u{1F44D}\u{1F3FC}", + "\u{1F44D}\u{1F3FD}", + "\u{1F44D}\u{1F3FE}", + "\u{1F44D}\u{1F3FF}", + "\u{1F44E}\u{1F3FB}", + "\u{1F44E}\u{1F3FC}", + "\u{1F44E}\u{1F3FD}", + "\u{1F44E}\u{1F3FE}", + "\u{1F44E}\u{1F3FF}", + "\u{1F44F}\u{1F3FB}", + "\u{1F44F}\u{1F3FC}", + "\u{1F44F}\u{1F3FD}", + "\u{1F44F}\u{1F3FE}", + "\u{1F44F}\u{1F3FF}", + "\u{1F450}\u{1F3FB}", + "\u{1F450}\u{1F3FC}", + "\u{1F450}\u{1F3FD}", + "\u{1F450}\u{1F3FE}", + "\u{1F450}\u{1F3FF}", + "\u{1F466}\u{1F3FB}", + "\u{1F466}\u{1F3FC}", + "\u{1F466}\u{1F3FD}", + "\u{1F466}\u{1F3FE}", + "\u{1F466}\u{1F3FF}", + "\u{1F467}\u{1F3FB}", + "\u{1F467}\u{1F3FC}", + "\u{1F467}\u{1F3FD}", + "\u{1F467}\u{1F3FE}", + "\u{1F467}\u{1F3FF}", + "\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FF}", + "\u{1F46B}\u{1F3FB}", + "\u{1F46B}\u{1F3FC}", + "\u{1F46B}\u{1F3FD}", + "\u{1F46B}\u{1F3FE}", + "\u{1F46B}\u{1F3FF}", + "\u{1F46C}\u{1F3FB}", + "\u{1F46C}\u{1F3FC}", + "\u{1F46C}\u{1F3FD}", + "\u{1F46C}\u{1F3FE}", + "\u{1F46C}\u{1F3FF}", + "\u{1F46D}\u{1F3FB}", + "\u{1F46D}\u{1F3FC}", + "\u{1F46D}\u{1F3FD}", + "\u{1F46D}\u{1F3FE}", + "\u{1F46D}\u{1F3FF}", + "\u{1F46E}\u{1F3FB}", + "\u{1F46E}\u{1F3FC}", + "\u{1F46E}\u{1F3FD}", + "\u{1F46E}\u{1F3FE}", + "\u{1F46E}\u{1F3FF}", + "\u{1F46F}\u{1F3FB}", + "\u{1F46F}\u{1F3FC}", + "\u{1F46F}\u{1F3FD}", + "\u{1F46F}\u{1F3FE}", + "\u{1F46F}\u{1F3FF}", + "\u{1F470}\u{1F3FB}", + "\u{1F470}\u{1F3FC}", + "\u{1F470}\u{1F3FD}", + "\u{1F470}\u{1F3FE}", + "\u{1F470}\u{1F3FF}", + "\u{1F471}\u{1F3FB}", + "\u{1F471}\u{1F3FC}", + "\u{1F471}\u{1F3FD}", + "\u{1F471}\u{1F3FE}", + "\u{1F471}\u{1F3FF}", + "\u{1F472}\u{1F3FB}", + "\u{1F472}\u{1F3FC}", + "\u{1F472}\u{1F3FD}", + "\u{1F472}\u{1F3FE}", + "\u{1F472}\u{1F3FF}", + "\u{1F473}\u{1F3FB}", + "\u{1F473}\u{1F3FC}", + "\u{1F473}\u{1F3FD}", + "\u{1F473}\u{1F3FE}", + "\u{1F473}\u{1F3FF}", + "\u{1F474}\u{1F3FB}", + "\u{1F474}\u{1F3FC}", + "\u{1F474}\u{1F3FD}", + "\u{1F474}\u{1F3FE}", + "\u{1F474}\u{1F3FF}", + "\u{1F475}\u{1F3FB}", + "\u{1F475}\u{1F3FC}", + "\u{1F475}\u{1F3FD}", + "\u{1F475}\u{1F3FE}", + "\u{1F475}\u{1F3FF}", + "\u{1F476}\u{1F3FB}", + "\u{1F476}\u{1F3FC}", + "\u{1F476}\u{1F3FD}", + "\u{1F476}\u{1F3FE}", + "\u{1F476}\u{1F3FF}", + "\u{1F477}\u{1F3FB}", + "\u{1F477}\u{1F3FC}", + "\u{1F477}\u{1F3FD}", + "\u{1F477}\u{1F3FE}", + "\u{1F477}\u{1F3FF}", + "\u{1F478}\u{1F3FB}", + "\u{1F478}\u{1F3FC}", + "\u{1F478}\u{1F3FD}", + "\u{1F478}\u{1F3FE}", + "\u{1F478}\u{1F3FF}", + "\u{1F47C}\u{1F3FB}", + "\u{1F47C}\u{1F3FC}", + "\u{1F47C}\u{1F3FD}", + "\u{1F47C}\u{1F3FE}", + "\u{1F47C}\u{1F3FF}", + "\u{1F481}\u{1F3FB}", + "\u{1F481}\u{1F3FC}", + "\u{1F481}\u{1F3FD}", + "\u{1F481}\u{1F3FE}", + "\u{1F481}\u{1F3FF}", + "\u{1F482}\u{1F3FB}", + "\u{1F482}\u{1F3FC}", + "\u{1F482}\u{1F3FD}", + "\u{1F482}\u{1F3FE}", + "\u{1F482}\u{1F3FF}", + "\u{1F483}\u{1F3FB}", + "\u{1F483}\u{1F3FC}", + "\u{1F483}\u{1F3FD}", + "\u{1F483}\u{1F3FE}", + "\u{1F483}\u{1F3FF}", + "\u{1F485}\u{1F3FB}", + "\u{1F485}\u{1F3FC}", + "\u{1F485}\u{1F3FD}", + "\u{1F485}\u{1F3FE}", + "\u{1F485}\u{1F3FF}", + "\u{1F486}\u{1F3FB}", + "\u{1F486}\u{1F3FC}", + "\u{1F486}\u{1F3FD}", + "\u{1F486}\u{1F3FE}", + "\u{1F486}\u{1F3FF}", + "\u{1F487}\u{1F3FB}", + "\u{1F487}\u{1F3FC}", + "\u{1F487}\u{1F3FD}", + "\u{1F487}\u{1F3FE}", + "\u{1F487}\u{1F3FF}", + "\u{1F48F}\u{1F3FB}", + "\u{1F48F}\u{1F3FC}", + "\u{1F48F}\u{1F3FD}", + "\u{1F48F}\u{1F3FE}", + "\u{1F48F}\u{1F3FF}", + "\u{1F491}\u{1F3FB}", + "\u{1F491}\u{1F3FC}", + "\u{1F491}\u{1F3FD}", + "\u{1F491}\u{1F3FE}", + "\u{1F491}\u{1F3FF}", + "\u{1F4AA}\u{1F3FB}", + "\u{1F4AA}\u{1F3FC}", + "\u{1F4AA}\u{1F3FD}", + "\u{1F4AA}\u{1F3FE}", + "\u{1F4AA}\u{1F3FF}", + "\u{1F574}\u{1F3FB}", + "\u{1F574}\u{1F3FC}", + "\u{1F574}\u{1F3FD}", + "\u{1F574}\u{1F3FE}", + "\u{1F574}\u{1F3FF}", + "\u{1F575}\u{1F3FB}", + "\u{1F575}\u{1F3FC}", + "\u{1F575}\u{1F3FD}", + "\u{1F575}\u{1F3FE}", + "\u{1F575}\u{1F3FF}", + "\u{1F57A}\u{1F3FB}", + "\u{1F57A}\u{1F3FC}", + "\u{1F57A}\u{1F3FD}", + "\u{1F57A}\u{1F3FE}", + "\u{1F57A}\u{1F3FF}", + "\u{1F590}\u{1F3FB}", + "\u{1F590}\u{1F3FC}", + "\u{1F590}\u{1F3FD}", + "\u{1F590}\u{1F3FE}", + "\u{1F590}\u{1F3FF}", + "\u{1F595}\u{1F3FB}", + "\u{1F595}\u{1F3FC}", + "\u{1F595}\u{1F3FD}", + "\u{1F595}\u{1F3FE}", + "\u{1F595}\u{1F3FF}", + "\u{1F596}\u{1F3FB}", + "\u{1F596}\u{1F3FC}", + "\u{1F596}\u{1F3FD}", + "\u{1F596}\u{1F3FE}", + "\u{1F596}\u{1F3FF}", + "\u{1F645}\u{1F3FB}", + "\u{1F645}\u{1F3FC}", + "\u{1F645}\u{1F3FD}", + "\u{1F645}\u{1F3FE}", + "\u{1F645}\u{1F3FF}", + "\u{1F646}\u{1F3FB}", + "\u{1F646}\u{1F3FC}", + "\u{1F646}\u{1F3FD}", + "\u{1F646}\u{1F3FE}", + "\u{1F646}\u{1F3FF}", + "\u{1F647}\u{1F3FB}", + "\u{1F647}\u{1F3FC}", + "\u{1F647}\u{1F3FD}", + "\u{1F647}\u{1F3FE}", + "\u{1F647}\u{1F3FF}", + "\u{1F64B}\u{1F3FB}", + "\u{1F64B}\u{1F3FC}", + "\u{1F64B}\u{1F3FD}", + "\u{1F64B}\u{1F3FE}", + "\u{1F64B}\u{1F3FF}", + "\u{1F64C}\u{1F3FB}", + "\u{1F64C}\u{1F3FC}", + "\u{1F64C}\u{1F3FD}", + "\u{1F64C}\u{1F3FE}", + "\u{1F64C}\u{1F3FF}", + "\u{1F64D}\u{1F3FB}", + "\u{1F64D}\u{1F3FC}", + "\u{1F64D}\u{1F3FD}", + "\u{1F64D}\u{1F3FE}", + "\u{1F64D}\u{1F3FF}", + "\u{1F64E}\u{1F3FB}", + "\u{1F64E}\u{1F3FC}", + "\u{1F64E}\u{1F3FD}", + "\u{1F64E}\u{1F3FE}", + "\u{1F64E}\u{1F3FF}", + "\u{1F64F}\u{1F3FB}", + "\u{1F64F}\u{1F3FC}", + "\u{1F64F}\u{1F3FD}", + "\u{1F64F}\u{1F3FE}", + "\u{1F64F}\u{1F3FF}", + "\u{1F6A3}\u{1F3FB}", + "\u{1F6A3}\u{1F3FC}", + "\u{1F6A3}\u{1F3FD}", + "\u{1F6A3}\u{1F3FE}", + "\u{1F6A3}\u{1F3FF}", + "\u{1F6B4}\u{1F3FB}", + "\u{1F6B4}\u{1F3FC}", + "\u{1F6B4}\u{1F3FD}", + "\u{1F6B4}\u{1F3FE}", + "\u{1F6B4}\u{1F3FF}", + "\u{1F6B5}\u{1F3FB}", + "\u{1F6B5}\u{1F3FC}", + "\u{1F6B5}\u{1F3FD}", + "\u{1F6B5}\u{1F3FE}", + "\u{1F6B5}\u{1F3FF}", + "\u{1F6B6}\u{1F3FB}", + "\u{1F6B6}\u{1F3FC}", + "\u{1F6B6}\u{1F3FD}", + "\u{1F6B6}\u{1F3FE}", + "\u{1F6B6}\u{1F3FF}", + "\u{1F6C0}\u{1F3FB}", + "\u{1F6C0}\u{1F3FC}", + "\u{1F6C0}\u{1F3FD}", + "\u{1F6C0}\u{1F3FE}", + "\u{1F6C0}\u{1F3FF}", + "\u{1F6CC}\u{1F3FB}", + "\u{1F6CC}\u{1F3FC}", + "\u{1F6CC}\u{1F3FD}", + "\u{1F6CC}\u{1F3FE}", + "\u{1F6CC}\u{1F3FF}", + "\u{1F90C}\u{1F3FB}", + "\u{1F90C}\u{1F3FC}", + "\u{1F90C}\u{1F3FD}", + "\u{1F90C}\u{1F3FE}", + "\u{1F90C}\u{1F3FF}", + "\u{1F90F}\u{1F3FB}", + "\u{1F90F}\u{1F3FC}", + "\u{1F90F}\u{1F3FD}", + "\u{1F90F}\u{1F3FE}", + "\u{1F90F}\u{1F3FF}", + "\u{1F918}\u{1F3FB}", + "\u{1F918}\u{1F3FC}", + "\u{1F918}\u{1F3FD}", + "\u{1F918}\u{1F3FE}", + "\u{1F918}\u{1F3FF}", + "\u{1F919}\u{1F3FB}", + "\u{1F919}\u{1F3FC}", + "\u{1F919}\u{1F3FD}", + "\u{1F919}\u{1F3FE}", + "\u{1F919}\u{1F3FF}", + "\u{1F91A}\u{1F3FB}", + "\u{1F91A}\u{1F3FC}", + "\u{1F91A}\u{1F3FD}", + "\u{1F91A}\u{1F3FE}", + "\u{1F91A}\u{1F3FF}", + "\u{1F91B}\u{1F3FB}", + "\u{1F91B}\u{1F3FC}", + "\u{1F91B}\u{1F3FD}", + "\u{1F91B}\u{1F3FE}", + "\u{1F91B}\u{1F3FF}", + "\u{1F91C}\u{1F3FB}", + "\u{1F91C}\u{1F3FC}", + "\u{1F91C}\u{1F3FD}", + "\u{1F91C}\u{1F3FE}", + "\u{1F91C}\u{1F3FF}", + "\u{1F91D}\u{1F3FB}", + "\u{1F91D}\u{1F3FC}", + "\u{1F91D}\u{1F3FD}", + "\u{1F91D}\u{1F3FE}", + "\u{1F91D}\u{1F3FF}", + "\u{1F91E}\u{1F3FB}", + "\u{1F91E}\u{1F3FC}", + "\u{1F91E}\u{1F3FD}", + "\u{1F91E}\u{1F3FE}", + "\u{1F91E}\u{1F3FF}", + "\u{1F91F}\u{1F3FB}", + "\u{1F91F}\u{1F3FC}", + "\u{1F91F}\u{1F3FD}", + "\u{1F91F}\u{1F3FE}", + "\u{1F91F}\u{1F3FF}", + "\u{1F926}\u{1F3FB}", + "\u{1F926}\u{1F3FC}", + "\u{1F926}\u{1F3FD}", + "\u{1F926}\u{1F3FE}", + "\u{1F926}\u{1F3FF}", + "\u{1F930}\u{1F3FB}", + "\u{1F930}\u{1F3FC}", + "\u{1F930}\u{1F3FD}", + "\u{1F930}\u{1F3FE}", + "\u{1F930}\u{1F3FF}", + "\u{1F931}\u{1F3FB}", + "\u{1F931}\u{1F3FC}", + "\u{1F931}\u{1F3FD}", + "\u{1F931}\u{1F3FE}", + "\u{1F931}\u{1F3FF}", + "\u{1F932}\u{1F3FB}", + "\u{1F932}\u{1F3FC}", + "\u{1F932}\u{1F3FD}", + "\u{1F932}\u{1F3FE}", + "\u{1F932}\u{1F3FF}", + "\u{1F933}\u{1F3FB}", + "\u{1F933}\u{1F3FC}", + "\u{1F933}\u{1F3FD}", + "\u{1F933}\u{1F3FE}", + "\u{1F933}\u{1F3FF}", + "\u{1F934}\u{1F3FB}", + "\u{1F934}\u{1F3FC}", + "\u{1F934}\u{1F3FD}", + "\u{1F934}\u{1F3FE}", + "\u{1F934}\u{1F3FF}", + "\u{1F935}\u{1F3FB}", + "\u{1F935}\u{1F3FC}", + "\u{1F935}\u{1F3FD}", + "\u{1F935}\u{1F3FE}", + "\u{1F935}\u{1F3FF}", + "\u{1F936}\u{1F3FB}", + "\u{1F936}\u{1F3FC}", + "\u{1F936}\u{1F3FD}", + "\u{1F936}\u{1F3FE}", + "\u{1F936}\u{1F3FF}", + "\u{1F937}\u{1F3FB}", + "\u{1F937}\u{1F3FC}", + "\u{1F937}\u{1F3FD}", + "\u{1F937}\u{1F3FE}", + "\u{1F937}\u{1F3FF}", + "\u{1F938}\u{1F3FB}", + "\u{1F938}\u{1F3FC}", + "\u{1F938}\u{1F3FD}", + "\u{1F938}\u{1F3FE}", + "\u{1F938}\u{1F3FF}", + "\u{1F939}\u{1F3FB}", + "\u{1F939}\u{1F3FC}", + "\u{1F939}\u{1F3FD}", + "\u{1F939}\u{1F3FE}", + "\u{1F939}\u{1F3FF}", + "\u{1F93C}\u{1F3FB}", + "\u{1F93C}\u{1F3FC}", + "\u{1F93C}\u{1F3FD}", + "\u{1F93C}\u{1F3FE}", + "\u{1F93C}\u{1F3FF}", + "\u{1F93D}\u{1F3FB}", + "\u{1F93D}\u{1F3FC}", + "\u{1F93D}\u{1F3FD}", + "\u{1F93D}\u{1F3FE}", + "\u{1F93D}\u{1F3FF}", + "\u{1F93E}\u{1F3FB}", + "\u{1F93E}\u{1F3FC}", + "\u{1F93E}\u{1F3FD}", + "\u{1F93E}\u{1F3FE}", + "\u{1F93E}\u{1F3FF}", + "\u{1F977}\u{1F3FB}", + "\u{1F977}\u{1F3FC}", + "\u{1F977}\u{1F3FD}", + "\u{1F977}\u{1F3FE}", + "\u{1F977}\u{1F3FF}", + "\u{1F9B5}\u{1F3FB}", + "\u{1F9B5}\u{1F3FC}", + "\u{1F9B5}\u{1F3FD}", + "\u{1F9B5}\u{1F3FE}", + "\u{1F9B5}\u{1F3FF}", + "\u{1F9B6}\u{1F3FB}", + "\u{1F9B6}\u{1F3FC}", + "\u{1F9B6}\u{1F3FD}", + "\u{1F9B6}\u{1F3FE}", + "\u{1F9B6}\u{1F3FF}", + "\u{1F9B8}\u{1F3FB}", + "\u{1F9B8}\u{1F3FC}", + "\u{1F9B8}\u{1F3FD}", + "\u{1F9B8}\u{1F3FE}", + "\u{1F9B8}\u{1F3FF}", + "\u{1F9B9}\u{1F3FB}", + "\u{1F9B9}\u{1F3FC}", + "\u{1F9B9}\u{1F3FD}", + "\u{1F9B9}\u{1F3FE}", + "\u{1F9B9}\u{1F3FF}", + "\u{1F9BB}\u{1F3FB}", + "\u{1F9BB}\u{1F3FC}", + "\u{1F9BB}\u{1F3FD}", + "\u{1F9BB}\u{1F3FE}", + "\u{1F9BB}\u{1F3FF}", + "\u{1F9CD}\u{1F3FB}", + "\u{1F9CD}\u{1F3FC}", + "\u{1F9CD}\u{1F3FD}", + "\u{1F9CD}\u{1F3FE}", + "\u{1F9CD}\u{1F3FF}", + "\u{1F9CE}\u{1F3FB}", + "\u{1F9CE}\u{1F3FC}", + "\u{1F9CE}\u{1F3FD}", + "\u{1F9CE}\u{1F3FE}", + "\u{1F9CE}\u{1F3FF}", + "\u{1F9CF}\u{1F3FB}", + "\u{1F9CF}\u{1F3FC}", + "\u{1F9CF}\u{1F3FD}", + "\u{1F9CF}\u{1F3FE}", + "\u{1F9CF}\u{1F3FF}", + "\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FF}", + "\u{1F9D2}\u{1F3FB}", + "\u{1F9D2}\u{1F3FC}", + "\u{1F9D2}\u{1F3FD}", + "\u{1F9D2}\u{1F3FE}", + "\u{1F9D2}\u{1F3FF}", + "\u{1F9D3}\u{1F3FB}", + "\u{1F9D3}\u{1F3FC}", + "\u{1F9D3}\u{1F3FD}", + "\u{1F9D3}\u{1F3FE}", + "\u{1F9D3}\u{1F3FF}", + "\u{1F9D4}\u{1F3FB}", + "\u{1F9D4}\u{1F3FC}", + "\u{1F9D4}\u{1F3FD}", + "\u{1F9D4}\u{1F3FE}", + "\u{1F9D4}\u{1F3FF}", + "\u{1F9D5}\u{1F3FB}", + "\u{1F9D5}\u{1F3FC}", + "\u{1F9D5}\u{1F3FD}", + "\u{1F9D5}\u{1F3FE}", + "\u{1F9D5}\u{1F3FF}", + "\u{1F9D6}\u{1F3FB}", + "\u{1F9D6}\u{1F3FC}", + "\u{1F9D6}\u{1F3FD}", + "\u{1F9D6}\u{1F3FE}", + "\u{1F9D6}\u{1F3FF}", + "\u{1F9D7}\u{1F3FB}", + "\u{1F9D7}\u{1F3FC}", + "\u{1F9D7}\u{1F3FD}", + "\u{1F9D7}\u{1F3FE}", + "\u{1F9D7}\u{1F3FF}", + "\u{1F9D8}\u{1F3FB}", + "\u{1F9D8}\u{1F3FC}", + "\u{1F9D8}\u{1F3FD}", + "\u{1F9D8}\u{1F3FE}", + "\u{1F9D8}\u{1F3FF}", + "\u{1F9D9}\u{1F3FB}", + "\u{1F9D9}\u{1F3FC}", + "\u{1F9D9}\u{1F3FD}", + "\u{1F9D9}\u{1F3FE}", + "\u{1F9D9}\u{1F3FF}", + "\u{1F9DA}\u{1F3FB}", + "\u{1F9DA}\u{1F3FC}", + "\u{1F9DA}\u{1F3FD}", + "\u{1F9DA}\u{1F3FE}", + "\u{1F9DA}\u{1F3FF}", + "\u{1F9DB}\u{1F3FB}", + "\u{1F9DB}\u{1F3FC}", + "\u{1F9DB}\u{1F3FD}", + "\u{1F9DB}\u{1F3FE}", + "\u{1F9DB}\u{1F3FF}", + "\u{1F9DC}\u{1F3FB}", + "\u{1F9DC}\u{1F3FC}", + "\u{1F9DC}\u{1F3FD}", + "\u{1F9DC}\u{1F3FE}", + "\u{1F9DC}\u{1F3FF}", + "\u{1F9DD}\u{1F3FB}", + "\u{1F9DD}\u{1F3FC}", + "\u{1F9DD}\u{1F3FD}", + "\u{1F9DD}\u{1F3FE}", + "\u{1F9DD}\u{1F3FF}", + "\u{1FAC3}\u{1F3FB}", + "\u{1FAC3}\u{1F3FC}", + "\u{1FAC3}\u{1F3FD}", + "\u{1FAC3}\u{1F3FE}", + "\u{1FAC3}\u{1F3FF}", + "\u{1FAC4}\u{1F3FB}", + "\u{1FAC4}\u{1F3FC}", + "\u{1FAC4}\u{1F3FD}", + "\u{1FAC4}\u{1F3FE}", + "\u{1FAC4}\u{1F3FF}", + "\u{1FAC5}\u{1F3FB}", + "\u{1FAC5}\u{1F3FC}", + "\u{1FAC5}\u{1F3FD}", + "\u{1FAC5}\u{1F3FE}", + "\u{1FAC5}\u{1F3FF}", + "\u{1FAF0}\u{1F3FB}", + "\u{1FAF0}\u{1F3FC}", + "\u{1FAF0}\u{1F3FD}", + "\u{1FAF0}\u{1F3FE}", + "\u{1FAF0}\u{1F3FF}", + "\u{1FAF1}\u{1F3FB}", + "\u{1FAF1}\u{1F3FC}", + "\u{1FAF1}\u{1F3FD}", + "\u{1FAF1}\u{1F3FE}", + "\u{1FAF1}\u{1F3FF}", + "\u{1FAF2}\u{1F3FB}", + "\u{1FAF2}\u{1F3FC}", + "\u{1FAF2}\u{1F3FD}", + "\u{1FAF2}\u{1F3FE}", + "\u{1FAF2}\u{1F3FF}", + "\u{1FAF3}\u{1F3FB}", + "\u{1FAF3}\u{1F3FC}", + "\u{1FAF3}\u{1F3FD}", + "\u{1FAF3}\u{1F3FE}", + "\u{1FAF3}\u{1F3FF}", + "\u{1FAF4}\u{1F3FB}", + "\u{1FAF4}\u{1F3FC}", + "\u{1FAF4}\u{1F3FD}", + "\u{1FAF4}\u{1F3FE}", + "\u{1FAF4}\u{1F3FF}", + "\u{1FAF5}\u{1F3FB}", + "\u{1FAF5}\u{1F3FC}", + "\u{1FAF5}\u{1F3FD}", + "\u{1FAF5}\u{1F3FE}", + "\u{1FAF5}\u{1F3FF}", + "\u{1FAF6}\u{1F3FB}", + "\u{1FAF6}\u{1F3FC}", + "\u{1FAF6}\u{1F3FD}", + "\u{1FAF6}\u{1F3FE}", + "\u{1FAF6}\u{1F3FF}", + "\u{1FAF7}\u{1F3FB}", + "\u{1FAF7}\u{1F3FC}", + "\u{1FAF7}\u{1F3FD}", + "\u{1FAF7}\u{1F3FE}", + "\u{1FAF7}\u{1F3FF}", + "\u{1FAF8}\u{1F3FB}", + "\u{1FAF8}\u{1F3FC}", + "\u{1FAF8}\u{1F3FD}", + "\u{1FAF8}\u{1F3FE}", + "\u{1FAF8}\u{1F3FF}", + "\u{1FAF9}\u{1F3FB}", + "\u{1FAF9}\u{1F3FC}", + "\u{1FAF9}\u{1F3FD}", + "\u{1FAF9}\u{1F3FE}", + "\u{1FAF9}\u{1F3FF}", + "\u{1FAFA}\u{1F3FB}", + "\u{1FAFA}\u{1F3FC}", + "\u{1FAFA}\u{1F3FD}", + "\u{1FAFA}\u{1F3FE}", + "\u{1FAFA}\u{1F3FF}", +]; + +/// The one-character members of RGI_Emoji_Tag_Sequence, as ranges. +pub static STRING_CHARS_RGI_EMOJI_TAG_SEQUENCE: &[(u32, u32)] = &[]; + +/// The members of RGI_Emoji_Tag_Sequence of two or more code points, in the order of the file. +pub static STRINGS_RGI_EMOJI_TAG_SEQUENCE: &[&str] = &[ + "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}", + "\u{1F3F4}\u{E0067}\u{E0062}\u{E0073}\u{E0063}\u{E0074}\u{E007F}", + "\u{1F3F4}\u{E0067}\u{E0062}\u{E0077}\u{E006C}\u{E0073}\u{E007F}", +]; + +/// The one-character members of RGI_Emoji_ZWJ_Sequence, as ranges. +pub static STRING_CHARS_RGI_EMOJI_ZWJ_SEQUENCE: &[(u32, u32)] = &[]; + +/// The members of RGI_Emoji_ZWJ_Sequence of two or more code points, in the order of the file. +pub static STRINGS_RGI_EMOJI_ZWJ_SEQUENCE: &[&str] = &[ + "\u{1F468}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}", + "\u{1F468}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}", + "\u{1F468}\u{200D}\u{1F466}", + "\u{1F468}\u{200D}\u{1F466}\u{200D}\u{1F466}", + "\u{1F468}\u{200D}\u{1F467}", + "\u{1F468}\u{200D}\u{1F467}\u{200D}\u{1F466}", + "\u{1F468}\u{200D}\u{1F467}\u{200D}\u{1F467}", + "\u{1F468}\u{200D}\u{1F468}\u{200D}\u{1F466}", + "\u{1F468}\u{200D}\u{1F468}\u{200D}\u{1F466}\u{200D}\u{1F466}", + "\u{1F468}\u{200D}\u{1F468}\u{200D}\u{1F467}", + "\u{1F468}\u{200D}\u{1F468}\u{200D}\u{1F467}\u{200D}\u{1F466}", + "\u{1F468}\u{200D}\u{1F468}\u{200D}\u{1F467}\u{200D}\u{1F467}", + "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F466}", + "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F466}\u{200D}\u{1F466}", + "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}", + "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F466}", + "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F467}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}", + "\u{1F469}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}", + "\u{1F469}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}", + "\u{1F469}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}", + "\u{1F469}\u{200D}\u{1F466}", + "\u{1F469}\u{200D}\u{1F466}\u{200D}\u{1F466}", + "\u{1F469}\u{200D}\u{1F467}", + "\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F466}", + "\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F467}", + "\u{1F469}\u{200D}\u{1F469}\u{200D}\u{1F466}", + "\u{1F469}\u{200D}\u{1F469}\u{200D}\u{1F466}\u{200D}\u{1F466}", + "\u{1F469}\u{200D}\u{1F469}\u{200D}\u{1F467}", + "\u{1F469}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F466}", + "\u{1F469}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F467}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F468}\u{1F3FF}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}\u{1F3FF}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FB}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FC}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FD}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F468}\u{1F3FE}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FB}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FC}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FD}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F469}\u{1F3FE}", + "\u{1F9D1}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}", + "\u{1F9D1}\u{200D}\u{1F9D1}\u{200D}\u{1F9D2}", + "\u{1F9D1}\u{200D}\u{1F9D1}\u{200D}\u{1F9D2}\u{200D}\u{1F9D2}", + "\u{1F9D1}\u{200D}\u{1F9D2}", + "\u{1F9D1}\u{200D}\u{1F9D2}\u{200D}\u{1F9D2}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F91D}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1FAF1}\u{1F3FB}\u{200D}\u{1FAF2}\u{1F3FC}", + "\u{1FAF1}\u{1F3FB}\u{200D}\u{1FAF2}\u{1F3FD}", + "\u{1FAF1}\u{1F3FB}\u{200D}\u{1FAF2}\u{1F3FE}", + "\u{1FAF1}\u{1F3FB}\u{200D}\u{1FAF2}\u{1F3FF}", + "\u{1FAF1}\u{1F3FC}\u{200D}\u{1FAF2}\u{1F3FB}", + "\u{1FAF1}\u{1F3FC}\u{200D}\u{1FAF2}\u{1F3FD}", + "\u{1FAF1}\u{1F3FC}\u{200D}\u{1FAF2}\u{1F3FE}", + "\u{1FAF1}\u{1F3FC}\u{200D}\u{1FAF2}\u{1F3FF}", + "\u{1FAF1}\u{1F3FD}\u{200D}\u{1FAF2}\u{1F3FB}", + "\u{1FAF1}\u{1F3FD}\u{200D}\u{1FAF2}\u{1F3FC}", + "\u{1FAF1}\u{1F3FD}\u{200D}\u{1FAF2}\u{1F3FE}", + "\u{1FAF1}\u{1F3FD}\u{200D}\u{1FAF2}\u{1F3FF}", + "\u{1FAF1}\u{1F3FE}\u{200D}\u{1FAF2}\u{1F3FB}", + "\u{1FAF1}\u{1F3FE}\u{200D}\u{1FAF2}\u{1F3FC}", + "\u{1FAF1}\u{1F3FE}\u{200D}\u{1FAF2}\u{1F3FD}", + "\u{1FAF1}\u{1F3FE}\u{200D}\u{1FAF2}\u{1F3FF}", + "\u{1FAF1}\u{1F3FF}\u{200D}\u{1FAF2}\u{1F3FB}", + "\u{1FAF1}\u{1F3FF}\u{200D}\u{1FAF2}\u{1F3FC}", + "\u{1FAF1}\u{1F3FF}\u{200D}\u{1FAF2}\u{1F3FD}", + "\u{1FAF1}\u{1F3FF}\u{200D}\u{1FAF2}\u{1F3FE}", + "\u{1F3C3}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FB}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FE}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{200D}\u{2695}\u{FE0F}", + "\u{1F468}\u{200D}\u{2696}\u{FE0F}", + "\u{1F468}\u{200D}\u{2708}\u{FE0F}", + "\u{1F468}\u{200D}\u{1F33E}", + "\u{1F468}\u{200D}\u{1F373}", + "\u{1F468}\u{200D}\u{1F37C}", + "\u{1F468}\u{200D}\u{1F393}", + "\u{1F468}\u{200D}\u{1F3A4}", + "\u{1F468}\u{200D}\u{1F3A8}", + "\u{1F468}\u{200D}\u{1F3EB}", + "\u{1F468}\u{200D}\u{1F3ED}", + "\u{1F468}\u{200D}\u{1F4BB}", + "\u{1F468}\u{200D}\u{1F4BC}", + "\u{1F468}\u{200D}\u{1F527}", + "\u{1F468}\u{200D}\u{1F52C}", + "\u{1F468}\u{200D}\u{1F680}", + "\u{1F468}\u{200D}\u{1F692}", + "\u{1F468}\u{200D}\u{1F9AF}", + "\u{1F468}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{200D}\u{1F9BC}", + "\u{1F468}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{200D}\u{1F9BD}", + "\u{1F468}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2695}\u{FE0F}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2696}\u{FE0F}", + "\u{1F468}\u{1F3FB}\u{200D}\u{2708}\u{FE0F}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F33E}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F373}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F37C}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F393}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F3A4}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F3A8}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F3EB}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F3ED}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F4BB}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F4BC}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F527}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F52C}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F680}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F692}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F9AF}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F9BC}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F9BD}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2695}\u{FE0F}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2696}\u{FE0F}", + "\u{1F468}\u{1F3FC}\u{200D}\u{2708}\u{FE0F}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F33E}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F373}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F37C}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F393}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F3A4}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F3A8}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F3EB}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F3ED}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F4BB}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F4BC}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F527}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F52C}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F680}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F692}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F9AF}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F9BC}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F9BD}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2695}\u{FE0F}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2696}\u{FE0F}", + "\u{1F468}\u{1F3FD}\u{200D}\u{2708}\u{FE0F}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F33E}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F373}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F37C}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F393}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F3A4}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F3A8}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F3EB}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F3ED}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F4BB}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F4BC}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F527}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F52C}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F680}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F692}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F9AF}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F9BC}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F9BD}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2695}\u{FE0F}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2696}\u{FE0F}", + "\u{1F468}\u{1F3FE}\u{200D}\u{2708}\u{FE0F}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F33E}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F373}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F37C}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F393}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F3A4}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F3A8}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F3EB}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F3ED}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F4BB}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F4BC}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F527}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F52C}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F680}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F692}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F9AF}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F9BC}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F9BD}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2695}\u{FE0F}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2696}\u{FE0F}", + "\u{1F468}\u{1F3FF}\u{200D}\u{2708}\u{FE0F}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F33E}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F373}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F37C}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F393}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F3A4}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F3A8}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F3EB}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F3ED}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F4BB}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F4BC}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F527}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F52C}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F680}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F692}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F9AF}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F9BC}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F9BD}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{200D}\u{2695}\u{FE0F}", + "\u{1F469}\u{200D}\u{2696}\u{FE0F}", + "\u{1F469}\u{200D}\u{2708}\u{FE0F}", + "\u{1F469}\u{200D}\u{1F33E}", + "\u{1F469}\u{200D}\u{1F373}", + "\u{1F469}\u{200D}\u{1F37C}", + "\u{1F469}\u{200D}\u{1F393}", + "\u{1F469}\u{200D}\u{1F3A4}", + "\u{1F469}\u{200D}\u{1F3A8}", + "\u{1F469}\u{200D}\u{1F3EB}", + "\u{1F469}\u{200D}\u{1F3ED}", + "\u{1F469}\u{200D}\u{1F4BB}", + "\u{1F469}\u{200D}\u{1F4BC}", + "\u{1F469}\u{200D}\u{1F527}", + "\u{1F469}\u{200D}\u{1F52C}", + "\u{1F469}\u{200D}\u{1F680}", + "\u{1F469}\u{200D}\u{1F692}", + "\u{1F469}\u{200D}\u{1F9AF}", + "\u{1F469}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{200D}\u{1F9BC}", + "\u{1F469}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{200D}\u{1F9BD}", + "\u{1F469}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2695}\u{FE0F}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2696}\u{FE0F}", + "\u{1F469}\u{1F3FB}\u{200D}\u{2708}\u{FE0F}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F33E}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F373}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F37C}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F393}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F3A4}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F3A8}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F3EB}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F3ED}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F4BB}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F4BC}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F527}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F52C}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F680}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F692}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F9AF}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F9BC}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F9BD}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2695}\u{FE0F}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2696}\u{FE0F}", + "\u{1F469}\u{1F3FC}\u{200D}\u{2708}\u{FE0F}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F33E}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F373}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F37C}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F393}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F3A4}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F3A8}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F3EB}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F3ED}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F4BB}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F4BC}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F527}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F52C}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F680}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F692}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F9AF}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F9BC}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F9BD}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2695}\u{FE0F}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2696}\u{FE0F}", + "\u{1F469}\u{1F3FD}\u{200D}\u{2708}\u{FE0F}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F33E}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F373}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F37C}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F393}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F3A4}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F3A8}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F3EB}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F3ED}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F4BB}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F4BC}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F527}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F52C}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F680}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F692}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F9AF}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F9BC}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F9BD}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2695}\u{FE0F}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2696}\u{FE0F}", + "\u{1F469}\u{1F3FE}\u{200D}\u{2708}\u{FE0F}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F33E}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F373}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F37C}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F393}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F3A4}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F3A8}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F3EB}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F3ED}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F4BB}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F4BC}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F527}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F52C}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F680}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F692}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F9AF}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F9BC}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F9BD}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2695}\u{FE0F}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2696}\u{FE0F}", + "\u{1F469}\u{1F3FF}\u{200D}\u{2708}\u{FE0F}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F33E}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F373}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F37C}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F393}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F3A4}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F3A8}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F3EB}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F3ED}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F4BB}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F4BC}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F527}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F52C}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F680}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F692}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F9AF}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F9BC}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F9BD}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FB}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FE}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FB}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FE}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{200D}\u{2695}\u{FE0F}", + "\u{1F9D1}\u{200D}\u{2696}\u{FE0F}", + "\u{1F9D1}\u{200D}\u{2708}\u{FE0F}", + "\u{1F9D1}\u{200D}\u{1F33E}", + "\u{1F9D1}\u{200D}\u{1F373}", + "\u{1F9D1}\u{200D}\u{1F37C}", + "\u{1F9D1}\u{200D}\u{1F384}", + "\u{1F9D1}\u{200D}\u{1F393}", + "\u{1F9D1}\u{200D}\u{1F3A4}", + "\u{1F9D1}\u{200D}\u{1F3A8}", + "\u{1F9D1}\u{200D}\u{1F3EB}", + "\u{1F9D1}\u{200D}\u{1F3ED}", + "\u{1F9D1}\u{200D}\u{1F4BB}", + "\u{1F9D1}\u{200D}\u{1F4BC}", + "\u{1F9D1}\u{200D}\u{1F527}", + "\u{1F9D1}\u{200D}\u{1F52C}", + "\u{1F9D1}\u{200D}\u{1F680}", + "\u{1F9D1}\u{200D}\u{1F692}", + "\u{1F9D1}\u{200D}\u{1F9AF}", + "\u{1F9D1}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{200D}\u{1F9BC}", + "\u{1F9D1}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{200D}\u{1F9BD}", + "\u{1F9D1}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2695}\u{FE0F}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2696}\u{FE0F}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{2708}\u{FE0F}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F33E}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F373}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F37C}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F384}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F393}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F3A4}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F3A8}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F3EB}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F3ED}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F4BB}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F4BC}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F527}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F52C}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F680}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F692}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F9AF}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F9BC}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F9BD}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2695}\u{FE0F}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2696}\u{FE0F}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{2708}\u{FE0F}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F33E}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F373}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F37C}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F384}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F393}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F3A4}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F3A8}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F3EB}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F3ED}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F4BB}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F4BC}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F527}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F52C}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F680}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F692}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F9AF}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F9BC}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F9BD}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2695}\u{FE0F}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2696}\u{FE0F}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{2708}\u{FE0F}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F33E}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F373}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F37C}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F384}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F393}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F3A4}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F3A8}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F3EB}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F3ED}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F4BB}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F4BC}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F527}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F52C}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F680}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F692}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F9AF}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F9BC}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F9BD}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2695}\u{FE0F}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2696}\u{FE0F}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{2708}\u{FE0F}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F33E}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F373}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F37C}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F384}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F393}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F3A4}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F3A8}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F3EB}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F3ED}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F4BB}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F4BC}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F527}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F52C}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F680}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F692}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F9AF}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F9BC}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F9BD}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2695}\u{FE0F}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2696}\u{FE0F}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{2708}\u{FE0F}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F33E}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F373}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F37C}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F384}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F393}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F3A4}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F3A8}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F3EB}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F3ED}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F4BB}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F4BC}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F527}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F52C}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F680}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F692}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F9AF}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F9AF}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F9BC}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F9BC}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F9BD}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F9BD}\u{200D}\u{27A1}\u{FE0F}", + "\u{26F9}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{26F9}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{26F9}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{26F9}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{26F9}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{26F9}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{26F9}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{26F9}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{26F9}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{26F9}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{26F9}\u{FE0F}\u{200D}\u{2640}\u{FE0F}", + "\u{26F9}\u{FE0F}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C3}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C3}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C3}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C3}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C3}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C3}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C3}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C3}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C3}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C3}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C3}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C3}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C3}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C3}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F3C4}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C4}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C4}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C4}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C4}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C4}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C4}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C4}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C4}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C4}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3C4}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3C4}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CA}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CA}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CA}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CA}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CA}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CA}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CA}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CA}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CA}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CA}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CA}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CA}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CB}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CB}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CB}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CB}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CB}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CB}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CB}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CB}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CB}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CB}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CB}\u{FE0F}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CB}\u{FE0F}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CC}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CC}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CC}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CC}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CC}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CC}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CC}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CC}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CC}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CC}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F3CC}\u{FE0F}\u{200D}\u{2640}\u{FE0F}", + "\u{1F3CC}\u{FE0F}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46E}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46E}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46E}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46E}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46E}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46E}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46E}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46E}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46E}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46E}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46E}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46E}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46F}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46F}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46F}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46F}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46F}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46F}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46F}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46F}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46F}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46F}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F46F}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F46F}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F470}\u{200D}\u{2640}\u{FE0F}", + "\u{1F470}\u{200D}\u{2642}\u{FE0F}", + "\u{1F470}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F470}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F470}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F470}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F470}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F470}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F470}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F470}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F470}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F470}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F471}\u{200D}\u{2640}\u{FE0F}", + "\u{1F471}\u{200D}\u{2642}\u{FE0F}", + "\u{1F471}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F471}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F471}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F471}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F471}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F471}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F471}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F471}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F471}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F471}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F473}\u{200D}\u{2640}\u{FE0F}", + "\u{1F473}\u{200D}\u{2642}\u{FE0F}", + "\u{1F473}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F473}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F473}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F473}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F473}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F473}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F473}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F473}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F473}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F473}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F477}\u{200D}\u{2640}\u{FE0F}", + "\u{1F477}\u{200D}\u{2642}\u{FE0F}", + "\u{1F477}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F477}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F477}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F477}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F477}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F477}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F477}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F477}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F477}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F477}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F481}\u{200D}\u{2640}\u{FE0F}", + "\u{1F481}\u{200D}\u{2642}\u{FE0F}", + "\u{1F481}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F481}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F481}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F481}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F481}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F481}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F481}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F481}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F481}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F481}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F482}\u{200D}\u{2640}\u{FE0F}", + "\u{1F482}\u{200D}\u{2642}\u{FE0F}", + "\u{1F482}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F482}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F482}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F482}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F482}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F482}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F482}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F482}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F482}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F482}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F486}\u{200D}\u{2640}\u{FE0F}", + "\u{1F486}\u{200D}\u{2642}\u{FE0F}", + "\u{1F486}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F486}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F486}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F486}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F486}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F486}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F486}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F486}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F486}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F486}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F487}\u{200D}\u{2640}\u{FE0F}", + "\u{1F487}\u{200D}\u{2642}\u{FE0F}", + "\u{1F487}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F487}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F487}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F487}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F487}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F487}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F487}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F487}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F487}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F487}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F575}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F575}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F575}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F575}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F575}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F575}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F575}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F575}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F575}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F575}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F575}\u{FE0F}\u{200D}\u{2640}\u{FE0F}", + "\u{1F575}\u{FE0F}\u{200D}\u{2642}\u{FE0F}", + "\u{1F645}\u{200D}\u{2640}\u{FE0F}", + "\u{1F645}\u{200D}\u{2642}\u{FE0F}", + "\u{1F645}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F645}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F645}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F645}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F645}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F645}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F645}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F645}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F645}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F645}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F646}\u{200D}\u{2640}\u{FE0F}", + "\u{1F646}\u{200D}\u{2642}\u{FE0F}", + "\u{1F646}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F646}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F646}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F646}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F646}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F646}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F646}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F646}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F646}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F646}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F647}\u{200D}\u{2640}\u{FE0F}", + "\u{1F647}\u{200D}\u{2642}\u{FE0F}", + "\u{1F647}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F647}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F647}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F647}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F647}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F647}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F647}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F647}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F647}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F647}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64B}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64B}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64B}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64B}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64B}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64B}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64B}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64B}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64B}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64B}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64B}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64B}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64D}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64D}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64D}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64D}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64D}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64D}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64D}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64D}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64D}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64D}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64D}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64D}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64E}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64E}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64E}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64E}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64E}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64E}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64E}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64E}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64E}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64E}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F64E}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F64E}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6A3}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6A3}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6A3}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6A3}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6A3}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6A3}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6A3}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6A3}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6A3}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6A3}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6A3}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6A3}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B4}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B4}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B4}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B4}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B4}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B4}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B4}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B4}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B4}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B4}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B4}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B4}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B5}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B5}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B5}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B5}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B5}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B5}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B5}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B5}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B5}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B5}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B5}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B5}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B6}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B6}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B6}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B6}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B6}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B6}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B6}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B6}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B6}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B6}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B6}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F6B6}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F6B6}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F6B6}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F926}\u{200D}\u{2640}\u{FE0F}", + "\u{1F926}\u{200D}\u{2642}\u{FE0F}", + "\u{1F926}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F926}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F926}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F926}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F926}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F926}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F926}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F926}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F926}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F926}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F935}\u{200D}\u{2640}\u{FE0F}", + "\u{1F935}\u{200D}\u{2642}\u{FE0F}", + "\u{1F935}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F935}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F935}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F935}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F935}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F935}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F935}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F935}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F935}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F935}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F937}\u{200D}\u{2640}\u{FE0F}", + "\u{1F937}\u{200D}\u{2642}\u{FE0F}", + "\u{1F937}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F937}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F937}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F937}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F937}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F937}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F937}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F937}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F937}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F937}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F938}\u{200D}\u{2640}\u{FE0F}", + "\u{1F938}\u{200D}\u{2642}\u{FE0F}", + "\u{1F938}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F938}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F938}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F938}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F938}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F938}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F938}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F938}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F938}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F938}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F939}\u{200D}\u{2640}\u{FE0F}", + "\u{1F939}\u{200D}\u{2642}\u{FE0F}", + "\u{1F939}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F939}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F939}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F939}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F939}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F939}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F939}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F939}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F939}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F939}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93C}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93C}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93C}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93C}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93C}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93C}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93C}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93C}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93C}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93C}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93C}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93C}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93D}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93D}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93D}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93D}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93D}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93D}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93D}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93D}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93D}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93D}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93D}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93D}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93E}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93E}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93E}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93E}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93E}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93E}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93E}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93E}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93E}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93E}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F93E}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F93E}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B8}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B8}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B8}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B8}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B8}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B8}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B8}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B8}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B8}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B8}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B8}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B8}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B9}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B9}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B9}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B9}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B9}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B9}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B9}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B9}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B9}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B9}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9B9}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9B9}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CD}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CD}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CD}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CD}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CD}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CD}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CD}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CD}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CD}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CD}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CE}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CE}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CE}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CE}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CE}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CE}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CE}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CE}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CE}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CE}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CE}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CE}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CE}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}\u{200D}\u{27A1}\u{FE0F}", + "\u{1F9CF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CF}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CF}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CF}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CF}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CF}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CF}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CF}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CF}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9CF}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9CF}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D4}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D4}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D4}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D4}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D4}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D4}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D4}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D4}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D4}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D4}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D4}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D4}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D6}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D6}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D6}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D6}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D6}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D6}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D6}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D6}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D6}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D6}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D6}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D6}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D7}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D7}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D7}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D7}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D7}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D7}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D7}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D7}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D7}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D7}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D7}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D7}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D8}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D8}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D8}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D8}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D8}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D8}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D8}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D8}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D8}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D8}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D8}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D8}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D9}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D9}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D9}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D9}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D9}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D9}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D9}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D9}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D9}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D9}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9D9}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9D9}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DA}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DA}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DA}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DA}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DA}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DA}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DA}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DA}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DA}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DA}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DA}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DA}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DB}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DB}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DB}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DB}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DB}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DB}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DB}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DB}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DB}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DB}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DC}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DC}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DC}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DC}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DC}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DC}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DC}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DC}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DC}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DC}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DD}\u{1F3FB}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DD}\u{1F3FB}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DD}\u{1F3FC}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DD}\u{1F3FC}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DD}\u{1F3FD}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DD}\u{1F3FD}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DD}\u{1F3FE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DD}\u{1F3FE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DD}\u{1F3FF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DD}\u{1F3FF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DE}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DE}\u{200D}\u{2642}\u{FE0F}", + "\u{1F9DF}\u{200D}\u{2640}\u{FE0F}", + "\u{1F9DF}\u{200D}\u{2642}\u{FE0F}", + "\u{1F468}\u{200D}\u{1F9B0}", + "\u{1F468}\u{200D}\u{1F9B1}", + "\u{1F468}\u{200D}\u{1F9B2}", + "\u{1F468}\u{200D}\u{1F9B3}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F9B0}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F9B1}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F9B2}", + "\u{1F468}\u{1F3FB}\u{200D}\u{1F9B3}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F9B0}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F9B1}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F9B2}", + "\u{1F468}\u{1F3FC}\u{200D}\u{1F9B3}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F9B0}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F9B1}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F9B2}", + "\u{1F468}\u{1F3FD}\u{200D}\u{1F9B3}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F9B0}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F9B1}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F9B2}", + "\u{1F468}\u{1F3FE}\u{200D}\u{1F9B3}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F9B0}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F9B1}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F9B2}", + "\u{1F468}\u{1F3FF}\u{200D}\u{1F9B3}", + "\u{1F469}\u{200D}\u{1F9B0}", + "\u{1F469}\u{200D}\u{1F9B1}", + "\u{1F469}\u{200D}\u{1F9B2}", + "\u{1F469}\u{200D}\u{1F9B3}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F9B0}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F9B1}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F9B2}", + "\u{1F469}\u{1F3FB}\u{200D}\u{1F9B3}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F9B0}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F9B1}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F9B2}", + "\u{1F469}\u{1F3FC}\u{200D}\u{1F9B3}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F9B0}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F9B1}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F9B2}", + "\u{1F469}\u{1F3FD}\u{200D}\u{1F9B3}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F9B0}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F9B1}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F9B2}", + "\u{1F469}\u{1F3FE}\u{200D}\u{1F9B3}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F9B0}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F9B1}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F9B2}", + "\u{1F469}\u{1F3FF}\u{200D}\u{1F9B3}", + "\u{1F9D1}\u{200D}\u{1F9B0}", + "\u{1F9D1}\u{200D}\u{1F9B1}", + "\u{1F9D1}\u{200D}\u{1F9B2}", + "\u{1F9D1}\u{200D}\u{1F9B3}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F9B0}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F9B1}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F9B2}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F9B3}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F9B0}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F9B1}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F9B2}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F9B3}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F9B0}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F9B1}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F9B2}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F9B3}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F9B0}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F9B1}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F9B2}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F9B3}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F9B0}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F9B1}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F9B2}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F9B3}", + "\u{26D3}\u{FE0F}\u{200D}\u{1F4A5}", + "\u{2764}\u{FE0F}\u{200D}\u{1F525}", + "\u{2764}\u{FE0F}\u{200D}\u{1FA79}", + "\u{1F344}\u{200D}\u{1F7EB}", + "\u{1F34B}\u{200D}\u{1F7E9}", + "\u{1F3F3}\u{FE0F}\u{200D}\u{26A7}\u{FE0F}", + "\u{1F3F3}\u{FE0F}\u{200D}\u{1F308}", + "\u{1F3F4}\u{200D}\u{2620}\u{FE0F}", + "\u{1F408}\u{200D}\u{2B1B}", + "\u{1F415}\u{200D}\u{1F9BA}", + "\u{1F426}\u{200D}\u{2B1B}", + "\u{1F426}\u{200D}\u{1F525}", + "\u{1F43B}\u{200D}\u{2744}\u{FE0F}", + "\u{1F441}\u{FE0F}\u{200D}\u{1F5E8}\u{FE0F}", + "\u{1F62E}\u{200D}\u{1F4A8}", + "\u{1F635}\u{200D}\u{1F4AB}", + "\u{1F636}\u{200D}\u{1F32B}\u{FE0F}", + "\u{1F642}\u{200D}\u{2194}\u{FE0F}", + "\u{1F642}\u{200D}\u{2195}\u{FE0F}", + "\u{1F9D1}\u{200D}\u{1FA70}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1FA70}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FB}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1FA70}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FC}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1FA70}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FD}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1FA70}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FE}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FF}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1F430}\u{200D}\u{1F9D1}\u{1F3FE}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1FA70}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FB}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FC}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FD}", + "\u{1F9D1}\u{1F3FF}\u{200D}\u{1FAEF}\u{200D}\u{1F9D1}\u{1F3FE}", +]; + +/// A property of strings (UTS #51 ED-27, for `\p{..}` with `v`). +#[derive(Debug, Clone, Copy)] +pub struct StringProperty { + /// The name (the short name is the same). + pub name: &'static str, + /// Its one-character members. + pub chars: &'static [(u32, u32)], + /// Its longer members, as several lists (their union). + pub strings: &'static [&'static [&'static str]], +} + +/// The properties of strings; `RGI_Emoji` is the union of the six sequence types. +pub static STRING_PROPERTIES: &[StringProperty] = &[ + StringProperty { + name: "Basic_Emoji", + chars: STRING_CHARS_BASIC_EMOJI, + strings: &[STRINGS_BASIC_EMOJI], + }, + StringProperty { + name: "Emoji_Keycap_Sequence", + chars: STRING_CHARS_EMOJI_KEYCAP_SEQUENCE, + strings: &[STRINGS_EMOJI_KEYCAP_SEQUENCE], + }, + StringProperty { + name: "RGI_Emoji_Flag_Sequence", + chars: STRING_CHARS_RGI_EMOJI_FLAG_SEQUENCE, + strings: &[STRINGS_RGI_EMOJI_FLAG_SEQUENCE], + }, + StringProperty { + name: "RGI_Emoji_Modifier_Sequence", + chars: STRING_CHARS_RGI_EMOJI_MODIFIER_SEQUENCE, + strings: &[STRINGS_RGI_EMOJI_MODIFIER_SEQUENCE], + }, + StringProperty { + name: "RGI_Emoji_Tag_Sequence", + chars: STRING_CHARS_RGI_EMOJI_TAG_SEQUENCE, + strings: &[STRINGS_RGI_EMOJI_TAG_SEQUENCE], + }, + StringProperty { + name: "RGI_Emoji_ZWJ_Sequence", + chars: STRING_CHARS_RGI_EMOJI_ZWJ_SEQUENCE, + strings: &[STRINGS_RGI_EMOJI_ZWJ_SEQUENCE], + }, + StringProperty { + name: "RGI_Emoji", + chars: STRING_CHARS_BASIC_EMOJI, + strings: &[ + STRINGS_BASIC_EMOJI, + STRINGS_EMOJI_KEYCAP_SEQUENCE, + STRINGS_RGI_EMOJI_FLAG_SEQUENCE, + STRINGS_RGI_EMOJI_MODIFIER_SEQUENCE, + STRINGS_RGI_EMOJI_TAG_SEQUENCE, + STRINGS_RGI_EMOJI_ZWJ_SEQUENCE, + ], + }, +]; diff --git a/crates/clazel-js-regexp/src/unicode.rs b/crates/clazel-js-regexp/src/unicode.rs new file mode 100644 index 000000000..918163f8a --- /dev/null +++ b/crates/clazel-js-regexp/src/unicode.rs @@ -0,0 +1,278 @@ +//! The Unicode data the matcher reads: case canonicalization (ECMA-262 +//! §22.2.2.7.3† `Canonicalize`), the equivalence classes it induces, and the +//! property sets of `\p{..}` (§22.2.2.9†, UnicodePropertyValueExpression). +//! +//! The tables are in [`crate::tables`], generated from the vendored UCD. Every +//! lookup is a bisection written here, with a loop that provably ends +//! (`lo` grows or `hi` shrinks on every pass), so that Kani can check it. + +use crate::tables; + +/// Finds `key` in a table of pairs sorted by their first element. +fn find_pair(table: &[(u32, u32)], key: u32) -> Option { + let mut lo = 0usize; + let mut hi = table.len(); + while lo < hi { + let mid = lo.saturating_add(hi.saturating_sub(lo) / 2); + let &(k, v) = table.get(mid)?; + if k == key { + return Some(v); + } + if k < key { + lo = mid.saturating_add(1); + } else { + hi = mid; + } + } + None +} + +/// The index of the first element of `table` (sorted by first element) whose +/// first element is at least `key`. +fn lower_bound(table: &[(u32, u32)], key: u32) -> usize { + let mut lo = 0usize; + let mut hi = table.len(); + while lo < hi { + let mid = lo.saturating_add(hi.saturating_sub(lo) / 2); + match table.get(mid) { + Some(&(k, _)) if k < key => lo = mid.saturating_add(1), + _ => hi = mid, + } + } + lo +} + +/// The range of `ranges` (sorted, disjoint) that holds `c`, as its index. +fn range_index(ranges: &[T], first: impl Fn(T) -> u32, c: u32) -> Option { + // The last range whose first element is at most `c`. + let mut lo = 0usize; + let mut hi = ranges.len(); + while lo < hi { + let mid = lo.saturating_add(hi.saturating_sub(lo) / 2); + match ranges.get(mid) { + Some(&r) if first(r) <= c => lo = mid.saturating_add(1), + _ => hi = mid, + } + } + lo.checked_sub(1) +} + +/// Whether `c` is in one of `ranges` (first, last), sorted and disjoint. +#[must_use] +pub fn in_ranges(ranges: &[(u32, u32)], c: u32) -> bool { + range_index(ranges, |r| r.0, c) + .and_then(|i| ranges.get(i)) + .is_some_and(|&(_, last)| c <= last) +} + +/// Simple Case Folding of `c` (`CaseFolding.txt`, statuses `C` and `S`): what +/// `Canonicalize` returns with `u` or `v` (§22.2.2.7.3† step 2.a). +#[must_use] +pub fn simple_fold(c: u32) -> u32 { + find_pair(tables::SIMPLE_FOLD, c).unwrap_or(c) +} + +/// `Canonicalize` without `u` or `v` of the code unit `c` +/// (§22.2.2.7.3† steps 3 to 7): `toUppercase` of it if that is one code unit, +/// unless that would turn a non-ASCII code unit into an ASCII one. +#[must_use] +pub fn canonicalize_unit(c: u32) -> u32 { + find_pair(tables::UPPER, c).unwrap_or(c) +} + +/// `Canonicalize(rer, c)` (§22.2.2.7.3†) for an expression with `i`: +/// [`simple_fold`] with `unicode` (`u` or `v`), else [`canonicalize_unit`]. +#[must_use] +pub fn canonicalize(c: u32, unicode: bool) -> u32 { + if unicode { + simple_fold(c) + } else { + canonicalize_unit(c) + } +} + +/// Puts into `out` every `a` with `canonicalize(a, unicode) == cc`: the +/// characters CharacterSetMatcher (§22.2.2.7.1† step 3) and the +/// backreference matcher treat as equal to one another. +pub fn equivalents(cc: u32, unicode: bool, out: &mut Vec) { + out.clear(); + if canonicalize(cc, unicode) == cc { + out.push(cc); + } + let inverse = if unicode { + tables::SIMPLE_FOLD_INVERSE + } else { + tables::UPPER_INVERSE + }; + let mut i = lower_bound(inverse, cc); + while let Some(&(k, v)) = inverse.get(i) { + if k != cc { + break; + } + out.push(v); + i = i.saturating_add(1); + } +} + +/// The index into [`tables::CATEGORY_NAMES`] of the general category of `c`. +#[must_use] +pub fn general_category(c: u32) -> u8 { + range_index(tables::GENERAL_CATEGORY, |r| r.0, c) + .and_then(|i| tables::GENERAL_CATEGORY.get(i)) + .map_or(CN, |&(_, last, cat)| if c <= last { cat } else { CN }) +} + +/// The index of `Cn` in [`tables::CATEGORY_NAMES`]. +const CN: u8 = 29; + +/// The bit of a category for a mask of categories. +fn bit(abbr: &str) -> u32 { + tables::CATEGORY_NAMES + .iter() + .position(|n| *n == abbr) + .and_then(|i| u32::try_from(i).ok()) + .and_then(|i| 1u32.checked_shl(i)) + .unwrap_or(0) +} + +fn mask(abbrs: &[&str]) -> u32 { + abbrs.iter().fold(0, |m, a| m | bit(a)) +} + +/// A set of code points named by `\p{..}`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Prop { + /// The code points whose general category is in the mask (a bit per index + /// of [`tables::CATEGORY_NAMES`]). + Category(u32), + /// A binary property's ranges. + Ranges(&'static [(u32, u32)]), + /// `Any`. + Any, + /// `ASCII`. + Ascii, + /// `Assigned`: every category but `Cn`. + Assigned, +} + +impl Prop { + /// Whether `c` has the property. + #[must_use] + pub fn contains(&self, c: u32) -> bool { + match *self { + Prop::Category(m) => 1u32 + .checked_shl(u32::from(general_category(c))) + .is_some_and(|b| m & b != 0), + Prop::Ranges(r) => in_ranges(r, c), + Prop::Any => c <= 0x10FFFF, + Prop::Ascii => c < 0x80, + Prop::Assigned => general_category(c) != CN, + } + } +} + +/// The mask of the general categories a `General_Category` value (a long name, +/// a short name or an alias, as in `PropertyValueAliases.txt`) stands for: +/// a group such as `L` is the union of its members. +#[must_use] +pub fn category_mask(value: &str) -> Option { + Some(match value { + "Lu" | "Uppercase_Letter" => mask(&["Lu"]), + "Ll" | "Lowercase_Letter" => mask(&["Ll"]), + "Lt" | "Titlecase_Letter" => mask(&["Lt"]), + "LC" | "Cased_Letter" => mask(&["Lu", "Ll", "Lt"]), + "Lm" | "Modifier_Letter" => mask(&["Lm"]), + "Lo" | "Other_Letter" => mask(&["Lo"]), + "L" | "Letter" => mask(&["Lu", "Ll", "Lt", "Lm", "Lo"]), + "Mn" | "Nonspacing_Mark" => mask(&["Mn"]), + "Mc" | "Spacing_Mark" => mask(&["Mc"]), + "Me" | "Enclosing_Mark" => mask(&["Me"]), + "M" | "Mark" | "Combining_Mark" => mask(&["Mn", "Mc", "Me"]), + "Nd" | "Decimal_Number" | "digit" => mask(&["Nd"]), + "Nl" | "Letter_Number" => mask(&["Nl"]), + "No" | "Other_Number" => mask(&["No"]), + "N" | "Number" => mask(&["Nd", "Nl", "No"]), + "Pc" | "Connector_Punctuation" => mask(&["Pc"]), + "Pd" | "Dash_Punctuation" => mask(&["Pd"]), + "Ps" | "Open_Punctuation" => mask(&["Ps"]), + "Pe" | "Close_Punctuation" => mask(&["Pe"]), + "Pi" | "Initial_Punctuation" => mask(&["Pi"]), + "Pf" | "Final_Punctuation" => mask(&["Pf"]), + "Po" | "Other_Punctuation" => mask(&["Po"]), + "P" | "Punctuation" | "punct" => mask(&["Pc", "Pd", "Ps", "Pe", "Pi", "Pf", "Po"]), + "Sm" | "Math_Symbol" => mask(&["Sm"]), + "Sc" | "Currency_Symbol" => mask(&["Sc"]), + "Sk" | "Modifier_Symbol" => mask(&["Sk"]), + "So" | "Other_Symbol" => mask(&["So"]), + "S" | "Symbol" => mask(&["Sm", "Sc", "Sk", "So"]), + "Zs" | "Space_Separator" => mask(&["Zs"]), + "Zl" | "Line_Separator" => mask(&["Zl"]), + "Zp" | "Paragraph_Separator" => mask(&["Zp"]), + "Z" | "Separator" => mask(&["Zs", "Zl", "Zp"]), + "Cc" | "Control" | "cntrl" => mask(&["Cc"]), + "Cf" | "Format" => mask(&["Cf"]), + "Cs" | "Surrogate" => mask(&["Cs"]), + "Co" | "Private_Use" => mask(&["Co"]), + "Cn" | "Unassigned" => mask(&["Cn"]), + "C" | "Other" => mask(&["Cc", "Cf", "Cs", "Co", "Cn"]), + _ => return None, + }) +} + +/// The set a binary property name (canonical or alias, exactly as written) +/// stands for: `Any`, `ASCII` and `Assigned` are the standard's; the others +/// come from [`tables::BINARY`]. +#[must_use] +pub fn binary_property(name: &str) -> Option { + match name { + "Any" => return Some(Prop::Any), + "ASCII" => return Some(Prop::Ascii), + "Assigned" => return Some(Prop::Assigned), + _ => {} + } + let canonical = crate::props::BINARY_PROPERTIES + .iter() + .find(|(full, alias)| *full == name.as_bytes() || *alias == name.as_bytes()) + .map(|(full, _)| *full)?; + tables::BINARY + .iter() + .find(|(n, _)| n.as_bytes() == canonical) + .map(|&(_, r)| Prop::Ranges(r)) +} + +/// The set a `Script` (or, with `extensions`, `Script_Extensions`) value +/// stands for: a long name, a short name or another alias, exactly as in +/// `PropertyValueAliases.txt`, from [`tables::SCRIPTS`]. +#[must_use] +pub fn script_property(value: &str, extensions: bool) -> Option { + tables::SCRIPTS + .iter() + .find(|s| s.names.contains(&value)) + .map(|s| Prop::Ranges(if extensions { s.scx } else { s.sc })) +} + +/// A property of strings (`v`): its one-character members as a set, and its +/// strings of other lengths as code points. +#[derive(Debug, Clone)] +pub struct StringSet { + /// The members of one code point. + pub chars: Prop, + /// The members of two or more, in the order of the Unicode files. + pub strings: Vec>, +} + +/// The property of strings named `name` (UTS #51; `RGI_Emoji` is the union of +/// the others), from [`tables::STRING_PROPERTIES`]. +#[must_use] +pub fn string_property(name: &str) -> Option { + let p = tables::STRING_PROPERTIES.iter().find(|p| p.name == name)?; + Some(StringSet { + chars: Prop::Ranges(p.chars), + strings: p + .strings + .iter() + .flat_map(|list| list.iter()) + .map(|s| s.chars().map(u32::from).collect()) + .collect(), + }) +} diff --git a/crates/clazel-js-regexp/src/vm.rs b/crates/clazel-js-regexp/src/vm.rs new file mode 100644 index 000000000..15786d55c --- /dev/null +++ b/crates/clazel-js-regexp/src/vm.rs @@ -0,0 +1,725 @@ +//! The matcher: a loop over a [`Program`]'s instructions and an explicit +//! backtrack stack of bounded size. ECMA-262 §22.2.2† (Pattern Semantics), +//! with `[[Input]]` the code units of the string (read as code points with `u` +//! or `v`). Section numbers marked † are from memory. +//! +//! # Modelling choices +//! +//! - **No recursion**, over the pattern or over the input: the standard's +//! closures became instructions ([`crate::compile`]) and a failure is a pop. +//! Every choice point is a stack entry, and so is every change to a +//! register (a capture, a loop counter) that a failure must undo. +//! - **A bounded stack, the one deviation.** The stack holds at most +//! [`Limits::max_stack`] entries; a match that needs more is +//! [`MatchError::StackOverflow`], where the standard has no limit. The +//! engine turns it into a `RangeError` (phase-7.md item 7.10). The `Vec` +//! grows by doubling, so its allocation can be up to twice that many +//! entries. The registers and the class scratch space depend only on the +//! pattern. +//! - **A step budget and an interrupt hook** (phase-7.md 7.0(g)): every +//! instruction, every entry popped and every character a greedy run +//! consumed is a step; past [`Limits::max_steps`] the match is +//! [`MatchError::StepLimit`], and every 4096 steps the hook is asked +//! whether to stop ([`MatchError::Interrupted`]). A catastrophic pattern +//! runs until one of these, or until the stack is full. +//! - **Indices** are code unit indices, in both modes. In `u`/`v` mode a match +//! that starts inside a surrogate pair starts at the pair's first unit +//! (RegExpBuiltinExec §22.2.7.2† step 12.a: the input index is that of the +//! character that contains the code unit `lastIndex`), and an unanchored +//! search advances by whole code points (AdvanceStringIndex §22.2.7.3†). +//! - **`lastIndex`, `g` and `y`** are the caller's: [`Program::exec`] takes a +//! start index and says whether to try only there (`y`) or to scan forward. +//! - **Lookaround** bodies are atomic (§22.2.2.3.2† Assertion: the body is +//! matched with a continuation that returns its state): when a positive +//! lookahead's body has matched, its choices are dropped, its captures kept. +//! A negative one that matches undoes everything its body did. + +use crate::program::{ClassOp, ClassProg, EscKind, Inst, One, OneKind, Program}; +use crate::stack::{BtStack, Entry}; +use crate::unicode::{canonicalize, equivalents}; + +/// The value of a register that holds no position. +const NONE: usize = usize::MAX; + +/// How many steps pass between asks of the interrupt hook. +const TICK: u64 = 4096; + +/// The matcher's resource limits. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Limits { + /// The most entries the backtrack stack may hold (16 to 24 bytes each). + pub max_stack: usize, + /// The most steps one [`Program::exec`] may take, or `None` for no limit. + pub max_steps: Option, +} + +impl Default for Limits { + /// A million stack entries (at most 24 MB, 48 with the `Vec`'s growth) and no + /// step limit. + fn default() -> Limits { + Limits { + max_stack: 1 << 20, + max_steps: None, + } + } +} + +/// Why a match was abandoned. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum MatchError { + /// The backtrack stack reached [`Limits::max_stack`] entries. The engine + /// throws a `RangeError`. + StackOverflow, + /// [`Limits::max_steps`] steps were taken. + StepLimit, + /// The interrupt hook returned `true`. + Interrupted, + /// The program is malformed (an instruction or register that does not + /// exist). Not reachable from [`crate::compile`]'s output. + Corrupt, +} + +impl core::fmt::Display for MatchError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + MatchError::StackOverflow => write!(f, "regular expression backtrack stack is full"), + MatchError::StepLimit => write!(f, "regular expression step limit reached"), + MatchError::Interrupted => write!(f, "regular expression match interrupted"), + MatchError::Corrupt => write!(f, "malformed regular expression program"), + } + } +} + +/// A successful match: the whole match and each group's capture, as code unit +/// indices `(start, end)`, or `None` for a group that did not take part. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Match { + /// Index 0 is the whole match; index `n` is group `n`. + pub captures: Vec>, +} + +impl Match { + /// The capture of group `n` (0 for the whole match). + #[must_use] + pub fn get(&self, n: usize) -> Option<(usize, usize)> { + self.captures.get(n).copied().flatten() + } +} + +impl Program { + /// Matches the pattern against `input`, starting at code unit index + /// `start`. + /// + /// With `sticky` the match must start at `start` (the `y` flag); without, + /// the starting index advances until a match is found or the input ends + /// (RegExpBuiltinExec §22.2.7.2†, steps 12 and 13). The caller decides + /// `start` (`lastIndex`, or 0) and what to do with the result. + /// + /// `interrupt`, if given, is asked every 4096 steps whether to stop. + /// + /// # Errors + /// + /// [`MatchError`]: a limit of `limits` was reached, or the hook said stop. + pub fn exec( + &self, + input: &[u16], + start: usize, + sticky: bool, + limits: &Limits, + interrupt: Option<&mut dyn FnMut() -> bool>, + ) -> Result, MatchError> { + if start > input.len() { + return Ok(None); + } + let mut vm = Vm { + prog: self, + input, + regs: vec![NONE; self.regs], + stack: BtStack::new(limits.max_stack), + steps: 0, + max_steps: limits.max_steps.unwrap_or(u64::MAX), + next_tick: TICK, + hook: interrupt, + eq: Vec::new(), + bools: Vec::new(), + end: 0, + }; + let unicode = self.unicode; + let mut s = start; + // Inside a surrogate pair, the character that contains the unit is the + // pair. + if unicode + && s > 0 + && input.get(s).is_some_and(|u| (0xDC00..0xE000).contains(u)) + && s.checked_sub(1) + .and_then(|p| input.get(p)) + .is_some_and(|u| (0xD800..0xDC00).contains(u)) + { + s = s.saturating_sub(1); + } + loop { + if vm.run(s)? { + return Ok(Some(vm.take_match(s))); + } + if sticky || s >= input.len() { + return Ok(None); + } + // AdvanceStringIndex. + s = match read(input, unicode, s, false) { + Some((_, next)) => next, + None => return Ok(None), + }; + } + } +} + +struct Vm<'a, 'h> { + prog: &'a Program, + input: &'a [u16], + regs: Vec, + stack: BtStack, + steps: u64, + max_steps: u64, + next_tick: u64, + hook: Option<&'h mut dyn FnMut() -> bool>, + /// Scratch space for class matching. + eq: Vec, + bools: Vec, + /// Where the last successful run ended. + end: usize, +} + +/// A code point from a high and a low surrogate. +fn combine(high: u16, low: u16) -> u32 { + 0x10000u32 + .wrapping_add(u32::from(high.wrapping_sub(0xD800)).wrapping_mul(0x400)) + .wrapping_add(u32::from(low.wrapping_sub(0xDC00))) +} + +/// The character at `pos` (forward) or ending at `pos` (backward), and the +/// position past (before) it: a code unit, or with `unicode` a code point. +pub(crate) fn read(input: &[u16], unicode: bool, pos: usize, back: bool) -> Option<(u32, usize)> { + if back { + let p1 = pos.checked_sub(1)?; + let u = *input.get(p1)?; + if unicode + && (0xDC00..0xE000).contains(&u) + && let Some(p0) = p1.checked_sub(1) + && let Some(&h) = input.get(p0) + && (0xD800..0xDC00).contains(&h) + { + return Some((combine(h, u), p0)); + } + Some((u32::from(u), p1)) + } else { + let u = *input.get(pos)?; + let p1 = pos.saturating_add(1); + if unicode + && (0xD800..0xDC00).contains(&u) + && let Some(&l) = input.get(p1) + && (0xDC00..0xE000).contains(&l) + { + return Some((combine(u, l), p1.saturating_add(1))); + } + Some((u32::from(u), p1)) + } +} + +/// LineTerminator (§12.3†). +fn is_line_terminator(c: u32) -> bool { + matches!(c, 0x0A | 0x0D | 0x2028 | 0x2029) +} + +/// WhiteSpace and LineTerminator (§12.2†, §12.3†): what `\s` matches. +fn is_space(c: u32) -> bool { + matches!( + c, + 0x09 | 0x0A | 0x0B | 0x0C | 0x0D | 0x20 | 0xA0 | 0x1680 | 0x2000 + ..=0x200A | 0x2028 | 0x2029 | 0x202F | 0x205F | 0x3000 | 0xFEFF + ) +} + +/// WordCharacters (§22.2.2.9†): ASCII letters, digits and `_`, and with +/// `extras` (`u` or `v` with `i`) the two characters that fold to them. +fn is_word(c: u32, extras: bool) -> bool { + matches!(c, 0x30..=0x39 | 0x41..=0x5A | 0x61..=0x7A | 0x5F) + || (extras && matches!(c, 0x17F | 0x212A)) +} + +fn escape_has(kind: &EscKind, c: u32, extras: bool) -> bool { + match kind { + EscKind::Digit => (0x30..=0x39).contains(&c), + EscKind::Space => is_space(c), + EscKind::Word => is_word(c, extras), + EscKind::Prop(p) => p.contains(c), + } +} + +/// CharacterSetMatcher (§22.2.2.7.1†) for a class: whether the input +/// character `ch` is matched. Runs the class's postfix program on a stack of +/// booleans. +fn class_matches( + cls: &ClassProg, + unicode: bool, + ch: u32, + eq: &mut Vec, + st: &mut Vec, +) -> bool { + eq.clear(); + if cls.ic { + // Step 3: "there exists a member a of A such that Canonicalize(rer, a) + // is cc", with cc the canonical form of the input character. + equivalents(canonicalize(ch, unicode), unicode, eq); + } else { + eq.push(ch); + } + st.clear(); + for op in &cls.ops { + match op { + ClassOp::Char(c) => st.push(eq.contains(c)), + ClassOp::Range(lo, hi) => st.push(eq.iter().any(|a| lo <= a && a <= hi)), + ClassOp::Esc { kind, neg } => st.push( + eq.iter() + .any(|&a| escape_has(kind, a, cls.word_extras) != *neg), + ), + ClassOp::Not => { + let t = st.pop().unwrap_or(false); + st.push(!t); + } + ClassOp::Or(n) => { + let mut acc = false; + for _ in 0..*n { + acc |= st.pop().unwrap_or(false); + } + st.push(acc); + } + ClassOp::And(n) => { + let mut acc = true; + for _ in 0..*n { + acc &= st.pop().unwrap_or(false); + } + st.push(acc); + } + ClassOp::Sub(n) => { + // The operands were pushed in order, so the first is popped last. + let mut first = false; + let mut rest = false; + for i in 0..*n { + let v = st.pop().unwrap_or(false); + if i.saturating_add(1) == *n { + first = v; + } else { + rest |= v; + } + } + st.push(first && !rest); + } + } + } + let found = st.pop().unwrap_or(false); + found != cls.invert +} + +impl Vm<'_, '_> { + fn tick(&mut self, n: u64) -> Result<(), MatchError> { + self.steps = self.steps.saturating_add(n); + if self.steps > self.max_steps { + return Err(MatchError::StepLimit); + } + if self.steps >= self.next_tick { + self.next_tick = self.steps.saturating_add(TICK); + let stop = match self.hook { + Some(ref mut h) => h(), + None => false, + }; + if stop { + return Err(MatchError::Interrupted); + } + } + Ok(()) + } + + fn push(&mut self, e: Entry) -> Result<(), MatchError> { + self.stack.push(e).map_err(|_| MatchError::StackOverflow) + } + + fn reg(&self, r: u32) -> Result { + self.regs + .get(r as usize) + .copied() + .ok_or(MatchError::Corrupt) + } + + /// Writes a register, and pushes what undoes it. + fn set(&mut self, r: u32, v: usize) -> Result<(), MatchError> { + let slot = self.regs.get_mut(r as usize).ok_or(MatchError::Corrupt)?; + let old = *slot; + *slot = v; + self.push(Entry::Restore { reg: r, old }) + } + + fn restore(&mut self, r: u32, old: usize) { + if let Some(slot) = self.regs.get_mut(r as usize) { + *slot = old; + } + } + + /// Matches one character at `pos` (CharacterSetMatcher §22.2.2.7.1† and + /// the matchers for a character and for `.`), returning the new position. + fn step_one(&mut self, one: &One, pos: usize) -> Option { + let unicode = self.prog.unicode; + let (ch, np) = read(self.input, unicode, pos, one.back)?; + let ok = match &one.kind { + OneKind::Char { c, ic } => { + if *ic { + canonicalize(ch, unicode) == *c + } else { + ch == *c + } + } + OneKind::Dot { all } => *all || !is_line_terminator(ch), + OneKind::Class(id) => { + let cls = self.prog.classes.get(*id as usize)?; + class_matches(cls, unicode, ch, &mut self.eq, &mut self.bools) + } + }; + ok.then_some(np) + } + + /// BackreferenceMatcher (§22.2.2.7.2†) against the capture `s..e`: the + /// new position, or `None` if the text does not match. + fn backref(&self, s: usize, e: usize, pos: usize, ic: bool, back: bool) -> Option { + let unicode = self.prog.unicode; + let input = self.input; + if !ic && !unicode { + let len = e.checked_sub(s)?; + let target = if back { + pos.checked_sub(len)? + } else { + pos.checked_add(len)? + }; + let (from, to) = if back { (target, pos) } else { (pos, target) }; + let a = input.get(s..e)?; + let b = input.get(from..to)?; + return (a == b).then_some(target); + } + let canon = |c: u32| if ic { canonicalize(c, unicode) } else { c }; + let mut b = pos; + if back { + let mut a = e; + while a > s { + let (c1, na) = read(input, unicode, a, true)?; + let (c2, nb) = read(input, unicode, b, true)?; + if canon(c1) != canon(c2) { + return None; + } + a = na; + b = nb; + } + } else { + let mut a = s; + while a < e { + let (c1, na) = read(input, unicode, a, false)?; + let (c2, nb) = read(input, unicode, b, false)?; + if canon(c1) != canon(c2) { + return None; + } + a = na; + b = nb; + } + } + Some(b) + } + + /// The instruction `Run`: as many repetitions as match, up to `max`; fail + /// if fewer than `min`; leave one stack entry for the shorter runs. + fn run_loop( + &mut self, + one: &One, + min: usize, + max: usize, + pos: usize, + next_pc: usize, + ) -> Result, MatchError> { + let mut p = pos; + let mut limit = pos; + let mut count = 0usize; + while count < max { + match self.step_one(one, p) { + Some(np) => { + p = np; + count = count.saturating_add(1); + if count == min { + limit = p; + } + } + None => break, + } + } + self.tick(u64::try_from(count).unwrap_or(u64::MAX))?; + if count < min { + return Ok(None); + } + if p != limit { + let pc = u32::try_from(next_pc).map_err(|_| MatchError::Corrupt)?; + self.push(Entry::Run { + pc, + back: one.back, + cur: p, + limit, + })?; + } + Ok(Some((next_pc, p))) + } + + /// Pops the stack, undoing registers, to the nearest choice: the `pc` and + /// position to resume at, or `None` if there is none (the match failed). + fn backtrack(&mut self) -> Result, MatchError> { + let unicode = self.prog.unicode; + loop { + let Some(e) = self.stack.pop() else { + return Ok(None); + }; + self.tick(1)?; + match e { + Entry::Branch { pc, pos } => return Ok(Some((pc as usize, pos))), + Entry::Restore { reg, old } => self.restore(reg, old), + Entry::Run { + pc, + back, + cur, + limit, + } => { + // One character nearer to the limit, never past it. + let n = if back { + read(self.input, unicode, cur, false) + .map_or(limit, |(_, np)| np) + .min(limit) + } else { + read(self.input, unicode, cur, true) + .map_or(limit, |(_, np)| np) + .max(limit) + }; + if n != limit { + self.push(Entry::Run { + pc, + back, + cur: n, + limit, + })?; + } + return Ok(Some((pc as usize, n))); + } + Entry::LookMark { neg, pc, pos } => { + // The body of a lookaround failed: a negative one succeeds. + if neg { + return Ok(Some((pc as usize, pos))); + } + } + } + } + } + + /// A negative lookaround whose body matched: undo what the body did, down + /// to and including its mark. The caller then fails. + fn unwind_look(&mut self) -> Result<(), MatchError> { + loop { + let Some(e) = self.stack.pop() else { + return Err(MatchError::Corrupt); + }; + self.tick(1)?; + match e { + Entry::Restore { reg, old } => self.restore(reg, old), + Entry::LookMark { .. } => return Ok(()), + Entry::Branch { .. } | Entry::Run { .. } => {} + } + } + } + + /// Runs the program from the beginning at input position `start`: whether + /// it matched. On a match, the registers hold the captures and `end` the + /// end of the whole match. + fn run(&mut self, start: usize) -> Result { + let prog = self.prog; + self.regs.fill(NONE); + self.stack.clear(); + let input = self.input; + let mut pc = 0usize; + let mut pos = start; + loop { + self.tick(1)?; + let inst = prog.insts.get(pc).ok_or(MatchError::Corrupt)?; + let next = pc.saturating_add(1); + let step: Option<(usize, usize)> = match inst { + Inst::One(one) => self.step_one(one, pos).map(|np| (next, np)), + Inst::Run { one, min, max } => self.run_loop(one, *min, *max, pos, next)?, + Inst::LineStart { multiline } => { + // §22.2.2.6.1† Assertion, `^`. + let at_start = pos == 0 + || (*multiline + && pos + .checked_sub(1) + .and_then(|p| input.get(p)) + .is_some_and(|&u| is_line_terminator(u32::from(u)))); + at_start.then_some((next, pos)) + } + Inst::LineEnd { multiline } => { + // `$`. + let at_end = pos >= input.len() + || (*multiline + && input + .get(pos) + .is_some_and(|&u| is_line_terminator(u32::from(u)))); + at_end.then_some((next, pos)) + } + Inst::WordBoundary { neg, extras } => { + // `\b`, `\B`: IsWordChar(e - 1) and IsWordChar(e). + let before = pos + .checked_sub(1) + .and_then(|p| input.get(p)) + .is_some_and(|&u| is_word(u32::from(u), *extras)); + let after = input + .get(pos) + .is_some_and(|&u| is_word(u32::from(u), *extras)); + ((before != after) != *neg).then_some((next, pos)) + } + Inst::BackRef { id, ic, back } => { + let groups = prog.backrefs.get(*id as usize).ok_or(MatchError::Corrupt)?; + let mut cap = None; + for &g in groups { + let a = g.saturating_mul(2); + let s = self.reg(a)?; + if s != NONE { + cap = Some((s, self.reg(a.saturating_add(1))?)); + break; + } + } + match cap { + // "If r is undefined, return c(x)." + None => Some((next, pos)), + Some((s, e)) => self.backref(s, e, pos, *ic, *back).map(|np| (next, np)), + } + } + Inst::Split { first, second } => { + self.push(Entry::Branch { pc: *second, pos })?; + Some((*first as usize, pos)) + } + Inst::Jmp(t) => Some((*t as usize, pos)), + Inst::GroupOpen { reg } => { + self.set(*reg, pos)?; + Some((next, pos)) + } + Inst::GroupClose { cap, reg, back } => { + let s = self.reg(*reg)?; + let (a, b) = if *back { (pos, s) } else { (s, pos) }; + let r = cap.saturating_mul(2); + self.set(r, a)?; + self.set(r.saturating_add(1), b)?; + Some((next, pos)) + } + Inst::ResetCaps { first, last } => { + for i in *first..=*last { + let r = i.saturating_mul(2); + if self.reg(r)? != NONE { + self.set(r, NONE)?; + self.set(r.saturating_add(1), NONE)?; + } + } + Some((next, pos)) + } + Inst::CounterInit { reg } => { + self.set(*reg, 0)?; + Some((next, pos)) + } + Inst::SavePos { reg } => { + self.set(*reg, pos)?; + Some((next, pos)) + } + Inst::LoopHead { + counter, + min, + max, + greedy, + body, + exit, + } => { + let n = self.reg(*counter)?; + if n < *min { + Some((*body as usize, pos)) + } else if n >= *max { + Some((*exit as usize, pos)) + } else if *greedy { + self.push(Entry::Branch { pc: *exit, pos })?; + Some((*body as usize, pos)) + } else { + self.push(Entry::Branch { pc: *body, pos })?; + Some((*exit as usize, pos)) + } + } + Inst::LoopEnd { + counter, + pos_reg, + min, + head, + } => { + let n = self.reg(*counter)?; + let began = self.reg(*pos_reg)?; + if n >= *min && began == pos { + None + } else { + self.set(*counter, n.saturating_add(1))?; + Some((*head as usize, pos)) + } + } + Inst::LookStart { neg, end } => { + self.push(Entry::LookMark { + neg: *neg, + pc: *end, + pos, + })?; + Some((next, pos)) + } + Inst::LookEnd { neg } => { + if *neg { + self.unwind_look()?; + None + } else { + let (p, work) = self.stack.commit_look().ok_or(MatchError::Corrupt)?; + self.tick(u64::try_from(work).unwrap_or(u64::MAX))?; + Some((next, p)) + } + } + Inst::Match => { + self.end = pos; + return Ok(true); + } + }; + match step { + Some((p, q)) => { + pc = p; + pos = q; + } + None => match self.backtrack()? { + Some((p, q)) => { + pc = p; + pos = q; + } + None => return Ok(false), + }, + } + } + } + + fn take_match(&self, start: usize) -> Match { + let mut captures = Vec::new(); + captures.push(Some((start, self.end))); + let n = self.prog.capture_count; + for i in 1..=n { + let a = usize::try_from(i.saturating_mul(2)).unwrap_or(usize::MAX); + let s = self.regs.get(a).copied().unwrap_or(NONE); + let e = self.regs.get(a.saturating_add(1)).copied().unwrap_or(NONE); + captures.push((s != NONE && e != NONE).then_some((s, e))); + } + Match { captures } + } +} diff --git a/crates/clazel-js-regexp/tests/conformance_names.rs b/crates/clazel-js-regexp/tests/conformance_names.rs index 03a8682f4..32e867917 100644 --- a/crates/clazel-js-regexp/tests/conformance_names.rs +++ b/crates/clazel-js-regexp/tests/conformance_names.rs @@ -234,6 +234,23 @@ fn script_names() -> Result<(), String> { bad.push(format!("{text} is accepted")); } } + // The generated table (what `parse` uses) holds the same names. + let ours: BTreeSet = clazel_js_regexp::tables::SCRIPTS + .iter() + .flat_map(|s| s.names.iter().map(|n| (*n).to_string())) + .collect(); + for n in scripts.difference(&ours) { + bad.push(format!("{n}: in the UCD, not in tables::SCRIPTS")); + } + for n in ours.difference(&scripts) { + bad.push(format!("{n}: in tables::SCRIPTS, not in the UCD")); + } + for s in &scripts { + let text = format!("\\p{{sc={s}}}"); + if parse(&units(&text), u_flags()).is_err() { + bad.push(format!("{text} is rejected by parse")); + } + } report("UCD script names", &bad, scripts.len()); assert!(bad.is_empty(), "{}", bad.join("\n")); assert!(scripts.len() > 150); diff --git a/crates/clazel-js-regexp/tests/differential.rs b/crates/clazel-js-regexp/tests/differential.rs new file mode 100644 index 000000000..706708d4c --- /dev/null +++ b/crates/clazel-js-regexp/tests/differential.rs @@ -0,0 +1,1418 @@ +//! A differential: the matcher against a reference matcher, on random small +//! patterns and inputs. +//! +//! The reference is a direct transcription of ECMA-262 §22.2.2† (Pattern +//! Semantics): Matchers are closures from a state and a continuation to a +//! state or failure, RepeatMatcher recurses exactly as the standard's steps +//! do, and the input is a list of characters (code points with `u` or `v`), +//! indexed by character, not by code unit. It is exponential and recursive, +//! which is the point: it shares nothing with the real matcher's loop, its +//! instruction list or its stack. It reads the same syntax tree +//! ([`clazel_js_regexp::parse`]) and the same case-folding function and +//! property tables (`src/tables.rs`, checked in `tests/tables.rs`); it does not +//! use the real matcher's tables of equivalent characters: for `i` it searches +//! a universe of characters for those with the same `Canonicalize` +//! (CharacterSetMatcher step 3, read literally). +//! +//! Patterns come from a small random grammar over a few letters and the +//! characters where case mappings are interesting: alternation, groups +//! (capturing, named, non-capturing, with modifiers), the quantifiers (greedy +//! and lazy, counted), lookahead and lookbehind, backreferences, `^ $ \b \B`, +//! `.`, classes and escapes, and for `v` class set operations; with +//! the flags `i m s u v` at random. Inputs are random strings over the same +//! characters. For each pattern and input, every start index is tried with and +//! without `y`, and the captures must be equal. + +use std::cell::{Cell, RefCell}; +use std::collections::HashMap; + +use clazel_js_regexp::ast::{ + Assertion, Atom, ClassContents, ClassEscape, ClassItem, ClassSetExpr, ClassSetItem, + ClassSetOperand, Disjunction, Property, Term, TermKind, +}; +use clazel_js_regexp::unicode::{ + Prop, binary_property, canonicalize, category_mask, script_property, string_property, +}; +use clazel_js_regexp::{Flags, Limits, Pattern, Program, compile, parse}; + +// --------------------------------------------------------------------------- +// The reference matcher. + +type Caps = Vec>; + +/// MatchState: an end index (in characters) and the captures. +#[derive(Clone)] +struct State { + end: usize, + caps: Caps, +} + +/// The flags in scope (modifiers can change them). +#[derive(Clone, Copy)] +struct Fl { + ic: bool, + ml: bool, + dot_all: bool, +} + +type Cont<'k> = &'k dyn Fn(State) -> Option; + +/// The input as characters, with each one's code unit index. +struct Input { + chars: Vec, + /// `pos[i]` is the code unit index of character `i`; `pos[chars.len()]` is + /// the length in code units. + pos: Vec, +} + +fn decode(units: &[u16], unicode: bool) -> Input { + let mut chars = Vec::new(); + let mut pos = Vec::new(); + let mut i = 0usize; + while i < units.len() { + pos.push(i); + let u = units.get(i).copied().unwrap_or(0); + let next = units.get(i.saturating_add(1)).copied(); + match next { + Some(l) + if unicode && (0xD800..0xDC00).contains(&u) && (0xDC00..0xE000).contains(&l) => + { + let cp = 0x10000u32 + .wrapping_add(u32::from(u).wrapping_sub(0xD800).wrapping_mul(0x400)) + .wrapping_add(u32::from(l).wrapping_sub(0xDC00)); + chars.push(cp); + i = i.saturating_add(2); + } + _ => { + chars.push(u32::from(u)); + i = i.saturating_add(1); + } + } + } + pos.push(units.len()); + Input { chars, pos } +} + +struct Reference<'a> { + input: &'a Input, + unicode: bool, + /// Matchers entered before giving up (the reference is exponential). + fuel: Cell, + memo: RefCell>>, +} + +/// Characters among which equivalents are searched: ASCII and Latin-1 and +/// Latin Extended, the places the test alphabet's case mappings live. +fn universe() -> impl Iterator { + (0u32..0x300) + .chain([0x17F, 0x1E9E, 0x212A, 0x2126, 0x3A3, 0x3C2, 0x3C3, 0x1FBE]) + // Greek and Cyrillic, where `Script` and `Script_Extensions` differ + // from what the case folding relates (U+0345 folds to U+03B9). + .chain(0x300..0x460) + .chain(0x10400..0x10450) +} + +fn is_lt(c: u32) -> bool { + matches!(c, 0x0A | 0x0D | 0x2028 | 0x2029) +} + +fn basic_word(c: u32) -> bool { + matches!(c, 0x30..=0x39 | 0x41..=0x5A | 0x61..=0x7A | 0x5F) +} + +fn is_space(c: u32) -> bool { + matches!( + c, + 0x09 | 0x0A | 0x0B | 0x0C | 0x0D | 0x20 | 0xA0 | 0xFEFF | 0x2028 | 0x2029 | 0x1680 | 0x2000 + ..=0x200A | 0x202F | 0x205F | 0x3000 + ) +} + +/// The count of left-capturing groups at or inside a term: their numbers. +fn caps_in_kind(k: &TermKind, out: &mut Vec) { + match k { + TermKind::Atom(Atom::Group(g)) => { + if let Some(i) = g.index { + out.push(i); + } + caps_in_disj(&g.body, out); + } + TermKind::Atom(Atom::Modified(m)) => caps_in_disj(&m.body, out), + TermKind::Look(l) => caps_in_disj(&l.body, out), + _ => {} + } +} + +fn caps_in_disj(d: &Disjunction, out: &mut Vec) { + for a in &d.alternatives { + for t in &a.terms { + caps_in_kind(&t.kind, out); + } + } +} + +impl Reference<'_> { + fn burn(&self) -> bool { + let f = self.fuel.get(); + if f == 0 { + return false; + } + self.fuel.set(f.saturating_sub(1)); + true + } + + fn exhausted(&self) -> bool { + self.fuel.get() == 0 + } + + fn canon(&self, c: u32) -> u32 { + canonicalize(c, self.unicode) + } + + /// Every `a` in the universe (and `ch`) with the same Canonicalize as `ch`. + fn equivalents(&self, ch: u32) -> Vec { + let cc = self.canon(ch); + let mut memo = self.memo.borrow_mut(); + let mut v = memo + .entry(cc) + .or_insert_with(|| universe().filter(|&a| self.canon(a) == cc).collect()) + .clone(); + if self.canon(ch) == cc && !v.contains(&ch) { + v.push(ch); + } + v + } + + fn is_word(&self, c: u32, fl: Fl) -> bool { + basic_word(c) || (self.unicode && fl.ic && basic_word(self.canon(c))) + } + + /// Disjunction. + fn disj(&self, d: &Disjunction, fl: Fl, fwd: bool, x: &State, c: Cont<'_>) -> Option { + if !self.burn() { + return None; + } + for alt in &d.alternatives { + if let Some(r) = self.seq(&alt.terms, fl, fwd, x, c) { + return Some(r); + } + } + None + } + + /// Alternative: the terms left to right, or right to left. + fn seq(&self, terms: &[Term], fl: Fl, fwd: bool, x: &State, c: Cont<'_>) -> Option { + let split = if fwd { + terms.split_first() + } else { + terms.split_last() + }; + match split { + None => c(x.clone()), + Some((t, rest)) => self.term(t, fl, fwd, x, &|y| self.seq(rest, fl, fwd, &y, c)), + } + } + + fn term(&self, t: &Term, fl: Fl, fwd: bool, x: &State, c: Cont<'_>) -> Option { + match &t.quantifier { + None => self.kind(&t.kind, fl, fwd, x, c), + Some(q) => { + let mut parens = Vec::new(); + caps_in_kind(&t.kind, &mut parens); + self.repeat(&t.kind, (q.min, q.max, q.greedy), &parens, (fl, fwd), x, c) + } + } + } + + /// RepeatMatcher (§22.2.2.3.2†), step by step. + fn repeat( + &self, + kind: &TermKind, + (min, max, greedy): (u64, Option, bool), + parens: &[u32], + (fl, fwd): (Fl, bool), + x: &State, + c: Cont<'_>, + ) -> Option { + if !self.burn() { + return None; + } + // 1. If max = 0, return c(x). + if max == Some(0) { + return c(x.clone()); + } + // 2. The continuation d. + let d = |y: State| -> Option { + // 2.a. If min = 0 and y's endIndex = x's endIndex, return failure. + if min == 0 && y.end == x.end { + return None; + } + let min2 = if min == 0 { 0 } else { min.saturating_sub(1) }; + let max2 = max.map(|m| m.saturating_sub(1)); + self.repeat(kind, (min2, max2, greedy), parens, (fl, fwd), &y, c) + }; + // 3-4. Clear the captures inside. + let mut xr = x.clone(); + for &i in parens { + if let Some(slot) = xr.caps.get_mut(i as usize) { + *slot = None; + } + } + // 5. If min != 0, return m(xr, d). + if min != 0 { + return self.kind(kind, fl, fwd, &xr, &d); + } + // 6. If greedy is false, try c(x) first. + if !greedy { + if let Some(z) = c(x.clone()) { + return Some(z); + } + return self.kind(kind, fl, fwd, &xr, &d); + } + // 7. Try m(xr, d), then c(x). + if let Some(z) = self.kind(kind, fl, fwd, &xr, &d) { + return Some(z); + } + c(x.clone()) + } + + fn kind(&self, k: &TermKind, fl: Fl, fwd: bool, x: &State, c: Cont<'_>) -> Option { + if !self.burn() { + return None; + } + let chars = &self.input.chars; + let at = |i: usize| chars.get(i).copied(); + match k { + TermKind::Assertion(a) => { + let e = x.end; + let before = e.checked_sub(1).and_then(at); + let after = at(e); + let ok = match a { + Assertion::Start => before.is_none_or(|ch| fl.ml && is_lt(ch)), + Assertion::End => after.is_none_or(|ch| fl.ml && is_lt(ch)), + Assertion::WordBoundary | Assertion::NotWordBoundary => { + let a1 = before.is_some_and(|ch| self.is_word(ch, fl)); + let b1 = after.is_some_and(|ch| self.is_word(ch, fl)); + (a1 != b1) == matches!(a, Assertion::WordBoundary) + } + }; + if ok { c(x.clone()) } else { None } + } + TermKind::Look(l) => { + let r = self.disj(&l.body, fl, !l.behind, x, &Some); + if l.negative { + if r.is_some() { None } else { c(x.clone()) } + } else { + let y = r?; + c(State { + end: x.end, + caps: y.caps, + }) + } + } + TermKind::Atom(a) => self.atom(a, fl, fwd, x, c), + } + } + + /// CharacterSetMatcher (§22.2.2.7.1†). + fn set( + &self, + pred: &dyn Fn(u32) -> bool, + invert: bool, + fl: Fl, + fwd: bool, + x: &State, + c: Cont<'_>, + ) -> Option { + let idx = if fwd { x.end } else { x.end.checked_sub(1)? }; + let ch = *self.input.chars.get(idx)?; + let found = if fl.ic { + self.equivalents(ch).into_iter().any(pred) + } else { + pred(ch) + }; + if found == invert { + return None; + } + let end = if fwd { x.end.saturating_add(1) } else { idx }; + c(State { + end, + caps: x.caps.clone(), + }) + } + + fn escape_has(&self, e: &ClassEscape, a: u32, fl: Fl) -> bool { + match e { + ClassEscape::Digit => (0x30..=0x39).contains(&a), + ClassEscape::NotDigit => !(0x30..=0x39).contains(&a), + ClassEscape::Space => is_space(a), + ClassEscape::NotSpace => !is_space(a), + ClassEscape::Word => self.is_word(a, fl), + ClassEscape::NotWord => !self.is_word(a, fl), + ClassEscape::Property { negated, property } => { + self.property_has(property, a) != *negated + } + } + } + + fn property_has(&self, p: &Property, a: u32) -> bool { + match p { + Property::GeneralCategory(v) => { + category_mask(v).is_some_and(|m| Prop::Category(m).contains(a)) + } + Property::Binary(n) => binary_property(n).is_some_and(|p| p.contains(a)), + Property::Script(v) => script_property(v, false).is_some_and(|p| p.contains(a)), + Property::ScriptExtensions(v) => { + script_property(v, true).is_some_and(|p| p.contains(a)) + } + // The members of one character (the longer ones are `has_multi`'s). + Property::Strings(n) => string_property(n).is_some_and(|s| s.chars.contains(a)), + } + } + + /// Whether the string `s` (not one character, already Canonicalized with + /// `i`) is an element of the set of an operand: CompileToCharSet + /// (§22.2.2.7†) with strings, read as a predicate on strings. + fn operand_has_multi(&self, o: &ClassSetOperand, s: &[u32], fl: Fl) -> bool { + let same = |t: &[u32]| { + t.len() == s.len() + && t.iter() + .zip(s) + .all(|(&a, &b)| if fl.ic { self.canon(a) == b } else { a == b }) + }; + match o { + ClassSetOperand::Char(_) => false, + ClassSetOperand::Escape(ClassEscape::Property { + property: Property::Strings(n), + .. + }) => string_property(n).is_some_and(|set| set.strings.iter().any(|t| same(t))), + ClassSetOperand::Escape(_) => false, + ClassSetOperand::Nested { negated, expr } => !*negated && self.has_multi(expr, s, fl), + ClassSetOperand::Strings(list) => list.iter().any(|t| same(t)), + } + } + + /// [`Self::operand_has_multi`] for an expression. + fn has_multi(&self, e: &ClassSetExpr, s: &[u32], fl: Fl) -> bool { + match e { + ClassSetExpr::Union(items) => items.iter().any(|it| match it { + ClassSetItem::Operand(o) => self.operand_has_multi(o, s, fl), + ClassSetItem::Range(..) => false, + }), + ClassSetExpr::Intersection(os) => os.iter().all(|o| self.operand_has_multi(o, s, fl)), + ClassSetExpr::Subtraction(os) => match os.split_first() { + Some((first, rest)) => { + self.operand_has_multi(first, s, fl) + && !rest.iter().any(|o| self.operand_has_multi(o, s, fl)) + } + None => false, + }, + } + } + + /// A `v` class that may contain strings, as CompileAtom (§22.2.2.7†) + /// builds it: the strings of the set that are not one character, + /// longest first; then the characters; then the empty string, if the set + /// has it. A string is tried as the input's slice of its length, which is + /// the only string of that length that can match there. + fn set_with_strings( + &self, + expr: &ClassSetExpr, + fl: Fl, + fwd: bool, + x: &State, + c: Cont<'_>, + ) -> Option { + let avail = if fwd { + self.input.chars.len().saturating_sub(x.end) + } else { + x.end + }; + for len in (2..=avail.min(32)).rev() { + if !self.burn() { + return None; + } + let slice = if fwd { + self.input.chars.get(x.end..x.end.saturating_add(len))? + } else { + self.input.chars.get(x.end.checked_sub(len)?..x.end)? + }; + let s: Vec = slice + .iter() + .map(|&ch| if fl.ic { self.canon(ch) } else { ch }) + .collect(); + if self.has_multi(expr, &s, fl) { + let end = if fwd { + x.end.saturating_add(len) + } else { + x.end.saturating_sub(len) + }; + if let Some(r) = c(State { + end, + caps: x.caps.clone(), + }) { + return Some(r); + } + } + } + if avail >= 1 { + let idx = if fwd { x.end } else { x.end.saturating_sub(1) }; + let ch = *self.input.chars.get(idx)?; + let cc = if fl.ic { self.canon(ch) } else { ch }; + if self.vset_has(expr, cc, fl) { + let end = if fwd { x.end.saturating_add(1) } else { idx }; + if let Some(r) = c(State { + end, + caps: x.caps.clone(), + }) { + return Some(r); + } + } + } + if self.has_multi(expr, &[], fl) { + return c(x.clone()); + } + None + } + + /// A positive escape (for `v`, where negation is the complement of the + /// folded set). + fn positive_escape_has(&self, e: &ClassEscape, a: u32, fl: Fl) -> bool { + match e { + ClassEscape::Digit | ClassEscape::NotDigit => (0x30..=0x39).contains(&a), + ClassEscape::Space | ClassEscape::NotSpace => is_space(a), + ClassEscape::Word | ClassEscape::NotWord => self.is_word(a, fl), + ClassEscape::Property { property, .. } => self.property_has(property, a), + } + } + + fn is_negated_escape(e: &ClassEscape) -> bool { + matches!( + e, + ClassEscape::NotDigit + | ClassEscape::NotSpace + | ClassEscape::NotWord + | ClassEscape::Property { negated: true, .. } + ) + } + + /// Membership in a `v` set, with the standard's folding: with `i`, the set + /// is the Canonicalize of each element, and `target` is the input + /// character's Canonicalize. + fn vset_has(&self, e: &ClassSetExpr, target: u32, fl: Fl) -> bool { + let same = |a: u32| { + if fl.ic { + self.canon(a) == target + } else { + a == target + } + }; + let operand = |o: &ClassSetOperand| -> bool { + match o { + ClassSetOperand::Char(c) => same(*c), + ClassSetOperand::Escape(esc) => { + let pos = self.exists(target, fl, |a| self.positive_escape_has(esc, a, fl)); + pos != Self::is_negated_escape(esc) + } + ClassSetOperand::Nested { negated, expr } => { + self.vset_has(expr, target, fl) != *negated + } + ClassSetOperand::Strings(list) => { + list.iter().any(|s| matches!(s.as_slice(), [c] if same(*c))) + } + } + }; + match e { + ClassSetExpr::Union(items) => items.iter().any(|it| match it { + ClassSetItem::Operand(o) => operand(o), + ClassSetItem::Range(lo, hi) => { + if fl.ic { + (*lo..=*hi).any(&same) + } else { + *lo <= target && target <= *hi + } + } + }), + ClassSetExpr::Intersection(os) => os.iter().all(operand), + ClassSetExpr::Subtraction(os) => match os.split_first() { + Some((first, rest)) => operand(first) && !rest.iter().any(operand), + None => false, + }, + } + } + + /// Whether some character that Canonicalizes to `target` (with `i`) or is + /// `target` (without) satisfies `f`. + fn exists(&self, target: u32, fl: Fl, f: impl Fn(u32) -> bool) -> bool { + if fl.ic { + self.equivalents(target).into_iter().any(f) + } else { + f(target) + } + } + + fn atom(&self, a: &Atom, fl: Fl, fwd: bool, x: &State, c: Cont<'_>) -> Option { + match a { + Atom::Char(ch) => self.set(&|a| a == *ch, false, fl, fwd, x, c), + Atom::Dot => { + let idx = if fwd { x.end } else { x.end.checked_sub(1)? }; + let ch = *self.input.chars.get(idx)?; + if !fl.dot_all && is_lt(ch) { + return None; + } + let end = if fwd { x.end.saturating_add(1) } else { idx }; + c(State { + end, + caps: x.caps.clone(), + }) + } + Atom::Escape(e) if e.may_contain_strings() => { + let expr = ClassSetExpr::Union(vec![ClassSetItem::Operand( + ClassSetOperand::Escape(e.clone()), + )]); + self.set_with_strings(&expr, fl, fwd, x, c) + } + Atom::Escape(e) => { + if self.v_mode() && Self::is_negated_escape(e) { + // `\P{..}` with `v`: the complement of the folded set. + let idx = if fwd { x.end } else { x.end.checked_sub(1)? }; + let ch = *self.input.chars.get(idx)?; + let cc = if fl.ic { self.canon(ch) } else { ch }; + let pos = self.exists(cc, fl, |a| self.positive_escape_has(e, a, fl)); + return if pos { + None + } else { + let end = if fwd { x.end.saturating_add(1) } else { idx }; + c(State { + end, + caps: x.caps.clone(), + }) + }; + } + self.set(&|a| self.escape_has(e, a, fl), false, fl, fwd, x, c) + } + Atom::Class(cls) => match &cls.contents { + ClassContents::Ranges(items) => { + let pred = |a: u32| { + items.iter().any(|it| match it { + ClassItem::Char(ch) => a == *ch, + ClassItem::Range(lo, hi) => *lo <= a && a <= *hi, + ClassItem::Escape(e) => self.escape_has(e, a, fl), + }) + }; + self.set(&pred, cls.negated, fl, fwd, x, c) + } + ClassContents::Set(expr) if !cls.negated && expr.may_contain_strings() => { + self.set_with_strings(expr, fl, fwd, x, c) + } + ClassContents::Set(expr) => { + let idx = if fwd { x.end } else { x.end.checked_sub(1)? }; + let ch = *self.input.chars.get(idx)?; + let cc = if fl.ic { self.canon(ch) } else { ch }; + let has = self.vset_has(expr, cc, fl) != cls.negated; + if !has { + return None; + } + let end = if fwd { x.end.saturating_add(1) } else { idx }; + c(State { + end, + caps: x.caps.clone(), + }) + } + }, + Atom::Group(g) => { + let after = |y: State| -> Option { + match g.index { + None => c(y), + Some(i) => { + let (s, e) = if fwd { (x.end, y.end) } else { (y.end, x.end) }; + let mut caps = y.caps.clone(); + if let Some(slot) = caps.get_mut(i as usize) { + *slot = Some((s, e)); + } + c(State { end: y.end, caps }) + } + } + }; + self.disj(&g.body, fl, fwd, x, &after) + } + Atom::Modified(m) => { + let flag = |cur: bool, add: bool, remove: bool| (cur || add) && !remove; + let inner = Fl { + ic: flag(fl.ic, m.add.ignore_case, m.remove.ignore_case), + ml: flag(fl.ml, m.add.multiline, m.remove.multiline), + dot_all: flag(fl.dot_all, m.add.dot_all, m.remove.dot_all), + }; + self.disj(&m.body, inner, fwd, x, c) + } + Atom::BackReference(n) => self.backref(&[*n], fl, fwd, x, c), + Atom::NamedBackReference(name) => { + let ns: Vec = self + .names() + .iter() + .filter(|(n, _)| n == name) + .map(|&(_, i)| i) + .collect(); + self.backref(&ns, fl, fwd, x, c) + } + } + } + + /// BackreferenceMatcher (§22.2.2.7.2†), over the first group of `ns` that + /// has a capture. + fn backref(&self, ns: &[u32], fl: Fl, fwd: bool, x: &State, c: Cont<'_>) -> Option { + let r = ns + .iter() + .find_map(|&n| x.caps.get(n as usize).copied().flatten()); + let Some((rs, re)) = r else { + return c(x.clone()); + }; + let len = re.checked_sub(rs)?; + let e = x.end; + let f = if fwd { + e.checked_add(len)? + } else { + e.checked_sub(len)? + }; + if f > self.input.chars.len() { + return None; + } + let g = e.min(f); + for i in 0..len { + let a = *self.input.chars.get(rs.checked_add(i)?)?; + let b = *self.input.chars.get(g.checked_add(i)?)?; + let (a, b) = if fl.ic { + (self.canon(a), self.canon(b)) + } else { + (a, b) + }; + if a != b { + return None; + } + } + c(State { + end: f, + caps: x.caps.clone(), + }) + } + + fn v_mode(&self) -> bool { + V_MODE.with(Cell::get) + } + + fn names(&self) -> Vec<(Vec, u32)> { + NAMES.with(|n| n.borrow().clone()) + } +} + +thread_local! { + static V_MODE: Cell = const { Cell::new(false) }; + static NAMES: RefCell, u32)>> = const { RefCell::new(Vec::new()) }; +} + +/// The reference's answer for a match starting at character `start`: +/// the captures in code unit indices, or `Err` if it ran out of fuel. +fn reference_at( + pattern: &Pattern, + flags: Flags, + input: &Input, + start: usize, +) -> Result, ()> { + V_MODE.with(|v| v.set(flags.unicode_sets)); + NAMES.with(|n| *n.borrow_mut() = pattern.group_names.clone()); + let r = Reference { + input, + unicode: flags.unicode_mode(), + fuel: Cell::new(300_000), + memo: RefCell::new(HashMap::new()), + }; + let fl = Fl { + ic: flags.ignore_case, + ml: flags.multiline, + dot_all: flags.dot_all, + }; + let x = State { + end: start, + caps: vec![None; (pattern.capture_count as usize).saturating_add(1)], + }; + let res = r.disj(&pattern.disjunction, fl, true, &x, &Some); + if r.exhausted() { + return Err(()); + } + let to_unit = |i: usize| input.pos.get(i).copied().unwrap_or(usize::MAX); + Ok(res.map(|st| { + let mut caps: Caps = st + .caps + .iter() + .map(|c| c.map(|(s, e)| (to_unit(s), to_unit(e)))) + .collect(); + if let Some(first) = caps.first_mut() { + *first = Some((to_unit(start), to_unit(st.end))); + } + caps + })) +} + +// --------------------------------------------------------------------------- +// Random patterns and inputs. + +struct Rng(u64); + +impl Rng { + fn next(&mut self) -> u64 { + // xorshift64* + let mut x = self.0; + x ^= x.wrapping_shr(12); + x ^= x.wrapping_shl(25); + x ^= x.wrapping_shr(27); + self.0 = x; + x.wrapping_mul(0x2545_F491_4F6C_DD1D) + } + + fn below(&mut self, n: usize) -> usize { + let n = u64::try_from(n).unwrap_or(1).max(1); + usize::try_from(self.next().checked_rem(n).unwrap_or(0)).unwrap_or(0) + } + + fn chance(&mut self, percent: usize) -> bool { + self.below(100) < percent + } + + fn pick<'a, T>(&mut self, list: &'a [T]) -> Option<&'a T> { + list.get(self.below(list.len())) + } +} + +const LETTERS: &[&str] = &["a", "b", "c", "a", "b", "A", "B"]; +const SPECIAL: &[&str] = &[ + "k", "K", "\u{212a}", "s", "S", "\u{17f}", "\u{e9}", "\u{c9}", "1", "_", " ", "-", +]; +const ASTRAL: &[&str] = &["\u{1F600}", "\u{10400}", "\u{10428}"]; + +struct Gen<'r> { + rng: &'r mut Rng, + unicode: bool, + v: bool, + groups: u32, + names: u32, +} + +impl Gen<'_> { + fn s(&mut self, list: &[&str]) -> String { + self.rng.pick(list).copied().unwrap_or("a").to_string() + } + + fn literal(&mut self) -> String { + let r = self.rng.below(100); + if r < 70 { + self.s(LETTERS) + } else if r < 92 || !self.unicode { + self.s(SPECIAL) + } else { + self.s(ASTRAL) + } + } + + fn disjunction(&mut self, depth: u32) -> String { + let n = if self.rng.chance(35) { + self.rng.below(2).saturating_add(2) + } else { + 1 + }; + let alts: Vec = (0..n).map(|_| self.alternative(depth)).collect(); + alts.join("|") + } + + fn alternative(&mut self, depth: u32) -> String { + let n = self.rng.below(4); + (0..n).map(|_| self.term(depth)).collect() + } + + fn quantifier(&mut self) -> String { + let q = self + .rng + .pick(&[ + "*", "+", "?", "{1,2}", "{0,2}", "{2}", "{2,}", "{0,1}", "{1}", + ]) + .copied() + .unwrap_or("*"); + let lazy = if self.rng.chance(30) { "?" } else { "" }; + format!("{q}{lazy}") + } + + fn term(&mut self, depth: u32) -> String { + let r = self.rng.below(100); + let (text, quantifiable) = if r < 36 { + (self.literal(), true) + } else if r < 44 { + (".".to_string(), true) + } else if r < 56 { + (self.class(), true) + } else if r < 62 { + (self.escape(), true) + } else if r < 78 && depth < 3 { + (self.group(depth), true) + } else if r < 85 && depth < 3 { + self.look(depth) + } else if r < 92 { + (self.s(&["^", "$", "\\b", "\\B"]), false) + } else { + (self.backref(), true) + }; + if quantifiable && self.rng.chance(35) { + format!("{text}{}", self.quantifier()) + } else { + text + } + } + + fn escape(&mut self) -> String { + let mut list: Vec<&str> = vec!["\\d", "\\w", "\\s", "\\D", "\\W", "\\S"]; + if self.unicode { + list.extend([ + "\\p{Lu}", + "\\P{Lu}", + "\\p{L}", + "\\p{Ll}", + "\\p{sc=Latin}", + "\\P{sc=Greek}", + "\\p{scx=Grek}", + "\\P{scx=Greek}", + "\\p{Script=Cyrillic}", + "\\p{scx=Latn}", + "\\p{sc=Zinh}", + "\\p{Lowercase}", + ]); + } + if self.v { + list.extend([ + "\\p{RGI_Emoji}", + "\\p{Emoji_Keycap_Sequence}", + "\\p{Basic_Emoji}", + "\\p{RGI_Emoji_Flag_Sequence}", + "\\p{RGI_Emoji_ZWJ_Sequence}", + ]); + } + self.s(&list) + } + + /// `\q{..}` (`v`): strings of several lengths, some empty. + fn v_string(&mut self) -> String { + self.s(&[ + "\\q{ab|c}", + "\\q{ab|abc|a}", + "\\q{ba|}", + "\\q{\u{1F600}a|b}", + "\\q{A\u{3b9}|\u{345}}", + ]) + } + + fn backref(&mut self) -> String { + if self.names > 0 && self.rng.chance(40) { + let n = self.rng.below(self.names as usize).saturating_add(1); + format!("\\k") + } else { + format!("\\{}", self.rng.below(3).saturating_add(1)) + } + } + + fn group(&mut self, depth: u32) -> String { + let r = self.rng.below(100); + if r < 45 { + self.groups = self.groups.saturating_add(1); + format!("({})", self.disjunction(depth.saturating_add(1))) + } else if r < 62 { + self.names = self.names.saturating_add(1); + self.groups = self.groups.saturating_add(1); + let n = self.names; + format!("(?{})", self.disjunction(depth.saturating_add(1))) + } else if r < 88 { + format!("(?:{})", self.disjunction(depth.saturating_add(1))) + } else { + let m = self + .rng + .pick(&["i", "-i", "s", "-s", "m", "-m", "i-s", "s-i", "ms"]) + .copied() + .unwrap_or("i"); + format!("(?{m}:{})", self.disjunction(depth.saturating_add(1))) + } + } + + fn look(&mut self, depth: u32) -> (String, bool) { + let kind = self + .rng + .pick(&["=", "!", "<=", " String { + self.s(&["a", "b", "c", "A", "B", "k", "s", "1", "_", "\u{e9}"]) + } + + fn class(&mut self) -> String { + let neg = if self.rng.chance(25) { "^" } else { "" }; + if self.v { + return format!("[{neg}{}]", self.v_contents(0)); + } + let n = self.rng.below(3).saturating_add(1); + let items: String = (0..n) + .map(|_| { + let r = self.rng.below(100); + if r < 50 { + self.class_char() + } else if r < 80 { + self.s(&["a-c", "b-b", "A-C", "0-9", "a-z", "A-Z", "\u{c0}-\u{ff}"]) + } else { + self.escape() + } + }) + .collect(); + format!("[{neg}{items}]") + } + + /// The contents of a `v` class: a union, an intersection or a subtraction. + fn v_contents(&mut self, depth: u32) -> String { + let r = self.rng.below(100); + if r < 55 || depth >= 2 { + let n = self.rng.below(3).saturating_add(1); + (0..n) + .map(|_| { + let r = self.rng.below(100); + if r < 40 { + self.class_char() + } else if r < 65 { + self.s(&["a-c", "A-C", "0-9", "a-z"]) + } else if r < 74 { + self.escape() + } else if r < 82 { + self.v_string() + } else { + format!("[{}]", self.v_contents(depth.saturating_add(1))) + } + }) + .collect() + } else { + let op = if r < 78 { "&&" } else { "--" }; + let n = self.rng.below(2).saturating_add(2); + let operands: Vec = (0..n) + .map(|_| { + let r = self.rng.below(100); + if r < 40 { + format!("[{}]", self.v_contents(depth.saturating_add(1))) + } else if r < 55 { + self.escape() + } else if r < 65 { + self.v_string() + } else if r < 80 { + format!("[^{}]", self.class_char()) + } else { + self.class_char() + } + }) + .collect(); + operands.join(op) + } + } +} + +const INPUT_CHARS: &[&str] = &[ + "a", + "b", + "c", + "a", + "b", + "A", + "B", + "k", + "K", + "\u{212a}", + "s", + "S", + "\u{17f}", + "\u{e9}", + "\u{c9}", + "1", + "_", + " ", + "\n", + "-", + "\u{1F600}", + "\u{3b1}", + "\u{3b9}", + "\u{399}", + "\u{345}", + "\u{b7}", + "\u{430}", + "\u{fe0f}", + "1\u{fe0f}\u{20e3}", + "\u{1F1E6}\u{1F1E8}", + "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}", + "\u{263a}\u{fe0f}", + "\u{10400}", + "\u{10428}", +]; + +fn random_input(rng: &mut Rng, max_len: usize) -> String { + let n = rng.below(max_len.saturating_add(1)); + (0..n) + .map(|_| rng.pick(INPUT_CHARS).copied().unwrap_or("a")) + .collect() +} + +/// The three kinds of mode, so each gets a share of the patterns. +fn random_flags(rng: &mut Rng) -> (String, Flags) { + let mut s = String::new(); + for (flag, pct) in [('i', 50), ('m', 30), ('s', 30)] { + if rng.chance(pct) { + s.push(flag); + } + } + match rng.below(10) { + 0..=2 => s.push('u'), + 3 | 4 => s.push('v'), + _ => {} + } + let units: Vec = s.encode_utf16().collect(); + let f = Flags::parse(&units).unwrap_or_default(); + (s, f) +} + +fn units(s: &str) -> Vec { + s.encode_utf16().collect() +} + +struct Tally { + patterns: usize, + parse_errors: usize, + compile_errors: usize, + compared: usize, + matched: usize, + out_of_fuel: usize, +} + +/// A pattern under test. +struct Subject<'a> { + pattern: &'a str, + flag_text: &'a str, + flags: Flags, + pat: &'a Pattern, + prog: &'a Program, +} + +fn compare_one( + subject: &Subject<'_>, + input_text: &str, + tally: &mut Tally, + rng: &mut Rng, +) -> Result<(), String> { + let Subject { + pattern, + flag_text, + flags, + pat, + prog, + } = *subject; + let text = units(input_text); + let input = decode(&text, flags.unicode_mode()); + // The standard scan: the first start index (in characters) that matches. + let mut expected: Option = None; + for k in 0..=input.chars.len() { + match reference_at(pat, flags, &input, k) { + Ok(Some(c)) => { + expected = Some(c); + break; + } + Ok(None) => {} + Err(()) => { + tally.out_of_fuel = tally.out_of_fuel.saturating_add(1); + return Ok(()); + } + } + } + let limits = Limits { + max_stack: 1 << 20, + max_steps: Some(50_000_000), + }; + let got = prog + .exec(&text, 0, false, &limits, None) + .map_err(|e| format!("/{pattern}/{flag_text} on {input_text:?}: {e}"))?; + tally.compared = tally.compared.saturating_add(1); + if got.is_some() { + tally.matched = tally.matched.saturating_add(1); + } + if got.map(|m| m.captures) != expected { + return Err(format!( + "/{pattern}/{flag_text} on {input_text:?} (scan): got {:?}, reference {expected:?}", + prog.exec(&text, 0, false, &limits, None) + .ok() + .flatten() + .map(|m| m.captures) + )); + } + // A sticky match at one start index, chosen at random among the + // characters' boundaries. + let k = rng.below(input.chars.len().saturating_add(1)); + let start = input.pos.get(k).copied().unwrap_or(0); + let want = match reference_at(pat, flags, &input, k) { + Ok(c) => c, + Err(()) => return Ok(()), + }; + let got = prog + .exec(&text, start, true, &limits, None) + .map_err(|e| format!("/{pattern}/{flag_text} on {input_text:?} at {start}: {e}"))?; + if got.map(|m| m.captures) != want { + return Err(format!( + "/{pattern}/{flag_text} on {input_text:?} sticky at {start}: differs from the reference ({want:?})" + )); + } + Ok(()) +} + +fn run_differential( + seed: u64, + patterns: usize, + inputs: usize, + max_len: usize, +) -> Result { + let mut rng = Rng(seed); + let mut tally = Tally { + patterns: 0, + parse_errors: 0, + compile_errors: 0, + compared: 0, + matched: 0, + out_of_fuel: 0, + }; + for _ in 0..patterns { + let (flag_text, flags) = random_flags(&mut rng); + let text = { + let mut g = Gen { + rng: &mut rng, + unicode: flags.unicode_mode(), + v: flags.unicode_sets, + groups: 0, + names: 0, + }; + g.disjunction(0) + }; + tally.patterns = tally.patterns.saturating_add(1); + let Ok(pat) = parse(&units(&text), flags) else { + tally.parse_errors = tally.parse_errors.saturating_add(1); + continue; + }; + let Ok(prog) = compile(&pat, flags) else { + tally.compile_errors = tally.compile_errors.saturating_add(1); + continue; + }; + for _ in 0..inputs { + let input = random_input(&mut rng, max_len); + let subject = Subject { + pattern: &text, + flag_text: &flag_text, + flags, + pat: &pat, + prog: &prog, + }; + compare_one(&subject, &input, &mut tally, &mut rng)?; + } + } + Ok(tally) +} + +#[test] +fn matcher_agrees_with_the_reference() -> Result<(), String> { + let (patterns, inputs) = if cfg!(miri) { (10, 2) } else { (6000, 8) }; + let mut total = Tally { + patterns: 0, + parse_errors: 0, + compile_errors: 0, + compared: 0, + matched: 0, + out_of_fuel: 0, + }; + for seed in [0x9E37_79B9_7F4A_7C15u64, 0xD1B5_4A32_D192_ED03] { + let t = run_differential(seed, patterns, inputs, 7)?; + total.patterns = total.patterns.saturating_add(t.patterns); + total.parse_errors = total.parse_errors.saturating_add(t.parse_errors); + total.compile_errors = total.compile_errors.saturating_add(t.compile_errors); + total.compared = total.compared.saturating_add(t.compared); + total.matched = total.matched.saturating_add(t.matched); + total.out_of_fuel = total.out_of_fuel.saturating_add(t.out_of_fuel); + } + println!( + "clazel-conformance: suite=\"differential vs reference matcher\" pass={} xfail=0 fail=0 total={}", + total.compared, total.compared + ); + println!( + "differential: {} patterns, {} parse errors, {} compile errors, {} comparisons ({} with a match), {} out of fuel", + total.patterns, + total.parse_errors, + total.compile_errors, + total.compared, + total.matched, + total.out_of_fuel + ); + // The test must not be vacuous. + assert!(total.compared > total.patterns / 2, "too few comparisons"); + assert!( + total.matched.saturating_mul(10) > total.compared, + "fewer than a tenth of the comparisons found a match" + ); + assert!(total.out_of_fuel.saturating_mul(20) < total.compared); + Ok(()) +} + +/// Patterns that put `Script`, `Script_Extensions` and the properties of +/// strings where the order of the alternatives or the folding matters, run on +/// every input of up to a few tokens from a small alphabet. The random +/// differential reaches them only by chance. +#[test] +fn properties_and_strings_agree_with_the_reference() -> Result<(), String> { + // (pattern, flags) + let cases: &[(&str, &str)] = &[ + ("\\p{sc=Greek}+", "u"), + ("\\P{sc=Greek}", "u"), + ("\\P{sc=Greek}", "iu"), + ("\\P{sc=Greek}", "iv"), + ("\\p{scx=Greek}+", "u"), + ("\\P{scx=Greek}", "iu"), + ("\\p{scx=Grek}", "iv"), + ("\\p{sc=Inherited}", "u"), + ("\\p{sc=Common}\\p{scx=Latin}?", "u"), + ("[\\p{sc=Greek}--\\p{Lu}]+", "v"), + ("[\\p{sc=Greek}&&\\p{Lu}]", "iv"), + ("[^\\p{scx=Greek}]", "iv"), + ("[\\q{ab|a}]b", "v"), + ("[\\q{a|ab|abc}]c", "v"), + ("^(?:[\\q{ab|a}]|b)(b*)$", "v"), + ("[\\q{ab|a|}]*c", "v"), + ("(?<=[\\q{ab|b}])c", "v"), + ("[\\q{AB|a}]b", "iv"), + ("[\\q{ab|abc|a}--\\q{ab}]c?", "v"), + ("[\\q{ab|abc}&&\\q{abc|a}]", "v"), + ("\\p{Emoji_Keycap_Sequence}", "v"), + ("\\p{Emoji_Keycap_Sequence}\\p{Emoji_Keycap_Sequence}?", "v"), + ("(\\p{Emoji_Keycap_Sequence}|1)\\u{FE0F}", "v"), + ("\\p{RGI_Emoji}\\u{200D}", "v"), + ("\\p{RGI_Emoji}+$", "v"), + ("^\\p{RGI_Emoji}$", "iv"), + ("(?<=\\p{RGI_Emoji})x", "v"), + ("\\p{Basic_Emoji}\\p{Basic_Emoji}?", "v"), + ("[\\p{RGI_Emoji}--\\q{\u{1F1E6}\u{1F1E8}}]", "v"), + ( + "[\\p{RGI_Emoji_Flag_Sequence}&&\\q{\u{1F1E6}\u{1F1E8}|a}]", + "v", + ), + ("[\\p{Basic_Emoji}--\\p{Emoji_Keycap_Sequence}]", "v"), + ("\\p{RGI_Emoji_ZWJ_Sequence}", "v"), + ("\\p{RGI_Emoji}{2}", "v"), + ]; + let alphabet: &[&str] = &[ + "a", + "b", + "A", + "c", + "x", + "1", + "\u{3b9}", + "\u{399}", + "\u{345}", + "\u{b7}", + "\u{fe0f}", + "\u{200d}", + "1\u{fe0f}\u{20e3}", + "\u{1F1E6}\u{1F1E8}", + "\u{1F468}", + "\u{1F468}\u{200D}\u{1F469}", + "\u{1F600}", + "\u{263a}\u{fe0f}", + ]; + let len = if cfg!(miri) { 0 } else { 3 }; + let mut rng = Rng(0x2545_F491_4F6C_DD1D); + let mut tally = Tally { + patterns: 0, + parse_errors: 0, + compile_errors: 0, + compared: 0, + matched: 0, + out_of_fuel: 0, + }; + // Every sequence of up to `len` tokens. + let mut inputs: Vec<(usize, String)> = vec![(0, String::new())]; + // And the ones that make the alternatives' order show: more than one + // string of the set matches at a start, and they end differently. + for s in [ + "abb", + "abc", + "abcc", + "abbc", + "aabbc", + "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}x", + "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}", + "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F466}\u{200D}\u{1F466}", + "1\u{fe0f}\u{20e3}1\u{fe0f}\u{20e3}x", + "\u{1F1E6}\u{1F1E8}\u{1F1E6}\u{1F1E8}a", + "\u{263a}\u{fe0f}\u{263a}\u{fe0f}", + "A\u{3b9}\u{345}\u{399}", + ] { + inputs.push((0, s.to_string())); + } + let mut layer: Vec = vec![String::new()]; + for n in 1..=len { + let mut next = Vec::new(); + for p in &layer { + for t in alphabet { + next.push(format!("{p}{t}")); + } + } + inputs.extend(next.iter().map(|s| (n, s.clone()))); + layer = next; + } + let mut idle = Vec::new(); + for (i, &(pattern, flag_text)) in cases.iter().enumerate() { + // Under Miri every fourth case, and not those that search the big + // string tables (about 2 800 strings): the run must stay short. + if cfg!(miri) + && (i % 4 != 0 || pattern.contains("RGI_Emoji") || pattern.contains("Basic_Emoji")) + { + continue; + } + let flags = Flags::parse(&units(flag_text)).map_err(|e| format!("{e:?}"))?; + let pat = parse(&units(pattern), flags).map_err(|e| format!("/{pattern}/: {e}"))?; + let prog = compile(&pat, flags).map_err(|e| format!("/{pattern}/: {e}"))?; + let before = tally.matched; + for (n, input) in &inputs { + // The longest inputs are many: take a twenty-fifth of them. + if *n > 2 && rng.below(25) != 0 { + continue; + } + let subject = Subject { + pattern, + flag_text, + flags, + pat: &pat, + prog: &prog, + }; + compare_one(&subject, input, &mut tally, &mut rng)?; + } + if tally.matched == before { + idle.push(format!("/{pattern}/{flag_text} matched no input")); + } + } + println!( + "clazel-conformance: suite=\"differential: scripts and strings\" pass={} xfail=0 fail=0 total={}", + tally.compared, tally.compared + ); + // Every pattern must match something, or it tests nothing. + assert!(idle.is_empty(), "{}", idle.join("\n")); + assert!(tally.out_of_fuel.saturating_mul(20) < tally.compared); + Ok(()) +} diff --git a/crates/clazel-js-regexp/tests/limits.rs b/crates/clazel-js-regexp/tests/limits.rs new file mode 100644 index 000000000..f8b14d792 --- /dev/null +++ b/crates/clazel-js-regexp/tests/limits.rs @@ -0,0 +1,203 @@ +//! The matcher's limits: the bounded backtrack stack, the step budget and the +//! interrupt hook (phase-7.md items 7.10, 7.0(g) and 7.19), on catastrophic +//! patterns. A catastrophic pattern runs until a limit says stop, and ends in +//! an error value, never in a crash or an unbounded allocation. + +use clazel_js_regexp::{Flags, Limits, MatchError, Program, compile, parse}; + +fn units(s: &str) -> Vec { + s.encode_utf16().collect() +} + +fn build(pattern: &str, flags: &str) -> Result { + let f = Flags::parse(&units(flags)).map_err(|e| format!("{e:?}"))?; + let p = parse(&units(pattern), f).map_err(|e| e.to_string())?; + compile(&p, f).map_err(|e| e.to_string()) +} + +fn is_match( + pattern: &str, + input: &[u16], + limits: &Limits, +) -> Result, String> { + let prog = build(pattern, "")?; + Ok(prog + .exec(input, 0, false, limits, None) + .map(|m| m.is_some())) +} + +/// How big the pathological inputs are: Miri is about 100 times slower. +fn scale(native: usize, miri: usize) -> usize { + if cfg!(miri) { miri } else { native } +} + +#[test] +fn nested_quantifier_hits_the_step_limit() -> Result<(), String> { + // (a*)*b on a run of a's: exponentially many ways to split the run, and + // the empty check keeps the stack shallow, so what stops it is the budget. + let input = units(&"a".repeat(40)); + let limits = Limits { + max_stack: 1 << 20, + max_steps: Some(scale(5_000_000, 20_000) as u64), + }; + assert_eq!( + is_match("(a*)*b", &input, &limits)?, + Err(MatchError::StepLimit) + ); + assert_eq!( + is_match("(a+)+b", &input, &limits)?, + Err(MatchError::StepLimit) + ); + assert_eq!( + is_match("(a|a)*b", &input, &limits)?, + Err(MatchError::StepLimit) + ); + Ok(()) +} + +#[test] +fn small_catastrophic_input_still_finishes() -> Result<(), String> { + // Without a limit the same pattern is just slow: 2^n paths, so n = 14 + // finishes at once and has no match. + let n = scale(14, 6); + let input = units(&"a".repeat(n)); + assert_eq!(is_match("(a*)*b", &input, &Limits::default())?, Ok(false)); + let with_b = units(&format!("{}b", "a".repeat(n))); + assert_eq!(is_match("(a*)*b", &with_b, &Limits::default())?, Ok(true)); + Ok(()) +} + +#[test] +fn deep_backtracking_hits_the_stack_bound() -> Result<(), String> { + // (?:a|aa)*b on a long run of a's with no b: every iteration leaves a + // choice behind, so the first descent is as deep as the input is long. The + // stack is bounded, so it ends in StackOverflow, with memory never + // beyond the bound. + let n = scale(400_000, 2_000); + let input = units(&"a".repeat(n)); + let limits = Limits { + max_stack: scale(1 << 20, 1 << 10), + max_steps: None, + }; + assert_eq!( + is_match("(?:a|aa)*b", &input, &limits)?, + Err(MatchError::StackOverflow) + ); + assert_eq!( + is_match("(?:a|b)*c", &input, &limits)?, + Err(MatchError::StackOverflow) + ); + // The same bound is what a lookahead in a loop meets. + assert_eq!( + is_match("(?:(?=a)a|b)*c", &input, &limits)?, + Err(MatchError::StackOverflow) + ); + Ok(()) +} + +#[test] +fn the_stack_bound_is_exact() -> Result<(), String> { + // `(?:a|b)*c` needs a number of entries that grows with the input; with a + // bound just above what it needs it matches, and with half it overflows. + let input = units(&format!("{}c", "ab".repeat(50))); + let prog = build("(?:a|b)*c", "")?; + let run = |max_stack: usize| { + prog.exec( + &input, + 0, + true, + &Limits { + max_stack, + max_steps: None, + }, + None, + ) + .map(|m| m.is_some()) + }; + assert_eq!(run(1 << 20), Ok(true)); + let mut need = None; + for bound in 1..2000 { + if run(bound) == Ok(true) { + need = Some(bound); + break; + } + } + let need = need.ok_or("never matched")?; + assert_eq!(run(need.saturating_sub(1)), Err(MatchError::StackOverflow)); + // 100 characters, a handful of entries each. + assert!(need > 100 && need < 1000, "{need}"); + Ok(()) +} + +#[test] +fn a_zero_bound_overflows_at_the_first_choice() -> Result<(), String> { + let input = units("ab"); + let limits = Limits { + max_stack: 0, + max_steps: None, + }; + // No choice point, no entry: a plain literal needs no stack. + assert_eq!(is_match("ab", &input, &limits)?, Ok(true)); + assert_eq!( + is_match("a|b", &input, &limits)?, + Err(MatchError::StackOverflow) + ); + Ok(()) +} + +#[test] +fn the_interrupt_hook_is_asked_and_can_stop_the_match() -> Result<(), String> { + let input = units(&"a".repeat(40)); + let prog = build("(a*)*b", "")?; + let mut asked = 0u32; + let mut hook = || { + asked = asked.saturating_add(1); + asked >= 3 + }; + let r = prog.exec(&input, 0, false, &Limits::default(), Some(&mut hook)); + assert_eq!(r, Err(MatchError::Interrupted)); + assert_eq!(asked, 3); + + // A hook that never says stop does not change the result. + let short = units("aaab"); + let prog = build("a+b", "")?; + let mut calls = 0u32; + let mut hook = || { + calls = calls.saturating_add(1); + false + }; + let r = prog + .exec(&short, 0, false, &Limits::default(), Some(&mut hook)) + .map_err(|e| e.to_string())?; + assert!(r.is_some()); + Ok(()) +} + +#[test] +fn errors_leave_nothing_behind() -> Result<(), String> { + // The same program is reusable after an error, and gives the same answers. + let prog = build("(?:a|aa)*b", "")?; + let long = units(&"a".repeat(scale(100_000, 2_000))); + let tight = Limits { + max_stack: 1000, + max_steps: None, + }; + assert_eq!( + prog.exec(&long, 0, false, &tight, None), + Err(MatchError::StackOverflow) + ); + let short = units("aab"); + let m = prog + .exec(&short, 0, false, &Limits::default(), None) + .map_err(|e| e.to_string())? + .ok_or("no match")?; + assert_eq!(m.get(0), Some((0, 3))); + Ok(()) +} + +#[test] +fn error_messages_name_the_limit() { + assert!(MatchError::StackOverflow.to_string().contains("stack")); + assert!(MatchError::StepLimit.to_string().contains("step")); + assert!(MatchError::Interrupted.to_string().contains("interrupted")); +} diff --git a/crates/clazel-js-regexp/tests/matching.rs b/crates/clazel-js-regexp/tests/matching.rs new file mode 100644 index 000000000..621e78007 --- /dev/null +++ b/crates/clazel-js-regexp/tests/matching.rs @@ -0,0 +1,898 @@ +//! The matcher on hand-written patterns and inputs (ECMA-262 §22.2.2†): each +//! feature of the semantics, with the result a conforming engine gives. Every +//! expected value is what the standard's algorithm yields (several are the +//! worked examples of the standard's own text). The differential against a +//! reference matcher is in `differential.rs`; limits and catastrophic patterns +//! are in `limits.rs`. + +use clazel_js_regexp::{CompileError, Flags, Limits, Match, Program, compile, parse}; + +fn units(s: &str) -> Vec { + s.encode_utf16().collect() +} + +fn build(pattern: &str, flags: &str) -> Result { + let f = Flags::parse(&units(flags)).map_err(|e| format!("flags {flags:?}: {e:?}"))?; + let p = parse(&units(pattern), f).map_err(|e| format!("/{pattern}/{flags}: {e}"))?; + compile(&p, f).map_err(|e| format!("/{pattern}/{flags}: {e}")) +} + +/// The captures of the first match as text (`None` for a group that did not +/// take part), with `lastIndex` 0 and `y` taken from the flags. +fn exec_at( + pattern: &str, + flags: &str, + input: &str, + start: usize, +) -> Result>>, String> { + let prog = build(pattern, flags)?; + let sticky = flags.contains('y'); + let text = units(input); + let m = prog + .exec(&text, start, sticky, &Limits::default(), None) + .map_err(|e| format!("/{pattern}/{flags} on {input:?}: {e}"))?; + Ok(m.map(|m| texts(&m, &text))) +} + +fn texts(m: &Match, text: &[u16]) -> Vec> { + m.captures + .iter() + .map(|c| c.and_then(|(a, b)| text.get(a..b).map(String::from_utf16_lossy))) + .collect() +} + +fn exec(pattern: &str, flags: &str, input: &str) -> Result>>, String> { + exec_at(pattern, flags, input, 0) +} + +const N: Option<&str> = None; + +/// `(pattern, flags, input, expected)`; `expected` is the whole match and +/// then each group, or `None` for no match. +type Case = ( + &'static str, + &'static str, + &'static str, + Option<&'static [Option<&'static str>]>, +); + +fn run(suite: &str, cases: &[Case]) -> Result<(), String> { + let mut bad = Vec::new(); + for &(pattern, flags, input, want) in cases { + let got = exec(pattern, flags, input)?; + let want: Option>> = + want.map(|w| w.iter().map(|x| x.map(str::to_string)).collect()); + if got != want { + bad.push(format!( + "/{pattern}/{flags}.exec({input:?}): got {got:?}, want {want:?}" + )); + } + } + println!( + "clazel-conformance: suite=\"{suite}\" pass={} xfail=0 fail={} total={}", + cases.len().saturating_sub(bad.len()), + bad.len(), + cases.len() + ); + if bad.is_empty() { + Ok(()) + } else { + Err(bad.join("\n")) + } +} + +#[test] +fn literals_alternation_and_quantifiers() -> Result<(), String> { + run( + "matcher: literals, alternation, quantifiers", + &[ + ("abc", "", "xxabcxx", Some(&[Some("abc")])), + ("abc", "", "ab", None), + ("", "", "abc", Some(&[Some("")])), + ("a|ab", "", "abc", Some(&[Some("a")])), + ("(a)|b", "", "b", Some(&[Some("b"), N])), + ("(a)|(b)", "", "b", Some(&[Some("b"), N, Some("b")])), + ("a*", "", "aaa", Some(&[Some("aaa")])), + ("a*?", "", "aaa", Some(&[Some("")])), + ("a+?", "", "aaa", Some(&[Some("a")])), + ("a?", "", "b", Some(&[Some("")])), + ("a{2,3}", "", "aaaa", Some(&[Some("aaa")])), + ("a{2,3}?", "", "aaaa", Some(&[Some("aa")])), + ("a{2}", "", "a", None), + ("a{2,}", "", "aaaa", Some(&[Some("aaaa")])), + ("a{0}b", "", "ab", Some(&[Some("b")])), + ("(?:ab){2}", "", "ababab", Some(&[Some("abab")])), + ("(?:ab){2,}?", "", "ababab", Some(&[Some("abab")])), + ( + "(a|ab)(c|bcd)(d*)", + "", + "abcd", + Some(&[Some("abcd"), Some("a"), Some("bcd"), Some("")]), + ), + ("a+b", "", "aaab", Some(&[Some("aaab")])), + ("a+?b", "", "aaab", Some(&[Some("aaab")])), + ("a*b", "", "aaac", None), + ("x*y+$", "", "xxyy", Some(&[Some("xxyy")])), + ("(?:)*", "", "x", Some(&[Some("")])), + // A brace that is no quantifier (Annex B). + ("a{", "", "a{", Some(&[Some("a{")])), + ("a{,5}", "", "a{,5}", Some(&[Some("a{,5}")])), + ("x{1}", "", "x", Some(&[Some("x")])), + ], + ) +} + +#[test] +fn captures_and_the_empty_check() -> Result<(), String> { + run( + "matcher: captures, the empty check", + &[ + // §22.2.2.3.1† RepeatMatcher's notes: an iteration beyond the + // least number that matches the empty string fails. + ("(a*)*", "", "b", Some(&[Some(""), N])), + ("(a*)+", "", "b", Some(&[Some(""), Some("")])), + ("(a*)*", "", "aa", Some(&[Some("aa"), Some("aa")])), + ("(a?)*", "", "aa", Some(&[Some("aa"), Some("a")])), + ("(?:a?)*?b", "", "aab", Some(&[Some("aab")])), + // The standard's example: the captures are reset on each iteration. + ( + "(z)((a+)?(b+)?(c))*", + "", + "zaacbbbcac", + Some(&[ + Some("zaacbbbcac"), + Some("z"), + Some("ac"), + Some("a"), + N, + Some("c"), + ]), + ), + ("(?:(a)|b)*", "", "ab", Some(&[Some("ab"), N])), + ("(?:(a)|(b))+", "", "ab", Some(&[Some("ab"), N, Some("b")])), + ( + "((a)|(b))*?c", + "", + "abc", + Some(&[Some("abc"), Some("b"), N, Some("b")]), + ), + ( + "(a)(?:(b)|c)\\2", + "", + "acb", + Some(&[Some("ac"), Some("a"), N]), + ), + // Backreferences. + ("(a)\\1", "", "aa", Some(&[Some("aa"), Some("a")])), + ("(a)\\1", "", "ab", None), + ("\\1(a)", "", "aa", Some(&[Some("a"), Some("a")])), + ("(a\\1)", "", "a", Some(&[Some("a"), Some("a")])), + ("(a)|\\1b", "", "b", Some(&[Some("b"), N])), + ("(?a)\\k", "", "aa", Some(&[Some("aa"), Some("a")])), + ("(a)\\2", "", "a\u{2}", Some(&[Some("a\u{2}"), Some("a")])), + ("\\1", "", "\u{1}", Some(&[Some("\u{1}")])), + // Duplicate names: the group that took part. + ( + "(?:(?a)|(?b))\\k", + "", + "bb", + Some(&[Some("bb"), N, Some("b")]), + ), + ( + "(?:(?a)|(?b))\\k", + "", + "aa", + Some(&[Some("aa"), Some("a"), N]), + ), + ("(?:(?a)|(?b))\\k", "", "ab", None), + ], + ) +} + +#[test] +fn assertions_and_dot() -> Result<(), String> { + run( + "matcher: assertions, dot", + &[ + ("^b", "", "a\nb", None), + ("^b", "m", "a\nb", Some(&[Some("b")])), + ("a$", "", "a\nb", None), + ("a$", "m", "a\nb", Some(&[Some("a")])), + ("^$", "", "", Some(&[Some("")])), + ("^$", "m", "a\n\nb", Some(&[Some("")])), + ("^b", "m", "a\u{2028}b", Some(&[Some("b")])), + ("^b", "m", "a\u{b}b", None), + ("\\bfoo\\b", "", "a foo b", Some(&[Some("foo")])), + ("\\bfoo\\b", "", "afoo b", None), + ("\\Boo", "", "foo", Some(&[Some("oo")])), + ("\\b", "", "", None), + ("\\B", "", "", Some(&[Some("")])), + ("\\b", "", "-a", Some(&[Some("")])), + (".", "", "\n", None), + (".", "s", "\n", Some(&[Some("\n")])), + (".", "", "\u{2028}", None), + (".", "", "\r", None), + (".+", "", "ab\ncd", Some(&[Some("ab")])), + (".+", "s", "ab\ncd", Some(&[Some("ab\ncd")])), + ("(?s:.)", "", "\n", Some(&[Some("\n")])), + ("(?-s:.)", "s", "\n", None), + ("(?m:^b)", "", "a\nb", Some(&[Some("b")])), + ("(?-m:^b)", "m", "a\nb", None), + ("(?m:^b)^", "", "a\nb", None), + ], + ) +} + +#[test] +fn lookaround() -> Result<(), String> { + run( + "matcher: lookahead, lookbehind", + &[ + ("a(?=b)", "", "ab", Some(&[Some("a")])), + ("a(?!b)", "", "ab", None), + ("a(?!b)", "", "ac", Some(&[Some("a")])), + ("(?=(a))\\1", "", "aa", Some(&[Some("a"), Some("a")])), + ("(?!(a))\\1", "", "b", Some(&[Some(""), N])), + // The standard's example: no backtracking into a lookahead. + ( + "(?=(a+))a*b\\1", + "", + "baaabac", + Some(&[Some("aba"), Some("a")]), + ), + ( + "(.*?)a(?!(a+)b\\2c)\\2(.*)", + "", + "baaabaac", + Some(&[Some("baaabaac"), Some("ba"), N, Some("abaac")]), + ), + ("(?=a)*b", "", "b", Some(&[Some("b")])), + ("(?<=a)b", "", "ab", Some(&[Some("b")])), + ("(?<=a)b", "", "cb", None), + ("(? Result<(), String> { + run( + "matcher: classes, escapes", + &[ + ("[^a]", "", "a", None), + ("[^a]", "", "ab", Some(&[Some("b")])), + ("[]", "", "a", None), + ("[^]", "", "\n", Some(&[Some("\n")])), + ("[\\d-x]", "", "-", Some(&[Some("-")])), + ("[a-c]+", "", "abcd", Some(&[Some("abc")])), + ("[\\w-]+", "", "a-b c", Some(&[Some("a-b")])), + ("[\\b]", "", "\u{8}", Some(&[Some("\u{8}")])), + ("\\s", "", "\u{a0}", Some(&[Some("\u{a0}")])), + ("\\s", "", "\u{feff}", Some(&[Some("\u{feff}")])), + ("\\s", "", "\u{200b}", None), + ( + "\\s+", + "", + "a \t\n\u{2003}b", + Some(&[Some(" \t\n\u{2003}")]), + ), + ("\\S+", "", " ab ", Some(&[Some("ab")])), + ("\\d+", "", "a12b", Some(&[Some("12")])), + ("\\D+", "", "12ab3", Some(&[Some("ab")])), + ("\\w+", "", "-ab_1-", Some(&[Some("ab_1")])), + ("\\W", "", "ab-", Some(&[Some("-")])), + ( + "\\x41\\u0042\\cJ\\0", + "", + "AB\n\u{0}", + Some(&[Some("AB\n\u{0}")]), + ), + ("\\101", "", "A", Some(&[Some("A")])), + ("[\\x41-\\x43]+", "", "ABCD", Some(&[Some("ABC")])), + ("[a-z&&[aeiou]]", "", "&]", Some(&[Some("&]")])), + ], + ) +} + +#[test] +fn ignore_case() -> Result<(), String> { + run( + "matcher: ignore case", + &[ + ("abc", "i", "ABC", Some(&[Some("ABC")])), + ("ABC", "i", "abc", Some(&[Some("abc")])), + ("[a-z]+", "i", "ABC", Some(&[Some("ABC")])), + ("[^a-z]", "i", "ABC1", Some(&[Some("1")])), + ("(a)\\1", "i", "aA", Some(&[Some("aA"), Some("a")])), + ("(?<=(a))\\1", "i", "aA", Some(&[Some("A"), Some("a")])), + // Without u: toUpperCase of the code unit, never into or out of ASCII. + ("\u{e9}", "i", "\u{c9}", Some(&[Some("\u{c9}")])), + ("\u{3c3}", "i", "\u{3c2}", Some(&[Some("\u{3c2}")])), + ("\u{212a}", "i", "k", None), + ("k", "i", "\u{212a}", None), + ("\u{17f}", "i", "s", None), + ("s", "i", "\u{17f}", None), + ("\u{df}", "i", "SS", None), + ("\\w", "i", "\u{17f}", None), + ("\\w", "i", "\u{212a}", None), + ("[\u{17f}]", "i", "S", None), + // With u: simple case folding. + ("\u{212a}", "iu", "k", Some(&[Some("k")])), + ("k", "iu", "\u{212a}", Some(&[Some("\u{212a}")])), + ("[k]", "iu", "\u{212a}", Some(&[Some("\u{212a}")])), + ("\u{17f}", "iu", "S", Some(&[Some("S")])), + ("\u{df}", "iu", "SS", None), + ("\u{df}", "iu", "\u{1e9e}", Some(&[Some("\u{1e9e}")])), + ("\\w", "iu", "\u{17f}", Some(&[Some("\u{17f}")])), + ("\\w", "iu", "\u{212a}", Some(&[Some("\u{212a}")])), + ("\\W", "iu", "S", None), + ("\\W", "iu", "\u{17f}", None), + ("\\bk", "iu", "\u{212a}", Some(&[Some("\u{212a}")])), + ("\u{10400}", "iu", "\u{10428}", Some(&[Some("\u{10428}")])), + ( + "[\u{10400}-\u{10410}]", + "iu", + "\u{10428}", + Some(&[Some("\u{10428}")]), + ), + ("\u{1c5}", "i", "\u{1c4}", Some(&[Some("\u{1c4}")])), + ("\u{1c5}", "i", "\u{1c6}", Some(&[Some("\u{1c6}")])), + ("\\p{Lu}", "iu", "a", Some(&[Some("a")])), + ("\\p{Lu}", "u", "a", None), + ("\\p{Ll}", "iu", "A", Some(&[Some("A")])), + // Without v, a negated escape is complemented before folding: the + // standard's behaviour, which v fixes. + ("\\P{Lu}", "iu", "A", Some(&[Some("A")])), + ("\\P{Lu}", "iv", "A", None), + ("[^\\P{Lu}]", "iv", "a", Some(&[Some("a")])), + ("[^\\p{Lu}]", "iv", "a", None), + ("[^\\p{Lu}]", "iu", "a", None), + ("\\D", "iu", "5", None), + ("(?i:a)b", "", "Ab", Some(&[Some("Ab")])), + ("(?i:a)b", "", "AB", None), + ("(?-i:a)b", "i", "aB", Some(&[Some("aB")])), + ("(?-i:a)b", "i", "AB", None), + ("(?i:[a-b]+)", "", "aBcD", Some(&[Some("aB")])), + ], + ) +} + +#[test] +fn unicode_mode() -> Result<(), String> { + run( + "matcher: code points", + &[ + ("^.$", "u", "\u{1F600}", Some(&[Some("\u{1F600}")])), + ("^.$", "", "\u{1F600}", None), + ("^..$", "", "\u{1F600}", Some(&[Some("\u{1F600}")])), + ("^.$", "v", "\u{1F600}", Some(&[Some("\u{1F600}")])), + ("\\u{1F600}", "u", "\u{1F600}", Some(&[Some("\u{1F600}")])), + ( + "\\ud83d\\ude00", + "u", + "\u{1F600}", + Some(&[Some("\u{1F600}")]), + ), + ("[\u{1F600}]", "u", "\u{1F600}", Some(&[Some("\u{1F600}")])), + ( + "[\u{1F600}-\u{1F64F}]+", + "u", + "x\u{1F601}\u{1F602}", + Some(&[Some("\u{1F601}\u{1F602}")]), + ), + ("^[^x]$", "u", "\u{1F600}", Some(&[Some("\u{1F600}")])), + ("^[^x]$", "", "\u{1F600}", None), + ( + "\u{1F600}+", + "u", + "\u{1F600}\u{1F600}", + Some(&[Some("\u{1F600}\u{1F600}")]), + ), + // Without u the quantifier is on the low surrogate alone. + ( + "\u{1F600}+", + "", + "\u{1F600}\u{1F600}", + Some(&[Some("\u{1F600}")]), + ), + // A lone surrogate is not half of a pair with u. + ("\\ud83d", "u", "\u{1F600}", None), + ("\\ud83d", "u", "\u{FFFD}", None), + ("\\ude00", "u", "\u{1F600}", None), + ( + "(?<=\\ud83d)\\ude00", + "", + "\u{1F600}", + Some(&[Some("\u{FFFD}")]), + ), + ( + "(.)\\1", + "u", + "\u{1F600}\u{1F600}", + Some(&[Some("\u{1F600}\u{1F600}"), Some("\u{1F600}")]), + ), + ( + "(?<=(.))x", + "u", + "\u{1F600}x", + Some(&[Some("x"), Some("\u{1F600}")]), + ), + ("\\b", "u", "\u{1F600}", None), + (".+?", "u", "\u{1F600}", Some(&[Some("\u{1F600}")])), + ( + ".{2}", + "u", + "\u{1F600}\u{1F601}", + Some(&[Some("\u{1F600}\u{1F601}")]), + ), + ( + "^.{3}$", + "u", + "\u{1F600}a\u{1F601}", + Some(&[Some("\u{1F600}a\u{1F601}")]), + ), + ], + ) +} + +#[test] +fn property_escapes() -> Result<(), String> { + run( + "matcher: property escapes", + &[ + ("\\p{Lu}", "u", "aBc", Some(&[Some("B")])), + ( + "\\p{L}+", + "u", + "h\u{e9}llo w\u{f6}rld", + Some(&[Some("h\u{e9}llo")]), + ), + ("\\p{Letter}+", "u", "ab1", Some(&[Some("ab")])), + ("\\p{gc=Nd}", "u", "x\u{663}", Some(&[Some("\u{663}")])), + ( + "\\p{General_Category=Decimal_Number}", + "u", + "x\u{663}", + Some(&[Some("\u{663}")]), + ), + ("\\P{L}", "u", "ab1", Some(&[Some("1")])), + ("\\p{ASCII}", "u", "\u{e9}a", Some(&[Some("a")])), + ("\\p{Any}", "u", "\u{1F600}", Some(&[Some("\u{1F600}")])), + ("\\p{Cn}", "u", "a\u{378}", Some(&[Some("\u{378}")])), + ("\\p{Assigned}", "u", "\u{378}a", Some(&[Some("a")])), + ("\\p{Alphabetic}+", "u", "ab1", Some(&[Some("ab")])), + ("\\p{Alpha}+", "u", "ab1", Some(&[Some("ab")])), + ("\\p{Emoji}", "u", "a\u{1F600}", Some(&[Some("\u{1F600}")])), + ( + "\\p{White_Space}", + "u", + "a\u{2003}", + Some(&[Some("\u{2003}")]), + ), + ("\\p{Uppercase}", "u", "aB", Some(&[Some("B")])), + ("\\p{ID_Start}+", "u", "$ab1", Some(&[Some("ab")])), + ("\\p{Hex_Digit}+", "u", "xfF9g", Some(&[Some("fF9")])), + ("\\p{Cs}", "u", "a", None), + ("[\\p{L}\\p{N}]+", "u", "-a1-", Some(&[Some("a1")])), + ("[^\\p{L}]", "u", "a1", Some(&[Some("1")])), + ("\\p{Lu}", "v", "aB", Some(&[Some("B")])), + ("\\p{Sc}", "u", "a$", Some(&[Some("$")])), + ("\\p{Pd}", "u", "a-", Some(&[Some("-")])), + ("\\p{Zs}", "u", "a ", Some(&[Some(" ")])), + ("\\p{Cc}", "u", "a\n", Some(&[Some("\n")])), + ("\\p{Cased_Letter}+", "u", "aB1", Some(&[Some("aB")])), + ], + ) +} + +#[test] +fn unicode_sets() -> Result<(), String> { + run( + "matcher: v-mode class sets", + &[ + ("[\\p{L}--[a-z]]", "v", "aB", Some(&[Some("B")])), + ("[[a-z]&&[aeiou]]+", "v", "hello", Some(&[Some("e")])), + ("[[a-z]--[aeiou]]+", "v", "hello", Some(&[Some("h")])), + ("[^[a-z]]", "v", "aB", Some(&[Some("B")])), + ("[\\w--\\d]+", "v", "12ab3", Some(&[Some("ab")])), + ("[\\q{abc|d}]", "v", "xabc", Some(&[Some("abc")])), + ("[\\q{abc|ab}]", "v", "abx", Some(&[Some("ab")])), + ("[\\q{ab|abc}]", "v", "abcx", Some(&[Some("abc")])), + ("^[\\q{ab|}]$", "v", "", Some(&[Some("")])), + ("^[\\q{ab|}]$", "v", "ab", Some(&[Some("ab")])), + ("[\\q{ab}x]+", "v", "xabx", Some(&[Some("xabx")])), + ("[\\q{a}b]+", "v", "ab", Some(&[Some("ab")])), + ("[\\q{abc}--\\q{abc}]", "v", "abc", None), + ("[\\q{ab|cd}&&\\q{cd|ef}]", "v", "abcd", Some(&[Some("cd")])), + ("[\\q{ab|cd}--\\q{cd}]", "v", "cdab", Some(&[Some("ab")])), + ("[\\q{ab}]", "iv", "AB", Some(&[Some("AB")])), + ("(?<=[\\q{ab|c}])d", "v", "abd", Some(&[Some("d")])), + ("[\\q{ab|c}]d", "v", "abd", Some(&[Some("abd")])), + ("[[a-c]&&[b-d]]", "v", "abcd", Some(&[Some("b")])), + ("[[a-c][x-z]]+", "v", "abxy-", Some(&[Some("abxy")])), + ], + ) +} + +#[test] +fn start_index_and_sticky() -> Result<(), String> { + let some = |p: &str, f: &str, s: &str, at: usize, want: Option<(usize, usize)>| { + let prog = build(p, f)?; + let m = prog + .exec(&units(s), at, f.contains('y'), &Limits::default(), None) + .map_err(|e| e.to_string())?; + let got = m.and_then(|m| m.get(0)); + if got == want { + Ok(()) + } else { + Err(format!( + "/{p}/{f} at {at} on {s:?}: got {got:?}, want {want:?}" + )) + } + }; + some("a", "", "aaa", 1, Some((1, 2)))?; + some("a", "y", "aaa", 1, Some((1, 2)))?; + some("b", "y", "ab", 0, None)?; + some("b", "", "ab", 0, Some((1, 2)))?; + some("b", "y", "ab", 1, Some((1, 2)))?; + some("^a", "", "aa", 1, None)?; + some("^a", "m", "a\na", 1, Some((2, 3)))?; + some("", "", "abc", 3, Some((3, 3)))?; + some("", "", "abc", 4, None)?; + some("a", "", "abc", 4, None)?; + some("(?<=a)b", "y", "ab", 1, Some((1, 2)))?; + some("\\b", "", "ab cd", 2, Some((2, 2)))?; + // A start inside a surrogate pair is the pair's start, with u or v; and an + // unanchored search steps by code points. + some(".", "uy", "\u{1F600}", 1, Some((0, 2)))?; + some(".", "y", "\u{1F600}", 1, Some((1, 2)))?; + some("\\udc00|x", "u", "\u{10000}x", 0, Some((2, 3)))?; + some("^", "", "\u{1F600}", 1, None)?; + Ok(()) +} + +#[test] +fn many_repetitions_in_a_long_input() -> Result<(), String> { + // A run of one character takes one stack entry, however long. (Under Miri + // the input is shorter, to keep the test under its time limit.) + let n: usize = if cfg!(miri) { 300 } else { 100_000 }; + let a = "a".repeat(n); + let input = format!("{a}b"); + let r = exec("a*b", "", &input)?; + assert_eq!(r.map(|v| v.len()), Some(1)); + let r = exec("a*a*a*b", "", &input)?; + assert!(r.is_some()); + let r = exec("[a-z]+?b", "", &input)?; + assert!(r.is_some()); + let r = exec("(?:a|b)*b", "", &input)?; + assert!(r.is_some()); + // A large count is a counter, not a copy of the body. + let r = exec(&format!("a{{{n}}}b"), "", &input)?; + assert!(r.is_some()); + let r = exec(&format!("(?:a){{{}}}ab", n - 1), "", &input)?; + assert!(r.is_some()); + // A count beyond the input fails, after trying every start. + let short = "a".repeat(if cfg!(miri) { 50 } else { 2000 }); + let r = exec("a{4000000000}", "", &short)?; + assert!(r.is_none()); + Ok(()) +} + +#[test] +fn named_groups_through_the_api() -> Result<(), String> { + let prog = build("(?:(?a)|(?b))(?c)?", "")?; + let text = units("bc"); + let m = prog + .exec(&text, 0, false, &Limits::default(), None) + .map_err(|e| e.to_string())? + .ok_or("no match")?; + let name = |s: &str| s.chars().map(u32::from).collect::>(); + assert_eq!(prog.named_capture(&m, &name("x")), Some((0, 1))); + assert_eq!(prog.named_capture(&m, &name("y")), Some((1, 2))); + assert_eq!(prog.named_capture(&m, &name("z")), None); + assert_eq!(prog.capture_count(), 3); + assert_eq!(prog.group_names().len(), 3); + assert!(!prog.unicode()); + Ok(()) +} + +#[test] +fn script_and_script_extensions() -> Result<(), String> { + run( + "matcher: Script and Script_Extensions", + &[ + // Long and short names, and the aliases. + ( + "\\p{Script=Latin}+", + "u", + "\u{3b1}\u{3b2}abc\u{3b3}", + Some(&[Some("abc")]), + ), + ( + "\\p{sc=Grek}+", + "u", + "abc\u{3b1}\u{3b2}\u{3b3}d", + Some(&[Some("\u{3b1}\u{3b2}\u{3b3}")]), + ), + ( + "\\P{sc=Greek}+", + "u", + "\u{3b1}\u{3b2}ab\u{3b3}", + Some(&[Some("ab")]), + ), + ("\\p{sc=Zinh}", "u", "\u{342}", Some(&[Some("\u{342}")])), + ( + "\\p{Script=Inherited}", + "u", + "\u{342}", + Some(&[Some("\u{342}")]), + ), + ("\\p{sc=Qaai}", "u", "\u{342}", Some(&[Some("\u{342}")])), + // U+0342 is Inherited, and its extensions are Greek alone. + ("\\p{sc=Greek}", "u", "\u{342}", None), + ("\\p{scx=Greek}", "u", "\u{342}", Some(&[Some("\u{342}")])), + ( + "\\p{Script_Extensions=Grek}", + "u", + "\u{342}", + Some(&[Some("\u{342}")]), + ), + ("\\P{scx=Greek}", "u", "\u{342}", None), + // U+00B7 is Common, and extends to Latin among others. + ("\\p{sc=Latn}", "u", "\u{b7}", None), + ("\\p{sc=Common}", "u", "\u{b7}", Some(&[Some("\u{b7}")])), + ("\\p{scx=Latn}", "u", "\u{b7}", Some(&[Some("\u{b7}")])), + ("\\p{scx=Cyrillic}", "u", "\u{b7}", None), + // A code point no row of ScriptExtensions lists extends to its script. + ("\\p{scx=Latin}", "u", "a", Some(&[Some("a")])), + ("\\p{scx=Greek}", "u", "a", None), + // Unknown: unassigned and private use; Hrkt has no code point. + ("\\p{sc=Zzzz}", "u", "\u{378}", Some(&[Some("\u{378}")])), + ( + "\\p{sc=Unknown}", + "u", + "\u{e000}", + Some(&[Some("\u{e000}")]), + ), + ("\\p{sc=Unknown}", "u", "a", None), + ("\\p{sc=Hrkt}", "u", "\u{3042}", None), + ( + "\\p{sc=Hiragana}", + "u", + "\u{3042}", + Some(&[Some("\u{3042}")]), + ), + // Astral scripts, by code point. + ( + "^\\p{sc=Deseret}+$", + "u", + "\u{10400}\u{10428}", + Some(&[Some("\u{10400}\u{10428}")]), + ), + // With i and u, a leaf holds if some character with the same + // Canonicalize is in the set: U+0345 (Inherited) folds to U+03B9. + ("\\P{sc=Greek}", "iu", "\u{3b9}", Some(&[Some("\u{3b9}")])), + ("\\p{sc=Greek}", "iu", "\u{345}", Some(&[Some("\u{345}")])), + ("\\p{sc=Greek}", "u", "\u{345}", None), + // ... and all of those are in Script_Extensions=Greek. + ("\\P{scx=Greek}", "iu", "\u{3b9}", None), + // With v the set is folded first and then complemented. + ("\\P{sc=Greek}", "iv", "\u{3b9}", None), + ("\\P{sc=Greek}", "iv", "a", Some(&[Some("a")])), + ( + "[\\p{sc=Greek}&&\\p{Lu}]", + "v", + "\u{3b1}\u{391}", + Some(&[Some("\u{391}")]), + ), + ( + "[\\p{sc=Greek}--\\p{Lu}]+", + "v", + "\u{3b1}\u{391}\u{3b2}", + Some(&[Some("\u{3b1}")]), + ), + ("[^\\p{scx=Greek}]", "v", "\u{342}a", Some(&[Some("a")])), + ], + ) +} + +#[test] +fn properties_of_strings() -> Result<(), String> { + // man, woman, girl, boy: a family and its three-person prefix. + const F3: &str = "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}"; + const F4: &str = "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F466}"; + const KEY: &str = "1\u{fe0f}\u{20e3}"; + const FR: &str = "\u{1F1EB}\u{1F1F7}"; + let cases: &[Case] = &[ + ("^\\p{Emoji_Keycap_Sequence}$", "v", KEY, Some(&[Some(KEY)])), + ("\\p{Emoji_Keycap_Sequence}", "v", "1", None), + ( + "\\p{Emoji_Keycap_Sequence}", + "v", + "a1\u{fe0f}\u{20e3}", + Some(&[Some(KEY)]), + ), + ( + "\\p{Emoji_Keycap_Sequence}{2}", + "v", + "1\u{fe0f}\u{20e3}1\u{fe0f}\u{20e3}x", + Some(&[Some("1\u{fe0f}\u{20e3}1\u{fe0f}\u{20e3}")]), + ), + ( + "(?<=\\p{Emoji_Keycap_Sequence})x", + "v", + "1\u{fe0f}\u{20e3}x", + Some(&[Some("x")]), + ), + ("(?<=\\p{Emoji_Keycap_Sequence})x", "v", "1x", None), + // Longest first. + ( + "\\p{RGI_Emoji}", + "v", + "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F466}x", + Some(&[Some(F4)]), + ), + ("\\p{RGI_Emoji_ZWJ_Sequence}", "v", F3, Some(&[Some(F3)])), + // ... and the next shorter one when the rest of the pattern fails. + ( + "\\p{RGI_Emoji}\u{200D}", + "v", + F4, + Some(&[Some("\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}")]), + ), + ( + "\\p{RGI_Emoji}", + "v", + "\u{1F468}x", + Some(&[Some("\u{1F468}")]), + ), + ("^\\p{RGI_Emoji_Flag_Sequence}$", "v", FR, Some(&[Some(FR)])), + ( + "\\p{RGI_Emoji_Flag_Sequence}", + "v", + "x\u{1F1EB}\u{1F1F7}", + Some(&[Some(FR)]), + ), + ( + "\\p{RGI_Emoji_Flag_Sequence}", + "v", + "\u{1F1EB}\u{1F1EB}", + None, + ), + // Basic_Emoji has one-character members and two-character ones. + ( + "\\p{Basic_Emoji}", + "v", + "\u{1F600}", + Some(&[Some("\u{1F600}")]), + ), + ( + "\\p{Basic_Emoji}", + "v", + "\u{263a}\u{fe0f}", + Some(&[Some("\u{263a}\u{fe0f}")]), + ), + ("\\p{Basic_Emoji}", "v", "\u{263a}", None), + ( + "\\p{RGI_Emoji_Modifier_Sequence}", + "v", + "\u{261D}\u{1F3FB}", + Some(&[Some("\u{261D}\u{1F3FB}")]), + ), + ( + "\\p{RGI_Emoji_Tag_Sequence}", + "v", + "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}", + Some(&[Some( + "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}", + )]), + ), + // Set operations on them. + ("[\\p{RGI_Emoji}--\\q{\u{1F1EB}\u{1F1F7}}]", "v", FR, None), + ( + "[\\p{RGI_Emoji}--\\q{\u{1F1EB}\u{1F1F7}}]", + "v", + "\u{1F1E9}\u{1F1EA}", + Some(&[Some("\u{1F1E9}\u{1F1EA}")]), + ), + ( + "[\\p{RGI_Emoji_Flag_Sequence}&&\\q{\u{1F1EB}\u{1F1F7}|a}]", + "v", + FR, + Some(&[Some(FR)]), + ), + ( + "[\\p{RGI_Emoji_Flag_Sequence}&&\\q{\u{1F1EB}\u{1F1F7}|a}]", + "v", + "a\u{1F1E9}\u{1F1EA}", + None, + ), + ( + "[\\p{Basic_Emoji}--\\p{Emoji_Keycap_Sequence}]", + "v", + "\u{1F600}", + Some(&[Some("\u{1F600}")]), + ), + ( + "[\\p{Emoji_Keycap_Sequence}\\q{ab}x]+", + "v", + "x1\u{fe0f}\u{20e3}abz", + Some(&[Some("x1\u{fe0f}\u{20e3}ab")]), + ), + // With i. + ("\\p{RGI_Emoji}", "iv", KEY, Some(&[Some(KEY)])), + // Inside other constructs. + ( + "(\\p{RGI_Emoji})+", + "v", + "\u{1F600}\u{1F600}", + Some(&[Some("\u{1F600}\u{1F600}"), Some("\u{1F600}")]), + ), + ]; + // Under Miri the union of the six (about 2 800 strings) is too slow to + // build and search; the one-type properties stay. + let cases: Vec = cases + .iter() + .copied() + .filter(|c| !(cfg!(miri) && c.0.contains("p{RGI_Emoji}"))) + .collect(); + run("matcher: properties of strings", &cases) +} + +#[test] +fn unknown_script_names_do_not_parse_or_compile() -> Result<(), String> { + let u = Flags::parse(&units("u")).map_err(|e| format!("{e:?}"))?; + let v = Flags::parse(&units("v")).map_err(|e| format!("{e:?}"))?; + for p in ["\\p{sc=Klingon}", "\\p{scx=latin}", "\\p{Script=Latn_}"] { + assert!(parse(&units(p), u).is_err(), "{p}"); + // Parsed without the name check, compile refuses the value. + let pat = clazel_js_regexp::parse_with(&units(p), u, &clazel_js_regexp::SyntaxOnly) + .map_err(|e| e.to_string())?; + match compile(&pat, u) { + Err(CompileError::UnsupportedProperty(_)) => {} + other => return Err(format!("{p}: {other:?}")), + } + } + // The properties of strings are for `v` only, and never negated. + for (p, f) in [ + ("\\p{RGI_Emoji}", u), + ("\\P{RGI_Emoji}", v), + ("[^\\p{RGI_Emoji}]", v), + ] { + assert!(parse(&units(p), f).is_err(), "{p}"); + } + Ok(()) +} diff --git a/crates/clazel-js-regexp/tests/tables.rs b/crates/clazel-js-regexp/tests/tables.rs new file mode 100644 index 000000000..2ed313b06 --- /dev/null +++ b/crates/clazel-js-regexp/tests/tables.rs @@ -0,0 +1,1062 @@ +//! The generated tables (`src/tables.rs`) against the vendored Unicode 18.0.0 +//! files (`tests/data/ucd-regexp/`, phase-7.md item 7.1). +//! +//! This file is the generator: [`render`] writes the whole of `src/tables.rs` +//! from the UCD, and the test `tables_are_current` fails if the committed file +//! differs from what it writes. To rewrite the file after changing this +//! generator or the data, run +//! `CLAZEL_WRITE_TABLES=1 cargo test -p clazel-js-regexp --test tables` +//! and then `cargo fmt -p clazel-js-regexp` (the output is already what rustfmt +//! writes, so the second step changes nothing; the test is what says so). +//! +//! What it derives, and from which file: +//! +//! - `SIMPLE_FOLD`: Simple Case Folding, the `C` and `S` rows of +//! `CaseFolding.txt` (ECMA-262's `Canonicalize` with `u` or `v`, §22.2.2.7.3†), +//! and `SIMPLE_FOLD_INVERSE`, the same pairs sorted by their second element. +//! - `UPPER`: `toUppercase` of a BMP code point where the result is one BMP +//! code point, different from the argument, and not an ASCII letter for a +//! non-ASCII argument (`Canonicalize` without `u` or `v`): the full +//! unconditional mapping of `SpecialCasing.txt` where it has one, else the +//! simple uppercase of `UnicodeData.txt`. `UPPER_INVERSE` likewise. +//! - `GENERAL_CATEGORY`: the ranges of `DerivedGeneralCategory.txt`, every +//! code point covered, a gap being `Cn`. +//! - One table per binary property of ECMA-262's list, from +//! `PropList.txt`, `DerivedCoreProperties.txt`, `DerivedNormalizationProps.txt`, +//! `DerivedBinaryProperties.txt` or `emoji-data.txt` (the first that lists it). +//! +//! - `SCRIPTS`: for each `Script` value of `PropertyValueAliases.txt` (`sc` +//! rows: short name, long name, any further alias), its code points under +//! `Script` (`Scripts.txt`; `Unknown` is every code point the file does not +//! list) and under `Script_Extensions` (`ScriptExtensions.txt`: the rows +//! that name the script, plus the code points of the script that no row +//! lists, whose extensions are their script alone). +//! - `STRING_PROPERTIES`: the properties of strings of UTS #51 (ED-27), from +//! `emoji-sequences.txt` and `emoji-zwj-sequences.txt`: the code points a +//! property holds as one-character strings (ranges), and its strings of two +//! or more (as Rust strings); `RGI_Emoji` is the union of the other six. +//! +//! The other tests are independent checks of the committed tables: the +//! categories against `UnicodeData.txt` (a different file from the one the +//! table is made from), and spot checks of the case mappings. + +use std::collections::BTreeMap; +use std::fmt::Write as _; +use std::path::PathBuf; + +use clazel_js_regexp::props::BINARY_PROPERTIES; +use clazel_js_regexp::tables; +use clazel_js_regexp::unicode::{canonicalize, equivalents, simple_fold}; + +const LAST: u32 = 0x10FFFF; + +/// The thirty general categories, in the order of the table's index. +const CATEGORIES: [&str; 30] = [ + "Lu", "Ll", "Lt", "Lm", "Lo", "Mn", "Mc", "Me", "Nd", "Nl", "No", "Pc", "Pd", "Ps", "Pe", "Pi", + "Pf", "Po", "Sm", "Sc", "Sk", "So", "Zs", "Zl", "Zp", "Cc", "Cf", "Cs", "Co", "Cn", +]; + +const CN: u8 = 29; + +fn data(file: &str) -> Result { + let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/data/ucd-regexp") + .join(file); + std::fs::read_to_string(&path).map_err(|e| format!("{}: {e}", path.display())) +} + +/// The `;`-separated fields of each line of a UCD file, comments removed. +fn rows(text: &str) -> Vec> { + text.lines() + .map(|l| l.split('#').next().unwrap_or_default()) + .filter(|l| !l.trim().is_empty()) + .map(|l| l.split(';').map(|f| f.trim().to_string()).collect()) + .collect() +} + +fn hex(s: &str) -> Result { + u32::from_str_radix(s.trim(), 16).map_err(|e| format!("{s:?}: {e}")) +} + +fn range(s: &str) -> Result<(u32, u32), String> { + match s.split_once("..") { + Some((a, b)) => Ok((hex(a)?, hex(b)?)), + None => hex(s).map(|c| (c, c)), + } +} + +/// Sorted, disjoint, with touching ranges merged. +fn merge(mut r: Vec<(u32, u32)>) -> Vec<(u32, u32)> { + r.sort_unstable(); + let mut out: Vec<(u32, u32)> = Vec::new(); + for (a, b) in r { + match out.last_mut() { + Some(last) if a <= last.1.saturating_add(1) => last.1 = last.1.max(b), + _ => out.push((a, b)), + } + } + out +} + +fn property_ranges(files: &[String], name: &str) -> Result, String> { + for text in files { + let mut found = Vec::new(); + for row in rows(text) { + if let (Some(r), Some(n)) = (row.first(), row.get(1)) + && n == name + { + found.push(range(r)?); + } + } + if !found.is_empty() { + return Ok(merge(found)); + } + } + Err(format!("{name}: in no file")) +} + +fn pairs(out: &mut String, name: &str, doc: &str, list: &[(u32, u32)]) { + let _ = writeln!(out, "/// {doc}"); + // rustfmt puts an array on one line if its elements take at most 60 + // characters; the generator writes what rustfmt would. + let one: Vec = list + .iter() + .map(|(a, b)| format!("({a:#X}, {b:#X})")) + .collect(); + let joined = one.join(", "); + if joined.len() <= 60 { + let line = format!("pub static {name}: &[(u32, u32)] = &[{joined}];"); + if line.len() <= 100 { + let _ = writeln!(out, "{line}\n"); + } else { + let _ = writeln!( + out, + "pub static {name}: &[(u32, u32)] =\n &[{joined}];\n" + ); + } + } else { + let _ = writeln!(out, "pub static {name}: &[(u32, u32)] = &["); + for (a, b) in list { + let _ = writeln!(out, " ({a:#X}, {b:#X}),"); + } + out.push_str("];\n\n"); + } +} + +fn fold_table() -> Result, String> { + let mut v = Vec::new(); + for row in rows(&data("CaseFolding.txt")?) { + if let [code, status, mapping, ..] = row.as_slice() + && (status == "C" || status == "S") + { + v.push((hex(code)?, hex(mapping)?)); + } + } + v.sort_unstable(); + Ok(v) +} + +fn upper_table() -> Result, String> { + // Unconditional full mappings of SpecialCasing.txt: code; lower; title; + // upper; (condition;)? + let mut special: BTreeMap> = BTreeMap::new(); + for row in rows(&data("SpecialCasing.txt")?) { + if let [code, _, _, upper, rest @ ..] = row.as_slice() { + let conditional = rest.iter().any(|f| !f.is_empty()); + if !conditional { + let u = upper + .split_whitespace() + .map(hex) + .collect::, _>>()?; + special.insert(hex(code)?, u); + } + } + } + let mut simple: BTreeMap = BTreeMap::new(); + for line in data("UnicodeData.txt")?.lines() { + let f: Vec<&str> = line.split(';').collect(); + if let (Some(code), Some(up)) = (f.first(), f.get(12)) + && !up.is_empty() + { + simple.insert(hex(code)?, hex(up)?); + } + } + let mut v = Vec::new(); + for c in 0..=0xFFFFu32 { + let result = match special.get(&c) { + Some(u) => u.clone(), + None => vec![simple.get(&c).copied().unwrap_or(c)], + }; + if let [y] = result.as_slice() { + let y = *y; + if y != c && y <= 0xFFFF && !(c >= 128 && y < 128) { + v.push((c, y)); + } + } + } + Ok(v) +} + +fn category_table() -> Result, String> { + let mut v: Vec<(u32, u32, u8)> = Vec::new(); + for row in rows(&data("DerivedGeneralCategory.txt")?) { + if let [r, cat, ..] = row.as_slice() { + let idx = CATEGORIES + .iter() + .position(|c| c == cat) + .ok_or_else(|| format!("category {cat}"))?; + let (a, b) = range(r)?; + v.push((a, b, u8::try_from(idx).map_err(|e| e.to_string())?)); + } + } + v.sort_unstable(); + let mut out: Vec<(u32, u32, u8)> = Vec::new(); + let mut next = 0u32; + let mut push = |a: u32, b: u32, c: u8| match out.last_mut() { + Some(last) if last.2 == c && last.1.saturating_add(1) == a => last.1 = b, + _ => out.push((a, b, c)), + }; + for (a, b, c) in v { + if a < next { + return Err(format!("overlap at {a:X}")); + } + if a > next { + push(next, a.saturating_sub(1), CN); + } + push(a, b, c); + next = b.saturating_add(1); + } + if next <= LAST { + push(next, LAST, CN); + } + Ok(out) +} + +/// `a` without the code points of `b`; both sorted and disjoint. +fn subtract(a: &[(u32, u32)], b: &[(u32, u32)]) -> Vec<(u32, u32)> { + let mut out = Vec::new(); + for &(lo, hi) in a { + let mut cur = lo; + let mut done = false; + for &(blo, bhi) in b { + if bhi < cur { + continue; + } + if blo > hi { + break; + } + if blo > cur { + out.push((cur, blo.saturating_sub(1))); + } + if bhi >= hi { + done = true; + break; + } + cur = bhi.saturating_add(1); + } + if !done { + out.push((cur, hi)); + } + } + out +} + +/// One `Script` value as the generator sees it. +struct ScriptData { + /// The long name. + long: String, + /// Short name, long name, further aliases (`PropertyValueAliases.txt`). + names: Vec, + sc: Vec<(u32, u32)>, + scx: Vec<(u32, u32)>, +} + +fn scripts() -> Result, String> { + // The values: `sc ; Short ; Long [; Alias]`. + let mut values: Vec> = rows(&data("PropertyValueAliases.txt")?) + .into_iter() + .filter(|r| r.first().is_some_and(|p| p == "sc")) + .map(|r| r.into_iter().skip(1).collect()) + .collect(); + values.sort_by(|a, b| a.get(1).cmp(&b.get(1))); + let long_of_short: BTreeMap = values + .iter() + .filter_map(|v| Some((v.first()?.clone(), v.get(1)?.clone()))) + .collect(); + // Scripts.txt: ranges by long name. + let mut sc: BTreeMap> = BTreeMap::new(); + for row in rows(&data("Scripts.txt")?) { + if let [r, long, ..] = row.as_slice() { + if !long_of_short.values().any(|l| l == long) { + return Err(format!("Scripts.txt: {long} is not an sc value")); + } + sc.entry(long.clone()).or_default().push(range(r)?); + } + } + let sc: BTreeMap> = + sc.into_iter().map(|(k, v)| (k, merge(v))).collect(); + let listed = merge(sc.values().flatten().copied().collect()); + let unknown = subtract(&[(0, LAST)], &listed); + // ScriptExtensions.txt: rows naming scripts by short name. + let mut ext: BTreeMap> = BTreeMap::new(); + let mut ext_listed = Vec::new(); + for row in rows(&data("ScriptExtensions.txt")?) { + if let [r, list, ..] = row.as_slice() { + let rg = range(r)?; + ext_listed.push(rg); + for short in list.split_whitespace() { + let long = long_of_short + .get(short) + .ok_or_else(|| format!("ScriptExtensions.txt: {short} is not an sc value"))?; + ext.entry(long.clone()).or_default().push(rg); + } + } + } + let ext_listed = merge(ext_listed); + let mut out = Vec::new(); + for v in values { + let long = v.get(1).ok_or("sc row without a long name")?.clone(); + let own = if long == "Unknown" { + unknown.clone() + } else { + sc.get(&long).cloned().unwrap_or_default() + }; + let mut x = subtract(&own, &ext_listed); + x.extend(ext.get(&long).cloned().unwrap_or_default()); + // A name that is both short and long (`Ahom`) is listed once. + let mut names: Vec = Vec::new(); + for n in v { + if !names.contains(&n) { + names.push(n); + } + } + out.push(ScriptData { + long, + names, + scx: merge(x), + sc: own, + }); + } + Ok(out) +} + +/// The text of the script tables, as rustfmt writes it. +fn render_scripts(out: &mut String) -> Result<(), String> { + let all = scripts()?; + for s in &all { + let long = &s.long; + let up = long.to_ascii_uppercase(); + pairs( + out, + &format!("SCRIPT_{up}"), + &format!("The code points whose Script is {long}."), + &s.sc, + ); + if s.scx != s.sc { + pairs( + out, + &format!("SCRIPT_EXT_{up}"), + &format!("The code points whose Script_Extensions include {long}."), + &s.scx, + ); + } + } + out.push_str("/// A value of the `Script` property.\n"); + out.push_str("#[derive(Debug, Clone, Copy)]\npub struct Script {\n"); + out.push_str(" /// Every name of the value: short name, long name, further aliases.\n"); + out.push_str(" pub names: &'static [&'static str],\n"); + out.push_str(" /// The code points with `Script` this value.\n"); + out.push_str(" pub sc: &'static [(u32, u32)],\n"); + out.push_str(" /// The code points whose `Script_Extensions` include this value.\n"); + out.push_str(" pub scx: &'static [(u32, u32)],\n}\n\n"); + out.push_str("/// The `Script` values of `PropertyValueAliases.txt`, by long name.\n"); + out.push_str("pub static SCRIPTS: &[Script] = &[\n"); + for s in &all { + let long = &s.long; + let up = long.to_ascii_uppercase(); + let names: Vec = s.names.iter().map(|n| format!("\"{n}\"")).collect(); + let ext = if s.scx == s.sc { + format!("SCRIPT_{up}") + } else { + format!("SCRIPT_EXT_{up}") + }; + let _ = writeln!( + out, + " Script {{\n names: &[{}],\n sc: SCRIPT_{up},\n scx: {ext},\n }},", + names.join(", ") + ); + } + out.push_str("];\n\n"); + Ok(()) +} + +/// The six sequence types and where each is listed. +const STRING_TYPES: [(&str, &str); 6] = [ + ("Basic_Emoji", "emoji-sequences.txt"), + ("Emoji_Keycap_Sequence", "emoji-sequences.txt"), + ("RGI_Emoji_Flag_Sequence", "emoji-sequences.txt"), + ("RGI_Emoji_Modifier_Sequence", "emoji-sequences.txt"), + ("RGI_Emoji_Tag_Sequence", "emoji-sequences.txt"), + ("RGI_Emoji_ZWJ_Sequence", "emoji-zwj-sequences.txt"), +]; + +type Strings = (Vec<(u32, u32)>, Vec>); + +/// A property of strings: its one-character members as ranges, and its +/// longer strings in file order. +fn string_property(name: &str, file: &str) -> Result { + let mut chars = Vec::new(); + let mut strings: Vec> = Vec::new(); + for row in rows(&data(file)?) { + if let [cps, ty, ..] = row.as_slice() + && ty == name + { + if cps.contains(' ') { + let s: Vec = cps.split(' ').map(hex).collect::>()?; + if strings.contains(&s) { + return Err(format!("{name}: {cps} twice")); + } + strings.push(s); + } else { + chars.push(range(cps)?); + } + } + } + Ok((merge(chars), strings)) +} + +fn render_strings(out: &mut String) -> Result<(), String> { + let mut with_chars = 0usize; + for (name, file) in STRING_TYPES { + let (chars, strings) = string_property(name, file)?; + if strings.is_empty() { + return Err(format!("{name}: no sequences")); + } + let up = name.to_ascii_uppercase(); + if !chars.is_empty() { + with_chars = with_chars.saturating_add(1); + if name != "Basic_Emoji" { + return Err(format!("{name}: one-character members")); + } + } + pairs( + out, + &format!("STRING_CHARS_{up}"), + &format!("The one-character members of {name}, as ranges."), + &chars, + ); + let _ = writeln!( + out, + "/// The members of {name} of two or more code points, in the order of the file.\npub static STRINGS_{up}: &[&str] = &[" + ); + for s in &strings { + let lit: String = s.iter().map(|c| format!("\\u{{{c:X}}}")).collect(); + let _ = writeln!(out, " \"{lit}\","); + } + out.push_str("];\n\n"); + } + if with_chars != 1 { + return Err("only Basic_Emoji has one-character members".into()); + } + out.push_str("/// A property of strings (UTS #51 ED-27, for `\\p{..}` with `v`).\n"); + out.push_str("#[derive(Debug, Clone, Copy)]\npub struct StringProperty {\n"); + out.push_str(" /// The name (the short name is the same).\n"); + out.push_str(" pub name: &'static str,\n"); + out.push_str(" /// Its one-character members.\n"); + out.push_str(" pub chars: &'static [(u32, u32)],\n"); + out.push_str(" /// Its longer members, as several lists (their union).\n"); + out.push_str(" pub strings: &'static [&'static [&'static str]],\n}\n\n"); + out.push_str( + "/// The properties of strings; `RGI_Emoji` is the union of the six sequence types.\n", + ); + out.push_str("pub static STRING_PROPERTIES: &[StringProperty] = &[\n"); + for (name, _) in STRING_TYPES { + let up = name.to_ascii_uppercase(); + let _ = writeln!( + out, + " StringProperty {{\n name: \"{name}\",\n chars: STRING_CHARS_{up},\n strings: &[STRINGS_{up}],\n }}," + ); + } + let all: Vec = STRING_TYPES + .iter() + .map(|(n, _)| format!(" STRINGS_{},", n.to_ascii_uppercase())) + .collect(); + let _ = writeln!( + out, + " StringProperty {{\n name: \"RGI_Emoji\",\n chars: STRING_CHARS_BASIC_EMOJI,\n strings: &[\n{}\n ],\n }},", + all.join("\n") + ); + out.push_str("];\n"); + Ok(()) +} + +/// The text of `src/tables.rs`. +fn render() -> Result { + let mut out = String::new(); + out.push_str( + "//! Unicode 18.0.0 tables for case folding, `\\p{..}` and `\\w`: generated.\n\ +//!\n\ +//! @generated by `tests/tables.rs` (`CLAZEL_WRITE_TABLES=1 cargo test -p\n\ +//! clazel-js-regexp --test tables`) from `tests/data/ucd-regexp/` (source,\n\ +//! commit and license: `SOURCE.md` there). Do not edit by hand: the test\n\ +//! `tables_are_current` fails if this file differs from what the generator\n\ +//! writes. What each table holds and which file it comes from is in the\n\ +//! header of that test file.\n\n", + ); + let fold = fold_table()?; + pairs( + &mut out, + "SIMPLE_FOLD", + "Simple Case Folding (CaseFolding.txt, statuses C and S): code point, folded, sorted by code point.", + &fold, + ); + let mut inv: Vec<(u32, u32)> = fold.iter().map(|&(a, b)| (b, a)).collect(); + inv.sort_unstable(); + pairs( + &mut out, + "SIMPLE_FOLD_INVERSE", + "`SIMPLE_FOLD` as (folded, code point), sorted.", + &inv, + ); + let upper = upper_table()?; + pairs( + &mut out, + "UPPER", + "toUppercase of a BMP code point where it is one different BMP code point, and not ASCII for a non-ASCII argument: code point, upper case, sorted by code point.", + &upper, + ); + let mut inv: Vec<(u32, u32)> = upper.iter().map(|&(a, b)| (b, a)).collect(); + inv.sort_unstable(); + pairs( + &mut out, + "UPPER_INVERSE", + "`UPPER` as (upper case, code point), sorted.", + &inv, + ); + let cats = category_table()?; + out.push_str("/// The general categories, in the order of the index in `GENERAL_CATEGORY`.\n"); + out.push_str("pub static CATEGORY_NAMES: [&str; 30] = [\n"); + for line in CATEGORIES.chunks(16) { + let items: Vec = line.iter().map(|c| format!("\"{c}\"")).collect(); + let _ = writeln!(out, " {},", items.join(", ")); + } + out.push_str("];\n\n"); + out.push_str("/// General_Category (DerivedGeneralCategory.txt): first, last, index into `CATEGORY_NAMES`. Sorted, disjoint, covers U+0000 to U+10FFFF.\n"); + out.push_str("pub static GENERAL_CATEGORY: &[(u32, u32, u8)] = &[\n"); + for (a, b, c) in &cats { + let _ = writeln!(out, " ({a:#X}, {b:#X}, {c}),"); + } + out.push_str("];\n\n"); + + let files: Vec = [ + "PropList.txt", + "DerivedCoreProperties.txt", + "DerivedNormalizationProps.txt", + "DerivedBinaryProperties.txt", + "emoji-data.txt", + ] + .iter() + .map(|f| data(f)) + .collect::>()?; + let mut names: Vec = BINARY_PROPERTIES + .iter() + .map(|(full, _)| String::from_utf8_lossy(full).into_owned()) + .filter(|n| !matches!(n.as_str(), "Any" | "ASCII" | "Assigned")) + .collect(); + names.sort(); + for n in &names { + let r = property_ranges(&files, n)?; + pairs( + &mut out, + &format!("BINARY_{}", n.to_ascii_uppercase()), + &format!("The code points with the binary property {n}, as ranges."), + &r, + ); + } + out.push_str("/// The binary properties of ECMA-262's list that need a table, by canonical name (`Any`, `ASCII` and `Assigned` need none).\n"); + out.push_str("pub static BINARY: &[(&str, &[(u32, u32)])] = &[\n"); + for n in &names { + let up = n.to_ascii_uppercase(); + let line = format!(" (\"{n}\", BINARY_{up}),"); + // rustfmt breaks a tuple whose contents take more than 60 characters. + if line.len() <= 100 && format!("\"{n}\", BINARY_{up}").len() <= 60 { + let _ = writeln!(out, "{line}"); + } else { + let _ = writeln!(out, " (\n \"{n}\",\n BINARY_{up},\n ),"); + } + } + out.push_str("];\n\n"); + render_scripts(&mut out)?; + render_strings(&mut out)?; + Ok(out) +} + +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn tables_are_current() -> Result<(), String> { + let want = render()?; + let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/tables.rs"); + if std::env::var_os("CLAZEL_WRITE_TABLES").is_some() { + std::fs::write(&path, &want).map_err(|e| e.to_string())?; + return Ok(()); + } + let have = std::fs::read_to_string(&path).map_err(|e| e.to_string())?; + if have == want { + println!("clazel-conformance: suite=\"generated tables\" pass=1 xfail=0 fail=0 total=1"); + Ok(()) + } else { + Err( + "src/tables.rs differs from the generator's output; see the header of tests/tables.rs" + .into(), + ) + } +} + +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn categories_agree_with_unicode_data() -> Result<(), String> { + // UnicodeData.txt lists each assigned code point, or a range between a + // `<.., First>` and `<.., Last>` pair; every other code point is Cn. + let total = usize::try_from(LAST) + .map_err(|e| e.to_string())? + .saturating_add(1); + let mut expect: Vec = vec![CN; total]; + let mut first: Option = None; + for line in data("UnicodeData.txt")?.lines() { + let f: Vec<&str> = line.split(';').collect(); + let (Some(code), Some(name), Some(cat)) = (f.first(), f.get(1), f.get(2)) else { + continue; + }; + let c = hex(code)?; + let idx = u8::try_from( + CATEGORIES + .iter() + .position(|x| x == cat) + .ok_or_else(|| format!("category {cat}"))?, + ) + .map_err(|e| e.to_string())?; + if name.ends_with(", First>") { + first = Some(c); + continue; + } + let lo = if name.ends_with(", Last>") { + first.take().unwrap_or(c) + } else { + c + }; + for x in lo..=c { + if let Some(slot) = expect.get_mut(usize::try_from(x).map_err(|e| e.to_string())?) { + *slot = idx; + } + } + } + let mut bad = 0u32; + let mut at = 0u32; + for &(a, b, c) in tables::GENERAL_CATEGORY { + if a != at { + return Err(format!("gap before {a:X}")); + } + for x in a..=b { + let want = expect.get(usize::try_from(x).map_err(|e| e.to_string())?); + if want != Some(&c) { + bad = bad.saturating_add(1); + } + } + at = b.saturating_add(1); + } + if at != LAST.saturating_add(1) { + return Err("does not reach U+10FFFF".into()); + } + let all = LAST.saturating_add(1); + println!( + "clazel-conformance: suite=\"general category vs UnicodeData.txt\" pass={} xfail=0 fail={bad} total={all}", + all.saturating_sub(bad) + ); + assert_eq!(bad, 0); + Ok(()) +} + +#[test] +fn tables_are_sorted() { + for t in [ + tables::SIMPLE_FOLD, + tables::SIMPLE_FOLD_INVERSE, + tables::UPPER, + tables::UPPER_INVERSE, + ] { + assert!(!t.is_empty()); + assert!(t.windows(2).all(|w| match w { + [a, b] => a < b, + _ => true, + })); + } + assert!(!tables::BINARY.is_empty()); + for (_, t) in tables::BINARY { + assert!(t.windows(2).all(|w| match w { + [a, b] => a.0 <= a.1 && a.1 < b.0, + _ => true, + })); + } +} + +#[test] +fn spot_checks_of_the_mappings() { + // Canonicalize with u: simple case folding. + assert_eq!(simple_fold(0x41), 0x61); + assert_eq!(simple_fold(0x61), 0x61); + assert_eq!(simple_fold(0xDF), 0xDF, "sharp s has only a full folding"); + assert_eq!(simple_fold(0x1E9E), 0xDF, "capital sharp s folds simply"); + assert_eq!(simple_fold(0x17F), 0x73, "long s folds to s"); + assert_eq!(simple_fold(0x212A), 0x6B, "kelvin folds to k"); + assert_eq!(simple_fold(0x130), 0x130, "dotted I: only T and F rows"); + assert_eq!(simple_fold(0x1C5), 0x1C6); + assert_eq!(simple_fold(0x10400), 0x10428); + assert_eq!(simple_fold(0x10FFFF), 0x10FFFF); + // Canonicalize without u: toUppercase of the code unit. + assert_eq!(canonicalize(0x61, false), 0x41); + assert_eq!(canonicalize(0x41, false), 0x41); + assert_eq!(canonicalize(0xE9, false), 0xC9); + assert_eq!(canonicalize(0xDF, false), 0xDF, "uppercase is two units"); + assert_eq!(canonicalize(0x17F, false), 0x17F, "S would be ASCII"); + assert_eq!(canonicalize(0x131, false), 0x131, "I would be ASCII"); + assert_eq!(canonicalize(0x3C3, false), 0x3A3); + assert_eq!(canonicalize(0x3C2, false), 0x3A3, "final sigma"); + assert_eq!(canonicalize(0x1C5, false), 0x1C4); + assert_eq!(canonicalize(0xD801, false), 0xD801, "a lone surrogate"); + assert_eq!(canonicalize(0x212A, false), 0x212A); +} + +/// How many code points to skip between checks: every one natively, a stride +/// under Miri. +fn stride() -> usize { + if cfg!(miri) { 61 } else { 1 } +} + +#[test] +fn equivalents_invert_canonicalize() { + // For every character, its equivalence class (what the matcher compares + // class members and backreferences by) holds it, and holds exactly the + // characters with its canonical form. + let mut out = Vec::new(); + for unicode in [false, true] { + let top = if unicode { 0x10FFFFu32 } else { 0xFFFF }; + for c in (0..=top).step_by(stride()) { + let cc = canonicalize(c, unicode); + equivalents(cc, unicode, &mut out); + assert!( + out.contains(&c), + "{c:X} (unicode: {unicode}) is not in the class of {cc:X}" + ); + for &a in &out { + assert_eq!(canonicalize(a, unicode), cc, "{a:X} in the class of {cc:X}"); + } + assert!(out.len() <= 6, "{cc:X} has {} equivalents", out.len()); + } + } +} + +#[test] +fn simple_folding_is_idempotent_and_canonicalize_unit_is_in_range() { + for c in (0..=0x10FFFFu32).step_by(stride()) { + let f = simple_fold(c); + assert_eq!(simple_fold(f), f, "{c:X}"); + if c <= 0xFFFF { + let u = canonicalize(c, false); + assert!(u <= 0xFFFF); + assert!(c < 128 || u >= 128, "{c:X} became ASCII"); + } + } +} + +#[test] +fn word_characters_with_u_and_i_are_the_two_extras() { + // §22.2.2.9† WordCharacters: the extras are the characters that are not + // basic word characters but whose Canonicalize is. The matcher hard-codes + // them (U+017F and U+212A). + let basic = |c: u32| matches!(c, 0x30..=0x39 | 0x41..=0x5A | 0x61..=0x7A | 0x5F); + let mut extras = Vec::new(); + for c in (0..=0x10FFFFu32).step_by(if cfg!(miri) { 1 << 12 } else { 1 }) { + if !basic(c) && basic(simple_fold(c)) { + extras.push(c); + } + } + if !cfg!(miri) { + assert_eq!(extras, vec![0x17F, 0x212A]); + } + // Without u the canonical form of a non-ASCII unit is never ASCII. + assert!( + (0x80..=0xFFFFu32) + .step_by(stride()) + .all(|c| !basic(canonicalize(c, false))) + ); +} + +#[test] +fn every_property_name_resolves() { + use clazel_js_regexp::props::{BINARY_NAMES, GENERAL_CATEGORY_VALUES}; + use clazel_js_regexp::unicode::{binary_property, category_mask}; + for n in GENERAL_CATEGORY_VALUES { + let n = String::from_utf8_lossy(n); + assert!(category_mask(&n).is_some(), "{n}"); + } + for n in BINARY_NAMES { + let n = String::from_utf8_lossy(n); + assert!(binary_property(&n).is_some(), "{n}"); + } + assert!(category_mask("Latin").is_none()); + assert!(binary_property("Latin").is_none()); +} + +#[test] +fn category_groups_are_unions_of_their_members() { + use clazel_js_regexp::unicode::{category_mask, general_category}; + let m = |n: &str| category_mask(n).unwrap_or(0); + assert_eq!(m("L"), m("Lu") | m("Ll") | m("Lt") | m("Lm") | m("Lo")); + assert_eq!(m("LC"), m("Lu") | m("Ll") | m("Lt")); + assert_eq!(m("M"), m("Mn") | m("Mc") | m("Me")); + assert_eq!(m("N"), m("Nd") | m("Nl") | m("No")); + assert_eq!(m("Z"), m("Zs") | m("Zl") | m("Zp")); + assert_eq!( + m("P"), + m("Pc") | m("Pd") | m("Ps") | m("Pe") | m("Pi") | m("Pf") | m("Po") + ); + assert_eq!(m("S"), m("Sm") | m("Sc") | m("Sk") | m("So")); + assert_eq!(m("C"), m("Cc") | m("Cf") | m("Cs") | m("Co") | m("Cn")); + // Together the thirty are every bit of a 30-bit mask, and the table + // yields one of them for every code point. + let all = m("L") | m("M") | m("N") | m("P") | m("S") | m("Z") | m("C"); + assert_eq!(all, 0x3FFF_FFFF); + assert_eq!(general_category(0x41), 0); + assert_eq!(general_category(0x378), 29); + assert_eq!(general_category(0x10FFFF), 29); +} + +// --------------------------------------------------------------------------- +// Script, Script_Extensions and the properties of strings. + +#[test] +fn every_script_name_resolves() -> Result<(), String> { + use clazel_js_regexp::props::UcdScripts; + use clazel_js_regexp::unicode::script_property; + use clazel_js_regexp::{Flags, Names, compile, parse}; + let u = Flags { + unicode: true, + ..Flags::default() + }; + // Every name in the generated table, and the table's own consistency. + let mut seen = std::collections::BTreeSet::new(); + // The parse and compile of every spelling is slow under Miri: a stride. + let stride = if cfg!(miri) { 12 } else { 1 }; + for (i, s) in tables::SCRIPTS.iter().enumerate() { + assert!(!s.names.is_empty(), "{:?}", s.names); + for n in s.names { + assert!(seen.insert(*n), "{n} names two scripts"); + for ext in [false, true] { + assert!(script_property(n, ext).is_some(), "{n}"); + } + assert!(UcdScripts.is_script(n)); + for prop in ["sc", "Script", "scx", "Script_Extensions"] { + if i % stride != 0 { + break; + } + let text: Vec = format!("\\p{{{prop}={n}}}").encode_utf16().collect(); + let pat = parse(&text, u).map_err(|e| format!("{prop}={n}: {e}"))?; + compile(&pat, u).map_err(|e| format!("{prop}={n}: {e}"))?; + } + } + for table in [s.sc, s.scx] { + for w in table.windows(2) { + if let [a, b] = w { + assert!(a.1.saturating_add(1) < b.0, "{:?}: {a:?} {b:?}", s.names); + } + } + assert!(table.iter().all(|&(a, b)| a <= b && b <= LAST)); + } + } + // Case, spelling and neighbours are not names. + for n in ["latin", "LATIN", "Latn ", "Latin_", "Klingon", "Zzzzz", ""] { + assert!(script_property(n, false).is_none(), "{n:?}"); + assert!(!UcdScripts.is_script(n)); + } + // Spot checks of well-known names. + for n in [ + "Latin", + "Latn", + "Greek", + "Grek", + "Common", + "Zyyy", + "Inherited", + "Zinh", + "Qaai", + "Unknown", + "Zzzz", + "Hrkt", + "Han", + "Hani", + "Nko", + "Nkoo", + ] { + assert!(seen.contains(n), "{n}"); + } + assert!(tables::SCRIPTS.len() > 150); + Ok(()) +} + +/// The scripts and extensions the files give each code point, read again and +/// without the generator's helpers: `Script` as a name per code point (a gap +/// is `Unknown`), `Script_Extensions` as the short names of each row. +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn scripts_agree_with_the_ucd_for_every_code_point() -> Result<(), String> { + use clazel_js_regexp::unicode::script_property; + let n = LAST as usize + 1; + let mut long_of_short: BTreeMap = BTreeMap::new(); + for line in data("PropertyValueAliases.txt")?.lines() { + let body = line.split('#').next().unwrap_or(""); + let f: Vec<&str> = body.split(';').map(str::trim).collect(); + if let [kind, short, long, ..] = f.as_slice() + && *kind == "sc" + { + long_of_short.insert((*short).to_string(), (*long).to_string()); + } + } + let longs: Vec<&String> = long_of_short.values().collect(); + let mut sc: Vec<&str> = vec!["Unknown"; n]; + for line in data("Scripts.txt")?.lines() { + let body = line.split('#').next().unwrap_or(""); + if let Some((r, name)) = body.split_once(';') { + let (a, b) = range(r.trim())?; + let name = longs + .iter() + .find(|l| l.as_str() == name.trim()) + .ok_or_else(|| name.to_string())?; + for cp in a..=b { + let slot = sc.get_mut(cp as usize).ok_or("range")?; + if *slot != "Unknown" { + return Err(format!("{cp:X} listed twice")); + } + *slot = name.as_str(); + } + } + } + let mut scx: BTreeMap> = BTreeMap::new(); + for line in data("ScriptExtensions.txt")?.lines() { + let body = line.split('#').next().unwrap_or(""); + if let Some((r, names)) = body.split_once(';') { + let (a, b) = range(r.trim())?; + let list: Vec = names + .split_whitespace() + .map(|s| long_of_short.get(s).cloned().ok_or_else(|| s.to_string())) + .collect::>()?; + for cp in a..=b { + scx.insert(cp as usize, list.clone()); + } + } + } + // Every code point is in the table of its script and of each of its + // extensions; and the tables hold no more than that (their sizes add up + // to exactly the code points, and the (code point, script) pairs). + let mut pairs_expected = 0usize; + for cp in 0..n { + let c = u32::try_from(cp).map_err(|e| e.to_string())?; + let own = sc.get(cp).copied().unwrap_or("Unknown"); + let in_sc = script_property(own, false).ok_or_else(|| own.to_string())?; + if !in_sc.contains(c) { + return Err(format!("{c:X}: not in Script={own}")); + } + let list = scx + .get(&cp) + .cloned() + .unwrap_or_else(|| vec![own.to_string()]); + pairs_expected = pairs_expected.saturating_add(list.len()); + for l in &list { + let p = script_property(l, true).ok_or_else(|| l.clone())?; + if !p.contains(c) { + return Err(format!("{c:X}: not in Script_Extensions={l}")); + } + } + } + let size = |t: &[(u32, u32)]| { + t.iter() + .map(|&(a, b)| usize::try_from(b.saturating_sub(a)).map_or(0, |d| d.saturating_add(1))) + .fold(0usize, usize::saturating_add) + }; + let sc_total: usize = tables::SCRIPTS.iter().map(|s| size(s.sc)).sum(); + let scx_total: usize = tables::SCRIPTS.iter().map(|s| size(s.scx)).sum(); + if sc_total != n { + return Err(format!( + "Script tables hold {sc_total} code points, not {n}" + )); + } + if scx_total != pairs_expected { + return Err(format!( + "Script_Extensions tables hold {scx_total} pairs, the files give {pairs_expected}" + )); + } + println!( + "clazel-conformance: suite=\"UCD scripts per code point\" pass={n} xfail=0 fail=0 total={n}" + ); + Ok(()) +} + +/// The sequences of the emoji files, read again, against the tables. +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn string_properties_agree_with_the_emoji_files() -> Result<(), String> { + use clazel_js_regexp::props::STRING_PROPERTIES as NAMES; + use clazel_js_regexp::unicode::{StringSet, string_property}; + use std::collections::BTreeSet; + type Members = (BTreeSet, BTreeSet>); + let mut by_type: BTreeMap = BTreeMap::new(); + for file in ["emoji-sequences.txt", "emoji-zwj-sequences.txt"] { + for row in rows(&data(file)?) { + if let [cps, ty, ..] = row.as_slice() { + let e = by_type.entry(ty.clone()).or_default(); + if cps.contains(' ') { + let s: Vec = cps.split(' ').map(hex).collect::>()?; + e.1.insert(s); + } else { + let (a, b) = range(cps)?; + e.0.extend(a..=b); + } + } + } + } + assert_eq!(by_type.len(), 6, "{:?}", by_type.keys()); + let members = |set: &StringSet| -> Members { + let chars = (0..=LAST).filter(|&c| set.chars.contains(c)).collect(); + (chars, set.strings.iter().cloned().collect()) + }; + let mut union: Members = Members::default(); + for (ty, want) in &by_type { + let set = string_property(ty).ok_or_else(|| ty.clone())?; + let got = members(&set); + if &got != want { + return Err(format!("{ty}: the table differs from the file")); + } + if set.strings.len() != got.1.len() { + return Err(format!("{ty}: a string twice")); + } + union.0.extend(want.0.iter()); + union.1.extend(want.1.iter().cloned()); + assert!(want.1.iter().all(|s| s.len() >= 2)); + } + let all = string_property("RGI_Emoji").ok_or("RGI_Emoji")?; + if members(&all) != union { + return Err("RGI_Emoji is not the union of the six".into()); + } + assert_eq!( + all.strings.len(), + union.1.len(), + "RGI_Emoji repeats a string" + ); + for name in NAMES { + let name = String::from_utf8_lossy(name); + assert!(string_property(&name).is_some(), "{name}"); + } + assert!(string_property("Emoji").is_none()); + assert!(string_property("basic_emoji").is_none()); + assert!(union.1.len() > 2500, "{}", union.1.len()); + Ok(()) +} diff --git a/crates/clazel-js-syntax/src/arena_d.rs b/crates/clazel-js-syntax/src/arena_d.rs index 063f408fe..78370b6a7 100644 --- a/crates/clazel-js-syntax/src/arena_d.rs +++ b/crates/clazel-js-syntax/src/arena_d.rs @@ -5,20 +5,27 @@ //! [`Derivation::Verified`] only if `derives_root` holds for it: every node //! derives the tokens it spans, bottom-up, in the context the root's goal gives. //! -//! **Covered:** a `Script` or `Module` of statements, in the one context of the -//! root: literals, parentheses, every binary level, unary, conditional and -//! assignment, member-dot, identifiers, arguments, calls, the comma operator, -//! spread, `this`/`null`/`true`/`false`; and the statements expression, empty, -//! `var`/`let`/`const` (no destructuring), block, `if`, `while`, `do`-`while`, -//! `throw`, `break`/`continue`, labelled, `try`/`catch`/`finally`, `switch` and -//! `debugger`, `return`; function declarations and expressions, generators, -//! `async` functions, arrow functions (simple parameters, defaults and `...rest` -//! of a name), `yield` and `await`. Each node is checked in the context its -//! parent's production gives it (`Done::cxs`, [`Cx5`]); the assignment is +//! **Covered:** a `Script` or `Module` whose nodes are: literals, identifiers, +//! parentheses, every binary level, unary, `++`/`--`, conditional, assignment +//! (with assignment patterns), member access (`.name`, `.#name`, `[expr]`), +//! arguments, calls, `new`, the comma operator, spread, array and object +//! literals (properties, methods, accessors, computed names), templates and tagged +//! templates, `this`/`null`/`true`/`false`, function and class declarations and +//! expressions (generators, `async`, arrows, methods, fields, static blocks, +//! private names), `yield` and `await`, binding patterns; and every statement of +//! §14 (`for` with declarations and identifier or member heads, `with`, +//! `switch`, `try`, labels, ...). Each node is checked in the context its +//! parent's production gives it (`Done::cxs`, [`Cx5`]: `[~In]` in a `for` head, +//! `cx_fn` in functions, `[+Await]` in a static block, ...); the assignment is //! untrusted and compared by the parent's check. For any other node the result is -//! [`Derivation::Unsupported`]: nothing is claimed. The `for` statements (their -//! heads are `[~In]`, which the checks do not take yet), destructuring, classes, -//! array and object literals, templates and modules have no checks yet. +//! [`Derivation::Unsupported`]: nothing is claimed. Also covered, in [`crate::arena_e`] +//! and here: optional chains, `super`, `new.target`, `import(...)`, `import.meta`, +//! patterns and Annex B forms as `for` heads, `for await`, function declarations as +//! statement bodies, and operands of `[~In]` heads (bracketed operands are `[+In]`: +//! `cx_ok_t`, `cx_ok_init`). `CoverInitializedName` never survives the early errors; its +//! assignment-pattern reading is covered. Modules' `import` and `export` (all forms, with import +//! attributes) are in [`crate::arena_m`]. Not covered: class fields and other `[+Default]`-sensitive +//! members of an `export default class`, and class fields in `[~In]` heads. //! //! [`crate::exec::parse_checked`] runs [`check_derivation_canon`] on every arena //! the parser builds, and rejects it if a covered node is `Failed`. @@ -48,13 +55,21 @@ pub struct DTok { pub nl: bool, /// Written with a `UnicodeEscapeSequence`. pub escaped: bool, + /// A numeric literal that is legacy octal or non-octal decimal, or a string literal with a + /// legacy escape sequence (what `early_local` reads for strict mode code). + pub legacy: bool, + /// A template component with an undefined TV. + pub invalid_escape: bool, + /// The StringValue of an identifier name, escapes resolved. + pub name: Vec, } /// The executable token `e` is the specification's token `t` of the source `src`. pub open spec fn dtok_matches(src: Seq, e: DTok, t: Tok) -> bool { e.kind == t.kind && e.start as int == t.start && e.end as int == t.end && e.start <= e.end && e.end <= src.len() && t.text =~= src.subrange(e.start as int, e.end as int) - && e.escaped == t.escaped && e.nl == t.nl_before + && e.escaped == t.escaped && e.nl == t.nl_before && e.legacy == t.legacy + && e.invalid_escape == t.invalid_escape && e.name@ =~= t.name } /// The executable tokens and gaps are the specification's stream `ts`. @@ -76,7 +91,7 @@ pub open spec fn arena_of(nodes: Seq) -> AstV { } /// `ed` of a node that spans tokens `lo..hi` is `hi`. -proof fn lemma_ed_of(a: AstV, ts: Stream, c: int, lo: int, hi: int) +pub(crate) proof fn lemma_ed_of(a: AstV, ts: Stream, c: int, lo: int, hi: int) requires toks_wf(ts), 0 <= c < a.nodes.len(), @@ -157,7 +172,7 @@ proof fn lemma_empty_statement_derives(a: AstV, ts: Stream, n: int, cx: Ctx, i: /// §16.1 and §16.2: a root whose items derive consecutive token ranges that make /// the whole stream. `dl` is built from the back. -proof fn lemma_dl_cons(a: AstV, ts: Stream, bound: int, ks: Seq, k: int, cx: Ctx, i: int, m: int, j: int) +pub(crate) proof fn lemma_dl_cons(a: AstV, ts: Stream, bound: int, ks: Seq, k: int, cx: Ctx, i: int, m: int, j: int) requires 0 <= k < ks.len(), ed(a, ts, ks[k], i) == m, @@ -168,6 +183,15 @@ proof fn lemma_dl_cons(a: AstV, ts: Stream, bound: int, ks: Seq, k: int, cx { } +/// The items of a root are `StatementListItem`s (a `Script`) or `ModuleItem`s (a `Module`). +pub open spec fn root_items(a: AstV, ks: Seq, g: SourceGoal) -> bool { + if g == SourceGoal::Script { + all_items(a, ks) + } else { + forall|k: int| 0 <= k < ks.len() ==> is_module_item_kind(kd(a, #[trigger] ks[k])) + } +} + /// The `Script` or `Module` root `n` derives the whole stream. proof fn lemma_root_derives(a: AstV, ts: Stream, n: int, cx: Ctx, g: SourceGoal) requires @@ -177,7 +201,7 @@ proof fn lemma_root_derives(a: AstV, ts: Stream, n: int, cx: Ctx, g: SourceGoal) } else { NodeKind::Module }), - all_items(a, a.nodes[n].children), + root_items(a, a.nodes[n].children, g), dl(a, ts, n, a.nodes[n].children, 0, cx, 0, tn(ts)), span_ok(a, ts, n, 0, tn(ts)), ensures @@ -419,7 +443,7 @@ fn op_slice(op: Op) -> (r: &'static [u32]) } /// The source text of the token is the code points `w`. -fn text_is(src: &[u32], t: &DTok, w: &[u32]) -> (r: bool) +pub(crate) fn text_is(src: &[u32], t: &DTok, w: &[u32]) -> (r: bool) requires t.start <= t.end <= src.len(), ensures @@ -492,10 +516,10 @@ fn tok_is_op(src: &[u32], t: &DTok, op: Op) -> (r: bool) } /// The index of the first element, named so that it is not read as a request for `first()`. -const ZERO: usize = 0; +pub(crate) const ZERO: usize = 0; /// The source text of the token is the one code point `code`. -fn text_is1(src: &[u32], t: &DTok, code: u32) -> (r: bool) +pub(crate) fn text_is1(src: &[u32], t: &DTok, code: u32) -> (r: bool) requires t.start <= t.end <= src.len(), ensures @@ -532,7 +556,7 @@ pub open spec fn op_code(op: Op) -> u32 { } /// The text of the punctuators the check reads, as code points. -proof fn lemma_punct_text() +pub(crate) proof fn lemma_punct_text() ensures cps(";"@) =~= seq![0x3Bu32], cps("("@) =~= seq![0x28u32], @@ -625,12 +649,24 @@ fn cx_eq(p: &Cx5, q: &Cx5) -> (r: bool) /// Node `m` is checked in `cx`, and so are all its children (the productions of /// the statements and expressions the check covers pass their context down). pub open spec fn cx_ok(a: AstV, cxs: Seq, m: int, cx: Ctx) -> bool { - 0 <= m < a.nodes.len() && m < cxs.len() && cx_of(cxs[m]) == cx && (forall|k: int| + kids_ok(a, cxs, m, cx) && cx_of(cxs[m]) == cx +} + +/// Every child of node `m` is checked in `cx` (the node itself is checked in another +/// context where its production gives its children `[+In]`: [`cx_ok_t`]). +pub open spec fn kids_ok(a: AstV, cxs: Seq, m: int, cx: Ctx) -> bool { + 0 <= m < a.nodes.len() && m < cxs.len() && (forall|k: int| 0 <= k < a.nodes[m].children.len() ==> 0 <= a.nodes[m].children[k] < cxs.len() && cx_of( cxs[#[trigger] a.nodes[m].children[k]], ) == cx) } +/// Node `m` is checked in `cx` and its children in `cx_in(cx, true)`: the +/// productions whose operands are in brackets, `[+In]` whatever the context. +pub open spec fn cx_ok_t(a: AstV, cxs: Seq, m: int, cx: Ctx) -> bool { + kids_ok(a, cxs, m, cx_in(cx, true)) && cx_of(cxs[m]) == cx +} + /// What a processed node `j < m` has proven: it derives tokens `lo[j]..hi[j]` in /// its context `cxs[j]`, and has the binding strength `lv[j]`. #[verifier::opaque] @@ -781,7 +817,7 @@ pub open spec fn env_ok( } /// The children of a node of a well-formed arena are before it. -proof fn lemma_child_before(a: AstV, m: int, k: int) +pub(crate) proof fn lemma_child_before(a: AstV, m: int, k: int) requires ast_wf(a), 0 <= m < a.nodes.len(), @@ -852,8 +888,7 @@ proof fn lemma_binary_done( 0 <= m < a.nodes.len(), 0 <= l < m, 0 <= r < m, - cx_in(cx, true) == cx, - cx.in_, + a.nodes[m].op == Op::In ==> cx.in_, a.nodes[m].children =~= seq![l, r], is_bin_kind(a.nodes[m].kind), bin_op_ok(a.nodes[m].kind, a.nodes[m].op), @@ -929,8 +964,6 @@ fn check_binary( ) -> (r: Option<(usize, usize, i32)>) requires m < nodes.len(), - cx_in(g.cx, true) == g.cx, - g.cx.in_, env_ok(src@, nodes@, toks@, gaps@, g.ts), inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), @@ -955,7 +988,13 @@ fn check_binary( }, }; let (own, lmin, rmin, opok) = bin_info(nd.kind, nd.op); - if !opok || nd.children.len() != 2 { + let cxm = match done.cxs.get(m) { + Some(c) => c, + None => { + return None; + }, + }; + if !opok || nd.children.len() != 2 || (matches!(nd.op, Op::In) && !cxm.i) { return None; } let l = match nd.children.as_slice().get(ZERO) { @@ -1082,6 +1121,9 @@ fn check_binary( assert(is_op_tok(g.ts, mi as int, op)) by { lemma_op_chars(op); } + assert(cxm == &done.cxs@[m as int]); + assert(g.cx.in_ == cxm.i); + assert(a.nodes[m as int].op == nd.op); lemma_binary_done(a, g.ts, g.cx, m as int, l as int, r as int, lo_l as int, mi as int, lo_r as int, j as int); } Some((lo_l, j, own)) @@ -1169,7 +1211,7 @@ pub open spec fn reserved_chars(w: int) -> Seq { } /// The table of reserved words has the 38 words of the specification, in its order. -proof fn lemma_reserved_chars(w: int) +pub(crate) proof fn lemma_reserved_chars(w: int) requires 0 <= w < 38, ensures @@ -1257,7 +1299,7 @@ proof fn lemma_reserved_chars(w: int) } /// The `w`th reserved word, as code points. -fn reserved_slice(w: usize) -> (r: &'static [u32]) +pub(crate) fn reserved_slice(w: usize) -> (r: &'static [u32]) requires w < 38, ensures @@ -1576,6 +1618,18 @@ pub open spec fn item_ok_val(l: int, k: NodeKind, mode: int) -> bool { k == NodeKind::VariableDeclaration } else if mode == 3 { k == NodeKind::LexicalBinding + } else if mode == 5 { + k == NodeKind::PropertyDefinition || k == NodeKind::MethodDefinition + } else if mode == 7 { + k == NodeKind::BindingProperty || k == NodeKind::BindingRestProperty + } else if mode == 9 { + k == NodeKind::AssignmentProperty || k == NodeKind::AssignmentRestProperty + } else if mode == 11 { + k == NodeKind::ImportSpecifier + } else if mode == 13 { + k == NodeKind::ImportAttribute + } else if mode == 15 { + k == NodeKind::ExportSpecifier } else { k == NodeKind::BindingElement || k == NodeKind::FunctionRestParameter } @@ -1594,6 +1648,18 @@ fn item_ok_exec(l: i32, k: NodeKind, mode: u8) -> (r: bool) matches!(k, NodeKind::VariableDeclaration) } else if mode == 3 { matches!(k, NodeKind::LexicalBinding) + } else if mode == 5 { + matches!(k, NodeKind::PropertyDefinition | NodeKind::MethodDefinition) + } else if mode == 7 { + matches!(k, NodeKind::BindingProperty | NodeKind::BindingRestProperty) + } else if mode == 9 { + matches!(k, NodeKind::AssignmentProperty | NodeKind::AssignmentRestProperty) + } else if mode == 11 { + matches!(k, NodeKind::ImportSpecifier) + } else if mode == 13 { + matches!(k, NodeKind::ImportAttribute) + } else if mode == 15 { + matches!(k, NodeKind::ExportSpecifier) } else { matches!(k, NodeKind::BindingElement | NodeKind::FunctionRestParameter) } @@ -1616,7 +1682,7 @@ pub struct ListSpec { /// The items of the list `ls`: separated by `,`, each an assignment expression (or a /// spread element where `spread_ok`), and ending at token `e` (after a trailing `,` /// where `trail` allows one). `spec::dsep` of the children. -fn check_list( +pub(crate) fn check_list( src: &[u32], nodes: &[Node], toks: &[DTok], @@ -1627,10 +1693,9 @@ fn check_list( ) -> (r: Option) requires ls.m < nodes.len(), - cx_in(g.cx, true) == g.cx, env_ok(src@, nodes@, toks@, gaps@, g.ts), inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, ls.m as int), - cx_ok(arena_of(nodes@), done.cxs@, ls.m as int, g.cx), + kids_ok(arena_of(nodes@), done.cxs@, ls.m as int, g.cx), ensures match r { Some(e) => ls.start <= e && e <= toks.len() && dsep( @@ -1720,7 +1785,11 @@ fn check_list( } // The end: after a trailing `,` if one is allowed and there. let mut comma_after = false; - let tok_after = if ls.trail && !matches!(kc_kind, NodeKind::FunctionRestParameter) { + let tok_after = if ls.trail + && !matches!( + kc_kind, + NodeKind::FunctionRestParameter | NodeKind::BindingRestProperty | NodeKind::AssignmentRestProperty + ) { toks.get(hc) } else { None @@ -1783,10 +1852,9 @@ fn check_list( e <= toks.len(), pos <= e, ast_wf(a), - cx_in(g.cx, true) == g.cx, env_ok(src@, nodes@, toks@, gaps@, g.ts), inv(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), - cx_ok(a, done.cxs@, m as int, g.cx), + kids_ok(a, done.cxs@, m as int, g.cx), dsep( a, g.ts, @@ -1904,13 +1972,12 @@ fn check_list( proof fn lemma_arguments_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, e: int) requires toks_wf(ts), - cx_in(cx, true) == cx, 0 <= m < a.nodes.len(), 0 <= x, e < tn(ts), x + 1 <= e, a.nodes[m].kind == NodeKind::Arguments, - dsep(a, ts, m, a.nodes[m].children, 0, cx, x + 1, e, true), + dsep(a, ts, m, a.nodes[m].children, 0, cx_in(cx, true), x + 1, e, true), forall|k: int| 0 <= k < a.nodes[m].children.len() ==> list_item_ok( a, @@ -1957,10 +2024,9 @@ fn check_arguments( ) -> (r: Option<(usize, usize, i32)>) requires m < nodes.len(), - cx_in(g.cx, true) == g.cx, env_ok(src@, nodes@, toks@, gaps@, g.ts), inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), - cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + cx_ok_t(arena_of(nodes@), done.cxs@, m as int, g.cx), ensures match r { Some((l0, h0, l)) => node_done( @@ -2003,8 +2069,9 @@ fn check_arguments( != nd.start { return None; } + let ghost gt = GCx { ts: g.ts, cx: cx_in(g.cx, true) }; let ls = ListSpec { m, start: x.saturating_add(1), mode: 1, trail: true }; - let e = match check_list(src, nodes, toks, gaps, done, &ls, Ghost(g)) { + let e = match check_list(src, nodes, toks, gaps, done, &ls, Ghost(gt)) { Some(e) => e, None => { return None; @@ -2044,7 +2111,7 @@ fn check_arguments( assert(e < toks.len()); assert(x as int + 1 <= e as int) by { // The list starts at x + 1 and ends at e; its items lie between. - assert(dsep(a, g.ts, m as int, a.nodes[m as int].children, 0, g.cx, x as int + 1, e as int, true)); + assert(dsep(a, g.ts, m as int, a.nodes[m as int].children, 0, cx_in(g.cx, true), x as int + 1, e as int, true)); } lemma_arguments_done(a, g.ts, g.cx, m as int, x as int, e as int); } @@ -2279,7 +2346,6 @@ fn check_expression( ) -> (r: Option<(usize, usize, i32)>) requires m < nodes.len(), - cx_in(g.cx, true) == g.cx, env_ok(src@, nodes@, toks@, gaps@, g.ts), inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), @@ -2519,7 +2585,7 @@ fn check_spread( } /// The words `this`, `null`, `true` and `false`, as code points. -proof fn lemma_literal_words() +pub(crate) proof fn lemma_literal_words() ensures cps("this"@) =~= seq![0x74u32, 0x68, 0x69, 0x73], cps("null"@) =~= seq![0x6Eu32, 0x75, 0x6C, 0x6C], @@ -2658,7 +2724,7 @@ fn check_keyword_leaf( /// An `Absent` node at token position `p` of the check: placed where the production /// has the optional symbol (`spec::span_ok`, `spec::d`). -fn check_absent( +pub(crate) fn check_absent( src: &[u32], nodes: &[Node], toks: &[DTok], @@ -2745,7 +2811,7 @@ fn check_absent( /// The end of a statement that ends with `;`: a real `;` at token `hc`, or one /// inserted in the gap before it; `end` is where the statement's last token ends. -fn find_semi( +pub(crate) fn find_semi( src: &[u32], nodes: &[Node], toks: &[DTok], @@ -2837,6 +2903,7 @@ proof fn lemma_list_next( /// A `VariableDeclaration` or `LexicalBinding` of the check: a binding identifier and /// an optional initializer. #[verifier::spinoff_prover] +#[verifier::rlimit(150)] proof fn lemma_binding_done( a: AstV, ts: Stream, @@ -2849,6 +2916,7 @@ proof fn lemma_binding_done( j: int, with_init: bool, lo_i: int, + ci: Ctx, ) requires toks_wf(ts), @@ -2856,9 +2924,16 @@ proof fn lemma_binding_done( 0 <= t < m, 0 <= init < m, a.nodes[m].kind == NodeKind::VariableDeclaration || a.nodes[m].kind - == NodeKind::LexicalBinding || (a.nodes[m].kind == NodeKind::BindingElement && cx_in(cx, true) == cx), + == NodeKind::LexicalBinding || a.nodes[m].kind == NodeKind::BindingElement + || a.nodes[m].kind == NodeKind::AssignmentElement, + ci == (if a.nodes[m].kind == NodeKind::BindingElement || a.nodes[m].kind + == NodeKind::AssignmentElement { + cx_in(cx, true) + } else { + cx + }), a.nodes[m].children =~= seq![t, init], - kd(a, t) == NodeKind::BindingIdentifier, + if a.nodes[m].kind == NodeKind::AssignmentElement { is_destructuring_target(a, t) } else { is_binding_target_kind(kd(a, t)) }, node_done(a, ts, t, cx, lo_t, mt), a.nodes[m].start == ts.toks[lo_t].start, a.nodes[m].end == ts.toks[j - 1].end, @@ -2870,12 +2945,12 @@ proof fn lemma_binding_done( a, ts, init, - cx, + ci, lo_i, j, ) } else { - kd(a, init) == NodeKind::Absent && j == mt && d(a, ts, init, cx, mt, mt) && ed( + kd(a, init) == NodeKind::Absent && (a.nodes[m].kind == NodeKind::BindingElement || a.nodes[m].kind == NodeKind::AssignmentElement || !is_binding_pattern_kind(kd(a, t))) && j == mt && d(a, ts, init, ci, mt, mt) && ed( a, ts, init, @@ -2896,13 +2971,14 @@ proof fn lemma_binding_done( assert("="@ =~= seq!['=']); assert(is_p(ts, mt, "="@)); assert(is_assign_expr(a, init)); - assert(dc(a, ts, m, init, cx, lo_i, j)); - assert(d_init(a, ts, m, init, cx, mt, j, false)); + assert(dc(a, ts, m, init, ci, lo_i, j)); + assert(d_init(a, ts, m, init, ci, mt, j, false)); + assert(d_init(a, ts, m, init, ci, mt, j, true)); } else { - assert(dc(a, ts, m, init, cx, mt, mt)); - assert(d_init(a, ts, m, init, cx, mt, j, false)); + assert(dc(a, ts, m, init, ci, mt, mt)); + assert(d_init(a, ts, m, init, ci, mt, j, false)); } - if a.nodes[m].kind == NodeKind::BindingElement { + if a.nodes[m].kind == NodeKind::BindingElement || a.nodes[m].kind == NodeKind::AssignmentElement { assert(d_pat(a, ts, m, cx, lo_t, j)); } else { assert(d_stmt(a, ts, m, cx, lo_t, j)); @@ -2923,10 +2999,20 @@ fn check_binding( ) -> (r: Option<(usize, usize, i32)>) requires m < nodes.len(), - cx_in(g.cx, true) == g.cx, env_ok(src@, nodes@, toks@, gaps@, g.ts), inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), - cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + cx_ok_init( + arena_of(nodes@), + done.cxs@, + m as int, + g.cx, + if nodes@[m as int].kind == NodeKind::BindingElement || nodes@[m as int].kind + == NodeKind::AssignmentElement { + cx_in(g.cx, true) + } else { + g.cx + }, + ), ensures match r { Some((l0, h0, l)) => node_done( @@ -2946,10 +3032,16 @@ fn check_binding( return None; }, }; - if !matches!(nd.kind, NodeKind::VariableDeclaration | NodeKind::LexicalBinding | NodeKind::BindingElement) + if !matches!(nd.kind, NodeKind::VariableDeclaration | NodeKind::LexicalBinding | NodeKind::BindingElement | NodeKind::AssignmentElement) || nd.children.len() != 2 { return None; } + let ghost ci: Ctx = if nd.kind == NodeKind::BindingElement || nd.kind == NodeKind::AssignmentElement { + cx_in(g.cx, true) + } else { + g.cx + }; + let ghost gi = GCx { ts: g.ts, cx: ci }; let t = match nd.children.as_slice().get(ZERO) { Some(x) => *x as usize, None => { @@ -2984,8 +3076,22 @@ fn check_binding( return None; }, }; + let lv_t = match done.lv.get(t) { + Some(x) => *x, + None => { + return None; + }, + }; let target_ok = match nodes.get(t) { - Some(x) => matches!(x.kind, NodeKind::BindingIdentifier), + Some(x) => if matches!(nd.kind, NodeKind::AssignmentElement) { + matches!(x.kind, NodeKind::ObjectAssignmentPattern | NodeKind::ArrayAssignmentPattern) + || (lv_t >= 14 && !matches!(x.kind, NodeKind::ObjectLiteral | NodeKind::ArrayLiteral)) + } else { + matches!( + x.kind, + NodeKind::BindingIdentifier | NodeKind::ObjectBindingPattern | NodeKind::ArrayBindingPattern + ) + }, None => false, }; if !target_ok { @@ -3002,7 +3108,12 @@ fn check_binding( }, }; if init_absent { - if !check_absent(src, nodes, toks, gaps, init, mt, Ghost(g)) { + let pat = !matches!(nd.kind, NodeKind::BindingElement | NodeKind::AssignmentElement) + && match nodes.get(t) { + Some(x) => !matches!(x.kind, NodeKind::BindingIdentifier), + None => true, + }; + if pat || !check_absent(src, nodes, toks, gaps, init, mt, Ghost(gi)) { return None; } let prev = match toks.get(mt.saturating_sub(1)) { @@ -3016,7 +3127,7 @@ fn check_binding( } proof { let a = arena_of(nodes@); - lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, t as int, g.cx, 0); + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, t as int, g.cx, ci, 0); lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_t as int); lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, mt as int - 1); assert(nd == &nodes@[m as int]); @@ -3033,7 +3144,7 @@ fn check_binding( assert(first.start as int == g.ts.toks[lo_t as int].start); assert(prev.end as int == g.ts.toks[mt as int - 1].end); assert(mt <= toks.len()); - lemma_binding_done(a, g.ts, g.cx, m as int, t as int, init as int, lo_t as int, mt as int, mt as int, false, 0); + lemma_binding_done(a, g.ts, g.cx, m as int, t as int, init as int, lo_t as int, mt as int, mt as int, false, 0, ci); } return Some((lo_t, mt, -100)); } @@ -3077,8 +3188,8 @@ fn check_binding( } proof { let a = arena_of(nodes@); - lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, t as int, g.cx, 0); - lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, init as int, g.cx, 1); + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, t as int, g.cx, ci, 0); + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, init as int, g.cx, ci, 1); lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_t as int); lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, j as int - 1); assert(nd == &nodes@[m as int]); @@ -3100,10 +3211,10 @@ fn check_binding( assert(g.ts.toks[mt as int].kind == TokKind::Punctuator); assert(g.ts.toks[mt as int].text =~= seq![0x3Du32]); assert(lvl(a, init as int) >= 0); - assert(node_done(a, g.ts, init as int, g.cx, lo_i as int, j as int)); + assert(node_done(a, g.ts, init as int, ci, lo_i as int, j as int)); assert(mt < j); assert(j <= toks.len()); - lemma_binding_done(a, g.ts, g.cx, m as int, t as int, init as int, lo_t as int, mt as int, j as int, true, lo_i as int); + lemma_binding_done(a, g.ts, g.cx, m as int, t as int, init as int, lo_t as int, mt as int, j as int, true, lo_i as int, ci); } Some((lo_t, j, -100)) } @@ -3137,7 +3248,7 @@ proof fn lemma_decl_stmt_done( ) requires toks_wf(ts), - cx_in(cx, true) == cx, + a.nodes[m].kind == NodeKind::VariableStatement ==> cx_in(cx, true) == cx, 0 <= m < a.nodes.len(), 0 <= x < tn(ts), a.nodes[m].children.len() > 0, @@ -3209,7 +3320,7 @@ fn check_decl_stmt( ) -> (r: Option<(usize, usize, i32)>) requires m < nodes.len(), - cx_in(g.cx, true) == g.cx, + nodes@[m as int].kind == NodeKind::VariableStatement ==> cx_in(g.cx, true) == g.cx, env_ok(src@, nodes@, toks@, gaps@, g.ts), inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), @@ -3344,6 +3455,15 @@ fn stmt_kind_exec(k: NodeKind) -> (r: bool) ) } +/// A `Statement` kind or a `FunctionDeclaration`, executable (`spec::is_stmt_or_function`): the +/// body of an `if` clause (Annex B.3.3) or a label (§14.13, Annex B.3.1). +fn stmt_or_fn_exec(k: NodeKind) -> (r: bool) + ensures + r ==> (is_statement_kind(k) || k == NodeKind::FunctionDeclaration), +{ + stmt_kind_exec(k) || matches!(k, NodeKind::FunctionDeclaration) +} + /// A `StatementListItem` kind, executable (`spec::is_item_kind`). fn item_kind_exec(k: NodeKind) -> (r: bool) ensures @@ -3555,7 +3675,7 @@ proof fn lemma_block_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, e: int) } /// The brace texts as code points. -proof fn lemma_brace_text() +pub(crate) proof fn lemma_brace_text() ensures cps("{"@) =~= seq![0x7Bu32], cps("}"@) =~= seq![0x7Du32], @@ -3877,7 +3997,7 @@ fn check_if( }, }; let c_ok = match nodes.get(c) { - Some(x) => stmt_kind_exec(x.kind), + Some(x) => stmt_or_fn_exec(x.kind), None => false, }; let kw = match toks.get(x) { @@ -3930,7 +4050,7 @@ fn check_if( (q, 0) } else { let alt_ok = match nodes.get(alt) { - Some(x) => stmt_kind_exec(x.kind), + Some(x) => stmt_or_fn_exec(x.kind), None => false, }; let ej = match done.hi.get(alt) { @@ -3999,7 +4119,6 @@ fn check_if( assert(a.nodes[m as int].end == nd.end as int); assert(kd(a, c as int) == nodes@[c as int].kind); assert(kd(a, c as int) != NodeKind::Absent); - assert(is_statement_kind(kd(a, c as int))); assert(is_stmt_or_function(a, c as int)); assert(last.end as int == g.ts.toks[j as int - 1].end); assert(kw.start as int == g.ts.toks[x as int].start); @@ -4019,7 +4138,6 @@ fn check_if( lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, q as int); assert(q < toks.len()); assert(kd(a, alt as int) == nodes@[alt as int].kind); - assert(is_statement_kind(kd(a, alt as int))); assert(is_stmt_or_function(a, alt as int)); assert(kd(a, alt as int) != NodeKind::Absent); lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, alt as int, g.cx, 2); @@ -4041,7 +4159,7 @@ proof fn lemma_comma_text() } /// The words that cannot start an `ExpressionStatement` (§14.5), as code points. -proof fn lemma_kw_text() +pub(crate) proof fn lemma_kw_text() ensures cps("function"@) =~= seq![0x66u32, 0x75, 0x6E, 0x63, 0x74, 0x69, 0x6F, 0x6E], cps("class"@) =~= seq![0x63u32, 0x6C, 0x61, 0x73, 0x73], @@ -4227,7 +4345,6 @@ proof fn lemma_conditional_done( ) requires toks_wf(ts), - cx_in(cx, true) == cx, 0 <= m < a.nodes.len(), 0 <= t < m, 0 <= c < m, @@ -4238,7 +4355,7 @@ proof fn lemma_conditional_done( lvl(a, c) >= 0, lvl(a, e) >= 0, node_done(a, ts, t, cx, lo_t, mt), - node_done(a, ts, c, cx, lo_c, mc), + node_done(a, ts, c, cx_in(cx, true), lo_c, mc), node_done(a, ts, e, cx, lo_e, j), lo_c == mt + 1, lo_e == mc + 1, @@ -4275,10 +4392,9 @@ fn check_conditional( ) -> (r: Option<(usize, usize, i32)>) requires m < nodes.len(), - cx_in(g.cx, true) == g.cx, env_ok(src@, nodes@, toks@, gaps@, g.ts), inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), - cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + cx_ok_init(arena_of(nodes@), done.cxs@, m as int, g.cx, cx_in(g.cx, true)), ensures match r { Some((l0, h0, l)) => node_done( @@ -4438,9 +4554,9 @@ fn check_conditional( assert(a.nodes[m as int].children[1] == c as int); assert(a.nodes[m as int].children[2] == e as int); } - lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, t as int, g.cx, 0); - lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 1); - lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, e as int, g.cx, 2); + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, t as int, g.cx, cx_in(g.cx, true), 0); + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, cx_in(g.cx, true), 1); + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, e as int, g.cx, cx_in(g.cx, true), 2); lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_t as int); lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, j as int - 1); assert(mt < toks.len()); @@ -4486,9 +4602,11 @@ proof fn lemma_assign_done( a.nodes[m].kind == NodeKind::AssignmentExpression, a.nodes[m].children =~= seq![l, r], is_assign_op(a.nodes[m].op), - lvl(a, l) >= 14, - kd(a, l) != NodeKind::ObjectLiteral, - kd(a, l) != NodeKind::ArrayLiteral, + if a.nodes[m].op == Op::Assign { + is_destructuring_target(a, l) + } else { + is_lhs_expr(a, l) + }, lvl(a, r) >= 0, node_done(a, ts, l, cx, lo_l, mi), node_done(a, ts, r, cx, lo_r, j), @@ -4502,8 +4620,6 @@ proof fn lemma_assign_done( lvl(a, m) == 0, { assert(span_ok(a, ts, m, lo_l, j)); - assert(is_lhs_expr(a, l)); - assert(is_destructuring_target(a, l)); crate::derive::lemma_assignment_derives(a, ts, m, cx, lo_l, mi, j, l, r); lemma_ed_of(a, ts, m, lo_l, j); } @@ -4627,7 +4743,13 @@ fn check_assign( Some(x) => !matches!(x.kind, NodeKind::ObjectLiteral | NodeKind::ArrayLiteral), None => false, }; - if lvl_l < 14 || !left_plain || lvl_r < 0 || lo_r != mi.saturating_add(1) { + let left_pattern = matches!(nd.op, Op::Assign) + && matches!( + kind_at(nodes, l), + NodeKind::ObjectAssignmentPattern | NodeKind::ArrayAssignmentPattern + ); + if !((lvl_l >= 14 && left_plain) || left_pattern) || lvl_r < 0 + || lo_r != mi.saturating_add(1) { return None; } let opt = match toks.get(mi) { @@ -4894,7 +5016,7 @@ fn check_member_dot( } /// Node `j < m`, the child `k` of node `m` checked in `cx`, is done (`inv`). -proof fn lemma_inv_get( +pub(crate) proof fn lemma_inv_get( a: AstV, ts: Stream, cxs: Seq, @@ -4908,7 +5030,7 @@ proof fn lemma_inv_get( ) requires inv(a, ts, cxs, lo, hi, lv, m), - cx_ok(a, cxs, m, cx), + kids_ok(a, cxs, m, cx), 0 <= j < m, 0 <= k < a.nodes[m].children.len(), a.nodes[m].children[k] == j, @@ -4926,8 +5048,81 @@ proof fn lemma_inv_get( } } +/// Node `m` is checked in `cx`, its second child (the initializer of a pattern +/// element) in `ci`, and every other child in `cx`: the productions with an +/// `Initializer[+In]` inside a `[~In]` head. +pub open spec fn cx_ok_init(a: AstV, cxs: Seq, m: int, cx: Ctx, ci: Ctx) -> bool { + 0 <= m < a.nodes.len() && m < cxs.len() && cx_of(cxs[m]) == cx && (forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> 0 <= a.nodes[m].children[k] < cxs.len() && cx_of( + cxs[#[trigger] a.nodes[m].children[k]], + ) == (if k == 1 { + ci + } else { + cx + })) +} + +/// [`lemma_inv_get`] for a node under [`cx_ok_init`]. +pub(crate) proof fn lemma_inv_get_init( + a: AstV, + ts: Stream, + cxs: Seq, + lo: Seq, + hi: Seq, + lv: Seq, + m: int, + j: int, + cx: Ctx, + ci: Ctx, + k: int, +) + requires + inv(a, ts, cxs, lo, hi, lv, m), + cx_ok_init(a, cxs, m, cx, ci), + 0 <= j < m, + 0 <= k < a.nodes[m].children.len(), + a.nodes[m].children[k] == j, + ensures + kd(a, j) != NodeKind::Absent ==> node_done( + a, + ts, + j, + if k == 1 { + ci + } else { + cx + }, + lo[j] as int, + hi[j] as int, + ), + lvl(a, j) != -100 ==> node_done( + a, + ts, + j, + if k == 1 { + ci + } else { + cx + }, + lo[j] as int, + hi[j] as int, + ), + lv[j] as int == lvl(a, j), +{ + reveal(inv); + assert(cx_of(cxs[a.nodes[m].children[k]]) == (if k == 1 { + ci + } else { + cx + })); + assert(lv[j] as int == lvl(a, j)); + if kd(a, j) == NodeKind::Absent { + assert(lvl(a, j) == -100); + } +} + /// Token `k` of the executable tokens is the specification's. -proof fn lemma_env_tok(src: Seq, nodes: Seq, toks: Seq, gaps: Seq, ts: Stream, k: int) +pub(crate) proof fn lemma_env_tok(src: Seq, nodes: Seq, toks: Seq, gaps: Seq, ts: Stream, k: int) requires env_ok(src, nodes, toks, gaps, ts), 0 <= k < toks.len(), @@ -4938,7 +5133,7 @@ proof fn lemma_env_tok(src: Seq, nodes: Seq, toks: Seq, gaps: S } /// The specification's tokens are not empty and their ends increase. -proof fn lemma_env_toks_wf(src: Seq, nodes: Seq, toks: Seq, gaps: Seq, ts: Stream) +pub(crate) proof fn lemma_env_toks_wf(src: Seq, nodes: Seq, toks: Seq, gaps: Seq, ts: Stream) requires env_ok(src, nodes, toks, gaps, ts), ensures @@ -5005,6 +5200,7 @@ proof fn lemma_env_gap(src: Seq, nodes: Seq, toks: Seq, gaps: S } /// Extending the processed nodes by one. +#[verifier::rlimit(100)] proof fn lemma_inv_push( a: AstV, ts: Stream, @@ -5035,23 +5231,28 @@ proof fn lemma_inv_push( assert(lo2.len() == m + 1); assert(hi2.len() == m + 1); assert(lv2.len() == m + 1); - assert forall|j: int| 0 <= j < m + 1 implies (#[trigger] lv2[j]) as int == lvl(a, j) - && (kd(a, j) != NodeKind::Absent ==> node_done( + assert forall|j: int| 0 <= j < m + 1 implies (#[trigger] lv2[j]) as int == lvl(a, j) by { + if j < m { + assert(lv2[j] == lv[j]); + } else { + assert(lv2[j] == l); + } + } + assert forall|j: int| 0 <= j < m + 1 implies (kd(a, j) != NodeKind::Absent ==> node_done( a, ts, j, cx_of(cxs[j]), - lo2[j] as int, + #[trigger] lo2[j] as int, hi2[j] as int, )) by { if j < m { + assert(lv[j] as int == lvl(a, j)); assert(lo2[j] == lo[j]); assert(hi2[j] == hi[j]); - assert(lv2[j] == lv[j]); } else { assert(lo2[j] == l0); assert(hi2[j] == h0); - assert(lv2[j] == l); } } } @@ -5059,6 +5260,7 @@ proof fn lemma_inv_push( /// Extending the processed nodes by an `Absent` node, which has no token range of /// its own: its parent places it. +#[verifier::rlimit(100)] proof fn lemma_inv_push_absent( a: AstV, ts: Stream, @@ -5118,12 +5320,11 @@ proof fn lemma_paren_done( ) requires toks_wf(ts), - cx_in(cx, true) == cx, 0 <= m < a.nodes.len(), 0 <= c < m, a.nodes[m].kind == NodeKind::ParenthesizedExpression, a.nodes[m].children =~= seq![c], - node_done(a, ts, c, cx, lo_c, hc), + node_done(a, ts, c, cx_in(cx, true), lo_c, hc), lvl(a, c) >= -1, 1 <= lo_c, hc < tn(ts), @@ -5232,10 +5433,9 @@ fn check_paren( ) -> (r: Option<(usize, usize, i32)>) requires m < nodes.len(), - cx_in(g.cx, true) == g.cx, env_ok(src@, nodes@, toks@, gaps@, g.ts), inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), - cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + cx_ok_t(arena_of(nodes@), done.cxs@, m as int, g.cx), ensures match r { Some((l0, h0, l)) => node_done(arena_of(nodes@), g.ts, m as int, g.cx, l0 as int, h0 as int) @@ -5320,7 +5520,7 @@ fn check_paren( assert(a.nodes[m as int].children.len() == 1); assert(a.nodes[m as int].children[0] == c as int); } - lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 0); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, cx_in(g.cx, true), 0); assert(a.nodes[m as int].kind == nd.kind); assert(a.nodes[m as int].start == nd.start as int); assert(a.nodes[m as int].end == nd.end as int); @@ -5678,7 +5878,7 @@ fn check_root( cx_ok(a, done.cxs@, root as int, cx), dl(a, ts, root as int, a.nodes[root as int].children, k as int, cx, pos as int, nt as int), forall|j: int| - k <= j < len ==> is_item_kind(kd(a, #[trigger] a.nodes[root as int].children[j])), + k <= j < len ==> (if module { is_module_item_kind(kd(a, #[trigger] a.nodes[root as int].children[j])) } else { is_item_kind(kd(a, #[trigger] a.nodes[root as int].children[j])) }), decreases k, { let c = match nd.children.as_slice().get(k.saturating_sub(1)) { @@ -5704,7 +5904,7 @@ fn check_root( }, }; let item = match nodes.get(c) { - Some(x) => item_kind_exec(x.kind), + Some(x) => item_kind_exec(x.kind) || (module && matches!(x.kind, NodeKind::ImportDeclaration | NodeKind::ExportDeclaration)), None => false, }; if hi_c != pos || !item { @@ -5715,7 +5915,7 @@ fn check_root( assert(a.nodes[c as int] == nodes@[c as int]@); let ks = a.nodes[root as int].children; lemma_dl_cons(a, ts, root as int, ks, k as int - 1, cx, lo_c as int, hi_c as int, nt as int); - assert(is_item_kind(kd(a, c as int))); + assert(is_module_item_kind(kd(a, c as int))); } pos = lo_c; k = k.saturating_sub(1); @@ -5754,7 +5954,7 @@ fn check_root( lemma_env_tok(src@, nodes@, toks@, gaps@, ts, nt as int - 1); assert(span_ok(a, ts, root as int, 0, tn(ts))); } - assert(all_items(a, a.nodes[root as int].children)); + assert(root_items(a, a.nodes[root as int].children, goal_of(module))); lemma_root_derives(a, ts, root as int, cx, goal_of(module)); } true @@ -5767,6 +5967,7 @@ fn check_root( /// (`gaps.len() == toks.len() + 1`). Proven: `Verified` implies /// `derives_root(ast, ts, goal)` for every stream `ts` that is the executable /// one (`stream_matches`), which is the assumption; see the module header. +#[verifier::rlimit(400)] pub fn check_derivation( src: &[u32], nodes: &[Node], @@ -5921,20 +6122,136 @@ pub fn check_derivation( return Derivation::Failed; }, }; - // The statements and expressions covered so far are checked with `[+In]`. - if !cxm.i || cxm.d { - return Derivation::Unsupported; - } + // Most statements and expressions are checked with `[+In]` and no `[Default]`. + let plain = cxm.i && !cxm.d; proof { lemma_env_toks_wf(src@, nodes@, toks@, gaps@, ts); } let ghost cx = cx_of(cxm); proof { - assert(cx_in(cx, true) == cx); + if plain { + assert(cx_in(cx, true) == cx); + assert(!cx.deflt); + } assert(cxm == done.cxs@[m as int]); } let is_arrow = matches!(nd.kind, NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction); - let res = if is_arrow { + let is_for = matches!( + nd.kind, + NodeKind::ForStatement | NodeKind::ForInStatement | NodeKind::ForOfStatement + ); + let res = if is_for { + if !plain { + return Derivation::Unsupported; + } + let cxi = cx5_noin(&cxm); + if matches!(nd.kind, NodeKind::ForStatement) { + if !kids_cx4(nodes, done.cxs.as_slice(), m, &cxi, &cxm, &cxm, &cxm) { + return Derivation::Unsupported; + } + proof { + assert(nd == &nodes@[m as int]); + } + check_for(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + } else { + if !kids_cx3(nodes, done.cxs.as_slice(), m, &cxi, &cxm, &cxm) { + return Derivation::Unsupported; + } + let sh = InOfShape { m, of: matches!(nd.kind, NodeKind::ForOfStatement) }; + proof { + assert(nd == &nodes@[m as int]); + } + check_for_in_of(src, nodes, toks, gaps, &done, &sh, Ghost(GCx { ts, cx })) + } + } else if matches!(nd.kind, NodeKind::MethodDefinition) { + let is_async = matches!(nd.op, Op::AsyncMethod | Op::AsyncGeneratorMethod); + let is_gen = matches!(nd.op, Op::GeneratorMethod | Op::AsyncGeneratorMethod); + let (c0, c1, c2) = fn_cx5(&cxm, is_async, is_gen, false); + if !kids_cx3(nodes, done.cxs.as_slice(), m, &c0, &c1, &c2) { + return Derivation::Unsupported; + } + proof { + assert(nd == &nodes@[m as int]); + } + check_method(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + } else if matches!(nd.kind, NodeKind::ClassStaticBlock) { + let c0 = Cx5 { y: false, a: true, i: true, r: false, d: false }; + if !kids_cx1(nodes, done.cxs.as_slice(), m, &c0) { + return Derivation::Unsupported; + } + proof { + assert(nd == &nodes@[m as int]); + } + check_static_block(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + } else if matches!( + nd.kind, + NodeKind::VariableDeclaration | NodeKind::LexicalBinding | NodeKind::BindingElement + | NodeKind::AssignmentElement | NodeKind::AssignmentProperty + ) { + // A binding or pattern element has an `Initializer[+In]`; a declaration has `[?In]`. + let own_init = matches!(nd.kind, NodeKind::BindingElement | NodeKind::AssignmentElement) + || (matches!(nd.kind, NodeKind::AssignmentProperty) && matches!(nd.op, Op::Shorthand)); + let ci = cx5_in(&cxm); + let c1 = if own_init { ci } else { cxm }; + if !kids_init(nodes, done.cxs.as_slice(), m, &cxm, &c1) { + return Derivation::Unsupported; + } + proof { + assert(nd == &nodes@[m as int]); + } + if matches!(nd.kind, NodeKind::AssignmentProperty) { + check_assignment_property(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + } else { + check_binding(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + } + } else if matches!(nd.kind, NodeKind::ExportDeclaration) { + // The declaration of `export default` has `[+Default]`. + let dd = matches!(nd.op, Op::ExportDefaultHoistable | Op::ExportDefaultClass); + let ck = if dd { Cx5 { y: cxm.y, a: cxm.a, i: cxm.i, r: cxm.r, d: true } } else { cxm }; + if !plain || !kids_cx_g(nodes, done.cxs.as_slice(), m, &cxm, &ck) { + return Derivation::Unsupported; + } + proof { + assert(nd == &nodes@[m as int]); + } + crate::arena_m::check_export(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + } else if matches!(nd.kind, NodeKind::ConditionalExpression) + || (matches!(nd.kind, NodeKind::MemberExpression) && matches!(nd.op, Op::Bracket)) + { + // The second child (the consequent, the bracketed property) is `[+In]`. + let ci = cx5_in(&cxm); + if !kids_init(nodes, done.cxs.as_slice(), m, &cxm, &ci) { + return Derivation::Unsupported; + } + proof { + assert(nd == &nodes@[m as int]); + } + if matches!(nd.kind, NodeKind::ConditionalExpression) { + check_conditional(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + } else { + check_member_bracket(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + } + } else if matches!( + nd.kind, + NodeKind::ParenthesizedExpression | NodeKind::Arguments | NodeKind::ArrayLiteral + | NodeKind::ObjectLiteral | NodeKind::TemplateLiteral + ) { + // Operands in brackets are `[+In]` whatever the context of the node. + let ct = cx5_in(&cxm); + if !kids_cx_t(nodes, done.cxs.as_slice(), m, &cxm, &ct) { + return Derivation::Unsupported; + } + proof { + assert(nd == &nodes@[m as int]); + } + match nd.kind { + NodeKind::ParenthesizedExpression => check_paren(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::Arguments => check_arguments(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ArrayLiteral => check_array(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ObjectLiteral => check_object(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + _ => check_template(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + } + } else if is_arrow { let is_async = matches!(nd.kind, NodeKind::AsyncArrowFunction); if nd.children.len() != 2 { return Derivation::Unsupported; @@ -5979,22 +6296,43 @@ pub fn check_derivation( if !kids_cx(nodes, done.cxs.as_slice(), m, &cxm) { return Derivation::Unsupported; } + // The kinds whose checks do not need `[+In]`. + let any_ctx = matches!( + nd.kind, + NodeKind::NumericLiteral | NodeKind::StringLiteral | NodeKind::RegularExpressionLiteral + | NodeKind::IdentifierName | NodeKind::AdditiveExpression | NodeKind::MultiplicativeExpression + | NodeKind::ExponentiationExpression | NodeKind::ShiftExpression | NodeKind::EqualityExpression + | NodeKind::BitwiseANDExpression | NodeKind::BitwiseXORExpression | NodeKind::BitwiseORExpression + | NodeKind::LogicalANDExpression | NodeKind::LogicalORExpression | NodeKind::CoalesceExpression + | NodeKind::RelationalExpression | NodeKind::IdentifierReference | NodeKind::BindingIdentifier + | NodeKind::LabelIdentifier | NodeKind::ThisExpression | NodeKind::NullLiteral + | NodeKind::BooleanLiteral | NodeKind::UnaryExpression | NodeKind::AssignmentExpression + | NodeKind::MemberExpression | NodeKind::VariableDeclaration | NodeKind::LexicalBinding + | NodeKind::VariableDeclarationList | NodeKind::ForDeclaration | NodeKind::LexicalDeclaration + | NodeKind::UpdateExpression | NodeKind::NewTarget | NodeKind::ImportMeta + | NodeKind::SuperProperty | NodeKind::SuperCall | NodeKind::OptionalChain + | NodeKind::OptionalExpression | NodeKind::ArrayBindingPattern + | NodeKind::ObjectBindingPattern | NodeKind::ArrayAssignmentPattern + | NodeKind::ObjectAssignmentPattern | NodeKind::BindingProperty + | NodeKind::BindingRestElement | NodeKind::BindingRestProperty + | NodeKind::AssignmentRestElement | NodeKind::AssignmentRestProperty + | NodeKind::Elision | NodeKind::CallExpression | NodeKind::NewExpression + | NodeKind::TaggedTemplate | NodeKind::Expression | NodeKind::ClassDeclaration + | NodeKind::ClassExpression | NodeKind::ClassBody | NodeKind::AwaitExpression + | NodeKind::YieldExpression + ); + if !plain && !any_ctx { + return Derivation::Unsupported; + } match nd.kind { + NodeKind::VariableDeclarationList => check_var_list(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ForDeclaration => check_for_decl(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::NumericLiteral | NodeKind::StringLiteral | NodeKind::RegularExpressionLiteral | NodeKind::IdentifierName => { proof { lemma_env_literal_facts(src@, nodes@, toks@, gaps@, ts); } check_literal(nodes, toks, &done.by_start, m, Ghost(ts), Ghost(cx)) }, - NodeKind::ParenthesizedExpression => check_paren( - src, - nodes, - toks, - gaps, - &done, - m, - Ghost(GCx { ts, cx }), - ), NodeKind::AdditiveExpression | NodeKind::MultiplicativeExpression | NodeKind::ExponentiationExpression | NodeKind::ShiftExpression | NodeKind::EqualityExpression | NodeKind::BitwiseANDExpression | NodeKind::BitwiseXORExpression | NodeKind::BitwiseORExpression | NodeKind::LogicalANDExpression | NodeKind::LogicalORExpression | NodeKind::CoalesceExpression | NodeKind::RelationalExpression => check_binary( src, @@ -6035,15 +6373,6 @@ pub fn check_derivation( m, Ghost(GCx { ts, cx }), ), - NodeKind::VariableDeclaration | NodeKind::LexicalBinding | NodeKind::BindingElement => check_binding( - src, - nodes, - toks, - gaps, - &done, - m, - Ghost(GCx { ts, cx }), - ), NodeKind::VariableStatement | NodeKind::LexicalDeclaration => check_decl_stmt( src, nodes, @@ -6053,15 +6382,6 @@ pub fn check_derivation( m, Ghost(GCx { ts, cx }), ), - NodeKind::Arguments => check_arguments( - src, - nodes, - toks, - gaps, - &done, - m, - Ghost(GCx { ts, cx }), - ), NodeKind::CallExpression => check_call( src, nodes, @@ -6098,15 +6418,6 @@ pub fn check_derivation( m, Ghost(GCx { ts, cx }), ), - NodeKind::ConditionalExpression => check_conditional( - src, - nodes, - toks, - gaps, - &done, - m, - Ghost(GCx { ts, cx }), - ), NodeKind::AssignmentExpression => check_assign( src, nodes, @@ -6116,15 +6427,17 @@ pub fn check_derivation( m, Ghost(GCx { ts, cx }), ), - NodeKind::MemberExpression => check_member_dot( - src, - nodes, - toks, - gaps, - &done, - m, - Ghost(GCx { ts, cx }), - ), + NodeKind::MemberExpression => { + if matches!(nd.op, Op::Bracket) { + return Derivation::Unsupported; + } else if matches!(nd.op, Op::PrivateDot) { + check_member_private(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + } else { + check_member_dot(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + } + }, + NodeKind::UpdateExpression => check_update(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::NewExpression => check_new(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::ExpressionStatement => check_exprstmt( src, nodes, @@ -6146,7 +6459,7 @@ pub fn check_derivation( NodeKind::DebuggerStatement => check_debugger(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::ThrowStatement => check_throw(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::BreakStatement | NodeKind::ContinueStatement => check_jump(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), - NodeKind::WhileStatement => check_while(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::WhileStatement | NodeKind::WithStatement => check_while(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::DoWhileStatement => check_do_while(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::LabelledStatement => check_labelled(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::TryStatement => check_try(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), @@ -6157,9 +6470,55 @@ pub fn check_derivation( NodeKind::DefaultClause => check_default(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::ReturnStatement => check_return(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::FormalParameters => check_params(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::AssignmentRestElement | NodeKind::AssignmentRestProperty => check_binding_rest(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ArrayBindingPattern | NodeKind::ObjectBindingPattern | NodeKind::ArrayAssignmentPattern + | NodeKind::ObjectAssignmentPattern => { + let sh = PatShape { + m, + array: matches!(nd.kind, NodeKind::ArrayBindingPattern | NodeKind::ArrayAssignmentPattern), + assign: matches!(nd.kind, NodeKind::ArrayAssignmentPattern | NodeKind::ObjectAssignmentPattern), + }; + proof { + assert(nd == &nodes@[m as int]); + } + check_pattern(src, nodes, toks, gaps, &done, &sh, Ghost(GCx { ts, cx })) + }, + NodeKind::BindingProperty => check_binding_property(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::BindingRestElement | NodeKind::BindingRestProperty => check_binding_rest(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::TemplateElement => check_template_element(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::TaggedTemplate => check_tagged(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::PrivateIdentifier => check_private(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::FieldDefinition => check_field(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ClassBody => check_class_body(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ClassDeclaration | NodeKind::ClassExpression => { + let sh = ClassShape { m, is_expr: matches!(nd.kind, NodeKind::ClassExpression) }; + proof { + assert(nd == &nodes@[m as int]); + } + check_class(src, nodes, toks, gaps, &done, &sh, Ghost(GCx { ts, cx })) + }, + NodeKind::PropertyDefinition => check_property(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ComputedPropertyName => check_computed(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::Elision => check_elision(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::FunctionRestParameter => check_rest_param(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::YieldExpression => check_yield(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), NodeKind::AwaitExpression => check_await(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ImportDeclaration => crate::arena_m::check_import(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ImportClause => crate::arena_m::check_import_clause(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::NameSpaceImport => crate::arena_m::check_ns_import(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::NamedImports | NodeKind::NamedExports | NodeKind::WithClause => crate::arena_m::check_braced(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ImportSpecifier | NodeKind::ImportAttribute | NodeKind::ExportSpecifier => crate::arena_m::check_pair(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::NewTarget | NodeKind::ImportMeta => crate::arena_e::check_meta(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::SuperProperty => crate::arena_e::check_super_prop(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::SuperCall => crate::arena_e::check_super_call(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::ImportCall => { + if !plain { + return Derivation::Unsupported; + } + crate::arena_e::check_import_call(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + }, + NodeKind::OptionalChain => crate::arena_e::check_chain_link(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::OptionalExpression => crate::arena_e::check_optional(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), _ => { return Derivation::Unsupported; }, @@ -6242,8 +6601,23 @@ fn assign_ctx(nodes: &[Node], module: bool) -> (r: Vec) let len = nd.children.len(); // An arrow function's parts depend on the kinds of its parameters and body. let arrow = matches!(nd.kind, NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction); - let ident = arrow && child_kind_is(nodes, nd, 0, true); - let braced = arrow && child_kind_is(nodes, nd, 1, false); + let method = matches!(nd.kind, NodeKind::MethodDefinition); + let ident = if method { + matches!(nd.op, Op::AsyncMethod | Op::AsyncGeneratorMethod) + } else if matches!(nd.kind, NodeKind::AssignmentProperty) { + matches!(nd.op, Op::Shorthand) + } else if matches!(nd.kind, NodeKind::MemberExpression) { + matches!(nd.op, Op::Bracket) + } else if matches!(nd.kind, NodeKind::ExportDeclaration) { + matches!(nd.op, Op::ExportDefaultHoistable | Op::ExportDefaultClass) + } else { + arrow && child_kind_is(nodes, nd, 0, true) + }; + let braced = if method { + matches!(nd.op, Op::GeneratorMethod | Op::AsyncGeneratorMethod) + } else { + arrow && child_kind_is(nodes, nd, 1, false) + }; let mut k: usize = 0; while k < len invariant @@ -6300,9 +6674,70 @@ fn child_cx(kind: NodeKind, k: usize, p: &Cx5, ident: bool, braced: bool) -> (r: let (c0, c1) = arrow_cx5(p, matches!(kind, NodeKind::AsyncArrowFunction), ident, braced); return if k == 0 { c0 } else { c1 }; } - Cx5 { y: p.y, a: p.a, i: true, r: p.r, d: false } -} - + if matches!(kind, NodeKind::MethodDefinition) { + // `ident` is `async`, `braced` a generator, for a method. + let (c0, c1, c2) = fn_cx5(p, ident, braced, false); + return if k == 0 { + c0 + } else if k == 1 { + c1 + } else { + c2 + }; + } + if matches!(kind, NodeKind::ClassStaticBlock) { + return Cx5 { y: false, a: true, i: true, r: false, d: false }; + } + if matches!( + kind, + NodeKind::ForStatement | NodeKind::ForInStatement | NodeKind::ForOfStatement + ) { + return if k == 0 { cx5_noin(p) } else { *p }; + } + // `export default` declarations (`ident`) have `[+Default]`; the other forms pass their context. + if matches!(kind, NodeKind::ExportDeclaration) { + return if ident { Cx5 { y: p.y, a: p.a, i: p.i, r: p.r, d: true } } else { *p }; + } + // `a[b]`: the property is `[+In]` (`ident` is the bracket form); `a ? b : c`: so is `b`. + if matches!(kind, NodeKind::MemberExpression) { + return if k == 1 && ident { Cx5 { y: p.y, a: p.a, i: true, r: p.r, d: false } } else { *p }; + } + if matches!(kind, NodeKind::ConditionalExpression) { + return if k == 1 { Cx5 { y: p.y, a: p.a, i: true, r: p.r, d: false } } else { *p }; + } + // These productions pass their own context to their children. + if matches!( + kind, + NodeKind::AdditiveExpression | NodeKind::MultiplicativeExpression | NodeKind::ExponentiationExpression + | NodeKind::ShiftExpression | NodeKind::EqualityExpression | NodeKind::BitwiseANDExpression + | NodeKind::BitwiseXORExpression | NodeKind::BitwiseORExpression | NodeKind::LogicalANDExpression + | NodeKind::LogicalORExpression | NodeKind::CoalesceExpression | NodeKind::RelationalExpression + | NodeKind::UnaryExpression | NodeKind::AssignmentExpression | NodeKind::MemberExpression + | NodeKind::VariableDeclaration | NodeKind::LexicalBinding | NodeKind::VariableDeclarationList + | NodeKind::LexicalDeclaration | NodeKind::ForDeclaration | NodeKind::UpdateExpression + | NodeKind::SuperProperty | NodeKind::SuperCall | NodeKind::OptionalChain + | NodeKind::OptionalExpression | NodeKind::ArrayBindingPattern + | NodeKind::ObjectBindingPattern | NodeKind::ArrayAssignmentPattern + | NodeKind::ObjectAssignmentPattern | NodeKind::BindingProperty + | NodeKind::BindingRestElement | NodeKind::BindingRestProperty + | NodeKind::AssignmentRestElement | NodeKind::AssignmentRestProperty + | NodeKind::CallExpression | NodeKind::NewExpression | NodeKind::TaggedTemplate + | NodeKind::Expression | NodeKind::ClassDeclaration | NodeKind::ClassExpression + | NodeKind::ClassBody | NodeKind::AwaitExpression | NodeKind::YieldExpression + ) { + return *p; + } + // A pattern element's initializer is `[+In]`, its target the element's own. + if matches!(kind, NodeKind::BindingElement | NodeKind::AssignmentElement) { + return if k == 0 { *p } else { Cx5 { y: p.y, a: p.a, i: true, r: p.r, d: false } }; + } + // `ident` is a shorthand property: `{ a = 1 }`. + if matches!(kind, NodeKind::AssignmentProperty) { + return if k == 0 || !ident { *p } else { Cx5 { y: p.y, a: p.a, i: true, r: p.r, d: false } }; + } + Cx5 { y: p.y, a: p.a, i: true, r: p.r, d: false } +} + /// Node `m` and its children are all checked in `p` (`cx_ok`). fn kids_cx(nodes: &[Node], cxs: &[Cx5], m: usize, p: &Cx5) -> (r: bool) requires @@ -6377,21 +6812,172 @@ fn kids_cx(nodes: &[Node], cxs: &[Cx5], m: usize, p: &Cx5) -> (r: bool) true } +/// Node `m` is checked in `c` and each of its children in `ct`, the `[+In]` context of `c` +/// (`cx_ok_t`): the productions whose operands are in brackets. +fn kids_cx_t(nodes: &[Node], cxs: &[Cx5], m: usize, c: &Cx5, ct: &Cx5) -> (r: bool) + requires + m < nodes.len(), + cxs.len() == nodes.len(), + ast_wf(arena_of(nodes@)), + cx_of(*ct) == cx_in(cx_of(*c), true), + ensures + r ==> cx_ok_t(arena_of(nodes@), cxs@, m as int, cx_of(*c)), +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + match cxs.get(m) { + Some(x) => { + if !cx_eq(x, c) { + return false; + } + }, + None => { + return false; + }, + } + let len = nd.children.len(); + let ghost a = arena_of(nodes@); + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == len); + } + let mut k: usize = 0; + while k < len + invariant + m < nodes.len(), + nodes@.len() == nodes.len(), + a == arena_of(nodes@), + ast_wf(a), + nd == &nodes@[m as int], + len == nd.children.len(), + len == a.nodes[m as int].children.len(), + k <= len, + cxs.len() == nodes.len(), + cx_of(*ct) == cx_in(cx_of(*c), true), + cx_of(cxs@[m as int]) == cx_of(*c), + forall|q: int| + 0 <= q < k ==> 0 <= #[trigger] a.nodes[m as int].children[q] < cxs.len() + && cx_of(cxs@[a.nodes[m as int].children[q]]) == cx_of(*ct), + decreases len - k, + { + let ch = match nd.children.as_slice().get(k) { + Some(x) => *x as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[k as int] == ch as int); + lemma_child_before(a, m as int, k as int); + } + let cc = match cxs.get(ch) { + Some(x) => x, + None => { + return false; + }, + }; + if !cx_eq(cc, ct) { + return false; + } + k = k.saturating_add(1); + } + true +} + +/// Node `m` is checked in `c` and each of its children in `ck` (any context). +pub(crate) fn kids_cx_g(nodes: &[Node], cxs: &[Cx5], m: usize, c: &Cx5, ck: &Cx5) -> (r: bool) + requires + m < nodes.len(), + cxs.len() == nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + r ==> (cx_of(cxs@[m as int]) == cx_of(*c) && kids_ok(arena_of(nodes@), cxs@, m as int, cx_of(*ck))), +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + match cxs.get(m) { + Some(x) => { + if !cx_eq(x, c) { + return false; + } + }, + None => { + return false; + }, + } + let len = nd.children.len(); + let ghost a = arena_of(nodes@); + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == len); + } + let mut k: usize = 0; + while k < len + invariant + m < nodes.len(), + nodes@.len() == nodes.len(), + a == arena_of(nodes@), + ast_wf(a), + nd == &nodes@[m as int], + len == nd.children.len(), + len == a.nodes[m as int].children.len(), + k <= len, + cxs.len() == nodes.len(), + cx_of(cxs@[m as int]) == cx_of(*c), + forall|q: int| + 0 <= q < k ==> 0 <= #[trigger] a.nodes[m as int].children[q] < cxs.len() + && cx_of(cxs@[a.nodes[m as int].children[q]]) == cx_of(*ck), + decreases len - k, + { + let ch = match nd.children.as_slice().get(k) { + Some(x) => *x as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[k as int] == ch as int); + lemma_child_before(a, m as int, k as int); + } + let cc = match cxs.get(ch) { + Some(x) => x, + None => { + return false; + }, + }; + if !cx_eq(cc, ck) { + return false; + } + k = k.saturating_add(1); + } + true +} + /// The specification's token for an executable one: the fields the check reads, and -/// the lexer's other fields (`name`, `legacy`, ...) left as the empty or false -/// default; the productions the check covers read none of them. +/// `bigint` and `regexp_valid` left false (the executable lexer does not compute +/// `regexp_valid`; no check reads them). pub open spec fn canon_tok(src: Seq, e: DTok) -> Tok { Tok { kind: e.kind, start: e.start as int, end: e.end as int, text: src.subrange(e.start as int, e.end as int), - name: Seq::empty(), + name: e.name@, escaped: e.escaped, nl_before: e.nl, - legacy: false, + legacy: e.legacy, bigint: false, - invalid_escape: false, + invalid_escape: e.invalid_escape, regexp_valid: false, } } @@ -6490,7 +7076,7 @@ proof fn lemma_empty_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int) // --------------------------------------------------------------------- /// The words of those statements, as code points. -proof fn lemma_words2() +pub(crate) proof fn lemma_words2() ensures cps("do"@) =~= seq![0x64u32, 0x6F], cps("while"@) =~= seq![0x77u32, 0x68, 0x69, 0x6C, 0x65], @@ -6505,7 +7091,10 @@ proof fn lemma_words2() cps("case"@) =~= seq![0x63u32, 0x61, 0x73, 0x65], cps("default"@) =~= seq![0x64u32, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74], cps("}"@) =~= seq![0x7Du32], + cps("with"@) =~= seq![0x77u32, 0x69, 0x74, 0x68], { + reveal_strlit("with"); + assert("with"@ =~= seq!['w', 'i', 't', 'h']); reveal_strlit("do"); reveal_strlit("while"); reveal_strlit("throw"); @@ -6535,7 +7124,7 @@ proof fn lemma_words2() } /// Child `k` of node `m` of a well-formed arena, which is before `m`. -fn kid_of(nodes: &[Node], m: usize, k: usize) -> (r: Option) +pub(crate) fn kid_of(nodes: &[Node], m: usize, k: usize) -> (r: Option) requires m < nodes.len(), ast_wf(arena_of(nodes@)), @@ -6572,7 +7161,7 @@ fn kid_of(nodes: &[Node], m: usize, k: usize) -> (r: Option) } /// The kind of node `c`. -fn kind_at(nodes: &[Node], c: usize) -> (r: NodeKind) +pub(crate) fn kind_at(nodes: &[Node], c: usize) -> (r: NodeKind) requires c < nodes.len(), ensures @@ -6597,7 +7186,7 @@ fn kind_at(nodes: &[Node], c: usize) -> (r: NodeKind) } /// What the check recorded of node `c`: first token, one past the last, strength. -fn done_at(done: &Done, c: usize) -> (r: Option<(usize, usize, i32)>) +pub(crate) fn done_at(done: &Done, c: usize) -> (r: Option<(usize, usize, i32)>) ensures match r { Some((l, h, v)) => c < done.lo@.len() && c < done.hi@.len() && c < done.lv@.len() && l @@ -6627,7 +7216,7 @@ fn done_at(done: &Done, c: usize) -> (r: Option<(usize, usize, i32)>) } /// The token a node starts at. -fn start_tok( +pub(crate) fn start_tok( src: &[u32], nodes: &[Node], toks: &[DTok], @@ -6684,7 +7273,7 @@ pub struct GW { } /// Token `x` is the keyword `w` (`gw.w` as characters). -fn word_at( +pub(crate) fn word_at( src: &[u32], nodes: &[Node], toks: &[DTok], @@ -6724,7 +7313,7 @@ fn word_at( } /// Token `x` is the one-character punctuator `lit` (code point `code`). -fn punct_at( +pub(crate) fn punct_at( src: &[u32], nodes: &[Node], toks: &[DTok], @@ -6764,7 +7353,7 @@ fn punct_at( } /// No line terminator before token `x`. -fn no_nl_at( +pub(crate) fn no_nl_at( src: &[u32], nodes: &[Node], toks: &[DTok], @@ -6794,7 +7383,7 @@ fn no_nl_at( } /// The token before `j` ends where `e` says. -fn end_ok( +pub(crate) fn end_ok( src: &[u32], nodes: &[Node], toks: &[DTok], @@ -6827,7 +7416,7 @@ fn end_ok( } /// [`find_semi`] for the `;` after `do`-`while` (§12.10.1 rule 1, third condition). -fn find_semi_dw( +pub(crate) fn find_semi_dw( src: &[u32], nodes: &[Node], toks: &[DTok], @@ -7287,10 +7876,10 @@ proof fn lemma_while_done( 0 <= m < a.nodes.len(), 0 <= t < m, 0 <= c < m, - a.nodes[m].kind == NodeKind::WhileStatement, + a.nodes[m].kind == NodeKind::WhileStatement || a.nodes[m].kind == NodeKind::WithStatement, a.nodes[m].children =~= seq![t, c], - is_kw(ts, x, "while"@), is_p(ts, x + 1, "("@), + is_kw(ts, x, if a.nodes[m].kind == NodeKind::WhileStatement { "while"@ } else { "with"@ }), lvl(a, t) >= -1, node_done(a, ts, t, cx, x + 2, mt), is_p(ts, mt, ")"@), @@ -7311,12 +7900,20 @@ proof fn lemma_while_done( assert(ed(a, ts, t, x + 2) == mt); assert(a.nodes[m].children.len() == 2); assert(is_p(ts, mt, ")"@)); - assert(d_stmt(a, ts, m, cx, x, j)); + if a.nodes[m].kind == NodeKind::WhileStatement { + assert(is_kw(ts, x, "while"@)); + assert(d_stmt(a, ts, m, cx, x, j)); + } else { + assert(a.nodes[m].kind == NodeKind::WithStatement); + assert(is_kw(ts, x, "with"@)); + assert(d_stmt(a, ts, m, cx, x, j)); + } lemma_ed_of(a, ts, m, x, j); } /// §14.7.3 `while ( Expression ) Statement`. #[verifier::spinoff_prover] +#[verifier::rlimit(100)] fn check_while( src: &[u32], nodes: &[Node], @@ -7356,7 +7953,7 @@ fn check_while( return None; }, }; - if !matches!(nd.kind, NodeKind::WhileStatement) || nd.children.len() != 2 { + if !matches!(nd.kind, NodeKind::WhileStatement | NodeKind::WithStatement) || nd.children.len() != 2 { return None; } let t = match kid_of(nodes, m, 0) { @@ -7393,7 +7990,12 @@ fn check_while( if !c_ok || lv_t < -1 || lo_t != x.saturating_add(2) || lo_c != mt.saturating_add(1) { return None; } - if !word_at(src, nodes, toks, gaps, x, &[0x77, 0x68, 0x69, 0x6C, 0x65], Ghost(GW { g, w: "while"@ })) + let kw_ok = if matches!(nd.kind, NodeKind::WhileStatement) { + word_at(src, nodes, toks, gaps, x, &[0x77, 0x68, 0x69, 0x6C, 0x65], Ghost(GW { g, w: "while"@ })) + } else { + word_at(src, nodes, toks, gaps, x, &[0x77, 0x69, 0x74, 0x68], Ghost(GW { g, w: "with"@ })) + }; + if !kw_ok || !punct_at(src, nodes, toks, gaps, x.saturating_add(1), 0x28, Ghost(GW { g, w: "("@ })) || !punct_at(src, nodes, toks, gaps, mt, 0x29, Ghost(GW { g, w: ")"@ })) || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { @@ -7416,6 +8018,11 @@ fn check_while( lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 1); assert(lvl(a, t as int) >= -1); assert(is_statement_kind(kd(a, c as int))); + if matches!(nd.kind, NodeKind::WhileStatement) { + assert(is_kw(g.ts, x as int, "while"@)); + } else { + assert(is_kw(g.ts, x as int, "with"@)); + } lemma_while_done(a, g.ts, g.cx, m as int, t as int, c as int, x as int, mt as int, j as int); } Some((x, j, -100)) @@ -7693,7 +8300,7 @@ fn check_labelled( return None; }, }; - let b_ok = stmt_kind_exec(kind_at(nodes, b)); + let b_ok = stmt_or_fn_exec(kind_at(nodes, b)); let l_ok = matches!(kind_at(nodes, l), NodeKind::LabelIdentifier); if !b_ok || !l_ok || lo_l != x || hi_l != x.saturating_add(1) || lo_b != x.saturating_add(2) { return None; @@ -7716,7 +8323,6 @@ fn check_labelled( assert(a.nodes[m as int].end == nd.end as int); lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, l as int, g.cx, 0); lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, b as int, g.cx, 1); - assert(is_statement_kind(kd(a, b as int))); assert(is_stmt_or_function(a, b as int)); lemma_labelled_done(a, g.ts, g.cx, m as int, l as int, b as int, x as int, j as int); } @@ -7942,7 +8548,7 @@ proof fn lemma_catch_done( is_kw(ts, x, "catch"@), kd(a, blk) == NodeKind::BlockStatement, if has_param { - kd(a, p) == NodeKind::BindingIdentifier && is_p(ts, x + 1, "("@) && node_done( + is_binding_target_kind(kd(a, p)) && is_p(ts, x + 1, "("@) && node_done( a, ts, p, @@ -8056,7 +8662,7 @@ fn check_catch( } let x1 = x.saturating_add(1); let kp = kind_at(nodes, p); - let has_param = matches!(kp, NodeKind::BindingIdentifier); + let has_param = matches!(kp, NodeKind::BindingIdentifier | NodeKind::ObjectBindingPattern | NodeKind::ArrayBindingPattern); let mp: usize = if has_param { let (lo_p, hi_p, _) = match done_at(done, p) { Some(r) => r, @@ -9369,6 +9975,7 @@ pub open spec fn fn_cx(cx: Ctx, is_async: bool, is_gen: bool, is_expr: bool) -> /// §15.2 `function BindingIdentifier? ( FormalParameters ) { FunctionBody }` and the /// generator, `async` and `async` generator forms (`d_function`). #[verifier::spinoff_prover] +#[verifier::rlimit(300)] proof fn lemma_function_done( a: AstV, ts: Stream, @@ -9411,7 +10018,7 @@ proof fn lemma_function_done( }), !is_gen || is_p(ts, nm - 1, "*"@), if kd(a, n0) == NodeKind::Absent { - is_expr && d(a, ts, n0, fn_cx(cx, is_async, is_gen, is_expr).0, nm, nm) && mn == nm && ed(a, ts, n0, nm) == nm + (is_expr || cx.deflt) && d(a, ts, n0, fn_cx(cx, is_async, is_gen, is_expr).0, nm, nm) && mn == nm && ed(a, ts, n0, nm) == nm } else { kd(a, n0) == NodeKind::BindingIdentifier && node_done( a, @@ -9470,7 +10077,8 @@ pub struct FuncShape { } /// §15.2 and §15.5, §15.8 and §15.6: a function, generator, `async` function or -/// `async` generator, declaration or expression. +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] #[verifier::spinoff_prover] fn check_function( src: &[u32], @@ -9568,7 +10176,13 @@ fn check_function( let ghost cxb = fn_cx(g.cx, fs.is_async, fs.is_gen, fs.is_expr).2; let kn = kind_at(nodes, n0); let mn: usize = if matches!(kn, NodeKind::Absent) { - if !fs.is_expr || !check_absent(src, nodes, toks, gaps, n0, nm, Ghost(GCx { ts: g.ts, cx: cxn })) { + let deflt = match done.cxs.get(m) { + Some(c) => c.d, + None => { + return None; + }, + }; + if !(fs.is_expr || deflt) || !check_absent(src, nodes, toks, gaps, n0, nm, Ghost(GCx { ts: g.ts, cx: cxn })) { return None; } nm @@ -10443,7 +11057,7 @@ fn check_await( } /// The words `yield` and `await`, as code points. -proof fn lemma_words4() +pub(crate) proof fn lemma_words4() ensures cps("yield"@) =~= seq![0x79u32, 0x69, 0x65, 0x6C, 0x64], cps("await"@) =~= seq![0x61u32, 0x77, 0x61, 0x69, 0x74], @@ -10464,7 +11078,7 @@ proof fn lemma_rest_param_done(a: AstV, ts: Stream, cx: Ctx, m: int, c: int, x: a.nodes[m].kind == NodeKind::FunctionRestParameter, a.nodes[m].children =~= seq![c], is_p(ts, x, "..."@), - kd(a, c) == NodeKind::BindingIdentifier, + is_binding_target_kind(kd(a, c)), node_done(a, ts, c, cx, x + 1, j), x < j <= tn(ts), a.nodes[m].start == ts.toks[x].start, @@ -10578,7 +11192,7 @@ fn check_rest_param( }, }; if !ellipsis_at(src, nodes, toks, gaps, x, Ghost(g)) - || !matches!(kind_at(nodes, c), NodeKind::BindingIdentifier) + || !matches!(kind_at(nodes, c), NodeKind::BindingIdentifier | NodeKind::ObjectBindingPattern | NodeKind::ArrayBindingPattern) || lo_c != x.saturating_add(1) || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { return None; @@ -10600,4 +11214,5272 @@ fn check_rest_param( Some((x, j, -100)) } +// --------------------------------------------------------------------- +// The `for` statements (§14.7.4, §14.7.5): their heads are `[~In]`. +// --------------------------------------------------------------------- + +/// The context of a `for` head: `[~In]`, `[Default]` cleared (`cx_in(cx, false)`). +fn cx5_noin(p: &Cx5) -> (r: Cx5) + ensures + cx_of(r) == cx_in(cx_of(*p), false), +{ + Cx5 { y: p.y, a: p.a, i: false, r: p.r, d: false } +} + +/// The same context with `[+In]` and `[Default]` cleared (`cx_in(cx, true)`). +fn cx5_in(p: &Cx5) -> (r: Cx5) + ensures + cx_of(r) == cx_in(cx_of(*p), true), +{ + Cx5 { y: p.y, a: p.a, i: true, r: p.r, d: false } +} + +/// Node `m` is checked in `c0`, its second child in `c1` (the initializer of a binding or +/// pattern element, the consequent of a conditional, the property of `a[b]`) and every other +/// child in `c0`: `cx_ok_init`. +fn kids_init(nodes: &[Node], cxs: &[Cx5], m: usize, c0: &Cx5, c1: &Cx5) -> (r: bool) + requires + m < nodes.len(), + cxs.len() == nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + r ==> cx_ok_init(arena_of(nodes@), cxs@, m as int, cx_of(*c0), cx_of(*c1)), +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + match cxs.get(m) { + Some(x) => { + if !cx_eq(x, c0) { + return false; + } + }, + None => { + return false; + }, + } + let len = nd.children.len(); + let ghost a = arena_of(nodes@); + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == len); + } + let mut k: usize = 0; + while k < len + invariant + m < nodes.len(), + nodes@.len() == nodes.len(), + a == arena_of(nodes@), + ast_wf(a), + nd == &nodes@[m as int], + len == nd.children.len(), + len == a.nodes[m as int].children.len(), + k <= len, + cxs.len() == nodes.len(), + cx_of(cxs@[m as int]) == cx_of(*c0), + forall|q: int| + 0 <= q < k ==> 0 <= #[trigger] a.nodes[m as int].children[q] < cxs.len() + && cx_of(cxs@[a.nodes[m as int].children[q]]) == (if q == 1 { + cx_of(*c1) + } else { + cx_of(*c0) + }), + decreases len - k, + { + let ch = match nd.children.as_slice().get(k) { + Some(x) => *x as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[k as int] == ch as int); + lemma_child_before(a, m as int, k as int); + } + let cc = match cxs.get(ch) { + Some(x) => x, + None => { + return false; + }, + }; + if !cx_eq(cc, if k == 1 { c1 } else { c0 }) { + return false; + } + k = k.saturating_add(1); + } + true +} + +/// The four children of node `m` are checked in `c0`..`c3`. +pub open spec fn cx_kids4(a: AstV, cxs: Seq, m: int, c0: Ctx, c1: Ctx, c2: Ctx, c3: Ctx) -> bool { + 0 <= m < a.nodes.len() && a.nodes[m].children.len() == 4 && (forall|k: int| + 0 <= k < 4 ==> 0 <= #[trigger] a.nodes[m].children[k] < cxs.len()) && cx_of( + cxs[a.nodes[m].children[0]], + ) == c0 && cx_of(cxs[a.nodes[m].children[1]]) == c1 && cx_of(cxs[a.nodes[m].children[2]]) + == c2 && cx_of(cxs[a.nodes[m].children[3]]) == c3 +} + +/// The four children of node `m` are checked in `c0`..`c3`. +fn kids_cx4(nodes: &[Node], cxs: &[Cx5], m: usize, c0: &Cx5, c1: &Cx5, c2: &Cx5, c3: &Cx5) -> (r: bool) + requires + m < nodes.len(), + cxs.len() == nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + r ==> cx_kids4(arena_of(nodes@), cxs@, m as int, cx_of(*c0), cx_of(*c1), cx_of(*c2), cx_of(*c3)), +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + if nd.children.len() != 4 { + return false; + } + let a0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return false; + }, + }; + let a1 = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return false; + }, + }; + let a2 = match kid_of(nodes, m, 2) { + Some(c) => c, + None => { + return false; + }, + }; + let a3 = match kid_of(nodes, m, 3) { + Some(c) => c, + None => { + return false; + }, + }; + let (k0, k1, k2, k3) = match (cxs.get(a0), cxs.get(a1), cxs.get(a2), cxs.get(a3)) { + (Some(x), Some(y), Some(z), Some(w)) => (x, y, z, w), + _ => { + return false; + }, + }; + let ok = cx_eq(k0, c0) && cx_eq(k1, c1) && cx_eq(k2, c2) && cx_eq(k3, c3); + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + if ok { + assert forall|k: int| 0 <= k < 4 implies 0 <= #[trigger] a.nodes[m as int].children[k] < cxs.len() by { + lemma_child_before(a, m as int, k); + } + } + } + ok +} + +/// Token `x` is not the keyword `w` (it may not exist). +pub(crate) fn not_word_at( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + x: usize, + w: &[u32], + Ghost(gw): Ghost, +) -> (r: bool) + requires + env_ok(src@, nodes@, toks@, gaps@, gw.g.ts), + cps(gw.w) =~= w@, + ensures + r ==> !is_kw(gw.g.ts, x as int, gw.w), +{ + let _ = (nodes.len(), gaps.len()); + let ghost g = gw.g; + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let t = match toks.get(x) { + Some(t) => t, + None => { + return true; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int); + assert(t == &toks@[x as int]); + } + let r = !(matches!(t.kind, TokKind::IdentifierName) && !t.escaped && text_is(src, t, w)); + proof { + if r { + assert(g.ts.toks[x as int].text =~= src@.subrange(t.start as int, t.end as int)); + } + } + r +} + +/// §14.3.2 `var VariableDeclarationList`, as the head of a `for`. +#[verifier::spinoff_prover] +proof fn lemma_vdl_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, e: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + a.nodes[m].kind == NodeKind::VariableDeclarationList, + a.nodes[m].children.len() > 0, + is_kw(ts, x, "var"@), + dsep(a, ts, m, a.nodes[m].children, 0, cx, x + 1, e, false), + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> list_item_ok(a, #[trigger] a.nodes[m].children[k], 2), + x + 1 <= e <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[e - 1].end, + ensures + node_done(a, ts, m, cx, x, e), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, e)); + assert forall|k: int| 0 <= k < a.nodes[m].children.len() implies kd( + a, + #[trigger] a.nodes[m].children[k], + ) == NodeKind::VariableDeclaration by { + assert(list_item_ok(a, a.nodes[m].children[k], 2)); + } + assert(d_stmt(a, ts, m, cx, x, e)); + lemma_ed_of(a, ts, m, x, e); +} + +/// §14.3.2 `var VariableDeclarationList`. +#[verifier::spinoff_prover] +fn check_var_list( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_decl_words(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::VariableDeclarationList) || nd.children.is_empty() { + return None; + } + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if !word_at(src, nodes, toks, gaps, x, &[0x76, 0x61, 0x72], Ghost(GW { g, w: "var"@ })) { + return None; + } + let ls = ListSpec { m, start: x.saturating_add(1), mode: 2, trail: false }; + let e = match check_list(src, nodes, toks, gaps, done, &ls, Ghost(g)) { + Some(e) => e, + None => { + return None; + }, + }; + if !end_ok(src, nodes, toks, gaps, nd.end, e, Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(a.nodes[m as int].children.len() > 0); + lemma_vdl_done(a, g.ts, g.cx, m as int, x as int, e as int); + } + Some((x, e, -100)) +} + +/// §14.7.5 `ForDeclaration : var|let|const ForBinding`. +#[verifier::spinoff_prover] +proof fn lemma_for_decl_done(a: AstV, ts: Stream, cx: Ctx, m: int, c: int, x: int, j: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::ForDeclaration, + a.nodes[m].children =~= seq![c], + if a.nodes[m].op == Op::Var { + is_kw(ts, x, "var"@) + } else if a.nodes[m].op == Op::Let { + is_kw(ts, x, "let"@) + } else { + a.nodes[m].op == Op::Const && is_kw(ts, x, "const"@) + }, + is_binding_target_kind(kd(a, c)) || (a.nodes[m].op == Op::Var && kd(a, c) == NodeKind::VariableDeclaration), + node_done(a, ts, c, cx, x + 1, j), + x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c); + assert(dc(a, ts, m, c, cx, x + 1, j)); + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.7.5 `ForDeclaration`. +#[verifier::spinoff_prover] +fn check_for_decl( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_decl_words(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ForDeclaration) || nd.children.len() != 1 { + return None; + } + let c = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let kw_ok = match nd.op { + Op::Var => word_at(src, nodes, toks, gaps, x, &[0x76, 0x61, 0x72], Ghost(GW { g, w: "var"@ })), + Op::Let => word_at(src, nodes, toks, gaps, x, &[0x6C, 0x65, 0x74], Ghost(GW { g, w: "let"@ })), + Op::Const => word_at(src, nodes, toks, gaps, x, &[0x63, 0x6F, 0x6E, 0x73, 0x74], Ghost(GW { g, w: "const"@ })), + _ => false, + }; + let (lo_c, j, _) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + let target_ok = match kind_at(nodes, c) { + NodeKind::BindingIdentifier | NodeKind::ObjectBindingPattern | NodeKind::ArrayBindingPattern => true, + NodeKind::VariableDeclaration => matches!(nd.op, Op::Var), + _ => false, + }; + if !kw_ok || !target_ok + || lo_c != x.saturating_add(1) || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) + || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(a.nodes[m as int].children =~= seq![c as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].op == nd.op); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 0); + lemma_for_decl_done(a, g.ts, g.cx, m as int, c as int, x as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.7.4 `for ( init ; test ; update ) Statement`. +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +proof fn lemma_for_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + k0: int, + k1: int, + k2: int, + k3: int, + x: int, + m0: int, + q: int, + r: int, + j: int, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= k0 < m, + 0 <= k1 < m, + 0 <= k2 < m, + 0 <= k3 < m, + a.nodes[m].kind == NodeKind::ForStatement, + a.nodes[m].children =~= seq![k0, k1, k2, k3], + is_kw(ts, x, "for"@), + is_p(ts, x + 1, "("@), + is_statement_kind(kd(a, k3)), + d(a, ts, k0, cx_in(cx, false), x + 2, m0), + ed(a, ts, k0, x + 2) == m0, + if kd(a, k0) == NodeKind::LexicalDeclaration { + is_p(ts, m0 - 1, ";"@) + } else { + is_p(ts, m0, ";"@) && (kd(a, k0) == NodeKind::Absent || kd(a, k0) + == NodeKind::VariableDeclarationList || (lvl(a, k0) >= -1 && !(is_kw( + ts, + x + 2, + "let"@, + ) && is_p(ts, x + 3, "["@)))) + }, + opt_expr(a, k1), + d(a, ts, k1, cx, for_init_next(a, ts, k0, x + 2), q), + ed(a, ts, k1, for_init_next(a, ts, k0, x + 2)) == q, + is_p(ts, q, ";"@), + opt_expr(a, k2), + d(a, ts, k2, cx, q + 1, r), + ed(a, ts, k2, q + 1) == r, + is_p(ts, r, ")"@), + d(a, ts, k3, cx, r + 1, j), + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == k0); + assert(a.nodes[m].children[1] == k1); + assert(a.nodes[m].children[2] == k2); + assert(a.nodes[m].children[3] == k3); + assert(for_init_ok(a, ts, m, k0, cx, x + 2)); + assert(dc(a, ts, m, k1, cx, for_init_next(a, ts, k0, x + 2), q)); + assert(dc(a, ts, m, k2, cx, q + 1, r)); + assert(dc(a, ts, m, k3, cx, r + 1, j)); + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.7.4 `for ( init ; test ; update ) Statement`. +#[verifier::spinoff_prover] +fn check_for( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + cx_in(g.cx, true) == g.cx, + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + 0 <= m < done.cxs@.len(), + cx_of(done.cxs@[m as int]) == g.cx, + cx_kids4( + arena_of(nodes@), + done.cxs@, + m as int, + cx_in(g.cx, false), + g.cx, + g.cx, + g.cx, + ), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words5(); + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ForStatement) || nd.children.len() != 4 { + return None; + } + let k0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let k1 = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let k2 = match kid_of(nodes, m, 2) { + Some(c) => c, + None => { + return None; + }, + }; + let k3 = match kid_of(nodes, m, 3) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if !word_at(src, nodes, toks, gaps, x, &[0x66, 0x6F, 0x72], Ghost(GW { g, w: "for"@ })) + || !punct_at(src, nodes, toks, gaps, x.saturating_add(1), 0x28, Ghost(GW { g, w: "("@ })) { + return None; + } + let s = x.saturating_add(2); + let ghost cxi = cx_in(g.cx, false); + // The initializer. + let ki = kind_at(nodes, k0); + let lexical = matches!(ki, NodeKind::LexicalDeclaration); + let (m0, lv0): (usize, i32) = if matches!(ki, NodeKind::Absent) { + if !check_absent(src, nodes, toks, gaps, k0, s, Ghost(GCx { ts: g.ts, cx: cxi })) { + return None; + } + (s, -100) + } else { + let (lo0, h0, lv0) = match done_at(done, k0) { + Some(r) => r, + None => { + return None; + }, + }; + if lo0 != s { + return None; + } + (h0, lv0) + }; + let p0: usize = if lexical { m0 } else { m0.saturating_add(1) }; + if lexical { + if m0 < 1 || !punct_at(src, nodes, toks, gaps, m0.saturating_sub(1), 0x3B, Ghost(GW { g, w: ";"@ })) { + return None; + } + } else { + let init_ok = matches!(ki, NodeKind::Absent | NodeKind::VariableDeclarationList) + || (lv0 >= -1 && not_word_at(src, nodes, toks, gaps, s, &[0x6C, 0x65, 0x74], Ghost(GW { g, w: "let"@ }))); + if !init_ok || !punct_at(src, nodes, toks, gaps, m0, 0x3B, Ghost(GW { g, w: ";"@ })) { + return None; + } + } + // The test. + let kt = kind_at(nodes, k1); + let q: usize = if matches!(kt, NodeKind::Absent) { + if !check_absent(src, nodes, toks, gaps, k1, p0, Ghost(g)) { + return None; + } + p0 + } else { + let (lo1, h1, lv1) = match done_at(done, k1) { + Some(r) => r, + None => { + return None; + }, + }; + if lo1 != p0 || lv1 < -1 { + return None; + } + h1 + }; + if !punct_at(src, nodes, toks, gaps, q, 0x3B, Ghost(GW { g, w: ";"@ })) { + return None; + } + // The update. + let q1 = q.saturating_add(1); + let ku = kind_at(nodes, k2); + let rr: usize = if matches!(ku, NodeKind::Absent) { + if !check_absent(src, nodes, toks, gaps, k2, q1, Ghost(g)) { + return None; + } + q1 + } else { + let (lo2, h2, lv2) = match done_at(done, k2) { + Some(r) => r, + None => { + return None; + }, + }; + if lo2 != q1 || lv2 < -1 { + return None; + } + h2 + }; + if !punct_at(src, nodes, toks, gaps, rr, 0x29, Ghost(GW { g, w: ")"@ })) { + return None; + } + let (lo3, j, _) = match done_at(done, k3) { + Some(r) => r, + None => { + return None; + }, + }; + if !stmt_kind_exec(kind_at(nodes, k3)) || lo3 != rr.saturating_add(1) + || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(s as int == x as int + 2); + assert(q1 as int == q as int + 1); + assert(rr.saturating_add(1) as int == rr as int + 1); + assert(a.nodes[m as int].children =~= seq![k0 as int, k1 as int, k2 as int, k3 as int]) by { + assert(a.nodes[m as int].children.len() == 4); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k0 as int); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k1 as int); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k2 as int); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k3 as int); + assert(kd(a, k0 as int) == ki); + assert(kd(a, k1 as int) == kt); + assert(kd(a, k2 as int) == ku); + assert(kd(a, k3 as int) == nodes@[k3 as int].kind); + assert(is_statement_kind(kd(a, k3 as int))); + assert(cx_of(done.cxs@[k0 as int]) == cxi); + assert(cx_of(done.cxs@[k1 as int]) == g.cx); + assert(cx_of(done.cxs@[k2 as int]) == g.cx); + assert(cx_of(done.cxs@[k3 as int]) == g.cx); + if !matches!(ki, NodeKind::Absent) { + assert(lvl(a, k0 as int) == lv0 as int); + assert(kd(a, k0 as int) != NodeKind::Absent); + } + if !matches!(kt, NodeKind::Absent) { + assert(kd(a, k1 as int) != NodeKind::Absent); + } + if !matches!(ku, NodeKind::Absent) { + assert(kd(a, k2 as int) != NodeKind::Absent); + } + assert(opt_expr(a, k1 as int)); + assert(opt_expr(a, k2 as int)); + assert(for_init_next(a, g.ts, k0 as int, x as int + 2) == p0 as int); + assert(m0 >= 1 || !lexical); + lemma_for_done(a, g.ts, g.cx, m as int, k0 as int, k1 as int, k2 as int, k3 as int, x as int, m0 as int, q as int, rr as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.7.5 `for ( ForDeclaration|LeftHandSideExpression in|of ... ) Statement`. +#[verifier::spinoff_prover] +#[verifier::rlimit(60)] +proof fn lemma_for_in_of_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + h: int, + e: int, + b: int, + of: bool, + x: int, + mh: int, + q: int, + j: int, + s: int, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= h < m, + 0 <= e < m, + 0 <= b < m, + a.nodes[m].kind == (if of { + NodeKind::ForOfStatement + } else { + NodeKind::ForInStatement + }), + a.nodes[m].children =~= seq![h, e, b], + a.nodes[m].flag ==> (of && cx.await_ && is_kw(ts, x + 1, "await"@)), + s == (if a.nodes[m].flag { + x + 3 + } else { + x + 2 + }), + is_kw(ts, x, "for"@), + is_p(ts, s - 1, "("@), + for_head_ok(a, ts, h, s, of, a.nodes[m].flag), + of ==> first_kid_kind(a, h) != NodeKind::VariableDeclaration, + if of { + is_assign_expr(a, e) + } else { + lvl(a, e) >= -1 + }, + is_statement_kind(kd(a, b)), + d(a, ts, h, cx_in(cx, false), s, mh), + ed(a, ts, h, s) == mh, + is_kw(ts, mh, if of { + "of"@ + } else { + "in"@ + }), + d(a, ts, e, cx, mh + 1, q), + ed(a, ts, e, mh + 1) == q, + is_p(ts, q, ")"@), + d(a, ts, b, cx, q + 1, j), + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == h); + assert(a.nodes[m].children[1] == e); + assert(a.nodes[m].children[2] == b); + assert(dc(a, ts, m, h, cx_in(cx, false), s, mh)); + assert(dc(a, ts, m, e, cx, mh + 1, q)); + assert(dc(a, ts, m, b, cx, q + 1, j)); + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// What `check_for_in_of` reads: the node, and whether it is `for`-`of`. +pub struct InOfShape { + /// The node. + pub m: usize, + /// `for`-`of` (else `for`-`in`). + pub of: bool, +} + +/// §14.7.5 `for ( head in|of expression ) Statement`, with a declaration or an +/// identifier as head. +#[verifier::spinoff_prover] +fn check_for_in_of( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + sh: &InOfShape, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + sh.m < nodes.len(), + cx_in(g.cx, true) == g.cx, + nodes@[sh.m as int].kind == (if sh.of { + NodeKind::ForOfStatement + } else { + NodeKind::ForInStatement + }), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, sh.m as int), + 0 <= sh.m < done.cxs@.len(), + cx_of(done.cxs@[sh.m as int]) == g.cx, + cx_kids3(arena_of(nodes@), done.cxs@, sh.m as int, cx_in(g.cx, false), g.cx, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + sh.m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), sh.m as int), + None => true, + }, +{ + let m = sh.m; + proof { + lemma_words5(); + lemma_words4(); + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if nd.children.len() != 3 || (nd.flag && !sh.of) { + return None; + } + let cxm = match done.cxs.get(m) { + Some(c) => c, + None => { + return None; + }, + }; + let h = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let e = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let b = match kid_of(nodes, m, 2) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if x > usize::MAX - 8 { + return None; + } + let flag = nd.flag; + // `for await (`: the `await` is the second token, the head starts after the `(`. + let s = if flag { x.saturating_add(3) } else { x.saturating_add(2) }; + if !word_at(src, nodes, toks, gaps, x, &[0x66, 0x6F, 0x72], Ghost(GW { g, w: "for"@ })) + || (flag && !(cxm.a && word_at(src, nodes, toks, gaps, x.saturating_add(1), &[0x61, 0x77, 0x61, 0x69, 0x74], Ghost(GW { g, w: "await"@ })))) + || !punct_at(src, nodes, toks, gaps, s.saturating_sub(1), 0x28, Ghost(GW { g, w: "("@ })) { + return None; + } + let kh = kind_at(nodes, h); + let decl = matches!(kh, NodeKind::ForDeclaration); + let (lo_h, mh, lv_h) = match done_at(done, h) { + Some(r) => r, + None => { + return None; + }, + }; + // A head that is not a declaration is a `LeftHandSideExpression` that is not an object or + // array literal, or an assignment pattern; not `let` (`async` for `of`). + let lhs_ok = matches!(kh, NodeKind::ArrayAssignmentPattern | NodeKind::ObjectAssignmentPattern) + || (lv_h >= 14 && !matches!(kh, NodeKind::ObjectLiteral | NodeKind::ArrayLiteral)); + let let_strict = not_word_at(src, nodes, toks, gaps, s, &[0x6C, 0x65, 0x74], Ghost(GW { g, w: "let"@ })); + // `for (let in o)` is a sloppy-mode identifier `let`; only `let [` is excluded for `in`. + let in_next = !sh.of + && word_at(src, nodes, toks, gaps, s.saturating_add(1), &[0x69, 0x6E], Ghost(GW { g, w: "in"@ })); + let head_ok = decl || (lhs_ok && (let_strict || in_next) + && (!sh.of || not_word_at(src, nodes, toks, gaps, s, &[0x61, 0x73, 0x79, 0x6E, 0x63], Ghost(GW { g, w: "async"@ })))); + // Only `for (var a = 1 in x)` (Annex B.3.5) has a `VariableDeclaration` in its head. + let head_first_ok = if decl { + match kid_of(nodes, h, 0) { + Some(c) => !sh.of || !matches!(kind_at(nodes, c), NodeKind::VariableDeclaration), + None => false, + } + } else { + match kid_of(nodes, h, 0) { + Some(c) => !matches!(kind_at(nodes, c), NodeKind::VariableDeclaration), + None => match nodes.get(h) { + Some(hn) => hn.children.is_empty(), + None => false, + }, + } + }; + let (lo_e, q, lv_e) = match done_at(done, e) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo_b, j, _) = match done_at(done, b) { + Some(r) => r, + None => { + return None; + }, + }; + let kw_ok = if sh.of { + word_at(src, nodes, toks, gaps, mh, &[0x6F, 0x66], Ghost(GW { g, w: "of"@ })) + } else { + word_at(src, nodes, toks, gaps, mh, &[0x69, 0x6E], Ghost(GW { g, w: "in"@ })) + }; + let e_ok = if sh.of { lv_e >= 0 } else { lv_e >= -1 }; + if !head_ok || !head_first_ok || lo_h != s || !kw_ok || !e_ok || lo_e != mh.saturating_add(1) + || !punct_at(src, nodes, toks, gaps, q, 0x29, Ghost(GW { g, w: ")"@ })) + || !stmt_kind_exec(kind_at(nodes, b)) || lo_b != q.saturating_add(1) + || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(a.nodes[m as int].flag == nd.flag); + assert(s as int == (if a.nodes[m as int].flag { + x as int + 3 + } else { + x as int + 2 + })); + assert(s.saturating_sub(1) as int == s as int - 1); + assert(s.saturating_add(1) as int == s as int + 1); + assert(cxm == &done.cxs@[m as int]); + assert(mh.saturating_add(1) as int == mh as int + 1); + assert(q.saturating_add(1) as int == q as int + 1); + assert(a.nodes[m as int].children =~= seq![h as int, e as int, b as int]) by { + assert(a.nodes[m as int].children.len() == 3); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].flag == nd.flag); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, h as int); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, e as int); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, b as int); + assert(kd(a, h as int) == kh); + assert(kd(a, b as int) == nodes@[b as int].kind); + assert(is_statement_kind(kd(a, b as int))); + assert(cx_of(done.cxs@[h as int]) == cx_in(g.cx, false)); + assert(cx_of(done.cxs@[e as int]) == g.cx); + assert(cx_of(done.cxs@[b as int]) == g.cx); + assert(lvl(a, e as int) == lv_e as int); + if sh.of { + assert(is_assign_expr(a, e as int)); + } + // The head. + if !decl { + assert(lvl(a, h as int) == lv_h as int); + assert(is_destructuring_target(a, h as int)); + } + assert(a.nodes[h as int] == nodes@[h as int]@); + assert(for_head_ok(a, g.ts, h as int, s as int, sh.of, a.nodes[m as int].flag)) by { + if !decl { + if sh.of || let_strict { + assert(!is_kw(g.ts, s as int, "let"@)); + } else { + assert(is_kw(g.ts, s as int + 1, "in"@)); + assert(!is_p(g.ts, s as int + 1, "["@)); + } + } + } + assert(sh.of ==> first_kid_kind(a, h as int) != NodeKind::VariableDeclaration) by { + if sh.of { + if decl { + let hc = a.nodes[h as int].children[0]; + assert(kid(kids(a, h as int), 0) == hc); + } else if a.nodes[h as int].children.len() == 0 { + assert(kid(kids(a, h as int), 0) == -1int); + } else { + let hc = a.nodes[h as int].children[0]; + assert(kid(kids(a, h as int), 0) == hc); + } + } + } + lemma_for_in_of_done(a, g.ts, g.cx, m as int, h as int, e as int, b as int, sh.of, x as int, mh as int, q as int, j as int, s as int); + } + Some((x, j, -100)) +} + +/// The words `for`, `of` and `in`, as code points. +proof fn lemma_words5() + ensures + cps("for"@) =~= seq![0x66u32, 0x6F, 0x72], + cps("of"@) =~= seq![0x6Fu32, 0x66], + cps("in"@) =~= seq![0x69u32, 0x6E], + cps("let"@) =~= seq![0x6Cu32, 0x65, 0x74], + cps("async"@) =~= seq![0x61u32, 0x73, 0x79, 0x6E, 0x63], +{ + reveal_strlit("for"); + reveal_strlit("of"); + reveal_strlit("in"); + reveal_strlit("let"); + reveal_strlit("async"); + assert("for"@ =~= seq!['f', 'o', 'r']); + assert("of"@ =~= seq!['o', 'f']); + assert("in"@ =~= seq!['i', 'n']); + assert("let"@ =~= seq!['l', 'e', 't']); + assert("async"@ =~= seq!['a', 's', 'y', 'n', 'c']); +} + +// --------------------------------------------------------------------- +// Array and object literals (§13.2.4, §13.2.5): elements, holes, spread, +// properties (shorthand, `key: value`, spread), computed names. +// --------------------------------------------------------------------- + +/// What an array literal accepts as an element. +pub open spec fn array_item_ok(a: AstV, c: int) -> bool { + lvl(a, c) >= 0 || kd(a, c) == NodeKind::SpreadElement || kd(a, c) == NodeKind::Elision +} + +/// What an array literal (mode 0) or an array binding pattern (mode 6) accepts as an element. +pub open spec fn array_item_mode_ok(a: AstV, c: int, mode: int) -> bool { + if mode == 6 { + kd(a, c) == NodeKind::BindingElement || kd(a, c) == NodeKind::Elision || kd(a, c) + == NodeKind::BindingRestElement + } else if mode == 8 { + kd(a, c) == NodeKind::AssignmentElement || kd(a, c) == NodeKind::Elision || kd(a, c) + == NodeKind::AssignmentRestElement + } else { + array_item_ok(a, c) + } +} + +/// `array_item_mode_ok`, executable. +fn array_item_exec(l: i32, k: NodeKind, mode: u8) -> (r: bool) + ensures + r ==> (if mode == 6 { + k == NodeKind::BindingElement || k == NodeKind::Elision || k + == NodeKind::BindingRestElement + } else if mode == 8 { + k == NodeKind::AssignmentElement || k == NodeKind::Elision || k + == NodeKind::AssignmentRestElement + } else { + l as int >= 0 || k == NodeKind::SpreadElement || k == NodeKind::Elision + }), +{ + if mode == 6 { + matches!(k, NodeKind::BindingElement | NodeKind::Elision | NodeKind::BindingRestElement) + } else if mode == 8 { + matches!(k, NodeKind::AssignmentElement | NodeKind::Elision | NodeKind::AssignmentRestElement) + } else { + l >= 0 || matches!(k, NodeKind::SpreadElement | NodeKind::Elision) + } +} + +/// §13.2.4 `Elision : ,` (a hole): a `,`. +#[verifier::spinoff_prover] +proof fn lemma_elision_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= x < tn(ts), + a.nodes[m].kind == NodeKind::Elision, + a.nodes[m].children.len() == 0, + is_p(ts, x, ","@), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[x].end, + ensures + node_done(a, ts, m, cx, x, x + 1), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, x + 1)); + assert(d_pat(a, ts, m, cx, x, x + 1)); + lemma_ed_of(a, ts, m, x, x + 1); +} + +/// §13.2.4 an `Elision`. +#[verifier::spinoff_prover] +fn check_elision( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_comma_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::Elision) || !nd.children.is_empty() { + return None; + } + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if !punct_at(src, nodes, toks, gaps, x, 0x2C, Ghost(GW { g, w: ","@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, x.saturating_add(1), Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].children.len() == 0); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_elision_done(a, g.ts, g.cx, m as int, x as int); + } + Some((x, x.saturating_add(1), -100)) +} + +/// The elements of an array literal: `spec::darray` of the children, ending at `e`. +fn check_array_items( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + ls: &ListSpec, + Ghost(g): Ghost, +) -> (r: Option) + requires + ls.m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, ls.m as int), + kids_ok(arena_of(nodes@), done.cxs@, ls.m as int, g.cx), + ensures + match r { + Some(e) => ls.start <= e && e <= toks.len() && darray( + arena_of(nodes@), + g.ts, + ls.m as int, + arena_of(nodes@).nodes[ls.m as int].children, + 0, + g.cx, + ls.start as int, + e as int, + ) && forall|k: int| + 0 <= k < arena_of(nodes@).nodes[ls.m as int].children.len() ==> array_item_mode_ok( + arena_of(nodes@), + #[trigger] arena_of(nodes@).nodes[ls.m as int].children[k], + ls.mode as int, + ), + None => true, + }, +{ + let _ = gaps.len(); + let m = ls.m; + let start = ls.start; + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let len = nd.children.len(); + let ghost a = arena_of(nodes@); + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == len); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + if len == 0 { + if start > toks.len() { + return None; + } + proof { + assert(darray(a, g.ts, m as int, a.nodes[m as int].children, 0, g.cx, start as int, start as int)); + } + return Some(start); + } + let last = len.saturating_sub(1); + let c = match nd.children.as_slice().get(last) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int].children[last as int] == c as int); + lemma_child_before(a, m as int, last as int); + } + if c >= m { + return None; + } + let (lo_c, hc, lvl_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + let kc_kind = kind_at(nodes, c); + if !array_item_exec(lvl_c, kc_kind, ls.mode) { + return None; + } + let is_hole = matches!(kc_kind, NodeKind::Elision); + let mut comma_after = false; + if let Some(t) = toks.get(hc) { + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int); + assert(t == &toks@[hc as int]); + } + if !is_hole + && !matches!(kc_kind, NodeKind::BindingRestElement | NodeKind::AssignmentRestElement) + && matches!(t.kind, TokKind::Punctuator) && text_is1(src, t, 0x2C) { + comma_after = true; + } + } + let e: usize = if comma_after { hc.saturating_add(1) } else { hc }; + proof { + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, last as int); + assert(hc <= toks.len()); + assert(kd(a, c as int) == nodes@[c as int].kind); + assert(kd(a, c as int) == kc_kind); + if comma_after { + lemma_comma_text(); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int); + assert(hc < toks.len()); + assert(e as int == hc as int + 1); + assert(g.ts.toks[hc as int].kind == TokKind::Punctuator); + assert(g.ts.toks[hc as int].text =~= seq![0x2Cu32]); + assert(is_p(g.ts, hc as int, ","@)); + assert(!is_rest_kind(kd(a, c as int))); + } else { + assert(e == hc); + } + assert(e <= toks.len()); + assert(ed(a, g.ts, c as int, lo_c as int) == hc as int); + let ks = a.nodes[m as int].children; + assert(dc(a, g.ts, m as int, c as int, g.cx, lo_c as int, hc as int)); + reveal_with_fuel(darray, 2); + assert(darray(a, g.ts, m as int, ks, last as int, g.cx, lo_c as int, e as int)); + } + let mut k = last; + let mut pos = lo_c; + while k > 0 + invariant + m < nodes.len(), + nodes@.len() == nodes.len(), + nd == &nodes@[m as int], + a == arena_of(nodes@), + len == nd.children.len(), + len == a.nodes[m as int].children.len(), + k < len, + e <= toks.len(), + pos <= e, + ast_wf(a), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + kids_ok(a, done.cxs@, m as int, g.cx), + darray(a, g.ts, m as int, a.nodes[m as int].children, k as int, g.cx, pos as int, e as int), + forall|q: int| + k <= q < len ==> array_item_mode_ok(a, #[trigger] a.nodes[m as int].children[q], ls.mode as int), + decreases k, + { + let c2 = match nd.children.as_slice().get(k.saturating_sub(1)) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + assert(k >= 1); + assert(k.saturating_sub(1) as int == k as int - 1); + assert(a.nodes[m as int].children[k as int - 1] == c2 as int); + lemma_child_before(a, m as int, k as int - 1); + } + if c2 >= m { + return None; + } + let (lo2, hi2, lvl2) = match done_at(done, c2) { + Some(r) => r, + None => { + return None; + }, + }; + let kind2 = kind_at(nodes, c2); + if !array_item_exec(lvl2, kind2, ls.mode) { + return None; + } + if matches!(kind2, NodeKind::Elision) { + if hi2 != pos { + return None; + } + } else { + let sep = match toks.get(hi2) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hi2 as int); + assert(sep == &toks@[hi2 as int]); + } + if !matches!(sep.kind, TokKind::Punctuator) || !text_is1(src, sep, 0x2C) + || hi2.saturating_add(1) != pos { + return None; + } + } + proof { + lemma_comma_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c2 as int, g.cx, k as int - 1); + assert(kd(a, c2 as int) == kind2); + assert(hi2 <= toks.len()); + assert(ed(a, g.ts, c2 as int, lo2 as int) == hi2 as int); + let ks = a.nodes[m as int].children; + assert(dc(a, g.ts, m as int, c2 as int, g.cx, lo2 as int, hi2 as int)); + if !matches!(kind2, NodeKind::Elision) { + assert(hi2 < toks.len()); + assert(hi2 + 1 == pos); + assert(g.ts.toks[hi2 as int].kind == TokKind::Punctuator); + assert(g.ts.toks[hi2 as int].text =~= seq![0x2Cu32]); + assert(is_p(g.ts, hi2 as int, ","@)); + } + reveal_with_fuel(darray, 2); + assert(darray(a, g.ts, m as int, ks, k as int - 1, g.cx, lo2 as int, e as int)); + } + pos = lo2; + k = k.saturating_sub(1); + } + if pos != start { + return None; + } + proof { + assert(forall|q: int| + 0 <= q < len ==> array_item_mode_ok(a, #[trigger] a.nodes[m as int].children[q], ls.mode as int)); + } + Some(e) +} + +/// §13.2.4 `ArrayLiteral : [ elements ]`. +#[verifier::spinoff_prover] +proof fn lemma_array_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, e: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= x, + x + 1 <= e < tn(ts), + a.nodes[m].kind == NodeKind::ArrayLiteral, + darray(a, ts, m, a.nodes[m].children, 0, cx_in(cx, true), x + 1, e), + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> array_item_ok(a, #[trigger] a.nodes[m].children[k]), + is_p(ts, x, "["@), + is_p(ts, e, "]"@), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[e].end, + ensures + node_done(a, ts, m, cx, x, e + 1), + lvl(a, m) == 17, +{ + assert(span_ok(a, ts, m, x, e + 1)); + assert(d_expr_lhs(a, ts, m, cx, x, e + 1)); + lemma_ed_of(a, ts, m, x, e + 1); +} + +/// §13.2.4 `ArrayLiteral`. +#[verifier::spinoff_prover] +fn check_array( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok_t(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_brackets(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ArrayLiteral) { + return None; + } + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if !punct_at(src, nodes, toks, gaps, x, 0x5B, Ghost(GW { g, w: "["@ })) { + return None; + } + let ghost gt = GCx { ts: g.ts, cx: cx_in(g.cx, true) }; + let als = ListSpec { m, start: x.saturating_add(1), mode: 0, trail: true }; + let e = match check_array_items(src, nodes, toks, gaps, done, &als, Ghost(gt)) { + Some(e) => e, + None => { + return None; + }, + }; + if !punct_at(src, nodes, toks, gaps, e, 0x5D, Ghost(GW { g, w: "]"@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, e.saturating_add(1), Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(e.saturating_add(1) as int == e as int + 1); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(forall|k: int| + 0 <= k < a.nodes[m as int].children.len() ==> array_item_ok(a, #[trigger] a.nodes[m as int].children[k])); + lemma_array_done(a, g.ts, g.cx, m as int, x as int, e as int); + } + Some((x, e.saturating_add(1), 17)) +} + +/// The brackets `[` and `]`, as code points. +pub(crate) proof fn lemma_brackets() + ensures + cps("["@) =~= seq![0x5Bu32], + cps("]"@) =~= seq![0x5Du32], +{ + reveal_strlit("["); + reveal_strlit("]"); + assert("["@ =~= seq!['[']); + assert("]"@ =~= seq![']']); +} + +/// §13.2.5 `ObjectLiteral : { PropertyDefinitionList ,? }`. +#[verifier::spinoff_prover] +proof fn lemma_object_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, e: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= x, + x + 1 <= e < tn(ts), + a.nodes[m].kind == NodeKind::ObjectLiteral, + dsep(a, ts, m, a.nodes[m].children, 0, cx_in(cx, true), x + 1, e, true), + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> list_item_ok(a, #[trigger] a.nodes[m].children[k], 5), + is_p(ts, x, "{"@), + is_p(ts, e, "}"@), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[e].end, + ensures + node_done(a, ts, m, cx, x, e + 1), + lvl(a, m) == 17, +{ + assert forall|k: int| 0 <= k < a.nodes[m].children.len() implies kd( + a, + #[trigger] a.nodes[m].children[k], + ) == NodeKind::PropertyDefinition || kd(a, a.nodes[m].children[k]) + == NodeKind::CoverInitializedName || kd(a, a.nodes[m].children[k]) + == NodeKind::MethodDefinition by { + assert(list_item_ok(a, a.nodes[m].children[k], 5)); + } + assert(span_ok(a, ts, m, x, e + 1)); + assert(d_expr_lhs(a, ts, m, cx, x, e + 1)); + lemma_ed_of(a, ts, m, x, e + 1); +} + +/// §13.2.5 `ObjectLiteral`. +#[verifier::spinoff_prover] +fn check_object( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok_t(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_brace_text(); + lemma_words2(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ObjectLiteral) { + return None; + } + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if !punct_at(src, nodes, toks, gaps, x, 0x7B, Ghost(GW { g, w: "{"@ })) { + return None; + } + let ghost gt = GCx { ts: g.ts, cx: cx_in(g.cx, true) }; + let ls = ListSpec { m, start: x.saturating_add(1), mode: 5, trail: true }; + let e = match check_list(src, nodes, toks, gaps, done, &ls, Ghost(gt)) { + Some(e) => e, + None => { + return None; + }, + }; + if !punct_at(src, nodes, toks, gaps, e, 0x7D, Ghost(GW { g, w: "}"@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, e.saturating_add(1), Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(e.saturating_add(1) as int == e as int + 1); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_object_done(a, g.ts, g.cx, m as int, x as int, e as int); + } + Some((x, e.saturating_add(1), 17)) +} + +/// §13.2.5 `PropertyDefinition`: `name`, `key : value`, `... value`. +#[verifier::spinoff_prover] +#[verifier::rlimit(100)] +proof fn lemma_property_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + k0: int, + k1: int, + x: int, + mk: int, + j: int, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= k0 < m, + 0 <= k1 < m, + a.nodes[m].kind == NodeKind::PropertyDefinition, + if a.nodes[m].op == Op::Shorthand { + a.nodes[m].children =~= seq![k0] && kd(a, k0) == NodeKind::IdentifierReference + && node_done(a, ts, k0, cx, x, x + 1) && j == x + 1 + } else if a.nodes[m].op == Op::KeyValue { + a.nodes[m].children =~= seq![k0, k1] && is_prop_name_kind(kd(a, k0)) && is_assign_expr( + a, + k1, + ) && node_done(a, ts, k0, cx, x, mk) && is_p(ts, mk, ":"@) && node_done( + a, + ts, + k1, + cx, + mk + 1, + j, + ) + } else { + a.nodes[m].op == Op::Spread && a.nodes[m].children =~= seq![k0] && is_p(ts, x, "..."@) + && is_assign_expr(a, k0) && node_done(a, ts, k0, cx, x + 1, j) + }, + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == k0); + if a.nodes[m].op == Op::KeyValue { + assert(a.nodes[m].children[1] == k1); + assert(dc(a, ts, m, k0, cx, x, mk)); + assert(dc(a, ts, m, k1, cx_in(cx, true), mk + 1, j)); + } else if a.nodes[m].op == Op::Shorthand { + assert(dc(a, ts, m, k0, cx, x, x + 1)); + } else { + assert(dc(a, ts, m, k0, cx_in(cx, true), x + 1, j)); + } + assert(d_expr_lhs(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.2.5 `PropertyDefinition` (without methods and `name = init`). +#[verifier::spinoff_prover] +fn check_property( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + cx_in(g.cx, true) == g.cx, + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::PropertyDefinition) { + return None; + } + let k0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let (lo0, h0, lv0) = match done_at(done, k0) { + Some(r) => r, + None => { + return None; + }, + }; + let kk0 = kind_at(nodes, k0); + let (j, k1, mk): (usize, usize, usize) = match nd.op { + Op::Shorthand => { + if nd.children.len() != 1 || !matches!(kk0, NodeKind::IdentifierReference) || lo0 != x { + return None; + } + (h0, k0, 0) + }, + Op::KeyValue => { + if nd.children.len() != 2 { + return None; + } + let v = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo1, h1, lv1) = match done_at(done, v) { + Some(r) => r, + None => { + return None; + }, + }; + if !matches!( + kk0, + NodeKind::IdentifierName | NodeKind::StringLiteral | NodeKind::NumericLiteral + | NodeKind::ComputedPropertyName + ) || lo0 != x || lv1 < 0 || !punct_at(src, nodes, toks, gaps, h0, 0x3A, Ghost(GW { g, w: ":"@ })) + || lo1 != h0.saturating_add(1) { + return None; + } + (h1, v, h0) + }, + Op::Spread => { + if nd.children.len() != 1 || lv0 < 0 || lo0 != x.saturating_add(1) + || !ellipsis_at(src, nodes, toks, gaps, x, Ghost(g)) { + return None; + } + (h0, k0, 0) + }, + _ => { + return None; + }, + }; + let _ = (k1, mk); + if !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].op == nd.op); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(a.nodes[m as int].children.len() == nd.children.len()); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k0 as int, g.cx, 0); + assert(kd(a, k0 as int) == kk0); + if nd.op == Op::KeyValue { + assert(mk.saturating_add(0) == mk); + assert(h0.saturating_add(1) as int == h0 as int + 1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k1 as int, g.cx, 1); + assert(a.nodes[m as int].children =~= seq![k0 as int, k1 as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + } else { + assert(a.nodes[m as int].children =~= seq![k0 as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + } + lemma_property_done(a, g.ts, g.cx, m as int, k0 as int, k1 as int, x as int, mk as int, j as int); + } + Some((x, j, -100)) +} + +/// §13.2.5 `ComputedPropertyName : [ AssignmentExpression ]`. +#[verifier::spinoff_prover] +proof fn lemma_computed_done(a: AstV, ts: Stream, cx: Ctx, m: int, c: int, x: int, mc: int) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::ComputedPropertyName, + a.nodes[m].children =~= seq![c], + is_p(ts, x, "["@), + is_assign_expr(a, c), + node_done(a, ts, c, cx, x + 1, mc), + is_p(ts, mc, "]"@), + 0 <= x, + mc < tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[mc].end, + ensures + node_done(a, ts, m, cx, x, mc + 1), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, mc + 1)); + assert(a.nodes[m].children[0] == c); + assert(dc(a, ts, m, c, cx_in(cx, true), x + 1, mc)); + assert(d_expr_lhs(a, ts, m, cx, x, mc + 1)); + lemma_ed_of(a, ts, m, x, mc + 1); +} + +/// §13.2.5 `ComputedPropertyName`. +#[verifier::spinoff_prover] +fn check_computed( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + cx_in(g.cx, true) == g.cx, + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_brackets(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ComputedPropertyName) || nd.children.len() != 1 { + return None; + } + let c = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let (lo_c, mc, lv_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + if !punct_at(src, nodes, toks, gaps, x, 0x5B, Ghost(GW { g, w: "["@ })) || lv_c < 0 + || lo_c != x.saturating_add(1) || !punct_at(src, nodes, toks, gaps, mc, 0x5D, Ghost(GW { g, w: "]"@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, mc.saturating_add(1), Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(mc.saturating_add(1) as int == mc as int + 1); + assert(a.nodes[m as int].children =~= seq![c as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 0); + assert(lvl(a, c as int) >= 0); + lemma_computed_done(a, g.ts, g.cx, m as int, c as int, x as int, mc as int); + } + Some((x, mc.saturating_add(1), -100)) +} + +// --------------------------------------------------------------------- +// Binding patterns (§14.3.3): array and object patterns, their elements and +// properties, and `...rest` of a name or a pattern. +// --------------------------------------------------------------------- + +/// §14.3.3 `BindingRestElement : ... target`, `BindingRestProperty : ... BindingIdentifier`. +#[verifier::spinoff_prover] +proof fn lemma_binding_rest_done(a: AstV, ts: Stream, cx: Ctx, m: int, c: int, x: int, j: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::BindingRestElement || a.nodes[m].kind + == NodeKind::BindingRestProperty || a.nodes[m].kind == NodeKind::AssignmentRestElement + || a.nodes[m].kind == NodeKind::AssignmentRestProperty, + a.nodes[m].children =~= seq![c], + is_p(ts, x, "..."@), + (a.nodes[m].kind == NodeKind::AssignmentRestElement || a.nodes[m].kind + == NodeKind::AssignmentRestProperty) ==> is_destructuring_target(a, c), + a.nodes[m].kind == NodeKind::BindingRestElement ==> is_binding_target_kind(kd(a, c)), + a.nodes[m].kind == NodeKind::BindingRestProperty ==> kd(a, c) == NodeKind::BindingIdentifier, + node_done(a, ts, c, cx, x + 1, j), + x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c); + assert(dc(a, ts, m, c, cx, x + 1, j)); + assert(d_pat(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.3.3 `BindingRestElement`, `BindingRestProperty`. +#[verifier::spinoff_prover] +fn check_binding_rest( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!( + nd.kind, + NodeKind::BindingRestElement | NodeKind::BindingRestProperty | NodeKind::AssignmentRestElement + | NodeKind::AssignmentRestProperty + ) || nd.children.len() != 1 { + return None; + } + let c = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let (lo_c, j, lv_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + let kc = kind_at(nodes, c); + let kind_ok = match nd.kind { + NodeKind::BindingRestElement => matches!( + kc, + NodeKind::BindingIdentifier | NodeKind::ObjectBindingPattern | NodeKind::ArrayBindingPattern + ), + NodeKind::BindingRestProperty => matches!(kc, NodeKind::BindingIdentifier), + _ => matches!(kc, NodeKind::ObjectAssignmentPattern | NodeKind::ArrayAssignmentPattern) + || (lv_c >= 14 && !matches!(kc, NodeKind::ObjectLiteral | NodeKind::ArrayLiteral)), + }; + if !ellipsis_at(src, nodes, toks, gaps, x, Ghost(g)) || !kind_ok || lo_c != x.saturating_add(1) + || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(a.nodes[m as int].children =~= seq![c as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 0); + assert(kd(a, c as int) == kc); + lemma_binding_rest_done(a, g.ts, g.cx, m as int, c as int, x as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.3.3 `BindingProperty : SingleNameBinding | PropertyName : BindingElement`. +#[verifier::spinoff_prover] +proof fn lemma_binding_property_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + k0: int, + k1: int, + x: int, + mk: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= k0 < m, + 0 <= k1 < m, + a.nodes[m].kind == NodeKind::BindingProperty, + if a.nodes[m].op == Op::Shorthand { + a.nodes[m].children =~= seq![k0] && kd(a, k0) == NodeKind::BindingElement + && first_kid_kind(a, k0) == NodeKind::BindingIdentifier && node_done( + a, + ts, + k0, + cx, + x, + j, + ) + } else { + a.nodes[m].op == Op::KeyValue && a.nodes[m].children =~= seq![k0, k1] + && is_prop_name_kind(kd(a, k0)) && kd(a, k1) == NodeKind::BindingElement && node_done( + a, + ts, + k0, + cx, + x, + mk, + ) && is_p(ts, mk, ":"@) && node_done(a, ts, k1, cx, mk + 1, j) + }, + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == k0); + if a.nodes[m].op == Op::KeyValue { + assert(a.nodes[m].children[1] == k1); + assert(dc(a, ts, m, k0, cx, x, mk)); + assert(dc(a, ts, m, k1, cx, mk + 1, j)); + } else { + assert(dc(a, ts, m, k0, cx, x, j)); + } + assert(d_pat(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.3.3 `BindingProperty`. +#[verifier::spinoff_prover] +fn check_binding_property( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::BindingProperty) { + return None; + } + let k0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let (lo0, h0, _) = match done_at(done, k0) { + Some(r) => r, + None => { + return None; + }, + }; + let kk0 = kind_at(nodes, k0); + let (j, k1, mk): (usize, usize, usize) = match nd.op { + Op::Shorthand => { + // The element's target is a name. + let first = match kid_of(nodes, k0, 0) { + Some(c) => matches!(kind_at(nodes, c), NodeKind::BindingIdentifier), + None => false, + }; + if nd.children.len() != 1 || !matches!(kk0, NodeKind::BindingElement) || !first + || lo0 != x { + return None; + } + (h0, k0, 0) + }, + Op::KeyValue => { + if nd.children.len() != 2 { + return None; + } + let v = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo1, h1, _) = match done_at(done, v) { + Some(r) => r, + None => { + return None; + }, + }; + if !matches!( + kk0, + NodeKind::IdentifierName | NodeKind::StringLiteral | NodeKind::NumericLiteral + | NodeKind::ComputedPropertyName + ) || !matches!(kind_at(nodes, v), NodeKind::BindingElement) || lo0 != x + || !punct_at(src, nodes, toks, gaps, h0, 0x3A, Ghost(GW { g, w: ":"@ })) + || lo1 != h0.saturating_add(1) { + return None; + } + (h1, v, h0) + }, + _ => { + return None; + }, + }; + let _ = (k1, mk); + if !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(a.nodes[k0 as int] == nodes@[k0 as int]@); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].op == nd.op); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k0 as int, g.cx, 0); + assert(kd(a, k0 as int) == kk0); + if nd.op == Op::KeyValue { + assert(h0.saturating_add(1) as int == h0 as int + 1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k1 as int, g.cx, 1); + assert(a.nodes[m as int].children =~= seq![k0 as int, k1 as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + } else { + assert(a.nodes[m as int].children =~= seq![k0 as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + } + lemma_binding_property_done(a, g.ts, g.cx, m as int, k0 as int, k1 as int, x as int, mk as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.3.3 `ArrayBindingPattern : [ elements ]`. +#[verifier::spinoff_prover] +proof fn lemma_array_pattern_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, e: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= x, + x + 1 <= e < tn(ts), + a.nodes[m].kind == NodeKind::ArrayBindingPattern, + darray(a, ts, m, a.nodes[m].children, 0, cx, x + 1, e), + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> array_item_mode_ok( + a, + #[trigger] a.nodes[m].children[k], + 6, + ), + rest_only_last(a, a.nodes[m].children), + is_p(ts, x, "["@), + is_p(ts, e, "]"@), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[e].end, + ensures + node_done(a, ts, m, cx, x, e + 1), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, e + 1)); + assert forall|k: int| 0 <= k < a.nodes[m].children.len() implies kd( + a, + #[trigger] a.nodes[m].children[k], + ) == NodeKind::BindingElement || kd(a, a.nodes[m].children[k]) == NodeKind::Elision || kd( + a, + a.nodes[m].children[k], + ) == NodeKind::BindingRestElement by { + assert(array_item_mode_ok(a, a.nodes[m].children[k], 6)); + } + assert(d_pat(a, ts, m, cx, x, e + 1)); + lemma_ed_of(a, ts, m, x, e + 1); +} + +/// §14.3.3 `ObjectBindingPattern : { BindingPropertyList ,? }`. +#[verifier::spinoff_prover] +proof fn lemma_object_pattern_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, e: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= x, + x + 1 <= e < tn(ts), + a.nodes[m].kind == NodeKind::ObjectBindingPattern, + dsep(a, ts, m, a.nodes[m].children, 0, cx, x + 1, e, true), + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> list_item_ok(a, #[trigger] a.nodes[m].children[k], 7), + rest_only_last(a, a.nodes[m].children), + is_p(ts, x, "{"@), + is_p(ts, e, "}"@), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[e].end, + ensures + node_done(a, ts, m, cx, x, e + 1), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, e + 1)); + assert(all_kind2(a, a.nodes[m].children, 0, NodeKind::BindingProperty, NodeKind::BindingRestProperty)) by { + assert forall|k: int| 0 <= k < a.nodes[m].children.len() implies kd( + a, + #[trigger] a.nodes[m].children[k], + ) == NodeKind::BindingProperty || kd(a, a.nodes[m].children[k]) + == NodeKind::BindingRestProperty by { + assert(list_item_ok(a, a.nodes[m].children[k], 7)); + } + } + assert(d_pat(a, ts, m, cx, x, e + 1)); + lemma_ed_of(a, ts, m, x, e + 1); +} + +/// What `check_pattern` reads: the node and whether it is an array pattern. +pub struct PatShape { + /// The node. + pub m: usize, + /// An array pattern (else an object pattern). + pub array: bool, + /// An assignment pattern (else a binding pattern). + pub assign: bool, +} + +/// §14.3.3 `ArrayBindingPattern`, `ObjectBindingPattern`. +#[verifier::spinoff_prover] +fn check_pattern( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + sh: &PatShape, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + sh.m < nodes.len(), + nodes@[sh.m as int].kind == (if sh.assign { + if sh.array { + NodeKind::ArrayAssignmentPattern + } else { + NodeKind::ObjectAssignmentPattern + } + } else if sh.array { + NodeKind::ArrayBindingPattern + } else { + NodeKind::ObjectBindingPattern + }), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, sh.m as int), + cx_ok(arena_of(nodes@), done.cxs@, sh.m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + sh.m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), sh.m as int), + None => true, + }, +{ + let m = sh.m; + proof { + lemma_brackets(); + lemma_brace_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let x1 = x.saturating_add(1); + let (e, close_ok): (usize, bool) = if sh.array { + if !punct_at(src, nodes, toks, gaps, x, 0x5B, Ghost(GW { g, w: "["@ })) { + return None; + } + let als = ListSpec { m, start: x1, mode: if sh.assign { 8 } else { 6 }, trail: true }; + let e = match check_array_items(src, nodes, toks, gaps, done, &als, Ghost(g)) { + Some(e) => e, + None => { + return None; + }, + }; + (e, punct_at(src, nodes, toks, gaps, e, 0x5D, Ghost(GW { g, w: "]"@ }))) + } else { + if !punct_at(src, nodes, toks, gaps, x, 0x7B, Ghost(GW { g, w: "{"@ })) { + return None; + } + let ls = ListSpec { m, start: x1, mode: if sh.assign { 9 } else { 7 }, trail: true }; + let e = match check_list(src, nodes, toks, gaps, done, &ls, Ghost(g)) { + Some(e) => e, + None => { + return None; + }, + }; + (e, punct_at(src, nodes, toks, gaps, e, 0x7D, Ghost(GW { g, w: "}"@ }))) + }; + if !close_ok || !end_ok(src, nodes, toks, gaps, nd.end, e.saturating_add(1), Ghost(g)) + || !check_rest_last(nodes, m) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x1 as int == x as int + 1); + assert(e.saturating_add(1) as int == e as int + 1); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + if sh.assign { + if sh.array { + lemma_array_apattern_done(a, g.ts, g.cx, m as int, x as int, e as int); + } else { + lemma_object_apattern_done(a, g.ts, g.cx, m as int, x as int, e as int); + } + } else if sh.array { + lemma_array_pattern_done(a, g.ts, g.cx, m as int, x as int, e as int); + } else { + lemma_object_pattern_done(a, g.ts, g.cx, m as int, x as int, e as int); + } + } + Some((x, e.saturating_add(1), -100)) +} + +// --------------------------------------------------------------------- +// Assignment patterns (§13.15.5): `[a, b = 1, ...c] = d`, `({a, b: c, ...d} = e)`. +// --------------------------------------------------------------------- + +/// §13.15.5 `ArrayAssignmentPattern : [ elements ]`. +#[verifier::spinoff_prover] +proof fn lemma_array_apattern_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, e: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= x, + x + 1 <= e < tn(ts), + a.nodes[m].kind == NodeKind::ArrayAssignmentPattern, + darray(a, ts, m, a.nodes[m].children, 0, cx, x + 1, e), + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> array_item_mode_ok( + a, + #[trigger] a.nodes[m].children[k], + 8, + ), + rest_only_last(a, a.nodes[m].children), + is_p(ts, x, "["@), + is_p(ts, e, "]"@), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[e].end, + ensures + node_done(a, ts, m, cx, x, e + 1), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, e + 1)); + assert forall|k: int| 0 <= k < a.nodes[m].children.len() implies kd( + a, + #[trigger] a.nodes[m].children[k], + ) == NodeKind::AssignmentElement || kd(a, a.nodes[m].children[k]) == NodeKind::Elision || kd( + a, + a.nodes[m].children[k], + ) == NodeKind::AssignmentRestElement by { + assert(array_item_mode_ok(a, a.nodes[m].children[k], 8)); + } + assert(d_pat(a, ts, m, cx, x, e + 1)); + lemma_ed_of(a, ts, m, x, e + 1); +} + +/// §13.15.5 `ObjectAssignmentPattern : { AssignmentPropertyList ,? }`. +#[verifier::spinoff_prover] +proof fn lemma_object_apattern_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, e: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= x, + x + 1 <= e < tn(ts), + a.nodes[m].kind == NodeKind::ObjectAssignmentPattern, + dsep(a, ts, m, a.nodes[m].children, 0, cx, x + 1, e, true), + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> list_item_ok(a, #[trigger] a.nodes[m].children[k], 9), + rest_only_last(a, a.nodes[m].children), + is_p(ts, x, "{"@), + is_p(ts, e, "}"@), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[e].end, + ensures + node_done(a, ts, m, cx, x, e + 1), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, e + 1)); + assert(all_kind2(a, a.nodes[m].children, 0, NodeKind::AssignmentProperty, NodeKind::AssignmentRestProperty)) by { + assert forall|k: int| 0 <= k < a.nodes[m].children.len() implies kd( + a, + #[trigger] a.nodes[m].children[k], + ) == NodeKind::AssignmentProperty || kd(a, a.nodes[m].children[k]) + == NodeKind::AssignmentRestProperty by { + assert(list_item_ok(a, a.nodes[m].children[k], 9)); + } + } + assert(d_pat(a, ts, m, cx, x, e + 1)); + lemma_ed_of(a, ts, m, x, e + 1); +} + +/// §13.15.5 `AssignmentProperty : IdentifierReference Initializer? | PropertyName : AssignmentElement`. +#[verifier::spinoff_prover] +#[verifier::rlimit(100)] +proof fn lemma_assignment_property_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + k0: int, + k1: int, + x: int, + mk: int, + j: int, + with_init: bool, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= k0 < m, + 0 <= k1 < m, + a.nodes[m].kind == NodeKind::AssignmentProperty, + a.nodes[m].children =~= seq![k0, k1], + 0 <= x < j <= tn(ts), + if a.nodes[m].op == Op::Shorthand { + kd(a, k0) == NodeKind::IdentifierReference && node_done(a, ts, k0, cx, x, x + 1) && ( + if with_init { + is_p(ts, x + 1, "="@) && is_assign_expr(a, k1) && node_done( + a, + ts, + k1, + cx_in(cx, true), + x + 2, + j, + ) + } else { + kd(a, k1) == NodeKind::Absent && j == x + 1 && d( + a, + ts, + k1, + cx_in(cx, true), + x + 1, + x + 1, + ) + }) + } else { + a.nodes[m].op == Op::KeyValue && is_prop_name_kind(kd(a, k0)) && kd(a, k1) + == NodeKind::AssignmentElement && node_done(a, ts, k0, cx, x, mk) && is_p( + ts, + mk, + ":"@, + ) && node_done(a, ts, k1, cx, mk + 1, j) + }, + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == k0); + assert(a.nodes[m].children[1] == k1); + if a.nodes[m].op == Op::KeyValue { + assert(dc(a, ts, m, k0, cx, x, mk)); + assert(dc(a, ts, m, k1, cx, mk + 1, j)); + } else { + assert(dc(a, ts, m, k0, cx, x, x + 1)); + if with_init { + assert(dc(a, ts, m, k1, cx_in(cx, true), x + 2, j)); + assert(d_init(a, ts, m, k1, cx_in(cx, true), x + 1, j, false)); + } else { + assert(dc(a, ts, m, k1, cx_in(cx, true), x + 1, x + 1)); + assert(d_init(a, ts, m, k1, cx_in(cx, true), x + 1, j, false)); + } + } + assert(d_pat(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.15.5 `AssignmentProperty`. +#[verifier::spinoff_prover] +fn check_assignment_property( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok_init( + arena_of(nodes@), + done.cxs@, + m as int, + g.cx, + if nodes@[m as int].op == Op::Shorthand { + cx_in(g.cx, true) + } else { + g.cx + }, + ), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + reveal_strlit("="); + assert("="@ =~= seq!['=']); + } + let ghost gi = GCx { ts: g.ts, cx: cx_in(g.cx, true) }; + if !matches!(nd.kind, NodeKind::AssignmentProperty) || nd.children.len() != 2 { + return None; + } + let k0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let k1 = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let (lo0, h0, _) = match done_at(done, k0) { + Some(r) => r, + None => { + return None; + }, + }; + let kk0 = kind_at(nodes, k0); + let kk1 = kind_at(nodes, k1); + let x1 = x.saturating_add(1); + let (j, with_init): (usize, bool) = match nd.op { + Op::Shorthand => { + if !matches!(kk0, NodeKind::IdentifierReference) || lo0 != x || h0 != x1 { + return None; + } + if matches!(kk1, NodeKind::Absent) { + if !check_absent(src, nodes, toks, gaps, k1, x1, Ghost(gi)) { + return None; + } + (x1, false) + } else { + let (lo1, h1, lv1) = match done_at(done, k1) { + Some(r) => r, + None => { + return None; + }, + }; + if lv1 < 0 || lo1 != x.saturating_add(2) + || !punct_at(src, nodes, toks, gaps, x1, 0x3D, Ghost(GW { g, w: "="@ })) { + return None; + } + (h1, true) + } + }, + Op::KeyValue => { + let (lo1, h1, _) = match done_at(done, k1) { + Some(r) => r, + None => { + return None; + }, + }; + if !matches!( + kk0, + NodeKind::IdentifierName | NodeKind::StringLiteral | NodeKind::NumericLiteral + | NodeKind::ComputedPropertyName + ) || !matches!(kk1, NodeKind::AssignmentElement) || lo0 != x + || !punct_at(src, nodes, toks, gaps, h0, 0x3A, Ghost(GW { g, w: ":"@ })) + || lo1 != h0.saturating_add(1) { + return None; + } + (h1, false) + }, + _ => { + return None; + }, + }; + let _ = with_init; + if !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x1 as int == x as int + 1); + assert(a.nodes[m as int].children =~= seq![k0 as int, k1 as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].op == nd.op); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + let ci = if nd.op == Op::Shorthand { cx_in(g.cx, true) } else { g.cx }; + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k0 as int, g.cx, ci, 0); + assert(kd(a, k0 as int) == kk0); + assert(kd(a, k1 as int) == kk1); + let mk: usize = if nd.op == Op::KeyValue { h0 } else { 0 }; + if nd.op == Op::KeyValue { + assert(h0.saturating_add(1) as int == h0 as int + 1); + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k1 as int, g.cx, ci, 1); + } else if with_init { + assert(x.saturating_add(2) as int == x as int + 2); + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k1 as int, g.cx, ci, 1); + assert(is_assign_expr(a, k1 as int)); + } + lemma_assignment_property_done(a, g.ts, g.cx, m as int, k0 as int, k1 as int, x as int, mk as int, j as int, with_init); + } + Some((x, j, -100)) +} + +// --------------------------------------------------------------------- +// `++` and `--` (§13.4), `a[b]` (§13.3.2) and `new` (§13.3.5). +// --------------------------------------------------------------------- + +/// The words `new` as code points, and the punctuators `++`, `--`. +proof fn lemma_words6() + ensures + cps("new"@) =~= seq![0x6Eu32, 0x65, 0x77], + cps("++"@) =~= seq![0x2Bu32, 0x2B], + cps("--"@) =~= seq![0x2Du32, 0x2D], +{ + reveal_strlit("new"); + reveal_strlit("++"); + reveal_strlit("--"); + assert("new"@ =~= seq!['n', 'e', 'w']); + assert("++"@ =~= seq!['+', '+']); + assert("--"@ =~= seq!['-', '-']); +} + +/// Token `x` is `++` (`inc`) or `--`. +fn incdec_at( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + x: usize, + inc: bool, + Ghost(g): Ghost, +) -> (r: bool) + requires + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + r ==> x < toks.len() && is_p(g.ts, x as int, if inc { + "++"@ + } else { + "--"@ + }), +{ + let _ = (nodes.len(), gaps.len()); + let t = match toks.get(x) { + Some(t) => t, + None => { + return false; + }, + }; + proof { + lemma_words6(); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(t == &toks@[x as int]); + } + let r = matches!(t.kind, TokKind::Punctuator) + && (if inc { text_is(src, t, &[0x2B, 0x2B]) } else { text_is(src, t, &[0x2D, 0x2D]) }); + proof { + if r { + assert(g.ts.toks[x as int].text =~= src@.subrange(t.start as int, t.end as int)); + } + } + r +} + +/// §13.4 `UpdateExpression`. +#[verifier::spinoff_prover] +#[verifier::rlimit(100)] +proof fn lemma_update_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c: int, + x: int, + prefix: bool, + inc: bool, + mo: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::UpdateExpression, + a.nodes[m].children =~= seq![c], + a.nodes[m].op == (if prefix { + if inc { + Op::PrefixIncrement + } else { + Op::PrefixDecrement + } + } else if inc { + Op::PostfixIncrement + } else { + Op::PostfixDecrement + }), + if prefix { + is_p(ts, x, if inc { + "++"@ + } else { + "--"@ + }) && lvl(a, c) >= 12 && node_done(a, ts, c, cx, x + 1, j) + } else { + lvl(a, c) >= 14 && node_done(a, ts, c, cx, x, mo) && is_p(ts, mo, if inc { + "++"@ + } else { + "--"@ + }) && !nl(ts, mo) && j == mo + 1 + }, + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == 13, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c); + if prefix { + assert(dc(a, ts, m, c, cx, x + 1, j)); + assert(is_op_tok(ts, x, a.nodes[m].op)); + } else { + assert(is_lhs_expr(a, c)); + assert(dc(a, ts, m, c, cx, x, mo)); + assert(is_op_tok(ts, mo, a.nodes[m].op)); + } + assert(d_expr(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.4 `UpdateExpression`. +#[verifier::spinoff_prover] +fn check_update( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::UpdateExpression) || nd.children.len() != 1 { + return None; + } + let (prefix, inc): (bool, bool) = match nd.op { + Op::PrefixIncrement => (true, true), + Op::PrefixDecrement => (true, false), + Op::PostfixIncrement => (false, true), + Op::PostfixDecrement => (false, false), + _ => { + return None; + }, + }; + let c = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let (lo_c, hi_c, lv_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + let x1 = x.saturating_add(1); + let j: usize = if prefix { + if lv_c < 12 || lo_c != x1 || !incdec_at(src, nodes, toks, gaps, x, inc, Ghost(g)) { + return None; + } + hi_c + } else { + if lv_c < 14 || lo_c != x || !incdec_at(src, nodes, toks, gaps, hi_c, inc, Ghost(g)) + || !no_nl_at(src, nodes, toks, gaps, hi_c, Ghost(g)) { + return None; + } + hi_c.saturating_add(1) + }; + if !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x1 as int == x as int + 1); + assert(a.nodes[m as int].children =~= seq![c as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].op == nd.op); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 0); + if !prefix { + assert(hi_c.saturating_add(1) as int == hi_c as int + 1); + } + lemma_update_done(a, g.ts, g.cx, m as int, c as int, x as int, prefix, inc, hi_c as int, j as int); + } + Some((x, j, 13)) +} + +/// §13.3.2 `MemberExpression [ Expression ]`. +#[verifier::spinoff_prover] +#[verifier::rlimit(100)] +proof fn lemma_member_bracket_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + o: int, + p: int, + x: int, + mo: int, + q: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= o < m, + 0 <= p < m, + a.nodes[m].kind == NodeKind::MemberExpression, + a.nodes[m].op == Op::Bracket, + a.nodes[m].children =~= seq![o, p], + is_member_or_call(a, o), + node_done(a, ts, o, cx, x, mo), + is_p(ts, mo, "["@), + lvl(a, p) >= -1, + node_done(a, ts, p, cx_in(cx, true), mo + 1, q), + is_p(ts, q, "]"@), + 0 <= x < q + 1 <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[q].end, + ensures + node_done(a, ts, m, cx, x, q + 1), + lvl(a, m) == (if lvl(a, o) == 17 { + 17int + } else { + 15int + }), +{ + assert(span_ok(a, ts, m, x, q + 1)); + assert(a.nodes[m].children[0] == o); + assert(a.nodes[m].children[1] == p); + assert(dc(a, ts, m, o, cx, x, mo)); + assert(dc(a, ts, m, p, cx_in(cx, true), mo + 1, q)); + assert(d_expr_lhs(a, ts, m, cx, x, q + 1)); + lemma_ed_of(a, ts, m, x, q + 1); +} + +/// §13.3.2 `object [ expression ]`. +#[verifier::spinoff_prover] +fn check_member_bracket( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok_init(arena_of(nodes@), done.cxs@, m as int, g.cx, cx_in(g.cx, true)), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_brackets(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::MemberExpression) || !matches!(nd.op, Op::Bracket) + || nd.children.len() != 2 { + return None; + } + let o = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let p = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_o, mo, lv_o) = match done_at(done, o) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo_p, q, lv_p) = match done_at(done, p) { + Some(r) => r, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if lo_o != x || !(lv_o == 17 || lv_o == 15) || lv_p < -1 || lo_p != mo.saturating_add(1) + || !punct_at(src, nodes, toks, gaps, mo, 0x5B, Ghost(GW { g, w: "["@ })) + || !punct_at(src, nodes, toks, gaps, q, 0x5D, Ghost(GW { g, w: "]"@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, q.saturating_add(1), Ghost(g)) || lo_o >= q { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(mo.saturating_add(1) as int == mo as int + 1); + assert(q.saturating_add(1) as int == q as int + 1); + assert(a.nodes[m as int].children =~= seq![o as int, p as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].op == nd.op); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, o as int, g.cx, cx_in(g.cx, true), 0); + lemma_inv_get_init(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, p as int, g.cx, cx_in(g.cx, true), 1); + assert(is_member_or_call(a, o as int)); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, q as int); + assert(g.ts.toks[q as int].end == nd.end as int); + lemma_member_bracket_done(a, g.ts, g.cx, m as int, o as int, p as int, lo_o as int, mo as int, q as int); + } + let own: i32 = if lv_o == 17 { 17 } else { 15 }; + Some((lo_o, q.saturating_add(1), own)) +} + +/// §13.3.5 `new MemberExpression Arguments`, `new NewExpression`. +#[verifier::spinoff_prover] +#[verifier::rlimit(100)] +proof fn lemma_new_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c: int, + ar: int, + x: int, + mc: int, + with_args: bool, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + 0 <= ar < m, + a.nodes[m].kind == NodeKind::NewExpression, + a.nodes[m].children =~= seq![c, ar], + is_kw(ts, x, "new"@), + node_done(a, ts, c, cx, x + 1, mc), + if with_args { + lvl(a, c) == 17 && kd(a, ar) == NodeKind::Arguments && node_done(a, ts, ar, cx, mc, j) + } else { + kd(a, ar) == NodeKind::Absent && (lvl(a, c) == 16 || lvl(a, c) == 17) && j == mc && d( + a, + ts, + ar, + cx, + mc, + mc, + ) && ed(a, ts, ar, mc) == mc + }, + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == (if with_args { + 17int + } else { + 16int + }), +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c); + assert(a.nodes[m].children[1] == ar); + assert(dc(a, ts, m, c, cx, x + 1, mc)); + if with_args { + assert(dc(a, ts, m, ar, cx, mc, j)); + } else { + assert(dc(a, ts, m, ar, cx, mc, mc)); + } + assert(d_expr_lhs(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.3.5 `new callee Arguments?`. +#[verifier::spinoff_prover] +fn check_new( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words6(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::NewExpression) || nd.children.len() != 2 { + return None; + } + let c = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let ar = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let (lo_c, mc, lv_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + if !word_at(src, nodes, toks, gaps, x, &[0x6E, 0x65, 0x77], Ghost(GW { g, w: "new"@ })) + || lo_c != x.saturating_add(1) { + return None; + } + let ka = kind_at(nodes, ar); + let with_args = !matches!(ka, NodeKind::Absent); + let j: usize = if with_args { + let (lo_a, h_a, _) = match done_at(done, ar) { + Some(r) => r, + None => { + return None; + }, + }; + if lv_c != 17 || !matches!(ka, NodeKind::Arguments) || lo_a != mc { + return None; + } + h_a + } else { + if !(lv_c == 16 || lv_c == 17) || !check_absent(src, nodes, toks, gaps, ar, mc, Ghost(g)) { + return None; + } + mc + }; + if !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(a.nodes[m as int].children =~= seq![c as int, ar as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 0); + assert(kd(a, ar as int) == ka); + if with_args { + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, ar as int, g.cx, 1); + } + lemma_new_done(a, g.ts, g.cx, m as int, c as int, ar as int, x as int, mc as int, with_args, j as int); + } + Some((x, j, if with_args { 17 } else { 16 })) +} + +// --------------------------------------------------------------------- +// Classes (§15.7) and methods (§15.4 to §15.8): `class`, its body, methods, +// getters and setters, fields, static blocks, private names. +// --------------------------------------------------------------------- + +/// §12.7 `PrivateIdentifier`. +#[verifier::spinoff_prover] +fn check_private( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::PrivateIdentifier) || !nd.children.is_empty() { + return None; + } + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let t = match toks.get(x) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int); + assert(t == &toks@[x as int]); + } + if !matches!(t.kind, TokKind::PrivateIdentifier) || t.end != nd.end { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].children.len() == 0); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(g.ts.toks[x as int].end == t.end as int); + assert(g.ts.toks[x as int].kind == TokKind::PrivateIdentifier); + assert(g.ts.toks[x as int].start < g.ts.toks[x as int].end); + assert(span_ok(a, g.ts, m as int, x as int, x as int + 1)); + assert(d_expr_lhs(a, g.ts, m as int, g.cx, x as int, x as int + 1)); + lemma_ed_of(a, g.ts, m as int, x as int, x as int + 1); + } + Some((x, x.saturating_add(1), -100)) +} + +/// The class element name kinds (§15.7 `ClassElementName`), executable. +fn element_name_exec(k: NodeKind) -> (r: bool) + ensures + r ==> is_class_element_name_kind(k), +{ + matches!( + k, + NodeKind::IdentifierName | NodeKind::StringLiteral | NodeKind::NumericLiteral + | NodeKind::ComputedPropertyName | NodeKind::PrivateIdentifier + ) +} + +/// §15.4 `MethodDefinition` and its generator, `async` and accessor forms. +#[verifier::spinoff_prover] +#[verifier::rlimit(150)] +proof fn lemma_method_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + n0: int, + p: int, + b: int, + x: int, + mn: int, + q: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= n0 < m, + 0 <= p < m, + 0 <= b < m, + a.nodes[m].kind == NodeKind::MethodDefinition, + a.nodes[m].children =~= seq![n0, p, b], + a.nodes[m].op == Op::Method || a.nodes[m].op == Op::Getter || a.nodes[m].op == Op::Setter + || a.nodes[m].op == Op::GeneratorMethod || a.nodes[m].op == Op::AsyncMethod + || a.nodes[m].op == Op::AsyncGeneratorMethod, + !a.nodes[m].flag || is_kw(ts, x, "static"@), + ({ + let p0 = if a.nodes[m].flag { + x + 1 + } else { + x + }; + let op = a.nodes[m].op; + let nm = match op { + Op::Method => p0, + Op::Getter | Op::Setter | Op::GeneratorMethod | Op::AsyncMethod => p0 + 1, + _ => p0 + 2, + }; + let is_gen = op == Op::GeneratorMethod || op == Op::AsyncGeneratorMethod; + let is_async = op == Op::AsyncMethod || op == Op::AsyncGeneratorMethod; + (match op { + Op::Method => true, + Op::Getter => is_kw(ts, p0, "get"@), + Op::Setter => is_kw(ts, p0, "set"@), + Op::GeneratorMethod => is_p(ts, p0, "*"@), + Op::AsyncMethod => is_kw(ts, p0, "async"@) && !nl(ts, p0 + 1), + Op::AsyncGeneratorMethod => is_kw(ts, p0, "async"@) && !nl(ts, p0 + 1) && is_p( + ts, + p0 + 1, + "*"@, + ), + _ => false, + }) && is_class_element_name_kind(kd(a, n0)) && kd(a, p) == NodeKind::FormalParameters + && kd(a, b) == NodeKind::FunctionBody && (op == Op::Getter ==> kids(a, p).len() + == 0) && (op == Op::Setter ==> (kids(a, p).len() == 1 && kd( + a, + kid(kids(a, p), 0), + ) == NodeKind::BindingElement)) && node_done(a, ts, n0, cx, nm, mn) && node_done( + a, + ts, + p, + cx_fn(is_gen, is_async, false), + mn, + q, + ) && node_done(a, ts, b, cx_fn(is_gen, is_async, true), q, j) + }), + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == n0); + assert(a.nodes[m].children[1] == p); + assert(a.nodes[m].children[2] == b); + let p0 = if a.nodes[m].flag { + x + 1 + } else { + x + }; + let op = a.nodes[m].op; + let nm = match op { + Op::Method => p0, + Op::Getter | Op::Setter | Op::GeneratorMethod | Op::AsyncMethod => p0 + 1, + _ => p0 + 2, + }; + let is_gen = op == Op::GeneratorMethod || op == Op::AsyncGeneratorMethod; + let is_async = op == Op::AsyncMethod || op == Op::AsyncGeneratorMethod; + assert(dc(a, ts, m, n0, cx, nm, mn)); + assert(dc(a, ts, m, p, cx_fn(is_gen, is_async, false), mn, q)); + assert(dc(a, ts, m, b, cx_fn(is_gen, is_async, true), q, j)); + assert(ed(a, ts, n0, nm) == mn); + assert(ed(a, ts, p, mn) == q); + assert(d_method(a, ts, m, cx, x, j)); + assert(d_func(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §15.4 `MethodDefinition`. +#[verifier::spinoff_prover] +fn check_method( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + 0 <= m < done.cxs@.len(), + cx_of(done.cxs@[m as int]) == g.cx, + cx_kids3( + arena_of(nodes@), + done.cxs@, + m as int, + g.cx, + cx_fn( + nodes@[m as int].op == Op::GeneratorMethod || nodes@[m as int].op + == Op::AsyncGeneratorMethod, + nodes@[m as int].op == Op::AsyncMethod || nodes@[m as int].op + == Op::AsyncGeneratorMethod, + false, + ), + cx_fn( + nodes@[m as int].op == Op::GeneratorMethod || nodes@[m as int].op + == Op::AsyncGeneratorMethod, + nodes@[m as int].op == Op::AsyncMethod || nodes@[m as int].op + == Op::AsyncGeneratorMethod, + true, + ), + ), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words3(); + lemma_words7(); + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::MethodDefinition) || nd.children.len() != 3 { + return None; + } + let n0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let p = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let b = match kid_of(nodes, m, 2) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if nd.flag && !word_at(src, nodes, toks, gaps, x, &[0x73, 0x74, 0x61, 0x74, 0x69, 0x63], Ghost(GW { g, w: "static"@ })) { + return None; + } + let p0: usize = if nd.flag { x.saturating_add(1) } else { x }; + let p1 = p0.saturating_add(1); + let nm: usize = match nd.op { + Op::Method => p0, + Op::Getter | Op::Setter | Op::GeneratorMethod | Op::AsyncMethod => p1, + Op::AsyncGeneratorMethod => p0.saturating_add(2), + _ => { + return None; + }, + }; + let prefix_ok = match nd.op { + Op::Method => true, + Op::Getter => word_at(src, nodes, toks, gaps, p0, &[0x67, 0x65, 0x74], Ghost(GW { g, w: "get"@ })), + Op::Setter => word_at(src, nodes, toks, gaps, p0, &[0x73, 0x65, 0x74], Ghost(GW { g, w: "set"@ })), + Op::GeneratorMethod => punct_at(src, nodes, toks, gaps, p0, 0x2A, Ghost(GW { g, w: "*"@ })), + Op::AsyncMethod => word_at(src, nodes, toks, gaps, p0, &[0x61, 0x73, 0x79, 0x6E, 0x63], Ghost(GW { g, w: "async"@ })) + && no_nl_at(src, nodes, toks, gaps, p1, Ghost(g)), + _ => word_at(src, nodes, toks, gaps, p0, &[0x61, 0x73, 0x79, 0x6E, 0x63], Ghost(GW { g, w: "async"@ })) + && no_nl_at(src, nodes, toks, gaps, p1, Ghost(g)) + && punct_at(src, nodes, toks, gaps, p1, 0x2A, Ghost(GW { g, w: "*"@ })), + }; + let kn = kind_at(nodes, n0); + let (lo_n, mn, _) = match done_at(done, n0) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo_p, q, _) = match done_at(done, p) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo_b, j, _) = match done_at(done, b) { + Some(r) => r, + None => { + return None; + }, + }; + // A getter has no parameters; a setter has one `BindingElement`. + let pn = match nodes.get(p) { + Some(x) => x, + None => { + return None; + }, + }; + let params_ok = match nd.op { + Op::Getter => pn.children.is_empty(), + Op::Setter => { + pn.children.len() == 1 && match kid_of(nodes, p, 0) { + Some(c) => matches!(kind_at(nodes, c), NodeKind::BindingElement), + None => false, + } + }, + _ => true, + }; + if !prefix_ok || !element_name_exec(kn) || lo_n != nm || !params_ok + || !matches!(kind_at(nodes, p), NodeKind::FormalParameters) || lo_p != mn + || !matches!(kind_at(nodes, b), NodeKind::FunctionBody) || lo_b != q + || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(a.nodes[p as int] == nodes@[p as int]@); + assert(pn == &nodes@[p as int]); + assert(a.nodes[m as int].children =~= seq![n0 as int, p as int, b as int]) by { + assert(a.nodes[m as int].children.len() == 3); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].op == nd.op); + assert(a.nodes[m as int].flag == nd.flag); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(x.saturating_add(1) as int == x as int + 1); + assert(p0 as int == (if nd.flag { x as int + 1 } else { x as int })); + assert(p0 < toks.len() || nd.op == Op::Method); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, n0 as int); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, p as int); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, b as int); + assert(kd(a, n0 as int) == kn); + assert(kd(a, p as int) == NodeKind::FormalParameters); + assert(kd(a, b as int) == NodeKind::FunctionBody); + assert(a.nodes[p as int].children.len() == pn.children.len()); + assert(kids(a, p as int) == a.nodes[p as int].children); + if nd.op == Op::Setter { + let c0 = a.nodes[p as int].children[0]; + assert(kid(kids(a, p as int), 0) == c0); + } + assert(cx_of(done.cxs@[n0 as int]) == g.cx); + lemma_method_done(a, g.ts, g.cx, m as int, n0 as int, p as int, b as int, x as int, mn as int, q as int, j as int); + } + Some((x, j, -100)) +} + +/// The words `static`, `get`, `set`, `extends`, as code points. +proof fn lemma_words7() + ensures + cps("static"@) =~= seq![0x73u32, 0x74, 0x61, 0x74, 0x69, 0x63], + cps("get"@) =~= seq![0x67u32, 0x65, 0x74], + cps("set"@) =~= seq![0x73u32, 0x65, 0x74], + cps("extends"@) =~= seq![0x65u32, 0x78, 0x74, 0x65, 0x6E, 0x64, 0x73], + cps("class"@) =~= seq![0x63u32, 0x6C, 0x61, 0x73, 0x73], +{ + reveal_strlit("static"); + reveal_strlit("get"); + reveal_strlit("set"); + reveal_strlit("extends"); + reveal_strlit("class"); + assert("static"@ =~= seq!['s', 't', 'a', 't', 'i', 'c']); + assert("get"@ =~= seq!['g', 'e', 't']); + assert("set"@ =~= seq!['s', 'e', 't']); + assert("extends"@ =~= seq!['e', 'x', 't', 'e', 'n', 'd', 's']); + assert("class"@ =~= seq!['c', 'l', 'a', 's', 's']); +} + +/// §15.7 `FieldDefinition : ClassElementName Initializer? ;`, with `static`. +#[verifier::spinoff_prover] +#[verifier::rlimit(100)] +proof fn lemma_field_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + n0: int, + k1: int, + x: int, + mn: int, + q: int, + j: int, + with_init: bool, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= n0 < m, + 0 <= k1 < m, + a.nodes[m].kind == NodeKind::FieldDefinition, + a.nodes[m].children =~= seq![n0, k1], + !a.nodes[m].flag || is_kw(ts, x, "static"@), + is_class_element_name_kind(kd(a, n0)), + node_done(a, ts, n0, cx, if a.nodes[m].flag { + x + 1 + } else { + x + }, mn), + if with_init { + is_p(ts, mn, "="@) && is_assign_expr(a, k1) && node_done(a, ts, k1, cx, mn + 1, q) + } else { + kd(a, k1) == NodeKind::Absent && q == mn && d(a, ts, k1, cx, mn, mn) && ed( + a, + ts, + k1, + mn, + ) == mn + }, + semi(ts, q, j, false), + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == n0); + assert(a.nodes[m].children[1] == k1); + let s = if a.nodes[m].flag { + x + 1 + } else { + x + }; + assert(dc(a, ts, m, n0, cx, s, mn)); + if with_init { + assert(dc(a, ts, m, k1, cx_in(cx, true), mn + 1, q)); + assert(d_init(a, ts, m, k1, cx_in(cx, true), mn, q, false)); + assert(ed(a, ts, k1, mn + 1) == q); + } else { + assert(dc(a, ts, m, k1, cx_in(cx, true), mn, mn)); + assert(d_init(a, ts, m, k1, cx_in(cx, true), mn, q, false)); + } + assert(d_func(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §15.7 `FieldDefinition`. +#[verifier::spinoff_prover] +fn check_field( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + cx_in(g.cx, true) == g.cx, + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words7(); + reveal_strlit("="); + assert("="@ =~= seq!['=']); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::FieldDefinition) || nd.children.len() != 2 { + return None; + } + let n0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let k1 = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if nd.flag && !word_at(src, nodes, toks, gaps, x, &[0x73, 0x74, 0x61, 0x74, 0x69, 0x63], Ghost(GW { g, w: "static"@ })) { + return None; + } + let s: usize = if nd.flag { x.saturating_add(1) } else { x }; + let (lo_n, mn, _) = match done_at(done, n0) { + Some(r) => r, + None => { + return None; + }, + }; + if !element_name_exec(kind_at(nodes, n0)) || lo_n != s { + return None; + } + let ki = kind_at(nodes, k1); + let with_init = !matches!(ki, NodeKind::Absent); + let q: usize = if with_init { + let (lo1, h1, lv1) = match done_at(done, k1) { + Some(r) => r, + None => { + return None; + }, + }; + if lv1 < 0 || lo1 != mn.saturating_add(1) + || !punct_at(src, nodes, toks, gaps, mn, 0x3D, Ghost(GW { g, w: "="@ })) { + return None; + } + h1 + } else { + if !check_absent(src, nodes, toks, gaps, k1, mn, Ghost(g)) { + return None; + } + mn + }; + let j = match find_semi(src, nodes, toks, gaps, q, nd.end, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + if j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(a.nodes[m as int].children =~= seq![n0 as int, k1 as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].flag == nd.flag); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(x.saturating_add(1) as int == x as int + 1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, n0 as int, g.cx, 0); + assert(kd(a, k1 as int) == ki); + if with_init { + assert(mn.saturating_add(1) as int == mn as int + 1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k1 as int, g.cx, 1); + assert(is_assign_expr(a, k1 as int)); + } + lemma_field_done(a, g.ts, g.cx, m as int, n0 as int, k1 as int, x as int, mn as int, q as int, j as int, with_init); + } + Some((x, j, -100)) +} + +/// The one child of node `m` is checked in `c0`. +pub open spec fn cx_kid1(a: AstV, cxs: Seq, m: int, c0: Ctx) -> bool { + 0 <= m < a.nodes.len() && a.nodes[m].children.len() == 1 && 0 <= a.nodes[m].children[0] + < cxs.len() && cx_of(cxs[a.nodes[m].children[0]]) == c0 +} + +/// The one child of node `m` is checked in `c0`. +fn kids_cx1(nodes: &[Node], cxs: &[Cx5], m: usize, c0: &Cx5) -> (r: bool) + requires + m < nodes.len(), + cxs.len() == nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + r ==> cx_kid1(arena_of(nodes@), cxs@, m as int, cx_of(*c0)), +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + if nd.children.len() != 1 { + return false; + } + let a0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return false; + }, + }; + match cxs.get(a0) { + Some(x) => cx_eq(x, c0), + None => false, + } +} + +/// §15.7 `ClassStaticBlock : static { ClassStaticBlockBody }`. +#[verifier::spinoff_prover] +proof fn lemma_static_block_done(a: AstV, ts: Stream, cx: Ctx, m: int, c: int, x: int, j: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::ClassStaticBlock, + a.nodes[m].children =~= seq![c], + is_kw(ts, x, "static"@), + kd(a, c) == NodeKind::FunctionBody, + node_done( + a, + ts, + c, + Ctx { yield_: false, await_: true, in_: true, return_: false, deflt: false }, + x + 1, + j, + ), + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c); + assert(dc( + a, + ts, + m, + c, + Ctx { yield_: false, await_: true, in_: true, return_: false, deflt: false }, + x + 1, + j, + )); + assert(d_func(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §15.7 `ClassStaticBlock`. +#[verifier::spinoff_prover] +fn check_static_block( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + 0 <= m < done.cxs@.len(), + cx_of(done.cxs@[m as int]) == g.cx, + cx_kid1( + arena_of(nodes@), + done.cxs@, + m as int, + Ctx { yield_: false, await_: true, in_: true, return_: false, deflt: false }, + ), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words7(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ClassStaticBlock) || nd.children.len() != 1 { + return None; + } + let c = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let (lo_c, j, _) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + if !word_at(src, nodes, toks, gaps, x, &[0x73, 0x74, 0x61, 0x74, 0x69, 0x63], Ghost(GW { g, w: "static"@ })) + || !matches!(kind_at(nodes, c), NodeKind::FunctionBody) || lo_c != x.saturating_add(1) + || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(a.nodes[m as int].children =~= seq![c as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int); + assert(kd(a, c as int) == NodeKind::FunctionBody); + lemma_static_block_done(a, g.ts, g.cx, m as int, c as int, x as int, j as int); + } + Some((x, j, -100)) +} + +/// A class element before a list (§15.7 `ClassElementList`). +#[verifier::spinoff_prover] +proof fn lemma_dclass_child( + a: AstV, + ts: Stream, + bound: int, + ks: Seq, + k: int, + cx: Ctx, + i: int, + m: int, + j: int, +) + requires + 0 <= k < ks.len(), + ed(a, ts, ks[k], i) == m, + dc(a, ts, bound, ks[k], cx, i, m), + dclass(a, ts, bound, ks, k + 1, cx, m, j), + ensures + dclass(a, ts, bound, ks, k, cx, i, j), +{ +} + +/// A `;` before a list (§15.7 `ClassElement : ;`). +#[verifier::spinoff_prover] +proof fn lemma_dclass_skip(a: AstV, ts: Stream, bound: int, ks: Seq, k: int, cx: Ctx, i: int, j: int) + requires + i < j, + is_p(ts, i, ";"@), + dclass(a, ts, bound, ks, k, cx, i + 1, j), + ensures + dclass(a, ts, bound, ks, k, cx, i, j), +{ +} + +/// §15.7 `ClassBody : { ClassElementList? }`. +#[verifier::spinoff_prover] +proof fn lemma_class_body_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, e: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= x, + x + 1 <= e < tn(ts), + a.nodes[m].kind == NodeKind::ClassBody, + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> kd(a, #[trigger] a.nodes[m].children[k]) + == NodeKind::MethodDefinition || kd(a, a.nodes[m].children[k]) + == NodeKind::FieldDefinition || kd(a, a.nodes[m].children[k]) + == NodeKind::ClassStaticBlock, + dclass(a, ts, m, a.nodes[m].children, 0, cx, x + 1, e), + is_p(ts, x, "{"@), + is_p(ts, e, "}"@), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[e].end, + ensures + node_done(a, ts, m, cx, x, e + 1), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, e + 1)); + assert(d_func(a, ts, m, cx, x, e + 1)); + lemma_ed_of(a, ts, m, x, e + 1); +} + +/// §15.7 `ClassBody`. +#[verifier::spinoff_prover] +fn check_class_body( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_brace_text(); + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ClassBody) { + return None; + } + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if !punct_at(src, nodes, toks, gaps, x, 0x7B, Ghost(GW { g, w: "{"@ })) { + return None; + } + let start = x.saturating_add(1); + let len = nd.children.len(); + // Where the list ends: after the last element and any `;` that follow it. + let mut close: usize = if len == 0 { + start + } else { + let last = match kid_of(nodes, m, len.saturating_sub(1)) { + Some(c) => c, + None => { + return None; + }, + }; + match done_at(done, last) { + Some((_, h, _)) => h, + None => { + return None; + }, + } + }; + let ghost a = arena_of(nodes@); + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == len); + } + if close > toks.len() || close < start { + return None; + } + let ghost close0 = close; + loop + invariant + close <= toks.len(), + close0 <= close, + env_ok(src@, nodes@, toks@, gaps@, g.ts), + cps(";"@) =~= seq![0x3Bu32], + decreases toks.len() - close, + { + if close < toks.len() && punct_at(src, nodes, toks, gaps, close, 0x3B, Ghost(GW { g, w: ";"@ })) { + close = close.saturating_add(1); + } else { + break; + } + } + if close > toks.len() || !punct_at(src, nodes, toks, gaps, close, 0x7D, Ghost(GW { g, w: "}"@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, close.saturating_add(1), Ghost(g)) { + return None; + } + // Backwards: each element ends where the rest starts, or a `;` is skipped. + let mut k = len; + let mut pos = close; + proof { + reveal_with_fuel(dclass, 2); + assert(dclass(a, g.ts, m as int, a.nodes[m as int].children, len as int, g.cx, close as int, close as int)); + } + while k > 0 || pos > start + invariant + m < nodes.len(), + nodes@.len() == nodes.len(), + nd == &nodes@[m as int], + a == arena_of(nodes@), + len == nd.children.len(), + len == a.nodes[m as int].children.len(), + k <= len, + start <= pos, + pos <= close, + close < toks.len(), + cps(";"@) =~= seq![0x3Bu32], + tn(g.ts) == toks.len(), + ast_wf(a), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(a, done.cxs@, m as int, g.cx), + dclass(a, g.ts, m as int, a.nodes[m as int].children, k as int, g.cx, pos as int, close as int), + forall|q: int| + k <= q < len ==> (kd(a, #[trigger] a.nodes[m as int].children[q]) == NodeKind::MethodDefinition + || kd(a, a.nodes[m as int].children[q]) == NodeKind::FieldDefinition + || kd(a, a.nodes[m as int].children[q]) == NodeKind::ClassStaticBlock), + decreases pos, + { + let ghost ks = a.nodes[m as int].children; + let child_ends_here = if k > 0 { + match kid_of(nodes, m, k.saturating_sub(1)) { + Some(c) => match done_at(done, c) { + Some((_, h, _)) => h == pos, + None => false, + }, + None => false, + } + } else { + false + }; + if child_ends_here { + let c = match kid_of(nodes, m, k.saturating_sub(1)) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_c, hi_c, _) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + let _ = hi_c; + let kc = kind_at(nodes, c); + if !matches!( + kc, + NodeKind::MethodDefinition | NodeKind::FieldDefinition | NodeKind::ClassStaticBlock + ) || lo_c < start { + return None; + } + proof { + assert(k >= 1); + assert(k.saturating_sub(1) as int == k as int - 1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, k as int - 1); + assert(kd(a, c as int) == kc); + assert(ed(a, g.ts, c as int, lo_c as int) == hi_c as int); + assert(dc(a, g.ts, m as int, c as int, g.cx, lo_c as int, hi_c as int)); + lemma_dclass_child(a, g.ts, m as int, ks, k as int - 1, g.cx, lo_c as int, hi_c as int, close as int); + } + pos = lo_c; + k = k.saturating_sub(1); + } else { + if pos <= start || pos < 1 { + return None; + } + let semi_before = pos.saturating_sub(1); + if !punct_at(src, nodes, toks, gaps, semi_before, 0x3B, Ghost(GW { g, w: ";"@ })) { + return None; + } + proof { + assert(semi_before as int == pos as int - 1); + lemma_dclass_skip(a, g.ts, m as int, ks, k as int, g.cx, pos as int - 1, close as int); + } + pos = semi_before; + } + } + proof { + assert(k == 0); + assert(pos == start); + assert(start as int == x as int + 1); + assert(forall|q: int| + 0 <= q < len ==> (kd(a, #[trigger] a.nodes[m as int].children[q]) == NodeKind::MethodDefinition + || kd(a, a.nodes[m as int].children[q]) == NodeKind::FieldDefinition + || kd(a, a.nodes[m as int].children[q]) == NodeKind::ClassStaticBlock)); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(close.saturating_add(1) as int == close as int + 1); + lemma_class_body_done(a, g.ts, g.cx, m as int, x as int, close as int); + } + Some((x, close.saturating_add(1), -100)) +} + +/// §15.7 `class Name? ClassHeritage? ClassBody`. +#[verifier::spinoff_prover] +#[verifier::rlimit(150)] +proof fn lemma_class_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + k0: int, + k1: int, + k2: int, + is_expr: bool, + x: int, + mn: int, + h: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= k0 < m, + 0 <= k1 < m, + 0 <= k2 < m, + a.nodes[m].kind == (if is_expr { + NodeKind::ClassExpression + } else { + NodeKind::ClassDeclaration + }), + a.nodes[m].children =~= seq![k0, k1, k2], + is_kw(ts, x, "class"@), + kd(a, k2) == NodeKind::ClassBody, + if kd(a, k0) == NodeKind::Absent { + (is_expr || cx.deflt) && d(a, ts, k0, cx, x + 1, x + 1) && mn == x + 1 && ed(a, ts, k0, x + 1) == mn + } else { + kd(a, k0) == NodeKind::BindingIdentifier && node_done(a, ts, k0, cx, x + 1, mn) + }, + if kd(a, k1) == NodeKind::Absent { + d(a, ts, k1, cx, mn, mn) && ed(a, ts, k1, mn) == mn && h == mn + } else { + is_lhs_expr(a, k1) && is_kw(ts, mn, "extends"@) && node_done(a, ts, k1, cx, mn + 1, h) + }, + node_done(a, ts, k2, cx, h, j), + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == (if is_expr { + 17int + } else { + -100int + }), +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == k0); + assert(a.nodes[m].children[1] == k1); + assert(a.nodes[m].children[2] == k2); + assert(dc(a, ts, m, k0, cx, x + 1, mn)); + let hs = if kd(a, k1) == NodeKind::Absent { + mn + } else { + mn + 1 + }; + assert(dc(a, ts, m, k1, cx, hs, h)); + assert(dc(a, ts, m, k2, cx, h, j)); + assert(d_class(a, ts, m, cx, x, j, is_expr)); + assert(d_func(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// What `check_class` reads: the node and whether it is an expression. +pub struct ClassShape { + /// The node. + pub m: usize, + /// A `ClassExpression` (else a `ClassDeclaration`). + pub is_expr: bool, +} + +/// §15.7 `ClassDeclaration`, `ClassExpression`. +#[verifier::spinoff_prover] +fn check_class( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + sh: &ClassShape, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + sh.m < nodes.len(), + nodes@[sh.m as int].kind == (if sh.is_expr { + NodeKind::ClassExpression + } else { + NodeKind::ClassDeclaration + }), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, sh.m as int), + cx_ok(arena_of(nodes@), done.cxs@, sh.m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + sh.m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), sh.m as int), + None => true, + }, +{ + let m = sh.m; + proof { + lemma_words7(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if nd.children.len() != 3 { + return None; + } + let k0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let k1 = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let k2 = match kid_of(nodes, m, 2) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if !word_at(src, nodes, toks, gaps, x, &[0x63, 0x6C, 0x61, 0x73, 0x73], Ghost(GW { g, w: "class"@ })) { + return None; + } + let x1 = x.saturating_add(1); + let kn = kind_at(nodes, k0); + let mn: usize = if matches!(kn, NodeKind::Absent) { + let deflt = match done.cxs.get(m) { + Some(c) => c.d, + None => { + return None; + }, + }; + if !(sh.is_expr || deflt) || !check_absent(src, nodes, toks, gaps, k0, x1, Ghost(g)) { + return None; + } + x1 + } else { + let (lo_n, hi_n, _) = match done_at(done, k0) { + Some(r) => r, + None => { + return None; + }, + }; + if !matches!(kn, NodeKind::BindingIdentifier) || lo_n != x1 { + return None; + } + hi_n + }; + let kh = kind_at(nodes, k1); + let h: usize = if matches!(kh, NodeKind::Absent) { + if !check_absent(src, nodes, toks, gaps, k1, mn, Ghost(g)) { + return None; + } + mn + } else { + let (lo_h, hi_h, lv_h) = match done_at(done, k1) { + Some(r) => r, + None => { + return None; + }, + }; + if lv_h < 14 || lo_h != mn.saturating_add(1) + || !word_at(src, nodes, toks, gaps, mn, &[0x65, 0x78, 0x74, 0x65, 0x6E, 0x64, 0x73], Ghost(GW { g, w: "extends"@ })) { + return None; + } + hi_h + }; + let (lo_b, j, _) = match done_at(done, k2) { + Some(r) => r, + None => { + return None; + }, + }; + if !matches!(kind_at(nodes, k2), NodeKind::ClassBody) || lo_b != h + || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x1 as int == x as int + 1); + assert(a.nodes[m as int].children =~= seq![k0 as int, k1 as int, k2 as int]) by { + assert(a.nodes[m as int].children.len() == 3); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(kd(a, k0 as int) == kn); + assert(kd(a, k1 as int) == kh); + assert(kd(a, k2 as int) == NodeKind::ClassBody); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k2 as int, g.cx, 2); + if !matches!(kn, NodeKind::Absent) { + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k0 as int, g.cx, 0); + } + if !matches!(kh, NodeKind::Absent) { + assert(mn.saturating_add(1) as int == mn as int + 1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, k1 as int, g.cx, 1); + assert(is_lhs_expr(a, k1 as int)); + } + lemma_class_done(a, g.ts, g.cx, m as int, k0 as int, k1 as int, k2 as int, sh.is_expr, x as int, mn as int, h as int, j as int); + } + let l: i32 = if sh.is_expr { 17 } else { -100 }; + Some((x, j, l)) +} + +// --------------------------------------------------------------------- +// Template literals (§13.2.8) and tagged templates (§13.3.11). +// --------------------------------------------------------------------- + +/// §13.2.8 a `TemplateElement`: one template token. +#[verifier::spinoff_prover] +fn check_template_element( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::TemplateElement) || !nd.children.is_empty() { + return None; + } + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let t = match toks.get(x) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int); + assert(t == &toks@[x as int]); + } + if !matches!(t.kind, TokKind::Template(_)) || t.end != nd.end { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].children.len() == 0); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(g.ts.toks[x as int].end == t.end as int); + assert(g.ts.toks[x as int].kind == t.kind); + assert(g.ts.toks[x as int].start < g.ts.toks[x as int].end); + assert(span_ok(a, g.ts, m as int, x as int, x as int + 1)); + assert(d_expr_lhs(a, g.ts, m as int, g.cx, x as int, x as int + 1)); + lemma_ed_of(a, g.ts, m as int, x as int, x as int + 1); + } + Some((x, x.saturating_add(1), -100)) +} + +/// The last component of a template. +#[verifier::spinoff_prover] +proof fn lemma_dtemplate_last(a: AstV, ts: Stream, bound: int, ks: Seq, k: int, cx: Ctx, i: int, j: int) + requires + 0 <= k, + k + 1 == ks.len(), + j == i + 1, + kd(a, ks[k]) == NodeKind::TemplateElement, + dc(a, ts, bound, ks[k], cx, i, j), + if k == 0 { + is_kind(ts, i, TokKind::Template(TemplateKind::NoSubstitution)) + } else { + is_kind(ts, i, TokKind::Template(TemplateKind::Tail)) + }, + ensures + dtemplate(a, ts, bound, ks, k, cx, i, j), +{ +} + +/// A component before an expression and the rest of a template. +#[verifier::spinoff_prover] +proof fn lemma_dtemplate_step( + a: AstV, + ts: Stream, + bound: int, + ks: Seq, + k: int, + cx: Ctx, + i: int, + e: int, + j: int, +) + requires + 0 <= k, + k + 1 < ks.len(), + kd(a, ks[k]) == NodeKind::TemplateElement, + dc(a, ts, bound, ks[k], cx, i, i + 1), + if k == 0 { + is_kind(ts, i, TokKind::Template(TemplateKind::Head)) + } else { + is_kind(ts, i, TokKind::Template(TemplateKind::Middle)) + }, + lvl(a, ks[k + 1]) >= -1, + dc(a, ts, bound, ks[k + 1], cx, i + 1, e), + ed(a, ts, ks[k + 1], i + 1) == e, + dtemplate(a, ts, bound, ks, k + 2, cx, e, j), + ensures + dtemplate(a, ts, bound, ks, k, cx, i, j), +{ +} + +/// §13.2.8 `TemplateLiteral`. +#[verifier::spinoff_prover] +proof fn lemma_template_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, j: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + a.nodes[m].kind == NodeKind::TemplateLiteral, + dtemplate(a, ts, m, a.nodes[m].children, 0, cx_in(cx, true), x, j), + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == 17, +{ + assert(span_ok(a, ts, m, x, j)); + assert(d_expr_lhs(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.2.8 `TemplateLiteral`: elements and substitutions alternate. +#[verifier::spinoff_prover] +fn check_template( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok_t(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + let ghost cxt = cx_in(g.cx, true); + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let len = nd.children.len(); + if !matches!(nd.kind, NodeKind::TemplateLiteral) || len == 0 || len % 2 == 0 { + return None; + } + let ghost a = arena_of(nodes@); + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == len); + } + let last_ix = len.saturating_sub(1); + let last = match kid_of(nodes, m, last_ix) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_l, hi_l, _) = match done_at(done, last) { + Some(r) => r, + None => { + return None; + }, + }; + let kl = kind_at(nodes, last); + let tk = match toks.get(lo_l) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_l as int); + assert(tk == &toks@[lo_l as int]); + } + let last_kind_ok = if last_ix == 0 { + matches!(tk.kind, TokKind::Template(TemplateKind::NoSubstitution)) + } else { + matches!(tk.kind, TokKind::Template(TemplateKind::Tail)) + }; + if !matches!(kl, NodeKind::TemplateElement) || !last_kind_ok || hi_l != lo_l.saturating_add(1) { + return None; + } + proof { + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, last as int, cxt, last_ix as int); + assert(lo_l.saturating_add(1) as int == lo_l as int + 1); + assert(kd(a, last as int) == kl); + assert(lo_l < toks.len()); + let ks = a.nodes[m as int].children; + assert(dc(a, g.ts, m as int, last as int, cxt, lo_l as int, hi_l as int)); + assert(g.ts.toks[lo_l as int].kind == tk.kind); + lemma_dtemplate_last(a, g.ts, m as int, ks, last_ix as int, cxt, lo_l as int, hi_l as int); + } + let end = hi_l; + let mut k = last_ix; + let mut pos = lo_l; + while k > 0 + invariant + m < nodes.len(), + nodes@.len() == nodes.len(), + nd == &nodes@[m as int], + a == arena_of(nodes@), + len == nd.children.len(), + len == a.nodes[m as int].children.len(), + len % 2 == 1, + k < len, + k % 2 == 0, + end <= toks.len(), + pos < end, + tn(g.ts) == toks.len(), + ast_wf(a), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + kids_ok(a, done.cxs@, m as int, cxt), + dtemplate(a, g.ts, m as int, a.nodes[m as int].children, k as int, cxt, pos as int, end as int), + decreases k, + { + let ix_e = k.saturating_sub(2); + let ix_x = k.saturating_sub(1); + let el = match kid_of(nodes, m, ix_e) { + Some(c) => c, + None => { + return None; + }, + }; + let ex = match kid_of(nodes, m, ix_x) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_e, hi_e, _) = match done_at(done, el) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo_x, hi_x, lv_x) = match done_at(done, ex) { + Some(r) => r, + None => { + return None; + }, + }; + let tt = match toks.get(lo_e) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_e as int); + assert(tt == &toks@[lo_e as int]); + } + let el_kind_ok = if ix_e == 0 { + matches!(tt.kind, TokKind::Template(TemplateKind::Head)) + } else { + matches!(tt.kind, TokKind::Template(TemplateKind::Middle)) + }; + if !matches!(kind_at(nodes, el), NodeKind::TemplateElement) || !el_kind_ok + || hi_e != lo_e.saturating_add(1) || lo_x != hi_e || hi_x != pos || lv_x < -1 { + return None; + } + proof { + assert(k >= 2); + assert(ix_e as int == k as int - 2); + assert(ix_x as int == k as int - 1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, el as int, cxt, k as int - 2); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, ex as int, cxt, k as int - 1); + assert(lo_e.saturating_add(1) as int == lo_e as int + 1); + assert(kd(a, el as int) == NodeKind::TemplateElement); + assert(lvl(a, ex as int) >= -1); + assert(lo_e < toks.len()); + let ks = a.nodes[m as int].children; + assert(g.ts.toks[lo_e as int].kind == tt.kind); + assert(dc(a, g.ts, m as int, el as int, cxt, lo_e as int, lo_e as int + 1)); + assert(dc(a, g.ts, m as int, ex as int, cxt, lo_e as int + 1, pos as int)); + assert(ed(a, g.ts, ex as int, lo_e as int + 1) == pos as int); + lemma_dtemplate_step(a, g.ts, m as int, ks, k as int - 2, cxt, lo_e as int, pos as int, end as int); + } + pos = lo_e; + k = k.saturating_sub(2); + } + let x = pos; + if !end_ok(src, nodes, toks, gaps, nd.end, end, Ghost(g)) { + return None; + } + let first = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if first != x || end <= x { + return None; + } + proof { + assert(k == 0); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_template_done(a, g.ts, g.cx, m as int, x as int, end as int); + } + Some((x, end, 17)) +} + +/// §13.3.11 `tag TemplateLiteral`. +#[verifier::spinoff_prover] +proof fn lemma_tagged_done(a: AstV, ts: Stream, cx: Ctx, m: int, t: int, l: int, x: int, mt: int, j: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= t < m, + 0 <= l < m, + a.nodes[m].kind == NodeKind::TaggedTemplate, + a.nodes[m].children =~= seq![t, l], + is_member_or_call(a, t), + kd(a, l) == NodeKind::TemplateLiteral, + node_done(a, ts, t, cx, x, mt), + node_done(a, ts, l, cx, mt, j), + 0 <= x < j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == (if lvl(a, t) == 17 { + 17int + } else { + 15int + }), +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == t); + assert(a.nodes[m].children[1] == l); + assert(dc(a, ts, m, t, cx, x, mt)); + assert(dc(a, ts, m, l, cx, mt, j)); + assert(d_expr_lhs(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.3.11 `TaggedTemplate`. +#[verifier::spinoff_prover] +fn check_tagged( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::TaggedTemplate) || nd.children.len() != 2 { + return None; + } + let t = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let l = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let (lo_t, mt, lv_t) = match done_at(done, t) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo_l, j, _) = match done_at(done, l) { + Some(r) => r, + None => { + return None; + }, + }; + if lo_t != x || !(lv_t == 17 || lv_t == 15) || lo_l != mt + || !matches!(kind_at(nodes, l), NodeKind::TemplateLiteral) + || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(a.nodes[m as int].children =~= seq![t as int, l as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, t as int, g.cx, 0); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, l as int, g.cx, 1); + assert(kd(a, l as int) == NodeKind::TemplateLiteral); + assert(is_member_or_call(a, t as int)); + lemma_tagged_done(a, g.ts, g.cx, m as int, t as int, l as int, x as int, mt as int, j as int); + } + let own: i32 = if lv_t == 17 { 17 } else { 15 }; + Some((x, j, own)) +} + +// --------------------------------------------------------------------- +// `object.#name` (§13.3.2). +// --------------------------------------------------------------------- + +/// §13.3.2 `MemberExpression . PrivateIdentifier`. +#[verifier::spinoff_prover] +#[verifier::rlimit(100)] +proof fn lemma_member_private_done(a: AstV, ts: Stream, cx: Ctx, m: int, o: int, p: int, x: int, mo: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= o < m, + 0 <= p < m, + a.nodes[m].kind == NodeKind::MemberExpression, + a.nodes[m].op == Op::PrivateDot, + a.nodes[m].children =~= seq![o, p], + is_member_or_call(a, o), + kd(a, p) == NodeKind::PrivateIdentifier, + node_done(a, ts, o, cx, x, mo), + is_p(ts, mo, "."@), + node_done(a, ts, p, cx, mo + 1, mo + 2), + 0 <= x, + mo + 2 <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[mo + 1].end, + ensures + node_done(a, ts, m, cx, x, mo + 2), + lvl(a, m) == (if lvl(a, o) == 17 { + 17int + } else { + 15int + }), +{ + assert(span_ok(a, ts, m, x, mo + 2)); + assert(a.nodes[m].children[0] == o); + assert(a.nodes[m].children[1] == p); + assert(dc(a, ts, m, o, cx, x, mo)); + assert(dc(a, ts, m, p, cx, mo + 1, mo + 2)); + assert(d_expr_lhs(a, ts, m, cx, x, mo + 2)); + lemma_ed_of(a, ts, m, x, mo + 2); +} + +/// §13.3.2 `object . #name`. +#[verifier::spinoff_prover] +fn check_member_private( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::MemberExpression) || !matches!(nd.op, Op::PrivateDot) + || nd.children.len() != 2 { + return None; + } + let o = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let p = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let (lo_o, mo, lv_o) = match done_at(done, o) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo_p, hi_p, _) = match done_at(done, p) { + Some(r) => r, + None => { + return None; + }, + }; + let mo1 = mo.saturating_add(1); + if lo_o != x || !(lv_o == 17 || lv_o == 15) || !matches!(kind_at(nodes, p), NodeKind::PrivateIdentifier) + || lo_p != mo1 || hi_p != mo.saturating_add(2) + || !punct_at(src, nodes, toks, gaps, mo, 0x2E, Ghost(GW { g, w: "."@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, hi_p, Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(mo1 as int == mo as int + 1); + assert(a.nodes[m as int].children =~= seq![o as int, p as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].op == nd.op); + assert(a.nodes[m as int].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, o as int, g.cx, 0); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, p as int, g.cx, 1); + assert(kd(a, p as int) == NodeKind::PrivateIdentifier); + assert(is_member_or_call(a, o as int)); + assert(hi_p as int == mo as int + 2); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hi_p as int - 1); + assert(g.ts.toks[mo as int + 1].end == nd.end as int); + lemma_member_private_done(a, g.ts, g.cx, m as int, o as int, p as int, x as int, mo as int); + } + let own: i32 = if lv_o == 17 { 17 } else { 15 }; + Some((x, hi_p, own)) +} + } // verus! diff --git a/crates/clazel-js-syntax/src/arena_decls.rs b/crates/clazel-js-syntax/src/arena_decls.rs new file mode 100644 index 000000000..e57550717 --- /dev/null +++ b/crates/clazel-js-syntax/src/arena_decls.rs @@ -0,0 +1,1707 @@ +//! The declared names of every node, computed over the arena and proven equal to the +//! specification's (item 7.3): LexicallyDeclaredNames and TopLevelLexicallyDeclaredNames +//! (`spec::lex_names_item`), VarDeclaredNames and TopLevelVarDeclaredNames +//! (`spec::var_names_item`), each as a list of [`Decl`]s (the token that names the +//! declaration, and whether it is a plain function declaration), bottom-up +//! ([`decl_names_all`]), and the lists of a statement list put together ([`cat_exec`]). +//! +//! The `import` and `export` declarations of a module are not covered: the computation +//! returns `None` for them. + +use crate::arena_d::*; +use crate::arena_early::*; +use crate::arena_names::*; +use crate::ast::*; +use crate::spec::*; +use vstd::prelude::*; + +verus! { + +/// A declared name: the token that names it, and whether the declaration is a +/// `FunctionDeclaration` that is not a generator or an async function. +#[derive(Clone, Copy)] +pub struct Decl { + /// The token. + pub x: usize, + /// A plain function declaration. + pub pf: bool, +} + +/// The specification's declared names of `ds`. +pub open spec fn decls_of(toks: Seq, ds: Seq) -> Seq { + Seq::new(ds.len(), |i: int| DeclName { name: toks[ds[i].x as int].name@, plain_function: ds[i].pf }) +} + +/// The names of `ds`. +pub open spec fn dn(toks: Seq, ds: Seq) -> Seq> { + Seq::new(ds.len(), |i: int| toks[ds[i].x as int].name@) +} + +/// Every declaration of `ds` names a token. +pub open spec fn decl_ok(toks: Seq, ds: Seq) -> bool { + forall|i: int| 0 <= i < ds.len() ==> (#[trigger] ds[i]).x < toks.len() +} + +/// `plain_names` of the declared names. +proof fn lemma_dn_plain(toks: Seq, ds: Seq) + ensures + plain_names(decls_of(toks, ds)) =~= dn(toks, ds), +{ +} + +/// The declared names of two lists put together. +pub proof fn lemma_decls_add(toks: Seq, xs: Seq, ys: Seq) + ensures + decls_of(toks, xs + ys) =~= decls_of(toks, xs) + decls_of(toks, ys), + dn(toks, xs + ys) =~= dn(toks, xs) + dn(toks, ys), +{ +} + +/// The declarations the names `xs` make, none of them a plain function declaration. +pub open spec fn decls_of_names(xs: Seq) -> Seq { + Seq::new(xs.len(), |i: int| Decl { x: xs[i], pf: false }) +} + +/// `dst` gets the elements of `src` after its own. +fn push_decls(dst: &mut Vec, src: &[Decl]) + ensures + final(dst)@ =~= old(dst)@ + src@, +{ + let len = src.len(); + let mut i: usize = 0; + while i < len + invariant + len == src.len(), + i <= len, + dst@ =~= old(dst)@ + src@.subrange(0, i as int), + decreases len - i, + { + match src.get(i) { + Some(x) => { + dst.push(*x); + }, + None => { + return; + }, + } + proof { + assert(src@.subrange(0, i as int + 1) =~= src@.subrange(0, i as int).push(src@[i as int])); + } + i = i.saturating_add(1); + } + proof { + assert(src@.subrange(0, len as int) =~= src@); + } +} + +/// The declarations of the names `xs`. +pub fn decls_from_names(xs: &[usize]) -> (r: Vec) + ensures + r@ =~= decls_of_names(xs@), +{ + let mut out: Vec = Vec::new(); + let mut i: usize = 0; + while i < xs.len() + invariant + i <= xs.len(), + out@ =~= decls_of_names(xs@).subrange(0, i as int), + decreases xs.len() - i, + { + match xs.get(i) { + Some(x) => { + out.push(Decl { x: *x, pf: false }); + }, + None => { + return out; + }, + } + proof { + assert(decls_of_names(xs@).subrange(0, i as int + 1) =~= decls_of_names(xs@).subrange( + 0, + i as int, + ).push(decls_of_names(xs@)[i as int])); + } + i = i.saturating_add(1); + } + proof { + assert(decls_of_names(xs@).subrange(0, xs@.len() as int) =~= decls_of_names(xs@)); + } + out +} + +/// The declarations of the children `ks[k..]`, put together, from the declarations `arr` of each node. +pub open spec fn cat_d(arr: Seq>, ks: Seq, k: int) -> Seq + decreases ks.len() - k, +{ + if k < 0 || k >= ks.len() { + Seq::empty() + } else { + arr[ks[k]]@ + cat_d(arr, ks, k + 1) + } +} + +/// The declarations `arr[j]` name tokens, for `j < m`. +pub open spec fn arr_ok(toks: Seq, arr: Seq>, m: int) -> bool { + forall|j: int| 0 <= j < m ==> decl_ok(toks, (#[trigger] arr[j])@) +} + +/// A concatenation of declarations that name tokens does. +proof fn lemma_cat_ok(toks: Seq, arr: Seq>, ks: Seq, k: int, m: int) + requires + arr_ok(toks, arr, m), + forall|q: int| 0 <= q < ks.len() ==> 0 <= #[trigger] ks[q] < m, + ensures + decl_ok(toks, cat_d(arr, ks, k)), + decreases ks.len() - k, +{ + if 0 <= k < ks.len() { + lemma_cat_ok(toks, arr, ks, k + 1, m); + let r = cat_d(arr, ks, k + 1); + let l = arr[ks[k]]@; + assert(decl_ok(toks, l)); + assert forall|i: int| 0 <= i < (l + r).len() implies (#[trigger] (l + r)[i]).x < toks.len() by { + if i < l.len() { + assert((l + r)[i] == l[i]); + } else { + assert((l + r)[i] == r[i - l.len()]); + } + } + } +} + +/// The declarations of the children of node `m` from `from` on, put together. +pub fn cat_exec(nodes: &[Node], arr: &[Vec], m: usize, from: usize) -> (r: Option>) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + arr.len() >= m, + ensures + match r { + Some(v) => v@ =~= cat_d(arr@, arena_of(nodes@).nodes[m as int].children, from as int), + None => true, + }, +{ + let ghost a = arena_of(nodes@); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let len = nd.children.len(); + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == len); + } + let ghost ks = a.nodes[m as int].children; + let mut acc: Vec = Vec::new(); + if from >= len { + return Some(acc); + } + let mut k = len; + while k > from + invariant + from <= k <= len, + len == nd.children.len(), + len == ks.len(), + m < nodes.len(), + arr.len() >= m, + nd == &nodes@[m as int], + a == arena_of(nodes@), + ast_wf(a), + ks == a.nodes[m as int].children, + acc@ =~= cat_d(arr@, ks, k as int), + decreases k, + { + k = k.saturating_sub(1); + let c = match nd.children.as_slice().get(k) { + Some(c) => *c as usize, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int].children[k as int] == c as int); + lemma_child_before(a, m as int, k as int); + } + let item = match arr.get(c) { + Some(v) => v, + None => { + return None; + }, + }; + let mut next: Vec = Vec::new(); + push_decls(&mut next, item.as_slice()); + push_decls(&mut next, acc.as_slice()); + proof { + assert(arr@[c as int] == item); + assert(next@ =~= cat_d(arr@, ks, k as int)); + } + acc = next; + } + Some(acc) +} + +// --------------------------------------------------------------------- +// The names of every node. +// --------------------------------------------------------------------- + +/// `arr[j]` is the lexically declared names of node `j` (`top`: top-level), for `j < m`. +pub open spec fn inv_l(a: AstV, ts: Stream, toks: Seq, arr: Seq>, top: bool, m: int) -> bool { + forall|j: int| + 0 <= j < m ==> { + let v = (#[trigger] arr[j])@; + decl_ok(toks, v) && decls_of(toks, v) =~= lex_names_item(a, ts, j, top) + } +} + +/// `arr[j]` is the var declared names of node `j` (`top`: top-level), for `j < m`. +pub open spec fn inv_v(a: AstV, ts: Stream, toks: Seq, arr: Seq>, top: bool, m: int) -> bool { + forall|j: int| + 0 <= j < m ==> { + let v = (#[trigger] arr[j])@; + decl_ok(toks, v) && dn(toks, v) =~= var_names_item(a, ts, j, top) + } +} + +/// The declarations of child `i` of a node, none if there is no such child. +pub open spec fn kid_spec(arr: Seq>, ks: Seq, i: int) -> Seq { + if 0 <= i < ks.len() { + arr[ks[i]]@ + } else { + Seq::empty() + } +} + +/// The declarations of child `i` of node `m`. +fn kid_d(nodes: &[Node], arr: &[Vec], m: usize, i: usize) -> (r: Option>) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + arr.len() >= m, + ensures + match r { + Some(v) => v@ =~= kid_spec(arr@, arena_of(nodes@).nodes[m as int].children, i as int), + None => true, + }, +{ + let ghost a = arena_of(nodes@); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == nd.children.len()); + } + let c = match nd.children.as_slice().get(i) { + Some(c) => *c as usize, + None => { + return Some(Vec::new()); + }, + }; + proof { + assert(a.nodes[m as int].children[i as int] == c as int); + lemma_child_before(a, m as int, i as int); + } + let item = match arr.get(c) { + Some(v) => v, + None => { + return None; + }, + }; + let mut out: Vec = Vec::new(); + push_decls(&mut out, item.as_slice()); + proof { + assert(arr@[c as int] == item); + } + Some(out) +} + +/// The declarations of children `i`, `j` and `l` of node `m` put together. +fn sum3(nodes: &[Node], arr: &[Vec], m: usize, i: usize, j: usize, l: usize) -> (r: Option>) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + arr.len() == m, + ensures + match r { + Some(v) => { + let ks = arena_of(nodes@).nodes[m as int].children; + v@ =~= kid_spec(arr@, ks, i as int) + kid_spec(arr@, ks, j as int) + kid_spec( + arr@, + ks, + l as int, + ) + }, + None => true, + }, +{ + let x = match kid_d(nodes, arr, m, i) { + Some(v) => v, + None => { + return None; + }, + }; + let y = match kid_d(nodes, arr, m, j) { + Some(v) => v, + None => { + return None; + }, + }; + let z = match kid_d(nodes, arr, m, l) { + Some(v) => v, + None => { + return None; + }, + }; + let mut out: Vec = Vec::new(); + push_decls(&mut out, x.as_slice()); + push_decls(&mut out, y.as_slice()); + push_decls(&mut out, z.as_slice()); + Some(out) +} + +/// A declaration list that names tokens, of a child. +proof fn lemma_kid_ok(toks: Seq, arr: Seq>, ks: Seq, i: int, m: int) + requires + arr_ok(toks, arr, m), + forall|q: int| 0 <= q < ks.len() ==> 0 <= #[trigger] ks[q] < m, + ensures + decl_ok(toks, kid_spec(arr, ks, i)), +{ +} + +/// Two lists of declarations that name tokens, put together, do. +proof fn lemma_ok_add(toks: Seq, xs: Seq, ys: Seq) + requires + decl_ok(toks, xs), + decl_ok(toks, ys), + ensures + decl_ok(toks, xs + ys), +{ + assert forall|i: int| 0 <= i < (xs + ys).len() implies (#[trigger] (xs + ys)[i]).x < toks.len() by { + if i < xs.len() { + assert((xs + ys)[i] == xs[i]); + } else { + assert((xs + ys)[i] == ys[i - xs.len()]); + } + } +} + +/// The lexically declared names of a list of items are those of its items put together. +proof fn lemma_lex_cat( + a: AstV, + ts: Stream, + toks: Seq, + arr: Seq>, + top: bool, + n: int, + ks: Seq, + k: int, +) + requires + inv_l(a, ts, toks, arr, top, n), + arr.len() >= n, + forall|q: int| 0 <= q < ks.len() ==> 0 <= #[trigger] ks[q] < n, + ensures + decls_of(toks, cat_d(arr, ks, k)) =~= lex_names_list(a, ts, n, ks, k, top), + decreases ks.len() - k, +{ + if 0 <= k < ks.len() { + lemma_lex_cat(a, ts, toks, arr, top, n, ks, k + 1); + lemma_decls_add(toks, arr[ks[k]]@, cat_d(arr, ks, k + 1)); + assert(decls_of(toks, arr[ks[k]]@) =~= lex_names_item(a, ts, ks[k], top)); + } +} + +/// The var declared names of a list of items are those of its items put together. +proof fn lemma_var_cat( + a: AstV, + ts: Stream, + toks: Seq, + arr: Seq>, + top: bool, + n: int, + ks: Seq, + k: int, +) + requires + inv_v(a, ts, toks, arr, top, n), + arr.len() >= n, + forall|q: int| 0 <= q < ks.len() ==> 0 <= #[trigger] ks[q] < n, + ensures + dn(toks, cat_d(arr, ks, k)) =~= (if top { + top_var_names_list(a, ts, n, ks, k) + } else { + var_names_list(a, ts, n, ks, k) + }), + decreases ks.len() - k, +{ + if 0 <= k < ks.len() { + lemma_var_cat(a, ts, toks, arr, top, n, ks, k + 1); + lemma_decls_add(toks, arr[ks[k]]@, cat_d(arr, ks, k + 1)); + assert(dn(toks, arr[ks[k]]@) =~= var_names_item(a, ts, ks[k], top)); + if top { + assert(top_var_names_list(a, ts, n, ks, k) =~= var_names_sub_top(a, ts, n, ks[k], true) + + top_var_names_list(a, ts, n, ks, k + 1)); + assert(var_names_sub_top(a, ts, n, ks[k], true) =~= var_names_item(a, ts, ks[k], true)); + } else { + assert(var_names_list(a, ts, n, ks, k) =~= var_names_sub(a, ts, n, ks[k]) + var_names_list( + a, + ts, + n, + ks, + k + 1, + )); + assert(var_names_sub(a, ts, n, ks[k]) =~= var_names_item(a, ts, ks[k], false)); + } + } +} + +/// The var declared names of child `i` of node `m`, as a `var_names_sub` (not top-level) or +/// the item itself. +proof fn lemma_kid_var(a: AstV, ts: Stream, toks: Seq, arr: Seq>, top: bool, m: int, i: int) + requires + inv_v(a, ts, toks, arr, top, m), + arr.len() >= m, + forall|q: int| 0 <= q < a.nodes[m].children.len() ==> 0 <= #[trigger] a.nodes[m].children[q] < m, + ensures + dn(toks, kid_spec(arr, a.nodes[m].children, i)) =~= var_names_sub_top( + a, + ts, + m, + kid(a.nodes[m].children, i), + top, + ), +{ +} + +/// The lexically declared names of child `i` of node `m`. +proof fn lemma_kid_lex(a: AstV, ts: Stream, toks: Seq, arr: Seq>, top: bool, m: int, i: int) + requires + inv_l(a, ts, toks, arr, top, m), + arr.len() >= m, + forall|q: int| 0 <= q < a.nodes[m].children.len() ==> 0 <= #[trigger] a.nodes[m].children[q] < m, + ensures + decls_of(toks, kid_spec(arr, a.nodes[m].children, i)) =~= (if 0 <= kid(a.nodes[m].children, i) + < m { + lex_names_item(a, ts, kid(a.nodes[m].children, i), top) + } else { + Seq::empty() + }), +{ +} + +/// The declaration of the name of the `BindingIdentifier` node `c`. +fn decl_of_ident( + tk: &Tk, + nodes: &[Node], + c: usize, + pf: bool, + Ghost(ts): Ghost, +) -> (r: Option) + requires + c < nodes.len(), + ast_wf(arena_of(nodes@)), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + ensures + match r { + Some(d) => d.x < tk.toks@.len() && d.pf == pf && tk.toks@[d.x as int].name@ =~= name_of( + arena_of(nodes@), + ts, + c as int, + ), + None => true, + }, +{ + let toks = tk.toks; + let x = match node_token(nodes, toks, tk.by_start, c) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(dtok_matches(tk.src@, toks@[x as int], ts.toks[x as int])); + lemma_node_tok_at(a, ts, c as int, x as int); + } + Some(Decl { x, pf }) +} + +/// The preconditions the per-node computations share. +pub open spec fn names_pre(tk: &Tk, nodes: Seq, ts: Stream, m: int) -> bool { + let a = arena_of(nodes); + 0 <= m < nodes.len() && ast_wf(a) && toks_match(tk.src@, tk.toks@, ts) && starts_inc(ts) + && tk.bn.len() == nodes.len() && bn_inv(a, ts, tk.toks@, tk.bn@, nodes.len() as int) +} + +/// The children of node `m` are before it. +proof fn lemma_kids_before(a: AstV, m: int) + requires + ast_wf(a), + 0 <= m < a.nodes.len(), + ensures + forall|q: int| 0 <= q < a.nodes[m].children.len() ==> 0 <= #[trigger] a.nodes[m].children[q] < m, +{ +} + +/// The lexically declared names of node `m`, not top-level and top-level. +#[verifier::rlimit(400)] +pub fn item_lex(tk: &Tk, nodes: &[Node], m: usize, lf: &[Vec], Ghost(ts): Ghost) -> (r: Option< + (Vec, Vec), +>) + requires + names_pre(tk, nodes@, ts, m as int), + lf.len() == m, + inv_l(arena_of(nodes@), ts, tk.toks@, lf@, false, m as int), + ensures + match r { + Some((l1, l2)) => { + let a = arena_of(nodes@); + decl_ok(tk.toks@, l1@) && decl_ok(tk.toks@, l2@) && decls_of(tk.toks@, l1@) =~= lex_names_item( + a, + ts, + m as int, + false, + ) && decls_of(tk.toks@, l2@) =~= lex_names_item(a, ts, m as int, true) + }, + None => true, + }, +{ + let ghost a = arena_of(nodes@); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].children.len() == nd.children.len()); + lemma_kids_before(a, m as int); + } + let kind = nd.kind; + match kind { + NodeKind::LexicalDeclaration => { + let names = match tk.bn.get(m) { + Some(v) => v, + None => { + return None; + }, + }; + let l1 = decls_from_names(names.as_slice()); + let l2 = decls_from_names(names.as_slice()); + proof { + assert(tk.bn@[m as int] == names); + assert(decls_of(tk.toks@, l1@) =~= bound_names(a, ts, m as int).map_values( + |n: Seq| DeclName { name: n, plain_function: false }, + )); + assert(decl_ok(tk.toks@, l1@)); + } + Some((l1, l2)) + }, + NodeKind::ClassDeclaration | NodeKind::FunctionDeclaration | NodeKind::GeneratorDeclaration + | NodeKind::AsyncFunctionDeclaration | NodeKind::AsyncGeneratorDeclaration => { + let k0 = match kid_kind(nodes, m, 0) { + Some(k) => k, + None => { + return None; + }, + }; + let is_class = matches!(kind, NodeKind::ClassDeclaration); + if !matches!(k0, NodeKind::BindingIdentifier) { + proof { + assert(decls_of(tk.toks@, Seq::::empty()) =~= Seq::::empty()); + } + return Some((Vec::new(), Vec::new())); + } + let c0 = match nd.children.as_slice().get(ZERO) { + Some(c) => *c as usize, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int].children[0] == c0 as int); + lemma_child_before(a, m as int, 0); + } + let pf = matches!(kind, NodeKind::FunctionDeclaration); + let d = match decl_of_ident(tk, nodes, c0, if is_class { + false + } else { + pf + }, Ghost(ts)) { + Some(d) => d, + None => { + return None; + }, + }; + let l1 = vec![d]; + let l2: Vec = if is_class { + vec![d] + } else { + Vec::new() + }; + proof { + assert(decls_of(tk.toks@, l1@) =~= seq![DeclName { + name: name_of(a, ts, c0 as int), + plain_function: if is_class { + false + } else { + pf + }, + }]); + assert(decl_ok(tk.toks@, l1@)); + assert(decl_ok(tk.toks@, l2@)); + if is_class { + assert(decls_of(tk.toks@, l2@) =~= seq![DeclName { + name: name_of(a, ts, c0 as int), + plain_function: false, + }]); + } else { + assert(decls_of(tk.toks@, l2@) =~= Seq::::empty()); + } + } + Some((l1, l2)) + }, + NodeKind::LabelledStatement => { + let l1 = match kid_d(nodes, lf, m, 1) { + Some(v) => v, + None => { + return None; + }, + }; + proof { + lemma_kid_lex(a, ts, tk.toks@, lf@, false, m as int, 1); + lemma_kid_ok(tk.toks@, lf@, a.nodes[m as int].children, 1, m as int); + assert(decls_of(tk.toks@, Seq::::empty()) =~= Seq::::empty()); + } + Some((l1, Vec::new())) + }, + NodeKind::ImportDeclaration | NodeKind::ExportDeclaration => None, + _ => { + proof { + assert(decls_of(tk.toks@, Seq::::empty()) =~= Seq::::empty()); + } + Some((Vec::new(), Vec::new())) + }, + } +} + +/// `dn` of three children's declarations put together is the three `var_names_sub`. +proof fn lemma_sum3_dn(a: AstV, ts: Stream, toks: Seq, vf: Seq>, m: int, i: int, j: int, l: int) + requires + inv_v(a, ts, toks, vf, false, m), + vf.len() >= m, + 0 <= m < a.nodes.len(), + forall|q: int| 0 <= q < a.nodes[m].children.len() ==> 0 <= #[trigger] a.nodes[m].children[q] < m, + ensures + ({ + let ks = a.nodes[m].children; + dn(toks, kid_spec(vf, ks, i) + kid_spec(vf, ks, j) + kid_spec(vf, ks, l)) =~= var_names_sub( + a, + ts, + m, + kid(ks, i), + ) + var_names_sub(a, ts, m, kid(ks, j)) + var_names_sub(a, ts, m, kid(ks, l)) + }), +{ + let ks = a.nodes[m].children; + lemma_kid_var(a, ts, toks, vf, false, m, i); + lemma_kid_var(a, ts, toks, vf, false, m, j); + lemma_kid_var(a, ts, toks, vf, false, m, l); + lemma_decls_add(toks, kid_spec(vf, ks, i), kid_spec(vf, ks, j)); + lemma_decls_add(toks, kid_spec(vf, ks, i) + kid_spec(vf, ks, j), kid_spec(vf, ks, l)); + assert(var_names_sub_top(a, ts, m, kid(ks, i), false) =~= var_names_sub(a, ts, m, kid(ks, i))); + assert(var_names_sub_top(a, ts, m, kid(ks, j), false) =~= var_names_sub(a, ts, m, kid(ks, j))); + assert(var_names_sub_top(a, ts, m, kid(ks, l), false) =~= var_names_sub(a, ts, m, kid(ks, l))); +} + +/// The var declared names of node `m` that are a sum of up to three children's, the same +/// whether top-level or not. +#[verifier::rlimit(400)] +fn item_var_sum( + nodes: &[Node], + Ghost(toks): Ghost>, + vf: &[Vec], + m: usize, + ix: (usize, usize, usize), + Ghost(ts): Ghost, +) -> (r: Option>) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + vf.len() == m, + inv_v(arena_of(nodes@), ts, toks, vf@, false, m as int), + ensures + match r { + Some(v) => { + let a = arena_of(nodes@); + decl_ok(toks, v@) && dn(toks, v@) =~= var_names_sub( + a, + ts, + m as int, + kid(a.nodes[m as int].children, ix.0 as int), + ) + var_names_sub(a, ts, m as int, kid(a.nodes[m as int].children, ix.1 as int)) + + var_names_sub(a, ts, m as int, kid(a.nodes[m as int].children, ix.2 as int)) + }, + None => true, + }, +{ + let v = match sum3(nodes, vf, m, ix.0, ix.1, ix.2) { + Some(v) => v, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + lemma_kids_before(a, m as int); + lemma_sum3_dn(a, ts, toks, vf@, m as int, ix.0 as int, ix.1 as int, ix.2 as int); + let ks = a.nodes[m as int].children; + lemma_kid_ok(toks, vf@, ks, ix.0 as int, m as int); + lemma_kid_ok(toks, vf@, ks, ix.1 as int, m as int); + lemma_kid_ok(toks, vf@, ks, ix.2 as int, m as int); + lemma_ok_add(toks, kid_spec(vf@, ks, ix.0 as int), kid_spec(vf@, ks, ix.1 as int)); + lemma_ok_add( + toks, + kid_spec(vf@, ks, ix.0 as int) + kid_spec(vf@, ks, ix.1 as int), + kid_spec(vf@, ks, ix.2 as int), + ); + } + Some(v) +} + +/// A kind whose var declared names depend on whether the item is top-level, or that +/// this module does not cover. +pub open spec fn var_special(k: NodeKind) -> bool { + k == NodeKind::LabelledStatement || k == NodeKind::FunctionDeclaration || k + == NodeKind::GeneratorDeclaration || k == NodeKind::AsyncFunctionDeclaration || k + == NodeKind::AsyncGeneratorDeclaration || k == NodeKind::ForInStatement || k + == NodeKind::ForOfStatement || k == NodeKind::ImportDeclaration || k + == NodeKind::ExportDeclaration +} + +/// The var declared names of node `m` of a kind that is not [`var_special`]: the same whether +/// top-level or not. +#[verifier::rlimit(800)] +fn item_var_same(tk: &Tk, nodes: &[Node], m: usize, vf: &[Vec], Ghost(ts): Ghost) -> (r: Option< + Vec, +>) + requires + names_pre(tk, nodes@, ts, m as int), + vf.len() == m, + inv_v(arena_of(nodes@), ts, tk.toks@, vf@, false, m as int), + !var_special(nodes@[m as int].kind), + ensures + match r { + Some(v) => { + let a = arena_of(nodes@); + decl_ok(tk.toks@, v@) && dn(tk.toks@, v@) =~= var_names_item(a, ts, m as int, false) + && dn(tk.toks@, v@) =~= var_names_item(a, ts, m as int, true) + }, + None => true, + }, +{ + let ghost a = arena_of(nodes@); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].children.len() == nd.children.len()); + lemma_kids_before(a, m as int); + assert(dn(tk.toks@, Seq::::empty()) =~= Seq::>::empty()); + } + let toks = Ghost(tk.toks@); + let kind = nd.kind; + let none = usize::MAX; + match kind { + NodeKind::VariableStatement | NodeKind::VariableDeclarationList => { + let names = match tk.bn.get(m) { + Some(v) => v, + None => { + return None; + }, + }; + let v = decls_from_names(names.as_slice()); + proof { + assert(tk.bn@[m as int] == names); + assert(dn(tk.toks@, v@) =~= bound_names(a, ts, m as int)); + assert(decl_ok(tk.toks@, v@)); + } + Some(v) + }, + NodeKind::BlockStatement | NodeKind::SwitchStatement | NodeKind::CaseClause + | NodeKind::DefaultClause => { + let from: usize = if matches!(kind, NodeKind::BlockStatement | NodeKind::DefaultClause) { + 0 + } else { + 1 + }; + let v = match cat_exec(nodes, vf, m, from) { + Some(v) => v, + None => { + return None; + }, + }; + proof { + let ks = a.nodes[m as int].children; + lemma_var_cat(a, ts, tk.toks@, vf@, false, m as int, ks, from as int); + lemma_cat_ok(tk.toks@, vf@, ks, from as int, m as int); + } + Some(v) + }, + NodeKind::IfStatement => item_var_sum(nodes, toks, vf, m, (1, 2, none), Ghost(ts)), + NodeKind::DoWhileStatement | NodeKind::Finally => item_var_sum(nodes, toks, vf, m, (0, none, none), Ghost(ts)), + NodeKind::WhileStatement | NodeKind::WithStatement => item_var_sum(nodes, toks, vf, m, (1, none, none), Ghost(ts)), + NodeKind::ForStatement => item_var_sum(nodes, toks, vf, m, (0, 3, none), Ghost(ts)), + NodeKind::TryStatement => item_var_sum(nodes, toks, vf, m, (0, 1, 2), Ghost(ts)), + NodeKind::Catch => item_var_sum(nodes, toks, vf, m, (1, none, none), Ghost(ts)), + _ => Some(Vec::new()), + } +} + +/// The var declared names of node `m`, not top-level and top-level. +#[verifier::rlimit(800)] +pub fn item_var( + tk: &Tk, + nodes: &[Node], + m: usize, + vf: &[Vec], + Ghost(ts): Ghost, +) -> (r: Option<(Vec, Vec)>) + requires + names_pre(tk, nodes@, ts, m as int), + vf.len() == m, + inv_v(arena_of(nodes@), ts, tk.toks@, vf@, false, m as int), + ensures + match r { + Some((v1, v2)) => { + let a = arena_of(nodes@); + decl_ok(tk.toks@, v1@) && decl_ok(tk.toks@, v2@) && dn(tk.toks@, v1@) =~= var_names_item( + a, + ts, + m as int, + false, + ) && dn(tk.toks@, v2@) =~= var_names_item(a, ts, m as int, true) + }, + None => true, + }, +{ + let ghost a = arena_of(nodes@); + let kind = match nodes.get(m) { + Some(x) => x.kind, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(a.nodes[m as int].kind == kind); + assert(dn(tk.toks@, Seq::::empty()) =~= Seq::>::empty()); + } + match kind { + NodeKind::FunctionDeclaration | NodeKind::GeneratorDeclaration | NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncGeneratorDeclaration => { + let k0 = match kid_kind(nodes, m, 0) { + Some(k) => k, + None => { + return None; + }, + }; + if !matches!(k0, NodeKind::BindingIdentifier) { + return Some((Vec::new(), Vec::new())); + } + let c0 = match nodes.get(m) { + Some(nd) => match nd.children.as_slice().get(ZERO) { + Some(c) => *c as usize, + None => { + return None; + }, + }, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int].children.len() > 0); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_child_before(a, m as int, 0); + } + let d = match decl_of_ident(tk, nodes, c0, false, Ghost(ts)) { + Some(d) => d, + None => { + return None; + }, + }; + proof { + assert(dn(tk.toks@, seq![d]) =~= seq![name_of(a, ts, c0 as int)]); + assert(a.nodes[m as int].children[0] == c0 as int); + } + Some((Vec::new(), vec![d])) + }, + NodeKind::LabelledStatement | NodeKind::ForInStatement | NodeKind::ForOfStatement + | NodeKind::ImportDeclaration | NodeKind::ExportDeclaration => None, + _ => { + let v = match item_var_same(tk, nodes, m, vf, Ghost(ts)) { + Some(v) => v, + None => { + return None; + }, + }; + let mut w: Vec = Vec::new(); + push_decls(&mut w, v.as_slice()); + Some((v, w)) + }, + } +} + +/// The declared names of every node, bottom-up. +pub struct Names { + /// LexicallyDeclaredNames of each node. + pub lf: Vec>, + /// TopLevelLexicallyDeclaredNames. + pub lt: Vec>, + /// VarDeclaredNames. + pub vf: Vec>, + /// TopLevelVarDeclaredNames. + pub vt: Vec>, +} + +/// Every node's four lists of declared names (`None` where a node is not covered). +#[verifier::rlimit(400)] +pub fn decl_names_all(tk: &Tk, nodes: &[Node], Ghost(ts): Ghost) -> (r: Option) + requires + nodes.len() > 0, + ast_wf(arena_of(nodes@)), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + tk.bn.len() == nodes.len(), + bn_inv(arena_of(nodes@), ts, tk.toks@, tk.bn@, nodes.len() as int), + ensures + match r { + Some(nm) => { + let a = arena_of(nodes@); + let n = nodes.len() as int; + nm.lf.len() == nodes.len() && nm.lt.len() == nodes.len() && nm.vf.len() == nodes.len() + && nm.vt.len() == nodes.len() && inv_l(a, ts, tk.toks@, nm.lf@, false, n) && inv_l( + a, + ts, + tk.toks@, + nm.lt@, + true, + n, + ) && inv_v(a, ts, tk.toks@, nm.vf@, false, n) && inv_v(a, ts, tk.toks@, nm.vt@, true, n) + }, + None => true, + }, +{ + let ghost a = arena_of(nodes@); + let n = nodes.len(); + let mut lf: Vec> = Vec::new(); + let mut lt: Vec> = Vec::new(); + let mut vf: Vec> = Vec::new(); + let mut vt: Vec> = Vec::new(); + let mut m: usize = 0; + while m < n + invariant + m <= n, + n == nodes.len(), + lf.len() == m, + lt.len() == m, + vf.len() == m, + vt.len() == m, + a == arena_of(nodes@), + ast_wf(a), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + tk.bn.len() == nodes.len(), + bn_inv(a, ts, tk.toks@, tk.bn@, nodes.len() as int), + inv_l(a, ts, tk.toks@, lf@, false, m as int), + inv_l(a, ts, tk.toks@, lt@, true, m as int), + inv_v(a, ts, tk.toks@, vf@, false, m as int), + inv_v(a, ts, tk.toks@, vt@, true, m as int), + decreases n - m, + { + let (l1, l2) = match item_lex(tk, nodes, m, lf.as_slice(), Ghost(ts)) { + Some(p) => p, + None => { + return None; + }, + }; + let (v1, v2) = match item_var(tk, nodes, m, vf.as_slice(), Ghost(ts)) { + Some(p) => p, + None => { + return None; + }, + }; + let ghost (olf, olt, ovf, ovt) = (lf@, lt@, vf@, vt@); + lf.push(l1); + lt.push(l2); + vf.push(v1); + vt.push(v2); + proof { + assert forall|j: int| 0 <= j < m as int + 1 implies { + let v = (#[trigger] lf@[j])@; + decl_ok(tk.toks@, v) && decls_of(tk.toks@, v) =~= lex_names_item(a, ts, j, false) + } by { + if j < m as int { + assert(lf@[j] == olf[j]); + } + } + assert forall|j: int| 0 <= j < m as int + 1 implies { + let v = (#[trigger] lt@[j])@; + decl_ok(tk.toks@, v) && decls_of(tk.toks@, v) =~= lex_names_item(a, ts, j, true) + } by { + if j < m as int { + assert(lt@[j] == olt[j]); + } + } + assert forall|j: int| 0 <= j < m as int + 1 implies { + let v = (#[trigger] vf@[j])@; + decl_ok(tk.toks@, v) && dn(tk.toks@, v) =~= var_names_item(a, ts, j, false) + } by { + if j < m as int { + assert(vf@[j] == ovf[j]); + } + } + assert forall|j: int| 0 <= j < m as int + 1 implies { + let v = (#[trigger] vt@[j])@; + decl_ok(tk.toks@, v) && dn(tk.toks@, v) =~= var_names_item(a, ts, j, true) + } by { + if j < m as int { + assert(vt@[j] == ovt[j]); + } + } + } + m = m.saturating_add(1); + } + Some(Names { lf, lt, vf, vt }) +} + +// --------------------------------------------------------------------- +// The rules that read the declared names. +// --------------------------------------------------------------------- + +/// `tk.nm` holds the declared names of every node. +pub open spec fn names_inv(tk: &Tk, nodes: Seq, ts: Stream) -> bool { + let a = arena_of(nodes); + let n = nodes.len() as int; + tk.nm.lf.len() == nodes.len() && tk.nm.lt.len() == nodes.len() && tk.nm.vf.len() == nodes.len() + && tk.nm.vt.len() == nodes.len() && inv_l(a, ts, tk.toks@, tk.nm.lf@, false, n) && inv_l( + a, + ts, + tk.toks@, + tk.nm.lt@, + true, + n, + ) && inv_v(a, ts, tk.toks@, tk.nm.vf@, false, n) && inv_v(a, ts, tk.toks@, tk.nm.vt@, true, n) +} + +/// No name of `ds` is declared twice, except by plain function declarations in sloppy mode code. +pub fn forbid_dup_exec(toks: &[DTok], ds: &[Decl], strict: bool) -> (r: bool) + requires + decl_ok(toks@, ds@), + ensures + r ==> !has_forbidden_duplicate(decls_of(toks@, ds@), strict), +{ + let len = ds.len(); + let mut i: usize = 0; + while i < len + invariant + len == ds.len(), + i <= len, + decl_ok(toks@, ds@), + forall|x: int, y: int| + 0 <= x < i && x < y < len ==> !(dn(toks@, ds@)[x] =~= dn(toks@, ds@)[y] && !(!strict + && ds@[x].pf && ds@[y].pf)), + decreases len - i, + { + let di = match ds.get(i) { + Some(d) => *d, + None => { + return false; + }, + }; + let ni = match toks.get(di.x) { + Some(t) => t, + None => { + return false; + }, + }; + let mut j: usize = i.saturating_add(1); + while j < len + invariant + len == ds.len(), + i < len, + i < j, + j <= len, + decl_ok(toks@, ds@), + di == ds@[i as int], + ni == &toks@[di.x as int], + forall|x: int, y: int| + 0 <= x < i && x < y < len ==> !(dn(toks@, ds@)[x] =~= dn(toks@, ds@)[y] && !( + !strict && ds@[x].pf && ds@[y].pf)), + forall|y: int| + i < y < j ==> !(dn(toks@, ds@)[i as int] =~= dn(toks@, ds@)[y] && !(!strict + && ds@[i as int].pf && ds@[y].pf)), + decreases len - j, + { + let dj = match ds.get(j) { + Some(d) => *d, + None => { + return false; + }, + }; + let nj = match toks.get(dj.x) { + Some(t) => t, + None => { + return false; + }, + }; + if seq_eq(ni.name.as_slice(), nj.name.as_slice()) && !(!strict && di.pf && dj.pf) { + return false; + } + j = j.saturating_add(1); + } + i = i.saturating_add(1); + } + proof { + let dd = decls_of(toks@, ds@); + assert forall|x: int, y: int| + 0 <= x < y < dd.len() implies !(#[trigger] decl_name(dd, x) =~= #[trigger] decl_name(dd, y) + && !(!strict && dd[x].plain_function && dd[y].plain_function)) by { + assert(x < len && y < len); + assert(decl_name(dd, x) == dn(toks@, ds@)[x]); + assert(decl_name(dd, y) == dn(toks@, ds@)[y]); + } + } + true +} + +/// No name is in both `xs` and `ys`. +pub fn no_intersect_dd(toks: &[DTok], xs: &[Decl], ys: &[Decl]) -> (r: bool) + requires + decl_ok(toks@, xs@), + decl_ok(toks@, ys@), + ensures + r ==> !intersects(dn(toks@, xs@), dn(toks@, ys@)), +{ + let xl = xs.len(); + let yl = ys.len(); + let mut i: usize = 0; + while i < xl + invariant + xl == xs.len(), + yl == ys.len(), + i <= xl, + decl_ok(toks@, xs@), + decl_ok(toks@, ys@), + forall|x: int, y: int| + 0 <= x < i && 0 <= y < yl ==> !(dn(toks@, xs@)[x] =~= dn(toks@, ys@)[y]), + decreases xl - i, + { + let dx = match xs.get(i) { + Some(d) => *d, + None => { + return false; + }, + }; + let nx = match toks.get(dx.x) { + Some(t) => t, + None => { + return false; + }, + }; + let mut j: usize = 0; + while j < yl + invariant + xl == xs.len(), + yl == ys.len(), + i < xl, + j <= yl, + decl_ok(toks@, xs@), + decl_ok(toks@, ys@), + dx == xs@[i as int], + nx == &toks@[dx.x as int], + forall|x: int, y: int| + 0 <= x < i && 0 <= y < yl ==> !(dn(toks@, xs@)[x] =~= dn(toks@, ys@)[y]), + forall|y: int| 0 <= y < j ==> !(dn(toks@, xs@)[i as int] =~= dn(toks@, ys@)[y]), + decreases yl - j, + { + let dy = match ys.get(j) { + Some(d) => *d, + None => { + return false; + }, + }; + let ny = match toks.get(dy.x) { + Some(t) => t, + None => { + return false; + }, + }; + if seq_eq(nx.name.as_slice(), ny.name.as_slice()) { + return false; + } + j = j.saturating_add(1); + } + i = i.saturating_add(1); + } + proof { + assert forall|x: int, y: int| + 0 <= x < dn(toks@, xs@).len() && 0 <= y < dn(toks@, ys@).len() implies !( + #[trigger] dn(toks@, xs@)[x] =~= #[trigger] dn(toks@, ys@)[y]) by { + assert(x < xl && y < yl); + } + } + true +} + +/// `list_early` of the statement list that is the children of node `m`. +#[verifier::rlimit(400)] +pub fn list_early_exec(tk: &Tk, nodes: &[Node], m: usize, strict: bool, top: bool, Ghost(ts): Ghost) -> (r: bool) + requires + names_pre(tk, nodes@, ts, m as int), + names_inv(tk, nodes@, ts), + ensures + r ==> list_early(arena_of(nodes@), ts, m as int, arena_of(nodes@).nodes[m as int].children, strict, top), +{ + let ghost a = arena_of(nodes@); + let toks = tk.toks; + let lexa: &Vec> = if top { + &tk.nm.lt + } else { + &tk.nm.lf + }; + let vara: &Vec> = if top { + &tk.nm.vt + } else { + &tk.nm.vf + }; + let lex = match cat_exec(nodes, lexa.as_slice(), m, 0) { + Some(v) => v, + None => { + return false; + }, + }; + let vars = match cat_exec(nodes, vara.as_slice(), m, 0) { + Some(v) => v, + None => { + return false; + }, + }; + proof { + lemma_kids_before(a, m as int); + let ks = a.nodes[m as int].children; + let la = if top { + tk.nm.lt@ + } else { + tk.nm.lf@ + }; + let va = if top { + tk.nm.vt@ + } else { + tk.nm.vf@ + }; + assert(arr_ok(toks@, la, m as int)); + assert(arr_ok(toks@, va, m as int)); + lemma_cat_ok(toks@, la, ks, 0, m as int); + lemma_cat_ok(toks@, va, ks, 0, m as int); + assert(lexa@ == la); + assert(vara@ == va); + lemma_lex_cat(a, ts, toks@, la, top, m as int, ks, 0); + lemma_var_cat(a, ts, toks@, va, top, m as int, ks, 0); + } + let ok1 = forbid_dup_exec(toks, lex.as_slice(), strict); + let ok2 = no_intersect_dd(toks, lex.as_slice(), vars.as_slice()); + proof { + lemma_dn_plain(toks@, lex@); + } + ok1 && ok2 +} + +/// The `LexicalDeclaration` node `m` has no `let` binding, no name twice, and an initializer for +/// each `const` (`early_local`, §14.3.1.1). +#[verifier::rlimit(400)] +pub fn lexical_decl_exec(tk: &Tk, nodes: &[Node], m: usize, Ghost(ts): Ghost) -> (r: bool) + requires + names_pre(tk, nodes@, ts, m as int), + arena_of(nodes@).nodes[m as int].kind == NodeKind::LexicalDeclaration, + ensures + r ==> ({ + let a = arena_of(nodes@); + let nd = a.nodes[m as int]; + let bn = bound_names(a, ts, m as int); + !has_name(bn, sv("let"@)) && !has_duplicate(bn) && (nd.op == Op::Const ==> forall|k: int| + 0 <= k < nd.children.len() ==> kd(a, kid(kids(a, #[trigger] nd.children[k]), 1)) + != NodeKind::Absent) + }), +{ + let ghost a = arena_of(nodes@); + let toks = tk.toks; + let names = match tk.bn.get(m) { + Some(v) => v, + None => { + return false; + }, + }; + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + proof { + assert(tk.bn@[m as int] == names); + assert(bound_names(a, ts, m as int) =~= names_of(toks@, names@)); + assert(idx_ok(toks@, names@)); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].op == nd.op); + assert(a.nodes[m as int].children.len() == nd.children.len()); + lemma_words_early(); + } + // No binding named `let`. + let len = names.len(); + let mut i: usize = 0; + while i < len + invariant + len == names.len(), + i <= len, + idx_ok(toks@, names@), + forall|q: int| 0 <= q < i ==> !(names_of(toks@, names@)[q] =~= sv("let"@)), + decreases len - i, + { + let t = match names.get(i) { + Some(x) => match toks.get(*x) { + Some(t) => t, + None => { + return false; + }, + }, + None => { + return false; + }, + }; + proof { + lemma_words_early(); + assert(t == &toks@[names@[i as int] as int]); + } + if seq_eq(t.name.as_slice(), &[0x6C, 0x65, 0x74]) { + return false; + } + i = i.saturating_add(1); + } + if !no_dup_exec(toks, names.as_slice()) { + return false; + } + if matches!(nd.op, Op::Const) { + let cl = nd.children.len(); + let mut k: usize = 0; + while k < cl + invariant + cl == nd.children.len(), + k <= cl, + m < nodes.len(), + nd == &nodes@[m as int], + a == arena_of(nodes@), + ast_wf(a), + cl == a.nodes[m as int].children.len(), + forall|q: int| + 0 <= q < k ==> kd(a, kid(kids(a, #[trigger] a.nodes[m as int].children[q]), 1)) + != NodeKind::Absent, + decreases cl - k, + { + let c = match nd.children.as_slice().get(k) { + Some(c) => *c as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[k as int] == c as int); + lemma_child_before(a, m as int, k as int); + } + match kid_kind(nodes, c, 1) { + Some(NodeKind::Absent) => { + return false; + }, + Some(_) => {}, + None => { + return false; + }, + } + proof { + assert(kids(a, c as int) == a.nodes[c as int].children); + } + k = k.saturating_add(1); + } + } + true +} + +/// A `for ( LexicalDeclaration ...; ...; ...) Statement` node `m` has no bound name that the +/// body var-declares (`early_local`, §14.7.4.1); true for any other head. +#[verifier::rlimit(400)] +pub fn for_lex_exec(tk: &Tk, nodes: &[Node], m: usize, Ghost(ts): Ghost) -> (r: bool) + requires + names_pre(tk, nodes@, ts, m as int), + names_inv(tk, nodes@, ts), + ensures + r ==> ({ + let a = arena_of(nodes@); + let ks = a.nodes[m as int].children; + kd(a, kid(ks, 0)) == NodeKind::LexicalDeclaration ==> !intersects( + bound_names(a, ts, kid(ks, 0)), + var_names_sub(a, ts, m as int, kid(ks, 3)), + ) + }), +{ + let ghost a = arena_of(nodes@); + let toks = tk.toks; + let k0 = match kid_kind(nodes, m, 0) { + Some(k) => k, + None => { + return false; + }, + }; + if !matches!(k0, NodeKind::LexicalDeclaration) { + return true; + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == nd.children.len()); + lemma_kids_before(a, m as int); + } + let (c0, c3) = match (nd.children.as_slice().get(ZERO), nd.children.as_slice().get(3)) { + (Some(x), Some(y)) => (*x as usize, *y as usize), + _ => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[0] == c0 as int); + assert(a.nodes[m as int].children[3] == c3 as int); + lemma_child_before(a, m as int, 0); + lemma_child_before(a, m as int, 3); + } + let names = match tk.bn.get(c0) { + Some(v) => v, + None => { + return false; + }, + }; + let body = match tk.nm.vf.get(c3) { + Some(v) => v, + None => { + return false; + }, + }; + let dd = decls_from_names(names.as_slice()); + proof { + assert(tk.bn@[c0 as int] == names); + assert(tk.nm.vf@[c3 as int] == body); + assert(dn(toks@, dd@) =~= names_of(toks@, names@)); + assert(bound_names(a, ts, c0 as int) =~= dn(toks@, dd@)); + assert(decl_ok(toks@, dd@)); + assert(dn(toks@, body@) =~= var_names_item(a, ts, c3 as int, false)); + assert(var_names_sub(a, ts, m as int, c3 as int) =~= var_names_item(a, ts, c3 as int, false)); + } + no_intersect_dd(toks, dd.as_slice(), body.as_slice()) +} + +/// The declarations of names that are tokens are of tokens. +pub proof fn lemma_decl_ok_names(toks: Seq, xs: Seq) + requires + idx_ok(toks, xs), + ensures + decl_ok(toks, decls_of_names(xs)), +{ +} + +/// The lexically declared names of the top-level items of node `b` (a function body). +pub proof fn lemma_lex_body(a: AstV, ts: Stream, tk: &Tk, nodes: Seq, b: int) + requires + names_inv(tk, nodes, ts), + a == arena_of(nodes), + ast_wf(a), + 0 <= b < nodes.len(), + ensures + decl_ok(tk.toks@, cat_d(tk.nm.lt@, a.nodes[b].children, 0)), + decls_of(tk.toks@, cat_d(tk.nm.lt@, a.nodes[b].children, 0)) =~= lex_names_list( + a, + ts, + a.nodes.len() as int, + a.nodes[b].children, + 0, + true, + ), +{ + let ks = a.nodes[b].children; + let n = nodes.len() as int; + lemma_kids_before(a, b); + assert forall|q: int| 0 <= q < ks.len() implies 0 <= #[trigger] ks[q] < n by { + assert(0 <= ks[q] < b); + } + assert(arr_ok(tk.toks@, tk.nm.lt@, n)); + lemma_cat_ok(tk.toks@, tk.nm.lt@, ks, 0, n); + lemma_lex_cat(a, ts, tk.toks@, tk.nm.lt@, true, n, ks, 0); +} + +/// The lexically and var declared names of the statements of block `b` (not top-level). +proof fn lemma_block_names(a: AstV, ts: Stream, tk: &Tk, nodes: Seq, b: int) + requires + names_inv(tk, nodes, ts), + a == arena_of(nodes), + ast_wf(a), + 0 <= b < nodes.len(), + ensures + decl_ok(tk.toks@, cat_d(tk.nm.lf@, a.nodes[b].children, 0)), + decl_ok(tk.toks@, cat_d(tk.nm.vf@, a.nodes[b].children, 0)), + decls_of(tk.toks@, cat_d(tk.nm.lf@, a.nodes[b].children, 0)) =~= lex_names_list( + a, + ts, + a.nodes.len() as int, + a.nodes[b].children, + 0, + false, + ), + dn(tk.toks@, cat_d(tk.nm.vf@, a.nodes[b].children, 0)) =~= var_names_list( + a, + ts, + a.nodes.len() as int, + a.nodes[b].children, + 0, + ), +{ + let ks = a.nodes[b].children; + let n = nodes.len() as int; + lemma_kids_before(a, b); + assert forall|q: int| 0 <= q < ks.len() implies 0 <= #[trigger] ks[q] < n by { + assert(0 <= ks[q] < b); + } + assert(arr_ok(tk.toks@, tk.nm.lf@, n)); + assert(arr_ok(tk.toks@, tk.nm.vf@, n)); + lemma_cat_ok(tk.toks@, tk.nm.lf@, ks, 0, n); + lemma_cat_ok(tk.toks@, tk.nm.vf@, ks, 0, n); + lemma_lex_cat(a, ts, tk.toks@, tk.nm.lf@, false, n, ks, 0); + lemma_var_cat(a, ts, tk.toks@, tk.nm.vf@, false, n, ks, 0); +} + +/// The `Catch` node `m`: its parameter has no name twice, no name that its block declares lexically, +/// and, unless a plain identifier, none that it var-declares (`early_local`, §14.15.1, Annex B.3.4). +#[verifier::rlimit(400)] +pub fn catch_exec(tk: &Tk, nodes: &[Node], m: usize, Ghost(ts): Ghost) -> (r: bool) + requires + names_pre(tk, nodes@, ts, m as int), + names_inv(tk, nodes@, ts), + ensures + r ==> ({ + let a = arena_of(nodes@); + let ks = a.nodes[m as int].children; + let bn = bound_names(a, ts, kid(ks, 0)); + let bk = kids(a, kid(ks, 1)); + let nb = a.nodes.len() as int; + !has_duplicate(bn) && !intersects(bn, plain_names(lex_names_list(a, ts, nb, bk, 0, false))) + && (kd(a, kid(ks, 0)) != NodeKind::BindingIdentifier ==> !intersects( + bn, + var_names_list(a, ts, nb, bk, 0), + )) + }), +{ + let ghost a = arena_of(nodes@); + let toks = tk.toks; + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == nd.children.len()); + lemma_kids_before(a, m as int); + } + let (c0, c1) = match (nd.children.as_slice().get(ZERO), nd.children.as_slice().get(1)) { + (Some(x), Some(y)) => (*x as usize, *y as usize), + _ => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[0] == c0 as int); + assert(a.nodes[m as int].children[1] == c1 as int); + lemma_child_before(a, m as int, 0); + lemma_child_before(a, m as int, 1); + } + let k0 = match kid_kind(nodes, m, 0) { + Some(k) => k, + None => { + return false; + }, + }; + let names = match tk.bn.get(c0) { + Some(v) => v, + None => { + return false; + }, + }; + let lex = match cat_exec(nodes, tk.nm.lf.as_slice(), c1, 0) { + Some(v) => v, + None => { + return false; + }, + }; + let vars = match cat_exec(nodes, tk.nm.vf.as_slice(), c1, 0) { + Some(v) => v, + None => { + return false; + }, + }; + let dd = decls_from_names(names.as_slice()); + proof { + assert(tk.bn@[c0 as int] == names); + lemma_decl_ok_names(toks@, names@); + lemma_block_names(a, ts, tk, nodes@, c1 as int); + lemma_dn_plain(toks@, lex@); + assert(bound_names(a, ts, c0 as int) =~= dn(toks@, dd@)); + assert(kids(a, c1 as int) == a.nodes[c1 as int].children); + } + if !no_dup_exec(toks, names.as_slice()) { + return false; + } + if !no_intersect_dd(toks, dd.as_slice(), lex.as_slice()) { + return false; + } + let ok_vars = no_intersect_dd(toks, dd.as_slice(), vars.as_slice()); + if !matches!(k0, NodeKind::BindingIdentifier) && !ok_vars { + return false; + } + true +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/arena_e.rs b/crates/clazel-js-syntax/src/arena_e.rs new file mode 100644 index 000000000..b45bfb640 --- /dev/null +++ b/crates/clazel-js-syntax/src/arena_e.rs @@ -0,0 +1,1129 @@ +//! More checks of [`spec::d`] for [`crate::arena_d::check_derivation`]: the +//! constructs of the script grammar that [`crate::arena_d`] did not cover when +//! it was written. Each `check_*` here is dispatched from `check_derivation` +//! and has the same contract as its siblings there: it reads what the parser +//! built (the node, its children's recorded ranges) and the executable tokens, +//! and returns the node's token range and binding strength only if +//! `node_done` (the node derives that range in its context, and ends there) +//! follows from the specification. +//! +//! **Covered here:** `new.target`, `import.meta` (§13.3.12), `super.name`, +//! `super[expr]` and `super(args)` (§13.3.7), `import(spec, options)` +//! (§13.3.10), and optional chains (§13.3.9: `a?.b`, `a?.[b]`, `a?.(c)`, +//! `a?.#p`, `a?.b.c(d)`). +//! +//! The assumptions are those of [`crate::arena_d`]. + +use crate::arena_d::*; +use crate::ast::*; +use crate::spec::*; +use vstd::prelude::*; + +verus! { + +/// The words and punctuators of these checks, as code points. +pub(crate) proof fn lemma_words_e() + ensures + cps("super"@) =~= seq![0x73u32, 0x75, 0x70, 0x65, 0x72], + cps("import"@) =~= seq![0x69u32, 0x6D, 0x70, 0x6F, 0x72, 0x74], + cps("meta"@) =~= seq![0x6Du32, 0x65, 0x74, 0x61], + cps("new"@) =~= seq![0x6Eu32, 0x65, 0x77], + cps("target"@) =~= seq![0x74u32, 0x61, 0x72, 0x67, 0x65, 0x74], + cps("?."@) =~= seq![0x3Fu32, 0x2E], + cps(","@) =~= seq![0x2Cu32], +{ + reveal_strlit("super"); + reveal_strlit("import"); + reveal_strlit("meta"); + reveal_strlit("new"); + reveal_strlit("target"); + reveal_strlit("?."); + reveal_strlit(","); + assert("super"@ =~= seq!['s', 'u', 'p', 'e', 'r']); + assert("import"@ =~= seq!['i', 'm', 'p', 'o', 'r', 't']); + assert("meta"@ =~= seq!['m', 'e', 't', 'a']); + assert("new"@ =~= seq!['n', 'e', 'w']); + assert("target"@ =~= seq!['t', 'a', 'r', 'g', 'e', 't']); + assert("?."@ =~= seq!['?', '.']); + assert(","@ =~= seq![',']); +} + +/// Token `x` is the punctuator `gw.w`, whose code points are `w`. +pub(crate) fn punct_n_at( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + x: usize, + w: &[u32], + Ghost(gw): Ghost, +) -> (r: bool) + requires + env_ok(src@, nodes@, toks@, gaps@, gw.g.ts), + cps(gw.w) =~= w@, + ensures + r ==> x < toks.len() && is_p(gw.g.ts, x as int, gw.w), +{ + let _ = (nodes.len(), gaps.len()); + let ghost g = gw.g; + let ghost lit = gw.w; + let t = match toks.get(x) { + Some(t) => t, + None => { + return false; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(t == &toks@[x as int]); + } + let r = matches!(t.kind, TokKind::Punctuator) && text_is(src, t, w); + proof { + if r { + assert(g.ts.toks[x as int].text =~= src@.subrange(t.start as int, t.end as int)); + assert(g.ts.toks[x as int].text =~= cps(lit)); + } + } + r +} + +/// The facts the checks read of node `m` of the arena of `nodes`. +pub(crate) proof fn lemma_node_view(nodes: Seq, m: int) + requires + 0 <= m < nodes.len(), + ensures + arena_of(nodes).nodes.len() == nodes.len(), + arena_of(nodes).nodes[m] == nodes[m]@, + arena_of(nodes).nodes[m].kind == nodes[m].kind, + arena_of(nodes).nodes[m].op == nodes[m].op, + arena_of(nodes).nodes[m].flag == nodes[m].flag, + arena_of(nodes).nodes[m].start == nodes[m].start as int, + arena_of(nodes).nodes[m].end == nodes[m].end as int, + arena_of(nodes).nodes[m].children.len() == nodes[m].children@.len(), +{ +} + +// --------------------------------------------------------------------- +// §13.3.12 `new.target`, `import.meta`. +// --------------------------------------------------------------------- + +/// `NewTarget : new . target` and `ImportMeta : import . meta`. +#[verifier::spinoff_prover] +proof fn lemma_meta_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= x, + x + 3 <= tn(ts), + a.nodes[m].children.len() == 0, + a.nodes[m].kind == NodeKind::NewTarget && is_kw(ts, x, "new"@) && is_p(ts, x + 1, "."@) + && is_kw(ts, x + 2, "target"@) || a.nodes[m].kind == NodeKind::ImportMeta && is_kw( + ts, + x, + "import"@, + ) && is_p(ts, x + 1, "."@) && is_kw(ts, x + 2, "meta"@), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[x + 2].end, + ensures + node_done(a, ts, m, cx, x, x + 3), + lvl(a, m) == 17, +{ + assert(span_ok(a, ts, m, x, x + 3)); + assert(d_expr_lhs(a, ts, m, cx, x, x + 3)); + assert(d_expr(a, ts, m, cx, x, x + 3)); + lemma_ed_of(a, ts, m, x, x + 3); +} + +/// §13.3.12 `new . target`, `import . meta`. +#[verifier::spinoff_prover] +pub(crate) fn check_meta( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_e(); + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let is_new = matches!(nd.kind, NodeKind::NewTarget); + if !(is_new || matches!(nd.kind, NodeKind::ImportMeta)) || !nd.children.is_empty() { + return None; + } + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let x1 = x.saturating_add(1); + let x2 = x.saturating_add(2); + let x3 = x.saturating_add(3); + if is_new { + if !word_at(src, nodes, toks, gaps, x, &[0x6E, 0x65, 0x77], Ghost(GW { g, w: "new"@ })) + || !word_at( + src, + nodes, + toks, + gaps, + x2, + &[0x74, 0x61, 0x72, 0x67, 0x65, 0x74], + Ghost(GW { g, w: "target"@ }), + ) { + return None; + } + } else { + if !word_at( + src, + nodes, + toks, + gaps, + x, + &[0x69, 0x6D, 0x70, 0x6F, 0x72, 0x74], + Ghost(GW { g, w: "import"@ }), + ) || !word_at(src, nodes, toks, gaps, x2, &[0x6D, 0x65, 0x74, 0x61], Ghost(GW { g, w: "meta"@ })) { + return None; + } + } + if !punct_at(src, nodes, toks, gaps, x1, 0x2E, Ghost(GW { g, w: "."@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, x3, Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(x1 as int == x as int + 1); + assert(x2 as int == x as int + 2); + assert(x3 as int == x as int + 3); + assert(a.nodes[m as int].children.len() == 0); + lemma_meta_done(a, g.ts, g.cx, m as int, x as int); + } + Some((x, x3, 17)) +} + +// --------------------------------------------------------------------- +// §13.3.7 `super.name`, `super[expr]`, `super(args)`. +// --------------------------------------------------------------------- + +/// `SuperProperty : super . IdentifierName | super [ Expression ]`. +#[verifier::spinoff_prover] +proof fn lemma_super_prop_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c: int, + x: int, + lo_c: int, + hi_c: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + 0 <= x, + a.nodes[m].kind == NodeKind::SuperProperty, + a.nodes[m].children =~= seq![c], + is_kw(ts, x, "super"@), + x + 3 <= j <= tn(ts), + lo_c == x + 2, + node_done(a, ts, c, cx, lo_c, hi_c), + match a.nodes[m].op { + Op::Dot => is_p(ts, x + 1, "."@) && kd(a, c) == NodeKind::IdentifierName && j == x + 3, + Op::Bracket => cx_in(cx, true) == cx && is_p(ts, x + 1, "["@) && lvl(a, c) >= -1 + && is_p(ts, hi_c, "]"@) && j == hi_c + 1, + _ => false, + }, + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == 17, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c); + assert(dc(a, ts, m, c, cx, lo_c, hi_c)); + assert(d_expr_lhs(a, ts, m, cx, x, j)); + assert(d_expr(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.3.7 `super . name` and `super [ expression ]`. +#[verifier::spinoff_prover] +pub(crate) fn check_super_prop( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_e(); + lemma_punct_text(); + lemma_brackets(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let dot = matches!(nd.op, Op::Dot); + if !matches!(nd.kind, NodeKind::SuperProperty) || !(dot || matches!(nd.op, Op::Bracket)) + || nd.children.len() != 1 { + return None; + } + let cxm = match done.cxs.get(m) { + Some(c) => c, + None => { + return None; + }, + }; + let c = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_c, hi_c, lv_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let x1 = x.saturating_add(1); + let x2 = x.saturating_add(2); + if lo_c != x2 + || !word_at(src, nodes, toks, gaps, x, &[0x73, 0x75, 0x70, 0x65, 0x72], Ghost(GW { g, w: "super"@ })) { + return None; + } + let j: usize; + if dot { + j = x.saturating_add(3); + if !punct_at(src, nodes, toks, gaps, x1, 0x2E, Ghost(GW { g, w: "."@ })) + || !matches!(kind_at(nodes, c), NodeKind::IdentifierName) || hi_c != j { + return None; + } + } else { + if !cxm.i || cxm.d || lv_c < -1 || !punct_at(src, nodes, toks, gaps, x1, 0x5B, Ghost(GW { g, w: "["@ })) + || !punct_at(src, nodes, toks, gaps, hi_c, 0x5D, Ghost(GW { g, w: "]"@ })) { + return None; + } + j = hi_c.saturating_add(1); + } + if !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(cxm == &done.cxs@[m as int]); + assert(x1 as int == x as int + 1); + assert(x2 as int == x as int + 2); + assert(a.nodes[m as int].children =~= seq![c as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 0); + if dot { + assert(j as int == x as int + 3); + } else { + assert(j as int == hi_c as int + 1); + assert(g.cx.in_ && !g.cx.deflt); + assert(cx_in(g.cx, true) == g.cx); + } + lemma_super_prop_done(a, g.ts, g.cx, m as int, c as int, x as int, lo_c as int, hi_c as int, j as int); + } + Some((x, j, 17)) +} + +/// `SuperCall : super Arguments`. +#[verifier::spinoff_prover] +proof fn lemma_super_call_done(a: AstV, ts: Stream, cx: Ctx, m: int, c: int, x: int, lo_c: int, j: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + 0 <= x, + a.nodes[m].kind == NodeKind::SuperCall, + a.nodes[m].children =~= seq![c], + is_kw(ts, x, "super"@), + kd(a, c) == NodeKind::Arguments, + lo_c == x + 1, + node_done(a, ts, c, cx, lo_c, j), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == 15, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c); + assert(dc(a, ts, m, c, cx, lo_c, j)); + assert(d_expr_lhs(a, ts, m, cx, x, j)); + assert(d_expr(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.3.7 `super Arguments`. +#[verifier::spinoff_prover] +pub(crate) fn check_super_call( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_e(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::SuperCall) || nd.children.len() != 1 { + return None; + } + let c = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_c, j, _lv_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let x1 = x.saturating_add(1); + if lo_c != x1 || !matches!(kind_at(nodes, c), NodeKind::Arguments) + || !word_at(src, nodes, toks, gaps, x, &[0x73, 0x75, 0x70, 0x65, 0x72], Ghost(GW { g, w: "super"@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(x1 as int == x as int + 1); + assert(a.nodes[m as int].children =~= seq![c as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 0); + lemma_super_call_done(a, g.ts, g.cx, m as int, c as int, x as int, lo_c as int, j as int); + } + Some((x, j, 15)) +} + +// --------------------------------------------------------------------- +// §13.3.10 `import(specifier)`, `import(specifier, options)`. +// --------------------------------------------------------------------- + +/// `ImportCall`: the specifier, the options or `Absent`, a trailing `,` allowed. +#[verifier::spinoff_prover] +proof fn lemma_import_call_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + s: int, + o: int, + x: int, + mc: int, + q: int, + lo_o: int, + j: int, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= s < m, + 0 <= o < m, + 0 <= x, + a.nodes[m].kind == NodeKind::ImportCall, + a.nodes[m].children =~= seq![s, o], + is_kw(ts, x, "import"@), + is_p(ts, x + 1, "("@), + lvl(a, s) >= 0, + node_done(a, ts, s, cx, x + 2, mc), + if kd(a, o) == NodeKind::Absent { + d(a, ts, o, cx, mc, mc) && ed(a, ts, o, mc) == mc && q == mc + } else { + lvl(a, o) >= 0 && is_p(ts, mc, ","@) && lo_o == mc + 1 && node_done(a, ts, o, cx, lo_o, q) + }, + (is_p(ts, q, ")"@) && j == q + 1) || (is_p(ts, q, ","@) && is_p(ts, q + 1, ")"@) && j == q + + 2), + x + 3 <= j <= tn(ts), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == 15, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == s); + assert(a.nodes[m].children[1] == o); + assert(dc(a, ts, m, s, cx, x + 2, mc)); + assert(is_assign_expr(a, s)); + if kd(a, o) == NodeKind::Absent { + assert(dc(a, ts, m, o, cx, mc, mc)); + } else { + assert(dc(a, ts, m, o, cx, mc + 1, q)); + assert(is_assign_expr(a, o)); + } + assert(d_expr_lhs(a, ts, m, cx, x, j)); + assert(d_expr(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.3.10 `import ( specifier ,? )` and `import ( specifier , options ,? )`. +#[verifier::spinoff_prover] +pub(crate) fn check_import_call( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + cx_in(g.cx, true) == g.cx, + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_e(); + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ImportCall) || nd.children.len() != 2 { + return None; + } + let s = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let o = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_s, mc, lv_s) = match done_at(done, s) { + Some(r) => r, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let x1 = x.saturating_add(1); + let x2 = x.saturating_add(2); + if lv_s < 0 || lo_s != x2 + || !word_at(src, nodes, toks, gaps, x, &[0x69, 0x6D, 0x70, 0x6F, 0x72, 0x74], Ghost(GW { g, w: "import"@ })) + || !punct_at(src, nodes, toks, gaps, x1, 0x28, Ghost(GW { g, w: "("@ })) { + return None; + } + let absent = matches!(kind_at(nodes, o), NodeKind::Absent); + let (lo_o, q_e, lv_o) = if absent { + if !check_absent(src, nodes, toks, gaps, o, mc, Ghost(g)) { + return None; + } + (mc, mc, 0i32) + } else { + match done_at(done, o) { + Some(r) => r, + None => { + return None; + }, + } + }; + if !absent && (lv_o < 0 || lo_o != mc.saturating_add(1) + || !punct_n_at(src, nodes, toks, gaps, mc, &[0x2C], Ghost(GW { g, w: ","@ }))) { + return None; + } + // After the last operand, which ends at `q_e`: `)`, or `, )`. + let e1 = q_e.saturating_add(1); + let j: usize = if punct_at(src, nodes, toks, gaps, q_e, 0x29, Ghost(GW { g, w: ")"@ })) { + e1 + } else if punct_n_at(src, nodes, toks, gaps, q_e, &[0x2C], Ghost(GW { g, w: ","@ })) + && punct_at(src, nodes, toks, gaps, e1, 0x29, Ghost(GW { g, w: ")"@ })) { + q_e.saturating_add(2) + } else { + return None; + }; + if !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(x1 as int == x as int + 1); + assert(x2 as int == x as int + 2); + assert(e1 as int == q_e as int + 1); + assert(a.nodes[m as int].children =~= seq![s as int, o as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, s as int, g.cx, 0); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, o as int, g.cx, 1); + if !absent { + assert(kd(a, o as int) != NodeKind::Absent); + } + lemma_import_call_done( + a, + g.ts, + g.cx, + m as int, + s as int, + o as int, + x as int, + mc as int, + q_e as int, + lo_o as int, + j as int, + ); + } + Some((x, j, 15)) +} + +// --------------------------------------------------------------------- +// §13.3.9 optional chains: `a?.b`, `a?.[b]`, `a?.(c)`, `a?.#p`, and the links that +// continue them. +// --------------------------------------------------------------------- + +/// One link of an `OptionalChain`. +#[verifier::spinoff_prover] +proof fn lemma_chain_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c: int, + x: int, + s: int, + lo_c: int, + hi_c: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + 0 <= x < j <= tn(ts), + a.nodes[m].kind == NodeKind::OptionalChain, + a.nodes[m].children =~= seq![c], + s == (if a.nodes[m].flag { + x + 1 + } else { + x + }), + a.nodes[m].flag ==> is_p(ts, x, "?."@), + node_done(a, ts, c, cx, lo_c, hi_c), + match a.nodes[m].op { + Op::ChainDot => (a.nodes[m].flag || is_p(ts, x, "."@)) && kd(a, c) + == NodeKind::IdentifierName && j == x + 2 && lo_c == x + 1 && hi_c == j, + Op::ChainPrivateDot => (a.nodes[m].flag || is_p(ts, x, "."@)) && kd(a, c) + == NodeKind::PrivateIdentifier && j == x + 2 && lo_c == x + 1 && hi_c == j, + Op::ChainBracket => cx_in(cx, true) == cx && is_p(ts, s, "["@) && lvl(a, c) >= -1 + && lo_c == s + 1 && is_p(ts, hi_c, "]"@) && j == hi_c + 1, + Op::ChainCall => kd(a, c) == NodeKind::Arguments && lo_c == s && j == hi_c, + _ => false, + }, + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c); + assert(dc(a, ts, m, c, cx, lo_c, hi_c)); + assert(d_chain_link(a, ts, m, cx, x, j)); + assert(d_expr_lhs(a, ts, m, cx, x, j)); + assert(d_expr(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.3.9 one link of an `OptionalChain`: `?. name`, `?. [ expr ]`, `?. ( args )`, `?. #name`, +/// or the same without the `?.` (`. name`, `[ expr ]`, `( args )`, `. #name`). +#[verifier::spinoff_prover] +pub(crate) fn check_chain_link( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_e(); + lemma_punct_text(); + lemma_brackets(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::OptionalChain) || nd.children.len() != 1 { + return None; + } + let cxm = match done.cxs.get(m) { + Some(c) => c, + None => { + return None; + }, + }; + let c = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_c, hi_c, lv_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let x1 = x.saturating_add(1); + let flag = nd.flag; + let s: usize = if flag { + x1 + } else { + x + }; + if flag && !punct_n_at(src, nodes, toks, gaps, x, &[0x3F, 0x2E], Ghost(GW { g, w: "?."@ })) { + return None; + } + let j: usize = match nd.op { + Op::ChainDot | Op::ChainPrivateDot => { + let private = matches!(nd.op, Op::ChainPrivateDot); + let kc = kind_at(nodes, c); + let kind_ok = if private { + matches!(kc, NodeKind::PrivateIdentifier) + } else { + matches!(kc, NodeKind::IdentifierName) + }; + let j = x.saturating_add(2); + if (!flag && !punct_at(src, nodes, toks, gaps, x, 0x2E, Ghost(GW { g, w: "."@ }))) || !kind_ok + || lo_c != x1 || hi_c != j { + return None; + } + j + }, + Op::ChainBracket => { + if !cxm.i || cxm.d || lv_c < -1 || lo_c != s.saturating_add(1) + || !punct_at(src, nodes, toks, gaps, s, 0x5B, Ghost(GW { g, w: "["@ })) + || !punct_at(src, nodes, toks, gaps, hi_c, 0x5D, Ghost(GW { g, w: "]"@ })) { + return None; + } + hi_c.saturating_add(1) + }, + Op::ChainCall => { + if !matches!(kind_at(nodes, c), NodeKind::Arguments) || lo_c != s { + return None; + } + hi_c + }, + _ => { + return None; + }, + }; + if !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(cxm == &done.cxs@[m as int]); + assert(x1 as int == x as int + 1); + assert(s as int == (if a.nodes[m as int].flag { + x as int + 1 + } else { + x as int + })); + assert(a.nodes[m as int].children =~= seq![c as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 0); + match a.nodes[m as int].op { + Op::ChainDot => { + assert(j as int == x as int + 2); + }, + Op::ChainPrivateDot => { + assert(j as int == x as int + 2); + }, + Op::ChainBracket => { + assert(s.saturating_add(1) as int == s as int + 1); + assert(hi_c.saturating_add(1) as int == hi_c as int + 1); + assert(g.cx.in_ && !g.cx.deflt); + assert(cx_in(g.cx, true) == g.cx); + }, + _ => {}, + } + lemma_chain_done( + a, + g.ts, + g.cx, + m as int, + c as int, + x as int, + s as int, + lo_c as int, + hi_c as int, + j as int, + ); + } + Some((x, j, -100)) +} + +/// `OptionalExpression : base link links...`. +#[verifier::spinoff_prover] +proof fn lemma_optional_done(a: AstV, ts: Stream, cx: Ctx, m: int, base: int, x: int, mb: int, j: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= base < m, + 0 <= x < j <= tn(ts), + a.nodes[m].kind == NodeKind::OptionalExpression, + a.nodes[m].children.len() >= 2, + a.nodes[m].children[0] == base, + is_member_or_call(a, base), + all_kind2(a, a.nodes[m].children, 1, NodeKind::OptionalChain, NodeKind::OptionalChain), + flag_of(a, a.nodes[m].children[1]), + node_done(a, ts, base, cx, x, mb), + dl(a, ts, m, a.nodes[m].children, 1, cx, mb, j), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == 14, +{ + assert(span_ok(a, ts, m, x, j)); + assert(dc(a, ts, m, base, cx, x, mb)); + assert(d_expr_lhs(a, ts, m, cx, x, j)); + assert(d_expr(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §13.3.9 `OptionalExpression`: a base and its links. +#[verifier::spinoff_prover] +pub(crate) fn check_optional( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let len = nd.children.len(); + if !matches!(nd.kind, NodeKind::OptionalExpression) || len < 2 { + return None; + } + let base = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let first_link = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let last_link = match kid_of(nodes, m, len.saturating_sub(1)) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_b, mb, lv_b) = match done_at(done, base) { + Some(r) => r, + None => { + return None; + }, + }; + let (_lo_l, j, _lv_l) = match done_at(done, last_link) { + Some(r) => r, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + let flag_first = match nodes.get(first_link) { + Some(l) => l.flag, + None => { + return None; + }, + }; + if !(lv_b == 15 || lv_b == 17) || lo_b != x || !flag_first + || !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + let ghost a = arena_of(nodes@); + proof { + assert(nd == &nodes@[m as int]); + assert(first_link < nodes.len()); + assert(nodes@[first_link as int].flag == a.nodes[first_link as int].flag); + } + let mut k = len; + let mut pos = j; + while k > 1 + invariant + m < nodes.len(), + nodes@.len() == nodes.len(), + a == arena_of(nodes@), + ast_wf(a), + nd == &nodes@[m as int], + len == nd.children.len(), + len == a.nodes[m as int].children.len(), + 1 <= k <= len, + pos <= toks.len(), + j <= toks.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(a, done.cxs@, m as int, g.cx), + dl(a, g.ts, m as int, a.nodes[m as int].children, k as int, g.cx, pos as int, j as int), + forall|q: int| + k <= q < len ==> kd(a, #[trigger] a.nodes[m as int].children[q]) + == NodeKind::OptionalChain, + decreases k, + { + let km1 = k.saturating_sub(1); + let c = match kid_of(nodes, m, km1) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_c, hi_c, _lv_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + if hi_c != pos || !matches!(kind_at(nodes, c), NodeKind::OptionalChain) { + return None; + } + proof { + assert(km1 as int == k as int - 1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, km1 as int); + assert(a.nodes[m as int].children[k as int - 1] == c as int); + lemma_dl_cons( + a, + g.ts, + m as int, + a.nodes[m as int].children, + k as int - 1, + g.cx, + lo_c as int, + hi_c as int, + j as int, + ); + } + pos = lo_c; + k = km1; + } + if pos != mb { + return None; + } + proof { + assert(a.nodes[m as int].children[0] == base as int); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, base as int, g.cx, 0); + assert(all_kind2( + a, + a.nodes[m as int].children, + 1, + NodeKind::OptionalChain, + NodeKind::OptionalChain, + )); + assert(a.nodes[m as int].children[1] == first_link as int); + assert(flag_of(a, first_link as int)); + assert(is_member_or_call(a, base as int)); + lemma_optional_done(a, g.ts, g.cx, m as int, base as int, x as int, mb as int, j as int); + } + Some((x, j, 14)) +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/arena_early.rs b/crates/clazel-js-syntax/src/arena_early.rs new file mode 100644 index 000000000..62a83a8c4 --- /dev/null +++ b/crates/clazel-js-syntax/src/arena_early.rs @@ -0,0 +1,1789 @@ +//! A proven check of the early errors over a whole arena (item 7.3): [`check_early`] +//! carries an [`EEnv`] per node (the executable form of `spec::EarlyEnv`), checks that +//! each node's is the one `spec::child_env` gives it from its parent's, and that +//! `spec::early_local` holds at each node, bottom-up. It returns `Verified` only if +//! `spec::early_errors_ok` holds. +//! +//! **Covered.** The environment: every node kind whose `child_env` arm is the default +//! (`[~Tagged]`) or one of the loop, `switch`, object literal, tagged template and +//! template arms, and the function declarations and expressions of every kind, arrow +//! functions and methods of object literals (`env_fn`, and `has_use_strict` of the body, +//! [`crate::arena_names::use_strict_exec`]); the `labels` and `privates` of the +//! environment are empty, so a labelled statement or a class makes the result `Unsupported`. +//! Function code: `function_early` ([`crate::arena_names::function_early_exec`]: Use Strict +//! Directive with non-simple parameters, duplicate parameters, from the BoundNames of +//! [`crate::arena_names::bound_names_all`]) and the statement list of a `FunctionBody` +//! that declares nothing lexically. +//! The rules of `early_local`: every kind with no rule (the `_ => true` arm); +//! `NumericLiteral` and `StringLiteral` (strict mode legacy forms); the three identifier +//! kinds (§13.1.1: strict reserved words, `eval`, `arguments`, `yield`, `await`, the +//! reserved words, `arguments` in field initializers); `TemplateElement`; +//! `SuperProperty`, `SuperCall`, `NewTarget`, `ImportMeta`; `YieldExpression` and +//! `AwaitExpression`; `MemberExpression` (not `.#x`), `OptionalChain` (not `?.#x` or a +//! template), `RelationalExpression` (not `#x in`), `UnaryExpression` (not `delete`); +//! `UpdateExpression` and `AssignmentExpression` with a `~simple~` target (members, +//! `super` properties, identifiers other than strict `eval` and `arguments`, through +//! parentheses) or an assignment pattern; `if`, `while`, `do`-`while`, `with` (no labelled +//! function as the body; no `with`, no function body of an `if` in strict mode); `for ( ; ; )` +//! without a lexical declaration; unlabelled `break` and `continue`; the statement lists +//! of a block and a script whose items declare nothing lexically (so +//! `LexicallyDeclaredNames` is empty). +//! +//! **Not covered** (the result is `Unsupported`): everything else, notably +//! class code, labels, lexical declarations, `for`-`in`/`of`, `switch`, `try` +//! (`catch` parameters), regular expression literals (`regexp_valid` is item 7.10's), +//! private names, a Use Strict Directive in a script, and `Module` roots. +//! +//! **Assumptions**, those of [`crate::arena_d`]: the executable tokens are the +//! specification's (`dtok_matches`, which now also covers `legacy`, `invalid_escape` +//! and `name`, the lexer's contract of item 7.2). + +use crate::arena_d::*; +use crate::arena_decls::*; +use crate::arena_names::*; +use crate::ast::*; +use crate::spec::*; +use vstd::prelude::*; + +verus! { + +/// The executable tokens are the specification's stream `ts`. +pub open spec fn toks_match(src: Seq, toks: Seq, ts: Stream) -> bool { + toks.len() == ts.toks.len() && (forall|k: int| + 0 <= k < toks.len() ==> dtok_matches(src, toks[k], #[trigger] ts.toks[k])) +} + +/// The tokens start at increasing positions, so a start names one token. +pub open spec fn starts_inc(ts: Stream) -> bool { + forall|x: int, y: int| + 0 <= x < y < tn(ts) ==> (#[trigger] ts.toks[x]).start < (#[trigger] ts.toks[y]).start +} + +/// The first token of node `n` is `ts.toks[x]`. +pub(crate) proof fn lemma_node_tok_at(a: AstV, ts: Stream, n: int, x: int) + requires + starts_inc(ts), + 0 <= n < a.nodes.len(), + 0 <= x < tn(ts), + ts.toks[x].start == a.nodes[n].start, + ensures + node_tok(a, ts, n) == ts.toks[x], +{ + assert(exists|y: int| 0 <= y < tn(ts) && ts.toks[y].start == a.nodes[n].start); + let y = choose|y: int| 0 <= y < tn(ts) && ts.toks[y].start == a.nodes[n].start; + if y < x { + assert(ts.toks[y].start < ts.toks[x].start); + } + if y > x { + assert(ts.toks[x].start < ts.toks[y].start); + } +} + +/// The words of the identifier rules, as code points. +pub(crate) proof fn lemma_words_early() + ensures + sv("yield"@) =~= seq![0x79u32, 0x69, 0x65, 0x6C, 0x64], + sv("await"@) =~= seq![0x61u32, 0x77, 0x61, 0x69, 0x74], + sv("arguments"@) =~= seq![0x61u32, 0x72, 0x67, 0x75, 0x6D, 0x65, 0x6E, 0x74, 0x73], + sv("eval"@) =~= seq![0x65u32, 0x76, 0x61, 0x6C], + sv("implements"@) =~= seq![0x69u32, 0x6D, 0x70, 0x6C, 0x65, 0x6D, 0x65, 0x6E, 0x74, 0x73], + sv("interface"@) =~= seq![0x69u32, 0x6E, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65], + sv("let"@) =~= seq![0x6Cu32, 0x65, 0x74], + sv("package"@) =~= seq![0x70u32, 0x61, 0x63, 0x6B, 0x61, 0x67, 0x65], + sv("private"@) =~= seq![0x70u32, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65], + sv("protected"@) =~= seq![0x70u32, 0x72, 0x6F, 0x74, 0x65, 0x63, 0x74, 0x65, 0x64], + sv("public"@) =~= seq![0x70u32, 0x75, 0x62, 0x6C, 0x69, 0x63], + sv("static"@) =~= seq![0x73u32, 0x74, 0x61, 0x74, 0x69, 0x63], +{ + reveal_strlit("yield"); + reveal_strlit("await"); + reveal_strlit("arguments"); + reveal_strlit("eval"); + reveal_strlit("implements"); + reveal_strlit("interface"); + reveal_strlit("let"); + reveal_strlit("package"); + reveal_strlit("private"); + reveal_strlit("protected"); + reveal_strlit("public"); + reveal_strlit("static"); + assert("yield"@ =~= seq!['y', 'i', 'e', 'l', 'd']); + assert("await"@ =~= seq!['a', 'w', 'a', 'i', 't']); + assert("arguments"@ =~= seq!['a', 'r', 'g', 'u', 'm', 'e', 'n', 't', 's']); + assert("eval"@ =~= seq!['e', 'v', 'a', 'l']); + assert("implements"@ =~= seq!['i', 'm', 'p', 'l', 'e', 'm', 'e', 'n', 't', 's']); + assert("interface"@ =~= seq!['i', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e']); + assert("let"@ =~= seq!['l', 'e', 't']); + assert("package"@ =~= seq!['p', 'a', 'c', 'k', 'a', 'g', 'e']); + assert("private"@ =~= seq!['p', 'r', 'i', 'v', 'a', 't', 'e']); + assert("protected"@ =~= seq!['p', 'r', 'o', 't', 'e', 'c', 't', 'e', 'd']); + assert("public"@ =~= seq!['p', 'u', 'b', 'l', 'i', 'c']); + assert("static"@ =~= seq!['s', 't', 'a', 't', 'i', 'c']); +} + +/// Two slices of code points are the same. +pub(crate) fn seq_eq(a: &[u32], b: &[u32]) -> (r: bool) + ensures + r == (a@ =~= b@), +{ + if a.len() != b.len() { + return false; + } + let mut i: usize = 0; + while i < a.len() + invariant + a.len() == b.len(), + i <= a.len(), + forall|q: int| 0 <= q < i ==> a@[q] == b@[q], + decreases a.len() - i, + { + let (x, y) = match (a.get(i), b.get(i)) { + (Some(x), Some(y)) => (*x, *y), + _ => { + return false; + }, + }; + if x != y { + return false; + } + i = i.saturating_add(1); + } + true +} + +/// The index of the reserved word `name` is, if it is one. +fn reserved_of(name: &[u32]) -> (r: Option) + ensures + match r { + Some(w) => w < 38 && name@ =~= reserved_chars(w as int), + None => forall|w: int| 0 <= w < 38 ==> !(name@ =~= #[trigger] reserved_chars(w)), + }, +{ + let mut w: usize = 0; + while w < 38 + invariant + w <= 38, + forall|q: int| 0 <= q < w ==> !(name@ =~= #[trigger] reserved_chars(q)), + decreases 38 - w, + { + let c = reserved_slice(w); + if seq_eq(name, c) { + return Some(w); + } + w = w.saturating_add(1); + } + None +} + +// --------------------------------------------------------------------- +// The environment. +// --------------------------------------------------------------------- + +/// The facts about the position of a node that the checked early errors read: the +/// executable form of `spec::EarlyEnv` with no labels and no private names. +#[derive(Clone, Copy)] +pub struct EEnv { + /// `IsStrict`. + pub strict: bool, + /// The goal symbol is `Module`. + pub module: bool, + /// `[Yield]`. + pub yield_: bool, + /// `[Await]`. + pub await_: bool, + /// In an iteration statement. + pub in_loop: bool, + /// In an iteration statement or a `switch`. + pub in_breakable: bool, + /// `SuperProperty` is allowed. + pub super_prop: bool, + /// `SuperCall` is allowed. + pub super_call: bool, + /// `new.target` is allowed. + pub new_target: bool, + /// `arguments` is not allowed. + pub no_arguments: bool, + /// `yield` and `await` expressions are not allowed. + pub no_yield_await: bool, + /// `[Tagged]`. + pub tagged: bool, + /// The nearest class has a heritage. + pub derived: bool, +} + +/// The specification's environment of an executable one. +pub open spec fn eenv_of(e: EEnv) -> EarlyEnv { + EarlyEnv { + strict: e.strict, + module: e.module, + yield_: e.yield_, + await_: e.await_, + labels: Seq::empty(), + in_loop: e.in_loop, + in_breakable: e.in_breakable, + super_prop: e.super_prop, + super_call: e.super_call, + new_target: e.new_target, + no_arguments: e.no_arguments, + no_yield_await: e.no_yield_await, + tagged: e.tagged, + privates: Seq::empty(), + derived: e.derived, + } +} + +/// Two environments are the same. +fn eenv_eq(p: &EEnv, q: &EEnv) -> (r: bool) + ensures + r == (eenv_of(*p) == eenv_of(*q)), +{ + p.strict == q.strict && p.module == q.module && p.yield_ == q.yield_ && p.await_ == q.await_ + && p.in_loop == q.in_loop && p.in_breakable == q.in_breakable && p.super_prop + == q.super_prop && p.super_call == q.super_call && p.new_target == q.new_target + && p.no_arguments == q.no_arguments && p.no_yield_await == q.no_yield_await && p.tagged + == q.tagged && p.derived == q.derived +} + +/// The arguments of `spec::env_fn` that say what the function code allows. +pub struct FnFlags { + /// `[Yield]`. + pub y: bool, + /// `[Await]`. + pub aw: bool, + /// `SuperProperty` is allowed. + pub sp: bool, + /// `SuperCall` is allowed. + pub sc: bool, + /// `new.target` is allowed. + pub nt: bool, + /// `arguments` is not allowed. + pub na: bool, + /// `yield` and `await` expressions are not allowed. + pub nya: bool, +} + +/// `spec::env_fn`. +fn env_fn_exec(e: &EEnv, strict: bool, f: FnFlags) -> (r: EEnv) + ensures + eenv_of(r) == env_fn(eenv_of(*e), strict, f.y, f.aw, f.sp, f.sc, f.nt, f.na, f.nya), +{ + EEnv { + strict, + module: e.module, + yield_: f.y, + await_: f.aw, + in_loop: false, + in_breakable: false, + super_prop: f.sp, + super_call: f.sc, + new_target: f.nt, + no_arguments: f.na, + no_yield_await: f.nya, + tagged: false, + derived: e.derived, + } +} + +/// `IsStrict` of the code of the function-like node `n` whose body is its third child: +/// the enclosing code's, or a Use Strict Directive of the body. +#[verifier::rlimit(400)] +fn fn_strict_exec( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + by_start: &[usize], + n: usize, + env_strict: bool, + Ghost(ts): Ghost, +) -> (r: Option) + requires + n < nodes.len(), + ast_wf(arena_of(nodes@)), + toks_match(src@, toks@, ts), + starts_inc(ts), + ensures + match r { + Some(v) => { + let a = arena_of(nodes@); + v == (env_strict || has_use_strict(a, ts, kids(a, kid(a.nodes[n as int].children, 2)))) + }, + None => true, + }, +{ + if env_strict { + return Some(true); + } + let ghost a = arena_of(nodes@); + let nd = match nodes.get(n) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + assert(a.nodes[n as int] == nodes@[n as int]@); + assert(nd == &nodes@[n as int]); + assert(a.nodes[n as int].children.len() == nd.children.len()); + } + match nd.children.as_slice().get(2) { + Some(b) => { + let b = *b as usize; + proof { + assert(a.nodes[n as int].children[2] == b as int); + lemma_child_before(a, n as int, 2); + } + use_strict_exec(src, nodes, toks, by_start, b, Ghost(ts)) + }, + None => { + proof { + assert(kid(a.nodes[n as int].children, 2) == -1int); + assert(kids(a, -1int) =~= Seq::::empty()); + } + Some(false) + }, + } +} + +/// `IsStrict` of the code of the arrow function `n`. +#[verifier::rlimit(400)] +fn arrow_strict_exec( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + by_start: &[usize], + n: usize, + env_strict: bool, + Ghost(ts): Ghost, +) -> (r: Option) + requires + n < nodes.len(), + ast_wf(arena_of(nodes@)), + toks_match(src@, toks@, ts), + starts_inc(ts), + ensures + match r { + Some(v) => { + let a = arena_of(nodes@); + let k1 = kid(a.nodes[n as int].children, 1); + v == (env_strict || (kd(a, k1) == NodeKind::FunctionBody && has_use_strict( + a, + ts, + kids(a, k1), + ))) + }, + None => true, + }, +{ + if env_strict { + return Some(true); + } + let ghost a = arena_of(nodes@); + let nd = match nodes.get(n) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + assert(a.nodes[n as int] == nodes@[n as int]@); + assert(nd == &nodes@[n as int]); + assert(a.nodes[n as int].children.len() == nd.children.len()); + } + match kid_kind(nodes, n, 1) { + Some(NodeKind::FunctionBody) => { + let b = match nd.children.as_slice().get(1) { + Some(b) => *b as usize, + None => { + return None; + }, + }; + proof { + assert(a.nodes[n as int].children[1] == b as int); + lemma_child_before(a, n as int, 1); + } + use_strict_exec(src, nodes, toks, by_start, b, Ghost(ts)) + }, + Some(_) => Some(false), + None => None, + } +} + +/// A function declaration or expression of any kind. +pub open spec fn is_function_kind(k: NodeKind) -> bool { + k == NodeKind::FunctionDeclaration || k == NodeKind::FunctionExpression || k + == NodeKind::GeneratorDeclaration || k == NodeKind::GeneratorExpression || k + == NodeKind::AsyncFunctionDeclaration || k == NodeKind::AsyncFunctionExpression || k + == NodeKind::AsyncGeneratorDeclaration || k == NodeKind::AsyncGeneratorExpression +} + +/// `spec::child_env` for a function declaration or expression of any kind. +#[verifier::rlimit(400)] +fn child_env_function( + tk: &Tk, + nodes: &[Node], + n: usize, + k: usize, + e: &EEnv, + Ghost(ts): Ghost, +) -> (r: Option) + requires + n < nodes.len(), + ast_wf(arena_of(nodes@)), + k < nodes@[n as int].children.len(), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + is_function_kind(nodes@[n as int].kind), + ensures + match r { + Some(c) => eenv_of(c) == child_env(arena_of(nodes@), ts, n as int, k as int, eenv_of(*e)), + None => true, + }, +{ + let src = tk.src; + let toks = tk.toks; + let by_start = tk.by_start; + let ghost a = arena_of(nodes@); + let kind = match nodes.get(n) { + Some(x) => x.kind, + None => { + return None; + }, + }; + proof { + assert(a.nodes[n as int] == nodes@[n as int]@); + assert(a.nodes[n as int].kind == kind); + } + let strict = match fn_strict_exec(src, nodes, toks, by_start, n, e.strict, Ghost(ts)) { + Some(v) => v, + None => { + return None; + }, + }; + let g = matches!( + kind, + NodeKind::GeneratorDeclaration | NodeKind::GeneratorExpression + | NodeKind::AsyncGeneratorDeclaration | NodeKind::AsyncGeneratorExpression + ); + let aw = matches!( + kind, + NodeKind::AsyncFunctionDeclaration | NodeKind::AsyncFunctionExpression + | NodeKind::AsyncGeneratorDeclaration | NodeKind::AsyncGeneratorExpression + ); + let expr = matches!( + kind, + NodeKind::FunctionExpression | NodeKind::GeneratorExpression + | NodeKind::AsyncFunctionExpression | NodeKind::AsyncGeneratorExpression + ); + if k == 0 { + Some( + EEnv { + strict, + yield_: if expr { + g + } else { + e.yield_ + }, + await_: if expr { + aw + } else { + e.await_ + }, + ..*e + }, + ) + } else { + Some(env_fn_exec(e, strict, FnFlags { y: g, aw, sp: false, sc: false, nt: true, na: false, nya: k == 1 })) + } +} + +/// `spec::child_env` for a method of an object literal. +#[verifier::rlimit(400)] +fn child_env_method( + tk: &Tk, + nodes: &[Node], + n: usize, + k: usize, + e: &EEnv, + Ghost(ts): Ghost, +) -> (r: Option) + requires + n < nodes.len(), + ast_wf(arena_of(nodes@)), + k < nodes@[n as int].children.len(), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + nodes@[n as int].kind == NodeKind::MethodDefinition, + ensures + match r { + Some(c) => eenv_of(c) == child_env(arena_of(nodes@), ts, n as int, k as int, eenv_of(*e)), + None => true, + }, +{ + let src = tk.src; + let toks = tk.toks; + let by_start = tk.by_start; + let ghost a = arena_of(nodes@); + let op = match nodes.get(n) { + Some(x) => x.op, + None => { + return None; + }, + }; + proof { + assert(a.nodes[n as int] == nodes@[n as int]@); + assert(a.nodes[n as int].op == op); + } + if k == 0 { + return Some(*e); + } + // A constructor of a derived class may `super()`: classes are not covered. + if e.derived { + return None; + } + let strict = match fn_strict_exec(src, nodes, toks, by_start, n, e.strict, Ghost(ts)) { + Some(v) => v, + None => { + return None; + }, + }; + let mg = matches!(op, Op::GeneratorMethod | Op::AsyncGeneratorMethod); + let ma = matches!(op, Op::AsyncMethod | Op::AsyncGeneratorMethod); + Some(env_fn_exec(e, strict, FnFlags { y: mg, aw: ma, sp: true, sc: false, nt: true, na: false, nya: k == 1 })) +} + +/// `spec::child_env` for an arrow function. +#[verifier::rlimit(400)] +fn child_env_arrow( + tk: &Tk, + nodes: &[Node], + n: usize, + k: usize, + e: &EEnv, + Ghost(ts): Ghost, +) -> (r: Option) + requires + n < nodes.len(), + ast_wf(arena_of(nodes@)), + k < nodes@[n as int].children.len(), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + nodes@[n as int].kind == NodeKind::ArrowFunction || nodes@[n as int].kind + == NodeKind::AsyncArrowFunction, + ensures + match r { + Some(c) => eenv_of(c) == child_env(arena_of(nodes@), ts, n as int, k as int, eenv_of(*e)), + None => true, + }, +{ + let src = tk.src; + let toks = tk.toks; + let by_start = tk.by_start; + let ghost a = arena_of(nodes@); + let kind = match nodes.get(n) { + Some(x) => x.kind, + None => { + return None; + }, + }; + proof { + assert(a.nodes[n as int] == nodes@[n as int]@); + assert(a.nodes[n as int].kind == kind); + } + let strict = match arrow_strict_exec(src, nodes, toks, by_start, n, e.strict, Ghost(ts)) { + Some(v) => v, + None => { + return None; + }, + }; + if matches!(kind, NodeKind::ArrowFunction) { + if k == 0 { + Some(env_fn_exec(e, strict, FnFlags { y: e.yield_, aw: e.await_, sp: e.super_prop, sc: e.super_call, nt: e.new_target, na: e.no_arguments, nya: true })) + } else { + Some(env_fn_exec(e, strict, FnFlags { y: false, aw: false, sp: e.super_prop, sc: e.super_call, nt: e.new_target, na: e.no_arguments, nya: false })) + } + } else if k == 0 { + let y = match kid_kind(nodes, n, 0) { + Some(NodeKind::BindingIdentifier) => e.yield_, + Some(_) => false, + None => { + return None; + }, + }; + Some(env_fn_exec(e, strict, FnFlags { y, aw: true, sp: e.super_prop, sc: e.super_call, nt: e.new_target, na: e.no_arguments, nya: true })) + } else { + Some(env_fn_exec(e, strict, FnFlags { y: false, aw: true, sp: e.super_prop, sc: e.super_call, nt: e.new_target, na: e.no_arguments, nya: false })) + } +} + +/// `spec::child_env` for the function-like nodes: child `k` of node `n`. +fn child_env_fnlike( + tk: &Tk, + nodes: &[Node], + n: usize, + k: usize, + e: &EEnv, + Ghost(ts): Ghost, +) -> (r: Option) + requires + n < nodes.len(), + ast_wf(arena_of(nodes@)), + k < nodes@[n as int].children.len(), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + ensures + match r { + Some(c) => eenv_of(c) == child_env(arena_of(nodes@), ts, n as int, k as int, eenv_of(*e)), + None => true, + }, +{ + let kind = match nodes.get(n) { + Some(x) => x.kind, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[n as int] == nodes@[n as int]@); + } + match kind { + NodeKind::FunctionDeclaration | NodeKind::FunctionExpression | NodeKind::GeneratorDeclaration + | NodeKind::GeneratorExpression | NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncFunctionExpression | NodeKind::AsyncGeneratorDeclaration + | NodeKind::AsyncGeneratorExpression => child_env_function(tk, nodes, n, k, e, Ghost(ts)), + NodeKind::MethodDefinition => child_env_method(tk, nodes, n, k, e, Ghost(ts)), + NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction => child_env_arrow(tk, nodes, n, k, e, Ghost(ts)), + _ => None, + } +} + + +/// `spec::child_env` for the node kinds whose arm is simple: child `k` of node `n`. +#[verifier::rlimit(400)] +fn child_env_exec( + tk: &Tk, + nodes: &[Node], + n: usize, + k: usize, + e: &EEnv, + Ghost(ts): Ghost, +) -> (r: Option) + requires + n < nodes.len(), + ast_wf(arena_of(nodes@)), + k < nodes@[n as int].children.len(), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + ensures + match r { + Some(c) => eenv_of(c) == child_env(arena_of(nodes@), ts, n as int, k as int, eenv_of(*e)), + None => true, + }, +{ + let nd = match nodes.get(n) { + Some(x) => x, + None => { + return None; + }, + }; + let ch = match nd.children.as_slice().get(k) { + Some(c) => *c as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[n as int] == nodes@[n as int]@); + assert(nd == &nodes@[n as int]); + assert(a.nodes[n as int].children[k as int] == ch as int); + assert(a.nodes[n as int].kind == nd.kind); + } + let plain = EEnv { tagged: false, ..*e }; + match nd.kind { + NodeKind::FunctionDeclaration | NodeKind::FunctionExpression | NodeKind::GeneratorDeclaration + | NodeKind::GeneratorExpression | NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncFunctionExpression | NodeKind::AsyncGeneratorDeclaration + | NodeKind::AsyncGeneratorExpression | NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction + | NodeKind::MethodDefinition => child_env_fnlike(tk, nodes, n, k, e, Ghost(ts)), + NodeKind::FieldDefinition | NodeKind::ClassStaticBlock | NodeKind::ClassDeclaration + | NodeKind::ClassExpression | NodeKind::ClassBody | NodeKind::LabelledStatement => None, + NodeKind::ObjectLiteral => Some(EEnv { derived: false, ..*e }), + NodeKind::DoWhileStatement => if k == 0 { + Some(EEnv { in_loop: true, in_breakable: true, ..*e }) + } else { + Some(*e) + }, + NodeKind::WhileStatement => if k == 1 { + Some(EEnv { in_loop: true, in_breakable: true, ..*e }) + } else { + Some(*e) + }, + NodeKind::ForStatement => if k == 3 { + Some(EEnv { in_loop: true, in_breakable: true, ..*e }) + } else { + Some(*e) + }, + NodeKind::ForInStatement | NodeKind::ForOfStatement => if k == 2 { + Some(EEnv { in_loop: true, in_breakable: true, ..*e }) + } else { + Some(*e) + }, + NodeKind::SwitchStatement => if k >= 1 { + Some(EEnv { in_breakable: true, ..*e }) + } else { + Some(*e) + }, + NodeKind::TaggedTemplate => Some(EEnv { tagged: k == 1, ..*e }), + NodeKind::TemplateLiteral => { + let ck = match nodes.get(ch) { + Some(x) => x.kind, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[ch as int] == nodes@[ch as int]@); + } + if matches!(ck, NodeKind::TemplateElement) { + Some(*e) + } else { + Some(plain) + } + }, + _ => Some(plain), + } +} + +// --------------------------------------------------------------------- +// Rules. +// --------------------------------------------------------------------- + +/// §13.1.1 the identifier rules in terms of the name, the kind of node and the environment. +pub open spec fn ident_name_ok(nm: Seq, kind: NodeKind, env: EarlyEnv) -> bool { + let is_yield = nm =~= sv("yield"@); + let is_await = nm =~= sv("await"@); + !(kind == NodeKind::BindingIdentifier && env.strict && (nm =~= sv("arguments"@) || nm + =~= sv("eval"@))) && !(env.strict && is_strict_reserved(nm)) && !(env.module && is_await) + && !(env.yield_ && is_yield) && !(env.await_ && is_await) && !(is_reserved_text(nm) + && !is_yield && !is_await) && !(kind == NodeKind::IdentifierReference && env.no_arguments + && nm =~= sv("arguments"@)) +} + +/// `early_identifier_ok` is `ident_name_ok` of the node's name. +proof fn lemma_ident_rule(a: AstV, ts: Stream, c: int, env: EarlyEnv) + ensures + early_identifier_ok(a, ts, c, env) == ident_name_ok(name_of(a, ts, c), kd(a, c), env), +{ +} + +/// §13.1.1 for an identifier of `name`. +fn ident_early(name: &[u32], binding: bool, reference: bool, e: &EEnv) -> (r: bool) + ensures + r ==> ident_name_ok( + name@, + if binding { + NodeKind::BindingIdentifier + } else if reference { + NodeKind::IdentifierReference + } else { + NodeKind::LabelIdentifier + }, + eenv_of(*e), + ), +{ + proof { + lemma_words_early(); + } + let is_yield = seq_eq(name, &[0x79, 0x69, 0x65, 0x6C, 0x64]); + let is_await = seq_eq(name, &[0x61, 0x77, 0x61, 0x69, 0x74]); + let is_args = seq_eq(name, &[0x61, 0x72, 0x67, 0x75, 0x6D, 0x65, 0x6E, 0x74, 0x73]); + let is_eval = seq_eq(name, &[0x65, 0x76, 0x61, 0x6C]); + let strict_reserved = is_yield || seq_eq(name, &[0x69, 0x6D, 0x70, 0x6C, 0x65, 0x6D, 0x65, 0x6E, 0x74, 0x73]) + || seq_eq(name, &[0x69, 0x6E, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65]) + || seq_eq(name, &[0x6C, 0x65, 0x74]) || seq_eq(name, &[0x70, 0x61, 0x63, 0x6B, 0x61, 0x67, 0x65]) + || seq_eq(name, &[0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65]) + || seq_eq(name, &[0x70, 0x72, 0x6F, 0x74, 0x65, 0x63, 0x74, 0x65, 0x64]) + || seq_eq(name, &[0x70, 0x75, 0x62, 0x6C, 0x69, 0x63]) + || seq_eq(name, &[0x73, 0x74, 0x61, 0x74, 0x69, 0x63]); + let reserved = reserved_of(name); + let is_reserved = reserved.is_some(); + proof { + if let Some(w) = reserved { + lemma_reserved_chars(w as int); + assert(is_reserved_text(name@)) by { + assert(0 <= w < reserved_words().len() && name@ =~= cps(reserved_words()[w as int])); + } + } else { + assert(!is_reserved_text(name@)) by { + assert forall|w: int| 0 <= w < reserved_words().len() implies !(name@ =~= cps( + #[trigger] reserved_words()[w], + )) by { + lemma_reserved_chars(w); + } + } + } + } + !(binding && e.strict && (is_args || is_eval)) && !(e.strict && strict_reserved) + && !(e.module && is_await) && !(e.yield_ && is_yield) && !(e.await_ && is_await) + && !(is_reserved && !is_yield && !is_await) && !(reference && e.no_arguments && is_args) +} + +/// The statement `c` is not a labelled function (`is_labelled_function`): it is not a label. +fn not_labelled(nodes: &[Node], c: usize) -> (r: bool) + requires + c < nodes.len(), + ensures + r ==> !is_labelled_function(arena_of(nodes@), c as int), +{ + match nodes.get(c) { + Some(x) => { + proof { + let a = arena_of(nodes@); + assert(a.nodes[c as int] == nodes@[c as int]@); + } + !matches!(x.kind, NodeKind::LabelledStatement) + }, + None => false, + } +} + +/// The token a node starts at, found from its start. +pub(crate) fn node_token(nodes: &[Node], toks: &[DTok], by_start: &[usize], m: usize) -> (r: Option) + requires + m < nodes.len(), + ensures + match r { + Some(x) => x < toks.len() && toks@[x as int].start as int == nodes@[m as int].start as int, + None => true, + }, +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let x = match by_start.get(nd.start) { + Some(x) => *x, + None => { + return None; + }, + }; + match toks.get(x) { + Some(t) => if t.start == nd.start { + Some(x) + } else { + None + }, + None => None, + } +} + +/// §8.6.4 the node `c` is a `~simple~` assignment target (through parentheses). +fn target_simple( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + by_start: &[usize], + c0: usize, + strict: bool, + Ghost(ts): Ghost, +) -> (r: bool) + requires + c0 < nodes.len(), + ast_wf(arena_of(nodes@)), + toks_match(src@, toks@, ts), + starts_inc(ts), + ensures + r ==> target_type(arena_of(nodes@), ts, c0 as int, strict) == TargetType::Simple, +{ + let _ = src.len(); + let ghost a = arena_of(nodes@); + let mut c = c0; + loop + invariant + c < nodes.len(), + a == arena_of(nodes@), + ast_wf(a), + toks_match(src@, toks@, ts), + starts_inc(ts), + target_type(a, ts, c0 as int, strict) == target_type(a, ts, c as int, strict), + decreases c, + { + let nd = match nodes.get(c) { + Some(x) => x, + None => { + return false; + }, + }; + proof { + assert(a.nodes[c as int] == nodes@[c as int]@); + assert(nd == &nodes@[c as int]); + assert(a.nodes[c as int].kind == nd.kind); + } + match nd.kind { + NodeKind::MemberExpression | NodeKind::SuperProperty => { + return true; + }, + NodeKind::IdentifierReference => { + let x = match node_token(nodes, toks, by_start, c) { + Some(x) => x, + None => { + return false; + }, + }; + let t = match toks.get(x) { + Some(t) => t, + None => { + return false; + }, + }; + let bad = strict && (seq_eq(t.name.as_slice(), &[0x65, 0x76, 0x61, 0x6C]) + || seq_eq(t.name.as_slice(), &[0x61, 0x72, 0x67, 0x75, 0x6D, 0x65, 0x6E, 0x74, 0x73])); + proof { + lemma_words_early(); + assert(t == &toks@[x as int]); + assert(dtok_matches(src@, toks@[x as int], ts.toks[x as int])); + lemma_node_tok_at(a, ts, c as int, x as int); + assert(name_of(a, ts, c as int) =~= t.name@); + assert(a.nodes.len() == nodes@.len()); + if !bad { + assert(a.nodes[c as int].kind == NodeKind::IdentifierReference); + assert(!(strict && (name_of(a, ts, c as int) =~= sv("eval"@) || name_of( + a, + ts, + c as int, + ) =~= sv("arguments"@)))); + assert(target_type(a, ts, c as int, strict) == TargetType::Simple); + } + } + return !bad; + }, + NodeKind::ParenthesizedExpression => { + let k0 = match nd.children.as_slice().get(ZERO) { + Some(k) => *k as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[c as int].children[0] == k0 as int); + } + if k0 >= c { + return false; + } + c = k0; + }, + _ => { + return false; + }, + } + } +} + +/// The kinds `early_local` has an arm for (other kinds have none: it is `true`). +fn has_arm(k: NodeKind) -> (r: bool) + ensures + !r ==> (k != NodeKind::IdentifierReference && k != NodeKind::BindingIdentifier && k + != NodeKind::LabelIdentifier && k != NodeKind::NumericLiteral && k + != NodeKind::StringLiteral && k != NodeKind::RegularExpressionLiteral && k + != NodeKind::TemplateElement && k != NodeKind::ObjectLiteral && k + != NodeKind::CoverInitializedName && k != NodeKind::SuperProperty && k + != NodeKind::SuperCall && k != NodeKind::NewTarget && k != NodeKind::ImportMeta && k + != NodeKind::MemberExpression && k != NodeKind::OptionalChain && k + != NodeKind::RelationalExpression && k != NodeKind::UnaryExpression && k + != NodeKind::UpdateExpression && k != NodeKind::AssignmentExpression && k + != NodeKind::AssignmentProperty && k != NodeKind::AssignmentElement && k + != NodeKind::AssignmentRestElement && k != NodeKind::AssignmentRestProperty && k + != NodeKind::YieldExpression && k != NodeKind::AwaitExpression && k + != NodeKind::BlockStatement && k != NodeKind::LexicalDeclaration && k + != NodeKind::IfStatement && k != NodeKind::DoWhileStatement && k + != NodeKind::WhileStatement && k != NodeKind::ForStatement && k + != NodeKind::ForInStatement && k != NodeKind::ForOfStatement && k + != NodeKind::ContinueStatement && k != NodeKind::BreakStatement && k + != NodeKind::WithStatement && k != NodeKind::SwitchStatement && k + != NodeKind::LabelledStatement && k != NodeKind::Catch && k != NodeKind::FunctionBody + && k != NodeKind::FunctionDeclaration && k != NodeKind::FunctionExpression && k + != NodeKind::GeneratorDeclaration && k != NodeKind::GeneratorExpression && k + != NodeKind::AsyncFunctionDeclaration && k != NodeKind::AsyncFunctionExpression && k + != NodeKind::AsyncGeneratorDeclaration && k != NodeKind::AsyncGeneratorExpression && k + != NodeKind::ArrowFunction && k != NodeKind::AsyncArrowFunction && k + != NodeKind::MethodDefinition && k != NodeKind::ClassBody && k != NodeKind::Script + && k != NodeKind::Module && k != NodeKind::WithClause && k != NodeKind::ExportDeclaration + && k != NodeKind::ExportSpecifier && k != NodeKind::ImportSpecifier), +{ + matches!( + k, + NodeKind::IdentifierReference | NodeKind::BindingIdentifier | NodeKind::LabelIdentifier + | NodeKind::NumericLiteral | NodeKind::StringLiteral | NodeKind::RegularExpressionLiteral + | NodeKind::TemplateElement | NodeKind::ObjectLiteral | NodeKind::CoverInitializedName + | NodeKind::SuperProperty | NodeKind::SuperCall | NodeKind::NewTarget | NodeKind::ImportMeta + | NodeKind::MemberExpression | NodeKind::OptionalChain | NodeKind::RelationalExpression + | NodeKind::UnaryExpression | NodeKind::UpdateExpression | NodeKind::AssignmentExpression + | NodeKind::AssignmentProperty | NodeKind::AssignmentElement | NodeKind::AssignmentRestElement + | NodeKind::AssignmentRestProperty | NodeKind::YieldExpression | NodeKind::AwaitExpression + | NodeKind::BlockStatement | NodeKind::LexicalDeclaration | NodeKind::IfStatement + | NodeKind::DoWhileStatement | NodeKind::WhileStatement | NodeKind::ForStatement + | NodeKind::ForInStatement | NodeKind::ForOfStatement | NodeKind::ContinueStatement + | NodeKind::BreakStatement | NodeKind::WithStatement | NodeKind::SwitchStatement + | NodeKind::LabelledStatement | NodeKind::Catch | NodeKind::FunctionBody + | NodeKind::FunctionDeclaration | NodeKind::FunctionExpression | NodeKind::GeneratorDeclaration + | NodeKind::GeneratorExpression | NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncFunctionExpression | NodeKind::AsyncGeneratorDeclaration + | NodeKind::AsyncGeneratorExpression | NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction + | NodeKind::MethodDefinition | NodeKind::ClassBody | NodeKind::Script | NodeKind::Module + | NodeKind::WithClause | NodeKind::ExportDeclaration | NodeKind::ExportSpecifier + | NodeKind::ImportSpecifier + ) +} + +/// An item whose `LexicallyDeclaredNames` is empty, in a list that is `top` (a script's) or not. +pub open spec fn lex_empty_kind(k: NodeKind, top: bool) -> bool { + !(k == NodeKind::LexicalDeclaration || k == NodeKind::ClassDeclaration || k + == NodeKind::ImportDeclaration || k == NodeKind::ExportDeclaration || (!top && (k + == NodeKind::FunctionDeclaration || k == NodeKind::GeneratorDeclaration || k + == NodeKind::AsyncFunctionDeclaration || k == NodeKind::AsyncGeneratorDeclaration || k + == NodeKind::LabelledStatement))) +} + +/// The lexically declared names of a list of such items are none. +pub(crate) proof fn lemma_lex_list_empty(a: AstV, ts: Stream, n: int, ks: Seq, k: int, top: bool) + requires + forall|j: int| 0 <= j < ks.len() ==> lex_empty_kind(kd(a, #[trigger] ks[j]), top), + ensures + lex_names_list(a, ts, n, ks, k, top) =~= Seq::empty(), + decreases ks.len() - k, +{ + if 0 <= k < ks.len() { + lemma_lex_list_empty(a, ts, n, ks, k + 1, top); + if 0 <= ks[k] < n { + assert(lex_empty_kind(kd(a, ks[k]), top)); + assert(lex_names_item(a, ts, ks[k], top) =~= Seq::empty()); + } + } +} + +/// `list_early` of a list whose items declare nothing lexically. +pub(crate) proof fn lemma_list_early_empty(a: AstV, ts: Stream, n: int, ks: Seq, strict: bool, top: bool) + requires + forall|j: int| 0 <= j < ks.len() ==> lex_empty_kind(kd(a, #[trigger] ks[j]), top), + ensures + list_early(a, ts, n, ks, strict, top), +{ + lemma_lex_list_empty(a, ts, n, ks, 0, top); + let lex = lex_names_list(a, ts, n, ks, 0, top); + assert(lex =~= Seq::empty()); + assert(!has_forbidden_duplicate(lex, strict)); + assert(plain_names(lex) =~= Seq::empty()); + assert(!intersects(plain_names(lex), if top { + top_var_names_list(a, ts, n, ks, 0) + } else { + var_names_list(a, ts, n, ks, 0) + })); +} + +/// The kind of child `i` of node `m`, `Script` where there is none (`spec::kd(a, kid(ks, i))`). +pub(crate) fn kid_kind(nodes: &[Node], m: usize, i: usize) -> (r: Option) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + match r { + Some(k) => i < arena_of(nodes@).nodes[m as int].children.len() && kd( + arena_of(nodes@), + arena_of(nodes@).nodes[m as int].children[i as int], + ) == k, + None => true, + }, +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let c = match nd.children.as_slice().get(i) { + Some(c) => *c as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children[i as int] == c as int); + lemma_child_before(a, m as int, i as int); + } + if c >= m { + return None; + } + match nodes.get(c) { + Some(x) => { + proof { + let a = arena_of(nodes@); + assert(a.nodes[c as int] == nodes@[c as int]@); + } + Some(x.kind) + }, + None => None, + } +} + +/// `early_local` of node `m` in `e`, for the rules this module covers (`false` otherwise). +#[verifier::rlimit(400)] +fn early_local_exec( + tk: &Tk, + nodes: &[Node], + m: usize, + e: &EEnv, + Ghost(ts): Ghost, +) -> (r: bool) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + tk.bn.len() == nodes.len(), + bn_inv(arena_of(nodes@), ts, tk.toks@, tk.bn@, nodes.len() as int), + names_inv(tk, nodes@, ts), + ensures + r ==> early_local(arena_of(nodes@), ts, m as int, eenv_of(*e)), +{ + let src = tk.src; + let toks = tk.toks; + let by_start = tk.by_start; + let ghost a = arena_of(nodes@); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].kind == nd.kind); + assert(a.nodes[m as int].op == nd.op); + assert(a.nodes[m as int].flag == nd.flag); + } + let kind = nd.kind; + if !has_arm(kind) { + return true; + } + match kind { + NodeKind::NumericLiteral | NodeKind::StringLiteral | NodeKind::TemplateElement + | NodeKind::IdentifierReference | NodeKind::BindingIdentifier | NodeKind::LabelIdentifier => { + let x = match node_token(nodes, toks, by_start, m) { + Some(x) => x, + None => { + return false; + }, + }; + let t = match toks.get(x) { + Some(t) => t, + None => { + return false; + }, + }; + proof { + assert(t == &toks@[x as int]); + assert(dtok_matches(src@, toks@[x as int], ts.toks[x as int])); + lemma_node_tok_at(a, ts, m as int, x as int); + } + match kind { + NodeKind::NumericLiteral | NodeKind::StringLiteral => !(e.strict && t.legacy), + NodeKind::TemplateElement => e.tagged || !t.invalid_escape, + _ => { + let ok = ident_early( + t.name.as_slice(), + matches!(kind, NodeKind::BindingIdentifier), + matches!(kind, NodeKind::IdentifierReference), + e, + ); + proof { + lemma_ident_rule(a, ts, m as int, eenv_of(*e)); + assert(name_of(a, ts, m as int) =~= t.name@); + } + ok + }, + } + }, + NodeKind::SuperProperty => e.super_prop, + NodeKind::SuperCall => e.super_call, + NodeKind::NewTarget => e.new_target, + NodeKind::ImportMeta => e.module, + NodeKind::YieldExpression | NodeKind::AwaitExpression => !e.no_yield_await, + NodeKind::MemberExpression => !matches!(nd.op, Op::PrivateDot), + NodeKind::OptionalChain => !matches!(nd.op, Op::ChainTemplate | Op::ChainPrivateDot), + NodeKind::RelationalExpression => match kid_kind(nodes, m, 0) { + Some(k0) => !matches!(k0, NodeKind::PrivateIdentifier), + None => false, + }, + NodeKind::UnaryExpression => !matches!(nd.op, Op::Delete) || !e.strict, + NodeKind::UpdateExpression => { + let c = match nd.children.as_slice().get(ZERO) { + Some(c) => *c as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[0] == c as int); + lemma_child_before(a, m as int, 0); + } + c < m && target_simple(src, nodes, toks, by_start, c, e.strict, Ghost(ts)) + }, + NodeKind::AssignmentExpression => { + let c = match nd.children.as_slice().get(ZERO) { + Some(c) => *c as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[0] == c as int); + lemma_child_before(a, m as int, 0); + } + if c >= m { + return false; + } + let ck = match nodes.get(c) { + Some(x) => x.kind, + None => { + return false; + }, + }; + proof { + assert(a.nodes[c as int] == nodes@[c as int]@); + } + (matches!(nd.op, Op::Assign) + && matches!(ck, NodeKind::ObjectAssignmentPattern | NodeKind::ArrayAssignmentPattern)) + || target_simple(src, nodes, toks, by_start, c, e.strict, Ghost(ts)) + }, + NodeKind::IfStatement => { + let (k1, k2) = match (kid_kind(nodes, m, 1), kid_kind(nodes, m, 2)) { + (Some(x), Some(y)) => (x, y), + _ => { + return false; + }, + }; + let c1 = match nd.children.as_slice().get(1) { + Some(c) => *c as usize, + None => { + return false; + }, + }; + let c2 = match nd.children.as_slice().get(2) { + Some(c) => *c as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[1] == c1 as int); + assert(a.nodes[m as int].children[2] == c2 as int); + lemma_child_before(a, m as int, 1); + lemma_child_before(a, m as int, 2); + } + c1 < m && c2 < m && not_labelled(nodes, c1) && not_labelled(nodes, c2) && !(e.strict + && (matches!(k1, NodeKind::FunctionDeclaration) || matches!(k2, NodeKind::FunctionDeclaration))) + }, + NodeKind::WhileStatement | NodeKind::WithStatement | NodeKind::DoWhileStatement => { + let which: usize = if matches!(kind, NodeKind::DoWhileStatement) { 0 } else { 1 }; + let c = match nd.children.as_slice().get(which) { + Some(c) => *c as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[which as int] == c as int); + lemma_child_before(a, m as int, which as int); + } + c < m && not_labelled(nodes, c) && (!matches!(kind, NodeKind::WithStatement) || !e.strict) + }, + NodeKind::ForStatement => { + let c = match nd.children.as_slice().get(3) { + Some(c) => *c as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[3] == c as int); + lemma_child_before(a, m as int, 3); + } + c < m && not_labelled(nodes, c) && for_lex_exec(tk, nodes, m, Ghost(ts)) + }, + NodeKind::ContinueStatement | NodeKind::BreakStatement => { + let k0 = match kid_kind(nodes, m, 0) { + Some(k) => k, + None => { + return false; + }, + }; + if matches!(k0, NodeKind::LabelIdentifier) { + return false; + } + if matches!(kind, NodeKind::ContinueStatement) { + e.in_loop + } else { + e.in_breakable + } + }, + NodeKind::BlockStatement => list_early_exec(tk, nodes, m, e.strict, false, Ghost(ts)), + NodeKind::Script => list_early_exec(tk, nodes, m, e.strict, true, Ghost(ts)), + NodeKind::ObjectLiteral => object_literal_exec(tk, nodes, m, Ghost(ts)), + NodeKind::FunctionBody => list_early_exec(tk, nodes, m, e.strict, true, Ghost(ts)), + NodeKind::LexicalDeclaration => lexical_decl_exec(tk, nodes, m, Ghost(ts)), + NodeKind::Catch => catch_exec(tk, nodes, m, Ghost(ts)), + NodeKind::FunctionDeclaration | NodeKind::FunctionExpression | NodeKind::GeneratorDeclaration + | NodeKind::GeneratorExpression | NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncFunctionExpression | NodeKind::AsyncGeneratorDeclaration + | NodeKind::AsyncGeneratorExpression | NodeKind::MethodDefinition => { + let (p, b) = match (nd.children.as_slice().get(1), nd.children.as_slice().get(2)) { + (Some(p), Some(b)) => (*p as usize, *b as usize), + _ => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[1] == p as int); + assert(a.nodes[m as int].children[2] == b as int); + lemma_child_before(a, m as int, 1); + lemma_child_before(a, m as int, 2); + } + function_early_exec( + tk, + nodes, + p, + Some(b), + matches!(kind, NodeKind::MethodDefinition), + e.strict, + Ghost(ts), + ) + }, + NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction => { + let (p, b) = match (nd.children.as_slice().get(ZERO), nd.children.as_slice().get(1)) { + (Some(p), Some(b)) => (*p as usize, *b as usize), + _ => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[0] == p as int); + assert(a.nodes[m as int].children[1] == b as int); + lemma_child_before(a, m as int, 0); + lemma_child_before(a, m as int, 1); + } + let bk = match kid_kind(nodes, m, 1) { + Some(k) => k, + None => { + return false; + }, + }; + function_early_exec( + tk, + nodes, + p, + if matches!(bk, NodeKind::FunctionBody) { + Some(b) + } else { + None + }, + true, + e.strict, + Ghost(ts), + ) + }, + _ => false, + } +} + + +/// [`check_early`] on the canonical stream of the executable tokens and gaps +/// ([`canon_stream`]), which needs no ghost argument. +pub fn check_early_canon( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + module: bool, +) -> (r: Derivation) + requires + ast_wf(arena_of(nodes@)), + gaps@.len() == toks@.len() + 1, + ensures + r is Verified ==> early_errors_ok( + arena_of(nodes@), + canon_stream(src@, toks@, gaps@), + goal_of(module), + ), +{ + let _ = gaps.len(); + let nt = toks.len(); + let mut k: usize = 0; + while k < nt + invariant + nt == toks.len(), + k <= nt, + forall|x: int| + 0 <= x < k ==> (#[trigger] toks@[x]).start <= toks@[x].end && toks@[x].end + <= src@.len(), + decreases nt - k, + { + let t = match toks.get(k) { + Some(t) => t, + None => { + return Derivation::Unsupported; + }, + }; + if t.start > t.end || t.end > src.len() { + return Derivation::Unsupported; + } + k = k.saturating_add(1); + } + let ghost ts = canon_stream(src@, toks@, gaps@); + proof { + assert(toks_match(src@, toks@, ts)) by { + assert(toks@.len() == ts.toks.len()); + assert forall|x: int| 0 <= x < toks@.len() implies dtok_matches( + src@, + toks@[x], + #[trigger] ts.toks[x], + ) by { + assert(ts.toks[x] == canon_tok(src@, toks@[x])); + } + } + } + check_early(src, nodes, toks, module, Ghost(ts)) +} + +/// Checks that the arena has no early error (`spec::early_errors_ok`), for the rules and +/// node kinds listed at the top of the module, given the executable tokens (the +/// assumption `toks_match`). `Failed` is never returned; what is not covered is +/// `Unsupported`. +#[verifier::rlimit(400)] +pub fn check_early( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + module: bool, + Ghost(ts): Ghost, +) -> (r: Derivation) + requires + ast_wf(arena_of(nodes@)), + toks_match(src@, toks@, ts), + ensures + r is Verified ==> early_errors_ok(arena_of(nodes@), ts, goal_of(module)), +{ + let n = nodes.len(); + if n == 0 || module { + return Derivation::Unsupported; + } + let last = n.saturating_sub(1); + let nt = toks.len(); + // The tokens start at increasing positions. + let mut k: usize = 0; + while k < nt + invariant + nt == toks.len(), + k <= nt, + toks_match(src@, toks@, ts), + forall|x: int, y: int| + 0 <= x < y < k ==> (#[trigger] toks@[x]).start < (#[trigger] toks@[y]).start, + decreases nt - k, + { + if k > 0 { + let (p, t) = match (toks.get(k.saturating_sub(1)), toks.get(k)) { + (Some(p), Some(t)) => (p, t), + _ => { + return Derivation::Unsupported; + }, + }; + if p.start >= t.start { + return Derivation::Unsupported; + } + } + k = k.saturating_add(1); + } + proof { + assert(starts_inc(ts)) by { + assert forall|x: int, y: int| 0 <= x < y < tn(ts) implies (#[trigger] ts.toks[x]).start + < (#[trigger] ts.toks[y]).start by { + assert(dtok_matches(src@, toks@[x], ts.toks[x])); + assert(dtok_matches(src@, toks@[y], ts.toks[y])); + } + } + } + // A hint for finding a token from its start. + let size = src.len().saturating_add(1); + let mut by_start: Vec = Vec::new(); + let mut q: usize = 0; + while q < size + invariant + q <= size, + by_start.len() == q, + decreases size - q, + { + by_start.push(usize::MAX); + q = q.saturating_add(1); + } + let mut x: usize = 0; + while x < nt + invariant + x <= nt, + nt == toks.len(), + by_start.len() == size, + decreases nt - x, + { + let t = match toks.get(x) { + Some(t) => t, + None => { + return Derivation::Unsupported; + }, + }; + if t.start < by_start.len() { + by_start.set(t.start, x); + } + x = x.saturating_add(1); + } + // The names the declarations bind. + let bn = match bound_names_all(src, nodes, toks, &by_start, Ghost(ts)) { + Some(bn) => bn, + None => { + return Derivation::Unsupported; + }, + }; + let empty = Names { lf: Vec::new(), lt: Vec::new(), vf: Vec::new(), vt: Vec::new() }; + let tk0 = Tk { src, toks, by_start: by_start.as_slice(), bn: bn.as_slice(), nm: &empty }; + let nm = match decl_names_all(&tk0, nodes, Ghost(ts)) { + Some(nm) => nm, + None => { + return Derivation::Unsupported; + }, + }; + let tk = Tk { src, toks, by_start: by_start.as_slice(), bn: bn.as_slice(), nm: &nm }; + proof { + assert(names_inv(&tk, nodes@, ts)); + } + // The root's environment: a script, strict if its Directive Prologue has a Use Strict Directive. + let root_strict = match use_strict_exec(src, nodes, toks, by_start.as_slice(), last, Ghost(ts)) { + Some(v) => v, + None => { + return Derivation::Unsupported; + }, + }; + let root_e = EEnv { + strict: root_strict, + module: false, + yield_: false, + await_: false, + in_loop: false, + in_breakable: false, + super_prop: false, + super_call: false, + new_target: false, + no_arguments: false, + no_yield_await: false, + tagged: false, + derived: false, + }; + let mut envs: Vec = Vec::new(); + let mut q: usize = 0; + while q < n + invariant + q <= n, + envs.len() == q, + decreases n - q, + { + envs.push(root_e); + q = q.saturating_add(1); + } + let ghost a = arena_of(nodes@); + proof { + assert(a.root == nodes.len() as int - 1); + } + // Untrusted: the environment each child is to be checked in, from the root down. + let mut i = n; + while i > 0 + invariant + i <= n, + n == nodes.len(), + envs.len() == n, + ast_wf(a), + a == arena_of(nodes@), + toks_match(src@, toks@, ts), + tk.src@ == src@, + tk.toks@ == toks@, + starts_inc(ts), + decreases i, + { + i = i.saturating_sub(1); + let nd = match nodes.get(i) { + Some(x) => x, + None => { + return Derivation::Unsupported; + }, + }; + let env_i = match envs.get(i) { + Some(x) => *x, + None => { + return Derivation::Unsupported; + }, + }; + let len = nd.children.len(); + let mut kk: usize = 0; + while kk < len + invariant + kk <= len, + len == nd.children.len(), + envs.len() == n, + n == nodes.len(), + i < nodes.len(), + nd == &nodes@[i as int], + ast_wf(a), + a == arena_of(nodes@), + toks_match(src@, toks@, ts), + tk.src@ == src@, + tk.toks@ == toks@, + starts_inc(ts), + len == nodes@[i as int].children.len(), + decreases len - kk, + { + if let (Some(ce), Some(c)) = ( + child_env_exec(&tk, nodes, i, kk, &env_i, Ghost(ts)), + nd.children.as_slice().get(kk), + ) { + let c = *c as usize; + if c < n { + envs.set(c, ce); + } + } + kk = kk.saturating_add(1); + } + } + // Bottom-up: each node's rules hold, and each child's recorded environment is the + // one `child_env` gives it. + let mut m: usize = 0; + while m < n + invariant + m <= n, + n == nodes.len(), + envs.len() == n, + ast_wf(a), + a == arena_of(nodes@), + toks_match(src@, toks@, ts), + tk.src@ == src@, + tk.toks@ == toks@, + tk.bn@ == bn@, + names_inv(&tk, nodes@, ts), + starts_inc(ts), + bn.len() == n, + bn_inv(a, ts, toks@, bn@, n as int), + forall|j: int| 0 <= j < m ==> early(a, ts, j, eenv_of(envs@[j])), + decreases n - m, + { + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return Derivation::Unsupported; + }, + }; + let env_m = match envs.get(m) { + Some(x) => *x, + None => { + return Derivation::Unsupported; + }, + }; + if !early_local_exec(&tk, nodes, m, &env_m, Ghost(ts)) { + return Derivation::Unsupported; + } + let len = nd.children.len(); + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == len); + } + let mut kk: usize = 0; + while kk < len + invariant + kk <= len, + len == nd.children.len(), + m < n, + n == nodes.len(), + envs.len() == n, + nd == &nodes@[m as int], + len == a.nodes[m as int].children.len(), + ast_wf(a), + a == arena_of(nodes@), + toks_match(src@, toks@, ts), + tk.src@ == src@, + tk.toks@ == toks@, + tk.bn@ == bn@, + names_inv(&tk, nodes@, ts), + starts_inc(ts), + forall|j: int| 0 <= j < m ==> early(a, ts, j, eenv_of(envs@[j])), + forall|q: int| + 0 <= q < kk ==> { + let c = #[trigger] a.nodes[m as int].children[q]; + 0 <= c < m && early(a, ts, c, child_env(a, ts, m as int, q, eenv_of(env_m))) + }, + decreases len - kk, + { + let c = match nd.children.as_slice().get(kk) { + Some(c) => *c as usize, + None => { + return Derivation::Unsupported; + }, + }; + proof { + assert(a.nodes[m as int].children[kk as int] == c as int); + lemma_child_before(a, m as int, kk as int); + } + let ce = match child_env_exec(&tk, nodes, m, kk, &env_m, Ghost(ts)) { + Some(ce) => ce, + None => { + return Derivation::Unsupported; + }, + }; + let have = match envs.get(c) { + Some(x) => *x, + None => { + return Derivation::Unsupported; + }, + }; + if c >= m || !eenv_eq(&have, &ce) { + return Derivation::Unsupported; + } + kk = kk.saturating_add(1); + } + proof { + assert(early(a, ts, m as int, eenv_of(envs@[m as int]))); + } + m = m.saturating_add(1); + } + let root_have = match envs.get(last) { + Some(x) => *x, + None => { + return Derivation::Unsupported; + }, + }; + if !eenv_eq(&root_have, &root_e) { + return Derivation::Unsupported; + } + proof { + let g = goal_of(module); + assert(g == SourceGoal::Script); + assert(a.root == last as int); + assert(root_env(a, ts, g) == eenv_of(root_e)); + assert(a.root == last as int); + assert(early(a, ts, a.root, eenv_of(envs@[a.root]))); + } + Derivation::Verified +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/arena_m.rs b/crates/clazel-js-syntax/src/arena_m.rs new file mode 100644 index 000000000..e4705c89e --- /dev/null +++ b/crates/clazel-js-syntax/src/arena_m.rs @@ -0,0 +1,1556 @@ +//! Checks of [`spec::d`] for the module items of §16.2: `import` and `export` in all +//! their forms, with import attributes, `export default`, `export * as ns` and string +//! `ModuleExportName`s. Dispatched from [`crate::arena_d::check_derivation`]; each +//! `check_*` has the contract of its siblings there (`node_done` of the node, in its +//! context, only if the specification's `d_module` arm follows from what the children +//! recorded and the executable tokens). The assumptions are those of [`crate::arena_d`]. + +use crate::arena_d::*; +use crate::arena_e::*; +use crate::ast::*; +use crate::spec::*; +use vstd::prelude::*; + +verus! { + +/// The words of the module items, as code points. +pub(crate) proof fn lemma_words_m() + ensures + cps("as"@) =~= seq![0x61u32, 0x73], + cps("from"@) =~= seq![0x66u32, 0x72, 0x6F, 0x6D], + cps("with"@) =~= seq![0x77u32, 0x69, 0x74, 0x68], + cps("default"@) =~= seq![0x64u32, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74], + cps("export"@) =~= seq![0x65u32, 0x78, 0x70, 0x6F, 0x72, 0x74], + cps("*"@) =~= seq![0x2Au32], + cps("}"@) =~= seq![0x7Du32], + cps(":"@) =~= seq![0x3Au32], +{ + reveal_strlit("as"); + reveal_strlit("from"); + reveal_strlit("with"); + reveal_strlit("default"); + reveal_strlit("export"); + reveal_strlit("*"); + reveal_strlit("}"); + reveal_strlit(":"); + assert("as"@ =~= seq!['a', 's']); + assert("from"@ =~= seq!['f', 'r', 'o', 'm']); + assert("with"@ =~= seq!['w', 'i', 't', 'h']); + assert("default"@ =~= seq!['d', 'e', 'f', 'a', 'u', 'l', 't']); + assert("export"@ =~= seq!['e', 'x', 'p', 'o', 'r', 't']); + assert("*"@ =~= seq!['*']); + assert("}"@ =~= seq!['}']); + assert(":"@ =~= seq![':']); +} + +/// The executable form of `is_module_export_name_kind`. +fn name_kind(k: NodeKind) -> (r: bool) + ensures + r == is_module_export_name_kind(k), +{ + matches!(k, NodeKind::IdentifierName | NodeKind::StringLiteral) +} + +// --------------------------------------------------------------------- +// Small nodes: `* as ns`, `a as b`, `k: "v"`. +// --------------------------------------------------------------------- + +/// `NameSpaceImport : * as ImportedBinding`. +#[verifier::spinoff_prover] +proof fn lemma_ns_done(a: AstV, ts: Stream, cx: Ctx, m: int, c: int, x: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + 0 <= x, + x + 3 <= tn(ts), + a.nodes[m].kind == NodeKind::NameSpaceImport, + a.nodes[m].children =~= seq![c], + is_p(ts, x, "*"@), + is_kw(ts, x + 1, "as"@), + kd(a, c) == NodeKind::BindingIdentifier, + node_done(a, ts, c, cx, x + 2, x + 3), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[x + 2].end, + ensures + node_done(a, ts, m, cx, x, x + 3), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, x + 3)); + assert(a.nodes[m].children[0] == c); + assert(dc(a, ts, m, c, cx, x + 2, x + 3)); + assert(d_module(a, ts, m, cx, x, x + 3)); + lemma_ed_of(a, ts, m, x, x + 3); +} + +/// §16.2.2 `* as ImportedBinding`. +#[verifier::spinoff_prover] +pub(crate) fn check_ns_import( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_m(); + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::NameSpaceImport) || nd.children.len() != 1 { + return None; + } + let c = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_c, hi_c, _) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if x > usize::MAX - 8 { + return None; + } + let x1 = x.saturating_add(1); + let x2 = x.saturating_add(2); + let x3 = x.saturating_add(3); + if !punct_at(src, nodes, toks, gaps, x, 0x2A, Ghost(GW { g, w: "*"@ })) + || !word_at(src, nodes, toks, gaps, x1, &[0x61, 0x73], Ghost(GW { g, w: "as"@ })) + || !matches!(kind_at(nodes, c), NodeKind::BindingIdentifier) || lo_c != x2 || hi_c != x3 + || !end_ok(src, nodes, toks, gaps, nd.end, x3, Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(x1 as int == x as int + 1); + assert(x2 as int == x as int + 2); + assert(x3 as int == x as int + 3); + assert(a.nodes[m as int].children =~= seq![c as int]) by { + assert(a.nodes[m as int].children.len() == 1); + } + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, 0); + lemma_ns_done(a, g.ts, g.cx, m as int, c as int, x as int); + } + Some((x, x3, -100)) +} + +/// A node of two children `name`, `x`: one token each, with the token between (`as` or `:`) or +/// nothing: `ImportSpecifier`, `ImportAttribute` and `ExportSpecifier`. +#[verifier::spinoff_prover] +proof fn lemma_pair_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c0: int, + c1: int, + x: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c0 < m, + 0 <= c1 < m, + 0 <= x, + x < j <= tn(ts), + a.nodes[m].children =~= seq![c0, c1], + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + match a.nodes[m].kind { + NodeKind::ImportSpecifier => kd(a, c1) == NodeKind::BindingIdentifier && (if kd(a, c0) + == NodeKind::Absent { + j == x + 1 && d(a, ts, c0, cx, x, x) && node_done(a, ts, c1, cx, x, j) + } else { + is_module_export_name_kind(kd(a, c0)) && is_kw(ts, x + 1, "as"@) && j == x + 3 + && node_done(a, ts, c0, cx, x, x + 1) && node_done(a, ts, c1, cx, x + 2, j) + }), + NodeKind::ImportAttribute => is_module_export_name_kind(kd(a, c0)) && kd(a, c1) + == NodeKind::StringLiteral && is_p(ts, x + 1, ":"@) && j == x + 3 && node_done( + a, + ts, + c0, + cx, + x, + x + 1, + ) && node_done(a, ts, c1, cx, x + 2, j), + NodeKind::ExportSpecifier => is_module_export_name_kind(kd(a, c0)) && node_done( + a, + ts, + c0, + cx, + x, + x + 1, + ) && (if kd(a, c1) == NodeKind::Absent { + j == x + 1 && d(a, ts, c1, cx, x + 1, x + 1) + } else { + is_module_export_name_kind(kd(a, c1)) && is_kw(ts, x + 1, "as"@) && j == x + 3 + && node_done(a, ts, c1, cx, x + 2, j) + }), + _ => false, + }, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c0); + assert(a.nodes[m].children[1] == c1); + match a.nodes[m].kind { + NodeKind::ImportSpecifier => { + if kd(a, c0) == NodeKind::Absent { + assert(dc(a, ts, m, c0, cx, x, x)); + assert(dc(a, ts, m, c1, cx, x, j)); + } else { + assert(dc(a, ts, m, c0, cx, x, x + 1)); + assert(dc(a, ts, m, c1, cx, x + 2, j)); + } + }, + NodeKind::ImportAttribute => { + assert(dc(a, ts, m, c0, cx, x, x + 1)); + assert(dc(a, ts, m, c1, cx, x + 2, j)); + }, + _ => { + assert(dc(a, ts, m, c0, cx, x, x + 1)); + if kd(a, c1) == NodeKind::Absent { + assert(dc(a, ts, m, c1, cx, x + 1, x + 1)); + } else { + assert(dc(a, ts, m, c1, cx, x + 2, j)); + } + }, + } + assert(d_module(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §16.2.2 `ImportSpecifier`, `ImportAttribute`; §16.2.3 `ExportSpecifier`. +#[verifier::spinoff_prover] +pub(crate) fn check_pair( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_m(); + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let kind = nd.kind; + if !matches!(kind, NodeKind::ImportSpecifier | NodeKind::ImportAttribute | NodeKind::ExportSpecifier) + || nd.children.len() != 2 { + return None; + } + let c0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let c1 = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if x > usize::MAX - 8 { + return None; + } + let x1 = x.saturating_add(1); + let x2 = x.saturating_add(2); + let x3 = x.saturating_add(3); + let k0 = kind_at(nodes, c0); + let k1 = kind_at(nodes, c1); + let a0 = matches!(k0, NodeKind::Absent); + let a1 = matches!(k1, NodeKind::Absent); + let j: usize; + match kind { + NodeKind::ImportSpecifier => { + if !matches!(k1, NodeKind::BindingIdentifier) { + return None; + } + let (lo1, hi1, _) = match done_at(done, c1) { + Some(r) => r, + None => { + return None; + }, + }; + if a0 { + if !check_absent(src, nodes, toks, gaps, c0, x, Ghost(g)) || lo1 != x || hi1 != x1 { + return None; + } + j = x1; + } else { + let (lo0, hi0, _) = match done_at(done, c0) { + Some(r) => r, + None => { + return None; + }, + }; + if !name_kind(k0) || lo0 != x || hi0 != x1 || lo1 != x2 || hi1 != x3 + || !word_at(src, nodes, toks, gaps, x1, &[0x61, 0x73], Ghost(GW { g, w: "as"@ })) { + return None; + } + j = x3; + } + }, + NodeKind::ImportAttribute => { + let (lo0, hi0, _) = match done_at(done, c0) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo1, hi1, _) = match done_at(done, c1) { + Some(r) => r, + None => { + return None; + }, + }; + if !name_kind(k0) || !matches!(k1, NodeKind::StringLiteral) || lo0 != x || hi0 != x1 + || lo1 != x2 || hi1 != x3 + || !punct_at(src, nodes, toks, gaps, x1, 0x3A, Ghost(GW { g, w: ":"@ })) { + return None; + } + j = x3; + }, + _ => { + let (lo0, hi0, _) = match done_at(done, c0) { + Some(r) => r, + None => { + return None; + }, + }; + if !name_kind(k0) || lo0 != x || hi0 != x1 { + return None; + } + if a1 { + if !check_absent(src, nodes, toks, gaps, c1, x1, Ghost(g)) { + return None; + } + j = x1; + } else { + let (lo1, hi1, _) = match done_at(done, c1) { + Some(r) => r, + None => { + return None; + }, + }; + if !name_kind(k1) || lo1 != x2 || hi1 != x3 + || !word_at(src, nodes, toks, gaps, x1, &[0x61, 0x73], Ghost(GW { g, w: "as"@ })) { + return None; + } + j = x3; + } + }, + } + if !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(x1 as int == x as int + 1); + assert(x2 as int == x as int + 2); + assert(x3 as int == x as int + 3); + assert(a.nodes[m as int].children =~= seq![c0 as int, c1 as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + assert(kd(a, c0 as int) == k0); + assert(kd(a, c1 as int) == k1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c0 as int, g.cx, 0); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c1 as int, g.cx, 1); + lemma_pair_done(a, g.ts, g.cx, m as int, c0 as int, c1 as int, x as int, j as int); + } + Some((x, j, -100)) +} + +// --------------------------------------------------------------------- +// Braced lists: `{ a, b as c }`, `with { type: "json" }`. +// --------------------------------------------------------------------- + +/// `NamedImports`, `NamedExports`: `{ list }`; `WithClause`: `with { list }`. +#[verifier::spinoff_prover] +proof fn lemma_braced_done(a: AstV, ts: Stream, cx: Ctx, m: int, x: int, s: int, e: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= x, + s == (if a.nodes[m].kind == NodeKind::WithClause { + x + 2 + } else { + x + 1 + }), + s <= e < tn(ts), + a.nodes[m].kind == NodeKind::NamedImports || a.nodes[m].kind == NodeKind::NamedExports + || a.nodes[m].kind == NodeKind::WithClause, + dsep(a, ts, m, a.nodes[m].children, 0, cx, s, e, true), + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> list_item_ok( + a, + #[trigger] a.nodes[m].children[k], + if a.nodes[m].kind == NodeKind::NamedImports { + 11int + } else if a.nodes[m].kind == NodeKind::NamedExports { + 15int + } else { + 13int + }, + ), + a.nodes[m].kind == NodeKind::WithClause ==> (is_kw(ts, x, "with"@) && is_p(ts, x + 1, "{"@)), + a.nodes[m].kind != NodeKind::WithClause ==> is_p(ts, x, "{"@), + is_p(ts, e, "}"@), + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[e].end, + ensures + node_done(a, ts, m, cx, x, e + 1), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, e + 1)); + let kk = a.nodes[m].kind; + assert forall|k: int| 0 <= k < a.nodes[m].children.len() implies kd( + a, + #[trigger] a.nodes[m].children[k], + ) == (if kk == NodeKind::NamedImports { + NodeKind::ImportSpecifier + } else if kk == NodeKind::NamedExports { + NodeKind::ExportSpecifier + } else { + NodeKind::ImportAttribute + }) by { + assert(list_item_ok(a, a.nodes[m].children[k], if kk == NodeKind::NamedImports { + 11int + } else if kk == NodeKind::NamedExports { + 15int + } else { + 13int + })); + } + assert(all_kind2(a, a.nodes[m].children, 0, NodeKind::ImportSpecifier, NodeKind::ImportSpecifier) + || kk != NodeKind::NamedImports); + assert(d_module(a, ts, m, cx, x, e + 1)); + lemma_ed_of(a, ts, m, x, e + 1); +} + +/// §16.2.2 `NamedImports`, `WithClause`; §16.2.3 `NamedExports`. +#[verifier::spinoff_prover] +pub(crate) fn check_braced( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_m(); + lemma_punct_text(); + lemma_brace_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let kind = nd.kind; + let with = matches!(kind, NodeKind::WithClause); + if !(with || matches!(kind, NodeKind::NamedImports | NodeKind::NamedExports)) { + return None; + } + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if x > usize::MAX - 8 { + return None; + } + let x1 = x.saturating_add(1); + let x2 = x.saturating_add(2); + let s: usize = if with { x2 } else { x1 }; + if with { + if !word_at(src, nodes, toks, gaps, x, &[0x77, 0x69, 0x74, 0x68], Ghost(GW { g, w: "with"@ })) + || !punct_at(src, nodes, toks, gaps, x1, 0x7B, Ghost(GW { g, w: "{"@ })) { + return None; + } + } else if !punct_at(src, nodes, toks, gaps, x, 0x7B, Ghost(GW { g, w: "{"@ })) { + return None; + } + let mode: u8 = if matches!(kind, NodeKind::NamedImports) { + 11 + } else if matches!(kind, NodeKind::NamedExports) { + 15 + } else { + 13 + }; + let ls = ListSpec { m, start: s, mode, trail: true }; + let e = match check_list(src, nodes, toks, gaps, done, &ls, Ghost(g)) { + Some(e) => e, + None => { + return None; + }, + }; + let e1 = e.saturating_add(1); + if !punct_at(src, nodes, toks, gaps, e, 0x7D, Ghost(GW { g, w: "}"@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, e1, Ghost(g)) { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(x1 as int == x as int + 1); + assert(x2 as int == x as int + 2); + assert(e1 as int == e as int + 1); + assert(a.nodes[m as int].kind == kind); + assert(s as int == (if a.nodes[m as int].kind == NodeKind::WithClause { + x as int + 2 + } else { + x as int + 1 + })); + assert(mode as int == (if a.nodes[m as int].kind == NodeKind::NamedImports { + 11int + } else if a.nodes[m as int].kind == NodeKind::NamedExports { + 15int + } else { + 13int + })); + lemma_braced_done(a, g.ts, g.cx, m as int, x as int, s as int, e as int); + } + Some((x, e1, -100)) +} + +// --------------------------------------------------------------------- +// `import` declarations. +// --------------------------------------------------------------------- + +/// Where [`check_with_semi`] looks: child `k` of node `m`, from token `s`, the node ending at `end`. +pub struct WsSpec { + /// The node. + pub m: usize, + /// The index of the child that is the optional `WithClause`. + pub k: usize, + /// The token the clause would start at. + pub s: usize, + /// Where the node's last token ends. + pub end: usize, +} + +/// Child `k` of node `m` is an optional `WithClause` starting at token `s`, then the `;` +/// (`spec::d_with_semi`); `end` is where the node's last token ends. +#[verifier::spinoff_prover] +pub(crate) fn check_with_semi( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + ws: &WsSpec, + Ghost(g): Ghost, +) -> (r: Option) + requires + ws.m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, ws.m as int), + kids_ok(arena_of(nodes@), done.cxs@, ws.m as int, g.cx), + ensures + match r { + Some(j) => ws.k < arena_of(nodes@).nodes[ws.m as int].children.len() && d_with_semi( + arena_of(nodes@), + g.ts, + ws.m as int, + arena_of(nodes@).nodes[ws.m as int].children[ws.k as int], + g.cx, + ws.s as int, + j as int, + ) && 1 <= j && j <= toks.len() && g.ts.toks[j as int - 1].end == ws.end as int, + None => true, + }, +{ + let (m, k, s, end) = (ws.m, ws.k, ws.s, ws.end); + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let c = match kid_of(nodes, m, k) { + Some(c) => c, + None => { + return None; + }, + }; + let kc = kind_at(nodes, c); + let r: usize = if matches!(kc, NodeKind::Absent) { + if !check_absent(src, nodes, toks, gaps, c, s, Ghost(g)) { + return None; + } + s + } else if matches!(kc, NodeKind::WithClause) { + let (lo, hi, _) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + if lo != s { + return None; + } + hi + } else { + return None; + }; + let j = match find_semi(src, nodes, toks, gaps, r, end, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c as int, g.cx, k as int); + assert(a.nodes[m as int].children[k as int] == c as int); + assert(kd(a, c as int) == kc); + if kc == NodeKind::WithClause { + assert(node_done(a, g.ts, c as int, g.cx, s as int, r as int)); + } + assert(d_with_semi(a, g.ts, m as int, c as int, g.cx, s as int, j as int)); + } + Some(j) +} + +/// `ImportClause`. +#[verifier::spinoff_prover] +proof fn lemma_import_clause_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c0: int, + c1: int, + x: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c0 < m, + 0 <= c1 < m, + 0 <= x, + x < j <= tn(ts), + a.nodes[m].kind == NodeKind::ImportClause, + a.nodes[m].children =~= seq![c0, c1], + kd(a, c0) == NodeKind::BindingIdentifier || kd(a, c0) == NodeKind::Absent, + kd(a, c1) == NodeKind::NameSpaceImport || kd(a, c1) == NodeKind::NamedImports || kd(a, c1) + == NodeKind::Absent, + if kd(a, c1) == NodeKind::Absent { + kd(a, c0) == NodeKind::BindingIdentifier && j == x + 1 && node_done( + a, + ts, + c0, + cx, + x, + x + 1, + ) && d(a, ts, c1, cx, j, j) + } else if kd(a, c0) == NodeKind::Absent { + d(a, ts, c0, cx, x, x) && node_done(a, ts, c1, cx, x, j) + } else { + node_done(a, ts, c0, cx, x, x + 1) && is_p(ts, x + 1, ","@) && node_done( + a, + ts, + c1, + cx, + x + 2, + j, + ) + }, + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c0); + assert(a.nodes[m].children[1] == c1); + if kd(a, c1) == NodeKind::Absent { + assert(dc(a, ts, m, c0, cx, x, j)); + assert(dc(a, ts, m, c1, cx, j, j)); + } else if kd(a, c0) == NodeKind::Absent { + assert(dc(a, ts, m, c0, cx, x, x)); + assert(dc(a, ts, m, c1, cx, x, j)); + } else { + assert(dc(a, ts, m, c0, cx, x, x + 1)); + assert(dc(a, ts, m, c1, cx, x + 2, j)); + } + assert(d_module(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §16.2.2 `ImportClause`. +#[verifier::spinoff_prover] +pub(crate) fn check_import_clause( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_e(); + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ImportClause) || nd.children.len() != 2 { + return None; + } + let c0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let c1 = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if x > usize::MAX - 8 { + return None; + } + let x1 = x.saturating_add(1); + let x2 = x.saturating_add(2); + let k0 = kind_at(nodes, c0); + let k1 = kind_at(nodes, c1); + if !(matches!(k0, NodeKind::BindingIdentifier | NodeKind::Absent) + && matches!(k1, NodeKind::NameSpaceImport | NodeKind::NamedImports | NodeKind::Absent)) { + return None; + } + let j: usize = if matches!(k1, NodeKind::Absent) { + let (lo0, hi0, _) = match done_at(done, c0) { + Some(r) => r, + None => { + return None; + }, + }; + if !matches!(k0, NodeKind::BindingIdentifier) || lo0 != x || hi0 != x1 + || !check_absent(src, nodes, toks, gaps, c1, x1, Ghost(g)) { + return None; + } + x1 + } else if matches!(k0, NodeKind::Absent) { + let (lo1, hi1, _) = match done_at(done, c1) { + Some(r) => r, + None => { + return None; + }, + }; + if !check_absent(src, nodes, toks, gaps, c0, x, Ghost(g)) || lo1 != x { + return None; + } + hi1 + } else { + let (lo0, hi0, _) = match done_at(done, c0) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo1, hi1, _) = match done_at(done, c1) { + Some(r) => r, + None => { + return None; + }, + }; + if lo0 != x || hi0 != x1 || lo1 != x2 + || !punct_n_at(src, nodes, toks, gaps, x1, &[0x2C], Ghost(GW { g, w: ","@ })) { + return None; + } + hi1 + }; + if !end_ok(src, nodes, toks, gaps, nd.end, j, Ghost(g)) || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(x1 as int == x as int + 1); + assert(x2 as int == x as int + 2); + assert(a.nodes[m as int].children =~= seq![c0 as int, c1 as int]) by { + assert(a.nodes[m as int].children.len() == 2); + } + assert(kd(a, c0 as int) == k0); + assert(kd(a, c1 as int) == k1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c0 as int, g.cx, 0); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c1 as int, g.cx, 1); + lemma_import_clause_done(a, g.ts, g.cx, m as int, c0 as int, c1 as int, x as int, j as int); + } + Some((x, j, -100)) +} + +/// `ImportDeclaration`. +#[verifier::spinoff_prover] +proof fn lemma_import_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c0: int, + c1: int, + c2: int, + x: int, + mm: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c0 < m, + 0 <= c1 < m, + 0 <= c2 < m, + 0 <= x, + x < j <= tn(ts), + a.nodes[m].kind == NodeKind::ImportDeclaration, + a.nodes[m].children =~= seq![c0, c1, c2], + is_kw(ts, x, "import"@), + kd(a, c1) == NodeKind::StringLiteral, + kd(a, c0) == NodeKind::Absent || kd(a, c0) == NodeKind::ImportClause, + if kd(a, c0) == NodeKind::Absent { + mm == x + 1 && d(a, ts, c0, cx, x + 1, x + 1) && ed(a, ts, c0, x + 1) == x + 1 + && node_done(a, ts, c1, cx, mm, mm + 1) && d_with_semi(a, ts, m, c2, cx, mm + 1, j) + } else { + node_done(a, ts, c0, cx, x + 1, mm) && is_kw(ts, mm, "from"@) && node_done( + a, + ts, + c1, + cx, + mm + 1, + mm + 2, + ) && d_with_semi(a, ts, m, c2, cx, mm + 2, j) + }, + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c0); + assert(a.nodes[m].children[1] == c1); + assert(a.nodes[m].children[2] == c2); + assert(dc(a, ts, m, c0, cx, x + 1, mm)); + assert(dc(a, ts, m, c1, cx, mm + if kd(a, c0) == NodeKind::Absent { 0int } else { 1int }, mm + if kd( + a, + c0, + ) == NodeKind::Absent { + 1int + } else { + 2int + })); + assert(d_module(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §16.2.2 `import ImportClause from "m" with {..} ;` and `import "m" with {..} ;`. +#[verifier::spinoff_prover] +pub(crate) fn check_import( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_m(); + lemma_words_e(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ImportDeclaration) || nd.children.len() != 3 { + return None; + } + let c0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let c1 = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if x > usize::MAX - 8 { + return None; + } + let x1 = x.saturating_add(1); + let k0 = kind_at(nodes, c0); + let (lo1, hi1, _) = match done_at(done, c1) { + Some(r) => r, + None => { + return None; + }, + }; + if !word_at(src, nodes, toks, gaps, x, &[0x69, 0x6D, 0x70, 0x6F, 0x72, 0x74], Ghost(GW { g, w: "import"@ })) + || !matches!(kind_at(nodes, c1), NodeKind::StringLiteral) + || !matches!(k0, NodeKind::Absent | NodeKind::ImportClause) { + return None; + } + let mm: usize; + let s: usize; + if matches!(k0, NodeKind::Absent) { + if !check_absent(src, nodes, toks, gaps, c0, x1, Ghost(g)) || lo1 != x1 { + return None; + } + mm = x1; + s = hi1; + } else { + let (lo0, hi0, _) = match done_at(done, c0) { + Some(r) => r, + None => { + return None; + }, + }; + let h1 = hi0.saturating_add(1); + if lo0 != x1 || !word_at(src, nodes, toks, gaps, hi0, &[0x66, 0x72, 0x6F, 0x6D], Ghost(GW { g, w: "from"@ })) + || lo1 != h1 { + return None; + } + mm = hi0; + s = hi1; + } + if s != mm.saturating_add(if matches!(k0, NodeKind::Absent) { 1 } else { 2 }) || hi1 != s { + return None; + } + let j = match check_with_semi(src, nodes, toks, gaps, done, &WsSpec { m, k: 2, s, end: nd.end }, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + if j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(x1 as int == x as int + 1); + let c2g: int = a.nodes[m as int].children[2]; + lemma_child_before(a, m as int, 2); + assert(a.nodes[m as int].children =~= seq![c0 as int, c1 as int, c2g]) by { + assert(a.nodes[m as int].children.len() == 3); + } + assert(kd(a, c0 as int) == k0); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c0 as int, g.cx, 0); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c1 as int, g.cx, 1); + assert(a.nodes[m as int].children[2] == c2g); + if k0 == NodeKind::Absent { + assert(mm as int == x as int + 1); + assert(s as int == mm as int + 1); + } else { + assert(mm.saturating_add(2) as int == mm as int + 2); + assert(s as int == mm as int + 2); + } + lemma_import_done( + a, + g.ts, + g.cx, + m as int, + c0 as int, + c1 as int, + c2g, + x as int, + mm as int, + j as int, + ); + } + Some((x, j, -100)) +} + +// --------------------------------------------------------------------- +// `export` declarations. +// --------------------------------------------------------------------- + +/// The context of `export default` declarations: `[+Default]`. +pub open spec fn cx_default(cx: Ctx) -> Ctx { + Ctx { yield_: cx.yield_, await_: cx.await_, in_: cx.in_, return_: cx.return_, deflt: true } +} + +/// `ExportDeclaration`: `c0`, `c1`, `c2` are the children there are; `mm` is where `c0` ends. +#[verifier::spinoff_prover] +#[verifier::rlimit(200)] +proof fn lemma_export_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c0: int, + c1: int, + c2: int, + x: int, + mm: int, + j: int, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= c0 < m, + 0 <= c1 < m, + 0 <= c2 < m, + 0 <= x, + x < j <= tn(ts), + a.nodes[m].kind == NodeKind::ExportDeclaration, + a.nodes[m].children.len() == (match a.nodes[m].op { + Op::ExportAll => 2int, + Op::ExportAllAs | Op::ExportFrom => 3int, + _ => 1int, + }), + a.nodes[m].children[0] == c0, + a.nodes[m].children.len() >= 2 ==> a.nodes[m].children[1] == c1, + a.nodes[m].children.len() >= 3 ==> a.nodes[m].children[2] == c2, + is_kw(ts, x, "export"@), + match a.nodes[m].op { + Op::ExportAll => is_p(ts, x + 1, "*"@) && is_kw(ts, x + 2, "from"@) && kd(a, c0) + == NodeKind::StringLiteral && node_done(a, ts, c0, cx, x + 3, x + 4) + && d_with_semi(a, ts, m, c1, cx, x + 4, j), + Op::ExportAllAs => is_p(ts, x + 1, "*"@) && is_kw(ts, x + 2, "as"@) + && is_module_export_name_kind(kd(a, c0)) && node_done(a, ts, c0, cx, x + 3, x + 4) + && is_kw(ts, x + 4, "from"@) && kd(a, c1) == NodeKind::StringLiteral && node_done( + a, + ts, + c1, + cx, + x + 5, + x + 6, + ) && d_with_semi(a, ts, m, c2, cx, x + 6, j), + Op::ExportFrom => kd(a, c0) == NodeKind::NamedExports && kd(a, c1) + == NodeKind::StringLiteral && node_done(a, ts, c0, cx, x + 1, mm) && is_kw( + ts, + mm, + "from"@, + ) && node_done(a, ts, c1, cx, mm + 1, mm + 2) && d_with_semi(a, ts, m, c2, cx, mm + 2, j), + Op::ExportNamed => kd(a, c0) == NodeKind::NamedExports && node_done( + a, + ts, + c0, + cx, + x + 1, + mm, + ) && semi(ts, mm, j, false), + Op::ExportVar => kd(a, c0) == NodeKind::VariableStatement && node_done( + a, + ts, + c0, + cx, + x + 1, + j, + ), + Op::ExportDecl => is_declaration_kind(kd(a, c0)) && node_done(a, ts, c0, cx, x + 1, j), + Op::ExportDefaultHoistable => is_kw(ts, x + 1, "default"@) && is_hoistable_kind(kd(a, c0)) + && node_done(a, ts, c0, cx_default(cx), x + 2, j), + Op::ExportDefaultClass => is_kw(ts, x + 1, "default"@) && kd(a, c0) + == NodeKind::ClassDeclaration && node_done(a, ts, c0, cx_default(cx), x + 2, j), + Op::ExportDefaultExpression => is_kw(ts, x + 1, "default"@) && !is_kw( + ts, + x + 2, + "function"@, + ) && !is_kw(ts, x + 2, "async"@) && !is_kw(ts, x + 2, "class"@) && lvl(a, c0) >= 0 + && node_done(a, ts, c0, cx, x + 2, mm) && semi(ts, mm, j, false), + _ => false, + }, + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + assert(span_ok(a, ts, m, x, j)); + assert(cx_default(cx) == Ctx { + yield_: cx.yield_, + await_: cx.await_, + in_: cx.in_, + return_: cx.return_, + deflt: true, + }); + match a.nodes[m].op { + Op::ExportAll => { + assert(dc(a, ts, m, c0, cx, x + 3, x + 4)); + }, + Op::ExportAllAs => { + assert(dc(a, ts, m, c0, cx, x + 3, x + 4)); + assert(dc(a, ts, m, c1, cx, x + 5, x + 6)); + }, + Op::ExportFrom => { + assert(dc(a, ts, m, c0, cx, x + 1, mm)); + assert(dc(a, ts, m, c1, cx, mm + 1, mm + 2)); + }, + Op::ExportNamed => { + assert(dc(a, ts, m, c0, cx, x + 1, mm)); + }, + Op::ExportVar | Op::ExportDecl => { + assert(dc(a, ts, m, c0, cx, x + 1, j)); + }, + Op::ExportDefaultHoistable | Op::ExportDefaultClass => { + assert(dc(a, ts, m, c0, cx_default(cx), x + 2, j)); + }, + _ => { + assert(is_assign_expr(a, c0)); + assert(dc(a, ts, m, c0, cx_in(cx, true), x + 2, mm)); + }, + } + assert(d_module(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §16.2.3 `ExportDeclaration`, in all its forms. +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub(crate) fn check_export( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + cx_in(g.cx, true) == g.cx, + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + cx_of(done.cxs@[m as int]) == g.cx, + kids_ok( + arena_of(nodes@), + done.cxs@, + m as int, + if nodes@[m as int].op == Op::ExportDefaultHoistable || nodes@[m as int].op + == Op::ExportDefaultClass { + cx_default(g.cx) + } else { + g.cx + }, + ), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + proof { + lemma_words_m(); + lemma_words_e(); + lemma_kw_text(); + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_node_view(nodes@, m as int); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ExportDeclaration) { + return None; + } + let op = nd.op; + let want: usize = match op { + Op::ExportAll => 2, + Op::ExportAllAs | Op::ExportFrom => 3, + Op::ExportNamed | Op::ExportVar | Op::ExportDecl | Op::ExportDefaultHoistable + | Op::ExportDefaultClass | Op::ExportDefaultExpression => 1, + _ => { + return None; + }, + }; + if nd.children.len() != want { + return None; + } + let c0 = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo0, hi0, lv0) = match done_at(done, c0) { + Some(r) => r, + None => { + return None; + }, + }; + let x = match start_tok(src, nodes, toks, gaps, done, m, Ghost(g)) { + Some(x) => x, + None => { + return None; + }, + }; + if x > usize::MAX - 16 || !word_at(src, nodes, toks, gaps, x, &[0x65, 0x78, 0x70, 0x6F, 0x72, 0x74], Ghost(GW { g, w: "export"@ })) { + return None; + } + let x1 = x.saturating_add(1); + let x2 = x.saturating_add(2); + let x3 = x.saturating_add(3); + let x4 = x.saturating_add(4); + let k0 = kind_at(nodes, c0); + // `c1` is the second child where there is one (`c0` stands in otherwise). + let c1 = if want >= 2 { + match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + } + } else { + c0 + }; + let j: usize; + match op { + Op::ExportAll | Op::ExportAllAs => { + let all_as = matches!(op, Op::ExportAllAs); + if !punct_at(src, nodes, toks, gaps, x1, 0x2A, Ghost(GW { g, w: "*"@ })) { + return None; + } + if all_as { + if !word_at(src, nodes, toks, gaps, x2, &[0x61, 0x73], Ghost(GW { g, w: "as"@ })) + || !name_kind(k0) { + return None; + } + } else if !word_at(src, nodes, toks, gaps, x2, &[0x66, 0x72, 0x6F, 0x6D], Ghost(GW { g, w: "from"@ })) + || !matches!(k0, NodeKind::StringLiteral) { + return None; + } + if lo0 != x3 || hi0 != x4 { + return None; + } + if all_as { + let (lo1, hi1, _) = match done_at(done, c1) { + Some(r) => r, + None => { + return None; + }, + }; + let x5 = x.saturating_add(5); + let x6 = x.saturating_add(6); + if !word_at(src, nodes, toks, gaps, x4, &[0x66, 0x72, 0x6F, 0x6D], Ghost(GW { g, w: "from"@ })) + || !matches!(kind_at(nodes, c1), NodeKind::StringLiteral) || lo1 != x5 || hi1 != x6 { + return None; + } + j = match check_with_semi(src, nodes, toks, gaps, done, &WsSpec { m, k: 2, s: x6, end: nd.end }, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + } else { + j = match check_with_semi(src, nodes, toks, gaps, done, &WsSpec { m, k: 1, s: x4, end: nd.end }, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + } + }, + Op::ExportFrom => { + if !matches!(k0, NodeKind::NamedExports) || lo0 != x1 + || !word_at(src, nodes, toks, gaps, hi0, &[0x66, 0x72, 0x6F, 0x6D], Ghost(GW { g, w: "from"@ })) { + return None; + } + let (lo1, hi1, _) = match done_at(done, c1) { + Some(r) => r, + None => { + return None; + }, + }; + let h1 = hi0.saturating_add(1); + let h2 = hi0.saturating_add(2); + if !matches!(kind_at(nodes, c1), NodeKind::StringLiteral) || lo1 != h1 || hi1 != h2 { + return None; + } + j = match check_with_semi(src, nodes, toks, gaps, done, &WsSpec { m, k: 2, s: h2, end: nd.end }, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + }, + Op::ExportNamed => { + if !matches!(k0, NodeKind::NamedExports) || lo0 != x1 { + return None; + } + j = match find_semi(src, nodes, toks, gaps, hi0, nd.end, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + }, + Op::ExportVar | Op::ExportDecl => { + let kind_ok = if matches!(op, Op::ExportVar) { + matches!(k0, NodeKind::VariableStatement) + } else { + matches!( + k0, + NodeKind::FunctionDeclaration | NodeKind::GeneratorDeclaration + | NodeKind::AsyncFunctionDeclaration | NodeKind::AsyncGeneratorDeclaration + | NodeKind::ClassDeclaration | NodeKind::LexicalDeclaration + ) + }; + if !kind_ok || lo0 != x1 || !end_ok(src, nodes, toks, gaps, nd.end, hi0, Ghost(g)) { + return None; + } + j = hi0; + }, + Op::ExportDefaultHoistable | Op::ExportDefaultClass => { + let kind_ok = if matches!(op, Op::ExportDefaultClass) { + matches!(k0, NodeKind::ClassDeclaration) + } else { + matches!( + k0, + NodeKind::FunctionDeclaration | NodeKind::GeneratorDeclaration + | NodeKind::AsyncFunctionDeclaration | NodeKind::AsyncGeneratorDeclaration + ) + }; + if !kind_ok || lo0 != x2 + || !word_at(src, nodes, toks, gaps, x1, &[0x64, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74], Ghost(GW { g, w: "default"@ })) + || !end_ok(src, nodes, toks, gaps, nd.end, hi0, Ghost(g)) { + return None; + } + j = hi0; + }, + _ => { + if lv0 < 0 || lo0 != x2 + || !word_at(src, nodes, toks, gaps, x1, &[0x64, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74], Ghost(GW { g, w: "default"@ })) + || !not_word_at(src, nodes, toks, gaps, x2, &[0x66, 0x75, 0x6E, 0x63, 0x74, 0x69, 0x6F, 0x6E], Ghost(GW { g, w: "function"@ })) + || !not_word_at(src, nodes, toks, gaps, x2, &[0x61, 0x73, 0x79, 0x6E, 0x63], Ghost(GW { g, w: "async"@ })) + || !not_word_at(src, nodes, toks, gaps, x2, &[0x63, 0x6C, 0x61, 0x73, 0x73], Ghost(GW { g, w: "class"@ })) { + return None; + } + j = match find_semi(src, nodes, toks, gaps, hi0, nd.end, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + }, + } + if j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].op == op); + assert(x1 as int == x as int + 1); + assert(x2 as int == x as int + 2); + assert(x3 as int == x as int + 3); + assert(x4 as int == x as int + 4); + let dd = op == Op::ExportDefaultHoistable || op == Op::ExportDefaultClass; + let ck = if dd { cx_default(g.cx) } else { g.cx }; + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c0 as int, ck, 0); + assert(a.nodes[m as int].children[0] == c0 as int); + if a.nodes[m as int].children.len() >= 2 { + assert(a.nodes[m as int].children[1] == c1 as int); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, c1 as int, ck, 1); + } + let c2g: int = if a.nodes[m as int].children.len() >= 3 { a.nodes[m as int].children[2] } else { c0 as int }; + if a.nodes[m as int].children.len() >= 3 { + lemma_child_before(a, m as int, 2); + assert(a.nodes[m as int].children[2] == c2g); + } + assert(kd(a, c0 as int) == k0); + match op { + Op::ExportAllAs => { + assert(x.saturating_add(5) as int == x as int + 5); + assert(x.saturating_add(6) as int == x as int + 6); + }, + Op::ExportFrom => { + assert(hi0.saturating_add(1) as int == hi0 as int + 1); + assert(hi0.saturating_add(2) as int == hi0 as int + 2); + }, + _ => {}, + } + assert(g.cx.in_ || !g.cx.in_); + lemma_export_done( + a, + g.ts, + g.cx, + m as int, + c0 as int, + c1 as int, + c2g, + x as int, + hi0 as int, + j as int, + ); + } + Some((x, j, -100)) +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/arena_names.rs b/crates/clazel-js-syntax/src/arena_names.rs new file mode 100644 index 000000000..8d47cfd43 --- /dev/null +++ b/crates/clazel-js-syntax/src/arena_names.rs @@ -0,0 +1,1215 @@ +//! The names the early errors of [`crate::arena_early`] compare, computed over the +//! arena and proven equal to the specification's (item 7.3): BoundNames (§8.2.1) of +//! every node as token indices ([`bound_names_all`]), a Use Strict Directive +//! (`has_use_strict`, §11.2.1, [`use_strict_exec`]), the simple-parameter and +//! duplicate-name rules of function code (`function_early`, §15.1.1, +//! [`function_early_exec`]). +//! +//! A name is kept as the index of the token it is the name of, so a list of names is +//! a `Vec` and [`names_of`] is the specification's `Seq>` of it. + +use crate::arena_d::*; +use crate::arena_early::*; +use crate::ast::*; +use crate::spec::*; +use vstd::prelude::*; + +verus! { + +/// What the executable checks read besides the arena: the source, the tokens, a hint for finding +/// a token from its start, and the BoundNames of every node. +pub struct Tk<'a> { + /// The source text. + pub src: &'a [u32], + /// The tokens the parser consumed. + pub toks: &'a [DTok], + /// For a start position, the index of the token that starts there (`usize::MAX` for none). + pub by_start: &'a [usize], + /// The BoundNames of each node ([`bn_inv`]). + pub bn: &'a [Vec], + /// The declared names of each node ([`crate::arena_decls::names_inv`]). + pub nm: &'a crate::arena_decls::Names, +} + +/// The names of the tokens `xs`. +pub open spec fn names_of(toks: Seq, xs: Seq) -> Seq> { + Seq::new(xs.len(), |i: int| toks[xs[i] as int].name@) +} + +/// Every index of `xs` is a token. +pub open spec fn idx_ok(toks: Seq, xs: Seq) -> bool { + forall|i: int| 0 <= i < xs.len() ==> (#[trigger] xs[i]) < toks.len() +} + +/// `bn[j]` is the BoundNames of node `j`, for every node before `m`. +pub open spec fn bn_inv(a: AstV, ts: Stream, toks: Seq, bn: Seq>, m: int) -> bool { + forall|j: int| + 0 <= j < m ==> { + let v = (#[trigger] bn[j])@; + idx_ok(toks, v) && names_of(toks, v) =~= bound_names(a, ts, j) + } +} + +/// The names of two lists put together. +proof fn lemma_names_add(toks: Seq, xs: Seq, ys: Seq) + ensures + names_of(toks, xs + ys) =~= names_of(toks, xs) + names_of(toks, ys), +{ +} + +/// `dst` gets the elements of `src` after its own. +fn push_all(dst: &mut Vec, src: &[usize]) + ensures + final(dst)@ =~= old(dst)@ + src@, +{ + let len = src.len(); + let mut i: usize = 0; + while i < len + invariant + len == src.len(), + i <= len, + dst@ =~= old(dst)@ + src@.subrange(0, i as int), + decreases len - i, + { + match src.get(i) { + Some(x) => { + dst.push(*x); + }, + None => { + return; + }, + } + proof { + assert(src@.subrange(0, i as int + 1) =~= src@.subrange(0, i as int).push(src@[i as int])); + } + i = i.saturating_add(1); + } + proof { + assert(src@.subrange(0, len as int) =~= src@); + } +} + +/// The BoundNames of node `m` with one child: the first, or the last. +pub open spec fn bn_one(a: AstV, ts: Stream, m: int, last: bool) -> Seq> { + let ch = a.nodes[m].children; + let idx = if last { + ch.len() - 1 + } else { + 0 + }; + if ch.len() > 0 && 0 <= ch[idx] < m { + bound_names(a, ts, ch[idx]) + } else { + Seq::empty() + } +} + +/// The BoundNames of the children `ks[..k]` of node `n`, put together. +pub open spec fn bn_pre(a: AstV, ts: Stream, n: int, ks: Seq, k: int) -> Seq> + decreases k, +{ + if k <= 0 { + Seq::empty() + } else { + bn_pre(a, ts, n, ks, k - 1) + (if 0 <= ks[k - 1] < n { + bound_names(a, ts, ks[k - 1]) + } else { + Seq::empty() + }) + } +} + +/// `bound_names_list` is the prefix and what is left. +proof fn lemma_bn_pre(a: AstV, ts: Stream, n: int, ks: Seq, k: int) + requires + 0 <= k <= ks.len(), + ensures + bound_names_list(a, ts, n, ks, 0) =~= bn_pre(a, ts, n, ks, k) + bound_names_list(a, ts, n, ks, k), + decreases k, +{ + if k > 0 { + lemma_bn_pre(a, ts, n, ks, k - 1); + reveal_with_fuel(bound_names_list, 2); + let here = if 0 <= ks[k - 1] < n { + bound_names(a, ts, ks[k - 1]) + } else { + Seq::empty() + }; + assert(bound_names_list(a, ts, n, ks, k - 1) =~= here + bound_names_list(a, ts, n, ks, k)); + assert(bn_pre(a, ts, n, ks, k) =~= bn_pre(a, ts, n, ks, k - 1) + here); + } +} + +/// The BoundNames of node `m` of a kind with one child. +fn bn_child( + nodes: &[Node], + toks: &[DTok], + bn: &[Vec], + m: usize, + last: bool, + Ghost(ts): Ghost, +) -> (r: Option>) + requires + m < nodes.len(), + bn.len() == m, + ast_wf(arena_of(nodes@)), + bn_inv(arena_of(nodes@), ts, toks@, bn@, m as int), + ensures + match r { + Some(v) => idx_ok(toks@, v@) && names_of(toks@, v@) =~= bn_one( + arena_of(nodes@), + ts, + m as int, + last, + ), + None => true, + }, +{ + let ghost a = arena_of(nodes@); + let _ = toks.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == nd.children.len()); + } + let len = nd.children.len(); + if len == 0 { + return Some(Vec::new()); + } + let idx = if last { + len.saturating_sub(1) + } else { + 0 + }; + let c = match nd.children.as_slice().get(idx) { + Some(c) => *c as usize, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int].children[idx as int] == c as int); + lemma_child_before(a, m as int, idx as int); + } + if c >= m { + return None; + } + let from = match bn.get(c) { + Some(v) => v, + None => { + return None; + }, + }; + let mut out: Vec = Vec::new(); + push_all(&mut out, from.as_slice()); + proof { + assert(bn@[c as int] == from); + assert(out@ =~= from@); + } + Some(out) +} + +/// The BoundNames of node `m` of a kind that binds its children's, in order. +fn bn_list( + nodes: &[Node], + toks: &[DTok], + bn: &[Vec], + m: usize, + Ghost(ts): Ghost, +) -> (r: Option>) + requires + m < nodes.len(), + bn.len() == m, + ast_wf(arena_of(nodes@)), + bn_inv(arena_of(nodes@), ts, toks@, bn@, m as int), + ensures + match r { + Some(v) => idx_ok(toks@, v@) && names_of(toks@, v@) =~= bound_names_list( + arena_of(nodes@), + ts, + m as int, + arena_of(nodes@).nodes[m as int].children, + 0, + ), + None => true, + }, +{ + let ghost a = arena_of(nodes@); + let _ = toks.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let len = nd.children.len(); + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == len); + } + let ghost ks = a.nodes[m as int].children; + let mut out: Vec = Vec::new(); + let mut k: usize = 0; + while k < len + invariant + k <= len, + len == nd.children.len(), + m < nodes.len(), + bn.len() == m, + nd == &nodes@[m as int], + a == arena_of(nodes@), + ast_wf(a), + ks == a.nodes[m as int].children, + len == ks.len(), + bn_inv(a, ts, toks@, bn@, m as int), + idx_ok(toks@, out@), + names_of(toks@, out@) =~= bn_pre(a, ts, m as int, ks, k as int), + decreases len - k, + { + let c = match nd.children.as_slice().get(k) { + Some(c) => *c as usize, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int].children[k as int] == c as int); + lemma_child_before(a, m as int, k as int); + } + if c >= m { + return None; + } + let from = match bn.get(c) { + Some(v) => v, + None => { + return None; + }, + }; + let ghost before = out@; + push_all(&mut out, from.as_slice()); + proof { + assert(bn@[c as int] == from); + lemma_names_add(toks@, before, from@); + assert(bn_pre(a, ts, m as int, ks, k as int + 1) =~= bn_pre(a, ts, m as int, ks, k as int) + + bound_names(a, ts, c as int)); + assert forall|i: int| 0 <= i < out@.len() implies (#[trigger] out@[i]) < toks.len() by { + if i < before.len() { + assert(out@[i] == before[i]); + } else { + assert(out@[i] == from@[i - before.len()]); + } + } + } + k = k.saturating_add(1); + } + proof { + lemma_bn_pre(a, ts, m as int, ks, len as int); + assert(bound_names_list(a, ts, m as int, ks, len as int) =~= Seq::>::empty()); + } + Some(out) +} + +/// The BoundNames of the `BindingIdentifier` node `m`. +fn bn_ident( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + by_start: &[usize], + m: usize, + Ghost(ts): Ghost, +) -> (r: Option>) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + toks_match(src@, toks@, ts), + starts_inc(ts), + ensures + match r { + Some(v) => idx_ok(toks@, v@) && names_of(toks@, v@) =~= seq![name_of( + arena_of(nodes@), + ts, + m as int, + )], + None => true, + }, +{ + let _ = src.len(); + let x = match node_token(nodes, toks, by_start, m) { + Some(x) => x, + None => { + return None; + }, + }; + let out: Vec = vec![x]; + proof { + let a = arena_of(nodes@); + assert(dtok_matches(src@, toks@[x as int], ts.toks[x as int])); + lemma_node_tok_at(a, ts, m as int, x as int); + assert(a.nodes.len() == nodes@.len()); + assert(out@ =~= seq![x]); + assert(names_of(toks@, out@) =~= seq![toks@[x as int].name@]); + assert(name_of(a, ts, m as int) =~= toks@[x as int].name@); + } + Some(out) +} + +/// The BoundNames of every node, as the tokens that name them. +#[verifier::rlimit(400)] +pub fn bound_names_all( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + by_start: &[usize], + Ghost(ts): Ghost, +) -> (r: Option>>) + requires + ast_wf(arena_of(nodes@)), + toks_match(src@, toks@, ts), + starts_inc(ts), + ensures + match r { + Some(bn) => bn.len() == nodes.len() && bn_inv( + arena_of(nodes@), + ts, + toks@, + bn@, + nodes.len() as int, + ), + None => true, + }, +{ + let ghost a = arena_of(nodes@); + let n = nodes.len(); + let mut bn: Vec> = Vec::new(); + let mut m: usize = 0; + while m < n + invariant + m <= n, + n == nodes.len(), + bn.len() == m, + a == arena_of(nodes@), + ast_wf(a), + toks_match(src@, toks@, ts), + starts_inc(ts), + bn_inv(a, ts, toks@, bn@, m as int), + decreases n - m, + { + let kind = match nodes.get(m) { + Some(x) => x.kind, + None => { + return None; + }, + }; + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(a.nodes[m as int].kind == kind); + } + let cur: Vec = match kind { + NodeKind::BindingIdentifier => match bn_ident(src, nodes, toks, by_start, m, Ghost(ts)) { + Some(v) => v, + None => { + return None; + }, + }, + NodeKind::BindingElement | NodeKind::BindingRestElement | NodeKind::BindingRestProperty + | NodeKind::FunctionRestParameter | NodeKind::VariableDeclaration | NodeKind::LexicalBinding + | NodeKind::ForDeclaration => match bn_child(nodes, toks, bn.as_slice(), m, false, Ghost(ts)) { + Some(v) => v, + None => { + return None; + }, + }, + NodeKind::BindingProperty => match bn_child(nodes, toks, bn.as_slice(), m, true, Ghost(ts)) { + Some(v) => v, + None => { + return None; + }, + }, + NodeKind::ObjectBindingPattern | NodeKind::ArrayBindingPattern | NodeKind::FormalParameters + | NodeKind::VariableStatement | NodeKind::LexicalDeclaration + | NodeKind::VariableDeclarationList => match bn_list(nodes, toks, bn.as_slice(), m, Ghost(ts)) { + Some(v) => v, + None => { + return None; + }, + }, + _ => Vec::new(), + }; + proof { + assert(idx_ok(toks@, cur@)); + assert(names_of(toks@, cur@) =~= bound_names(a, ts, m as int)); + } + let ghost old_bn = bn@; + bn.push(cur); + proof { + assert forall|j: int| 0 <= j < m as int + 1 implies { + let v = (#[trigger] bn@[j])@; + idx_ok(toks@, v) && names_of(toks@, v) =~= bound_names(a, ts, j) + } by { + if j < m as int { + assert(bn@[j] == old_bn[j]); + } + } + } + m = m.saturating_add(1); + } + Some(bn) +} + +// --------------------------------------------------------------------- +// Use Strict Directive. +// --------------------------------------------------------------------- + +/// The text of the two forms of a Use Strict Directive, as code points. +proof fn lemma_use_strict_words() + ensures + cps("\"use strict\""@) =~= seq![34u32, 117, 115, 101, 32, 115, 116, 114, 105, 99, 116, 34], + cps("'use strict'"@) =~= seq![39u32, 117, 115, 101, 32, 115, 116, 114, 105, 99, 116, 39], +{ + reveal_strlit("\"use strict\""); + reveal_strlit("'use strict'"); + assert("\"use strict\""@ =~= seq!['"', 'u', 's', 'e', ' ', 's', 't', 'r', 'i', 'c', 't', '"']); + assert("'use strict'"@ =~= seq!['\'', 'u', 's', 'e', ' ', 's', 't', 'r', 'i', 'c', 't', '\'']); +} + +/// The code points of `src` from `start` to `end` are `lit`. +fn range_is(src: &[u32], start: usize, end: usize, lit: &[u32]) -> (r: bool) + requires + start <= end, + end <= src.len(), + ensures + r == (src@.subrange(start as int, end as int) =~= lit@), +{ + if end.saturating_sub(start) != lit.len() { + return false; + } + let len = lit.len(); + let mut i: usize = 0; + while i < len + invariant + len == lit.len(), + len == end - start, + end <= src.len(), + start <= end, + i <= len, + forall|q: int| 0 <= q < i ==> src@[start as int + q] == lit@[q], + decreases len - i, + { + let (x, y) = match (src.get(start.saturating_add(i)), lit.get(i)) { + (Some(x), Some(y)) => (*x, *y), + _ => { + return false; + }, + }; + if x != y { + proof { + assert(src@.subrange(start as int, end as int)[i as int] == src@[start as int + i as int]); + } + return false; + } + i = i.saturating_add(1); + } + proof { + assert(src@.subrange(start as int, end as int) =~= lit@); + } + true +} + +/// No item of `ks` before `k` is a Use Strict Directive, they are all directives, and `k` +/// is not a directive or the end: the items have no Use Strict Directive. +proof fn lemma_no_use_strict(a: AstV, ts: Stream, ks: Seq, k: int) + requires + 0 <= k <= ks.len(), + forall|m: int| + 0 <= m < k ==> is_directive(a, #[trigger] ks[m]) && !is_use_strict_directive( + a, + ts, + ks[m], + ), + k == ks.len() || !is_directive(a, ks[k]), + ensures + !has_use_strict(a, ts, ks), +{ + assert forall|kk: int| + 0 <= kk < ks.len() implies !(is_use_strict_directive(a, ts, #[trigger] ks[kk]) && (forall|m: int| + 0 <= m <= kk ==> is_directive(a, #[trigger] ks[m]))) by { + if kk < k { + assert(!is_use_strict_directive(a, ts, ks[kk])); + } else { + assert(0 <= k <= kk); + assert(k < ks.len()); + assert(!is_directive(a, ks[k])); + } + } +} + +/// The items of node `b` have a Use Strict Directive (`has_use_strict`). +#[verifier::rlimit(400)] +pub fn use_strict_exec( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + by_start: &[usize], + b: usize, + Ghost(ts): Ghost, +) -> (r: Option) + requires + b < nodes.len(), + ast_wf(arena_of(nodes@)), + toks_match(src@, toks@, ts), + starts_inc(ts), + ensures + match r { + Some(v) => v == has_use_strict(arena_of(nodes@), ts, kids(arena_of(nodes@), b as int)), + None => true, + }, +{ + let ghost a = arena_of(nodes@); + let nd = match nodes.get(b) { + Some(x) => x, + None => { + return None; + }, + }; + let len = nd.children.len(); + proof { + assert(a.nodes[b as int] == nodes@[b as int]@); + assert(nd == &nodes@[b as int]); + assert(a.nodes[b as int].children.len() == len); + lemma_use_strict_words(); + } + let ghost ks = a.nodes[b as int].children; + let mut k: usize = 0; + while k < len + invariant + k <= len, + len == nd.children.len(), + b < nodes.len(), + nd == &nodes@[b as int], + a == arena_of(nodes@), + ast_wf(a), + toks_match(src@, toks@, ts), + starts_inc(ts), + ks == a.nodes[b as int].children, + len == ks.len(), + forall|m: int| + 0 <= m < k ==> is_directive(a, #[trigger] ks[m]) && !is_use_strict_directive( + a, + ts, + ks[m], + ), + decreases len - k, + { + let c = match nd.children.as_slice().get(k) { + Some(c) => *c as usize, + None => { + return None; + }, + }; + proof { + assert(a.nodes[b as int].children[k as int] == c as int); + lemma_child_before(a, b as int, k as int); + } + let cnd = match nodes.get(c) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + assert(a.nodes[c as int] == nodes@[c as int]@); + assert(cnd == &nodes@[c as int]); + } + let not_directive = if !matches!(cnd.kind, NodeKind::ExpressionStatement) { + true + } else { + match kid_kind(nodes, c, 0) { + Some(k0) => !matches!(k0, NodeKind::StringLiteral), + None => { + return None; + }, + } + }; + if not_directive { + proof { + assert(!is_directive(a, ks[k as int])); + lemma_no_use_strict(a, ts, ks, k as int); + } + return Some(false); + } + let e = match cnd.children.as_slice().get(ZERO) { + Some(e) => *e as usize, + None => { + return None; + }, + }; + proof { + assert(a.nodes[c as int].children[0] == e as int); + lemma_child_before(a, c as int, 0); + assert(is_directive(a, ks[k as int])); + } + let x = match node_token(nodes, toks, by_start, e) { + Some(x) => x, + None => { + return None; + }, + }; + let t = match toks.get(x) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + assert(t == &toks@[x as int]); + assert(dtok_matches(src@, toks@[x as int], ts.toks[x as int])); + lemma_node_tok_at(a, ts, e as int, x as int); + } + let r1 = range_is(src, t.start, t.end, &[34, 117, 115, 101, 32, 115, 116, 114, 105, 99, 116, 34]); + let r2 = range_is(src, t.start, t.end, &[39, 117, 115, 101, 32, 115, 116, 114, 105, 99, 116, 39]); + proof { + let c_ = ks[k as int]; + assert(c_ == c as int); + assert(kids(a, c as int) == a.nodes[c as int].children); + assert(kid(kids(a, c_), 0) == e as int); + assert(node_tok(a, ts, e as int) == ts.toks[x as int]); + assert(ts.toks[x as int].text =~= src@.subrange(t.start as int, t.end as int)); + assert(is_directive(a, c_)); + lemma_use_strict_words(); + assert(r1 == (ts.toks[x as int].text =~= cps("\"use strict\""@))); + assert(r2 == (ts.toks[x as int].text =~= cps("'use strict'"@))); + assert(is_use_strict_directive(a, ts, c_) == (ts.toks[x as int].text =~= cps( + "\"use strict\""@, + ) || ts.toks[x as int].text =~= cps("'use strict'"@))); + } + if r1 || r2 { + proof { + assert(is_use_strict_directive(a, ts, ks[k as int])); + assert(0 <= k as int && (k as int) < ks.len() && is_use_strict_directive( + a, + ts, + #[trigger] ks[k as int], + ) && (forall|m: int| 0 <= m <= k as int ==> is_directive(a, #[trigger] ks[m]))); + assert(has_use_strict(a, ts, ks)); + } + return Some(true); + } + proof { + assert(!is_use_strict_directive(a, ts, ks[k as int])); + } + k = k.saturating_add(1); + } + proof { + lemma_no_use_strict(a, ts, ks, len as int); + } + Some(false) +} + +// --------------------------------------------------------------------- +// Parameters. +// --------------------------------------------------------------------- + +/// Each parameter of the `FormalParameters` node `p` is a plain `BindingIdentifier`, without an +/// initializer (`is_simple_params`). +pub fn simple_params_exec(nodes: &[Node], p: usize) -> (r: bool) + requires + p < nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + r ==> is_simple_params(arena_of(nodes@), p as int), +{ + let ghost a = arena_of(nodes@); + let nd = match nodes.get(p) { + Some(x) => x, + None => { + return false; + }, + }; + let len = nd.children.len(); + proof { + assert(a.nodes[p as int] == nodes@[p as int]@); + assert(nd == &nodes@[p as int]); + assert(a.nodes[p as int].children.len() == len); + } + let mut k: usize = 0; + while k < len + invariant + k <= len, + len == nd.children.len(), + p < nodes.len(), + nd == &nodes@[p as int], + a == arena_of(nodes@), + ast_wf(a), + len == a.nodes[p as int].children.len(), + forall|q: int| + 0 <= q < k ==> { + let e = #[trigger] kids(a, p as int)[q]; + kd(a, e) == NodeKind::BindingElement && first_kid_kind(a, e) + == NodeKind::BindingIdentifier && kd(a, kid(kids(a, e), 1)) + == NodeKind::Absent + }, + decreases len - k, + { + let c = match nd.children.as_slice().get(k) { + Some(c) => *c as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[p as int].children[k as int] == c as int); + lemma_child_before(a, p as int, k as int); + assert(kids(a, p as int) == a.nodes[p as int].children); + } + let ck = match nodes.get(c) { + Some(x) => x.kind, + None => { + return false; + }, + }; + proof { + assert(a.nodes[c as int] == nodes@[c as int]@); + } + if !matches!(ck, NodeKind::BindingElement) { + return false; + } + match (kid_kind(nodes, c, 0), kid_kind(nodes, c, 1)) { + (Some(NodeKind::BindingIdentifier), Some(NodeKind::Absent)) => {}, + _ => { + return false; + }, + } + proof { + assert(kd(a, c as int) == NodeKind::BindingElement); + } + k = k.saturating_add(1); + } + true +} + +/// The names `xs` have no name twice. +pub fn no_dup_exec(toks: &[DTok], xs: &[usize]) -> (r: bool) + requires + idx_ok(toks@, xs@), + ensures + r ==> !has_duplicate(names_of(toks@, xs@)), +{ + let len = xs.len(); + let mut i: usize = 0; + while i < len + invariant + len == xs.len(), + i <= len, + idx_ok(toks@, xs@), + forall|x: int, y: int| + 0 <= x < i && x < y < len ==> !(names_of(toks@, xs@)[x] =~= names_of(toks@, xs@)[y]), + decreases len - i, + { + let ni = match xs.get(i) { + Some(x) => match toks.get(*x) { + Some(t) => t, + None => { + return false; + }, + }, + None => { + return false; + }, + }; + let mut j: usize = i.saturating_add(1); + while j < len + invariant + len == xs.len(), + i < len, + i < j, + j <= len, + idx_ok(toks@, xs@), + ni == &toks@[xs@[i as int] as int], + forall|x: int, y: int| + 0 <= x < i && x < y < len ==> !(names_of(toks@, xs@)[x] =~= names_of(toks@, xs@)[y]), + forall|y: int| i < y < j ==> !(names_of(toks@, xs@)[i as int] =~= names_of(toks@, xs@)[y]), + decreases len - j, + { + let nj = match xs.get(j) { + Some(x) => match toks.get(*x) { + Some(t) => t, + None => { + return false; + }, + }, + None => { + return false; + }, + }; + if seq_eq(ni.name.as_slice(), nj.name.as_slice()) { + return false; + } + j = j.saturating_add(1); + } + i = i.saturating_add(1); + } + proof { + assert forall|x: int, y: int| + 0 <= x < y < names_of(toks@, xs@).len() implies !(#[trigger] names_of(toks@, xs@)[x] + =~= #[trigger] names_of(toks@, xs@)[y]) by { + assert(x < len && y < len); + } + } + true +} + +/// `function_early` of the function-like production with the `FormalParameters` node `params` +/// and the body whose statements are the children of node `body` (none, for `None`), in code +/// that is strict if `env_strict` or if the body has a Use Strict Directive. +#[verifier::rlimit(400)] +pub fn function_early_exec( + tk: &Tk, + nodes: &[Node], + params: usize, + body: Option, + unique: bool, + env_strict: bool, + Ghost(ts): Ghost, +) -> (r: bool) + requires + params < nodes.len(), + match body { + Some(b) => b < nodes.len(), + None => true, + }, + ast_wf(arena_of(nodes@)), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + tk.bn.len() == nodes.len(), + bn_inv(arena_of(nodes@), ts, tk.toks@, tk.bn@, nodes.len() as int), + crate::arena_decls::names_inv(tk, nodes@, ts), + ensures + r ==> { + let a = arena_of(nodes@); + let body_ks = match body { + Some(b) => kids(a, b as int), + None => Seq::empty(), + }; + function_early(a, ts, params as int, body_ks, unique, env_strict || has_use_strict( + a, + ts, + body_ks, + )) + }, +{ + let src = tk.src; + let toks = tk.toks; + let by_start = tk.by_start; + let bn = tk.bn; + let ghost a = arena_of(nodes@); + let (hus, lex) = match body { + Some(b) => { + let h = match use_strict_exec(src, nodes, toks, by_start, b, Ghost(ts)) { + Some(h) => h, + None => { + return false; + }, + }; + let l = match crate::arena_decls::cat_exec(nodes, tk.nm.lt.as_slice(), b, 0) { + Some(l) => l, + None => { + return false; + }, + }; + (h, l) + }, + None => (false, Vec::new()), + }; + let simple = simple_params_exec(nodes, params); + if hus && !simple { + return false; + } + let names = match bn.get(params) { + Some(v) => v, + None => { + return false; + }, + }; + proof { + assert(bn@[params as int] == names); + assert(idx_ok(toks@, names@)); + } + let need_unique = env_strict || hus || unique || !simple; + let distinct = no_dup_exec(toks, names.as_slice()); + if need_unique && !distinct { + return false; + } + let names_dd = crate::arena_decls::decls_from_names(names.as_slice()); + proof { + crate::arena_decls::lemma_decl_ok_names(toks@, names@); + match body { + Some(b) => { + crate::arena_decls::lemma_lex_body(arena_of(nodes@), ts, tk, nodes@, b as int); + }, + None => { + assert(lex@ =~= Seq::::empty()); + }, + } + assert(crate::arena_decls::decl_ok(toks@, lex@)); + } + if !crate::arena_decls::no_intersect_dd(toks, names_dd.as_slice(), lex.as_slice()) { + return false; + } + proof { + let body_ks = match body { + Some(b) => kids(a, b as int), + None => Seq::empty(), + }; + let nb = a.nodes.len() as int; + let strict = env_strict || has_use_strict(a, ts, body_ks); + assert(has_use_strict(a, ts, body_ks) == hus); + let lexn = lex_names_list(a, ts, nb, body_ks, 0, true); + match body { + Some(b) => { + crate::arena_decls::lemma_lex_body(a, ts, tk, nodes@, b as int); + assert(crate::arena_decls::decls_of(toks@, lex@) =~= lexn); + }, + None => { + assert(body_ks =~= Seq::::empty()); + assert(lexn =~= Seq::::empty()); + assert(crate::arena_decls::decls_of(toks@, lex@) =~= Seq::::empty()); + }, + } + let bnames = bound_names(a, ts, params as int); + assert(bnames =~= names_of(toks@, names@)); + assert(crate::arena_decls::dn(toks@, names_dd@) =~= bnames); + assert(plain_names(lexn) =~= crate::arena_decls::dn(toks@, lex@)); + assert(!intersects(bnames, plain_names(lexn))); + if strict || unique || !is_simple_params(a, params as int) { + if env_strict || hus || unique || !simple { + assert(!has_duplicate(names_of(toks@, names@))); + } else { + assert(simple); + assert(is_simple_params(a, params as int)); + } + } + } + true +} + +// --------------------------------------------------------------------- +// Object literals. +// --------------------------------------------------------------------- + +/// `__proto__` as code points. +proof fn lemma_proto_word() + ensures + sv("__proto__"@) =~= seq![0x5Fu32, 0x5F, 0x70, 0x72, 0x6F, 0x74, 0x6F, 0x5F, 0x5F], +{ + reveal_strlit("__proto__"); + assert("__proto__"@ =~= seq!['_', '_', 'p', 'r', 'o', 't', 'o', '_', '_']); +} + +/// The property `c` is a `__proto__: value` entry (`is_proto_entry`). +#[verifier::rlimit(400)] +fn is_proto_exec(tk: &Tk, nodes: &[Node], c: usize, Ghost(ts): Ghost) -> (r: Option) + requires + c < nodes.len(), + ast_wf(arena_of(nodes@)), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + ensures + match r { + Some(v) => v == is_proto_entry(arena_of(nodes@), ts, c as int), + None => true, + }, +{ + let src = tk.src; + let toks = tk.toks; + let by_start = tk.by_start; + let _ = src.len(); + let ghost a = arena_of(nodes@); + let nd = match nodes.get(c) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + assert(a.nodes[c as int] == nodes@[c as int]@); + assert(nd == &nodes@[c as int]); + assert(a.nodes[c as int].kind == nd.kind); + assert(a.nodes[c as int].op == nd.op); + assert(a.nodes[c as int].children.len() == nd.children.len()); + assert(kids(a, c as int) == a.nodes[c as int].children); + } + if !matches!(nd.kind, NodeKind::PropertyDefinition) || !matches!(nd.op, Op::KeyValue) { + return Some(false); + } + let key = match nd.children.as_slice().get(ZERO) { + Some(k) => *k as usize, + None => { + return Some(false); + }, + }; + proof { + assert(a.nodes[c as int].children[0] == key as int); + lemma_child_before(a, c as int, 0); + } + let kk = match nodes.get(key) { + Some(x) => x.kind, + None => { + return None; + }, + }; + proof { + assert(a.nodes[key as int] == nodes@[key as int]@); + assert(a.nodes[key as int].kind == kk); + } + if !matches!(kk, NodeKind::IdentifierName | NodeKind::StringLiteral) { + return Some(false); + } + let x = match node_token(nodes, toks, by_start, key) { + Some(x) => x, + None => { + return None; + }, + }; + let t = match toks.get(x) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + assert(t == &toks@[x as int]); + assert(dtok_matches(src@, toks@[x as int], ts.toks[x as int])); + lemma_node_tok_at(a, ts, key as int, x as int); + lemma_proto_word(); + } + Some(seq_eq(t.name.as_slice(), &[0x5F, 0x5F, 0x70, 0x72, 0x6F, 0x74, 0x6F, 0x5F, 0x5F])) +} + +/// `early_local` of an `ObjectLiteral` node `m`: at most one `__proto__: v` entry, and no method +/// with a private name. +#[verifier::rlimit(400)] +pub fn object_literal_exec(tk: &Tk, nodes: &[Node], m: usize, Ghost(ts): Ghost) -> (r: bool) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + ensures + r ==> { + let a = arena_of(nodes@); + let ks = a.nodes[m as int].children; + (!exists|x: int, y: int| + 0 <= x < y < ks.len() && is_proto_entry(a, ts, #[trigger] ks[x]) && is_proto_entry( + a, + ts, + #[trigger] ks[y], + )) && (forall|x: int| + 0 <= x < ks.len() ==> !(kd(a, #[trigger] ks[x]) == NodeKind::MethodDefinition && kd( + a, + kid(kids(a, ks[x]), 0), + ) == NodeKind::PrivateIdentifier)) + }, +{ + let ghost a = arena_of(nodes@); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + let len = nd.children.len(); + proof { + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children.len() == len); + } + let ghost ks = a.nodes[m as int].children; + let mut seen = false; + let mut k: usize = 0; + while k < len + invariant + k <= len, + len == nd.children.len(), + m < nodes.len(), + nd == &nodes@[m as int], + a == arena_of(nodes@), + ast_wf(a), + toks_match(tk.src@, tk.toks@, ts), + starts_inc(ts), + ks == a.nodes[m as int].children, + len == ks.len(), + seen ==> exists|x: int| 0 <= x < k && is_proto_entry(a, ts, #[trigger] ks[x]), + !seen ==> forall|x: int| 0 <= x < k ==> !is_proto_entry(a, ts, #[trigger] ks[x]), + forall|x: int, y: int| + 0 <= x < y < k ==> !(is_proto_entry(a, ts, #[trigger] ks[x]) && is_proto_entry( + a, + ts, + #[trigger] ks[y], + )), + forall|x: int| + 0 <= x < k ==> !(kd(a, #[trigger] ks[x]) == NodeKind::MethodDefinition && kd( + a, + kid(kids(a, ks[x]), 0), + ) == NodeKind::PrivateIdentifier), + decreases len - k, + { + let c = match nd.children.as_slice().get(k) { + Some(c) => *c as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[m as int].children[k as int] == c as int); + lemma_child_before(a, m as int, k as int); + } + let is_p = match is_proto_exec(tk, nodes, c, Ghost(ts)) { + Some(v) => v, + None => { + return false; + }, + }; + if is_p && seen { + return false; + } + let ck = match nodes.get(c) { + Some(x) => x.kind, + None => { + return false; + }, + }; + proof { + assert(a.nodes[c as int] == nodes@[c as int]@); + assert(a.nodes[c as int].kind == ck); + } + if matches!(ck, NodeKind::MethodDefinition) { + match kid_kind(nodes, c, 0) { + Some(NodeKind::PrivateIdentifier) => { + return false; + }, + Some(_) => {}, + None => { + return false; + }, + } + } + proof { + assert(kids(a, c as int) == a.nodes[c as int].children); + assert(ks[k as int] == c as int); + } + if is_p { + seen = true; + } + k = k.saturating_add(1); + } + true +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/exec.rs b/crates/clazel-js-syntax/src/exec.rs index d8ad9ec9a..19aa1da19 100644 --- a/crates/clazel-js-syntax/src/exec.rs +++ b/crates/clazel-js-syntax/src/exec.rs @@ -25,6 +25,17 @@ pub fn parse(src: &[u32], goal: SourceGoal) -> Result { /// with `Verified` carries a proof of `spec::derives_root` (modulo the lexer), and /// with `Unsupported` is only tested. pub fn parse_checked(src: &[u32], goal: SourceGoal) -> Result<(Ast, Derivation), ParseError> { + parse_checked_early(src, goal).map(|(ast, d, _)| (ast, d)) +} + +/// [`parse_checked`], and what [`crate::arena_early::check_early_canon`] found of the arena: +/// `Verified` (the third component) means the arena has no early error +/// (`spec::early_errors_ok`), so `Verified` in both means `spec::is_valid_parse` holds, modulo +/// the assumptions of [`crate::arena_d`]; `Unsupported` that a rule or node kind is not covered. +pub fn parse_checked_early( + src: &[u32], + goal: SourceGoal, +) -> Result<(Ast, Derivation, Derivation), ParseError> { // Recursion is bounded by `MAX_DEPTH` nested nodes, each a few frames; a // thread with room for that is cheaper than a stack overflow. Short // sources cannot nest that deep. @@ -45,7 +56,7 @@ pub fn parse_checked(src: &[u32], goal: SourceGoal) -> Result<(Ast, Derivation), }) } -fn parse_inner(src: &[u32], goal: SourceGoal) -> Result<(Ast, Derivation), ParseError> { +fn parse_inner(src: &[u32], goal: SourceGoal) -> Result<(Ast, Derivation, Derivation), ParseError> { let module = goal == SourceGoal::Module; let mut p = Parser::new(src, module)?; let root = p.parse_program()?; @@ -67,7 +78,8 @@ fn parse_inner(src: &[u32], goal: SourceGoal) -> Result<(Ast, Derivation), Parse if d == Derivation::Failed { return Err(ParseError::Syntax); } - Ok((ast, d)) + let early = crate::arena_early::check_early_canon(src, &ast.nodes, &p.rec, &gaps, module); + Ok((ast, d, early)) } /// The nodes reachable from `root`, in their original order, renumbered. The diff --git a/crates/clazel-js-syntax/src/lib.rs b/crates/clazel-js-syntax/src/lib.rs index 82b96270f..4d9f545c3 100644 --- a/crates/clazel-js-syntax/src/lib.rs +++ b/crates/clazel-js-syntax/src/lib.rs @@ -39,14 +39,18 @@ //! parse ([`contract::parse_ensures`]). So [`exec::parse`] does not implement //! [`api::JsParser`], and the crate stays at Tier 0. The first steps towards //! that proof are in [`arena_d`], a proven check of `spec::d` over the whole arena -//! against the tokens the parser consumed, for the productions it covers (a -//! `Script` or `Module` of empty and expression statements over literals, -//! parentheses and `+ - * / % < >`), run by [`exec::parse_checked`]; and in [`derive`]: rule lemmas that a node of the shape the parser -//! builds is derived by [`spec::d`] (literals, names, keywords, unary, binary, -//! conditional, assignment, parenthesized and member-dot expressions), and -//! `check_leaf`, a checked leaf against the executable tokens. They are not yet -//! applied to the parser's output, and they assume the tokens are the lexer -//! specification's, which the executable lexer does not meet yet. +//! against the tokens the parser consumed, for the productions it covers (every +//! statement and nearly every expression, function, class and pattern; the list is +//! at the top of the module, and 1568 of the 1568 vendored positive Test262 runs +//! are covered end to end), run by [`exec::parse_checked`]; and in [`derive`]: rule +//! lemmas that a node of the shape the parser builds is derived by [`spec::d`]. +//! The check assumes the tokens are the lexer specification's, which the +//! executable lexer does not meet yet, and that inserted `;` are justified. +//! [`arena_e`] and [`arena_m`] cover the rest of the script grammar and every +//! `import` and `export` form. [`arena_early`] is the same for the early errors: +//! a proven check of `spec::early` over the arena for a growing set of rules (the +//! list is at the top of the module), run by [`exec::parse_checked_early`]; where +//! both checks say `Verified`, `spec::is_valid_parse` holds, modulo the assumptions. //! //! Where the parser differs from the specification, on purpose or not: //! @@ -57,6 +61,11 @@ pub mod api; pub mod arena_d; +pub mod arena_decls; +pub mod arena_e; +pub mod arena_early; +pub mod arena_m; +pub mod arena_names; pub mod ast; pub mod check; pub mod contract; @@ -71,3 +80,6 @@ pub mod parser; pub mod spec; pub mod stmt; pub mod token; + +#[cfg(kani)] +mod proofs; diff --git a/crates/clazel-js-syntax/src/proofs.rs b/crates/clazel-js-syntax/src/proofs.rs new file mode 100644 index 000000000..c54bf19f7 --- /dev/null +++ b/crates/clazel-js-syntax/src/proofs.rs @@ -0,0 +1,120 @@ +//! Kani harnesses for `clazel-js-syntax` (Tier 1): the small total functions +//! and the loops over bounded inputs, checked for panics, overflow and +//! out-of-bounds access with symbolic inputs. +//! +//! Not harnessed, because CBMC does not finish in reasonable time (measured): +//! `token::lex` on one symbolic code point, `exec::parse` on one code point, +//! `Parser::new`/`bump`, and `name_is_reserved`, `name_is_reserved_always`, +//! `name_is_strict_reserved` (each scans the 63-word keyword table). The +//! recursive-descent parser itself is covered by the Verus proofs and tests, +//! not by these harnesses. + +use crate::ast::{Node, NodeKind, Op}; +use crate::check::finish; +use crate::exec::{compact, is_root_kind}; +use crate::parser::Cx; +use crate::token::*; +use clazel_js_lexer::spec::{ElementKind, TemplateKind}; + +fn node(kind: NodeKind, start: usize, end: usize, children: Vec) -> Node { + Node { + kind, + op: Op::None, + flag: false, + start, + end, + children, + } +} + +#[kani::proof] +#[kani::unwind(8)] +fn name_is_total() { + let a: [u32; 3] = kani::any(); + let n: usize = kani::any(); + kani::assume(n <= 3); + let _ = name_is(&a[..n], "await"); +} + +#[kani::proof] +fn tok_predicates_total() { + let kinds = [ + ElementKind::IdentifierName, + ElementKind::Punctuator, + ElementKind::Template(TemplateKind::Head), + ]; + let i: usize = kani::any(); + kani::assume(i < 3); + let t = Tok { + kind: kinds[i], + start: kani::any(), + end: kani::any(), + nl: kani::any(), + eof: kani::any(), + s: "(", + name: Vec::new(), + escaped: kani::any(), + bigint: kani::any(), + legacy: kani::any(), + invalid_escape: kani::any(), + }; + let _ = (t.is_p("("), t.is_w("("), t.is_name()); + let _ = t.is_tpl(TemplateKind::Head); + let _ = dtok_of(&t); +} + +#[kani::proof] +#[kani::unwind(4)] +fn compact_one_node() { + let c: u32 = kani::any(); + let root: u32 = kani::any(); + kani::assume(root < 2); + let nodes = vec![node(NodeKind::Absent, 0, 0, vec![c])]; + let out = compact(nodes, root); + assert!(out.len() <= 1); +} + +#[kani::proof] +#[kani::unwind(5)] +fn finish_two_nodes() { + let c: u32 = kani::any(); + let len: usize = kani::any(); + let (s0, e0, s1, e1): (usize, usize, usize, usize) = + (kani::any(), kani::any(), kani::any(), kani::any()); + let root_first: bool = kani::any(); + let k0 = if root_first { + NodeKind::Script + } else { + NodeKind::Absent + }; + let nodes = vec![ + node(k0, s0, e0, Vec::new()), + node(NodeKind::Script, s1, e1, vec![c]), + ]; + let _ = finish(nodes, len); +} + +#[kani::proof] +fn is_root_kind_total() { + let _ = is_root_kind(NodeKind::Script); +} + +#[kani::proof] +fn parser_cx() { + let cx = Cx { + y: kani::any(), + a: kani::any(), + i: kani::any(), + r: kani::any(), + }; + let _ = cx.with_in(kani::any()); +} + +#[kani::proof] +#[kani::unwind(6)] +fn has_legacy_escape_total() { + let a: [u32; 4] = kani::any(); + let n: usize = kani::any(); + kani::assume(n <= 4); + let _ = has_legacy_escape(&a[..n]); +} diff --git a/crates/clazel-js-syntax/src/token.rs b/crates/clazel-js-syntax/src/token.rs index 5c18f48eb..0408418ea 100644 --- a/crates/clazel-js-syntax/src/token.rs +++ b/crates/clazel-js-syntax/src/token.rs @@ -205,7 +205,7 @@ pub fn lex(src: &[u32], pos: usize, goal: Goal, html: bool) -> Option { /// The source text of a `StringLiteral` holds a `LegacyOctalEscapeSequence` or /// a `NonOctalDecimalEscapeSequence` (§12.9.4.1): `\1` to `\9`, or `\0` followed /// by a decimal digit. -fn has_legacy_escape(text: &[u32]) -> bool { +pub(crate) fn has_legacy_escape(text: &[u32]) -> bool { let digit = |c: Option<&u32>| c.is_some_and(|c| (0x30..=0x39).contains(c)); let mut i = 0usize; while let Some(c) = text.get(i) { @@ -251,6 +251,9 @@ pub fn dtok_of(t: &Tok) -> crate::arena_d::DTok { end: t.end, nl: t.nl, escaped: t.escaped, + legacy: t.legacy, + invalid_escape: t.invalid_escape, + name: t.name.clone(), } } diff --git a/crates/clazel-js-syntax/tests/derivation.rs b/crates/clazel-js-syntax/tests/derivation.rs index fa20d5535..bab638be5 100644 --- a/crates/clazel-js-syntax/tests/derivation.rs +++ b/crates/clazel-js-syntax/tests/derivation.rs @@ -82,6 +82,69 @@ var c;", "(async a => await a);", "(async (a) => 1);", "function f() { function g() { return 1 } return g }", + "`a`;", + "`a${b}c`;", + "`${a}${b}`;", + "`a${b}c${d + 1}e${`n${f}`}`;", + "f`x${y}z`;", + "a.b`c`.d;", + "class A {}", + "class A extends B { }", + "class A { m() {} static s(a) { return a } get g() { return 1 } set g(v) {} }", + "class A { *gen() { yield 1 } async am() { await 1 } async *ag() { yield 1 } }", + "class A { x; y = 1; static z = 2; 'q' = 3; [k] = 4; }", + "class A { #p = 1; #m() {} static #s; get #g() { return this.#p } m(o) { return o.#p.x } }", + "class A { ; ; m() {} ; }", + "class A { static { a; } static { var x = 1; } }", + "(class {}); (class B extends C {});", + "x = {m() {}, get a() { return 1 }, set a(v) {}, *g() {}, async f() {}, [k]() {}};", + "a++; b--; ++c; --d.e;", + "x = a[0] + b[c][d];", + "new a;", + "new a();", + "new a.b(1, 2);", + "new new a()();", + "new (f())();", + "[a] = b;", + "[a, , b = 1, ...c.d] = e;", + "({a} = b);", + "({a = 1, b: c, d: [e], f: {g}, ...h} = i);", + "[a.b, c.d] = d;", + "var [a] = 1;", + "var [a, , b = 2, ...c] = d;", + "var {a, b: c, d = 3, ...e} = f;", + "let {a: {b}, c: [d, e]} = g;", + "const [x, [y, z], {w}] = h;", + "function f({a, b}, [c], d = 1, ...[e, f]) {}", + "function g(...{length}) {}", + "try { a } catch ({message}) { b }", + "[];", + "[1, 2, 3];", + "[1, 2,];", + "[,];", + "[, , 1, , 2, ,];", + "[...a, b, ...c];", + "[[1], [2, [3]]];", + "({});", + "({a});", + "({a: 1, b: 2,});", + "({'a': 1, 2: 3, [k]: 4, ...r});", + "({a, b: [1], c: {d: e}});", + "var o = {x: 1}, p = [o];", + "for (var k of [1, 2]) ;", + "for (;;) ;", + "for (;;) { break; }", + "for (a = 0; a < 10; a = a + 1) b;", + "for (var i = 0, n = 5; i < n; i = i + 1) { c }", + "for (let i = 0; i < n; i = i + 1) c;", + "for (const i = 0;;) c;", + "for (var i = 0 + 1 * 2;;) ;", + "for (x of y) z;", + "for (let x in o) z;", + "for (x in o) ;", + "for (const a of b.c) { d }", + "for (var i in a, b) ;", + "for (a in b) for (c of d) e;", "while (a) b;", "while (a) { b; c }", "do a; while (b)", @@ -116,9 +179,228 @@ var c;", } } +/// The remaining script syntax: optional chains, `super`, `new.target`, `import()`, +/// `import.meta`, `for await`, Annex B's `for (var x = 1 in o)` and function declarations +/// as statement bodies, patterns as `for` heads, and operands of `[~In]` heads. +#[test] +fn remaining_script_syntax_is_verified() { + for s in [ + "a?.b;", + "a?.[b];", + "a?.();", + "a?.b.c(d)?.[e];", + "a?.b\n.c?.d(e)\n[f];", + "class A extends B { constructor() { super(); super(1, ...a); } m() { super.x; super[x](); } }", + "x = { m() { return super.m; } };", + "function f() { new.target; new.target.x; }", + "function f() { return a?.b?.(c)?.[d]; }", + "class A { #x; m() { return this?.#x; } }", + "import('a');", + "import('a', {});", + "import('a',);", + "import('a', b,);", + "async function f() { for await (x of y) ; }", + "async function f() { for await (const [a, b] of c) d; }", + "for (var x = 1 in o) ;", + "if (a) function f() {} else function g() {}", + "l: function f() {}", + "for (let [a, b] of o) ;", + "for (const {a, b: [c = 1]} of o) ;", + "for (var [a] in o) ;", + "for ([a, b = 1, ...c] of o) ;", + "for ({a, b: c = 2, ...d} in o) ;", + "for (a.b[c] of d) ;", + "for (f().x of y) ;", + "for (let [a = (1, 2)] of b) ;", + "for (a, b;;) ;", + "for (i = 0, j = 1; i < j; i++) ;", + "for (var i = a ? b : c; ;) ;", + "for (var i = f(1), j = new A(2); ;) ;", + "for (var i = a.b[c]; ;) ;", + "for (var i = (1, 2); ;) ;", + "for (var i = [1, ...a]; ;) ;", + "for (var i = {a: 1, ...b, [c]: 2, m() {}}; ;) ;", + "for (var i = `a${b}`; ;) ;", + "for (var i = a`x`; ;) ;", + "for (var i = a?.b; ;) ;", + "for (var i = ({a} = b); ;) ;", + "for (var i = 0, j = (1, 2); i < j; i++) ;", + ] { + assert!( + derivation(s, SourceGoal::Script) == Some(Derivation::Verified), + "{s}" + ); + } +} + +/// `import.meta` is in a `Module` only. +#[test] +fn import_meta_is_verified_in_a_module() { + for s in ["import.meta;", "import.meta.url;", "f(import.meta);"] { + assert!( + derivation(s, SourceGoal::Module) == Some(Derivation::Verified), + "{s}" + ); + assert!(derivation(s, SourceGoal::Script).is_none(), "{s}"); + } +} + +/// Every form of `import` and `export` (§16.2). +#[test] +fn module_items_are_verified() { + for s in [ + "import a from 'b';", + "import 'b';", + "import * as ns from 'b';", + "import a, * as ns from 'b';", + "import a, { b, c as d } from 'b';", + "import { } from 'b';", + "import { a, } from 'b'\nx", + "import { \"str\" as s } from 'b';", + "import { default as x } from 'b';", + "import a from 'b' with { type: 'json' };", + "import 'b' with { };", + "import a from 'b' with { 'type': 'json', };", + "export default 1;", + "export default function () {}", + "export default function f() {}", + "export default async function () {}", + "export default function* () {}", + "export default class {}", + "export default class A { m() {} static s() {} }", + "export default a + b", + "export { };", + "var a; export { a as \"b\", a as c, };", + "export { a } from 'b';", + "export { a as b } from 'b' with { type: 'json' };", + "export * from 'b';", + "export * as ns from 'b';", + "export * as \"x y\" from 'b' with { a: 'b' };", + "export const x = 1, y = 2;", + "export let z;", + "export var v;", + "export function f() {}", + "export async function f() {}", + "export class A {}", + "import.meta; await 1; export default await 1;", + ] { + assert!( + derivation(s, SourceGoal::Module) == Some(Derivation::Verified), + "{s}" + ); + } +} + +/// The last gaps of the script grammar: `let` as an identifier head, and `await`, `yield` +/// and classes in `[~In]` heads. +#[test] +fn leftover_script_gaps_are_verified() { + for s in [ + "for (let in o) ;", + "for (var i = class {}; ;) ;", + "for (var i = class A extends B { m() {} }; ;) ;", + "async function f() { for (var i = await x; ;) ; }", + "function* g() { for (var i = yield x; ;) ; }", + ] { + assert!( + derivation(s, SourceGoal::Script) == Some(Derivation::Verified), + "{s}" + ); + } +} + +fn early(s: &str) -> Option { + clazel_js_syntax::exec::parse_checked_early(&cps(s), SourceGoal::Script) + .ok() + .map(|(_, _, e)| e) +} + +/// The early rules the proven check covers. +#[test] +fn covered_early_rules_are_verified() { + for s in [ + "", + "1 + 2;", + "a = b; a.c = d; a[0]++; ++a.b;", + "010; '\\07';", + "yield = 1; var await = 2; let = 3;", + "`a${b}c`; f`\\u{`;", + "if (a) b; else c;", + "while (a) { b; if (c) break; else continue; }", + "do { break; } while (a);", + "for (;;) { break; }", + "for (var i = 0; i < n; i++) { continue; }", + "switch_ = 1; with (a) b;", + "a?.b?.[c]?.(d);", + "delete a.b; typeof a; void 0; a in b; a instanceof b;", + "{ a; { b; } }", + "var a; var a;", + "'use strict'; a = 1;", + "let a; const b = 1; { let a; var c; function f() {} function f() {} }", + "try {} catch (e) { var e; } finally { let z; }", + "try {} catch ([a, b]) { let c; }", + "for (let i = 0; i < 1; i++) { var j; }", + "function f(a) { let b; { let a; } }", + "x = { m(a) { return a; }, get g() { return 1; }, __proto__: null, ['__proto__']: 1 };", + "(function () {})", + "function f(a, [b], {c}, ...d) { return a; }", + "function f(a, a) { }", + "(a, b) => a + b;", + "function* g(x) { yield x; } async function h() { await 1; }", + "function f() { 'use strict'; return 1; }", + "new.target", + ] { + let r = early(s); + if s == "new.target" { + assert!(r.is_none(), "{s}"); + } else { + assert!(r == Some(Derivation::Verified), "{s}"); + } + } +} + +/// What the proven early-error check leaves alone. +#[test] +fn other_early_rules_are_unsupported_not_failed() { + for s in [ + "a: b;", + "class A {}", + "for (a in b) ;", + "/a/;", + "a.#b;", + "switch (a) { case 1: }", + ] { + let r = early(s); + assert!( + r != Some(Derivation::Verified) && r != Some(Derivation::Failed), + "{s}" + ); + } +} + +/// Productions only a sloppy-mode `Script` has. +#[test] +fn covered_sloppy_programs_are_verified() { + for s in [ + "with (a) b;", + "with (a) { b = 1; }", + "for (a.b of c) ;", + "for (a.b.c in d) ;", + "for (x of y) with (x) z;", + ] { + assert!( + derivation(s, SourceGoal::Script) == Some(Derivation::Verified), + "{s}" + ); + } +} + #[test] fn other_programs_are_unsupported_not_failed() { - for s in ["for (;;) ;", "var [a] = 1;", "[1];", "({});"] { + for s in [ + "for (var i = class { x = 1 };;) ;", + "for (var i = class { #p; };;) ;", + ] { assert!( derivation(s, SourceGoal::Script) == Some(Derivation::Unsupported), "{s}" @@ -141,7 +423,7 @@ fn errors_are_still_errors() { fn what_the_check_costs() { let body = "(1 + 2 * 3) < 4 - 5 / 6 % 7;\n".repeat(60_000); let covered = cps(&body); - let stopped = cps(&format!("[];\n{body}")); + let stopped = cps(&format!("for (var i = class {{ x = 1 }};;) ;\n{body}")); let time = |src: &[u32]| { let t = Instant::now(); let r = parse_checked(src, SourceGoal::Script); diff --git a/crates/clazel-js-syntax/tests/test262.rs b/crates/clazel-js-syntax/tests/test262.rs index baa87faaa..dad0e302d 100644 --- a/crates/clazel-js-syntax/tests/test262.rs +++ b/crates/clazel-js-syntax/tests/test262.rs @@ -249,16 +249,18 @@ fn test262_fixtures_parse() -> Result<(), String> { Ok(()) } -/// How much of Test262 the proven derivation check ([`clazel_js_syntax::arena_d`]) -/// covers: the share of the positive tests and of the harness files whose whole arena -/// comes out `Verified`, not `Unsupported`. Printed, not asserted: it grows as the -/// check covers more of the grammar. +/// How much of Test262 the proven checks ([`clazel_js_syntax::arena_d`]) cover: the +/// share of the positive runs (scripts and modules apart) and of the harness files +/// whose whole arena comes out `Verified`, not `Unsupported`. Printed, not +/// asserted: it grows as the checks cover more of the grammar. #[test] #[cfg_attr(miri, ignore = "reads files")] fn test262_verified_share() -> Result<(), String> { use clazel_js_syntax::arena_d::Derivation; - use clazel_js_syntax::exec::parse_checked; + use clazel_js_syntax::exec::{parse_checked, parse_checked_early}; let (mut positives, mut verified) = (0usize, 0usize); + let (mut mpositives, mut mverified) = (0usize, 0usize); + let (mut early, mut both, mut mearly, mut mboth) = (0usize, 0usize, 0usize, 0usize); for case in TEST262 { if !judged(case) || case.negative.is_some() { continue; @@ -270,9 +272,20 @@ fn test262_verified_share() -> Result<(), String> { s.extend("\"use strict\";\n".chars().map(u32::from)); } s.extend(src.iter().copied()); - positives += 1; - if let Ok((_, Derivation::Verified)) = parse_checked(&s, goal) { - verified += 1; + let (ok, eok) = match parse_checked_early(&s, goal) { + Ok((_, d, e)) => (d == Derivation::Verified, e == Derivation::Verified), + Err(_) => (false, false), + }; + if goal == SourceGoal::Module { + mpositives += 1; + mverified += usize::from(ok); + mearly += usize::from(eok); + mboth += usize::from(ok && eok); + } else { + positives += 1; + verified += usize::from(ok); + early += usize::from(eok); + both += usize::from(ok && eok); } } } @@ -287,6 +300,99 @@ fn test262_verified_share() -> Result<(), String> { hv += 1; } } - println!("Verified: {verified} of {positives} positive runs, {hv} of {hn} harness files"); + // The `_FIXTURE.js` modules the tests import. + let mut fixtures = Vec::new(); + all_js(&root(), &mut fixtures); + let (mut fv, mut fnn) = (0usize, 0usize); + for f in fixtures + .iter() + .filter(|f| f.to_string_lossy().ends_with("_FIXTURE.js")) + { + let text = std::fs::read_to_string(f).map_err(|e| e.to_string())?; + let src: Vec = text.chars().map(u32::from).collect(); + if let Ok((_, d)) = parse_checked(&src, SourceGoal::Module) { + fnn += 1; + fv += usize::from(d == Derivation::Verified); + } + } + println!( + "Early errors verified: {early} of {positives} script positive runs ({both} with the derivation too), {mearly} of {mpositives} module positive runs ({mboth} with the derivation too)" + ); + println!( + "Verified: {verified} of {positives} script positive runs, {mverified} of {mpositives} module positive runs, {fv} of {fnn} module fixtures, {hv} of {hn} harness files" + ); + Ok(()) +} + +/// Which constructs the unverified runs contain: a histogram of the kinds the +/// check may not cover. Printed with `--nocapture`; for finding what to cover. +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn test262_unverified_kinds() -> Result<(), String> { + use clazel_js_syntax::arena_d::Derivation; + use clazel_js_syntax::ast::NodeKind as K; + use clazel_js_syntax::exec::parse_checked; + use std::collections::BTreeMap; + let mut hist: BTreeMap<&'static str, usize> = BTreeMap::new(); + let mut note = |src: &[u32], goal: SourceGoal| -> bool { + let Ok((ast, d)) = parse_checked(src, goal) else { + return false; + }; + if d == Derivation::Verified { + return false; + } + let mut seen: BTreeSet<&'static str> = BTreeSet::new(); + for n in &ast.nodes { + let l = match n.kind { + K::OptionalExpression => "optional", + K::SuperProperty => "super.x", + K::SuperCall => "super()", + K::ImportCall => "import()", + K::ImportMeta => "import.meta", + K::NewTarget => "new.target", + K::CoverInitializedName => "cover-init", + K::ImportDeclaration => "import-decl", + K::ExportDeclaration => "export-decl", + K::ForOfStatement if n.flag => "for-await", + K::ForOfStatement | K::ForInStatement => "for-in-of", + K::ForStatement => "for", + _ => continue, + }; + seen.insert(l); + } + if seen.is_empty() { + seen.insert("(none of the listed)"); + } + for l in seen { + *hist.entry(l).or_default() += 1; + } + true + }; + for case in TEST262 { + if !judged(case) || case.negative.is_some() { + continue; + } + let src = read(case)?; + for (goal, strict) in runs(case) { + let mut s: Vec = Vec::new(); + if strict { + s.extend("\"use strict\";\n".chars().map(u32::from)); + } + s.extend(src.iter().copied()); + if note(&s, goal) && std::env::var("SHOW_UNVERIFIED").is_ok() { + println!("unverified {} {:?}", case.path, goal == SourceGoal::Module); + } + } + } + let mut files = Vec::new(); + all_js(&root().join("../harness"), &mut files); + for f in &files { + let text = std::fs::read_to_string(f).map_err(|e| e.to_string())?; + let src: Vec = text.chars().map(u32::from).collect(); + if note(&src, SourceGoal::Script) { + println!("harness unverified: {}", f.display()); + } + } + println!("unverified, by construct: {hist:?}"); Ok(()) } diff --git a/crates/clazel-js-vm/Cargo.toml b/crates/clazel-js-vm/Cargo.toml index d6ea3c203..d646ac939 100644 --- a/crates/clazel-js-vm/Cargo.toml +++ b/crates/clazel-js-vm/Cargo.toml @@ -10,12 +10,17 @@ description = "The JavaScript engine's bytecode and its semantics for Clazel's P vstd = "=0.0.0-2026-10-04-0306" clazel-js-lexer = { path = "../clazel-js-lexer" } clazel-js-syntax = { path = "../clazel-js-syntax" } +clazel-js-heap = { path = "../clazel-js-heap" } +clazel-js-number = { path = "../clazel-js-number" } # Verification tier (docs/VERIFICATION.md). `tier` is what is proven about the # shipped code today: nothing. The crate holds the specification of item 7.4 -# and the compiler of item 7.7 (first and second slice, plain Rust: its evidence -# is unit tests and the Test262 sources it compiles, tests/); the handlers and the -# interpreter loop come later, in 7.8. `target-tier` is 0 for the shipped +# and the compiler of item 7.7 (first to third slice, plain Rust: its evidence +# is unit tests and the Test262 sources it compiles, tests/); and the first +# slice of item 7.8, the interpreter loop and its first handlers (plain Rust: +# unit tests, a differential test against a transcription of `spec fn step`, +# and a Test262 run, tests/; Kani harnesses for its arithmetic and frame +# indexing, src/proofs.rs). `target-tier` is 0 for the shipped # code, as the Crates table of phase-7.md states; the spec itself is written # to Tier 2 standards (Verus checks it and the lemmas in src/lemmas.rs), # because the JIT's front-end proofs (jit.md J.10) refine `step`. diff --git a/crates/clazel-js-vm/mutations.json b/crates/clazel-js-vm/mutations.json index ad21d74ec..1fbae0687 100644 --- a/crates/clazel-js-vm/mutations.json +++ b/crates/clazel-js-vm/mutations.json @@ -1923,6 +1923,1163 @@ "lemma": "crate::examples::example_global_lexical_classes()", "witness": "op_class(Op::InitGlobalLexical { key: 2, src: 0 }) is Core" } + }, + { + "id": "new-reg-exp-dst-wf", + "file": "src/spec.rs", + "find": "Op::NewRegExp { dst, k, .. } => reg_ok(c, dst) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::NewRegExp { dst, k, .. } => k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "NewRegExp accepts a register outside the frame (operand `dst`): example_regex_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_regex_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::NewRegExp { dst: 3, k: 0 })" + } + }, + { + "id": "new-reg-exp-k-wf", + "file": "src/spec.rs", + "find": "Op::NewRegExp { dst, k, .. } => reg_ok(c, dst) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::NewRegExp { dst, k, .. } => reg_ok(c, dst) && k < c.consts.len(),", + "expect": "killed", + "reason": "NewRegExp accepts a record constant that is not a value (operand `k`): example_regex_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_regex_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::NewRegExp { dst: 0, k: 2 })" + } + }, + { + "id": "new-reg-exp-handler", + "file": "src/ops.rs", + "find": "Op::NewRegExp { .. } => HandlerId::NewRegExp,", + "replace": "Op::NewRegExp { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "NewRegExp calls the throw handler instead of its own: example_regex_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_regex_classes()", + "witness": "op_handler(Op::NewRegExp { dst: 0, k: 0 }) == HandlerId::Throw" + } + }, + { + "id": "new-reg-exp-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::NewRegExp { .. } => OpClass::Core,", + "expect": "killed", + "reason": "NewRegExp is a core op, which never calls out: example_regex_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_regex_classes()", + "witness": "op_class(Op::NewRegExp { dst: 0, k: 0 }) is Core" + } + }, + { + "id": "yield-delegate-src-wf", + "file": "src/spec.rs", + "find": "Op::YieldDelegate { src, value, mode, .. } => reg_ok(c, src) && reg_ok(c, value) && reg_ok(c, mode),", + "replace": "Op::YieldDelegate { src, value, mode, .. } => reg_ok(c, value) && reg_ok(c, mode),", + "expect": "killed", + "reason": "YieldDelegate accepts a register outside the frame (operand `src`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::YieldDelegate { src: 3, value: 0, mode: 0 })" + } + }, + { + "id": "yield-delegate-value-wf", + "file": "src/spec.rs", + "find": "Op::YieldDelegate { src, value, mode, .. } => reg_ok(c, src) && reg_ok(c, value) && reg_ok(c, mode),", + "replace": "Op::YieldDelegate { src, value, mode, .. } => reg_ok(c, src) && reg_ok(c, mode),", + "expect": "killed", + "reason": "YieldDelegate accepts a register outside the frame (operand `value`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::YieldDelegate { src: 0, value: 3, mode: 0 })" + } + }, + { + "id": "yield-delegate-mode-wf", + "file": "src/spec.rs", + "find": "Op::YieldDelegate { src, value, mode, .. } => reg_ok(c, src) && reg_ok(c, value) && reg_ok(c, mode),", + "replace": "Op::YieldDelegate { src, value, mode, .. } => reg_ok(c, src) && reg_ok(c, value),", + "expect": "killed", + "reason": "YieldDelegate accepts a register outside the frame (operand `mode`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::YieldDelegate { src: 0, value: 0, mode: 3 })" + } + }, + { + "id": "get-async-iterator-dst-wf", + "file": "src/spec.rs", + "find": "Op::GetAsyncIterator { dst, src, .. } => reg_ok(c, dst) && reg_ok(c, src),", + "replace": "Op::GetAsyncIterator { dst, src, .. } => reg_ok(c, src),", + "expect": "killed", + "reason": "GetAsyncIterator accepts a register outside the frame (operand `dst`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::GetAsyncIterator { dst: 3, src: 0 })" + } + }, + { + "id": "get-async-iterator-src-wf", + "file": "src/spec.rs", + "find": "Op::GetAsyncIterator { dst, src, .. } => reg_ok(c, dst) && reg_ok(c, src),", + "replace": "Op::GetAsyncIterator { dst, src, .. } => reg_ok(c, dst),", + "expect": "killed", + "reason": "GetAsyncIterator accepts a register outside the frame (operand `src`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::GetAsyncIterator { dst: 0, src: 3 })" + } + }, + { + "id": "iter-call-method-dst-wf", + "file": "src/spec.rs", + "find": "Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, dst) && reg_ok(c, found) && reg_ok(c, iter) && reg_ok(c, arg) && which < 5,", + "replace": "Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, found) && reg_ok(c, iter) && reg_ok(c, arg) && which < 5,", + "expect": "killed", + "reason": "IterCallMethod accepts a register outside the frame (operand `dst`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::IterCallMethod { dst: 3, found: 0, iter: 0, arg: 0, which: 0 })" + } + }, + { + "id": "iter-call-method-found-wf", + "file": "src/spec.rs", + "find": "Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, dst) && reg_ok(c, found) && reg_ok(c, iter) && reg_ok(c, arg) && which < 5,", + "replace": "Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, dst) && reg_ok(c, iter) && reg_ok(c, arg) && which < 5,", + "expect": "killed", + "reason": "IterCallMethod accepts a register outside the frame (operand `found`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::IterCallMethod { dst: 0, found: 3, iter: 0, arg: 0, which: 0 })" + } + }, + { + "id": "iter-call-method-iter-wf", + "file": "src/spec.rs", + "find": "Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, dst) && reg_ok(c, found) && reg_ok(c, iter) && reg_ok(c, arg) && which < 5,", + "replace": "Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, dst) && reg_ok(c, found) && reg_ok(c, arg) && which < 5,", + "expect": "killed", + "reason": "IterCallMethod accepts a register outside the frame (operand `iter`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 3, arg: 0, which: 0 })" + } + }, + { + "id": "iter-call-method-arg-wf", + "file": "src/spec.rs", + "find": "Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, dst) && reg_ok(c, found) && reg_ok(c, iter) && reg_ok(c, arg) && which < 5,", + "replace": "Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, dst) && reg_ok(c, found) && reg_ok(c, iter) && which < 5,", + "expect": "killed", + "reason": "IterCallMethod accepts a register outside the frame (operand `arg`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 3, which: 0 })" + } + }, + { + "id": "iter-call-method-which-wf", + "file": "src/spec.rs", + "find": "Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, dst) && reg_ok(c, found) && reg_ok(c, iter) && reg_ok(c, arg) && which < 5,", + "replace": "Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, dst) && reg_ok(c, found) && reg_ok(c, iter) && reg_ok(c, arg) && which < 6,", + "expect": "killed", + "reason": "IterCallMethod accepts an enumerated operand one past its bound (operand `which`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 5 })" + } + }, + { + "id": "iter-complete-done-wf", + "file": "src/spec.rs", + "find": "Op::IterComplete { done, result, .. } => reg_ok(c, done) && reg_ok(c, result),", + "replace": "Op::IterComplete { done, result, .. } => reg_ok(c, result),", + "expect": "killed", + "reason": "IterComplete accepts a register outside the frame (operand `done`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::IterComplete { done: 3, result: 0 })" + } + }, + { + "id": "iter-complete-result-wf", + "file": "src/spec.rs", + "find": "Op::IterComplete { done, result, .. } => reg_ok(c, done) && reg_ok(c, result),", + "replace": "Op::IterComplete { done, result, .. } => reg_ok(c, done),", + "expect": "killed", + "reason": "IterComplete accepts a register outside the frame (operand `result`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::IterComplete { done: 0, result: 3 })" + } + }, + { + "id": "iter-value-dst-wf", + "file": "src/spec.rs", + "find": "Op::IterValue { dst, result, .. } => reg_ok(c, dst) && reg_ok(c, result),", + "replace": "Op::IterValue { dst, result, .. } => reg_ok(c, result),", + "expect": "killed", + "reason": "IterValue accepts a register outside the frame (operand `dst`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::IterValue { dst: 3, result: 0 })" + } + }, + { + "id": "iter-value-result-wf", + "file": "src/spec.rs", + "find": "Op::IterValue { dst, result, .. } => reg_ok(c, dst) && reg_ok(c, result),", + "replace": "Op::IterValue { dst, result, .. } => reg_ok(c, dst),", + "expect": "killed", + "reason": "IterValue accepts a register outside the frame (operand `result`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::IterValue { dst: 0, result: 3 })" + } + }, + { + "id": "throw-if-not-object-src-wf", + "file": "src/spec.rs", + "find": "Op::ThrowIfNotObject { src, .. } => reg_ok(c, src),", + "replace": "Op::ThrowIfNotObject { src, .. } => true,", + "expect": "killed", + "reason": "ThrowIfNotObject accepts a register outside the frame (operand `src`): example_generators_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::ThrowIfNotObject { src: 3 })" + } + }, + { + "id": "generator-start-handler", + "file": "src/ops.rs", + "find": "Op::GeneratorStart => HandlerId::GeneratorStart,", + "replace": "Op::GeneratorStart => HandlerId::Throw,", + "expect": "killed", + "reason": "GeneratorStart calls the throw handler instead of its own: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_handler(Op::GeneratorStart) == HandlerId::Throw" + } + }, + { + "id": "generator-start-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::GeneratorStart => OpClass::Core,", + "expect": "killed", + "reason": "GeneratorStart is a core op, which never calls out: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_class(Op::GeneratorStart) is Core" + } + }, + { + "id": "yield-delegate-handler", + "file": "src/ops.rs", + "find": "Op::YieldDelegate { .. } => HandlerId::YieldDelegate,", + "replace": "Op::YieldDelegate { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "YieldDelegate calls the throw handler instead of its own: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_handler(Op::YieldDelegate { src: 0, value: 0, mode: 0 }) == HandlerId::Throw" + } + }, + { + "id": "yield-delegate-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::YieldDelegate { .. } => OpClass::Core,", + "expect": "killed", + "reason": "YieldDelegate is a core op, which never calls out: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_class(Op::YieldDelegate { src: 0, value: 0, mode: 0 }) is Core" + } + }, + { + "id": "get-async-iterator-handler", + "file": "src/ops.rs", + "find": "Op::GetAsyncIterator { .. } => HandlerId::GetAsyncIterator,", + "replace": "Op::GetAsyncIterator { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "GetAsyncIterator calls the throw handler instead of its own: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_handler(Op::GetAsyncIterator { dst: 0, src: 0 }) == HandlerId::Throw" + } + }, + { + "id": "get-async-iterator-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::GetAsyncIterator { .. } => OpClass::Core,", + "expect": "killed", + "reason": "GetAsyncIterator is a core op, which never calls out: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_class(Op::GetAsyncIterator { dst: 0, src: 0 }) is Core" + } + }, + { + "id": "iter-call-method-handler", + "file": "src/ops.rs", + "find": "Op::IterCallMethod { .. } => HandlerId::IterCallMethod,", + "replace": "Op::IterCallMethod { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "IterCallMethod calls the throw handler instead of its own: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_handler(Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 0 }) == HandlerId::Throw" + } + }, + { + "id": "iter-call-method-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::IterCallMethod { .. } => OpClass::Core,", + "expect": "killed", + "reason": "IterCallMethod is a core op, which never calls out: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_class(Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 0 }) is Core" + } + }, + { + "id": "iter-complete-handler", + "file": "src/ops.rs", + "find": "Op::IterComplete { .. } => HandlerId::IterComplete,", + "replace": "Op::IterComplete { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "IterComplete calls the throw handler instead of its own: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_handler(Op::IterComplete { done: 0, result: 0 }) == HandlerId::Throw" + } + }, + { + "id": "iter-complete-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::IterComplete { .. } => OpClass::Core,", + "expect": "killed", + "reason": "IterComplete is a core op, which never calls out: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_class(Op::IterComplete { done: 0, result: 0 }) is Core" + } + }, + { + "id": "iter-value-handler", + "file": "src/ops.rs", + "find": "Op::IterValue { .. } => HandlerId::IterValue,", + "replace": "Op::IterValue { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "IterValue calls the throw handler instead of its own: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_handler(Op::IterValue { dst: 0, result: 0 }) == HandlerId::Throw" + } + }, + { + "id": "iter-value-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::IterValue { .. } => OpClass::Core,", + "expect": "killed", + "reason": "IterValue is a core op, which never calls out: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_class(Op::IterValue { dst: 0, result: 0 }) is Core" + } + }, + { + "id": "throw-if-not-object-handler", + "file": "src/ops.rs", + "find": "Op::ThrowIfNotObject { .. } => HandlerId::ThrowIfNotObject,", + "replace": "Op::ThrowIfNotObject { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "ThrowIfNotObject calls the throw handler instead of its own: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_handler(Op::ThrowIfNotObject { src: 0 }) == HandlerId::Throw" + } + }, + { + "id": "throw-if-not-object-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::ThrowIfNotObject { .. } => OpClass::Core,", + "expect": "killed", + "reason": "ThrowIfNotObject is a core op, which never calls out: example_generators_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_generators_classes()", + "witness": "op_class(Op::ThrowIfNotObject { src: 0 }) is Core" + } + }, + { + "id": "to-object-dst-wf", + "file": "src/spec.rs", + "find": "Op::ToObject { dst, src, .. } => reg_ok(c, dst) && reg_ok(c, src),", + "replace": "Op::ToObject { dst, src, .. } => reg_ok(c, src),", + "expect": "killed", + "reason": "ToObject accepts a register outside the frame (operand `dst`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::ToObject { dst: 3, src: 0 })" + } + }, + { + "id": "to-object-src-wf", + "file": "src/spec.rs", + "find": "Op::ToObject { dst, src, .. } => reg_ok(c, dst) && reg_ok(c, src),", + "replace": "Op::ToObject { dst, src, .. } => reg_ok(c, dst),", + "expect": "killed", + "reason": "ToObject accepts a register outside the frame (operand `src`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::ToObject { dst: 0, src: 3 })" + } + }, + { + "id": "with-has-dst-wf", + "file": "src/spec.rs", + "find": "Op::WithHas { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(),", + "replace": "Op::WithHas { dst, obj, key, .. } => reg_ok(c, obj) && key < c.consts.len(),", + "expect": "killed", + "reason": "WithHas accepts a register outside the frame (operand `dst`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithHas { dst: 3, obj: 0, key: 0 })" + } + }, + { + "id": "with-has-obj-wf", + "file": "src/spec.rs", + "find": "Op::WithHas { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(),", + "replace": "Op::WithHas { dst, obj, key, .. } => reg_ok(c, dst) && key < c.consts.len(),", + "expect": "killed", + "reason": "WithHas accepts a register outside the frame (operand `obj`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithHas { dst: 0, obj: 3, key: 0 })" + } + }, + { + "id": "with-has-key-wf", + "file": "src/spec.rs", + "find": "Op::WithHas { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(),", + "replace": "Op::WithHas { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key <= c.consts.len(),", + "expect": "killed", + "reason": "WithHas accepts a constant index one past the pool (operand `key`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithHas { dst: 0, obj: 0, key: 3 })" + } + }, + { + "id": "with-get-dst-wf", + "file": "src/spec.rs", + "find": "Op::WithGet { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(),", + "replace": "Op::WithGet { dst, obj, key, .. } => reg_ok(c, obj) && key < c.consts.len(),", + "expect": "killed", + "reason": "WithGet accepts a register outside the frame (operand `dst`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithGet { dst: 3, obj: 0, key: 0, strict: false })" + } + }, + { + "id": "with-get-obj-wf", + "file": "src/spec.rs", + "find": "Op::WithGet { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(),", + "replace": "Op::WithGet { dst, obj, key, .. } => reg_ok(c, dst) && key < c.consts.len(),", + "expect": "killed", + "reason": "WithGet accepts a register outside the frame (operand `obj`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithGet { dst: 0, obj: 3, key: 0, strict: false })" + } + }, + { + "id": "with-get-key-wf", + "file": "src/spec.rs", + "find": "Op::WithGet { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(),", + "replace": "Op::WithGet { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key <= c.consts.len(),", + "expect": "killed", + "reason": "WithGet accepts a constant index one past the pool (operand `key`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithGet { dst: 0, obj: 0, key: 3, strict: false })" + } + }, + { + "id": "with-set-obj-wf", + "file": "src/spec.rs", + "find": "Op::WithSet { obj, key, val, .. } => reg_ok(c, obj) && key < c.consts.len() && reg_ok(c, val),", + "replace": "Op::WithSet { obj, key, val, .. } => key < c.consts.len() && reg_ok(c, val),", + "expect": "killed", + "reason": "WithSet accepts a register outside the frame (operand `obj`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithSet { obj: 3, key: 0, val: 0, strict: false })" + } + }, + { + "id": "with-set-key-wf", + "file": "src/spec.rs", + "find": "Op::WithSet { obj, key, val, .. } => reg_ok(c, obj) && key < c.consts.len() && reg_ok(c, val),", + "replace": "Op::WithSet { obj, key, val, .. } => reg_ok(c, obj) && key <= c.consts.len() && reg_ok(c, val),", + "expect": "killed", + "reason": "WithSet accepts a constant index one past the pool (operand `key`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithSet { obj: 0, key: 3, val: 0, strict: false })" + } + }, + { + "id": "with-set-val-wf", + "file": "src/spec.rs", + "find": "Op::WithSet { obj, key, val, .. } => reg_ok(c, obj) && key < c.consts.len() && reg_ok(c, val),", + "replace": "Op::WithSet { obj, key, val, .. } => reg_ok(c, obj) && key < c.consts.len(),", + "expect": "killed", + "reason": "WithSet accepts a register outside the frame (operand `val`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithSet { obj: 0, key: 0, val: 3, strict: false })" + } + }, + { + "id": "with-delete-dst-wf", + "file": "src/spec.rs", + "find": "Op::WithDelete { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(),", + "replace": "Op::WithDelete { dst, obj, key, .. } => reg_ok(c, obj) && key < c.consts.len(),", + "expect": "killed", + "reason": "WithDelete accepts a register outside the frame (operand `dst`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithDelete { dst: 3, obj: 0, key: 0 })" + } + }, + { + "id": "with-delete-obj-wf", + "file": "src/spec.rs", + "find": "Op::WithDelete { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(),", + "replace": "Op::WithDelete { dst, obj, key, .. } => reg_ok(c, dst) && key < c.consts.len(),", + "expect": "killed", + "reason": "WithDelete accepts a register outside the frame (operand `obj`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithDelete { dst: 0, obj: 3, key: 0 })" + } + }, + { + "id": "with-delete-key-wf", + "file": "src/spec.rs", + "find": "Op::WithDelete { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(),", + "replace": "Op::WithDelete { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key <= c.consts.len(),", + "expect": "killed", + "reason": "WithDelete accepts a constant index one past the pool (operand `key`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::WithDelete { dst: 0, obj: 0, key: 3 })" + } + }, + { + "id": "delete-global-dst-wf", + "file": "src/spec.rs", + "find": "Op::DeleteGlobal { dst, key, .. } => reg_ok(c, dst) && key < c.consts.len(),", + "replace": "Op::DeleteGlobal { dst, key, .. } => key < c.consts.len(),", + "expect": "killed", + "reason": "DeleteGlobal accepts a register outside the frame (operand `dst`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DeleteGlobal { dst: 3, key: 0 })" + } + }, + { + "id": "delete-global-key-wf", + "file": "src/spec.rs", + "find": "Op::DeleteGlobal { dst, key, .. } => reg_ok(c, dst) && key < c.consts.len(),", + "replace": "Op::DeleteGlobal { dst, key, .. } => reg_ok(c, dst) && key <= c.consts.len(),", + "expect": "killed", + "reason": "DeleteGlobal accepts a constant index one past the pool (operand `key`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DeleteGlobal { dst: 0, key: 3 })" + } + }, + { + "id": "direct-eval-dst-wf", + "file": "src/spec.rs", + "find": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, callee) && reg_ok(c, this) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "DirectEval accepts a register outside the frame (operand `dst`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEval { dst: 3, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 })" + } + }, + { + "id": "direct-eval-callee-wf", + "file": "src/spec.rs", + "find": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, this) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "DirectEval accepts a register outside the frame (operand `callee`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEval { dst: 0, callee: 3, this: 0, args: 0, argc: 3, env: 0, k: 0 })" + } + }, + { + "id": "direct-eval-this-wf", + "file": "src/spec.rs", + "find": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "DirectEval accepts a register outside the frame (operand `this`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEval { dst: 0, callee: 0, this: 3, args: 0, argc: 3, env: 0, k: 0 })" + } + }, + { + "id": "direct-eval-args-wf", + "file": "src/spec.rs", + "find": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "DirectEval accepts a register range that leaves the frame (operand `args`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 2, argc: 2, env: 0, k: 0 })" + } + }, + { + "id": "direct-eval-env-wf", + "file": "src/spec.rs", + "find": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && regs_ok(c, args, argc) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "DirectEval accepts a register outside the frame (operand `env`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 3, k: 0 })" + } + }, + { + "id": "direct-eval-k-wf", + "file": "src/spec.rs", + "find": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len(),", + "expect": "killed", + "reason": "DirectEval accepts a record constant that is not a value (operand `k`): example_withs_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 2 })" + } + }, + { + "id": "to-object-handler", + "file": "src/ops.rs", + "find": "Op::ToObject { .. } => HandlerId::ToObject,", + "replace": "Op::ToObject { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "ToObject calls the throw handler instead of its own: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_handler(Op::ToObject { dst: 0, src: 0 }) == HandlerId::Throw" + } + }, + { + "id": "to-object-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::ToObject { .. } => OpClass::Core,", + "expect": "killed", + "reason": "ToObject is a core op, which never calls out: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_class(Op::ToObject { dst: 0, src: 0 }) is Core" + } + }, + { + "id": "with-has-handler", + "file": "src/ops.rs", + "find": "Op::WithHas { .. } => HandlerId::WithHas,", + "replace": "Op::WithHas { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "WithHas calls the throw handler instead of its own: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_handler(Op::WithHas { dst: 0, obj: 0, key: 0 }) == HandlerId::Throw" + } + }, + { + "id": "with-has-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::WithHas { .. } => OpClass::Core,", + "expect": "killed", + "reason": "WithHas is a core op, which never calls out: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_class(Op::WithHas { dst: 0, obj: 0, key: 0 }) is Core" + } + }, + { + "id": "with-get-handler", + "file": "src/ops.rs", + "find": "Op::WithGet { .. } => HandlerId::WithGet,", + "replace": "Op::WithGet { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "WithGet calls the throw handler instead of its own: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_handler(Op::WithGet { dst: 0, obj: 0, key: 0, strict: false }) == HandlerId::Throw" + } + }, + { + "id": "with-get-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::WithGet { .. } => OpClass::Core,", + "expect": "killed", + "reason": "WithGet is a core op, which never calls out: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_class(Op::WithGet { dst: 0, obj: 0, key: 0, strict: false }) is Core" + } + }, + { + "id": "with-set-handler", + "file": "src/ops.rs", + "find": "Op::WithSet { .. } => HandlerId::WithSet,", + "replace": "Op::WithSet { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "WithSet calls the throw handler instead of its own: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_handler(Op::WithSet { obj: 0, key: 0, val: 0, strict: false }) == HandlerId::Throw" + } + }, + { + "id": "with-set-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::WithSet { .. } => OpClass::Core,", + "expect": "killed", + "reason": "WithSet is a core op, which never calls out: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_class(Op::WithSet { obj: 0, key: 0, val: 0, strict: false }) is Core" + } + }, + { + "id": "with-delete-handler", + "file": "src/ops.rs", + "find": "Op::WithDelete { .. } => HandlerId::WithDelete,", + "replace": "Op::WithDelete { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "WithDelete calls the throw handler instead of its own: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_handler(Op::WithDelete { dst: 0, obj: 0, key: 0 }) == HandlerId::Throw" + } + }, + { + "id": "with-delete-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::WithDelete { .. } => OpClass::Core,", + "expect": "killed", + "reason": "WithDelete is a core op, which never calls out: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_class(Op::WithDelete { dst: 0, obj: 0, key: 0 }) is Core" + } + }, + { + "id": "delete-global-handler", + "file": "src/ops.rs", + "find": "Op::DeleteGlobal { .. } => HandlerId::DeleteGlobal,", + "replace": "Op::DeleteGlobal { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "DeleteGlobal calls the throw handler instead of its own: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_handler(Op::DeleteGlobal { dst: 0, key: 0 }) == HandlerId::Throw" + } + }, + { + "id": "delete-global-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::DeleteGlobal { .. } => OpClass::Core,", + "expect": "killed", + "reason": "DeleteGlobal is a core op, which never calls out: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_class(Op::DeleteGlobal { dst: 0, key: 0 }) is Core" + } + }, + { + "id": "direct-eval-handler", + "file": "src/ops.rs", + "find": "Op::DirectEval { .. } => HandlerId::DirectEval,", + "replace": "Op::DirectEval { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "DirectEval calls the throw handler instead of its own: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_handler(Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 }) == HandlerId::Throw" + } + }, + { + "id": "direct-eval-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::DirectEval { .. } => OpClass::Core,", + "expect": "killed", + "reason": "DirectEval is a core op, which never calls out: example_withs_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_withs_classes()", + "witness": "op_class(Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 }) is Core" + } + }, + { + "id": "get-import-binding-dst-wf", + "file": "src/spec.rs", + "find": "Op::GetImportBinding { dst, env, .. } => reg_ok(c, dst) && reg_ok(c, env),", + "replace": "Op::GetImportBinding { dst, env, .. } => reg_ok(c, env),", + "expect": "killed", + "reason": "GetImportBinding accepts a register outside the frame (operand `dst`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::GetImportBinding { dst: 3, env: 0, idx: 0 })" + } + }, + { + "id": "get-import-binding-env-wf", + "file": "src/spec.rs", + "find": "Op::GetImportBinding { dst, env, .. } => reg_ok(c, dst) && reg_ok(c, env),", + "replace": "Op::GetImportBinding { dst, env, .. } => reg_ok(c, dst),", + "expect": "killed", + "reason": "GetImportBinding accepts a register outside the frame (operand `env`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::GetImportBinding { dst: 0, env: 3, idx: 0 })" + } + }, + { + "id": "import-call-dst-wf", + "file": "src/spec.rs", + "find": "Op::ImportCall { dst, specifier, options, .. } => reg_ok(c, dst) && reg_ok(c, specifier) && reg_ok(c, options),", + "replace": "Op::ImportCall { dst, specifier, options, .. } => reg_ok(c, specifier) && reg_ok(c, options),", + "expect": "killed", + "reason": "ImportCall accepts a register outside the frame (operand `dst`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::ImportCall { dst: 3, specifier: 0, options: 0 })" + } + }, + { + "id": "import-call-specifier-wf", + "file": "src/spec.rs", + "find": "Op::ImportCall { dst, specifier, options, .. } => reg_ok(c, dst) && reg_ok(c, specifier) && reg_ok(c, options),", + "replace": "Op::ImportCall { dst, specifier, options, .. } => reg_ok(c, dst) && reg_ok(c, options),", + "expect": "killed", + "reason": "ImportCall accepts a register outside the frame (operand `specifier`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::ImportCall { dst: 0, specifier: 3, options: 0 })" + } + }, + { + "id": "import-call-options-wf", + "file": "src/spec.rs", + "find": "Op::ImportCall { dst, specifier, options, .. } => reg_ok(c, dst) && reg_ok(c, specifier) && reg_ok(c, options),", + "replace": "Op::ImportCall { dst, specifier, options, .. } => reg_ok(c, dst) && reg_ok(c, specifier),", + "expect": "killed", + "reason": "ImportCall accepts a register outside the frame (operand `options`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::ImportCall { dst: 0, specifier: 0, options: 3 })" + } + }, + { + "id": "import-meta-dst-wf", + "file": "src/spec.rs", + "find": "Op::ImportMeta { dst, .. } => reg_ok(c, dst),", + "replace": "Op::ImportMeta { dst, .. } => true,", + "expect": "killed", + "reason": "ImportMeta accepts a register outside the frame (operand `dst`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::ImportMeta { dst: 3 })" + } + }, + { + "id": "direct-eval-spread-dst-wf", + "file": "src/spec.rs", + "find": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "DirectEvalSpread accepts a register outside the frame (operand `dst`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEvalSpread { dst: 3, callee: 0, this: 0, args: 0, env: 0, k: 0 })" + } + }, + { + "id": "direct-eval-spread-callee-wf", + "file": "src/spec.rs", + "find": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, this) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "DirectEvalSpread accepts a register outside the frame (operand `callee`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEvalSpread { dst: 0, callee: 3, this: 0, args: 0, env: 0, k: 0 })" + } + }, + { + "id": "direct-eval-spread-this-wf", + "file": "src/spec.rs", + "find": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "DirectEvalSpread accepts a register outside the frame (operand `this`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 3, args: 0, env: 0, k: 0 })" + } + }, + { + "id": "direct-eval-spread-args-wf", + "file": "src/spec.rs", + "find": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "DirectEvalSpread accepts a register outside the frame (operand `args`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 3, env: 0, k: 0 })" + } + }, + { + "id": "direct-eval-spread-env-wf", + "file": "src/spec.rs", + "find": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, args) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "expect": "killed", + "reason": "DirectEvalSpread accepts a register outside the frame (operand `env`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 3, k: 0 })" + } + }, + { + "id": "direct-eval-spread-k-wf", + "file": "src/spec.rs", + "find": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]),", + "replace": "Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len(),", + "expect": "killed", + "reason": "DirectEvalSpread accepts a record constant that is not a value (operand `k`): example_imports_wf is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_wf()", + "witness": "op_wf(crate::examples::example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 2 })" + } + }, + { + "id": "get-import-binding-handler", + "file": "src/ops.rs", + "find": "Op::GetImportBinding { .. } => HandlerId::GetImportBinding,", + "replace": "Op::GetImportBinding { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "GetImportBinding calls the throw handler instead of its own: example_imports_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_classes()", + "witness": "op_handler(Op::GetImportBinding { dst: 0, env: 0, idx: 0 }) == HandlerId::Throw" + } + }, + { + "id": "get-import-binding-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::GetImportBinding { .. } => OpClass::Core,", + "expect": "killed", + "reason": "GetImportBinding is a core op, which never calls out: example_imports_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_classes()", + "witness": "op_class(Op::GetImportBinding { dst: 0, env: 0, idx: 0 }) is Core" + } + }, + { + "id": "import-call-handler", + "file": "src/ops.rs", + "find": "Op::ImportCall { .. } => HandlerId::ImportCall,", + "replace": "Op::ImportCall { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "ImportCall calls the throw handler instead of its own: example_imports_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_classes()", + "witness": "op_handler(Op::ImportCall { dst: 0, specifier: 0, options: 0 }) == HandlerId::Throw" + } + }, + { + "id": "import-call-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::ImportCall { .. } => OpClass::Core,", + "expect": "killed", + "reason": "ImportCall is a core op, which never calls out: example_imports_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_classes()", + "witness": "op_class(Op::ImportCall { dst: 0, specifier: 0, options: 0 }) is Core" + } + }, + { + "id": "import-meta-handler", + "file": "src/ops.rs", + "find": "Op::ImportMeta { .. } => HandlerId::ImportMeta,", + "replace": "Op::ImportMeta { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "ImportMeta calls the throw handler instead of its own: example_imports_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_classes()", + "witness": "op_handler(Op::ImportMeta { dst: 0 }) == HandlerId::Throw" + } + }, + { + "id": "import-meta-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::ImportMeta { .. } => OpClass::Core,", + "expect": "killed", + "reason": "ImportMeta is a core op, which never calls out: example_imports_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_classes()", + "witness": "op_class(Op::ImportMeta { dst: 0 }) is Core" + } + }, + { + "id": "direct-eval-spread-handler", + "file": "src/ops.rs", + "find": "Op::DirectEvalSpread { .. } => HandlerId::DirectEvalSpread,", + "replace": "Op::DirectEvalSpread { .. } => HandlerId::Throw,", + "expect": "killed", + "reason": "DirectEvalSpread calls the throw handler instead of its own: example_imports_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_classes()", + "witness": "op_handler(Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 0 }) == HandlerId::Throw" + } + }, + { + "id": "direct-eval-spread-class-core", + "file": "src/ops.rs", + "find": "| Op::JumpIfNotNullish { .. } => OpClass::Core,", + "replace": "| Op::JumpIfNotNullish { .. } | Op::DirectEvalSpread { .. } => OpClass::Core,", + "expect": "killed", + "reason": "DirectEvalSpread is a core op, which never calls out: example_imports_classes is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "crate::examples::example_imports_classes()", + "witness": "op_class(Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 0 }) is Core" + } } ] } diff --git a/crates/clazel-js-vm/src/bytecode.rs b/crates/clazel-js-vm/src/bytecode.rs index 76d5e4785..d7965f449 100644 --- a/crates/clazel-js-vm/src/bytecode.rs +++ b/crates/clazel-js-vm/src/bytecode.rs @@ -193,6 +193,25 @@ instrs! { CreateRestArray { dst: Reg, from: u32 }, ForInIterator { dst: Reg, src: Reg }, InitGlobalLexical { key: u32, src: Reg }, + NewRegExp { dst: Reg, k: u32 }, + GeneratorStart, + YieldDelegate { src: Reg, value: Reg, mode: Reg }, + GetAsyncIterator { dst: Reg, src: Reg }, + IterCallMethod { dst: Reg, found: Reg, iter: Reg, arg: Reg, which: u32 }, + IterComplete { done: Reg, result: Reg }, + IterValue { dst: Reg, result: Reg }, + ThrowIfNotObject { src: Reg }, + ToObject { dst: Reg, src: Reg }, + WithHas { dst: Reg, obj: Reg, key: u32 }, + WithGet { dst: Reg, obj: Reg, key: u32, strict: bool }, + WithSet { obj: Reg, key: u32, val: Reg, strict: bool }, + WithDelete { dst: Reg, obj: Reg, key: u32 }, + DeleteGlobal { dst: Reg, key: u32 }, + DirectEval { dst: Reg, callee: Reg, this: Reg, args: Reg, argc: u32, env: Reg, k: u32 }, + GetImportBinding { dst: Reg, env: Reg, idx: u32 }, + ImportCall { dst: Reg, specifier: Reg, options: Reg }, + ImportMeta { dst: Reg }, + DirectEvalSpread { dst: Reg, callee: Reg, this: Reg, args: Reg, env: Reg, k: u32 }, } impl Instr { @@ -265,6 +284,119 @@ pub enum Const { /// parameter, the slot of the environment object it is bound to, or none /// when a later parameter of the same name replaces it. ArgumentsMap(Vec>), + /// A regular expression literal's record, which [`Instr::NewRegExp`] reads + /// (see [`crate::ops::Op::NewRegExp`]). + RegExp(RegExpSite), + /// The scopes of a direct eval's call, which [`Instr::DirectEval`] reads + /// (see [`crate::ops::Op::DirectEval`]). + EvalScope(EvalScopeInfo), +} + +/// What a direct eval's code is compiled against (the host compile path of +/// [`crate::ops::Op::DirectEval`]): the caller's strictness, what eval code +/// may use of the caller (§19.2.1.1† PerformEval steps 5 to 9) and the scopes +/// the caller is in. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct EvalScopeInfo { + /// The caller's code is strict mode code (strictCaller). + pub strict: bool, + /// The caller is in a function, so `new.target` is allowed (inFunction). + pub in_function: bool, + /// The caller is in a method, so `super.x` is allowed (inMethod). + pub in_method: bool, + /// The caller is in a derived constructor, so `super()` is allowed. + pub in_derived_constructor: bool, + /// The caller is in a class field initializer, so `arguments` is not + /// allowed (inClassFieldInitializer). + pub in_class_field_initializer: bool, + /// The scopes that have an environment object, innermost first: each is + /// one `GetEnvParent` hop from the one before, the first being the + /// call's `env` operand. + pub scopes: Vec, +} + +/// One scope of [`EvalScopeInfo`]. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct EvalScope { + /// What kind of scope it is. + pub kind: EvalScopeKind, + /// For a function scope of sloppy code that calls direct eval: the slot + /// of its environment object that holds the variable object, which the + /// declarations of sloppy eval code are properties of. + pub var_object: Option, + /// The names the scope binds, each with its slot. + pub names: Vec, +} + +/// What kind of scope an [`EvalScope`] is. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum EvalScopeKind { + /// The script's. + Script, + /// A module's: every binding of the module scope. + Module, + /// A function's: its parameters, `var`s and top-level declarations. + Function, + /// A block, `for`, `switch` or `catch` scope. + Block, + /// A class's scope: its name and private names. + Class, + /// A `with` statement's: slot 0 holds the object. + With, +} + +/// A name bound in an [`EvalScope`], in a slot of its environment object. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct EvalName { + /// The name, as UTF-16 code units (a private name keeps its `#`; the + /// compiler's own bindings have an empty name and a kind that says what + /// they are). + pub name: Vec, + /// The slot in the scope's environment object. + pub slot: u32, + /// What declared it. + pub kind: EvalBinding, +} + +/// What declared an [`EvalName`] (`scope::BKind`). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum EvalBinding { + /// A parameter, a `var` or a function declaration of a function. + Var, + /// A `let` or a function declared in a block (a temporal dead zone). + Let, + /// A `const`. + Const, + /// A `catch` parameter. + Catch, + /// A function expression's own name (immutable). + FuncName, + /// The `arguments` object. + Arguments, + /// The caller's `this`. + This, + /// The caller's `this` in a derived constructor (the hole before `super()`). + DerivedThis, + /// The caller's `new.target`. + NewTarget, + /// The caller's function, for `super`. + Callee, + /// A private name of a class. + PrivateName, + /// A named or default import: an indirect binding, read with + /// [`Instr::GetImportBinding`]. + Import, +} + +/// The source text and the flags of one regular expression literal +/// (§13.2.7.3†): what `RegExpCreate` takes. The crate does not compile the +/// pattern, which the parser has checked (§13.2.7.2†). +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct RegExpSite { + /// The text between the slashes, as UTF-16 code units (BodyText). + pub pattern: Vec, + /// The flags after the closing slash (FlagText). + pub flags: Vec, } /// The strings of one template literal (§13.2.8.3† TemplateStrings). @@ -321,6 +453,9 @@ pub struct Handler { pub enum FunctionKind { /// The script's own code. Script, + /// A module's body (see [`crate::module`]): run with the module + /// environment as its callee environment and `this` undefined. + Module, /// A function declaration or expression. Normal, /// An arrow function. @@ -334,6 +469,23 @@ pub enum FunctionKind { Derived, } +/// Whether a function is a generator, an async function or both: the fact +/// about a template that decides what a call does with the frame (a generator's +/// call returns at [`Instr::GeneratorStart`] with its generator object, an async +/// function's call returns a promise that the body settles), and what `yield` +/// and `await` are. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Flavor { + /// An ordinary function. + Plain, + /// A generator function (§15.5). + Generator, + /// An async function or async arrow function (§15.8, §15.9). + Async, + /// An async generator function (§15.6). + AsyncGenerator, +} + /// One function's code (`spec::Code` and the facts about the template the /// module header lists). #[derive(Clone, Debug, PartialEq)] @@ -343,6 +495,8 @@ pub struct FunctionCode { pub name: Vec, /// What kind of function it is. pub kind: FunctionKind, + /// Whether it is a generator, an async function or both. + pub flavor: Flavor, /// Whether the code is strict mode code. pub strict: bool, /// The span of the whole function in the source text. @@ -623,6 +777,71 @@ pub fn check_function(f: &FunctionCode, nfuncs: usize) -> Result<(), &'static st Instr::ConstructResult { dst, val, this } => { reg_ok(f, dst) && reg_ok(f, val) && reg_ok(f, this) } + Instr::GeneratorStart => true, + Instr::YieldDelegate { src, value, mode } => { + reg_ok(f, src) && reg_ok(f, value) && reg_ok(f, mode) + } + Instr::GetAsyncIterator { dst, src } + | Instr::IterValue { dst, result: src } + | Instr::ToObject { dst, src } => reg_ok(f, dst) && reg_ok(f, src), + Instr::IterCallMethod { + dst, + found, + iter, + arg, + which, + } => { + reg_ok(f, dst) && reg_ok(f, found) && reg_ok(f, iter) && reg_ok(f, arg) && which < 5 + } + Instr::IterComplete { done, result } => reg_ok(f, done) && reg_ok(f, result), + Instr::ThrowIfNotObject { src } => reg_ok(f, src), + Instr::WithHas { dst, obj, key } | Instr::WithDelete { dst, obj, key } => { + reg_ok(f, dst) && reg_ok(f, obj) && konst(key) + } + Instr::WithGet { dst, obj, key, .. } => reg_ok(f, dst) && reg_ok(f, obj) && konst(key), + Instr::WithSet { obj, key, val, .. } => reg_ok(f, obj) && konst(key) && reg_ok(f, val), + Instr::DeleteGlobal { dst, key } => reg_ok(f, dst) && konst(key), + Instr::DirectEval { + dst, + callee, + this, + args, + argc, + env, + k, + } => { + reg_ok(f, dst) + && reg_ok(f, callee) + && reg_ok(f, this) + && regs_ok(f, args, argc) + && reg_ok(f, env) + && matches!(f.consts.get(k as usize), Some(Const::EvalScope(_))) + } + Instr::GetImportBinding { dst, env, .. } => reg_ok(f, dst) && reg_ok(f, env), + Instr::ImportCall { + dst, + specifier, + options, + } => reg_ok(f, dst) && reg_ok(f, specifier) && reg_ok(f, options), + Instr::ImportMeta { dst } => reg_ok(f, dst), + Instr::DirectEvalSpread { + dst, + callee, + this, + args, + env, + k, + } => { + reg_ok(f, dst) + && reg_ok(f, callee) + && reg_ok(f, this) + && reg_ok(f, args) + && reg_ok(f, env) + && matches!(f.consts.get(k as usize), Some(Const::EvalScope(_))) + } + Instr::NewRegExp { dst, k } => { + reg_ok(f, dst) && matches!(f.consts.get(k as usize), Some(Const::RegExp(_))) + } Instr::CreateMappedArguments { dst, env, k } => { reg_ok(f, dst) && reg_ok(f, env) diff --git a/crates/clazel-js-vm/src/compile.rs b/crates/clazel-js-vm/src/compile.rs index 747c821f4..4233da264 100644 --- a/crates/clazel-js-vm/src/compile.rs +++ b/crates/clazel-js-vm/src/compile.rs @@ -1,60 +1,88 @@ -//! The compiler from the AST to bytecode (phase-7.md item 7.7, first and second -//! slice): [`compile_script`]. +//! The compiler from the AST to bytecode (phase-7.md item 7.7, first to fourth +//! slice): [`compile_script`] and [`compile_module`]. //! //! # What it compiles //! //! - **Expressions:** literals (numbers, BigInts, strings, `true`, `false`, -//! `null`), identifiers, `this`, the unary, binary, logical, conditional and -//! comma operators, assignment (simple, compound, logical and destructuring), -//! `++` and `--`, calls and `new` (with spread arguments), member access, -//! optional chaining, object literals (data properties, methods, accessors, -//! computed names, spread and `__proto__:`) and array literals (with spread), -//! template literals and tagged templates, function and arrow function -//! expressions, class expressions, `super` properties and `super()`, -//! `new.target`, private names (`o.#x`, `#x in o`); `typeof` of a global, -//! unary `+` as `ToNumber`. +//! `null`, regular expressions), identifiers, `this`, the unary, binary, +//! logical, conditional and comma operators, assignment (simple, compound, +//! logical and destructuring), `++` and `--`, calls and `new` (with spread +//! arguments), direct `eval`, member access, optional chaining, object +//! literals (data properties, methods, accessors, computed names, spread and +//! `__proto__:`) and array literals (with spread), template literals and +//! tagged templates, function, arrow function, generator and async function +//! expressions, class expressions, `yield`, `yield*` and `await`, `super` +//! properties and `super()`, `new.target`, private names (`o.#x`, `#x in o`); +//! `typeof` and `delete` of an identifier, unary `+` as `ToNumber`. //! - **Statements:** `var`, `let` and `const` with temporal-dead-zone checks and -//! binding patterns, blocks, `if`, `while`, `do`-`while` and `for`, `for`-`in` -//! and `for`-`of` (with iterator closing), `switch`, labels, `break`, -//! `continue`, `return`, `throw`, `try`/`catch`/`finally` (with a pattern as the -//! `catch` parameter), function and class declarations, with Annex B.3.3's -//! block-level hoisting. -//! - **Functions:** default and rest parameters and patterns as parameters, the -//! mapped and unmapped `arguments` object; classes with fields, methods, -//! accessors, static members and blocks, private names, `extends` and the -//! default constructors. +//! binding patterns, blocks, `if`, `while`, `do`-`while` and `for`, `for`-`in`, +//! `for`-`of` and `for await` (with iterator closing), `switch`, `with`, labels, +//! `break`, `continue`, `return`, `throw`, `try`/`catch`/`finally` (with a +//! pattern as the `catch` parameter), function, generator, async and class +//! declarations, with Annex B.3.3's block-level hoisting. +//! - **Functions:** default and rest parameters and patterns as parameters (with +//! a separate environment for the body when the parameters have expressions), +//! the mapped and unmapped `arguments` object (and a `var arguments` that +//! shares it); generators, async functions, async arrow functions and async +//! generators, and their methods; classes with fields, methods, accessors, +//! static members and blocks, private names, `extends` and the default +//! constructors; NamedEvaluation of anonymous functions and classes with a +//! name known at compile time. //! - **Closures:** scope analysis ([`crate::scope`]) puts a binding that no //! inner function refers to in a register and a captured one in a slot of an //! environment object that its scope creates on entry. +//! - **Modules** ([`compile_module`], §16.2): the body of a module and the static +//! record the runtime links ([`crate::module`]): requested modules, import, +//! local, indirect and star export entries, the module environment's bindings +//! (every binding of the module scope has a slot, so other modules can reach +//! an export), `import` bindings read by [`Instr::GetImportBinding`] and +//! immutable, namespace imports, `export default` (an expression, an +//! anonymous function or class, with NamedEvaluation to `"default"`), and +//! top-level `await` (the body is an async function body). `import(...)` and +//! `import.meta` are [`Instr::ImportCall`] and [`Instr::ImportMeta`], in scripts +//! and modules alike. +//! - **The rest of the fourth slice:** `delete` of an optional chain, direct +//! `eval` with a spread argument ([`Instr::DirectEvalSpread`]), and a function +//! declaration as the body of an `if` (Annex B.3.4†, treated as in a block of +//! its own) or of a label. //! //! # What it does not compile yet //! //! It returns [`CompileError::NotYetCompiled`] with a stated feature, never a -//! panic: generators and async functions (and generator and async methods, -//! `for await`), `with` and direct `eval`, modules, regular expression -//! literals, dynamic `import()`, and the few forms listed on -//! [`CompileError::NotYetCompiled`]'s users in this file (`delete` of a variable -//! or of an optional chain, a function declaration as the body of an `if` or a -//! label). +//! panic, for a construct it does not compile. After this slice there is none: +//! every source of the vendored Test262 subset compiles. Linking and evaluating +//! a module (ResolveExport, GetExportedNames, InitializeEnvironment, the +//! cyclic and asynchronous evaluation algorithms of §16.2.1.5†) are the +//! runtime's (7.8), and `compile_eval` is not written (see below). //! //! # Known gaps in what it compiles //! -//! - A function with a non-simple parameter list has one scope for its -//! parameters and its body, where §10.2.11† step 28 has a separate -//! environment for the body's `var`s: a closure in a default value sees -//! the body's declarations. -//! - NamedEvaluation (§8.4.5†) of an anonymous function or class assigned to a -//! name is not done (only a function's own name, and a class constructor's -//! name, are in its template). //! - `super()` reads the active function's prototype when the op runs, after //! the arguments are evaluated, where §13.3.7.1† step 5 reads it before; the //! difference shows only if an argument expression changes that prototype. -//! - `var arguments` in a function body does not share the `arguments` -//! object's binding (§10.2.11† step 27.b). +//! - NamedEvaluation (§8.4.5†) covers a name known when the code is compiled: +//! an identifier, an identifier or string key, a private name. A computed +//! key, and a numeric key, name a function when `DefineMethod` and its +//! siblings run or not at all. //! - A member expression's computed key is converted when the access runs, as //! §13.3.3† (ES2024) has it; a computed property name of a literal, a class or //! a pattern is converted when it is evaluated, by //! [`crate::bytecode::Instr::ToPropertyKey`]. +//! - A name that a `with` object or a direct eval's variable object may supply +//! is resolved before the right side of an assignment to it, an update or a +//! call (§13.15.2 step 1.a), but not for the target of a destructuring +//! assignment or a `for` head, which look it up when they store. +//! - Sloppy direct eval in a parameter expression declares its `var`s in the +//! function's variable object, where §19.2.1.3† would use the parameters' +//! own environment (§10.2.11† step 20). +//! - Direct eval's host compile path (`compile_eval`, see +//! [`crate::ops::Op::DirectEval`]) is specified, not written: eval code is not +//! compiled yet, only the call and the record of the scopes it needs. The same +//! goes for `import()` in eval code. +//! - A labelled function declaration is compiled as a function declaration at +//! its position, which is Annex B.3.2†'s reading; `if (x) l: function f() {}` +//! does not parse. +//! - No differential against Boa yet. //! //! # Conventions of the output //! @@ -81,35 +109,49 @@ //! //! # Where the standard is followed //! -//! Comments cite ECMA-262 (see `spec.rs` for the pinned edition). The steps -//! this slice implements: §14.15.3 (try-finally completion), §14.7.4.4 +//! Comments cite ECMA-262 (see `spec.rs` for the pinned edition). The third +//! slice adds §13.2.7.3 (regular expression literals), §15.5.2, §15.5.5 and +//! §27.5.3 (generators and `yield*`), §15.6, §15.8 and §27.6 to §27.7 (async +//! functions and async generators), §14.7.5.7 (`for await`), §14.11.2 (`with`), +//! §13.3.6.1, §19.2.1.1 and §19.2.1.3 (direct eval), §13.5.1.2 (`delete`), +//! §8.4.5 (NamedEvaluation) and §10.2.11 steps 27 and 28. The steps the second +//! slice implements: §14.15.3 (try-finally completion), §14.7.4.4 //! CreatePerIterationEnvironment, §14.12.4 (switch evaluation), §9.1.1.1.5 //! SetMutableBinding steps 3 to 6, §9.1.1.1.6 GetBindingValue step 2 (TDZ), //! §15.2.5 InstantiateOrdinaryFunctionExpression, §10.2.11 //! FunctionDeclarationInstantiation, §13.15.5 and §14.3.3 (destructuring), //! §14.7.5 (`for`-`in` and `for`-`of`), §15.7.14 ClassDefinitionEvaluation, //! §13.2.8 (templates), §13.3.9 (optional chains), and Annex B.3.3.1. Section -//! numbers marked † are from memory (phase-7.md's convention). +//! numbers marked † are from memory (phase-7.md's convention). The fourth slice +//! adds §16.2 (modules: ParseModule §16.2.1.6.1†, InitializeEnvironment +//! §16.2.1.6.4†, §16.2.2 and §16.2.3 for import and export declarations), +//! §9.1.1.5† (module Environment Records), §13.3.10.1† (`import()`), +//! §13.3.12.1† (`import.meta`), §13.5.1.2† (`delete` of an optional chain) and +//! Annex B.3.4† (a function declaration as the body of an `if`). use crate::bytecode::{ - Const, ErrorKind, FunctionCode, FunctionKind, GlobalDecls, Handler, Instr, LIMIT, Program, Reg, - Span, TemplateSite, VarScoped, + Const, ErrorKind, EvalBinding, EvalName, EvalScope, EvalScopeInfo, EvalScopeKind, Flavor, + FunctionCode, FunctionKind, GlobalDecls, Handler, Instr, LIMIT, Program, Reg, RegExpSite, Span, + TemplateSite, VarScoped, }; use crate::literal::{self, Number}; +use crate::module::{self, ImportName, ModuleBinding, ModuleBindingInit, ModuleProgram}; use crate::scope::{self, Analysis, BKind, NONE, SKind, Target, span_of}; use clazel_js_syntax::ast::{Ast, Node, NodeKind, Op as AOp}; use std::collections::HashMap; mod classes; +mod exports; mod loops; mod misc; mod patterns; +mod suspend; /// Why a script was not compiled. #[derive(Clone, Debug, PartialEq, Eq)] pub enum CompileError { - /// The script uses a construct this slice does not compile (see the module - /// header). Not a failure of the compiler: the construct is `feature`, at + /// The code uses a construct the compiler does not compile (see the module + /// header; none is left in the vendored Test262 subset). Not a failure of the compiler: the construct is `feature`, at /// `span`. NotYetCompiled { /// The construct. @@ -149,31 +191,58 @@ type R = Result; /// [`Program`]. /// /// Total: every AST has a result, and the compiler never panics. A script -/// with a construct of the module header's second list is a -/// [`CompileError::NotYetCompiled`]. +/// with a construct that the module header lists as not compiled is a +/// [`CompileError::NotYetCompiled`]. An AST that is a module is +/// [`CompileError::Internal`]: [`compile_module`] compiles it. pub fn compile_script(ast: &Ast, src: &[u32]) -> Result { + with_stack(ast, src, compile_inner) +} + +/// Compiles a parsed module (`ast` was parsed from `src` with the module goal) +/// to a [`ModuleProgram`]: the code of its body and the static record that the +/// runtime links and evaluates (see [`crate::module`] for its shape). +/// +/// Total, like [`compile_script`]. An AST that is a script is +/// [`CompileError::Internal`]. +pub fn compile_module(ast: &Ast, src: &[u32]) -> Result { + with_stack(ast, src, module_inner) +} + +/// Runs `f` on a thread with a big stack when the AST is large. +fn with_stack( + ast: &Ast, + src: &[u32], + f: fn(&Ast, &[u32]) -> Result, +) -> Result { // Compilation recurses once per level of the AST (at most MAX_DEPTH of // them, each a few frames): a program that deep needs a bigger stack than // a test thread has, as the parser does. const SMALL: usize = 256; if ast.nodes.len() <= SMALL { - return compile_inner(ast, src); + return f(ast, src); } std::thread::scope(|s| { match std::thread::Builder::new() .stack_size(256 * 1024 * 1024) - .spawn_scoped(s, || compile_inner(ast, src)) + .spawn_scoped(s, || f(ast, src)) { Ok(h) => h .join() .unwrap_or(Err(CompileError::Internal("the compiler thread panicked"))), - Err(_) => compile_inner(ast, src), + Err(_) => f(ast, src), } }) } +fn eval_name_of(an: &Analysis) -> u32 { + an.names + .iter() + .position(|n| n.iter().copied().eq("eval".chars().map(u32::from))) + .and_then(|p| u32::try_from(p).ok()) + .unwrap_or(NONE) +} + fn compile_inner(ast: &Ast, src: &[u32]) -> Result { - scope::check_supported(ast)?; let an = scope::analyze(ast, src)?; let root = ast.root; let rn = ast @@ -181,17 +250,11 @@ fn compile_inner(ast: &Ast, src: &[u32]) -> Result { .get(root as usize) .ok_or(CompileError::Internal("no root"))?; if rn.kind != NodeKind::Script { - return Err(CompileError::NotYetCompiled { - feature: "modules", - span: span_of(ast, root as usize), - }); + return Err(CompileError::Internal( + "compile_script was given a module: use compile_module", + )); } - let eval_name = an - .names - .iter() - .position(|n| n.iter().copied().eq("eval".chars().map(u32::from))) - .and_then(|p| u32::try_from(p).ok()) - .unwrap_or(NONE); + let eval_name = eval_name_of(&an); let mut cg = Cg { ast, src, @@ -200,6 +263,8 @@ fn compile_inner(ast: &Ast, src: &[u32]) -> Result { loc: vec![None; an.bindings.len()], eval_name, name_override: None, + module_fns: Vec::new(), + module_tla: false, f: FnState::new(FunctionKind::Script, false, Span { start: 0, end: 0 }, 0), }; let main = cg.compile_function(root, FunctionKind::Script)?; @@ -212,6 +277,112 @@ fn compile_inner(ast: &Ast, src: &[u32]) -> Result { Ok(Program { functions, main }) } +/// Whether the body of the module contains `await` or `for await` outside any +/// function (§16.2.1.6.1† step 7: HasTLA is ContainsAwait of the body, which +/// does not look inside functions). +fn module_has_tla(ast: &Ast, an: &Analysis) -> bool { + ast.nodes.iter().enumerate().any(|(i, n)| { + let awaits = + n.kind == NodeKind::AwaitExpression || (n.kind == NodeKind::ForOfStatement && n.flag); + awaits + && an + .owner_scope + .get(i) + .and_then(|&s| an.scopes.get(s as usize)) + .is_some_and(|sc| sc.func == 0) + }) +} + +fn module_inner(ast: &Ast, src: &[u32]) -> Result { + let an = scope::analyze(ast, src)?; + let root = ast.root; + let rn = ast + .nodes + .get(root as usize) + .ok_or(CompileError::Internal("no root"))?; + if rn.kind != NodeKind::Module { + return Err(CompileError::Internal( + "compile_module was given a script: use compile_script", + )); + } + let ent = module::entries(ast, src)?; + let has_tla = module_has_tla(ast, &an); + let eval_name = eval_name_of(&an); + let mut cg = Cg { + ast, + src, + an: &an, + out: Vec::new(), + loc: vec![None; an.bindings.len()], + eval_name, + name_override: None, + module_fns: Vec::new(), + module_tla: has_tla, + f: FnState::new(FunctionKind::Module, true, Span { start: 0, end: 0 }, 0), + }; + let main = cg.compile_function(root, FunctionKind::Module)?; + let module_fns = std::mem::take(&mut cg.module_fns); + // The module environment's bindings (§16.2.1.6.4† InitializeEnvironment). + let sc_id = an.own_scope.get(root as usize).copied().unwrap_or(NONE); + let sc = cg.scope(sc_id)?; + let mut bindings = Vec::new(); + for &b in &sc.bindings { + let bd = cg.binding(b)?; + // The compiler's own bindings (the module's `this` for its arrow + // functions) have no name and are written by the body's first ops. + if !matches!( + bd.kind, + BKind::Var | BKind::Let | BKind::Const | BKind::Import + ) { + continue; + } + let name = cg.name_units(bd.name)?; + let entry = ent.imports.iter().position(|e| e.local_name == name); + let init = match (entry, bd.kind) { + (Some(e), _) => { + let e32 = u32::try_from(e).map_err(|_| CompileError::TooLarge("imports"))?; + match ent.imports.get(e).map(|x| &x.import_name) { + Some(ImportName::Namespace) => ModuleBindingInit::Namespace(e32), + _ => ModuleBindingInit::Import(e32), + } + } + (None, BKind::Var) if bd.decl_fn != NONE => { + let idx = module_fns + .iter() + .find(|(f, _)| *f == bd.decl_fn) + .map(|&(_, i)| i) + .ok_or(CompileError::Internal("a module function has no template"))?; + ModuleBindingInit::Function(idx) + } + (None, BKind::Var) => ModuleBindingInit::Undefined, + (None, _) => ModuleBindingInit::Uninitialized, + }; + bindings.push(ModuleBinding { + name, + slot: bd.slot, + init, + }); + } + let env_size = sc.env_size; + let mut functions = Vec::with_capacity(cg.out.len()); + for f in cg.out { + functions.push(f.ok_or(CompileError::Internal( + "a function was reserved but not compiled", + ))?); + } + Ok(ModuleProgram { + program: Program { functions, main }, + requested_modules: ent.requested, + import_entries: ent.imports, + local_export_entries: ent.locals, + indirect_export_entries: ent.indirects, + star_export_entries: ent.stars, + has_tla, + env_size, + bindings, + }) +} + /// How a `break` or `continue` reaches its target, and what a `return` does, /// when it passes a `finally`. #[derive(Clone, Copy, Debug)] @@ -231,9 +402,10 @@ enum Ctl { labels: Vec, brk: usize, cont: usize, - /// For a `for`-`of` loop: the iterator register, and the label of the - /// stub that rethrows (see [`Cg::emit_iter_close`]). - close: Option<(Reg, usize)>, + /// For a `for`-`of` loop: the iterator register, the label of the + /// stub that rethrows (see [`Cg::emit_iter_close`]), and whether the + /// loop is a `for await`, whose close awaits. + close: Option<(Reg, usize, bool)>, }, Switch { labels: Vec, @@ -254,6 +426,8 @@ enum Ctl { /// The state of the function being compiled. struct FnState { kind: FunctionKind, + /// Whether the function is a generator, an async function or both. + flavor: Flavor, strict: bool, span: Span, instrs: Vec, @@ -287,6 +461,7 @@ impl FnState { fn new(kind: FunctionKind, strict: bool, span: Span, nparams: u32) -> Self { FnState { kind, + flavor: Flavor::Plain, strict, span, instrs: Vec::new(), @@ -314,6 +489,7 @@ impl FnState { struct FnMeta { name: Vec, kind: FunctionKind, + flavor: Flavor, nparams: u32, length: u32, span: Span, @@ -332,6 +508,11 @@ struct Cg<'a> { /// The name the next compiled function gets instead of its own (a class /// constructor is named for its class). name_override: Option>, + /// For a module: each hoisted function declaration (node) with the index of + /// its template, which the module record lists (see [`crate::module`]). + module_fns: Vec<(u32, u32)>, + /// The module body has top-level `await`: it is an async function body. + module_tla: bool, f: FnState, } @@ -345,12 +526,17 @@ fn final_op_kind(k: NodeKind) -> bool { NodeKind::NumericLiteral | NodeKind::StringLiteral | NodeKind::NullLiteral + | NodeKind::RegularExpressionLiteral | NodeKind::BooleanLiteral | NodeKind::ThisExpression | NodeKind::IdentifierReference | NodeKind::NewTarget | NodeKind::FunctionExpression + | NodeKind::GeneratorExpression + | NodeKind::AsyncFunctionExpression + | NodeKind::AsyncGeneratorExpression | NodeKind::ArrowFunction + | NodeKind::AsyncArrowFunction | NodeKind::MemberExpression | NodeKind::CallExpression | NodeKind::NewExpression @@ -634,6 +820,11 @@ impl<'a> Cg<'a> { /// releases the temporary with `mark`/`release`. fn env_for(&mut self, b: u32, sp: Span) -> R { let target = self.binding(b)?.scope; + self.env_for_scope(target, sp) + } + + /// As [`Self::env_for`], for the environment object of scope `target`. + fn env_for_scope(&mut self, target: u32, sp: Span) -> R { let func = self.cur_func_index()?; let mut s = self.f.cur_scope; let mut hops: u32 = 0; @@ -708,6 +899,7 @@ impl<'a> Cg<'a> { } let bd = self.binding(b)?; if bd.captured + || !self.dyn_scopes(bd.scope)?.is_empty() || matches!( bd.kind, BKind::Const @@ -732,6 +924,21 @@ impl<'a> Cg<'a> { /// `dst := value of binding b`, referenced at `refpos`. fn read_binding(&mut self, b: u32, refpos: usize, dst: Reg, sp: Span) -> R<()> { let bd = self.binding(b)?; + if bd.kind == BKind::Import { + // §9.1.1.5.1† GetBindingValue of an indirect binding. + let m = self.mark(); + let env = self.env_for(b, sp)?; + self.emit( + Instr::GetImportBinding { + dst, + env, + idx: bd.slot, + }, + sp, + )?; + self.release(m); + return Ok(()); + } let tdz = self.needs_tdz(b, refpos)?; if bd.captured { let m = self.mark(); @@ -798,7 +1005,8 @@ impl<'a> Cg<'a> { let bd = self.binding(b)?; let tdz = self.needs_tdz(b, refpos)?; match bd.kind { - BKind::Const => { + // An import binding is immutable (§9.1.1.5.5† CreateImportBinding). + BKind::Const | BKind::Import => { if tdz { self.check_hole(b, sp)?; } @@ -858,9 +1066,155 @@ impl<'a> Cg<'a> { Ok(()) } + /// The scopes between the current one and `target` (a binding's scope, or + /// [`NONE`] for a global) whose object a reference to a name that none of + /// the scopes in between binds must consult first, innermost first, each with + /// whether it is a `with` statement's (§9.1.2.1 GetIdentifierReference step 1: + /// the environment's HasBinding comes before the outer environment's). A + /// `with` body is one; the function scope of sloppy code that calls direct eval + /// is another (its variable object, §19.2.1.3†), but only for a reference + /// that leaves the function: its own bindings cannot be shadowed by what eval + /// declares. + fn dyn_scopes(&self, target: u32) -> R> { + let mut out = Vec::new(); + let mut s = self.f.cur_scope; + while s != NONE && s != target { + let sc = self.scope(s)?; + match sc.kind { + SKind::With => out.push((s, true)), + SKind::Function | SKind::Body if self.an.dyn_binding.contains_key(&s) => { + out.push((s, false)); + } + _ => {} + } + s = sc.parent; + } + Ok(out) + } + + /// The dynamic scopes of the identifier reference `n`. + fn ident_dyn(&self, n: u32) -> R> { + let b = self.res(n); + let target = if b == NONE { + NONE + } else { + self.binding(b)?.scope + }; + self.dyn_scopes(target) + } + + /// ResolveBinding of the identifier reference `n` when a dynamic scope lies + /// in front of its static resolution (§9.1.2.1 step 1: "If env.HasBinding(name) + /// is true, return the Reference Record"): tests each such scope's object + /// with [`Instr::WithHas`], innermost first, and leaves the first that has + /// the name in the returned register, `undefined` when none does. When + /// `this` is wanted (a call, §13.3.6.2† EvaluateCall: the reference's + /// WithBaseObject §9.1.1.2.11†), it is that object for a `with` scope and + /// `undefined` otherwise. `None` if no dynamic scope applies. + fn resolve_dyn(&mut self, n: u32, this: Option) -> R> { + let dyns = self.ident_dyn(n)?; + if dyns.is_empty() { + return Ok(None); + } + let sp = self.sp(n); + let name = self.name_of(n); + let key = self.konst(Const::String(self.name_units(name)?))?; + let holder = self.alloc()?; + self.emit(Instr::LoadUndefined { dst: holder }, sp)?; + if let Some(t) = this { + self.emit(Instr::LoadUndefined { dst: t }, sp)?; + } + let done = self.label(); + for (s, is_with) in dyns { + let m = self.mark(); + let (obj, found) = (self.alloc()?, self.alloc()?); + let slot = self.dyn_slot(s)?; + let env = self.env_for_scope(s, sp)?; + self.emit( + Instr::GetEnvSlot { + dst: obj, + env, + idx: slot, + }, + sp, + )?; + self.emit( + Instr::WithHas { + dst: found, + obj, + key, + }, + sp, + )?; + let next = self.label(); + self.jump_if_false(found, next, sp)?; + self.emit( + Instr::Move { + dst: holder, + src: obj, + }, + sp, + )?; + if let (Some(t), true) = (this, is_with) { + self.emit(Instr::Move { dst: t, src: obj }, sp)?; + } + self.jump(done, sp)?; + self.bind(next)?; + self.release(m); + } + self.bind(done)?; + Ok(Some(holder)) + } + + /// The slot of scope `s`'s environment object that holds its binding object. + fn dyn_slot(&self, s: u32) -> R { + let b = self + .an + .dyn_binding + .get(&s) + .copied() + .ok_or(CompileError::Internal("scope without a binding object"))?; + Ok(self.binding(b)?.slot) + } + /// `dst := the identifier reference n`: a binding, or a global /// (§9.1.2.1 GetIdentifierReference, GetValue §6.2.5.5). fn read_ident(&mut self, n: u32, dst: Reg) -> R<()> { + let m = self.mark(); + let h = self.resolve_dyn(n, None)?; + self.read_ident_h(n, dst, h)?; + self.release(m); + Ok(()) + } + + /// As [`Self::read_ident`], for a reference resolved by + /// [`Self::resolve_dyn`] to `holder` (GetBindingValue §9.1.1.2.6†). + fn read_ident_h(&mut self, n: u32, dst: Reg, holder: Option) -> R<()> { + let sp = self.sp(n); + let Some(h) = holder else { + return self.read_ident_static(n, dst); + }; + let (st, end) = (self.label(), self.label()); + let name = self.name_of(n); + let key = self.konst(Const::String(self.name_units(name)?))?; + let target = Self::label_id(st)?; + self.emit(Instr::JumpIfUndefined { src: h, target }, sp)?; + self.emit( + Instr::WithGet { + dst, + obj: h, + key, + strict: self.f.strict, + }, + sp, + )?; + self.jump(end, sp)?; + self.bind(st)?; + self.read_ident_static(n, dst)?; + self.bind(end) + } + + fn read_ident_static(&mut self, n: u32, dst: Reg) -> R<()> { let sp = self.sp(n); let b = self.res(n); if b == NONE { @@ -876,6 +1230,41 @@ impl<'a> Cg<'a> { /// Assigns `src` to the identifier reference `n` (PutValue §6.2.5.6). fn write_ident(&mut self, n: u32, src: Reg) -> R<()> { + let m = self.mark(); + let h = self.resolve_dyn(n, None)?; + self.write_ident_h(n, src, h)?; + self.release(m); + Ok(()) + } + + /// As [`Self::write_ident`], for a reference resolved by + /// [`Self::resolve_dyn`] to `holder` (SetMutableBinding §9.1.1.2.5†). + fn write_ident_h(&mut self, n: u32, src: Reg, holder: Option) -> R<()> { + let sp = self.sp(n); + let Some(h) = holder else { + return self.write_ident_static(n, src); + }; + let (st, end) = (self.label(), self.label()); + let name = self.name_of(n); + let key = self.konst(Const::String(self.name_units(name)?))?; + let target = Self::label_id(st)?; + self.emit(Instr::JumpIfUndefined { src: h, target }, sp)?; + self.emit( + Instr::WithSet { + obj: h, + key, + val: src, + strict: self.f.strict, + }, + sp, + )?; + self.jump(end, sp)?; + self.bind(st)?; + self.write_ident_static(n, src)?; + self.bind(end) + } + + fn write_ident_static(&mut self, n: u32, src: Reg) -> R<()> { let sp = self.sp(n); let b = self.res(n); if b == NONE { @@ -916,7 +1305,14 @@ impl<'a> Cg<'a> { *slot = Some(r); } } - if sc.has_env() { + if sc.kind == SKind::Module { + // The runtime created the module environment when it linked the + // module (§16.2.1.6.4†), and the body runs in it: it is the callee + // environment. + if sc.has_env() { + self.f.env_regs.insert(s, self.f.base_env); + } + } else if sc.has_env() { let parent = if matches!(sc.kind, SKind::Function | SKind::Script) { self.f.base_env } else { @@ -936,7 +1332,17 @@ impl<'a> Cg<'a> { self.f.cur_scope = s; for &b in &sc.bindings { let bd = self.binding(b)?; + // A module's declarations were initialized when it was linked. + if sc.kind == SKind::Module + && matches!( + bd.kind, + BKind::Var | BKind::Let | BKind::Const | BKind::Import + ) + { + continue; + } match bd.kind { + BKind::Import => {} BKind::Var => { if bd.param != NONE { if bd.captured { @@ -945,7 +1351,18 @@ impl<'a> Cg<'a> { } else if bd.decl_fn == NONE { let m = self.mark(); let t = self.alloc()?; - self.emit(Instr::LoadUndefined { dst: t }, sp)?; + // §10.2.11† step 28.f.i: in a body scope, a `var` that is + // also a parameter (or `arguments`) starts with that + // binding's value, any other `var` with `undefined`. + let param = if sc.kind == SKind::Body { + self.scope(self.f.fscope)?.names.get(&bd.name).copied() + } else { + None + }; + match param { + Some(pb) => self.read_binding(pb, usize::MAX, t, sp)?, + None => self.emit(Instr::LoadUndefined { dst: t }, sp)?, + } self.init_binding(b, t, sp)?; self.release(m); } @@ -1029,6 +1446,21 @@ impl<'a> Cg<'a> { self.release(m); } BKind::Hidden => {} + // Set by the `with` statement, once its object is known. + BKind::WithObject => {} + BKind::EvalVars => { + // The variable object of sloppy eval code's declarations: + // an ordinary object without a prototype, so that + // WithHas finds only what eval declared and no + // @@unscopables. + let m = self.mark(); + let (o, nul) = (self.alloc()?, self.alloc()?); + self.emit(Instr::NewObject { dst: o }, sp)?; + self.emit(Instr::LoadNull { dst: nul }, sp)?; + self.emit(Instr::SetLiteralProto { obj: o, val: nul }, sp)?; + self.init_binding(b, o, sp)?; + self.release(m); + } } } // Hoisted function declarations, in source order. @@ -1039,7 +1471,7 @@ impl<'a> Cg<'a> { let m = self.mark(); // A script's functions were created by `DeclareGlobals` (§16.1.7† // step 16), before this prologue. - if sc.kind != SKind::Script + if !matches!(sc.kind, SKind::Script | SKind::Module) && let Some(&b) = sc.names.get(&name) && self.binding(b)?.decl_fn == fnode { @@ -1082,9 +1514,9 @@ impl<'a> Cg<'a> { self.out.push(None); // Parameters, name and body. let (nparams, length, body): (u32, u32, Option) = match nd.kind { - NodeKind::Script => (0, 0, None), + NodeKind::Script | NodeKind::Module => (0, 0, None), NodeKind::ClassStaticBlock => (0, 0, Some(self.child(nd, 0)?)), - NodeKind::ArrowFunction => { + k if scope::is_arrow_kind(k) => { let p = self.child(nd, 0)?; let pn = self.node(p)?; let (np, len) = if pn.kind == NodeKind::FormalParameters { @@ -1103,7 +1535,7 @@ impl<'a> Cg<'a> { let override_name = self.name_override.take(); let name: Vec = match nd.kind { _ if override_name.is_some() => override_name.unwrap_or_default(), - NodeKind::FunctionDeclaration | NodeKind::FunctionExpression => { + k if scope::is_decl_kind(k) || scope::is_fexpr_kind(k) => { let nm = self.child(nd, 0)?; if self.node(nm)?.kind == NodeKind::BindingIdentifier { self.name_units(self.name_of(nm))? @@ -1136,9 +1568,17 @@ impl<'a> Cg<'a> { }; let mut fresh = FnState::new(kind, sc.strict, span, nparams); fresh.fscope = sc_id; + let flavor = if nd.kind == NodeKind::Module && self.module_tla { + // ExecuteAsyncModule (§16.2.1.5.3†): the body is evaluated as an + // async function body (AsyncBlockStart). + Flavor::Async + } else { + scope::flavor_of(nd) + }; + fresh.flavor = flavor; fresh.simple_params = match nd.kind { - NodeKind::Script | NodeKind::ClassStaticBlock => true, - NodeKind::ArrowFunction => { + NodeKind::Script | NodeKind::Module | NodeKind::ClassStaticBlock => true, + k if scope::is_arrow_kind(k) => { let p = self.child(nd, 0)?; self.node(p)?.kind != NodeKind::FormalParameters || scope::is_simple_params(self.ast, p)? @@ -1155,6 +1595,7 @@ impl<'a> Cg<'a> { FnMeta { name, kind, + flavor, nparams, length, span, @@ -1169,6 +1610,7 @@ impl<'a> Cg<'a> { let FnMeta { name, kind, + flavor, nparams, length, span, @@ -1200,6 +1642,7 @@ impl<'a> Cg<'a> { let code = FunctionCode { name, kind, + flavor, strict: st.strict, span, nparams, @@ -1265,6 +1708,26 @@ impl<'a> Cg<'a> { self.emit(Instr::DeclareGlobals { k }, sp) } + /// Compiles the module's top-level function declarations to templates (the + /// runtime instantiates them when it links the module, §16.2.1.6.4† + /// step 27, and the record lists them: [`ModuleBindingInit::Function`]). + fn module_functions(&mut self, sc_id: u32) -> R<()> { + let sc = self.scope(sc_id)?; + for &fnode in &sc.funcs { + let fnn = self.node(fnode)?; + let name_node = self.child(fnn, 0)?; + if self.node(name_node)?.kind != NodeKind::BindingIdentifier { + // `export default function () {}` is named "default" + // (§16.2.3.7†: the function's name is "default"). + self.name_override = Some("default".encode_utf16().collect()); + } + let idx = self.compile_function(fnode, FunctionKind::Normal)?; + self.name_override = None; + self.module_fns.push((fnode, idx)); + } + Ok(()) + } + /// The prologue, the body and the implicit return of function `nd`. fn function_body(&mut self, nd: &Node, sc_id: u32, body: Option) -> R<()> { let span = self.f.span; @@ -1287,10 +1750,30 @@ impl<'a> Cg<'a> { if self.f.kind == FunctionKind::Script { self.declare_globals(sc_id, start)?; } + if self.f.kind == FunctionKind::Module { + self.module_functions(sc_id)?; + } self.enter_scope(sc_id, start)?; self.function_prologue(nd, start)?; + // The body's own scope, when the parameters have expressions (§10.2.11† + // step 28). + let body_scope = match body { + Some(b) if self.node(b)?.kind == NodeKind::FunctionBody => { + self.an.own_scope.get(b as usize).copied().unwrap_or(NONE) + } + _ => NONE, + }; + if body_scope != NONE { + self.enter_scope(body_scope, start)?; + } + if matches!(self.f.flavor, Flavor::Generator | Flavor::AsyncGenerator) { + // EvaluateGeneratorBody §15.5.2† steps 2 to 4: the generator object + // is created after FunctionDeclarationInstantiation (step 1), which + // this prologue was, and returned to the caller. + self.emit(Instr::GeneratorStart, start)?; + } match nd.kind { - NodeKind::Script => { + NodeKind::Script | NodeKind::Module => { for &c in &nd.children { self.stmt(c, &[])?; } @@ -1312,6 +1795,9 @@ impl<'a> Cg<'a> { } } } + if body_scope != NONE { + self.exit_scope(body_scope)?; + } let m = self.mark(); let t = self.alloc()?; self.emit(Instr::LoadUndefined { dst: t }, end)?; @@ -1374,14 +1860,14 @@ impl<'a> Cg<'a> { let c = self.expr_any(test)?; self.jump_if_false(c, else_l, self.sp(test))?; self.release(m); - self.stmt(cons, &[])?; + self.if_clause(cons)?; if self.node(alt)?.kind == NodeKind::Absent { self.bind(else_l) } else { let end_l = self.label(); self.jump(end_l, sp)?; self.bind(else_l)?; - self.stmt(alt, &[])?; + self.if_clause(alt)?; self.bind(end_l) } } @@ -1392,6 +1878,10 @@ impl<'a> Cg<'a> { self.for_in_of(n, nd, sp, labels) } NodeKind::ClassDeclaration => self.class_declaration(n), + // ImportDeclaration evaluation (§16.2.2†) is "Return empty": the + // imports were bound when the module was linked. + NodeKind::ImportDeclaration => Ok(()), + NodeKind::ExportDeclaration => self.export_declaration(nd), NodeKind::SwitchStatement => self.switch_stmt(n, nd, sp, labels), NodeKind::LabelledStatement => { let lab = self.child(nd, 0)?; @@ -1439,6 +1929,13 @@ impl<'a> Cg<'a> { let t = self.alloc()?; self.emit(Instr::LoadUndefined { dst: t }, sp)?; t + } else if self.f.flavor == Flavor::AsyncGenerator { + // §14.10.1 ReturnStatement evaluation step 3.b: in an async + // generator the operand is awaited first. + let v = self.alloc()?; + let e = self.expr_any(e)?; + self.emit_await(e, v, sp)?; + v } else { self.expr_any(e)? }; @@ -1455,11 +1952,56 @@ impl<'a> Cg<'a> { Ok(()) } NodeKind::TryStatement => self.try_stmt(nd, sp), + NodeKind::WithStatement => { + // §14.11.2† WithStatement evaluation: "Let obj be ? + // ToObject(? GetValue(val))" (step 2), a new object + // environment over it (steps 3 and 4), then the body. + let (e, body) = (self.child(nd, 0)?, self.child(nd, 1)?); + let s = self.an.own_scope.get(n as usize).copied().unwrap_or(NONE); + let m = self.mark(); + let obj = self.alloc()?; + { + let m2 = self.mark(); + let v = self.expr_any(e)?; + self.emit(Instr::ToObject { dst: obj, src: v }, self.sp(e))?; + self.release(m2); + } + self.enter_scope(s, sp)?; + let wb = self + .an + .dyn_binding + .get(&s) + .copied() + .ok_or(CompileError::Internal("with scope without a binding"))?; + self.init_binding(wb, obj, sp)?; + self.stmt(body, &[])?; + self.exit_scope(s)?; + self.release(m); + Ok(()) + } NodeKind::FunctionDeclaration => self.annexb_copy(n, sp), + // Hoisted to the entry of their scope; Annex B.3.3 does not apply to + // generators and async functions. + NodeKind::GeneratorDeclaration + | NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncGeneratorDeclaration => Ok(()), _ => Err(CompileError::Internal("unexpected statement kind")), } } + /// A clause of an `if`: a function declaration there is treated as if it + /// were in a block of its own (Annex B.3.4†). + fn if_clause(&mut self, c: u32) -> R<()> { + match self.an.implicit_blocks.get(&c) { + Some(&s) => { + self.enter_scope(s, self.sp(c))?; + self.stmt(c, &[])?; + self.exit_scope(s) + } + None => self.stmt(c, &[]), + } + } + /// `var a = e, b;`: an initializer is an assignment to the name (a `var` /// inside a `catch (a)` assigns the parameter: Annex B.3.4). fn var_declaration(&mut self, d: u32) -> R<()> { @@ -1498,6 +2040,9 @@ impl<'a> Cg<'a> { /// names: initialization if `is_init`, else assignment. fn assign_to_ident(&mut self, target: u32, init: u32, is_init: bool) -> R<()> { let sp = self.sp(target); + // §14.3.2.1† and §14.3.1.2† step 3.a: NamedEvaluation of an anonymous + // function or class definition with the binding's name. + self.named_ident(init, target)?; let b = self.res(target); if b != NONE { let bd = self.binding(b)?; @@ -2136,6 +2681,13 @@ impl<'a> Cg<'a> { self.emit(Instr::LoadConst { dst: d, k }, sp) } NodeKind::NullLiteral => self.emit(Instr::LoadNull { dst: d }, sp), + NodeKind::RegularExpressionLiteral => { + // §13.2.7.3† RegularExpressionLiteral evaluation: a new RegExp + // object per evaluation, from the pattern and the flags. + let (pattern, flags) = literal::regex_at(self.src, nd.start, nd.end)?; + let k = self.konst(Const::RegExp(RegExpSite { pattern, flags }))?; + self.emit(Instr::NewRegExp { dst: d, k }, sp) + } NodeKind::BooleanLiteral => { let flag = self.src.get(nd.start).copied() == Some(u32::from(b't')); self.emit(Instr::LoadBool { dst: d, flag }, sp) @@ -2145,8 +2697,42 @@ impl<'a> Cg<'a> { self.read_this(b, d, sp) } NodeKind::IdentifierReference => self.read_ident(n, d), - NodeKind::FunctionExpression => self.make_closure(n, d, FunctionKind::Normal), - NodeKind::ArrowFunction => self.make_closure(n, d, FunctionKind::Arrow), + k if scope::is_fexpr_kind(k) => self.make_closure(n, d, FunctionKind::Normal), + k if scope::is_arrow_kind(k) => self.make_closure(n, d, FunctionKind::Arrow), + NodeKind::AwaitExpression => { + // §15.8.5† AwaitExpression evaluation: "Return ? Await(value)". + let m = self.mark(); + let arg = self.child(nd, 0)?; + let v = self.expr_any(arg)?; + self.emit_await(v, d, sp)?; + self.release(m); + Ok(()) + } + NodeKind::YieldExpression => self.yield_expr(nd, d, sp), + NodeKind::ImportMeta => self.emit(Instr::ImportMeta { dst: d }, sp), + NodeKind::ImportCall => { + // EvaluateImportCall (§13.3.10.1†): the specifier, then the + // options (undefined when absent), then the op. + let m = self.mark(); + let (spec, opts) = (self.child(nd, 0)?, self.child(nd, 1)?); + let (sr, or) = (self.alloc()?, self.alloc()?); + self.expr_to(spec, Some(sr), true)?; + if self.node(opts)?.kind == NodeKind::Absent { + self.emit(Instr::LoadUndefined { dst: or }, sp)?; + } else { + self.expr_to(opts, Some(or), true)?; + } + self.emit( + Instr::ImportCall { + dst: d, + specifier: sr, + options: or, + }, + sp, + )?; + self.release(m); + Ok(()) + } NodeKind::MemberExpression => { let (obj, prop) = (self.child(nd, 0)?, self.child(nd, 1)?); let o = self.operand(obj, &[prop])?; @@ -2278,9 +2864,39 @@ impl<'a> Cg<'a> { } AOp::Typeof => { let inner = self.unparen(operand)?; - if self.node(inner)?.kind == NodeKind::IdentifierReference - && self.res(inner) == NONE - { + let is_ident = self.node(inner)?.kind == NodeKind::IdentifierReference; + if is_ident && !self.ident_dyn(inner)?.is_empty() { + // The reference is resolved first; a name that no `with` + // object or eval variable has is the static case below. + let m = self.mark(); + let t = self.alloc()?; + let h = self.resolve_dyn(inner, None)?; + let (st, end) = (self.label(), self.label()); + let target = Self::label_id(st)?; + let h = h.ok_or(CompileError::Internal("dynamic reference lost"))?; + self.emit(Instr::JumpIfUndefined { src: h, target }, sp)?; + let name = self.name_of(inner); + let key = self.konst(Const::String(self.name_units(name)?))?; + self.emit( + Instr::WithGet { + dst: t, + obj: h, + key, + strict: self.f.strict, + }, + sp, + )?; + self.jump(end, sp)?; + self.bind(st)?; + if self.res(inner) == NONE { + self.emit(Instr::GetGlobalOrUndefined { dst: t, key }, sp)?; + } else { + self.read_ident_static(inner, t)?; + } + self.bind(end)?; + self.emit(Instr::TypeOf { dst: d, src: t }, sp)?; + self.release(m); + } else if is_ident && self.res(inner) == NONE { // §13.5.3.1 step 2.a: `typeof` of an unresolvable reference is // "undefined", and GetGlobal throws for one; step 3 is // GetValue otherwise. @@ -2318,6 +2934,15 @@ impl<'a> Cg<'a> { let result: Reg; match tn.kind { NodeKind::IdentifierReference => { + // §13.15.2 step 1.a: the reference is resolved (ResolveBinding + // §9.4.2†, which a `with` object or eval variable object can answer) + // before the right side is evaluated. + let dynh = self.resolve_dyn(t, None)?; + // §13.15.2 steps 1.c, 5.d, 6.d: NamedEvaluation of an anonymous + // function definition on the right of `=`, `&&=`, `||=`, `??=`. + if nd.op == AOp::Assign || Self::logical_assign(nd.op).is_some() { + self.named_ident(value, t)?; + } if nd.op == AOp::Assign { if let Some(r) = self.simple_local(t)? { self.expr_to(value, Some(r), false)?; @@ -2325,16 +2950,16 @@ impl<'a> Cg<'a> { } else { let v = self.alloc()?; self.expr_to(value, Some(v), true)?; - self.write_ident(t, v)?; + self.write_ident_h(t, v, dynh)?; result = v; } } else if let Some(short) = Self::logical_assign(nd.op) { let cur = self.alloc()?; - self.read_ident(t, cur)?; + self.read_ident_h(t, cur, dynh)?; let end = self.label(); self.short_circuit(short, cur, end, sp)?; self.expr_to(value, Some(cur), true)?; - self.write_ident(t, cur)?; + self.write_ident_h(t, cur, dynh)?; self.bind(end)?; result = cur; } else { @@ -2358,7 +2983,7 @@ impl<'a> Cg<'a> { } None => { let c = self.alloc()?; - self.read_ident(t, c)?; + self.read_ident_h(t, c, dynh)?; c } }; @@ -2371,7 +2996,7 @@ impl<'a> Cg<'a> { .ok_or(CompileError::Internal("unknown compound assignment"))?; self.emit(ins, sp)?; if local.is_none() { - self.write_ident(t, out)?; + self.write_ident_h(t, out, dynh)?; } result = out; } @@ -2526,10 +3151,13 @@ impl<'a> Cg<'a> { result = r; } } else { + // §13.4.2.1† step 1 (and the like): the reference is resolved + // once, then read and written. + let dynh = self.resolve_dyn(t, None)?; let cur = self.alloc()?; - self.read_ident(t, cur)?; + self.read_ident_h(t, cur, dynh)?; let (old, new) = self.step_values(cur, wanted_old, &step, sp)?; - self.write_ident(t, new)?; + self.write_ident_h(t, new, dynh)?; result = if prefix { new } else { old }; } } diff --git a/crates/clazel-js-vm/src/compile/classes.rs b/crates/clazel-js-vm/src/compile/classes.rs index 6ccba4c0c..bfbbde1f4 100644 --- a/crates/clazel-js-vm/src/compile/classes.rs +++ b/crates/clazel-js-vm/src/compile/classes.rs @@ -50,10 +50,12 @@ impl<'a> Cg<'a> { .get(&n) .ok_or(CompileError::Internal("class without scopes"))?; let derived = self.node(heritage)?.kind != NodeKind::Absent; + // The class's name: its own, or the name NamedEvaluation (§8.4.5†) gives + // an anonymous class (taken before anything else is compiled). let class_name: Vec = if self.node(name_node)?.kind == NodeKind::BindingIdentifier { self.name_units(self.name_of(name_node))? } else { - Vec::new() + self.name_override.take().unwrap_or_default() }; let m = self.mark(); self.enter_scope(info.scope, sp)?; @@ -275,6 +277,7 @@ impl<'a> Cg<'a> { index, st, FnMeta { + flavor: Flavor::Plain, name, kind, nparams: 0, @@ -351,6 +354,7 @@ impl<'a> Cg<'a> { index, st, FnMeta { + flavor: Flavor::Plain, name: Vec::new(), kind: FunctionKind::Method, nparams: 0, @@ -414,6 +418,11 @@ impl<'a> Cg<'a> { if self.node(init)?.kind == NodeKind::Absent { self.emit(Instr::LoadUndefined { dst: v }, esp)?; } else { + // §15.7.10† ClassFieldDefinition evaluation step 3.b: + // NamedEvaluation of the initializer with the field's name. + if let Some(units) = self.static_key_name(key_node)? { + self.named(init, &units)?; + } self.expr_to(init, Some(v), true)?; } let kk = self.node(key_node)?.kind; diff --git a/crates/clazel-js-vm/src/compile/exports.rs b/crates/clazel-js-vm/src/compile/exports.rs new file mode 100644 index 000000000..ff2cc8066 --- /dev/null +++ b/crates/clazel-js-vm/src/compile/exports.rs @@ -0,0 +1,72 @@ +//! Export declarations (§16.2.3): what a module's body evaluates for each. +//! +//! Everything about modules that is static (the requests, the import and export +//! entries, the bindings of the module environment) is in the record +//! ([`crate::module`]); the body only runs the initializers. An `import`, an +//! `export { .. }`, an `export * from` and an `export default` of a function +//! declaration evaluate to nothing: the runtime linked them (§16.2.1.6.4† +//! InitializeEnvironment). The rest are the declaration they wrap, and +//! `export default` of an expression or an anonymous class, which initializes +//! the binding `*default*` (§16.2.3.7† ExportDeclaration : `export default` +//! AssignmentExpression `;`). + +use super::*; + +impl<'a> Cg<'a> { + /// Evaluates the `ExportDeclaration` node `nd`. + pub(super) fn export_declaration(&mut self, nd: &Node) -> R<()> { + match nd.op { + AOp::ExportAll + | AOp::ExportAllAs + | AOp::ExportFrom + | AOp::ExportNamed + | AOp::ExportDefaultHoistable => Ok(()), + // `export var`, and `export` of a lexical declaration, a function + // or a class: the declaration's own evaluation. + AOp::ExportVar | AOp::ExportDecl => { + let d = self.child(nd, 0)?; + self.stmt(d, &[]) + } + AOp::ExportDefaultClass => { + let c = self.child(nd, 0)?; + let cn = self.node(c)?; + let name_node = self.child(cn, 0)?; + if self.node(name_node)?.kind == NodeKind::BindingIdentifier { + return self.class_declaration(c); + } + // §16.2.3.7† ExportDeclaration : `export default` ClassDeclaration: + // the class is named "default" (BindingClassDeclarationEvaluation + // step 2), and `*default*` is initialized with it. + let m = self.mark(); + let t = self.alloc()?; + self.name_override = Some("default".encode_utf16().collect()); + self.class_definition(c, t)?; + self.init_default(t, self.sp(c))?; + self.release(m); + Ok(()) + } + AOp::ExportDefaultExpression => { + let e = self.child(nd, 0)?; + let m = self.mark(); + // "If IsAnonymousFunctionDefinition(AssignmentExpression) is true, + // then Let value be ? NamedEvaluation of AssignmentExpression with + // argument "default"." + self.named(e, &"default".encode_utf16().collect::>())?; + let v = self.expr_any(e)?; + self.init_default(v, self.sp(e))?; + self.release(m); + Ok(()) + } + _ => Err(CompileError::Internal("unknown export declaration")), + } + } + + /// "Perform ? env.InitializeBinding("*default*", value)". + fn init_default(&mut self, src: Reg, sp: Span) -> R<()> { + let b = self.an.default_binding; + if b == NONE { + return Err(CompileError::Internal("no *default* binding")); + } + self.init_binding(b, src, sp) + } +} diff --git a/crates/clazel-js-vm/src/compile/loops.rs b/crates/clazel-js-vm/src/compile/loops.rs index b09948156..eb55549ca 100644 --- a/crates/clazel-js-vm/src/compile/loops.rs +++ b/crates/clazel-js-vm/src/compile/loops.rs @@ -17,12 +17,16 @@ impl<'a> Cg<'a> { /// `return`: the op has its own exception-table entry (which comes before /// the loop's, as inner entries do), so that a throw from it is not caught by /// the loop's own catch-all. - pub(super) fn emit_iter_close(&mut self, close: Option<(Reg, usize)>, sp: Span) -> R<()> { - if let Some((iter, rethrow)) = close { + pub(super) fn emit_iter_close(&mut self, close: Option<(Reg, usize, bool)>, sp: Span) -> R<()> { + if let Some((iter, rethrow, asyn)) = close { let s = self.label(); let e = self.label(); self.bind(s)?; - self.emit(Instr::IterClose { iter }, sp)?; + if asyn { + self.emit_async_close(iter, sp)?; + } else { + self.emit(Instr::IterClose { iter }, sp)?; + } self.bind(e)?; self.f.handlers.push((s, e, rethrow)); } @@ -58,6 +62,9 @@ impl<'a> Cg<'a> { pub(super) fn for_in_of(&mut self, n: u32, nd: &Node, sp: Span, labels: &[u32]) -> R<()> { let (head, expr, body) = (self.child(nd, 0)?, self.child(nd, 1)?, self.child(nd, 2)?); let is_of = nd.kind == NodeKind::ForOfStatement; + // `for await (x of y)` (§14.7.5.6† ForIn/OfHeadEvaluation with iterKind + // async-iterate): the flag of the node. + let is_await = is_of && nd.flag; let s = self.an.own_scope.get(n as usize).copied().unwrap_or(NONE); let m = self.mark(); // The loop's lexical bindings are in their dead zone while the @@ -77,7 +84,9 @@ impl<'a> Cg<'a> { { let m2 = self.mark(); let v = self.expr_any(expr)?; - if is_of { + if is_await { + self.emit(Instr::GetAsyncIterator { dst: iter, src: v }, self.sp(expr))?; + } else if is_of { self.emit(Instr::GetIterator { dst: iter, src: v }, self.sp(expr))?; } else { self.emit(Instr::ForInIterator { dst: iter, src: v }, self.sp(expr))?; @@ -92,22 +101,59 @@ impl<'a> Cg<'a> { let exc_l = self.label(); let rethrow = self.label(); self.bind(top)?; - self.emit( - Instr::IterNext { - value: val, - done, - iter, - }, - self.sp(expr), - )?; - let end_t = Self::label_id(end)?; - self.emit( - Instr::JumpIfTrue { - cond: done, - target: end_t, - }, - self.sp(expr), - )?; + if is_await { + // ForIn/OfBodyEvaluation §14.7.5.7† step 6.a to 6.d for async-iterate: + // call `next`, await the result, check it is an Object (IterComplete), + // then read `done` and `value`. + let esp = self.sp(expr); + let m2 = self.mark(); + let (res, found) = (self.alloc()?, self.alloc()?); + self.emit( + Instr::IterCallMethod { + dst: res, + found, + iter, + arg: res, + which: 4, + }, + esp, + )?; + self.emit_await(res, res, esp)?; + self.emit(Instr::IterComplete { done, result: res }, esp)?; + let end_t = Self::label_id(end)?; + self.emit( + Instr::JumpIfTrue { + cond: done, + target: end_t, + }, + esp, + )?; + self.emit( + Instr::IterValue { + dst: val, + result: res, + }, + esp, + )?; + self.release(m2); + } else { + self.emit( + Instr::IterNext { + value: val, + done, + iter, + }, + self.sp(expr), + )?; + let end_t = Self::label_id(end)?; + self.emit( + Instr::JumpIfTrue { + cond: done, + target: end_t, + }, + self.sp(expr), + )?; + } self.bind(region_start)?; if s != NONE && self.scope(s)?.has_env() { self.fresh_env(s, sp)?; @@ -135,7 +181,11 @@ impl<'a> Cg<'a> { labels: labels.to_vec(), brk: end, cont: top, - close: if is_of { Some((iter, rethrow)) } else { None }, + close: if is_of { + Some((iter, rethrow, is_await)) + } else { + None + }, }); self.stmt(body, &[])?; self.f.ctl.pop(); @@ -147,7 +197,11 @@ impl<'a> Cg<'a> { self.emit(Instr::GetException { dst: exc }, sp)?; let (qs, qe, sw) = (self.label(), self.label(), self.label()); self.bind(qs)?; - self.emit(Instr::IterClose { iter }, sp)?; + if is_await { + self.emit_async_close_quiet(iter, sp)?; + } else { + self.emit(Instr::IterClose { iter }, sp)?; + } self.bind(qe)?; self.emit(Instr::Throw { src: exc }, sp)?; self.bind(sw)?; diff --git a/crates/clazel-js-vm/src/compile/misc.rs b/crates/clazel-js-vm/src/compile/misc.rs index 7f6aa4e8b..df7478df8 100644 --- a/crates/clazel-js-vm/src/compile/misc.rs +++ b/crates/clazel-js-vm/src/compile/misc.rs @@ -189,6 +189,16 @@ impl<'a> Cg<'a> { Ok(()) } NodeKind::OptionalExpression => self.optional_chain(cn_id, fr, Some(this), sp), + NodeKind::IdentifierReference if !self.ident_dyn(cn_id)?.is_empty() => { + // §13.3.6.1 step 3.b: a reference whose base is an object + // Environment Record of a `with` has WithBaseObject as its `this` + // (§9.1.1.2.11†), and otherwise `undefined`. + let m = self.mark(); + let h = self.resolve_dyn(cn_id, Some(this))?; + self.read_ident_h(cn_id, fr, h)?; + self.release(m); + Ok(()) + } _ => { self.expr_to(cn_id, Some(fr), true)?; self.emit(Instr::LoadUndefined { dst: this }, sp) @@ -207,7 +217,7 @@ impl<'a> Cg<'a> { && self.eval_name != NONE && self.name_of(cn_id) == self.eval_name { - return self.not_yet("direct eval", cn_id); + return self.direct_eval(cn_id, args, d, sp); } let m = self.mark(); let (fr, this) = (self.alloc()?, self.alloc()?); @@ -237,6 +247,188 @@ impl<'a> Cg<'a> { Ok(()) } + /// NamedEvaluation (§8.4.5†) of the initializer `e` for the static name + /// `name`: when `e` is an anonymous function or class definition (§8.4.3† + /// IsAnonymousFunctionDefinition, which sees through parentheses), the next + /// function or class compiled gets `name`, which is the `name` property its + /// SetFunctionName (§10.2.9†) gives it. A name known only when the code runs + /// (a computed key) is not covered. + pub(super) fn named(&mut self, e: u32, name: &[u16]) -> R<()> { + let u = self.unparen(e)?; + let nd = self.node(u)?; + let anonymous = |this: &Self| -> R { + let first = this.child(nd, 0)?; + Ok(this.node(first)?.kind != NodeKind::BindingIdentifier) + }; + let is_anon = match nd.kind { + k if scope::is_fexpr_kind(k) => anonymous(self)?, + NodeKind::ClassExpression => anonymous(self)?, + k if scope::is_arrow_kind(k) => true, + _ => false, + }; + if is_anon { + self.name_override = Some(name.to_vec()); + } + Ok(()) + } + + /// NamedEvaluation of `e` for the name of identifier node `target`. + pub(super) fn named_ident(&mut self, e: u32, target: u32) -> R<()> { + let name = self.name_units(self.name_of(target))?; + self.named(e, &name) + } + + /// The static name a class element or property key gives (§8.4.5† + /// ClassElementName and PropertyName evaluation): an identifier or string + /// literal, or a private name with its `#`; `None` for a computed or numeric key. + pub(super) fn static_key_name(&self, key: u32) -> R>> { + let kn = self.node(key)?; + Ok(match kn.kind { + NodeKind::IdentifierName | NodeKind::IdentifierReference => { + Some(literal::name_units_at(self.src, kn.start)?) + } + NodeKind::StringLiteral => Some(literal::string_at(self.src, kn.start)?), + NodeKind::PrivateIdentifier => { + let mut v: Vec = vec![u16::from(b'#')]; + v.extend(literal::name_units_at(self.src, kn.start)?); + Some(v) + } + _ => None, + }) + } + + /// `eval(args)` written as a direct call (§13.3.6.1† step 6): the value of + /// `eval`, the `this` of the call and the arguments, then + /// [`Instr::DirectEval`], which compares the callee with %eval% and either + /// calls it or performs the eval (PerformEval §19.2.1.1†). + fn direct_eval(&mut self, cn_id: u32, args: u32, d: Reg, sp: Span) -> R<()> { + let m = self.mark(); + let (fr, this) = (self.alloc()?, self.alloc()?); + self.callee_and_this(cn_id, fr, this, sp)?; + match self.arg_list(args)? { + ArgList::Plain(base, argc) => { + let info = self.eval_scope_info()?; + let k = self.konst(Const::EvalScope(info))?; + let env = self.current_env_reg()?; + self.emit( + Instr::DirectEval { + dst: d, + callee: fr, + this, + args: base, + argc, + env, + k, + }, + sp, + )?; + } + ArgList::Spread(arr) => { + let info = self.eval_scope_info()?; + let k = self.konst(Const::EvalScope(info))?; + let env = self.current_env_reg()?; + self.emit( + Instr::DirectEvalSpread { + dst: d, + callee: fr, + this, + args: arr, + env, + k, + }, + sp, + )?; + } + } + self.release(m); + Ok(()) + } + + /// What the host compile path needs of the scopes around the current + /// position (see [`crate::bytecode::EvalScopeInfo`]). + fn eval_scope_info(&self) -> R { + let cur = self.scope(self.f.cur_scope)?; + let mut f = cur.func; + while let Some(fi) = self.an.funcs.get(f as usize) { + if fi.arrow { + f = self.scope(fi.parent_scope)?.func; + } else { + break; + } + } + let (node_kind, derived) = match self.an.funcs.get(f as usize) { + Some(fi) => (self.node(fi.node)?.kind, fi.derived_ctor), + None => return Err(CompileError::Internal("no such function")), + }; + let in_class_init = matches!( + node_kind, + NodeKind::ClassStaticBlock | NodeKind::ClassDeclaration | NodeKind::ClassExpression + ); + let mut scopes = Vec::new(); + let mut s = self.f.cur_scope; + while s != NONE { + let sc = self.scope(s)?; + if sc.has_env() { + let mut names = Vec::new(); + for &b in &sc.bindings { + let bd = self.binding(b)?; + let (kind, named) = match bd.kind { + BKind::Var => (EvalBinding::Var, true), + BKind::Let => (EvalBinding::Let, true), + BKind::Const => (EvalBinding::Const, true), + BKind::Catch => (EvalBinding::Catch, true), + BKind::FuncName => (EvalBinding::FuncName, true), + BKind::Arguments => (EvalBinding::Arguments, true), + BKind::PrivateName => (EvalBinding::PrivateName, true), + BKind::Import => (EvalBinding::Import, true), + BKind::This => (EvalBinding::This, false), + BKind::DerivedThis => (EvalBinding::DerivedThis, false), + BKind::NewTarget => (EvalBinding::NewTarget, false), + BKind::Callee => (EvalBinding::Callee, false), + BKind::Hidden | BKind::WithObject | BKind::EvalVars => continue, + }; + names.push(EvalName { + name: if named { + self.name_units(bd.name)? + } else { + Vec::new() + }, + slot: bd.slot, + kind, + }); + } + let kind = match sc.kind { + SKind::Script => EvalScopeKind::Script, + SKind::Module => EvalScopeKind::Module, + SKind::Function | SKind::Body => EvalScopeKind::Function, + SKind::Class => EvalScopeKind::Class, + SKind::With => EvalScopeKind::With, + SKind::Block | SKind::For | SKind::Switch | SKind::Catch => { + EvalScopeKind::Block + } + }; + let var_object = match (sc.kind, self.an.dyn_binding.get(&s)) { + (SKind::Function | SKind::Body, Some(&b)) => Some(self.binding(b)?.slot), + _ => None, + }; + scopes.push(EvalScope { + kind, + var_object, + names, + }); + } + s = sc.parent; + } + Ok(EvalScopeInfo { + strict: cur.strict, + in_function: f != 0, + in_method: matches!(node_kind, NodeKind::MethodDefinition) || in_class_init, + in_derived_constructor: derived, + in_class_field_initializer: in_class_init, + scopes, + }) + } + /// Evaluates an `Arguments` node: into consecutive registers, or, if it has /// a spread element, into an array. pub(super) fn arg_list(&mut self, args: u32) -> R { @@ -481,6 +673,29 @@ impl<'a> Cg<'a> { )?; self.release(m); } + // Trailing elisions leave no element: ArrayAccumulation (§13.2.4.1†) + // for an Elision adds to the length only, so `length` is stored + // after the last element. + if let Some(&last) = nd.children.last() + && self.node(last)?.kind == NodeKind::Elision + { + let m = self.mark(); + let idx = self.alloc()?; + let imm = i32::try_from(len).map_err(|_| CompileError::TooLarge("array literal"))?; + self.load_int(idx, imm, sp)?; + let k = self.str_const("length")?; + let ic = self.ic(); + self.emit( + Instr::SetProp { + obj: d, + key: k, + val: idx, + ic, + }, + sp, + )?; + self.release(m); + } Ok(()) } @@ -577,6 +792,7 @@ impl<'a> Cg<'a> { NodeKind::StringLiteral => Some(literal::string_at(self.src, kn.start)?), _ => None, }; + let name_units = plain_key.clone(); let proto: Vec = "__proto__".encode_utf16().collect(); // §13.2.5.5† step 3: `__proto__: value`, with a non-computed key. let is_proto_setter = @@ -591,6 +807,12 @@ impl<'a> Cg<'a> { Prop::Computed(r) } }; + if !is_proto_setter + && pn.op == AOp::KeyValue + && let Some(units) = &name_units + { + self.named(value_node, units)?; + } let v = self.expr_any(value_node)?; if is_proto_setter { self.emit(Instr::SetLiteralProto { obj: d, val: v }, psp)?; @@ -716,6 +938,22 @@ impl<'a> Cg<'a> { d: Reg, this_out: Option, sp: Span, + ) -> R<()> { + self.optional_chain_impl(n, d, this_out, false, sp) + } + + /// [`Self::optional_chain`], or with `del` the `delete` of the chain + /// (§13.5.1.2†: `delete` of an OptionalExpression evaluates the chain as a + /// reference; a chain that ends early is `true`, a chain that ends in a + /// property reference deletes it, and one that ends in a call is a value, so + /// `true`). + pub(super) fn optional_chain_impl( + &mut self, + n: u32, + d: Reg, + this_out: Option, + del: bool, + sp: Span, ) -> R<()> { let nd = self.node(n)?; let m = self.mark(); @@ -726,7 +964,8 @@ impl<'a> Cg<'a> { let base = self.child(nd, 0)?; let base = self.unparen(base)?; self.callee_and_this(base, cur, this, sp)?; - for &lk in nd.children.iter().skip(1) { + let last = nd.children.len().saturating_sub(1); + for (li, &lk) in nd.children.iter().enumerate().skip(1) { let ln = self.node(lk)?; let lsp = self.sp(lk); if ln.flag { @@ -739,6 +978,34 @@ impl<'a> Cg<'a> { )?; } let m2 = self.mark(); + if del && li == last && matches!(ln.op, AOp::ChainDot | AOp::ChainBracket) { + let prop = self.child(ln, 0)?; + let idx = if ln.op == AOp::ChainDot { + let pn = self.node(prop)?; + let units = literal::name_units_at(self.src, pn.start)?; + let k = self.konst(Const::String(units))?; + let r = self.alloc()?; + self.emit(Instr::LoadConst { dst: r, k }, lsp)?; + r + } else { + self.expr_any(prop)? + }; + self.emit( + Instr::DeleteProp { + dst: d, + obj: cur, + idx, + }, + lsp, + )?; + self.release(m2); + self.jump(end, sp)?; + self.bind(undef)?; + self.emit(Instr::LoadBool { dst: d, flag: true }, sp)?; + self.bind(end)?; + self.release(m); + return Ok(()); + } match ln.op { AOp::ChainDot | AOp::ChainBracket | AOp::ChainPrivateDot => { self.emit( @@ -793,13 +1060,23 @@ impl<'a> Cg<'a> { } self.release(m2); } - self.emit(Instr::Move { dst: d, src: cur }, sp)?; + if del { + // The chain ended in a call, or in a `?.` that was the whole + // chain: not a property reference, so `delete` is true. + self.emit(Instr::LoadBool { dst: d, flag: true }, sp)?; + } else { + self.emit(Instr::Move { dst: d, src: cur }, sp)?; + } if let Some(t) = this_out { self.emit(Instr::Move { dst: t, src: this }, sp)?; } self.jump(end, sp)?; self.bind(undef)?; - self.emit(Instr::LoadUndefined { dst: d }, sp)?; + if del { + self.emit(Instr::LoadBool { dst: d, flag: true }, sp)?; + } else { + self.emit(Instr::LoadUndefined { dst: d }, sp)?; + } self.bind(end)?; self.release(m); Ok(()) @@ -841,8 +1118,49 @@ impl<'a> Cg<'a> { sp, ) } - NodeKind::IdentifierReference => self.not_yet("delete of a variable", inner), - NodeKind::OptionalExpression => self.not_yet("delete of an optional chain", inner), + NodeKind::IdentifierReference => { + // §13.5.1.2† step 5: `delete name` is DeleteBinding on the + // environment that holds the name. A binding of a function or block + // scope is not deletable (§9.1.1.1.7†: false), a global is + // [`Instr::DeleteGlobal`], and the object of a `with` or the + // variable object of eval is [`Instr::WithDelete`]. + let m = self.mark(); + let h = self.resolve_dyn(inner, None)?; + let name = self.name_of(inner); + let key = self.konst(Const::String(self.name_units(name)?))?; + let (st, end) = (self.label(), self.label()); + if let Some(h) = h { + let target = Self::label_id(st)?; + self.emit(Instr::JumpIfUndefined { src: h, target }, sp)?; + self.emit( + Instr::WithDelete { + dst: d, + obj: h, + key, + }, + sp, + )?; + self.jump(end, sp)?; + self.bind(st)?; + } + if self.res(inner) == NONE { + self.emit(Instr::DeleteGlobal { dst: d, key }, sp)?; + } else { + self.emit( + Instr::LoadBool { + dst: d, + flag: false, + }, + sp, + )?; + } + if h.is_some() { + self.bind(end)?; + } + self.release(m); + Ok(()) + } + NodeKind::OptionalExpression => self.optional_chain_impl(inner, d, None, true, sp), _ => { // §13.5.1.2 step 5: `delete` of a non-reference is true, // after evaluating the operand. diff --git a/crates/clazel-js-vm/src/compile/patterns.rs b/crates/clazel-js-vm/src/compile/patterns.rs index 777ff349f..6ff3a0bbe 100644 --- a/crates/clazel-js-vm/src/compile/patterns.rs +++ b/crates/clazel-js-vm/src/compile/patterns.rs @@ -136,8 +136,18 @@ impl<'a> Cg<'a> { /// The default value: `if t is undefined, t := init` (§14.3.3.3† /// SingleNameBinding steps 3 and 4: "If Initializer is present and v is /// undefined"). - fn default_value(&mut self, t: Reg, init: Option) -> R<()> { + fn default_value(&mut self, t: Reg, init: Option, target: u32) -> R<()> { if let Some(i) = init { + // NamedEvaluation (§8.4.5†) of the initializer when the target is a + // plain identifier (SingleNameBinding step 4.a: "If IsAnonymousFunctionDefinition(Initializer) + // is true, set v to ? NamedEvaluation of Initializer with argument bindingId"). + let tk = self.node(target)?.kind; + if matches!( + tk, + NodeKind::BindingIdentifier | NodeKind::IdentifierReference + ) { + self.named_ident(i, target)?; + } let skip = self.label(); let target = Self::label_id(skip)?; self.emit(Instr::JumpIfNotUndefined { src: t, target }, self.sp(i))?; @@ -269,7 +279,7 @@ impl<'a> Cg<'a> { let tgt = self.prepare_target(target_node)?; let t = self.alloc()?; self.load_prop(t, val, &key, psp)?; - self.default_value(t, init)?; + self.default_value(t, init, target_node)?; self.store_target(&tgt, t, mode)?; self.release(m2); } @@ -430,7 +440,7 @@ impl<'a> Cg<'a> { let tgt = self.prepare_target(target_node)?; let t = self.alloc()?; self.iter_step(iter, done, t, esp)?; - self.default_value(t, init)?; + self.default_value(t, init, target_node)?; self.store_target(&tgt, t, mode)?; } } @@ -525,8 +535,8 @@ impl<'a> Cg<'a> { self.release(m); } let fp = match nd.kind { - NodeKind::Script | NodeKind::ClassStaticBlock => return Ok(()), - NodeKind::ArrowFunction => self.child(nd, 0)?, + NodeKind::Script | NodeKind::Module | NodeKind::ClassStaticBlock => return Ok(()), + k if scope::is_arrow_kind(k) => self.child(nd, 0)?, _ => self.child(nd, 1)?, }; if self.node(fp)?.kind != NodeKind::FormalParameters || self.f.simple_params { @@ -554,7 +564,7 @@ impl<'a> Cg<'a> { let t = self.alloc()?; let src = u32::try_from(idx).map_err(|_| CompileError::TooLarge("parameters"))?; self.emit(Instr::Move { dst: t, src }, esp)?; - self.default_value(t, init)?; + self.default_value(t, init, target_node)?; let tgt = self.prepare_target(target_node)?; self.store_target(&tgt, t, Mode::Init)?; } diff --git a/crates/clazel-js-vm/src/compile/suspend.rs b/crates/clazel-js-vm/src/compile/suspend.rs new file mode 100644 index 000000000..9b9812647 --- /dev/null +++ b/crates/clazel-js-vm/src/compile/suspend.rs @@ -0,0 +1,384 @@ +//! Generators and async functions (phase-7.md item 7.7: "frames are copied to a +//! heap object on suspend and back on resume"): `await`, `yield`, `yield*` and +//! the async closing of iterators. +//! +//! The compiler writes a suspension as one op ([`Instr::Await`], [`Instr::Yield`] +//! or [`Instr::YieldDelegate`]) that gives a value to the consumer and, when the +//! frame is resumed, writes the sent value and a mode into two registers: 0 for a +//! value, 1 for a throw and 2 for a return. The ops after it are jumps on the +//! mode, so everything a resumption can do (throw at the suspension, return +//! through the enclosing `finally` blocks) is ordinary compiled control flow, and +//! the suspension is the only thing the frame copy has to know about. The +//! registers are the frame, so a suspension keeps all locals. +//! +//! - **`await`** (§15.8.5†): `Await`, then a throw if the mode is 1 (Await +//! §6.2.3.1† step "If result is a throw completion, return ? result"). +//! - **`yield`** (§15.5.5†): in a generator `Yield`; in an async generator the +//! operand is awaited first and a return resumption awaits its value +//! (AsyncGeneratorYield §27.6.3.8† step 9). A throw resumption throws at the +//! `yield`; a return resumption is a `return` statement there, so it runs the +//! `finally` blocks that enclose it. +//! - **`yield*`** (§15.5.5† YieldExpression : `yield *` AssignmentExpression): +//! the loop of the standard, with the received completion held as a value and +//! a mode, calling `next`, `throw` or `return` of the inner iterator +//! ([`Instr::IterCallMethod`]), and in an async generator awaiting each result. + +use super::*; + +impl<'a> Cg<'a> { + /// `dst := await src` (Await §6.2.3.1†): the value, or a throw of the + /// rejection. + pub(super) fn emit_await(&mut self, src: Reg, dst: Reg, sp: Span) -> R<()> { + let m = self.mark(); + let mode = self.alloc()?; + self.emit( + Instr::Await { + src, + value: dst, + mode, + }, + sp, + )?; + let ok = self.label(); + self.jump_if_false(mode, ok, sp)?; + self.emit(Instr::Throw { src: dst }, sp)?; + self.bind(ok)?; + self.release(m); + Ok(()) + } + + /// An `await` whose rejection is ignored (the close of an iterator that is + /// being closed for a throw, §7.4.13† AsyncIteratorClose step 5: the + /// completion of the await does not replace the throw). + fn emit_await_quiet(&mut self, src: Reg, sp: Span) -> R<()> { + let m = self.mark(); + let (value, mode) = (self.alloc()?, self.alloc()?); + self.emit(Instr::Await { src, value, mode }, sp)?; + self.release(m); + Ok(()) + } + + /// AsyncIteratorClose of an iterator for a normal completion (a `break` out + /// of a `for await` loop, §7.4.13†): the result of `return` is awaited and + /// must be an Object, and a throw replaces the completion. + pub(super) fn emit_async_close(&mut self, iter: Reg, sp: Span) -> R<()> { + let m = self.mark(); + let (res, found) = (self.alloc()?, self.alloc()?); + self.emit( + Instr::IterCallMethod { + dst: res, + found, + iter, + arg: res, + which: 3, + }, + sp, + )?; + let skip = self.label(); + self.jump_if_false(found, skip, sp)?; + self.emit_await(res, res, sp)?; + self.emit(Instr::ThrowIfNotObject { src: res }, sp)?; + self.bind(skip)?; + self.release(m); + Ok(()) + } + + /// AsyncIteratorClose for a throw completion (§7.4.13† step 5: errors of + /// the `return` call and of the await are dropped, so the caller's throw + /// stands; the caller covers this with its own handler for the call). + pub(super) fn emit_async_close_quiet(&mut self, iter: Reg, sp: Span) -> R<()> { + let m = self.mark(); + let (res, found) = (self.alloc()?, self.alloc()?); + self.emit( + Instr::IterCallMethod { + dst: res, + found, + iter, + arg: res, + which: 3, + }, + sp, + )?; + let skip = self.label(); + self.jump_if_false(found, skip, sp)?; + self.emit_await_quiet(res, sp)?; + self.bind(skip)?; + self.release(m); + Ok(()) + } + + /// What follows a suspension: the mode in `mode` (0 value, 1 throw, 2 + /// return) says whether the value in `rv` is the result of the expression + /// (jump to `ok`), is thrown, or is returned from the function. + fn after_yield(&mut self, rv: Reg, mode: Reg, ok: usize, sp: Span) -> R<()> { + self.jump_if_false(mode, ok, sp)?; + let m = self.mark(); + let (k, c) = (self.alloc()?, self.alloc()?); + self.load_int(k, 1, sp)?; + self.emit( + Instr::StrictEq { + dst: c, + a: mode, + b: k, + }, + sp, + )?; + let ret = self.label(); + self.jump_if_false(c, ret, sp)?; + self.emit(Instr::Throw { src: rv }, sp)?; + self.bind(ret)?; + if self.f.flavor == Flavor::AsyncGenerator { + // AsyncGeneratorYield §27.6.3.8† step 9.b: a return resumption + // awaits its value; a rejection is thrown. + self.emit_await(rv, rv, sp)?; + } + self.emit_exit(Exit::Return, Some(rv), sp)?; + self.release(m); + Ok(()) + } + + /// `yield` and `yield*` into `d`. + pub(super) fn yield_expr(&mut self, nd: &Node, d: Reg, sp: Span) -> R<()> { + if nd.op == AOp::YieldDelegate { + return self.yield_star(nd, d, sp); + } + let arg = self.child(nd, 0)?; + let m = self.mark(); + let (v, rv, mode) = (self.alloc()?, self.alloc()?, self.alloc()?); + if self.node(arg)?.kind == NodeKind::Absent { + self.emit(Instr::LoadUndefined { dst: v }, sp)?; + } else { + self.expr_to(arg, Some(v), true)?; + } + if self.f.flavor == Flavor::AsyncGenerator { + // §15.5.5† step 3.b: "Return ? AsyncGeneratorYield(? Await(value))". + self.emit_await(v, v, sp)?; + } + self.emit( + Instr::Yield { + src: v, + value: rv, + mode, + }, + sp, + )?; + let ok = self.label(); + self.after_yield(rv, mode, ok, sp)?; + self.bind(ok)?; + self.emit(Instr::Move { dst: d, src: rv }, sp)?; + self.release(m); + Ok(()) + } + + /// `yield* operand`. + fn yield_star(&mut self, nd: &Node, d: Reg, sp: Span) -> R<()> { + let asyn = self.f.flavor == Flavor::AsyncGenerator; + let arg = self.child(nd, 0)?; + let m = self.mark(); + let iter = self.alloc()?; + let (rv, mode, res, found, done, val) = ( + self.alloc()?, + self.alloc()?, + self.alloc()?, + self.alloc()?, + self.alloc()?, + self.alloc()?, + ); + { + let m2 = self.mark(); + let v = self.expr_any(arg)?; + let ins = if asyn { + Instr::GetAsyncIterator { dst: iter, src: v } + } else { + Instr::GetIterator { dst: iter, src: v } + }; + self.emit(ins, sp)?; + self.release(m2); + } + // received := NormalCompletion(undefined). + self.emit(Instr::LoadUndefined { dst: rv }, sp)?; + self.load_int(mode, 0, sp)?; + let (top, do_next, do_throw, yield_l, throw_missing, end) = ( + self.label(), + self.label(), + self.label(), + self.label(), + self.label(), + self.label(), + ); + self.bind(top)?; + // Dispatch on the received completion. + self.jump_if_false(mode, do_next, sp)?; + let (k, c) = (self.alloc()?, self.alloc()?); + self.load_int(k, 1, sp)?; + self.emit( + Instr::StrictEq { + dst: c, + a: mode, + b: k, + }, + sp, + )?; + self.jump_if_true(c, do_throw, sp)?; + // A return completion (step 7.c): `return` of the inner iterator. + let ret_missing = self.label(); + self.emit( + Instr::IterCallMethod { + dst: res, + found, + iter, + arg: rv, + which: 2, + }, + sp, + )?; + self.jump_if_false(found, ret_missing, sp)?; + if asyn { + self.emit_await(res, res, sp)?; + } + self.emit(Instr::IterComplete { done, result: res }, sp)?; + self.jump_if_false(done, yield_l, sp)?; + self.emit( + Instr::IterValue { + dst: val, + result: res, + }, + sp, + )?; + self.emit_exit(Exit::Return, Some(val), sp)?; + // No `return` method (step 7.c.iii): return the received value, + // awaited in an async generator. + self.bind(ret_missing)?; + if asyn { + self.emit_await(rv, rv, sp)?; + } + self.emit_exit(Exit::Return, Some(rv), sp)?; + // A throw completion (step 7.b). + self.bind(do_throw)?; + self.emit( + Instr::IterCallMethod { + dst: res, + found, + iter, + arg: rv, + which: 1, + }, + sp, + )?; + self.jump_if_false(found, throw_missing, sp)?; + if asyn { + self.emit_await(res, res, sp)?; + } + self.emit(Instr::IterComplete { done, result: res }, sp)?; + self.jump_if_false(done, yield_l, sp)?; + self.emit( + Instr::IterValue { + dst: val, + result: res, + }, + sp, + )?; + self.jump(end, sp)?; + // No `throw` method (step 7.b.iii): close the iterator, then a + // TypeError. + self.bind(throw_missing)?; + if asyn { + self.emit_async_close(iter, sp)?; + } else { + self.emit(Instr::IterClose { iter }, sp)?; + } + self.throw_error( + ErrorKind::TypeError, + "The iterator does not provide a 'throw' method", + sp, + )?; + // A normal completion (step 7.a). + self.bind(do_next)?; + self.emit( + Instr::IterCallMethod { + dst: res, + found, + iter, + arg: rv, + which: 0, + }, + sp, + )?; + if asyn { + self.emit_await(res, res, sp)?; + } + self.emit(Instr::IterComplete { done, result: res }, sp)?; + self.jump_if_false(done, yield_l, sp)?; + self.emit( + Instr::IterValue { + dst: val, + result: res, + }, + sp, + )?; + self.jump(end, sp)?; + // The inner result is not done: yield it, and handle the resumption. + self.bind(yield_l)?; + let again = self.label(); + if asyn { + // AsyncGeneratorYield(? IteratorValue(innerResult)), whose return + // resumption awaits its value (§27.6.3.8† step 9.b); a rejection of + // that await is the received completion, a throw. + self.emit( + Instr::IterValue { + dst: val, + result: res, + }, + sp, + )?; + self.emit( + Instr::Yield { + src: val, + value: rv, + mode, + }, + sp, + )?; + self.load_int(k, 2, sp)?; + self.emit( + Instr::StrictEq { + dst: c, + a: mode, + b: k, + }, + sp, + )?; + self.jump_if_false(c, again, sp)?; + let m3 = self.mark(); + let (av, am) = (self.alloc()?, self.alloc()?); + self.emit( + Instr::Await { + src: rv, + value: av, + mode: am, + }, + sp, + )?; + self.emit(Instr::Move { dst: rv, src: av }, sp)?; + self.jump_if_false(am, again, sp)?; + self.load_int(mode, 1, sp)?; + self.release(m3); + } else { + // GeneratorYield(innerResult): the result object goes out as it is. + self.emit( + Instr::YieldDelegate { + src: res, + value: rv, + mode, + }, + sp, + )?; + } + self.bind(again)?; + self.jump(top, sp)?; + self.bind(end)?; + self.emit(Instr::Move { dst: d, src: val }, sp)?; + self.release(m); + Ok(()) + } +} diff --git a/crates/clazel-js-vm/src/examples.rs b/crates/clazel-js-vm/src/examples.rs index 76010965f..b6d960304 100644 --- a/crates/clazel-js-vm/src/examples.rs +++ b/crates/clazel-js-vm/src/examples.rs @@ -510,4 +510,290 @@ pub proof fn example_global_lexical_classes() assert(op_handler(Op::InitGlobalLexical { key: 2, src: 0 }) == HandlerId::InitGlobalLexical) by (compute_only); } +/// The operands `op_wf` accepts of the ops added for regular expression literals (`NewRegExp`): +/// every register inside the frame (registers 0 to 2), every register range inside it, every +/// constant index inside the pool (0 to 2), a record constant that is a value (constant 2 is not), +/// and each small enumerated operand below its bound. +pub proof fn example_regex_wf() + ensures + op_wf(example_code(), Op::NewRegExp { dst: 0, k: 0 }), + !op_wf(example_code(), Op::NewRegExp { dst: 3, k: 0 }), + !op_wf(example_code(), Op::NewRegExp { dst: 0, k: 2 }), +{ + assert(op_wf(example_code(), Op::NewRegExp { dst: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::NewRegExp { dst: 3, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::NewRegExp { dst: 0, k: 2 })) by (compute_only); +} + +/// The class and the handler of each op added for regular expression literals: each always calls +/// out (it can allocate, call user code or throw), so each is a handler op, which the JIT lowers to +/// an exit, with its own handler. +pub proof fn example_regex_classes() + ensures + op_class(Op::NewRegExp { dst: 0, k: 0 }) is Handler, + op_handler(Op::NewRegExp { dst: 0, k: 0 }) == HandlerId::NewRegExp, +{ + assert(op_class(Op::NewRegExp { dst: 0, k: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::NewRegExp { dst: 0, k: 0 }) == HandlerId::NewRegExp) by (compute_only); +} + +/// The operands `op_wf` accepts of the ops added for generators, async functions and `for await` +/// (`GeneratorStart`, `YieldDelegate`, `GetAsyncIterator`, `IterCallMethod`, `IterComplete`, +/// `IterValue`, `ThrowIfNotObject`): every register inside the frame (registers 0 to 2), every +/// register range inside it, every constant index inside the pool (0 to 2), a record constant that +/// is a value (constant 2 is not), and each small enumerated operand below its bound. +pub proof fn example_generators_wf() + ensures + op_wf(example_code(), Op::GeneratorStart), + op_wf(example_code(), Op::YieldDelegate { src: 0, value: 0, mode: 0 }), + !op_wf(example_code(), Op::YieldDelegate { src: 3, value: 0, mode: 0 }), + !op_wf(example_code(), Op::YieldDelegate { src: 0, value: 3, mode: 0 }), + !op_wf(example_code(), Op::YieldDelegate { src: 0, value: 0, mode: 3 }), + op_wf(example_code(), Op::GetAsyncIterator { dst: 0, src: 0 }), + !op_wf(example_code(), Op::GetAsyncIterator { dst: 3, src: 0 }), + !op_wf(example_code(), Op::GetAsyncIterator { dst: 0, src: 3 }), + op_wf(example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 0 }), + !op_wf(example_code(), Op::IterCallMethod { dst: 3, found: 0, iter: 0, arg: 0, which: 0 }), + !op_wf(example_code(), Op::IterCallMethod { dst: 0, found: 3, iter: 0, arg: 0, which: 0 }), + !op_wf(example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 3, arg: 0, which: 0 }), + !op_wf(example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 3, which: 0 }), + !op_wf(example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 5 }), + op_wf(example_code(), Op::IterComplete { done: 0, result: 0 }), + !op_wf(example_code(), Op::IterComplete { done: 3, result: 0 }), + !op_wf(example_code(), Op::IterComplete { done: 0, result: 3 }), + op_wf(example_code(), Op::IterValue { dst: 0, result: 0 }), + !op_wf(example_code(), Op::IterValue { dst: 3, result: 0 }), + !op_wf(example_code(), Op::IterValue { dst: 0, result: 3 }), + op_wf(example_code(), Op::ThrowIfNotObject { src: 0 }), + !op_wf(example_code(), Op::ThrowIfNotObject { src: 3 }), +{ + assert(op_wf(example_code(), Op::GeneratorStart)) by (compute_only); + assert(op_wf(example_code(), Op::YieldDelegate { src: 0, value: 0, mode: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::YieldDelegate { src: 3, value: 0, mode: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::YieldDelegate { src: 0, value: 3, mode: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::YieldDelegate { src: 0, value: 0, mode: 3 })) by (compute_only); + assert(op_wf(example_code(), Op::GetAsyncIterator { dst: 0, src: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::GetAsyncIterator { dst: 3, src: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::GetAsyncIterator { dst: 0, src: 3 })) by (compute_only); + assert(op_wf(example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::IterCallMethod { dst: 3, found: 0, iter: 0, arg: 0, which: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::IterCallMethod { dst: 0, found: 3, iter: 0, arg: 0, which: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 3, arg: 0, which: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 3, which: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 5 })) by (compute_only); + assert(op_wf(example_code(), Op::IterComplete { done: 0, result: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::IterComplete { done: 3, result: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::IterComplete { done: 0, result: 3 })) by (compute_only); + assert(op_wf(example_code(), Op::IterValue { dst: 0, result: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::IterValue { dst: 3, result: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::IterValue { dst: 0, result: 3 })) by (compute_only); + assert(op_wf(example_code(), Op::ThrowIfNotObject { src: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::ThrowIfNotObject { src: 3 })) by (compute_only); +} + +/// The class and the handler of each op added for generators, async functions and `for await`: each +/// always calls out (it can allocate, call user code or throw), so each is a handler op, which the +/// JIT lowers to an exit, with its own handler. +pub proof fn example_generators_classes() + ensures + op_class(Op::GeneratorStart) is Handler, + op_handler(Op::GeneratorStart) == HandlerId::GeneratorStart, + op_class(Op::YieldDelegate { src: 0, value: 0, mode: 0 }) is Handler, + op_handler(Op::YieldDelegate { src: 0, value: 0, mode: 0 }) == HandlerId::YieldDelegate, + op_class(Op::GetAsyncIterator { dst: 0, src: 0 }) is Handler, + op_handler(Op::GetAsyncIterator { dst: 0, src: 0 }) == HandlerId::GetAsyncIterator, + op_class(Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 0 }) is Handler, + op_handler(Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 0 }) == HandlerId::IterCallMethod, + op_class(Op::IterComplete { done: 0, result: 0 }) is Handler, + op_handler(Op::IterComplete { done: 0, result: 0 }) == HandlerId::IterComplete, + op_class(Op::IterValue { dst: 0, result: 0 }) is Handler, + op_handler(Op::IterValue { dst: 0, result: 0 }) == HandlerId::IterValue, + op_class(Op::ThrowIfNotObject { src: 0 }) is Handler, + op_handler(Op::ThrowIfNotObject { src: 0 }) == HandlerId::ThrowIfNotObject, +{ + assert(op_class(Op::GeneratorStart) is Handler) by (compute_only); + assert(op_handler(Op::GeneratorStart) == HandlerId::GeneratorStart) by (compute_only); + assert(op_class(Op::YieldDelegate { src: 0, value: 0, mode: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::YieldDelegate { src: 0, value: 0, mode: 0 }) == HandlerId::YieldDelegate) by (compute_only); + assert(op_class(Op::GetAsyncIterator { dst: 0, src: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::GetAsyncIterator { dst: 0, src: 0 }) == HandlerId::GetAsyncIterator) by (compute_only); + assert(op_class(Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::IterCallMethod { dst: 0, found: 0, iter: 0, arg: 0, which: 0 }) == HandlerId::IterCallMethod) by (compute_only); + assert(op_class(Op::IterComplete { done: 0, result: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::IterComplete { done: 0, result: 0 }) == HandlerId::IterComplete) by (compute_only); + assert(op_class(Op::IterValue { dst: 0, result: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::IterValue { dst: 0, result: 0 }) == HandlerId::IterValue) by (compute_only); + assert(op_class(Op::ThrowIfNotObject { src: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::ThrowIfNotObject { src: 0 }) == HandlerId::ThrowIfNotObject) by (compute_only); +} + +/// The operands `op_wf` accepts of the ops added for `with`, direct `eval` and `delete` of a +/// variable (`ToObject`, `WithHas`, `WithGet`, `WithSet`, `WithDelete`, `DeleteGlobal`, +/// `DirectEval`): every register inside the frame (registers 0 to 2), every register range inside +/// it, every constant index inside the pool (0 to 2), a record constant that is a value (constant 2 +/// is not), and each small enumerated operand below its bound. +pub proof fn example_withs_wf() + ensures + op_wf(example_code(), Op::ToObject { dst: 0, src: 0 }), + !op_wf(example_code(), Op::ToObject { dst: 3, src: 0 }), + !op_wf(example_code(), Op::ToObject { dst: 0, src: 3 }), + op_wf(example_code(), Op::WithHas { dst: 0, obj: 0, key: 0 }), + !op_wf(example_code(), Op::WithHas { dst: 3, obj: 0, key: 0 }), + !op_wf(example_code(), Op::WithHas { dst: 0, obj: 3, key: 0 }), + !op_wf(example_code(), Op::WithHas { dst: 0, obj: 0, key: 3 }), + op_wf(example_code(), Op::WithGet { dst: 0, obj: 0, key: 0, strict: false }), + !op_wf(example_code(), Op::WithGet { dst: 3, obj: 0, key: 0, strict: false }), + !op_wf(example_code(), Op::WithGet { dst: 0, obj: 3, key: 0, strict: false }), + !op_wf(example_code(), Op::WithGet { dst: 0, obj: 0, key: 3, strict: false }), + op_wf(example_code(), Op::WithSet { obj: 0, key: 0, val: 0, strict: false }), + !op_wf(example_code(), Op::WithSet { obj: 3, key: 0, val: 0, strict: false }), + !op_wf(example_code(), Op::WithSet { obj: 0, key: 3, val: 0, strict: false }), + !op_wf(example_code(), Op::WithSet { obj: 0, key: 0, val: 3, strict: false }), + op_wf(example_code(), Op::WithDelete { dst: 0, obj: 0, key: 0 }), + !op_wf(example_code(), Op::WithDelete { dst: 3, obj: 0, key: 0 }), + !op_wf(example_code(), Op::WithDelete { dst: 0, obj: 3, key: 0 }), + !op_wf(example_code(), Op::WithDelete { dst: 0, obj: 0, key: 3 }), + op_wf(example_code(), Op::DeleteGlobal { dst: 0, key: 0 }), + !op_wf(example_code(), Op::DeleteGlobal { dst: 3, key: 0 }), + !op_wf(example_code(), Op::DeleteGlobal { dst: 0, key: 3 }), + op_wf(example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 }), + !op_wf(example_code(), Op::DirectEval { dst: 3, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 }), + !op_wf(example_code(), Op::DirectEval { dst: 0, callee: 3, this: 0, args: 0, argc: 3, env: 0, k: 0 }), + !op_wf(example_code(), Op::DirectEval { dst: 0, callee: 0, this: 3, args: 0, argc: 3, env: 0, k: 0 }), + !op_wf(example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 2, argc: 2, env: 0, k: 0 }), + !op_wf(example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 3, k: 0 }), + !op_wf(example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 2 }), +{ + assert(op_wf(example_code(), Op::ToObject { dst: 0, src: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::ToObject { dst: 3, src: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::ToObject { dst: 0, src: 3 })) by (compute_only); + assert(op_wf(example_code(), Op::WithHas { dst: 0, obj: 0, key: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::WithHas { dst: 3, obj: 0, key: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::WithHas { dst: 0, obj: 3, key: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::WithHas { dst: 0, obj: 0, key: 3 })) by (compute_only); + assert(op_wf(example_code(), Op::WithGet { dst: 0, obj: 0, key: 0, strict: false })) by (compute_only); + assert(!op_wf(example_code(), Op::WithGet { dst: 3, obj: 0, key: 0, strict: false })) by (compute_only); + assert(!op_wf(example_code(), Op::WithGet { dst: 0, obj: 3, key: 0, strict: false })) by (compute_only); + assert(!op_wf(example_code(), Op::WithGet { dst: 0, obj: 0, key: 3, strict: false })) by (compute_only); + assert(op_wf(example_code(), Op::WithSet { obj: 0, key: 0, val: 0, strict: false })) by (compute_only); + assert(!op_wf(example_code(), Op::WithSet { obj: 3, key: 0, val: 0, strict: false })) by (compute_only); + assert(!op_wf(example_code(), Op::WithSet { obj: 0, key: 3, val: 0, strict: false })) by (compute_only); + assert(!op_wf(example_code(), Op::WithSet { obj: 0, key: 0, val: 3, strict: false })) by (compute_only); + assert(op_wf(example_code(), Op::WithDelete { dst: 0, obj: 0, key: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::WithDelete { dst: 3, obj: 0, key: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::WithDelete { dst: 0, obj: 3, key: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::WithDelete { dst: 0, obj: 0, key: 3 })) by (compute_only); + assert(op_wf(example_code(), Op::DeleteGlobal { dst: 0, key: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DeleteGlobal { dst: 3, key: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DeleteGlobal { dst: 0, key: 3 })) by (compute_only); + assert(op_wf(example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEval { dst: 3, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEval { dst: 0, callee: 3, this: 0, args: 0, argc: 3, env: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEval { dst: 0, callee: 0, this: 3, args: 0, argc: 3, env: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 2, argc: 2, env: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 3, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 2 })) by (compute_only); +} + +/// The class and the handler of each op added for `with`, direct `eval` and `delete` of a variable: +/// each always calls out (it can allocate, call user code or throw), so each is a handler op, which +/// the JIT lowers to an exit, with its own handler. +pub proof fn example_withs_classes() + ensures + op_class(Op::ToObject { dst: 0, src: 0 }) is Handler, + op_handler(Op::ToObject { dst: 0, src: 0 }) == HandlerId::ToObject, + op_class(Op::WithHas { dst: 0, obj: 0, key: 0 }) is Handler, + op_handler(Op::WithHas { dst: 0, obj: 0, key: 0 }) == HandlerId::WithHas, + op_class(Op::WithGet { dst: 0, obj: 0, key: 0, strict: false }) is Handler, + op_handler(Op::WithGet { dst: 0, obj: 0, key: 0, strict: false }) == HandlerId::WithGet, + op_class(Op::WithSet { obj: 0, key: 0, val: 0, strict: false }) is Handler, + op_handler(Op::WithSet { obj: 0, key: 0, val: 0, strict: false }) == HandlerId::WithSet, + op_class(Op::WithDelete { dst: 0, obj: 0, key: 0 }) is Handler, + op_handler(Op::WithDelete { dst: 0, obj: 0, key: 0 }) == HandlerId::WithDelete, + op_class(Op::DeleteGlobal { dst: 0, key: 0 }) is Handler, + op_handler(Op::DeleteGlobal { dst: 0, key: 0 }) == HandlerId::DeleteGlobal, + op_class(Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 }) is Handler, + op_handler(Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 }) == HandlerId::DirectEval, +{ + assert(op_class(Op::ToObject { dst: 0, src: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::ToObject { dst: 0, src: 0 }) == HandlerId::ToObject) by (compute_only); + assert(op_class(Op::WithHas { dst: 0, obj: 0, key: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::WithHas { dst: 0, obj: 0, key: 0 }) == HandlerId::WithHas) by (compute_only); + assert(op_class(Op::WithGet { dst: 0, obj: 0, key: 0, strict: false }) is Handler) by (compute_only); + assert(op_handler(Op::WithGet { dst: 0, obj: 0, key: 0, strict: false }) == HandlerId::WithGet) by (compute_only); + assert(op_class(Op::WithSet { obj: 0, key: 0, val: 0, strict: false }) is Handler) by (compute_only); + assert(op_handler(Op::WithSet { obj: 0, key: 0, val: 0, strict: false }) == HandlerId::WithSet) by (compute_only); + assert(op_class(Op::WithDelete { dst: 0, obj: 0, key: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::WithDelete { dst: 0, obj: 0, key: 0 }) == HandlerId::WithDelete) by (compute_only); + assert(op_class(Op::DeleteGlobal { dst: 0, key: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::DeleteGlobal { dst: 0, key: 0 }) == HandlerId::DeleteGlobal) by (compute_only); + assert(op_class(Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::DirectEval { dst: 0, callee: 0, this: 0, args: 0, argc: 3, env: 0, k: 0 }) == HandlerId::DirectEval) by (compute_only); +} + +/// The operands `op_wf` accepts of the ops added for modules and `import()` (`GetImportBinding`, +/// `ImportCall`, `ImportMeta`) and for direct `eval` with a spread argument (`DirectEvalSpread`): +/// every register inside the frame (registers 0 to 2), every constant index inside the pool (0 to +/// 2), and a record constant that is a value (constant 2 is not). +pub proof fn example_imports_wf() + ensures + op_wf(example_code(), Op::GetImportBinding { dst: 0, env: 0, idx: 0 }), + !op_wf(example_code(), Op::GetImportBinding { dst: 3, env: 0, idx: 0 }), + !op_wf(example_code(), Op::GetImportBinding { dst: 0, env: 3, idx: 0 }), + op_wf(example_code(), Op::ImportCall { dst: 0, specifier: 0, options: 0 }), + !op_wf(example_code(), Op::ImportCall { dst: 3, specifier: 0, options: 0 }), + !op_wf(example_code(), Op::ImportCall { dst: 0, specifier: 3, options: 0 }), + !op_wf(example_code(), Op::ImportCall { dst: 0, specifier: 0, options: 3 }), + op_wf(example_code(), Op::ImportMeta { dst: 0 }), + !op_wf(example_code(), Op::ImportMeta { dst: 3 }), + op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 0 }), + !op_wf(example_code(), Op::DirectEvalSpread { dst: 3, callee: 0, this: 0, args: 0, env: 0, k: 0 }), + !op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 3, this: 0, args: 0, env: 0, k: 0 }), + !op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 3, args: 0, env: 0, k: 0 }), + !op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 3, env: 0, k: 0 }), + !op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 3, k: 0 }), + !op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 2 }), +{ + assert(op_wf(example_code(), Op::GetImportBinding { dst: 0, env: 0, idx: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::GetImportBinding { dst: 3, env: 0, idx: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::GetImportBinding { dst: 0, env: 3, idx: 0 })) by (compute_only); + assert(op_wf(example_code(), Op::ImportCall { dst: 0, specifier: 0, options: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::ImportCall { dst: 3, specifier: 0, options: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::ImportCall { dst: 0, specifier: 3, options: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::ImportCall { dst: 0, specifier: 0, options: 3 })) by (compute_only); + assert(op_wf(example_code(), Op::ImportMeta { dst: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::ImportMeta { dst: 3 })) by (compute_only); + assert(op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEvalSpread { dst: 3, callee: 0, this: 0, args: 0, env: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 3, this: 0, args: 0, env: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 3, args: 0, env: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 3, env: 0, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 3, k: 0 })) by (compute_only); + assert(!op_wf(example_code(), Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 2 })) by (compute_only); +} + +/// The class and the handler of each op added for modules and `import()`, and for direct `eval` +/// with a spread argument: each always calls out (it can allocate, call user code, call the host +/// or throw), so each is a handler op, which the JIT lowers to an exit, with its own handler. +pub proof fn example_imports_classes() + ensures + op_class(Op::GetImportBinding { dst: 0, env: 0, idx: 0 }) is Handler, + op_handler(Op::GetImportBinding { dst: 0, env: 0, idx: 0 }) == HandlerId::GetImportBinding, + op_class(Op::ImportCall { dst: 0, specifier: 0, options: 0 }) is Handler, + op_handler(Op::ImportCall { dst: 0, specifier: 0, options: 0 }) == HandlerId::ImportCall, + op_class(Op::ImportMeta { dst: 0 }) is Handler, + op_handler(Op::ImportMeta { dst: 0 }) == HandlerId::ImportMeta, + op_class(Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 0 }) is Handler, + op_handler(Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 0 }) == HandlerId::DirectEvalSpread, +{ + assert(op_class(Op::GetImportBinding { dst: 0, env: 0, idx: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::GetImportBinding { dst: 0, env: 0, idx: 0 }) == HandlerId::GetImportBinding) by (compute_only); + assert(op_class(Op::ImportCall { dst: 0, specifier: 0, options: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::ImportCall { dst: 0, specifier: 0, options: 0 }) == HandlerId::ImportCall) by (compute_only); + assert(op_class(Op::ImportMeta { dst: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::ImportMeta { dst: 0 }) == HandlerId::ImportMeta) by (compute_only); + assert(op_class(Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 0 }) is Handler) by (compute_only); + assert(op_handler(Op::DirectEvalSpread { dst: 0, callee: 0, this: 0, args: 0, env: 0, k: 0 }) == HandlerId::DirectEvalSpread) by (compute_only); +} + } // verus! diff --git a/crates/clazel-js-vm/src/frame.rs b/crates/clazel-js-vm/src/frame.rs new file mode 100644 index 000000000..17a62ab68 --- /dev/null +++ b/crates/clazel-js-vm/src/frame.rs @@ -0,0 +1,296 @@ +//! The frame region: the value stack of phase-7.md's "Shapes fixed here for +//! the JIT" (item 7.8, first slice). +//! +//! Each frame is a window of [`Value`] slots in one [`Region`]: four header +//! slots (callee, `this`, the argument count with the frame kind, the return +//! address), then `nparams` argument registers, then `nregs` local registers. +//! The layout is `spec::hdr_*`, `spec::reg_slot`, `spec::enc_argc` and +//! `spec::pack_ret` of [`crate::spec`], written as executable code; the +//! interpreter and (later) the JIT read and write the same slots. Every access +//! is checked: an index past the region is an `Err`, never a panic. + +use clazel_js_heap::value::{Decoded, Value}; + +/// `spec::hdr_slots`: the header slots of a frame. +pub const HDR_SLOTS: usize = 4; +/// `spec::hdr_callee`. +pub const HDR_CALLEE: usize = 0; +/// `spec::hdr_this`. +pub const HDR_THIS: usize = 1; +/// `spec::hdr_argc`. +pub const HDR_ARGC: usize = 2; +/// `spec::hdr_ret`. +pub const HDR_RET: usize = 3; + +/// `spec::kind_plain`: a plain call's frame. +pub const KIND_PLAIN: u32 = 0; +/// `spec::kind_construct`: a construct call's frame. +pub const KIND_CONSTRUCT: u32 = 1; +/// `spec::kind_suspendable`: a generator or async frame. +pub const KIND_SUSPENDABLE: u32 = 2; + +/// `spec::ret_limit`: a return address packs a frame base and a `pc`, each +/// below this. +pub const RET_LIMIT: usize = 0xFF_FFFF; +/// `spec::RET_NONE`: the return-address payload of a frame with no caller in +/// the region (the entry of a [`crate::Vm::call`]). +pub const RET_NONE: u64 = 0xFFFF_FFFF_FFFF; + +/// One argument count packs with the kind as `argc + kind * 2^28` +/// (`spec::enc_argc`). +const KIND_UNIT: i64 = 268_435_456; + +/// The frame region could not be indexed or grown. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FrameError { + /// A slot index is outside the region. + OutOfRegion, + /// The region would pass its limit: the stack overflowed. + Overflow, +} + +/// The slot of register `r` of the frame at `fp` (`spec::reg_slot`), or +/// `None` if the sum does not fit. +#[must_use] +pub fn reg_slot(fp: usize, r: u32) -> Option { + fp.checked_add(HDR_SLOTS)? + .checked_add(usize::try_from(r).ok()?) +} + +/// The number of slots of a frame of a function with `nparams` parameters and +/// `nregs` registers (`spec::frame_size`). +#[must_use] +pub fn frame_size(nparams: u32, nregs: u32) -> Option { + let n = usize::try_from(nparams) + .ok()? + .checked_add(usize::try_from(nregs).ok()?)?; + n.checked_add(HDR_SLOTS) +} + +/// `spec::enc_argc`: the int32 value of the argument-count slot. +#[must_use] +pub fn enc_argc(argc: u32, kind: u32) -> Option { + let n = i64::from(argc).checked_add(i64::from(kind).checked_mul(KIND_UNIT)?)?; + i32::try_from(n).ok().map(Value::int32) +} + +/// `spec::argc_of` and `spec::kind_of`: the argument count and the frame kind +/// of an argument-count slot. +#[must_use] +pub fn dec_argc(v: Value) -> Option<(u32, u32)> { + match v.decode() { + Decoded::Int32(i) if i >= 0 => { + let n = i64::from(i); + let argc = u32::try_from(n.checked_rem(KIND_UNIT)?).ok()?; + let kind = u32::try_from(n.checked_div(KIND_UNIT)?).ok()?; + Some((argc, kind)) + } + _ => None, + } +} + +/// `spec::pack_ret`: the return-address value for a caller with frame base +/// `base` at `pc`; `None` if either is not below [`RET_LIMIT`]. +#[must_use] +pub fn pack_ret(base: usize, pc: usize) -> Option { + if base >= RET_LIMIT || pc >= RET_LIMIT { + return None; + } + let payload = u64::try_from(base).ok()?.checked_shl(24)? | u64::try_from(pc).ok()?; + Value::raw(payload) +} + +/// What a return-address slot holds. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Ret { + /// The frame has no caller in the region. + None, + /// The caller's frame base and the `pc` of its call op. + Caller { + /// The caller's frame base. + base: usize, + /// The caller's `pc`. + pc: usize, + }, +} + +/// Reads a return-address slot (the inverse of [`pack_ret`]). +#[must_use] +pub fn unpack_ret(v: Value) -> Option { + match v.decode() { + Decoded::Raw(RET_NONE) => Some(Ret::None), + Decoded::Raw(p) => Some(Ret::Caller { + base: usize::try_from(p >> 24).ok()?, + pc: usize::try_from(p & 0xFF_FFFF).ok()?, + }), + _ => None, + } +} + +/// The value-stack region. +#[derive(Debug)] +pub struct Region { + slots: Vec, + limit: usize, +} + +impl Region { + /// An empty region that may grow to `limit` slots (at most + /// [`RET_LIMIT`] - 1, so every frame base fits a return address). + #[must_use] + pub fn new(limit: usize) -> Self { + Self { + slots: Vec::new(), + limit: limit.min(RET_LIMIT.saturating_sub(1)), + } + } + + /// The number of slots the region holds. + #[must_use] + pub fn len(&self) -> usize { + self.slots.len() + } + + /// Whether the region holds no slots. + #[must_use] + pub fn is_empty(&self) -> bool { + self.slots.is_empty() + } + + /// The slot limit. + #[must_use] + pub const fn limit(&self) -> usize { + self.limit + } + + /// Makes the region hold at least `n` slots, new ones `undefined`. + /// + /// # Errors + /// [`FrameError::Overflow`] if `n` passes the limit. + pub fn ensure(&mut self, n: usize) -> Result<(), FrameError> { + if n > self.limit { + return Err(FrameError::Overflow); + } + if n > self.slots.len() { + self.slots.resize(n, Value::UNDEFINED); + } + Ok(()) + } + + /// The slot at `i`. + /// + /// # Errors + /// [`FrameError::OutOfRegion`] if `i` is past the region. + pub fn get(&self, i: usize) -> Result { + self.slots.get(i).copied().ok_or(FrameError::OutOfRegion) + } + + /// Sets the slot at `i`. + /// + /// # Errors + /// [`FrameError::OutOfRegion`] if `i` is past the region. + pub fn set(&mut self, i: usize, v: Value) -> Result<(), FrameError> { + match self.slots.get_mut(i) { + Some(s) => { + *s = v; + Ok(()) + } + None => Err(FrameError::OutOfRegion), + } + } + + /// Register `r` of the frame at `fp`. + /// + /// # Errors + /// [`FrameError::OutOfRegion`] if the slot is past the region. + pub fn reg(&self, fp: usize, r: u32) -> Result { + self.get(reg_slot(fp, r).ok_or(FrameError::OutOfRegion)?) + } + + /// Sets register `r` of the frame at `fp`. + /// + /// # Errors + /// [`FrameError::OutOfRegion`] if the slot is past the region. + pub fn set_reg(&mut self, fp: usize, r: u32, v: Value) -> Result<(), FrameError> { + self.set(reg_slot(fp, r).ok_or(FrameError::OutOfRegion)?, v) + } + + /// A header slot of the frame at `fp`. + /// + /// # Errors + /// [`FrameError::OutOfRegion`] if the slot is past the region. + pub fn header(&self, fp: usize, which: usize) -> Result { + self.get(fp.checked_add(which).ok_or(FrameError::OutOfRegion)?) + } + + /// Writes a frame's header and fills the rest of it (`size` slots in all) + /// with `undefined`: `spec::pushed_region`. + /// + /// # Errors + /// [`FrameError::Overflow`] if the frame passes the limit. + pub fn push_frame( + &mut self, + base: usize, + size: usize, + header: [Value; HDR_SLOTS], + ) -> Result<(), FrameError> { + let end = base.checked_add(size).ok_or(FrameError::Overflow)?; + self.ensure(end)?; + let window = self + .slots + .get_mut(base..end) + .ok_or(FrameError::OutOfRegion)?; + for s in window.iter_mut() { + *s = Value::UNDEFINED; + } + for (i, h) in header.into_iter().enumerate() { + if let Some(s) = window.get_mut(i) { + *s = h; + } + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn argc_and_kind_round_trip() { + for argc in [0u32, 1, 7, 268_435_455] { + for kind in [KIND_PLAIN, KIND_CONSTRUCT, KIND_SUSPENDABLE] { + let v = enc_argc(argc, kind); + assert_eq!(v.and_then(dec_argc), Some((argc, kind))); + } + } + assert_eq!(enc_argc(0, 8), None); + } + + #[test] + fn return_addresses_round_trip() { + let v = pack_ret(12, 345); + assert_eq!( + v.and_then(unpack_ret), + Some(Ret::Caller { base: 12, pc: 345 }) + ); + assert_eq!(pack_ret(RET_LIMIT, 0), None); + assert_eq!(pack_ret(0, RET_LIMIT), None); + assert_eq!(Value::raw(RET_NONE).and_then(unpack_ret), Some(Ret::None)); + } + + #[test] + fn a_frame_is_pushed_and_bounded() { + let mut r = Region::new(10); + let hdr = [Value::NULL, Value::TRUE, Value::int32(0), Value::UNDEFINED]; + assert_eq!(r.push_frame(2, 6, hdr), Ok(())); + assert_eq!(r.header(2, HDR_THIS), Ok(Value::TRUE)); + assert_eq!(r.reg(2, 0), Ok(Value::UNDEFINED)); + assert_eq!(r.set_reg(2, 1, Value::int32(5)), Ok(())); + assert_eq!(r.reg(2, 1), Ok(Value::int32(5))); + assert_eq!(r.get(8), Err(FrameError::OutOfRegion)); + assert_eq!(r.push_frame(8, 6, hdr), Err(FrameError::Overflow)); + assert_eq!(r.push_frame(usize::MAX, 6, hdr), Err(FrameError::Overflow)); + assert_eq!(reg_slot(usize::MAX, 0), None); + } +} diff --git a/crates/clazel-js-vm/src/handlers.rs b/crates/clazel-js-vm/src/handlers.rs new file mode 100644 index 000000000..77ed063d9 --- /dev/null +++ b/crates/clazel-js-vm/src/handlers.rs @@ -0,0 +1,1262 @@ +//! The handlers of item 7.8's first slice: plain methods over the frame +//! region and the heap, which the interpreter loop ([`crate::interp`]) calls +//! for every op that can allocate, call out or throw, and which jit.md's +//! exits will call too. +//! +//! The handlers here cover: the slow paths of the arithmetic, comparison, +//! equality and truthiness ops on primitives; string concatenation with `+` +//! and Number to String through `clazel-js-number`; `typeof`; property get +//! and set on ordinary objects with inline caches; globals (the global object +//! and the global declarative record, `DeclareGlobals`); closures; and the +//! creation of the errors the VM throws. ToPrimitive and OrdinaryToPrimitive +//! (§7.1.1) call `valueOf` and `toString`, so an object converts only as +//! far as its methods exist: `clazel-js-builtins` (item 7.9) installs them, +//! and without it an object has none and the conversion is a `TypeError`. +//! The natives' own needs (a native constructor's `NewTarget`, the +//! intrinsics table, bound functions, wrapper objects) are here too. +//! +//! Each handler's contract is the clause of ECMA-262 it cites; the evidence is +//! the Test262 run in `tests/test262_run.rs`. + +use crate::bytecode::{Const, ErrorKind, FunctionKind, VarScoped}; +use crate::heap::{Cell, ObjKind}; +use crate::interp::{Abrupt, ArithOp, Hint, Intrinsic, Rel, Vm}; +use crate::numeric::{self, Num, num}; +use clazel_js_heap::shape::Attrs; +use clazel_js_heap::string::{JsString, Key}; +use clazel_js_heap::value::{Decoded, Value}; + +/// A property made by a function declaration or a `var` of the global +/// environment: writable and enumerable, not configurable (§9.1.1.4.17† +/// CreateGlobalVarBinding with `D` false). +const GLOBAL_VAR: Attrs = Attrs(Attrs::WRITABLE | Attrs::ENUMERABLE); +/// Attributes of `length` and `name` of a function (§10.2.9†, §10.2.10†). +const CONFIGURABLE_ONLY: Attrs = Attrs(Attrs::CONFIGURABLE); +/// Attributes of the `name` and `message` of an error the VM throws. +const WRITABLE_CONFIGURABLE: Attrs = Attrs(Attrs::WRITABLE | Attrs::CONFIGURABLE); + +impl Vm { + // ----------------------------------------------------------------- + // Errors + // ----------------------------------------------------------------- + + /// The abrupt completion of throwing a new error of `kind`: an ordinary + /// object with `name` and `message` (the error constructors are + /// built-ins, 7.9). + pub(crate) fn throw_kind(&mut self, kind: ErrorKind, msg: &str) -> Abrupt { + let name = match kind { + ErrorKind::TypeError => "TypeError", + ErrorKind::ReferenceError => "ReferenceError", + ErrorKind::SyntaxError => "SyntaxError", + ErrorKind::RangeError => "RangeError", + }; + match self.make_error(name, msg) { + Ok(v) => Abrupt::Throw(v), + Err(e) => e, + } + } + + /// A new error object. When the realm has the error constructors + /// (`clazel-js-builtins` installed them), it is what `new TypeError(msg)` + /// makes (§20.5.6.1.1† steps 2 to 4): `[[ErrorData]]`, the native error's + /// prototype and an own non-enumerable `message`. Without them it is an + /// ordinary object with own `name` and `message`. + fn make_error(&mut self, name: &str, msg: &str) -> Result { + let which = match name { + "TypeError" => Intrinsic::TypeErrorPrototype, + "RangeError" => Intrinsic::RangeErrorPrototype, + "SyntaxError" => Intrinsic::SyntaxErrorPrototype, + _ => Intrinsic::ReferenceErrorPrototype, + }; + let proto = self.intrinsic(which); + let msg_v = self.heap.new_str(msg)?; + let (kn, km) = (self.atoms.k_name, self.atoms.k_message); + if self.heap.is_object(proto) { + let o = self.heap.new_object(proto, ObjKind::Error)?; + self.heap.define(o, km, msg_v, WRITABLE_CONFIGURABLE)?; + return Ok(o); + } + let proto = self.atoms.object_proto; + let o = self.heap.new_object(proto, ObjKind::Plain)?; + let name_v = self.heap.new_str(name)?; + self.heap.define(o, kn, name_v, WRITABLE_CONFIGURABLE)?; + self.heap.define(o, km, msg_v, WRITABLE_CONFIGURABLE)?; + Ok(o) + } + + /// A thrown `TypeError`. (Also for natives: `Err(vm.type_error(..))`.) + pub fn type_error(&mut self, msg: &str) -> Abrupt { + self.throw_kind(ErrorKind::TypeError, msg) + } + + /// A thrown `ReferenceError`. + pub fn reference_error(&mut self, msg: &str) -> Abrupt { + self.throw_kind(ErrorKind::ReferenceError, msg) + } + + /// A thrown `RangeError`. + pub fn range_error(&mut self, msg: &str) -> Abrupt { + self.throw_kind(ErrorKind::RangeError, msg) + } + + /// A thrown `SyntaxError`. + pub fn syntax_error(&mut self, msg: &str) -> Abrupt { + self.throw_kind(ErrorKind::SyntaxError, msg) + } + + // ----------------------------------------------------------------- + // Type conversions (§7.1), on primitives + // ----------------------------------------------------------------- + + /// ToBoolean, §7.1.2. + #[must_use] + pub fn to_boolean(&self, v: Value) -> bool { + match v.decode() { + Decoded::Undefined | Decoded::Null | Decoded::Hole | Decoded::Raw(_) => false, + Decoded::Bool(b) => b, + Decoded::Int32(i) => i != 0, + Decoded::Double(d) => !(d == 0.0 || d.is_nan()), + Decoded::Ref(_) => self.heap.string(v).is_none_or(|s| !s.is_empty()), + } + } + + /// ToPrimitive, §7.1.1†, and OrdinaryToPrimitive, §7.1.1.1†. A primitive + /// is its own result (step 2). For an object, `@@toPrimitive` is not + /// consulted (Symbols are a later slice); the hint `string` tries + /// `toString` then `valueOf`, any other hint the reverse; the first + /// callable that returns a non-object wins, and none is a TypeError. + /// + /// # Errors + /// The exceptions of the methods it calls, or a `TypeError`. + pub fn to_primitive(&mut self, v: Value, hint: Hint) -> Result { + if !self.heap.is_object(v) { + return Ok(v); + } + let names = if hint == Hint::String { + ["toString", "valueOf"] + } else { + ["valueOf", "toString"] + }; + for name in names { + // Step 2.a: Get(O, name); step 2.b: IsCallable(method). + let method = self.get_named(v, name)?; + if self.heap.is_callable(method) { + let result = self.call(method, v, &[])?; + if !self.heap.is_object(result) { + return Ok(result); + } + } + } + Err(self.type_error("Cannot convert object to primitive value")) + } + + /// ToObject, §7.1.18†: `undefined` and `null` are a TypeError, a + /// Boolean, Number or String gets its wrapper object (with the + /// intrinsic prototype, or `%Object.prototype%` when the realm has no + /// wrappers), an object is itself. + /// + /// # Errors + /// A `TypeError` for `undefined` and `null`. + pub fn to_object(&mut self, v: Value) -> Result { + let which = match v.decode() { + Decoded::Undefined | Decoded::Null => { + return Err(self.type_error("Cannot convert undefined or null to object")); + } + Decoded::Bool(_) => Intrinsic::BooleanPrototype, + Decoded::Int32(_) | Decoded::Double(_) => Intrinsic::NumberPrototype, + Decoded::Ref(_) if self.heap.is_string(v) => Intrinsic::StringPrototype, + Decoded::Ref(_) => return Ok(v), + Decoded::Hole | Decoded::Raw(_) => { + return Err(Abrupt::Internal("an internal value reached ToObject")); + } + }; + let proto = self.intrinsic(which); + let proto = if self.heap.is_object(proto) { + proto + } else { + self.atoms.object_proto + }; + self.new_wrapper(v, proto) + } + + /// A wrapper object for a primitive with the given prototype: for a + /// String, with the own `length` that §10.4.3.5† StringCreate step 6 + /// defines (non-writable, non-enumerable, non-configurable). + /// + /// # Errors + /// [`Abrupt::OutOfMemory`]. + pub fn new_wrapper(&mut self, prim: Value, proto: Value) -> Result { + let o = self.heap.new_object(proto, ObjKind::Primitive(prim))?; + if let Some(s) = self.heap.string(prim) { + let n = i32::try_from(s.len()).map_err(|_| Abrupt::OutOfMemory)?; + let kl = self.atoms.k_length; + self.heap.define(o, kl, Value::int32(n), Attrs(0))?; + } + Ok(o) + } + + /// ToNumber as an `f64`, §7.1.4†. + /// + /// # Errors + /// The exceptions of ToPrimitive. + pub fn to_number(&mut self, v: Value) -> Result { + Ok(self.coerce_number(v)?.to_f64()) + } + + /// ToString, §7.1.17†. + /// + /// # Errors + /// The exceptions of ToPrimitive. + pub fn to_js_string(&mut self, v: Value) -> Result { + self.coerce_js_string(v) + } + + /// ToNumber, §7.1.4†: a primitive directly, an object through + /// ToPrimitive with the hint `number`. + pub(crate) fn coerce_number(&mut self, v: Value) -> Result { + match v.decode() { + Decoded::Int32(i) => Ok(Num::I(i)), + Decoded::Double(d) => Ok(Num::D(d)), + Decoded::Undefined => Ok(Num::D(f64::NAN)), + Decoded::Null => Ok(Num::I(0)), + Decoded::Bool(b) => Ok(Num::I(i32::from(b))), + Decoded::Ref(_) => match self.heap.string(v) { + Some(s) => Ok(numeric::narrow(clazel_js_number::string_to_number( + &s.to_string_lossy(), + ))), + None => { + let p = self.to_primitive(v, Hint::Number)?; + self.coerce_number(p) + } + }, + Decoded::Hole | Decoded::Raw(_) => { + Err(Abrupt::Internal("an internal value reached ToNumber")) + } + } + } + + /// The Number in register `r`, converted if need be. + pub(crate) fn number_in_reg(&mut self, r: u32) -> Result { + let v = self.r(r)?; + match num(v) { + Some(n) => Ok(n), + None => self.coerce_number(v), + } + } + + /// ToString, §7.1.17†. + pub fn coerce_js_string(&mut self, v: Value) -> Result { + let text = match v.decode() { + Decoded::Undefined => "undefined".to_string(), + Decoded::Null => "null".to_string(), + Decoded::Bool(b) => if b { "true" } else { "false" }.to_string(), + Decoded::Int32(i) => i.to_string(), + Decoded::Double(d) => clazel_js_number::number_to_string(d), + Decoded::Ref(_) => { + return match self.heap.string(v) { + Some(s) => Ok(s.clone()), + None => { + let p = self.to_primitive(v, Hint::String)?; + self.coerce_js_string(p) + } + }; + } + Decoded::Hole | Decoded::Raw(_) => { + return Err(Abrupt::Internal("an internal value reached ToString")); + } + }; + JsString::from_rust_str(&text).map_err(|_| Abrupt::OutOfMemory) + } + + /// ToString as a value (the string itself when it is one). + pub(crate) fn coerce_string_value(&mut self, v: Value) -> Result { + if self.heap.is_string(v) { + return Ok(v); + } + let s = self.coerce_js_string(v)?; + Ok(self.heap.new_string(s)?) + } + + /// ToPropertyKey, §7.1.19†: ToPrimitive with the hint `string`, then + /// ToString (no Symbols yet). + pub fn coerce_property_key(&mut self, v: Value) -> Result { + let s = self.coerce_js_string(v)?; + Ok(self.heap.intern(&s)?) + } + + // ----------------------------------------------------------------- + // Operators + // ----------------------------------------------------------------- + + /// `+` off the Number path, §13.15.3 ApplyStringOrNumericBinaryOperator: + /// a string on either side concatenates the two ToStrings. + pub(crate) fn add_slow(&mut self, x: Value, y: Value) -> Result { + if self.heap.is_object(x) || self.heap.is_object(y) { + // Steps 1.a and 1.b of §13.15.3: ToPrimitive of the left then the + // right operand, with the hint `default`. + let p = self.to_primitive(x, Hint::Default)?; + let q = self.to_primitive(y, Hint::Default)?; + return self.add_slow(p, q); + } + if self.heap.is_string(x) || self.heap.is_string(y) { + let (a, b) = (self.coerce_js_string(x)?, self.coerce_js_string(y)?); + return match a.concat(&b) { + Ok(s) => Ok(self.heap.new_string(s)?), + Err(_) => Err(self.range_error("Invalid string length")), + }; + } + let (p, q) = (self.coerce_number(x)?, self.coerce_number(y)?); + Ok(numeric::add(p, q)) + } + + /// The other binary arithmetic operators off the Number path: ToNumeric + /// on both operands, then the Number operation. + pub(crate) fn arith_slow(&mut self, op: ArithOp, x: Value, y: Value) -> Result { + let p = self.coerce_number(x)?; + let q = self.coerce_number(y)?; + Ok(op.apply(p, q)) + } + + /// `<`, `<=`, `>`, `>=` off the Number path: IsLessThan, §7.2.12. + pub(crate) fn relational(&mut self, op: Rel, x: Value, y: Value) -> Result { + if self.heap.is_object(x) || self.heap.is_object(y) { + // Steps 1 and 2 of IsLessThan: ToPrimitive with the hint `number`, + // the left operand (as written in the source) first. + let p = self.to_primitive(x, Hint::Number)?; + let q = self.to_primitive(y, Hint::Number)?; + return self.relational(op, p, q); + } + if let (Some(a), Some(b)) = (self.heap.string(x), self.heap.string(y)) { + // Step 3: both Strings: code unit order. + let ord = a.compare(b); + return Ok(match op { + Rel::Lt => ord.is_lt(), + Rel::Le => ord.is_le(), + Rel::Gt => ord.is_gt(), + Rel::Ge => ord.is_ge(), + }); + } + let (p, q) = (self.coerce_number(x)?, self.coerce_number(y)?); + Ok(match op { + Rel::Lt => numeric::lt(p, q), + Rel::Le => numeric::le(p, q), + Rel::Gt => numeric::gt(p, q), + Rel::Ge => numeric::ge(p, q), + }) + } + + /// IsStrictlyEqual, §7.2.14. + pub fn strict_equals(&self, x: Value, y: Value) -> bool { + if let (Some(a), Some(b)) = (num(x), num(y)) { + return numeric::equal(a, b); + } + if let (Some(a), Some(b)) = (self.heap.string(x), self.heap.string(y)) { + return a == b; + } + x == y + } + + /// IsLooselyEqual, §7.2.13, for primitives; an object against a + /// primitive needs ToPrimitive. + pub(crate) fn loose_equals(&mut self, x: Value, y: Value) -> Result { + let nullish = |v: Value| v == Value::UNDEFINED || v == Value::NULL; + let (ox, oy) = (self.heap.is_object(x), self.heap.is_object(y)); + if ox && oy { + return Ok(x == y); // Step 1.a, SameType: IsStrictlyEqual. + } + if nullish(x) || nullish(y) { + return Ok(nullish(x) && nullish(y)); // Steps 2 to 4. + } + if ox || oy { + // Steps 12 and 13: an Object against a String, Number or Boolean + // compares its ToPrimitive (hint `default`). + let (p, q) = if ox { + (self.to_primitive(x, Hint::Default)?, y) + } else { + (x, self.to_primitive(y, Hint::Default)?) + }; + return self.loose_equals(p, q); + } + let (sx, sy) = (self.heap.is_string(x), self.heap.is_string(y)); + if sx && sy { + return Ok(self.strict_equals(x, y)); + } + // Steps 6 to 12 on primitives: Booleans, Strings and Numbers all + // meet at Numbers. + let (p, q) = (self.coerce_number(x)?, self.coerce_number(y)?); + Ok(numeric::equal(p, q)) + } + + /// `typeof`, §13.5.3.1. + pub(crate) fn type_of(&self, v: Value) -> Value { + match v.decode() { + Decoded::Undefined | Decoded::Hole | Decoded::Raw(_) => self.atoms.undefined, + Decoded::Null => self.atoms.object, + Decoded::Bool(_) => self.atoms.boolean, + Decoded::Int32(_) | Decoded::Double(_) => self.atoms.number, + Decoded::Ref(_) => { + if self.heap.is_string(v) { + self.atoms.string + } else if self.heap.is_callable(v) { + self.atoms.function + } else { + self.atoms.object + } + } + } + } + + // ----------------------------------------------------------------- + // Properties + // ----------------------------------------------------------------- + + /// An inline-cache hit for a get: the value, or `None` for a miss. + pub(crate) fn ic_get(&self, base: Value, ic: u32) -> Option { + let e = *self + .codes + .get(self.cur)? + .ics + .get(usize::try_from(ic).ok()?)?; + if e.shape == 0 { + return None; + } + let o = self.heap.obj(base)?; + if o.shape.0 != e.shape { + return None; + } + o.slots.get(usize::try_from(e.slot).ok()?).copied() + } + + /// An inline-cache hit for a set: stores and returns true, or false for + /// a miss. + pub(crate) fn ic_set(&mut self, base: Value, ic: u32, v: Value) -> bool { + let Some(e) = usize::try_from(ic) + .ok() + .and_then(|i| self.codes.get(self.cur)?.ics.get(i).copied()) + else { + return false; + }; + if e.shape == 0 { + return false; + } + let Some(o) = self.heap.obj_mut(base) else { + return false; + }; + // An array's `length` is not an ordinary data property (§10.4.2.4†). + if o.shape.0 != e.shape || matches!(o.kind, ObjKind::Array) { + return false; + } + let Some(slot) = usize::try_from(e.slot) + .ok() + .and_then(|i| o.slots.get_mut(i)) + else { + return false; + }; + *slot = v; + true + } + + fn ic_install(&mut self, ic: Option, shape: u32, slot: u32) { + let Some(ic) = ic.and_then(|i| usize::try_from(i).ok()) else { + return; + }; + if let Some(e) = self.codes.get_mut(self.cur).and_then(|f| f.ics.get_mut(ic)) { + e.shape = shape; + e.slot = slot; + } + } + + /// `[[Get]]` of `key` on a value, §6.2.5.5 GetValue step 3 and §10.1.8: + /// an own or inherited data property of an object, `length` and the + /// indices of a string, and a TypeError for `undefined` and `null`. + pub(crate) fn get_property( + &mut self, + base: Value, + key: Key, + ic: Option, + ) -> Result { + match base.decode() { + Decoded::Undefined | Decoded::Null => { + let name = self.key_text(key); + let what = if base == Value::NULL { + "null" + } else { + "undefined" + }; + Err(self.type_error(&format!( + "Cannot read properties of {what} (reading '{name}')" + ))) + } + Decoded::Ref(_) => match self.heap.cell(base) { + Some(Cell::Object(_)) => match self.heap.find(base, key)? { + Some(f) => { + if f.own && !self.heap.is_dictionary(f.shape) { + self.ic_install(ic, f.shape.0, f.slot); + } + self.heap + .slot(f.holder, f.slot) + .ok_or(Abrupt::Internal("property slot")) + } + None => Ok(Value::UNDEFINED), + }, + Some(Cell::Str(s)) => { + if key == self.atoms.k_length { + let n = i32::try_from(s.len()).map_err(|_| Abrupt::OutOfMemory)?; + return Ok(Value::int32(n)); + } + let unit = self + .heap + .key_string(key) + .and_then(array_index) + .and_then(|i| s.unit(i)); + match unit { + Some(u) => { + let c = JsString::from_units(&[u]).map_err(|_| Abrupt::OutOfMemory)?; + Ok(self.heap.new_string(c)?) + } + None => self.get_from_primitive_proto(Intrinsic::StringPrototype, key), + } + } + _ => Err(Abrupt::Internal("property get on an internal cell")), + }, + Decoded::Bool(_) => self.get_from_primitive_proto(Intrinsic::BooleanPrototype, key), + Decoded::Int32(_) | Decoded::Double(_) => { + self.get_from_primitive_proto(Intrinsic::NumberPrototype, key) + } + _ => Err(Abrupt::NotYet("property of a primitive".into())), + } + } + + /// `[[Get]]` on a primitive base: GetValue step 3.a makes ToObject(base) + /// the object the property is looked up on, so the lookup is on the + /// wrapper's prototype (the wrapper has no own properties but a String's + /// `length` and indices, which the caller has handled). The properties + /// are data properties, so the receiver does not matter. `NotYet` while + /// the realm has no such prototype. + fn get_from_primitive_proto(&mut self, which: Intrinsic, key: Key) -> Result { + let proto = self.intrinsic(which); + if !self.heap.is_object(proto) { + return Err(Abrupt::NotYet("property of a primitive".into())); + } + match self.heap.find(proto, key)? { + Some(f) => self + .heap + .slot(f.holder, f.slot) + .ok_or(Abrupt::Internal("property slot")), + None => Ok(Value::UNDEFINED), + } + } + + /// `base[key]`, GetValue §6.2.5.5† (an own or inherited data property; + /// a primitive base is looked up on its wrapper's prototype). + /// + /// # Errors + /// A `TypeError` for `undefined` and `null`, [`Abrupt::OutOfMemory`]. + pub fn get_value(&mut self, base: Value, key: Key) -> Result { + self.get_property(base, key, None) + } + + /// `base[key] = v`, PutValue §6.2.5.6† (OrdinarySet for data + /// properties), failing as strict code does when `throw` is set. + /// + /// # Errors + /// A `TypeError` when the property is read-only and `throw`, or the base + /// is `undefined` or `null`. + pub fn put_value( + &mut self, + base: Value, + key: Key, + v: Value, + throw: bool, + ) -> Result<(), Abrupt> { + if self.heap.is_object(base) + && let Some(f) = self.heap.find(base, key)? + && f.attrs.0 & Attrs::WRITABLE == 0 + { + if throw { + let name = self.key_text(key); + return Err( + self.type_error(&format!("Cannot assign to read only property '{name}'")) + ); + } + return Ok(()); + } + self.set_property(base, key, v, None) + } + + /// The heap, mutably: natives define properties and allocate objects + /// through it. + pub fn heap_mut(&mut self) -> &mut crate::heap::Heap { + &mut self.heap + } + + /// `obj[idx]`: a string's character at an integer index, else + /// ToPropertyKey and `[[Get]]`. + pub(crate) fn get_element(&mut self, base: Value, idx: Value) -> Result { + if let (Some(s), Some(n)) = (self.heap.string(base), num(idx)) { + let d = n.to_f64(); + if d >= 0.0 && d == d.trunc() && d < 4_294_967_295.0 { + // Integral and in range: exact. + if let Some(u) = s.unit(d as usize) { + let c = JsString::from_units(&[u]).map_err(|_| Abrupt::OutOfMemory)?; + return Ok(self.heap.new_string(c)?); + } + } + } + if base == Value::UNDEFINED || base == Value::NULL { + let what = if base == Value::NULL { + "null" + } else { + "undefined" + }; + return Err(self.type_error(&format!("Cannot read properties of {what}"))); + } + let key = self.coerce_property_key(idx)?; + self.get_property(base, key, None) + } + + /// `[[Set]]` of `key` on a value (PutValue §6.2.5.6, OrdinarySet + /// §10.1.9.2 for data properties): sets an own writable property, adds a + /// property when none is found (or an inherited writable one is shadowed), + /// and fails on a read-only one: a TypeError in strict code, nothing in + /// sloppy code. + pub(crate) fn set_property( + &mut self, + base: Value, + key: Key, + v: Value, + ic: Option, + ) -> Result<(), Abrupt> { + if base == Value::UNDEFINED || base == Value::NULL { + let name = self.key_text(key); + return Err(self.type_error(&format!( + "Cannot set properties of {} (setting '{name}')", + if base == Value::NULL { + "null" + } else { + "undefined" + } + ))); + } + if !self.heap.is_object(base) { + // A primitive receiver: OrdinarySet step 2.d.i "If Receiver is + // not an Object, return false". + if self.strict() { + let name = self.key_text(key); + return Err( + self.type_error(&format!("Cannot create property '{name}' on a primitive")) + ); + } + return Ok(()); + } + let array_key = self.heap.is_array_object(base) + && (key == self.atoms.k_length || self.heap.key_index(key).is_some()); + let ok = match self.heap.find(base, key)? { + Some(f) if f.attrs.0 & Attrs::WRITABLE == 0 => false, + // OrdinarySetWithOwnDescriptor (§10.1.9.2†) step 2.c.ii: an + // existing own data property of an array is changed with + // `[[DefineOwnProperty]]` { [[Value]]: V }, which ArraySetLength + // (§10.4.2.4†) handles for `length`. + Some(f) if f.own && array_key && key == self.atoms.k_length => { + let d = crate::props::PropDesc { + value: Some(v), + ..crate::props::PropDesc::default() + }; + self.define_own_property(base, key, &d)? + } + // Step 2.d: no own property, so CreateDataProperty(Receiver, P, V), + // which for an array index may grow `length` (§10.4.2.1†). + None if array_key => self.create_data_property(base, key, v)?, + Some(f) if !f.own && array_key => self.create_data_property(base, key, v)?, + Some(f) if f.own => { + self.heap.set_slot(base, f.slot, v)?; + if !self.heap.is_dictionary(f.shape) { + self.ic_install(ic, f.shape.0, f.slot); + } + true + } + _ => { + self.heap.define(base, key, v, Attrs::ALL)?; + true + } + }; + if !ok && self.strict() { + let name = self.key_text(key); + return Err(self.type_error(&format!("Cannot assign to read only property '{name}'"))); + } + Ok(()) + } + + /// The text of a key, for messages. + pub(crate) fn key_text(&self, key: Key) -> String { + self.heap + .key_string(key) + .map(JsString::to_string_lossy) + .unwrap_or_default() + } + + // ----------------------------------------------------------------- + // Closures + // ----------------------------------------------------------------- + + /// OrdinaryFunctionCreate §10.2.3 and SetFunctionLength, SetFunctionName + /// (§10.2.10†): a closure over `env` of function `code`, with `length` and + /// `name`. (`prototype` comes with `[[Construct]]`, not yet.) + pub(crate) fn new_closure(&mut self, code: usize, env: Value) -> Result { + let (length, name_value, name_units, code_kind) = { + let f = self + .codes + .get(code) + .ok_or(Abrupt::Internal("closure of an unknown function"))?; + ( + f.code.length, + f.name_value, + f.code.name.clone(), + f.code.kind, + ) + }; + let name_value = match name_value { + Some(v) => v, + None => { + let s = JsString::from_units(&name_units).map_err(|_| Abrupt::OutOfMemory)?; + let v = self.heap.new_string(s)?; + if let Some(f) = self.codes.get_mut(code) { + f.name_value = Some(v); + } + v + } + }; + let code_u32 = u32::try_from(code).map_err(|_| Abrupt::Internal("function index"))?; + let proto = self.atoms.function_proto; + let f = self.heap.new_object( + proto, + ObjKind::Closure { + code: code_u32, + env, + }, + )?; + let len = i32::try_from(length).map_err(|_| Abrupt::Internal("function length"))?; + let (kl, kn) = (self.atoms.k_length, self.atoms.k_name); + self.heap + .define(f, kl, Value::int32(len), CONFIGURABLE_ONLY)?; + self.heap.define(f, kn, name_value, CONFIGURABLE_ONLY)?; + if is_constructor_kind(code_kind) { + // MakeConstructor, §10.2.5†: `prototype` is a fresh object whose + // `constructor` is the function (writable, not enumerable, not + // configurable on the function; writable and configurable on the + // prototype). + let object_proto = self.atoms.object_proto; + let p = self.heap.new_object(object_proto, ObjKind::Plain)?; + let (kc, kp) = (self.atoms.k_constructor, self.atoms.k_prototype); + self.heap.define(p, kc, f, WRITABLE_CONFIGURABLE)?; + self.heap.define(f, kp, p, Attrs(Attrs::WRITABLE))?; + } + Ok(f) + } + + /// `[[Construct]]` of a function value, §10.2.2, for a base constructor + /// (`function`): `this` is a new object whose prototype is the function's + /// `prototype` property when that is an object, else `%Object.prototype%` + /// (OrdinaryCreateFromConstructor, §10.1.13); the body runs in a frame of + /// kind construct, whose return gives the object. Class constructors and + /// native constructors are not yet. + /// + /// A native constructor runs at once ([`NativeFn`](crate::interp::NativeFn) + /// with `new_target` set) and its result is `Some`; a bound function + /// constructs its target (§10.4.1.2†); a closure enters a frame and the + /// result is `None`. + pub(crate) fn construct( + &mut self, + f: Value, + args: &[Value], + ret: Option, + ) -> Result, Abrupt> { + let mut f = f; + let mut all_args: Option> = None; + // §10.4.1.2 [[Construct]] of a bound function: the target, with the + // bound arguments first; step 5, NewTarget is the target when it was + // the bound function. + while let Some(ObjKind::Bound(i)) = self.heap.obj(f).map(|o| o.kind) { + let b = self.bound_fn(i)?; + let mut all = b.args; + all.extend_from_slice(all_args.as_deref().unwrap_or(args)); + all_args = Some(all); + f = b.target; + } + let args = all_args.as_deref().unwrap_or(args); + let code = match self.heap.obj(f).map(|o| o.kind) { + Some(ObjKind::Closure { code, .. }) => code, + Some(ObjKind::Native(i)) => { + if !self.native(i)?.1 { + return Err(self.type_error("value is not a constructor")); + } + self.charge()?; + let v = self.invoke_native(i, f, Value::UNDEFINED, args, f)?; + return Ok(Some(v)); + } + _ => return Err(self.type_error("value is not a constructor")), + }; + let kind = self + .codes + .get(usize::try_from(code).map_err(|_| Abrupt::Internal("code index"))?) + .map(|c| c.code.kind) + .ok_or(Abrupt::Internal("code index"))?; + match kind { + FunctionKind::Normal => {} + FunctionKind::Class | FunctionKind::Derived => { + return Err(Abrupt::NotYet("class constructors".into())); + } + _ => return Err(self.type_error("value is not a constructor")), + } + let kp = self.atoms.k_prototype; + let proto = match self.heap.find(f, kp)? { + Some(found) => self.heap.slot(found.holder, found.slot), + None => None, + }; + let proto = match proto { + Some(p) if self.heap.is_object(p) => p, + _ => self.atoms.object_proto, + }; + let this = self.heap.new_object(proto, ObjKind::Plain)?; + self.enter_closure(f, code, this, args, ret, crate::frame::KIND_CONSTRUCT)?; + Ok(None) + } + + /// `new f(...args)` for a native: [[Construct]] to completion. + /// + /// # Errors + /// A `TypeError` if `f` is not a constructor, or what the constructor + /// throws. + pub fn construct_value(&mut self, f: Value, args: &[Value]) -> Result { + let saved = (self.fp, self.pc, self.cur, self.top); + let result = match self.construct(f, args, None) { + Ok(Some(v)) => Ok(v), + Ok(None) => self.execute(), + Err(e) => Err(e), + }; + (self.fp, self.pc, self.cur, self.top) = saved; + result + } + + /// Whether `f` has `[[Construct]]`: a `function` closure, a native + /// made as a constructor, or a bound function of a constructor. + #[must_use] + pub fn is_constructor(&self, f: Value) -> bool { + match self.heap.obj(f).map(|o| o.kind) { + Some(ObjKind::Closure { code, .. }) => usize::try_from(code) + .ok() + .and_then(|i| self.codes.get(i)) + .is_some_and(|c| { + matches!( + c.code.kind, + FunctionKind::Normal | FunctionKind::Class | FunctionKind::Derived + ) + }), + Some(ObjKind::Native(i)) => self.native(i).is_ok_and(|n| n.1), + Some(ObjKind::Bound(i)) => self + .bound_fn(i) + .is_ok_and(|b| self.is_constructor(b.target)), + _ => false, + } + } + + /// OrdinaryCreateFromConstructor, §10.1.13†: a new ordinary object + /// whose prototype is the `prototype` of `new_target` if that is an + /// object, else `fallback`'s intrinsic. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`]. + pub fn create_from_constructor( + &mut self, + new_target: Value, + fallback: Intrinsic, + kind: ObjKind, + ) -> Result { + let kp = self.atoms.k_prototype; + let proto = if self.heap.is_object(new_target) { + self.get_property(new_target, kp, None)? + } else { + Value::UNDEFINED + }; + let proto = if self.heap.is_object(proto) { + proto + } else { + let p = self.intrinsic(fallback); + if self.heap.is_object(p) { + p + } else { + self.atoms.object_proto + } + }; + Ok(self.heap.new_object(proto, kind)?) + } + + // ----------------------------------------------------------------- + // Globals + // ----------------------------------------------------------------- + + /// GetValue of an identifier reference that resolved in the global + /// environment (§9.1.1.4.1† HasBinding, §9.1.1.4.6† GetBindingValue): the + /// declarative record first (a hole is the temporal dead zone), then the + /// global object. An unresolvable reference is a ReferenceError when + /// `throw_unresolvable`, else `undefined` (`typeof`'s operand). + pub(crate) fn get_global( + &mut self, + key: Key, + throw_unresolvable: bool, + ) -> Result { + if let Some(&(v, _)) = self.global_lex.get(&key) { + if v == Value::HOLE { + let name = self.key_text(key); + return Err( + self.reference_error(&format!("Cannot access '{name}' before initialization")) + ); + } + return Ok(v); + } + let global = self.global; + match self.heap.find(global, key)? { + Some(f) => self + .heap + .slot(f.holder, f.slot) + .ok_or(Abrupt::Internal("global property slot")), + None if throw_unresolvable => { + let name = self.key_text(key); + Err(self.reference_error(&format!("{name} is not defined"))) + } + None => Ok(Value::UNDEFINED), + } + } + + /// PutValue on an identifier reference resolved in the global + /// environment (§9.1.1.4.5† SetMutableBinding). + pub(crate) fn set_global(&mut self, key: Key, v: Value) -> Result<(), Abrupt> { + if let Some(&(old, is_const)) = self.global_lex.get(&key) { + let name = self.key_text(key); + if old == Value::HOLE { + return Err( + self.reference_error(&format!("Cannot access '{name}' before initialization")) + ); + } + if is_const { + return Err(self.type_error("Assignment to constant variable.")); + } + if let Some(entry) = self.global_lex.get_mut(&key) { + entry.0 = v; + } + return Ok(()); + } + let global = self.global; + if self.heap.find(global, key)?.is_none() && self.strict() { + // §9.1.1.2.5† step 4 on an unresolvable reference in strict code. + let name = self.key_text(key); + return Err(self.reference_error(&format!("{name} is not defined"))); + } + self.set_property(global, key, v, None) + } + + /// GlobalDeclarationInstantiation, §16.1.7†, steps 1 to 18 (see + /// [`crate::ops::Op::DeclareGlobals`]). The checks come before any + /// binding is created. + pub(crate) fn declare_globals(&mut self, k: u32) -> Result<(), Abrupt> { + let (decls, base) = { + let f = self + .codes + .get(self.cur) + .ok_or(Abrupt::Internal("code index"))?; + let i = usize::try_from(k).map_err(|_| Abrupt::Internal("constant index"))?; + match f.code.consts.get(i) { + Some(Const::GlobalDeclarations(d)) => (d.clone(), f.base), + _ => { + return Err(Abrupt::Internal( + "DeclareGlobals without a declaration record", + )); + } + } + }; + let global = self.global; + let mut lexical = Vec::new(); + for (name, is_const) in &decls.lexical { + lexical.push((self.name_key(name)?, *is_const)); + } + let mut var_names = Vec::new(); + let mut functions = Vec::new(); + for d in &decls.var_scoped { + match d { + VarScoped::Var(n) => var_names.push(self.name_key(n)?), + VarScoped::Function { name, func } => { + let key = self.name_key(name)?; + var_names.push(key); + functions.push((key, *func)); + } + } + } + let mut annexb = Vec::new(); + for n in &decls.annexb { + annexb.push(self.name_key(n)?); + } + // Steps 3 and 4: a lexical name may not clash with a var name or a + // lexical binding (HasVarDeclaration, HasLexicalDeclaration), nor with + // a non-configurable global property (HasRestrictedGlobalProperty). + for (key, _) in &lexical { + let restricted = match self.heap.find(global, *key)? { + Some(f) => f.own && f.attrs.0 & Attrs::CONFIGURABLE == 0, + None => false, + }; + if self.var_names.contains(key) || self.global_lex.contains_key(key) || restricted { + let name = self.key_text(*key); + return Err( + self.syntax_error(&format!("Identifier '{name}' has already been declared")) + ); + } + } + // Step 5: a var name may not clash with a lexical binding. + for key in &var_names { + if self.global_lex.contains_key(key) { + let name = self.key_text(*key); + return Err( + self.syntax_error(&format!("Identifier '{name}' has already been declared")) + ); + } + } + // Step 8 and 10: CanDeclareGlobalFunction: an existing property must be + // configurable, or a writable enumerable data property (the global + // object is extensible). + for (key, _) in &functions { + if let Some(f) = self.heap.find(global, *key)? { + let ok = f.own + && (f.attrs.0 & Attrs::CONFIGURABLE != 0 + || f.attrs.0 & (Attrs::WRITABLE | Attrs::ENUMERABLE) + == (Attrs::WRITABLE | Attrs::ENUMERABLE)); + if f.own && !ok { + let name = self.key_text(*key); + return Err( + self.type_error(&format!("Cannot declare global function '{name}'")) + ); + } + } + } + // Step 15: the lexical bindings, uninitialized. + for (key, is_const) in &lexical { + self.global_lex.insert(*key, (Value::HOLE, *is_const)); + } + // Step 16: the function objects (the last declaration of a name wins). + for (key, func) in &functions { + let code = base + .checked_add(*func) + .ok_or(Abrupt::Internal("function index"))?; + let code = usize::try_from(code).map_err(|_| Abrupt::Internal("function index"))?; + let f = self.new_closure(code, Value::UNDEFINED)?; + self.heap.define(global, *key, f, GLOBAL_VAR)?; + self.var_names.insert(*key); + } + // Step 17: the variables. + for key in &var_names { + if self.heap.find(global, *key)?.is_none() { + self.heap + .define(global, *key, Value::UNDEFINED, GLOBAL_VAR)?; + } + self.var_names.insert(*key); + } + // Step 12 (Annex B.3.2.2†): functions in blocks, when no lexical + // binding of the name exists. + for key in &annexb { + if !self.global_lex.contains_key(key) && self.heap.find(global, *key)?.is_none() { + self.heap + .define(global, *key, Value::UNDEFINED, GLOBAL_VAR)?; + self.var_names.insert(*key); + } + } + Ok(()) + } + + fn name_key(&mut self, units: &[u16]) -> Result { + let s = JsString::from_units(units).map_err(|_| Abrupt::OutOfMemory)?; + Ok(self.heap.intern(&s)?) + } + + // ----------------------------------------------------------------- + // Embedding helpers (used by natives and by hosts) + // ----------------------------------------------------------------- + + /// A new string value. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`]. + pub fn new_string(&mut self, s: &str) -> Result { + Ok(self.heap.new_str(s)?) + } + + /// The text of a string value, if it is one. + #[must_use] + pub fn string_text(&self, v: Value) -> Option { + self.heap.string(v).map(JsString::to_string_lossy) + } + + /// A new ordinary object. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`]. + pub fn new_object(&mut self) -> Result { + let proto = self.atoms.object_proto; + Ok(self.heap.new_object(proto, ObjKind::Plain)?) + } + + /// A new native function with `length` and `name`. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`]. + pub fn new_native( + &mut self, + name: &str, + length: i32, + f: crate::interp::NativeFn, + ) -> Result { + self.new_native_with(name, length, f, false) + } + + /// A new native function that has `[[Construct]]`: `new` runs `f` with + /// [`Vm::new_target`] set to the constructor and `this` `undefined`; the + /// result of `f` is the result of `new`. `prototype` is not made here. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`]. + pub fn new_native_constructor( + &mut self, + name: &str, + length: i32, + f: crate::interp::NativeFn, + ) -> Result { + self.new_native_with(name, length, f, true) + } + + fn new_native_with( + &mut self, + name: &str, + length: i32, + f: crate::interp::NativeFn, + constructor: bool, + ) -> Result { + let index = u32::try_from(self.natives.len()).map_err(|_| Abrupt::OutOfMemory)?; + self.natives.push((f, constructor)); + let proto = self.atoms.function_proto; + let o = self.heap.new_object(proto, ObjKind::Native(index))?; + let name_v = self.heap.new_str(name)?; + let (kl, kn) = (self.atoms.k_length, self.atoms.k_name); + self.heap + .define(o, kl, Value::int32(length), CONFIGURABLE_ONLY)?; + self.heap.define(o, kn, name_v, CONFIGURABLE_ONLY)?; + Ok(o) + } + + /// BoundFunctionCreate, §10.4.1.3†, without the `length` and `name` + /// (the caller defines them): a callable that calls `target` with + /// `this` and `args` first. Its prototype is the target's. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`]. + pub fn new_bound_function( + &mut self, + target: Value, + this: Value, + args: Vec, + ) -> Result { + let index = u32::try_from(self.bound.len()).map_err(|_| Abrupt::OutOfMemory)?; + self.bound + .push(crate::interp::BoundFn { target, this, args }); + let proto = self.heap.proto_of(target).unwrap_or(Value::NULL); + Ok(self.heap.new_object(proto, ObjKind::Bound(index))?) + } + + /// Defines (or overwrites) a writable, enumerable, configurable data + /// property `name` of `obj`. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`] or [`Abrupt::Internal`] if `obj` is not an + /// object. + pub fn define_property(&mut self, obj: Value, name: &str, v: Value) -> Result<(), Abrupt> { + let key = self.heap.intern_str(name)?; + Ok(self.heap.define(obj, key, v, Attrs::ALL)?) + } + + /// Defines a property of the global object. + /// + /// # Errors + /// As [`Vm::define_property`]. + pub fn define_global(&mut self, name: &str, v: Value) -> Result<(), Abrupt> { + let g = self.global; + self.define_property(g, name, v) + } + + /// `obj[name]`, for an own or inherited data property: `undefined` when + /// there is none. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`] or [`Abrupt::Internal`]. + pub fn get_named(&mut self, obj: Value, name: &str) -> Result { + let key = self.heap.intern_str(name)?; + match self.heap.find(obj, key)? { + Some(f) => self + .heap + .slot(f.holder, f.slot) + .ok_or(Abrupt::Internal("slot")), + None => Ok(Value::UNDEFINED), + } + } + + /// A short description of a value, for failure reports: a string's text, + /// a Number, or an error object's `name: message`. + pub fn describe(&mut self, v: Value) -> String { + match v.decode() { + Decoded::Ref(_) => { + if let Some(s) = self.string_text(v) { + return format!("{s:?}"); + } + let name = self + .get_named(v, "name") + .ok() + .and_then(|n| self.string_text(n)); + let message = self + .get_named(v, "message") + .ok() + .and_then(|m| self.string_text(m)); + match (name, message) { + (Some(n), Some(m)) => format!("{n}: {m}"), + (None, Some(m)) => m, + _ => "[object]".to_string(), + } + } + _ => self + .coerce_js_string(v) + .map(|s| s.to_string_lossy()) + .unwrap_or_default(), + } + } +} + +/// The array index a canonical numeric string names: decimal digits, no +/// leading zero (except `0`), below 2^32 - 1 (§6.1.7). +fn array_index(s: &JsString) -> Option { + let mut n: u64 = 0; + let mut len = 0usize; + let mut first = 0u16; + for u in s.units() { + let d = u.checked_sub(u16::from(b'0')).filter(|d| *d < 10)?; + if len == 0 { + first = u; + } + n = n.checked_mul(10)?.checked_add(u64::from(d))?; + len = len.checked_add(1)?; + } + if len == 0 || (len > 1 && first == u16::from(b'0')) || n >= 4_294_967_295 { + return None; + } + usize::try_from(n).ok() +} + +/// Whether a closure of this kind has a `prototype` (§10.2.3† step 4: +/// OrdinaryFunctionCreate then MakeConstructor for a `function`; arrows and +/// methods have none; a class's `prototype` is made by `ClassSetup`). +fn is_constructor_kind(k: FunctionKind) -> bool { + k == FunctionKind::Normal +} diff --git a/crates/clazel-js-vm/src/heap.rs b/crates/clazel-js-vm/src/heap.rs new file mode 100644 index 000000000..8e65b1653 --- /dev/null +++ b/crates/clazel-js-vm/src/heap.rs @@ -0,0 +1,690 @@ +//! The cells the interpreter allocates in `clazel-js-heap`'s arena, and the +//! property operations on them (item 7.8, first slice). +//! +//! `clazel-js-heap` on `main` has the arena, strings, interned keys and +//! shapes, but no object type: the elements, symbols and collector slices are +//! open pull requests. This module is the VM's own cell type over the arena +//! (`spec::Cell`): a string, an ordinary object (properties by shape, a +//! prototype, and what kind of object it is) and a declarative environment. +//! It holds only data properties; accessors, elements and exotic objects come +//! with the later slices. Nothing here is collected: the collector is item +//! 7.6. + +use clazel_js_heap::arena::{Arena, Error as ArenaError}; +use clazel_js_heap::shape::{Attrs, Error as ShapeError, ROOT, ShapeId, Shapes}; +use clazel_js_heap::string::{Interner, JsString, Key, TooLong}; +use clazel_js_heap::value::Value; + +/// The longest prototype chain a lookup walks before it gives up (a cycle +/// cannot be built by this slice's handlers; the bound keeps a corrupt heap +/// from looping). +pub const MAX_PROTO_DEPTH: usize = 100_000; + +/// What an object is. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ObjKind { + /// An ordinary object. + Plain, + /// A compiled function: its index in the VM's code table and its + /// captured environment (`spec::FnInfo`): a heap reference, or + /// `undefined`. + Closure { + /// The index of the function's code in the VM's table. + code: u32, + /// The captured environment. + env: Value, + }, + /// A native function: its index in the VM's native table. + Native(u32), + /// A bound function exotic object (§10.4.1†): its index in the VM's + /// table of bound targets, `this` values and arguments. + Bound(u32), + /// An object with `[[ErrorData]]` (§20.5†): made by an `Error` + /// constructor or thrown by the VM. + Error, + /// A Boolean, Number or String wrapper object: the object holds the + /// primitive as `[[BooleanData]]`, `[[NumberData]]` or `[[StringData]]` + /// (§20.3†, §21.1†, §22.1†). + Primitive(Value), + /// An Array exotic object (§10.4.2†). Its `length` is the first property + /// (slot 0), made by [`Heap::new_array`] and never removed, so reading it + /// is one slot access. The index properties are ordinary data properties + /// in the shape, because `clazel-js-heap`'s elements (#211) are not on + /// this base. + Array, +} + +/// An object: properties by shape, a prototype and a kind. +#[derive(Debug)] +pub struct Obj { + /// The shape: the keys, slot indices and attributes of the properties. + pub shape: ShapeId, + /// The property values, by the shape's slot indices. + pub slots: Vec, + /// `[[Prototype]]`: an object reference or `null`. + pub proto: Value, + /// What the object is. + pub kind: ObjKind, +} + +/// A declarative environment (`spec::Class::Env`). +#[derive(Debug)] +pub struct Env { + /// The enclosing environment, or `undefined` for the outermost. + pub parent: Value, + /// The bindings. + pub slots: Vec, +} + +/// A heap cell. +#[derive(Debug)] +pub enum Cell { + /// A String value. + Str(JsString), + /// An object. + Object(Obj), + /// An environment. + Env(Env), +} + +/// Why a heap operation failed. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HeapError { + /// The arena is full. + Full, + /// A string would pass the length limit. + TooLong, + /// A reference is stale or out of range, or a cell is not what the + /// operation needs: the heap invariants do not hold. + Corrupt, +} + +impl From for HeapError { + fn from(e: ArenaError) -> Self { + match e { + ArenaError::Full => HeapError::Full, + _ => HeapError::Corrupt, + } + } +} + +impl From for HeapError { + fn from(_: TooLong) -> Self { + HeapError::TooLong + } +} + +impl From for HeapError { + fn from(e: ShapeError) -> Self { + match e { + ShapeError::TooMany => HeapError::Full, + _ => HeapError::Corrupt, + } + } +} + +/// A property found on an object or its prototype chain. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Found { + /// The object that has the property. + pub holder: Value, + /// The holder's shape. + pub shape: ShapeId, + /// The property's slot. + pub slot: u32, + /// Its attributes. + pub attrs: Attrs, + /// Whether the holder is the object the search started at. + pub own: bool, +} + +/// The heap: the arena of cells, the shape table and the key interner. +#[derive(Debug)] +pub struct Heap { + arena: Arena, + shapes: Shapes, + keys: Interner, + /// The interned `length`, which an array keeps in slot 0. + length_key: Key, +} + +impl Heap { + /// An empty heap whose arena holds at most `limit` cells. + #[must_use] + pub fn new(limit: usize) -> Self { + let mut keys = Interner::new(); + // "length" is 6 code units: it fits the string limit, so the + // interner cannot refuse it; the fallback key is never used. + let length_key = JsString::from_rust_str("length") + .ok() + .and_then(|s| keys.intern(&s).ok()) + .unwrap_or(Key(0)); + Self { + arena: Arena::with_limit(limit), + shapes: Shapes::new(), + keys, + length_key, + } + } + + /// The number of live cells. + #[must_use] + pub fn live(&self) -> usize { + self.arena.live() + } + + /// Allocates a cell and returns the reference to it as a value. + /// + /// # Errors + /// [`HeapError::Full`] if the arena is full. + pub fn alloc(&mut self, cell: Cell) -> Result { + let r = self.arena.alloc(cell)?; + Ok(Value::heap_ref(r)) + } + + /// Allocates a string. + /// + /// # Errors + /// [`HeapError::Full`] if the arena is full. + pub fn new_string(&mut self, s: JsString) -> Result { + self.alloc(Cell::Str(s)) + } + + /// Allocates a string from a Rust string. + /// + /// # Errors + /// [`HeapError::Full`] or [`HeapError::TooLong`]. + pub fn new_str(&mut self, s: &str) -> Result { + self.new_string(JsString::from_rust_str(s)?) + } + + /// Allocates an ordinary object with `proto` as its prototype. + /// + /// # Errors + /// [`HeapError::Full`] if the arena is full. + pub fn new_object(&mut self, proto: Value, kind: ObjKind) -> Result { + self.alloc(Cell::Object(Obj { + shape: ROOT, + slots: Vec::new(), + proto, + kind, + })) + } + + /// Allocates an Array exotic object whose `length` is `len`: the + /// property is writable, not enumerable, not configurable + /// (§10.4.2.2† ArrayCreate steps 6 to 8). `len` must already be checked + /// against 2^32 - 1. + /// + /// # Errors + /// [`HeapError::Full`] if the arena is full. + pub fn new_array(&mut self, proto: Value, len: u32) -> Result { + let a = self.new_object(proto, ObjKind::Array)?; + let k = self.length_key; + self.define(a, k, length_value(len), Attrs(Attrs::WRITABLE))?; + Ok(a) + } + + /// The `length` of an Array exotic object, with whether it is writable. + #[must_use] + pub fn array_length(&self, arr: Value) -> Option<(u32, bool)> { + let o = self.obj(arr)?; + if !matches!(o.kind, ObjKind::Array) { + return None; + } + let (slot, attrs) = self.shapes.lookup(o.shape, self.length_key)?; + let v = o.slots.get(usize::try_from(slot).ok()?).copied()?; + let d = crate::numeric::num(v)?.to_f64(); + let n = d as u32; + (f64::from(n) == d).then_some((n, attrs.0 & Attrs::WRITABLE != 0)) + } + + /// Stores `len` as the `length` of an array (the caller has checked that + /// the property is writable, or is applying a definition). + /// + /// # Errors + /// [`HeapError::Corrupt`] if `arr` is not an array. + pub fn set_array_length(&mut self, arr: Value, len: u32) -> Result<(), HeapError> { + let o = self.obj(arr).ok_or(HeapError::Corrupt)?; + let (slot, _) = self + .shapes + .lookup(o.shape, self.length_key) + .ok_or(HeapError::Corrupt)?; + self.set_slot(arr, slot, length_value(len)) + } + + /// Whether a value is an Array exotic object. + #[must_use] + pub fn is_array_object(&self, v: Value) -> bool { + self.obj(v) + .is_some_and(|o| matches!(o.kind, ObjKind::Array)) + } + + /// The array index a key names (§6.1.7†), if it names one. + #[must_use] + pub fn key_index(&self, k: Key) -> Option { + self.keys.resolve(k).and_then(canonical_index) + } + + /// Removes the own properties `keys` of `obj` (a key it does not have is + /// ignored): the shape is rebuilt from the root without them, so the + /// others keep their order and get consecutive slots. A shared shape + /// reuses its transitions; a dictionary shape (an object with more than + /// 128 properties) gets a new dictionary shape each time, which stays in + /// the shape table until the collector of #236 frees it. + /// + /// # Errors + /// [`HeapError::Corrupt`] if `obj` is not an object, [`HeapError::Full`] + /// on overflow. + pub fn remove_properties(&mut self, obj: Value, keys: &[Key]) -> Result<(), HeapError> { + let o = self.obj(obj).ok_or(HeapError::Corrupt)?; + let props = self.shapes.properties(o.shape); + if !props.iter().any(|(k, _, _)| keys.contains(k)) { + return Ok(()); + } + let old_slots = o.slots.clone(); + let mut shape = ROOT; + let mut slots: Vec = Vec::with_capacity(props.len()); + for (k, slot, a) in props { + if keys.contains(&k) { + continue; + } + let v = usize::try_from(slot) + .ok() + .and_then(|i| old_slots.get(i)) + .copied() + .ok_or(HeapError::Corrupt)?; + let (next, new_slot) = self.shapes.add_property(shape, k, a)?; + if usize::try_from(new_slot).ok() != Some(slots.len()) { + return Err(HeapError::Corrupt); + } + shape = next; + slots.push(v); + } + let o = self.obj_mut(obj).ok_or(HeapError::Corrupt)?; + o.shape = shape; + o.slots = slots; + Ok(()) + } + + /// The live cell a value refers to. + #[must_use] + pub fn cell(&self, v: Value) -> Option<&Cell> { + self.arena.get(v.as_ref()?).ok() + } + + fn cell_mut(&mut self, v: Value) -> Option<&mut Cell> { + self.arena.get_mut(v.as_ref()?).ok() + } + + /// The object a value refers to, if it refers to one. + #[must_use] + pub fn obj(&self, v: Value) -> Option<&Obj> { + match self.cell(v)? { + Cell::Object(o) => Some(o), + _ => None, + } + } + + /// The object a value refers to, mutably. + pub fn obj_mut(&mut self, v: Value) -> Option<&mut Obj> { + match self.cell_mut(v)? { + Cell::Object(o) => Some(o), + _ => None, + } + } + + /// The string a value refers to, if it refers to one. + #[must_use] + pub fn string(&self, v: Value) -> Option<&JsString> { + match self.cell(v)? { + Cell::Str(s) => Some(s), + _ => None, + } + } + + /// The environment a value refers to, if it refers to one. + #[must_use] + pub fn env(&self, v: Value) -> Option<&Env> { + match self.cell(v)? { + Cell::Env(e) => Some(e), + _ => None, + } + } + + /// The environment a value refers to, mutably. + pub fn env_mut(&mut self, v: Value) -> Option<&mut Env> { + match self.cell_mut(v)? { + Cell::Env(e) => Some(e), + _ => None, + } + } + + /// Whether a value is a reference to a string. + #[must_use] + pub fn is_string(&self, v: Value) -> bool { + self.string(v).is_some() + } + + /// Whether a value is a reference to an object. + #[must_use] + pub fn is_object(&self, v: Value) -> bool { + self.obj(v).is_some() + } + + /// Whether a value is a reference to a callable object. + #[must_use] + pub fn is_callable(&self, v: Value) -> bool { + self.obj(v).is_some_and(|o| { + matches!( + o.kind, + ObjKind::Closure { .. } | ObjKind::Native(_) | ObjKind::Bound(_) + ) + }) + } + + /// The interned key of a string. + /// + /// # Errors + /// [`HeapError::TooLong`]. + pub fn intern(&mut self, s: &JsString) -> Result { + Ok(self.keys.intern(s)?) + } + + /// The interned key of a Rust string. + /// + /// # Errors + /// [`HeapError::TooLong`]. + pub fn intern_str(&mut self, s: &str) -> Result { + let js = JsString::from_rust_str(s)?; + self.intern(&js) + } + + /// The string a key stands for. + #[must_use] + pub fn key_string(&self, k: Key) -> Option<&JsString> { + self.keys.resolve(k) + } + + /// Searches `start` and its prototype chain for the own data property + /// `key` (`[[GetOwnProperty]]` on each object, §10.1.5, then + /// `[[Prototype]]`, §10.1.8.1 step 3). + /// + /// # Errors + /// [`HeapError::Corrupt`] if the chain runs past [`MAX_PROTO_DEPTH`]. + pub fn find(&self, start: Value, key: Key) -> Result, HeapError> { + let mut cur = start; + for depth in 0..MAX_PROTO_DEPTH { + let Some(o) = self.obj(cur) else { + return Ok(None); + }; + if let Some((slot, attrs)) = self.shapes.lookup(o.shape, key) { + return Ok(Some(Found { + holder: cur, + shape: o.shape, + slot, + attrs, + own: depth == 0, + })); + } + cur = o.proto; + } + Err(HeapError::Corrupt) + } + + /// The value in a slot of an object. + #[must_use] + pub fn slot(&self, holder: Value, slot: u32) -> Option { + let i = usize::try_from(slot).ok()?; + self.obj(holder)?.slots.get(i).copied() + } + + /// Whether the shape is a dictionary shape, which an inline cache does not + /// key on (its id stays the same as properties are added). + #[must_use] + pub fn is_dictionary(&self, shape: ShapeId) -> bool { + self.shapes.is_dictionary(shape) + } + + /// Defines or overwrites the own data property `key` of `obj`: a new + /// property gets `attrs`; an existing one keeps its attributes and takes + /// the value (`CreateDataProperty` on an extensible object, §10.1.6.3, + /// for every case the first slice reaches). + /// + /// # Errors + /// [`HeapError::Corrupt`] if `obj` is not an object, [`HeapError::Full`] + /// on overflow. + pub fn define( + &mut self, + obj: Value, + key: Key, + value: Value, + attrs: Attrs, + ) -> Result<(), HeapError> { + let shape = self.obj(obj).ok_or(HeapError::Corrupt)?.shape; + if let Some((slot, _)) = self.shapes.lookup(shape, key) { + return self.set_slot(obj, slot, value); + } + let (next, slot) = self.shapes.add_property(shape, key, attrs)?; + let o = self.obj_mut(obj).ok_or(HeapError::Corrupt)?; + let i = usize::try_from(slot).map_err(|_| HeapError::Full)?; + if o.slots.len() != i { + return Err(HeapError::Corrupt); + } + o.slots.push(value); + o.shape = next; + Ok(()) + } + + /// Stores into a slot of an object. + /// + /// # Errors + /// [`HeapError::Corrupt`] if there is no such slot. + pub fn set_slot(&mut self, obj: Value, slot: u32, value: Value) -> Result<(), HeapError> { + let i = usize::try_from(slot).map_err(|_| HeapError::Corrupt)?; + let s = self + .obj_mut(obj) + .and_then(|o| o.slots.get_mut(i)) + .ok_or(HeapError::Corrupt)?; + *s = value; + Ok(()) + } + + /// The own properties of an object in the order `[[OwnPropertyKeys]]` + /// gives them (§10.1.11.1†): the array indices in ascending order, then + /// the other string keys in the order they were created. (Symbols and + /// exotic objects' keys come with the later slices.) + #[must_use] + pub fn own_properties(&self, obj: Value) -> Vec<(Key, Attrs, Value)> { + let Some(o) = self.obj(obj) else { + return Vec::new(); + }; + let mut indices: Vec<(u32, Key, Attrs, Value)> = Vec::new(); + let mut names: Vec<(Key, Attrs, Value)> = Vec::new(); + for (k, slot, a) in self.shapes.properties(o.shape) { + let Some(v) = usize::try_from(slot) + .ok() + .and_then(|i| o.slots.get(i)) + .copied() + else { + continue; + }; + match self.keys.resolve(k).and_then(canonical_index) { + Some(i) => indices.push((i, k, a, v)), + None => names.push((k, a, v)), + } + } + indices.sort_by_key(|e| e.0); + let mut out: Vec<(Key, Attrs, Value)> = + indices.into_iter().map(|(_, k, a, v)| (k, a, v)).collect(); + out.extend(names); + out + } + + /// Changes the value and attributes of the own property `key` (adding it + /// if there is none): the shape is rebuilt from the root with the same + /// order, so the property keeps its place in the key order. + /// + /// # Errors + /// [`HeapError::Corrupt`] if `obj` is not an object, [`HeapError::Full`] + /// on overflow. + pub fn redefine( + &mut self, + obj: Value, + key: Key, + value: Value, + attrs: Attrs, + ) -> Result<(), HeapError> { + let o = self.obj(obj).ok_or(HeapError::Corrupt)?; + let Some((slot, old)) = self.shapes.lookup(o.shape, key) else { + return self.define(obj, key, value, attrs); + }; + if old == attrs { + return self.set_slot(obj, slot, value); + } + let props = self.shapes.properties(o.shape); + let mut shape = ROOT; + for (k, _, a) in props { + let a = if k == key { attrs } else { a }; + let (next, _) = self.shapes.add_property(shape, k, a)?; + shape = next; + } + self.set_slot(obj, slot, value)?; + self.obj_mut(obj).ok_or(HeapError::Corrupt)?.shape = shape; + Ok(()) + } + + /// Changes the `[[Prototype]]` of an object (no cycle check: the callers + /// only pass fresh objects). + /// + /// # Errors + /// [`HeapError::Corrupt`] if `obj` is not an object. + pub fn set_proto(&mut self, obj: Value, proto: Value) -> Result<(), HeapError> { + self.obj_mut(obj).ok_or(HeapError::Corrupt)?.proto = proto; + Ok(()) + } + + /// The `[[Prototype]]` of an object. + #[must_use] + pub fn proto_of(&self, obj: Value) -> Option { + self.obj(obj).map(|o| o.proto) + } +} + +/// A `length` value: an int32 when it fits, else a double. +#[must_use] +pub fn length_value(len: u32) -> Value { + match i32::try_from(len) { + Ok(i) => Value::int32(i), + Err(_) => Value::double(f64::from(len)), + } +} + +/// The array index a string names (§6.1.7†): decimal digits, no leading +/// zero except `0` itself, below 2^32 - 1. +fn canonical_index(s: &JsString) -> Option { + let mut n: u64 = 0; + let mut len = 0usize; + let mut first = 0u16; + for u in s.units() { + let d = u.checked_sub(u16::from(b'0')).filter(|d| *d < 10)?; + if len == 0 { + first = u; + } + n = n.checked_mul(10)?.checked_add(u64::from(d))?; + len = len.checked_add(1)?; + } + if len == 0 || (len > 1 && first == u16::from(b'0')) || n >= 4_294_967_295 { + return None; + } + u32::try_from(n).ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn heap() -> Heap { + Heap::new(1000) + } + + #[test] + fn properties_are_found_on_the_chain() -> Result<(), HeapError> { + let mut h = heap(); + let proto = h.new_object(Value::NULL, ObjKind::Plain)?; + let obj = h.new_object(proto, ObjKind::Plain)?; + let (a, b) = (h.intern_str("a")?, h.intern_str("b")?); + h.define(proto, a, Value::int32(1), Attrs::ALL)?; + h.define(obj, b, Value::int32(2), Attrs::ALL)?; + let fa = h.find(obj, a)?.ok_or(HeapError::Corrupt)?; + assert!(!fa.own); + assert_eq!(h.slot(fa.holder, fa.slot), Some(Value::int32(1))); + let fb = h.find(obj, b)?.ok_or(HeapError::Corrupt)?; + assert!(fb.own); + assert_eq!(h.find(proto, b)?, None); + h.define(obj, b, Value::int32(3), Attrs::ALL)?; + assert_eq!(h.slot(obj, fb.slot), Some(Value::int32(3))); + Ok(()) + } + + #[test] + fn objects_that_gain_the_same_keys_share_a_shape() -> Result<(), HeapError> { + let mut h = heap(); + let (x, y) = ( + h.new_object(Value::NULL, ObjKind::Plain)?, + h.new_object(Value::NULL, ObjKind::Plain)?, + ); + let k = h.intern_str("k")?; + h.define(x, k, Value::TRUE, Attrs::ALL)?; + h.define(y, k, Value::FALSE, Attrs::ALL)?; + assert_eq!(h.obj(x).map(|o| o.shape), h.obj(y).map(|o| o.shape)); + Ok(()) + } + + #[test] + fn own_properties_list_indices_first_and_redefine_keeps_order() -> Result<(), HeapError> { + let mut h = heap(); + let o = h.new_object(Value::NULL, ObjKind::Plain)?; + let names = ["b", "10", "a", "2"]; + for (i, n) in names.iter().enumerate() { + let k = h.intern_str(n)?; + h.define( + o, + k, + Value::int32(i32::try_from(i).unwrap_or(0)), + Attrs::ALL, + )?; + } + let listed = |h: &Heap| -> Vec { + h.own_properties(o) + .iter() + .map(|(k, _, _)| { + h.key_string(*k) + .map(JsString::to_string_lossy) + .unwrap_or_default() + }) + .collect() + }; + assert_eq!(listed(&h), ["2", "10", "b", "a"]); + let a = h.intern_str("a")?; + h.redefine(o, a, Value::int32(9), Attrs(Attrs::WRITABLE))?; + assert_eq!(listed(&h), ["2", "10", "b", "a"]); + let found = h.find(o, a)?.ok_or(HeapError::Corrupt)?; + assert_eq!(found.attrs, Attrs(Attrs::WRITABLE)); + assert_eq!(h.slot(o, found.slot), Some(Value::int32(9))); + let b = h.intern_str("b")?; + let fb = h.find(o, b)?.ok_or(HeapError::Corrupt)?; + assert_eq!(h.slot(o, fb.slot), Some(Value::int32(0))); + Ok(()) + } + + #[test] + fn a_stale_or_wrong_reference_is_not_an_object() -> Result<(), HeapError> { + let mut h = heap(); + let s = h.new_str("abc")?; + assert!(h.is_string(s)); + assert!(!h.is_object(s)); + assert_eq!(h.obj(Value::int32(1)).map(|o| o.shape), None); + Ok(()) + } +} diff --git a/crates/clazel-js-vm/src/interp.rs b/crates/clazel-js-vm/src/interp.rs new file mode 100644 index 000000000..8c2f3e5fa --- /dev/null +++ b/crates/clazel-js-vm/src/interp.rs @@ -0,0 +1,1411 @@ +//! The interpreter loop (phase-7.md item 7.8, first slice). +//! +//! [`Vm`] owns the frame region ([`crate::frame`]), the heap +//! ([`crate::heap`]) and the loaded code. [`Vm::run`] and [`Vm::call`] push a +//! frame and run the loop: it fetches an [`Instr`], runs it, and counts +//! function entries and loop back-edges for jit.md's tiering (J.10); neither +//! count is observable by JS, as `spec::step`'s state shows. Core ops and the +//! fast paths of guarded ops are in [`Vm::step`]; every other case calls a +//! handler of [`crate::handlers`], a plain method over the frame region and +//! the heap, which jit.md's exits will share. +//! +//! # What refines what +//! +//! `spec fn step` is the semantics: for each op, [`Vm::step`] computes +//! `fast_step` when the op is core or its `fast_path` holds, and the handler +//! (uninterpreted there) otherwise. The differential test +//! (`tests/differential.rs`) compares the two for straight-line programs, and +//! it is a test, not a proof: nothing ties this code to the spec. +//! +//! # Differences from the spec's `step`, all listed +//! +//! - **A call passes at most `nparams` arguments to the frame.** The header +//! keeps the real argument count (`GetArgc`), but an argument past the +//! parameters has no slot: `spec::pushed_region` copies only `argc <= +//! nparams`, and the `arguments` object (`CreateUnmappedArguments`) is not +//! implemented, so nothing can read it yet. +//! - **Constants.** A Number constant is loaded as an int32 when it is an +//! integer in range other than `-0`, else as a double; the spec leaves the +//! pool's encoding to the loader (`Code::consts`). +//! - **A step budget.** [`Vm::set_budget`] bounds the back-edges and calls a +//! run may make, and ends the run as [`Abrupt::Terminated`] when it is +//! spent: a host's deterministic form of the `Terminator`. +//! - **No inline-cache entries for globals or prototype hits**, only own data +//! properties of non-dictionary shapes, as `spec::IcEntry` models. +//! +//! # Unwinding +//! +//! A throw looks in the current function's exception table at the `pc` of +//! the op that threw; if no entry covers it, the frame is popped through its +//! return address and the search continues at the caller's call op, until a +//! frame whose return address is [`crate::frame::RET_NONE`] (the entry of +//! [`Vm::call`]) is left, which makes the throw the result of that call. + +use crate::bytecode::{Const, FunctionCode, FunctionKind, Instr, Program, check_program}; +use crate::frame::{ + FrameError, HDR_ARGC, HDR_CALLEE, HDR_RET, KIND_CONSTRUCT, KIND_PLAIN, RET_NONE, Region, Ret, + enc_argc, frame_size, pack_ret, unpack_ret, +}; +use crate::heap::{Heap, HeapError, ObjKind}; +use crate::numeric::{self, Num, num}; +use clazel_js_heap::shape::Attrs; +use clazel_js_heap::string::{JsString, Key}; +use clazel_js_heap::value::Value; +use std::any::Any; +use std::collections::{HashMap, HashSet}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; + +/// The shared termination flag (phase-7.md, "Limits and stopping"). A host +/// sets it from another thread; the loop reads it at every back-edge and call +/// and ends the run with [`Abrupt::Terminated`], which JS cannot catch. The +/// embedding API (`clazel-js`, an open pull request) will wrap this. +#[derive(Clone, Debug, Default)] +pub struct Terminator(Arc); + +impl Terminator { + /// A flag that is clear. + #[must_use] + pub fn new() -> Self { + Self::default() + } + + /// Asks the engine to stop. + pub fn terminate(&self) { + self.0.store(true, Ordering::Relaxed); + } + + /// Clears the flag. + pub fn reset(&self) { + self.0.store(false, Ordering::Relaxed); + } + + /// Whether the flag is set. + #[must_use] + pub fn is_set(&self) -> bool { + self.0.load(Ordering::Relaxed) + } +} + +/// How a run ended other than by returning a value. +#[derive(Clone, Debug, PartialEq)] +pub enum Abrupt { + /// A JS exception: the thrown value. Catchable by JS. + Throw(Value), + /// A construct this slice does not implement, named. Not catchable. + NotYet(String), + /// The [`Terminator`] was set, or the step budget ran out. Not catchable. + Terminated, + /// The heap's arena is full or a string is too long. Not catchable. + OutOfMemory, + /// An invariant of the VM does not hold (an unchecked program, a stale + /// reference). Unreachable by construction; reported, not a panic. + Internal(&'static str), +} + +impl From for Abrupt { + fn from(_: FrameError) -> Self { + Abrupt::Internal("frame index out of the region") + } +} + +impl From for Abrupt { + fn from(e: HeapError) -> Self { + match e { + HeapError::Corrupt => Abrupt::Internal("heap invariant"), + _ => Abrupt::OutOfMemory, + } + } +} + +/// A native function: `this`, the arguments, the result. While it runs, +/// [`Vm::active_function`] is its own function object and [`Vm::new_target`] +/// is `undefined` for a call and the constructor for a `new`. +pub type NativeFn = fn(&mut Vm, Value, &[Value]) -> Result; + +/// The objects a realm's built-ins share (§6.1.7.4† "Well-Known Intrinsic +/// Objects"). [`Vm::new`] makes the first two; `clazel-js-builtins` installs +/// the others with [`Vm::set_intrinsic`]. The VM reads them where the +/// standard names an intrinsic: the prototype of a thrown error, of a +/// wrapper object, of a primitive's property lookup. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Intrinsic { + /// `%Object.prototype%`. + ObjectPrototype, + /// `%Function.prototype%`. + FunctionPrototype, + /// `%Error.prototype%`. + ErrorPrototype, + /// `%TypeError.prototype%`. + TypeErrorPrototype, + /// `%RangeError.prototype%`. + RangeErrorPrototype, + /// `%SyntaxError.prototype%`. + SyntaxErrorPrototype, + /// `%ReferenceError.prototype%`. + ReferenceErrorPrototype, + /// `%Boolean.prototype%`. + BooleanPrototype, + /// `%Number.prototype%`. + NumberPrototype, + /// `%String.prototype%`. + StringPrototype, + /// `%Array.prototype%`. + ArrayPrototype, + /// `%Array%`. + ArrayConstructor, +} + +/// The most natives that may run one inside another. +const MAX_NATIVE_DEPTH: u32 = 150; + +/// How many [`Intrinsic`]s there are. +const INTRINSICS: usize = 12; + +/// The hint of ToPrimitive (§7.1.1†). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Hint { + /// `default`. + Default, + /// `number`. + Number, + /// `string`. + String, +} + +/// The state of a bound function exotic object (§10.4.1†): +/// `[[BoundTargetFunction]]`, `[[BoundThis]]`, `[[BoundArguments]]`. +#[derive(Clone, Debug)] +pub(crate) struct BoundFn { + pub(crate) target: Value, + pub(crate) this: Value, + pub(crate) args: Vec, +} + +/// A constant of a loaded function. +#[derive(Clone, Copy, Debug)] +pub(crate) enum ConstVal { + /// A value of the pool. + Value(Value), + /// A declaration record or other table the handler reads from the code. + Record, + /// A constant this slice cannot load. + Unsupported(&'static str), +} + +/// An inline-cache entry (`spec::IcEntry`): shape id 0 never hits. +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct IcEntry { + pub(crate) shape: u32, + pub(crate) slot: u32, +} + +/// A function, loaded into a VM. +#[derive(Debug)] +pub(crate) struct LoadedFn { + pub(crate) code: FunctionCode, + pub(crate) consts: Vec, + pub(crate) keys: Vec>, + /// The index in the VM's table of the first function of this function's + /// program: `NewClosure::func` and the declarations are relative to it. + pub(crate) base: u32, + pub(crate) ics: Vec, + pub(crate) entries: u64, + pub(crate) back_edges: u64, + pub(crate) name_value: Option, +} + +/// A script loaded into a VM. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Script { + /// The index of the script's own function in the VM's code table. + pub main: u32, +} + +/// What the loop does after an op. +pub(crate) enum Flow { + Continue, + Done(Value), +} + +/// The prototypes and strings the VM makes at the start. +#[derive(Debug)] +pub(crate) struct Atoms { + pub(crate) object_proto: Value, + pub(crate) function_proto: Value, + pub(crate) undefined: Value, + pub(crate) object: Value, + pub(crate) boolean: Value, + pub(crate) number: Value, + pub(crate) string: Value, + pub(crate) function: Value, + pub(crate) k_length: Key, + pub(crate) k_name: Key, + pub(crate) k_message: Key, + pub(crate) k_prototype: Key, + pub(crate) k_constructor: Key, +} + +/// Limits of a [`Vm`]. +#[derive(Clone, Copy, Debug)] +pub struct Config { + /// The frame region's slot limit: the depth of JS calls. + pub region_slots: usize, + /// The arena's cell limit. + pub heap_cells: usize, +} + +impl Default for Config { + fn default() -> Self { + Self { + region_slots: 1 << 18, + heap_cells: 1 << 24, + } + } +} + +/// The virtual machine: region, heap, code, and the running frame. +#[derive(Debug)] +pub struct Vm { + pub(crate) heap: Heap, + pub(crate) region: Region, + pub(crate) codes: Vec, + pub(crate) fp: usize, + pub(crate) pc: usize, + pub(crate) cur: usize, + /// The first slot after the running frame. + pub(crate) top: usize, + pub(crate) exception: Value, + pub(crate) global: Value, + /// The global declarative record: name, value (the hole until + /// initialized), and whether it is a constant. + pub(crate) global_lex: HashMap, + /// The `VarNames` of the global environment record. + pub(crate) var_names: HashSet, + /// The native functions, and whether each has `[[Construct]]`. + pub(crate) natives: Vec<(NativeFn, bool)>, + pub(crate) bound: Vec, + pub(crate) intrinsics: [Value; INTRINSICS], + pub(crate) new_target: Value, + pub(crate) active: Value, + /// How many natives are running, one inside another (each re-enters the + /// interpreter or another native on the host stack). + pub(crate) native_depth: u32, + pub(crate) terminator: Terminator, + pub(crate) budget: u64, + pub(crate) atoms: Atoms, + /// The embedder's state (`clazel-js`'s runtime), see [`Vm::set_embedder`]. + pub(crate) embedder: Option>, +} + +impl Vm { + /// A VM with an empty global object. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`] if `config.heap_cells` is too small for the + /// VM's own first cells. + pub fn new(config: Config) -> Result { + let mut heap = Heap::new(config.heap_cells); + let object_proto = heap.new_object(Value::NULL, ObjKind::Plain)?; + let function_proto = heap.new_object(object_proto, ObjKind::Plain)?; + let global = heap.new_object(object_proto, ObjKind::Plain)?; + let atoms = Atoms { + object_proto, + function_proto, + undefined: heap.new_str("undefined")?, + object: heap.new_str("object")?, + boolean: heap.new_str("boolean")?, + number: heap.new_str("number")?, + string: heap.new_str("string")?, + function: heap.new_str("function")?, + k_length: heap.intern_str("length")?, + k_name: heap.intern_str("name")?, + k_message: heap.intern_str("message")?, + k_prototype: heap.intern_str("prototype")?, + k_constructor: heap.intern_str("constructor")?, + }; + // §19.1 Value Properties of the Global Object: `undefined`, `NaN` and + // `Infinity` are not writable, enumerable or configurable. + for (name, v) in [ + ("undefined", Value::UNDEFINED), + ("NaN", Value::double(f64::NAN)), + ("Infinity", Value::double(f64::INFINITY)), + ] { + let k = heap.intern_str(name)?; + heap.define(global, k, v, Attrs(0))?; + } + Ok(Self { + heap, + region: Region::new(config.region_slots), + codes: Vec::new(), + fp: 0, + pc: 0, + cur: 0, + top: 0, + exception: Value::UNDEFINED, + global, + global_lex: HashMap::new(), + var_names: HashSet::new(), + natives: Vec::new(), + bound: Vec::new(), + intrinsics: std::array::from_fn(|i| { + if i == Intrinsic::ObjectPrototype as usize { + object_proto + } else if i == Intrinsic::FunctionPrototype as usize { + function_proto + } else { + Value::UNDEFINED + } + }), + new_target: Value::UNDEFINED, + active: Value::UNDEFINED, + native_depth: 0, + terminator: Terminator::new(), + budget: u64::MAX, + atoms, + embedder: None, + }) + } + + /// The termination flag of this VM; setting it from another thread ends + /// the run at its next back-edge or call. + #[must_use] + pub fn terminator(&self) -> Terminator { + self.terminator.clone() + } + + /// Stores the embedder's own state in the VM, replacing what was there. + /// + /// A [`NativeFn`] is a plain function pointer that gets only `&mut Vm`, so + /// an embedder whose natives are closures (`clazel-js`) keeps the table of + /// closures here and finds it again from inside the native. The VM never + /// looks at it. Dropping the VM drops it. + pub fn set_embedder(&mut self, data: T) { + self.embedder = Some(Box::new(data)); + } + + /// The embedder's state, if it is a `T`. + #[must_use] + pub fn embedder(&self) -> Option<&T> { + self.embedder.as_deref().and_then(|e| e.downcast_ref::()) + } + + /// The embedder's state, mutably, if it is a `T`. + pub fn embedder_mut(&mut self) -> Option<&mut T> { + self.embedder + .as_deref_mut() + .and_then(|e| e.downcast_mut::()) + } + + /// Bounds the back-edges and calls the VM may still make; when spent, a + /// run ends as [`Abrupt::Terminated`]. + pub fn set_budget(&mut self, steps: u64) { + self.budget = steps; + } + + /// The global object. + #[must_use] + pub fn global(&self) -> Value { + self.global + } + + /// The heap, for reading results. + #[must_use] + pub fn heap(&self) -> &Heap { + &self.heap + } + + /// A slot of the frame region, as the JIT reads it (`spec::State::region`). + #[must_use] + pub fn region_slot(&self, i: usize) -> Option { + self.region.get(i).ok() + } + + /// How many times function `code` was entered (jit.md J.10's counter). + #[must_use] + pub fn entries(&self, code: u32) -> Option { + let i = usize::try_from(code).ok()?; + self.codes.get(i).map(|f| f.entries) + } + + /// How many back-edges function `code` took. + #[must_use] + pub fn back_edges(&self, code: u32) -> Option { + let i = usize::try_from(code).ok()?; + self.codes.get(i).map(|f| f.back_edges) + } + + // ----------------------------------------------------------------- + // Loading + // ----------------------------------------------------------------- + + /// Loads a compiled script: checks it ([`check_program`]) and its + /// constant pools become heap values. + /// + /// # Errors + /// [`Abrupt::Internal`] if the program is ill-formed, + /// [`Abrupt::OutOfMemory`] if the heap is full. + pub fn load(&mut self, program: &Program) -> Result { + check_program(program).map_err(Abrupt::Internal)?; + let base = u32::try_from(self.codes.len()).map_err(|_| Abrupt::OutOfMemory)?; + let main = base.checked_add(program.main).ok_or(Abrupt::OutOfMemory)?; + for f in &program.functions { + let loaded = self.load_function(f, base)?; + self.codes.push(loaded); + } + Ok(Script { main }) + } + + fn load_function(&mut self, f: &FunctionCode, base: u32) -> Result { + let mut consts = Vec::with_capacity(f.consts.len()); + let mut keys = Vec::with_capacity(f.consts.len()); + for c in &f.consts { + let (cv, key) = match c { + Const::Number(bits) => ( + ConstVal::Value(number_constant(f64::from_bits(*bits))), + None, + ), + Const::String(units) => { + let s = JsString::from_units(units).map_err(|_| Abrupt::OutOfMemory)?; + let key = self.heap.intern(&s)?; + (ConstVal::Value(self.heap.new_string(s)?), Some(key)) + } + Const::BigInt(_) => (ConstVal::Unsupported("BigInt literals"), None), + Const::RegExp(_) => (ConstVal::Unsupported("RegExp literals"), None), + Const::GlobalDeclarations(_) + | Const::Template(_) + | Const::ArgumentsMap(_) + | Const::EvalScope(_) => (ConstVal::Record, None), + }; + consts.push(cv); + keys.push(key); + } + let nics = usize::try_from(f.nics).map_err(|_| Abrupt::OutOfMemory)?; + Ok(LoadedFn { + code: f.clone(), + consts, + keys, + base, + ics: vec![IcEntry::default(); nics], + entries: 0, + back_edges: 0, + name_value: None, + }) + } + + /// Runs a loaded script with `this` the global object, and returns the + /// value its function returns. + /// + /// # Errors + /// The [`Abrupt`] the run ended with. + pub fn run(&mut self, script: &Script) -> Result { + let global = self.global; + let main = usize::try_from(script.main).map_err(|_| Abrupt::Internal("script index"))?; + let f = self.new_closure(main, Value::UNDEFINED)?; + self.call(f, global, &[]) + } + + // ----------------------------------------------------------------- + // Calls and frames + // ----------------------------------------------------------------- + + /// Calls a function value: a native runs at once; a closure gets a frame + /// on top of the running one and the loop runs until it returns. Natives + /// call back into JS through this. + /// + /// # Errors + /// The [`Abrupt`] the call ended with; a JS exception that escapes the + /// function is [`Abrupt::Throw`]. + pub fn call(&mut self, callee: Value, this: Value, args: &[Value]) -> Result { + let kind = self.heap.obj(callee).map(|o| o.kind); + match kind { + Some(ObjKind::Native(i)) => self.invoke_native(i, callee, this, args, Value::UNDEFINED), + Some(ObjKind::Bound(i)) => { + let b = self.bound_fn(i)?; + let mut all = b.args; + all.extend_from_slice(args); + self.call(b.target, b.this, &all) + } + Some(ObjKind::Closure { code, .. }) => { + let saved = (self.fp, self.pc, self.cur, self.top); + let entered = self.enter_closure(callee, code, this, args, None, KIND_PLAIN); + let result = match entered { + Ok(()) => self.execute(), + Err(e) => Err(e), + }; + (self.fp, self.pc, self.cur, self.top) = saved; + result + } + _ => Err(self.type_error("not a function")), + } + } + + pub(crate) fn native(&self, i: u32) -> Result<(NativeFn, bool), Abrupt> { + usize::try_from(i) + .ok() + .and_then(|i| self.natives.get(i)) + .copied() + .ok_or(Abrupt::Internal("native index")) + } + + /// Runs native `i`, which is function object `callee`, with `new_target` + /// the constructor for a `new` and `undefined` for a call; the previous + /// active function and new target come back afterwards. + pub(crate) fn invoke_native( + &mut self, + i: u32, + callee: Value, + this: Value, + args: &[Value], + new_target: Value, + ) -> Result { + let (f, _) = self.native(i)?; + // Natives nest on the host stack (`join` of an array that holds + // itself): a bound keeps that from overflowing it. + if self.native_depth >= MAX_NATIVE_DEPTH { + return Err(self.range_error("Maximum call stack size exceeded")); + } + let saved = (self.active, self.new_target); + (self.active, self.new_target) = (callee, new_target); + self.native_depth = self.native_depth.saturating_add(1); + let r = f(self, this, args); + self.native_depth = self.native_depth.saturating_sub(1); + (self.active, self.new_target) = saved; + r + } + + /// The function object of the running native: the callee a native does + /// not otherwise know (one native serves several constructors). + #[must_use] + pub fn active_function(&self) -> Value { + self.active + } + + /// `NewTarget` of the running native: `undefined` when it was called, the + /// constructor when it was constructed. + #[must_use] + pub fn new_target(&self) -> Value { + self.new_target + } + + /// An intrinsic of the realm, `undefined` until it is installed. + #[must_use] + pub fn intrinsic(&self, which: Intrinsic) -> Value { + self.intrinsics + .get(which as usize) + .copied() + .unwrap_or(Value::UNDEFINED) + } + + /// Installs an intrinsic. + pub fn set_intrinsic(&mut self, which: Intrinsic, v: Value) { + if let Some(slot) = self.intrinsics.get_mut(which as usize) { + *slot = v; + } + } + + pub(crate) fn code_of(&self, callee: Value) -> Result { + match self.heap.obj(callee).map(|o| o.kind) { + Some(ObjKind::Closure { code, .. }) => { + usize::try_from(code).map_err(|_| Abrupt::Internal("code index")) + } + _ => Err(Abrupt::Internal("frame callee is not a closure")), + } + } + + pub(crate) fn bound_fn(&self, i: u32) -> Result { + usize::try_from(i) + .ok() + .and_then(|i| self.bound.get(i)) + .cloned() + .ok_or(Abrupt::Internal("bound function index")) + } + + fn func(&self, code: usize) -> Result<&LoadedFn, Abrupt> { + self.codes.get(code).ok_or(Abrupt::Internal("code index")) + } + + fn set_frame(&mut self, fp: usize, pc: usize, cur: usize) -> Result<(), Abrupt> { + let f = self.func(cur)?; + let size = + frame_size(f.code.nparams, f.code.nregs).ok_or(Abrupt::Internal("frame size"))?; + let top = fp.checked_add(size).ok_or(Abrupt::Internal("frame top"))?; + (self.fp, self.pc, self.cur, self.top) = (fp, pc, cur, top); + Ok(()) + } + + /// Pushes a frame for closure `callee` (code `code`) on top of the + /// running frame (`spec::Op::Call`'s `pushed_region`), and makes it the + /// running one. `ret` is the caller's call op, or `None` for an entry from + /// outside the loop. + pub(crate) fn enter_closure( + &mut self, + callee: Value, + code: u32, + this: Value, + args: &[Value], + ret: Option, + frame_kind: u32, + ) -> Result<(), Abrupt> { + let code = usize::try_from(code).map_err(|_| Abrupt::Internal("code index"))?; + let (kind, strict, nparams, nregs) = { + let f = self.func(code)?; + (f.code.kind, f.code.strict, f.code.nparams, f.code.nregs) + }; + if frame_kind == KIND_PLAIN && matches!(kind, FunctionKind::Class | FunctionKind::Derived) { + return Err(self.type_error("Class constructor cannot be invoked without 'new'")); + } + // §10.2.1.2 OrdinaryCallBindThis: a sloppy function's `this` is the + // global object for undefined and null; a primitive's wrapper object + // needs the wrapper constructors (not yet). + let this = if strict || kind == FunctionKind::Arrow { + this + } else if matches!( + this.decode(), + clazel_js_heap::value::Decoded::Undefined | clazel_js_heap::value::Decoded::Null + ) { + self.global + } else if self.heap.is_object(this) { + this + } else { + // Step 6.b: ToObject(thisArgument), the wrapper object. + self.to_object(this)? + }; + self.charge()?; + let size = frame_size(nparams, nregs).ok_or(Abrupt::Internal("frame size"))?; + let ret_value = match ret { + Some(pc) => pack_ret(self.fp, pc), + None => Value::raw(RET_NONE), + }; + let Some(ret_value) = ret_value else { + return Err(self.range_error("Maximum call stack size exceeded")); + }; + let argc = u32::try_from(args.len()).unwrap_or(u32::MAX >> 4); + let argc_slot = enc_argc(argc.min(0x0FFF_FFFF), frame_kind) + .ok_or(Abrupt::Internal("argument count"))?; + let base = self.top; + let header = [callee, this, argc_slot, ret_value]; + if self.region.push_frame(base, size, header) == Err(FrameError::Overflow) { + return Err(self.range_error("Maximum call stack size exceeded")); + } + for (i, a) in args + .iter() + .take(usize::try_from(nparams).unwrap_or(0)) + .enumerate() + { + let r = u32::try_from(i).map_err(|_| Abrupt::Internal("argument index"))?; + self.region.set_reg(base, r, *a)?; + } + self.set_frame(base, 0, code)?; + if let Some(f) = self.codes.get_mut(code) { + f.entries = f.entries.saturating_add(1); + } + Ok(()) + } + + /// The termination check (phase-7.md, "Limits and stopping"): at every + /// back-edge and call. + pub(crate) fn charge(&mut self) -> Result<(), Abrupt> { + if self.terminator.is_set() || self.budget == 0 { + return Err(Abrupt::Terminated); + } + self.budget = self.budget.saturating_sub(1); + Ok(()) + } + + // ----------------------------------------------------------------- + // The loop + // ----------------------------------------------------------------- + + /// Runs the running frame (which has no caller in the region) until it + /// returns, unwinding exceptions through the frames above it. + pub(crate) fn execute(&mut self) -> Result { + loop { + match self.step() { + Ok(Flow::Continue) => {} + Ok(Flow::Done(v)) => return Ok(v), + Err(Abrupt::Throw(v)) => { + if !self.unwind(v)? { + return Err(Abrupt::Throw(v)); + } + } + Err(e) => return Err(e), + } + } + } + + /// Unwinds a thrown value: true if a handler caught it (the running frame + /// and `pc` are now the handler's), false if it left the entry frame. + fn unwind(&mut self, exc: Value) -> Result { + loop { + let pc = u32::try_from(self.pc).map_err(|_| Abrupt::Internal("pc"))?; + let target = self + .func(self.cur)? + .code + .handlers + .iter() + .find(|h| h.start <= pc && pc < h.end) + .map(|h| h.target); + if let Some(t) = target { + self.exception = exc; + let target = usize::try_from(t).map_err(|_| Abrupt::Internal("pc"))?; + self.pc = target; + return Ok(true); + } + match self.pop_frame()? { + Some(()) => {} + None => return Ok(false), + } + } + } + + /// Pops the running frame to its caller, at the caller's call op. `None` + /// if the frame has no caller in the region. + fn pop_frame(&mut self) -> Result, Abrupt> { + let ret = unpack_ret(self.region.header(self.fp, HDR_RET)?) + .ok_or(Abrupt::Internal("return address"))?; + match ret { + Ret::None => Ok(None), + Ret::Caller { base, pc } => { + let callee = self.region.header(base, HDR_CALLEE)?; + let code = self.code_of(callee)?; + self.set_frame(base, pc, code)?; + Ok(Some(())) + } + } + } + + fn do_return(&mut self, v: Value) -> Result { + let (_, kind) = crate::frame::dec_argc(self.region.header(self.fp, HDR_ARGC)?) + .ok_or(Abrupt::Internal("argument count slot"))?; + // [[Construct]] §10.2.2 step 10 and 11: a base constructor's result is + // the value returned if it is an object, else `this`. + let v = match kind { + KIND_PLAIN => v, + KIND_CONSTRUCT if self.heap.is_object(v) => v, + KIND_CONSTRUCT => self.region.header(self.fp, crate::frame::HDR_THIS)?, + _ => { + return Err(Abrupt::NotYet("return from a suspendable frame".into())); + } + }; + let ret = unpack_ret(self.region.header(self.fp, HDR_RET)?) + .ok_or(Abrupt::Internal("return address"))?; + match ret { + Ret::None => Ok(Flow::Done(v)), + Ret::Caller { base, pc } => { + let callee = self.region.header(base, HDR_CALLEE)?; + let code = self.code_of(callee)?; + let dst = match self.func(code)?.code.instrs.get(pc) { + Some(Instr::Call { dst, .. } | Instr::New { dst, .. }) => *dst, + _ => return Err(Abrupt::Internal("return address is not a call op")), + }; + self.region.set_reg(base, dst, v)?; + self.set_frame(base, pc.saturating_add(1), code)?; + Ok(Flow::Continue) + } + } + } + + fn fetch(&self) -> Result { + self.func(self.cur)? + .code + .instrs + .get(self.pc) + .cloned() + .ok_or(Abrupt::Internal("pc past the end of the function")) + } + + pub(crate) fn r(&self, r: u32) -> Result { + Ok(self.region.reg(self.fp, r)?) + } + + pub(crate) fn w(&mut self, r: u32, v: Value) -> Result<(), Abrupt> { + Ok(self.region.set_reg(self.fp, r, v)?) + } + + pub(crate) fn konst(&self, k: u32) -> Result { + let i = usize::try_from(k).map_err(|_| Abrupt::Internal("constant index"))?; + self.func(self.cur)? + .consts + .get(i) + .copied() + .ok_or(Abrupt::Internal("constant index")) + } + + pub(crate) fn key_const(&self, k: u32) -> Result { + let i = usize::try_from(k).map_err(|_| Abrupt::Internal("constant index"))?; + self.func(self.cur)? + .keys + .get(i) + .copied() + .flatten() + .ok_or(Abrupt::Internal("property name constant is not a string")) + } + + pub(crate) fn strict(&self) -> bool { + self.codes.get(self.cur).is_some_and(|f| f.code.strict) + } + + /// Runs one op of the running frame. + pub(crate) fn step(&mut self) -> Result { + let ins = self.fetch()?; + let mut next = self.pc.saturating_add(1); + macro_rules! target { + ($t:expr) => { + usize::try_from($t).map_err(|_| Abrupt::Internal("jump target"))? + }; + } + macro_rules! arith { + ($dst:expr, $a:expr, $b:expr, $f:path, $op:expr) => {{ + let (x, y) = (self.r($a)?, self.r($b)?); + let v = match (num(x), num(y)) { + (Some(p), Some(q)) => $f(p, q), + _ => self.arith_slow($op, x, y)?, + }; + self.w($dst, v)?; + }}; + } + macro_rules! compare { + ($dst:expr, $a:expr, $b:expr, $f:path, $op:expr) => {{ + let (x, y) = (self.r($a)?, self.r($b)?); + let v = match (num(x), num(y)) { + (Some(p), Some(q)) => $f(p, q), + _ => self.relational($op, x, y)?, + }; + self.w($dst, Value::boolean(v))?; + }}; + } + match ins { + // --- Core --- + Instr::Nop => {} + Instr::Move { dst, src } => { + let v = self.r(src)?; + self.w(dst, v)?; + } + Instr::LoadConst { dst, k } => match self.konst(k)? { + ConstVal::Value(v) => self.w(dst, v)?, + ConstVal::Unsupported(what) => return Err(Abrupt::NotYet(what.into())), + ConstVal::Record => return Err(Abrupt::Internal("LoadConst of a record")), + }, + Instr::LoadInt { dst, imm } => self.w(dst, Value::int32(imm))?, + Instr::LoadUndefined { dst } => self.w(dst, Value::UNDEFINED)?, + Instr::LoadNull { dst } => self.w(dst, Value::NULL)?, + Instr::LoadBool { dst, flag } => self.w(dst, Value::boolean(flag))?, + Instr::LoadHole { dst } => self.w(dst, Value::HOLE)?, + Instr::GetThis { dst } => { + let v = self.region.header(self.fp, crate::frame::HDR_THIS)?; + self.w(dst, v)?; + } + Instr::GetCallee { dst } => { + let v = self.region.header(self.fp, HDR_CALLEE)?; + self.w(dst, v)?; + } + Instr::GetArgc { dst } => { + let (argc, _) = crate::frame::dec_argc(self.region.header(self.fp, HDR_ARGC)?) + .ok_or(Abrupt::Internal("argument count slot"))?; + let argc = i32::try_from(argc).map_err(|_| Abrupt::Internal("argument count"))?; + self.w(dst, Value::int32(argc))?; + } + Instr::GetException { dst } => { + let v = self.exception; + self.w(dst, v)?; + } + Instr::Jump { target } => next = target!(target), + Instr::JumpIfUndefined { src, target } => { + if self.r(src)? == Value::UNDEFINED { + next = target!(target); + } + } + Instr::JumpIfNotUndefined { src, target } => { + if self.r(src)? != Value::UNDEFINED { + next = target!(target); + } + } + Instr::JumpIfNullish { src, target } => { + let v = self.r(src)?; + if v == Value::UNDEFINED || v == Value::NULL { + next = target!(target); + } + } + Instr::JumpIfNotNullish { src, target } => { + let v = self.r(src)?; + if v != Value::UNDEFINED && v != Value::NULL { + next = target!(target); + } + } + + // --- Guarded: Numbers --- + Instr::Add { dst, a, b } => { + let (x, y) = (self.r(a)?, self.r(b)?); + let v = match (num(x), num(y)) { + (Some(p), Some(q)) => numeric::add(p, q), + _ => self.add_slow(x, y)?, + }; + self.w(dst, v)?; + } + Instr::Sub { dst, a, b } => arith!(dst, a, b, numeric::sub, ArithOp::Sub), + Instr::Mul { dst, a, b } => arith!(dst, a, b, numeric::mul, ArithOp::Mul), + Instr::Div { dst, a, b } => arith!(dst, a, b, numeric::div, ArithOp::Div), + Instr::Mod { dst, a, b } => arith!(dst, a, b, numeric::rem, ArithOp::Mod), + Instr::BitAnd { dst, a, b } => arith!(dst, a, b, numeric::bit_and, ArithOp::BitAnd), + Instr::BitOr { dst, a, b } => arith!(dst, a, b, numeric::bit_or, ArithOp::BitOr), + Instr::BitXor { dst, a, b } => arith!(dst, a, b, numeric::bit_xor, ArithOp::BitXor), + Instr::Shl { dst, a, b } => arith!(dst, a, b, numeric::shl, ArithOp::Shl), + Instr::Shr { dst, a, b } => arith!(dst, a, b, numeric::shr, ArithOp::Shr), + Instr::UShr { dst, a, b } => arith!(dst, a, b, numeric::ushr, ArithOp::UShr), + Instr::Exp { dst, a, b } => { + let (x, y) = (self.r(a)?, self.r(b)?); + let (p, q) = (self.coerce_number(x)?, self.coerce_number(y)?); + self.w( + dst, + Value::double(numeric::exponentiate(p.to_f64(), q.to_f64())), + )?; + } + Instr::Neg { dst, src } => { + let n = self.number_in_reg(src)?; + self.w(dst, numeric::neg(n))?; + } + Instr::Inc { dst, src } => { + let n = self.number_in_reg(src)?; + self.w(dst, numeric::inc(n))?; + } + Instr::Dec { dst, src } => { + let n = self.number_in_reg(src)?; + self.w(dst, numeric::dec(n))?; + } + Instr::BitNot { dst, src } => { + let n = self.number_in_reg(src)?; + self.w(dst, numeric::bit_not(n))?; + } + Instr::Lt { dst, a, b } => compare!(dst, a, b, numeric::lt, Rel::Lt), + Instr::Le { dst, a, b } => compare!(dst, a, b, numeric::le, Rel::Le), + Instr::Gt { dst, a, b } => compare!(dst, a, b, numeric::gt, Rel::Gt), + Instr::Ge { dst, a, b } => compare!(dst, a, b, numeric::ge, Rel::Ge), + Instr::StrictEq { dst, a, b } => { + let (x, y) = (self.r(a)?, self.r(b)?); + let v = self.strict_equals(x, y); + self.w(dst, Value::boolean(v))?; + } + Instr::StrictNe { dst, a, b } => { + let (x, y) = (self.r(a)?, self.r(b)?); + let v = self.strict_equals(x, y); + self.w(dst, Value::boolean(!v))?; + } + Instr::LooseEq { dst, a, b } => { + let (x, y) = (self.r(a)?, self.r(b)?); + let v = self.loose_equals(x, y)?; + self.w(dst, Value::boolean(v))?; + } + Instr::LooseNe { dst, a, b } => { + let (x, y) = (self.r(a)?, self.r(b)?); + let v = self.loose_equals(x, y)?; + self.w(dst, Value::boolean(!v))?; + } + Instr::Not { dst, src } => { + let v = self.r(src)?; + let t = self.to_boolean(v); + self.w(dst, Value::boolean(!t))?; + } + Instr::ToNumeric { dst, src } | Instr::ToNumber { dst, src } => { + let v = self.r(src)?; + let n = self.coerce_number(v)?; + let out = match n { + Num::I(i) => Value::int32(i), + Num::D(d) => Value::double(d), + }; + self.w(dst, out)?; + } + Instr::JumpIfTrue { cond, target } => { + let v = self.r(cond)?; + if self.to_boolean(v) { + next = target!(target); + } + } + Instr::JumpIfFalse { cond, target } => { + let v = self.r(cond)?; + if !self.to_boolean(v) { + next = target!(target); + } + } + Instr::Loop { target } => { + self.charge()?; + if let Some(f) = self.codes.get_mut(self.cur) { + f.back_edges = f.back_edges.saturating_add(1); + } + next = target!(target); + } + Instr::ThrowIfHole { src } => { + if self.r(src)? == Value::HOLE { + return Err( + self.reference_error("Cannot access a binding before initialization") + ); + } + } + + // --- Properties --- + Instr::GetProp { dst, obj, key, ic } => { + let base = self.r(obj)?; + let v = match self.ic_get(base, ic) { + Some(v) => v, + None => { + let key = self.key_const(key)?; + self.get_property(base, key, Some(ic))? + } + }; + self.w(dst, v)?; + } + Instr::SetProp { obj, key, val, ic } => { + let base = self.r(obj)?; + let v = self.r(val)?; + if !self.ic_set(base, ic, v) { + let key = self.key_const(key)?; + self.set_property(base, key, v, Some(ic))?; + } + } + Instr::GetElem { dst, obj, idx } => { + let (base, k) = (self.r(obj)?, self.r(idx)?); + let v = self.get_element(base, k)?; + self.w(dst, v)?; + } + Instr::SetElem { obj, idx, val } => { + let (base, k, v) = (self.r(obj)?, self.r(idx)?, self.r(val)?); + let key = self.coerce_property_key(k)?; + self.set_property(base, key, v, None)?; + } + Instr::DefineProp { obj, key, val } => { + let (o, v) = (self.r(obj)?, self.r(val)?); + let key = self.key_const(key)?; + if self.heap.is_array_object(o) { + self.create_data_property_or_throw(o, key, v)?; + } else { + self.heap.define(o, key, v, Attrs::ALL)?; + } + } + Instr::DefineElem { obj, idx, val } => { + let (o, k, v) = (self.r(obj)?, self.r(idx)?, self.r(val)?); + // An elision is the hole: no property. + if v != Value::HOLE { + let key = self.coerce_property_key(k)?; + if self.heap.is_array_object(o) { + self.create_data_property_or_throw(o, key, v)?; + } else { + self.heap.define(o, key, v, Attrs::ALL)?; + } + } + } + // ArrayCreate(0), §10.4.2.2†: `len` is only a capacity hint (the + // elements are ordinary properties of the shape). + Instr::NewArray { dst, len: _ } => { + let a = self.array_create(0.0, None)?; + self.w(dst, a)?; + } + // `delete obj[idx]`, §13.5.1.2† step 5: ToObject(base), then + // `[[Delete]]`; false is a TypeError in strict code. + Instr::DeleteProp { dst, obj, idx } => { + let (base, k) = (self.r(obj)?, self.r(idx)?); + let o = self.to_object(base)?; + let key = self.coerce_property_key(k)?; + let ok = self.delete_property(o, key)?; + if !ok && self.strict() { + let name = self.key_text(key); + return Err(self.type_error(&format!("Cannot delete property '{name}'"))); + } + self.w(dst, Value::boolean(ok))?; + } + Instr::SetLiteralProto { obj, val } => { + let (o, v) = (self.r(obj)?, self.r(val)?); + if v == Value::NULL || self.heap.is_object(v) { + self.heap + .obj_mut(o) + .ok_or(Abrupt::Internal("SetLiteralProto on a non-object"))? + .proto = v; + } + } + Instr::HasProperty { dst, a, b } => { + let (k, o) = (self.r(a)?, self.r(b)?); + if !self.heap.is_object(o) { + return Err( + self.type_error("Cannot use 'in' operator to search in a non-object") + ); + } + let key = self.coerce_property_key(k)?; + let found = self.heap.find(o, key)?.is_some(); + self.w(dst, Value::boolean(found))?; + } + Instr::NewObject { dst } => { + let proto = self.atoms.object_proto; + let o = self.heap.new_object(proto, ObjKind::Plain)?; + self.w(dst, o)?; + } + + // --- Environments and closures --- + Instr::NewEnv { dst, parent, size } => { + let p = self.r(parent)?; + let n = usize::try_from(size).map_err(|_| Abrupt::OutOfMemory)?; + let e = self.heap.alloc(crate::heap::Cell::Env(crate::heap::Env { + parent: p, + slots: vec![Value::HOLE; n], + }))?; + self.w(dst, e)?; + } + Instr::GetEnvSlot { dst, env, idx } => { + let e = self.r(env)?; + let i = usize::try_from(idx).map_err(|_| Abrupt::Internal("env slot"))?; + let v = self + .heap + .env(e) + .and_then(|e| e.slots.get(i)) + .copied() + .ok_or(Abrupt::Internal("GetEnvSlot on a bad environment"))?; + self.w(dst, v)?; + } + Instr::SetEnvSlot { env, idx, src } => { + let (e, v) = (self.r(env)?, self.r(src)?); + let i = usize::try_from(idx).map_err(|_| Abrupt::Internal("env slot"))?; + *self + .heap + .env_mut(e) + .and_then(|e| e.slots.get_mut(i)) + .ok_or(Abrupt::Internal("SetEnvSlot on a bad environment"))? = v; + } + Instr::GetEnvParent { dst, env } => { + let e = self.r(env)?; + let p = self + .heap + .env(e) + .map(|e| e.parent) + .ok_or(Abrupt::Internal("GetEnvParent on a bad environment"))?; + self.w(dst, p)?; + } + Instr::GetCalleeEnv { dst } => { + let callee = self.region.header(self.fp, HDR_CALLEE)?; + let env = match self.heap.obj(callee).map(|o| o.kind) { + Some(ObjKind::Closure { env, .. }) => env, + _ => return Err(Abrupt::Internal("GetCalleeEnv without a closure")), + }; + self.w(dst, env)?; + } + Instr::NewClosure { dst, func, env } => { + let e = self.r(env)?; + let base = self.func(self.cur)?.base; + let code = base + .checked_add(func) + .ok_or(Abrupt::Internal("function index"))?; + let code = usize::try_from(code).map_err(|_| Abrupt::Internal("function index"))?; + let c = self.new_closure(code, e)?; + self.w(dst, c)?; + } + + // --- Calls --- + Instr::Call { + dst, + callee, + this, + args, + argc, + } => { + let mut f = self.r(callee)?; + let mut t = self.r(this)?; + let mut vals = Vec::with_capacity(usize::try_from(argc).unwrap_or(0)); + for i in 0..argc { + let reg = args + .checked_add(i) + .ok_or(Abrupt::Internal("argument register"))?; + vals.push(self.r(reg)?); + } + // §10.4.1.1 [[Call]] of a bound function: the target, with + // the bound `this` and the bound arguments first. + while let Some(ObjKind::Bound(i)) = self.heap.obj(f).map(|o| o.kind) { + let b = self.bound_fn(i)?; + f = b.target; + t = b.this; + let mut all = b.args; + all.extend_from_slice(&vals); + vals = all; + } + match self.heap.obj(f).map(|o| o.kind) { + Some(ObjKind::Closure { code, .. }) => { + self.enter_closure(f, code, t, &vals, Some(self.pc), KIND_PLAIN)?; + return Ok(Flow::Continue); + } + Some(ObjKind::Native(i)) => { + self.charge()?; + let v = self.invoke_native(i, f, t, &vals, Value::UNDEFINED)?; + self.w(dst, v)?; + } + _ => { + let t = self.type_of(f); + let t = self.string_text(t).unwrap_or_default(); + return Err(self.type_error(&format!("{t} is not a function"))); + } + } + } + Instr::New { + dst, + callee, + args, + argc, + } => { + let f = self.r(callee)?; + let mut vals = Vec::with_capacity(usize::try_from(argc).unwrap_or(0)); + for i in 0..argc { + let reg = args + .checked_add(i) + .ok_or(Abrupt::Internal("argument register"))?; + vals.push(self.r(reg)?); + } + match self.construct(f, &vals, Some(self.pc))? { + // A native constructor finished at once. + Some(v) => self.w(dst, v)?, + // A frame was entered; its return writes `dst`. + None => return Ok(Flow::Continue), + } + } + Instr::Return { src } => { + let v = self.r(src)?; + return self.do_return(v); + } + + // --- Globals --- + Instr::GetGlobal { dst, key, ic: _ } => { + let key = self.key_const(key)?; + let v = self.get_global(key, true)?; + self.w(dst, v)?; + } + Instr::GetGlobalOrUndefined { dst, key } => { + let key = self.key_const(key)?; + let v = self.get_global(key, false)?; + self.w(dst, v)?; + } + Instr::SetGlobal { key, src, ic: _ } => { + let key = self.key_const(key)?; + let v = self.r(src)?; + self.set_global(key, v)?; + } + Instr::DeclareGlobals { k } => self.declare_globals(k)?, + // MakeMethod (§10.2.7†) records [[HomeObject]], which only `super` + // property access reads (`GetSuperBase`, not yet implemented), so + // it is accepted and not stored. + Instr::SetHomeObject { .. } => {} + // DefineMethodProperty (§15.4.5†) for a method (form 0); the + // accessor forms need accessor properties, not yet. + Instr::DefineMethod { + obj, + key, + func, + form: 0, + enumerable, + } => { + let (o, k, f) = (self.r(obj)?, self.r(key)?, self.r(func)?); + let name = self.coerce_string_value(k)?; + let pk = self.coerce_property_key(k)?; + let kn = self.atoms.k_name; + // SetFunctionName(func, key), §10.2.9†. + self.heap.define(f, kn, name, Attrs(Attrs::CONFIGURABLE))?; + let attrs = Attrs( + Attrs::WRITABLE + | Attrs::CONFIGURABLE + | if enumerable { Attrs::ENUMERABLE } else { 0 }, + ); + self.heap.define(o, pk, f, attrs)?; + } + Instr::InitGlobalLexical { key, src } => { + let key = self.key_const(key)?; + let v = self.r(src)?; + match self.global_lex.get_mut(&key) { + Some(entry) => entry.0 = v, + None => { + return Err(Abrupt::Internal("InitGlobalLexical of an undeclared name")); + } + } + } + + // --- Conversions and the rest of the handler ops --- + Instr::TypeOf { dst, src } => { + let v = self.r(src)?; + let s = self.type_of(v); + self.w(dst, s)?; + } + Instr::ToPropertyKey { dst, src } => { + let v = self.r(src)?; + let s = self.coerce_string_value(v)?; + self.w(dst, s)?; + } + Instr::ToString { dst, src } => { + let v = self.r(src)?; + let s = self.coerce_string_value(v)?; + self.w(dst, s)?; + } + Instr::Throw { src } => { + let v = self.r(src)?; + return Err(Abrupt::Throw(v)); + } + Instr::ThrowError { kind, msg } => { + let text = match self.konst(msg)? { + ConstVal::Value(v) => self.heap.string(v).map(JsString::to_string_lossy), + _ => None, + } + .unwrap_or_default(); + return Err(self.throw_kind(kind, &text)); + } + other => return Err(Abrupt::NotYet(op_name(&other))), + } + self.pc = next; + Ok(Flow::Continue) + } +} + +/// The binary operators whose slow path is ToNumeric on both sides. +#[derive(Clone, Copy, Debug)] +pub(crate) enum ArithOp { + Sub, + Mul, + Div, + Mod, + BitAnd, + BitOr, + BitXor, + Shl, + Shr, + UShr, +} + +impl ArithOp { + pub(crate) fn apply(self, a: Num, b: Num) -> Value { + match self { + ArithOp::Sub => numeric::sub(a, b), + ArithOp::Mul => numeric::mul(a, b), + ArithOp::Div => numeric::div(a, b), + ArithOp::Mod => numeric::rem(a, b), + ArithOp::BitAnd => numeric::bit_and(a, b), + ArithOp::BitOr => numeric::bit_or(a, b), + ArithOp::BitXor => numeric::bit_xor(a, b), + ArithOp::Shl => numeric::shl(a, b), + ArithOp::Shr => numeric::shr(a, b), + ArithOp::UShr => numeric::ushr(a, b), + } + } +} + +/// The relational operators. +#[derive(Clone, Copy, Debug)] +pub(crate) enum Rel { + Lt, + Le, + Gt, + Ge, +} + +/// The name of an op, for the "not yet implemented" error. +fn op_name(i: &Instr) -> String { + let dbg = format!("{i:?}"); + dbg.split([' ', '{']).next().unwrap_or("op").to_string() +} + +/// The value of a Number constant (see the module header). +fn number_constant(d: f64) -> Value { + match crate::numeric::narrow(d) { + Num::I(i) => Value::int32(i), + Num::D(d) => Value::double(d), + } +} diff --git a/crates/clazel-js-vm/src/lib.rs b/crates/clazel-js-vm/src/lib.rs index 0a21e50fc..57eb31581 100644 --- a/crates/clazel-js-vm/src/lib.rs +++ b/crates/clazel-js-vm/src/lib.rs @@ -14,14 +14,28 @@ //! the executable form of `spec::code_wf`; //! - [`scope`]: scope analysis, which bindings are captured (item 7.7); //! - [`compile`]: [`compile::compile_script`], the compiler from the AST of -//! `clazel-js-syntax` to bytecode (item 7.7, first and second slice: its +//! `clazel-js-syntax` to bytecode (item 7.7, first to fourth slice: its //! module header lists what it compiles and what it does not yet); +//! - [`module`]: the static record of a compiled module ([`module::ModuleProgram`]), +//! which [`compile::compile_module`] produces and the runtime (7.8) links; //! - [`literal`]: the values of names and literals, which the AST leaves in //! the source text. //! -//! The handlers and the interpreter loop (7.8) come later and refine `step`. +//! The interpreter (item 7.8, first slice) refines `step`: +//! +//! - [`frame`]: the value-stack region and the frame layout of "Shapes fixed +//! here for the JIT"; +//! - [`numeric`]: the fast paths on Numbers, `spec::arith_*` as code; +//! - [`heap`]: the VM's cells (strings, objects, environments) over +//! `clazel-js-heap`'s arena and shapes; +//! - [`interp`]: [`interp::Vm`], the loop, calls, unwinding, the termination +//! check and the tiering counters; +//! - [`handlers`]: the handlers (conversions, operators, properties, globals, +//! closures), plain methods over the region and the heap. +//! //! The crate depends on no browser crate, as phase-7.md's "The engine as a -//! library" requires: only `clazel-js-lexer` and `clazel-js-syntax`. +//! library" requires: `clazel-js-lexer` and `clazel-js-syntax`, and the +//! engine's own `clazel-js-heap` and `clazel-js-number`. //! //! Tier: see `[package.metadata.clazel]` in `Cargo.toml` and //! `docs/VERIFICATION.md`. @@ -29,8 +43,18 @@ pub mod bytecode; pub mod compile; pub mod examples; +pub mod frame; +pub mod handlers; +pub mod heap; +pub mod interp; pub mod lemmas; pub mod literal; +pub mod module; +pub mod numeric; pub mod ops; +pub mod props; pub mod scope; pub mod spec; + +#[cfg(kani)] +mod proofs; diff --git a/crates/clazel-js-vm/src/literal.rs b/crates/clazel-js-vm/src/literal.rs index c6104df97..7dcddbf73 100644 --- a/crates/clazel-js-vm/src/literal.rs +++ b/crates/clazel-js-vm/src/literal.rs @@ -72,6 +72,32 @@ pub fn template_at( } } +/// The body and the flags of the regular expression literal `src[start..end]` +/// (`/body/flags`, §12.9.5†): the text between the first slash and the last, +/// and the text after the last, as UTF-16 code units. The flags contain no +/// slash, so the last slash closes the body even when the body has a `/` in a +/// class. +pub fn regex_at( + src: &[u32], + start: usize, + end: usize, +) -> Result<(Vec, Vec), CompileError> { + let text = src + .get(start..end) + .ok_or(CompileError::Internal("regular expression out of range"))?; + let slash = u32::from(b'/'); + let last = text + .iter() + .rposition(|&c| c == slash) + .ok_or(CompileError::Internal("regular expression without a slash"))?; + if text.first() != Some(&slash) || last == 0 { + return Err(CompileError::Internal("malformed regular expression")); + } + let body = text.get(1..last).unwrap_or(&[]); + let flags = text.get(last.saturating_add(1)..).unwrap_or(&[]); + Ok((to_utf16(body), to_utf16(flags))) +} + /// Code points to UTF-16 code units (§6.1.4: a code point above U+FFFF is a /// surrogate pair; a lone surrogate is kept). pub fn to_utf16(cps: &[u32]) -> Vec { diff --git a/crates/clazel-js-vm/src/module.rs b/crates/clazel-js-vm/src/module.rs new file mode 100644 index 000000000..a5bc8bfca --- /dev/null +++ b/crates/clazel-js-vm/src/module.rs @@ -0,0 +1,599 @@ +//! The static record of a compiled module (phase-7.md item 7.7, fourth slice): +//! what ParseModule (§16.2.1.6.1†) produces from the source text, and what the +//! runtime (7.8) needs to link and evaluate it. +//! +//! # The shape +//! +//! [`ModuleProgram`] is the Source Text Module Record's static fields +//! (§16.2.1.6†) as data, with the compiled code: +//! +//! | Spec field | Here | +//! |---|---| +//! | `[[ECMAScriptCode]]`, `[[Context]]` | [`ModuleProgram::program`]: function 0 is the module body, run with the module environment as its callee environment and `this` undefined | +//! | `[[RequestedModules]]` | [`ModuleProgram::requested_modules`] | +//! | `[[ImportEntries]]` | [`ModuleProgram::import_entries`] | +//! | `[[LocalExportEntries]]` | [`ModuleProgram::local_export_entries`] | +//! | `[[IndirectExportEntries]]` | [`ModuleProgram::indirect_export_entries`] | +//! | `[[StarExportEntries]]` | [`ModuleProgram::star_export_entries`] | +//! | `[[HasTLA]]` | [`ModuleProgram::has_tla`] | +//! | (the module Environment Record's shape) | [`ModuleProgram::bindings`] and [`ModuleProgram::env_size`] | +//! +//! The Cyclic Module Record's dynamic fields (`[[Status]]`, `[[DFSIndex]]`, +//! `[[EvaluationError]]`, the async bookkeeping) and `[[ImportMeta]]` belong to +//! the runtime: this crate does not link or evaluate (7.8 does). The names +//! follow `clazel-js`'s `Module`, `ModuleRequest`, `ImportAttribute` +//! (the embedding API's contract): a request is the specifier and its +//! attributes in source order; a request that two declarations share is one +//! entry (§16.2.1.3† ModuleRequests: same specifier and the same set of +//! attributes), at the position of the first. +//! +//! # Strings +//! +//! Names and specifiers are UTF-16 code units, as the spec's Strings are. The +//! embedding API's `ModuleRequest` holds a Rust `String`: a specifier with a +//! lone surrogate cannot be written there, and [`ModuleRequest::specifier_lossy`] +//! is what the host sees in that one case. +//! +//! # The module environment +//! +//! The runtime creates the module Environment Record when it links the module +//! (InitializeEnvironment §16.2.1.6.4†) as an environment object of +//! [`ModuleProgram::env_size`] slots, and runs the body with it as the callee +//! environment of function 0 ([`crate::bytecode::Instr::GetCalleeEnv`]), so +//! the body and every closure in it reach a binding by its slot +//! ([`crate::bytecode::Instr::GetEnvSlot`]) and other modules reach an export +//! by the slot of its local name. Every binding of the module scope has a slot, +//! so that other modules can name it. Each entry of +//! [`ModuleProgram::bindings`] says what the runtime puts in the slot at +//! InitializeEnvironment time ([`ModuleBindingInit`]); slots that are in no +//! entry belong to the compiler (the module's `this` for its arrow functions) +//! and are written by the first ops of the body. +//! +//! # Where the standard is followed +//! +//! ParseModule steps 7 to 10 (§16.2.1.6.1†: requestedModules, importEntries, +//! the export entry partition), ImportEntries, ExportEntries and +//! ModuleRequests (§16.2.2.2†, §16.2.3.2†, §16.2.1.3†), and HasTLA / ContainsAwait. +//! Section numbers marked † are from memory (phase-7.md's convention). + +use crate::bytecode::Program; +use crate::compile::CompileError; +use crate::literal; +use crate::scope::bound_idents; +use clazel_js_syntax::ast::{Ast, Node, NodeKind, Op as AOp}; + +/// An import attribute (`with { type: "json" }` is `("type", "json")`). +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct ImportAttribute { + /// The attribute's key. + pub key: Vec, + /// Its value. + pub value: Vec, +} + +/// ModuleRequest Record (§16.2.1.3†). +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct ModuleRequest { + /// The module specifier, as written. + pub specifier: Vec, + /// The attributes, in source order. + pub attributes: Vec, +} + +impl ModuleRequest { + /// The specifier as a Rust string, a lone surrogate becoming U+FFFD. + pub fn specifier_lossy(&self) -> String { + String::from_utf16_lossy(&self.specifier) + } + + /// ModuleRequestsEqual (§16.2.1.3†): the same specifier and the same set of + /// attributes, whatever their order. + pub fn same_as(&self, other: &ModuleRequest) -> bool { + self.specifier == other.specifier + && self.attributes.len() == other.attributes.len() + && self + .attributes + .iter() + .all(|a| other.attributes.iter().any(|b| a == b)) + } +} + +/// An ImportEntry's `[[ImportName]]`. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ImportName { + /// A name the imported module exports (`default` for a default import). + Name(Vec), + /// `namespace-object`: the whole module (`import * as ns`). + Namespace, +} + +/// ImportEntry Record (§16.2.1.6†). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ImportEntry { + /// `[[ModuleRequest]]`: an index into [`ModuleProgram::requested_modules`]. + pub module_request: u32, + /// `[[ImportName]]`. + pub import_name: ImportName, + /// `[[LocalName]]`. + pub local_name: Vec, +} + +/// An ExportEntry's `[[ImportName]]`. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ExportImportName { + /// A name of the requested module. + Name(Vec), + /// `all`: the namespace object (`export * as n from`). + All, + /// `all-but-default`: every export but `default` (`export * from`). + AllButDefault, +} + +/// ExportEntry Record (§16.2.1.6†). Which of the fields are present depends on +/// the list the entry is in (local: `local_name`; indirect: `module_request` and +/// `import_name`; star: `module_request` and `import_name`). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ExportEntry { + /// `[[ExportName]]`: null for `export * from`. + pub export_name: Option>, + /// `[[ModuleRequest]]`: an index into [`ModuleProgram::requested_modules`], + /// or null for a local export. + pub module_request: Option, + /// `[[ImportName]]`. + pub import_name: Option, + /// `[[LocalName]]`: a name in [`ModuleProgram::bindings`]. + pub local_name: Option>, +} + +/// What the runtime puts in a slot of the module environment when it links the +/// module (InitializeEnvironment §16.2.1.6.4†). +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ModuleBindingInit { + /// A `var` (step 24: "Perform ! env.InitializeBinding(dn, undefined)"): the + /// value undefined. + Undefined, + /// A `let`, `const` or `class` (and `*default*` of `export default + /// expression`), step 26: a binding in its temporal dead zone: the hole. + Uninitialized, + /// A function declaration (step 27): a closure of the template at this + /// index of [`Program::functions`], over the module environment. + Function(u32), + /// A named or default import (step 8): an indirect binding to what + /// ResolveExport finds for the entry at this index of + /// [`ModuleProgram::import_entries`], read with + /// [`crate::bytecode::Instr::GetImportBinding`]. + Import(u32), + /// A namespace import (step 7): an immutable binding holding the namespace + /// object of the module the entry at this index requests. + Namespace(u32), +} + +/// One binding of the module scope, with its slot. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ModuleBinding { + /// The name (`*default*` for the binding of an anonymous default export). + pub name: Vec, + /// The slot in the module environment object. + pub slot: u32, + /// What link time puts there. + pub init: ModuleBindingInit, +} + +/// A compiled module: see the module header. +#[derive(Clone, Debug, PartialEq)] +pub struct ModuleProgram { + /// The code: function 0 is the module body, of kind + /// [`crate::bytecode::FunctionKind::Module`], async if the module has + /// top-level `await`. + pub program: Program, + /// `[[RequestedModules]]`, in order of first appearance. + pub requested_modules: Vec, + /// `[[ImportEntries]]`. + pub import_entries: Vec, + /// `[[LocalExportEntries]]`. + pub local_export_entries: Vec, + /// `[[IndirectExportEntries]]`. + pub indirect_export_entries: Vec, + /// `[[StarExportEntries]]`. + pub star_export_entries: Vec, + /// `[[HasTLA]]`: the body contains `await` or `for await` outside any + /// function, so it is evaluated as an async function body. + pub has_tla: bool, + /// The number of slots of the module environment object. + pub env_size: u32, + /// The user-visible bindings of the module environment. + pub bindings: Vec, +} + +/// The static entries of a module, from the syntax alone. +pub(crate) struct Entries { + pub requested: Vec, + pub imports: Vec, + pub locals: Vec, + pub indirects: Vec, + pub stars: Vec, +} + +/// The name of the `*default*` binding (§16.2.3.7†: BoundNames of an anonymous +/// default export). +pub(crate) const DEFAULT_LOCAL: &str = "*default*"; + +fn node(ast: &Ast, i: u32) -> Result<&Node, CompileError> { + ast.nodes + .get(i as usize) + .ok_or(CompileError::Internal("node index out of range")) +} + +fn child(nd: &Node, i: usize) -> Result { + nd.children + .get(i) + .copied() + .ok_or(CompileError::Internal("missing child")) +} + +/// The string value of a ModuleExportName, a ModuleSpecifier or an attribute key: +/// an IdentifierName (its StringValue) or a StringLiteral (its SV). +fn export_name_of(ast: &Ast, src: &[u32], n: u32) -> Result, CompileError> { + let nd = node(ast, n)?; + match nd.kind { + NodeKind::StringLiteral => literal::string_at(src, nd.start), + _ => literal::name_units_at(src, nd.start), + } +} + +/// Adds the request of a declaration (`spec`: its StringLiteral, `with`: its +/// WithClause or Absent) to `requested`, and returns its index. +fn request( + ast: &Ast, + src: &[u32], + requested: &mut Vec, + spec: u32, + with: u32, +) -> Result { + let sn = node(ast, spec)?; + let mut attributes = Vec::new(); + let wn = node(ast, with)?; + if wn.kind == NodeKind::WithClause { + for &a in &wn.children { + let an = node(ast, a)?; + let key = export_name_of(ast, src, child(an, 0)?)?; + let value = export_name_of(ast, src, child(an, 1)?)?; + attributes.push(ImportAttribute { key, value }); + } + } + let req = ModuleRequest { + specifier: literal::string_at(src, sn.start)?, + attributes, + }; + let idx = match requested.iter().position(|r| r.same_as(&req)) { + Some(i) => i, + None => { + requested.push(req); + requested.len().saturating_sub(1) + } + }; + u32::try_from(idx).map_err(|_| CompileError::TooLarge("module requests")) +} + +fn units(s: &str) -> Vec { + s.encode_utf16().collect() +} + +/// The BoundNames of the declaration node `d` of an `export`: the names of a +/// variable statement, lexical declaration, function or class. +fn declared_names(ast: &Ast, src: &[u32], d: u32) -> Result>, CompileError> { + let dn = node(ast, d)?; + let mut targets: Vec = Vec::new(); + match dn.kind { + NodeKind::VariableStatement + | NodeKind::VariableDeclarationList + | NodeKind::LexicalDeclaration => { + for &b in &dn.children { + targets.push(child(node(ast, b)?, 0)?); + } + } + _ => targets.push(child(dn, 0)?), + } + let mut out = Vec::new(); + for t in targets { + for id in bound_idents(ast, t)? { + out.push(literal::name_units_at(src, node(ast, id)?.start)?); + } + } + Ok(out) +} + +/// ModuleRequests, ImportEntries and ExportEntries of the module body, and +/// ParseModule's partition of the export entries (§16.2.1.6.1† steps 8 to 10). +pub(crate) fn entries(ast: &Ast, src: &[u32]) -> Result { + let root = node(ast, ast.root)?; + let mut requested: Vec = Vec::new(); + let mut imports: Vec = Vec::new(); + let mut exports: Vec = Vec::new(); + for &item in &root.children { + let it = node(ast, item)?; + match it.kind { + NodeKind::ImportDeclaration => { + let (clause, spec, with) = (child(it, 0)?, child(it, 1)?, child(it, 2)?); + let r = request(ast, src, &mut requested, spec, with)?; + let cn = node(ast, clause)?; + if cn.kind != NodeKind::ImportClause { + continue; + } + let local_of = |b: u32| -> Result, CompileError> { + literal::name_units_at(src, node(ast, b)?.start) + }; + let default = child(cn, 0)?; + if node(ast, default)?.kind == NodeKind::BindingIdentifier { + imports.push(ImportEntry { + module_request: r, + import_name: ImportName::Name(units("default")), + local_name: local_of(default)?, + }); + } + let named = child(cn, 1)?; + let nn = node(ast, named)?; + match nn.kind { + NodeKind::NameSpaceImport => { + imports.push(ImportEntry { + module_request: r, + import_name: ImportName::Namespace, + local_name: local_of(child(nn, 0)?)?, + }); + } + NodeKind::NamedImports => { + for &s in &nn.children { + let sn = node(ast, s)?; + let (imported, binding) = (child(sn, 0)?, child(sn, 1)?); + let local = local_of(binding)?; + let name = if node(ast, imported)?.kind == NodeKind::Absent { + local.clone() + } else { + export_name_of(ast, src, imported)? + }; + imports.push(ImportEntry { + module_request: r, + import_name: ImportName::Name(name), + local_name: local, + }); + } + } + _ => {} + } + } + NodeKind::ExportDeclaration => match it.op { + AOp::ExportAll => { + let r = request(ast, src, &mut requested, child(it, 0)?, child(it, 1)?)?; + exports.push(ExportEntry { + export_name: None, + module_request: Some(r), + import_name: Some(ExportImportName::AllButDefault), + local_name: None, + }); + } + AOp::ExportAllAs => { + let r = request(ast, src, &mut requested, child(it, 1)?, child(it, 2)?)?; + exports.push(ExportEntry { + export_name: Some(export_name_of(ast, src, child(it, 0)?)?), + module_request: Some(r), + import_name: Some(ExportImportName::All), + local_name: None, + }); + } + AOp::ExportFrom => { + let r = request(ast, src, &mut requested, child(it, 1)?, child(it, 2)?)?; + let ne = node(ast, child(it, 0)?)?; + for &s in &ne.children { + let sn = node(ast, s)?; + let name = export_name_of(ast, src, child(sn, 0)?)?; + let exported = child(sn, 1)?; + let export_name = if node(ast, exported)?.kind == NodeKind::Absent { + name.clone() + } else { + export_name_of(ast, src, exported)? + }; + exports.push(ExportEntry { + export_name: Some(export_name), + module_request: Some(r), + import_name: Some(ExportImportName::Name(name)), + local_name: None, + }); + } + } + AOp::ExportNamed => { + let ne = node(ast, child(it, 0)?)?; + for &s in &ne.children { + let sn = node(ast, s)?; + let name = export_name_of(ast, src, child(sn, 0)?)?; + let exported = child(sn, 1)?; + let export_name = if node(ast, exported)?.kind == NodeKind::Absent { + name.clone() + } else { + export_name_of(ast, src, exported)? + }; + exports.push(ExportEntry { + export_name: Some(export_name), + module_request: None, + import_name: None, + local_name: Some(name), + }); + } + } + AOp::ExportVar | AOp::ExportDecl => { + for name in declared_names(ast, src, child(it, 0)?)? { + exports.push(ExportEntry { + export_name: Some(name.clone()), + module_request: None, + import_name: None, + local_name: Some(name), + }); + } + } + AOp::ExportDefaultHoistable | AOp::ExportDefaultClass => { + let d = node(ast, child(it, 0)?)?; + let nm = child(d, 0)?; + let local = if node(ast, nm)?.kind == NodeKind::BindingIdentifier { + literal::name_units_at(src, node(ast, nm)?.start)? + } else { + units(DEFAULT_LOCAL) + }; + exports.push(ExportEntry { + export_name: Some(units("default")), + module_request: None, + import_name: None, + local_name: Some(local), + }); + } + AOp::ExportDefaultExpression => { + exports.push(ExportEntry { + export_name: Some(units("default")), + module_request: None, + import_name: None, + local_name: Some(units(DEFAULT_LOCAL)), + }); + } + _ => return Err(CompileError::Internal("unknown export declaration")), + }, + _ => {} + } + } + // §16.2.1.6.1† steps 9 and 10: the partition. + let mut locals = Vec::new(); + let mut indirects = Vec::new(); + let mut stars = Vec::new(); + for ee in exports { + match ee.module_request { + None => { + let imported = imports + .iter() + .find(|ie| Some(&ie.local_name) == ee.local_name.as_ref()); + match imported { + None => locals.push(ee), + Some(ie) => match &ie.import_name { + // Re-exporting a namespace import is a local export of + // its (immutable) binding. + ImportName::Namespace => locals.push(ee), + ImportName::Name(n) => indirects.push(ExportEntry { + export_name: ee.export_name, + module_request: Some(ie.module_request), + import_name: Some(ExportImportName::Name(n.clone())), + local_name: None, + }), + }, + } + } + Some(_) => { + if ee.import_name == Some(ExportImportName::AllButDefault) { + stars.push(ee); + } else { + indirects.push(ee); + } + } + } + } + Ok(Entries { + requested, + imports, + locals, + indirects, + stars, + }) +} + +/// The executable form of the record's consistency conditions, as +/// [`crate::bytecode::check_program`] is for the code: every request index is +/// in range, slots are below `env_size` and distinct, function and import-entry +/// references are in range, every local export names a binding, and the +/// body is function 0 of kind Module. +pub fn check_module(m: &ModuleProgram) -> Result<(), &'static str> { + crate::bytecode::check_program(&m.program)?; + if m.program.main != 0 { + return Err("the module body is not function 0"); + } + match m.program.functions.first() { + Some(f) if f.kind == crate::bytecode::FunctionKind::Module => {} + _ => return Err("function 0 is not a module body"), + } + let nreq = m.requested_modules.len(); + let req_ok = |r: u32| (r as usize) < nreq; + for ie in &m.import_entries { + if !req_ok(ie.module_request) { + return Err("an import entry's request is out of range"); + } + } + for (i, a) in m.requested_modules.iter().enumerate() { + if m.requested_modules + .iter() + .skip(i.saturating_add(1)) + .any(|b| a.same_as(b)) + { + return Err("a module request is listed twice"); + } + } + let mut seen_slots = std::collections::HashSet::new(); + let mut seen_names = std::collections::HashSet::new(); + for b in &m.bindings { + if b.slot >= m.env_size || !seen_slots.insert(b.slot) { + return Err("a binding's slot is out of range or shared"); + } + if !seen_names.insert(b.name.clone()) { + return Err("a binding is listed twice"); + } + match &b.init { + ModuleBindingInit::Function(f) if (*f as usize) >= m.program.functions.len() => { + return Err("a hoisted function is out of range"); + } + ModuleBindingInit::Import(e) | ModuleBindingInit::Namespace(e) + if (*e as usize) >= m.import_entries.len() => + { + return Err("a binding's import entry is out of range"); + } + _ => {} + } + } + for e in &m.import_entries { + if !m.bindings.iter().any(|b| b.name == e.local_name) { + return Err("an import entry has no binding"); + } + } + for e in &m.local_export_entries { + let ok = e.module_request.is_none() + && e.export_name.is_some() + && e.local_name + .as_ref() + .is_some_and(|n| m.bindings.iter().any(|b| &b.name == n)); + if !ok { + return Err("a local export names no binding"); + } + } + for e in m + .indirect_export_entries + .iter() + .chain(&m.star_export_entries) + { + let ok = e.module_request.is_some_and(req_ok) + && e.import_name.is_some() + && e.local_name.is_none(); + if !ok { + return Err("an indirect or star export is malformed"); + } + } + for e in &m.star_export_entries { + if e.export_name.is_some() || e.import_name != Some(ExportImportName::AllButDefault) { + return Err("a star export is malformed"); + } + } + let mut exported = std::collections::HashSet::new(); + for e in m + .local_export_entries + .iter() + .chain(&m.indirect_export_entries) + { + if let Some(n) = &e.export_name + && !exported.insert(n.clone()) + { + return Err("an export name is exported twice"); + } + } + Ok(()) +} diff --git a/crates/clazel-js-vm/src/numeric.rs b/crates/clazel-js-vm/src/numeric.rs new file mode 100644 index 000000000..153ba36f8 --- /dev/null +++ b/crates/clazel-js-vm/src/numeric.rs @@ -0,0 +1,310 @@ +//! The fast paths on Numbers: `spec::arith_*`, the bitwise and shift ops and +//! the relational ops of [`crate::spec`], written as executable code over +//! [`Value`]. +//! +//! The encoding rule is the spec's ("int32 and double encodings of one Number +//! are both Numbers"): an arithmetic op returns an int32 exactly when both +//! operands are int32 and the exact result is an int32 and not `-0`, and a +//! double otherwise; the bitwise ops return an int32, `>>>` the int32 when +//! the result fits and a double otherwise, `/` always a double. + +use clazel_js_heap::value::{Decoded, Value}; + +/// A Number operand, in the encoding it has in a register. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Num { + /// An int32 value. + I(i32), + /// A double value. + D(f64), +} + +impl Num { + /// The Number as a double (`spec::num_bits`). + #[must_use] + pub fn to_f64(self) -> f64 { + match self { + Num::I(i) => f64::from(i), + Num::D(d) => d, + } + } +} + +/// The Number a value is, if it is one (`spec::is_number`). +#[must_use] +pub fn num(v: Value) -> Option { + match v.decode() { + Decoded::Int32(i) => Some(Num::I(i)), + Decoded::Double(d) => Some(Num::D(d)), + _ => None, + } +} + +/// The value for an integral result: an int32 when it fits, else the double +/// (`spec::enc_number_int`, for `|n| < 2^53`). +#[must_use] +pub fn number_from_i64(n: i64) -> Value { + match i32::try_from(n) { + Ok(i) => Value::int32(i), + // Exact: callers pass |n| < 2^53. + Err(_) => Value::double(n as f64), + } +} + +/// The Number `d` as an int32 encoding when it is an integer in range other +/// than `-0` (the same Number, in the encoding the arithmetic ops keep +/// narrow), else a double. +#[must_use] +pub fn narrow(d: f64) -> Num { + if d == d.trunc() && d.abs() < 2_147_483_648.0 && !(d == 0.0 && d.is_sign_negative()) { + // Integral, in range and not -0: exact. + Num::I(d as i32) + } else { + Num::D(d) + } +} + +/// `a + b` (`spec::arith_add`). +#[must_use] +pub fn add(a: Num, b: Num) -> Value { + if let (Num::I(x), Num::I(y)) = (a, b) + && let Some(r) = x.checked_add(y) + { + return Value::int32(r); + } + Value::double(a.to_f64() + b.to_f64()) +} + +/// `a - b` (`spec::arith_sub`). +#[must_use] +pub fn sub(a: Num, b: Num) -> Value { + if let (Num::I(x), Num::I(y)) = (a, b) + && let Some(r) = x.checked_sub(y) + { + return Value::int32(r); + } + Value::double(a.to_f64() - b.to_f64()) +} + +/// `a * b` (`spec::arith_mul`): a zero product with a negative operand is +/// `-0`, which no int32 is. +#[must_use] +pub fn mul(a: Num, b: Num) -> Value { + if let (Num::I(x), Num::I(y)) = (a, b) + && let Some(r) = x.checked_mul(y) + && !(r == 0 && (x < 0 || y < 0)) + { + return Value::int32(r); + } + Value::double(a.to_f64() * b.to_f64()) +} + +/// `a / b` (`spec::arith_div`): always a double. +#[must_use] +pub fn div(a: Num, b: Num) -> Value { + Value::double(a.to_f64() / b.to_f64()) +} + +/// `a % b` (`spec::arith_mod`): the int32 remainder unless the divisor is +/// zero or the result is a zero from a negative dividend (`-0`); a double is +/// C's `fmod`, which is Rust's `%` on `f64`. +#[must_use] +pub fn rem(a: Num, b: Num) -> Value { + if let (Num::I(x), Num::I(y)) = (a, b) + && let Some(r) = x.checked_rem(y) + && !(r == 0 && x < 0) + { + return Value::int32(r); + } + Value::double(a.to_f64() % b.to_f64()) +} + +/// `-a` (`spec::arith_neg`): `-0` is not an int32 and `-(-2^31)` does not fit. +#[must_use] +pub fn neg(a: Num) -> Value { + if let Num::I(x) = a + && x != 0 + && let Some(r) = x.checked_neg() + { + return Value::int32(r); + } + Value::double(-a.to_f64()) +} + +/// `a + 1` (`spec::arith_inc`). +#[must_use] +pub fn inc(a: Num) -> Value { + add(a, Num::I(1)) +} + +/// `a - 1` (`spec::arith_dec`). +#[must_use] +pub fn dec(a: Num) -> Value { + sub(a, Num::I(1)) +} + +/// ToInt32, §7.1.6 (`spec::to_int32`). +#[must_use] +pub fn to_int32(a: Num) -> i32 { + match a { + Num::I(i) => i, + Num::D(d) => { + if !d.is_finite() || d == 0.0 { + return 0; + } + // The integral part modulo 2^32, exactly (a remainder of integers + // below 2^53 in magnitude), as an unsigned 32-bit pattern. + let m = d.trunc().rem_euclid(4_294_967_296.0); + // 0 <= m < 2^32, so the conversion is exact. + (m as u32) as i32 + } + } +} + +/// ToUint32, §7.1.7 (`spec::to_uint32`). +#[must_use] +pub fn to_uint32(a: Num) -> u32 { + to_int32(a) as u32 +} + +/// `a & b`. +#[must_use] +pub fn bit_and(a: Num, b: Num) -> Value { + Value::int32(to_int32(a) & to_int32(b)) +} + +/// `a | b`. +#[must_use] +pub fn bit_or(a: Num, b: Num) -> Value { + Value::int32(to_int32(a) | to_int32(b)) +} + +/// `a ^ b`. +#[must_use] +pub fn bit_xor(a: Num, b: Num) -> Value { + Value::int32(to_int32(a) ^ to_int32(b)) +} + +/// `a << b`: the shift count is `ToUint32(b) mod 32`. +#[must_use] +pub fn shl(a: Num, b: Num) -> Value { + let n = to_uint32(b) & 31; + Value::int32(to_int32(a).wrapping_shl(n)) +} + +/// `a >> b`, sign-extending. +#[must_use] +pub fn shr(a: Num, b: Num) -> Value { + let n = to_uint32(b) & 31; + Value::int32(to_int32(a).wrapping_shr(n)) +} + +/// `a >>> b`, zero-filling: the int32 when the result fits, else a double. +#[must_use] +pub fn ushr(a: Num, b: Num) -> Value { + let n = to_uint32(b) & 31; + number_from_i64(i64::from(to_uint32(a).wrapping_shr(n))) +} + +/// `~a`. +#[must_use] +pub fn bit_not(a: Num) -> Value { + Value::int32(!to_int32(a)) +} + +/// `a < b` on Numbers (`spec::rel_lt`): false when either is NaN. +#[must_use] +pub fn lt(a: Num, b: Num) -> bool { + a.to_f64() < b.to_f64() +} + +/// `a <= b` (`spec::rel_le`). +#[must_use] +pub fn le(a: Num, b: Num) -> bool { + a.to_f64() <= b.to_f64() +} + +/// `a > b` (`spec::rel_gt`). +#[must_use] +pub fn gt(a: Num, b: Num) -> bool { + a.to_f64() > b.to_f64() +} + +/// `a >= b` (`spec::rel_ge`). +#[must_use] +pub fn ge(a: Num, b: Num) -> bool { + a.to_f64() >= b.to_f64() +} + +/// Number::equal, §6.1.6.1.13: `-0` equals `+0`, NaN equals nothing. +#[must_use] +pub fn equal(a: Num, b: Num) -> bool { + a.to_f64() == b.to_f64() +} + +/// Number::exponentiate, §6.1.6.1.3. +#[must_use] +pub fn exponentiate(base: f64, exponent: f64) -> f64 { + if exponent.is_nan() { + return f64::NAN; + } + if exponent == 0.0 { + return 1.0; + } + // Step 6 and 9: a base of magnitude 1 with an infinite exponent is NaN + // (`powf` gives 1). + if exponent.is_infinite() && base.abs() == 1.0 { + return f64::NAN; + } + base.powf(exponent) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn int32_overflow_goes_to_double() { + let max = Num::I(i32::MAX); + assert_eq!(add(max, Num::I(1)), Value::double(2_147_483_648.0)); + assert_eq!( + sub(Num::I(i32::MIN), Num::I(1)), + Value::double(-2_147_483_649.0) + ); + assert_eq!(mul(max, Num::I(2)), Value::double(4_294_967_294.0)); + assert_eq!(neg(Num::I(i32::MIN)), Value::double(2_147_483_648.0)); + assert_eq!(add(Num::I(2), Num::I(3)), Value::int32(5)); + } + + #[test] + fn negative_zero_is_a_double() { + assert_eq!(mul(Num::I(0), Num::I(-5)), Value::double(-0.0)); + assert_eq!(mul(Num::I(-5), Num::I(0)), Value::double(-0.0)); + assert_eq!(mul(Num::I(0), Num::I(5)), Value::int32(0)); + assert_eq!(neg(Num::I(0)), Value::double(-0.0)); + assert_eq!(rem(Num::I(-4), Num::I(2)), Value::double(-0.0)); + assert_eq!(rem(Num::I(i32::MIN), Num::I(-1)), Value::double(-0.0)); + assert_eq!(rem(Num::I(5), Num::I(0)), Value::double(f64::NAN)); + } + + #[test] + fn the_bitwise_ops_follow_to_int32() { + assert_eq!(to_int32(Num::D(4_294_967_296.0 + 5.0)), 5); + assert_eq!(to_int32(Num::D(-1.5)), -1); + assert_eq!(to_int32(Num::D(f64::NAN)), 0); + assert_eq!(to_int32(Num::D(2_147_483_648.0)), i32::MIN); + assert_eq!(shl(Num::I(1), Num::I(33)), Value::int32(2)); + assert_eq!(shr(Num::I(-8), Num::I(1)), Value::int32(-4)); + assert_eq!(ushr(Num::I(-1), Num::I(0)), Value::double(4_294_967_295.0)); + assert_eq!(ushr(Num::I(-1), Num::I(1)), Value::int32(i32::MAX)); + assert_eq!(bit_not(Num::I(0)), Value::int32(-1)); + } + + #[test] + #[cfg_attr(miri, ignore = "Miri deliberately perturbs libm results such as powf")] + fn exponentiation_special_cases() { + assert!(exponentiate(1.0, f64::INFINITY).is_nan()); + assert_eq!(exponentiate(f64::NAN, 0.0), 1.0); + assert_eq!(exponentiate(2.0, 10.0), 1024.0); + } +} diff --git a/crates/clazel-js-vm/src/ops.rs b/crates/clazel-js-vm/src/ops.rs index 0be27b43f..0642cb322 100644 --- a/crates/clazel-js-vm/src/ops.rs +++ b/crates/clazel-js-vm/src/ops.rs @@ -247,6 +247,50 @@ pub enum HandlerId { ForInIterator, /// InitializeBinding on the global declarative record, §9.1.1.4.2†. InitGlobalLexical, + /// RegExpCreate §22.2.3.1†, for a regular expression literal (§13.2.7.3†). + NewRegExp, + /// EvaluateGeneratorBody §15.5.2† steps 2 to 4 and GeneratorStart §27.5.3.1†, and the same for + /// async generators (§15.6.2†). + GeneratorStart, + /// GeneratorYield §27.5.3.6† of an iterator result object that exists already, for `yield*` + /// (§15.5.5†). + YieldDelegate, + /// GetIterator §7.4.4† with the hint async, and CreateAsyncFromSyncIterator §27.1.4.1†. + GetAsyncIterator, + /// A call of an iterator's `next`, `throw` or `return` (§15.5.5†, §7.4.6†, §7.4.11†, §7.4.13†). + IterCallMethod, + /// The checks and IteratorComplete §7.4.7† of an iterator result, as `yield*` and `for await` + /// make them. + IterComplete, + /// IteratorValue §7.4.8†. + IterValue, + /// The check of the result of an iterator's `return`, §7.4.11† and §7.4.13†. + ThrowIfNotObject, + /// ToObject §7.1.18†. + ToObject, + /// HasBinding of an object Environment Record, §9.1.1.2.1†. + WithHas, + /// GetBindingValue of an object Environment Record, §9.1.1.2.6†. + WithGet, + /// SetMutableBinding of an object Environment Record, §9.1.1.2.5†. + WithSet, + /// DeleteBinding of an object Environment Record, §9.1.1.2.7†. + WithDelete, + /// `delete` of an identifier that resolves to the global environment, §13.5.1.2† and + /// DeleteBinding §9.1.1.4.7†. + DeleteGlobal, + /// A call of `eval` that may be a direct eval: EvaluateCall §13.3.6.2†, PerformEval §19.2.1.1† + /// and EvalDeclarationInstantiation §19.2.1.3†, through the host's compile path. + DirectEval, + /// GetBindingValue of a Module Environment Record's indirect binding, §9.1.1.5.1†. + GetImportBinding, + /// EvaluateImportCall §13.3.10.1†: `import(specifier, options)`, handing the request to + /// HostLoadImportedModule §16.2.1.8†. + ImportCall, + /// `import.meta`, §13.3.12.1†, with HostGetImportMetaProperties and HostFinalizeImportMeta. + ImportMeta, + /// As [`HandlerId::DirectEval`], for a call whose arguments were spread into an array. + DirectEvalSpread, } /// The error constructors a compiler-known throw can name. @@ -1334,6 +1378,360 @@ pub enum Op { /// The register read. src: nat, }, + /// `dst := RegExpCreate(pattern, flags)` for the literal whose record is `consts[k]` (its + /// pattern source text and its flags). RegularExpressionLiteral evaluation (§13.2.7.3†): "Let + /// pattern be CodePointsAsString of the BodyText" (step 1), "Let flags be CodePointsAsString of + /// the FlagText" (step 2), "Return ! RegExpCreate(pattern, flags)" (step 3). Every evaluation + /// creates a new RegExp object, so a literal in a loop gives a new object each time and the + /// record is shared. The `!` is the parser's early error (§13.2.7.2†: the body is a valid + /// Pattern for the flags and the flags are valid), so the handler never sees a pattern it + /// cannot compile; this crate does not compile the pattern, the regular expression engine does, + /// when the heap loads the record. It allocates, so it is always a handler call. + NewRegExp { + /// The register written. + dst: nat, + /// The constant-pool index of the literal's record (pattern text and flags). + k: nat, + }, + /// The end of FunctionDeclarationInstantiation in a generator or async generator function, + /// where the function returns its generator object and its body is left to the first `next()`. + /// EvaluateGeneratorBody (§15.5.2†) is: "Perform ? + /// FunctionDeclarationInstantiation(functionObject, argumentsList)" (step 1), "Let G be ? + /// OrdinaryCreateFromConstructor(functionObject, "%GeneratorFunction.prototype.prototype%", + /// ...)" (step 2), "Perform GeneratorStart(G, FunctionBody)" (step 3), "Return Completion + /// Record { [[Type]]: return, [[Value]]: G }" (step 4); EvaluateAsyncGeneratorBody (§15.6.2†) + /// is the same with the async generator intrinsics and AsyncGeneratorStart (§27.6.3.1†), whose + /// queue starts empty. The ops before this one (the parameters and the declarations of the + /// function) are step 1, so an error in them is thrown to the caller. The handler creates G + /// with the prototype `functionObject.prototype` when that is an Object and the realm's + /// intrinsic otherwise, copies the frame to G (phase-7.md 7.7: frames are copied to a heap + /// object on suspend and back on resume) in the state suspended-start, and returns G as the + /// call's result. The next op is the first of the body and runs on the first resumption + /// (GeneratorResume §27.5.3.3†, whose sent value of a first `next(v)` is dropped). `return()` + /// and `throw()` of a generator in suspended-start complete it without running the frame + /// (GeneratorResumeAbrupt §27.5.3.4† step 3.a), so this suspension is never resumed with a + /// mode. An async function has no such op: its body runs at once in the frame of the call + /// (AsyncFunctionStart §27.7.5.1†), and its promise is settled when the function returns or + /// throws (§27.7.5.2†); the handler of a call knows to do that from the function's kind. + GeneratorStart, + /// As [`Op::Yield`], but `src` is already an iterator result object, and the consumer of the + /// generator receives that object as it is instead of a new one holding a value. It is the + /// suspension of `yield*` in a generator: "Set received to + /// Completion(GeneratorYield(innerResult))" (YieldExpression : `yield *` AssignmentExpression + /// evaluation, §15.5.5† step 7.a.vi), where GeneratorYield (§27.5.3.6†) takes the iterator + /// result object itself and Yield (§27.5.3.7†) is what wraps a value in CreateIterResultObject. + /// In an async generator `yield*` gives `IteratorValue(innerResult)` to [`Op::Yield`] (step + /// 7.a.vii: AsyncGeneratorYield) instead. `value` and `mode` are as for [`Op::Yield`]: the + /// resumption's value and its mode (0 next, 1 throw, 2 return). + YieldDelegate { + /// The register holding the iterator result object. + src: nat, + /// The register receiving the value of the resumption. + value: nat, + /// The register receiving the resume mode, an int32. + mode: nat, + }, + /// `dst := GetIterator(src, async)`, §7.4.4†: "Let method be ? GetMethod(obj, @@asyncIterator)" + /// (step 1.a.i); if that is undefined, "Let syncMethod be ? GetMethod(obj, @@iterator)" (step + /// 1.a.ii), "Let syncIteratorRecord be ? GetIteratorFromMethod(obj, syncMethod)" and "Return + /// CreateAsyncFromSyncIterator(syncIteratorRecord)" (§27.1.4.1†, step 1.a.ii.3); an object + /// without either method is a TypeError. The result is an Iterator Record, as + /// [`Op::GetIterator`]'s, for the ops of a `for await` loop and of `yield*` in an async + /// generator ([`Op::IterCallMethod`], [`Op::IterComplete`], [`Op::IterValue`]). + GetAsyncIterator { + /// The register written. + dst: nat, + /// The register read. + src: nat, + }, + /// Calls a method of the Iterator Record in `iter` on its iterator object, and gives the result + /// as it is: it does not check it and does not await it (the compiler follows with `Await` in + /// an async loop, then with [`Op::IterComplete`] or [`Op::ThrowIfNotObject`]). `which` 0 is the + /// stored `next` method called with `arg` (`yield*`, §15.5.5† step 7.a.i: + /// "Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]], « received.[[Value]] »)"); + /// `which` 4 is the same with no arguments (IteratorNext §7.4.6† step 1: "If value is not + /// present, Call(...)", as a `for`-`of` loop does, which `arg` is then ignored for). `which` 1 + /// is `GetMethod(iterator, "throw")` (§7.3.10†) and, if it is not undefined, a call with `arg` + /// (step 7.b.i to 7.b.ii.1); `which` 2 is `GetMethod(iterator, "return")` and a call with `arg` + /// (step 7.c.ii and 7.c.iv); `which` 3 is the same `return` call with no arguments + /// (IteratorClose §7.4.11† steps 4 to 6 and AsyncIteratorClose §7.4.13†). `found` receives + /// true, and `dst` the call's result; but for `which` 1 to 3 when GetMethod gives undefined (it + /// gives undefined for null as well), `found` receives false and `dst` is not written. A next + /// method that is not callable is a TypeError of the call; a throw of the call or of GetMethod + /// is the op's. + IterCallMethod { + /// The register receiving the call's result. + dst: nat, + /// The register receiving whether the method was there. + found: nat, + /// The register holding the iterator record. + iter: nat, + /// The register holding the argument. + arg: nat, + /// The method: 0 `next` with an argument, 1 `throw`, 2 `return` with an argument, 3 + /// `return` without, 4 `next` without. + which: nat, + }, + /// `done := IteratorComplete(result)`, §7.4.7†, after the check that `result` is an Object: "If + /// innerResult is not an Object, throw a TypeError exception" (`yield*`, §15.5.5† step 7.a.iv) + /// and "If nextResult is not an Object, throw a TypeError exception" (ForIn/OfBodyEvaluation + /// §14.7.5.7† step 6.c.iii), then ToBoolean(? Get(result, "done")). It reads `done` and nothing + /// else: `value` is read by [`Op::IterValue`], only when the loop uses it. + IterComplete { + /// The register receiving the flag, a boolean. + done: nat, + /// The register holding the iterator result. + result: nat, + }, + /// `dst := IteratorValue(result)`, §7.4.8†: `Get(result, "value")`. `result` is an Object + /// already, which [`Op::IterComplete`] checked. + IterValue { + /// The register written. + dst: nat, + /// The register holding the iterator result. + result: nat, + }, + /// Throws a TypeError if `src` is not an Object: the check of what an iterator's `return` + /// method gave, "If innerResult.[[Value]] is not an Object, throw a TypeError exception" + /// (IteratorClose §7.4.11† step 9, AsyncIteratorClose §7.4.13† step 7), which reads none of its + /// properties. It throws, so it is a handler op. + ThrowIfNotObject { + /// The register read. + src: nat, + }, + /// `dst := ToObject(src)`, §7.1.18†: `undefined` and `null` are a TypeError, a primitive + /// becomes its wrapper object (step 1, the table), an Object is itself. It is the evaluation of + /// the expression of a `with` statement (§14.11.2† step 2: "Let obj be ? ToObject(? + /// GetValue(val))"). A `with` statement's object environment is an environment object + /// ([`Op::NewEnv`]) whose slot 0 holds the object this op gave, and the ops [`Op::WithHas`], + /// [`Op::WithGet`], [`Op::WithSet`] and [`Op::WithDelete`] are the Object Environment Record's + /// operations on it. + ToObject { + /// The register written. + dst: nat, + /// The register read. + src: nat, + }, + /// `dst := HasBinding(consts[key])` on the Object Environment Record whose binding object is + /// `obj`, with [[IsWithEnvironment]] true (§9.1.1.2.1†): "Let foundBinding be ? + /// HasProperty(bindingObject, N)" (step 2), "If foundBinding is false, return false" (step 3), + /// "If envRec.[[IsWithEnvironment]] is false, return true" (step 4), "Let unscopables be ? + /// Get(bindingObject, @@unscopables)" and, if it is an Object, "Let blocked be ToBoolean(? + /// Get(unscopables, N))", "If blocked is true, return false" (steps 5 and 6), "Return true" + /// (step 7). `obj` is the object that the environment's slot 0 holds (a `with` statement's, + /// from [`Op::ToObject`]). The same op serves the variable object of a sloppy function that + /// calls direct eval: the compiler makes that object with a null prototype, which has no + /// @@unscopables, so step 4's two cases give the same answer. It allocates and calls user code + /// (proxies, getters), so it is a handler op. + WithHas { + /// The register receiving the boolean. + dst: nat, + /// The register holding the binding object. + obj: nat, + /// The constant-pool index of the name. + key: nat, + }, + /// `dst := GetBindingValue(consts[key], strict)` on the Object Environment Record of + /// [`Op::WithHas`] (§9.1.1.2.6†): "Let value be ? HasProperty(bindingObject, N)" (step 1), "If + /// value is false, then: if S is false, return undefined; otherwise throw a ReferenceError + /// exception" (step 2), "Return ? Get(bindingObject, N)" (step 3). `strict` is the Reference + /// Record's [[Strict]]: the strictness of the code that names the variable. + WithGet { + /// The register written. + dst: nat, + /// The register holding the binding object. + obj: nat, + /// The constant-pool index of the name. + key: nat, + /// Whether the referencing code is strict. + strict: bool, + }, + /// `SetMutableBinding(consts[key], val, strict)` on the Object Environment Record of + /// [`Op::WithHas`] (§9.1.1.2.5†): "Let stillExists be ? HasProperty(bindingObject, N)" (step + /// 1), "If stillExists is false and S is true, throw a ReferenceError exception" (step 2), + /// "Perform ? Set(bindingObject, N, V, S)" (step 3). + WithSet { + /// The register holding the binding object. + obj: nat, + /// The constant-pool index of the name. + key: nat, + /// The register holding the value. + val: nat, + /// Whether the referencing code is strict. + strict: bool, + }, + /// `dst := DeleteBinding(consts[key])` on the Object Environment Record of [`Op::WithHas`] + /// (§9.1.1.2.7†: "Return ? bindingObject.[[Delete]](N)"), the result of `delete name` for a + /// name that resolved to it (§13.5.1.2† step 5.d: "Return ? + /// base.DeleteBinding(ref.[[ReferencedName]])"). A property of the variable object of a sloppy + /// function that calls direct eval is deletable. + WithDelete { + /// The register receiving the boolean. + dst: nat, + /// The register holding the binding object. + obj: nat, + /// The constant-pool index of the name. + key: nat, + }, + /// `dst := ` the result of `delete name` for an identifier that no scope of the code binds, so + /// it is a reference into the global environment or an unresolvable one (§13.5.1.2† step 5: "If + /// IsUnresolvableReference(ref) is true, then ... return true" (step 5.c); "Else, Let base be + /// ref.[[Base]]; return ? base.DeleteBinding(ref.[[ReferencedName]])" (step 5.d)). + /// DeleteBinding of the global Environment Record (§9.1.1.4.7†): "If DclRec.HasBinding(N) is + /// true, return ! DclRec.DeleteBinding(N)" (step 2: false, a declarative binding cannot be + /// deleted), "Let globalObject be ObjRec.[[BindingObject]]" and "Let existingProp be ? + /// HasOwnProperty(globalObject, N)" (steps 3 and 4), "If existingProp is true, return ? + /// ObjRec.DeleteBinding(N)" (step 5, a [[Delete]] of the global object), "Return true" (step + /// 6). `delete` of a binding of a function or block scope is false, which the compiler writes + /// as a constant. + DeleteGlobal { + /// The register receiving the boolean. + dst: nat, + /// The constant-pool index of the name. + key: nat, + }, + /// `dst := ` the value of the call `eval(args[0..argc])` written as a direct call + /// (CallExpression : CoverCallExpression, §13.3.6.1† step 6: the callee is the + /// IdentifierReference `eval`, not parenthesized and not optional). `callee` holds the value + /// that the name `eval` resolved to, read as any identifier is (a `with` object or an eval + /// variable object may supply it), `this` the call's `this` value (EvaluateCall §13.3.6.2† step + /// 1: the `with` object or undefined), `args` the arguments, and `env` the current environment + /// object (the innermost scope of the code that has one, §9.1.1.1; see below). + /// The handler does what step 6 says. "If SameValue(func, %eval%) is true" is false, this is an + /// ordinary call of `callee` (EvaluateCall steps 4 to 6). If it is true: "If argList has no + /// elements, return undefined" (step 6.d.i); evalArg is the first argument, strictCaller is + /// `consts[k].strict` ("Let strictCaller be IsStrict of this CallExpression", step 6.d.iii), + /// and the result is "? PerformEval(evalArg, strictCaller, true)" (step 6.d.vi), §19.2.1.1†: an + /// evalArg that is not a String is the result (step 2); otherwise it is parsed as an eval + /// script, with `inFunction`, `inMethod`, `inDerivedConstructor` and `inClassFieldInitializer` + /// taken from the record (steps 5 to 9, which make `new.target`, `super.x`, `super()` and + /// `arguments` early errors where the caller does not allow them), a SyntaxError of the parse + /// being thrown in the running realm (step 10). + /// The host compile path. The handler hands the parsed eval script and the record `consts[k]` + /// to the compiler's entry for eval code, `compile::compile_eval(ast, source, &EvalScopeInfo)` + /// (not yet written: the interpreter, 7.8, is its first user), which returns a Program whose + /// main function runs as the eval code. The record, built by the compiler from what scope + /// analysis knows at the call, lists the scopes from the innermost out to the script, each with + /// its kind and, for each name it binds, the slot of the name in the scope's environment object + /// and what kind of binding it is. It lists only scopes that have an environment object, + /// because the compiler gave every binding of every enclosing scope an environment slot when + /// the function has a direct eval: an enclosing function's registers are gone otherwise. Names + /// of eval code that it does not declare itself resolve through that list, one `GetEnvParent` + /// per scope that precedes the one that binds them, as the compiler's own references do; names + /// that none binds are globals. The caller's `this`, `new.target` and function are bindings in + /// the list (of kinds This, NewTarget and Callee) that eval code reads for `this`, `new.target` + /// and `super`. The eval code is strict if strictCaller is true or it starts with a Use Strict + /// Directive (step 21: strictEval). A strict eval has its own variable scope (step 24: "If + /// strictEval is true, set varEnv to lexEnv"), so its `var`s and functions are not visible + /// outside; a sloppy eval's declarations are made by EvalDeclarationInstantiation (§19.2.1.3†): + /// in the script's global code they are properties of the global object + /// (CreateGlobalVarBinding(vn, true), configurable), and in a function they are properties of + /// the variable object of the nearest scope of kind EvalVars, an environment slot that the + /// function's entry filled with a null-prototype object (CreateMutableBinding(vn, true), so + /// deletable); every reference that the compiler could not resolve inside the function consults + /// that object with [`Op::WithHas`] first. The step-3 early errors (a `var` of eval code + /// colliding with a lexical binding of a scope between the call and the variable scope, + /// §19.2.1.3† step 3.d) are the handler's SyntaxError, from the same list. `let`, `const` and + /// `class` declarations of the eval code are in a new scope of its own (step 25). The result of + /// the op is the completion value of the eval code (step 30). + DirectEval { + /// The register written. + dst: nat, + /// The register holding the value of the name `eval`. + callee: nat, + /// The register holding the call's `this` value. + this: nat, + /// The first of the consecutive argument registers. + args: nat, + /// The number of arguments. + argc: nat, + /// The register holding the current environment object. + env: nat, + /// The constant-pool index of the record of the call's scopes and strictness. + k: nat, + }, + /// `dst := ` the value of the indirect binding that slot `idx` of the module environment + /// object in `env` holds: an `import { x } from "m"` binding. GetBindingValue(N, S) of a Module + /// Environment Record (§9.1.1.5.1†): "If the binding for N is an indirect binding, then Let M + /// and N2 be the indirection values provided when this binding for N was created" (step 3), + /// "Let targetEnv be M.[[Environment]]" (step 3.b), "If targetEnv is empty, throw a + /// ReferenceError exception" (step 3.c), "Return ? targetEnv.GetBindingValue(N2, true)" (step + /// 3.d), which throws a ReferenceError while the binding of the target module is still + /// uninitialized (§9.1.1.1.6† step 2: its temporal dead zone, which a cyclic import or an + /// asynchronous module can reach). The slot holds the indirection (M and N2, the module and the + /// binding name that ResolveExport found) because linking put it there: InitializeEnvironment + /// (§16.2.1.6.4†) step 8, "Perform env.CreateImportBinding(in.[[LocalName]], + /// resolution.[[Module]], resolution.[[BindingName]])" (§9.1.1.5.5†). A namespace import + /// (`import * as ns`) is not an indirect binding: step 7 initializes an ordinary immutable + /// binding with the module namespace object, which an ordinary [`Op::GetEnvSlot`] reads. The + /// op always reads another module's environment, so it is a handler call. + GetImportBinding { + /// The register written. + dst: nat, + /// The register holding the module environment object. + env: nat, + /// The slot of the indirect binding in that environment object. + idx: nat, + }, + /// `dst := ` the Promise of `import(specifier, options)`: EvaluateImportCall (§13.3.10.1†) + /// after its operands are evaluated and read ("Let specifier be ? GetValue(specifierRef)", + /// step 4; "Let options be ? GetValue(optionsRef)", step 5.b, or undefined, step 6, which the + /// compiler loads when the call has no second argument): "Let promiseCapability be ! + /// NewPromiseCapability(%Promise%)" (step 7), "Let specifierString be Completion(ToString( + /// specifier))" and "IfAbruptRejectPromise(specifierString, promiseCapability)" (steps 8 and + /// 9), then, if options is not undefined, the checks that reject the promise with a TypeError + /// (options is not an Object, its `with` property is not an Object, a value of it is not a + /// String, an attribute key is one the host does not support: steps 11.a to 11.e) and the + /// attribute list sorted by key (step 11.f), "Let moduleRequest be a new ModuleRequest Record + /// { [[Specifier]]: specifierString, [[Attributes]]: attributes }" (step 12), "Perform + /// HostLoadImportedModule(referrer, moduleRequest, empty, promiseCapability)" (step 13) and + /// "Return promiseCapability.[[Promise]]" (step 14). `referrer` (step 1: GetActiveScriptOrModule + /// §9.4.1†, or the current Realm Record if there is none, step 2) is the script or module of + /// the running function, which the handler reads from the function (the function template + /// belongs to the one that compiled it), so the op has no operand for it. Whatever goes wrong + /// after the operands is a rejection, so the op never throws; it allocates and calls the host. + ImportCall { + /// The register written. + dst: nat, + /// The register holding the specifier. + specifier: nat, + /// The register holding the options (undefined if the call has none). + options: nat, + }, + /// `dst := ` `import.meta` of the running module: MetaProperty : `import . meta` (§13.3.12.1†): + /// "Let module be GetActiveScriptOrModule()" (step 1), "Let importMeta be module.[[ImportMeta]]" + /// (step 3); "If importMeta is empty, then Set importMeta to OrdinaryObjectCreate(null)" + /// (step 4.a), "Let importMetaValues be HostGetImportMetaProperties(module)" (step 4.b), "For + /// each Record { [[Key]], [[Value]] } p of importMetaValues, Perform ! + /// CreateDataPropertyOrThrow(importMeta, p.[[Key]], p.[[Value]])" (step 4.c), "Perform + /// HostFinalizeImportMeta(importMeta, module)" (step 4.d), "Set module.[[ImportMeta]] to + /// importMeta" (step 4.e) and return it (step 4.f); otherwise return the object that is there + /// (step 5). The module is the one that the running function belongs to, as for + /// [`Op::ImportCall`]; the parser allows `import.meta` only in a module, so there is one. The + /// first evaluation allocates and calls the host, so the op is a handler call. + ImportMeta { + /// The register written. + dst: nat, + }, + /// As [`Op::DirectEval`], for a call with a spread argument: `args` holds not the first of + /// the argument registers but an Array that ArgumentListEvaluation (§13.3.8.1†) filled, in + /// order, with the argument values, spread elements iterated (`eval(...a, b)`). The handler does + /// what [`Op::DirectEval`] does with the elements of the array as the argument list; the array + /// is the compiler's own and is not visible to the program, so reading it is not observable. + DirectEvalSpread { + /// The register written. + dst: nat, + /// The register holding the value of the name `eval`. + callee: nat, + /// The register holding the call's `this` value. + this: nat, + /// The register holding the array of arguments. + args: nat, + /// The register holding the current environment object. + env: nat, + /// The constant-pool index of the record of the call's scopes and strictness. + k: nat, + }, } /// The class of an op (see the file header). @@ -1447,6 +1845,25 @@ pub open spec fn op_class(op: Op) -> OpClass { | Op::CreateRestArray { .. } | Op::ForInIterator { .. } | Op::InitGlobalLexical { .. } + | Op::NewRegExp { .. } + | Op::GeneratorStart + | Op::YieldDelegate { .. } + | Op::GetAsyncIterator { .. } + | Op::IterCallMethod { .. } + | Op::IterComplete { .. } + | Op::IterValue { .. } + | Op::ThrowIfNotObject { .. } + | Op::ToObject { .. } + | Op::WithHas { .. } + | Op::WithGet { .. } + | Op::WithSet { .. } + | Op::WithDelete { .. } + | Op::DeleteGlobal { .. } + | Op::DirectEval { .. } + | Op::GetImportBinding { .. } + | Op::ImportCall { .. } + | Op::ImportMeta { .. } + | Op::DirectEvalSpread { .. } | Op::ThrowError { .. } => OpClass::Handler, } } @@ -1518,6 +1935,25 @@ pub open spec fn op_handler(op: Op) -> HandlerId { Op::Await { .. } => HandlerId::Await, Op::Throw { .. } | Op::Nop => HandlerId::Throw, Op::ThrowError { .. } => HandlerId::ThrowError, + Op::ToObject { .. } => HandlerId::ToObject, + Op::WithHas { .. } => HandlerId::WithHas, + Op::WithGet { .. } => HandlerId::WithGet, + Op::WithSet { .. } => HandlerId::WithSet, + Op::WithDelete { .. } => HandlerId::WithDelete, + Op::DeleteGlobal { .. } => HandlerId::DeleteGlobal, + Op::DirectEval { .. } => HandlerId::DirectEval, + Op::GetImportBinding { .. } => HandlerId::GetImportBinding, + Op::ImportCall { .. } => HandlerId::ImportCall, + Op::ImportMeta { .. } => HandlerId::ImportMeta, + Op::DirectEvalSpread { .. } => HandlerId::DirectEvalSpread, + Op::GeneratorStart => HandlerId::GeneratorStart, + Op::YieldDelegate { .. } => HandlerId::YieldDelegate, + Op::GetAsyncIterator { .. } => HandlerId::GetAsyncIterator, + Op::IterCallMethod { .. } => HandlerId::IterCallMethod, + Op::IterComplete { .. } => HandlerId::IterComplete, + Op::IterValue { .. } => HandlerId::IterValue, + Op::ThrowIfNotObject { .. } => HandlerId::ThrowIfNotObject, + Op::NewRegExp { .. } => HandlerId::NewRegExp, Op::InitGlobalLexical { .. } => HandlerId::InitGlobalLexical, Op::ToPropertyKey { .. } => HandlerId::ToPropertyKey, Op::ToString { .. } => HandlerId::ToString, diff --git a/crates/clazel-js-vm/src/proofs.rs b/crates/clazel-js-vm/src/proofs.rs new file mode 100644 index 000000000..f51b0d340 --- /dev/null +++ b/crates/clazel-js-vm/src/proofs.rs @@ -0,0 +1,215 @@ +//! Kani harnesses, compiled only under `cargo kani`. +//! +//! Small pieces of the interpreter (item 7.8, first slice), each finishing in +//! seconds to minutes. They are evidence, not a tier claim: the crate stays at +//! tier 0, as `Cargo.toml` says. +//! +//! - [`add_sub_int32_fits`], [`add_sub_int32_overflow`], [`mul_int32_fits`], +//! [`mul_negative_zero_is_a_double`]: for any two int32 operands, `+` and +//! `-` give the int32 when the exact result fits and a double when it does +//! not; `*` is checked for 16-bit operands (a 32 x 32 bit symbolic product +//! does not finish) and for the `-0` cases: `spec::arith_add`, `arith_sub`, +//! `arith_mul`'s encoding rule. Overflow never panics. The values of the +//! doubles, and an overflowing `*`, are left to the differential test. +//! - [`neg_int32`], [`rem_int32`]: unary minus and `%` on int32, including the +//! two operand pairs that overflow `i32` arithmetic (`-(-2^31)`, +//! `-2^31 % -1`). +//! - [`shifts_int32`]: `<<`, `>>`, `>>>` on int32 against the exact integer +//! results of §6.1.6.1.9 to .11. +//! - [`frame_indexing`]: for any frame base, register and slot, the +//! region accessors return `Ok` for a slot inside the region and `Err` +//! otherwise, and never panic or overflow, over regions of up to 6 slots. +//! - [`return_address_round_trip`]: `unpack_ret` inverts `pack_ret` for every +//! base and `pc` below `RET_LIMIT`, and `pack_ret` refuses the rest. +//! - [`argc_round_trip`]: `dec_argc` inverts `enc_argc`. +//! +//! Not covered: doubles (CBMC's floating-point reasoning is too slow for the +//! bitwise conversions; `ToInt32` on doubles is covered by the differential +//! test), and the loop and the handlers, which touch the heap's collections. + +use crate::frame::{ + HDR_SLOTS, KIND_CONSTRUCT, KIND_PLAIN, KIND_SUSPENDABLE, RET_LIMIT, Region, Ret, dec_argc, + enc_argc, pack_ret, reg_slot, unpack_ret, +}; +use crate::numeric::{self, Num}; +use clazel_js_heap::value::{Decoded, Value}; + +#[kani::proof] +fn add_sub_int32_fits() { + // The exact result fits an int32: the result is that int32. (Assuming it + // prunes the double path, whose floating-point arithmetic CBMC is slow + // on.) + let x: i32 = kani::any(); + let y: i32 = kani::any(); + let sum = i64::from(x) + i64::from(y); + kani::assume(i32::try_from(sum).is_ok()); + assert!(numeric::add(Num::I(x), Num::I(y)) == Value::int32(sum as i32)); + let diff = i64::from(x) - i64::from(y); + kani::assume(i32::try_from(diff).is_ok()); + assert!(numeric::sub(Num::I(x), Num::I(y)) == Value::int32(diff as i32)); +} + +#[kani::proof] +fn mul_int32_fits() { + // A symbolic 32 x 32 bit multiplication does not finish in minutes under + // CBMC, so the operands are any 16-bit values (whose product always + // fits): the result is the exact int32. + let p: i16 = kani::any(); + let q: i16 = kani::any(); + let m = i32::from(p) * i32::from(q); + // Not the `-0` case, which is `mul_negative_zero_is_a_double`'s (its + // double path has a symbolic floating-point product). + kani::assume(!(m == 0 && (p < 0 || q < 0))); + let got = numeric::mul(Num::I(i32::from(p)), Num::I(i32::from(q))); + assert!(got == Value::int32(m)); +} + +#[kani::proof] +fn add_sub_int32_overflow() { + // The exact result does not fit: a double (the value is the `f64` sum, + // which the differential test checks). + let x: i32 = kani::any(); + let y: i32 = kani::any(); + kani::assume(i32::try_from(i64::from(x) + i64::from(y)).is_err()); + assert!(matches!( + numeric::add(Num::I(x), Num::I(y)).decode(), + Decoded::Double(_) + )); + let z: i32 = kani::any(); + let w: i32 = kani::any(); + kani::assume(i32::try_from(i64::from(z) - i64::from(w)).is_err()); + assert!(matches!( + numeric::sub(Num::I(z), Num::I(w)).decode(), + Decoded::Double(_) + )); +} + +#[kani::proof] +fn mul_negative_zero_is_a_double() { + // `0 * y` and `x * 0` with a negative operand are `-0`, not the int32 0. + for y in [-1i32, i32::MIN] { + assert!(numeric::mul(Num::I(0), Num::I(y)) == Value::double(-0.0)); + assert!(numeric::mul(Num::I(y), Num::I(0)) == Value::double(-0.0)); + } +} + +#[kani::proof] +fn neg_int32() { + let x: i32 = kani::any(); + match numeric::neg(Num::I(x)).decode() { + Decoded::Int32(r) => assert!(x != 0 && x != i32::MIN && i64::from(r) == -i64::from(x)), + Decoded::Double(_) => assert!(x == 0 || x == i32::MIN), + _ => assert!(false), + } +} + +#[kani::proof] +fn rem_int32() { + // A symbolic 64-bit remainder by a symbolic divisor does not finish in + // minutes under CBMC, so the divisor is any value in -8..=8 (and the + // overflowing pair `-2^31 % -1` is its own case); the dividend is any + // int32. + let x: i32 = kani::any(); + let y: i32 = kani::any(); + kani::assume((-8..=8).contains(&y)); + match numeric::rem(Num::I(x), Num::I(y)).decode() { + Decoded::Int32(r) => { + assert!(y != 0); + assert!(!(r == 0 && x < 0)); + // |r| < |y| and the sign of the dividend. + assert!(i64::from(r).abs() < i64::from(y).abs()); + assert!(r == 0 || (r < 0) == (x < 0)); + } + // A double for a zero divisor, or a zero remainder of a negative + // dividend (`-0`). + Decoded::Double(_) => assert!(y == 0 || x < 0), + _ => assert!(false), + } + // The pair that overflows i32 arithmetic is `-0`, a double. + assert!(matches!( + numeric::rem(Num::I(i32::MIN), Num::I(-1)).decode(), + Decoded::Double(_) + )); +} + +#[kani::proof] +#[kani::unwind(34)] +fn shifts_int32() { + // Each shift count in turn (a symbolic divisor 2^count is as slow as + // above), with any int32 operand; the left operand's count is + // `b mod 32`, which the harness reaches through `b = count + 32 * k`. + let x: i32 = kani::any(); + let k: i32 = kani::any(); + kani::assume((-4..=4).contains(&k)); + let mut count = 0u32; + while count < 32 { + let y = (count as i32) + 32 * k; + let (a, b) = (Num::I(x), Num::I(y)); + let l = i64::from(x) * (1i64 << count); + assert!(numeric::shl(a, b) == Value::int32(l as i32)); + let r = i64::from(x) >> count; // arithmetic shift: floor division + assert!(numeric::shr(a, b) == Value::int32(r as i32)); + let u = i64::from(x as u32) >> count; + match numeric::ushr(a, b).decode() { + Decoded::Int32(v) => assert!(i64::from(v) == u), + Decoded::Double(d) => assert!(d == u as f64 && u > i64::from(i32::MAX)), + _ => assert!(false), + } + count += 1; + } +} + +#[kani::proof] +#[kani::unwind(8)] +fn frame_indexing() { + let limit: usize = kani::any(); + kani::assume(limit <= 6); + let mut region = Region::new(limit); + let base: usize = kani::any(); + let size: usize = kani::any(); + kani::assume(base <= 8 && size <= 8); + let hdr = [Value::NULL, Value::TRUE, Value::int32(0), Value::UNDEFINED]; + let pushed = region.push_frame(base, size, hdr); + // A push succeeds exactly when the frame fits the limit. + assert!(pushed.is_ok() == (base + size <= limit)); + if pushed.is_ok() { + assert!(region.len() >= base + size); + } + let fp: usize = kani::any(); + let r: u32 = kani::any(); + match reg_slot(fp, r) { + Some(slot) => { + assert!(slot == fp + HDR_SLOTS + (r as usize)); + assert!(region.reg(fp, r).is_ok() == (slot < region.len())); + assert!(region.set_reg(fp, r, Value::NULL).is_ok() == (slot < region.len())); + } + None => assert!(region.reg(fp, r).is_err()), + } + let i: usize = kani::any(); + assert!(region.get(i).is_ok() == (i < region.len())); + assert!(region.header(fp, 3).is_ok() == fp.checked_add(3).is_some_and(|s| s < region.len())); +} + +#[kani::proof] +fn return_address_round_trip() { + let base: usize = kani::any(); + let pc: usize = kani::any(); + match pack_ret(base, pc) { + Some(v) => { + assert!(base < RET_LIMIT && pc < RET_LIMIT); + assert!(unpack_ret(v) == Some(Ret::Caller { base, pc })); + } + None => assert!(base >= RET_LIMIT || pc >= RET_LIMIT), + } +} + +#[kani::proof] +fn argc_round_trip() { + let argc: u32 = kani::any(); + let kind: u32 = kani::any(); + kani::assume(argc < (1 << 28)); + kani::assume(kind == KIND_PLAIN || kind == KIND_CONSTRUCT || kind == KIND_SUSPENDABLE); + let v = enc_argc(argc, kind); + assert!(v.is_some()); + assert!(v.and_then(dec_argc) == Some((argc, kind))); +} diff --git a/crates/clazel-js-vm/src/props.rs b/crates/clazel-js-vm/src/props.rs new file mode 100644 index 000000000..50c91e54a --- /dev/null +++ b/crates/clazel-js-vm/src/props.rs @@ -0,0 +1,502 @@ +//! Property descriptors, `[[DefineOwnProperty]]`, `[[Delete]]` and Array +//! exotic objects (item 7.9, second group: arrays). +//! +//! The VM's objects hold data properties by shape (`crate::heap`); this module +//! is the standard's property-level operations over them: +//! +//! - [`PropDesc`], a Property Descriptor Specification Type (§6.2.6†) for data +//! properties (each field present or absent); +//! - [`Vm::define_own_property`]: `OrdinaryDefineOwnProperty` (§10.1.6.1†) with +//! `ValidateAndApplyPropertyDescriptor` (§10.1.6.3†), and the Array exotic +//! object's `[[DefineOwnProperty]]` (§10.4.2.1†) with `ArraySetLength` +//! (§10.4.2.4†); +//! - [`Vm::array_create`] (§10.4.2.2†) and [`Vm::create_array_from_list`] +//! (§7.3.18†); +//! - `CreateDataProperty` (§7.3.5†), `DefinePropertyOrThrow` (§7.3.8†), +//! `[[Delete]]` (§10.1.10.1†) and `DeletePropertyOrThrow` (§7.3.10†). +//! +//! Objects are always extensible in this slice (no `Object.preventExtensions` +//! yet) and have no accessor properties. + +use crate::heap::length_value; +use crate::interp::{Abrupt, Intrinsic, Vm}; +use crate::numeric::to_uint32; +use clazel_js_heap::shape::Attrs; +use clazel_js_heap::string::Key; +use clazel_js_heap::value::Value; + +/// A Property Descriptor (§6.2.6†) for a data property: each field is present +/// (`Some`) or absent (`None`). +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct PropDesc { + /// `[[Value]]`. + pub value: Option, + /// `[[Writable]]`. + pub writable: Option, + /// `[[Enumerable]]`. + pub enumerable: Option, + /// `[[Configurable]]`. + pub configurable: Option, +} + +impl PropDesc { + /// A complete descriptor: `value` with the attributes `attrs`. + #[must_use] + pub fn data(value: Value, attrs: Attrs) -> Self { + Self { + value: Some(value), + writable: Some(attrs.0 & Attrs::WRITABLE != 0), + enumerable: Some(attrs.0 & Attrs::ENUMERABLE != 0), + configurable: Some(attrs.0 & Attrs::CONFIGURABLE != 0), + } + } + + /// `{ [[Value]]: value, [[Writable]]: true, [[Enumerable]]: true, + /// [[Configurable]]: true }`, the descriptor of `CreateDataProperty` + /// (§7.3.5† step 1). + #[must_use] + pub fn all(value: Value) -> Self { + Self::data(value, Attrs::ALL) + } +} + +impl Vm { + /// SameValue, §7.2.10†: Numbers by value with NaN equal to itself and `+0` + /// distinct from `-0`, Strings by code units, everything else by identity. + #[must_use] + pub fn same_value(&self, a: Value, b: Value) -> bool { + if let (Some(x), Some(y)) = (crate::numeric::num(a), crate::numeric::num(b)) { + let (x, y) = (x.to_f64(), y.to_f64()); + return if x.is_nan() || y.is_nan() { + x.is_nan() && y.is_nan() + } else { + x.to_bits() == y.to_bits() + }; + } + if let (Some(x), Some(y)) = (self.heap.string(a), self.heap.string(b)) { + return x == y; + } + a == b + } + + /// The own data property `key` of `obj` (`OrdinaryGetOwnProperty`, + /// §10.1.5.1†): its attributes and value. + /// + /// # Errors + /// [`Abrupt::Internal`] if the heap is corrupt. + pub fn get_own_property(&self, obj: Value, key: Key) -> Result, Abrupt> { + let Some(f) = self.heap.find(obj, key)?.filter(|f| f.own) else { + return Ok(None); + }; + let v = self + .heap + .slot(f.holder, f.slot) + .ok_or(Abrupt::Internal("property slot"))?; + Ok(Some((f.attrs, v))) + } + + /// `ValidateAndApplyPropertyDescriptor` (§10.1.6.3†) for an extensible + /// object and data properties, as `OrdinaryDefineOwnProperty` (§10.1.6.1†) + /// calls it: + /// 1. (Assert: key is a property key.) + /// 2. If current is undefined, then a. (extensible is true) b. create the + /// data property: each absent field is its default (`undefined`, + /// `false`). c. Return true. + /// 3. (Assert: current is a fully populated Property Descriptor.) + /// 4. If every field of Desc is absent, return true. + /// 5. If current.[[Configurable]] is false, then a. If Desc has + /// [[Configurable]] and it is true, return false. b. If Desc has + /// [[Enumerable]] and it differs from current's, return false. (c, d: + /// the generic and accessor cases, not reachable with data properties.) + /// e. If current.[[Writable]] is false, then i. If Desc has [[Writable]] + /// and it is true, return false. ii. If Desc has [[Value]] and + /// SameValue(Desc.[[Value]], current.[[Value]]) is false, return false. + /// 6. (Accessor/data conversion: not reachable.) The fields of Desc + /// replace those of the property. + /// 7. Return true. + fn ordinary_define_own_property( + &mut self, + obj: Value, + key: Key, + desc: &PropDesc, + ) -> Result { + let current = self.get_own_property(obj, key)?; + let Some((cur_attrs, cur_value)) = current else { + let mut bits = 0; + for (flag, bit) in [ + (desc.writable, Attrs::WRITABLE), + (desc.enumerable, Attrs::ENUMERABLE), + (desc.configurable, Attrs::CONFIGURABLE), + ] { + if flag == Some(true) { + bits |= bit; + } + } + let v = desc.value.unwrap_or(Value::UNDEFINED); + self.heap.define(obj, key, v, Attrs(bits))?; + return Ok(true); + }; + let flag = |bit: u8| cur_attrs.0 & bit != 0; + if !flag(Attrs::CONFIGURABLE) { + let bad = desc.configurable == Some(true) + || desc + .enumerable + .is_some_and(|e| e != flag(Attrs::ENUMERABLE)) + || (!flag(Attrs::WRITABLE) + && (desc.writable == Some(true) + || desc.value.is_some_and(|v| !self.same_value(v, cur_value)))); + if bad { + return Ok(false); + } + } + let mut bits = 0; + for (new, bit) in [ + (desc.writable, Attrs::WRITABLE), + (desc.enumerable, Attrs::ENUMERABLE), + (desc.configurable, Attrs::CONFIGURABLE), + ] { + if new.unwrap_or_else(|| flag(bit)) { + bits |= bit; + } + } + let v = desc.value.unwrap_or(cur_value); + self.heap.redefine(obj, key, v, Attrs(bits))?; + Ok(true) + } + + /// `[[DefineOwnProperty]]` (P, Desc) of an object: for an Array exotic + /// object §10.4.2.1†, else `OrdinaryDefineOwnProperty` (§10.1.6.1†). + /// 1. If P is "length", return ? ArraySetLength(A, Desc). + /// 2. Else if P is an array index, then + /// a. Let lengthDesc be OrdinaryGetOwnProperty(A, "length"). + /// b. (Assert: it is a data descriptor.) c. Let length be + /// lengthDesc.[[Value]]. d. Let index be ! ToUint32(P). + /// e. If index >= length and lengthDesc.[[Writable]] is false, return + /// false. + /// f. Let succeeded be ! OrdinaryDefineOwnProperty(A, P, Desc). + /// g. If succeeded is false, return false. + /// h. If index >= length, then i. Set lengthDesc.[[Value]] to + /// index + 1. ii. Set succeeded to ! OrdinaryDefineOwnProperty(A, + /// "length", lengthDesc). iii. (Assert: succeeded is true.) + /// j. Return true. + /// 3. Return ? OrdinaryDefineOwnProperty(A, P, Desc). + /// + /// # Errors + /// A `RangeError` (ArraySetLength step 5), the exceptions of ToNumber, or + /// [`Abrupt::OutOfMemory`]. + pub fn define_own_property( + &mut self, + obj: Value, + key: Key, + desc: &PropDesc, + ) -> Result { + if !self.heap.is_array_object(obj) { + return self.ordinary_define_own_property(obj, key, desc); + } + if key == self.atoms.k_length { + return self.array_set_length(obj, desc); + } + let Some(index) = self.heap.key_index(key) else { + return self.ordinary_define_own_property(obj, key, desc); + }; + let (length, writable) = self + .heap + .array_length(obj) + .ok_or(Abrupt::Internal("array length"))?; + if index >= length && !writable { + return Ok(false); + } + if !self.ordinary_define_own_property(obj, key, desc)? { + return Ok(false); + } + if index >= length { + let new_len = index + .checked_add(1) + .ok_or(Abrupt::Internal("array index"))?; + self.heap.set_array_length(obj, new_len)?; + } + Ok(true) + } + + /// ArraySetLength ( A, Desc ), §10.4.2.4†: + /// 1. If Desc does not have a [[Value]] field, then a. Return ! + /// OrdinaryDefineOwnProperty(A, "length", Desc). + /// 2. Let newLenDesc be a copy of Desc. + /// 3. Let newLen be ? ToUint32(Desc.[[Value]]). + /// 4. Let numberLen be ? ToNumber(Desc.[[Value]]). + /// 5. If SameValueZero(newLen, numberLen) is false, throw a RangeError. + /// 6. Set newLenDesc.[[Value]] to newLen. + /// 7. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length"). + /// 9. Let oldLen be oldLenDesc.[[Value]]. + /// 10. If newLen >= oldLen, then a. Return ! OrdinaryDefineOwnProperty(A, + /// "length", newLenDesc). + /// 11. If oldLenDesc.[[Writable]] is false, return false. + /// 12. If newLenDesc does not have a [[Writable]] field or + /// newLenDesc.[[Writable]] is true, let newWritable be true. + /// 13. Else, a. (Need to defer setting the [[Writable]] attribute to false + /// in case any elements cannot be deleted.) b. Let newWritable be + /// false. c. Set newLenDesc.[[Writable]] to true. + /// 14. Let succeeded be ! OrdinaryDefineOwnProperty(A, "length", + /// newLenDesc). + /// 15. If succeeded is false, return false. + /// 16. For each own property key P of A such that P is an array index and + /// ! ToUint32(P) >= newLen, in descending numeric index order, do + /// a. Let deleteSucceeded be ! A.[[Delete]](P). + /// b. If deleteSucceeded is false, then i. Set newLenDesc.[[Value]] to + /// ! ToUint32(P) + 1. ii. If newWritable is false, set + /// newLenDesc.[[Writable]] to false. iii. Perform ! + /// OrdinaryDefineOwnProperty(A, "length", newLenDesc). iv. Return + /// false. + /// 17. If newWritable is false, then a. Set succeeded to ! + /// OrdinaryDefineOwnProperty(A, "length", { [[Writable]]: false }). + /// 18. Return true. + fn array_set_length(&mut self, a: Value, desc: &PropDesc) -> Result { + let k_length = self.atoms.k_length; + let Some(v) = desc.value else { + return self.ordinary_define_own_property(a, k_length, desc); + }; + let mut new_len_desc = *desc; + let new_len = to_uint32(self.coerce_number(v)?); + let number_len = self.to_number(v)?; + if f64::from(new_len) != number_len { + return Err(self.range_error("Invalid array length")); + } + new_len_desc.value = Some(length_value(new_len)); + let (old_len, old_writable) = self + .heap + .array_length(a) + .ok_or(Abrupt::Internal("array length"))?; + if new_len >= old_len { + return self.ordinary_define_own_property(a, k_length, &new_len_desc); + } + if !old_writable { + return Ok(false); + } + let new_writable = new_len_desc.writable != Some(false); + if !new_writable { + new_len_desc.writable = Some(true); + } + if !self.ordinary_define_own_property(a, k_length, &new_len_desc)? { + return Ok(false); + } + // Step 16: the indices at or above newLen, in descending order. + let mut doomed: Vec<(u32, Key)> = self + .heap + .own_properties(a) + .into_iter() + .filter_map(|(k, _, _)| self.heap.key_index(k).map(|i| (i, k))) + .filter(|(i, _)| *i >= new_len) + .collect(); + doomed.sort_by_key(|x| std::cmp::Reverse(x.0)); + for (i, k) in &doomed { + let configurable = self + .get_own_property(a, *k)? + .is_some_and(|(at, _)| at.0 & Attrs::CONFIGURABLE != 0); + if !configurable { + // The properties above this one are already gone. + let keep = i.checked_add(1).ok_or(Abrupt::Internal("array index"))?; + new_len_desc.value = Some(length_value(keep)); + if !new_writable { + new_len_desc.writable = Some(false); + } + self.ordinary_define_own_property(a, k_length, &new_len_desc)?; + self.remove_doomed(a, &doomed, Some(*i))?; + return Ok(false); + } + } + self.remove_doomed(a, &doomed, None)?; + if !new_writable { + let w = PropDesc { + writable: Some(false), + ..PropDesc::default() + }; + self.ordinary_define_own_property(a, k_length, &w)?; + } + Ok(true) + } + + /// Removes the doomed indices above `stop` (all of them when `stop` is + /// `None`): the deletions of step 16.a that happened before the one that + /// failed. + fn remove_doomed( + &mut self, + a: Value, + doomed: &[(u32, Key)], + stop: Option, + ) -> Result<(), Abrupt> { + let keys: Vec = doomed + .iter() + .filter(|(i, _)| stop.is_none_or(|s| *i > s)) + .map(|(_, k)| *k) + .collect(); + Ok(self.heap.remove_properties(a, &keys)?) + } + + /// ArrayCreate ( length [ , proto ] ), §10.4.2.2†: + /// 1. If length > 2^32 - 1, throw a RangeError exception. + /// 2. If proto is not present, set proto to %Array.prototype%. + /// 3. Let A be MakeBasicObject(<<[[Prototype]], [[Extensible]]>>). + /// 4. Set A.[[Prototype]] to proto. + /// 5. Set A.[[DefineOwnProperty]] to the definition in §10.4.2.1†. + /// 6. Perform ! OrdinaryDefineOwnProperty(A, "length", { [[Value]]: length, + /// [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false + /// }). + /// 7. Return A. + /// + /// # Errors + /// A `RangeError`, [`Abrupt::OutOfMemory`]. + pub fn array_create(&mut self, length: f64, proto: Option) -> Result { + if length > 4_294_967_295.0 || length < 0.0 || length.is_nan() { + return Err(self.range_error("Invalid array length")); + } + let proto = match proto { + Some(p) => p, + None => self.array_prototype(), + }; + Ok(self.heap.new_array(proto, length as u32)?) + } + + /// `%Array.prototype%`, or `%Object.prototype%` while the realm has no + /// `Array` built-in. + #[must_use] + pub fn array_prototype(&self) -> Value { + let p = self.intrinsic(Intrinsic::ArrayPrototype); + if self.heap.is_object(p) { + p + } else { + self.atoms.object_proto + } + } + + /// CreateArrayFromList ( elements ), §7.3.18†: + /// 1. (Assert: each element is an ECMAScript language value.) + /// 2. Let array be ! ArrayCreate(0). + /// 3. Let n be 0. + /// 4. For each element e of elements, do a. Perform ! + /// CreateDataPropertyOrThrow(array, ! ToString(𝔽(n)), e). b. Set n to n + 1. + /// 5. Return array. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`]. + pub fn create_array_from_list(&mut self, items: &[Value]) -> Result { + let a = self.array_create(0.0, None)?; + for (i, v) in items.iter().enumerate() { + let k = self.index_key(i as u64)?; + self.create_data_property(a, k, *v)?; + } + Ok(a) + } + + /// The interned key for the integer `i` (ToString of a Number that is an + /// integer, §6.1.6.1.20†). + /// + /// # Errors + /// [`Abrupt::OutOfMemory`]. + pub fn index_key(&mut self, i: u64) -> Result { + Ok(self.heap.intern_str(&i.to_string())?) + } + + /// Whether `v` is an Array exotic object (IsArray, §7.2.2† steps 1 to 3; + /// no Proxies). + #[must_use] + pub fn is_array(&self, v: Value) -> bool { + self.heap.is_array_object(v) + } + + /// CreateDataProperty ( O, P, V ), §7.3.5†: + /// 1. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: + /// true, [[Enumerable]]: true, [[Configurable]]: true }. + /// 2. Return ? O.[[DefineOwnProperty]](P, newDesc). + /// + /// # Errors + /// As [`Vm::define_own_property`]. + pub fn create_data_property(&mut self, o: Value, k: Key, v: Value) -> Result { + self.define_own_property(o, k, &PropDesc::all(v)) + } + + /// CreateDataPropertyOrThrow ( O, P, V ), §7.3.7†: + /// 1. Let success be ? CreateDataProperty(O, P, V). + /// 2. If success is false, throw a TypeError exception. + /// 3. Return unused. + /// + /// # Errors + /// A `TypeError`, or as [`Vm::define_own_property`]. + pub fn create_data_property_or_throw( + &mut self, + o: Value, + k: Key, + v: Value, + ) -> Result<(), Abrupt> { + if self.create_data_property(o, k, v)? { + Ok(()) + } else { + let name = self.key_text(k); + Err(self.type_error(&format!("Cannot define property {name}"))) + } + } + + /// DefinePropertyOrThrow ( O, P, desc ), §7.3.8†: + /// 1. Let success be ? O.[[DefineOwnProperty]](P, desc). + /// 2. If success is false, throw a TypeError exception. + /// + /// # Errors + /// A `TypeError`, or as [`Vm::define_own_property`]. + pub fn define_property_or_throw( + &mut self, + o: Value, + k: Key, + desc: &PropDesc, + ) -> Result<(), Abrupt> { + if self.define_own_property(o, k, desc)? { + Ok(()) + } else { + let name = self.key_text(k); + Err(self.type_error(&format!("Cannot redefine property: {name}"))) + } + } + + /// `[[Delete]]` ( P ), OrdinaryDelete §10.1.10.1†: + /// 1. Let desc be ? O.[[GetOwnProperty]](P). + /// 2. If desc is undefined, return true. + /// 3. If desc.[[Configurable]] is true, then a. Remove the own property + /// with name P from O. b. Return true. + /// 4. Return false. + /// + /// The `length` of an array is not configurable, so it is never removed. + /// + /// # Errors + /// [`Abrupt::OutOfMemory`]. + pub fn delete_property(&mut self, o: Value, k: Key) -> Result { + let Some((attrs, _)) = self.get_own_property(o, k)? else { + return Ok(true); + }; + if attrs.0 & Attrs::CONFIGURABLE != 0 { + self.heap.remove_properties(o, &[k])?; + return Ok(true); + } + Ok(false) + } + + /// DeletePropertyOrThrow ( O, P ), §7.3.10†: + /// 1. Let success be ? O.[[Delete]](P). + /// 2. If success is false, throw a TypeError exception. + /// + /// # Errors + /// A `TypeError`, [`Abrupt::OutOfMemory`]. + pub fn delete_property_or_throw(&mut self, o: Value, k: Key) -> Result<(), Abrupt> { + if self.delete_property(o, k)? { + Ok(()) + } else { + let name = self.key_text(k); + Err(self.type_error(&format!("Cannot delete property '{name}'"))) + } + } + + /// The step budget's check, for a native's long loops: one unit per call. + /// + /// # Errors + /// [`Abrupt::Terminated`] when the budget is spent or the terminator set. + pub fn charge_step(&mut self) -> Result<(), Abrupt> { + self.charge() + } +} diff --git a/crates/clazel-js-vm/src/scope.rs b/crates/clazel-js-vm/src/scope.rs index ceff5604d..dce1b1850 100644 --- a/crates/clazel-js-vm/src/scope.rs +++ b/crates/clazel-js-vm/src/scope.rs @@ -13,15 +13,25 @@ //! //! - One **function scope** per function holds its parameters, its `var`s, its //! function declarations and the `let`, `const` bindings at the top of its -//! body. (ECMA-262 §10.2.11 FunctionDeclarationInstantiation uses a separate -//! environment for the body only when a parameter has an initializer, which -//! this slice does not compile.) +//! body. When a parameter has an initializer or a computed name +//! (ECMA-262 §10.2.11 FunctionDeclarationInstantiation step 28), the function +//! scope holds the parameters only and a **body scope** inside it holds the +//! body's declarations, so that a closure in a default value does not see them. //! - The **script scope** holds only the script's top-level `let` and `const`. //! Its `var`s and functions are properties of the global object //! (§16.1.7 GlobalDeclarationInstantiation) and so have no binding: a //! reference to one is a global reference. //! - Blocks, `for` statements with a lexical declaration, `switch` statements -//! and `catch` clauses have their own scope. +//! and `catch` clauses have their own scope, and so does a `with` statement's +//! body: an object environment (§14.11.2†), whose environment object holds +//! the object in its one slot. +//! - **Direct eval** (§19.2.1.1†): the code that `eval` runs can name any +//! binding visible at the call, so each such binding of each enclosing scope is +//! captured, and the caller's `this`, `new.target`, function and `arguments` +//! are bindings. A sloppy caller's function scope also has a binding for the +//! variable object that the `var`s of its eval code become properties of. +//! A reference that leaves a `with` body or such a function without finding its +//! name inside first looks in that object (the compiler's `dyn_scopes`). //! - A named function expression's own name (§15.2.5 step 3, `funcEnv`) is a //! binding of its function scope when nothing there declares that name, //! which is observationally the same as the standard's extra scope. @@ -38,7 +48,7 @@ //! parameter. [`Analysis::annexb`] records, for such a declaration, the variable //! its evaluation copies the block's binding to. -use crate::bytecode::Span; +use crate::bytecode::{Flavor, Span}; use crate::compile::CompileError; use crate::literal; use clazel_js_syntax::ast::{Ast, Node, NodeKind, Op as AOp}; @@ -78,6 +88,18 @@ pub enum BKind { /// A value the compiler keeps for a class: the key of a computed field name /// and a private method's function. Hidden, + /// A named or default import (`import { x } from "m"`): an indirect + /// binding that [`crate::bytecode::Instr::GetImportBinding`] reads and + /// that cannot be assigned (§9.1.1.5.5† CreateImportBinding). + Import, + /// The binding object of a `with` statement's object environment, in slot 0 + /// of the environment object its scope creates (§14.11.2† step 4: + /// NewObjectEnvironment(obj, true, oldEnv)). + WithObject, + /// The variable object of a sloppy function that calls direct eval: a + /// null-prototype object that the declarations of the eval code become + /// properties of (§19.2.1.3† EvalDeclarationInstantiation). + EvalVars, } /// A binding. @@ -108,6 +130,10 @@ pub struct Binding { pub enum SKind { /// The script's. Script, + /// A module's (§16.2.1.6.4† InitializeEnvironment): like a function's, but + /// every binding is in the module environment object, which the runtime + /// creates when it links the module and the body runs in. + Module, /// A function's. Function, /// A block's. @@ -121,6 +147,14 @@ pub enum SKind { /// A class: its inner name binding, its private names and the values the /// compiler keeps for its elements (§15.7.14† classScope). Class, + /// A `with` statement's body: an object environment (§14.11.2†), whose + /// environment object holds the object in slot 0. + With, + /// The body of a function whose parameter list has expressions: the + /// separate environment for the body's `var`s and top-level declarations + /// (§10.2.11† step 28.f: "varEnv be NewDeclarativeEnvironment(env)"), inside + /// the function scope that holds the parameters. + Body, } /// A scope. @@ -244,6 +278,18 @@ pub struct Analysis { /// with whether it is a constant, in source order: bindings of the global /// declarative record, not of a scope (§16.1.7† step 13). pub global_lexicals: Vec<(u32, bool)>, + /// For a scope whose references may reach a property of an object before + /// any binding (a `with` body, or the function scope of sloppy code that + /// calls direct eval), the binding that holds that object. + pub dyn_binding: HashMap, + /// The direct eval calls (`CallExpression` nodes), in arena order. + pub eval_calls: Vec, + /// For a function declaration that is the body of an `if` clause (Annex + /// B.3.4†), the block scope it is treated as being in. + pub implicit_blocks: HashMap, + /// The binding `*default*` of an `export default` of an expression, an + /// anonymous function or an anonymous class, or [`NONE`]. + pub default_binding: u32, } /// The node at `i`. @@ -266,16 +312,59 @@ pub fn span_of(ast: &Ast, i: usize) -> Span { /// Whether the kind creates a function scope. fn is_function(k: NodeKind) -> bool { + is_decl_kind(k) + || is_fexpr_kind(k) + || is_arrow_kind(k) + || matches!(k, NodeKind::MethodDefinition | NodeKind::ClassStaticBlock) +} + +/// A function declaration of any flavor (§15.2, §15.5, §15.6, §15.8). +pub fn is_decl_kind(k: NodeKind) -> bool { matches!( k, NodeKind::FunctionDeclaration - | NodeKind::FunctionExpression - | NodeKind::ArrowFunction - | NodeKind::MethodDefinition - | NodeKind::ClassStaticBlock + | NodeKind::GeneratorDeclaration + | NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncGeneratorDeclaration + ) +} + +/// A function expression of any flavor. +pub fn is_fexpr_kind(k: NodeKind) -> bool { + matches!( + k, + NodeKind::FunctionExpression + | NodeKind::GeneratorExpression + | NodeKind::AsyncFunctionExpression + | NodeKind::AsyncGeneratorExpression ) } +/// An arrow function, async or not (§15.3, §15.9). +pub fn is_arrow_kind(k: NodeKind) -> bool { + matches!(k, NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction) +} + +/// Whether the function node is a generator, an async function or both. +pub fn flavor_of(nd: &Node) -> Flavor { + match nd.kind { + NodeKind::GeneratorDeclaration | NodeKind::GeneratorExpression => Flavor::Generator, + NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncFunctionExpression + | NodeKind::AsyncArrowFunction => Flavor::Async, + NodeKind::AsyncGeneratorDeclaration | NodeKind::AsyncGeneratorExpression => { + Flavor::AsyncGenerator + } + NodeKind::MethodDefinition => match nd.op { + AOp::GeneratorMethod => Flavor::Generator, + AOp::AsyncMethod => Flavor::Async, + AOp::AsyncGeneratorMethod => Flavor::AsyncGenerator, + _ => Flavor::Plain, + }, + _ => Flavor::Plain, + } +} + /// The `BindingIdentifier` nodes a binding target binds (BoundNames, §8.2.1†): /// the target itself, or the identifiers of a pattern, in source order, /// without those inside initializers and computed names. @@ -329,73 +418,6 @@ pub fn is_simple_params(ast: &Ast, fp: u32) -> Result { Ok(true) } -/// The first construct of the AST this slice of the compiler does not compile, -/// as a "not yet compiled" error; `Ok` if there is none. Checked before -/// anything else, so that a program with such a construct is reported as -/// not yet compiled, never as an internal failure of what follows. -pub fn check_supported(ast: &Ast) -> Result<(), CompileError> { - for (i, n) in ast.nodes.iter().enumerate() { - let feature: Option<&'static str> = match n.kind { - NodeKind::Module - | NodeKind::ImportDeclaration - | NodeKind::ImportClause - | NodeKind::NameSpaceImport - | NodeKind::NamedImports - | NodeKind::ImportSpecifier - | NodeKind::WithClause - | NodeKind::ImportAttribute - | NodeKind::ExportDeclaration - | NodeKind::NamedExports - | NodeKind::ExportSpecifier - | NodeKind::ImportMeta => Some("modules"), - NodeKind::ImportCall => Some("dynamic import"), - NodeKind::ForOfStatement if n.flag => Some("generators and async functions"), - NodeKind::WithStatement => Some("with statements"), - NodeKind::GeneratorDeclaration - | NodeKind::GeneratorExpression - | NodeKind::AsyncFunctionDeclaration - | NodeKind::AsyncFunctionExpression - | NodeKind::AsyncGeneratorDeclaration - | NodeKind::AsyncGeneratorExpression - | NodeKind::AsyncArrowFunction - | NodeKind::YieldExpression - | NodeKind::AwaitExpression => Some("generators and async functions"), - NodeKind::RegularExpressionLiteral => Some("regular expression literals"), - NodeKind::MethodDefinition - if matches!( - n.op, - AOp::GeneratorMethod | AOp::AsyncMethod | AOp::AsyncGeneratorMethod - ) => - { - Some("generator or async methods") - } - NodeKind::IfStatement | NodeKind::LabelledStatement => { - // Annex B.3.2: a function declaration as the body of an `if` or a - // label. - let mut found = false; - for &c in &n.children { - if node(ast, c as usize)?.kind == NodeKind::FunctionDeclaration { - found = true; - } - } - if found { - Some("a function declaration as the body of an if or a label") - } else { - None - } - } - _ => None, - }; - if let Some(feature) = feature { - return Err(CompileError::NotYetCompiled { - feature, - span: span_of(ast, i), - }); - } - } - Ok(()) -} - /// Whether `stmts` start with a Use Strict Directive (§11.2.1): an expression /// statement that is a string literal written exactly `"use strict"` or /// `'use strict'`, among the directives that start the list. @@ -465,7 +487,10 @@ impl Builder<'_> { fn fn_scope(&self, mut s: u32) -> Result { loop { let sc = self.scope(s)?; - if matches!(sc.kind, SKind::Function | SKind::Script) { + if matches!( + sc.kind, + SKind::Function | SKind::Script | SKind::Body | SKind::Module + ) { return Ok(s); } s = sc.parent; @@ -543,6 +568,10 @@ pub fn analyze(ast: &Ast, src: &[u32]) -> Result { special: HashMap::new(), hidden: HashMap::new(), global_lexicals: Vec::new(), + dyn_binding: HashMap::new(), + eval_calls: Vec::new(), + implicit_blocks: HashMap::new(), + default_binding: NONE, }, intern: HashMap::new(), }; @@ -550,9 +579,12 @@ pub fn analyze(ast: &Ast, src: &[u32]) -> Result { scopes_pass(&mut b, src, &parent)?; derived_pass(&mut b, src)?; declare_pass(&mut b, &parent)?; + with_pass(&mut b)?; + var_arguments_pass(&mut b)?; annexb_pass(&mut b)?; funcname_pass(&mut b)?; reference_pass(&mut b, &parent)?; + eval_pass(&mut b)?; finish_pass(&mut b)?; Ok(b.a) } @@ -599,13 +631,12 @@ fn scopes_pass(b: &mut Builder<'_>, src: &[u32], parent: &[u32]) -> Result<(), C NONE } else { let pn = node(ast, p as usize)?; - let outer_first = matches!( + let outer_first = (matches!( pn.kind, - NodeKind::SwitchStatement - | NodeKind::FunctionDeclaration - | NodeKind::FunctionExpression - | NodeKind::MethodDefinition - ) && pn.children.first() == Some(&i32_); + NodeKind::SwitchStatement | NodeKind::MethodDefinition | NodeKind::WithStatement + ) || is_decl_kind(pn.kind) + || is_fexpr_kind(pn.kind)) + && pn.children.first() == Some(&i32_); let from = if outer_first { &b.a.owner_scope } else { &ctx }; let mut o = from.get(p as usize).copied().unwrap_or(NONE); // A field's initializer runs in the class's initializer function, @@ -630,11 +661,40 @@ fn scopes_pass(b: &mut Builder<'_>, src: &[u32], parent: &[u32]) -> Result<(), C } o }; + // Annex B.3.4†: a function declaration as the body of an `if` is treated + // as if it were in a block of its own. + let owner = if nd.kind == NodeKind::FunctionDeclaration + && p != NONE + && node(ast, p as usize)?.kind == NodeKind::IfStatement + && owner != NONE + { + let sid = + u32::try_from(b.a.scopes.len()).map_err(|_| CompileError::TooLarge("scopes"))?; + let (func, strict) = { + let os = b.scope(owner)?; + (os.func, os.strict) + }; + b.a.scopes.push(Scope { + parent: owner, + func, + kind: SKind::Block, + strict, + bindings: Vec::new(), + names: HashMap::new(), + funcs: Vec::new(), + env_size: 0, + }); + b.a.implicit_blocks.insert(i32_, sid); + sid + } else { + owner + }; if let Some(o) = b.a.owner_scope.get_mut(i) { *o = owner; } let kind = match nd.kind { NodeKind::Script => Some(SKind::Script), + NodeKind::Module => Some(SKind::Module), k if is_function(k) => Some(SKind::Function), NodeKind::BlockStatement => Some(SKind::Block), NodeKind::ForStatement => { @@ -657,6 +717,8 @@ fn scopes_pass(b: &mut Builder<'_>, src: &[u32], parent: &[u32]) -> Result<(), C } } NodeKind::SwitchStatement => Some(SKind::Switch), + NodeKind::WithStatement => Some(SKind::With), + NodeKind::FunctionBody if body_scope_needed(ast, parent, i)? => Some(SKind::Body), NodeKind::Catch => Some(SKind::Catch), NodeKind::ClassDeclaration | NodeKind::ClassExpression => Some(SKind::Class), _ => None, @@ -692,12 +754,24 @@ fn scopes_pass(b: &mut Builder<'_>, src: &[u32], parent: &[u32]) -> Result<(), C b.a.funcs.push(Func { node: i32_, scope: sid, - arrow: nd.kind == NodeKind::ArrowFunction, + arrow: is_arrow_kind(nd.kind), parent_scope: owner, derived_ctor: false, }); (fid, strict) } + SKind::Module => { + let fid = u32::try_from(b.a.funcs.len()) + .map_err(|_| CompileError::TooLarge("functions"))?; + b.a.funcs.push(Func { + node: i32_, + scope: sid, + arrow: false, + parent_scope: owner, + derived_ctor: false, + }); + (fid, true) + } SKind::Class => (b.scope(owner)?.func, true), _ => (b.scope(owner)?.func, inherited_strict), }; @@ -833,6 +907,41 @@ fn declare_pass(b: &mut Builder<'_>, parent: &[u32]) -> Result<(), CompileError> _ => None, }; match nd.kind { + NodeKind::BindingIdentifier => { + // The local names of an import declaration (§16.2.1.6.4† + // InitializeEnvironment steps 7 and 8). + let p = parent.get(i).copied().unwrap_or(NONE); + if p != NONE { + let kind = match node(ast, p as usize)?.kind { + NodeKind::ImportClause | NodeKind::ImportSpecifier => Some(BKind::Import), + NodeKind::NameSpaceImport => Some(BKind::Const), + _ => None, + }; + if let (Some(kind), Some(&name)) = (kind, b.a.name_of.get(i)) { + b.declare(owner, name, kind)?; + } + } + } + NodeKind::ExportDeclaration => { + // `export default` of an expression or an anonymous class binds + // `*default*` (§16.2.3.7† ExportDeclaration evaluation). + let anonymous_class = nd.op == AOp::ExportDefaultClass + && match nd.children.first() { + Some(&c) => match node(ast, c as usize)?.children.first() { + Some(&nm) => { + node(ast, nm as usize)?.kind != NodeKind::BindingIdentifier + } + None => false, + }, + None => false, + }; + if nd.op == AOp::ExportDefaultExpression || anonymous_class { + let name = b.intern("*default*".chars().map(u32::from).collect())?; + let bid = b.declare(owner, name, BKind::Let)?; + b.binding_mut(bid)?.init_end = nd.end; + b.a.default_binding = bid; + } + } NodeKind::VariableDeclaration => { let Some(&target) = nd.children.first() else { return Err(CompileError::Internal("var declaration has no target")); @@ -897,17 +1006,22 @@ fn declare_pass(b: &mut Builder<'_>, parent: &[u32]) -> Result<(), CompileError> b.binding_mut(bid)?.init_end = nd.end; } } - NodeKind::FunctionDeclaration => { - let Some(name) = first_name else { - return Err(CompileError::Internal("function declaration has no name")); + k if is_decl_kind(k) => { + // `export default function () {}` binds `*default*`. + let name = match first_name { + Some(n) => n, + None => b.intern("*default*".chars().map(u32::from).collect())?, }; let owner_kind = b.scope(owner)?.kind; b.scope_mut(owner)?.funcs.push(node_id); match owner_kind { SKind::Script => {} - SKind::Function => { + SKind::Function | SKind::Body | SKind::Module => { let bid = b.declare(owner, name, BKind::Var)?; b.binding_mut(bid)?.decl_fn = node_id; + if first_name.is_none() { + b.a.default_binding = bid; + } } _ => { let bid = b.declare(owner, name, BKind::Let)?; @@ -936,7 +1050,7 @@ fn declare_pass(b: &mut Builder<'_>, parent: &[u32]) -> Result<(), CompileError> } } } - NodeKind::ArrowFunction => { + k if is_arrow_kind(k) => { if let Some(name) = first_name { let own = b.a.own_scope.get(i).copied().unwrap_or(NONE); let bid = b.declare(own, name, BKind::Var)?; @@ -988,7 +1102,10 @@ fn declare_vars(b: &mut Builder<'_>, owner: u32, target: u32) -> Result<(), Comp let fs = b.fn_scope(owner)?; for id in bound_idents(b.ast, target)? { let name = b.a.name_of.get(id as usize).copied().unwrap_or(NONE); - if b.scope(fs)?.kind == SKind::Function { + if matches!( + b.scope(fs)?.kind, + SKind::Function | SKind::Body | SKind::Module + ) { b.declare(fs, name, BKind::Var)?; } else { b.a.global_vars.push(id); @@ -1044,7 +1161,11 @@ fn annexb_pass(b: &mut Builder<'_>) -> Result<(), CompileError> { } let owner = b.a.owner_scope.get(i).copied().unwrap_or(NONE); let osc = b.scope(owner)?; - if matches!(osc.kind, SKind::Script | SKind::Function) || osc.strict { + if matches!( + osc.kind, + SKind::Script | SKind::Function | SKind::Body | SKind::Module + ) || osc.strict + { continue; } let Some(&nm) = nd.children.first() else { @@ -1057,7 +1178,7 @@ fn annexb_pass(b: &mut Builder<'_>) -> Result<(), CompileError> { let mut blocked = false; let fs = loop { let sc = b.scope(s)?; - if matches!(sc.kind, SKind::Function | SKind::Script) { + if matches!(sc.kind, SKind::Function | SKind::Script | SKind::Body) { break s; } if let Some(&bid) = sc.names.get(&name) @@ -1077,6 +1198,14 @@ fn annexb_pass(b: &mut Builder<'_>) -> Result<(), CompileError> { { blocked = true; } + // The parameters are in the function scope outside a body scope. + if fsc.kind == SKind::Body + && let Some(&bid) = b.scope(fsc.parent)?.names.get(&name) + && let Some(bd) = b.a.bindings.get(bid as usize) + && bd.param != NONE + { + blocked = true; + } if blocked { continue; } @@ -1096,7 +1225,7 @@ fn annexb_pass(b: &mut Builder<'_>) -> Result<(), CompileError> { fn funcname_pass(b: &mut Builder<'_>) -> Result<(), CompileError> { let ast = b.ast; for (i, nd) in ast.nodes.iter().enumerate() { - if nd.kind != NodeKind::FunctionExpression { + if !is_fexpr_kind(nd.kind) { continue; } let Some(&nm) = nd.children.first() else { @@ -1221,35 +1350,239 @@ fn arguments_binding(b: &mut Builder<'_>, owner: u32, r: u32) -> Result match nd.children.get(1) { - Some(&fp) => { - node(b.ast, fp as usize)?.kind == NodeKind::FormalParameters - && is_simple_params(b.ast, fp)? + capture_mapped_params(b, home_scope, fnode)?; + } + if here != f { + b.binding_mut(bid)?.captured = true; + } + Ok(bid) +} + +/// Whether the `FunctionBody` node `i` gets a scope of its own, apart from the +/// function's: its function has parameter expressions (§15.1.5† +/// ContainsExpression of the FormalParameters: an initializer, or a computed +/// property name in a pattern), which is when §10.2.11† step 28 makes a separate +/// environment for the body's `var`s, so that a closure in a default value does +/// not see them. +fn body_scope_needed(ast: &Ast, parent: &[u32], i: usize) -> Result { + let f = parent.get(i).copied().unwrap_or(NONE); + if f == NONE { + return Ok(false); + } + let fnn = node(ast, f as usize)?; + if !is_function(fnn.kind) || fnn.children.len() < 2 { + return Ok(false); + } + let Some(&fp) = fnn.children.get(fnn.children.len().saturating_sub(2)) else { + return Ok(false); + }; + if node(ast, fp as usize)?.kind != NodeKind::FormalParameters { + return Ok(false); + } + let mut stack = vec![fp]; + while let Some(n) = stack.pop() { + let nd = node(ast, n as usize)?; + match nd.kind { + NodeKind::FormalParameters + | NodeKind::ObjectBindingPattern + | NodeKind::ArrayBindingPattern + | NodeKind::BindingRestElement + | NodeKind::BindingRestProperty + | NodeKind::FunctionRestParameter => stack.extend(nd.children.iter().copied()), + NodeKind::BindingIdentifier | NodeKind::Absent => {} + NodeKind::BindingElement => { + if let Some(&init) = nd.children.get(1) + && node(ast, init as usize)?.kind != NodeKind::Absent + { + return Ok(true); } - None => false, - }, - None => false, - }; - if !strict && simple { - let ids: Vec = b.scope(home_scope)?.bindings.clone(); - for id in ids { - let bd = b.binding_mut(id)?; - if bd.param != NONE { - bd.captured = true; + stack.extend(nd.children.first().copied()); + } + NodeKind::BindingProperty => { + if nd.children.len() == 2 + && let Some(&key) = nd.children.first() + && node(ast, key as usize)?.kind == NodeKind::ComputedPropertyName + { + return Ok(true); } + stack.extend(nd.children.last().copied()); } + _ => return Ok(true), } } - if here != f { + Ok(false) +} + +/// The binding object of each `with` statement's scope (always captured: the +/// scope's environment object holds it in slot 0). +fn with_pass(b: &mut Builder<'_>) -> Result<(), CompileError> { + let name = b.intern(vec![u32::MAX - 5])?; + for s in 0..b.a.scopes.len() { + let sid = u32::try_from(s).map_err(|_| CompileError::TooLarge("scopes"))?; + if b.scope(sid)?.kind != SKind::With { + continue; + } + let bid = b.declare(sid, name, BKind::WithObject)?; b.binding_mut(bid)?.captured = true; + b.a.dyn_binding.insert(sid, bid); } - Ok(bid) + Ok(()) +} + +/// §10.2.11† FunctionDeclarationInstantiation step 27.b: a `var arguments` in +/// a function that has an `arguments` object shares its binding (the object +/// is bound to `arguments` in step 22, before the `var`s, which skip a name +/// that is bound already). The function has no parameter, function +/// declaration or lexical declaration of that name; the binding that +/// `declare_pass` made for the `var` becomes the `arguments` binding. +fn var_arguments_pass(b: &mut Builder<'_>) -> Result<(), CompileError> { + let arguments = b.intern("arguments".chars().map(u32::from).collect())?; + for bid in 0..b.a.bindings.len() { + let bd = + b.a.bindings + .get(bid) + .ok_or(CompileError::Internal("binding"))?; + if bd.name != arguments || bd.kind != BKind::Var || bd.param != NONE || bd.decl_fn != NONE { + continue; + } + let sc = b.scope(bd.scope)?; + if !matches!(sc.kind, SKind::Function | SKind::Body) { + continue; + } + let in_body = sc.kind == SKind::Body; + let Some(f) = b.a.funcs.get(sc.func as usize) else { + continue; + }; + let (fnode, fscope, arrow) = (f.node, f.scope, f.arrow); + if arrow || (!in_body && fscope != bd.scope) { + continue; + } + // The synthetic functions of a class (initializers) and static blocks + // have no `arguments`. + let nk = node(b.ast, fnode as usize)?.kind; + if matches!( + nk, + NodeKind::ClassDeclaration | NodeKind::ClassExpression | NodeKind::ClassStaticBlock + ) { + continue; + } + if in_body { + // The `arguments` object is bound in the parameter scope (step 22), + // and the body's `var` of that name starts with its value + // (step 28.f.i.2: "initialValue be ! env.GetBindingValue(n, false)"). + b.declare(fscope, arguments, BKind::Arguments)?; + continue; + } + let id = u32::try_from(bid).map_err(|_| CompileError::TooLarge("bindings"))?; + b.binding_mut(id)?.kind = BKind::Arguments; + capture_mapped_params(b, fscope, fnode)?; + } + Ok(()) +} + +/// A sloppy function with a simple parameter list gets a mapped `arguments` +/// object, whose indices alias the parameters: they live in the environment +/// object (§10.4.4.7†). +fn capture_mapped_params( + b: &mut Builder<'_>, + home_scope: u32, + fnode: u32, +) -> Result<(), CompileError> { + let strict = b.scope(home_scope)?.strict; + let simple = match b.ast.nodes.get(fnode as usize) { + Some(nd) => match nd.children.get(1) { + Some(&fp) => { + node(b.ast, fp as usize)?.kind == NodeKind::FormalParameters + && is_simple_params(b.ast, fp)? + } + None => false, + }, + None => false, + }; + if !strict && simple { + let ids: Vec = b.scope(home_scope)?.bindings.clone(); + for id in ids { + let bd = b.binding_mut(id)?; + if bd.param != NONE { + bd.captured = true; + } + } + } + Ok(()) +} + +/// Direct eval (§13.3.6.1† step 6, §19.2.1.1†): the code that `eval` runs can +/// name any binding visible at the call, by name, and can declare `var`s in the +/// calling function. So every binding of every scope that encloses a direct +/// eval is captured (an environment slot, which the eval code's compiler finds +/// by name through [`crate::bytecode::EvalScopeInfo`]), the caller's `this`, +/// `new.target`, function and `arguments` are bindings too, and a sloppy +/// caller's function scope has a binding for the variable object. +fn eval_pass(b: &mut Builder<'_>) -> Result<(), CompileError> { + let ast = b.ast; + let eval = b.intern("eval".chars().map(u32::from).collect())?; + let arguments = b.intern("arguments".chars().map(u32::from).collect())?; + let this_name = b.intern(vec![u32::MAX])?; + let nt_name = b.intern(vec![u32::MAX - 2])?; + let callee_name = b.intern(vec![u32::MAX - 3])?; + let vars_name = b.intern(vec![u32::MAX - 4])?; + let mut calls: Vec = Vec::new(); + for (i, nd) in ast.nodes.iter().enumerate() { + if nd.kind != NodeKind::CallExpression { + continue; + } + let Some(&c) = nd.children.first() else { + continue; + }; + if node(ast, c as usize)?.kind == NodeKind::IdentifierReference + && b.a.name_of.get(c as usize).copied() == Some(eval) + { + calls.push(u32::try_from(i).map_err(|_| CompileError::TooLarge("nodes"))?); + } + } + for &i in &calls { + let owner = b.a.owner_scope.get(i as usize).copied().unwrap_or(NONE); + let (_, home) = home_func(b, owner)?; + if home != 0 { + let (home_scope, home_node, derived) = match b.a.funcs.get(home as usize) { + Some(f) => (f.scope, f.node, f.derived_ctor), + None => return Err(CompileError::Internal("no such function")), + }; + if !derived { + b.declare(home_scope, this_name, BKind::This)?; + } + b.declare(home_scope, nt_name, BKind::NewTarget)?; + b.declare(home_scope, callee_name, BKind::Callee)?; + let nk = node(ast, home_node as usize)?.kind; + if !matches!( + nk, + NodeKind::ClassDeclaration | NodeKind::ClassExpression | NodeKind::ClassStaticBlock + ) { + let r = b.resolve(owner, arguments)?; + arguments_binding(b, owner, r)?; + } + } + if !b.scope(owner)?.strict { + let fs = b.fn_scope(owner)?; + if matches!(b.scope(fs)?.kind, SKind::Function | SKind::Body) { + let bid = b.declare(fs, vars_name, BKind::EvalVars)?; + b.a.dyn_binding.insert(fs, bid); + } + } + b.a.eval_calls.push(i); + } + for &i in &calls { + let mut s = b.a.owner_scope.get(i as usize).copied().unwrap_or(NONE); + while s != NONE { + let ids: Vec = b.scope(s)?.bindings.clone(); + for id in ids { + b.binding_mut(id)?.captured = true; + } + s = b.scope(s)?.parent; + } + } + Ok(()) } /// Resolves every reference, marking captured bindings. @@ -1272,7 +1605,7 @@ fn reference_pass(b: &mut Builder<'_>, parent: &[u32]) -> Result<(), CompileErro } } NodeKind::FormalParameters => roots.extend(nd.children.iter().copied()), - NodeKind::ArrowFunction => { + k if is_arrow_kind(k) => { if let Some(&t) = nd.children.first() && node(ast, t as usize)?.kind == NodeKind::BindingIdentifier { @@ -1356,6 +1689,13 @@ fn finish_pass(b: &mut Builder<'_>) -> Result<(), CompileError> { .get(s) .map(|sc| sc.bindings.clone()) .unwrap_or_default(); + // Every binding of a module's scope has a slot of the module + // environment, so that other modules can reach an export. + if b.a.scopes.get(s).map(|sc| sc.kind) == Some(SKind::Module) { + for &bid in &ids { + b.binding_mut(bid)?.captured = true; + } + } let mut size: u32 = 0; for bid in ids { let bd = b.binding_mut(bid)?; diff --git a/crates/clazel-js-vm/src/spec.rs b/crates/clazel-js-vm/src/spec.rs index 331c7205c..0c0a019eb 100644 --- a/crates/clazel-js-vm/src/spec.rs +++ b/crates/clazel-js-vm/src/spec.rs @@ -1220,6 +1220,25 @@ pub open spec fn op_wf(c: Code, op: Op) -> bool { Op::CreateMappedArguments { dst, env, k, .. } => reg_ok(c, dst) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]), Op::CreateRestArray { dst, .. } => reg_ok(c, dst), Op::ForInIterator { dst, src, .. } => reg_ok(c, dst) && reg_ok(c, src), + Op::ToObject { dst, src, .. } => reg_ok(c, dst) && reg_ok(c, src), + Op::WithHas { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(), + Op::WithGet { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(), + Op::WithSet { obj, key, val, .. } => reg_ok(c, obj) && key < c.consts.len() && reg_ok(c, val), + Op::WithDelete { dst, obj, key, .. } => reg_ok(c, dst) && reg_ok(c, obj) && key < c.consts.len(), + Op::DeleteGlobal { dst, key, .. } => reg_ok(c, dst) && key < c.consts.len(), + Op::DirectEval { dst, callee, this, args, argc, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && regs_ok(c, args, argc) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]), + Op::GeneratorStart => true, + Op::YieldDelegate { src, value, mode, .. } => reg_ok(c, src) && reg_ok(c, value) && reg_ok(c, mode), + Op::GetAsyncIterator { dst, src, .. } => reg_ok(c, dst) && reg_ok(c, src), + Op::IterCallMethod { dst, found, iter, arg, which, .. } => reg_ok(c, dst) && reg_ok(c, found) && reg_ok(c, iter) && reg_ok(c, arg) && which < 5, + Op::IterComplete { done, result, .. } => reg_ok(c, done) && reg_ok(c, result), + Op::IterValue { dst, result, .. } => reg_ok(c, dst) && reg_ok(c, result), + Op::ThrowIfNotObject { src, .. } => reg_ok(c, src), + Op::NewRegExp { dst, k, .. } => reg_ok(c, dst) && k < c.consts.len() && value_wf(c.consts[k as int]), + Op::GetImportBinding { dst, env, .. } => reg_ok(c, dst) && reg_ok(c, env), + Op::ImportCall { dst, specifier, options, .. } => reg_ok(c, dst) && reg_ok(c, specifier) && reg_ok(c, options), + Op::ImportMeta { dst, .. } => reg_ok(c, dst), + Op::DirectEvalSpread { dst, callee, this, args, env, k, .. } => reg_ok(c, dst) && reg_ok(c, callee) && reg_ok(c, this) && reg_ok(c, args) && reg_ok(c, env) && k < c.consts.len() && value_wf(c.consts[k as int]), } } diff --git a/crates/clazel-js-vm/tests/compile.rs b/crates/clazel-js-vm/tests/compile.rs index ea4a1b5d3..a27e09c5c 100644 --- a/crates/clazel-js-vm/tests/compile.rs +++ b/crates/clazel-js-vm/tests/compile.rs @@ -860,57 +860,22 @@ fn string_values_resolve_escapes() -> Result<(), String> { Ok(()) } -/// Constructs this slice does not compile: the feature named, never a panic. +/// A script and a module are compiled by different entry points: the wrong one +/// is an internal error, never a panic or a wrong program. #[test] -fn not_yet_compiled() -> Result<(), String> { - let cases: &[(&str, &str)] = &[ - ("function* g() {}", "generators and async functions"), - ("async function g() {}", "generators and async functions"), - ("async () => 1;", "generators and async functions"), - ("async function* g() {}", "generators and async functions"), - ("({ *g() {} });", "generator or async methods"), - ("class C { async m() {} }", "generator or async methods"), - ( - "function* g() { yield 1; }", - "generators and async functions", - ), - ("with (o) {}", "with statements"), - ("eval('1');", "direct eval"), - ("/a/;", "regular expression literals"), - ("delete x;", "delete of a variable"), - ("delete a?.b;", "delete of an optional chain"), - ( - "if (a) function f() {}", - "a function declaration as the body of an if or a label", - ), - ("import('m');", "dynamic import"), - ]; - for &(src, feature) in cases { - let c = cps(src); - let Ok(ast) = parse(&c, SourceGoal::Script) else { - return Err(format!("does not parse: {src}")); - }; - match compile_script(&ast, &c) { - Err(CompileError::NotYetCompiled { feature: f, span }) => { - assert_eq!(f, feature, "{src}"); - assert!(span.start <= span.end && span.end <= c.len(), "{src}"); - } - other => return Err(format!("{src}: expected not yet compiled, got {other:?}")), - } - } - // A module. +fn goal_mismatch() -> Result<(), String> { let c = cps("export var a = 1;"); - if let Ok(ast) = parse(&c, SourceGoal::Module) { - assert!(matches!( - compile_script(&ast, &c), - Err(CompileError::NotYetCompiled { - feature: "modules", - .. - }) - )); - } else { - return Err("module does not parse".to_string()); - } + let ast = parse(&c, SourceGoal::Module).map_err(|_| "module does not parse".to_string())?; + assert!(matches!( + compile_script(&ast, &c), + Err(CompileError::Internal(_)) + )); + let c = cps("var a = 1;"); + let ast = parse(&c, SourceGoal::Script).map_err(|_| "script does not parse".to_string())?; + assert!(matches!( + clazel_js_vm::compile::compile_module(&ast, &c), + Err(CompileError::Internal(_)) + )); Ok(()) } diff --git a/crates/clazel-js-vm/tests/conformance_interp.rs b/crates/clazel-js-vm/tests/conformance_interp.rs new file mode 100644 index 000000000..933751ef6 --- /dev/null +++ b/crates/clazel-js-vm/tests/conformance_interp.rs @@ -0,0 +1,494 @@ +//! Test262 through the interpreter: the scripts the compiler accepts, run in a +//! fresh [`Vm`] each, with a minimal native harness (item 7.8, first slice). +//! +//! The suite is every row of the generated Test262 table (the same table the +//! compiler's test reads), and each test lands in exactly one class: +//! +//! - **pass**: it ran in every mode it asks for (sloppy and strict, or the one +//! its flags name) and finished without throwing, or threw the error type +//! its `negative:` block names; +//! - **xfail**, an expected failure with a named reason (printed, with the +//! count of each): the test needs something this slice does not have; or +//! - **fail**: it ran and went wrong (an assertion failed, an exception other +//! than the expected one, the VM reported an internal error or ran out of +//! its step budget). This must be zero, and `cargo xtask verify` fails the +//! run otherwise. +//! +//! The harness is native, as phase-7.md asks of the shell's `$262` ("built +//! from public calls only"): `assert`, `assert.sameValue`, +//! `assert.notSameValue`, `assert.throws`, and a `$262` object with `global` +//! and `gc`; its other members report "not yet" when called. The real +//! `harness/assert.js` and `sta.js` need `new` and the `Error` built-ins, which +//! 7.9 provides, so a test that includes any other harness file +//! (`propertyHelper.js`, `compareArray.js`, ...) is an expected failure. +//! +//! A script that throws an uncaught `ReferenceError` for a name that is not +//! defined is classed by the name ("global X is missing"): the built-ins +//! (`Object`, `Array`, `Symbol`, ...) are 7.9, and a test that reaches one is +//! waiting for it. A test that catches the error itself is judged on what it +//! does next. +//! +//! The counts are in units of tests, not runs. + +use clazel_js_heap::value::{Decoded, Value}; +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; +use clazel_js_vm::bytecode::Program; +use clazel_js_vm::compile::{CompileError, compile_script}; +use clazel_js_vm::interp::{Abrupt, Config, Vm}; +use std::collections::BTreeMap; +use std::path::PathBuf; +use std::sync::OnceLock; + +/// A test's row in the generated table. +struct Case { + path: &'static str, + flags: &'static [&'static str], + features: &'static [&'static str], + includes: &'static [&'static str], + negative: Option<(&'static str, &'static str)>, + expect: Expect, +} + +/// Whether a test is expected to pass, and if not why. +enum Expect { + Run, + Fail(&'static str), +} + +include!("../../clazel-js-lexer/tests/generated/test262.rs"); + +fn root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../clazel-js-lexer/tests/data/test262/test") +} + +/// The most steps (back-edges and calls) one test run may take. +const BUDGET: u64 = 2_000_000; + +// ----------------------------------------------------------------------- +// The native harness +// ----------------------------------------------------------------------- + +/// Throws what a failed assertion throws: an object with `name` +/// `Test262Error` and the message. +fn test262_error(vm: &mut Vm, msg: &str) -> Abrupt { + let make = |vm: &mut Vm| -> Result { + let o = vm.new_object()?; + let n = vm.new_string("Test262Error")?; + let m = vm.new_string(msg)?; + vm.define_property(o, "name", n)?; + vm.define_property(o, "message", m)?; + Ok(o) + }; + match make(vm) { + Ok(o) => Abrupt::Throw(o), + Err(e) => e, + } +} + +/// The message argument of an assertion, as text. +fn message(vm: &mut Vm, v: Option<&Value>) -> String { + match v { + Some(v) if *v != Value::UNDEFINED => vm.describe(*v), + _ => String::new(), + } +} + +/// SameValue, §7.2.10 (strings by content, Numbers by value with NaN equal to +/// NaN and `+0` distinct from `-0`). +fn same_value(vm: &Vm, a: Value, b: Value) -> bool { + use clazel_js_vm::numeric::num; + if let (Some(x), Some(y)) = (num(a), num(b)) { + let (x, y) = (x.to_f64(), y.to_f64()); + return if x.is_nan() || y.is_nan() { + x.is_nan() && y.is_nan() + } else { + x.to_bits() == y.to_bits() + }; + } + if let (Some(x), Some(y)) = (vm.heap().string(a), vm.heap().string(b)) { + return x == y; + } + a == b +} + +fn assert_true(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + if args.first() == Some(&Value::TRUE) { + return Ok(Value::UNDEFINED); + } + let m = message(vm, args.get(1)); + Err(test262_error( + vm, + &format!("Expected true but got something else. {m}"), + )) +} + +fn assert_same_value(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let (a, b) = ( + args.first().copied().unwrap_or(Value::UNDEFINED), + args.get(1).copied().unwrap_or(Value::UNDEFINED), + ); + if same_value(vm, a, b) { + return Ok(Value::UNDEFINED); + } + let (da, db) = (vm.describe(a), vm.describe(b)); + let m = message(vm, args.get(2)); + Err(test262_error( + vm, + &format!("Expected SameValue({da}, {db}) to be true. {m}"), + )) +} + +fn assert_not_same_value(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let (a, b) = ( + args.first().copied().unwrap_or(Value::UNDEFINED), + args.get(1).copied().unwrap_or(Value::UNDEFINED), + ); + if !same_value(vm, a, b) { + return Ok(Value::UNDEFINED); + } + let da = vm.describe(a); + let m = message(vm, args.get(2)); + Err(test262_error( + vm, + &format!("Expected SameValue({da}, {da}) to be false. {m}"), + )) +} + +fn assert_throws(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let ctor = args.first().copied().unwrap_or(Value::UNDEFINED); + let f = args.get(1).copied().unwrap_or(Value::UNDEFINED); + match vm.call(f, Value::UNDEFINED, &[]) { + Ok(_) => Err(test262_error( + vm, + "Expected an exception, but none was thrown", + )), + Err(Abrupt::Throw(e)) => { + // The real assert.js compares `thrown.constructor` with the + // expected constructor; the errors the VM itself throws have no + // constructor yet (7.9), so they are compared by `name`. + let same_ctor = vm.get_named(e, "constructor")? == ctor && ctor != Value::UNDEFINED; + let want = vm.get_named(ctor, "name")?; + let got = vm.get_named(e, "name")?; + if same_ctor || (want != Value::UNDEFINED && same_value(vm, want, got)) { + Ok(Value::UNDEFINED) + } else { + let d = vm.describe(e); + Err(test262_error( + vm, + &format!("Expected a different error, got {d}"), + )) + } + } + Err(other) => Err(other), + } +} + +fn not_yet(_vm: &mut Vm, _this: Value, _args: &[Value]) -> Result { + Err(Abrupt::NotYet("$262 member".into())) +} + +fn ignore(_vm: &mut Vm, _this: Value, _args: &[Value]) -> Result { + Ok(Value::UNDEFINED) +} + +/// `harness/sta.js`, compiled once: it defines `Test262Error`. +fn sta_program() -> Option<&'static Program> { + static STA: OnceLock> = OnceLock::new(); + STA.get_or_init(|| { + let path = root().join("../harness/sta.js"); + let text = std::fs::read_to_string(path).ok()?; + let src: Vec = text.chars().map(u32::from).collect(); + let ast = parse(&src, SourceGoal::Script).ok()?; + compile_script(&ast, &src).ok() + }) + .as_ref() +} + +/// Installs `assert` (unless `raw`), `print` and `$262`, and runs the real +/// `sta.js` (unless `raw`). +fn install_harness(vm: &mut Vm, raw: bool) -> Result<(), Abrupt> { + if !raw { + let sta = sta_program().ok_or(Abrupt::Internal("sta.js does not compile"))?; + let script = vm.load(sta)?; + vm.run(&script)?; + let assert = vm.new_native("assert", 2, assert_true)?; + for (name, len, f) in [ + ( + "sameValue", + 3, + assert_same_value as clazel_js_vm::interp::NativeFn, + ), + ("notSameValue", 3, assert_not_same_value), + ("throws", 3, assert_throws), + ] { + let n = vm.new_native(name, len, f)?; + vm.define_property(assert, name, n)?; + } + vm.define_global("assert", assert)?; + } + let print = vm.new_native("print", 1, ignore)?; + vm.define_global("print", print)?; + let host = vm.new_object()?; + let g = vm.global(); + vm.define_property(host, "global", g)?; + for name in ["gc", "createRealm", "evalScript", "detachArrayBuffer"] { + let f = vm.new_native(name, 0, if name == "gc" { ignore } else { not_yet })?; + vm.define_property(host, name, f)?; + } + vm.define_global("$262", host) +} + +// ----------------------------------------------------------------------- +// Running and classifying +// ----------------------------------------------------------------------- + +/// What happened to one run of a test. +enum Outcome { + Pass, + /// An expected failure, with its category. + Xfail(String), + /// An unexpected failure, with what happened. + Fail(String), +} + +/// The text of a thrown error's `name` and `message`, if it is an object. +fn thrown(vm: &mut Vm, v: Value) -> (Option, Option) { + if !matches!(v.decode(), Decoded::Ref(_)) { + return (None, None); + } + let name = vm.get_named(v, "name").ok().and_then(|n| vm.string_text(n)); + // An error made by `new Test262Error` has no own `name`: its constructor's + // name stands for it. + let name = name.or_else(|| { + let c = vm.get_named(v, "constructor").ok()?; + let n = vm.get_named(c, "name").ok()?; + vm.string_text(n) + }); + let msg = vm + .get_named(v, "message") + .ok() + .and_then(|n| vm.string_text(n)); + (name, msg) +} + +/// Runs one source in a new VM and classifies the result. +fn run_one(case: &Case, src: &[u32], goal: SourceGoal) -> Outcome { + let Ok(ast) = parse(src, goal) else { + return Outcome::Fail("does not parse".into()); + }; + let program = match compile_script(&ast, src) { + Ok(p) => p, + Err(CompileError::NotYetCompiled { feature, .. }) => { + return Outcome::Xfail(format!("the compiler does not yet compile {feature}")); + } + Err(e) => return Outcome::Fail(format!("compile error: {e}")), + }; + let Ok(mut vm) = Vm::new(Config::default()) else { + return Outcome::Fail("could not make a VM".into()); + }; + if let Err(e) = install_harness(&mut vm, case.flags.contains(&"raw")) { + return Outcome::Fail(format!("harness: {e:?}")); + } + let script = match vm.load(&program) { + Ok(s) => s, + Err(Abrupt::NotYet(what)) => { + return Outcome::Xfail(format!("the VM does not yet load {what}")); + } + Err(e) => return Outcome::Fail(format!("load: {e:?}")), + }; + vm.set_budget(BUDGET); + let result = vm.run(&script); + let negative = case.negative.filter(|(phase, _)| *phase == "runtime"); + match result { + Ok(_) => match negative { + None => Outcome::Pass, + Some((_, ty)) => Outcome::Fail(format!("expected {ty}, nothing was thrown")), + }, + Err(Abrupt::NotYet(what)) => { + Outcome::Xfail(format!("the VM does not yet implement {what}")) + } + Err(Abrupt::Throw(e)) => { + let (name, msg) = thrown(&mut vm, e); + if let Some((_, ty)) = negative + && name.as_deref() == Some(ty) + { + return Outcome::Pass; + } + if name.as_deref() == Some("ReferenceError") + && let Some(m) = &msg + && let Some(var) = m.strip_suffix(" is not defined") + { + return Outcome::Xfail(format!("global {var} is missing (a built-in, 7.9)")); + } + if name.as_deref() == Some("TypeError") + && let Some(m) = &msg + && m == "undefined is not a function" + { + return Outcome::Xfail( + "calls a method that is not there (a built-in of 7.9: Function.prototype.call, Object.prototype.toString, ...)" + .into(), + ); + } + if name.as_deref() == Some("TypeError") + && let Some(m) = &msg + && m == "Cannot convert object to primitive value" + { + // ToPrimitive (§7.1.1) runs, but this VM has no + // `Object.prototype`, so no object has `valueOf` or `toString`. + return Outcome::Xfail( + "an object reaches ToPrimitive and has no valueOf or toString (the built-ins of 7.9 are not installed in this runner)" + .into(), + ); + } + match negative { + Some((_, ty)) => Outcome::Fail(format!("expected {ty}, got {}", vm.describe(e))), + None => Outcome::Fail(vm.describe(e)), + } + } + Err(Abrupt::Terminated) => Outcome::Fail("step budget exhausted".into()), + Err(e) => Outcome::Fail(format!("{e:?}")), + } +} + +fn bump(m: &mut BTreeMap, k: String) { + let e = m.entry(k).or_insert(0); + *e = e.saturating_add(1); +} + +/// The runs of a test: its goal and whether to prefix a Use Strict Directive. +fn runs(case: &Case) -> Vec<(SourceGoal, bool)> { + let has = |f: &str| case.flags.contains(&f); + if has("raw") || has("noStrict") { + vec![(SourceGoal::Script, false)] + } else if has("onlyStrict") { + vec![(SourceGoal::Script, true)] + } else { + vec![(SourceGoal::Script, false), (SourceGoal::Script, true)] + } +} + +/// The harness files the native harness stands in for. +const NATIVE_INCLUDES: [&str; 2] = ["assert.js", "sta.js"]; + +/// Tests that run and give a different result for a known reason, each with +/// its reason: expected failures. +const KNOWN_GAPS: &[(&str, &str)] = &[ + ( + "language/identifier-resolution/assign-to-global-undefined.js", + "strict assignment to an unresolvable reference throws before the right-hand side runs (§13.15.2 step 1.a, ResolveBinding first); SetGlobal checks when it runs, after it, and the op list has no resolve-first op", + ), + ( + "language/comments/S7.4_A5.js", + "calls eval, which is not there yet (a built-in, 7.9); the test's own catch turns the ReferenceError into a bare Test262Error, so the runner cannot class it by name (reached since array literals run, #290)", + ), +]; + +/// Classifies a test that is not run, or `None` if it should be. +fn not_run(case: &Case) -> Option { + if let Some((_, why)) = KNOWN_GAPS.iter().find(|(p, _)| *p == case.path) { + return Some((*why).to_string()); + } + if let Expect::Fail(reason) = case.expect { + return Some(format!("expected failure of the table: {reason}")); + } + if matches!(case.negative, Some((phase, _)) if phase == "parse" || phase == "early" || phase == "resolution") + { + return Some("negative parse/early test (the parser's evidence, not run)".into()); + } + if case.flags.contains(&"module") { + return Some("module (the compiler's third slice, an open pull request)".into()); + } + if case.flags.contains(&"async") { + return Some("async test (Promises and the job queue, 7.9)".into()); + } + if case.path.starts_with("harness/") { + return Some("test of the real harness files (replaced by the native harness)".into()); + } + case.includes + .iter() + .find(|i| !NATIVE_INCLUDES.contains(i)) + .map(|i| format!("includes {i} (needs built-ins, 7.9)")) +} + +#[test] +#[cfg_attr(miri, ignore = "reads files; Miri runs the other tests")] +fn test262_through_the_interpreter() -> Result<(), String> { + assert_eq!(TREE_SHA256.len(), 64); + let (mut pass, mut xfail, mut fail) = (0usize, 0usize, 0usize); + let mut categories: BTreeMap = BTreeMap::new(); + let mut failures: Vec = Vec::new(); + for case in TEST262 { + if let Some(reason) = not_run(case) { + xfail = xfail.saturating_add(1); + bump(&mut categories, reason); + continue; + } + let text = std::fs::read_to_string(root().join(case.path)) + .map_err(|e| format!("{}: {e}", case.path))?; + let body: Vec = text.chars().map(u32::from).collect(); + let mut worst = Outcome::Pass; + for (goal, strict) in runs(case) { + let mut src: Vec = Vec::new(); + if strict { + src.extend("\"use strict\";\n".chars().map(u32::from)); + } + src.extend(body.iter().copied()); + let out = run_one(case, &src, goal); + let mode = if strict { "strict" } else { "sloppy" }; + worst = match (worst, out) { + (Outcome::Fail(m), _) => Outcome::Fail(m), + (_, Outcome::Fail(m)) => Outcome::Fail(format!("{mode}: {m}")), + (Outcome::Xfail(m), _) => Outcome::Xfail(m), + (_, Outcome::Xfail(m)) => Outcome::Xfail(m), + (Outcome::Pass, Outcome::Pass) => Outcome::Pass, + }; + } + match worst { + Outcome::Pass => pass = pass.saturating_add(1), + Outcome::Xfail(reason) => { + xfail = xfail.saturating_add(1); + bump(&mut categories, reason); + } + Outcome::Fail(m) => { + fail = fail.saturating_add(1); + failures.push(format!("{} {:?}: {m}", case.path, case.features)); + } + } + } + let total = TEST262.len(); + println!( + "clazel-conformance: suite=\"Test262 through the interpreter\" pass={pass} xfail={xfail} fail={fail} total={total}" + ); + // Group the categories by their stem, so the print stays readable. + let mut stems: BTreeMap = BTreeMap::new(); + for (reason, n) in &categories { + let stem = if reason.starts_with("global ") { + "a global that is a built-in is missing (7.9)".to_string() + } else { + reason.clone() + }; + let e = stems.entry(stem).or_insert(0); + *e = e.saturating_add(*n); + } + let mut by_count: Vec<_> = stems.iter().collect(); + by_count.sort_by(|a, b| b.1.cmp(a.1).then(a.0.cmp(b.0))); + for (reason, n) in by_count { + println!(" expected failure: {n:6} {reason}"); + } + let mut globals: Vec<_> = categories + .iter() + .filter(|(r, _)| r.starts_with("global ")) + .collect(); + globals.sort_by(|a, b| b.1.cmp(a.1)); + for (reason, n) in globals.into_iter().take(15) { + println!(" {n:6} {reason}"); + } + for f in failures.iter().take(60) { + println!(" FAIL {f}"); + } + assert_eq!(pass.saturating_add(xfail).saturating_add(fail), total); + assert!(pass > 0); + assert_eq!(fail, 0, "{fail} tests failed unexpectedly"); + Ok(()) +} diff --git a/crates/clazel-js-vm/tests/differential.rs b/crates/clazel-js-vm/tests/differential.rs new file mode 100644 index 000000000..df6534c3a --- /dev/null +++ b/crates/clazel-js-vm/tests/differential.rs @@ -0,0 +1,667 @@ +//! A differential test: the interpreter against a model of `spec fn step`. +//! +//! `spec::step` is a Verus `spec fn`: it does not run, so there is no +//! executable form of it to compare against. This test therefore carries +//! `model`, a second transcription of `spec::fast_step` for the core ops and +//! the guarded ops on their fast paths, written from the spec's text over raw +//! `u64` bit patterns (the spec's own representation) with integer arithmetic +//! in `i64`/`i128` where `src/numeric.rs` uses checked `i32` operations and +//! `rem_euclid`, so that the two are not copies of each other. The spec's +//! uninterpreted rounding (`f64_add_finite` and the others) is the hardware's +//! `f64`, the assumption `spec.rs` lists. +//! +//! Random straight-line programs (with forward jumps) over eight registers +//! are built directly as bytecode, run by the interpreter and by the model, +//! and every register's bits are compared, so the encoding rule (int32 versus +//! double) is checked, not only the Number. The generator keeps the operands +//! of an arithmetic op Numbers, so every op takes its fast path, where the +//! spec defines the result. +//! +//! What this is not: a proof that the interpreter refines `step`. It covers +//! the ops listed in `OPS` below, on 8 registers, with edge-case operands; the +//! handler ops (calls, properties, globals) are outside it, as `step` leaves +//! them uninterpreted. + +use clazel_js_heap::value::Value; +use clazel_js_vm::bytecode::{ + Const, Flavor, FunctionCode, FunctionKind, Instr, Program, Span, check_program, +}; +use clazel_js_vm::interp::{Config, Vm}; + +/// The model of `spec::fast_step`. +mod model { + pub const UNDEFINED: u64 = 0xFFFA_0000_0000_0000; + pub const NULL: u64 = 0xFFFA_0000_0000_0001; + pub const FALSE: u64 = 0xFFFA_0000_0000_0002; + pub const TRUE: u64 = 0xFFFA_0000_0000_0003; + pub const CANON_NAN: u64 = 0x7FF8_0000_0000_0000; + + pub fn enc_int32(i: i32) -> u64 { + 0xFFF9_0000_0000_0000 | u64::from(i as u32) + } + pub fn enc_bool(b: bool) -> u64 { + if b { TRUE } else { FALSE } + } + pub fn enc_double(bits: u64) -> u64 { + if f64::from_bits(bits).is_nan() { + CANON_NAN + } else { + bits + } + } + pub fn is_int32(v: u64) -> bool { + v >> 48 == 0xFFF9 && (v >> 32) & 0xFFFF == 0 + } + pub fn is_double(v: u64) -> bool { + v >> 48 < 0xFFF9 + } + pub fn is_number(v: u64) -> bool { + is_int32(v) || is_double(v) + } + pub fn int32_of(v: u64) -> i64 { + i64::from(v as u32 as i32) + } + /// `spec::num_bits`: the double that represents the same Number. + pub fn num_bits(v: u64) -> u64 { + if is_int32(v) { + (int32_of(v) as f64).to_bits() + } else { + v + } + } + pub fn fits_i32(n: i64) -> bool { + (-2_147_483_648..=2_147_483_647).contains(&n) + } + fn f(v: u64) -> f64 { + f64::from_bits(num_bits(v)) + } + + // The int32 operands are widened to `i64`, where no operation below can + // overflow (|a * b| <= 2^62), so the wrapping forms are the exact ones. + pub fn arith_add(a: u64, b: u64) -> u64 { + let sum = int32_of(a).wrapping_add(int32_of(b)); + if is_int32(a) && is_int32(b) && fits_i32(sum) { + enc_int32(sum as i32) + } else { + enc_double((f(a) + f(b)).to_bits()) + } + } + pub fn arith_sub(a: u64, b: u64) -> u64 { + let diff = int32_of(a).wrapping_sub(int32_of(b)); + if is_int32(a) && is_int32(b) && fits_i32(diff) { + enc_int32(diff as i32) + } else { + enc_double((f(a) - f(b)).to_bits()) + } + } + pub fn arith_mul(a: u64, b: u64) -> u64 { + let p = int32_of(a).wrapping_mul(int32_of(b)); + if is_int32(a) + && is_int32(b) + && fits_i32(p) + && !(p == 0 && (int32_of(a) < 0 || int32_of(b) < 0)) + { + enc_int32(p as i32) + } else { + enc_double((f(a) * f(b)).to_bits()) + } + } + pub fn arith_div(a: u64, b: u64) -> u64 { + enc_double((f(a) / f(b)).to_bits()) + } + /// The truncated remainder of two integers, `d != 0` (`spec::int_rem_trunc`). + fn int_rem_trunc(n: i64, d: i64) -> i64 { + let m = n.abs().checked_rem(d.abs()).unwrap_or(0); + if n < 0 { m.wrapping_neg() } else { m } + } + pub fn arith_mod(a: u64, b: u64) -> u64 { + if is_int32(a) + && is_int32(b) + && int32_of(b) != 0 + && !(int_rem_trunc(int32_of(a), int32_of(b)) == 0 && int32_of(a) < 0) + { + enc_int32(int_rem_trunc(int32_of(a), int32_of(b)) as i32) + } else { + enc_double((f(a) % f(b)).to_bits()) + } + } + pub fn arith_neg(a: u64) -> u64 { + if is_int32(a) && int32_of(a) != 0 && int32_of(a) != -2_147_483_648 { + enc_int32(int32_of(a).wrapping_neg() as i32) + } else { + enc_double((-f(a)).to_bits()) + } + } + + /// `spec::f64_int32bit`: the integral part of a finite non-zero double, + /// modulo 2^32, from the bit fields (not through `f64` arithmetic). + fn int32bit(bits: u64) -> u32 { + let neg = bits >> 63 == 1; + let exp = ((bits >> 52) & 0x7FF) as i64; + let frac = bits & 0xF_FFFF_FFFF_FFFF; + let (m, e) = if exp == 0 { + (frac, -1074i64) + } else { + (frac | (1 << 52), exp.wrapping_sub(1075)) + }; + let magnitude: u128 = if e >= 0 { + u128::from(m) + .checked_shl(e as u32) + .filter(|_| e < 32) + .unwrap_or(0) + } else { + m.checked_shr(e.unsigned_abs() as u32).map_or(0, u128::from) + }; + let low = (magnitude & 0xFFFF_FFFF) as u32; + if neg { low.wrapping_neg() } else { low } + } + fn finite_nonzero(bits: u64) -> bool { + let x = f64::from_bits(bits); + x.is_finite() && x != 0.0 + } + pub fn to_int32(v: u64) -> i32 { + let b = num_bits(v); + if finite_nonzero(b) { + int32bit(b) as i32 + } else { + 0 + } + } + pub fn to_uint32(v: u64) -> u32 { + let b = num_bits(v); + if finite_nonzero(b) { int32bit(b) } else { 0 } + } + fn wrap_i32(n: i64) -> i32 { + n.rem_euclid(1i64 << 32) as u32 as i32 + } + + pub fn bit_and(a: u64, b: u64) -> u64 { + enc_int32(to_int32(a) & to_int32(b)) + } + pub fn bit_or(a: u64, b: u64) -> u64 { + enc_int32(to_int32(a) | to_int32(b)) + } + pub fn bit_xor(a: u64, b: u64) -> u64 { + enc_int32(to_int32(a) ^ to_int32(b)) + } + pub fn shl(a: u64, b: u64) -> u64 { + let count = to_uint32(b) & 31; + enc_int32(wrap_i32(i64::from(to_int32(a)).wrapping_mul(1i64 << count))) + } + pub fn shr(a: u64, b: u64) -> u64 { + let count = to_uint32(b) & 31; + enc_int32(i64::from(to_int32(a)).div_euclid(1i64 << count) as i32) + } + pub fn ushr(a: u64, b: u64) -> u64 { + let count = to_uint32(b) & 31; + let n = i64::from(to_uint32(a)) + .checked_div(1i64 << count) + .unwrap_or(0); + if fits_i32(n) { + enc_int32(n as i32) + } else { + enc_double((n as f64).to_bits()) + } + } + pub fn bit_not(a: u64) -> u64 { + enc_int32((-1i64).wrapping_sub(i64::from(to_int32(a))) as i32) + } + + /// `number_less_than`: `None` when either is NaN. + fn less_than(x: u64, y: u64) -> Option { + f(x).partial_cmp(&f(y)) + .map(|o| o == std::cmp::Ordering::Less) + } + pub fn rel_lt(x: u64, y: u64) -> bool { + less_than(x, y).unwrap_or(false) + } + pub fn rel_gt(x: u64, y: u64) -> bool { + less_than(y, x).unwrap_or(false) + } + pub fn rel_le(x: u64, y: u64) -> bool { + less_than(y, x).map(|r| !r).unwrap_or(false) + } + pub fn rel_ge(x: u64, y: u64) -> bool { + less_than(x, y).map(|r| !r).unwrap_or(false) + } + + /// `spec::strict_eq_fast` on values with no heap references. + pub fn strict_eq(a: u64, b: u64) -> bool { + if is_number(a) && is_number(b) { + f(a) == f(b) + } else { + a == b + } + } + /// `spec::to_boolean_fast`. + pub fn to_boolean(v: u64) -> bool { + if is_int32(v) { + int32_of(v) != 0 + } else if is_double(v) { + let x = f64::from_bits(v); + !(x == 0.0 || x.is_nan()) + } else { + v == TRUE + } + } +} + +/// What a register holds, for the generator. +#[derive(Clone, Copy, PartialEq)] +enum Kind { + Number, + Other, +} + +fn kind_of(kinds: &[Kind], r: u32) -> Kind { + kinds.get(r as usize).copied().unwrap_or(Kind::Other) +} + +fn set_kind(kinds: &mut [Kind], r: u32, k: Kind) { + if let Some(slot) = kinds.get_mut(r as usize) { + *slot = k; + } +} + +struct Rng(u64); +impl Rng { + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + fn below(&mut self, n: u64) -> u64 { + self.next().checked_rem(n).unwrap_or(0) + } + fn pick(&mut self, xs: &[T]) -> T { + let i = self.below(xs.len() as u64) as usize; + xs.get(i).copied().unwrap_or_default() + } +} + +const NREGS: u32 = 8; +const INTS: [i32; 14] = [ + 0, + 1, + -1, + 2, + 7, + 31, + 32, + 33, + 65_536, + 46_341, + i32::MAX, + i32::MIN, + 1 << 30, + -(1 << 30), +]; +const DOUBLES: [f64; 16] = [ + 0.5, + -0.5, + -0.0, + 0.0, + 1.5, + -2.5, + 1e300, + 1e-300, + 2_147_483_648.0, + -2_147_483_649.0, + 4_294_967_296.5, + 4_294_967_295.0, + 9_007_199_254_740_992.0, + f64::INFINITY, + f64::NEG_INFINITY, + 123_456_789.75, +]; + +/// The model's loader: an integral Number in int32 range other than `-0` is +/// an int32 (the documented rule of `Vm::load`), else the double. +fn model_const(bits: u64) -> u64 { + let d = f64::from_bits(bits); + if d == d.trunc() && d.abs() < 2_147_483_648.0 && bits != (-0.0f64).to_bits() { + model::enc_int32(d as i32) + } else { + bits + } +} + +/// One generated program, and the model's final registers. +fn generate(seed: u64, len: usize) -> (Program, [u64; NREGS as usize]) { + let mut rng = Rng(seed | 1); + let mut consts = Vec::new(); + for d in DOUBLES { + consts.push(Const::Number(d.to_bits())); + } + let mut instrs: Vec = Vec::new(); + let mut kinds = [Kind::Other; NREGS as usize]; + // Every register starts as a Number, so any op may use any register. + for r in 0..NREGS { + if rng.below(2) == 0 { + instrs.push(Instr::LoadInt { + dst: r, + imm: rng.pick(&INTS), + }); + } else { + instrs.push(Instr::LoadConst { + dst: r, + k: rng.below(DOUBLES.len() as u64) as u32, + }); + } + set_kind(&mut kinds, r, Kind::Number); + } + let numbers = |kinds: &[Kind; NREGS as usize]| -> Vec { + (0..NREGS) + .filter(|&r| kind_of(kinds, r) == Kind::Number) + .collect() + }; + while instrs.len() < len { + let nums = numbers(&kinds); + let any = |rng: &mut Rng| rng.below(u64::from(NREGS)) as u32; + let dst = any(&mut rng); + if nums.is_empty() { + // No register holds a Number: make one, so every operand pick is + // a Number and every arithmetic op takes its fast path. + instrs.push(Instr::LoadInt { + dst, + imm: rng.pick(&INTS), + }); + set_kind(&mut kinds, dst, Kind::Number); + continue; + } + let (a, b) = (rng.pick(&nums), rng.pick(&nums)); + let (ins, kind) = match rng.below(26) { + 0 => (Instr::Add { dst, a, b }, Kind::Number), + 1 => (Instr::Sub { dst, a, b }, Kind::Number), + 2 => (Instr::Mul { dst, a, b }, Kind::Number), + 3 => (Instr::Div { dst, a, b }, Kind::Number), + 4 => (Instr::Mod { dst, a, b }, Kind::Number), + 5 => (Instr::Neg { dst, src: a }, Kind::Number), + 6 => (Instr::Inc { dst, src: a }, Kind::Number), + 7 => (Instr::Dec { dst, src: a }, Kind::Number), + 8 => (Instr::BitAnd { dst, a, b }, Kind::Number), + 9 => (Instr::BitOr { dst, a, b }, Kind::Number), + 10 => (Instr::BitXor { dst, a, b }, Kind::Number), + 11 => (Instr::Shl { dst, a, b }, Kind::Number), + 12 => (Instr::Shr { dst, a, b }, Kind::Number), + 13 => (Instr::UShr { dst, a, b }, Kind::Number), + 14 => (Instr::BitNot { dst, src: a }, Kind::Number), + 15 => (Instr::Lt { dst, a, b }, Kind::Other), + 16 => (Instr::Le { dst, a, b }, Kind::Other), + 17 => (Instr::Gt { dst, a, b }, Kind::Other), + 18 => (Instr::Ge { dst, a, b }, Kind::Other), + 19 => { + let (x, y) = (any(&mut rng), any(&mut rng)); + if rng.below(2) == 0 { + (Instr::StrictEq { dst, a: x, b: y }, Kind::Other) + } else { + (Instr::StrictNe { dst, a: x, b: y }, Kind::Other) + } + } + 20 => ( + Instr::Not { + dst, + src: any(&mut rng), + }, + Kind::Other, + ), + 21 => (Instr::ToNumeric { dst, src: a }, Kind::Number), + 22 => match rng.below(5) { + 0 => (Instr::LoadUndefined { dst }, Kind::Other), + 1 => (Instr::LoadNull { dst }, Kind::Other), + 2 => ( + Instr::LoadBool { + dst, + flag: rng.below(2) == 0, + }, + Kind::Other, + ), + 3 => ( + Instr::LoadInt { + dst, + imm: rng.pick(&INTS), + }, + Kind::Number, + ), + _ => ( + Instr::LoadConst { + dst, + k: rng.below(DOUBLES.len() as u64) as u32, + }, + Kind::Number, + ), + }, + 23 => { + let src = any(&mut rng); + (Instr::Move { dst, src }, kind_of(&kinds, src)) + } + 24 => { + // A forward jump over the next one to three ops (the target + // is fixed up below); the kinds after it are those of the + // fall-through path only if the ops it skips keep them, so + // the generator uses the weaker of both: it emits the jump, + // and the skipped ops are then limited to ones that do not + // change a register's kind. + let src = any(&mut rng); + ( + Instr::JumpIfTrue { + cond: src, + target: 0, + }, + kind_of(&kinds, dst), + ) + } + _ => { + let src = any(&mut rng); + ( + Instr::JumpIfNullish { src, target: 0 }, + kind_of(&kinds, dst), + ) + } + }; + let is_jump = ins.target().is_some(); + instrs.push(ins.clone()); + if is_jump { + // The ops the jump may skip write the same kinds as before: use + // `Move` of a register onto itself and plain arithmetic on + // Numbers into Number registers. + let skip = (rng.below(3) as usize).saturating_add(1); + let n = instrs.len(); + for _ in 0..skip { + let nums = numbers(&kinds); + let d = rng.pick(&nums); + let (p, q) = (rng.pick(&nums), rng.pick(&nums)); + instrs.push(match rng.below(3) { + 0 => Instr::Add { dst: d, a: p, b: q }, + 1 => Instr::Mul { dst: d, a: p, b: q }, + _ => Instr::BitXor { dst: d, a: p, b: q }, + }); + } + let target = u32::try_from(instrs.len()).unwrap_or(0); + if let Some(j) = instrs.get_mut(n.saturating_sub(1)) { + j.set_target(target); + } + } else { + set_kind(&mut kinds, dst, kind); + } + } + instrs.push(Instr::Return { src: 0 }); + // The jump targets of a trailing jump point at the Return, which exists. + let n = instrs.len(); + let program = Program { + functions: vec![FunctionCode { + name: Vec::new(), + kind: FunctionKind::Script, + flavor: Flavor::Plain, + strict: false, + span: Span { start: 0, end: 0 }, + nparams: 0, + length: 0, + nregs: NREGS, + nics: 0, + instrs, + spans: vec![Span { start: 0, end: 0 }; n], + consts, + handlers: Vec::new(), + }], + main: 0, + }; + let regs = model_run(&program); + (program, regs) +} + +/// The model's run of the (single, straight-line) function. +fn model_run(p: &Program) -> [u64; NREGS as usize] { + let mut regs = [model::UNDEFINED; NREGS as usize]; + let mut pc = 0usize; + macro_rules! rd { + ($i:expr) => { + regs.get($i as usize).copied().unwrap_or(model::UNDEFINED) + }; + } + macro_rules! wr { + ($i:expr, $v:expr) => {{ + let v = $v; + if let Some(slot) = regs.get_mut($i as usize) { + *slot = v; + } + }}; + } + let Some(f) = p.functions.first() else { + return regs; + }; + while let Some(ins) = f.instrs.get(pc) { + let mut next = pc.saturating_add(1); + match *ins { + Instr::Move { dst, src } => wr!(dst, rd!(src)), + Instr::LoadConst { dst, k } => { + if let Some(Const::Number(bits)) = f.consts.get(k as usize) { + wr!(dst, model_const(*bits)); + } + } + Instr::LoadInt { dst, imm } => wr!(dst, model::enc_int32(imm)), + Instr::LoadUndefined { dst } => wr!(dst, model::UNDEFINED), + Instr::LoadNull { dst } => wr!(dst, model::NULL), + Instr::LoadBool { dst, flag } => wr!(dst, model::enc_bool(flag)), + Instr::JumpIfTrue { cond, target } => { + if model::to_boolean(rd!(cond)) { + next = target as usize; + } + } + Instr::JumpIfNullish { src, target } => { + if rd!(src) == model::UNDEFINED || rd!(src) == model::NULL { + next = target as usize; + } + } + Instr::Add { dst, a, b } => wr!(dst, model::arith_add(rd!(a), rd!(b))), + Instr::Sub { dst, a, b } => wr!(dst, model::arith_sub(rd!(a), rd!(b))), + Instr::Mul { dst, a, b } => wr!(dst, model::arith_mul(rd!(a), rd!(b))), + Instr::Div { dst, a, b } => wr!(dst, model::arith_div(rd!(a), rd!(b))), + Instr::Mod { dst, a, b } => wr!(dst, model::arith_mod(rd!(a), rd!(b))), + Instr::Neg { dst, src } => wr!(dst, model::arith_neg(rd!(src))), + Instr::Inc { dst, src } => wr!(dst, model::arith_add(rd!(src), model::enc_int32(1))), + Instr::Dec { dst, src } => wr!(dst, model::arith_sub(rd!(src), model::enc_int32(1))), + Instr::BitAnd { dst, a, b } => wr!(dst, model::bit_and(rd!(a), rd!(b))), + Instr::BitOr { dst, a, b } => wr!(dst, model::bit_or(rd!(a), rd!(b))), + Instr::BitXor { dst, a, b } => wr!(dst, model::bit_xor(rd!(a), rd!(b))), + Instr::Shl { dst, a, b } => wr!(dst, model::shl(rd!(a), rd!(b))), + Instr::Shr { dst, a, b } => wr!(dst, model::shr(rd!(a), rd!(b))), + Instr::UShr { dst, a, b } => wr!(dst, model::ushr(rd!(a), rd!(b))), + Instr::BitNot { dst, src } => wr!(dst, model::bit_not(rd!(src))), + Instr::Lt { dst, a, b } => wr!(dst, model::enc_bool(model::rel_lt(rd!(a), rd!(b)))), + Instr::Le { dst, a, b } => wr!(dst, model::enc_bool(model::rel_le(rd!(a), rd!(b)))), + Instr::Gt { dst, a, b } => wr!(dst, model::enc_bool(model::rel_gt(rd!(a), rd!(b)))), + Instr::Ge { dst, a, b } => wr!(dst, model::enc_bool(model::rel_ge(rd!(a), rd!(b)))), + Instr::StrictEq { dst, a, b } => { + wr!(dst, model::enc_bool(model::strict_eq(rd!(a), rd!(b)))); + } + Instr::StrictNe { dst, a, b } => { + wr!(dst, model::enc_bool(!model::strict_eq(rd!(a), rd!(b)))); + } + Instr::Not { dst, src } => wr!(dst, model::enc_bool(!model::to_boolean(rd!(src)))), + Instr::ToNumeric { dst, src } => wr!(dst, rd!(src)), + Instr::Return { .. } => break, + _ => break, + } + pc = next; + } + regs +} + +/// Runs a generated program in the interpreter and compares every register. +fn check(seed: u64, len: usize) -> Result<(), String> { + let (program, expected) = generate(seed, len); + check_program(&program).map_err(|e| format!("seed {seed}: ill-formed: {e}"))?; + let mut vm = Vm::new(Config::default()).map_err(|e| format!("{e:?}"))?; + let script = vm + .load(&program) + .map_err(|e| format!("seed {seed}: {e:?}"))?; + vm.run(&script).map_err(|e| format!("seed {seed}: {e:?}"))?; + for (r, want) in expected.iter().enumerate() { + // The script's frame is at slot 0: four header slots, then registers. + let got = vm + .region_slot(r.saturating_add(4)) + .ok_or_else(|| format!("seed {seed}: no slot for register {r}"))?; + if got.bits() != *want { + return Err(format!( + "seed {seed}, register {r}: interpreter {:#018x} ({:?}), model {:#018x}\nprogram: {:#?}", + got.bits(), + got.decode(), + want, + program.functions.first().map(|f| &f.instrs) + )); + } + } + Ok(()) +} + +#[test] +fn the_interpreter_matches_the_model_of_step() -> Result<(), String> { + let (programs, len) = if cfg!(miri) { (6, 30) } else { (3000, 60) }; + for seed in 0..programs as u64 { + check( + seed.wrapping_mul(0x9E37_79B9_7F4A_7C15).wrapping_add(1), + len, + )?; + } + Ok(()) +} + +#[test] +fn the_model_is_checked_by_known_results() { + // The model is itself a transcription: pin it to values worked out by hand + // from ECMA-262, so that a mistake common to the model and the + // interpreter's `numeric.rs` would have to also be a mistake of the + // standard's worked examples. + use model::*; + assert_eq!( + arith_add(enc_int32(i32::MAX), enc_int32(1)), + (2_147_483_648.0f64).to_bits() + ); + assert_eq!(arith_mul(enc_int32(0), enc_int32(-3)), (-0.0f64).to_bits()); + assert_eq!(arith_mod(enc_int32(-4), enc_int32(2)), (-0.0f64).to_bits()); + assert_eq!( + arith_neg(enc_int32(i32::MIN)), + (2_147_483_648.0f64).to_bits() + ); + assert_eq!(to_int32(4_294_967_301.0f64.to_bits()), 5); + assert_eq!(to_int32((-1.9f64).to_bits()), -1); + assert_eq!(to_int32(1e300f64.to_bits()), 0); + assert_eq!(to_uint32((-1.0f64).to_bits()), u32::MAX); + assert_eq!(shl(enc_int32(1), enc_int32(31)), enc_int32(i32::MIN)); + assert_eq!(shr(enc_int32(-9), enc_int32(1)), enc_int32(-5)); + assert_eq!( + ushr(enc_int32(-1), enc_int32(0)), + 4_294_967_295.0f64.to_bits() + ); + assert_eq!(bit_not(enc_int32(5)), enc_int32(-6)); + assert!(!rel_lt(CANON_NAN, enc_int32(1)) && !rel_ge(CANON_NAN, enc_int32(1))); + assert!(rel_le(enc_int32(0), (-0.0f64).to_bits())); + assert!(strict_eq(enc_int32(0), (-0.0f64).to_bits())); + assert!(!strict_eq(CANON_NAN, CANON_NAN)); + assert_eq!( + Value::from_bits(arith_div(enc_int32(1), enc_int32(0))).map(Value::bits), + Some(f64::INFINITY.to_bits()) + ); +} diff --git a/crates/clazel-js-vm/tests/features.rs b/crates/clazel-js-vm/tests/features.rs index 023af1334..1fd63734b 100644 --- a/crates/clazel-js-vm/tests/features.rs +++ b/crates/clazel-js-vm/tests/features.rs @@ -11,7 +11,7 @@ use clazel_js_syntax::spec::SourceGoal; use clazel_js_vm::bytecode::{ Const, ErrorKind, FunctionCode, FunctionKind, Handler, Instr, Program, check_program, }; -use clazel_js_vm::compile::{CompileError, compile_script}; +use clazel_js_vm::compile::compile_script; fn cps(src: &str) -> Vec { src.chars().map(u32::from).collect() @@ -697,36 +697,19 @@ fn optional_chaining() -> Result<(), String> { Ok(()) } -/// Constructs that stay out of scope are still reported, never a panic, also -/// inside the constructs of this slice. +/// Constructs the second slice left out, inside the constructs of this slice, +/// compile since the fourth (`tests/modules.rs` has their expectations). #[test] -fn out_of_scope_constructs_are_not_yet_compiled() -> Result<(), String> { - let cases: &[(&str, &str)] = &[ - ("class C { *m() {} }", "generator or async methods"), - ("class C { async m() {} }", "generator or async methods"), - ( - "class C { static async *m() {} }", - "generator or async methods", - ), - ( - "class C { x = function* () {}; }", - "generators and async functions", - ), - ("var { a = await 1 } = o;", "generators and async functions"), - ("function f({ a = eval('1') }) {}", "direct eval"), - ("for (var x of /a/) ;", "regular expression literals"), - ("`${/a/}`;", "regular expression literals"), - ]; - for &(src, feature) in cases { +fn formerly_out_of_scope_constructs_compile() -> Result<(), String> { + for src in [ + "function f({ a = eval(...b) }) {}", + "delete a?.b;", + "if (a) function f() {}", + ] { let c = cps(src); - let Ok(ast) = parse(&c, SourceGoal::Script) else { - // `await` outside an async function is not a script the parser accepts. - continue; - }; - match compile_script(&ast, &c) { - Err(CompileError::NotYetCompiled { feature: f, .. }) => assert_eq!(f, feature, "{src}"), - other => return Err(format!("{src}: expected not yet compiled, got {other:?}")), - } + let ast = parse(&c, SourceGoal::Script).map_err(|_| format!("does not parse: {src}"))?; + let p = compile_script(&ast, &c).map_err(|e| format!("{src}: {e}"))?; + check_program(&p).map_err(|e| format!("{src}: {e}"))?; } Ok(()) } diff --git a/crates/clazel-js-vm/tests/features3.rs b/crates/clazel-js-vm/tests/features3.rs new file mode 100644 index 000000000..2470e172c --- /dev/null +++ b/crates/clazel-js-vm/tests/features3.rs @@ -0,0 +1,694 @@ +//! Tests of the compiler's third slice: regular expression literals, +//! generators, async functions and `for await`, `with`, direct `eval`, +//! `delete` of a variable, NamedEvaluation and `var arguments`. Each expectation +//! was read against the op docs in `ops.rs` (which cite ECMA-262), not copied +//! from the output. +//! +//! These tests read no files, so they also run under Miri. + +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; +use clazel_js_vm::bytecode::{ + Const, EvalBinding, EvalScopeKind, Flavor, FunctionCode, FunctionKind, Instr, Program, + check_program, +}; +use clazel_js_vm::compile::compile_script; + +fn cps(src: &str) -> Vec { + src.chars().map(u32::from).collect() +} + +fn compile(src: &str) -> Result { + let c = cps(src); + let shown: String = src.chars().take(80).collect(); + let ast = parse(&c, SourceGoal::Script).map_err(|_| format!("does not parse: {shown}"))?; + let p = compile_script(&ast, &c).map_err(|e| format!("{shown}: {e}"))?; + check_program(&p).map_err(|e| format!("{shown}: ill-formed bytecode: {e}"))?; + Ok(p) +} + +fn func(p: &Program, i: usize) -> Result<&FunctionCode, String> { + p.functions.get(i).ok_or_else(|| format!("no function {i}")) +} + +fn count(f: &FunctionCode, pred: fn(&Instr) -> bool) -> usize { + f.instrs.iter().filter(|i| pred(i)).count() +} + +fn has(f: &FunctionCode, pred: fn(&Instr) -> bool) -> bool { + f.instrs.iter().any(pred) +} + +fn units(s: &str) -> Vec { + s.encode_utf16().collect() +} + +/// §13.2.7.3†: each evaluation of a regular expression literal is a new +/// object from the pattern and the flags (`NewRegExp`), whose record is a +/// constant. The body is the text between the first slash and the last, so a +/// `/` inside a class or an escaped one is part of it; the flags follow. +#[test] +fn regex_literals() -> Result<(), String> { + let p = compile("var r = /a\\/b[/]c/gi;")?; + let f = func(&p, 0)?; + assert_eq!(count(f, |i| matches!(i, Instr::NewRegExp { .. })), 1); + let site = f.consts.iter().find_map(|c| match c { + Const::RegExp(s) => Some(s.clone()), + _ => None, + }); + let site = site.ok_or("no RegExp constant")?; + assert_eq!(site.pattern, units("a\\/b[/]c")); + assert_eq!(site.flags, units("gi")); + // Two literals with the same text share the constant, but each is an op. + let p = compile("for (;;) { /x/; /x/y; /x/; }")?; + let f = func(&p, 0)?; + assert_eq!(count(f, |i| matches!(i, Instr::NewRegExp { .. })), 3); + assert_eq!( + f.consts + .iter() + .filter(|c| matches!(c, Const::RegExp(_))) + .count(), + 2 + ); + // The empty flags and a literal that contains `//` in a class. + let p = compile("/[//]/;")?; + let f = func(&p, 0)?; + assert!(f.consts.iter().any( + |c| matches!(c, Const::RegExp(s) if s.pattern == units("[//]") && s.flags.is_empty()) + )); + Ok(()) +} + +/// EvaluateGeneratorBody §15.5.2†: the parameters (FunctionDeclarationInstantiation) +/// first, then `GeneratorStart`, then the body; a `yield` is `Yield` followed by +/// the jumps on the resume mode (0 value, 1 throw, 2 return, which is a `return`). +#[test] +fn generator_function() -> Result<(), String> { + let p = compile("function* g(a) { var x = yield a; return x; }")?; + let g = func(&p, 1)?; + assert_eq!(g.flavor, Flavor::Generator); + assert_eq!(g.kind, FunctionKind::Normal); + let start = g + .instrs + .iter() + .position(|i| matches!(i, Instr::GeneratorStart)) + .ok_or("no GeneratorStart")?; + let y = g + .instrs + .iter() + .position(|i| matches!(i, Instr::Yield { .. })) + .ok_or("no Yield")?; + assert!(start < y); + // Nothing of the body runs before GeneratorStart but the declarations. + assert!( + !g.instrs + .get(..start) + .ok_or("short")? + .iter() + .any(|i| matches!(i, Instr::Yield { .. } | Instr::Return { .. })) + ); + let Some(Instr::Yield { src, value, mode }) = g.instrs.get(y).cloned() else { + return Err("no yield".into()); + }; + // The operand is the parameter, copied to the register the op reads. + assert!( + g.instrs + .get(..y) + .ok_or("short")? + .iter() + .any(|i| matches!(i, Instr::Move { dst, src: 0 } if *dst == src)) + ); + // mode 0 skips ahead; else mode 1 throws the value, else it is returned. + let Some(Instr::JumpIfFalse { cond, target }) = g.instrs.get(y + 1).cloned() else { + return Err("no jump on the mode".into()); + }; + assert_eq!(cond, mode); + assert!(matches!( + g.instrs.get(y + 4), + Some(Instr::JumpIfFalse { .. }) + )); + assert!(matches!( + g.instrs.get(y + 5), + Some(Instr::Throw { src }) if *src == value + )); + assert!(matches!( + g.instrs.get(y + 6), + Some(Instr::Return { src }) if *src == value + )); + assert_eq!(target as usize, y + 7); + // A plain function and a script have none of it. + let p = compile("function f() { return 1; }")?; + assert_eq!(func(&p, 1)?.flavor, Flavor::Plain); + assert!(!has(func(&p, 1)?, |i| matches!(i, Instr::GeneratorStart))); + Ok(()) +} + +/// A return resumption of a `yield` inside `try`/`finally` returns through the +/// `finally` block (§27.5.3.4† GeneratorResumeAbrupt with a return completion +/// continues the body as a `return` at the `yield`). +#[test] +fn return_resumption_runs_finally() -> Result<(), String> { + let p = compile("function* g() { try { yield 1; } finally { cleanup(); } }")?; + let g = func(&p, 1)?; + // The resumption's return and the normal exit both enter the one finally + // block, which is compiled once: one call of `cleanup`. + assert_eq!(count(g, |i| matches!(i, Instr::Call { .. })), 1); + assert_eq!(count(g, |i| matches!(i, Instr::Yield { .. })), 1); + Ok(()) +} + +/// §15.8.5† and Await §6.2.3.1†: `Await`, then a throw if the mode is 1. An +/// async function has no `GeneratorStart` (its body runs at once, AsyncFunctionStart +/// §27.7.5.1†). +#[test] +fn await_expression() -> Result<(), String> { + let p = compile("async function f(p) { return (await p) + 1; }")?; + let f = func(&p, 1)?; + assert_eq!(f.flavor, Flavor::Async); + assert!(!has(f, |i| matches!(i, Instr::GeneratorStart))); + let a = f + .instrs + .iter() + .position(|i| matches!(i, Instr::Await { .. })) + .ok_or("no Await")?; + let Some(Instr::Await { src, value, mode }) = f.instrs.get(a).cloned() else { + return Err("no await".into()); + }; + assert_eq!(src, 0); + assert!(matches!( + f.instrs.get(a + 1), + Some(Instr::JumpIfFalse { cond, target }) if *cond == mode && *target as usize == a + 3 + )); + assert!(matches!( + f.instrs.get(a + 2), + Some(Instr::Throw { src }) if *src == value + )); + // The async arrow function keeps the arrow's `this`. + let p = compile("var h = async (x) => await x;")?; + let h = func(&p, 1)?; + assert_eq!(h.flavor, Flavor::Async); + assert_eq!(h.kind, FunctionKind::Arrow); + Ok(()) +} + +/// Async generators: `yield` awaits its operand first (§15.5.5† step 3.b), +/// a return resumption awaits its value (AsyncGeneratorYield §27.6.3.8† step +/// 9), and `return x` awaits `x` (§14.10.1 step 3.b). +#[test] +fn async_generator() -> Result<(), String> { + let p = compile("async function* g(a) { yield a; return a; }")?; + let g = func(&p, 1)?; + assert_eq!(g.flavor, Flavor::AsyncGenerator); + assert_eq!(count(g, |i| matches!(i, Instr::GeneratorStart)), 1); + // Await before the yield, on the resumption's return, and before `return a`. + assert_eq!(count(g, |i| matches!(i, Instr::Await { .. })), 3); + let y = g + .instrs + .iter() + .position(|i| matches!(i, Instr::Yield { .. })) + .ok_or("no Yield")?; + assert!( + g.instrs + .get(..y) + .ok_or("short")? + .iter() + .any(|i| matches!(i, Instr::Await { .. })) + ); + // A generator's `return a` is not awaited. + let p = compile("function* g(a) { return a; }")?; + assert_eq!(count(func(&p, 1)?, |i| matches!(i, Instr::Await { .. })), 0); + Ok(()) +} + +/// Generator, async and async generator methods of an object literal and a +/// class, and in a class's static and private forms. +#[test] +fn generator_and_async_methods() -> Result<(), String> { + let p = compile( + "var o = { *g() { yield 1; }, async a() { await 1; }, async *ag() { yield 1; } }; + class C { *g() {} static async m() {} async *#p() {} get x() { return 1; } }", + )?; + let flavors: Vec = p.functions.iter().map(|f| f.flavor).collect(); + assert!(flavors.contains(&Flavor::Generator)); + assert!(flavors.contains(&Flavor::Async)); + assert!(flavors.contains(&Flavor::AsyncGenerator)); + for f in &p.functions { + if f.flavor != Flavor::Plain { + assert_eq!(f.kind, FunctionKind::Method, "{:?}", f.name); + } + } + Ok(()) +} + +/// `yield*` in a generator (§15.5.5† step 7): GetIterator, a loop that calls +/// `next`, `throw` or `return` of the inner iterator according to the received +/// mode (`IterCallMethod` 0, 1 and 2), and `YieldDelegate`, which gives the +/// inner result object as it is. Never `Yield`, which wraps a value. +#[test] +fn yield_star_in_a_generator() -> Result<(), String> { + let p = compile("function* g(it) { var r = yield* it; return r; }")?; + let g = func(&p, 1)?; + assert_eq!(count(g, |i| matches!(i, Instr::GetIterator { .. })), 1); + assert_eq!(count(g, |i| matches!(i, Instr::YieldDelegate { .. })), 1); + assert!(!has(g, |i| matches!(i, Instr::Yield { .. }))); + assert!(!has(g, |i| matches!(i, Instr::Await { .. }))); + for which in 0..3 { + assert!( + g.instrs + .iter() + .any(|i| matches!(i, Instr::IterCallMethod { which: w, .. } if *w == which)), + "IterCallMethod {which}" + ); + } + // No `throw` method: the iterator is closed, then a TypeError. + assert!(has(g, |i| matches!(i, Instr::IterClose { .. }))); + assert!(has(g, |i| matches!(i, Instr::ThrowError { .. }))); + // The loop ends in a back-edge, so a runaway delegate is stoppable. + assert!(has(g, |i| matches!(i, Instr::Loop { .. }))); + Ok(()) +} + +/// `yield*` in an async generator: GetIterator with the async hint, each +/// inner result awaited, the value yielded with `Yield` (AsyncGeneratorYield), +/// and a return resumption awaited again. +#[test] +fn yield_star_in_an_async_generator() -> Result<(), String> { + let p = compile("async function* g(it) { yield* it; }")?; + let g = func(&p, 1)?; + assert_eq!(count(g, |i| matches!(i, Instr::GetAsyncIterator { .. })), 1); + assert!(!has(g, |i| matches!(i, Instr::GetIterator { .. }))); + assert!(!has(g, |i| matches!(i, Instr::YieldDelegate { .. }))); + assert_eq!(count(g, |i| matches!(i, Instr::Yield { .. })), 1); + // `next`, `throw` and `return` results, the close, the `return` + // resumption and the missing-`return` case. + assert!(count(g, |i| matches!(i, Instr::Await { .. })) >= 6); + Ok(()) +} + +/// `for await` (§14.7.5.7† with async-iterate): GetIterator with the async +/// hint; each step calls `next` with no argument (`IterCallMethod` 4), awaits +/// the result, checks it (`IterComplete`) and reads `value`; leaving the loop by +/// `break` closes the iterator with `return` (`IterCallMethod` 3) and awaits +/// that, and an Object check of its result. +#[test] +fn for_await() -> Result<(), String> { + let p = compile("async function f(o) { for await (var x of o) { if (x) break; } }")?; + let f = func(&p, 1)?; + assert_eq!(count(f, |i| matches!(i, Instr::GetAsyncIterator { .. })), 1); + assert!(has(f, |i| matches!( + i, + Instr::IterCallMethod { which: 4, .. } + ))); + assert!(has(f, |i| matches!(i, Instr::IterComplete { .. }))); + assert!(has(f, |i| matches!(i, Instr::IterValue { .. }))); + assert!(has(f, |i| matches!( + i, + Instr::IterCallMethod { which: 3, .. } + ))); + assert!(has(f, |i| matches!(i, Instr::ThrowIfNotObject { .. }))); + // The `next` await, the `break` close's, and the close for a throw. + assert_eq!(count(f, |i| matches!(i, Instr::Await { .. })), 3); + // The body is covered by a catch-all that closes and rethrows. + assert!(!f.handlers.is_empty()); + // A plain `for`-`of` has none of these. + let p = compile("function f(o) { for (var x of o) { break; } }")?; + let f = func(&p, 1)?; + assert!(!has(f, |i| matches!(i, Instr::IterCallMethod { .. }))); + assert!(has(f, |i| matches!(i, Instr::IterNext { .. }))); + Ok(()) +} + +/// §14.11.2†: ToObject of the expression, a new object environment (an +/// environment object with the object in slot 0), the body inside it. A name +/// the body uses that nothing in it binds is looked up in the object first +/// (`WithHas`), then read (`WithGet`), written (`WithSet`) or called with the +/// object as `this`; a binding declared inside the body is not. +#[test] +fn with_statement() -> Result<(), String> { + let p = compile("function f(o) { var local = 1; with (o) { x = local; g(); } }")?; + let f = func(&p, 1)?; + assert_eq!(count(f, |i| matches!(i, Instr::ToObject { .. })), 1); + assert!(has(f, |i| matches!(i, Instr::NewEnv { size: 1, .. }))); + // `x`, `g` are looked up; `local` is a binding outside the `with`, so it is + // looked up too (the object may have a `local` property that shadows it). + assert_eq!(count(f, |i| matches!(i, Instr::WithHas { .. })), 3); + assert_eq!(count(f, |i| matches!(i, Instr::WithSet { .. })), 1); + assert_eq!(count(f, |i| matches!(i, Instr::WithGet { .. })), 2); + // The call's `this` is the object when the name is found in it. + let call = f + .instrs + .iter() + .find_map(|i| match i { + Instr::Call { this, .. } => Some(*this), + _ => None, + }) + .ok_or("no call")?; + assert!(f.instrs.iter().any(|i| matches!( + i, + Instr::Move { dst, .. } if *dst == call + ))); + // A name bound inside the body is not looked up in the object. + let p = compile("function f(o) { with (o) { let y = 1; y; } }")?; + let f = func(&p, 1)?; + assert_eq!(count(f, |i| matches!(i, Instr::WithHas { .. })), 0); + Ok(()) +} + +/// A closure made in a `with` body captures its environment, so the name it +/// uses is looked up in the object from inside the closure (one `GetEnvParent` +/// hop to the `with`'s environment), and `typeof` of such a name is `WithGet` or +/// the static `typeof`. +#[test] +fn with_and_closures() -> Result<(), String> { + let p = compile("function f(o) { with (o) { return function () { return typeof k + z; }; } }")?; + let inner = func(&p, 2)?; + assert_eq!(count(inner, |i| matches!(i, Instr::WithHas { .. })), 2); + assert!(has(inner, |i| matches!( + i, + Instr::GetEnvSlot { idx: 0, .. } + ))); + assert!(has(inner, |i| matches!( + i, + Instr::GetGlobalOrUndefined { .. } + ))); + // The with statement is also fine at the top of a script, in strict-free + // code, nested, with `delete` and update expressions. + compile("with ({}) { with ({}) { v++; delete v; v += 1; var w = 2; } }")?; + Ok(()) +} + +/// `delete name` (§13.5.1.2† step 5): a global is `DeleteGlobal`, a binding of +/// a function is not deletable (false), a name that a `with` object may have is +/// `WithDelete` first. +#[test] +fn delete_of_a_variable() -> Result<(), String> { + let p = compile("delete g;")?; + assert_eq!( + count(func(&p, 0)?, |i| matches!(i, Instr::DeleteGlobal { .. })), + 1 + ); + let p = compile("function f(a) { return delete a; }")?; + let f = func(&p, 1)?; + assert!(!has(f, |i| matches!(i, Instr::DeleteGlobal { .. }))); + assert!(has(f, |i| matches!(i, Instr::LoadBool { flag: false, .. }))); + let p = compile("function f(o) { with (o) { delete q; } }")?; + let f = func(&p, 1)?; + assert_eq!(count(f, |i| matches!(i, Instr::WithDelete { .. })), 1); + assert_eq!(count(f, |i| matches!(i, Instr::DeleteGlobal { .. })), 1); + Ok(()) +} + +/// Direct eval (§13.3.6.1† step 6): `DirectEval` with the callee `eval` +/// resolved as a name, the call's `this`, the arguments, the current +/// environment and the record of the scopes. Every binding of the function (and +/// `this`, `new.target`, the function and `arguments`) is in the environment +/// object, by name, in the record; a sloppy function's variable object is a slot +/// with a null-prototype object. +#[test] +fn direct_eval_sloppy() -> Result<(), String> { + let p = compile("function f(a) { var b = 1; return eval('a + b'); }")?; + let f = func(&p, 1)?; + assert_eq!( + count(f, |i| matches!(i, Instr::DirectEval { argc: 1, .. })), + 1 + ); + assert!(!has(f, |i| matches!(i, Instr::Call { .. }))); + let info = f + .consts + .iter() + .find_map(|c| match c { + Const::EvalScope(i) => Some(i.clone()), + _ => None, + }) + .ok_or("no EvalScope")?; + assert!(!info.strict); + assert!(info.in_function); + assert!(!info.in_method); + assert!(!info.in_derived_constructor); + let scope = info.scopes.first().ok_or("no scope")?; + assert_eq!(scope.kind, EvalScopeKind::Function); + let slot_of = |name: &str| { + scope + .names + .iter() + .find(|n| n.name == units(name)) + .map(|n| (n.slot, n.kind)) + }; + assert!(matches!(slot_of("a"), Some((_, EvalBinding::Var)))); + assert!(matches!(slot_of("b"), Some((_, EvalBinding::Var)))); + assert!(matches!( + slot_of("arguments"), + Some((_, EvalBinding::Arguments)) + )); + for kind in [ + EvalBinding::This, + EvalBinding::NewTarget, + EvalBinding::Callee, + ] { + assert!(scope.names.iter().any(|n| n.kind == kind), "{kind:?}"); + } + // The variable object: created at entry, with a null prototype. + assert!(scope.var_object.is_some()); + assert!(has(f, |i| matches!(i, Instr::SetLiteralProto { .. }))); + // The record's slots are the environment object's slots: no name shares one. + let mut slots: Vec = scope.names.iter().map(|n| n.slot).collect(); + slots.sort_unstable(); + slots.dedup(); + assert_eq!(slots.len(), scope.names.len()); + // A name that the function does not bind is looked up in the variable + // object first, since eval code may have declared it. + let p = compile("function f() { eval('var q = 1'); return q; }")?; + let f = func(&p, 1)?; + assert!(has(f, |i| matches!(i, Instr::WithHas { .. }))); + assert!(has(f, |i| matches!(i, Instr::GetGlobal { .. }))); + Ok(()) +} + +/// Strict code has no variable object (the eval code's `var`s are its own, +/// §19.2.1.1† step 24), but still the record. +#[test] +fn direct_eval_strict_and_global() -> Result<(), String> { + let p = compile("function f(a) { 'use strict'; return eval('a'); }")?; + let f = func(&p, 1)?; + assert!(!has(f, |i| matches!(i, Instr::SetLiteralProto { .. }))); + let info = f + .consts + .iter() + .find_map(|c| match c { + Const::EvalScope(i) => Some(i.clone()), + _ => None, + }) + .ok_or("no EvalScope")?; + assert!(info.strict); + assert_eq!(info.scopes.first().ok_or("scope")?.var_object, None); + // At the top level of a script: no function scope, the eval's `var`s are + // global. + let p = compile("eval('var v = 1'); v;")?; + let m = func(&p, 0)?; + assert_eq!(count(m, |i| matches!(i, Instr::DirectEval { .. })), 1); + assert!(!has(m, |i| matches!(i, Instr::WithHas { .. }))); + // In a method of a class: `super.x` and `new.target` are allowed in eval + // code; in a derived constructor `super()` too. + let p = compile( + "class A {} class B extends A { constructor() { super(); eval('1'); } m() { eval('2'); } }", + )?; + let infos: Vec<_> = p + .functions + .iter() + .flat_map(|f| f.consts.iter()) + .filter_map(|c| match c { + Const::EvalScope(i) => Some(i.clone()), + _ => None, + }) + .collect(); + assert_eq!(infos.len(), 2); + assert!(infos.iter().all(|i| i.in_method && i.in_function)); + assert_eq!(infos.iter().filter(|i| i.in_derived_constructor).count(), 1); + Ok(()) +} + +/// Only `eval(...)` written as the identifier is direct (§13.3.6.1† step 6: +/// the callee is the IdentifierReference `eval`): a parenthesized `(eval)(x)` +/// is not (the callee is a ParenthesizedExpression), nor are `(0, eval)(x)`, +/// `a.eval(x)` and `eval?.(x)`. +#[test] +fn only_the_plain_name_is_direct() -> Result<(), String> { + for src in [ + "(eval)('1');", + "(0, eval)('1');", + "a.eval('1');", + "eval?.('1');", + "eval`1`;", + ] { + let p = compile(src)?; + assert!( + !p.functions + .iter() + .any(|f| has(f, |i| matches!(i, Instr::DirectEval { .. }))), + "{src}" + ); + } + // An arrow function and a nested block inside the eval's function see + // the same record, one scope per environment object. + let p = compile("function f(a) { { let b = 1; return () => eval('a + b'); } }")?; + let arrow = p + .functions + .iter() + .find(|f| f.kind == FunctionKind::Arrow) + .ok_or("no arrow")?; + let info = arrow + .consts + .iter() + .find_map(|c| match c { + Const::EvalScope(i) => Some(i.clone()), + _ => None, + }) + .ok_or("no EvalScope")?; + // The arrow's own scope (the variable object), the block's, the function's. + assert!(info.scopes.len() >= 2); + assert!(info.scopes.iter().any(|s| s.kind == EvalScopeKind::Block + && s.names.iter().any(|n| n.name == units("b")))); + assert!( + info.scopes + .iter() + .any(|s| s.kind == EvalScopeKind::Function + && s.names.iter().any(|n| n.name == units("a"))) + ); + Ok(()) +} + +/// NamedEvaluation (§8.4.5†): an anonymous function or class gets the name of +/// the binding, assignment target, property or field it initializes; a named +/// one keeps its own; an anonymous one used otherwise stays anonymous. +#[test] +fn named_evaluation() -> Result<(), String> { + let p = compile( + "var a = function () {}; let b = () => 1; const c = class {}; d = function () {}; + var o = { e: function () {}, 'f g': () => 1, [h]: function () {} }; + function k(l = function () {}, { m = () => 2 } = {}, [n = class {}] = []) {} + var q = function named() {}; (function () {}); var r = (function () {}); + class C { s = function () {}; #t = () => 1; static u = class {}; } + var v; v ||= function () {}; v &&= () => 1;", + )?; + let names: Vec = p + .functions + .iter() + .map(|f| String::from_utf16_lossy(&f.name)) + .collect(); + let want = [ + "a", "b", "c", "d", "e", "f g", "l", "m", "n", "named", "r", "s", "#t", "u", "v", + ]; + for w in want { + assert!( + names.iter().any(|n| n == w), + "no function named {w}: {names:?}" + ); + } + // `[h]: function () {}` is named when the code runs, `(function () {});` + // is not named: exactly two anonymous functions remain (the script has + // an empty name too, and the two class initializers of C). + let anon = p + .functions + .iter() + .filter(|f| f.name.is_empty() && f.kind == FunctionKind::Normal) + .count(); + assert_eq!(anon, 2, "{names:?}"); + Ok(()) +} + +/// §10.2.11† step 27.b: a `var arguments` in a function with an `arguments` +/// object shares its binding, which holds the object, not `undefined`. A +/// parameter or a function declaration of that name keeps its own. +#[test] +fn var_arguments() -> Result<(), String> { + let p = compile("function f() { var arguments; return arguments; }")?; + let f = func(&p, 1)?; + assert_eq!( + count(f, |i| matches!( + i, + Instr::CreateMappedArguments { .. } | Instr::CreateUnmappedArguments { .. } + )), + 1 + ); + let p = compile("function f(arguments) { var arguments; return arguments; }")?; + let f = func(&p, 1)?; + assert_eq!( + count(f, |i| matches!( + i, + Instr::CreateMappedArguments { .. } | Instr::CreateUnmappedArguments { .. } + )), + 0 + ); + // In an arrow function the name is the arrow's own var (no `arguments`). + let p = compile("var g = () => { var arguments = 1; return arguments; };")?; + assert_eq!( + count(func(&p, 1)?, |i| matches!( + i, + Instr::CreateMappedArguments { .. } | Instr::CreateUnmappedArguments { .. } + )), + 0 + ); + Ok(()) +} + +/// §10.2.11† step 28: with parameter expressions the body's `var`s are in a +/// separate environment, so a closure in a default value does not see them +/// (here `v` is the global, and `a` in the closure is the parameter), while the +/// body does; a `var` that is also a parameter starts with its value. +#[test] +fn separate_environment_for_the_body() -> Result<(), String> { + let p = compile("function f(a, b = () => v + a) { var v = 1; var a; return b; }")?; + let arrow = p + .functions + .iter() + .find(|f| f.kind == FunctionKind::Arrow) + .ok_or("no arrow")?; + assert!(has(arrow, |i| matches!(i, Instr::GetGlobal { .. }))); + // `a` is the parameter, in the function's environment; `v` is not captured. + assert_eq!(count(arrow, |i| matches!(i, Instr::GetEnvSlot { .. })), 1); + // Without parameter expressions there is one scope: the closure sees `v`. + let p = compile("function f(a, b = 1) { var v = 1; return () => v; }")?; + let arrow = p + .functions + .iter() + .find(|f| f.kind == FunctionKind::Arrow) + .ok_or("no arrow")?; + assert!(!has(arrow, |i| matches!(i, Instr::GetGlobal { .. }))); + // A `var arguments` in such a function starts with the `arguments` object. + let p = compile("function f(a = 1) { var arguments; return arguments; }")?; + let f = func(&p, 1)?; + assert_eq!( + count(f, |i| matches!(i, Instr::CreateUnmappedArguments { .. })), + 1 + ); + Ok(()) +} + +/// A corpus of programs with the constructs of this slice, nested in each other +/// and in the earlier slices': every one compiles and passes `check_program`. +#[test] +fn corpus_is_well_formed() -> Result<(), String> { + let corpus = [ + "async function f() { try { for await (const x of y) { await x; if (x) continue; } } catch (e) { await e; } finally { await 1; } }", + "async function* g() { try { yield* h(); } finally { await cleanup(); } for await (var a of b) { yield a; } return await 1; }", + "function* g() { var x = yield; x = yield* [1, 2]; try { yield 1; } catch (e) { yield e; } finally { yield 2; } }", + "function* g() { for (var x of yield) { switch (x) { case yield: break; default: yield* g(); } } }", + "var o = { async *[k]() { yield await 1; }, *g() { yield* arguments; }, async m() { return () => await_(this); } };", + "class C extends D { async m() { super.m(); await 1; } static async *s() { yield super.s(); } *[Symbol.iterator]() { yield this; } }", + "async () => { for await ([a, b] of c) ; for await (a.b of c) ; for await (let {d} of e) ; };", + "function f(a = function* () { yield 1; }, b = async () => await a) { with (a) { return b; } }", + "with (a) { with (b) { c = d; e(); typeof f; delete g; h++; i += 1; for (j in k) ; try { l; } catch (m) { n; } } }", + "function f() { with (a) { return () => { with (b) { return x; } }; } }", + "function f(a, b) { eval('1'); { let c; eval('c'); } try { eval(a, b); } catch (e) { eval(e); } return function () { eval('2'); }; }", + "function f() { 'use strict'; eval('1'); (0, eval)('2'); }", + "var r = /a/g, s = [/b/y, /c[/]/u], t = { u: /d/s }; if (/e/.test(f)) g = `${/h/}`; for (var m of /i/g[Symbol.matchAll](j)) ;", + "function f() { var arguments; delete x; delete arguments; delete f; return eval('arguments'); }", + "label: { var q = async function* named() { yield* arguments; }; }", + "function f(a, { b = a } = {}, [c = () => b] = [], d = function () { return e; }) { var e = d, a; function g() { return a; } { function h() {} } return eval('a'); }", + "async function f(a = await_(), ...r) { var r; for await (var x of a) { var a; } }", + ]; + for src in corpus { + compile(src)?; + } + Ok(()) +} diff --git a/crates/clazel-js-vm/tests/interp.rs b/crates/clazel-js-vm/tests/interp.rs new file mode 100644 index 000000000..164f7bdb9 --- /dev/null +++ b/crates/clazel-js-vm/tests/interp.rs @@ -0,0 +1,600 @@ +//! Unit tests of the interpreter: small scripts compiled by `compile_script` +//! and run, their results read from global variables. Each expectation is +//! what ECMA-262 says of the script, written by hand. +//! +//! These tests read no files, so they also run under Miri. + +use clazel_js_heap::value::{Decoded, Value}; +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; +use clazel_js_vm::compile::compile_script; +use clazel_js_vm::interp::{Abrupt, Config, Vm}; + +fn cps(src: &str) -> Vec { + src.chars().map(u32::from).collect() +} + +/// Compiles and runs `src` in a new VM; the VM and the result. +fn run(src: &str) -> Result<(Vm, Result), String> { + let c = cps(src); + let ast = parse(&c, SourceGoal::Script).map_err(|_| format!("does not parse: {src}"))?; + let p = compile_script(&ast, &c).map_err(|e| format!("{src}: {e}"))?; + let mut vm = Vm::new(Config::default()).map_err(|e| format!("{e:?}"))?; + let script = vm.load(&p).map_err(|e| format!("{e:?}"))?; + let r = vm.run(&script); + Ok((vm, r)) +} + +/// Runs `src` and returns the global `r`. +fn global_r(src: &str) -> Result<(Vm, Value), String> { + let (mut vm, res) = run(src)?; + res.map_err(|e| format!("{src}: {e:?}"))?; + let g = vm.global(); + let r = vm.get_named(g, "r").map_err(|e| format!("{e:?}"))?; + Ok((vm, r)) +} + +/// Expects `r` to be the int32 `n`. +fn expect_int(src: &str, n: i32) -> Result<(), String> { + let (_, r) = global_r(src)?; + if r == Value::int32(n) { + Ok(()) + } else { + Err(format!("{src}: expected {n}, got {:?}", r.decode())) + } +} + +/// Expects `r` to be the double `d` (NaN equal to NaN, -0 distinct from +0). +fn expect_double(src: &str, d: f64) -> Result<(), String> { + let (_, r) = global_r(src)?; + if r == Value::double(d) { + Ok(()) + } else { + Err(format!("{src}: expected {d:?}, got {:?}", r.decode())) + } +} + +/// Expects `r` to be the string `s`. +fn expect_str(src: &str, s: &str) -> Result<(), String> { + let (vm, r) = global_r(src)?; + match vm.string_text(r) { + Some(t) if t == s => Ok(()), + other => Err(format!( + "{src}: expected {s:?}, got {other:?} ({:?})", + r.decode() + )), + } +} + +fn expect_bool(src: &str, b: bool) -> Result<(), String> { + let (_, r) = global_r(src)?; + if r == Value::boolean(b) { + Ok(()) + } else { + Err(format!("{src}: expected {b}, got {:?}", r.decode())) + } +} + +#[test] +fn arithmetic() -> Result<(), String> { + expect_int("var r = 1 + 2;", 3)?; + expect_int("var r = 7 * 6 - 2;", 40)?; + expect_int("var r = 17 % 5;", 2)?; + expect_double("var r = 1 / 4;", 0.25)?; + expect_double("var r = 2147483647 + 1;", 2_147_483_648.0)?; + expect_double("var r = -2147483648 - 1;", -2_147_483_649.0)?; + expect_double("var r = 0 * -1;", -0.0)?; + expect_double("var r = -0 + 0;", 0.0)?; + // Miri deliberately perturbs libm results such as powf, so the exact + // value is checked only natively. + if !cfg!(miri) { + expect_double("var r = 2 ** 10;", 1024.0)?; + } + expect_int("var r = 5 & 3;", 1)?; + expect_int("var r = 5 | 3;", 7)?; + expect_int("var r = 5 ^ 3;", 6)?; + expect_int("var r = 1 << 31 >> 31;", -1)?; + expect_double("var r = -1 >>> 0;", 4_294_967_295.0)?; + expect_int("var r = ~5;", -6)?; + expect_int("var r = -(-5);", 5)?; + expect_double("var r = 0.1 + 0.2;", 0.1 + 0.2)?; + expect_double("var r = 0 / 0;", f64::NAN) +} + +#[test] +fn comparisons_and_equality() -> Result<(), String> { + expect_bool("var r = 1 < 2;", true)?; + expect_bool("var r = 2 <= 1;", false)?; + expect_bool("var n = 0 / 0; var r = n === n;", false)?; + expect_bool("var r = NaN === NaN;", false)?; + expect_bool("var r = 1 === 1.0;", true)?; + expect_bool("var r = 1 !== 2;", true)?; + expect_bool("var r = 'a' === 'a';", true)?; + expect_bool("var r = 'a' < 'b';", true)?; + expect_bool("var r = '10' < '9';", true)?; + expect_bool("var r = 10 < '9';", false)?; + expect_bool("var r = null == undefined;", true)?; + expect_bool("var r = null == 0;", false)?; + expect_bool("var r = '1' == 1;", true)?; + expect_bool("var r = true == 1;", true)?; + expect_bool("var r = 'a' != 'b';", true)?; + expect_bool("var r = !0;", true)?; + expect_bool("var r = !'';", true)?; + expect_bool("var r = !'x';", false) +} + +#[test] +fn control_flow() -> Result<(), String> { + expect_int("var r = 0; for (var i = 0; i < 10; i++) { r += i; }", 45)?; + expect_int("var r = 1; while (r < 100) r = r * 2;", 128)?; + expect_int("var r; if (1 < 2) r = 1; else r = 2;", 1)?; + expect_int("var r = 0 || 5;", 5)?; + expect_int("var r = 3 && 4;", 4)?; + expect_int("var r = null ?? 7;", 7)?; + expect_int("var r = 1 ? 10 : 20;", 10)?; + expect_int("var r = 0; do { r++; if (r == 3) break; } while (true);", 3) +} + +#[test] +fn strings_and_conversions() -> Result<(), String> { + expect_str("var r = 'a' + 'b';", "ab")?; + expect_str("var r = 'x' + 1;", "x1")?; + expect_str("var r = 1 + 'x';", "1x")?; + expect_str("var r = 1.5 + '';", "1.5")?; + expect_str("var r = 1e21 + '';", "1e+21")?; + expect_str("var r = 0.000001 + '';", "0.000001")?; + expect_str("var r = 1e-7 + '';", "1e-7")?; + expect_str("var r = -0 + '';", "0")?; + expect_str("var r = (0 / 0) + '';", "NaN")?; + expect_str("var r = null + 'x';", "nullx")?; + expect_str("var r = undefined + 'x';", "undefinedx")?; + expect_str("var r = true + 'x';", "truex")?; + expect_int("var r = '5' * '4';", 20)?; + expect_int("var r = '5' - 2;", 3)?; + expect_int("var r = +'0x10';", 16)?; + expect_double("var r = +'abc';", f64::NAN)?; + expect_int("var r = +true;", 1)?; + expect_int("var r = 'abc'.length;", 3)?; + expect_str("var r = 'abc'[1];", "b")?; + expect_str("var r = `a${1 + 1}b`;", "a2b") +} + +#[test] +fn typeof_operator() -> Result<(), String> { + expect_str("var r = typeof 1;", "number")?; + expect_str("var r = typeof 'x';", "string")?; + expect_str("var r = typeof true;", "boolean")?; + expect_str("var r = typeof undefined;", "undefined")?; + expect_str("var r = typeof null;", "object")?; + expect_str("var r = typeof {};", "object")?; + expect_str("var r = typeof function () {};", "function")?; + expect_str("var r = typeof notDefinedAnywhere;", "undefined") +} + +#[test] +fn objects_and_properties() -> Result<(), String> { + expect_int("var o = { a: 1, b: 2 }; var r = o.a + o.b;", 3)?; + expect_int("var o = {}; o.x = 5; var r = o.x;", 5)?; + expect_int("var o = { a: 1 }; o.a = 9; var r = o.a;", 9)?; + expect_int( + "var o = { a: 1 }; var p = { __proto__: o }; var r = p.a;", + 1, + )?; + expect_int( + "var o = { a: 1 }; var p = { __proto__: o }; p.a = 2; var r = o.a * 10 + p.a;", + 12, + )?; + expect_bool("var o = { a: 1 }; var r = 'a' in o;", true)?; + expect_bool("var o = { a: 1 }; var r = 'b' in o;", false)?; + expect_int("var o = { 'k': 3 }; var r = o['k'];", 3)?; + expect_int("var k = 'z'; var o = { [k]: 4 }; var r = o.z;", 4)?; + expect_undefined("var o = {}; var r = o.missing;") +} + +fn expect_undefined(src: &str) -> Result<(), String> { + let (_, r) = global_r(src)?; + if r == Value::UNDEFINED { + Ok(()) + } else { + Err(format!("{src}: expected undefined, got {:?}", r.decode())) + } +} + +#[test] +fn an_inline_cache_hits_across_objects_of_one_shape() -> Result<(), String> { + // One site, many objects of one shape, then one of another shape. + expect_int( + "function get(o) { return o.v; } + var a = { v: 1 }, b = { v: 2 }, c = { w: 0, v: 3 }; + var r = get(a) * 100 + get(b) * 10 + get(c);", + 123, + )?; + expect_int( + "function set(o, x) { o.v = x; } + var a = { v: 1 }; set(a, 5); set(a, 6); + var r = a.v;", + 6, + ) +} + +#[test] +fn functions_and_calls() -> Result<(), String> { + expect_int("function f(a, b) { return a + b; } var r = f(1, 2);", 3)?; + expect_int("function f(a, b) { return a; } var r = f(7);", 7)?; + expect_undefined("function f(a, b) { return b; } var r = f(7);")?; + expect_int("function f(a) { return a; } var r = f(1, 2, 3);", 1)?; + expect_int( + "function fact(n) { return n < 2 ? 1 : n * fact(n - 1); } var r = fact(10);", + 3_628_800, + )?; + expect_int("var f = function () { return 5; }; var r = f();", 5)?; + expect_int("var r = (function () { return 6; })();", 6)?; + expect_int("var r = ((a) => a * 2)(4);", 8)?; + expect_int( + "function mk(n) { return function () { return n++; }; } + var c = mk(10); c(); c(); + var r = c();", + 12, + )?; + expect_int( + "function f() { return arguments_count_; } var arguments_count_ = 4; var r = f();", + 4, + )?; + expect_int("function f() { return 1; } var r = f.length + 10;", 10)?; + expect_str("function foo() {} var r = foo.name;", "foo")?; + expect_int("function f(a, b, c) {} var r = f.length;", 3)?; + expect_int( + "var o = { m() { return this.v; }, v: 8 }; var r = o.m();", + 8, + ) +} + +#[test] +fn this_in_sloppy_and_strict_functions() -> Result<(), String> { + expect_bool("function f() { return this; } var r = f() === this;", true)?; + expect_undefined("function f() { 'use strict'; return this; } var r = f();") +} + +#[test] +fn exceptions() -> Result<(), String> { + expect_int("var r; try { throw 5; } catch (e) { r = e; }", 5)?; + expect_int("var r = 0; try { r = 1; } finally { r = r + 10; }", 11)?; + expect_int( + "var r = 0; try { try { throw 1; } finally { r += 10; } } catch (e) { r += e; }", + 11, + )?; + expect_int( + "function f() { throw 4; } var r; try { f(); } catch (e) { r = e + 1; }", + 5, + )?; + expect_int( + "function g() { throw 2; } function f() { g(); return 0; } + var r; try { f(); } catch (e) { r = e; }", + 2, + )?; + expect_int( + "function f() { try { return 1; } finally { r = 9; } } var r = 0; var v = f(); r = r * 10 + v;", + 91, + )?; + expect_int("var r; try { null.x; } catch (e) { r = 1; }", 1)?; + expect_str( + "var r; try { undefinedName; } catch (e) { r = e.name; }", + "ReferenceError", + )?; + expect_str( + "var r; try { null.x; } catch (e) { r = e.name; }", + "TypeError", + )?; + expect_str( + "var r; try { var f = 1; f(); } catch (e) { r = e.name; }", + "TypeError", + )?; + expect_str( + "var r; try { let x = x; } catch (e) { r = e.name; }", + "ReferenceError", + )?; + expect_str( + "var r; try { const c = 1; c = 2; } catch (e) { r = e.name; }", + "TypeError", + ) +} + +#[test] +fn an_uncaught_throw_is_the_result() -> Result<(), String> { + let (vm, r) = run("throw 42;")?; + match r { + Err(Abrupt::Throw(v)) => { + assert_eq!(v, Value::int32(42)); + Ok(()) + } + other => { + let _ = vm.heap(); + Err(format!("{other:?}")) + } + } +} + +#[test] +fn global_declarations() -> Result<(), String> { + expect_int("var r = 1; var r;", 1)?; + expect_int("let a = 2; const b = 3; var r = a * b;", 6)?; + expect_int("function f() { return 1; } var f; var r = f();", 1)?; + expect_str( + "var r; try { g = 1; r = 'no'; } catch (e) { r = e.name; } var g;", + "no", + )?; + expect_str( + "'use strict'; var r; try { nope = 1; } catch (e) { r = e.name; }", + "ReferenceError", + )?; + expect_int("undeclared = 5; var r = undeclared;", 5)?; + expect_bool("var r = this.hasOwn_ === undefined; var hasOwn_;", true) +} + +#[test] +fn a_redeclaration_is_a_syntax_error_before_anything_runs() -> Result<(), String> { + // Two scripts in one VM: the second redeclares the first's `let`. + let mut vm = Vm::new(Config::default()).map_err(|e| format!("{e:?}"))?; + let load = |vm: &mut Vm, src: &str| -> Result<_, String> { + let c = cps(src); + let ast = parse(&c, SourceGoal::Script).map_err(|_| "parse".to_string())?; + let p = compile_script(&ast, &c).map_err(|e| e.to_string())?; + vm.load(&p).map_err(|e| format!("{e:?}")) + }; + let first = load(&mut vm, "let a = 1;")?; + vm.run(&first).map_err(|e| format!("{e:?}"))?; + let second = load(&mut vm, "var marker = 1; var a;")?; + match vm.run(&second) { + Err(Abrupt::Throw(e)) => { + let n = vm.get_named(e, "name").map_err(|e| format!("{e:?}"))?; + assert_eq!(vm.string_text(n).as_deref(), Some("SyntaxError")); + let g = vm.global(); + let m = vm.get_named(g, "marker").map_err(|e| format!("{e:?}"))?; + assert_eq!( + m, + Value::UNDEFINED, + "no binding is made after a failed check" + ); + Ok(()) + } + other => Err(format!("{other:?}")), + } +} + +#[test] +fn what_is_not_implemented_is_an_error_not_a_panic() -> Result<(), String> { + for src in [ + "var a = [1, 2];", + "var o = new Object();", + "var r = delete this.x;", + "var x = 10n;", + "function* g() {}", + ] { + let c = cps(src); + let Ok(ast) = parse(&c, SourceGoal::Script) else { + return Err(format!("does not parse: {src}")); + }; + let Ok(p) = compile_script(&ast, &c) else { + continue; // The compiler does not accept it yet either. + }; + let mut vm = Vm::new(Config::default()).map_err(|e| format!("{e:?}"))?; + let s = vm.load(&p).map_err(|e| format!("{e:?}"))?; + match vm.run(&s) { + Err(Abrupt::NotYet(_) | Abrupt::Throw(_)) | Ok(_) => {} + Err(e) => return Err(format!("{src}: {e:?}")), + } + } + // Spread needs iterators (GetIterator), which are not implemented. + let (_, r) = run("var a = [...[1, 2]];")?; + assert!(matches!(r, Err(Abrupt::NotYet(_))), "{r:?}"); + Ok(()) +} + +#[test] +fn a_loop_that_never_ends_is_ended_by_the_terminator() -> Result<(), String> { + let src = "while (true) {}"; + let c = cps(src); + let ast = parse(&c, SourceGoal::Script).map_err(|_| "parse".to_string())?; + let p = compile_script(&ast, &c).map_err(|e| e.to_string())?; + let mut vm = Vm::new(Config::default()).map_err(|e| format!("{e:?}"))?; + let s = vm.load(&p).map_err(|e| format!("{e:?}"))?; + vm.terminator().terminate(); + assert_eq!(vm.run(&s), Err(Abrupt::Terminated)); + vm.terminator().reset(); + vm.set_budget(1000); + assert_eq!(vm.run(&s), Err(Abrupt::Terminated)); + Ok(()) +} + +#[test] +fn the_terminator_is_checked_at_calls_and_not_catchable() -> Result<(), String> { + let src = "function f() { return 1; } try { f(); } catch (e) { var caught = 1; }"; + let c = cps(src); + let ast = parse(&c, SourceGoal::Script).map_err(|_| "parse".to_string())?; + let p = compile_script(&ast, &c).map_err(|e| e.to_string())?; + let mut vm = Vm::new(Config::default()).map_err(|e| format!("{e:?}"))?; + let s = vm.load(&p).map_err(|e| format!("{e:?}"))?; + vm.terminator().terminate(); + assert_eq!(vm.run(&s), Err(Abrupt::Terminated)); + let g = vm.global(); + let caught = vm.get_named(g, "caught").map_err(|e| format!("{e:?}"))?; + assert_eq!(caught, Value::UNDEFINED); + Ok(()) +} + +#[test] +fn the_counters_count_entries_and_back_edges() -> Result<(), String> { + let src = "function f() { return 1; } for (var i = 0; i < 5; i++) { f(); }"; + let c = cps(src); + let ast = parse(&c, SourceGoal::Script).map_err(|_| "parse".to_string())?; + let p = compile_script(&ast, &c).map_err(|e| e.to_string())?; + let mut vm = Vm::new(Config::default()).map_err(|e| format!("{e:?}"))?; + let s = vm.load(&p).map_err(|e| format!("{e:?}"))?; + vm.run(&s).map_err(|e| format!("{e:?}"))?; + assert_eq!(vm.entries(s.main), Some(1)); + assert_eq!(vm.back_edges(s.main), Some(5)); + let f = s.main.checked_add(1).ok_or("overflow")?; + assert_eq!(vm.entries(f), Some(5)); + assert_eq!(vm.back_edges(f), Some(0)); + Ok(()) +} + +#[test] +fn deep_recursion_is_a_range_error_not_a_crash() -> Result<(), String> { + // A small region under Miri, so the overflow is reached in few steps. + let src = "function f() { return f(); } var r; try { f(); } catch (e) { r = e.name; }"; + let c = cps(src); + let ast = parse(&c, SourceGoal::Script).map_err(|_| "parse".to_string())?; + let p = compile_script(&ast, &c).map_err(|e| e.to_string())?; + let config = Config { + region_slots: if cfg!(miri) { 1 << 9 } else { 1 << 18 }, + ..Config::default() + }; + let mut vm = Vm::new(config).map_err(|e| format!("{e:?}"))?; + let s = vm.load(&p).map_err(|e| format!("{e:?}"))?; + vm.run(&s).map_err(|e| format!("{e:?}"))?; + let g = vm.global(); + let r = vm.get_named(g, "r").map_err(|e| format!("{e:?}"))?; + assert_eq!(vm.string_text(r).as_deref(), Some("RangeError")); + Ok(()) +} + +#[test] +fn a_native_function_is_called_and_may_call_back() -> Result<(), String> { + fn twice(vm: &mut Vm, _this: Value, args: &[Value]) -> Result { + let f = args.first().copied().unwrap_or(Value::UNDEFINED); + let x = args.get(1).copied().unwrap_or(Value::UNDEFINED); + let once = vm.call(f, Value::UNDEFINED, &[x])?; + vm.call(f, Value::UNDEFINED, &[once]) + } + let src = "var r = twice(function (n) { return n * 3; }, 2);"; + let c = cps(src); + let ast = parse(&c, SourceGoal::Script).map_err(|_| "parse".to_string())?; + let p = compile_script(&ast, &c).map_err(|e| e.to_string())?; + let mut vm = Vm::new(Config::default()).map_err(|e| format!("{e:?}"))?; + let native = vm + .new_native("twice", 2, twice) + .map_err(|e| format!("{e:?}"))?; + vm.define_global("twice", native) + .map_err(|e| format!("{e:?}"))?; + let s = vm.load(&p).map_err(|e| format!("{e:?}"))?; + vm.run(&s).map_err(|e| format!("{e:?}"))?; + let g = vm.global(); + let r = vm.get_named(g, "r").map_err(|e| format!("{e:?}"))?; + assert_eq!(r.decode(), Decoded::Int32(18)); + Ok(()) +} + +#[test] +fn an_exception_thrown_in_a_native_called_from_js_is_caught_by_js() -> Result<(), String> { + fn boom(_vm: &mut Vm, _this: Value, _args: &[Value]) -> Result { + Err(Abrupt::Throw(Value::int32(77))) + } + let src = "var r; try { boom(); } catch (e) { r = e; }"; + let c = cps(src); + let ast = parse(&c, SourceGoal::Script).map_err(|_| "parse".to_string())?; + let p = compile_script(&ast, &c).map_err(|e| e.to_string())?; + let mut vm = Vm::new(Config::default()).map_err(|e| format!("{e:?}"))?; + let native = vm + .new_native("boom", 0, boom) + .map_err(|e| format!("{e:?}"))?; + vm.define_global("boom", native) + .map_err(|e| format!("{e:?}"))?; + let s = vm.load(&p).map_err(|e| format!("{e:?}"))?; + vm.run(&s).map_err(|e| format!("{e:?}"))?; + let g = vm.global(); + let r = vm.get_named(g, "r").map_err(|e| format!("{e:?}"))?; + assert_eq!(r, Value::int32(77)); + Ok(()) +} + +#[test] +fn constructors_and_prototypes() -> Result<(), String> { + expect_int( + "function F() { this.a = 1; } var o = new F(); var r = o.a;", + 1, + )?; + expect_int( + "function F(x) { this.x = x; } F.prototype.get = function () { return this.x; }; + var r = new F(7).get();", + 7, + )?; + expect_int("function F() { return { z: 5 }; } var r = new F().z;", 5)?; + expect_int( + "function F() { this.q = 2; return 3; } var r = new F().q;", + 2, + )?; + expect_bool( + "function F() {} var o = new F(); var r = o.constructor === F;", + true, + )?; + expect_bool( + "function F() {} var r = F.prototype.constructor === F;", + true, + )?; + expect_str( + "var r; try { new (() => 1)(); } catch (e) { r = e.name; }", + "TypeError", + )?; + expect_str( + "var r; try { new 1; } catch (e) { r = e.name; }", + "TypeError", + ) +} + +fn make_native_object(vm: &mut Vm, _this: Value, _args: &[Value]) -> Result { + // A constructor sees `new_target` set; a call sees `undefined`. + let o = vm.new_object()?; + let called_as_new = vm.new_target() != Value::UNDEFINED; + vm.define_property(o, "viaNew", Value::boolean(called_as_new))?; + let me = vm.active_function(); + vm.define_property(o, "me", me)?; + Ok(o) +} + +#[test] +fn native_constructors_and_bound_functions() -> Result<(), String> { + let c = cps("var a = new Ctor(); var b = Ctor(); + var e = ''; try { new Plain(); } catch (x) { e = x.name; } + var made = new BoundCtor();"); + let ast = parse(&c, SourceGoal::Script).map_err(|_| "parse".to_string())?; + let p = compile_script(&ast, &c).map_err(|e| e.to_string())?; + let mut vm = Vm::new(Config::default()).map_err(|e| format!("{e:?}"))?; + let ctor = vm + .new_native_constructor("Ctor", 0, make_native_object) + .map_err(|e| format!("{e:?}"))?; + let plain = vm + .new_native("Plain", 0, make_native_object) + .map_err(|e| format!("{e:?}"))?; + vm.define_global("Ctor", ctor) + .map_err(|e| format!("{e:?}"))?; + vm.define_global("Plain", plain) + .map_err(|e| format!("{e:?}"))?; + let bound_ctor = vm + .new_bound_function(ctor, Value::UNDEFINED, Vec::new()) + .map_err(|e| format!("{e:?}"))?; + vm.define_global("BoundCtor", bound_ctor) + .map_err(|e| format!("{e:?}"))?; + let script = vm.load(&p).map_err(|e| format!("{e:?}"))?; + vm.run(&script).map_err(|e| format!("{e:?}"))?; + let g = vm.global(); + let get = |vm: &mut Vm, o: Value, n: &str| vm.get_named(o, n).map_err(|e| format!("{e:?}")); + let (a, b) = (get(&mut vm, g, "a")?, get(&mut vm, g, "b")?); + assert_eq!(get(&mut vm, a, "viaNew")?, Value::TRUE); + assert_eq!(get(&mut vm, a, "me")?, ctor); + assert_eq!(get(&mut vm, b, "viaNew")?, Value::FALSE); + // `new` of a native without [[Construct]] is a TypeError. + let e = get(&mut vm, g, "e")?; + assert_eq!(vm.string_text(e).as_deref(), Some("TypeError")); + // A bound native constructor constructs its target. + let made = get(&mut vm, g, "made")?; + assert_eq!(get(&mut vm, made, "viaNew")?, Value::TRUE); + // The embedding entry points agree with the ops. + let direct = vm + .construct_value(ctor, &[]) + .map_err(|e| format!("{e:?}"))?; + assert_eq!(get(&mut vm, direct, "viaNew")?, Value::TRUE); + assert!(vm.is_constructor(ctor) && !vm.is_constructor(plain)); + assert!(vm.is_constructor(bound_ctor)); + Ok(()) +} diff --git a/crates/clazel-js-vm/tests/modules.rs b/crates/clazel-js-vm/tests/modules.rs new file mode 100644 index 000000000..ec21d7ef4 --- /dev/null +++ b/crates/clazel-js-vm/tests/modules.rs @@ -0,0 +1,876 @@ +//! Tests of the compiler's fourth slice: modules, `import()`, `import.meta`, +//! `delete` of an optional chain, direct `eval` with a spread argument, and a +//! function declaration as the body of an `if` or a label. Each expectation was +//! read against the op docs in `ops.rs` and the record's docs in `module.rs` +//! (which cite ECMA-262), not copied from the output. +//! +//! These tests read no files, so they also run under Miri. + +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; +use clazel_js_vm::bytecode::{ + Const, ErrorKind, EvalBinding, EvalScopeKind, Flavor, FunctionCode, FunctionKind, Instr, + Program, check_program, +}; +use clazel_js_vm::compile::{compile_module, compile_script}; +use clazel_js_vm::module::{ + ExportImportName, ImportName, ModuleBindingInit, ModuleProgram, check_module, +}; + +fn cps(src: &str) -> Vec { + src.chars().map(u32::from).collect() +} + +/// `v[i]`, as an error instead of a panic. +fn at(v: &[T], i: usize) -> Result<&T, String> { + v.get(i).ok_or_else(|| format!("no element {i}")) +} + +fn units(s: &str) -> Vec { + s.encode_utf16().collect() +} + +fn module(src: &str) -> Result { + let c = cps(src); + let shown: String = src.chars().take(80).collect(); + let ast = parse(&c, SourceGoal::Module).map_err(|_| format!("does not parse: {shown}"))?; + let m = compile_module(&ast, &c).map_err(|e| format!("{shown}: {e}"))?; + check_module(&m).map_err(|e| format!("{shown}: ill-formed record: {e}"))?; + Ok(m) +} + +fn script(src: &str) -> Result { + let c = cps(src); + let shown: String = src.chars().take(80).collect(); + let ast = parse(&c, SourceGoal::Script).map_err(|_| format!("does not parse: {shown}"))?; + let p = compile_script(&ast, &c).map_err(|e| format!("{shown}: {e}"))?; + check_program(&p).map_err(|e| format!("{shown}: ill-formed bytecode: {e}"))?; + Ok(p) +} + +fn body(m: &ModuleProgram) -> Result<&FunctionCode, String> { + m.program + .functions + .first() + .ok_or_else(|| "no body".to_string()) +} + +fn func(p: &Program, i: usize) -> Result<&FunctionCode, String> { + p.functions.get(i).ok_or_else(|| format!("no function {i}")) +} + +fn count(f: &FunctionCode, pred: fn(&Instr) -> bool) -> usize { + f.instrs.iter().filter(|i| pred(i)).count() +} + +fn binding<'a>( + m: &'a ModuleProgram, + name: &str, +) -> Result<&'a clazel_js_vm::module::ModuleBinding, String> { + let n = units(name); + m.bindings + .iter() + .find(|b| b.name == n) + .ok_or_else(|| format!("no binding {name}")) +} + +/// §16.2.1.3† ModuleRequests: the requests of the import and export +/// declarations that have a `from`, in order of first appearance; the same +/// specifier with the same set of attributes is one request. +#[test] +fn requested_modules() -> Result<(), String> { + let m = module( + "import a from 'x'; import 'y'; export * from 'x'; export {b} from 'z';\n\ + import c from 'x' with { type: 'json' };\n\ + import d from 'x' with { type: 'json', other: 'v' };\n\ + import e from 'x' with { other: 'v', type: 'json' };", + )?; + let specs: Vec = m + .requested_modules + .iter() + .map(|r| r.specifier_lossy()) + .collect(); + assert_eq!(specs, ["x", "y", "z", "x", "x"]); + assert!(at(&m.requested_modules, 0)?.attributes.is_empty()); + let r3 = at(&m.requested_modules, 3)?; + assert_eq!(r3.attributes.len(), 1); + assert_eq!(at(&r3.attributes, 0)?.key, units("type")); + assert_eq!(at(&r3.attributes, 0)?.value, units("json")); + // The last two have the same attributes in another order: one request, + // which keeps the first one's order. + let r4 = at(&m.requested_modules, 4)?; + assert_eq!(at(&r4.attributes, 0)?.key, units("type")); + assert_eq!(r4.attributes.len(), 2); + // The entries point at them. + let reqs: Vec = m.import_entries.iter().map(|e| e.module_request).collect(); + assert_eq!(reqs, [0, 3, 4, 4]); + Ok(()) +} + +/// §16.2.2.2† ImportEntries: a default import is the name `default`, a +/// namespace import is `namespace-object`, a named import with no `as` binds the +/// imported name, and a string literal can be the imported name. +#[test] +fn import_entries() -> Result<(), String> { + let m = module( + "import d, { a, b as c, 'x y' as z, default as e } from 'm';\n\ + import * as ns from 'n';\n\ + import d2, * as ns2 from 'o';", + )?; + let got: Vec<(u32, ImportName, String)> = m + .import_entries + .iter() + .map(|e| { + ( + e.module_request, + e.import_name.clone(), + String::from_utf16_lossy(&e.local_name), + ) + }) + .collect(); + let name = |s: &str| ImportName::Name(units(s)); + assert_eq!( + got, + vec![ + (0, name("default"), "d".to_string()), + (0, name("a"), "a".to_string()), + (0, name("b"), "c".to_string()), + (0, name("x y"), "z".to_string()), + (0, name("default"), "e".to_string()), + (1, ImportName::Namespace, "ns".to_string()), + (2, name("default"), "d2".to_string()), + (2, ImportName::Namespace, "ns2".to_string()), + ] + ); + // Each local name has a binding, an indirect one except a namespace's. + for (i, e) in m.import_entries.iter().enumerate() { + let b = m + .bindings + .iter() + .find(|b| b.name == e.local_name) + .ok_or("an import has no binding")?; + let i = u32::try_from(i).map_err(|_| "index")?; + let want = if e.import_name == ImportName::Namespace { + ModuleBindingInit::Namespace(i) + } else { + ModuleBindingInit::Import(i) + }; + assert_eq!(b.init, want); + } + Ok(()) +} + +/// §16.2.3.2† ExportEntries and ParseModule's partition (§16.2.1.6.1† steps 9 +/// and 10): a declaration or `export { a as b }` of a local binding is a local +/// export; a re-export of an import, `export { x } from`, and `export * as n +/// from` are indirect; `export * from` is a star export. +#[test] +fn export_entries() -> Result<(), String> { + let m = module( + "export var v = 1, [w] = [2];\n\ + export let l; export const c = 1;\n\ + export function f() {} export class K {}\n\ + export async function af() {} export function* g() {}\n\ + var p = 1; export { p as q, p };\n\ + import { i } from 'a'; export { i }; export { i as j };\n\ + import * as ns from 'b'; export { ns };\n\ + export { x as y, z } from 'c';\n\ + export * as all from 'd';\n\ + export * from 'e';\n\ + export { p as 'str name' };", + )?; + let names = |v: &[clazel_js_vm::module::ExportEntry]| -> Vec { + v.iter() + .map(|e| { + e.export_name + .as_ref() + .map(|n| String::from_utf16_lossy(n)) + .unwrap_or_else(|| "".to_string()) + }) + .collect() + }; + assert_eq!( + names(&m.local_export_entries), + [ + "v", "w", "l", "c", "f", "K", "af", "g", "q", "p", "ns", "str name" + ] + ); + for e in &m.local_export_entries { + assert!(e.module_request.is_none() && e.import_name.is_none()); + assert!(e.local_name.is_some()); + } + // `export { p as q }` has export name q and local name p. + let q = at(&m.local_export_entries, 8)?; + assert_eq!(q.local_name, Some(units("p"))); + assert_eq!( + names(&m.indirect_export_entries), + ["i", "j", "y", "z", "all"] + ); + // `export { i }` re-exports the import `i` of module 0. + let i = at(&m.indirect_export_entries, 0)?; + assert_eq!(i.module_request, Some(0)); + assert_eq!(i.import_name, Some(ExportImportName::Name(units("i")))); + assert!(i.local_name.is_none()); + // `export { x as y } from 'c'`: request 2, import name x. + let y = at(&m.indirect_export_entries, 2)?; + assert_eq!(y.module_request, Some(2)); + assert_eq!(y.import_name, Some(ExportImportName::Name(units("x")))); + let all = at(&m.indirect_export_entries, 4)?; + assert_eq!(all.import_name, Some(ExportImportName::All)); + assert_eq!(m.star_export_entries.len(), 1); + let star = at(&m.star_export_entries, 0)?; + assert!(star.export_name.is_none()); + assert_eq!(star.import_name, Some(ExportImportName::AllButDefault)); + assert_eq!(star.module_request, Some(4)); + Ok(()) +} + +/// §16.2.3.7† and §16.2.1.6.4†: what link time puts in each slot: `var` is +/// undefined, `let`, `const` and `class` are in their dead zone, a function is a +/// closure of its template, and every slot is below the environment's size. +#[test] +fn bindings_and_their_initial_state() -> Result<(), String> { + let m = module("var v; let l; const c = 1; class K {} function f() {} function* g() {}")?; + assert_eq!(binding(&m, "v")?.init, ModuleBindingInit::Undefined); + for n in ["l", "c", "K"] { + assert_eq!( + binding(&m, n)?.init, + ModuleBindingInit::Uninitialized, + "{n}" + ); + } + for n in ["f", "g"] { + match binding(&m, n)?.init { + ModuleBindingInit::Function(i) => { + let t = m.program.functions.get(i as usize).ok_or("no template")?; + assert_eq!(t.name, units(n)); + assert_eq!(t.kind, FunctionKind::Normal); + } + ref other => return Err(format!("{n}: {other:?}")), + } + } + assert_eq!( + m.program + .functions + .get(match binding(&m, "g")?.init { + ModuleBindingInit::Function(i) => i as usize, + _ => return Err("g".to_string()), + }) + .map(|f| f.flavor), + Some(Flavor::Generator) + ); + assert_eq!(m.bindings.len(), 6); + assert_eq!(m.env_size, 6); + // No hoisted function is created by the body (the runtime does it): the one + // closure is the constructor of the class K. + let b = body(&m)?; + assert_eq!(count(b, |i| matches!(i, Instr::NewClosure { .. })), 1); + assert_eq!(count(b, |i| matches!(i, Instr::NewEnv { .. })), 0); + assert_eq!(count(b, |i| matches!(i, Instr::DeclareGlobals { .. })), 0); + Ok(()) +} + +/// The body of a module: kind Module, strict code (§16.2.1†: module code is +/// strict), run in the module environment (its callee environment, read first), +/// with no global declarations. +#[test] +fn the_body_of_a_module() -> Result<(), String> { + let m = module("export let a = 1; a = a + 1;")?; + let b = body(&m)?; + assert_eq!(b.kind, FunctionKind::Module); + assert_eq!(b.flavor, Flavor::Plain); + assert!(b.strict); + assert_eq!(m.program.main, 0); + assert!(matches!(b.instrs.first(), Some(Instr::GetCalleeEnv { .. }))); + // The binding is in a slot of the module environment, so other modules read + // it: the writes are slot writes. + assert!(count(b, |i| matches!(i, Instr::SetEnvSlot { .. })) >= 2); + assert!(count(b, |i| matches!(i, Instr::GetEnvSlot { .. })) >= 1); + // An empty module compiles. + let e = module("")?; + assert_eq!(e.env_size, 0); + assert!(e.bindings.is_empty() && e.requested_modules.is_empty()); + Ok(()) +} + +/// §9.1.1.5.1† GetBindingValue of an indirect binding: a read of a named or +/// default import is `GetImportBinding` of the slot, in the body and, through +/// the parent hops, in a closure; a namespace import is an ordinary immutable +/// binding. +#[test] +fn reading_imports() -> Result<(), String> { + let m = module( + "import d, { a as b } from 'm'; import * as ns from 'n';\n\ + export function f() { return b + d; }\n\ + var x = b; var y = ns;", + )?; + let body_f = body(&m)?; + let slot_of = |n: &str| -> Result { Ok(binding(&m, n)?.slot) }; + let imports: Vec<(u32, u32)> = body_f + .instrs + .iter() + .filter_map(|i| match i { + Instr::GetImportBinding { env, idx, .. } => Some((*env, *idx)), + _ => None, + }) + .collect(); + // `var x = b;` reads b once in the body. + assert_eq!(imports.len(), 1); + assert_eq!(at(&imports, 0)?.1, slot_of("b")?); + // The namespace is read from its slot. + assert!(body_f.instrs.iter().any(|i| matches!( + i, + Instr::GetEnvSlot { idx, .. } if *idx == slot_of("ns").unwrap_or(u32::MAX) + ))); + // The function reads b and d, in a closure of the module environment (its + // callee environment, no hops: the function has no scope of its own). + let f = match binding(&m, "f")?.init { + ModuleBindingInit::Function(i) => m.program.functions.get(i as usize).ok_or("f")?, + _ => return Err("f".to_string()), + }; + let in_f: Vec = f + .instrs + .iter() + .filter_map(|i| match i { + Instr::GetImportBinding { idx, .. } => Some(*idx), + _ => None, + }) + .collect(); + assert_eq!(in_f, [slot_of("b")?, slot_of("d")?]); + assert!(matches!(f.instrs.first(), Some(Instr::GetCalleeEnv { .. }))); + Ok(()) +} + +/// §9.1.1.5.5† CreateImportBinding creates an immutable binding: assigning to an +/// import, or to a namespace import, is a TypeError, after the right side is +/// evaluated. +#[test] +fn assigning_to_an_import_throws() -> Result<(), String> { + for src in [ + "import { a } from 'm'; a = 1;", + "import { a } from 'm'; a++;", + "import * as ns from 'm'; ns = 1;", + "import d from 'm'; d += 1;", + ] { + let m = module(src)?; + let b = body(&m)?; + let throws = b.instrs.iter().any(|i| { + matches!( + i, + Instr::ThrowError { + kind: ErrorKind::TypeError, + .. + } + ) + }); + assert!(throws, "{src}"); + } + Ok(()) +} + +/// A closure that reads a module binding with a scope of its own between goes +/// up by `GetEnvParent`, as it does for a script's. +#[test] +fn closures_reach_the_module_environment() -> Result<(), String> { + let m = module( + "import { a } from 'm'; let n = 0;\n\ + export function f(p) { let q = p; return () => { n = n + q; return a; }; }", + )?; + let f = match binding(&m, "f")?.init { + ModuleBindingInit::Function(i) => m.program.functions.get(i as usize).ok_or("f")?, + _ => return Err("f".to_string()), + }; + // f's `q` is captured by the arrow, so f has an environment object, and the + // arrow reaches the module environment one hop up from its own callee + // environment (f's). + assert_eq!(count(f, |i| matches!(i, Instr::NewEnv { .. })), 1); + let arrow = m + .program + .functions + .iter() + .find(|g| g.kind == FunctionKind::Arrow) + .ok_or("no arrow")?; + assert!(count(arrow, |i| matches!(i, Instr::GetEnvParent { .. })) >= 1); + assert_eq!( + count(arrow, |i| matches!(i, Instr::GetImportBinding { .. })), + 1 + ); + Ok(()) +} + +/// §16.2.3.7† `export default` of an expression: NamedEvaluation of an +/// anonymous function or class with the name "default", then the binding +/// `*default*` is initialized. +#[test] +fn export_default_of_an_expression() -> Result<(), String> { + let m = module("export default 1 + 2;")?; + assert_eq!( + binding(&m, "*default*")?.init, + ModuleBindingInit::Uninitialized + ); + assert_eq!(m.local_export_entries.len(), 1); + let e = at(&m.local_export_entries, 0)?; + assert_eq!(e.export_name, Some(units("default"))); + assert_eq!(e.local_name, Some(units("*default*"))); + let b = body(&m)?; + assert!(count(b, |i| matches!(i, Instr::SetEnvSlot { .. })) >= 1); + // An anonymous arrow, function expression and class get the name "default". + for (src, kind) in [ + ("export default () => {};", FunctionKind::Arrow), + ("export default (function () {});", FunctionKind::Normal), + ("export default (class {});", FunctionKind::Class), + ("export default (async () => {});", FunctionKind::Arrow), + ] { + let m = module(src)?; + let f = m + .program + .functions + .iter() + .skip(1) + .find(|f| f.kind == kind) + .ok_or_else(|| format!("{src}: no function"))?; + assert_eq!(f.name, units("default"), "{src}"); + } + // A named function expression keeps its own name. + let m = module("export default (function named() {});")?; + assert_eq!( + m.program.functions.get(1).map(|f| f.name.clone()), + Some(units("named")) + ); + Ok(()) +} + +/// `export default function () {}` and `export default class {}`: declarations, +/// named "default", bound to `*default*`; a named one binds its own name and +/// exports it as "default". +#[test] +fn export_default_of_a_declaration() -> Result<(), String> { + let m = module("export default function () {}")?; + match binding(&m, "*default*")?.init { + ModuleBindingInit::Function(i) => { + let f = m.program.functions.get(i as usize).ok_or("f")?; + assert_eq!(f.name, units("default")); + } + ref other => return Err(format!("{other:?}")), + } + assert_eq!( + at(&m.local_export_entries, 0)?.local_name, + Some(units("*default*")) + ); + let m = module("export default function* () {}")?; + assert!(matches!( + binding(&m, "*default*")?.init, + ModuleBindingInit::Function(_) + )); + let m = module("export default async function () {}")?; + assert!(matches!( + binding(&m, "*default*")?.init, + ModuleBindingInit::Function(_) + )); + let m = module("export default class {}")?; + assert_eq!( + binding(&m, "*default*")?.init, + ModuleBindingInit::Uninitialized + ); + let ctor = m + .program + .functions + .iter() + .find(|f| f.kind == FunctionKind::Class) + .ok_or("no constructor")?; + assert_eq!(ctor.name, units("default")); + let m = module("export default class extends Object {}")?; + let ctor = m + .program + .functions + .iter() + .find(|f| f.kind == FunctionKind::Derived) + .ok_or("no constructor")?; + assert_eq!(ctor.name, units("default")); + // Named ones. + let m = module("export default function f() {}")?; + assert!(m.bindings.iter().all(|b| b.name != units("*default*"))); + assert_eq!( + at(&m.local_export_entries, 0)?.export_name, + Some(units("default")) + ); + assert_eq!(at(&m.local_export_entries, 0)?.local_name, Some(units("f"))); + let m = module("export default class C {}")?; + assert_eq!(at(&m.local_export_entries, 0)?.local_name, Some(units("C"))); + assert_eq!(binding(&m, "C")?.init, ModuleBindingInit::Uninitialized); + Ok(()) +} + +/// §16.2.1.6.1† HasTLA: `await` or `for await` outside any function makes the +/// body async (an Await op); one inside a function does not. +#[test] +fn top_level_await() -> Result<(), String> { + let m = module("await 1; export let a = await Promise.resolve(2);")?; + assert!(m.has_tla); + let b = body(&m)?; + assert_eq!(b.flavor, Flavor::Async); + assert_eq!(b.kind, FunctionKind::Module); + assert_eq!(count(b, |i| matches!(i, Instr::Await { .. })), 2); + let m = module("for await (const x of y) {}")?; + assert!(m.has_tla); + assert_eq!(body(&m)?.flavor, Flavor::Async); + assert!(count(body(&m)?, |i| matches!(i, Instr::Await { .. })) >= 1); + // Inside a function, an arrow, a method or a nested block of the body. + let m = module("async function f() { await 1; } export const g = async () => { await 2; };")?; + assert!(!m.has_tla); + assert_eq!(body(&m)?.flavor, Flavor::Plain); + let m = module("if (1) { try { await 1; } catch {} }")?; + assert!(m.has_tla); + // `await` as a binding name in a module is an error (the parser's), and a + // module with no await is plain. + let m = module("export var a = 1;")?; + assert!(!m.has_tla); + Ok(()) +} + +/// `this` at the top of a module is `undefined` (the runtime gives the body no +/// `this`), and an arrow function there reads it from a binding of the module +/// environment that the body's first ops write: not a user-visible binding. +#[test] +fn this_in_a_module() -> Result<(), String> { + let m = module("export const t = () => this; var u = this;")?; + let b = body(&m)?; + // The body reads `this` for `u` and, to capture it, once more. + assert!(count(b, |i| matches!(i, Instr::GetThis { .. })) >= 2); + assert_eq!(m.bindings.len(), 2); + assert!(m.env_size >= 3); + assert!(m.bindings.iter().all(|b| b.slot < m.env_size)); + Ok(()) +} + +/// Direct eval in a module is strict and sees the module's bindings, imports +/// among them, by name. +#[test] +fn direct_eval_in_a_module() -> Result<(), String> { + let m = module("import { a } from 'm'; let l = 1; var v; eval('l');")?; + let b = body(&m)?; + let info = b + .consts + .iter() + .find_map(|c| match c { + Const::EvalScope(i) => Some(i.clone()), + _ => None, + }) + .ok_or("no eval record")?; + assert!(info.strict); + let scope = info.scopes.last().ok_or("no scope")?; + assert_eq!(scope.kind, EvalScopeKind::Module); + assert!(scope.var_object.is_none()); + let kind_of = |n: &str| { + scope + .names + .iter() + .find(|e| e.name == units(n)) + .map(|e| e.kind) + }; + assert_eq!(kind_of("a"), Some(EvalBinding::Import)); + assert_eq!(kind_of("l"), Some(EvalBinding::Let)); + assert_eq!(kind_of("v"), Some(EvalBinding::Var)); + Ok(()) +} + +/// TDZ: an exported `let` read before its declaration in the body is checked, and +/// one read in a function is checked too (the function may run first). +#[test] +fn temporal_dead_zone_in_a_module() -> Result<(), String> { + let m = module("export function f() { return l; } export let l = 1; l;")?; + let b = body(&m)?; + // The read after the declaration, in the same function, needs no check. + assert_eq!(count(b, |i| matches!(i, Instr::ThrowIfHole { .. })), 0); + let f = match binding(&m, "f")?.init { + ModuleBindingInit::Function(i) => m.program.functions.get(i as usize).ok_or("f")?, + _ => return Err("f".to_string()), + }; + assert_eq!(count(f, |i| matches!(i, Instr::ThrowIfHole { .. })), 1); + let m = module("l; export let l = 1;")?; + assert_eq!( + count(body(&m)?, |i| matches!(i, Instr::ThrowIfHole { .. })), + 1 + ); + Ok(()) +} + +/// §13.3.10.1† `import(specifier)`: the specifier, an `undefined` for the +/// absent options, then `ImportCall`; with a second argument, its value. Allowed +/// in a script, in a function and in a module. +#[test] +fn dynamic_import() -> Result<(), String> { + let p = script("var p = import('./m.js');")?; + let f = func(&p, 0)?; + assert_eq!(count(f, |i| matches!(i, Instr::ImportCall { .. })), 1); + let at = f + .instrs + .iter() + .position(|i| matches!(i, Instr::ImportCall { .. })) + .ok_or("no import call")?; + // The options register is loaded with undefined just before. + let (spec, opts) = match f.instrs.get(at) { + Some(Instr::ImportCall { + specifier, options, .. + }) => (*specifier, *options), + _ => return Err("not an import call".to_string()), + }; + assert_ne!(spec, opts); + assert!( + f.instrs + .iter() + .take(at) + .any(|i| matches!(i, Instr::LoadUndefined { dst } if *dst == opts)) + ); + let p = script( + "function f(o) { return import('m', o); } import('a', { with: { type: 'json' } });", + )?; + assert_eq!( + count(func(&p, 0)?, |i| matches!(i, Instr::ImportCall { .. })), + 1 + ); + assert_eq!( + count(func(&p, 1)?, |i| matches!(i, Instr::ImportCall { .. })), + 1 + ); + // The options of the second call are evaluated: an object with a `with`. + assert!( + func(&p, 0)? + .instrs + .iter() + .any(|i| matches!(i, Instr::NewObject { .. })) + ); + let m = module("export const lazy = () => import('./x.js'); await import('y');")?; + let n: usize = m + .program + .functions + .iter() + .map(|f| count(f, |i| matches!(i, Instr::ImportCall { .. }))) + .sum(); + assert_eq!(n, 2); + // The argument's own evaluation order: the specifier first. + let p = script("import(a(), b());")?; + let f = func(&p, 0)?; + let calls: Vec = f + .instrs + .iter() + .enumerate() + .filter(|(_, i)| matches!(i, Instr::Call { .. })) + .map(|(k, _)| k) + .collect(); + let at = f + .instrs + .iter() + .position(|i| matches!(i, Instr::ImportCall { .. })) + .ok_or("no import call")?; + assert_eq!(calls.len(), 2); + assert!(calls.iter().all(|&c| c < at)); + Ok(()) +} + +/// §13.3.12.1† `import.meta`: one op, no operand but the destination. +#[test] +fn import_meta() -> Result<(), String> { + let m = module("export const url = import.meta.url; const f = () => import.meta;")?; + let n: usize = m + .program + .functions + .iter() + .map(|f| count(f, |i| matches!(i, Instr::ImportMeta { .. }))) + .sum(); + assert_eq!(n, 2); + let b = body(&m)?; + assert!(b.instrs.iter().any(|i| matches!(i, Instr::GetProp { .. }))); + Ok(()) +} + +/// Every op is emitted with the span of the AST node it was compiled for: +/// `import.meta`'s is the expression's. +#[test] +fn spans_of_import_ops() -> Result<(), String> { + let m = module("import.meta;")?; + let b = body(&m)?; + let at = b + .instrs + .iter() + .position(|i| matches!(i, Instr::ImportMeta { .. })) + .ok_or("no op")?; + let sp = b.spans.get(at).ok_or("no span")?; + assert_eq!((sp.start, sp.end), (0, 11)); + Ok(()) +} + +/// §13.5.1.2† `delete` of an optional chain: a property reference at the end is +/// deleted (the chain ending early is `true`); a chain that ends in a call is a +/// value, so `true`, after the call. +#[test] +fn delete_of_an_optional_chain() -> Result<(), String> { + for src in [ + "delete a?.b;", + "delete a?.[k];", + "delete a.b?.c;", + "delete (a?.b);", + ] { + let p = script(src)?; + let f = func(&p, 0)?; + assert_eq!( + count(f, |i| matches!(i, Instr::DeleteProp { .. })), + 1, + "{src}" + ); + assert!( + f.instrs + .iter() + .any(|i| matches!(i, Instr::JumpIfNullish { .. })), + "{src}" + ); + // The short-circuit path gives true. + assert!( + f.instrs + .iter() + .any(|i| matches!(i, Instr::LoadBool { flag: true, .. })), + "{src}" + ); + } + let p = script("delete a?.b();")?; + let f = func(&p, 0)?; + assert_eq!(count(f, |i| matches!(i, Instr::DeleteProp { .. })), 0); + assert!(count(f, |i| matches!(i, Instr::Call { .. })) >= 1); + assert!( + f.instrs + .iter() + .any(|i| matches!(i, Instr::LoadBool { flag: true, .. })) + ); + // `delete a?.b.c`: the property deleted is `c`, of `a.b`, which is read. + let p = script("delete a?.b.c;")?; + let f = func(&p, 0)?; + assert_eq!(count(f, |i| matches!(i, Instr::DeleteProp { .. })), 1); + assert!(count(f, |i| matches!(i, Instr::GetProp { .. })) >= 1); + Ok(()) +} + +/// §13.3.6.1† with a spread argument (`eval(...args)`): `DirectEvalSpread` over +/// the array ArgumentListEvaluation built, not `DirectEval`. +#[test] +fn direct_eval_with_a_spread_argument() -> Result<(), String> { + let p = script("var x = 1; eval(...args); eval('x', ...rest);")?; + let f = func(&p, 0)?; + assert_eq!(count(f, |i| matches!(i, Instr::DirectEvalSpread { .. })), 2); + assert_eq!(count(f, |i| matches!(i, Instr::DirectEval { .. })), 0); + assert!(count(f, |i| matches!(i, Instr::NewArray { .. })) >= 2); + // The scope record is the same as a plain direct eval's. + let info = f + .consts + .iter() + .find_map(|c| match c { + Const::EvalScope(i) => Some(i.clone()), + _ => None, + }) + .ok_or("no eval record")?; + assert!(!info.strict); + // The script has no binding with a slot, so no scope with an environment. + assert!(info.scopes.is_empty()); + Ok(()) +} + +/// Annex B.3.4†: a function declaration as the body of an `if` is treated as if +/// it were in a block, so Annex B.3.3.1's hoisting applies: its own binding in +/// the block, the variable of the same name assigned when the declaration is +/// evaluated. +#[test] +fn function_declaration_as_an_if_body() -> Result<(), String> { + let p = script("if (a) function f() {} else function g() {}")?; + // Both functions are templates; the global variables f and g are declared. + let f = func(&p, 0)?; + assert_eq!(count(f, |i| matches!(i, Instr::NewClosure { .. })), 2); + let decls = f + .consts + .iter() + .find_map(|c| match c { + Const::GlobalDeclarations(d) => Some(d.clone()), + _ => None, + }) + .ok_or("no declarations")?; + assert_eq!(decls.annexb, [units("f"), units("g")]); + // Evaluating each declaration assigns the global. + assert_eq!(count(f, |i| matches!(i, Instr::SetGlobal { .. })), 2); + // In a function the variable is a binding of the function. + let p = script("function h(a) { if (a) function f() {} return f; }")?; + let h = func(&p, 1)?; + assert_eq!(count(h, |i| matches!(i, Instr::NewClosure { .. })), 1); + assert_eq!(count(h, |i| matches!(i, Instr::SetGlobal { .. })), 0); + Ok(()) +} + +/// A function declaration as the body of a label (§14.13†, Annex B.3.2): an +/// ordinary function declaration at the position of the label. +#[test] +fn function_declaration_as_a_label_body() -> Result<(), String> { + let p = script("l: function f() {} function g() { m: function h() {} return h; }")?; + let f = func(&p, 0)?; + let decls = f + .consts + .iter() + .find_map(|c| match c { + Const::GlobalDeclarations(d) => Some(d.clone()), + _ => None, + }) + .ok_or("no declarations")?; + let names: Vec> = decls + .var_scoped + .iter() + .filter_map(|v| match v { + clazel_js_vm::bytecode::VarScoped::Function { name, .. } => Some(name.clone()), + _ => None, + }) + .collect(); + assert_eq!(names, [units("f"), units("g")]); + // In a function, h is hoisted: a closure at the function's entry. + let g = p + .functions + .iter() + .find(|f| f.name == units("g")) + .ok_or("no g")?; + assert_eq!(count(g, |i| matches!(i, Instr::NewClosure { .. })), 1); + Ok(()) +} + +/// Compiling a module twice gives the same record and the same code. +#[test] +fn compilation_is_deterministic() -> Result<(), String> { + let src = "import a, * as n from 'm'; export { a as b }; export default function () { return n; }\n\ + export const c = await 1; export * from 'z';"; + assert_eq!(module(src)?, module(src)?); + Ok(()) +} + +/// A larger module that uses most of the features at once compiles, passes +/// both checks, and its exports are all bindings. +#[test] +fn a_module_with_everything() -> Result<(), String> { + let m = module( + "import def, { named, other as alias } from './dep.js';\n\ + import * as ns from './ns.js' with { type: 'json' };\n\ + export { named as renamed } from './dep.js';\n\ + export * from './star.js';\n\ + export * as inner from './inner.js';\n\ + export let counter = 0;\n\ + export function inc() { counter++; return def + named + alias; }\n\ + export class Box { static make() { return new Box(); } #p = ns; get p() { return this.#p; } }\n\ + export default async function () { return await import('./late.js'); }\n\ + for (const k of Object.keys(ns)) { counter += k.length; }\n\ + try { await inc(); } finally { counter = 0; }\n\ + label: for (;;) { break label; }\n\ + export const meta = import.meta.url;", + )?; + assert!(m.has_tla); + assert_eq!(m.requested_modules.len(), 4); + for e in m + .local_export_entries + .iter() + .filter_map(|e| e.local_name.as_ref()) + { + assert!(m.bindings.iter().any(|b| &b.name == e)); + } + Ok(()) +} diff --git a/crates/clazel-js-vm/tests/test262.rs b/crates/clazel-js-vm/tests/test262.rs index 64fef8315..1dec20041 100644 --- a/crates/clazel-js-vm/tests/test262.rs +++ b/crates/clazel-js-vm/tests/test262.rs @@ -22,7 +22,8 @@ use clazel_js_syntax::ast::{Ast, NodeKind, Op as AOp}; use clazel_js_syntax::exec::parse; use clazel_js_syntax::spec::SourceGoal; use clazel_js_vm::bytecode::{Instr, check_program}; -use clazel_js_vm::compile::{CompileError, compile_script}; +use clazel_js_vm::compile::{CompileError, compile_module, compile_script}; +use clazel_js_vm::module::check_module; use std::collections::BTreeMap; use std::path::PathBuf; @@ -69,13 +70,15 @@ struct Counts { not_yet: usize, failed: usize, no_parse: usize, + /// Of the compiled sources, the modules. + modules: usize, features: BTreeMap<&'static str, usize>, /// For each construct, the number of compiled sources that use it. constructs: BTreeMap<&'static str, usize>, failures: Vec, } -/// The constructs of the compiler's second slice that `ast` uses. +/// The constructs of the compiler's second and third slices that `ast` uses. fn constructs_of(ast: &Ast) -> Vec<&'static str> { let mut found = std::collections::BTreeSet::new(); for (i, n) in ast.nodes.iter().enumerate() { @@ -106,9 +109,30 @@ fn constructs_of(ast: &Ast) -> Vec<&'static str> { NodeKind::PrivateIdentifier => "private names", NodeKind::NewTarget => "new.target", NodeKind::ForInStatement => "for-in", + NodeKind::ForOfStatement if n.flag => "for await", NodeKind::ForOfStatement => "for-of", + NodeKind::RegularExpressionLiteral => "regex literals", + NodeKind::GeneratorDeclaration | NodeKind::GeneratorExpression => "generators", + NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncFunctionExpression + | NodeKind::AsyncArrowFunction => "async functions", + NodeKind::AsyncGeneratorDeclaration | NodeKind::AsyncGeneratorExpression => { + "async generators" + } + NodeKind::YieldExpression if n.op == AOp::YieldDelegate => "yield*", + NodeKind::YieldExpression => "yield", + NodeKind::AwaitExpression => "await", + NodeKind::WithStatement => "with", NodeKind::OptionalExpression => "optional chaining", NodeKind::MethodDefinition if matches!(n.op, AOp::Getter | AOp::Setter) => "accessors", + NodeKind::MethodDefinition + if matches!( + n.op, + AOp::GeneratorMethod | AOp::AsyncMethod | AOp::AsyncGeneratorMethod + ) => + { + "generator and async methods" + } _ => continue, }; found.insert(name); @@ -122,7 +146,16 @@ impl Counts { self.no_parse = self.no_parse.saturating_add(1); return; }; - match compile_script(&ast, src) { + let compiled = if goal == SourceGoal::Module { + compile_module(&ast, src).and_then(|m| { + check_module(&m).map_err(CompileError::Internal)?; + self.modules = self.modules.saturating_add(1); + Ok(m.program) + }) + } else { + compile_script(&ast, src) + }; + match compiled { Ok(p) => match check_program(&p) { Ok(()) => { self.compiled = self.compiled.saturating_add(1); @@ -144,6 +177,22 @@ impl Counts { let n = self.constructs.entry("arguments object").or_insert(0); *n = n.saturating_add(1); } + // Direct eval and `delete` of a variable are in the bytecode too. + for (name, pred) in [ + ( + "direct eval", + (|i: &Instr| matches!(i, Instr::DirectEval { .. })) + as fn(&Instr) -> bool, + ), + ("delete of a variable", |i: &Instr| { + matches!(i, Instr::DeleteGlobal { .. } | Instr::WithDelete { .. }) + }), + ] { + if p.functions.iter().any(|f| f.instrs.iter().any(pred)) { + let n = self.constructs.entry(name).or_insert(0); + *n = n.saturating_add(1); + } + } } Err(e) => { self.failed = self.failed.saturating_add(1); @@ -239,6 +288,7 @@ fn test262_compiles() -> Result<(), String> { for (feature, n) in by_count { println!(" not yet: {n:6} {feature}"); } + println!(" of the compiled sources, {} are modules", counts.modules); for (construct, n) in &counts.constructs { println!(" compiled with: {n:6} {construct}"); } diff --git a/crates/clazel-js/Cargo.toml b/crates/clazel-js/Cargo.toml new file mode 100644 index 000000000..b3f22f52b --- /dev/null +++ b/crates/clazel-js/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "clazel-js" +version = "0.0.0" +edition.workspace = true +license.workspace = true +publish.workspace = true +description = "The JavaScript engine's embedding API for Clazel (phase-7.md item 7.8): Runtime, realms, scripts and modules, native functions, host objects, rooted handles, the Host trait, limits and the Terminator. This is the contract only; nothing here is implemented yet." + +# Engine crates only (`cargo xtask check` enforces that no engine crate reaches +# a browser crate; see ENGINE in xtask/src/status.rs): the parser and compiler +# for scripts, the interpreter, the built-ins, and the heap's value type. +[dependencies] +clazel-js-builtins = { path = "../clazel-js-builtins" } +clazel-js-heap = { path = "../clazel-js-heap" } +clazel-js-syntax = { path = "../clazel-js-syntax" } +clazel-js-vm = { path = "../clazel-js-vm" } + +# Verification tier (docs/VERIFICATION.md). Tier 0: this crate holds the +# contract and its stubs, and nothing is proven about it. +[package.metadata.clazel] +tier = 0 +target-tier = 0 + +[lints] +workspace = true diff --git a/crates/clazel-js/README.md b/crates/clazel-js/README.md new file mode 100644 index 000000000..a637ba087 --- /dev/null +++ b/crates/clazel-js/README.md @@ -0,0 +1,406 @@ +# clazel-js: the engine's embedding API + +`clazel-js` is the one crate a program names to embed Clazel's JavaScript +engine. The browser embeds it, and so can a command-line runtime, a +Node-style server, or another Rust program. This is phase-7.md item 7.8, and +"The engine as a library" in that file is the plan this README works out. + +**Status: the contract, and its first implementation.** The types and +signatures here are the proposal. The contract was written first, as the plan +says, so that people can review it before `clazel-webidl` and `clazel-web` +build on it; the first part of the implementation is on top of it without +changing a signature, over the interpreter (`clazel-js-vm`) and the built-ins +(`clazel-js-builtins`). What the VM does not have yet still returns +`Error::NotImplemented`; "What is implemented" below lists each item and why. +Tier 0, no proofs. What to review is in "Open questions". + +Read the API in `src/`, one file per topic: `runtime.rs` (Runtime, limits, +Terminator), `value.rs` (the four ways to hold a value), `scope.rs` (all the +operations), `code.rs` (realms, source, scripts, modules), `native.rs` +(native functions, host objects), `host.rs` (the `Host` trait), `error.rs`. +`cargo doc -p clazel-js --no-deps --open` shows the same with the examples. + +## What is implemented + +Works now, with tests that use the public API only (`tests/api.rs`, and +the doctests): `Runtime::new` (VM, built-ins, host), `scope`, `run_jobs`, +`terminator` (the interpreter's own flag), `limits`, `heap_used` (an estimate); +primitives and strings (UTF-16 exact), `kind` and the `as_*`, `to_*` and `is_*` +conversions, `strict_equals`, `same_value`, `instance_of`; `new_object`, `get`, +`set`, `has_property`, `define_property` (data properties), +`get_prototype_of`, `set_prototype_of`, `get_method`, `call`, `construct`; +`function`, `define_method`, `Args`, `new_host_object` with `host_data`, +`get_prototype_from_constructor`, `host_mut`, `check_terminated`; errors, +`new_exception`, `throw_error`, `throw`, `report`, `Error::Thrown`; +`create_realm` (once), `global`, realm and script host data; `compile_script`, +`run_script`, `eval_script`; `Handle`, `Weak`, `Member`; `gc` and +`clear_kept_objects` as no-ops. + +Still `NotImplemented`, each because the VM lacks the feature: arrays +(`new_array*`), promises (`new_promise` and the rest), `iterate` and symbols +(`new_symbol`, `well_known_symbol`), BigInt, ArrayBuffer, `delete_property` +(the heap cannot remove a property), accessor descriptors in `define_property`, +modules and synthetic modules (the compiler's module goal and the runtime's +module records), a second realm and `RealmOptions::global`, `construct` with a +`new_target` other than the constructor, `instance_of` of a bound function, +`ErrorKind::EvalError` and `UriError`. The infallible constructors for the +missing values (`new_symbol`, `well_known_symbol`, `bigint_from_*`) cannot +return an error, so they record it and the next fallible call returns it. + +**Where it deviates, so reviewers see it:** + +- **No completion value.** `run_script` and `eval_script` return `undefined` + because the compiler's script has no completion value. Results go out + through globals or native functions. +- **Re-entrance.** A native closure that JavaScript re-enters while it runs + gets `Error::Busy`, because the closures are stored in the VM they borrow + (the table is where a plain function pointer can find it). Open question 14. +- **Rooting.** There is no collector in this tree (open pull requests), so + nothing is ever freed: `Local`, `Handle`, `Weak` and `Member` are checked + (the runtime they belong to, the arena's generation) but root nothing; + `Weak::upgrade` is always `Some`, `HostObject::trace` and `Host::trace_roots` + are never called, `Scope::nested` releases nothing, and a `Handle` drop does + nothing. The API is written so that a collector can start rooting without a + signature change: every value passes through `Scope`, `Handle` is the only + long-lived root, and `Member` is reported by its owner. +- **The `Host` hooks.** None is called: they are what promises, modules, + `Date`, `eval` of strings and `FinalizationRegistry` call, and the VM has + none of them. The engine's job queue works (`Scope::enqueue_job_internal`, + `run_job`, `run_jobs`) but nothing makes a job yet, so `run_jobs` returns 0. +- **Exotic hooks of `HostObject`** are not called: a host object is an + ordinary object with its data beside it. +- **Limits.** `heap_bytes` and `value_stack_bytes` map to the arena's cell + limit and the frame region's slots (estimates), the heap limit gives + `OutOfMemory` and not a `RangeError`, `native_depth` counts only closures + made by `Scope::function`, the rest are not used (see `Runtime::new`). +- **Stack traces.** `ErrorReport::stack` is empty and `Source::start_line` is + not used: the VM keeps no source positions. + +## The boundary + +- Engine crates depend on no browser crate. `cargo xtask check` enforces it + with the `ENGINE` list in `xtask/src/status.rs`, which holds this crate. +- `clazel-js` depends on the engine's `clazel-js-vm`, `clazel-js-builtins`, + `clazel-js-syntax` and `clazel-js-heap` and no other crate. No type of + theirs is in its public API: a `Local` is private data over the heap's value. +- The browser uses only this public API. A binding that needs something the API + lacks gets it added here, not a private path. +- The Test262 runner (`clazel-js-shell`, a later item) is a second embedder and + builds `$262` from public calls alone. + +## A `Runtime` + +A `Runtime` is one ECMAScript agent: a heap, a job queue, limits and a `Host`. + +- **No global state.** No `static`, no thread-local. A process can hold many + runtimes. +- **`Send`, not `Sync`.** A runtime can move to another thread between calls, and + only one thread uses it at a time. Compile-checked by doctests. For this to + hold the `Host` is `Send` too. The browser runs one runtime per renderer, on + one thread (7.0(e)). +- **Everything goes through a scope.** `Runtime::scope(|scope| ...)` is the only + way in. Inside native functions and `Host` hooks the engine hands over a + `Scope` for that call. +- **Dropping a runtime** frees the heap and drops native closures and host + object data. It runs no JavaScript. + +Configuration is a `Config` of `Limits` and a `JitMode` (only `Off` until jit.md's +crate exists), and a `Host`: + +```rust +use clazel_js::{Config, DefaultHost, Key, Limits, RealmOptions, Runtime, Source}; + +fn main() -> Result<(), clazel_js::Error> { + let mut rt = Runtime::new(Config::new(Limits::unlimited()), DefaultHost::new())?; + let realm = rt.scope(|scope| scope.create_realm(RealmOptions::default()))?; + let sum = rt.scope(|scope| { + // The script's completion value is not produced yet (see "What is + // implemented"), so the result goes out through a global. + scope.eval_script(&realm, Source::new("sum.js", "var sum = 1 + 2;"))?; + let global = scope.global(&realm)?; + let value = scope.get(global, Key::from("sum"))?; + Ok(scope.as_number(value)) + })?; + assert_eq!(sum, Some(3.0)); + rt.run_jobs()?; + Ok(()) +} +``` + +## Realms + +`Scope::create_realm` makes a realm: a global object and its own built-ins. +`$262.createRealm()` is exactly this, and frames would be later. A `Realm` is a +root (cloneable, `Send`, `Sync`) and every call that runs code names its realm. +`Scope::global` reaches the global object, and installing `console` or `window` +is ordinary property definition on it. `RealmOptions::global` lets the +global object be a host object, which the browser's `Window` needs. +`Scope::set_realm_host_defined` attaches the embedder's data to a realm (HTML's +environment settings object). + +## Scripts and modules + +- **Source** is `Source { name, text, start_line, host_defined }`. The text is a + `&str`, a sequence of code points, as ECMA-262 defines source text. The name + is for stack traces and reports, and `host_defined` is the embedder's data + (base URL, fetch options), kept with the compiled code. +- **Scripts.** `eval_script` does `compile_script` then `run_script`, which are + separate so a host can follow HTML's "prepare the script element". A syntax + error is a thrown `SyntaxError`. +- **Modules** follow ECMA-262's phases, one method each: `parse_module`, + `load_requested_modules`, `link`, `evaluate_module`, plus `module_namespace` and + `module_status`. Loading is asynchronous and the host's: the engine calls + `Host::load_imported_module(referrer, request, payload)`, and the host + answers with `Scope::finish_loading_imported_module(payload, result)`, now or + on a later event-loop turn. Dynamic `import()` uses the same hook. The + `Referrer` carries the importing code's `host_defined` data, so the host knows + what to resolve against. `import.meta` goes through two more hooks. A host + can add its own resolver, or CommonJS (through synthetic modules), without + engine changes. +- **Jobs.** The engine queues jobs and runs them only when asked: + `Runtime::run_jobs` is a microtask checkpoint. The event loop is the host's. + HTML's lives in `clazel-web`, and a Node-style runtime would drive the same + calls from its own. + +## Values, and holding them + +Values live in the collector's heap, which does not move them (7.0(f)). Four +types hold a value, from cheapest to most durable: + +| Type | Roots the value? | Lives | Use | +|---|---|---|---| +| `Local<'s>` | yes, until its scope ends | one `Scope` | everything inside a call. `Copy`; cannot escape the closure (compile-checked) | +| `Handle` | yes, until dropped | any time, any thread | what outlives a call: a callback to run later, a promise's resolvers. `Clone`, `Send`, `Sync`; dropping needs no runtime | +| `Weak` | no | any time | side tables from platform objects to wrappers; `upgrade` gives `None` after collection | +| `Member` | only if its owner reports it | owner's choice | JS values inside a host object's data or the host, reported from `trace` so there is no uncollectable cycle | + +- **Stale is an error, never an alias.** Every reference carries the arena's + generation (7.5, 7.6). Using one that no longer names its object gives + `Error::StaleHandle`. A handle from another runtime gives `Error::WrongRuntime`. +- **When the collector runs:** inside an operation that allocates, or on + `Scope::gc()` (`$262.gc()`), at points the engine chooses. Never between two + of the embedder's own Rust statements, and never while tracing, which is given + no `Scope`. +- **Roots** are the value stack, every live `Local`, every `Handle`, the job + queue, and what the host reports from `Host::trace_roots` (for the browser: + the wrapper of every node in a tree) and each host object's `trace`. +- **Strings** are UTF-16 code unit sequences, as in the language. + `Scope::string` takes Rust text; `string_utf16` takes units, including lone + surrogates, which WebIDL's conversions need. `string_to_utf16` reads exactly, + and `string_to_string_lossy` replaces lone surrogates. + +## Native functions and host objects + +- **`Scope::function(name, length, Callable, f)`** makes a JavaScript function + from a Rust closure `Fn(&mut Scope, &Args) -> Result`. It is `Fn`, not + `FnMut`, because a native function can be re-entered through JavaScript it + calls. State it needs lives in a host object, in the `Host`, or behind a lock. + `Callable::Constructor` makes it usable with `new`, with `Args::new_target` + set, and `get_prototype_from_constructor` gives subclasses their prototype. +- **`HostObject`** is a trait for the Rust data of a JavaScript object. It is + typed (`host_data::` checks, `Error::WrongType` otherwise), is dropped when + the collector frees the object, reports the values it refers to from `trace`, + and may have four exotic hooks (`get_own_property`, `define_own_property`, + `delete_property`, `own_property_keys`) for WebIDL's legacy platform objects. + This is how `clazel-webidl` builds DOM wrappers. + +## `Host` + +One trait for everything ECMA-262 leaves to the host, each with a stated +default, so `DefaultHost` works for a program that needs none. Hooks get +`&mut self` and a `Scope`, may re-enter the engine, and may fail with an +`Error`, which the calling operation passes on. + +| Group | Methods (ECMA-262 name) | Default | +|---|---|---| +| Jobs | `enqueue_promise_job`, `enqueue_generic_job`, `enqueue_timeout_job`, `enqueue_finalization_registry_cleanup_job` | the engine's own queue | +| Job callbacks | `make_job_callback`, `call_job_callback` | a plain callback; call it | +| Modules | `load_imported_module`, `supported_import_attributes`, `get_import_meta_properties`, `finalize_import_meta` | refuse with a `TypeError`; none; none; nothing | +| Promises | `promise_rejection_tracker` | nothing | +| Code from strings | `ensure_can_compile_strings`, `has_source_text_available` | allow; yes | +| Objects | `ensure_can_add_private_element`, `resize_array_buffer` | allow; `Unhandled` | +| The world | `now_ms`, `local_tza`, `random_u64`, `default_locale` | system clock; UTC; (required); `en-US` | +| The collector | `trace_roots` | nothing | + +The browser's answers for the world are its own: 7.0(b)'s fixed offset and +`en-US`, in `clazel-web`. The engine fixes none of them. `random_u64` has no +default because the engine has no entropy source and rule 8 forbids adding a +dependency for one. `DefaultHost` implements it from the standard library. + +**`$262`, built from public calls.** The shell's hooks need nothing private: + +| `$262` | Public call | +|---|---| +| `createRealm()` | `Scope::create_realm`, then `global` | +| `evalScript(src)` | `Scope::eval_script` in the realm | +| `gc()` | `Scope::gc` | +| `detachArrayBuffer(buf)` | `Scope::detach_array_buffer` | +| `agent` | none; see the open questions | + +## Errors and exceptions + +`Result` throughout. One variant is a JavaScript value; the rest are the +engine's and cannot be caught by JavaScript: + +- `Thrown(Exception)`: JavaScript threw. An `Exception` holds the value in a + `Handle`, so it can be reported after the call returned. This includes the + `SyntaxError` for bad source and the `RangeError`s the limits raise. + `Scope::report` turns it into an `ErrorReport` (name, message, stack) without + running user code. +- `Terminated`: a `Terminator` ended the evaluation. +- `OutOfMemory`: the heap limit was hit and not even the `RangeError` could be + allocated. Rare. +- `StaleHandle`, `WrongRuntime`, `WrongType`, `Busy`: misuse of the API, + caught and reported, never undefined behaviour. +- `NotImplemented`: something the VM does not have yet; see "What is + implemented". + +A native function returns errors of either kind with `?`. To throw: +`return Err(scope.throw_error(ErrorKind::TypeError, "..."))`, or +`scope.throw(value)` for a value the embedder made (a `DOMException`). + +## Limits and the `Terminator` + +**Limits are the embedder's.** `Limits` has no `Default`, and a struct literal must +name every field, so nothing is fixed in the engine and adding a limit makes +each embedder choose. `Limits::unlimited()` is a named choice for tests. + +| Limit | When passed | +|---|---| +| `heap_bytes` | full collection first, then a `RangeError` | +| `value_stack_bytes` | `RangeError`, "too much recursion" | +| `native_depth` | `RangeError`; counts built-ins that call back into JS | +| `code_bytes` | the JIT stops compiling; not an error | +| `regexp_backtrack_entries` | `RangeError` from the match | +| `nesting_depth` | `SyntaxError` (parser), `RangeError` (`JSON.parse`) | + +Timers and fetches in flight are the host's to bound, not the engine's. + +**`Terminator`** (`Runtime::terminator()`; `Clone`, `Send`, `Sync`) stops a runtime +from another thread. It sets a flag; the interpreter checks it at loop back-edges +and calls, JIT code at the same points, built-ins in their long loops, and native +functions via `Scope::check_terminated`. The evaluation ends with +`Error::Terminated`. JavaScript cannot catch it, and no `finally` runs. The flag +stays set until `Terminator::clear`. The `Terminator` itself is implemented +and tested here, because it is only an atomic flag. The browser keeps 7.0(g)'s +policy (it ends a stuck renderer, on the user's word) and may use this later to +keep the tab. jit.md adds no interaction beyond "JIT code checks the same flag at +the same points". + +**Executable memory** is the embedder's choice (jit.md J.0(b)). `JitMode` gains a +variant that carries a `CodeMapper` when `clazel-jit` exists. Until then it is +interpreter only, which is complete. + +## What the browser's bindings will use + +| Browser need | API | +|---|---| +| Install `window`, `document`, `console` | `create_realm` with a host-object global; `global`; `define_property`, `define_method` | +| Interface objects and prototypes (7.12) | `function` with `Callable::Constructor`; `get_prototype_from_constructor`; `define_property`; `set_prototype_of` | +| Wrappers, one per platform object (7.12, 7.13) | `new_host_object` with typed data; `Weak` for the side table; `Host::trace_roots` for nodes in a tree | +| `NodeList[0]`, `HTMLCollection.name` | `HostObject`'s exotic hooks | +| WebIDL conversions (3.2) | `to_number`, `to_string`, `to_boolean`, `to_object`, `is_callable`, `get_method`, `iterate`, `string_utf16`, `bigint_from_*` | +| `DOMException`, TypeErrors | `throw_error`, `throw` | +| Event listeners calling JS (7.14) | `Handle` or `Member` to the callback; `call`; `report` for uncaught exceptions | +| Promises from `fetch()`, `import()` (7.15, 7.16) | `new_promise`, `resolve_promise`, `reject_promise`, `promise_then`, `promise_state` | +| Script elements (7.15) | `compile_script`, `run_script`, `parse_module`, `load_requested_modules`, `link`, `evaluate_module`, `Host::load_imported_module` | +| Event loop, microtasks, `queueMicrotask` (7.15) | `Runtime::run_jobs`; `Host::enqueue_promise_job`, `make_job_callback`, `call_job_callback`; `clear_kept_objects` after a checkpoint | +| Unhandled rejections | `Host::promise_rejection_tracker` | +| `Date`, `Math.random`, locale | `Host::now_ms`, `local_tza`, `random_u64`, `default_locale` | +| A stuck page (7.0(g)) | `Terminator` | + +## What is deliberately not in the API + +- **A C API** (`clazel-js-capi`): Phase 8, and needs its own decision under rule 1. +- **Stability.** Rust only, unstable, no semver promise until Phase 8 has a second + real user. +- **Node as a drop-in.** A Node-compatible runtime on `clazel-js` is the + realistic path, not V8 API emulation. +- **Threads and shared memory**: `SharedArrayBuffer`, `Atomics`, and so + `$262.agent`; one runtime per thread only (7.0(b), 7.0(e)). +- **`Intl`, Temporal, WebAssembly, a debugger protocol, heap snapshots.** +- **Event loops, timers, `console`, `fetch`, a DOM.** The embedder's. The engine + has none and calls the `Host` instead. +- **Catching panics from native functions.** The lints forbid them here, and an + embedder's panic is its own. +- **Safety against a hostile embedder is not a goal; safety against hostile + JavaScript is.** An embedder that runs untrusted code without the browser's + sandbox relies on the engine's own guarantees: no `unsafe` outside + `clazel-sys`, the arena's generation checks, the validator's theorem for JIT + code, and the limits. + +## Open questions + +Nothing here is a decision the plan marks as the maintainer's. These are what the +plan leaves open and this contract had to settle, or leaves unsettled, so please +look at each. + +1. **After `Terminated`, is the runtime usable?** The proposal: the flag is + sticky, so every call ends `Terminated` until the embedder calls + `Terminator::clear`; after that the heap is consistent and the runtime + usable. It rests on `finally` blocks not running. It makes termination safe + for a runtime that is then dropped, and for the browser, but "usable after" + is a promise to implement and test. +2. **Handle model.** `Local<'s>` scoped to a closure, plus `Handle` (reference + counted, dropped without the runtime) and `Member` (reported from `trace`). + Plan 7.0(f) says "a scoped handle stack, checked on scope exit". The scope + here is a closure with a lifetime the compiler enforces, which is checked + earlier than at exit. The cost is that a value cannot be returned from a + nested scope except as a `Handle`; an `escape` call is possible if that bites. +3. **`Host: Send`.** It keeps `Runtime: Send`. It means the browser's host cannot + hold `Rc`s of DOM state: nodes are arena ids (Phase 4), which should work, + but it is the contract's strictest demand on `clazel-web`. +4. **Exotic hooks.** Four are given; WebIDL §3.9 and §3.10 (legacy platform + objects, named properties) may need `set`/`get` receivers or more. The + set is to be fixed against 7.12's generator, and may grow. +5. **The global's `this` value** (HTML's `WindowProxy`) is not in `RealmOptions`. + ECMA-262 lets a host set it separately from the global object. Needed by + 7.15, and the contract does not yet say how. +6. **Synthetic modules** (`new_synthetic_module`, `set_synthetic_export`) are + reserved in the contract, since "a host can add CommonJS" needs them and + JSON modules do. The plan does not list them as a Phase 7 deliverable. +7. **`$262.agent`.** The plan lists `agent` among the host hooks, and 7.0(b) + leaves out `SharedArrayBuffer` and `Atomics`, which `agent` exists to test. + This API has no agent hook, and the shell skips those tests. If another + host wants agents in Phase 8, a `Runtime` per thread plus shared memory + needs its own design. +8. **Hooks the plan's list lacks.** `HostEnqueueFinalizationRegistryCleanupJob` + (7.6 needs it) and `HostGetSupportedImportAttributes` are in the trait; + the plan's list does not name them. 7.4's check against the pinned edition + decides which stay. +9. **ECMA-262 section numbers** are not cited on the hooks, because 7.1 has not + pinned an edition. They are named by abstract operation instead, which does + not change between editions. Rule 5 asks for sections, so they are added when + 7.1 pins one. Until then the citations are incomplete. +10. **`run_jobs` and a throwing job.** A job that throws stops the run and + returns the exception; later jobs stay queued. A host that wants + "report and continue" overrides the queue hooks and runs jobs itself. + The alternative is one more hook. The plan names none. +11. **`Source` text as `&str`.** Source with a lone surrogate cannot be written. ECMA-262 + source text is code points, and the browser decodes with `clazel-encoding`, + which gives scalar values only, so nothing is lost in practice. Say so if + UTF-16 source is wanted. +12. **Where `CodeMapper` lives.** jit.md names it; this crate cannot depend on a + crate that does not exist. Likely `clazel-jit` defines it and `clazel-js` + re-exports it. +13. **`OutOfMemory` versus `RangeError`.** 7.19 says the heap limit gives a + `RangeError`. This contract keeps `OutOfMemory` for when even that cannot + be allocated. If that case can be avoided by reserving room for one + error object, the variant can go. +14. **Native closures and re-entrance.** `Scope::function` takes `Fn + Send`. + Re-entering a running closure needs it shared with the call that is + running, which for a `Send` runtime means `Fn + Send + Sync` (an `Arc`), + or a runtime that is not `Send`. With `Send` alone the implementation + stores each closure in the VM's embedder slot and takes it out while it + runs, so a re-entrant call is `Error::Busy`. Proposal: add `Sync` to the + bound (the browser's closures hold ids, not `Rc`s, as open question 3 + already asks of the host). +15. **Infallible constructors that cannot work yet.** `bigint_from_*`, + `new_symbol` and `well_known_symbol` return a `Local`, not a `Result`, so + when the VM lacks the type the error is held for the next fallible call. + Proposal: make them return `Result` (they allocate, so `string` and + `string_utf16` could fail the same way, with `OutOfMemory`, which is also + held today). +16. **Script completion value.** `run_script` documents the value of the last + expression statement; the compiler does not produce it yet. No signature + change, but embedders should not rely on it until the compiler does. diff --git a/crates/clazel-js/src/code.rs b/crates/clazel-js/src/code.rs new file mode 100644 index 000000000..962c73cf6 --- /dev/null +++ b/crates/clazel-js/src/code.rs @@ -0,0 +1,330 @@ +//! Realms, source text, scripts and modules. + +use core::any::Any; +use core::marker::PhantomData; + +use crate::native::HostObject; +use crate::value::Handle; + +/// A realm: a global object and its own copy of every built-in +/// (ECMA-262's Realm Record). +/// +/// - **Why more than one.** `$262.createRealm()` in Test262, and later a +/// frame. A realm's `Array` is not another realm's `Array`, and +/// `instanceof` across them is false, as in a browser. +/// - **Lifetime.** A `Realm` is a root: the realm and everything reachable +/// from its global object live until the last clone is dropped. Like +/// [`Handle`] it is `Send`, `Sync` and `Clone`, and needs a +/// [`Scope`](crate::Scope) to do anything. +/// - **No ambient realm.** Every call that runs code names the realm. Inside a +/// native function, [`Scope::current_realm`](crate::Scope::current_realm) +/// is the realm of the function being called. +/// +/// This stays `no_run`: the VM has one global environment, so a second +/// `create_realm` is `NotImplemented`. +/// +/// ```no_run +/// use clazel_js::{Runtime, RealmOptions, Source}; +/// +/// fn two_realms(rt: &mut Runtime) -> clazel_js::Result<()> { +/// let (a, b) = rt.scope(|scope| { +/// Ok((scope.create_realm(RealmOptions::default())?, +/// scope.create_realm(RealmOptions::default())?)) +/// })?; +/// rt.scope(|scope| { +/// let same = scope.eval_script(&a, Source::new("t.js", "[] instanceof Array"))?; +/// assert_eq!(scope.as_boolean(same), Some(true)); +/// // A realm's own object is not an instance of the other realm's Array. +/// let global_b = scope.global(&b)?; +/// let _ = global_b; +/// Ok(()) +/// }) +/// } +/// ``` +#[derive(Clone, Debug)] +pub struct Realm { + root: Handle, +} + +impl Realm { + pub(crate) fn new(root: Handle) -> Realm { + Realm { root } + } + + /// The root that keeps the realm alive. + pub fn as_handle(&self) -> &Handle { + &self.root + } +} + +/// How to build a realm. The default is an ordinary global object with the +/// standard built-ins. +/// +/// A host-object global is `NotImplemented` yet (the VM's global is an +/// ordinary object), so this only builds the options: +/// +/// ``` +/// use clazel_js::{Exotic, HostObject, RealmOptions}; +/// +/// /// The browser's `Window`: a host object serving as the global object. +/// struct Window; +/// impl HostObject for Window {} +/// +/// let options = RealmOptions { +/// global: Some(Box::new(Window)), +/// }; +/// # let _ = options; +/// ``` +#[derive(Default)] +pub struct RealmOptions { + /// Make the global object a host object with this data, whose prototype is + /// the realm's `Object.prototype`. `None` makes an ordinary object. Its + /// exotic hooks answer for the global's own properties. The global's `this` + /// value (HTML's `WindowProxy`) is an open question in the README. + pub global: Option>, +} + +/// Source text to compile, with where it came from. +/// +/// - **Text** is a `&str`: a sequence of Unicode code points, as ECMA-262 +/// defines source text. The browser decodes bytes to text with +/// `clazel-encoding` before calling. +/// - **Name** is what stack traces and error reports show: a URL for a +/// script from the network, `"inline"` or similar for the rest. +/// - **`start_line`** is the 1-based line number of the text's first line, so +/// an inline script in a page reports page line numbers. +/// - **`host_defined`** is the embedder's data about this code (a base URL, +/// fetch options). It is kept with the compiled [`Script`] or [`Module`] and +/// given back by [`Scope::host_defined`](crate::Scope::host_defined) +/// and to the [`Host`](crate::Host) hooks as part of a [`Referrer`]. +/// +/// ``` +/// use clazel_js::Source; +/// +/// struct BaseUrl(String); +/// +/// let source = Source::new("https://example.test/app.js", "export default 1;") +/// .with_start_line(1) +/// .with_host_defined(Box::new(BaseUrl("https://example.test/".to_owned()))); +/// assert_eq!(source.name(), "https://example.test/app.js"); +/// ``` +pub struct Source<'a> { + name: &'a str, + text: &'a str, + start_line: u32, + host_defined: Option>, +} + +impl<'a> Source<'a> { + /// Source text with a name, starting at line 1, with no host data. + pub fn new(name: &'a str, text: &'a str) -> Source<'a> { + Source { + name, + text, + start_line: 1, + host_defined: None, + } + } + + /// Sets the line number of the first line. + #[must_use] + pub fn with_start_line(mut self, start_line: u32) -> Source<'a> { + self.start_line = start_line; + self + } + + /// Attaches the embedder's data. + #[must_use] + pub fn with_host_defined(mut self, data: Box) -> Source<'a> { + self.host_defined = Some(data); + self + } + + pub(crate) fn into_host_defined(self) -> Option> { + self.host_defined + } + + /// The name. + pub fn name(&self) -> &str { + self.name + } + + /// The text. + pub fn text(&self) -> &str { + self.text + } + + /// The line number of the first line. + pub fn start_line(&self) -> u32 { + self.start_line + } + + /// The embedder's data, if any. + pub fn host_defined(&self) -> Option<&(dyn Any + Send)> { + self.host_defined.as_deref() + } +} + +/// A parsed classic script (ECMA-262's Script Record), not yet run. +/// +/// Parsing and running are separate so a host can follow HTML's "prepare the +/// script element", which fetches and compiles, then runs when the parser +/// or the event loop says. [`Scope::eval_script`](crate::Scope::eval_script) does both at +/// once. A `Script` is a root, `Clone`, `Send` and `Sync`, and may be run +/// more than once; top-level `var`s and functions are created in the realm +/// each time it runs, as for a script run twice. +/// +/// ``` +/// use clazel_js::{Key, Realm, Runtime, Script, Source}; +/// +/// fn compile_then_run(rt: &mut Runtime, realm: &Realm) -> clazel_js::Result<()> { +/// let script: Script = rt.scope(|scope| { +/// scope.compile_script(realm, Source::new("later.js", "globalThis.n = 1")) +/// })?; +/// // ... the parser unblocks ... +/// rt.scope(|scope| { +/// scope.run_script(&script)?; +/// Ok(()) +/// }) +/// } +/// # let mut rt = clazel_js::Runtime::new(clazel_js::Config::new(clazel_js::Limits::unlimited()), clazel_js::DefaultHost::new())?; +/// # let realm = rt.scope(|scope| scope.create_realm(clazel_js::RealmOptions::default()))?; +/// # compile_then_run(&mut rt, &realm)?; +/// # let n = rt.scope(|scope| { +/// # let global = scope.global(&realm)?; +/// # let n = scope.get(global, Key::from("n"))?; +/// # Ok(scope.as_number(n)) +/// # })?; +/// # assert_eq!(n, Some(1.0)); +/// # Ok::<(), clazel_js::Error>(()) +/// ``` +#[derive(Clone, Debug)] +pub struct Script { + root: Handle, + /// The index of the script's code in the VM's table. + main: u32, +} + +impl Script { + pub(crate) fn new(root: Handle, main: u32) -> Script { + Script { root, main } + } + + pub(crate) fn main(&self) -> u32 { + self.main + } + + /// The root that keeps the script alive. + pub fn as_handle(&self) -> &Handle { + &self.root + } +} + +/// A module (ECMA-262's Source Text Module Record or a synthetic one). +/// +/// The life of a module follows ECMA-262 §16.2.1.5, which the methods of +/// [`Scope`](crate::Scope) name: parse, load requested modules (the host +/// supplies each import through [`Host::load_imported_module`](crate::Host::load_imported_module)), +/// link, evaluate. `Module` is a root, `Clone`, `Send` and `Sync`. +/// +/// This stays `no_run`: modules return `NotImplemented` until the VM has +/// them. +/// +/// ```no_run +/// use clazel_js::{ModuleStatus, Realm, Runtime, Source}; +/// +/// /// Loads and runs a module whose imports the host can already resolve. +/// fn run_module(rt: &mut Runtime, realm: &Realm, text: &str) -> clazel_js::Result<()> { +/// let module = rt.scope(|scope| scope.parse_module(realm, Source::new("m.js", text)))?; +/// rt.scope(|scope| { +/// let loaded = scope.load_requested_modules(&module)?; +/// let _ = loaded; // a promise: settled when every import has been finished +/// Ok(()) +/// })?; +/// rt.run_jobs()?; +/// rt.scope(|scope| { +/// if scope.module_status(&module)? == ModuleStatus::Unlinked { +/// scope.link(&module)?; +/// } +/// let _done = scope.evaluate_module(&module)?; // a promise, for top-level await +/// Ok(()) +/// }) +/// } +/// ``` +#[derive(Clone, Debug)] +pub struct Module { + root: Handle, +} + +impl Module { + /// The root that keeps the module alive. + pub fn as_handle(&self) -> &Handle { + &self.root + } +} + +/// A module's state (ECMA-262's Cyclic Module Record `[[Status]]`). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ModuleStatus { + /// Just created by `parse_module`. + New, + /// Its requested modules are loaded; not yet linked. + Unlinked, + /// `link` is running. + Linking, + /// Linked, not yet evaluated. + Linked, + /// Evaluation is running. + Evaluating, + /// Evaluation is waiting on top-level `await`. + EvaluatingAsync, + /// Evaluation finished, with a value or an error (kept by the module). + Evaluated, +} + +/// One import attribute: `with { type: "json" }` is `("type", "json")`. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ImportAttribute { + /// The attribute's key. + pub key: String, + /// Its value. + pub value: String, +} + +/// What a module asks to import: ECMA-262's ModuleRequest Record. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ModuleRequest { + /// The module specifier, exactly as written in the source. + pub specifier: String, + /// The import attributes, in source order. + pub attributes: Vec, +} + +/// The code that asked for an import: the referrer of +/// [`Host::load_imported_module`](crate::Host::load_imported_module). Its +/// [host data](crate::Scope::host_defined) tells the host where to resolve +/// the specifier against. +#[derive(Clone, Copy, Debug)] +pub enum Referrer<'a> { + /// A classic script (a dynamic `import()` in one). + Script(&'a Script), + /// A module (a static `import` or a dynamic `import()` in one). + Module(&'a Module), + /// Neither: `import()` in `eval` or `Function` code outside any script + /// or module, which has only the realm. + Realm(&'a Realm), +} + +/// The engine's token for one pending import, passed to +/// [`Host::load_imported_module`](crate::Host::load_imported_module) and given +/// back to [`Scope::finish_loading_imported_module`](crate::Scope::finish_loading_imported_module). +/// +/// A host may finish it at once or on a later turn of its event loop, from +/// the same thread as the runtime. It must be finished exactly once; the type +/// is consumed by finishing it. Dropping it without finishing leaves the +/// module graph's load promise pending for ever, which is a bug in the host +/// but not unsafe. +#[derive(Debug)] +pub struct LoadPayload { + _pending: PhantomData<()>, +} diff --git a/crates/clazel-js/src/error.rs b/crates/clazel-js/src/error.rs new file mode 100644 index 000000000..6b70fb3e1 --- /dev/null +++ b/crates/clazel-js/src/error.rs @@ -0,0 +1,200 @@ +//! Errors and exceptions. + +use core::fmt; + +use crate::value::Handle; + +/// What every fallible call in this crate returns. +pub type Result = core::result::Result; + +/// The kinds of error object the engine can create (ECMA-262's native error +/// types). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ErrorKind { + /// `Error`. + Error, + /// `EvalError`. + EvalError, + /// `RangeError`. + RangeError, + /// `ReferenceError`. + ReferenceError, + /// `SyntaxError`. + SyntaxError, + /// `TypeError`. + TypeError, + /// `URIError`. + UriError, +} + +/// A value JavaScript threw, held by a rooted [`Handle`]. +/// +/// An `Exception` is what `catch` would see. It stays alive for as long as it +/// is held, whichever scope it was thrown in, so an embedder can report it +/// after the call that threw it has returned. +/// +/// ``` +/// use clazel_js::{Error, Realm, Scope, Source}; +/// +/// fn report(scope: &mut Scope<'_>, realm: &Realm) -> clazel_js::Result { +/// match scope.eval_script(realm, Source::new("page.js", "null.x")) { +/// Err(Error::Thrown(exception)) => { +/// let report = scope.report(&exception)?; +/// println!("{}: {}", report.name, report.message); +/// Ok(report.name) +/// } +/// other => other.map(|_| String::new()), +/// } +/// } +/// # let mut rt = clazel_js::Runtime::new(clazel_js::Config::new(clazel_js::Limits::unlimited()), clazel_js::DefaultHost::new())?; +/// # let realm = rt.scope(|scope| scope.create_realm(clazel_js::RealmOptions::default()))?; +/// # let name = rt.scope(|scope| report(scope, &realm))?; +/// # assert_eq!(name, "TypeError"); +/// # Ok::<(), clazel_js::Error>(()) +/// ``` +#[derive(Clone, Debug)] +pub struct Exception { + value: Handle, +} + +impl Exception { + /// Wraps a value to be reported as thrown, for example to refuse a module + /// load in [`Scope::finish_loading_imported_module`](crate::Scope::finish_loading_imported_module). + /// The engine builds the ones it returns in [`Error::Thrown`]. + pub fn from_handle(value: Handle) -> Exception { + Exception { value } + } + + /// The thrown value, as a rooted handle. Turn it back into a + /// [`Local`](crate::Local) with [`Scope::local`](crate::Scope::local). + pub fn value(&self) -> &Handle { + &self.value + } +} + +/// A thrown error described in plain Rust types, for logging and for the +/// browser's "report the exception" step (which is HTML's, not the engine's; +/// the engine only supplies these fields). +/// +/// Built by [`Scope::report`](crate::Scope::report). It never runs user code: +/// a getter that throws while the report is built leaves that field empty. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct ErrorReport { + /// `name` of the thrown object, or `"Error"` if it has none. For a thrown + /// non-object, the type of the value, such as `"string"`. + pub name: String, + /// `message` of the thrown object, or the string conversion of a thrown + /// primitive. + pub message: String, + /// The call stack when the error object was created, innermost first. + /// Empty if the thrown value is not an error object. + pub stack: Vec, +} + +/// One frame of a call stack. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct StackFrame { + /// The function's name, or `None` for top-level code and anonymous + /// functions. + pub function: Option, + /// The `name` of the [`Source`](crate::Source) the frame's code came from. + pub source: String, + /// 1-based line, counting from the source's `start_line`. + pub line: u32, + /// 1-based column, in UTF-16 code units. + pub column: u32, +} + +/// Why a call failed. +/// +/// Exactly one variant, [`Thrown`](Error::Thrown), is a JavaScript value. +/// Every other variant is the engine's or the embedder's, and JavaScript +/// cannot observe or catch it. A native function passes both kinds back with +/// `?`. +/// +/// This stays `no_run`: the script would loop for ever. +/// +/// ```no_run +/// use clazel_js::{Error, Realm, Scope, Source}; +/// +/// fn run(scope: &mut Scope<'_>, realm: &Realm) { +/// match scope.eval_script(realm, Source::new("a.js", "while (true) {}")) { +/// Ok(_) => {} +/// Err(Error::Thrown(_)) => { /* the script threw; `catch` could have caught it */ } +/// Err(Error::Terminated) => { /* a Terminator stopped it; nothing in JS ran after */ } +/// Err(other) => eprintln!("engine error: {other}"), +/// } +/// } +/// ``` +#[derive(Clone, Debug)] +#[non_exhaustive] +pub enum Error { + /// JavaScript threw this value. Includes the `SyntaxError` for source + /// that does not parse, and the `RangeError`s the limits report + /// (heap, value stack, nested native calls, nesting depth, RegExp + /// backtracking). All of them can be caught by JavaScript. + Thrown(Exception), + /// A [`Terminator`](crate::Terminator) ended the evaluation. JavaScript + /// cannot catch it, and no `finally` block runs while it propagates. + Terminated, + /// The heap limit was reached and the engine could not even allocate the + /// `RangeError` to report it. This is the rare case; the ordinary report + /// of the heap limit is [`Thrown`](Error::Thrown). + OutOfMemory, + /// The handle names something that no longer exists, so using it would + /// have aliased a different object. The generation check caught it. + StaleHandle, + /// The handle, realm, script or module belongs to another + /// [`Runtime`](crate::Runtime). + WrongRuntime, + /// The value was not of the type the call needs. The text names what was + /// wanted, for example `"host object of this type"`. + WrongType(&'static str), + /// The data being asked for is already borrowed by a call further up the + /// stack (a host object's data while one of its own hooks runs). + Busy, + /// The contract is written but the body is not. The text names the call. + /// This variant exists only until item 7.8 builds the implementation. + NotImplemented(&'static str), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::Thrown(_) => f.write_str("JavaScript threw a value"), + Error::Terminated => f.write_str("terminated"), + Error::OutOfMemory => f.write_str("out of memory"), + Error::StaleHandle => f.write_str("stale handle"), + Error::WrongRuntime => f.write_str("handle from another runtime"), + Error::WrongType(what) => write!(f, "wrong type: wanted {what}"), + Error::Busy => f.write_str("already borrowed further up the stack"), + Error::NotImplemented(what) => write!(f, "not implemented: {what}"), + } + } +} + +impl std::error::Error for Error {} + +/// The body of every call whose implementation is still to come: names the +/// call, uses its arguments so the contract's parameter names stay as written, +/// and returns [`Error::NotImplemented`]. +macro_rules! not_implemented { + ($what:expr $(, $arg:expr)* $(,)?) => {{ + let _ = ($(&$arg,)*); + ::core::result::Result::Err($crate::Error::NotImplemented($what)) + }}; +} +pub(crate) use not_implemented; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn display_names_the_unimplemented_call() { + assert_eq!( + Error::NotImplemented("Runtime::new").to_string(), + "not implemented: Runtime::new" + ); + } +} diff --git a/crates/clazel-js/src/host.rs b/crates/clazel-js/src/host.rs new file mode 100644 index 000000000..3564523bd --- /dev/null +++ b/crates/clazel-js/src/host.rs @@ -0,0 +1,488 @@ +//! The [`Host`] trait: everything ECMA-262 leaves to the host, and what the +//! engine needs from the world. +//! +//! The ECMA-262 section of each hook is not cited yet. Rule 5 asks for the +//! exact section, and the edition they refer to is pinned in item 7.1, which +//! has not run. Each hook is named by the abstract operation's name, which +//! does not change between editions; section numbers join when 7.1 pins one, +//! and 7.4 checks this list against the pinned edition's list of host hooks. + +use core::any::Any; +use std::hash::{BuildHasher, RandomState}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use crate::code::{LoadPayload, Module, ModuleRequest, Realm, Referrer}; +use crate::error::{ErrorKind, Result}; +use crate::native::Tracer; +use crate::scope::Scope; +use crate::value::{Handle, Local}; + +/// A queued unit of work for the event loop: a promise reaction, a +/// `FinalizationRegistry` cleanup, or a generic job. +/// +/// The engine makes a `Job` and gives it to the host to queue. Running it is +/// [`Scope::run_job`](crate::Scope::run_job), once, on the runtime's thread. A +/// `Job` is a root, so dropping one without running it only loses the work. +/// It is `Send` and `Sync` like a [`Handle`]. +#[derive(Debug)] +pub struct Job { + root: Handle, + realm: Option, +} + +impl Job { + /// The realm the job runs in, if the operation that made it names one. + pub fn realm(&self) -> Option<&Realm> { + self.realm.as_ref() + } + + /// The root that keeps the job's closure alive. + pub fn as_handle(&self) -> &Handle { + &self.root + } +} + +/// A callback and the host's data about it: ECMA-262's JobCallback Record. +/// +/// [`Host::make_job_callback`] makes one when a promise reaction or a +/// `FinalizationRegistry` is created, and [`Host::call_job_callback`] calls it +/// later. HTML uses the data to remember the "incumbent settings object". +#[derive(Debug)] +pub struct JobCallback { + callback: Handle, + host_defined: Option>, +} + +impl JobCallback { + /// A job callback with no host data. + pub fn new(callback: Handle) -> JobCallback { + JobCallback { + callback, + host_defined: None, + } + } + + /// Attaches the host's data. + #[must_use] + pub fn with_host_defined(mut self, data: Box) -> JobCallback { + self.host_defined = Some(data); + self + } + + /// The callback function. + pub fn callback(&self) -> &Handle { + &self.callback + } + + /// The host's data, if any. + pub fn host_defined(&self) -> Option<&(dyn Any + Send)> { + self.host_defined.as_deref() + } +} + +/// What [`Host::promise_rejection_tracker`] is told happened. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RejectionOperation { + /// A promise with no handler was rejected. + Reject, + /// A handler was added to an already-rejected promise that had none. + Handle, +} + +/// Which dynamic code-from-strings feature is asking +/// [`Host::ensure_can_compile_strings`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum DynamicCodeKind { + /// `eval(...)`; `direct` is true for a direct call that sees local scope. + Eval { + /// Whether this was a direct `eval`. + direct: bool, + }, + /// `Function(...)`. + Function, + /// `GeneratorFunction(...)`. + GeneratorFunction, + /// `AsyncFunction(...)`. + AsyncFunction, + /// `AsyncGeneratorFunction(...)`. + AsyncGeneratorFunction, +} + +/// The strings handed to [`Host::ensure_can_compile_strings`]. They are +/// UTF-16 because the program can build them with lone surrogates. +#[derive(Clone, Copy, Debug)] +pub struct CompileStrings<'a> { + /// Which feature is compiling code. + pub kind: DynamicCodeKind, + /// The parameter strings (empty for `eval`). + pub parameters: &'a [Vec], + /// The body, or for `eval` the code. + pub body: &'a [u16], +} + +/// The answer of [`Host::resize_array_buffer`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Resize { + /// The host did the resize. + Handled, + /// The host left it to the engine's default, which allocates. + Unhandled, +} + +/// What the embedder supplies. Every method has a default so a program that +/// needs none of the hooks can use [`DefaultHost`], and an embedder +/// overrides what it needs. +/// +/// - **Reentrancy.** A hook gets `&mut self` and a [`Scope`] and may call +/// back into the engine through that scope, including evaluating code. +/// While a hook runs, the host is lent to it, so [`Scope::host_mut`] from +/// code the hook runs gives [`Error::Busy`](crate::Error::Busy). +/// - **Errors.** A hook that returns `Err` makes the operation that called it +/// fail with that error, as ECMA-262 says for an abrupt completion from a +/// host-defined operation. `Err(Error::Terminated)` is passed on as is. +/// - **Threads.** `Host: Send`, which keeps [`Runtime`](crate::Runtime) +/// `Send`. A host that needs a value that is not `Send` keeps an id or a +/// channel in the host and the value elsewhere. +/// - **The event loop is the host's.** Nothing here runs a job: the hooks +/// queue, and the embedder calls [`Runtime::run_jobs`](crate::Runtime::run_jobs) +/// when its loop says to. +/// +/// The defaults named in each method are the engine's behaviour for a host +/// that says nothing. The browser's answers (HTML's, and 7.0(b)'s fixed +/// time zone offset and `en-US`) are the browser's, in `clazel-web`. +/// +/// None of these hooks is called yet: the VM has no promises, modules or +/// `Date`, which are what call them. The example compiles and runs its type +/// checks: +/// +/// ``` +/// use clazel_js::{ +/// Host, Job, Local, Module, ModuleRequest, LoadPayload, Referrer, RejectionOperation, +/// Result, Scope, +/// }; +/// +/// /// A host with a task queue of its own, rejection reporting, and a fixed clock. +/// struct PageHost { +/// tasks: Vec, +/// unhandled: Vec, +/// } +/// +/// impl Host for PageHost { +/// fn enqueue_promise_job(&mut self, _scope: &mut Scope<'_>, job: Job) -> Result<()> { +/// self.tasks.push(job); +/// Ok(()) +/// } +/// +/// fn promise_rejection_tracker( +/// &mut self, +/// _scope: &mut Scope<'_>, +/// _promise: Local<'_>, +/// operation: RejectionOperation, +/// ) { +/// self.unhandled.push(format!("{operation:?}")); +/// } +/// +/// fn now_ms(&mut self) -> f64 { +/// 1_700_000_000_000.0 +/// } +/// +/// fn random_u64(&mut self) -> u64 { +/// 4 // chosen by fair dice roll +/// } +/// } +/// ``` +pub trait Host: Any + Send { + // ---- Jobs ----------------------------------------------------------- + + /// `HostEnqueuePromiseJob(job, realm)`: queue a promise reaction. + /// + /// Default: append it to the engine's own queue, which + /// [`Runtime::run_jobs`](crate::Runtime::run_jobs) drains in order. A host + /// that overrides this takes over the order and calls + /// [`Scope::run_job`] itself. + fn enqueue_promise_job(&mut self, scope: &mut Scope<'_>, job: Job) -> Result<()> { + scope.enqueue_job_internal(job) + } + + /// `HostEnqueueGenericJob(job, realm)`. Default: as for promise jobs. + fn enqueue_generic_job(&mut self, scope: &mut Scope<'_>, job: Job) -> Result<()> { + scope.enqueue_job_internal(job) + } + + /// `HostEnqueueTimeoutJob(job, realm, milliseconds)`: run `job` after at + /// least `delay_ms`. Only a feature with timers in the language uses it + /// (`Atomics.waitAsync`, which the browser does not ship), so no + /// embedder is required to override it. Default: the engine's own queue, + /// with the delay ignored. + fn enqueue_timeout_job( + &mut self, + scope: &mut Scope<'_>, + job: Job, + delay_ms: f64, + ) -> Result<()> { + let _ = delay_ms; + scope.enqueue_job_internal(job) + } + + /// `HostEnqueueFinalizationRegistryCleanupJob(finalizationRegistry)`: a + /// registry has cells to clean up. The phase plan's list of hooks does not + /// name this one and 7.6 needs it (finalization callbacks are queued as + /// tasks); it is listed under the README's open questions. Default: the + /// engine's own queue. + fn enqueue_finalization_registry_cleanup_job( + &mut self, + scope: &mut Scope<'_>, + job: Job, + ) -> Result<()> { + scope.enqueue_job_internal(job) + } + + /// `HostMakeJobCallback(callback)`. Default: a [`JobCallback`] holding the + /// function and no host data. + fn make_job_callback( + &mut self, + scope: &mut Scope<'_>, + callback: Local<'_>, + ) -> Result { + Ok(JobCallback::new(scope.persist(callback))) + } + + /// `HostCallJobCallback(jobCallback, V, argumentsList)`. Default: call the + /// function with `this` and the arguments. + fn call_job_callback<'s>( + &mut self, + scope: &mut Scope<'s>, + callback: &JobCallback, + this: Local<'_>, + arguments: &[Local<'_>], + ) -> Result> { + let function = scope.local(callback.callback())?; + scope.call(function, this, arguments) + } + + // ---- Modules -------------------------------------------------------- + + /// `HostLoadImportedModule(referrer, moduleRequest, hostDefined, payload)`: + /// find (and fetch, if need be) the module `request` names, then call + /// [`Scope::finish_loading_imported_module`] with it or with the error. + /// It may do so before returning or on a later turn. + /// + /// ECMA-262 requires the same `(referrer, request)` to give the same module + /// every time or fail every time; keeping that is the host's job. + /// + /// Default: finish with a `TypeError` saying this host loads no modules. + fn load_imported_module( + &mut self, + scope: &mut Scope<'_>, + referrer: Referrer<'_>, + request: &ModuleRequest, + payload: LoadPayload, + ) -> Result<()> { + let _ = (referrer, request); + let error = scope.new_exception(ErrorKind::TypeError, "this host does not load modules")?; + scope.finish_loading_imported_module(payload, Err(error)) + } + + /// `HostGetSupportedImportAttributes()`: the attribute keys this host + /// understands (for example `"type"`). The engine refuses an import with + /// any other key with a `SyntaxError`. Default: none. Whether the pinned + /// edition has this hook is checked in 7.4. + fn supported_import_attributes(&self) -> &[&'static str] { + &[] + } + + /// `HostGetImportMetaProperties(moduleRecord)`: the properties of + /// `import.meta` for `module`, as (key, value) pairs. Default: none. + fn get_import_meta_properties<'s>( + &mut self, + scope: &mut Scope<'s>, + module: &Module, + ) -> Result)>> { + let _ = (scope, module); + Ok(Vec::new()) + } + + /// `HostFinalizeImportMeta(importMeta, moduleRecord)`: last chance to change + /// the `import.meta` object. Default: nothing. + fn finalize_import_meta( + &mut self, + scope: &mut Scope<'_>, + import_meta: Local<'_>, + module: &Module, + ) -> Result<()> { + let _ = (scope, import_meta, module); + Ok(()) + } + + // ---- Promises, code from strings, and the like ------------------------ + + /// `HostPromiseRejectionTracker(promise, operation)`: a promise was + /// rejected with no handler, or got its first handler after that. It + /// cannot fail or throw; the host records what it needs. Default: nothing. + fn promise_rejection_tracker( + &mut self, + scope: &mut Scope<'_>, + promise: Local<'_>, + operation: RejectionOperation, + ) { + let _ = (scope, promise, operation); + } + + /// `HostEnsureCanCompileStrings(calleeRealm, ...)`: may `eval` and the + /// `Function` constructors compile this text in `callee`? `Err` refuses, + /// and the error is what the call throws. Default: yes. + fn ensure_can_compile_strings( + &mut self, + scope: &mut Scope<'_>, + callee: &Realm, + strings: &CompileStrings<'_>, + ) -> Result<()> { + let _ = (scope, callee, strings); + Ok(()) + } + + /// `HostHasSourceTextAvailable(func)`: may `Function.prototype.toString` + /// show the source of `function`? Default: yes. + fn has_source_text_available(&mut self, scope: &mut Scope<'_>, function: Local<'_>) -> bool { + let _ = (scope, function); + true + } + + /// `HostEnsureCanAddPrivateElement(O)`: may a private element be added to + /// `object`? `Err` refuses. Default: yes. + fn ensure_can_add_private_element( + &mut self, + scope: &mut Scope<'_>, + object: Local<'_>, + ) -> Result<()> { + let _ = (scope, object); + Ok(()) + } + + /// `HostResizeArrayBuffer(buffer, newByteLength)`. Default: + /// [`Resize::Unhandled`]. + fn resize_array_buffer( + &mut self, + scope: &mut Scope<'_>, + buffer: Local<'_>, + new_byte_length: u64, + ) -> Result { + let _ = (scope, buffer, new_byte_length); + Ok(Resize::Unhandled) + } + + // ---- What the engine needs from the world ----------------------------- + + /// The current time in milliseconds since the Unix epoch, for + /// `Date.now()` and `new Date()`. Default: the system clock. + fn now_ms(&mut self) -> f64 { + match SystemTime::now().duration_since(UNIX_EPOCH) { + Ok(d) => d.as_secs_f64() * 1000.0, + Err(_) => 0.0, + } + } + + /// The local time zone's offset from UTC in milliseconds at `time_ms` + /// (ECMA-262's `LocalTZA`). `is_utc` says whether `time_ms` is a UTC time + /// or a local one to convert back. Default: 0, that is, UTC. + fn local_tza(&mut self, time_ms: f64, is_utc: bool) -> f64 { + let _ = (time_ms, is_utc); + 0.0 + } + + /// Random bits for `Math.random`. Required, because the engine has no + /// source of its own (rule 8: no randomness dependency). It need not be + /// cryptographic. [`DefaultHost`] draws from the standard library's + /// per-process random hash keys. + fn random_u64(&mut self) -> u64; + + /// The default locale, as a BCP 47 tag, for `toLocaleString` and friends. + /// Default: `"en-US"`. + fn default_locale(&self) -> &str { + "en-US" + } + + // ---- The collector ------------------------------------------------------ + + /// Reports the JavaScript values the host keeps alive: for the browser, + /// the wrapper of every node in a document's tree, the task queues and the + /// timers. Called during every collection. See [`Tracer`]. Default: + /// nothing. + fn trace_roots(&mut self, tracer: &mut Tracer<'_>) { + let _ = tracer; + } +} + +/// A host that says nothing: every hook has its default, and the clock and +/// randomness come from the standard library. +/// +/// Test262's runner, small tools and the examples use it. A real embedder +/// supplies its own. +/// +/// ``` +/// use clazel_js::{Config, DefaultHost, Limits, Runtime}; +/// +/// fn main() -> Result<(), clazel_js::Error> { +/// let _rt = Runtime::new(Config::new(Limits::unlimited()), DefaultHost::new())?; +/// Ok(()) +/// } +/// ``` +#[derive(Debug, Default)] +pub struct DefaultHost { + draws: u64, +} + +impl DefaultHost { + /// A default host. + pub fn new() -> DefaultHost { + DefaultHost { draws: 0 } + } +} + +impl Host for DefaultHost { + fn random_u64(&mut self) -> u64 { + // A fresh `RandomState` has per-process random keys; hashing a counter + // gives different bits each call. Good enough for `Math.random`, which + // ECMA-262 does not require to be unpredictable. + self.draws = self.draws.wrapping_add(1); + RandomState::new().hash_one(self.draws) + } +} + +// The engine's own queue is reached through this one call, which the default +// hooks use and an embedder that forwards to it may too. +impl Scope<'_> { + /// Appends `job` to the engine's own job queue, the one + /// [`Runtime::run_jobs`](crate::Runtime::run_jobs) drains. What the default + /// `enqueue_*` hooks do, and what an overriding host may call to forward. + pub fn enqueue_job_internal(&mut self, job: Job) -> Result<()> { + self.push_job(job) + } + + /// Runs a job to completion. A job that throws returns the exception. + pub fn run_job(&mut self, job: Job) -> Result<()> { + // A job's root is the function to run: no arguments, `undefined` for + // `this`. Nothing in the engine makes jobs yet (no promises), so this + // serves hosts that queue their own through `Scope::function`. + let function = self.local(job.as_handle())?; + let this = self.undefined(); + self.call(function, this, &[])?; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[cfg_attr(miri, ignore = "reads the system clock and the OS random source")] + fn default_host_answers_for_the_world() { + let mut host = DefaultHost::new(); + assert_eq!(host.local_tza(0.0, true), 0.0); + assert_eq!(host.default_locale(), "en-US"); + assert!(host.now_ms() > 1.5e12); + assert_ne!(host.random_u64(), host.random_u64()); + assert!(host.supported_import_attributes().is_empty()); + } +} diff --git a/crates/clazel-js/src/lib.rs b/crates/clazel-js/src/lib.rs new file mode 100644 index 000000000..bbf749d39 --- /dev/null +++ b/crates/clazel-js/src/lib.rs @@ -0,0 +1,51 @@ +//! The JavaScript engine's embedding API (phase-7.md item 7.8, "The engine as +//! a library"). +//! +//! **This is the contract, and its first implementation** (phase-7.md item +//! 7.8, first part), over `clazel-js-vm`'s interpreter and +//! `clazel-js-builtins`. Every type and signature is the reviewed contract; +//! what the VM does not have yet (arrays, promises, symbols, BigInt, +//! ArrayBuffer, modules, a second realm, a collector) returns +//! [`Error::NotImplemented`], and the README's "What is implemented" section +//! lists each item with its reason. The README also states the rules in prose +//! (threads, rooting, termination, what is left out) and lists the open +//! questions. The doc comments carry examples that run, except the few that +//! need what is not there yet, which stay `no_run` and say so. +//! +//! The pieces, in the order a program meets them: +//! +//! - [`Runtime`], [`Config`], [`Limits`] and [`Terminator`]: one agent, its +//! limits, and the handle that stops it from another thread. +//! - [`Scope`]: every operation, inside [`Runtime::scope`]. Its values are +//! [`Local`]s; [`Handle`], [`Weak`] and [`Member`] hold them longer. +//! - [`Realm`], [`Source`], [`Script`] and [`Module`]: code to run. +//! - [`Scope::function`], [`Args`] and [`HostObject`]: Rust inside JavaScript. +//! - [`Host`]: what ECMA-262 leaves to the embedder, and [`DefaultHost`]. +//! - [`Error`], [`Exception`] and [`ErrorReport`]: what goes wrong. +//! +//! An embedder names this crate and no other engine crate. + +mod code; +mod error; +mod host; +mod native; +mod runtime; +mod scope; +mod state; +mod value; + +pub use code::{ + ImportAttribute, LoadPayload, Module, ModuleRequest, ModuleStatus, Realm, RealmOptions, + Referrer, Script, Source, +}; +pub use error::{Error, ErrorKind, ErrorReport, Exception, Result, StackFrame}; +pub use host::{ + CompileStrings, DefaultHost, DynamicCodeKind, Host, Job, JobCallback, RejectionOperation, + Resize, +}; +pub use native::{Args, Callable, Exotic, HostObject, NativeFn, Tracer}; +pub use runtime::{Config, JitMode, Limits, Runtime, Terminator}; +pub use scope::{Resolvers, Scope}; +pub use value::{ + Handle, Key, Local, Member, PromiseState, PropertyDescriptor, ValueKind, Weak, WellKnownSymbol, +}; diff --git a/crates/clazel-js/src/native.rs b/crates/clazel-js/src/native.rs new file mode 100644 index 000000000..ca87573e0 --- /dev/null +++ b/crates/clazel-js/src/native.rs @@ -0,0 +1,267 @@ +//! Native functions and host objects: how Rust code becomes part of a +//! JavaScript program. + +use core::any::Any; +use core::marker::PhantomData; + +use clazel_js_heap::value::Value; + +use crate::error::Result; +use crate::scope::Scope; +use crate::value::{Key, Local, Member, PropertyDescriptor}; + +/// What a native function is called with. +/// +/// `this` and the arguments are [`Local`]s of the call's own scope, so the +/// function can use them freely and cannot keep them. A missing argument is +/// `undefined`, as for any JavaScript function. +/// +/// ``` +/// use clazel_js::{Args, Callable, Key, Local, Result, Scope}; +/// +/// /// `add(a, b)` as JavaScript sees it. +/// fn add<'s>(scope: &mut Scope<'s>, args: &Args<'s>) -> Result> { +/// let a = scope.to_number(args.get(0))?; +/// let b = scope.to_number(args.get(1))?; +/// Ok(scope.number(a + b)) +/// } +/// # let mut rt = clazel_js::Runtime::new(clazel_js::Config::new(clazel_js::Limits::unlimited()), clazel_js::DefaultHost::new())?; +/// # let sum = rt.scope(|scope| { +/// # let function = scope.function("add", 2, Callable::Function, add)?; +/// # let this = scope.undefined(); +/// # let (a, b) = (scope.number(40.0), scope.number(2.0)); +/// # let sum = scope.call(function, this, &[a, b])?; +/// # // A missing argument is `undefined`, which is NaN as a number. +/// # let missing = scope.call(function, this, &[a])?; +/// # assert!(scope.as_number(missing).is_some_and(f64::is_nan)); +/// # let _ = Key::from("unused"); +/// # Ok(scope.as_number(sum)) +/// # })?; +/// # assert_eq!(sum, Some(42.0)); +/// # Ok::<(), clazel_js::Error>(()) +/// ``` +#[derive(Clone, Copy, Debug)] +pub struct Args<'s> { + runtime: usize, + this: Value, + args: &'s [Value], + new_target: Value, +} + +impl<'s> Args<'s> { + pub(crate) fn new(runtime: usize, this: Value, args: &'s [Value], new_target: Value) -> Self { + Args { + runtime, + this, + args, + new_target, + } + } + + /// The `this` value. + pub fn this(&self) -> Local<'s> { + Local::new(self.this, self.runtime) + } + + /// How many arguments the caller passed. + pub fn len(&self) -> usize { + self.args.len() + } + + /// Whether the caller passed no arguments. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Argument `index`, or `undefined` if the caller passed fewer. + pub fn get(&self, index: usize) -> Local<'s> { + let value = self.args.get(index).copied().unwrap_or(Value::UNDEFINED); + Local::new(value, self.runtime) + } + + /// `new.target`: `Some` when the function was called with `new` (or by + /// `Reflect.construct`), and the constructor `new` was applied to, which + /// may be a subclass. `None` for an ordinary call. + pub fn new_target(&self) -> Option> { + (self.new_target != Value::UNDEFINED).then(|| Local::new(self.new_target, self.runtime)) + } +} + +/// Whether a native function can be used with `new`. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Callable { + /// An ordinary function: `new` throws a `TypeError`, and + /// [`Args::new_target`] is always `None`. What WebIDL calls an operation. + #[default] + Function, + /// A constructor: `new` calls it with [`Args::new_target`] set. A call + /// without `new` is the function's own business; it can throw. What + /// WebIDL calls an interface object. + Constructor, +} + +/// A request to run a native function body on behalf of the engine. Boxed +/// closures of this shape are what [`Scope::function`] takes. +/// +/// `Fn`, not `FnMut`: a native function can be re-entered through JavaScript +/// it calls, so mutable state it needs goes in a host object, in the +/// [`Host`](crate::Host), or behind a lock, and not in the closure. `Send` +/// because the [`Runtime`](crate::Runtime) is. +pub type NativeFn = dyn for<'s> Fn(&mut Scope<'s>, &Args<'s>) -> Result> + Send; + +/// An answer from an exotic hook: do what an ordinary object does, or here is +/// the answer. +#[derive(Clone, Copy, Debug)] +pub enum Exotic { + /// No special behaviour for this key: the engine continues as for an + /// ordinary object (looking at the object's own properties, then its + /// prototype). + Ordinary, + /// The embedder's answer. + Handled(T), +} + +/// The data of a host object, and the object's special behaviour. +/// +/// A host object is a JavaScript object that carries a Rust value, which the +/// embedder owns. This is how `clazel-webidl` builds a wrapper for a DOM +/// node: the wrapper is a JavaScript object, and its data says which node. +/// +/// - **Typed.** The data's type is the slot's type. [`Scope::host_data`] and +/// [`Scope::host_data_mut`] check it and give +/// [`Error::WrongType`](crate::Error::WrongType) for any other object, so a +/// method called on the wrong `this` is a defined error and never a +/// reinterpreted pointer. +/// - **Lifetime.** The data is dropped, on the runtime's thread, when the +/// collector frees the object or the runtime is dropped. +/// - **Tracing.** If the data refers to other JavaScript values it reports +/// them from [`trace`](HostObject::trace), using [`Member`]. The default +/// reports nothing. +/// - **Exotic behaviour.** The four hooks let a host object be what WebIDL +/// calls a legacy platform object: `NodeList[0]`, `HTMLCollection.byName`, +/// `localStorage.key`. Each defaults to [`Exotic::Ordinary`], and the +/// engine checks the result against ECMA-262's invariants for the internal +/// method (a non-configurable property cannot be reported missing, and so +/// on), throwing `TypeError` if the hook breaks them. While a hook runs the +/// object's data is lent to it and taken out of the object, so another +/// access to the same data gets [`Error::Busy`](crate::Error::Busy). +/// - **Which hooks exist** is an open question in the README: these four are +/// what WebIDL §3.9 and §3.10 appear to need and the list may grow. +/// +/// The hooks are not called yet (the VM has no exotic dispatch), so this +/// example only compiles and runs its type checks: +/// +/// ``` +/// use clazel_js::{Exotic, HostObject, Key, Local, PropertyDescriptor, Result, Runtime, Scope}; +/// +/// /// The data of a `NodeList` wrapper: the ids of the nodes it holds. +/// struct NodeList { +/// nodes: Vec, +/// } +/// +/// impl HostObject for NodeList { +/// fn get_own_property<'s>( +/// &mut self, +/// scope: &mut Scope<'s>, +/// _this: Local<'s>, +/// key: Key<'_, 's>, +/// ) -> Result>>> { +/// let Key::Index(i) = key else { return Ok(Exotic::Ordinary) }; +/// let Some(id) = self.nodes.get(i as usize) else { +/// return Ok(Exotic::Handled(None)); +/// }; +/// let wrapper = scope.number(*id as f64); // a real binding makes the node's wrapper +/// Ok(Exotic::Handled(Some(PropertyDescriptor { +/// value: Some(wrapper), +/// writable: Some(false), +/// enumerable: Some(true), +/// configurable: Some(true), +/// ..PropertyDescriptor::default() +/// }))) +/// } +/// } +/// +/// fn make(rt: &mut Runtime) -> clazel_js::Result<()> { +/// rt.scope(|scope| { +/// let list = scope.new_host_object(None, NodeList { nodes: vec![1, 2] })?; +/// let n = scope.host_data::(list)?.nodes.len(); +/// assert_eq!(n, 2); +/// Ok(()) +/// }) +/// } +/// ``` +pub trait HostObject: Any + Send { + /// Reports every [`Member`] this data holds. See [`Tracer`]. + fn trace(&self, tracer: &mut Tracer<'_>) { + let _ = tracer; + } + + /// `[[GetOwnProperty]]` for `key`. + fn get_own_property<'s>( + &mut self, + scope: &mut Scope<'s>, + this: Local<'s>, + key: Key<'_, 's>, + ) -> Result>>> { + let _ = (scope, this, key); + Ok(Exotic::Ordinary) + } + + /// `[[DefineOwnProperty]]` for `key`. `Handled(true)` means it succeeded + /// and `Handled(false)` that it was refused. + fn define_own_property<'s>( + &mut self, + scope: &mut Scope<'s>, + this: Local<'s>, + key: Key<'_, 's>, + descriptor: PropertyDescriptor<'s>, + ) -> Result> { + let _ = (scope, this, key, descriptor); + Ok(Exotic::Ordinary) + } + + /// `[[Delete]]` for `key`. + fn delete_property<'s>( + &mut self, + scope: &mut Scope<'s>, + this: Local<'s>, + key: Key<'_, 's>, + ) -> Result> { + let _ = (scope, this, key); + Ok(Exotic::Ordinary) + } + + /// The extra own keys `[[OwnPropertyKeys]]` should list, which the + /// engine puts in front of the object's ordinary keys in the order + /// ECMA-262 fixes (indices first). `Handled` keys that are not strings, + /// symbols or indices are a `TypeError`. + fn own_property_keys<'s>( + &mut self, + scope: &mut Scope<'s>, + this: Local<'s>, + ) -> Result>>> { + let _ = (scope, this); + Ok(Exotic::Ordinary) + } +} + +/// The collector's marker, handed to [`HostObject::trace`] and +/// [`Host::trace_roots`](crate::Host::trace_roots). +/// +/// During a collection the engine calls each live host object's `trace`, and +/// the host's `trace_roots` once. Everything reported is treated as reachable +/// from there. Collection runs only at points the engine chooses (phase-7.md +/// item 7.6), and tracing cannot run JavaScript or allocate: the type gives it +/// no [`Scope`]. +#[derive(Debug)] +pub struct Tracer<'a> { + _collector: PhantomData<&'a mut ()>, +} + +impl Tracer<'_> { + /// Reports that the value is reachable. Reporting one twice is fine, and + /// reporting a stale one is ignored (the generation check). + pub fn mark(&mut self, member: Member) { + let _ = member; + } +} diff --git a/crates/clazel-js/src/runtime.rs b/crates/clazel-js/src/runtime.rs new file mode 100644 index 000000000..4cec60650 --- /dev/null +++ b/crates/clazel-js/src/runtime.rs @@ -0,0 +1,422 @@ +//! [`Runtime`], its [`Config`] and [`Limits`], and the [`Terminator`]. + +use core::cell::Cell; +use core::marker::PhantomData; + +use clazel_js_vm::interp::{Abrupt, Config as VmConfig, Terminator as VmTerminator, Vm}; + +use crate::error::{Error, Result}; +use crate::host::{DefaultHost, Host}; +use crate::scope::{CELL_BYTES, Scope}; +use crate::state::State; + +/// Every limit is the embedder's to set, and the engine has no default for +/// any of them (phase-7.md, "Limits and stopping"). +/// +/// A struct literal must name every field, so adding a limit later makes +/// every embedder choose a value. The numbers the browser picks are not in +/// this crate. Each limit has its own report, listed with the field. +/// +/// ``` +/// use clazel_js::Limits; +/// +/// // A test harness with limits generous enough never to matter, and one +/// // that is not: a small heap that is meant to be hit. +/// let relaxed = Limits::unlimited(); +/// let strict = Limits { +/// heap_bytes: 64 << 20, +/// ..relaxed +/// }; +/// assert_eq!(strict.heap_bytes, 64 << 20); +/// assert_eq!(strict.native_depth, relaxed.native_depth); +/// ``` +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Limits { + /// Heap size in bytes. An allocation that would pass it first runs a full + /// collection, then throws a `RangeError` if it still would. + pub heap_bytes: u64, + /// Size of the value-stack region in bytes. Passing it throws a + /// `RangeError` ("too much recursion"). + pub value_stack_bytes: u64, + /// How many native functions may be on the call stack at once, counting + /// built-ins that call back into JavaScript such as `Array.prototype.map`. + /// Passing it throws a `RangeError`. + pub native_depth: u32, + /// Bytes of generated machine code the JIT may hold. Passing it stops + /// compiling and leaves functions to the interpreter; it is not an error. + /// Ignored when [`JitMode::Off`]. + pub code_bytes: u64, + /// Entries in the RegExp matcher's backtrack stack. Passing it throws a + /// `RangeError` from the match, rather than hanging on `(a+)+b`. + pub regexp_backtrack_entries: u64, + /// Nesting depth of the parser (source text) and of `JSON.parse`. Passing + /// it throws a `SyntaxError` for source and a `RangeError` for JSON. + pub nesting_depth: u32, +} + +impl Limits { + /// Limits that no ordinary program reaches, for tests and for a runner + /// that wants the engine's behaviour without its limits getting in the + /// way. This is a named choice, not a default: nothing in the engine + /// calls it. + pub const fn unlimited() -> Limits { + Limits { + heap_bytes: u64::MAX, + value_stack_bytes: u64::MAX, + native_depth: u32::MAX, + code_bytes: u64::MAX, + regexp_backtrack_entries: u64::MAX, + nesting_depth: u32::MAX, + } + } +} + +/// Whether the JIT is on. Only [`Off`](JitMode::Off) exists until jit.md's +/// `clazel-jit` does; then a variant carrying a `CodeMapper` is added +/// (jit.md J.0(b)): the embedder's choice of how validated code becomes +/// executable. The interpreter is complete without it. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[non_exhaustive] +pub enum JitMode { + /// Interpret only. + #[default] + Off, +} + +/// What a [`Runtime`] is built from. +/// +/// ``` +/// use clazel_js::{Config, JitMode, Limits}; +/// +/// let config = Config::new(Limits::unlimited()); +/// assert_eq!(config.jit, JitMode::Off); +/// ``` +#[derive(Clone, Copy, Debug)] +pub struct Config { + /// The limits. See [`Limits`]. + pub limits: Limits, + /// Whether to use the JIT. + pub jit: JitMode, +} + +impl Config { + /// A configuration with the given limits and the JIT off. + pub const fn new(limits: Limits) -> Config { + Config { + limits, + jit: JitMode::Off, + } + } +} + +/// A handle that stops a [`Runtime`] from another thread. +/// +/// - **How it works.** `terminate` sets a flag. The interpreter checks it at +/// loop back-edges and at calls, and JIT code checks it at the same points. +/// An evaluation that sees it ends with +/// [`Error::Terminated`](crate::Error::Terminated). +/// - **JavaScript cannot catch it.** `catch` does not run, and neither does +/// `finally`: running either would run JavaScript after being told to stop. +/// Native frames see `Err(Error::Terminated)` and must pass it on. One that +/// does not still stops at the next check, which raises it again. +/// - **It is sticky.** The flag stays set, so every later call into the same +/// `Runtime` also ends in `Terminated`, until [`Terminator::clear`] (an open +/// question in the README is whether a terminated runtime stays usable). +/// - **Cost.** The check is one relaxed atomic load. It is not a way to +/// interrupt a native function that is itself in a long loop; built-ins +/// check it too, and an embedder's native function should +/// [`Scope::check_terminated`] in a long loop. +/// - **It is the embedder's policy.** The browser still ends a stuck renderer +/// from outside, on the user's word (phase-7.md 7.0(g)), and may use this +/// later to keep the tab. +/// +/// A `Terminator` is `Clone`, `Send` and `Sync`. +/// +/// ``` +/// use std::thread; +/// use std::time::Duration; +/// use clazel_js::{Config, DefaultHost, Limits, Realm, RealmOptions, Runtime}; +/// +/// fn run_until_stopped(rt: &mut Runtime, realm: &Realm) -> bool { +/// let terminator = rt.terminator(); +/// let watchdog = thread::spawn(move || { +/// thread::sleep(Duration::from_millis(20)); +/// terminator.terminate(); +/// }); +/// let result = rt.scope(|scope| { +/// scope.eval_script(realm, clazel_js::Source::new("loop.js", "for (;;) {}"))?; +/// Ok::<(), clazel_js::Error>(()) +/// }); +/// let _ = watchdog.join(); +/// rt.terminator().clear(); +/// matches!(result, Err(clazel_js::Error::Terminated)) +/// } +/// # let mut rt = Runtime::new(Config::new(Limits::unlimited()), DefaultHost::new())?; +/// # let realm = rt.scope(|scope| scope.create_realm(RealmOptions::default()))?; +/// # assert!(run_until_stopped(&mut rt, &realm)); +/// # Ok::<(), clazel_js::Error>(()) +/// ``` +#[derive(Clone, Debug, Default)] +pub struct Terminator { + /// The interpreter's own flag, which its loop reads at back-edges and + /// calls. + flag: VmTerminator, +} + +impl Terminator { + /// Asks the runtime to stop. Safe to call from any thread, any number of + /// times. + pub fn terminate(&self) { + self.flag.terminate(); + } + + /// Whether `terminate` has been called and not cleared. + pub fn is_terminated(&self) -> bool { + self.flag.is_set() + } + + /// Lets the runtime run again. Call it after an evaluation has returned + /// `Terminated`, never while one is running. + pub fn clear(&self) { + self.flag.reset(); + } +} + +/// One ECMAScript agent: a heap, a job queue, limits and a [`Host`]. +/// +/// - **No global state.** Everything the engine knows lives in the `Runtime`, +/// with no `static` and no thread-local, so a process can hold many. +/// - **Threads.** A `Runtime` is `Send` and not `Sync`. It can move to another +/// thread between calls, and only one thread can use it at a time. The +/// browser runs one per renderer, on one thread, as phase-7.md 7.0(e) +/// decided. [`Terminator`] is the one piece usable from other threads. +/// `Host: Send` is what keeps this true, because the host lives inside. +/// - **Calls go through a scope.** [`Runtime::scope`] runs a closure with a +/// [`Scope`], and everything else is a method of that. +/// - **Dropping** frees the whole heap, runs each native closure's and host +/// object's destructor, and does not run any JavaScript (no finalizer). +/// +/// ```compile_fail +/// use clazel_js::Runtime; +/// +/// fn needs_sync() {} +/// +/// // Does not compile: a Runtime is not Sync. +/// fn check() { needs_sync::(); } +/// ``` +/// +/// The compiler gives a script no completion value yet, so `eval_script` +/// returns `undefined` and this example reads its result from a global (see +/// the README): +/// +/// ``` +/// use clazel_js::{Config, DefaultHost, Key, Limits, RealmOptions, Runtime, Source}; +/// +/// fn main() -> Result<(), clazel_js::Error> { +/// let mut rt = Runtime::new(Config::new(Limits::unlimited()), DefaultHost::new())?; +/// let realm = rt.scope(|scope| scope.create_realm(RealmOptions::default()))?; +/// let sum = rt.scope(|scope| { +/// scope.eval_script(&realm, Source::new("sum.js", "var sum = 1 + 2;"))?; +/// let global = scope.global(&realm)?; +/// let value = scope.get(global, Key::from("sum"))?; +/// Ok(scope.as_number(value)) +/// })?; +/// assert_eq!(sum, Some(3.0)); +/// rt.run_jobs()?; +/// Ok(()) +/// } +/// ``` +/// +/// A `Runtime` moves to another thread as a whole: +/// +/// ``` +/// use clazel_js::Runtime; +/// +/// fn assert_send() {} +/// fn check() { assert_send::(); } +/// check(); +/// ``` +pub struct Runtime { + limits: Limits, + terminator: Terminator, + /// The VM, with the runtime's state (and so the host) in its embedder + /// slot, see [`crate::state`]. + vm: Box, + /// What [`Runtime::host_mut`] answers with if the state is ever missing, + /// which it cannot be: the method has no way to fail. + fallback_host: DefaultHost, + // `Cell` is `Send` and not `Sync`, which is the rule. + _not_sync: PhantomData>, +} + +impl Runtime { + /// Builds a runtime. The `host` is taken for the life of the runtime; + /// reach it again with [`Runtime::host_mut`] or, from inside a native + /// function, [`Scope::host_mut`]. + /// + /// The VM and the built-ins are made here, so the one realm the VM has + /// exists at once, and [`Scope::create_realm`] hands it out (the + /// contract's "no realms" is not what a VM with one global can do). The + /// limits that have a counterpart are applied: `heap_bytes` becomes the + /// arena's cell count (an estimate of 64 bytes a cell), `value_stack_bytes` + /// the frame region's slot count (8 bytes a slot), and `native_depth` is + /// checked on every call of a closure made by [`Scope::function`]. + /// `code_bytes` (no JIT), `regexp_backtrack_entries` (no RegExp) and + /// `nesting_depth` (the parser's own fixed bound applies) are accepted + /// and not used yet. The heap limit gives [`Error::OutOfMemory`] rather + /// than a `RangeError`, since the VM cannot allocate an error object + /// when it is full and there is no collector to run first. + pub fn new(config: Config, host: impl Host) -> Result { + let limits = config.limits; + // Limits the VM has a counterpart for: its arena counts cells (an + // estimate of `CELL_BYTES` each) and its frame region counts 8-byte + // slots. The VM clamps both to what it can address. + let heap_cells = usize::try_from(limits.heap_bytes / CELL_BYTES).unwrap_or(usize::MAX); + let region_slots = usize::try_from(limits.value_stack_bytes / 8).unwrap_or(usize::MAX); + let mut vm = Vm::new(VmConfig { + region_slots, + heap_cells, + }) + .map_err(vm_failure)?; + clazel_js_builtins::install(&mut vm).map_err(vm_failure)?; + let terminator = Terminator { + flag: vm.terminator(), + }; + vm.set_embedder(State::new(Box::new(host), limits)); + Ok(Runtime { + limits, + terminator, + vm: Box::new(vm), + fallback_host: DefaultHost::new(), + _not_sync: PhantomData, + }) + } + + /// Runs `f` with a [`Scope`]. Every [`Local`](crate::Local) made inside is + /// rooted until `f` returns, and cannot leave it; what `f` returns must + /// not borrow from the scope. This is the only way in. + /// + /// `f` cannot call `scope` again, because it has no access to the + /// `Runtime`. Code that wants an inner scope uses [`Scope::nested`]. + pub fn scope(&mut self, f: impl for<'s> FnOnce(&mut Scope<'s>) -> R) -> R { + let runtime = self.runtime_id(); + let mut scope = Scope::new(&mut self.vm, runtime); + f(&mut scope) + } + + /// Runs queued jobs (promise reactions, generic jobs, finalization + /// callbacks) until the queue is empty, and returns how many ran. This is + /// a microtask checkpoint; the engine never decides when to run one. + /// + /// The queue is the engine's own unless the [`Host`] diverted it: + /// [`Host::enqueue_promise_job`] and its siblings default to appending to + /// it. A job that throws stops the run: the exception comes back as + /// `Err`, the jobs after it stay queued, and calling `run_jobs` again + /// continues with them. A [`Terminator`] stops it as it stops anything. + /// + /// ``` + /// use clazel_js::Runtime; + /// + /// fn checkpoint(rt: &mut Runtime) -> clazel_js::Result<()> { + /// // After a script returns, with the host's own tasks still to run: + /// let ran = rt.run_jobs()?; + /// println!("{ran} jobs"); + /// Ok(()) + /// } + /// # let mut rt = clazel_js::Runtime::new(clazel_js::Config::new(clazel_js::Limits::unlimited()), clazel_js::DefaultHost::new())?; + /// # checkpoint(&mut rt)?; + /// # Ok::<(), clazel_js::Error>(()) + /// ``` + pub fn run_jobs(&mut self) -> Result { + let runtime = self.runtime_id(); + let mut scope = Scope::new(&mut self.vm, runtime); + scope.check_deferred()?; + let mut ran: u64 = 0; + while let Some(job) = scope.pop_job() { + scope.run_job(job)?; + ran = ran.saturating_add(1); + } + Ok(ran) + } + + /// A handle that stops this runtime from another thread. Every call gives + /// a handle to the same flag. + pub fn terminator(&self) -> Terminator { + self.terminator.clone() + } + + /// The limits this runtime was built with. + pub fn limits(&self) -> Limits { + self.limits + } + + /// The host, between calls. Not available while a hook is running; there + /// the hook's own `&mut self` is the host. + pub fn host_mut(&mut self) -> &mut dyn Host { + match self.vm.embedder_mut::() { + Some(state) => &mut *state.host, + None => &mut self.fallback_host, + } + } + + /// Bytes the heap holds now, live and not yet swept, for the embedder's + /// own accounting and for tests of [`Limits::heap_bytes`]. + pub fn heap_used(&self) -> Result { + // An estimate: live cells at `CELL_BYTES` each. Nothing is swept yet. + let cells = u64::try_from(self.vm.heap().live()).map_err(|_| Error::OutOfMemory)?; + cells.checked_mul(CELL_BYTES).ok_or(Error::OutOfMemory) + } +} + +impl Runtime { + /// The runtime's identity, as `Local`s carry it. + fn runtime_id(&mut self) -> usize { + self.vm.embedder::().map_or(0, |state| state.id) + } +} + +/// A failure of the VM while a runtime is being built, before there is a +/// runtime state to take errors from. +fn vm_failure(abrupt: Abrupt) -> Error { + match abrupt { + Abrupt::OutOfMemory => Error::OutOfMemory, + Abrupt::Terminated => Error::Terminated, + Abrupt::Throw(_) | Abrupt::NotYet(_) | Abrupt::Internal(_) => { + Error::NotImplemented("the VM could not start with these limits") + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn is_send() {} + fn is_send_sync() {} + + #[test] + fn terminator_is_a_shared_sticky_flag() { + is_send_sync::(); + let a = Terminator::default(); + let b = a.clone(); + assert!(!a.is_terminated()); + b.terminate(); + assert!(a.is_terminated()); + b.terminate(); + assert!(a.is_terminated()); + a.clear(); + assert!(!b.is_terminated()); + } + + #[test] + fn runtime_is_send() { + is_send::(); + } + + #[test] + fn unlimited_is_the_largest_of_each() { + let l = Limits::unlimited(); + assert_eq!(l.heap_bytes, u64::MAX); + assert_eq!(l.native_depth, u32::MAX); + } +} diff --git a/crates/clazel-js/src/scope.rs b/crates/clazel-js/src/scope.rs new file mode 100644 index 000000000..6bed37a66 --- /dev/null +++ b/crates/clazel-js/src/scope.rs @@ -0,0 +1,1436 @@ +//! [`Scope`]: every operation on values, realms, code and objects. + +use core::any::Any; +use core::marker::PhantomData; +use std::sync::Arc; + +use clazel_js_builtins::object::{Descriptor, define_property_or_throw}; +use clazel_js_builtins::util; +use clazel_js_heap::shape::Attrs; +use clazel_js_heap::string::{JsString, Key as HeapKey}; +use clazel_js_heap::value::{Decoded, Value}; +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; +use clazel_js_vm::compile::{CompileError, compile_script}; +use clazel_js_vm::heap::{HeapError, MAX_PROTO_DEPTH, ObjKind}; +use clazel_js_vm::interp::{Abrupt, Intrinsic, Vm}; + +use crate::code::{ + LoadPayload, Module, ModuleStatus, Realm, RealmOptions, Referrer, Script, Source, +}; +use crate::error::{Error, ErrorKind, ErrorReport, Exception, Result, not_implemented}; +use crate::host::Host; +use crate::native::{Args, Callable, HostObject}; +use crate::state::{State, abrupt_to_error, trampoline}; +use crate::value::{ + Handle, Key, Local, Member, PromiseState, PropertyDescriptor, ValueKind, Weak, WellKnownSymbol, +}; + +/// The resolving functions of a promise made by [`Scope::new_promise`]. +/// +/// Held by the embedder until the work the promise stands for is done: a +/// fetch finishes, a timer fires. It is two [`Handle`]s, `Send` and `Sync`, +/// and settling uses [`Scope::resolve_promise`] and [`Scope::reject_promise`] +/// on the runtime's thread. Settling twice is a no-op, as for JavaScript's own +/// resolving functions. +#[derive(Clone, Debug)] +pub struct Resolvers { + resolve: Handle, + reject: Handle, +} + +impl Resolvers { + /// The `resolve` function. + pub fn resolve_function(&self) -> &Handle { + &self.resolve + } + + /// The `reject` function. + pub fn reject_function(&self) -> &Handle { + &self.reject + } +} + +/// Where all work on a [`Runtime`](crate::Runtime) happens, and the owner of +/// the lifetime `'s` that every [`Local`] it makes carries. +/// +/// A `Scope` exists inside [`Runtime::scope`](crate::Runtime::scope), inside +/// each native function and hook (which get one for their call), and inside +/// [`Scope::nested`]. All the engine's operations are methods of it, grouped +/// below. +/// +/// - **Rooting.** Each `Local` is rooted until its scope ends. A loop that +/// makes many temporary values uses `nested` per iteration so they are not +/// all held until the end. +/// - **Collection.** The collector runs only inside an operation that +/// allocates or that the embedder asks for ([`Scope::gc`]), never between +/// two lines of the embedder's own code, so no `Local` is ever seen half +/// moved. The heap does not move objects (phase-7.md 7.0(f)). +/// - **Errors.** Operations that can run JavaScript, and so throw, return +/// `Result`. Operations that cannot (making a number, reading a kind) do not. +/// +/// There are no Arrays in the VM yet, so the list here is an ordinary object: +/// +/// ``` +/// use clazel_js::{Key, Runtime}; +/// +/// fn sum_squares(rt: &mut Runtime, n: u32) -> clazel_js::Result { +/// rt.scope(|scope| { +/// let list = scope.new_object()?; +/// for i in 0..n { +/// // Temporaries die at the end of each pass, not at the end of the call. +/// scope.nested(|inner| { +/// let x = inner.number(f64::from(i) * f64::from(i)); +/// inner.set(list, Key::Index(i), x) +/// })?; +/// } +/// let mut sum = 0.0; +/// for i in 0..n { +/// let x = scope.get(list, Key::Index(i))?; +/// sum += scope.as_number(x).unwrap_or(0.0); +/// } +/// Ok(sum) +/// }) +/// } +/// # let mut rt = clazel_js::Runtime::new(clazel_js::Config::new(clazel_js::Limits::unlimited()), clazel_js::DefaultHost::new())?; +/// # assert_eq!(sum_squares(&mut rt, 4)?, 14.0); +/// # Ok::<(), clazel_js::Error>(()) +/// ``` +#[derive(Debug)] +pub struct Scope<'s> { + /// The runtime's VM. A scope is this borrow and the runtime's identity; + /// everything else is in the VM's embedder slot (see [`crate::state`]). + vm: &'s mut Vm, + runtime: usize, + // Invariant in `'s`, so a `Local<'s>` cannot be coerced to another scope's. + _scope: PhantomData &'s ()>, +} + +/// What the heap's arena stands for in bytes, for `Limits::heap_bytes` and +/// `Runtime::heap_used`. The arena counts cells (a string, an object or an +/// environment), whose size varies, so both are estimates. +pub(crate) const CELL_BYTES: u64 = 64; + +/// A key of the property tables, an `Attrs` for methods and the like. +const METHOD_ATTRS: Attrs = Attrs(Attrs::WRITABLE | Attrs::CONFIGURABLE); + +// The machinery every operation uses. +impl<'s> Scope<'s> { + pub(crate) fn new(vm: &'s mut Vm, runtime: usize) -> Scope<'s> { + Scope { + vm, + runtime, + _scope: PhantomData, + } + } + + /// The runtime's state. + fn state(&self) -> Result<&State> { + self.vm.embedder::().ok_or(Error::WrongRuntime) + } + + /// The runtime's state, mutably. + fn state_mut(&mut self) -> Result<&mut State> { + self.vm.embedder_mut::().ok_or(Error::WrongRuntime) + } + + /// A `Local` of this scope for a value of the VM. + fn lv(&self, value: Value) -> Local<'s> { + Local::new(value, self.runtime) + } + + /// The VM's value of a `Local`, if it is of this runtime. + fn peek(&self, local: Local<'_>) -> Option { + (local.runtime() == self.runtime).then(|| local.value()) + } + + /// The VM's value of a `Local`: [`Error::WrongRuntime`] if it is another + /// runtime's. + fn val(&self, local: Local<'_>) -> Result { + self.peek(local).ok_or(Error::WrongRuntime) + } + + /// A VM operation's failure as this API's error. + fn err(&mut self, abrupt: Abrupt) -> Error { + abrupt_to_error(self.vm, abrupt) + } + + /// A VM operation's result as this API's. + fn run(&mut self, result: core::result::Result) -> Result { + result.map_err(|abrupt| self.err(abrupt)) + } + + /// A heap operation's failure as this API's error. + fn heap(&mut self, result: core::result::Result) -> Result { + result.map_err(|e| match e { + HeapError::Full | HeapError::TooLong => Error::OutOfMemory, + HeapError::Corrupt => Error::StaleHandle, + }) + } + + /// Records the error an infallible constructor could not return, for the + /// next operation that can fail to return instead. + fn defer(&mut self, error: Error) { + if let Ok(state) = self.state_mut() { + state.deferred.get_or_insert(error); + } + } + + /// Returns the error an earlier infallible constructor recorded, once. + pub(crate) fn check_deferred(&mut self) -> Result<()> { + match self.state_mut()?.deferred.take() { + Some(error) => Err(error), + None => Ok(()), + } + } + + /// Whether a reference still names a live cell (the arena's generation + /// check). Values that are not references are always alive. + fn alive(&self, value: Value) -> bool { + value.as_ref().is_none() || self.vm.heap().cell(value).is_some() + } + + fn token(&self) -> Arc<()> { + match self.state() { + Ok(state) => state.token.clone(), + Err(_) => Arc::new(()), + } + } + + /// Whether a handle's token is this runtime's. + fn owns(&self, token: &Arc<()>) -> Result<()> { + if Arc::ptr_eq(token, &self.state()?.token) { + Ok(()) + } else { + Err(Error::WrongRuntime) + } + } + + fn persist_value(&mut self, value: Value) -> Handle { + Handle::new(self.token(), value) + } + + /// An object value, or [`Error::WrongType`]. + fn object(&self, local: Local<'_>) -> Result { + let value = self.val(local)?; + if self.vm.heap().is_object(value) { + Ok(value) + } else { + Err(Error::WrongType("object")) + } + } + + /// A property key as the heap's interned key. A string key is interned, so + /// a long-lived runtime that is given endlessly new names grows its + /// interner (nothing collects it yet). + fn heap_key(&mut self, key: Key<'_, '_>) -> Result { + match key { + Key::Str(text) => { + let interned = self.vm.heap_mut().intern_str(text); + self.heap(interned) + } + Key::Index(index) => { + let interned = self.vm.heap_mut().intern_str(&index.to_string()); + self.heap(interned) + } + Key::Value(local) => { + let value = self.val(local)?; + let key = self.vm.coerce_property_key(value); + self.run(key) + } + } + } + + /// A string value from a conversion that can fail for length. + fn string_value( + &mut self, + text: core::result::Result, + ) -> Local<'s> { + let value = match text { + Ok(text) => self.vm.heap_mut().new_string(text), + Err(_) => Err(HeapError::TooLong), + }; + match value { + Ok(value) => self.lv(value), + Err(_) => { + self.defer(Error::OutOfMemory); + self.lv(Value::UNDEFINED) + } + } + } + + /// An infallible constructor of something this runtime lacks. + fn missing(&mut self, what: &'static str) -> Local<'s> { + self.defer(Error::NotImplemented(what)); + self.lv(Value::UNDEFINED) + } + + /// The realm: the VM's one global environment. + fn realm(&self) -> Realm { + Realm::new(Handle::new(self.token(), self.vm.global())) + } + + /// [`Error::WrongRuntime`] unless the realm is this runtime's. + fn check_realm(&self, realm: &Realm) -> Result<()> { + let handle = realm.as_handle(); + self.owns(handle.token())?; + if handle.value() == self.vm.global() { + Ok(()) + } else { + Err(Error::WrongRuntime) + } + } + + /// Takes the oldest job of the engine's own queue. + pub(crate) fn pop_job(&mut self) -> Option { + self.state_mut().ok()?.jobs.pop_front() + } + + /// Queues a job on the engine's own queue. + pub(crate) fn push_job(&mut self, job: crate::host::Job) -> Result<()> { + self.check_deferred()?; + self.state_mut()?.jobs.push_back(job); + Ok(()) + } +} + +impl<'s> Scope<'s> { + /// Runs `f` with an inner scope. The `Local`s `f` makes are released when + /// it returns, and cannot escape it. What `f` returns must be owned + /// (a [`Handle`], a number, an [`Exception`]); to keep a value, call + /// [`Scope::persist`] inside `f`. + pub fn nested(&mut self, f: impl for<'i> FnOnce(&mut Scope<'i>) -> R) -> R { + let runtime = self.runtime; + let mut inner = Scope::new(&mut *self.vm, runtime); + f(&mut inner) + } + + // ---- Holding values ---------------------------------------------------- + + /// Roots `value` beyond this scope. See [`Handle`]. + pub fn persist(&mut self, value: Local<'_>) -> Handle { + match self.peek(value) { + Some(value) => self.persist_value(value), + None => { + self.defer(Error::WrongRuntime); + self.persist_value(Value::UNDEFINED) + } + } + } + + /// The value a handle names, as a `Local` of this scope. + /// [`Error::WrongRuntime`] if the handle is another runtime's, + /// [`Error::StaleHandle`] if its generation does not match. + pub fn local(&mut self, handle: &Handle) -> Result> { + self.check_deferred()?; + self.owns(handle.token())?; + let value = handle.value(); + if self.alive(value) { + Ok(self.lv(value)) + } else { + Err(Error::StaleHandle) + } + } + + /// A weak reference to `value`. See [`Weak`]. + pub fn downgrade(&mut self, value: Local<'_>) -> Weak { + match self.peek(value) { + Some(value) => Weak::new(self.token(), value), + None => { + self.defer(Error::WrongRuntime); + Weak::new(self.token(), Value::UNDEFINED) + } + } + } + + /// The value, if it is still alive. `Ok(None)` once it has been collected. + pub fn upgrade(&mut self, weak: &Weak) -> Result>> { + self.check_deferred()?; + self.owns(weak.token())?; + let value = weak.value(); + Ok(self.alive(value).then(|| self.lv(value))) + } + + /// A [`Member`] for `value`, to store in a host object's data and report + /// from its `trace`. + pub fn member(&mut self, value: Local<'_>) -> Member { + match self.peek(value) { + Some(value) => Member::new(value, self.runtime), + None => { + self.defer(Error::WrongRuntime); + Member::new(Value::UNDEFINED, self.runtime) + } + } + } + + /// The value a member names. [`Error::StaleHandle`] if the owner did not + /// report it and it has been collected. + pub fn local_from_member(&mut self, member: Member) -> Result> { + self.check_deferred()?; + if member.runtime() != self.runtime { + return Err(Error::WrongRuntime); + } + let value = member.value(); + if self.alive(value) { + Ok(self.lv(value)) + } else { + Err(Error::StaleHandle) + } + } + + // ---- Making and reading primitive values ----------------------------- + + /// `undefined`. + pub fn undefined(&mut self) -> Local<'s> { + self.lv(Value::UNDEFINED) + } + + /// `null`. + pub fn null(&mut self) -> Local<'s> { + self.lv(Value::NULL) + } + + /// A Boolean. + pub fn boolean(&mut self, value: bool) -> Local<'s> { + self.lv(Value::boolean(value)) + } + + /// A Number. NaN is canonicalized, as the value encoding requires. + pub fn number(&mut self, value: f64) -> Local<'s> { + self.lv(util::number(value)) + } + + /// A String from Rust text. + pub fn string(&mut self, value: &str) -> Local<'s> { + self.string_value(JsString::from_rust_str(value)) + } + + /// A String from UTF-16 code units, which may hold lone surrogates that a + /// Rust `&str` cannot. This is the exact form JavaScript strings take, + /// and what WebIDL's `DOMString` and `USVString` conversions start from. + pub fn string_utf16(&mut self, units: &[u16]) -> Local<'s> { + self.string_value(JsString::from_units(units)) + } + + /// A BigInt from an `i64`. + pub fn bigint_from_i64(&mut self, value: i64) -> Local<'s> { + let _ = value; + self.missing("Scope::bigint_from_i64: the VM has no BigInt yet") + } + + /// A BigInt from a `u64`. + pub fn bigint_from_u64(&mut self, value: u64) -> Local<'s> { + let _ = value; + self.missing("Scope::bigint_from_u64: the VM has no BigInt yet") + } + + /// A new unique Symbol with an optional description. + pub fn new_symbol(&mut self, description: Option<&str>) -> Local<'s> { + let _ = description; + self.missing("Scope::new_symbol: the VM has no Symbols yet") + } + + /// A well-known symbol. The same symbol in every call and every realm of + /// this runtime, as ECMA-262 says. + pub fn well_known_symbol(&mut self, symbol: WellKnownSymbol) -> Local<'s> { + let _ = symbol; + self.missing("Scope::well_known_symbol: the VM has no Symbols yet") + } + + /// What kind of value this is. Never throws and never runs JavaScript. + pub fn kind(&self, value: Local<'_>) -> ValueKind { + let Some(v) = self.peek(value) else { + return ValueKind::Undefined; + }; + let heap = self.vm.heap(); + match v.decode() { + Decoded::Null => ValueKind::Null, + Decoded::Bool(_) => ValueKind::Boolean, + Decoded::Int32(_) | Decoded::Double(_) => ValueKind::Number, + Decoded::Ref(_) if heap.is_string(v) => ValueKind::String, + Decoded::Ref(_) if heap.is_callable(v) => ValueKind::Function, + Decoded::Ref(_) if heap.is_object(v) => ValueKind::Object, + _ => ValueKind::Undefined, + } + } + + /// The Boolean, or `None` if the value is not one. + pub fn as_boolean(&self, value: Local<'_>) -> Option { + match self.peek(value)?.decode() { + Decoded::Bool(b) => Some(b), + _ => None, + } + } + + /// The Number, or `None` if the value is not one. + pub fn as_number(&self, value: Local<'_>) -> Option { + util::as_number(self.peek(value)?) + } + + /// A String's UTF-16 code units, exactly. `Err(WrongType)` for a + /// non-string. + pub fn string_to_utf16(&self, value: Local<'_>) -> Result> { + let v = self.val(value)?; + let text = self.vm.heap().string(v).ok_or(Error::WrongType("string"))?; + Ok(text.units().collect()) + } + + /// A String as Rust text, with each lone surrogate replaced by U+FFFD. + /// `Err(WrongType)` for a non-string. + pub fn string_to_string_lossy(&self, value: Local<'_>) -> Result { + let v = self.val(value)?; + let text = self.vm.heap().string(v).ok_or(Error::WrongType("string"))?; + Ok(text.to_string_lossy()) + } + + // ---- Conversions and tests (what WebIDL's conversions are built from) -- + + /// `ToBoolean`. Cannot throw. + pub fn to_boolean(&mut self, value: Local<'_>) -> bool { + match self.peek(value) { + Some(v) => self.vm.to_boolean(v), + None => { + self.defer(Error::WrongRuntime); + false + } + } + } + + /// `ToNumber`. Can throw: a Symbol, a BigInt, or an object whose + /// `valueOf` throws. + pub fn to_number(&mut self, value: Local<'_>) -> Result { + self.check_deferred()?; + let v = self.val(value)?; + let n = self.vm.to_number(v); + self.run(n) + } + + /// `ToString`, as a String value. Can throw. + pub fn to_string(&mut self, value: Local<'_>) -> Result> { + self.check_deferred()?; + let v = self.val(value)?; + if self.vm.heap().is_string(v) { + return Ok(self.lv(v)); + } + let text = self.vm.to_js_string(v); + let text = self.run(text)?; + let made = self.vm.heap_mut().new_string(text); + let made = self.heap(made)?; + Ok(self.lv(made)) + } + + /// `ToObject`. Throws a `TypeError` for `undefined` and `null`. + pub fn to_object(&mut self, value: Local<'_>) -> Result> { + self.check_deferred()?; + let v = self.val(value)?; + let o = self.vm.to_object(v); + let o = self.run(o)?; + Ok(self.lv(o)) + } + + /// `IsCallable`. + pub fn is_callable(&self, value: Local<'_>) -> bool { + self.peek(value) + .is_some_and(|v| self.vm.heap().is_callable(v)) + } + + /// `IsConstructor`. + pub fn is_constructor(&self, value: Local<'_>) -> bool { + self.peek(value).is_some_and(|v| self.vm.is_constructor(v)) + } + + /// `IsArray`. Throws for a revoked proxy. + pub fn is_array(&mut self, value: Local<'_>) -> Result { + // The VM has no Array objects yet, so no value is one. + self.check_deferred()?; + self.val(value)?; + Ok(false) + } + + /// `IsStrictlyEqual` (`===`). + pub fn strict_equals(&self, a: Local<'_>, b: Local<'_>) -> bool { + let (Some(x), Some(y)) = (self.peek(a), self.peek(b)) else { + return false; + }; + if let (Some(x), Some(y)) = (util::as_number(x), util::as_number(y)) { + return x == y; + } + let heap = self.vm.heap(); + if let (Some(x), Some(y)) = (heap.string(x), heap.string(y)) { + return x == y; + } + x == y + } + + /// `SameValue` (`Object.is`). + pub fn same_value(&self, a: Local<'_>, b: Local<'_>) -> bool { + match (self.peek(a), self.peek(b)) { + (Some(x), Some(y)) => util::same_value(self.vm, x, y), + _ => false, + } + } + + /// `InstanceofOperator` (`value instanceof constructor`). Can throw. + pub fn instance_of(&mut self, value: Local<'_>, constructor: Local<'_>) -> Result { + // InstanceofOperator (§13.10.2) without step 2 (`@@hasInstance`, there + // are no Symbols yet): step 3 is IsCallable, step 4 OrdinaryHasInstance. + self.check_deferred()?; + let v = self.val(value)?; + let c = self.val(constructor)?; + if !self.vm.heap().is_object(c) { + let e = self + .vm + .type_error("Right-hand side of 'instanceof' is not an object"); + return Err(self.err(e)); + } + if !self.vm.heap().is_callable(c) { + let e = self + .vm + .type_error("Right-hand side of 'instanceof' is not callable"); + return Err(self.err(e)); + } + if matches!( + self.vm.heap().obj(c).map(|o| o.kind), + Some(ObjKind::Bound(_)) + ) { + return Err(Error::NotImplemented( + "Scope::instance_of with a bound function: its target is not readable yet", + )); + } + if !self.vm.heap().is_object(v) { + return Ok(false); + } + let key = self.heap_key(Key::Str("prototype"))?; + let proto = self.vm.get_value(c, key); + let proto = self.run(proto)?; + if !self.vm.heap().is_object(proto) { + let e = self + .vm + .type_error("Function has non-object prototype in instanceof check"); + return Err(self.err(e)); + } + let mut current = v; + for _ in 0..MAX_PROTO_DEPTH { + current = self.vm.heap().proto_of(current).unwrap_or(Value::NULL); + if current == Value::NULL || !self.vm.heap().is_object(current) { + return Ok(false); + } + if current == proto { + return Ok(true); + } + } + Err(Error::StaleHandle) + } + + // ---- Objects ------------------------------------------------------------ + + /// A new ordinary object, as `{}` in the current realm. + pub fn new_object(&mut self) -> Result> { + self.check_deferred()?; + let o = self.vm.new_object(); + let o = self.run(o)?; + Ok(self.lv(o)) + } + + /// A new Array of the given length, as `new Array(length)`. + pub fn new_array(&mut self, length: u32) -> Result> { + let _ = length; + not_implemented!("Scope::new_array: the VM has no Array objects yet") + } + + /// A new Array holding these values. + pub fn new_array_from(&mut self, elements: &[Local<'_>]) -> Result> { + let _ = elements; + not_implemented!("Scope::new_array_from: the VM has no Array objects yet") + } + + /// `Get(object, key)`. Runs getters and proxy traps, so can throw. + pub fn get(&mut self, object: Local<'_>, key: Key<'_, '_>) -> Result> { + self.check_deferred()?; + let o = self.val(object)?; + let k = self.heap_key(key)?; + let v = self.vm.get_value(o, k); + let v = self.run(v)?; + Ok(self.lv(v)) + } + + /// `Set(object, key, value, true)`: throws in the cases strict-mode + /// assignment throws (a read-only property, a frozen object). + pub fn set(&mut self, object: Local<'_>, key: Key<'_, '_>, value: Local<'_>) -> Result<()> { + self.check_deferred()?; + let o = self.object(object)?; + let v = self.val(value)?; + let k = self.heap_key(key)?; + let done = self.vm.put_value(o, k, v, true); + self.run(done) + } + + /// `HasProperty` (`key in object`). + pub fn has_property(&mut self, object: Local<'_>, key: Key<'_, '_>) -> Result { + self.check_deferred()?; + let o = self.object(object)?; + let k = self.heap_key(key)?; + let found = util::has_property(self.vm, o, k); + let found = self.run(found)?; + Ok(found) + } + + /// `DeletePropertyOrThrow`, but returning `false` for a refusal instead of + /// throwing, as sloppy-mode `delete` does. + pub fn delete_property(&mut self, object: Local<'_>, key: Key<'_, '_>) -> Result { + let _ = (object, key); + not_implemented!("Scope::delete_property: the heap cannot remove a property yet") + } + + /// `DefinePropertyOrThrow`: `Object.defineProperty`. + pub fn define_property( + &mut self, + object: Local<'_>, + key: Key<'_, '_>, + descriptor: PropertyDescriptor<'_>, + ) -> Result<()> { + self.check_deferred()?; + let o = self.object(object)?; + if descriptor.get.is_some() || descriptor.set.is_some() { + return Err(Error::NotImplemented( + "Scope::define_property with an accessor: the heap has no accessor properties yet", + )); + } + let k = self.heap_key(key)?; + let value = match descriptor.value { + Some(v) => Some(self.val(v)?), + None => None, + }; + let desc = Descriptor { + value, + writable: descriptor.writable, + enumerable: descriptor.enumerable, + configurable: descriptor.configurable, + }; + let done = define_property_or_throw(self.vm, o, k, &desc); + self.run(done) + } + + /// `GetPrototypeOf`. `null` is returned as the `null` value. + pub fn get_prototype_of(&mut self, object: Local<'_>) -> Result> { + self.check_deferred()?; + let o = self.object(object)?; + let proto = self.vm.heap().proto_of(o).unwrap_or(Value::NULL); + Ok(self.lv(proto)) + } + + /// `SetPrototypeOf`: throws if the object refuses (not extensible, or a + /// cycle). + pub fn set_prototype_of(&mut self, object: Local<'_>, prototype: Local<'_>) -> Result<()> { + // OrdinarySetPrototypeOf (§10.1.2.1): an object is always extensible + // here, so only the cycle check (step 8) can refuse. + self.check_deferred()?; + let o = self.object(object)?; + let p = self.val(prototype)?; + if p != Value::NULL && !self.vm.heap().is_object(p) { + let e = self + .vm + .type_error("Object prototype may only be an Object or null"); + return Err(self.err(e)); + } + let mut walk = p; + for _ in 0..MAX_PROTO_DEPTH { + if walk == Value::NULL { + let done = self.vm.heap_mut().set_proto(o, p); + return self.heap(done); + } + if walk == o { + let e = self.vm.type_error("Cyclic prototype value"); + return Err(self.err(e)); + } + walk = self.vm.heap().proto_of(walk).unwrap_or(Value::NULL); + } + Err(Error::StaleHandle) + } + + /// `GetMethod(value, key)`: `Ok(None)` if the property is `undefined` or + /// `null`, a `TypeError` if it is anything else but callable. + pub fn get_method(&mut self, value: Local<'_>, key: Key<'_, '_>) -> Result>> { + let f = self.get(value, key)?; + let v = self.val(f)?; + if v == Value::UNDEFINED || v == Value::NULL { + return Ok(None); + } + if self.vm.heap().is_callable(v) { + return Ok(Some(f)); + } + let e = self.vm.type_error("property is not a function"); + Err(self.err(e)) + } + + /// `Call(function, this, arguments)`. + pub fn call( + &mut self, + function: Local<'_>, + this: Local<'_>, + arguments: &[Local<'_>], + ) -> Result> { + self.check_deferred()?; + let f = self.val(function)?; + let t = self.val(this)?; + let mut args = Vec::with_capacity(arguments.len()); + for a in arguments { + args.push(self.val(*a)?); + } + let r = self.vm.call(f, t, &args); + let r = self.run(r)?; + Ok(self.lv(r)) + } + + /// `Construct(constructor, arguments, newTarget)`; `new_target` of `None` + /// is the constructor itself. + pub fn construct( + &mut self, + constructor: Local<'_>, + arguments: &[Local<'_>], + new_target: Option>, + ) -> Result> { + self.check_deferred()?; + let c = self.val(constructor)?; + if let Some(nt) = new_target + && self.val(nt)? != c + { + return Err(Error::NotImplemented( + "Scope::construct with a new_target other than the constructor", + )); + } + let mut args = Vec::with_capacity(arguments.len()); + for a in arguments { + args.push(self.val(*a)?); + } + let r = self.vm.construct_value(c, &args); + let r = self.run(r)?; + Ok(self.lv(r)) + } + + /// Runs the iterator protocol over `iterable`, calling `f` with each value + /// until it returns `Ok(false)` or the iterator ends. If `f` returns `Err`, + /// or `Ok(false)`, the iterator is closed (`IteratorClose`) first. This is + /// the operation WebIDL's `sequence` and `iterable` conversions need. + /// + /// This stays `no_run`: `iterate` returns `NotImplemented` until the VM + /// has Symbols and iterators. + /// + /// ```no_run + /// use clazel_js::Runtime; + /// + /// fn count(rt: &mut Runtime, iterable: &clazel_js::Handle) -> clazel_js::Result { + /// rt.scope(|scope| { + /// let iterable = scope.local(iterable)?; + /// let mut n = 0; + /// scope.iterate(iterable, |_scope, _item| { + /// n += 1; + /// Ok(true) + /// })?; + /// Ok(n) + /// }) + /// } + /// ``` + pub fn iterate( + &mut self, + iterable: Local<'_>, + f: impl for<'i> FnMut(&mut Scope<'i>, Local<'i>) -> Result, + ) -> Result<()> { + let _ = (iterable, f); + not_implemented!("Scope::iterate: the VM has no iteration protocol or Symbols yet") + } + + // ---- Native functions and host objects ------------------------------------ + + /// A JavaScript function that runs `f`. + /// + /// `name` and `length` are the function's `name` and `length`. `callable` + /// says whether `new` works on it. `f` gets a fresh scope for the call, + /// the arguments, and returns the result or an [`Error`]. Panics in `f` + /// are not caught: the lints forbid them in this repository, and an + /// embedder that panics gets its own panic. + /// + /// **Re-entrance, not yet:** `f` is `Fn` so that JavaScript can call it + /// again while it runs, but the closures live inside the VM that `f` + /// borrows, so the running closure is out of its table: a call that + /// reaches the same function again gets [`Error::Busy`] (which JavaScript + /// cannot catch). Different functions nest freely. Supporting the same + /// one needs `f: Sync`, see the README's open questions. + /// + /// ``` + /// use clazel_js::{Args, Callable, Key, Local, Result, Runtime, Scope, Source}; + /// + /// fn install(rt: &mut Runtime, realm: &clazel_js::Realm) -> Result<()> { + /// rt.scope(|scope| { + /// let global = scope.global(realm)?; + /// let greet = scope.function("greet", 1, Callable::Function, |scope, args| { + /// let who = scope.to_string(args.get(0))?; + /// let who = scope.string_to_string_lossy(who)?; + /// Ok(scope.string(&format!("hello, {who}"))) + /// })?; + /// scope.set(global, Key::from("greet"), greet) + /// }) + /// } + /// # let mut rt = clazel_js::Runtime::new(clazel_js::Config::new(clazel_js::Limits::unlimited()), clazel_js::DefaultHost::new())?; + /// # let realm = rt.scope(|scope| scope.create_realm(clazel_js::RealmOptions::default()))?; + /// # install(&mut rt, &realm)?; + /// # let text = rt.scope(|scope| { + /// # scope.eval_script(&realm, Source::new("g.js", "var said = greet('world');"))?; + /// # let global = scope.global(&realm)?; + /// # let said = scope.get(global, Key::from("said"))?; + /// # scope.string_to_string_lossy(said) + /// # })?; + /// # assert_eq!(text, "hello, world"); + /// # Ok::<(), clazel_js::Error>(()) + /// ``` + pub fn function( + &mut self, + name: &str, + length: u32, + callable: Callable, + f: impl for<'a> Fn(&mut Scope<'a>, &Args<'a>) -> Result> + Send + 'static, + ) -> Result> { + self.check_deferred()?; + let length = i32::try_from(length).unwrap_or(i32::MAX); + let made = match callable { + Callable::Function => self.vm.new_native(name, length, trampoline), + Callable::Constructor => self.vm.new_native_constructor(name, length, trampoline), + }; + let function = self.run(made)?; + let boxed: Box = Box::new(f); + self.state_mut()? + .natives + .insert(function.bits(), Some(boxed)); + Ok(self.lv(function)) + } + + /// Makes a function with [`Scope::function`] and defines it as a method + /// of `object`: writable, non-enumerable, configurable, as WebIDL + /// operations and built-in methods are. + pub fn define_method( + &mut self, + object: Local<'_>, + name: &str, + length: u32, + f: impl for<'a> Fn(&mut Scope<'a>, &Args<'a>) -> Result> + Send + 'static, + ) -> Result<()> { + self.check_deferred()?; + let o = self.object(object)?; + let function = self.function(name, length, Callable::Function, f)?; + let k = self.heap_key(Key::Str(name))?; + let v = self.val(function)?; + let done = self.vm.heap_mut().define(o, k, v, METHOD_ATTRS); + self.heap(done) + } + + /// A new host object carrying `data`, with this prototype (`None` is + /// `null`). See [`HostObject`]. + pub fn new_host_object( + &mut self, + prototype: Option>, + data: impl HostObject, + ) -> Result> { + self.check_deferred()?; + let proto = match prototype { + Some(p) => { + let p = self.val(p)?; + if p != Value::NULL && !self.vm.heap().is_object(p) { + return Err(Error::WrongType("object or null")); + } + p + } + None => Value::NULL, + }; + let made = self.vm.heap_mut().new_object(proto, ObjKind::Plain); + let object = self.heap(made)?; + self.state_mut()? + .host_objects + .insert(object.bits(), Box::new(data)); + Ok(self.lv(object)) + } + + /// The data of a host object of type `T`. [`Error::WrongType`] if the + /// value is anything else (another type of host object included), and + /// [`Error::Busy`] if one of its own hooks is running. + pub fn host_data(&self, object: Local<'_>) -> Result<&T> { + let o = self.val(object)?; + let data = self + .state()? + .host_objects + .get(&o.bits()) + .ok_or(Error::WrongType("a host object"))?; + let data: &dyn Any = &**data; + data.downcast_ref::() + .ok_or(Error::WrongType("a host object of the requested type")) + } + + /// As [`Scope::host_data`], mutably. + pub fn host_data_mut(&mut self, object: Local<'_>) -> Result<&mut T> { + let o = self.val(object)?; + let data = self + .state_mut()? + .host_objects + .get_mut(&o.bits()) + .ok_or(Error::WrongType("a host object"))?; + let data: &mut dyn Any = &mut **data; + data.downcast_mut::() + .ok_or(Error::WrongType("a host object of the requested type")) + } + + /// `GetPrototypeFromConstructor(newTarget, ...)`: the `prototype` of + /// `new_target` if it is an object, else `fallback`. A constructor that + /// makes a host object calls this with its [`Args::new_target`] so that a + /// subclass (`class X extends Node`) gets its own prototype. + pub fn get_prototype_from_constructor( + &mut self, + new_target: Local<'_>, + fallback: Local<'_>, + ) -> Result> { + self.check_deferred()?; + let nt = self.val(new_target)?; + let fb = self.val(fallback)?; + if self.vm.heap().is_object(nt) { + let key = self.heap_key(Key::Str("prototype"))?; + let proto = self.vm.get_value(nt, key); + let proto = self.run(proto)?; + if self.vm.heap().is_object(proto) { + return Ok(self.lv(proto)); + } + } + Ok(self.lv(fb)) + } + + /// The embedder's [`Host`] as its own type. [`Error::WrongType`] if the + /// host is another type, and [`Error::Busy`] while a hook of the host is + /// running (the hook already has the host as `&mut self`). + pub fn host_mut(&mut self) -> Result<&mut H> { + let host: &mut dyn Any = &mut *self.state_mut()?.host; + host.downcast_mut::() + .ok_or(Error::WrongType("the host's type")) + } + + /// For a native function: ends the call with [`Error::Terminated`] if a + /// [`Terminator`](crate::Terminator) has been used. Call it in any native + /// loop that can run long. + pub fn check_terminated(&self) -> Result<()> { + if self.vm.terminator().is_set() { + Err(Error::Terminated) + } else { + Ok(()) + } + } + + // ---- Errors --------------------------------------------------------------- + + /// A new error object of `kind` with `message`, as an [`Exception`] ready to + /// throw or to refuse a module load with. + pub fn new_exception(&mut self, kind: ErrorKind, message: &str) -> Result { + self.check_deferred()?; + let which = match kind { + ErrorKind::Error => Intrinsic::ErrorPrototype, + ErrorKind::RangeError => Intrinsic::RangeErrorPrototype, + ErrorKind::ReferenceError => Intrinsic::ReferenceErrorPrototype, + ErrorKind::SyntaxError => Intrinsic::SyntaxErrorPrototype, + ErrorKind::TypeError => Intrinsic::TypeErrorPrototype, + ErrorKind::EvalError | ErrorKind::UriError => { + return Err(Error::NotImplemented( + "Scope::new_exception for EvalError and URIError: the built-ins have no such constructor yet", + )); + } + }; + // §20.5.6.1.1 NativeError ( message ), steps 2 to 4: `[[ErrorData]]`, + // the intrinsic prototype and an own, non-enumerable `message`. + let proto = self.vm.intrinsic(which); + let made = self.vm.heap_mut().new_object(proto, ObjKind::Error); + let object = self.heap(made)?; + let text = JsString::from_rust_str(message).map_err(|_| Error::OutOfMemory)?; + let text = self.vm.heap_mut().new_string(text); + let text = self.heap(text)?; + let k = self.heap_key(Key::Str("message"))?; + let done = self.vm.heap_mut().define(object, k, text, METHOD_ATTRS); + self.heap(done)?; + Ok(Exception::from_handle(self.persist_value(object))) + } + + /// The `Err` for throwing a new error: `return Err(scope.throw_error(...))`. + /// If creating the error object itself fails, that failure is returned. + pub fn throw_error(&mut self, kind: ErrorKind, message: &str) -> Error { + match self.new_exception(kind, message) { + Ok(exception) => Error::Thrown(exception), + Err(other) => other, + } + } + + /// The `Err` for throwing any value, for example a `DOMException` the + /// embedder made. + pub fn throw(&mut self, value: Local<'_>) -> Error { + Error::Thrown(Exception::from_handle(self.persist(value))) + } + + /// The value inside an exception, as a `Local` of this scope. + pub fn exception_value(&mut self, exception: &Exception) -> Result> { + self.local(exception.value()) + } + + /// Describes a thrown value. See [`ErrorReport`]. Reads `name`, `message` + /// and `stack` as data properties and does not call getters, so it cannot + /// run JavaScript or throw. + pub fn report(&mut self, exception: &Exception) -> Result { + let value = self.exception_value(exception)?; + let v = self.val(value)?; + if self.vm.heap().is_object(v) { + // Data properties only: there are no accessors to run, and + // `get_named` runs no JavaScript. + let name = self.vm.get_named(v, "name"); + let name = self.run(name)?; + let message = self.vm.get_named(v, "message"); + let message = self.run(message)?; + return Ok(ErrorReport { + name: self.vm.string_text(name).unwrap_or_default(), + message: self.vm.string_text(message).unwrap_or_default(), + // The VM keeps no stack of source positions yet. + stack: Vec::new(), + }); + } + // A primitive (a Symbol is the only one ToString can throw on, and + // there are none). + let text = self.vm.to_js_string(v); + let text = self.run(text)?; + Ok(ErrorReport { + name: String::new(), + message: text.to_string_lossy(), + stack: Vec::new(), + }) + } + + // ---- Promises --------------------------------------------------------------- + + /// A new pending promise and its resolving functions. For an API that + /// returns a promise and settles it later (`fetch`). + /// + /// This stays `no_run`: promises return `NotImplemented` until the VM has + /// them. + /// + /// ```no_run + /// use clazel_js::{Handle, Resolvers, Runtime}; + /// + /// // `fetch()` returns the promise now and keeps the resolvers for later. + /// fn start_fetch(rt: &mut Runtime) -> clazel_js::Result<(Handle, Resolvers)> { + /// rt.scope(|scope| { + /// let (promise, resolvers) = scope.new_promise()?; + /// Ok((scope.persist(promise), resolvers)) + /// }) + /// } + /// + /// // The kernel answered; settle it, then let the reactions run. + /// fn finish_fetch(rt: &mut Runtime, resolvers: &Resolvers) -> clazel_js::Result<()> { + /// rt.scope(|scope| { + /// let response = scope.new_object()?; + /// scope.resolve_promise(resolvers, response) + /// })?; + /// rt.run_jobs()?; + /// Ok(()) + /// } + /// ``` + pub fn new_promise(&mut self) -> Result<(Local<'s>, Resolvers)> { + not_implemented!("Scope::new_promise: the VM has no Promise objects yet") + } + + /// Calls the promise's `resolve` with `value`. If the value is a thenable + /// the promise follows it, as for any resolve. + pub fn resolve_promise(&mut self, resolvers: &Resolvers, value: Local<'_>) -> Result<()> { + let _ = (resolvers, value); + not_implemented!("Scope::resolve_promise: the VM has no Promise objects yet") + } + + /// Calls the promise's `reject` with `reason`. + pub fn reject_promise(&mut self, resolvers: &Resolvers, reason: Local<'_>) -> Result<()> { + let _ = (resolvers, reason); + not_implemented!("Scope::reject_promise: the VM has no Promise objects yet") + } + + /// `Promise.resolve(value)` in the current realm. + pub fn promise_resolve(&mut self, value: Local<'_>) -> Result> { + let _ = value; + not_implemented!("Scope::promise_resolve: the VM has no Promise objects yet") + } + + /// `PerformPromiseThen` on `promise` with these reactions (each a function + /// or `None`), returning the derived promise. This is how the embedder + /// continues after a promise it did not make, such as the one + /// [`Scope::evaluate_module`] returns. + pub fn promise_then( + &mut self, + promise: Local<'_>, + on_fulfilled: Option>, + on_rejected: Option>, + ) -> Result> { + let _ = (promise, on_fulfilled, on_rejected); + not_implemented!("Scope::promise_then: the VM has no Promise objects yet") + } + + /// A promise's state now. Reading it never marks a rejection handled. + pub fn promise_state(&self, promise: Local<'_>) -> Result> { + let _ = promise; + not_implemented!("Scope::promise_state: the VM has no Promise objects yet") + } + + // ---- ArrayBuffers ------------------------------------------------------------ + + /// A new `ArrayBuffer` that owns `bytes`. + pub fn new_array_buffer(&mut self, bytes: Vec) -> Result> { + let _ = bytes; + not_implemented!("Scope::new_array_buffer: the VM has no ArrayBuffer yet") + } + + /// An `ArrayBuffer`'s bytes. Empty for a detached one. The borrow ends + /// before any call that can run JavaScript, which could detach or resize it. + pub fn array_buffer_bytes(&self, buffer: Local<'_>) -> Result<&[u8]> { + let _ = buffer; + not_implemented!("Scope::array_buffer_bytes: the VM has no ArrayBuffer yet") + } + + /// `DetachArrayBuffer(buffer, key)`: after it the buffer has length 0. + /// `key` must be the `ArrayBuffer`'s detach key if it has one, else a + /// `TypeError`. This is Test262's `$262.detachArrayBuffer`, and what + /// `postMessage` transfers and `structuredClone` use later. + pub fn detach_array_buffer(&mut self, buffer: Local<'_>, key: Option>) -> Result<()> { + let _ = (buffer, key); + not_implemented!("Scope::detach_array_buffer: the VM has no ArrayBuffer yet") + } + + // ---- Realms --------------------------------------------------------------------- + + /// `CreateRealm` plus `SetDefaultGlobalBindings`: a new realm with its own + /// built-ins. This is `$262.createRealm`. + /// + /// **Not yet:** the VM has one global environment, and + /// [`Runtime::new`](crate::Runtime::new) has already installed the + /// built-ins in it. The first call returns that realm and a second is + /// [`Error::NotImplemented`]; so is `RealmOptions::global`. + pub fn create_realm(&mut self, options: RealmOptions) -> Result { + self.check_deferred()?; + if options.global.is_some() { + return Err(Error::NotImplemented( + "Scope::create_realm with RealmOptions::global: the VM's global is an ordinary object", + )); + } + let state = self.state_mut()?; + if state.realm_made { + return Err(Error::NotImplemented( + "a second realm: a VM has one global environment", + )); + } + state.realm_made = true; + Ok(self.realm()) + } + + /// The realm of the function being run. Inside a native function, the + /// function's own realm; inside a hook, the realm of the code that + /// triggered it, if there is one. + pub fn current_realm(&mut self) -> Result { + self.check_deferred()?; + Ok(self.realm()) + } + + /// The realm's global object. Add properties to it with [`Scope::set`] and + /// [`Scope::define_property`]; that is how an embedder installs `console` + /// or `window`. + pub fn global(&mut self, realm: &Realm) -> Result> { + self.check_deferred()?; + self.check_realm(realm)?; + Ok(self.lv(self.vm.global())) + } + + /// Attaches the embedder's data to a realm (HTML's environment settings + /// object), replacing what was there. + pub fn set_realm_host_defined( + &mut self, + realm: &Realm, + data: Box, + ) -> Result<()> { + self.check_realm(realm)?; + self.state_mut()?.realm_host_defined = Some(data); + Ok(()) + } + + /// The data attached with [`Scope::set_realm_host_defined`]. + pub fn realm_host_defined(&self, realm: &Realm) -> Result> { + self.check_realm(realm)?; + Ok(self.state()?.realm_host_defined.as_deref()) + } + + // ---- Scripts and modules ------------------------------------------------------------ + + /// `ParseScript`: compiles `source`. A syntax error is + /// [`Error::Thrown`] holding a `SyntaxError` of `realm`. + pub fn compile_script(&mut self, realm: &Realm, source: Source<'_>) -> Result