diff --git a/Cargo.lock b/Cargo.lock index c3651b419..c8972623b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -217,6 +217,14 @@ dependencies = [ "vstd", ] +[[package]] +name = "clazel-codecheck" +version = "0.0.0" +dependencies = [ + "clazel-x64", + "vstd", +] + [[package]] name = "clazel-cookie" version = "0.0.0" @@ -395,6 +403,7 @@ version = "0.0.0" name = "clazel-js-syntax" version = "0.0.0" dependencies = [ + "clazel-js-lexer", "vstd", ] diff --git a/crates/clazel-codecheck/Cargo.toml b/crates/clazel-codecheck/Cargo.toml new file mode 100644 index 000000000..0ce4c94de --- /dev/null +++ b/crates/clazel-codecheck/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "clazel-codecheck" +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 first: what an accepted code blob is, and the contract of the validator. Only the spec, the contract and examples exist so far; there is no validator yet." + +[dependencies] +vstd = "=0.0.0-2026-10-04-0306" +# The instruction subset the validator accepts, and its canonical encoding. +# The decoder of untrusted bytes is this crate's own (CLAUDE.md rule 8); the +# only thing taken from `clazel-x64` is the specification of the subset. +clazel-x64 = { path = "../clazel-x64" } + +# Verification tier (docs/VERIFICATION.md). `tier` is what is proven about the +# shipped code today: nothing, because no validator exists yet (the spec, the +# contract and examples are written first, CLAUDE.md rule 3). `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-codecheck/mutations.json b/crates/clazel-codecheck/mutations.json new file mode 100644 index 000000000..62584ccc7 --- /dev/null +++ b/crates/clazel-codecheck/mutations.json @@ -0,0 +1,121 @@ +{ + "mutations": [ + { + "id": "index-fact-ignored", + "file": "src/spec.rs", + "find": "(Fact::Base(k), Fact::Idx(k2, n)) => k == k2 && a.size <= n,", + "replace": "(Fact::Base(k), _) => true,", + "expect": "killed", + "reason": "Accepts a region access through any base register, whatever is known of the index (J.6: an index register compared against the region's bound): a load through an index no bounds check touched would be accepted, so lemma_region_access_facts is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "lemma_region_access_facts(cx_base2(), cx_load())", + "witness": "access_ok(cx_base2(), cx_load()) && !(fact_of(cx_base2(), 1) is Idx)" + } + }, + { + "id": "check-size-order-reversed", + "file": "src/spec.rs", + "find": "k == k2 && a.size <= n,", + "replace": "k == k2 && n <= a.size,", + "expect": "killed", + "reason": "Reverses the comparison of the checked size and the access size, so a check for 4 bytes would license an 8-byte access and the last 4 bytes could lie outside the region: lemma_region_access_facts is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "lemma_region_access_facts(cx_idx4(), cx_load())", + "witness": "access_ok(cx_idx4(), cx_load()) && !idx_covers(fact_of(cx_idx4(), 1), 8)" + } + }, + { + "id": "check-region-not-compared", + "file": "src/spec.rs", + "find": "k == k2 && a.size <= n,", + "replace": "a.size <= n,", + "expect": "killed", + "reason": "Does not compare the region of the base register with the region the index was checked against, so an index checked against a small region could be used with a large region's base: lemma_region_access_facts is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "lemma_region_access_facts(cx_idx_region2(), cx_load())", + "witness": "access_ok(cx_idx_region2(), cx_load()) && !same_region(fact_of(cx_idx_region2(), 2), fact_of(cx_idx_region2(), 1))" + } + }, + { + "id": "stack-pointer-not-reserved", + "file": "src/spec.rs", + "find": "r == ctx_reg() || r == sp_reg()", + "replace": "r == ctx_reg()", + "expect": "killed", + "reason": "Lets the code write the stack pointer (J.6: reserved registers and the stack pointer are written only in the fixed prologue and epilogue), which would make the exit sequence's ret return anywhere: lemma_plain_ok_safe is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "lemma_plain_ok_safe(no_facts(), cx_mov_rsp())", + "witness": "plain_ok(no_facts(), cx_mov_rsp()) is Some" + } + }, + { + "id": "targets-not-boundaries", + "file": "src/spec.rs", + "find": "Some(starts) => subset_of(all_targets(ins, entries), starts),", + "replace": "Some(starts) => true,", + "expect": "killed", + "reason": "Drops the rule that every direct target is the start of a unit (J.6: every direct target is a decoded boundary inside the blob): a jump into the middle of a bounds check, skipping its first instructions, would be accepted, so lemma_policy_targets_are_unit_starts is false. (example_jump_into_check_rejected is false too.)", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "lemma_policy_targets_are_unit_starts(super::examples::prog_jump_into_check(), seq![0int])", + "witness": "policy(super::examples::prog_jump_into_check(), seq![0int]) && !subset_of(all_targets(super::examples::prog_jump_into_check(), seq![0int]), unit_starts(super::examples::prog_jump_into_check(), seq![0int]))" + } + }, + { + "id": "facts-kept-at-targets", + "file": "src/spec.rs", + "find": "let fs0 = if seq_has(tg, here) {", + "replace": "let fs0 = if false && seq_has(tg, here) {", + "expect": "killed", + "reason": "Keeps what is known about registers at a jump target, though the target can be reached without the instructions that established it (J.6: earlier in the same basic block): a jump past a bounds check to the access would be accepted, so lemma_scan_forgets_at_targets is false. (example_jump_over_check_rejected is false too.)", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "lemma_scan_forgets_at_targets(super::examples::prog_jump_over_check(), all_targets(super::examples::prog_jump_over_check(), seq![0int]), 10, cx_checked())", + "witness": "seq_has(all_targets(super::examples::prog_jump_over_check(), seq![0int]), start_of(super::examples::prog_jump_over_check(), 10)) && scan(super::examples::prog_jump_over_check(), all_targets(super::examples::prog_jump_over_check(), seq![0int]), 10, cx_checked()) is Some && scan(super::examples::prog_jump_over_check(), all_targets(super::examples::prog_jump_over_check(), seq![0int]), 10, no_facts()) is None" + } + }, + { + "id": "region-table-writable", + "file": "src/spec.rs", + "find": "!a.store || abi::exit_code_off() <= a.mem.disp", + "replace": "true", + "expect": "killed", + "reason": "Lets a store go anywhere in the context, including the region table: the code could rewrite a region's base or bound, then pass its own bounds check against the forged bound. lemma_ctx_store_in_exit_area is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "lemma_ctx_store_in_exit_area(no_facts(), cx_table_store())", + "witness": "access_ok(no_facts(), cx_table_store())" + } + }, + { + "id": "end-of-blob-not-checked", + "file": "src/spec.rs", + "find": "(ins.last() is Ret || ins.last() is Jmp) && match scan(", + "replace": "true && match scan(", + "expect": "killed", + "reason": "Drops the rule that the last instruction is ret or jmp (J.6: every path ends in the exit sequence or a branch): control could run past the end of the blob into whatever follows. lemma_policy_ends is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "lemma_policy_ends(cx_falls_off(), seq![0int])", + "witness": "policy(cx_falls_off(), seq![0int])" + } + }, + { + "id": "bare-ret-allowed", + "file": "src/spec.rs", + "find": "if i is Ret {", + "replace": "if i is Ret && false {", + "expect": "killed", + "reason": "Accepts a ret that is not the end of an exit sequence (J.6: no indirect branch except the exit sequence): the blob could return without having stored an exit code. lemma_plain_ok_safe is false.", + "certificate": { + "file": "src/lemmas.rs", + "lemma": "lemma_plain_ok_safe(no_facts(), Instr::Ret)", + "witness": "plain_ok(no_facts(), Instr::Ret) is Some" + } + } + ] +} diff --git a/crates/clazel-codecheck/src/acc_lemmas.rs b/crates/clazel-codecheck/src/acc_lemmas.rs new file mode 100644 index 000000000..391d2cde9 --- /dev/null +++ b/crates/clazel-codecheck/src/acc_lemmas.rs @@ -0,0 +1,103 @@ +//! Facts about the spec's scan and policy that the completeness of the +//! validator needs: an accepted blob's targets all lie inside it, and the +//! list `seq_has` and `subset_of` behave as membership and inclusion. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `seq_has` of a concatenation. +pub proof fn lemma_seq_has_concat(a: Seq, b: Seq, x: int) + ensures + seq_has(a + b, x) == (seq_has(a, x) || seq_has(b, x)), + decreases b.len(), +{ + if b.len() == 0 { + assert(a + b =~= a); + } else { + let d = b.drop_last(); + lemma_seq_has_concat(a, d, x); + assert((a + b).drop_last() =~= a + d); + assert((a + b).last() == b.last()); + } +} + +/// A one-element list has just that element. +pub proof fn lemma_seq_has_single(y: int, x: int) + ensures + seq_has(seq![y], x) == (y == x), +{ + reveal_with_fuel(seq_has, 2); + assert(seq![y].drop_last() =~= Seq::::empty()); +} + +/// Membership in a subset implies membership in the superset. +pub proof fn lemma_subset_elim(a: Seq, b: Seq, x: int) + requires + subset_of(a, b), + seq_has(a, x), + ensures + seq_has(b, x), + decreases a.len(), +{ + if a.len() > 0 { + if a.last() != x { + lemma_subset_elim(a.drop_last(), b, x); + } + } +} + +/// Every unit start the scan finds is the start of an instruction at or after +/// `j`. +pub proof fn lemma_scan_starts(ins: Seq, tg: Seq, j: int, fs: Facts, x: int) + requires + 0 <= j, + scan(ins, tg, j, fs) is Some, + seq_has(scan(ins, tg, j, fs)->Some_0, x), + ensures + exists|k: int| j <= k < ins.len() && x == start_of(ins, k), + decreases ins.len() - j, +{ + reveal_with_fuel(scan, 2); + if j < ins.len() { + let here = start_of(ins, j); + let fs0 = if seq_has(tg, here) { + no_facts() + } else { + fs + }; + let u = unit_at(ins, j, fs0)->Some_0; + let rest = scan(ins, tg, j + u.len, u.facts)->Some_0; + lemma_seq_has_concat(seq![here], rest, x); + lemma_seq_has_single(here, x); + if x != here { + lemma_scan_starts(ins, tg, j + u.len, u.facts, x); + } else { + assert(j <= j < ins.len() && x == start_of(ins, j)); + } + } +} + +/// **An accepted blob's targets lie inside it**: every entry and every jump +/// target of an instruction list that passes the policy is the offset of one +/// of its instructions, so in `[0, start_of(ins, len))`. +pub proof fn lemma_policy_in_range(ins: Seq, entries: Seq, x: int) + requires + policy(ins, entries), + seq_has(all_targets(ins, entries), x), + ensures + 0 <= x < start_of(ins, ins.len() as int), +{ + let tg = all_targets(ins, entries); + let starts = scan(ins, tg, 0, no_facts())->Some_0; + lemma_subset_elim(tg, starts, x); + lemma_scan_starts(ins, tg, 0, no_facts(), x); + let k = choose|k: int| 0 <= k < ins.len() && x == start_of(ins, k); + super::scan_exec::lemma_start_of_lt(ins, k, ins.len() as int); + super::validate::lemma_start_of_ge(ins, k); +} + +} // verus! diff --git a/crates/clazel-codecheck/src/api.rs b/crates/clazel-codecheck/src/api.rs new file mode 100644 index 000000000..d5e14d07e --- /dev/null +++ b/crates/clazel-codecheck/src/api.rs @@ -0,0 +1,32 @@ +//! The contract-only API of the validator: a trait with no implementation, +//! so Verus accepts it with no escape hatch (CLAUDE.md rule 4), the pattern +//! of `clazel-x64`'s `api.rs`. The implementation (a later commit of J.6) +//! implements it, or replaces it with a free function whose `requires` and +//! `ensures` are the same predicates. +//! +//! jit.md J.0(a) has a `Validated` value that only this crate can build and +//! `clazel-sys`'s `enter` takes. Building it from a `true` result is a later +//! step, with the implementation: it is a wrapper around a blob for which +//! this function answered yes. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The x86-64 validator, with its contract. +pub trait Validator { + /// Whether `blob`, entered at the byte offsets `entries`, is accepted + /// (see [`crate::spec::accepted`]). Reads the bytes once, left to right, + /// and never panics, whatever the bytes are. + fn validate(blob: &[u8], entries: &[usize]) -> (ok: bool) + requires + crate::contract::validate_requires(blob@, entries@), + ensures + crate::contract::validate_ensures(blob@, entries@, ok), + ; +} + +} // verus! diff --git a/crates/clazel-codecheck/src/contract.rs b/crates/clazel-codecheck/src/contract.rs new file mode 100644 index 000000000..006149308 --- /dev/null +++ b/crates/clazel-codecheck/src/contract.rs @@ -0,0 +1,58 @@ +//! Contract of the validator (CLAUDE.md rule 3, docs/SPEC-STYLE.md): one +//! `requires` and one `ensures` predicate for [`super::api::Validator`], in +//! terms of [`super::spec`]. Reviewing these and the spec is enough to know +//! what an implementation guarantees. +//! +//! What they say (jit.md J.6): +//! +//! - **Soundness.** If the validator says yes, the blob is [`accepted`]: +//! exactly the canonical encodings of subset instructions that pass the +//! policy. This is the direction safety rests on: a `true` the spec does +//! not license would let unchecked code run. +//! - **Completeness, given a prefix code.** If the blob is accepted, the +//! validator says yes, provided `clazel-x64`'s `canonical_prefix_free` +//! holds (a statement about its encoder that is to be proven there). It +//! makes the instruction sequence of a blob unique, so a decoder that reads +//! the first match finds the one the spec quantifies over. Without it only +//! soundness is promised, so a validator proven against this contract is +//! safe whether or not that lemma is ever proven; what it would lack is the +//! guarantee that it refuses nothing the spec accepts. +//! - **Entries** are byte offsets inside the blob; each must be the start of +//! a unit, or the answer is no ([`accepted`] requires it). +//! - **A blob is at most 2^31 - 1 bytes** (`requires`). +//! +//! Not stated here: that an accepted blob is *safe to run*. That is jit.md's +//! theorem (exit criterion 2), over `clazel_x64::spec::step`, and it is not +//! yet written; see [`super::spec`]'s header. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The largest blob the validator takes, in bytes. +pub open spec fn max_blob_len() -> int { + 0x7FFF_FFFF +} + +/// The entries as the spec's integer offsets. +pub open spec fn entry_offsets(entries: Seq) -> Seq { + entries.map_values(|e: usize| e as int) +} + +/// Precondition of the validator: the blob is within the size limit. Every +/// other input, including random bytes and any entries, is legal. +pub open spec fn validate_requires(blob: Seq, entries: Seq) -> bool { + blob.len() <= max_blob_len() +} + +/// Postcondition of the validator. +pub open spec fn validate_ensures(blob: Seq, entries: Seq, ok: bool) -> bool { + &&& ok ==> accepted(blob, entry_offsets(entries)) + &&& clazel_x64::contract::canonical_prefix_free() ==> (accepted(blob, entry_offsets(entries)) + ==> ok) +} + +} // verus! diff --git a/crates/clazel-codecheck/src/decode_all.rs b/crates/clazel-codecheck/src/decode_all.rs new file mode 100644 index 000000000..9fef72b53 --- /dev/null +++ b/crates/clazel-codecheck/src/decode_all.rs @@ -0,0 +1,166 @@ +//! The decoding half of the validator, proven: [`decode_all`] reads a blob +//! into the instruction list the spec quantifies over (`spec::decodes_to`), +//! with each instruction's start offset. Its `Some` result is sound by +//! `clazel-x64`'s proven executable decoder, and it finds **the** +//! decomposition: if the blob decomposes as `w` at all, the answer is `Some` +//! with exactly `w` (by the prefix-freeness of the encoding, proven in +//! `clazel-x64`). + +#[allow(unused_imports)] +use super::spec::*; +use clazel_x64::decoder_exec::decode_exec; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Every instruction's encoding has at least one byte, so decoding makes +/// progress. +pub proof fn lemma_encode_nonempty(i: Instr) + ensures + encode(i).len() >= 1, +{ + reveal_with_fuel(le_bytes, 1); +} + +/// The start offset of an instruction does not depend on later ones. +pub proof fn lemma_start_of_push(ins: Seq, x: Instr, j: int) + requires + 0 <= j <= ins.len(), + ensures + start_of(ins.push(x), j) == start_of(ins, j), + decreases j, +{ + if j > 0 { + lemma_start_of_push(ins, x, j - 1); + assert(ins.push(x)[j - 1] == ins[j - 1]); + } +} + +/// The instruction list of a [`decode_all`] result. +pub open spec fn res_ins(r: Option<(Vec, Vec)>) -> Seq { + match r { + Some((i, _)) => i@, + None => Seq::empty(), + } +} + +/// Decodes the whole blob into instructions and their start offsets. It +/// finds **the** decomposition: if the blob decomposes as `w` at all +/// (`spec::decodes_to`), the answer is `Some` and the instructions are `w`. +#[verifier::spinoff_prover] +#[verifier::rlimit(2000)] +pub fn decode_all(blob: &[u8]) -> (r: Option<(Vec, Vec)>) + requires + blob@.len() < 0x8000_0000, + ensures + match r { + Some((ins, st)) => { + &&& decodes_to(blob@, ins@) + &&& st@.len() == ins@.len() + &&& forall|j: int| 0 <= j < ins@.len() ==> st@[j] as int == start_of(ins@, j) + }, + None => true, + }, + forall|w: Seq| + #[trigger] decodes_to(blob@, w) ==> (r is Some && res_ins(r) =~= w), +{ + let mut ins: Vec = Vec::new(); + let mut st: Vec = Vec::new(); + let mut pos: usize = 0; + while pos < blob.len() + invariant + pos <= blob.len(), + blob@.len() < 0x8000_0000, + all_valid(ins@), + enc_all(ins@) =~= blob@.subrange(0, pos as int), + st@.len() == ins@.len(), + forall|j: int| 0 <= j < ins@.len() ==> st@[j] as int == start_of(ins@, j), + pos as int == start_of(ins@, ins@.len() as int), + forall|w: Seq| + #[trigger] decodes_to(blob@, w) ==> (ins@.len() <= w.len() && w.subrange( + 0, + ins@.len() as int, + ) =~= ins@), + decreases blob.len() - pos, + { + let rest = vstd::slice::slice_subrange(blob, pos, blob.len()); + match decode_exec(rest) { + None => { + proof { + assert forall|w: Seq| decodes_to(blob@, w) implies false by { + let k = ins@.len() as int; + assert(w.subrange(0, k) =~= ins@); + super::uniq::lemma_start_of_prefix(w, k, k); + super::uniq::lemma_next_instr(blob@, w, k, pos as int); + } + } + return None; + }, + Some((c, n)) => { + proof { + lemma_encode_nonempty(c); + assert(rest@.subrange(0, n as int) =~= encode(c)); + } + if n == 0 { + return None; + } + let ghost old_ins = ins@; + ins.push(c); + st.push(pos); + proof { + let c2 = ins@.last(); + assert(ins@ =~= old_ins.push(c2)); + assert(ins@.drop_last() =~= old_ins); + lemma_start_of_push(old_ins, c2, old_ins.len() as int); + assert(start_of(ins@, ins@.len() as int) == start_of(old_ins, old_ins.len() as int) + + instr_len(c2) as int); + assert(enc_all(ins@) =~= blob@.subrange(0, (pos + n) as int)) by { + assert(blob@.subrange(0, (pos + n) as int) =~= blob@.subrange( + 0, + pos as int, + ) + rest@.subrange(0, n as int)); + } + assert forall|j: int| 0 <= j < ins@.len() implies valid_instr( + #[trigger] ins@[j], + ) by { + if j < old_ins.len() { + assert(ins@[j] == old_ins[j]); + } + } + assert forall|j: int| 0 <= j < ins@.len() implies st@[j] as int == start_of( + ins@, + j, + ) by { + lemma_start_of_push(old_ins, c2, j); + } + clazel_x64::decoder_proofs::lemma_canonical_prefix_free(); + assert forall|w: Seq| decodes_to(blob@, w) implies (ins@.len() <= w.len() + && w.subrange(0, ins@.len() as int) =~= ins@) by { + let k = old_ins.len() as int; + assert(w.subrange(0, k) =~= old_ins); + super::uniq::lemma_start_of_prefix(w, k, k); + super::uniq::lemma_next_instr(blob@, w, k, pos as int); + assert(starts_with_encoding(rest@, c2)); + assert(c2 == w[k]); + assert(w.subrange(0, k + 1) =~= old_ins.push(c2)); + } + } + pos = pos.saturating_add(n); + }, + } + } + proof { + assert(blob@.subrange(0, pos as int) =~= blob@); + assert forall|w: Seq| decodes_to(blob@, w) implies res_ins(Some((ins, st))) =~= w by { + let k = ins@.len() as int; + assert(w.subrange(0, k) =~= ins@); + super::uniq::lemma_start_of_prefix(w, k, k); + super::uniq::lemma_finish(blob@, w, k); + assert(w.subrange(0, k) =~= w); + } + } + Some((ins, st)) +} + +} // verus! diff --git a/crates/clazel-codecheck/src/examples.rs b/crates/clazel-codecheck/src/examples.rs new file mode 100644 index 000000000..adbad20f4 --- /dev/null +++ b/crates/clazel-codecheck/src/examples.rs @@ -0,0 +1,323 @@ +//! Examples: small programs, accepted and refused by the spec, proven by +//! evaluating it. They are evidence that [`super::spec`] says what jit.md +//! J.6 means, and the lemmas that `mutations.json` cites in its kill +//! certificates: each refused program is one that a particular bug in the spec +//! would accept. +//! +//! The programs are given as instruction sequences (the level of +//! [`policy`]), because that is where the checks are. The last example +//! ([`example_bytes_decode`]) ties one program to its bytes, with +//! `clazel-x64`'s canonical encoding, so the two levels meet. +//! +//! The jump displacements in the comments are worked out from the instruction +//! lengths of `clazel-x64`'s `encode`: `mov r64, [mem]` and every `r, [mem]` +//! instruction is 8 bytes, `mov r, r` 3, `add r, imm32` 7, `jcc rel32` 6, +//! `mov r32, imm32` 5, `cmovcc` 4, `jmp rel32` 5, `ret` 1; so the bounds +//! check is 47 bytes (its two jumps end 16 and 30 bytes in) and the exit +//! sequence 14. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A context operand: `[r15 + disp]`. +pub open spec fn ctxm(disp: int) -> Mem { + Mem { base: 15, index: None, disp: disp as i32 } +} + +/// The exit sequence with scratch register `t`: `mov t, code`; `mov +/// [r15 + 256], t`; `ret`. +pub open spec fn exit_seq(t: u8, code: u32) -> Seq { + seq![ + Instr::MovImm32 { dst: t, imm_u32: code }, + Instr::MovStore { sz: Size::W64, mem: ctxm(256), src: t }, + Instr::Ret, + ] +} + +/// `mov dst, [r15 + 16k]`: load region `k`'s base. +pub open spec fn base_load(dst: u8, k: int) -> Instr { + Instr::MovLoad { sz: Size::W64, dst: dst, mem: ctxm(16 * k) } +} + +/// `mov dst, [base + idx]`: a 64-bit load from a region. +pub open spec fn region_load(dst: u8, base: u8, idx: u8) -> Instr { + Instr::MovLoad { + sz: Size::W64, + dst: dst, + mem: Mem { base: base, index: Some(Index { reg: idx, scale: 0 }), disp: 0 }, + } +} + +/// A bounds check of region `k`, `n` bytes, index `r1`, scratch `r5` and `r6`, +/// both jumps going `r1`/`r2` bytes on. +pub open spec fn chk(k: int, n: int, r1: i32, r2: i32) -> Chk { + Chk { k: k, n: n, i: 1, t: 5, z: 6, r1: r1, r2: r2 } +} + +/// Load region 3's base into `r2`, check `r1` for 8 bytes, load `[r2 + r1]` +/// into `r0`, exit with code 1. Byte offsets: the base load is at 0, the check +/// at 8 (its jumps end at 24 and 38), the access at 55, the exit at 63: so +/// the jumps are 63 - 24 = 39 and 63 - 38 = 25. +pub open spec fn prog_checked_load(c: Chk) -> Seq { + seq![base_load(2, 3)] + check_seq(c) + seq![region_load(0, 2, 1)] + exit_seq(7, 1) +} + +/// [`prog_checked_load`] with region 3, 8 bytes: the accepted case. +pub open spec fn prog_ok() -> Seq { + prog_checked_load(chk(3, 8, 39, 25)) +} + +/// A checked load is accepted: the base is region 3's, the index was checked +/// for 8 bytes of region 3, and both jumps go to the exit sequence. +pub proof fn example_checked_load_accepted() + ensures + policy(prog_ok(), seq![0int]), +{ + assert(policy(prog_ok(), seq![0int])) by (compute_only); +} + +/// The same load with no check in between is refused: `r1` has no fact. +pub open spec fn prog_unchecked() -> Seq { + seq![base_load(2, 3), region_load(0, 2, 1)] + exit_seq(7, 1) +} + +/// A load whose index was never bounds-checked is refused (J.6: "an index +/// register that was compared against that region's bound"). +pub proof fn example_unchecked_load_rejected() + ensures + !policy(prog_unchecked(), seq![0int]), +{ + assert(!policy(prog_unchecked(), seq![0int])) by (compute_only); +} + +/// A check for 4 bytes licenses no 8-byte load. +pub proof fn example_short_check_rejected() + ensures + !policy(prog_checked_load(chk(3, 4, 39, 25)), seq![0int]), +{ + assert(!policy(prog_checked_load(chk(3, 4, 39, 25)), seq![0int])) by (compute_only); +} + +/// A check against region 2's bound does not license an access through +/// region 3's base. +pub proof fn example_wrong_region_rejected() + ensures + !policy(prog_checked_load(chk(2, 8, 39, 25)), seq![0int]), +{ + assert(!policy(prog_checked_load(chk(2, 8, 39, 25)), seq![0int])) by (compute_only); +} + +/// `mov rsp, rax` then an exit sequence: refused, the stack pointer is +/// reserved. +pub open spec fn prog_write_rsp() -> Seq { + seq![Instr::MovRR { sz: Size::W64, dst: 4, src: 0 }] + exit_seq(7, 1) +} + +/// A write to the stack pointer is refused (J.6: "reserved registers and the +/// stack pointer are written only in the fixed prologue and epilogue", which +/// are empty here). +pub proof fn example_write_rsp_rejected() + ensures + !policy(prog_write_rsp(), seq![0int]), +{ + assert(!policy(prog_write_rsp(), seq![0int])) by (compute_only); +} + +/// `mov r15d, 0` then an exit sequence. +pub open spec fn prog_write_ctx() -> Seq { + seq![Instr::MovImm32 { dst: 15, imm_u32: 0 }] + exit_seq(7, 1) +} + +/// A write to the context register is refused. +pub proof fn example_write_ctx_rejected() + ensures + !policy(prog_write_ctx(), seq![0int]), +{ + assert(!policy(prog_write_ctx(), seq![0int])) by (compute_only); +} + +/// `jmp` into the second instruction of a bounds check, which would skip the +/// first. `jmp +3` is at 0 (5 bytes) and the check at 5, whose second +/// instruction is at 8; the check's jumps end at 21 and 35 and go to the exit +/// sequence at 52. +pub open spec fn prog_jump_into_check() -> Seq { + seq![Instr::Jmp { rel: 3 }] + check_seq(chk(3, 8, 31, 17)) + exit_seq(7, 1) +} + +/// A jump target that is not the start of a unit is refused. +pub proof fn example_jump_into_check_rejected() + ensures + !policy(prog_jump_into_check(), seq![0int]), +{ + assert(!policy(prog_jump_into_check(), seq![0int])) by (compute_only); +} + +/// A jump from before the check to the access after it: `jmp` at 0, the base +/// load at 5, the check at 13 (its jumps end at 29 and 43), the access at 60 +/// and the exit at 68. +pub open spec fn prog_jump_over_check() -> Seq { + seq![Instr::Jmp { rel: 55 }, base_load(2, 3)] + check_seq(chk(3, 8, 39, 25)) + seq![ + region_load(0, 2, 1), + ] + exit_seq(7, 1) +} + +/// The access is a jump target, so what the check established is forgotten +/// there: it could be reached without the check. Refused. +pub proof fn example_jump_over_check_rejected() + ensures + !policy(prog_jump_over_check(), seq![0int]), +{ + assert(!policy(prog_jump_over_check(), seq![0int])) by (compute_only); +} + +/// A store to the region table of the context, `mov [r15 + 0], rax`. +pub open spec fn prog_store_table() -> Seq { + seq![Instr::MovStore { sz: Size::W64, mem: ctxm(0), src: 0 }] + exit_seq(7, 1) +} + +/// The region table is never written (a store to the context must be in the +/// exit area), or code could widen its own regions. +pub proof fn example_store_table_rejected() + ensures + !policy(prog_store_table(), seq![0int]), +{ + assert(!policy(prog_store_table(), seq![0int])) by (compute_only); +} + +/// `mov [r15 + 264], rax` (an exit operand) then the exit sequence. +pub open spec fn prog_store_operand() -> Seq { + seq![Instr::MovStore { sz: Size::W64, mem: ctxm(264), src: 0 }] + exit_seq(7, 1) +} + +/// A store to an exit operand is accepted. +pub proof fn example_store_operand_accepted() + ensures + policy(prog_store_operand(), seq![0int]), +{ + assert(policy(prog_store_operand(), seq![0int])) by (compute_only); +} + +/// A bare `add rax, rbx`: nothing after it, control runs off the end. +pub open spec fn prog_falls_off() -> Seq { + seq![Instr::AluRR { alu: AluOp::Add, sz: Size::W64, dst: 0, src: 3 }] +} + +/// A blob that can run past its end is refused (J.6: "every path ends in the +/// exit sequence or a branch"). +pub proof fn example_falls_off_rejected() + ensures + !policy(prog_falls_off(), seq![0int]), +{ + assert(!policy(prog_falls_off(), seq![0int])) by (compute_only); +} + +/// A `ret` that is not the end of an exit sequence. +pub open spec fn prog_bare_ret() -> Seq { + seq![Instr::Ret] +} + +/// `ret` outside the exit sequence is refused (J.6: "no indirect branch +/// except the exit sequence"). +pub proof fn example_bare_ret_rejected() + ensures + !policy(prog_bare_ret(), seq![0int]), +{ + assert(!policy(prog_bare_ret(), seq![0int])) by (compute_only); +} + +/// A loop: `add rax, rbx; jmp -8` (the add is 3 bytes and the jump 5, so the +/// next instruction is at 8, and the target is the add at 0: 0 - 8 = -8). +pub open spec fn prog_loop() -> Seq { + seq![ + Instr::AluRR { alu: AluOp::Add, sz: Size::W64, dst: 0, src: 3 }, + Instr::Jmp { rel: -8i32 }, + ] +} + +/// A backward jump to the first instruction is accepted, and the blob ends in +/// a `jmp`. +pub proof fn example_loop_accepted() + ensures + policy(prog_loop(), seq![0int]), +{ + assert(policy(prog_loop(), seq![0int])) by (compute_only); +} + +/// An entry in the middle of a unit is refused: the same program as +/// [`prog_ok`], entered at 9 (inside the bounds check). +pub proof fn example_entry_inside_unit_rejected() + ensures + !policy(prog_ok(), seq![9int]), +{ + assert(!policy(prog_ok(), seq![9int])) by (compute_only); +} + +/// A second entry at a unit start is accepted: the exit sequence at 63. +pub proof fn example_second_entry_accepted() + ensures + policy(prog_ok(), seq![0int, 63int]), +{ + assert(policy(prog_ok(), seq![0int, 63int])) by (compute_only); +} + +/// A jump out of the blob: `jmp +100` from a one-instruction blob. +pub proof fn example_jump_out_rejected() + ensures + !policy(seq![Instr::Jmp { rel: 100 }], seq![0int]), +{ + assert(!policy(seq![Instr::Jmp { rel: 100 }], seq![0int])) by (compute_only); +} +/// The exit sequence alone is accepted, entered at 0. +pub proof fn example_exit_accepted() + ensures + policy(exit_seq(0, 7), seq![0int]), +{ + assert(policy(exit_seq(0, 7), seq![0int])) by (compute_only); +} + +/// The three instructions of an exit sequence are in the subset. +pub proof fn example_exit_valid() + ensures + all_valid(exit_seq(0, 7)), +{ + assert(valid_instr(exit_seq(0, 7)[0]) && valid_instr(exit_seq(0, 7)[1]) && valid_instr( + exit_seq(0, 7)[2], + )) by (compute_only); + assert forall|j: int| 0 <= j < exit_seq(0, 7).len() implies valid_instr( + #[trigger] exit_seq(0, 7)[j], + ) by { + assert(j == 0 || j == 1 || j == 2); + } +} + +/// The exit sequence with `r0` as scratch, as bytes: `mov eax, 7` (`B8 07 00 +/// 00 00`), `mov [r15 + 256], rax` (`49 89 84 27 00 01 00 00`), `ret` (`C3`). +pub proof fn example_bytes_decode() + ensures + enc_all(exit_seq(0, 7)) =~= seq![ + 0xB8u8, 0x07, 0x00, 0x00, 0x00, 0x49, 0x89, 0x84, 0x27, 0x00, 0x01, 0x00, 0x00, 0xC3, + ], + accepted( + seq![ + 0xB8u8, 0x07, 0x00, 0x00, 0x00, 0x49, 0x89, 0x84, 0x27, 0x00, 0x01, 0x00, 0x00, + 0xC3, + ], + seq![0int], + ), +{ + reveal_with_fuel(le_bytes, 5); + reveal_with_fuel(enc_all, 4); + example_exit_accepted(); + example_exit_valid(); + let blob = seq![ + 0xB8u8, 0x07, 0x00, 0x00, 0x00, 0x49, 0x89, 0x84, 0x27, 0x00, 0x01, 0x00, 0x00, 0xC3, + ]; + assert(enc_all(exit_seq(0, 7)) =~= blob); + assert(decodes_to(blob, exit_seq(0, 7))); +} + +} // verus! diff --git a/crates/clazel-codecheck/src/lemmas.rs b/crates/clazel-codecheck/src/lemmas.rs new file mode 100644 index 000000000..7615aec1b --- /dev/null +++ b/crates/clazel-codecheck/src/lemmas.rs @@ -0,0 +1,191 @@ +//! Facts proven about [`super::spec`] (docs/SPEC-STYLE.md: "prove at least one +//! property of the spec"). They are evidence that the transcription says what +//! jit.md J.6 means, not part of what the validator must meet: each one +//! restates a rule of J.6 in a form that does not mention how the spec +//! computes it (the reserved registers are the numbers 4 and 15, not +//! `abi::reserved`; the exit area is the bytes 256 to 511), and the proof is +//! that the spec implies it. +//! +//! They are also what the kill certificates of `mutations.json` cite. A +//! lemma is general (every facts list, every instruction), so a spec bug that +//! breaks it has a counterexample, and a certificate evaluates the spec on that +//! one. The examples in [`super::examples`] are kept apart, so that a mutant +//! that breaks one of them does not stop Verus before it checks a certificate +//! here. +//! +//! The concrete data for the certificates are the `cx_*` spec functions at the +//! end of this file. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The register of a memory operand's index (0 if it has none). +pub open spec fn idx_reg(m: Mem) -> u8 { + match m.index { + Some(ix) => ix.reg, + None => 0, + } +} + +/// The fact is a bounds check that admits at least `size` bytes. +pub open spec fn idx_covers(f: Fact, size: int) -> bool { + match f { + Fact::Idx(_, n) => size <= n, + _ => false, + } +} + +/// The two facts name one region: a base of region `k`, an index checked +/// against region `k`. +pub open spec fn same_region(base: Fact, idx: Fact) -> bool { + match (base, idx) { + (Fact::Base(k), Fact::Idx(k2, _)) => k == k2, + _ => false, + } +} + +/// **A region access is a checked access** (J.6: "every load and store uses a +/// region base register plus an index register that was compared against that +/// region's bound"). If an access not through the context is permitted, its +/// index register is known to be bounds-checked ([`Fact::Idx`]), for at least +/// as many bytes as the access touches, against the same region as the base +/// register, which is known to hold that region's base; and there is no +/// displacement. +pub proof fn lemma_region_access_facts(fs: Facts, a: Access) + requires + access_ok(fs, a), + a.mem.base != 15, + ensures + a.mem.disp == 0, + fact_of(fs, idx_reg(a.mem)) is Idx, + idx_covers(fact_of(fs, idx_reg(a.mem)), a.size), + same_region(fact_of(fs, a.mem.base), fact_of(fs, idx_reg(a.mem))), +{ +} + +/// **The region table is never written** (J.6, and jit.md: "a fixed table of +/// (base, bound) pairs"): a store through the context register lies in the +/// exit area, bytes 256 to 511 of the 512-byte context. +pub proof fn lemma_ctx_store_in_exit_area(fs: Facts, a: Access) + requires + access_ok(fs, a), + a.mem.base == 15, + a.store, + ensures + 256 <= a.mem.disp, + a.mem.disp + a.size <= 512, +{ +} + +/// **No other instruction is a write to a reserved register or a `ret`** (J.6: +/// "reserved registers and the stack pointer are written only in the fixed +/// prologue and epilogue", empty here; "no indirect branch except the exit +/// sequence"): an instruction the scan takes alone never writes `rsp` (4) or +/// `r15` (15), and is not `ret`. +pub proof fn lemma_plain_ok_safe(fs: Facts, i: Instr) + requires + plain_ok(fs, i) is Some, + ensures + !(i is Ret), + written_gpr(i) != Some(4u8), + written_gpr(i) != Some(15u8), +{ +} + +/// **No blob runs off its end** (J.6: "every path ends in the exit sequence +/// or a branch"): an accepted instruction list is not empty, and ends in +/// `ret` (which only an exit sequence has) or `jmp`. +pub proof fn lemma_policy_ends(ins: Seq, entries: Seq) + requires + policy(ins, entries), + ensures + ins.len() > 0, + ins.last() is Ret || ins.last() is Jmp, +{ +} + +/// The offsets at which the scan found units begin (empty if it refused one). +pub open spec fn unit_starts(ins: Seq, entries: Seq) -> Seq { + match scan(ins, all_targets(ins, entries), 0, no_facts()) { + Some(starts) => starts, + None => Seq::empty(), + } +} + +/// **Every direct target and every entry is the start of a unit** (J.6: +/// "every direct target is a decoded boundary inside the blob"; stronger here, +/// because a jump into the middle of a recognized sequence would skip part of +/// it). A unit starts at an instruction boundary of the blob, so a target that +/// is a unit start is a boundary inside the blob. +pub proof fn lemma_policy_targets_are_unit_starts(ins: Seq, entries: Seq) + requires + policy(ins, entries), + ensures + subset_of(all_targets(ins, entries), unit_starts(ins, entries)), +{ +} + +/// **What is known about registers does not flow into a jump target** (J.6: +/// the check is "earlier in the same basic block"): the scan from an +/// instruction whose offset is a target gives the same result whatever was +/// known before it. +pub proof fn lemma_scan_forgets_at_targets(ins: Seq, tg: Seq, j: int, fs: Facts) + requires + 0 <= j < ins.len(), + seq_has(tg, start_of(ins, j)), + ensures + scan(ins, tg, j, fs) == scan(ins, tg, j, no_facts()), +{ +} + +// ---- Data for the certificates ---- + +/// `r2` is region 3's base and `r1` is checked for 8 bytes of region 3. +pub open spec fn cx_checked() -> Facts { + set_fact(cx_base2(), 1, Fact::Idx(3, 8)) +} + +/// `r2` is region 3's base. +pub open spec fn cx_base2() -> Facts { + set_fact(no_facts(), 2, Fact::Base(3)) +} + +/// `r2` is region 3's base, `r1` is checked for 4 bytes of region 3. +pub open spec fn cx_idx4() -> Facts { + set_fact(cx_base2(), 1, Fact::Idx(3, 4)) +} + +/// `r2` is region 3's base, `r1` is checked for 8 bytes of region 2. +pub open spec fn cx_idx_region2() -> Facts { + set_fact(cx_base2(), 1, Fact::Idx(2, 8)) +} + +/// An 8-byte load `[r2 + r1]`. +pub open spec fn cx_load() -> Access { + Access { + mem: Mem { base: 2, index: Some(Index { reg: 1, scale: 0 }), disp: 0 }, + size: 8, + store: false, + } +} + +/// An 8-byte store to the region table, `[r15 + 0]`. +pub open spec fn cx_table_store() -> Access { + Access { mem: Mem { base: 15, index: None, disp: 0 }, size: 8, store: true } +} + +/// `mov rsp, rax`. +pub open spec fn cx_mov_rsp() -> Instr { + Instr::MovRR { sz: Size::W64, dst: 4, src: 0 } +} + +/// A one-instruction list that is not an exit: `add rax, rbx`. +pub open spec fn cx_falls_off() -> Seq { + seq![Instr::AluRR { alu: AluOp::Add, sz: Size::W64, dst: 0, src: 3 }] +} + +} // verus! diff --git a/crates/clazel-codecheck/src/lib.rs b/crates/clazel-codecheck/src/lib.rs new file mode 100644 index 000000000..b7918a1b0 --- /dev/null +++ b/crates/clazel-codecheck/src/lib.rs @@ -0,0 +1,53 @@ +//! The JIT's code validators (docs/phases/jit.md item J.6), the x86-64 one +//! first. The A64 validator and the second, independent x86-64 validator are +//! 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 +//! validator (CLAUDE.md rule 3). +//! +//! - [`spec`]: **what an accepted blob is.** The checks of jit.md J.6 as spec +//! functions over the instruction subset of `clazel-x64`. Read its header +//! first: it lists the modelling choices and the ABI the validator fixes +//! (the context register, the region table, the bounds-check and exit +//! sequences). +//! - [`contract`]: what the validator must be proven to satisfy. +//! - [`api`]: the contract-only trait [`api::Validator`] (no implementation). +//! - [`examples`]: instruction sequences (and one byte sequence) that are +//! accepted and refused, proven by evaluating the spec. +//! - [`lemmas`]: general facts proven about the spec (a region access is a +//! checked access, the region table is never written, ...). They are what +//! the kill certificates of `mutations.json` cite. +//! +//! The decoder of untrusted bytes will be this crate's own (CLAUDE.md rule +//! 8); the only thing shared with `clazel-x64` is its specification of the +//! subset and its canonical encoding. +//! +//! # Status +//! +//! Tier 0. [`validate::validate`] is the executable validator, written in +//! `verus!` and proven **sound and complete**: it returns `true` if and only +//! if the blob is `spec::accepted`, which is `contract::validate_ensures`. It +//! is built from verified parts: `clazel-x64`'s executable decoder and +//! encoder, [`decode_all`] (which finds the unique decomposition), [`unit_exec`] +//! (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 not stated or proven here; see the end of [`spec`]'s +//! header. +//! +//! Tier: see `[package.metadata.clazel]` in `Cargo.toml` and +//! `docs/VERIFICATION.md`. + +pub mod acc_lemmas; +pub mod api; +pub mod contract; +pub mod decode_all; +pub mod examples; +pub mod lemmas; +pub mod scan_exec; +pub mod spec; +pub mod uniq; +pub mod unit_exec; +pub mod validate; diff --git a/crates/clazel-codecheck/src/scan_exec.rs b/crates/clazel-codecheck/src/scan_exec.rs new file mode 100644 index 000000000..9c39d122a --- /dev/null +++ b/crates/clazel-codecheck/src/scan_exec.rs @@ -0,0 +1,302 @@ +//! The executable unit scan, proven to refine the spec's recursive +//! [`crate::spec::scan`]: [`scan_exec`] walks the instructions with an exec +//! array of register facts, and either finds that the spec scan refuses a +//! unit (`None`) or returns, for every byte offset, whether a unit starts +//! there (`Some`), which is membership in the spec's list of unit starts. + +#[allow(unused_imports)] +use super::spec::*; +pub use super::unit_exec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Instruction start offsets increase: `start_of(ins, j) < start_of(ins, k)` +/// for `j < k <= len`. +pub proof fn lemma_start_of_lt(ins: Seq, j: int, k: int) + requires + 0 <= j < k <= ins.len(), + ensures + start_of(ins, j) < start_of(ins, k), + decreases k, +{ + super::decode_all::lemma_encode_nonempty(ins[k - 1]); + if j < k - 1 { + lemma_start_of_lt(ins, j, k - 1); + } +} + +/// One step of the scan: after taking the unit at `j`, the whole scan is +/// the accumulated starts followed by the scan from the next unit. +pub proof fn lemma_scan_step( + ins: Seq, + tg: Seq, + j: int, + fs: Facts, + fs0: Facts, + acc: Seq, + ulen: int, + ufacts: Facts, +) + requires + 0 <= j < ins.len(), + scan(ins, tg, 0, no_facts()) == (match scan(ins, tg, j, fs) { + None => None::>, + Some(rest) => Some(acc + rest), + }), + fs0 == (if seq_has(tg, start_of(ins, j)) { + no_facts() + } else { + fs + }), + unit_at(ins, j, fs0) == Some(Unit { len: ulen, facts: ufacts }), + ulen >= 1, + j + ulen <= ins.len(), + ensures + scan(ins, tg, 0, no_facts()) == (match scan(ins, tg, j + ulen, ufacts) { + None => None::>, + Some(rest) => Some(acc.push(start_of(ins, j)) + rest), + }), +{ + reveal_with_fuel(scan, 2); + match scan(ins, tg, j + ulen, ufacts) { + None => {}, + Some(rest) => { + assert(acc + (seq![start_of(ins, j)] + rest) =~= acc.push(start_of(ins, j)) + rest); + }, + } +} + +/// A unit that the spec refuses makes the whole scan refuse. +pub proof fn lemma_scan_refuses( + ins: Seq, + tg: Seq, + j: int, + fs: Facts, + fs0: Facts, + acc: Seq, +) + requires + 0 <= j < ins.len(), + scan(ins, tg, 0, no_facts()) == (match scan(ins, tg, j, fs) { + None => None::>, + Some(rest) => Some(acc + rest), + }), + fs0 == (if seq_has(tg, start_of(ins, j)) { + no_facts() + } else { + fs + }), + unit_at(ins, j, fs0) is None, + ensures + scan(ins, tg, 0, no_facts()) is None, +{ + reveal_with_fuel(scan, 2); +} + +/// Resets what is known about every register. +pub fn reset_facts(fa: &mut Vec) + requires + old(fa)@.len() == 16, + ensures + rel(final(fa)@, no_facts()), +{ + let mut k: usize = 0; + while k < 16 + invariant + k <= 16, + fa@.len() == 16, + forall|r: int| 0 <= r < k ==> fa@[r] == FactE::Unknown, + decreases 16 - k, + { + fa.set(k, FactE::Unknown); + k = k.saturating_add(1); + } + assert forall|r: int| 0 <= r < 16 implies fact_view(#[trigger] fa@[r]) == fact_of( + no_facts(), + r as u8, + ) by {} +} + +/// The scan of the spec, executed. `target[p]` says whether offset `p` is a +/// jump target or an entry (`seq_has(tg, p)`); the result says which offsets +/// start a unit. +#[verifier::spinoff_prover] +#[verifier::rlimit(4000)] +pub fn scan_exec( + ins: &[Instr], + st: &[usize], + target: &[bool], + Ghost(tg): Ghost>, + total: usize, +) -> (r: Option>) + requires + ins@.len() == st@.len(), + ins@.len() < 0x8000_0000, + total < 0x8000_0000, + total as int == start_of(ins@, ins@.len() as int), + forall|j: int| 0 <= j < ins@.len() ==> st@[j] as int == start_of(ins@, j), + all_valid(ins@), + target@.len() == total, + forall|p: int| 0 <= p < total ==> target@[p] == seq_has(tg, p), + ensures + match r { + Some(us) => { + &&& scan(ins@, tg, 0, no_facts()) is Some + &&& us@.len() == total + &&& forall|p: int| + 0 <= p < total ==> us@[p] == seq_has(scan(ins@, tg, 0, no_facts())->Some_0, p) + }, + None => scan(ins@, tg, 0, no_facts()) is None, + }, +{ + let n = ins.len(); + let mut fa: Vec = vec![FactE::Unknown; 16]; + let mut us: Vec = vec![false; total]; + let mut j: usize = 0; + let ghost mut fs: Facts = no_facts(); + let ghost mut acc: Seq = Seq::empty(); + proof { + assert forall|r: int| 0 <= r < 16 implies fact_view(#[trigger] fa@[r]) == fact_of( + no_facts(), + r as u8, + ) by {} + } + while j < n + invariant + n == ins@.len(), + j <= n, + ins@.len() == st@.len(), + ins@.len() < 0x8000_0000, + total < 0x8000_0000, + total as int == start_of(ins@, ins@.len() as int), + forall|q: int| 0 <= q < ins@.len() ==> st@[q] as int == start_of(ins@, q), + all_valid(ins@), + target@.len() == total, + forall|p: int| 0 <= p < total ==> target@[p] == seq_has(tg, p), + rel(fa@, fs), + us@.len() == total, + forall|p: int| 0 <= p < total ==> us@[p] == seq_has(acc, p), + scan(ins@, tg, 0, no_facts()) == (match scan(ins@, tg, j as int, fs) { + None => None::>, + Some(rest) => Some(acc + rest), + }), + decreases n - j, + { + let here: usize = match st.get(j) { + Some(h) => *h, + None => { + return None; + }, + }; + proof { + lemma_start_of_lt(ins@, j as int, n as int); + } + let is_target = match target.get(here) { + Some(t) => *t, + None => false, + }; + let ghost fs0 = if seq_has(tg, here as int) { + no_facts() + } else { + fs + }; + if is_target { + reset_facts(&mut fa); + } + proof { + assert(rel(fa@, fs0)); + } + us.set(here, true); + let ghost acc2 = acc.push(here as int); + proof { + assert forall|p: int| 0 <= p < total implies us@[p] == seq_has(acc2, p) by { + assert(acc2.drop_last() =~= acc); + assert(acc2.last() == here as int); + reveal_with_fuel(seq_has, 2); + assert(seq_has(acc2, p) == (p == here as int || seq_has(acc, p))); + if p == here as int { + assert(us@[p] == true); + } else { + assert(us@[p] == seq_has(acc, p)); + } + } + assert(here as int == start_of(ins@, j as int)); + } + if exit_at_e(ins, j) { + reset_facts(&mut fa); + proof { + assert(unit_at(ins@, j as int, fs0) == Some(Unit { len: 3, facts: no_facts() })); + lemma_scan_step(ins@, tg, j as int, fs, fs0, acc, 3, no_facts()); + fs = no_facts(); + acc = acc2; + } + j = j.saturating_add(3); + } else { + match check_at_e(ins, j) { + Some(c) => { + proof { + assert(valid_instr(ins@[j as int])); + assert(valid_instr(ins@[j as int + 1])); + } + fa.set(c.t as usize, FactE::Unknown); + fa.set(c.z as usize, FactE::Unknown); + fa.set(c.i as usize, FactE::Idx(c.k, c.n)); + proof { + let p = check_params(ins@, j as int)->Some_0; + let f1 = set_fact(fs0, c.t, Fact::Unknown); + let f2 = set_fact(f1, c.z, Fact::Unknown); + let f3 = set_fact(f2, c.i, Fact::Idx(c.k as int, c.n as int)); + assert(check_facts(fs0, ins@, j as int) == f3); + assert(unit_at(ins@, j as int, fs0) == Some(Unit { len: 8, facts: f3 })); + assert forall|r: int| 0 <= r < 16 implies fact_view(#[trigger] fa@[r]) + == fact_of(f3, r as u8) by { + lemma_fact_of_set(fs0, c.t, Fact::Unknown, r as u8); + lemma_fact_of_set(f1, c.z, Fact::Unknown, r as u8); + lemma_fact_of_set(f2, c.i, Fact::Idx(c.k as int, c.n as int), r as u8); + } + lemma_scan_step(ins@, tg, j as int, fs, fs0, acc, 8, f3); + fs = f3; + acc = acc2; + } + j = j.saturating_add(8); + }, + None => { + let i = match ins.get(j) { + Some(i) => i, + None => { + return None; + }, + }; + proof { + assert(valid_instr(*i)); + } + let ok = plain_ok_e(&mut fa, Ghost(fs0), i); + if !ok { + proof { + assert(unit_at(ins@, j as int, fs0) is None); + lemma_scan_refuses(ins@, tg, j as int, fs, fs0, acc); + } + return None; + } + proof { + let f = plain_ok(fs0, ins@[j as int])->Some_0; + assert(unit_at(ins@, j as int, fs0) == Some(Unit { len: 1, facts: f })); + lemma_scan_step(ins@, tg, j as int, fs, fs0, acc, 1, f); + fs = f; + acc = acc2; + } + j = j.saturating_add(1); + }, + } + } + } + proof { + assert(scan(ins@, tg, j as int, fs) == Some(Seq::::empty())); + assert(acc + Seq::::empty() =~= acc); + } + Some(us) +} + +} // verus! diff --git a/crates/clazel-codecheck/src/spec.rs b/crates/clazel-codecheck/src/spec.rs new file mode 100644 index 000000000..4213e9936 --- /dev/null +++ b/crates/clazel-codecheck/src/spec.rs @@ -0,0 +1,795 @@ +//! The specification of the **x86-64 validator** (jit.md item J.6): which +//! blobs of machine code are *accepted*, written as spec functions over the +//! instruction subset of `clazel-x64`. Nothing here runs. +//! +//! # Source +//! +//! There is no standard to transcribe. The sources are: +//! +//! - **jit.md, "J.6 `clazel-codecheck`: the validators"**, which lists the +//! checks, and its "Design choices that keep WebAssembly in mind" (regions, +//! a context register, no indirect branches, exits, immutable code). Each +//! rule below names the bullet it implements, as `J.6/`. +//! - **`clazel-x64`'s `spec`**: the instruction subset ([`Instr`]), its +//! canonical encoding ([`encode`]) and its semantics ([`step`]). *What the +//! bytes mean* is that crate's business; this crate says which sequences of +//! its instructions may be run. The Intel SDM is not read here: x86-64 +//! encoding is not re-specified, it is `clazel-x64`'s `encode`. +//! +//! # What the validator accepts, in one paragraph +//! +//! A blob is **accepted** when it is exactly the canonical encodings of a +//! sequence of instructions of the subset, one after the other +//! ([`decodes_to`]), and that sequence passes [`policy`]. The policy groups +//! the instructions into **units** (an exit sequence of 3, a bounds-check +//! sequence of 8, or any other single instruction) with one left-to-right +//! scan ([`scan`]) that tracks what is known about each register, and: +//! +//! 1. every load and store is either to the **context** (through the +//! context register, at a fixed offset) or to a **region**, through a base +//! register loaded from the context's region table and an index register +//! that a bounds-check unit checked and masked, with no other change to it +//! since (`J.6/every load and store ...`); +//! 2. the context register and the stack pointer are never written +//! (`J.6/reserved registers ...`); +//! 3. every `jcc` and `jmp` target, and every entry, is the first byte of a +//! unit, so inside the blob and on an instruction boundary (`J.6/every +//! direct target ...`), and what is known about registers is forgotten at +//! each such place; +//! 4. `ret` occurs only as the last instruction of an **exit sequence** +//! (`J.6/no indirect branch except the exit sequence`); +//! 5. the last instruction is `ret` or `jmp`, so control never runs past the +//! end (`J.6/every path ends in the exit sequence or a branch`); +//! 6. there is no system instruction (`J.6/no system instruction`): the +//! subset has none, and decoding accepts only subset encodings. +//! +//! # Modelling choices +//! +//! Each is a difference of form that does not change the meaning, or a +//! decision jit.md leaves open and this file makes. The ABI choices (the +//! second group) are *ours*: jit.md says "a context register" and "fixed +//! recognizable sequences" without fixing them, so the back end (J.9) must +//! emit exactly these. They are collected in [`abi`], so one place changes if +//! they change. +//! +//! Form: +//! +//! - **Decoding is a relation, not a function.** [`decodes_to`] says the +//! blob is the concatenation of canonical encodings; [`accepted`] +//! quantifies over the instruction sequence. `clazel-x64`'s +//! `canonical_prefix_free` (stated there, to be proven) says there is at +//! most one such sequence, which makes a decoder deterministic. This crate +//! does not rely on it for *soundness* (a blob is accepted if *some* +//! decoding passes the policy), only for the validator being *complete* +//! ([`crate::contract::validate_ensures`]). +//! - **Positions are byte offsets** from the start of the blob, as `int`s. +//! A `jcc`/`jmp` target is the offset of the next instruction plus the +//! signed displacement (Intel SDM vol. 2, `Jcc`: "relative to the end of +//! the instruction"), which is how [`jump_targets`] computes it. +//! - **"Earlier in the same basic block" is a scan with forgetting.** The +//! register facts ([`Fact`]) hold from the instruction that establishes +//! them until the register is written, and are dropped at every unit +//! start that is a jump target or an entry, and after `jmp` and `ret`. +//! That is conservative: a fact is kept only where every way of arriving +//! has it, because arriving by a jump or an entry is the only way not to +//! arrive by falling through from the previous unit. +//! - **The targets are computed first, from all instructions**, and the scan +//! uses them; then every target must be a unit start. (Whether a unit starts +//! at a position depends on the scan, and the scan on the targets; the +//! order here is a fixpoint-free reading: targets are a function of the +//! instruction list only.) +//! - **A recognized sequence is a unit.** The 3-instruction exit sequence and +//! the 8-instruction bounds check are matched wherever they occur +//! ([`exit_unit_at`], [`check_unit_at`]). A position strictly inside a unit +//! is not a unit start, so no jump or entry may land there: a jump into the +//! middle of a check would skip it, and the targets rule refuses it. The +//! same bytes that begin a check are always taken as one. +//! +//! ABI (jit.md leaves these open; see [`abi`]): +//! +//! - **The context** is a 512-byte block, reached only through `r15` +//! ([`abi::ctx_reg`]): a table of 16 regions, `base` at offset `16k` and +//! `bound` at `16k + 8`, in bytes `0..256`, which the code may read but +//! never write, then an exit area of bytes `256..512` (the exit code at +//! 256, the operands after it), which the code may read and write. +//! jit.md: "a context register pointing at a fixed table of (base, bound) +//! pairs", and an exit "stores an exit code and its operands in the +//! context". +//! - **A region access** is `[base + index]` (scale 1, displacement 0, a +//! size of at most 8 bytes). The base register was loaded with a 64-bit +//! load of `[r15 + 16k]` ([`Fact::Base`]); the index register holds an `i` +//! with `i + n <= bound(k)` ([`Fact::Idx`]), established by the **bounds +//! check sequence** ([`check_instr`]) of 8 instructions, which also masks +//! (`J.6/... compared against that region's bound, and masked`; jit.md: +//! "Indexes are masked after the check"; LIR's `masked(bound, i)`). The +//! addition is checked for carry, so `i + n` cannot wrap. +//! - **The exit sequence** is `mov t, code` (32-bit immediate), `mov +//! [r15 + 256], t` (64-bit store), `ret`. The code is the exit code; the +//! operands were stored before by ordinary stores to the exit area. The +//! enter stub (`clazel-sys`, J.7) is the code that `ret` returns to. The +//! validator accepts a `ret` in no other place, so there is no prologue or +//! epilogue: the blob never touches `rsp`, and the stub does all the ABI +//! work (jit.md: "the enter stub is the only code that meets the platform +//! ABI"), so the plan's "fixed prologue and epilogue" is empty in v1. +//! - **Entries** are given to the validator as byte offsets (jit.md J.1: +//! re-entry "at a recorded resume point"), each a unit start. A blob with +//! one entry at 0 is the common case. +//! - **Calls, pushes and pops are not in the subset**, so the native stack +//! depth of an entry is constant (jit.md: "the native stack depth of an +//! entry is a constant the validator checks"): it is 0 beyond what the stub +//! pushed, because nothing in the subset reads or writes `rsp`. +//! - **A blob is at most 2^31 - 1 bytes** (a `requires` of the validator): a +//! `rel32` cannot reach further, and the code cache is bounded far below. +//! +//! # What is not here +//! +//! - **The safety theorem of jit.md exit criterion 2** (every execution of an +//! accepted blob stays in its regions, leaves through the exit, and so on) +//! is *not stated or proven here*. It needs the machine-level invariant over +//! `clazel_x64::spec::step` and the region layout; this file defines +//! *acceptance* only, so that the theorem has something to be about. +//! - **Termination.** A blob may loop for ever (a backward `jmp` is a direct +//! branch to a unit start). jit.md names no preemption mechanism (see the +//! report in the pull request); nothing here rules a loop out. +//! - **The second validator** (a dataflow analysis, jit.md J.6) and the +//! A64 validator. + +/// The instruction subset's vocabulary, re-exported so that this crate's +/// modules (and its users) read one namespace: [`Instr`], [`encode`], +/// [`valid_instr`] and the rest are `clazel-x64`'s. +pub use clazel_x64::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +// ============================================================================ +// Section: abi +// ============================================================================ +// +// The fixed choices: which register is the context, where things are in it. +// (Modelling choices, second group, above.) + +/// The ABI constants. Spec functions, so a witness may evaluate them. +pub mod abi { + #[allow(unused_imports)] + use vstd::prelude::*; + + /// The context register: `r15`, number 15. Reserved: never written. + pub open spec fn ctx_reg() -> u8 { + 15 + } + + /// The stack pointer: `rsp`, number 4. Reserved: never written. + pub open spec fn sp_reg() -> u8 { + 4 + } + + /// A register the code may not write. + pub open spec fn reserved(r: u8) -> bool { + r == ctx_reg() || r == sp_reg() + } + + /// The number of regions in the context's table. + pub open spec fn num_regions() -> int { + 16 + } + + /// The offset in the context of region `k`'s base address (8 bytes). + pub open spec fn base_off(k: int) -> int { + 16 * k + } + + /// The offset in the context of region `k`'s bound in bytes (8 bytes). + pub open spec fn bound_off(k: int) -> int { + 16 * k + 8 + } + + /// The first byte after the region table, where the exit area starts. + pub open spec fn table_end() -> int { + 16 * num_regions() + } + + /// The offset of the exit code, the first word of the exit area. + pub open spec fn exit_code_off() -> int { + table_end() + } + + /// The size of the context in bytes: the table, then the exit area. + pub open spec fn ctx_size() -> int { + 2 * table_end() + } + + /// The most bytes one load or store touches: `mov r64` or `movsd`. + pub open spec fn max_access() -> int { + 8 + } +} + +// ============================================================================ +// Section: decoding +// ============================================================================ +// +// jit.md J.6, "every instruction is in the subset, in canonical encoding". + +/// The bytes of `ins` one after the other, each the canonical encoding of +/// its instruction. +pub open spec fn enc_all(ins: Seq) -> Seq + decreases ins.len(), +{ + if ins.len() == 0 { + Seq::empty() + } else { + enc_all(ins.drop_last()) + encode(ins.last()) + } +} + +/// Every instruction is in the subset ([`valid_instr`]): a register number +/// of 16 or more, an index of `rsp` or a shift count of 0 is not. +pub open spec fn all_valid(ins: Seq) -> bool { + forall|j: int| 0 <= j < ins.len() ==> valid_instr(#[trigger] ins[j]) +} + +/// `blob` is exactly the canonical encodings of the instructions `ins`, in +/// the subset. No byte is left over, and none is unread. +pub open spec fn decodes_to(blob: Seq, ins: Seq) -> bool { + all_valid(ins) && blob == enc_all(ins) +} + +/// The byte offset at which instruction `j` begins (`start_of(ins, 0) == 0`; +/// `start_of(ins, ins.len())` is the length of the blob). +pub open spec fn start_of(ins: Seq, j: int) -> int + decreases j, +{ + if j <= 0 { + 0 + } else { + start_of(ins, j - 1) + instr_len(ins[j - 1]) as int + } +} + +// ============================================================================ +// Section: registers and what is known about them +// ============================================================================ + +/// What the scan knows about the value of one general register. +pub enum Fact { + /// Nothing. + Unknown, + /// The register holds the base address of region `k`: it was loaded + /// from `[r15 + 16k]` (64-bit), and the table is never written. + Base(int), + /// The register holds an `i` with `i + n <= bound(k)` (no wrap-around), + /// so an `n`-byte access at `base(k) + i` lies inside region `k`. A + /// bounds-check sequence established it. + Idx(int, int), +} + +/// One entry of the fact list: the register `reg` has the fact `fact`. +pub struct Binding { + /// The register number. + pub reg: u8, + /// What is known of it from here on. + pub fact: Fact, +} + +/// What is known about every register: a list of bindings, the *last* one +/// for a register being current (a register with none is [`Fact::Unknown`]). +pub type Facts = Seq; + +/// Nothing is known about any register. +pub open spec fn no_facts() -> Facts { + Seq::empty() +} + +/// What is known about register `r`: its latest binding. +pub open spec fn fact_of(fs: Facts, r: u8) -> Fact + decreases fs.len(), +{ + if fs.len() == 0 { + Fact::Unknown + } else if fs.last().reg == r { + fs.last().fact + } else { + fact_of(fs.drop_last(), r) + } +} + +/// `fs` with `r` now known as `f`. +pub open spec fn set_fact(fs: Facts, r: u8, f: Fact) -> Facts { + fs.push(Binding { reg: r, fact: f }) +} + +// ============================================================================ +// Section: what an instruction writes and touches +// ============================================================================ + +/// The general-purpose register an instruction writes, if any: its +/// destination (`J.6/reserved registers ...`). `cmp`, `test` and the stores +/// write none; the SSE instructions write only XMM registers, except the +/// ones that return an integer (`cvttsd2si`, `cvtsd2si`, `movq r, xmm`). +pub open spec fn written_gpr(i: Instr) -> Option { + match i { + Instr::MovRR { dst, .. } => Some(dst), + Instr::MovLoad { dst, .. } => Some(dst), + Instr::MovStore { .. } => None, + Instr::MovImm32 { dst, .. } => Some(dst), + Instr::MovImm32S { dst, .. } => Some(dst), + Instr::MovImm64 { dst, .. } => Some(dst), + Instr::AluRR { alu, dst, .. } => if alu is Cmp { + None + } else { + Some(dst) + }, + Instr::AluRM { alu, dst, .. } => if alu is Cmp { + None + } else { + Some(dst) + }, + Instr::AluRI { alu, dst, .. } => if alu is Cmp { + None + } else { + Some(dst) + }, + Instr::TestRR { .. } => None, + Instr::TestRI { .. } => None, + Instr::Neg { r, .. } => Some(r), + Instr::Not { r, .. } => Some(r), + Instr::ShiftImm { r, .. } => Some(r), + Instr::ShiftCl { r, .. } => Some(r), + Instr::ImulRR { dst, .. } => Some(dst), + Instr::ImulRRI { dst, .. } => Some(dst), + Instr::Jcc { .. } => None, + Instr::Jmp { .. } => None, + Instr::Ret => None, + Instr::Cmovcc { dst, .. } => Some(dst), + Instr::Setcc { dst, .. } => Some(dst), + Instr::FpArith { .. } => None, + Instr::Sqrtsd { .. } => None, + Instr::Ucomisd { .. } => None, + Instr::Cvtsi2sd { .. } => None, + Instr::Cvttsd2si { dst, .. } => Some(dst), + Instr::Cvtsd2si { dst, .. } => Some(dst), + Instr::MovqToXmm { .. } => None, + Instr::MovqFromXmm { dst, .. } => Some(dst), + Instr::MovsdLoad { .. } => None, + Instr::MovsdStore { .. } => None, + } +} + +/// The instruction writes the context register or the stack pointer. +pub open spec fn writes_reserved(i: Instr) -> bool { + match written_gpr(i) { + Some(d) => abi::reserved(d), + None => false, + } +} + +/// One memory access of an instruction: the operand, how many bytes are +/// touched, and whether it is a store. Every instruction of the subset +/// touches memory in at most one place and does not touch the stack +/// (`ret` reads `[rsp]`; it is accepted only in the exit sequence, where the +/// enter stub put the return address). +pub struct Access { + /// The memory operand. + pub mem: Mem, + /// The number of bytes read or written. + pub size: int, + /// A store (else a load). + pub store: bool, +} + +/// The memory access of an instruction, if it makes one. +pub open spec fn mem_access(i: Instr) -> Option { + match i { + Instr::MovLoad { sz, mem, .. } => Some( + Access { mem: mem, size: nbytes(sz) as int, store: false }, + ), + Instr::MovStore { sz, mem, .. } => Some( + Access { mem: mem, size: nbytes(sz) as int, store: true }, + ), + Instr::AluRM { sz, mem, .. } => Some( + Access { mem: mem, size: nbytes(sz) as int, store: false }, + ), + Instr::FpArith { xsrc: XmmSrc::Mem(m), .. } => Some( + Access { mem: m, size: 8, store: false }, + ), + Instr::Sqrtsd { xsrc: XmmSrc::Mem(m), .. } => Some( + Access { mem: m, size: 8, store: false }, + ), + Instr::Ucomisd { xsrc: XmmSrc::Mem(m), .. } => Some( + Access { mem: m, size: 8, store: false }, + ), + Instr::MovsdLoad { mem, .. } => Some(Access { mem: mem, size: 8, store: false }), + Instr::MovsdStore { mem, .. } => Some(Access { mem: mem, size: 8, store: true }), + _ => None, + } +} + +/// The access is permitted, given what is known of the registers +/// (`J.6/every load and store ...`). +/// +/// 1. **To the context:** base `r15`, no index, and the `size` bytes at +/// `disp` inside the context. A store must also lie in the exit area, so +/// the region table is never written. +/// 2. **To a region:** base a register known to hold region `k`'s base, an +/// index register known to be checked for at least `size` bytes of the +/// same region, scale 1 and displacement 0. +/// +/// Anything else (another base, an unchecked index, a wider access than was +/// checked, a displacement) is refused. +pub open spec fn access_ok(fs: Facts, a: Access) -> bool { + if 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) + } else { + match a.mem.index { + None => false, + Some(ix) => ix.scale == 0 && a.mem.disp == 0 && match ( + fact_of(fs, a.mem.base), + fact_of(fs, ix.reg), + ) { + (Fact::Base(k), Fact::Idx(k2, n)) => k == k2 && a.size <= n, + _ => false, + }, + } + } +} + +/// The instruction is a 64-bit load of the base of a region from the +/// context's table, `mov r, [r15 + 16k]`: the region `k`. +pub open spec fn base_load_region(i: Instr) -> Option { + match i { + Instr::MovLoad { sz, mem, .. } => if sz is W64 && mem.base == abi::ctx_reg() && mem.index + is None && 0 <= mem.disp && mem.disp < abi::table_end() && mem.disp % 16 == 0 { + Some(mem.disp as int / 16) + } else { + None + }, + _ => None, + } +} + +// ============================================================================ +// Section: units +// ============================================================================ +// +// jit.md J.6: "a fixed recognizable sequence". A unit is one or more +// instructions that the scan takes together. + +/// What a unit does to the scan: how many instructions it covers and what is +/// known after it. +pub struct Unit { + /// The number of instructions. + pub len: int, + /// What is known about the registers after the unit. + pub facts: Facts, +} + +/// A single instruction that is not part of a recognized sequence, and what +/// is known after it, if it is permitted (`J.6/reserved registers ...`, `J.6/ +/// every load and store ...`, `J.6/no indirect branch ...`). +/// +/// - `ret` is refused: it is only in the exit sequence. +/// - A write to the context register or the stack pointer is refused. +/// - A memory access must pass [`access_ok`]. +/// - `jmp` and `ret` end a block, so nothing is known after them; `jcc` does +/// not. +/// - Writing a register ends what is known about it; loading `[r15 + 16k]` +/// in 64 bits makes it region `k`'s base. +pub open spec fn plain_ok(fs: Facts, i: Instr) -> Option { + if i is Ret { + None + } else if writes_reserved(i) { + None + } else if match mem_access(i) { + Some(a) => !access_ok(fs, a), + None => false, + } { + None + } else if i is Jmp { + Some(no_facts()) + } else { + match written_gpr(i) { + None => Some(fs), + Some(d) => match base_load_region(i) { + Some(k) => Some(set_fact(fs, d, Fact::Base(k))), + None => Some(set_fact(fs, d, Fact::Unknown)), + }, + } + } +} + +/// The parameters of a bounds check, read off the first instructions of a +/// candidate sequence. +pub struct Chk { + /// The region (0 to 15). + pub k: int, + /// The number of bytes the check admits (1 to 8). + pub n: int, + /// The index register, checked and masked in place. + pub i: u8, + /// A scratch register for `i + n`. + pub t: u8, + /// A scratch register holding zero, for the mask. + pub z: u8, + /// The displacement of the first conditional jump (the carry exit). + pub r1: i32, + /// The displacement of the second (the bound exit). + pub r2: i32, +} + +/// The context memory operand holding region `k`'s bound. +pub open spec fn bound_mem(k: int) -> Mem { + Mem { base: abi::ctx_reg(), index: None, disp: abi::bound_off(k) as i32 } +} + +/// Instruction `m` (0 to 7) of the **bounds-check sequence** for `c`. With +/// `i` the index, `t` and `z` scratch, `n` the size, `K` the region: +/// +/// ```text +/// 0 mov t, i ; t := i +/// 1 add t, n ; t := i + n, CF set if it wrapped +/// 2 jb exit1 ; wrapped: refuse (cc 2, "b") +/// 3 cmp t, [r15 + bound(K)] +/// 4 ja exit2 ; i + n > bound: refuse (cc 7, "a") +/// 5 mov z, 0 +/// 6 cmp i, [r15 + bound(K)] +/// 7 cmovae i, z ; i := 0 if i >= bound (never, after 1-4) +/// ``` +/// +/// Falling through, `i + n <= bound(K)` with no wrap-around, so +/// `base(K) + i .. + n` is inside the region ([`Fact::Idx`]), and the mask in +/// 5 to 7 is the identity architecturally (it is what LIR's `bounds_check` +/// does, and it bounds the index under speculation). The two jumps may go +/// anywhere the targets rule allows. +pub open spec fn check_instr(m: int, c: Chk) -> Instr { + if m == 0 { + Instr::MovRR { sz: Size::W64, dst: c.t, src: c.i } + } else if m == 1 { + Instr::AluRI { alu: AluOp::Add, sz: Size::W64, dst: c.t, imm: c.n as i32 } + } else if m == 2 { + Instr::Jcc { cc: 2, rel: c.r1 } + } else if m == 3 { + Instr::AluRM { alu: AluOp::Cmp, sz: Size::W64, dst: c.t, mem: bound_mem(c.k) } + } else if m == 4 { + Instr::Jcc { cc: 7, rel: c.r2 } + } else if m == 5 { + Instr::MovImm32 { dst: c.z, imm_u32: 0 } + } else if m == 6 { + Instr::AluRM { alu: AluOp::Cmp, sz: Size::W64, dst: c.i, mem: bound_mem(c.k) } + } else { + Instr::Cmovcc { cc: 3, sz: Size::W64, dst: c.i, src: c.z } + } +} + +/// The whole bounds-check sequence for `c`, eight instructions. +pub open spec fn check_seq(c: Chk) -> Seq { + seq![ + check_instr(0, c), + check_instr(1, c), + check_instr(2, c), + check_instr(3, c), + check_instr(4, c), + check_instr(5, c), + check_instr(6, c), + check_instr(7, c), + ] +} + +/// The parameters a bounds-check sequence at `j` would have, read from the +/// instructions that carry them; `None` if there is no room for 8 +/// instructions or those do not have the right shapes. The match against +/// [`check_instr`] is [`check_unit_at`]'s. +pub open spec fn check_params(ins: Seq, j: int) -> Option { + if j < 0 || j + 8 > ins.len() { + None + } else { + match (ins[j], ins[j + 1], ins[j + 2], ins[j + 3], ins[j + 4], ins[j + 5]) { + ( + Instr::MovRR { dst: t, src: i, .. }, + Instr::AluRI { imm: n, .. }, + Instr::Jcc { rel: r1, .. }, + Instr::AluRM { mem: m, .. }, + Instr::Jcc { rel: r2, .. }, + Instr::MovImm32 { dst: z, .. }, + ) => Some( + Chk { k: (m.disp as int - 8) / 16, n: n as int, i: i, t: t, z: z, r1: r1, r2: r2 }, + ), + _ => None, + } + } +} + +/// The parameters are the ones the sequence admits: a region in the table, a +/// size of 1 to 8 bytes, three distinct registers, none of them reserved. +pub open spec fn check_ok(c: Chk) -> bool { + 0 <= c.k < abi::num_regions() && 1 <= c.n <= abi::max_access() && c.i != c.t && c.i != c.z + && c.t != c.z && !abi::reserved(c.i) && !abi::reserved(c.t) && !abi::reserved(c.z) +} + +/// A bounds-check sequence begins at instruction `j`: eight instructions +/// equal to [`check_instr`] for some admitted parameters. +pub open spec fn check_unit_at(ins: Seq, j: int) -> bool { + match check_params(ins, j) { + None => false, + Some(c) => check_ok(c) && ins[j] == check_instr(0, c) && ins[j + 1] == check_instr(1, c) + && ins[j + 2] == check_instr(2, c) && ins[j + 3] == check_instr(3, c) && ins[j + 4] + == check_instr(4, c) && ins[j + 5] == check_instr(5, c) && ins[j + 6] == check_instr( + 6, + c, + ) && ins[j + 7] == check_instr(7, c), + } +} + +/// What is known after the bounds-check sequence at `j` (assumed present): +/// the scratch registers are unknown, and the index register is checked for +/// `n` bytes of region `k`. +pub open spec fn check_facts(fs: Facts, ins: Seq, j: int) -> Facts { + match check_params(ins, j) { + None => fs, + Some(c) => set_fact( + set_fact(set_fact(fs, c.t, Fact::Unknown), c.z, Fact::Unknown), + c.i, + Fact::Idx(c.k, c.n), + ), + } +} + +/// An **exit sequence** begins at instruction `j` (`J.6/no indirect branch +/// except the exit sequence`, `J.6/every path ends in the exit sequence or a +/// branch`): `mov t, code`; `mov [r15 + exit_code_off], t` (64-bit); +/// `ret`, with `t` not reserved. +pub open spec fn exit_unit_at(ins: Seq, j: int) -> bool { + 0 <= j && j + 3 <= ins.len() && match (ins[j], ins[j + 1], ins[j + 2]) { + (Instr::MovImm32 { dst: t, .. }, Instr::MovStore { sz, mem, src }, Instr::Ret) => !abi::reserved( + t, + ) && sz is W64 && mem.base == abi::ctx_reg() && mem.index is None && mem.disp + == abi::exit_code_off() && src == t, + _ => false, + } +} + +/// The unit that begins at instruction `j`, given what is known there: +/// the exit sequence, else the bounds-check sequence, else one permitted +/// instruction. `None` if the instruction at `j` is not permitted. +pub open spec fn unit_at(ins: Seq, j: int, fs: Facts) -> Option { + if exit_unit_at(ins, j) { + Some(Unit { len: 3, facts: no_facts() }) + } else if check_unit_at(ins, j) { + Some(Unit { len: 8, facts: check_facts(fs, ins, j) }) + } else if 0 <= j && j < ins.len() { + match plain_ok(fs, ins[j]) { + Some(f) => Some(Unit { len: 1, facts: f }), + None => None, + } + } else { + None + } +} + +// ============================================================================ +// Section: targets +// ============================================================================ +// +// `J.6/every direct target is a decoded boundary inside the blob`. + +/// The displacement of a direct jump (`jcc rel32`, `jmp rel32`). +pub open spec fn jump_rel(i: Instr) -> Option { + match i { + Instr::Jcc { rel, .. } => Some(rel as int), + Instr::Jmp { rel } => Some(rel as int), + _ => None, + } +} + +/// The byte offsets that instructions `j` and after jump to: the offset of +/// the next instruction plus the displacement (Intel SDM vol. 2, `Jcc` and +/// `JMP`: "relative to the end of the instruction"). +pub open spec fn jump_targets(ins: Seq, j: int) -> Seq + decreases ins.len() - j, +{ + if j < 0 || j >= ins.len() { + Seq::empty() + } else { + let here: Seq = match jump_rel(ins[j]) { + Some(r) => seq![start_of(ins, j + 1) + r], + None => Seq::empty(), + }; + here + jump_targets(ins, j + 1) + } +} + +/// `x` is an element of `s`. +pub open spec fn seq_has(s: Seq, x: int) -> bool + decreases s.len(), +{ + if s.len() == 0 { + false + } else { + s.last() == x || seq_has(s.drop_last(), x) + } +} + +/// Every element of `a` is an element of `b`. +pub open spec fn subset_of(a: Seq, b: Seq) -> bool + decreases a.len(), +{ + if a.len() == 0 { + true + } else { + seq_has(b, a.last()) && subset_of(a.drop_last(), b) + } +} + +// ============================================================================ +// Section: the scan and the policy +// ============================================================================ + +/// The one linear pass (`J.6`: "Checks, in one linear decode"). From +/// instruction `j`, knowing `fs`: forget everything if this offset is a +/// target; take the unit that begins here; continue after it. The result is +/// the offsets at which units begin (from `j` on), or `None` if any unit is +/// refused. +pub open spec fn scan(ins: Seq, tg: Seq, j: int, fs: Facts) -> Option> + decreases ins.len() - j, +{ + if j < 0 || j >= ins.len() { + Some(Seq::empty()) + } else { + let here = start_of(ins, j); + let fs0 = if seq_has(tg, here) { + no_facts() + } else { + fs + }; + match unit_at(ins, j, fs0) { + None => None, + Some(u) => if u.len >= 1 && j + u.len <= ins.len() { + match scan(ins, tg, j + u.len, u.facts) { + None => None, + Some(rest) => Some(seq![here] + rest), + } + } else { + None + }, + } + } +} + +/// Every offset the blob can be entered at or jumped to: the entries and the +/// jump targets. +pub open spec fn all_targets(ins: Seq, entries: Seq) -> Seq { + entries + jump_targets(ins, 0) +} + +/// The instructions `ins`, entered at the byte offsets `entries`, pass the +/// validator's checks: +/// +/// - there is an instruction, and the last is `ret` or `jmp` +/// (`J.6/every path ends in the exit sequence or a branch`; with `ret` +/// accepted only in the exit sequence, this is "ends in an exit sequence or +/// an unconditional jump"); +/// - the scan refuses no unit; +/// - every entry and every jump target is the offset of a unit's first +/// instruction (`J.6/every direct target ...`). +pub open spec fn policy(ins: Seq, entries: Seq) -> bool { + 0 < ins.len() && (ins.last() is Ret || ins.last() is Jmp) && match scan( + ins, + all_targets(ins, entries), + 0, + no_facts(), + ) { + None => false, + Some(starts) => subset_of(all_targets(ins, entries), starts), + } +} + +/// `blob`, entered at the byte offsets `entries`, is **accepted**: it is the +/// canonical encoding of some sequence of instructions of the subset that +/// passes the [`policy`]. +pub open spec fn accepted(blob: Seq, entries: Seq) -> bool { + exists|ins: Seq| decodes_to(blob, ins) && policy(ins, entries) +} + +} // verus! diff --git a/crates/clazel-codecheck/src/uniq.rs b/crates/clazel-codecheck/src/uniq.rs new file mode 100644 index 000000000..633aaf196 --- /dev/null +++ b/crates/clazel-codecheck/src/uniq.rs @@ -0,0 +1,112 @@ +//! The decomposition of a blob into instructions is unique: the facts about +//! `enc_all` and `start_of` that [`crate::decode_all::decode_all`] needs to +//! show it finds the decomposition the spec quantifies over. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Start offsets of a prefix are the start offsets of the whole. +pub proof fn lemma_start_of_prefix(w: Seq, k: int, j: int) + requires + 0 <= j <= k <= w.len(), + ensures + start_of(w.subrange(0, k), j) == start_of(w, j), + decreases j, +{ + if j > 0 { + lemma_start_of_prefix(w, k, j - 1); + assert(w.subrange(0, k)[j - 1] == w[j - 1]); + } +} + +/// Instruction `k` of `w` is encoded at its start offset in `enc_all(w)`. +pub proof fn lemma_enc_all_index(w: Seq, k: int) + requires + 0 <= k < w.len(), + ensures + start_of(w, k) + encode(w[k]).len() <= enc_all(w).len(), + enc_all(w).subrange(start_of(w, k), start_of(w, k) + encode(w[k]).len()) =~= encode(w[k]), + decreases w.len(), +{ + let d = w.drop_last(); + assert(w =~= d.push(w.last())); + super::validate::lemma_enc_all_len(d); + if k == w.len() - 1 { + super::decode_all::lemma_start_of_push(d, w.last(), d.len() as int); + assert(start_of(w, k) == enc_all(d).len()); + assert(enc_all(w).subrange(start_of(w, k), start_of(w, k) + encode(w[k]).len()) + =~= encode(w[k])); + } else { + lemma_enc_all_index(d, k); + super::decode_all::lemma_start_of_push(d, w.last(), k); + assert(d[k] == w[k]); + super::validate::lemma_start_of_ge(d, k); + let a = start_of(d, k); + let b = a + encode(d[k]).len(); + assert(enc_all(w) =~= enc_all(d) + encode(w.last())); + assert(b <= enc_all(d).len()); + assert forall|i: int| 0 <= i < b - a implies enc_all(w).subrange(a, b)[i] == enc_all( + d, + ).subrange(a, b)[i] by { + assert(a >= 0); + assert(enc_all(w).subrange(a, b)[i] == enc_all(w)[a + i]); + assert(enc_all(d).subrange(a, b)[i] == enc_all(d)[a + i]); + assert(enc_all(w)[a + i] == enc_all(d)[a + i]); + } + assert(enc_all(w).subrange(a, b) =~= enc_all(d).subrange(a, b)); + assert(enc_all(w).subrange(start_of(w, k), start_of(w, k) + encode(w[k]).len()) + =~= enc_all(d).subrange(start_of(d, k), start_of(d, k) + encode(d[k]).len())); + } +} + +/// If the blob decomposes as `w`, the bytes after the first `k` instructions +/// begin with the encoding of the next one. +pub proof fn lemma_next_instr(blob: Seq, w: Seq, k: int, pos: int) + requires + decodes_to(blob, w), + 0 <= k <= w.len(), + pos == start_of(w, k), + pos < blob.len(), + ensures + k < w.len(), + starts_with_encoding(blob.subrange(pos, blob.len() as int), w[k]), +{ + super::validate::lemma_enc_all_len(w); + assert(blob == enc_all(w)); + if k < w.len() { + lemma_enc_all_index(w, k); + let rest = blob.subrange(pos, blob.len() as int); + let e = encode(w[k]); + assert(valid_instr(w[k])); + super::validate::lemma_start_of_ge(w, k); + assert(pos >= 0); + assert(pos + e.len() <= blob.len()); + assert(blob.subrange(pos, pos + e.len()) =~= e); + assert(rest.subrange(0, e.len() as int) =~= blob.subrange(pos, pos + e.len())); + assert(e.len() <= rest.len()); + } else { + assert(false); + } +} + +/// If the blob decomposes as `w` and `k` instructions reach its end, they are +/// all of `w`. +pub proof fn lemma_finish(blob: Seq, w: Seq, k: int) + requires + decodes_to(blob, w), + 0 <= k <= w.len(), + start_of(w, k) == blob.len(), + ensures + k == w.len(), +{ + super::validate::lemma_enc_all_len(w); + if k < w.len() { + super::scan_exec::lemma_start_of_lt(w, k, w.len() as int); + } +} + +} // verus! diff --git a/crates/clazel-codecheck/src/unit_exec.rs b/crates/clazel-codecheck/src/unit_exec.rs new file mode 100644 index 000000000..e1c46930c --- /dev/null +++ b/crates/clazel-codecheck/src/unit_exec.rs @@ -0,0 +1,437 @@ +//! The executable unit checks of the validator, each proven equal to its +//! spec function: [`plain_ok_e`] to [`crate::spec::plain_ok`], [`exit_at_e`] +//! to [`crate::spec::exit_unit_at`], and [`check_at_e`] to +//! [`crate::spec::check_unit_at`] with its parameters. +//! +//! What is known about the 16 registers is an exec array of [`FactE`] +//! ([`rel`] relates it to the spec's list of bindings, [`crate::spec::Facts`]). + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// What is known about one register (the exec form of [`Fact`]). +#[derive(Clone, Copy)] +pub enum FactE { + /// Nothing. + Unknown, + /// The base of region `k`. + Base(u8), + /// Checked for `n` bytes of region `k`. + Idx(u8, u8), +} + +/// The spec fact an exec fact stands for. +pub open spec fn fact_view(f: FactE) -> Fact { + match f { + FactE::Unknown => Fact::Unknown, + FactE::Base(k) => Fact::Base(k as int), + FactE::Idx(k, n) => Fact::Idx(k as int, n as int), + } +} + +/// The array `fa` of 16 exec facts is what the spec list `fs` says. +pub open spec fn rel(fa: Seq, fs: Facts) -> bool { + fa.len() == 16 && forall|r: int| + 0 <= r < 16 ==> fact_view(#[trigger] fa[r]) == fact_of(fs, r as u8) +} + +/// Binding a register changes that register's fact only. +pub proof fn lemma_fact_of_set(fs: Facts, d: u8, f: Fact, r: u8) + ensures + fact_of(set_fact(fs, d, f), r) == if r == d { + f + } else { + fact_of(fs, r) + }, +{ + let s = set_fact(fs, d, f); + assert(s.drop_last() =~= fs); + assert(s.last() == (Binding { reg: d, fact: f })); +} + +/// The exec fact of register `r`. +pub fn fact_at(fa: &[FactE], r: u8) -> (f: FactE) + requires + fa@.len() == 16, + r < 16, + ensures + f == fa@[r as int], +{ + match fa.get(r as usize) { + Some(x) => *x, + None => FactE::Unknown, + } +} + +/// The spec's `access_ok`. +pub fn access_ok_e(fa: &[FactE], Ghost(fs): Ghost, m: &Mem, size: u8, store: bool) -> (r: bool) + requires + rel(fa@, fs), + mem_ok(*m), + ensures + r == access_ok(fs, Access { mem: *m, size: size as int, store: store }), +{ + if m.base == 15 { + let none = m.index.is_none(); + none && m.disp >= 0 && (m.disp as i64).wrapping_add(size as i64) <= 512 && (!store + || 256 <= m.disp) + } else { + match &m.index { + None => false, + Some(ix) => ix.scale == 0 && m.disp == 0 && match ( + fact_at(fa, m.base), + fact_at(fa, ix.reg), + ) { + (FactE::Base(k), FactE::Idx(k2, n)) => k == k2 && size <= n, + _ => false, + }, + } + } +} + +/// The spec's `written_gpr`. +pub fn written_gpr_e(i: &Instr) -> (r: Option) + ensures + r == written_gpr(*i), +{ + match i { + Instr::MovRR { dst, .. } => Some(*dst), + Instr::MovLoad { dst, .. } => Some(*dst), + Instr::MovStore { .. } => None, + Instr::MovImm32 { dst, .. } => Some(*dst), + Instr::MovImm32S { dst, .. } => Some(*dst), + Instr::MovImm64 { dst, .. } => Some(*dst), + Instr::AluRR { alu, dst, .. } => match alu { + AluOp::Cmp => None, + _ => Some(*dst), + }, + Instr::AluRM { alu, dst, .. } => match alu { + AluOp::Cmp => None, + _ => Some(*dst), + }, + Instr::AluRI { alu, dst, .. } => match alu { + AluOp::Cmp => None, + _ => Some(*dst), + }, + Instr::TestRR { .. } => None, + Instr::TestRI { .. } => None, + Instr::Neg { r, .. } => Some(*r), + Instr::Not { r, .. } => Some(*r), + Instr::ShiftImm { r, .. } => Some(*r), + Instr::ShiftCl { r, .. } => Some(*r), + Instr::ImulRR { dst, .. } => Some(*dst), + Instr::ImulRRI { dst, .. } => Some(*dst), + Instr::Jcc { .. } => None, + Instr::Jmp { .. } => None, + Instr::Ret => None, + Instr::Cmovcc { dst, .. } => Some(*dst), + Instr::Setcc { dst, .. } => Some(*dst), + Instr::FpArith { .. } => None, + Instr::Sqrtsd { .. } => None, + Instr::Ucomisd { .. } => None, + Instr::Cvtsi2sd { .. } => None, + Instr::Cvttsd2si { dst, .. } => Some(*dst), + Instr::Cvtsd2si { dst, .. } => Some(*dst), + Instr::MovqToXmm { .. } => None, + Instr::MovqFromXmm { dst, .. } => Some(*dst), + Instr::MovsdLoad { .. } => None, + Instr::MovsdStore { .. } => None, + } +} + +/// The spec's `mem_access`: the operand, its size, whether it is a store. +pub fn mem_access_e(i: &Instr) -> (r: Option<(&Mem, u8, bool)>) + ensures + match r { + Some((m, s, st)) => mem_access(*i) == Some( + Access { mem: *m, size: s as int, store: st }, + ), + None => mem_access(*i) is None, + }, +{ + match i { + Instr::MovLoad { sz, mem, .. } => Some( + ( + mem, + match sz { + Size::W64 => 8u8, + Size::W32 => 4u8, + }, + false, + ), + ), + Instr::MovStore { sz, mem, .. } => Some( + ( + mem, + match sz { + Size::W64 => 8u8, + Size::W32 => 4u8, + }, + true, + ), + ), + Instr::AluRM { sz, mem, .. } => Some( + ( + mem, + match sz { + Size::W64 => 8u8, + Size::W32 => 4u8, + }, + false, + ), + ), + Instr::FpArith { xsrc, .. } => match xsrc { + XmmSrc::Mem(m) => Some((m, 8u8, false)), + XmmSrc::Reg(_) => None, + }, + Instr::Sqrtsd { xsrc, .. } => match xsrc { + XmmSrc::Mem(m) => Some((m, 8u8, false)), + XmmSrc::Reg(_) => None, + }, + Instr::Ucomisd { xsrc, .. } => match xsrc { + XmmSrc::Mem(m) => Some((m, 8u8, false)), + XmmSrc::Reg(_) => None, + }, + Instr::MovsdLoad { mem, .. } => Some((mem, 8u8, false)), + Instr::MovsdStore { mem, .. } => Some((mem, 8u8, true)), + _ => None, + } +} + +/// The spec's `base_load_region`: the region a table-base load names. +pub fn base_load_region_e(i: &Instr) -> (r: Option) + ensures + match r { + Some(k) => base_load_region(*i) == Some(k as int), + None => base_load_region(*i) is None, + }, +{ + match i { + Instr::MovLoad { sz, mem, .. } => { + let w64 = match sz { + Size::W64 => true, + Size::W32 => false, + }; + let noidx = mem.index.is_none(); + if w64 && mem.base == 15 && noidx && 0 <= mem.disp && mem.disp < 256 && mem.disp % 16 + == 0 { + Some((mem.disp / 16) as u8) + } else { + None + } + }, + _ => None, + } +} + +/// The spec's `plain_ok`: `true` and the new facts if one instruction is +/// permitted, else `false`. +#[verifier::spinoff_prover] +#[verifier::rlimit(1000)] +pub fn plain_ok_e(fa: &mut Vec, Ghost(fs): Ghost, i: &Instr) -> (r: bool) + requires + rel(old(fa)@, fs), + valid_instr(*i), + ensures + r == (plain_ok(fs, *i) is Some), + r ==> rel(final(fa)@, plain_ok(fs, *i)->Some_0), +{ + if let Instr::Ret = i { + return false; + } + let wg = written_gpr_e(i); + let wres = match wg { + Some(d) => d == 15 || d == 4, + None => false, + }; + if wres { + return false; + } + let bad = match mem_access_e(i) { + Some((m, size, store)) => !access_ok_e(fa.as_slice(), Ghost(fs), m, size, store), + None => false, + }; + if bad { + return false; + } + if let Instr::Jmp { .. } = i { + let mut k: usize = 0; + while k < 16 + invariant + k <= 16, + fa@.len() == 16, + forall|r: int| 0 <= r < k ==> fa@[r] == FactE::Unknown, + decreases 16 - k, + { + fa.set(k, FactE::Unknown); + k = k.saturating_add(1); + } + proof { + assert(rel(fa@, no_facts())) by { + assert forall|r: int| 0 <= r < 16 implies fact_view(#[trigger] fa@[r]) == fact_of( + no_facts(), + r as u8, + ) by {} + } + } + return true; + } + match wg { + None => true, + Some(d) => { + let f = match base_load_region_e(i) { + Some(k) => FactE::Base(k), + None => FactE::Unknown, + }; + fa.set(d as usize, f); + proof { + let ns = set_fact(fs, d, fact_view(f)); + assert forall|r: int| 0 <= r < 16 implies fact_view(#[trigger] fa@[r]) + == fact_of(ns, r as u8) by { + lemma_fact_of_set(fs, d, fact_view(f), r as u8); + } + } + true + }, + } +} + +/// The spec's `exit_unit_at`. +pub fn exit_at_e(ins: &[Instr], j: usize) -> (r: bool) + requires + j < 0x8000_0000, + ensures + r == exit_unit_at(ins@, j as int), +{ + match (ins.get(j), ins.get(j.saturating_add(1)), ins.get(j.saturating_add(2))) { + ( + Some(Instr::MovImm32 { dst: t, .. }), + Some(Instr::MovStore { sz, mem, src }), + Some(Instr::Ret), + ) => { + let w64 = match sz { + Size::W64 => true, + Size::W32 => false, + }; + let noidx = mem.index.is_none(); + !(*t == 15 || *t == 4) && w64 && mem.base == 15 && noidx && mem.disp == 256 && *src + == *t + }, + _ => false, + } +} + +/// The parameters of a bounds-check sequence (the spec's `Chk`, without the +/// jump displacements). +pub struct ChkE { + /// The region. + pub k: u8, + /// The size. + pub n: u8, + /// The index register. + pub i: u8, + /// The first scratch register. + pub t: u8, + /// The zero register. + pub z: u8, +} + +/// The spec's `check_unit_at` with the parameters `check_params` reads. +#[verifier::spinoff_prover] +#[verifier::rlimit(2000)] +pub fn check_at_e(ins: &[Instr], j: usize) -> (r: Option) + requires + j < 0x8000_0000, + ensures + match r { + Some(c) => { + &&& check_unit_at(ins@, j as int) + &&& check_params(ins@, j as int) is Some + &&& check_params(ins@, j as int)->Some_0.k == c.k as int + &&& check_params(ins@, j as int)->Some_0.n == c.n as int + &&& check_params(ins@, j as int)->Some_0.i == c.i + &&& check_params(ins@, j as int)->Some_0.t == c.t + &&& check_params(ins@, j as int)->Some_0.z == c.z + }, + None => !check_unit_at(ins@, j as int), + }, +{ + let (t, i) = match ins.get(j) { + Some(Instr::MovRR { sz: Size::W64, dst, src }) => (*dst, *src), + _ => { + return None; + }, + }; + let n = match ins.get(j.saturating_add(1)) { + Some(Instr::AluRI { alu: AluOp::Add, sz: Size::W64, dst, imm }) if *dst == t => *imm, + _ => { + return None; + }, + }; + match ins.get(j.saturating_add(2)) { + Some(Instr::Jcc { cc: 2, .. }) => {}, + _ => { + return None; + }, + } + let disp = match ins.get(j.saturating_add(3)) { + Some( + Instr::AluRM { alu: AluOp::Cmp, sz: Size::W64, dst, mem: Mem { base: 15, index: None, disp } }, + ) if *dst == t => *disp, + _ => { + return None; + }, + }; + match ins.get(j.saturating_add(4)) { + Some(Instr::Jcc { cc: 7, .. }) => {}, + _ => { + return None; + }, + } + let z = match ins.get(j.saturating_add(5)) { + Some(Instr::MovImm32 { dst, imm_u32: 0 }) => *dst, + _ => { + return None; + }, + }; + match ins.get(j.saturating_add(6)) { + Some( + Instr::AluRM { alu: AluOp::Cmp, sz: Size::W64, dst, mem: Mem { base: 15, index: None, disp: d6 } }, + ) if *dst == i && *d6 == disp => {}, + _ => { + return None; + }, + } + match ins.get(j.saturating_add(7)) { + Some(Instr::Cmovcc { cc: 3, sz: Size::W64, dst, src }) if *dst == i && *src == z => {}, + _ => { + return None; + }, + } + if disp < 8 { + return None; + } + let off = disp.wrapping_sub(8); + if off % 16 != 0 || off / 16 >= 16 { + return None; + } + if n < 1 { + return None; + } + if n > 8 { + return None; + } + if i == t || i == z || t == z { + return None; + } + if i == 15 || i == 4 || t == 15 || t == 4 || z == 15 || z == 4 { + return None; + } + Some(ChkE { k: (off / 16) as u8, n: n as u8, i, t, z }) +} + +} // verus! diff --git a/crates/clazel-codecheck/src/validate.rs b/crates/clazel-codecheck/src/validate.rs new file mode 100644 index 000000000..f153a94be --- /dev/null +++ b/crates/clazel-codecheck/src/validate.rs @@ -0,0 +1,454 @@ +//! The executable validator, proven **sound and complete**: [`validate`] +//! returns `true` if and only if the blob is [`crate::spec::accepted`] for the +//! entries, so it meets `contract::validate_ensures`. It decodes once +//! ([`crate::decode_all::decode_all`]), marks the entries and the jump +//! targets, runs the unit scan ([`crate::scan_exec::scan_exec`]) and checks +//! that every target is a unit start, in time and space linear in the blob. +//! Being Verus code it has no panic and no overflow. +use super::decode_all::decode_all; +use super::scan_exec::scan_exec; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// `seq_has(s, s[i])`. +pub proof fn lemma_seq_has_at(s: Seq, i: int) + requires + 0 <= i < s.len(), + ensures + seq_has(s, s[i]), + decreases s.len(), +{ + if i < s.len() - 1 { + lemma_seq_has_at(s.drop_last(), i); + assert(s.drop_last()[i] == s[i]); + } +} + +/// If every element of `a` is in `b`, `a` is a subset of `b`. +pub proof fn lemma_subset_intro(a: Seq, b: Seq) + requires + forall|i: int| 0 <= i < a.len() ==> seq_has(b, #[trigger] a[i]), + ensures + subset_of(a, b), + decreases a.len(), +{ + if a.len() > 0 { + let d = a.drop_last(); + assert forall|i: int| 0 <= i < d.len() implies seq_has(b, #[trigger] d[i]) by { + assert(d[i] == a[i]); + } + lemma_subset_intro(d, b); + assert(a[a.len() - 1] == a.last()); + } +} + +/// Every instruction takes a byte: the start offset of instruction `k` is at +/// least `k`. +pub proof fn lemma_start_of_ge(ins: Seq, k: int) + requires + 0 <= k <= ins.len(), + ensures + start_of(ins, k) >= k, + decreases k, +{ + if k > 0 { + lemma_start_of_ge(ins, k - 1); + super::decode_all::lemma_encode_nonempty(ins[k - 1]); + } +} + +/// The bytes of the instructions have as many bytes as the last start offset. +pub proof fn lemma_enc_all_len(ins: Seq) + ensures + enc_all(ins).len() == start_of(ins, ins.len() as int), + decreases ins.len(), +{ + if ins.len() > 0 { + let d = ins.drop_last(); + lemma_enc_all_len(d); + assert(ins =~= d.push(ins.last())); + super::decode_all::lemma_start_of_push(d, ins.last(), d.len() as int); + } +} + +/// The offsets of the direct jumps of `ins[j..]` unfold one instruction at a +/// time. +pub proof fn lemma_jump_targets_step(ins: Seq, j: int) + requires + 0 <= j < ins.len(), + ensures + jump_targets(ins, j) == (match jump_rel(ins[j]) { + Some(r) => seq![start_of(ins, j + 1) + r], + None => Seq::::empty(), + } + jump_targets(ins, j + 1)), +{ +} + +/// The jump displacement of an instruction (the spec's `jump_rel`). +pub fn jump_rel_e(i: &Instr) -> (r: Option) + ensures + match r { + Some(d) => jump_rel(*i) == Some(d as int), + None => jump_rel(*i) is None, + }, +{ + match i { + Instr::Jcc { rel, .. } => Some(*rel), + Instr::Jmp { rel } => Some(*rel), + _ => None, + } +} + +/// If the decomposition `ins` of the blob is the only one, an accepted blob +/// passes the policy as `ins`. +pub proof fn lemma_acc_policy(blob: Seq, ent: Seq, ins: Seq) + requires + decodes_to(blob, ins), + forall|w: Seq| #[trigger] decodes_to(blob, w) ==> ins =~= w, + ensures + accepted(blob, ent) ==> policy(ins, ent), +{ + if accepted(blob, ent) { + let w = choose|w: Seq| decodes_to(blob, w) && policy(w, ent); + assert(decodes_to(blob, w)); + assert(ins =~= w); + } +} + +/// Whether `blob`, entered at `entries`, is accepted: `true` if and only if +/// it is, so the contract's soundness and completeness both hold. +#[verifier::spinoff_prover] +#[verifier::rlimit(4000)] +pub fn validate(blob: &[u8], entries: &[usize]) -> (ok: bool) + requires + blob@.len() <= 0x7FFF_FFFF, + ensures + crate::contract::validate_ensures(blob@, entries@, ok), +{ + let total = blob.len(); + let ghost ent = crate::contract::entry_offsets(entries@); + let (ins, st) = match decode_all(blob) { + Some(x) => x, + None => { + proof { + assert(!accepted(blob@, ent)) by { + if accepted(blob@, ent) { + let w = choose|w: Seq| decodes_to(blob@, w) && policy(w, ent); + assert(decodes_to(blob@, w)); + } + } + } + return false; + }, + }; + let n = ins.len(); + proof { + lemma_enc_all_len(ins@); + assert(total as int == start_of(ins@, n as int)); + lemma_start_of_ge(ins@, n as int); + lemma_acc_policy(blob@, ent, ins@); + } + if n == 0 { + proof { + assert(!accepted(blob@, ent)); + } + return false; + } + // The last instruction is ret or jmp. + let last_ok = matches!(ins.get(n.saturating_sub(1)), Some(Instr::Ret | Instr::Jmp { .. })); + if !last_ok { + proof { + assert(!accepted(blob@, ent)) by { + if accepted(blob@, ent) { + super::lemmas::lemma_policy_ends(ins@, ent); + } + } + } + return false; + } + // Targets: the entries, then the jump targets. + let mut target: Vec = vec![false; total]; + let ghost mut tgacc: Seq = Seq::empty(); + let mut k: usize = 0; + while k < entries.len() + invariant + k <= entries.len(), + target@.len() == total, + total <= 0x7FFF_FFFF, + n == ins@.len(), + total as int == start_of(ins@, ins@.len() as int), + accepted(blob@, ent) ==> policy(ins@, ent), + ent == crate::contract::entry_offsets(entries@), + tgacc.len() == k, + forall|q: int| 0 <= q < k ==> #[trigger] tgacc[q] == entries@[q] as int, + forall|q: int| 0 <= q < tgacc.len() ==> 0 <= #[trigger] tgacc[q] < total, + forall|p: int| 0 <= p < total ==> target@[p] == seq_has(tgacc, p), + decreases entries.len() - k, + { + let e = match entries.get(k) { + Some(e) => *e, + None => { + proof { assert(false); } + return false; + }, + }; + if e >= total { + proof { + assert(!accepted(blob@, ent)) by { + if accepted(blob@, ent) { + assert(ent.len() == entries@.len()); + assert(e == entries@[k as int]); + assert(ent[k as int] == entries@[k as int] as int); + assert(ent[k as int] == e as int); + lemma_seq_has_at(ent, k as int); + super::acc_lemmas::lemma_seq_has_concat( + ent, + jump_targets(ins@, 0), + e as int, + ); + assert(all_targets(ins@, ent) =~= ent + jump_targets(ins@, 0)); + super::acc_lemmas::lemma_policy_in_range(ins@, ent, e as int); + } + } + } + return false; + } + target.set(e, true); + let ghost old = tgacc; + proof { + tgacc = old.push(e as int); + assert(tgacc.drop_last() =~= old); + reveal_with_fuel(seq_has, 2); + assert forall|p: int| 0 <= p < total implies target@[p] == seq_has(tgacc, p) by { + assert(seq_has(tgacc, p) == (p == e as int || seq_has(old, p))); + } + } + k = k.saturating_add(1); + } + proof { + assert(tgacc =~= ent); + } + let mut j: usize = 0; + while j < n + invariant + j <= n, + n == ins@.len(), + ins@.len() == st@.len(), + ins@.len() < 0x8000_0000, + total as int == start_of(ins@, n as int), + forall|q: int| 0 <= q < ins@.len() ==> st@[q] as int == start_of(ins@, q), + all_valid(ins@), + target@.len() == total, + total <= 0x7FFF_FFFF, + accepted(blob@, ent) ==> policy(ins@, ent), + ent == crate::contract::entry_offsets(entries@), + forall|q: int| 0 <= q < tgacc.len() ==> 0 <= #[trigger] tgacc[q] < total, + forall|p: int| 0 <= p < total ==> target@[p] == seq_has(tgacc, p), + tgacc + jump_targets(ins@, j as int) =~= all_targets(ins@, ent), + decreases n - j, + { + proof { + lemma_jump_targets_step(ins@, j as int); + } + let j1 = j.saturating_add(1); + let next: usize = if j1 < n { + match st.get(j1) { + Some(s) => *s, + None => { + proof { assert(false); } + return false; + }, + } + } else { + total + }; + proof { + if j + 1 < n { + super::scan_exec::lemma_start_of_lt(ins@, j as int + 1, n as int); + } + } + assert(next as int == start_of(ins@, j as int + 1)); + assert(next <= total); + let rel = match ins.get(j) { + Some(i) => jump_rel_e(i), + None => { + proof { assert(false); } + return false; + }, + }; + match rel { + None => { + proof { + assert(jump_rel(ins@[j as int]) is None); + assert(tgacc + jump_targets(ins@, j as int + 1) =~= all_targets(ins@, ent)); + } + }, + Some(d) => { + proof { + // The target `next + d` is in the targets, so inside the blob if + // the blob is accepted. + assert(jump_rel(ins@[j as int]) == Some(d as int)); + assert(jump_targets(ins@, j as int) =~= seq![next as int + d as int] + + jump_targets(ins@, j as int + 1)); + super::acc_lemmas::lemma_seq_has_concat( + seq![next as int + d as int], + jump_targets(ins@, j as int + 1), + next as int + d as int, + ); + super::acc_lemmas::lemma_seq_has_single( + next as int + d as int, + next as int + d as int, + ); + super::acc_lemmas::lemma_seq_has_concat( + tgacc, + jump_targets(ins@, j as int), + next as int + d as int, + ); + assert(seq_has(all_targets(ins@, ent), next as int + d as int)); + assert(accepted(blob@, ent) ==> (0 <= next as int + d as int && (next as int + + d as int) < total)) by { + if accepted(blob@, ent) { + super::acc_lemmas::lemma_policy_in_range( + ins@, + ent, + next as int + d as int, + ); + } + } + } + let t: i64 = match (next as i64).checked_add(d as i64) { + Some(t) => t, + None => { + proof { + assert(!accepted(blob@, ent)); + } + return false; + }, + }; + proof { + let a64 = next as i64; + let b64 = d as i64; + assert(a64 as int == next as int); + assert(b64 as int == d as int); + assert(a64.checked_add(b64) == Some(t)); + assert(t as int == a64 as int + b64 as int); + assert(t as int == next as int + d as int); + } + if t < 0 || t >= total as i64 { + proof { + assert(!accepted(blob@, ent)); + } + return false; + } + target.set(t as usize, true); + let ghost old = tgacc; + proof { + tgacc = old.push(t as int); + assert(tgacc.drop_last() =~= old); + reveal_with_fuel(seq_has, 2); + assert forall|p: int| 0 <= p < total implies target@[p] == seq_has( + tgacc, + p, + ) by { + assert(seq_has(tgacc, p) == (p == t as int || seq_has(old, p))); + } + assert(jump_targets(ins@, j as int) =~= seq![t as int] + jump_targets( + ins@, + j as int + 1, + )); + assert(tgacc + jump_targets(ins@, j as int + 1) =~= old + jump_targets( + ins@, + j as int, + )); + } + }, + } + j = j.saturating_add(1); + } + proof { + assert(jump_targets(ins@, n as int) =~= Seq::::empty()); + assert(tgacc =~= all_targets(ins@, ent)); + } + let tg = Ghost(tgacc); + let us = match scan_exec(ins.as_slice(), st.as_slice(), target.as_slice(), tg, total) { + Some(us) => us, + None => { + proof { + assert(!accepted(blob@, ent)) by { + if accepted(blob@, ent) { + assert(tgacc =~= all_targets(ins@, ent)); + } + } + } + return false; + }, + }; + proof { + assert(tgacc =~= all_targets(ins@, ent)); + } + // Every target is a unit start. + let mut p: usize = 0; + while p < total + invariant + p <= total, + target@.len() == total, + total <= 0x7FFF_FFFF, + accepted(blob@, ent) ==> policy(ins@, ent), + ent == crate::contract::entry_offsets(entries@), + us@.len() == total, + forall|q: int| 0 <= q < p ==> (target@[q] ==> us@[q]), + tgacc =~= all_targets(ins@, ent), + scan(ins@, tgacc, 0, no_facts()) is Some, + forall|q: int| 0 <= q < total ==> target@[q] == seq_has(tgacc, q), + forall|q: int| + 0 <= q < total ==> us@[q] == seq_has( + scan(ins@, tgacc, 0, no_facts())->Some_0, + q, + ), + accepted(blob@, ent) ==> policy(ins@, ent), + decreases total - p, + { + let t = match target.get(p) { + Some(t) => *t, + None => { + proof { assert(false); } + return false; + }, + }; + let u = match us.get(p) { + Some(u) => *u, + None => { + proof { assert(false); } + return false; + }, + }; + if t && !u { + proof { + assert(!accepted(blob@, ent)) by { + if accepted(blob@, ent) { + let starts = scan(ins@, tgacc, 0, no_facts())->Some_0; + super::acc_lemmas::lemma_subset_elim(tgacc, starts, p as int); + } + } + } + return false; + } + p = p.saturating_add(1); + } + proof { + let starts = scan(ins@, tgacc, 0, no_facts())->Some_0; + assert forall|i: int| 0 <= i < tgacc.len() implies seq_has(starts, #[trigger] tgacc[i]) by { + lemma_seq_has_at(tgacc, i); + } + lemma_subset_intro(tgacc, starts); + assert(policy(ins@, ent)); + assert(decodes_to(blob@, ins@)); + } + true +} + +} // verus! diff --git a/crates/clazel-codecheck/tests/validator.rs b/crates/clazel-codecheck/tests/validator.rs new file mode 100644 index 000000000..72e062dfd --- /dev/null +++ b/crates/clazel-codecheck/tests/validator.rs @@ -0,0 +1,445 @@ +//! Tests of the executable validator and decoder: accepted programs, a +//! hostile corpus (jit.md exit criterion 6, the hand-assembled part), and +//! decoder checks. The programs are the ones of `src/examples.rs`, whose +//! jump displacements are worked out there. + +use clazel_codecheck::validate::validate; +use clazel_x64::decoder_exec::decode_exec as decode; +use clazel_x64::encoder::encode_exec as encode; +use clazel_x64::spec::{AluOp, Index, Instr, Mem, Size}; + +fn ctxm(disp: i32) -> Mem { + Mem { + base: 15, + index: None, + disp, + } +} + +fn exit_seq(t: u8, code: u32) -> Vec { + vec![ + Instr::MovImm32 { + dst: t, + imm_u32: code, + }, + Instr::MovStore { + sz: Size::W64, + mem: ctxm(256), + src: t, + }, + Instr::Ret, + ] +} + +fn base_load(dst: u8, k: i32) -> Instr { + Instr::MovLoad { + sz: Size::W64, + dst, + mem: ctxm(k.wrapping_mul(16)), + } +} + +fn region_load(dst: u8, base: u8, idx: u8) -> Instr { + Instr::MovLoad { + sz: Size::W64, + dst, + mem: Mem { + base, + index: Some(Index { reg: idx, scale: 0 }), + disp: 0, + }, + } +} + +/// The bounds check of region `k` for `n` bytes: index `r1`, scratch `r5`, +/// `r6`. +fn check_seq(k: i32, n: i32, r1: i32, r2: i32) -> Vec { + let bound = Mem { + base: 15, + index: None, + disp: k.wrapping_mul(16).wrapping_add(8), + }; + vec![ + Instr::MovRR { + sz: Size::W64, + dst: 5, + src: 1, + }, + Instr::AluRI { + alu: AluOp::Add, + sz: Size::W64, + dst: 5, + imm: n, + }, + Instr::Jcc { cc: 2, rel: r1 }, + Instr::AluRM { + alu: AluOp::Cmp, + sz: Size::W64, + dst: 5, + mem: Mem { + base: 15, + index: None, + disp: bound.disp, + }, + }, + Instr::Jcc { cc: 7, rel: r2 }, + Instr::MovImm32 { dst: 6, imm_u32: 0 }, + Instr::AluRM { + alu: AluOp::Cmp, + sz: Size::W64, + dst: 1, + mem: bound, + }, + Instr::Cmovcc { + cc: 3, + sz: Size::W64, + dst: 1, + src: 6, + }, + ] +} + +fn bytes(ins: &[Instr]) -> Vec { + ins.iter().flat_map(encode).collect() +} + +fn checked_load(k: i32, n: i32) -> Vec { + let mut p = vec![base_load(2, 3)]; + p.extend(check_seq(k, n, 39, 25)); + p.push(region_load(0, 2, 1)); + p.extend(exit_seq(7, 1)); + p +} + +fn ok(ins: &[Instr]) -> bool { + validate(&bytes(ins), &[0]) +} + +#[test] +fn exit_sequence_bytes_are_accepted() { + let blob = [0xB8, 7, 0, 0, 0, 0x49, 0x89, 0x84, 0x27, 0, 1, 0, 0, 0xC3]; + assert!(validate(&blob, &[0])); + assert_eq!(bytes(&exit_seq(0, 7)), blob); +} + +#[test] +fn accepted_programs() { + assert!(ok(&checked_load(3, 8))); + // A second entry at the exit sequence (offset 63). + assert!(validate(&bytes(&checked_load(3, 8)), &[0, 63])); + // A loop. + let l = [ + Instr::AluRR { + alu: AluOp::Add, + sz: Size::W64, + dst: 0, + src: 3, + }, + Instr::Jmp { rel: -8 }, + ]; + assert!(ok(&l)); + // A store to an exit operand. + let mut p = vec![Instr::MovStore { + sz: Size::W64, + mem: ctxm(264), + src: 0, + }]; + p.extend(exit_seq(7, 1)); + assert!(ok(&p)); +} + +#[test] +fn unchecked_index_is_refused() { + let mut p = vec![base_load(2, 3), region_load(0, 2, 1)]; + p.extend(exit_seq(7, 1)); + assert!(!ok(&p)); +} + +#[test] +fn check_for_too_few_bytes_or_the_wrong_region_is_refused() { + assert!(!ok(&checked_load(3, 4))); + assert!(!ok(&checked_load(2, 8))); +} + +#[test] +fn writes_to_reserved_registers_are_refused() { + for dst in [4u8, 15] { + let mut p = vec![Instr::MovRR { + sz: Size::W64, + dst, + src: 0, + }]; + p.extend(exit_seq(7, 1)); + assert!(!ok(&p), "mov r{dst}, rax"); + let mut p = vec![Instr::MovImm32 { dst, imm_u32: 0 }]; + p.extend(exit_seq(7, 1)); + assert!(!ok(&p), "mov r{dst}d, 0"); + let mut p = vec![Instr::AluRI { + alu: AluOp::Add, + sz: Size::W64, + dst, + imm: 8, + }]; + p.extend(exit_seq(7, 1)); + assert!(!ok(&p), "add r{dst}, 8"); + } + // `cmp rsp, 8` writes nothing. + let mut p = vec![Instr::AluRI { + alu: AluOp::Cmp, + sz: Size::W64, + dst: 4, + imm: 8, + }]; + p.extend(exit_seq(7, 1)); + assert!(ok(&p)); +} + +#[test] +fn jump_into_a_bounds_check_is_refused() { + let mut p = vec![Instr::Jmp { rel: 3 }]; + p.extend(check_seq(3, 8, 31, 17)); + p.extend(exit_seq(7, 1)); + assert!(!ok(&p)); +} + +#[test] +fn jump_over_a_bounds_check_to_the_access_is_refused() { + let mut p = vec![Instr::Jmp { rel: 55 }, base_load(2, 3)]; + p.extend(check_seq(3, 8, 39, 25)); + p.push(region_load(0, 2, 1)); + p.extend(exit_seq(7, 1)); + assert!(!ok(&p)); +} + +#[test] +fn store_to_the_region_table_is_refused() { + let mut p = vec![Instr::MovStore { + sz: Size::W64, + mem: ctxm(0), + src: 0, + }]; + p.extend(exit_seq(7, 1)); + assert!(!ok(&p)); + // Out of the context altogether. + let mut p = vec![Instr::MovLoad { + sz: Size::W64, + dst: 0, + mem: ctxm(508), + }]; + p.extend(exit_seq(7, 1)); + assert!(!ok(&p)); + let mut p = vec![Instr::MovLoad { + sz: Size::W64, + dst: 0, + mem: ctxm(-8), + }]; + p.extend(exit_seq(7, 1)); + assert!(!ok(&p)); +} + +#[test] +fn running_off_the_end_is_refused() { + assert!(!ok(&[Instr::AluRR { + alu: AluOp::Add, + sz: Size::W64, + dst: 0, + src: 3 + }])); + // A conditional jump last: its fall-through leaves the blob. + let mut p = exit_seq(7, 1); + p.push(Instr::Jcc { cc: 4, rel: -14 }); + assert!(!ok(&p)); +} + +#[test] +fn bare_ret_and_other_exits_are_refused() { + assert!(!ok(&[Instr::Ret])); + // An exit sequence that stores through the wrong operand. + let p = vec![ + Instr::MovImm32 { dst: 7, imm_u32: 1 }, + Instr::MovStore { + sz: Size::W64, + mem: ctxm(264), + src: 7, + }, + Instr::Ret, + ]; + assert!(!ok(&p)); +} + +#[test] +fn entries_and_jumps_must_hit_unit_starts() { + let blob = bytes(&checked_load(3, 8)); + assert!(!validate(&blob, &[9]), "entry inside the check"); + assert!(!validate(&blob, &[blob.len()]), "entry at the end"); + assert!(!validate(&blob, &[usize::MAX]), "entry out of range"); + assert!(!ok(&[Instr::Jmp { rel: 100 }]), "jump out"); + assert!(!ok(&[Instr::Jmp { rel: -100 }]), "jump before the start"); +} + +#[test] +fn truncated_and_empty_blobs_are_refused() { + assert!(!validate(&[], &[])); + let blob = bytes(&checked_load(3, 8)); + for n in 0..blob.len() { + assert!( + !validate(blob.get(..n).unwrap_or(&[]), &[0]), + "prefix of {n} bytes" + ); + } +} + +#[test] +fn non_canonical_and_forbidden_bytes_are_refused() { + let tails = exit_seq(7, 1); + let tail = bytes(&tails); + let cases: &[(&str, &[u8])] = &[ + ("nop", &[0x90]), + ("syscall", &[0x0F, 0x05]), + ("int 0x80", &[0xCD, 0x80]), + ("jmp rax", &[0xFF, 0xE0]), + ("call rel32", &[0xE8, 0, 0, 0, 0]), + ("ret imm16", &[0xC2, 0, 0]), + ("push rax", &[0x50]), + ("pop rsp", &[0x5C]), + ("hlt", &[0xF4]), + ("add rax, rbx, direction 03", &[0x48, 0x03, 0xC3]), + ("add eax, ebx with a redundant REX", &[0x40, 0x01, 0xD8]), + ("add rax, rbx with a 66 prefix", &[0x66, 0x48, 0x01, 0xD8]), + ("add rax, imm8", &[0x48, 0x83, 0xC0, 0x01]), + ("mov rax, [rbx] (mod 00)", &[0x48, 0x8B, 0x03]), + ( + "mov rax, fs:[rbx]", + &[0x64, 0x48, 0x8B, 0x84, 0x23, 0, 0, 0, 0], + ), + ("lock add", &[0xF0, 0x48, 0x01, 0xD8]), + ("rep movsb", &[0xF3, 0xA4]), + ("rip-relative load", &[0x48, 0x8B, 0x05, 0, 0, 0, 0]), + ( + "address-size override", + &[0x67, 0x48, 0x8B, 0x84, 0x23, 0, 0, 0, 0], + ), + ("VEX", &[0xC5, 0xF8, 0x77]), + ("jmp rel8", &[0xEB, 0x00]), + ]; + for (name, bad) in cases { + let mut blob = bad.to_vec(); + blob.extend(&tail); + assert!(!validate(&blob, &[0]), "{name}"); + assert!(decode(bad).is_none(), "{name} decodes"); + } +} + +#[test] +fn the_decoder_reads_back_what_the_encoder_writes() { + let samples = [ + Instr::MovRR { + sz: Size::W64, + dst: 9, + src: 3, + }, + Instr::MovLoad { + sz: Size::W32, + dst: 12, + mem: Mem { + base: 13, + index: Some(Index { reg: 12, scale: 3 }), + disp: -5, + }, + }, + Instr::MovImm64 { + dst: 11, + imm_u64: 0x0123_4567_89AB_CDEF, + }, + Instr::MovImm32S { dst: 2, imm: -1 }, + Instr::ShiftImm { + shift: clazel_x64::spec::ShiftOp::Sar, + sz: Size::W64, + r: 14, + count: 63, + }, + Instr::Setcc { cc: 4, dst: 6 }, + Instr::Jcc { + cc: 15, + rel: i32::MIN, + }, + Instr::Ret, + ]; + for i in &samples { + let e = encode(i); + let mut padded = e.clone(); + padded.extend([0xAA, 0xBB]); + let got = decode(&padded); + assert_eq!( + got.as_ref().map(|(g, n)| (encode(g), *n)), + Some((e.clone(), e.len())) + ); + for n in 0..e.len() { + assert!( + decode(e.get(..n).unwrap_or(&[])).is_none(), + "{n}-byte prefix of {e:02X?}" + ); + } + } +} + +#[test] +fn shift_counts_out_of_range_are_refused() { + use clazel_x64::spec::ShiftOp; + for (sz, count) in [ + (Size::W64, 0u8), + (Size::W64, 64), + (Size::W32, 32), + (Size::W32, 0), + ] { + let i = Instr::ShiftImm { + shift: ShiftOp::Shl, + sz, + r: 0, + count, + }; + assert!(decode(&encode(&i)).is_none()); + } +} + +#[test] +fn random_bytes_never_panic() { + let mut x: u64 = 0x9E37_79B9_7F4A_7C15; + let mut next = move || { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + x + }; + // Miri interprets every step: 20 000 rounds took 401 s there, over + // xtask's 300 s limit, so it runs the first 2 000 of the same sequence + // (about 40 s). The full count runs in the normal test job. + let rounds: usize = if cfg!(miri) { 2_000 } else { 20_000 }; + let mut accepted = 0usize; + for round in 0..rounds { + let n = usize::try_from(next() % 64).unwrap_or(0); + let blob: Vec = (0..n).map(|_| next().to_le_bytes()[0]).collect(); + let entries: Vec = (0..round % 3) + .map(|_| usize::try_from(next() % 70).unwrap_or(0)) + .collect(); + accepted += usize::from(validate(&blob, &entries)); + } + assert!(accepted < rounds); +} + +#[test] +fn single_byte_mutations_of_an_accepted_blob_are_decided_without_panic() { + let blob = bytes(&checked_load(3, 8)); + let mut still_ok = 0usize; + for pos in 0..blob.len() { + for v in [0u8, 0x0F, 0x40, 0x49, 0x66, 0x80, 0xC3, 0xE9, 0xFF] { + let mut m = blob.clone(); + if let Some(b) = m.get_mut(pos) { + *b = v; + } + still_ok += usize::from(validate(&m, &[0])); + } + } + // Some mutations are harmless (a new exit code); most are not. + assert!(still_ok < blob.len() * 9); +} diff --git a/crates/clazel-js-syntax/Cargo.toml b/crates/clazel-js-syntax/Cargo.toml index 787d18904..27294e3ea 100644 --- a/crates/clazel-js-syntax/Cargo.toml +++ b/crates/clazel-js-syntax/Cargo.toml @@ -8,11 +8,16 @@ description = "The JavaScript parser for Clazel's Phase 7 browser (phase-7.md it [dependencies] vstd = "=0.0.0-2026-10-04-0306" +clazel-js-lexer = { path = "../clazel-js-lexer" } -# Verification tier (docs/VERIFICATION.md). Tier 0: this is the first half of -# item 7.3 (CLAUDE.md rule 3): the AST, the specification and the contract, -# with a few proven examples. There is no parser yet, so nothing about shipped -# code is proven. `target-tier` is 1 (phase-7.md, Crates). +# Verification tier (docs/VERIFICATION.md). Tier 0: the AST, the specification +# and the contract (item 7.3, first half, CLAUDE.md rule 3), and the parser +# (second half). Proven (src/check.rs): the arena the parser returns is well +# formed, inside the source text and at most MAX_DEPTH high. Not proven: that it +# is the parse the specification defines (that is tested: tests/), so the +# parser does not implement the contract's trait, there are no Kani harnesses +# (Tier 1) and no independent transcription (Tier 2). `target-tier` is 1 +# (phase-7.md, Crates). [package.metadata.clazel] tier = 0 target-tier = 1 diff --git a/crates/clazel-js-syntax/mutations.json b/crates/clazel-js-syntax/mutations.json index 3804e7b1c..3ec59a8ef 100644 --- a/crates/clazel-js-syntax/mutations.json +++ b/crates/clazel-js-syntax/mutations.json @@ -38,6 +38,45 @@ "lemma": "example_inserted_semicolon_consumes_no_token()", "witness": "semi(Stream { toks: Seq::empty(), gaps: seq![Ins::DoWhile] }, 0, 0, false)" } + }, + { + "id": "duplicate-exports-allowed", + "file": "src/spec.rs", + "find": "!names_repeat_from(ns, 0)", + "replace": "true", + "expect": "killed", + "reason": "Drops the rule of §16.2.1.1 that the ExportedNames of a module have no duplicate, so `export {a}; export {a}` would be valid: example_repeated_export_names_are_not_unique is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_repeated_export_names_are_not_unique()", + "witness": "exported_names_unique(seq![seq![0x61u32], seq![0x61u32]])" + } + }, + { + "id": "lone-low-surrogate-allowed", + "file": "src/spec.rs", + "find": "<= s[k] <= 0xDFFF ==> (k > 0 && 0xD800 <= s[k - 1] <= 0xDBFF))", + "replace": "<= s[k] <= 0xDFFF ==> (true || 0xD800 <= s[k - 1] <= 0xDBFF))", + "expect": "killed", + "reason": "Lets a low surrogate stand alone in the name of a ModuleExportName, which IsStringWellFormedUnicode forbids: example_lone_surrogate_is_ill_formed is false (its surrogate_ok_at conjunct).", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_lone_surrogate_is_ill_formed()", + "witness": "surrogate_ok_at(seq![0xDC00u32], 0)" + } + }, + { + "id": "rest-anywhere-in-list", + "file": "src/spec.rs", + "find": "!is_rest_kind(kd(a, ks[k])) && rest_free_from(a, ks, k + 1)", + "replace": "rest_free_from(a, ks, k + 1)", + "expect": "killed", + "reason": "Lets a `...rest` parameter or element come before another item of its list, so `function f(...a, b) {}` would parse, which §15.1 and §14.3.3 forbid: example_rest_ends_its_list is false (its first conjunct).", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_rest_ends_its_list()", + "witness": "rest_only_last(AstV { nodes: seq![node(NodeKind::FunctionRestParameter, Op::None, 0, 0, Seq::empty()), node(NodeKind::FunctionRestParameter, Op::None, 0, 0, Seq::empty())], root: 1 }, seq![0int, 1int])" + } } ] } diff --git a/crates/clazel-js-syntax/src/arena_d.rs b/crates/clazel-js-syntax/src/arena_d.rs new file mode 100644 index 000000000..063f408fe --- /dev/null +++ b/crates/clazel-js-syntax/src/arena_d.rs @@ -0,0 +1,10603 @@ +//! A proven check of [`spec::d`] over a whole arena, for the part of the grammar +//! it covers (item 7.3): [`check_derivation`] takes the arena of an +//! [`Ast`] that has passed [`crate::check::finish`] and the tokens the parser +//! consumed (with the gaps where it inserted a `;`), and returns +//! [`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 +//! 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. +//! +//! [`crate::exec::parse_checked`] runs [`check_derivation_canon`] on every arena +//! the parser builds, and rejects it if a covered node is `Failed`. +//! +//! **Assumptions, stated plainly:** the executable tokens are the specification's +//! (`dtok_matches`, `gaps_match`): that they are what the lexer's specification says +//! (`lexes_as`) is the lexer's contract (item 7.2), which the executable lexer does +//! not meet yet; and that every inserted `;` is justified (`gap_justified`: no +//! sentence of the grammar goes on with the offending token), which this check +//! does not look at. + +use crate::ast::*; +use crate::spec::*; +use vstd::prelude::*; + +verus! { + +/// A token as the parser consumed it. +pub struct DTok { + /// The kind, as the specification's. + pub kind: TokKind, + /// The index of the first code point. + pub start: usize, + /// One past the last code point. + pub end: usize, + /// A line terminator precedes it. + pub nl: bool, + /// Written with a `UnicodeEscapeSequence`. + pub escaped: bool, +} + +/// 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 +} + +/// The executable tokens and gaps are the specification's stream `ts`. +pub open spec fn stream_matches(src: Seq, toks: Seq, gaps: Seq, ts: Stream) -> bool { + toks.len() == ts.toks.len() && gaps =~= ts.gaps && (forall|k: int| + 0 <= k < toks.len() ==> dtok_matches(src, toks[k], #[trigger] ts.toks[k])) +} + +/// Tokens are not empty and their ends increase. +pub open spec fn toks_wf(ts: Stream) -> bool { + (forall|k: int| 0 <= k < tn(ts) ==> (#[trigger] ts.toks[k]).start < ts.toks[k].end) && ( + forall|x: int, y: int| + 0 <= x < y < tn(ts) ==> (#[trigger] ts.toks[x]).end < (#[trigger] ts.toks[y]).end) +} + +/// The arena of `nodes` with the root last. +pub open spec fn arena_of(nodes: Seq) -> AstV { + AstV { nodes: nodes.map_values(|nd: Node| nd@), root: nodes.len() - 1 } +} + +/// `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) + requires + toks_wf(ts), + 0 <= c < a.nodes.len(), + 0 <= lo < hi <= tn(ts), + a.nodes[c].start == ts.toks[lo].start, + a.nodes[c].end == ts.toks[hi - 1].end, + ensures + ed(a, ts, c, lo) == hi, +{ + assert(ts.toks[lo].start < ts.toks[lo].end); + assert(ts.toks[lo].end <= ts.toks[hi - 1].end) by { + if lo < hi - 1 { + assert(ts.toks[lo].end < ts.toks[hi - 1].end); + } + } + assert(a.nodes[c].start < a.nodes[c].end); + assert(exists|x: int| lo <= x < tn(ts) && ts.toks[x].end == a.nodes[c].end) by { + assert(ts.toks[hi - 1].end == a.nodes[c].end); + } + let y = choose|x: int| lo <= x < tn(ts) && ts.toks[x].end == a.nodes[c].end; + assert(y == hi - 1) by { + if y < hi - 1 { + assert(ts.toks[y].end < ts.toks[hi - 1].end); + } + if y > hi - 1 { + assert(ts.toks[hi - 1].end < ts.toks[y].end); + } + } +} + +/// A node is derived over tokens `lo..hi` in context `cx`, and its `ed` is `hi`. +pub open spec fn node_done(a: AstV, ts: Stream, c: int, cx: Ctx, lo: int, hi: int) -> bool { + 0 <= lo < hi <= tn(ts) && d(a, ts, c, cx, lo, hi) && ed(a, ts, c, lo) == hi +} + +/// §14.5 `ExpressionStatement : Expression ;`, for a first token that is not an +/// `IdentifierName` and not `{`. +proof fn lemma_expr_stmt_derives(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, m: int, j: int, c: int) + requires + 0 <= n < a.nodes.len(), + 0 <= i < m <= tn(ts), + a.nodes[n].kind == NodeKind::ExpressionStatement, + a.nodes[n].children =~= seq![c], + 0 <= c < n, + lvl(a, c) >= -1, + !is_kw(ts, i, "function"@), + !is_kw(ts, i, "class"@), + !is_kw(ts, i, "async"@), + !is_kw(ts, i, "let"@), + !is_p(ts, i, "{"@), + ed(a, ts, c, i) == m, + d(a, ts, c, cx_in(cx, true), i, m), + semi(ts, m, j, false), + 0 <= j <= tn(ts), + span_ok(a, ts, n, i, j), + ensures + d(a, ts, n, cx, i, j), +{ + assert(a.nodes[n].children[0] == c); + assert(dc(a, ts, n, c, cx_in(cx, true), i, m)); + assert(d_stmt(a, ts, n, cx, i, j)); +} + +/// §14.4 `EmptyStatement : ;`. +proof fn lemma_empty_statement_derives(a: AstV, ts: Stream, n: int, cx: Ctx, i: int) + requires + 0 <= n < a.nodes.len(), + 0 <= i < tn(ts), + a.nodes[n].kind == NodeKind::EmptyStatement, + a.nodes[n].children.len() == 0, + is_p(ts, i, ";"@), + span_ok(a, ts, n, i, i + 1), + ensures + d(a, ts, n, cx, i, i + 1), +{ + assert(d_stmt(a, ts, n, cx, i, i + 1)); +} + +/// §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) + requires + 0 <= k < ks.len(), + ed(a, ts, ks[k], i) == m, + dc(a, ts, bound, ks[k], cx, i, m), + dl(a, ts, bound, ks, k + 1, cx, m, j), + ensures + dl(a, ts, bound, ks, k, cx, i, j), +{ +} + +/// 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 + 0 <= n < a.nodes.len(), + a.nodes[n].kind == (if g == SourceGoal::Script { + NodeKind::Script + } else { + NodeKind::Module + }), + all_items(a, a.nodes[n].children), + dl(a, ts, n, a.nodes[n].children, 0, cx, 0, tn(ts)), + span_ok(a, ts, n, 0, tn(ts)), + ensures + d(a, ts, n, cx, 0, tn(ts)), +{ + assert(d_stmt(a, ts, n, cx, 0, tn(ts))) by { + if g == SourceGoal::Module { + assert forall|k: int| 0 <= k < a.nodes[n].children.len() implies is_module_item_kind( + kd(a, #[trigger] a.nodes[n].children[k]), + ) by {} + } + } +} + +/// The operators of the expressions the check covers: unary, binary and assignment. +pub open spec fn op_supported(op: Op) -> bool { + is_unary_op(op) || is_assign_op(op) || op == Op::Exp || op == Op::Mul || op == Op::Div || op + == Op::Rem || op == Op::Add || op == Op::Sub || op == Op::Shl || op == Op::Shr || op + == Op::UShr || op == Op::Lt || op == Op::Gt || op == Op::Le || op == Op::Ge || op + == Op::InstanceOf || op == Op::In || op == Op::Eq || op == Op::Ne || op == Op::StrictEq + || op == Op::StrictNe || op == Op::BitAnd || op == Op::BitXor || op == Op::BitOr || op + == Op::LogicalAnd || op == Op::LogicalOr || op == Op::Coalesce +} + +/// The code points of the text of an operator. +pub open spec fn op_chars(op: Op) -> Seq { + match op { + Op::Delete => seq![0x64u32, 0x65, 0x6C, 0x65, 0x74, 0x65], + Op::Void => seq![0x76u32, 0x6F, 0x69, 0x64], + Op::Typeof => seq![0x74u32, 0x79, 0x70, 0x65, 0x6F, 0x66], + Op::Plus | Op::Add => seq![0x2Bu32], + Op::Minus | Op::Sub => seq![0x2Du32], + Op::BitNot => seq![0x7Eu32], + Op::LogicalNot => seq![0x21u32], + Op::Exp => seq![0x2Au32, 0x2A], + Op::Mul => seq![0x2Au32], + Op::Div => seq![0x2Fu32], + Op::Rem => seq![0x25u32], + Op::Shl => seq![0x3Cu32, 0x3C], + Op::Shr => seq![0x3Eu32, 0x3E], + Op::UShr => seq![0x3Eu32, 0x3E, 0x3E], + Op::Lt => seq![0x3Cu32], + Op::Gt => seq![0x3Eu32], + Op::Le => seq![0x3Cu32, 0x3D], + Op::Ge => seq![0x3Eu32, 0x3D], + Op::InstanceOf => seq![0x69u32, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x63, 0x65, 0x6F, 0x66], + Op::In => seq![0x69u32, 0x6E], + Op::Eq => seq![0x3Du32, 0x3D], + Op::Ne => seq![0x21u32, 0x3D], + Op::StrictEq => seq![0x3Du32, 0x3D, 0x3D], + Op::StrictNe => seq![0x21u32, 0x3D, 0x3D], + Op::BitAnd => seq![0x26u32], + Op::BitXor => seq![0x5Eu32], + Op::BitOr => seq![0x7Cu32], + Op::LogicalAnd => seq![0x26u32, 0x26], + Op::LogicalOr => seq![0x7Cu32, 0x7C], + Op::Coalesce => seq![0x3Fu32, 0x3F], + Op::Assign => seq![0x3Du32], + Op::MulAssign => seq![0x2Au32, 0x3D], + Op::DivAssign => seq![0x2Fu32, 0x3D], + Op::RemAssign => seq![0x25u32, 0x3D], + Op::AddAssign => seq![0x2Bu32, 0x3D], + Op::SubAssign => seq![0x2Du32, 0x3D], + Op::ShlAssign => seq![0x3Cu32, 0x3C, 0x3D], + Op::ShrAssign => seq![0x3Eu32, 0x3E, 0x3D], + Op::UShrAssign => seq![0x3Eu32, 0x3E, 0x3E, 0x3D], + Op::BitAndAssign => seq![0x26u32, 0x3D], + Op::BitXorAssign => seq![0x5Eu32, 0x3D], + Op::BitOrAssign => seq![0x7Cu32, 0x3D], + Op::ExpAssign => seq![0x2Au32, 0x2A, 0x3D], + Op::LogicalAndAssign => seq![0x26u32, 0x26, 0x3D], + Op::LogicalOrAssign => seq![0x7Cu32, 0x7C, 0x3D], + Op::CoalesceAssign => seq![0x3Fu32, 0x3F, 0x3D], + _ => Seq::empty(), + } +} + +/// The text of an operator, as the specification's `op_text` and the table agree. +proof fn lemma_op_chars(op: Op) + requires + op_supported(op), + ensures + cps(op_text(op)) =~= op_chars(op), +{ + reveal_strlit("delete"); + reveal_strlit("void"); + reveal_strlit("typeof"); + reveal_strlit("+"); + reveal_strlit("-"); + reveal_strlit("~"); + reveal_strlit("!"); + reveal_strlit("**"); + reveal_strlit("*"); + reveal_strlit("/"); + reveal_strlit("%"); + reveal_strlit("<<"); + reveal_strlit(">>"); + reveal_strlit(">>>"); + reveal_strlit("<"); + reveal_strlit(">"); + reveal_strlit("?"); + reveal_strlit(":"); + reveal_strlit("."); + reveal_strlit("<="); + reveal_strlit(">="); + reveal_strlit("instanceof"); + reveal_strlit("in"); + reveal_strlit("=="); + reveal_strlit("!="); + reveal_strlit("==="); + reveal_strlit("!=="); + reveal_strlit("&"); + reveal_strlit("^"); + reveal_strlit("|"); + reveal_strlit("&&"); + reveal_strlit("||"); + reveal_strlit("??"); + reveal_strlit("="); + reveal_strlit("*="); + reveal_strlit("/="); + reveal_strlit("%="); + reveal_strlit("+="); + reveal_strlit("-="); + reveal_strlit("<<="); + reveal_strlit(">>="); + reveal_strlit(">>>="); + reveal_strlit("&="); + reveal_strlit("^="); + reveal_strlit("|="); + reveal_strlit("**="); + reveal_strlit("&&="); + reveal_strlit("||="); + reveal_strlit("??="); + match op { + Op::Delete => assert("delete"@ =~= seq!['d', 'e', 'l', 'e', 't', 'e']), + Op::Void => assert("void"@ =~= seq!['v', 'o', 'i', 'd']), + Op::Typeof => assert("typeof"@ =~= seq!['t', 'y', 'p', 'e', 'o', 'f']), + Op::Plus | Op::Add => assert("+"@ =~= seq!['+']), + Op::Minus | Op::Sub => assert("-"@ =~= seq!['-']), + Op::BitNot => assert("~"@ =~= seq!['~']), + Op::LogicalNot => assert("!"@ =~= seq!['!']), + Op::Exp => assert("**"@ =~= seq!['*', '*']), + Op::Mul => assert("*"@ =~= seq!['*']), + Op::Div => assert("/"@ =~= seq!['/']), + Op::Rem => assert("%"@ =~= seq!['%']), + Op::Shl => assert("<<"@ =~= seq!['<', '<']), + Op::Shr => assert(">>"@ =~= seq!['>', '>']), + Op::UShr => assert(">>>"@ =~= seq!['>', '>', '>']), + Op::Lt => assert("<"@ =~= seq!['<']), + Op::Gt => assert(">"@ =~= seq!['>']), + Op::Le => assert("<="@ =~= seq!['<', '=']), + Op::Ge => assert(">="@ =~= seq!['>', '=']), + Op::InstanceOf => assert("instanceof"@ =~= seq!['i', 'n', 's', 't', 'a', 'n', 'c', 'e', 'o', 'f']), + Op::In => assert("in"@ =~= seq!['i', 'n']), + Op::Eq => assert("=="@ =~= seq!['=', '=']), + Op::Ne => assert("!="@ =~= seq!['!', '=']), + Op::StrictEq => assert("==="@ =~= seq!['=', '=', '=']), + Op::StrictNe => assert("!=="@ =~= seq!['!', '=', '=']), + Op::BitAnd => assert("&"@ =~= seq!['&']), + Op::BitXor => assert("^"@ =~= seq!['^']), + Op::BitOr => assert("|"@ =~= seq!['|']), + Op::LogicalAnd => assert("&&"@ =~= seq!['&', '&']), + Op::LogicalOr => assert("||"@ =~= seq!['|', '|']), + Op::Coalesce => assert("??"@ =~= seq!['?', '?']), + Op::Assign => assert("="@ =~= seq!['=']), + Op::MulAssign => assert("*="@ =~= seq!['*', '=']), + Op::DivAssign => assert("/="@ =~= seq!['/', '=']), + Op::RemAssign => assert("%="@ =~= seq!['%', '=']), + Op::AddAssign => assert("+="@ =~= seq!['+', '=']), + Op::SubAssign => assert("-="@ =~= seq!['-', '=']), + Op::ShlAssign => assert("<<="@ =~= seq!['<', '<', '=']), + Op::ShrAssign => assert(">>="@ =~= seq!['>', '>', '=']), + Op::UShrAssign => assert(">>>="@ =~= seq!['>', '>', '>', '=']), + Op::BitAndAssign => assert("&="@ =~= seq!['&', '=']), + Op::BitXorAssign => assert("^="@ =~= seq!['^', '=']), + Op::BitOrAssign => assert("|="@ =~= seq!['|', '=']), + Op::ExpAssign => assert("**="@ =~= seq!['*', '*', '=']), + Op::LogicalAndAssign => assert("&&="@ =~= seq!['&', '&', '=']), + Op::LogicalOrAssign => assert("||="@ =~= seq!['|', '|', '=']), + Op::CoalesceAssign => assert("??="@ =~= seq!['?', '?', '=']), + _ => {}, + } +} + +/// The code points of an operator's text. +fn op_slice(op: Op) -> (r: &'static [u32]) + ensures + op_supported(op) ==> r@ =~= op_chars(op), +{ + match op { + Op::Delete => &[0x64, 0x65, 0x6C, 0x65, 0x74, 0x65], + Op::Void => &[0x76, 0x6F, 0x69, 0x64], + Op::Typeof => &[0x74, 0x79, 0x70, 0x65, 0x6F, 0x66], + Op::Plus | Op::Add => &[0x2B], + Op::Minus | Op::Sub => &[0x2D], + Op::BitNot => &[0x7E], + Op::LogicalNot => &[0x21], + Op::Exp => &[0x2A, 0x2A], + Op::Mul => &[0x2A], + Op::Div => &[0x2F], + Op::Rem => &[0x25], + Op::Shl => &[0x3C, 0x3C], + Op::Shr => &[0x3E, 0x3E], + Op::UShr => &[0x3E, 0x3E, 0x3E], + Op::Lt => &[0x3C], + Op::Gt => &[0x3E], + Op::Le => &[0x3C, 0x3D], + Op::Ge => &[0x3E, 0x3D], + Op::InstanceOf => &[0x69, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x63, 0x65, 0x6F, 0x66], + Op::In => &[0x69, 0x6E], + Op::Eq => &[0x3D, 0x3D], + Op::Ne => &[0x21, 0x3D], + Op::StrictEq => &[0x3D, 0x3D, 0x3D], + Op::StrictNe => &[0x21, 0x3D, 0x3D], + Op::BitAnd => &[0x26], + Op::BitXor => &[0x5E], + Op::BitOr => &[0x7C], + Op::LogicalAnd => &[0x26, 0x26], + Op::LogicalOr => &[0x7C, 0x7C], + Op::Coalesce => &[0x3F, 0x3F], + Op::Assign => &[0x3D], + Op::MulAssign => &[0x2A, 0x3D], + Op::DivAssign => &[0x2F, 0x3D], + Op::RemAssign => &[0x25, 0x3D], + Op::AddAssign => &[0x2B, 0x3D], + Op::SubAssign => &[0x2D, 0x3D], + Op::ShlAssign => &[0x3C, 0x3C, 0x3D], + Op::ShrAssign => &[0x3E, 0x3E, 0x3D], + Op::UShrAssign => &[0x3E, 0x3E, 0x3E, 0x3D], + Op::BitAndAssign => &[0x26, 0x3D], + Op::BitXorAssign => &[0x5E, 0x3D], + Op::BitOrAssign => &[0x7C, 0x3D], + Op::ExpAssign => &[0x2A, 0x2A, 0x3D], + Op::LogicalAndAssign => &[0x26, 0x26, 0x3D], + Op::LogicalOrAssign => &[0x7C, 0x7C, 0x3D], + Op::CoalesceAssign => &[0x3F, 0x3F, 0x3D], + _ => &[], + } +} + +/// The source text of the token is the code points `w`. +fn text_is(src: &[u32], t: &DTok, w: &[u32]) -> (r: bool) + requires + t.start <= t.end <= src.len(), + ensures + r == (src@.subrange(t.start as int, t.end as int) =~= w@), +{ + let len = t.end.saturating_sub(t.start); + if len != w.len() { + return false; + } + let mut k: usize = 0; + while k < len + invariant + t.start <= t.end <= src.len(), + len == t.end - t.start, + len == w.len(), + k <= len, + forall|q: int| 0 <= q < k ==> src@[t.start as int + q] == w@[q], + decreases len - k, + { + let a = match src.get(t.start.saturating_add(k)) { + Some(x) => *x, + None => { + return false; + }, + }; + let b = match w.get(k) { + Some(x) => *x, + None => { + return false; + }, + }; + if a != b { + proof { + assert(src@.subrange(t.start as int, t.end as int)[k as int] == src@[t.start as int + + k]); + } + return false; + } + k = k.saturating_add(1); + } + true +} + +/// The token is the operator `op`: a punctuator, or for `delete void typeof +/// instanceof in` a keyword written without escapes (`spec::is_op_tok`). +fn tok_is_op(src: &[u32], t: &DTok, op: Op) -> (r: bool) + requires + t.start <= t.end <= src.len(), + op_supported(op), + ensures + r ==> (src@.subrange(t.start as int, t.end as int) =~= cps(op_text(op)) && (if op + == Op::Delete || op == Op::Void || op == Op::Typeof || op == Op::InstanceOf || op + == Op::In { + t.kind == TokKind::IdentifierName && !t.escaped + } else { + t.kind == TokKind::Punctuator + })), +{ + let keyword = matches!(op, Op::Delete | Op::Void | Op::Typeof | Op::InstanceOf | Op::In); + let kind_ok = if keyword { + matches!(t.kind, TokKind::IdentifierName) && !t.escaped + } else { + matches!(t.kind, TokKind::Punctuator) + }; + let r = kind_ok && text_is(src, t, op_slice(op)); + proof { + lemma_op_chars(op); + } + r +} + +/// The index of the first element, named so that it is not read as a request for `first()`. +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) + requires + t.start <= t.end <= src.len(), + ensures + r == (src@.subrange(t.start as int, t.end as int) =~= seq![code]), +{ + if t.end.saturating_sub(t.start) != 1 { + return false; + } + let r = match src.get(t.start) { + Some(x) => *x == code, + None => false, + }; + proof { + let sub = src@.subrange(t.start as int, t.end as int); + assert(sub.len() == 1); + assert(sub[0] == src@[t.start as int]); + assert(sub =~= seq![src@[t.start as int]]); + } + r +} + +/// The code point of a one-character operator that the check covers. +pub open spec fn op_code(op: Op) -> u32 { + match op { + Op::Add => 0x2B, + Op::Sub => 0x2D, + Op::Mul => 0x2A, + Op::Div => 0x2F, + Op::Rem => 0x25, + Op::Lt => 0x3C, + Op::Gt => 0x3E, + _ => 0, + } +} + +/// The text of the punctuators the check reads, as code points. +proof fn lemma_punct_text() + ensures + cps(";"@) =~= seq![0x3Bu32], + cps("("@) =~= seq![0x28u32], + cps(")"@) =~= seq![0x29u32], + cps("{"@) =~= seq![0x7Bu32], + cps("+"@) =~= seq![0x2Bu32], + cps("-"@) =~= seq![0x2Du32], + cps("*"@) =~= seq![0x2Au32], + cps("/"@) =~= seq![0x2Fu32], + cps("%"@) =~= seq![0x25u32], + cps("<"@) =~= seq![0x3Cu32], + cps(">"@) =~= seq![0x3Eu32], + cps("?"@) =~= seq![0x3Fu32], + cps(":"@) =~= seq![0x3Au32], + cps("."@) =~= seq![0x2Eu32], +{ + reveal_strlit(";"); + reveal_strlit("("); + reveal_strlit(")"); + reveal_strlit("{"); + reveal_strlit("+"); + reveal_strlit("-"); + reveal_strlit("*"); + reveal_strlit("/"); + reveal_strlit("%"); + reveal_strlit("<"); + reveal_strlit(">"); + reveal_strlit("?"); + reveal_strlit(":"); + reveal_strlit("."); + assert(";"@ =~= seq![';']); + assert("("@ =~= seq!['(']); + assert(")"@ =~= seq![')']); + assert("{"@ =~= seq!['{']); + assert("+"@ =~= seq!['+']); + assert("-"@ =~= seq!['-']); + assert("*"@ =~= seq!['*']); + assert("/"@ =~= seq!['/']); + assert("%"@ =~= seq!['%']); + assert("<"@ =~= seq!['<']); + assert(">"@ =~= seq!['>']); + assert("?"@ =~= seq!['?']); + assert(":"@ =~= seq![':']); + assert("."@ =~= seq!['.']); +} + +/// A one-character operator of the check is the punctuator with its code point. +proof fn lemma_op_is_tok(ts: Stream, i: int, op: Op) + requires + op == Op::Add || op == Op::Sub || op == Op::Mul || op == Op::Div || op == Op::Rem || op + == Op::Lt || op == Op::Gt, + 0 <= i < tn(ts), + ts.toks[i].kind == TokKind::Punctuator, + ts.toks[i].text =~= seq![op_code(op)], + ensures + is_op_tok(ts, i, op), +{ + lemma_punct_text(); +} + +/// The context a node is checked in, as the check carries it: the booleans of +/// [`Ctx`] (`[Yield]`, `[Await]`, `[In]`, `[Return]`, `[Default]`). +#[derive(Clone, Copy)] +pub struct Cx5 { + /// `[Yield]`. + pub y: bool, + /// `[Await]`. + pub a: bool, + /// `[In]`. + pub i: bool, + /// `[Return]`. + pub r: bool, + /// `[Default]`. + pub d: bool, +} + +/// The specification's context of a carried one. +pub open spec fn cx_of(c: Cx5) -> Ctx { + Ctx { yield_: c.y, await_: c.a, in_: c.i, return_: c.r, deflt: c.d } +} + +/// Two carried contexts are the same. +fn cx_eq(p: &Cx5, q: &Cx5) -> (r: bool) + ensures + r == (cx_of(*p) == cx_of(*q)), +{ + p.y == q.y && p.a == q.a && p.i == q.i && p.r == q.r && p.d == q.d +} + +/// 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| + 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) +} + +/// 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] +pub open spec fn inv( + a: AstV, + ts: Stream, + cxs: Seq, + lo: Seq, + hi: Seq, + lv: Seq, + m: int, +) -> bool { + lo.len() == m && hi.len() == m && lv.len() == m && m <= cxs.len() && (forall|j: int| + 0 <= j < m ==> (#[trigger] lv[j]) as int == lvl(a, j) && (kd(a, j) != NodeKind::Absent + ==> node_done(a, ts, j, cx_of(cxs[j]), lo[j] as int, hi[j] as int))) +} + +/// The binding strengths of the kinds the check covers. +pub open spec fn covered_kind(k: NodeKind) -> bool { + k == NodeKind::NumericLiteral || k == NodeKind::StringLiteral || k + == NodeKind::RegularExpressionLiteral || k == NodeKind::ParenthesizedExpression || k + == NodeKind::AdditiveExpression || k == NodeKind::MultiplicativeExpression || k + == NodeKind::RelationalExpression || k == NodeKind::ExpressionStatement || k + == NodeKind::EmptyStatement +} + +/// §13.2.3, §13.2.7 a literal leaf: its token is found from its start. +fn check_literal( + nodes: &[Node], + toks: &[DTok], + by_start: &[usize], + m: usize, + Ghost(ts): Ghost, + Ghost(cx): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + m < nodes.len(), + arena_of(nodes@).nodes.len() == nodes.len(), + toks_wf(ts), + toks@.len() == tn(ts), + forall|k: int| 0 <= k < toks@.len() ==> (toks@[k].kind == (#[trigger] ts.toks[k]).kind + && toks@[k].start as int == ts.toks[k].start && toks@[k].end as int == ts.toks[k].end), + ensures + match r { + Some((lo, hi, l)) => node_done(arena_of(nodes@), ts, m as int, cx, lo as int, hi as int) + && l as int == lvl(arena_of(nodes@), m as int), + None => true, + }, +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let kind_ok = match nd.kind { + NodeKind::NumericLiteral => 1, + NodeKind::StringLiteral => 2, + NodeKind::RegularExpressionLiteral => 3, + NodeKind::IdentifierName => 4, + _ => 0, + }; + if kind_ok == 0 || !nd.children.is_empty() { + return None; + } + let x = match by_start.get(nd.start) { + Some(x) => *x, + None => { + return None; + }, + }; + let t = match toks.get(x) { + Some(t) => t, + None => { + return None; + }, + }; + let ok = t.start == nd.start && t.end == nd.end && (match nd.kind { + NodeKind::NumericLiteral => matches!(t.kind, TokKind::NumericLiteral), + NodeKind::StringLiteral => matches!(t.kind, TokKind::StringLiteral), + NodeKind::IdentifierName => matches!(t.kind, TokKind::IdentifierName), + _ => matches!(t.kind, TokKind::RegularExpressionLiteral), + }); + if !ok { + return None; + } + let x1 = x.saturating_add(1); + proof { + let av = arena_of(nodes@); + assert(av.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(t == &toks@[x as int]); + assert(nd.children@.len() == 0); + assert(av.nodes[m as int].children.len() == 0); + assert(av.nodes[m as int].start == nd.start as int); + assert(av.nodes[m as int].end == nd.end as int); + assert(ts.toks[x as int].start == t.start as int); + assert(ts.toks[x as int].end == t.end as int); + assert(ts.toks[x as int].kind == t.kind); + assert(x < toks.len()); + assert(toks.len() <= usize::MAX); + assert(x1 as int == x as int + 1); + assert(x as int + 1 <= tn(ts)); + match nd.kind { + NodeKind::NumericLiteral => { + assert(t.kind == TokKind::NumericLiteral); + }, + NodeKind::StringLiteral => { + assert(t.kind == TokKind::StringLiteral); + }, + NodeKind::IdentifierName => { + assert(t.kind == TokKind::IdentifierName); + }, + _ => { + assert(t.kind == TokKind::RegularExpressionLiteral); + }, + } + lemma_leaf_literal_in(av, ts, m as int, cx, x as int); + assert(span_ok(av, ts, m as int, x as int, x as int + 1)); + lemma_ed_of(av, ts, m as int, x as int, x as int + 1); + assert(lvl(av, m as int) == if nd.kind == NodeKind::IdentifierName { + -100int + } else { + 17int + }); + } + let l: i32 = if matches!(nd.kind, NodeKind::IdentifierName) { + -100 + } else { + 17 + }; + Some((x, x1, l)) +} + +/// What the helpers below assume of the executable data: the stream is the +/// specification's, the tokens are not empty and increase, and the arena is +/// well formed. +#[verifier::opaque] +pub open spec fn env_ok( + src: Seq, + nodes: Seq, + toks: Seq, + gaps: Seq, + ts: Stream, +) -> bool { + stream_matches(src, toks, gaps, ts) && toks_wf(ts) && ast_wf(arena_of(nodes)) && gaps.len() + == toks.len() + 1 +} + +/// The children of a node of a well-formed arena are before it. +proof fn lemma_child_before(a: AstV, m: int, k: int) + requires + ast_wf(a), + 0 <= m < a.nodes.len(), + 0 <= k < a.nodes[m].children.len(), + ensures + 0 <= a.nodes[m].children[k] < m, +{ +} + + +/// The kinds of binary expression: §13.6 to §13.13. +pub open spec fn is_bin_kind(k: NodeKind) -> bool { + k == NodeKind::ExponentiationExpression || k == NodeKind::MultiplicativeExpression || k + == NodeKind::AdditiveExpression || k == NodeKind::ShiftExpression || k + == NodeKind::RelationalExpression || k == NodeKind::EqualityExpression || k + == NodeKind::BitwiseANDExpression || k == NodeKind::BitwiseXORExpression || k + == NodeKind::BitwiseORExpression || k == NodeKind::LogicalANDExpression || k + == NodeKind::LogicalORExpression || k == NodeKind::CoalesceExpression +} + +/// The binding strength of a binary kind, and the least strength of its left and +/// right operands (for `??` the left may also be a `CoalesceExpression`). +pub open spec fn bin_levels(k: NodeKind) -> (int, int, int) { + match k { + NodeKind::ExponentiationExpression => (11, 13, 11), + NodeKind::MultiplicativeExpression => (10, 10, 11), + NodeKind::AdditiveExpression => (9, 9, 10), + NodeKind::ShiftExpression => (8, 8, 9), + NodeKind::RelationalExpression => (7, 7, 8), + NodeKind::EqualityExpression => (6, 6, 7), + NodeKind::BitwiseANDExpression => (5, 5, 6), + NodeKind::BitwiseXORExpression => (4, 4, 5), + NodeKind::BitwiseORExpression => (3, 3, 4), + NodeKind::LogicalANDExpression => (2, 2, 3), + NodeKind::LogicalORExpression => (1, 1, 2), + _ => (0, 3, 3), + } +} + +/// The operand levels make `binary_operands_ok`. +proof fn lemma_operands(a: AstV, k: NodeKind, op: Op, l: int, r: int) + requires + is_bin_kind(k), + lvl(a, l) >= bin_levels(k).1 || (k == NodeKind::CoalesceExpression && kd(a, l) + == NodeKind::CoalesceExpression), + lvl(a, r) >= bin_levels(k).2, + ensures + binary_operands_ok(a, k, op, l, r), +{ +} + +/// A binary node of the check whose operands and operator token are as read. +#[verifier::spinoff_prover] +proof fn lemma_binary_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + l: int, + r: int, + lo_l: int, + mi: int, + lo_r: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= l < m, + 0 <= r < m, + cx_in(cx, true) == cx, + 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), + lvl(a, l) >= bin_levels(a.nodes[m].kind).1 || (a.nodes[m].kind + == NodeKind::CoalesceExpression && kd(a, l) == NodeKind::CoalesceExpression), + lvl(a, r) >= bin_levels(a.nodes[m].kind).2, + node_done(a, ts, l, cx, lo_l, mi), + node_done(a, ts, r, cx, lo_r, j), + lo_r == mi + 1, + 0 <= mi < tn(ts), + is_op_tok(ts, mi, a.nodes[m].op), + a.nodes[m].start == ts.toks[lo_l].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, lo_l, j), + lvl(a, m) == bin_levels(a.nodes[m].kind).0, +{ + assert(span_ok(a, ts, m, lo_l, j)); + lemma_operands(a, a.nodes[m].kind, a.nodes[m].op, l, r); + crate::derive::lemma_binary_derives(a, ts, m, cx, lo_l, mi, j, l, r); + lemma_ed_of(a, ts, m, lo_l, j); +} + +/// The binding strengths of a binary kind and whether `op` is one of its operators. +fn bin_info(kind: NodeKind, op: Op) -> (r: (i32, i32, i32, bool)) + ensures + r.3 ==> (is_bin_kind(kind) && bin_op_ok(kind, op) && op_supported(op) && r.0 == bin_levels( + kind, + ).0 && r.1 == bin_levels(kind).1 && r.2 == bin_levels(kind).2), +{ + match kind { + NodeKind::ExponentiationExpression => (11, 13, 11, matches!(op, Op::Exp)), + NodeKind::MultiplicativeExpression => ( + 10, + 10, + 11, + matches!(op, Op::Mul | Op::Div | Op::Rem), + ), + NodeKind::AdditiveExpression => (9, 9, 10, matches!(op, Op::Add | Op::Sub)), + NodeKind::ShiftExpression => (8, 8, 9, matches!(op, Op::Shl | Op::Shr | Op::UShr)), + NodeKind::RelationalExpression => ( + 7, + 7, + 8, + matches!(op, Op::Lt | Op::Gt | Op::Le | Op::Ge | Op::InstanceOf | Op::In), + ), + NodeKind::EqualityExpression => ( + 6, + 6, + 7, + matches!(op, Op::Eq | Op::Ne | Op::StrictEq | Op::StrictNe), + ), + NodeKind::BitwiseANDExpression => (5, 5, 6, matches!(op, Op::BitAnd)), + NodeKind::BitwiseXORExpression => (4, 4, 5, matches!(op, Op::BitXor)), + NodeKind::BitwiseORExpression => (3, 3, 4, matches!(op, Op::BitOr)), + NodeKind::LogicalANDExpression => (2, 2, 3, matches!(op, Op::LogicalAnd)), + NodeKind::LogicalORExpression => (1, 1, 2, matches!(op, Op::LogicalOr)), + NodeKind::CoalesceExpression => (0, 3, 3, matches!(op, Op::Coalesce)), + _ => (0, 0, 0, false), + } +} + +/// §13.6 to §13.13: a binary node of the check. +#[verifier::spinoff_prover] +fn check_binary( + 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, + 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), + 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 _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let (own, lmin, rmin, opok) = bin_info(nd.kind, nd.op); + if !opok || nd.children.len() != 2 { + return None; + } + let l = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + let r = match nd.children.as_slice().get(1) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + lemma_child_before(a, m as int, 1); + } + if l >= m || r >= m { + return None; + } + let lo_l = match done.lo.get(l) { + Some(x) => *x, + None => { + return None; + }, + }; + let mi = match done.hi.get(l) { + Some(x) => *x, + None => { + return None; + }, + }; + let lo_r = match done.lo.get(r) { + Some(x) => *x, + None => { + return None; + }, + }; + let j = match done.hi.get(r) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_l = match done.lv.get(l) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_r = match done.lv.get(r) { + Some(x) => *x, + None => { + return None; + }, + }; + let left_coalesce = match nodes.get(l) { + Some(x) => matches!(x.kind, NodeKind::CoalesceExpression), + None => false, + }; + let left_ok = lvl_l >= lmin || (matches!(nd.kind, NodeKind::CoalesceExpression) + && left_coalesce); + if !left_ok || lvl_r < rmin || lo_r != mi.saturating_add(1) { + return None; + } + let opt = match toks.get(mi) { + Some(t) => t, + None => { + return None; + }, + }; + let first = match toks.get(lo_l) { + Some(t) => t, + None => { + return None; + }, + }; + let last = match toks.get(j.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, mi as int); + assert(opt == &toks@[mi as int]); + } + if !tok_is_op(src, opt, nd.op) || nd.start != first.start || nd.end != last.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(a.nodes[m as int].children =~= seq![l as int, r as int]) by { + assert(a.nodes[m as int].children.len() == 2); + assert(a.nodes[m as int].children[0] == l as int); + assert(a.nodes[m as int].children[1] == r 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, r as int, g.cx, 1); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, mi as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_l as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, j as int - 1); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(mi < toks.len()); + assert(toks.len() <= usize::MAX); + assert(lo_r == mi + 1); + assert(j >= 1); + assert(first.start as int == g.ts.toks[lo_l as int].start); + assert(last.end as int == g.ts.toks[j as int - 1].end); + 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(kd(a, l as int) == nodes@[l as int].kind); + assert(left_coalesce ==> kd(a, l as int) == NodeKind::CoalesceExpression); + let op = nd.op; + assert(dtok_matches(src@, *opt, g.ts.toks[mi as int])); + assert(is_op_tok(g.ts, mi as int, op)) by { + lemma_op_chars(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)) +} + +/// The code points of the `w`th `ReservedWord` of `spec::reserved_words` (§12.7.2). +pub open spec fn reserved_chars(w: int) -> Seq { + if w == 0 { + seq![0x61u32, 0x77, 0x61, 0x69, 0x74] + } else if w == 1 { + seq![0x62u32, 0x72, 0x65, 0x61, 0x6B] + } else if w == 2 { + seq![0x63u32, 0x61, 0x73, 0x65] + } else if w == 3 { + seq![0x63u32, 0x61, 0x74, 0x63, 0x68] + } else if w == 4 { + seq![0x63u32, 0x6C, 0x61, 0x73, 0x73] + } else if w == 5 { + seq![0x63u32, 0x6F, 0x6E, 0x73, 0x74] + } else if w == 6 { + seq![0x63u32, 0x6F, 0x6E, 0x74, 0x69, 0x6E, 0x75, 0x65] + } else if w == 7 { + seq![0x64u32, 0x65, 0x62, 0x75, 0x67, 0x67, 0x65, 0x72] + } else if w == 8 { + seq![0x64u32, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74] + } else if w == 9 { + seq![0x64u32, 0x65, 0x6C, 0x65, 0x74, 0x65] + } else if w == 10 { + seq![0x64u32, 0x6F] + } else if w == 11 { + seq![0x65u32, 0x6C, 0x73, 0x65] + } else if w == 12 { + seq![0x65u32, 0x6E, 0x75, 0x6D] + } else if w == 13 { + seq![0x65u32, 0x78, 0x70, 0x6F, 0x72, 0x74] + } else if w == 14 { + seq![0x65u32, 0x78, 0x74, 0x65, 0x6E, 0x64, 0x73] + } else if w == 15 { + seq![0x66u32, 0x61, 0x6C, 0x73, 0x65] + } else if w == 16 { + seq![0x66u32, 0x69, 0x6E, 0x61, 0x6C, 0x6C, 0x79] + } else if w == 17 { + seq![0x66u32, 0x6F, 0x72] + } else if w == 18 { + seq![0x66u32, 0x75, 0x6E, 0x63, 0x74, 0x69, 0x6F, 0x6E] + } else if w == 19 { + seq![0x69u32, 0x66] + } else if w == 20 { + seq![0x69u32, 0x6D, 0x70, 0x6F, 0x72, 0x74] + } else if w == 21 { + seq![0x69u32, 0x6E] + } else if w == 22 { + seq![0x69u32, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x63, 0x65, 0x6F, 0x66] + } else if w == 23 { + seq![0x6Eu32, 0x65, 0x77] + } else if w == 24 { + seq![0x6Eu32, 0x75, 0x6C, 0x6C] + } else if w == 25 { + seq![0x72u32, 0x65, 0x74, 0x75, 0x72, 0x6E] + } else if w == 26 { + seq![0x73u32, 0x75, 0x70, 0x65, 0x72] + } else if w == 27 { + seq![0x73u32, 0x77, 0x69, 0x74, 0x63, 0x68] + } else if w == 28 { + seq![0x74u32, 0x68, 0x69, 0x73] + } else if w == 29 { + seq![0x74u32, 0x68, 0x72, 0x6F, 0x77] + } else if w == 30 { + seq![0x74u32, 0x72, 0x75, 0x65] + } else if w == 31 { + seq![0x74u32, 0x72, 0x79] + } else if w == 32 { + seq![0x74u32, 0x79, 0x70, 0x65, 0x6F, 0x66] + } else if w == 33 { + seq![0x76u32, 0x61, 0x72] + } else if w == 34 { + seq![0x76u32, 0x6F, 0x69, 0x64] + } else if w == 35 { + seq![0x77u32, 0x68, 0x69, 0x6C, 0x65] + } else if w == 36 { + seq![0x77u32, 0x69, 0x74, 0x68] + } else { + seq![0x79u32, 0x69, 0x65, 0x6C, 0x64] + } +} + +/// The table of reserved words has the 38 words of the specification, in its order. +proof fn lemma_reserved_chars(w: int) + requires + 0 <= w < 38, + ensures + reserved_words().len() == 38, + cps(reserved_words()[w]) =~= reserved_chars(w), +{ + reveal_strlit("await"); + reveal_strlit("break"); + reveal_strlit("case"); + reveal_strlit("catch"); + reveal_strlit("class"); + reveal_strlit("const"); + reveal_strlit("continue"); + reveal_strlit("debugger"); + reveal_strlit("default"); + reveal_strlit("delete"); + reveal_strlit("do"); + reveal_strlit("else"); + reveal_strlit("enum"); + reveal_strlit("export"); + reveal_strlit("extends"); + reveal_strlit("false"); + reveal_strlit("finally"); + reveal_strlit("for"); + reveal_strlit("function"); + reveal_strlit("if"); + reveal_strlit("import"); + reveal_strlit("in"); + reveal_strlit("instanceof"); + reveal_strlit("new"); + reveal_strlit("null"); + reveal_strlit("return"); + reveal_strlit("super"); + reveal_strlit("switch"); + reveal_strlit("this"); + reveal_strlit("throw"); + reveal_strlit("true"); + reveal_strlit("try"); + reveal_strlit("typeof"); + reveal_strlit("var"); + reveal_strlit("void"); + reveal_strlit("while"); + reveal_strlit("with"); + reveal_strlit("yield"); + let rw = reserved_words(); + assert(rw.len() == 38); + assert("await"@ =~= seq!['a', 'w', 'a', 'i', 't']); + assert("break"@ =~= seq!['b', 'r', 'e', 'a', 'k']); + assert("case"@ =~= seq!['c', 'a', 's', 'e']); + assert("catch"@ =~= seq!['c', 'a', 't', 'c', 'h']); + assert("class"@ =~= seq!['c', 'l', 'a', 's', 's']); + assert("const"@ =~= seq!['c', 'o', 'n', 's', 't']); + assert("continue"@ =~= seq!['c', 'o', 'n', 't', 'i', 'n', 'u', 'e']); + assert("debugger"@ =~= seq!['d', 'e', 'b', 'u', 'g', 'g', 'e', 'r']); + assert("default"@ =~= seq!['d', 'e', 'f', 'a', 'u', 'l', 't']); + assert("delete"@ =~= seq!['d', 'e', 'l', 'e', 't', 'e']); + assert("do"@ =~= seq!['d', 'o']); + assert("else"@ =~= seq!['e', 'l', 's', 'e']); + assert("enum"@ =~= seq!['e', 'n', 'u', 'm']); + assert("export"@ =~= seq!['e', 'x', 'p', 'o', 'r', 't']); + assert("extends"@ =~= seq!['e', 'x', 't', 'e', 'n', 'd', 's']); + assert("false"@ =~= seq!['f', 'a', 'l', 's', 'e']); + assert("finally"@ =~= seq!['f', 'i', 'n', 'a', 'l', 'l', 'y']); + assert("for"@ =~= seq!['f', 'o', 'r']); + assert("function"@ =~= seq!['f', 'u', 'n', 'c', 't', 'i', 'o', 'n']); + assert("if"@ =~= seq!['i', 'f']); + assert("import"@ =~= seq!['i', 'm', 'p', 'o', 'r', 't']); + assert("in"@ =~= seq!['i', 'n']); + assert("instanceof"@ =~= seq!['i', 'n', 's', 't', 'a', 'n', 'c', 'e', 'o', 'f']); + assert("new"@ =~= seq!['n', 'e', 'w']); + assert("null"@ =~= seq!['n', 'u', 'l', 'l']); + assert("return"@ =~= seq!['r', 'e', 't', 'u', 'r', 'n']); + assert("super"@ =~= seq!['s', 'u', 'p', 'e', 'r']); + assert("switch"@ =~= seq!['s', 'w', 'i', 't', 'c', 'h']); + assert("this"@ =~= seq!['t', 'h', 'i', 's']); + assert("throw"@ =~= seq!['t', 'h', 'r', 'o', 'w']); + assert("true"@ =~= seq!['t', 'r', 'u', 'e']); + assert("try"@ =~= seq!['t', 'r', 'y']); + assert("typeof"@ =~= seq!['t', 'y', 'p', 'e', 'o', 'f']); + assert("var"@ =~= seq!['v', 'a', 'r']); + assert("void"@ =~= seq!['v', 'o', 'i', 'd']); + assert("while"@ =~= seq!['w', 'h', 'i', 'l', 'e']); + assert("with"@ =~= seq!['w', 'i', 't', 'h']); + assert("yield"@ =~= seq!['y', 'i', 'e', 'l', 'd']); +} + +/// The `w`th reserved word, as code points. +fn reserved_slice(w: usize) -> (r: &'static [u32]) + requires + w < 38, + ensures + r@ =~= reserved_chars(w as int), +{ + match w { + 0 => &[0x61, 0x77, 0x61, 0x69, 0x74], + 1 => &[0x62, 0x72, 0x65, 0x61, 0x6B], + 2 => &[0x63, 0x61, 0x73, 0x65], + 3 => &[0x63, 0x61, 0x74, 0x63, 0x68], + 4 => &[0x63, 0x6C, 0x61, 0x73, 0x73], + 5 => &[0x63, 0x6F, 0x6E, 0x73, 0x74], + 6 => &[0x63, 0x6F, 0x6E, 0x74, 0x69, 0x6E, 0x75, 0x65], + 7 => &[0x64, 0x65, 0x62, 0x75, 0x67, 0x67, 0x65, 0x72], + 8 => &[0x64, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74], + 9 => &[0x64, 0x65, 0x6C, 0x65, 0x74, 0x65], + 10 => &[0x64, 0x6F], + 11 => &[0x65, 0x6C, 0x73, 0x65], + 12 => &[0x65, 0x6E, 0x75, 0x6D], + 13 => &[0x65, 0x78, 0x70, 0x6F, 0x72, 0x74], + 14 => &[0x65, 0x78, 0x74, 0x65, 0x6E, 0x64, 0x73], + 15 => &[0x66, 0x61, 0x6C, 0x73, 0x65], + 16 => &[0x66, 0x69, 0x6E, 0x61, 0x6C, 0x6C, 0x79], + 17 => &[0x66, 0x6F, 0x72], + 18 => &[0x66, 0x75, 0x6E, 0x63, 0x74, 0x69, 0x6F, 0x6E], + 19 => &[0x69, 0x66], + 20 => &[0x69, 0x6D, 0x70, 0x6F, 0x72, 0x74], + 21 => &[0x69, 0x6E], + 22 => &[0x69, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x63, 0x65, 0x6F, 0x66], + 23 => &[0x6E, 0x65, 0x77], + 24 => &[0x6E, 0x75, 0x6C, 0x6C], + 25 => &[0x72, 0x65, 0x74, 0x75, 0x72, 0x6E], + 26 => &[0x73, 0x75, 0x70, 0x65, 0x72], + 27 => &[0x73, 0x77, 0x69, 0x74, 0x63, 0x68], + 28 => &[0x74, 0x68, 0x69, 0x73], + 29 => &[0x74, 0x68, 0x72, 0x6F, 0x77], + 30 => &[0x74, 0x72, 0x75, 0x65], + 31 => &[0x74, 0x72, 0x79], + 32 => &[0x74, 0x79, 0x70, 0x65, 0x6F, 0x66], + 33 => &[0x76, 0x61, 0x72], + 34 => &[0x76, 0x6F, 0x69, 0x64], + 35 => &[0x77, 0x68, 0x69, 0x6C, 0x65], + 36 => &[0x77, 0x69, 0x74, 0x68], + _ => &[0x79, 0x69, 0x65, 0x6C, 0x64], + } +} + +/// The index of the reserved word that is the token's text, if there is one. +fn reserved_index(src: &[u32], t: &DTok) -> (r: Option) + requires + t.start <= t.end <= src.len(), + ensures + match r { + Some(w) => w < 38 && src@.subrange(t.start as int, t.end as int) =~= reserved_chars( + w as int, + ), + None => forall|w: int| + 0 <= w < 38 ==> !(src@.subrange(t.start as int, t.end as int) =~= #[trigger] reserved_chars(w)), + }, +{ + let mut w: usize = 0; + while w < 38 + invariant + t.start <= t.end <= src.len(), + w <= 38, + forall|q: int| + 0 <= q < w ==> !(src@.subrange(t.start as int, t.end as int) =~= #[trigger] reserved_chars(q)), + decreases 38 - w, + { + if text_is(src, t, reserved_slice(w)) { + return Some(w); + } + w = w.saturating_add(1); + } + None +} + +/// The text of the token is not a reserved word, or is `yield` where `[~Yield]`, or +/// `await` where `[~Await]`: `spec::ident_tok` for a token of an identifier name. +fn ident_ok(src: &[u32], t: &DTok, yield_: bool, await_: bool) -> (r: bool) + requires + t.start <= t.end <= src.len(), + ensures + r ==> (t.kind == TokKind::IdentifierName ==> (t.escaped || !(exists|w: int| + 0 <= w < 38 && src@.subrange(t.start as int, t.end as int) =~= #[trigger] reserved_chars(w)) + || (src@.subrange(t.start as int, t.end as int) =~= reserved_chars(37) && !yield_) || ( + src@.subrange(t.start as int, t.end as int) =~= reserved_chars(0) && !await_))), +{ + if t.escaped { + return true; + } + match reserved_index(src, t) { + None => true, + Some(w) => (w == 37 && !yield_) || (w == 0 && !await_), + } +} + +/// An identifier-name token whose text is not a reserved word, or is `yield` or +/// `await` where those are identifiers, is an `Identifier` (§13.1, `spec::ident_tok`). +proof fn lemma_ident_tok(ts: Stream, x: int, cx: Ctx) + requires + 0 <= x < tn(ts), + ts.toks[x].kind == TokKind::IdentifierName, + ts.toks[x].escaped || !(exists|w: int| + 0 <= w < 38 && ts.toks[x].text =~= #[trigger] reserved_chars(w)) || (ts.toks[x].text + =~= reserved_chars(37) && !cx.yield_) || (ts.toks[x].text =~= reserved_chars(0) + && !cx.await_), + ensures + ident_tok(ts, x, cx), +{ + lemma_reserved_chars(37); + lemma_reserved_chars(0); + assert(reserved_words()[37] == "yield"@) by { + reveal_strlit("yield"); + } + assert(reserved_words()[0] == "await"@) by { + reveal_strlit("await"); + } + let t = ts.toks[x]; + if !t.escaped && is_reserved_text(t.text) { + let w = choose|w: int| + 0 <= w < reserved_words().len() && t.text =~= cps(#[trigger] reserved_words()[w]); + lemma_reserved_chars(w); + assert(t.text =~= reserved_chars(w)); + assert(exists|w2: int| 0 <= w2 < 38 && t.text =~= #[trigger] reserved_chars(w2)); + assert(is_kw(ts, x, "yield"@) || is_kw(ts, x, "await"@)); + } +} + +/// A leaf `IdentifierReference`, `BindingIdentifier` or `LabelIdentifier` of the check. +#[verifier::spinoff_prover] +proof fn lemma_ident_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].children.len() == 0, + a.nodes[m].kind == NodeKind::IdentifierReference || a.nodes[m].kind + == NodeKind::BindingIdentifier || a.nodes[m].kind == NodeKind::LabelIdentifier, + ts.toks[x].kind == TokKind::IdentifierName, + ident_tok(ts, x, cx), + 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) == (if a.nodes[m].kind == NodeKind::IdentifierReference { + 17int + } else { + -100int + }), +{ + assert(ts.toks[x].start < ts.toks[x].end); + crate::derive::lemma_leaf_identifier(a, ts, m, cx, x); + assert(span_ok(a, ts, m, x, x + 1)); + lemma_ed_of(a, ts, m, x, x + 1); +} + +/// §13.1 an identifier leaf. +#[verifier::spinoff_prover] +fn check_ident( + 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_ok(arena_of(nodes@), done.cxs@, m as int, g.cx), + 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, + }, +{ + let _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let cxm = match done.cxs.get(m) { + Some(c) => c, + None => { + return None; + }, + }; + if !matches!( + nd.kind, + NodeKind::IdentifierReference | NodeKind::BindingIdentifier | NodeKind::LabelIdentifier, + ) || !nd.children.is_empty() { + return None; + } + let x = match done.by_start.get(nd.start) { + 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::IdentifierName) || t.start != nd.start || t.end != nd.end + || !ident_ok(src, t, cxm.y, cxm.a) { + return None; + } + let x1 = x.saturating_add(1); + proof { + let a = arena_of(nodes@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(a.nodes[m as int].children.len() == 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); + assert(x < toks.len()); + assert(toks.len() <= usize::MAX); + assert(x1 as int == x as int + 1); + assert(g.ts.toks[x as int].start == t.start as int); + assert(g.ts.toks[x as int].end == t.end as int); + assert(g.ts.toks[x as int].kind == TokKind::IdentifierName); + assert(g.ts.toks[x as int].escaped == t.escaped); + assert(g.ts.toks[x as int].text =~= src@.subrange(t.start as int, t.end as int)); + assert(cxm == &done.cxs@[m as int]); + assert(g.cx.yield_ == cxm.y); + assert(g.cx.await_ == cxm.a); + lemma_ident_tok(g.ts, x as int, g.cx); + lemma_ident_done(a, g.ts, g.cx, m as int, x as int); + } + let l: i32 = if matches!(nd.kind, NodeKind::IdentifierReference) { 17 } else { -100 }; + Some((x, x1, l)) +} + +/// The last item of a `dsep` list. +proof fn lemma_dsep_last( + a: AstV, + ts: Stream, + bound: int, + ks: Seq, + k: int, + cx: Ctx, + i: int, + m: int, + j: int, + trail: bool, +) + requires + 0 <= k, + k + 1 == ks.len(), + ed(a, ts, ks[k], i) == m, + dc(a, ts, bound, ks[k], cx, i, m), + j == m || (trail && !is_rest_kind(kd(a, ks[k])) && is_p(ts, m, ","@) && j == m + 1), + ensures + dsep(a, ts, bound, ks, k, cx, i, j, trail), +{ +} + +/// An item before another in a `dsep` list. +proof fn lemma_dsep_cons( + a: AstV, + ts: Stream, + bound: int, + ks: Seq, + k: int, + cx: Ctx, + i: int, + m: int, + j: int, + trail: bool, +) + requires + 0 <= k, + k + 1 < ks.len(), + ed(a, ts, ks[k], i) == m, + dc(a, ts, bound, ks[k], cx, i, m), + is_p(ts, m, ","@), + dsep(a, ts, bound, ks, k + 1, cx, m + 1, j, trail), + ensures + dsep(a, ts, bound, ks, k, cx, i, j, trail), +{ +} + +/// A child may be in a list of arguments or of expressions. +pub open spec fn list_item_ok(a: AstV, c: int, mode: int) -> bool { + item_ok_val(lvl(a, c), kd(a, c), mode) +} + +/// What a list of mode `mode` accepts as an item of binding strength `l` and kind `k`: +/// 0 an assignment expression, 1 that or a `SpreadElement`, 2 a `VariableDeclaration`, +/// 3 a `LexicalBinding`. +pub open spec fn item_ok_val(l: int, k: NodeKind, mode: int) -> bool { + if mode == 0 { + l >= 0 + } else if mode == 1 { + l >= 0 || k == NodeKind::SpreadElement + } else if mode == 2 { + k == NodeKind::VariableDeclaration + } else if mode == 3 { + k == NodeKind::LexicalBinding + } else { + k == NodeKind::BindingElement || k == NodeKind::FunctionRestParameter + } +} + +/// `item_ok_val`, executable. +fn item_ok_exec(l: i32, k: NodeKind, mode: u8) -> (r: bool) + ensures + r == item_ok_val(l as int, k, mode as int), +{ + if mode == 0 { + l >= 0 + } else if mode == 1 { + l >= 0 || matches!(k, NodeKind::SpreadElement) + } else if mode == 2 { + matches!(k, NodeKind::VariableDeclaration) + } else if mode == 3 { + matches!(k, NodeKind::LexicalBinding) + } else { + matches!(k, NodeKind::BindingElement | NodeKind::FunctionRestParameter) + } +} + + +/// Which list `check_list` reads: the node, where its first item starts, whether a +/// spread element is an item, and whether a trailing `,` is allowed. +pub struct ListSpec { + /// The node whose children are the items. + pub m: usize, + /// The token the first item starts at (an empty list: where it ends). + pub start: usize, + /// A `SpreadElement` is an item. + pub mode: u8, + /// One trailing `,` is allowed. + pub trail: bool, +} + +/// 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( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + ls: &ListSpec, + Ghost(g): Ghost, +) -> (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), + ensures + match r { + Some(e) => ls.start <= e && e <= toks.len() && dsep( + 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, + ls.trail, + ) && forall|k: int| + 0 <= k < arena_of(nodes@).nodes[ls.m as int].children.len() ==> list_item_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 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 ls.start > toks.len() { + return None; + } + proof { + assert(dsep(a, g.ts, m as int, a.nodes[m as int].children, 0, g.cx, ls.start as int, ls.start as int, ls.trail)); + } + return Some(ls.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 = match done.lo.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let hc = match done.hi.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_c = match done.lv.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let kc_kind = match nodes.get(c) { + Some(x) => x.kind, + None => { + return None; + }, + }; + if !item_ok_exec(lvl_c, kc_kind, ls.mode) { + return None; + } + // 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) { + toks.get(hc) + } else { + None + }; + if let Some(t) = tok_after { + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int); + assert(t == &toks@[hc as int]); + } + if 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); + if comma_after { + assert(!is_rest_kind(kd(a, c as int))); + lemma_comma_text(); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int); + assert(hc < toks.len()); + assert(toks.len() <= usize::MAX); + 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, ","@)); + } else { + assert(e == hc); + } + assert(e <= toks.len()); + lemma_dsep_last( + a, + g.ts, + m as int, + a.nodes[m as int].children, + last as int, + g.cx, + lo_c as int, + hc as int, + e as int, + ls.trail, + ); + } + let mut k = last; + proof { + assert(node_done(a, g.ts, c as int, g.cx, lo_c as int, hc as int)); + } + 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), + 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), + dsep( + a, + g.ts, + m as int, + a.nodes[m as int].children, + k as int, + g.cx, + pos as int, + e as int, + ls.trail, + ), + forall|q: int| + k <= q < len ==> list_item_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 = match done.lo.get(c2) { + Some(x) => *x, + None => { + return None; + }, + }; + let hi2 = match done.hi.get(c2) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl2 = match done.lv.get(c2) { + Some(x) => *x, + None => { + return None; + }, + }; + let kind2 = match nodes.get(c2) { + Some(x) => x.kind, + None => { + return None; + }, + }; + 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 !item_ok_exec(lvl2, kind2, ls.mode) || !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(hi2 < toks.len()); + assert(toks.len() <= usize::MAX); + assert(hi2 + 1 == pos); + assert(kd(a, c2 as int) == nodes@[c2 as int].kind); + 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, ","@)); + lemma_dsep_cons( + a, + g.ts, + m as int, + a.nodes[m as int].children, + k as int - 1, + g.cx, + lo2 as int, + hi2 as int, + e as int, + ls.trail, + ); + } + proof { + assert(node_done(a, g.ts, c2 as int, g.cx, lo2 as int, hi2 as int)); + } + pos = lo2; + k = k.saturating_sub(1); + } + if pos != ls.start { + return None; + } + proof { + assert(forall|q: int| + 0 <= q < len ==> list_item_ok(a, #[trigger] a.nodes[m as int].children[q], ls.mode as int)); + } + Some(e) +} + +/// An `Arguments` node of the check whose list and brackets are as read. +#[verifier::spinoff_prover] +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), + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> list_item_ok( + a, + #[trigger] a.nodes[m].children[k], + 1, + ), + ts.toks[x].kind == TokKind::Punctuator, + ts.toks[x].text =~= seq![0x28u32], + ts.toks[e].kind == TokKind::Punctuator, + ts.toks[e].text =~= seq![0x29u32], + 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, +{ + lemma_punct_text(); + assert(is_p(ts, x, "("@)); + assert(is_p(ts, e, ")"@)); + assert(all_args(a, a.nodes[m].children)) by { + assert forall|k: int| 0 <= k < a.nodes[m].children.len() implies is_assign_expr( + a, + #[trigger] a.nodes[m].children[k], + ) || kd(a, a.nodes[m].children[k]) == NodeKind::SpreadElement by { + assert(list_item_ok(a, a.nodes[m].children[k], 1)); + } + } + assert(span_ok(a, ts, m, x, e + 1)); + assert(d_expr_lhs(a, ts, m, cx, x, e + 1)); + assert(d(a, ts, m, cx, x, e + 1)); + lemma_ed_of(a, ts, m, x, e + 1); +} + +/// §13.3.8 `( ArgumentList )`. +#[verifier::spinoff_prover] +fn check_arguments( + 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, + }, +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::Arguments) { + return None; + } + let x = match done.by_start.get(nd.start) { + Some(x) => *x, + None => { + return None; + }, + }; + let open = match toks.get(x) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int); + assert(open == &toks@[x as int]); + } + if !matches!(open.kind, TokKind::Punctuator) || !text_is1(src, open, 0x28) || open.start + != nd.start { + return None; + } + 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)) { + Some(e) => e, + None => { + return None; + }, + }; + let close = match toks.get(e) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, e as int); + assert(close == &toks@[e as int]); + } + if !matches!(close.kind, TokKind::Punctuator) || !text_is1(src, close, 0x29) || close.end + != nd.end { + return None; + } + proof { + let a = arena_of(nodes@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(x < toks.len()); + assert(toks.len() <= usize::MAX); + assert(x.saturating_add(1) as int == x as int + 1); + 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].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(open.start as int == g.ts.toks[x as int].start); + assert(close.end as int == g.ts.toks[e as int].end); + assert(g.ts.toks[x as int].kind == TokKind::Punctuator); + assert(g.ts.toks[x as int].text =~= seq![0x28u32]); + assert(g.ts.toks[e as int].kind == TokKind::Punctuator); + assert(g.ts.toks[e as int].text =~= seq![0x29u32]); + 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)); + } + lemma_arguments_done(a, g.ts, g.cx, m as int, x as int, e as int); + } + Some((x, e.saturating_add(1), -100)) +} + +/// A `CallExpression` of the check whose callee and arguments are as read. +#[verifier::spinoff_prover] +proof fn lemma_call_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + f: int, + args: int, + lo_f: int, + mf: int, + lo_a: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= f < m, + 0 <= args < m, + a.nodes[m].kind == NodeKind::CallExpression, + a.nodes[m].children =~= seq![f, args], + lvl(a, f) == 15 || lvl(a, f) == 17, + kd(a, args) == NodeKind::Arguments, + node_done(a, ts, f, cx, lo_f, mf), + node_done(a, ts, args, cx, lo_a, j), + lo_a == mf, + a.nodes[m].start == ts.toks[lo_f].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, lo_f, j), + lvl(a, m) == 15, +{ + assert(is_member_or_call(a, f)); + assert(span_ok(a, ts, m, lo_f, j)); + assert(a.nodes[m].children[0] == f); + assert(a.nodes[m].children[1] == args); + assert(dc(a, ts, m, f, cx, lo_f, mf)); + assert(dc(a, ts, m, args, cx, mf, j)); + assert(d_expr_lhs(a, ts, m, cx, lo_f, j)); + assert(d(a, ts, m, cx, lo_f, j)); + lemma_ed_of(a, ts, m, lo_f, j); +} + +/// §13.3.6 `callee Arguments`. +#[verifier::spinoff_prover] +fn check_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, + }, +{ + let _ = gaps.len(); + let _ = src.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::CallExpression) || nd.children.len() != 2 { + return None; + } + let f = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + let args = match nd.children.as_slice().get(1) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + lemma_child_before(a, m as int, 1); + } + if f >= m || args >= m { + return None; + } + let lo_f = match done.lo.get(f) { + Some(x) => *x, + None => { + return None; + }, + }; + let mf = match done.hi.get(f) { + Some(x) => *x, + None => { + return None; + }, + }; + let lo_a = match done.lo.get(args) { + Some(x) => *x, + None => { + return None; + }, + }; + let j = match done.hi.get(args) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_f = match done.lv.get(f) { + Some(x) => *x, + None => { + return None; + }, + }; + let args_ok = match nodes.get(args) { + Some(x) => matches!(x.kind, NodeKind::Arguments), + None => false, + }; + if !(lvl_f == 15 || lvl_f == 17) || !args_ok || lo_a != mf { + return None; + } + let first = match toks.get(lo_f) { + Some(t) => t, + None => { + return None; + }, + }; + let last = match toks.get(j.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + if nd.start != first.start || nd.end != last.end { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children =~= seq![f as int, args as int]) by { + assert(a.nodes[m as int].children.len() == 2); + assert(a.nodes[m as int].children[0] == f as int); + assert(a.nodes[m as int].children[1] == args as int); + } + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, f as int, g.cx, 0); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, args as int, g.cx, 1); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_f as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, j as int - 1); + assert(j >= 1); + assert(first.start as int == g.ts.toks[lo_f as int].start); + assert(last.end as int == g.ts.toks[j as int - 1].end); + 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, args as int) == nodes@[args as int].kind); + lemma_call_done(a, g.ts, g.cx, m as int, f as int, args as int, lo_f as int, mf as int, lo_a as int, j as int); + } + Some((lo_f, j, 15)) +} + +/// An `Expression` (comma list) of the check. +#[verifier::spinoff_prover] +proof fn lemma_expression_done(a: AstV, ts: Stream, cx: Ctx, m: int, lo: int, j: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= lo < j <= tn(ts), + a.nodes[m].kind == NodeKind::Expression, + a.nodes[m].children.len() >= 2, + dsep(a, ts, m, a.nodes[m].children, 0, cx, lo, j, false), + forall|k: int| + 0 <= k < a.nodes[m].children.len() ==> list_item_ok( + a, + #[trigger] a.nodes[m].children[k], + 0, + ), + a.nodes[m].start == ts.toks[lo].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, lo, j), + lvl(a, m) == -1, +{ + assert(span_ok(a, ts, m, lo, j)); + assert forall|k: int| 0 <= k < a.nodes[m].children.len() implies is_assign_expr( + a, + #[trigger] a.nodes[m].children[k], + ) by { + assert(list_item_ok(a, a.nodes[m].children[k], 0)); + } + assert(d_expr(a, ts, m, cx, lo, j)); + assert(d(a, ts, m, cx, lo, j)); + lemma_ed_of(a, ts, m, lo, j); +} + +/// §13.16 `Expression , AssignmentExpression`. +#[verifier::spinoff_prover] +fn check_expression( + 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, + }, +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::Expression) || nd.children.len() < 2 { + return None; + } + let first_c = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + } + if first_c >= m { + return None; + } + let lo = match done.lo.get(first_c) { + Some(x) => *x, + None => { + return None; + }, + }; + let ls = ListSpec { m, start: lo, mode: 0, trail: false }; + let e = match check_list(src, nodes, toks, gaps, done, &ls, Ghost(g)) { + Some(e) => e, + None => { + return None; + }, + }; + let first = match toks.get(lo) { + Some(t) => t, + None => { + return None; + }, + }; + let last = match toks.get(e.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + if lo >= e || nd.start != first.start || nd.end != last.end { + return None; + } + proof { + let a = arena_of(nodes@); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, e as int - 1); + assert(nd == &nodes@[m as int]); + 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() == nd.children.len()); + assert(e >= 1); + assert(first.start as int == g.ts.toks[lo as int].start); + assert(last.end as int == g.ts.toks[e as int - 1].end); + lemma_expression_done(a, g.ts, g.cx, m as int, lo as int, e as int); + } + Some((lo, e, -1)) +} + +/// A `SpreadElement` of the check. +#[verifier::spinoff_prover] +proof fn lemma_spread_done(a: AstV, ts: Stream, cx: Ctx, m: int, c: int, x: int, hc: int) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::SpreadElement, + a.nodes[m].children =~= seq![c], + node_done(a, ts, c, cx, x + 1, hc), + lvl(a, c) >= 0, + 0 <= x < tn(ts), + ts.toks[x].kind == TokKind::Punctuator, + ts.toks[x].text =~= seq![0x2Eu32, 0x2E, 0x2E], + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[hc - 1].end, + ensures + node_done(a, ts, m, cx, x, hc), + lvl(a, m) == -100, +{ + reveal_strlit("..."); + assert("..."@ =~= seq!['.', '.', '.']); + assert(is_p(ts, x, "..."@)); + assert(span_ok(a, ts, m, x, hc)); + assert(a.nodes[m].children[0] == c); + assert(dc(a, ts, m, c, cx_in(cx, true), x + 1, hc)); + assert(d_expr(a, ts, m, cx, x, hc)); + assert(d(a, ts, m, cx, x, hc)); + lemma_ed_of(a, ts, m, x, hc); +} + +/// §13.3.8 `... AssignmentExpression`. +#[verifier::spinoff_prover] +fn check_spread( + 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, + }, +{ + let _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::SpreadElement) || nd.children.len() != 1 { + return None; + } + let c = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + } + if c >= m { + return None; + } + let lo_c = match done.lo.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let hc = match done.hi.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_c = match done.lv.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + if lvl_c < 0 || lo_c < 1 { + return None; + } + let dots = match toks.get(lo_c.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + let last = match toks.get(hc.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_c as int - 1); + assert(dots == &toks@[lo_c as int - 1]); + } + if !matches!(dots.kind, TokKind::Punctuator) || !text_is(src, dots, &[0x2E, 0x2E, 0x2E]) + || nd.start != dots.start || nd.end != last.end { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + 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].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_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int - 1); + assert(lo_c >= 1); + assert(lo_c.saturating_sub(1) as int == lo_c as int - 1); + assert(hc >= 1); + assert(hc.saturating_sub(1) as int == hc 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(dots.start as int == g.ts.toks[lo_c as int - 1].start); + assert(last.end as int == g.ts.toks[hc as int - 1].end); + assert(g.ts.toks[lo_c as int - 1].kind == TokKind::Punctuator); + assert(g.ts.toks[lo_c as int - 1].text =~= seq![0x2Eu32, 0x2E, 0x2E]); + lemma_spread_done(a, g.ts, g.cx, m as int, c as int, lo_c as int - 1, hc as int); + } + Some((lo_c.saturating_sub(1), hc, -100)) +} + +/// The words `this`, `null`, `true` and `false`, as code points. +proof fn lemma_literal_words() + ensures + cps("this"@) =~= seq![0x74u32, 0x68, 0x69, 0x73], + cps("null"@) =~= seq![0x6Eu32, 0x75, 0x6C, 0x6C], + cps("true"@) =~= seq![0x74u32, 0x72, 0x75, 0x65], + cps("false"@) =~= seq![0x66u32, 0x61, 0x6C, 0x73, 0x65], +{ + reveal_strlit("this"); + reveal_strlit("null"); + reveal_strlit("true"); + reveal_strlit("false"); + assert("this"@ =~= seq!['t', 'h', 'i', 's']); + assert("null"@ =~= seq!['n', 'u', 'l', 'l']); + assert("true"@ =~= seq!['t', 'r', 'u', 'e']); + assert("false"@ =~= seq!['f', 'a', 'l', 's', 'e']); +} + +/// A `this`, `null`, `true` or `false` leaf of the check. +#[verifier::spinoff_prover] +proof fn lemma_kw_leaf_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].children.len() == 0, + ts.toks[x].kind == TokKind::IdentifierName, + !ts.toks[x].escaped, + match a.nodes[m].kind { + NodeKind::ThisExpression => ts.toks[x].text =~= seq![0x74u32, 0x68, 0x69, 0x73], + NodeKind::NullLiteral => ts.toks[x].text =~= seq![0x6Eu32, 0x75, 0x6C, 0x6C], + NodeKind::BooleanLiteral => ts.toks[x].text =~= seq![0x74u32, 0x72, 0x75, 0x65] + || ts.toks[x].text =~= seq![0x66u32, 0x61, 0x6C, 0x73, 0x65], + _ => false, + }, + 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) == 17, +{ + lemma_literal_words(); + assert(ts.toks[x].start < ts.toks[x].end); + crate::derive::lemma_leaf_keyword(a, ts, m, cx, x); + assert(span_ok(a, ts, m, x, x + 1)); + lemma_ed_of(a, ts, m, x, x + 1); +} + +/// §13.2.1, §13.2.3 `this`, `null`, `true`, `false`. +#[verifier::spinoff_prover] +fn check_keyword_leaf( + 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, + }, +{ + let _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !nd.children.is_empty() { + return None; + } + let x = match done.by_start.get(nd.start) { + 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]); + } + let text_ok = match nd.kind { + NodeKind::ThisExpression => text_is(src, t, &[0x74, 0x68, 0x69, 0x73]), + NodeKind::NullLiteral => text_is(src, t, &[0x6E, 0x75, 0x6C, 0x6C]), + NodeKind::BooleanLiteral => text_is(src, t, &[0x74, 0x72, 0x75, 0x65]) || text_is( + src, + t, + &[0x66, 0x61, 0x6C, 0x73, 0x65], + ), + _ => false, + }; + if !text_ok || !matches!(t.kind, TokKind::IdentifierName) || t.escaped || t.start != nd.start + || t.end != nd.end { + return None; + } + let x1 = x.saturating_add(1); + proof { + let a = arena_of(nodes@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(a.nodes[m as int].children.len() == 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); + assert(x < toks.len()); + assert(toks.len() <= usize::MAX); + assert(x1 as int == x as int + 1); + assert(g.ts.toks[x as int].start == t.start as int); + assert(g.ts.toks[x as int].end == t.end as int); + assert(g.ts.toks[x as int].kind == TokKind::IdentifierName); + assert(g.ts.toks[x as int].escaped == t.escaped); + assert(g.ts.toks[x as int].text =~= src@.subrange(t.start as int, t.end as int)); + lemma_kw_leaf_done(a, g.ts, g.cx, m as int, x as int); + } + Some((x, x1, 17)) +} + +/// 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( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + c: usize, + p: usize, + Ghost(g): Ghost, +) -> (r: bool) + requires + c < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + r ==> (p <= toks.len() && kd(arena_of(nodes@), c as int) == NodeKind::Absent && d( + arena_of(nodes@), + g.ts, + c as int, + g.cx, + p as int, + p as int, + ) && ed(arena_of(nodes@), g.ts, c as int, p as int) == p as int), +{ + let _ = gaps.len(); + let _ = src.len(); + let nd = match nodes.get(c) { + Some(x) => x, + None => { + return false; + }, + }; + if !matches!(nd.kind, NodeKind::Absent) || !nd.children.is_empty() || nd.start != nd.end + || p > toks.len() { + return false; + } + let prev: usize = if p == 0 { + 0 + } else { + match toks.get(p.saturating_sub(1)) { + Some(t) => t.end, + None => { + return false; + }, + } + }; + if prev > nd.start { + return false; + } + if p < toks.len() { + match toks.get(p) { + Some(t) => { + if nd.start > t.start { + return false; + } + }, + None => { + return false; + }, + } + } else if nd.start != prev { + return false; + } + proof { + let a = arena_of(nodes@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(nd == &nodes@[c as int]); + assert(a.nodes[c as int] == nodes@[c as int]@); + assert(a.nodes[c as int].children.len() == 0); + assert(a.nodes[c as int].kind == NodeKind::Absent); + assert(a.nodes[c as int].start == nd.start as int); + assert(a.nodes[c as int].end == nd.start as int); + if p >= 1 { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, p as int - 1); + assert(p.saturating_sub(1) as int == p as int - 1); + } + if p < toks.len() { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, p as int); + } + assert(prev_end(g.ts, p as int) == prev as int); + assert(span_ok(a, g.ts, c as int, p as int, p as int)); + assert(d_expr(a, g.ts, c as int, g.cx, p as int, p as int)); + assert(d(a, g.ts, c as int, g.cx, p as int, p as int)); + } + true +} + +/// 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( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + hc: usize, + end: usize, + Ghost(g): Ghost, +) -> (r: Option) + requires + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + match r { + Some(j) => semi(g.ts, hc as int, j as int, false) && 1 <= j && j <= toks.len() && g.ts.toks[j as int + - 1].end == end as int && hc <= j, + None => true, + }, +{ + let _ = nodes.len(); + if let Some(st) = toks.get(hc) { + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int); + assert(st == &toks@[hc as int]); + } + if matches!(st.kind, TokKind::Punctuator) && text_is1(src, st, 0x3B) && end == st.end { + proof { + lemma_punct_text(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(hc < toks.len()); + assert(toks.len() <= usize::MAX); + assert(g.ts.toks[hc as int].kind == TokKind::Punctuator); + assert(g.ts.toks[hc as int].text =~= seq![0x3Bu32]); + assert(is_p(g.ts, hc as int, ";"@)); + assert(hc.saturating_add(1) as int == hc as int + 1); + assert(st.end as int == g.ts.toks[hc as int].end); + } + return Some(hc.saturating_add(1)); + } + } + let g0 = match gaps.get(hc) { + Some(x) => x, + None => { + return None; + }, + }; + let prev = match toks.get(hc.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + if !matches!(g0, Ins::Semicolon) || hc < 1 || end != prev.end { + return None; + } + proof { + lemma_env_gap(src@, nodes@, toks@, gaps@, g.ts, hc as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int - 1); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(hc.saturating_sub(1) as int == hc as int - 1); + assert(prev.end as int == g.ts.toks[hc as int - 1].end); + assert(hc <= toks.len()); + } + Some(hc) +} + +/// The end of a list of `dsep` items is `list_next` when the last item ends it. +proof fn lemma_list_next( + a: AstV, + ts: Stream, + bound: int, + ks: Seq, + k: int, + cx: Ctx, + i: int, + j: int, +) + requires + 0 <= k < ks.len(), + dsep(a, ts, bound, ks, k, cx, i, j, false), + ensures + list_next(a, ts, ks, k, i) == j, + decreases ks.len() - k, +{ + if k + 1 < ks.len() { + let m = ed(a, ts, ks[k], i); + lemma_list_next(a, ts, bound, ks, k + 1, cx, m + 1, j); + } +} + +/// A `VariableDeclaration` or `LexicalBinding` of the check: a binding identifier and +/// an optional initializer. +#[verifier::spinoff_prover] +proof fn lemma_binding_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + t: int, + init: int, + lo_t: int, + mt: int, + j: int, + with_init: bool, + lo_i: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 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), + a.nodes[m].children =~= seq![t, init], + kd(a, t) == NodeKind::BindingIdentifier, + 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, + 0 <= j <= tn(ts), + lo_t < j, + if with_init { + lvl(a, init) >= 0 && mt < tn(ts) && ts.toks[mt].kind == TokKind::Punctuator + && ts.toks[mt].text =~= seq![0x3Du32] && lo_i == mt + 1 && node_done( + a, + ts, + init, + cx, + lo_i, + j, + ) + } else { + kd(a, init) == NodeKind::Absent && j == mt && d(a, ts, init, cx, mt, mt) && ed( + a, + ts, + init, + mt, + ) == mt + }, + ensures + node_done(a, ts, m, cx, lo_t, j), + lvl(a, m) == -100, +{ + lemma_punct_text(); + assert(span_ok(a, ts, m, lo_t, j)); + assert(a.nodes[m].children[0] == t); + assert(a.nodes[m].children[1] == init); + assert(dc(a, ts, m, t, cx, lo_t, mt)); + if with_init { + reveal_strlit("="); + 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)); + } else { + assert(dc(a, ts, m, init, cx, mt, mt)); + assert(d_init(a, ts, m, init, cx, mt, j, false)); + } + if a.nodes[m].kind == NodeKind::BindingElement { + assert(d_pat(a, ts, m, cx, lo_t, j)); + } else { + assert(d_stmt(a, ts, m, cx, lo_t, j)); + } + lemma_ed_of(a, ts, m, lo_t, j); +} + +/// §14.3.2, §14.3.1 `BindingIdentifier Initializer?`. +#[verifier::spinoff_prover] +fn check_binding( + 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, + }, +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::VariableDeclaration | NodeKind::LexicalBinding | NodeKind::BindingElement) + || nd.children.len() != 2 { + return None; + } + let t = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + let init = match nd.children.as_slice().get(1) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + lemma_child_before(a, m as int, 1); + } + if t >= m || init >= m { + return None; + } + let lo_t = match done.lo.get(t) { + Some(x) => *x, + None => { + return None; + }, + }; + let mt = match done.hi.get(t) { + Some(x) => *x, + None => { + return None; + }, + }; + let target_ok = match nodes.get(t) { + Some(x) => matches!(x.kind, NodeKind::BindingIdentifier), + None => false, + }; + if !target_ok { + return None; + } + let init_absent = match nodes.get(init) { + Some(x) => matches!(x.kind, NodeKind::Absent), + None => false, + }; + let first = match toks.get(lo_t) { + Some(t) => t, + None => { + return None; + }, + }; + if init_absent { + if !check_absent(src, nodes, toks, gaps, init, mt, Ghost(g)) { + return None; + } + let prev = match toks.get(mt.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + if mt < 1 || nd.start != first.start || nd.end != prev.end || lo_t >= mt { + 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, t as int, g.cx, 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]); + assert(a.nodes[m as int].children =~= seq![t as int, init as int]) by { + assert(a.nodes[m as int].children.len() == 2); + assert(a.nodes[m as int].children[0] == t as int); + assert(a.nodes[m as int].children[1] == init as int); + } + assert(kd(a, t as int) == nodes@[t as int].kind); + 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(mt.saturating_sub(1) as int == mt as int - 1); + 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); + } + return Some((lo_t, mt, -100)); + } + let lo_i = match done.lo.get(init) { + Some(x) => *x, + None => { + return None; + }, + }; + let j = match done.hi.get(init) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_i = match done.lv.get(init) { + Some(x) => *x, + None => { + return None; + }, + }; + let eq = match toks.get(mt) { + Some(t) => t, + None => { + return None; + }, + }; + let last = match toks.get(j.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, mt as int); + assert(eq == &toks@[mt as int]); + } + if lvl_i < 0 || !matches!(eq.kind, TokKind::Punctuator) || !text_is1(src, eq, 0x3D) + || lo_i != mt.saturating_add(1) || nd.start != first.start || nd.end != last.end { + 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, 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_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]); + assert(a.nodes[m as int].children =~= seq![t as int, init as int]) by { + assert(a.nodes[m as int].children.len() == 2); + assert(a.nodes[m as int].children[0] == t as int); + assert(a.nodes[m as int].children[1] == init as int); + } + assert(kd(a, t as int) == nodes@[t as int].kind); + 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(mt < toks.len()); + assert(toks.len() <= usize::MAX); + assert(lo_i == mt + 1); + assert(j >= 1); + assert(first.start as int == g.ts.toks[lo_t as int].start); + assert(last.end as int == g.ts.toks[j as int - 1].end); + 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(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); + } + Some((lo_t, j, -100)) +} + +/// The words `var`, `let` and `const`, as code points. +proof fn lemma_decl_words() + ensures + cps("var"@) =~= seq![0x76u32, 0x61, 0x72], + cps("let"@) =~= seq![0x6Cu32, 0x65, 0x74], + cps("const"@) =~= seq![0x63u32, 0x6F, 0x6E, 0x73, 0x74], +{ + reveal_strlit("var"); + reveal_strlit("let"); + reveal_strlit("const"); + assert("var"@ =~= seq!['v', 'a', 'r']); + assert("let"@ =~= seq!['l', 'e', 't']); + assert("const"@ =~= seq!['c', 'o', 'n', 's', 't']); +} + +/// A `VariableStatement` or `LexicalDeclaration` of the check. +#[verifier::spinoff_prover] +proof fn lemma_decl_stmt_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + x: int, + e: int, + j: int, + word: Seq, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= x < tn(ts), + a.nodes[m].children.len() > 0, + a.nodes[m].kind == NodeKind::VariableStatement || a.nodes[m].kind + == NodeKind::LexicalDeclaration, + a.nodes[m].kind == NodeKind::VariableStatement ==> (word =~= seq![0x76u32, 0x61, 0x72]), + a.nodes[m].kind == NodeKind::LexicalDeclaration ==> (a.nodes[m].op == Op::Let && word + =~= seq![0x6Cu32, 0x65, 0x74]) || (a.nodes[m].op == Op::Const && word + =~= seq![0x63u32, 0x6F, 0x6E, 0x73, 0x74]), + ts.toks[x].kind == TokKind::IdentifierName, + !ts.toks[x].escaped, + ts.toks[x].text =~= word, + 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], + if a.nodes[m].kind == NodeKind::VariableStatement { + 2int + } else { + 3int + }, + ), + semi(ts, e, j, false), + x + 1 <= e, + e <= tn(ts), + 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, +{ + lemma_decl_words(); + let ks = a.nodes[m].children; + lemma_list_next(a, ts, m, ks, 0, cx, x + 1, e); + assert(span_ok(a, ts, m, x, j)); + if a.nodes[m].kind == NodeKind::VariableStatement { + assert(is_kw(ts, x, "var"@)); + assert forall|k: int| 0 <= k < ks.len() implies kd(a, #[trigger] ks[k]) + == NodeKind::VariableDeclaration by { + assert(list_item_ok(a, ks[k], 2)); + } + } else { + assert forall|k: int| 0 <= k < ks.len() implies kd(a, #[trigger] ks[k]) + == NodeKind::LexicalBinding by { + assert(list_item_ok(a, ks[k], 3)); + } + if a.nodes[m].op == Op::Let { + assert(is_kw(ts, x, "let"@)); + } else { + assert(is_kw(ts, x, "const"@)); + } + } + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.3.2, §14.3.1 `var`, `let` or `const` and a list of bindings, then `;`. +#[verifier::spinoff_prover] +fn check_decl_stmt( + 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, + }, +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let is_var = matches!(nd.kind, NodeKind::VariableStatement); + let ok_kind = is_var || (matches!(nd.kind, NodeKind::LexicalDeclaration) && matches!( + nd.op, + Op::Let | Op::Const, + )); + if !ok_kind || nd.children.is_empty() { + return None; + } + let x = match done.by_start.get(nd.start) { + Some(x) => *x, + None => { + return None; + }, + }; + let kw = match toks.get(x) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int); + assert(kw == &toks@[x as int]); + } + let word: &'static [u32] = if is_var { + &[0x76, 0x61, 0x72] + } else if matches!(nd.op, Op::Let) { + &[0x6C, 0x65, 0x74] + } else { + &[0x63, 0x6F, 0x6E, 0x73, 0x74] + }; + if !matches!(kw.kind, TokKind::IdentifierName) || kw.escaped || !text_is(src, kw, word) + || kw.start != nd.start { + return None; + } + let ls = ListSpec { + m, + start: x.saturating_add(1), + mode: if is_var { 2 } else { 3 }, + trail: false, + }; + let e = match check_list(src, nodes, toks, gaps, done, &ls, Ghost(g)) { + Some(e) => e, + None => { + return None; + }, + }; + let j = match find_semi(src, nodes, toks, gaps, e, nd.end, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(x < toks.len()); + assert(toks.len() <= usize::MAX); + assert(x.saturating_add(1) as int == x as int + 1); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(a.nodes[m as int].children.len() == nd.children.len()); + 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(kw.start as int == g.ts.toks[x as int].start); + assert(g.ts.toks[x as int].kind == TokKind::IdentifierName); + assert(g.ts.toks[x as int].escaped == kw.escaped); + assert(g.ts.toks[x as int].text =~= src@.subrange(kw.start as int, kw.end as int)); + assert(word@ =~= (if is_var { + seq![0x76u32, 0x61, 0x72] + } else if nd.op == Op::Let { + seq![0x6Cu32, 0x65, 0x74] + } else { + seq![0x63u32, 0x6F, 0x6E, 0x73, 0x74] + })); + assert(x < j); + lemma_decl_stmt_done(a, g.ts, g.cx, m as int, x as int, e as int, j as int, word@); + } + Some((x, j, -100)) +} + +/// A statement kind, executable (`spec::is_statement_kind`). +fn stmt_kind_exec(k: NodeKind) -> (r: bool) + ensures + r ==> is_statement_kind(k), +{ + matches!( + k, + NodeKind::BlockStatement + | NodeKind::VariableStatement + | NodeKind::EmptyStatement + | NodeKind::ExpressionStatement + | NodeKind::IfStatement + | NodeKind::DoWhileStatement + | NodeKind::WhileStatement + | NodeKind::ForStatement + | NodeKind::ForInStatement + | NodeKind::ForOfStatement + | NodeKind::ContinueStatement + | NodeKind::BreakStatement + | NodeKind::ReturnStatement + | NodeKind::WithStatement + | NodeKind::SwitchStatement + | NodeKind::LabelledStatement + | NodeKind::ThrowStatement + | NodeKind::TryStatement + | NodeKind::DebuggerStatement + ) +} + +/// A `StatementListItem` kind, executable (`spec::is_item_kind`). +fn item_kind_exec(k: NodeKind) -> (r: bool) + ensures + r ==> is_item_kind(k), +{ + stmt_kind_exec(k) || matches!( + k, + NodeKind::LexicalDeclaration + | NodeKind::FunctionDeclaration + | NodeKind::GeneratorDeclaration + | NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncGeneratorDeclaration + | NodeKind::ClassDeclaration + ) +} + +/// The token range a statement list of node `m` covers, from `start` to `end`. +pub struct Rng { + /// The node whose children are the statements. + pub m: usize, + /// The first token of the list (where it ends, if empty). + pub start: usize, + /// One past the last token of the list. + pub end: usize, +} + +/// The children of `rng.m` are `StatementListItem`s that derive consecutive token +/// ranges from `start` to `end` (`spec::dl`, `spec::all_items`). +fn check_dl( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + rng: &Rng, + Ghost(g): Ghost, +) -> (r: bool) + requires + rng.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@, rng.m as int), + cx_ok(arena_of(nodes@), done.cxs@, rng.m as int, g.cx), + rng.end <= toks.len(), + ensures + r ==> (dl( + arena_of(nodes@), + g.ts, + rng.m as int, + arena_of(nodes@).nodes[rng.m as int].children, + 0, + g.cx, + rng.start as int, + rng.end as int, + ) && all_items(arena_of(nodes@), arena_of(nodes@).nodes[rng.m as int].children) && rng.start <= rng.end), +{ + let _ = (src.len(), gaps.len(), toks.len()); + let m = rng.m; + let nd = match nodes.get(m) { + Some(x) => x, + 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); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let mut k = len; + let mut pos = rng.end; + 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, + pos <= toks.len(), + pos <= rng.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), + 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, + rng.end as int, + ), + forall|q: int| + k <= q < len ==> is_item_kind(kd(a, #[trigger] a.nodes[m as int].children[q])), + decreases k, + { + let c = match nd.children.as_slice().get(k.saturating_sub(1)) { + Some(x) => *x as usize, + None => { + return false; + }, + }; + 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] == c as int); + lemma_child_before(a, m as int, k as int - 1); + } + if c >= m { + return false; + } + let lo_c = match done.lo.get(c) { + Some(x) => *x, + None => { + return false; + }, + }; + let hi_c = match done.hi.get(c) { + Some(x) => *x, + None => { + return false; + }, + }; + let item = match nodes.get(c) { + Some(x) => item_kind_exec(x.kind), + None => false, + }; + if hi_c != pos || !item { + return false; + } + proof { + assert(a.nodes[c as int] == nodes@[c as int]@); + assert(kd(a, c as int) == nodes@[c as int].kind); + assert(is_item_kind(kd(a, c as int))); + assert(kd(a, c as int) != NodeKind::Absent); + 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); + let ks = a.nodes[m as int].children; + assert(node_done(a, g.ts, c as int, g.cx, lo_c as int, hi_c as int)); + lemma_dl_cons(a, g.ts, m as int, ks, k as int - 1, g.cx, lo_c as int, hi_c as int, rng.end as int); + } + pos = lo_c; + k = k.saturating_sub(1); + } + if pos != rng.start { + return false; + } + proof { + assert(all_items(a, a.nodes[m as int].children)); + } + true +} + +/// The words `if`, `else`, `while` and `throw`, as code points. +proof fn lemma_stmt_words() + ensures + cps("if"@) =~= seq![0x69u32, 0x66], + cps("else"@) =~= seq![0x65u32, 0x6C, 0x73, 0x65], + cps("while"@) =~= seq![0x77u32, 0x68, 0x69, 0x6C, 0x65], + cps("throw"@) =~= seq![0x74u32, 0x68, 0x72, 0x6F, 0x77], +{ + reveal_strlit("if"); + reveal_strlit("else"); + reveal_strlit("while"); + reveal_strlit("throw"); + assert("if"@ =~= seq!['i', 'f']); + assert("else"@ =~= seq!['e', 'l', 's', 'e']); + assert("while"@ =~= seq!['w', 'h', 'i', 'l', 'e']); + assert("throw"@ =~= seq!['t', 'h', 'r', 'o', 'w']); +} + +/// A `Block` of the check. +#[verifier::spinoff_prover] +proof fn lemma_block_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 < e < tn(ts), + a.nodes[m].kind == NodeKind::BlockStatement || a.nodes[m].kind == NodeKind::FunctionBody, + all_items(a, a.nodes[m].children), + dl(a, ts, m, a.nodes[m].children, 0, cx, x + 1, e), + ts.toks[x].kind == TokKind::Punctuator, + ts.toks[x].text =~= seq![0x7Bu32], + ts.toks[e].kind == TokKind::Punctuator, + ts.toks[e].text =~= seq![0x7Du32], + 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, +{ + lemma_brace_text(); + assert(is_p(ts, x, "{"@)); + assert(is_p(ts, e, "}"@)); + assert(span_ok(a, ts, m, x, e + 1)); + if a.nodes[m].kind == NodeKind::BlockStatement { + assert(d_stmt(a, ts, m, cx, x, e + 1)); + } else { + assert(d_func(a, ts, m, cx, x, e + 1)); + } + lemma_ed_of(a, ts, m, x, e + 1); +} + +/// The brace texts as code points. +proof fn lemma_brace_text() + ensures + cps("{"@) =~= seq![0x7Bu32], + cps("}"@) =~= seq![0x7Du32], +{ + reveal_strlit("{"); + reveal_strlit("}"); + assert("{"@ =~= seq!['{']); + assert("}"@ =~= seq!['}']); +} + +/// §14.2 `{ StatementList? }`. +#[verifier::spinoff_prover] +fn check_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), + 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, + }, +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::BlockStatement | NodeKind::FunctionBody) { + return None; + } + let x = match done.by_start.get(nd.start) { + Some(x) => *x, + None => { + return None; + }, + }; + let open = match toks.get(x) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int); + assert(open == &toks@[x as int]); + } + if !matches!(open.kind, TokKind::Punctuator) || !text_is1(src, open, 0x7B) || open.start + != nd.start { + return None; + } + let len = nd.children.len(); + let close: usize = if len == 0 { + x.saturating_add(1) + } else { + let last = match nd.children.as_slice().get(len.saturating_sub(1)) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + match done.hi.get(last) { + Some(h) => *h, + None => { + return None; + }, + } + }; + if close > toks.len() { + return None; + } + let rng = Rng { m, start: x.saturating_add(1), end: close }; + if !check_dl(src, nodes, toks, gaps, done, &rng, Ghost(g)) { + return None; + } + let cl = match toks.get(close) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, close as int); + assert(cl == &toks@[close as int]); + } + if !matches!(cl.kind, TokKind::Punctuator) || !text_is1(src, cl, 0x7D) || cl.end != nd.end { + return None; + } + proof { + let a = arena_of(nodes@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(x < toks.len()); + assert(toks.len() <= usize::MAX); + assert(x.saturating_add(1) as int == x as int + 1); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int] == nodes@[m as int]@); + 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(open.start as int == g.ts.toks[x as int].start); + assert(cl.end as int == g.ts.toks[close as int].end); + assert(g.ts.toks[x as int].kind == TokKind::Punctuator); + assert(g.ts.toks[x as int].text =~= seq![0x7Bu32]); + assert(g.ts.toks[close as int].kind == TokKind::Punctuator); + assert(g.ts.toks[close as int].text =~= seq![0x7Du32]); + assert(a.nodes[m as int].children.len() == len); + assert(x as int + 1 <= close as int); + lemma_block_done(a, g.ts, g.cx, m as int, x as int, close as int); + } + Some((x, close.saturating_add(1), -100)) +} + +/// An `IfStatement` of the check. +#[verifier::spinoff_prover] +proof fn lemma_if_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + t: int, + c: int, + alt: int, + x: int, + lo_t: int, + mt: int, + lo_c: int, + q: int, + j: int, + lo_a: int, + with_else: bool, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= t < m, + 0 <= c < m, + 0 <= alt < m, + a.nodes[m].kind == NodeKind::IfStatement, + a.nodes[m].children =~= seq![t, c, alt], + 0 <= x, + x + 1 < tn(ts), + ts.toks[x].kind == TokKind::IdentifierName, + !ts.toks[x].escaped, + ts.toks[x].text =~= seq![0x69u32, 0x66], + ts.toks[x + 1].kind == TokKind::Punctuator, + ts.toks[x + 1].text =~= seq![0x28u32], + lo_t == x + 2, + node_done(a, ts, t, cx, lo_t, mt), + lvl(a, t) >= -1, + mt < tn(ts), + ts.toks[mt].kind == TokKind::Punctuator, + ts.toks[mt].text =~= seq![0x29u32], + lo_c == mt + 1, + node_done(a, ts, c, cx, lo_c, q), + is_stmt_or_function(a, c), + 0 <= j <= tn(ts), + if with_else { + q < tn(ts) && ts.toks[q].kind == TokKind::IdentifierName && !ts.toks[q].escaped + && ts.toks[q].text =~= seq![0x65u32, 0x6C, 0x73, 0x65] && is_stmt_or_function( + a, + alt, + ) && lo_a == q + 1 && node_done(a, ts, alt, cx, lo_a, j) + } else { + kd(a, alt) == NodeKind::Absent && j == q && d(a, ts, alt, cx, q, q) && ed( + a, + ts, + alt, + q, + ) == q + }, + a.nodes[m].start == ts.toks[x].start, + a.nodes[m].end == ts.toks[j - 1].end, + x < j, + ensures + node_done(a, ts, m, cx, x, j), + lvl(a, m) == -100, +{ + lemma_stmt_words(); + lemma_punct_text(); + assert(is_kw(ts, x, "if"@)); + assert(is_p(ts, x + 1, "("@)); + assert(is_p(ts, mt, ")"@)); + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == t); + assert(a.nodes[m].children[1] == c); + assert(a.nodes[m].children[2] == alt); + assert(dc(a, ts, m, t, cx_in(cx, true), x + 2, mt)); + assert(dc(a, ts, m, c, cx, mt + 1, q)); + if with_else { + assert(is_kw(ts, q, "else"@)); + assert(dc(a, ts, m, alt, cx, q + 1, j)); + } else { + assert(dc(a, ts, m, alt, cx, q, q)); + } + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.6 `if ( Expression ) Statement else Statement`. +#[verifier::spinoff_prover] +fn check_if( + 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, + }, +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::IfStatement) || nd.children.len() != 3 { + return None; + } + let t = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + let c = match nd.children.as_slice().get(1) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + let alt = match nd.children.as_slice().get(2) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + lemma_child_before(a, m as int, 1); + lemma_child_before(a, m as int, 2); + } + if t >= m || c >= m || alt >= m { + return None; + } + let x = match done.by_start.get(nd.start) { + Some(x) => *x, + None => { + return None; + }, + }; + let lo_t = match done.lo.get(t) { + Some(x) => *x, + None => { + return None; + }, + }; + let mt = match done.hi.get(t) { + Some(x) => *x, + None => { + return None; + }, + }; + let lo_c = match done.lo.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let q = match done.hi.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_t = match done.lv.get(t) { + Some(x) => *x, + None => { + return None; + }, + }; + let c_ok = match nodes.get(c) { + Some(x) => stmt_kind_exec(x.kind), + None => false, + }; + let kw = match toks.get(x) { + Some(t) => t, + None => { + return None; + }, + }; + let lp = match toks.get(x.saturating_add(1)) { + Some(t) => t, + None => { + return None; + }, + }; + let rp = match toks.get(mt) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + assert(x < toks.len()); + assert(toks.len() <= usize::MAX); + assert(x.saturating_add(1) as int == x as int + 1); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int + 1); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, mt as int); + assert(kw == &toks@[x as int]); + assert(lp == &toks@[x as int + 1]); + assert(rp == &toks@[mt as int]); + } + if !c_ok || lvl_t < -1 || !matches!(kw.kind, TokKind::IdentifierName) || kw.escaped + || !text_is(src, kw, &[0x69, 0x66]) || kw.start != nd.start || !matches!( + lp.kind, + TokKind::Punctuator, + ) || !text_is1(src, lp, 0x28) || lo_t != x.saturating_add(2) || !matches!( + rp.kind, + TokKind::Punctuator, + ) || !text_is1(src, rp, 0x29) || lo_c != mt.saturating_add(1) { + return None; + } + let alt_absent = match nodes.get(alt) { + Some(a) => matches!(a.kind, NodeKind::Absent), + None => false, + }; + let (j, lo_a): (usize, usize) = if alt_absent { + if !check_absent(src, nodes, toks, gaps, alt, q, Ghost(g)) { + return None; + } + (q, 0) + } else { + let alt_ok = match nodes.get(alt) { + Some(x) => stmt_kind_exec(x.kind), + None => false, + }; + let ej = match done.hi.get(alt) { + Some(x) => *x, + None => { + return None; + }, + }; + let lo_a = match done.lo.get(alt) { + Some(x) => *x, + None => { + return None; + }, + }; + let ek = match toks.get(q) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, q as int); + assert(ek == &toks@[q as int]); + } + if !alt_ok || !matches!(ek.kind, TokKind::IdentifierName) || ek.escaped || !text_is( + src, + ek, + &[0x65, 0x6C, 0x73, 0x65], + ) || lo_a != q.saturating_add(1) { + return None; + } + (ej, lo_a) + }; + let _ = lo_a; + let last = match toks.get(j.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + if nd.end != last.end || j <= x { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children =~= seq![t as int, c as int, alt as int]) by { + assert(a.nodes[m as int].children.len() == 3); + assert(a.nodes[m as int].children[0] == t as int); + assert(a.nodes[m as int].children[1] == c as int); + assert(a.nodes[m as int].children[2] == alt 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_env_tok(src@, nodes@, toks@, gaps@, g.ts, j as int - 1); + assert(x < toks.len()); + assert(toks.len() <= usize::MAX); + assert(x.saturating_add(1) as int == x as int + 1); + assert(x.saturating_add(2) as int == x as int + 2); + assert(mt < toks.len()); + assert(lo_c == mt + 1); + assert(j >= 1); + assert(j.saturating_sub(1) as int == j 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(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); + assert(g.ts.toks[x as int].kind == TokKind::IdentifierName); + assert(g.ts.toks[x as int].escaped == kw.escaped); + assert(g.ts.toks[x as int].text =~= src@.subrange(kw.start as int, kw.end as int)); + assert(g.ts.toks[x as int + 1].kind == TokKind::Punctuator); + assert(g.ts.toks[x as int + 1].text =~= seq![0x28u32]); + assert(g.ts.toks[mt as int].kind == TokKind::Punctuator); + assert(g.ts.toks[mt as int].text =~= seq![0x29u32]); + assert(j <= toks.len()); + if alt_absent { + assert(kd(a, alt as int) == NodeKind::Absent); + lemma_if_done(a, g.ts, g.cx, m as int, t as int, c as int, alt as int, x as int, lo_t as int, mt as int, lo_c as int, q as int, j as int, 0, false); + } else { + let ek = toks@[q as int]; + 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); + assert(g.ts.toks[q as int].kind == TokKind::IdentifierName); + assert(g.ts.toks[q as int].text =~= seq![0x65u32, 0x6C, 0x73, 0x65]); + lemma_if_done(a, g.ts, g.cx, m as int, t as int, c as int, alt as int, x as int, lo_t as int, mt as int, lo_c as int, q as int, j as int, lo_a as int, true); + } + } + Some((x, j, -100)) +} + +/// The `,` text as code points. +proof fn lemma_comma_text() + ensures + cps(","@) =~= seq![0x2Cu32], +{ + reveal_strlit(","); + assert(","@ =~= seq![',']); +} + +/// The words that cannot start an `ExpressionStatement` (§14.5), as code points. +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], + cps("async"@) =~= seq![0x61u32, 0x73, 0x79, 0x6E, 0x63], + cps("let"@) =~= seq![0x6Cu32, 0x65, 0x74], +{ + reveal_strlit("function"); + reveal_strlit("class"); + reveal_strlit("async"); + reveal_strlit("let"); + assert("function"@ =~= seq!['f', 'u', 'n', 'c', 't', 'i', 'o', 'n']); + assert("class"@ =~= seq!['c', 'l', 'a', 's', 's']); + assert("async"@ =~= seq!['a', 's', 'y', 'n', 'c']); + assert("let"@ =~= seq!['l', 'e', 't']); +} + +/// A unary node of the check whose operand and operator token are as read. +#[verifier::spinoff_prover] +proof fn lemma_unary_done(a: AstV, ts: Stream, cx: Ctx, m: int, c: int, lo_c: int, hc: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::UnaryExpression, + a.nodes[m].children =~= seq![c], + is_unary_op(a.nodes[m].op), + 1 <= lo_c, + node_done(a, ts, c, cx, lo_c, hc), + lvl(a, c) >= 12, + is_op_tok(ts, lo_c - 1, a.nodes[m].op), + a.nodes[m].start == ts.toks[lo_c - 1].start, + a.nodes[m].end == ts.toks[hc - 1].end, + ensures + node_done(a, ts, m, cx, lo_c - 1, hc), + lvl(a, m) == 12, +{ + assert(span_ok(a, ts, m, lo_c - 1, hc)); + crate::derive::lemma_unary_derives(a, ts, m, cx, lo_c - 1, hc, c); + lemma_ed_of(a, ts, m, lo_c - 1, hc); +} + +/// §13.5 `delete void typeof + - ~ !`. +#[verifier::spinoff_prover] +fn check_unary( + 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, + }, +{ + let _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::UnaryExpression) || nd.children.len() != 1 || !matches!( + nd.op, + Op::Delete | Op::Void | Op::Typeof | Op::Plus | Op::Minus | Op::BitNot | Op::LogicalNot, + ) { + return None; + } + let c = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + } + if c >= m { + return None; + } + let lo_c = match done.lo.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let hc = match done.hi.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_c = match done.lv.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + if lvl_c < 12 || lo_c < 1 { + return None; + } + let opt = match toks.get(lo_c.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + let last = match toks.get(hc.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_c as int - 1); + assert(opt == &toks@[lo_c as int - 1]); + } + if !tok_is_op(src, opt, nd.op) || nd.start != opt.start || nd.end != last.end { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + 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].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_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int - 1); + assert(lo_c >= 1); + assert(lo_c.saturating_sub(1) as int == lo_c as int - 1); + assert(hc >= 1); + assert(hc.saturating_sub(1) as int == hc 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(opt.start as int == g.ts.toks[lo_c as int - 1].start); + assert(last.end as int == g.ts.toks[hc as int - 1].end); + assert(dtok_matches(src@, *opt, g.ts.toks[lo_c as int - 1])); + assert(is_op_tok(g.ts, lo_c as int - 1, nd.op)) by { + lemma_op_chars(nd.op); + } + lemma_unary_done(a, g.ts, g.cx, m as int, c as int, lo_c as int, hc as int); + } + Some((lo_c.saturating_sub(1), hc, 12)) +} + +/// A `ConditionalExpression` of the check whose parts are as read. +#[verifier::spinoff_prover] +proof fn lemma_conditional_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + t: int, + c: int, + e: int, + lo_t: int, + mt: int, + lo_c: int, + mc: int, + lo_e: int, + j: int, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= t < m, + 0 <= c < m, + 0 <= e < m, + a.nodes[m].kind == NodeKind::ConditionalExpression, + a.nodes[m].children =~= seq![t, c, e], + lvl(a, t) >= 1 || kd(a, t) == NodeKind::CoalesceExpression, + 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, e, cx, lo_e, j), + lo_c == mt + 1, + lo_e == mc + 1, + mt < tn(ts), + mc < tn(ts), + ts.toks[mt].kind == TokKind::Punctuator, + ts.toks[mt].text =~= seq![0x3Fu32], + ts.toks[mc].kind == TokKind::Punctuator, + ts.toks[mc].text =~= seq![0x3Au32], + a.nodes[m].start == ts.toks[lo_t].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, lo_t, j), + lvl(a, m) == 0, +{ + lemma_punct_text(); + assert(is_p(ts, mt, "?"@)); + assert(is_p(ts, mc, ":"@)); + assert(span_ok(a, ts, m, lo_t, j)); + crate::derive::lemma_conditional_derives(a, ts, m, cx, lo_t, mt, mc, j, t, c, e); + lemma_ed_of(a, ts, m, lo_t, j); +} + +/// §13.14 `test ? consequent : alternate`. +#[verifier::spinoff_prover] +fn check_conditional( + 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, + }, +{ + let _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ConditionalExpression) || nd.children.len() != 3 { + return None; + } + let t = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + let c = match nd.children.as_slice().get(1) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + let e = match nd.children.as_slice().get(2) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + lemma_child_before(a, m as int, 1); + lemma_child_before(a, m as int, 2); + } + if t >= m || c >= m || e >= m { + return None; + } + let lo_t = match done.lo.get(t) { + Some(x) => *x, + None => { + return None; + }, + }; + let mt = match done.hi.get(t) { + Some(x) => *x, + None => { + return None; + }, + }; + let lo_c = match done.lo.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let mc = match done.hi.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let lo_e = match done.lo.get(e) { + Some(x) => *x, + None => { + return None; + }, + }; + let j = match done.hi.get(e) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_t = match done.lv.get(t) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_c = match done.lv.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_e = match done.lv.get(e) { + Some(x) => *x, + None => { + return None; + }, + }; + let test_coalesce = match nodes.get(t) { + Some(x) => matches!(x.kind, NodeKind::CoalesceExpression), + None => false, + }; + if !(lvl_t >= 1 || test_coalesce) || lvl_c < 0 || lvl_e < 0 || lo_c != mt.saturating_add(1) + || lo_e != mc.saturating_add(1) { + return None; + } + let q = match toks.get(mt) { + Some(x) => x, + None => { + return None; + }, + }; + let k = match toks.get(mc) { + Some(x) => x, + None => { + return None; + }, + }; + let first = match toks.get(lo_t) { + Some(x) => x, + None => { + return None; + }, + }; + let last = match toks.get(j.saturating_sub(1)) { + Some(x) => x, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, mt as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, mc as int); + assert(q == &toks@[mt as int]); + assert(k == &toks@[mc as int]); + } + if !matches!(q.kind, TokKind::Punctuator) || !text_is1(src, q, 0x3F) || !matches!( + k.kind, + TokKind::Punctuator, + ) || !text_is1(src, k, 0x3A) || nd.start != first.start || nd.end != last.end { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children =~= seq![t as int, c as int, e as int]) by { + assert(a.nodes[m as int].children.len() == 3); + assert(a.nodes[m as int].children[0] == t as int); + 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_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()); + assert(mc < toks.len()); + assert(toks.len() <= usize::MAX); + assert(lo_c == mt + 1); + assert(lo_e == mc + 1); + assert(j >= 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(kd(a, t as int) == nodes@[t as int].kind); + assert(first.start as int == g.ts.toks[lo_t as int].start); + assert(last.end as int == g.ts.toks[j as int - 1].end); + assert(g.ts.toks[mt as int].kind == TokKind::Punctuator); + assert(g.ts.toks[mt as int].text =~= seq![0x3Fu32]); + assert(g.ts.toks[mc as int].kind == TokKind::Punctuator); + assert(g.ts.toks[mc as int].text =~= seq![0x3Au32]); + lemma_conditional_done(a, g.ts, g.cx, m as int, t as int, c as int, e as int, lo_t as int, mt as int, lo_c as int, mc as int, lo_e as int, j as int); + } + Some((lo_t, j, 0)) +} + +/// An assignment node of the check whose parts are as read. +#[verifier::spinoff_prover] +proof fn lemma_assign_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + l: int, + r: int, + lo_l: int, + mi: int, + lo_r: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= l < m, + 0 <= r < m, + 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, + lvl(a, r) >= 0, + node_done(a, ts, l, cx, lo_l, mi), + node_done(a, ts, r, cx, lo_r, j), + lo_r == mi + 1, + 0 <= mi < tn(ts), + is_op_tok(ts, mi, a.nodes[m].op), + a.nodes[m].start == ts.toks[lo_l].start, + a.nodes[m].end == ts.toks[j - 1].end, + ensures + node_done(a, ts, m, cx, lo_l, j), + 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); +} + +/// §13.15 `=` and the compound assignment operators. +#[verifier::spinoff_prover] +fn check_assign( + 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, + }, +{ + let _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::AssignmentExpression) || nd.children.len() != 2 || !matches!( + nd.op, + Op::Assign + | Op::MulAssign + | Op::DivAssign + | Op::RemAssign + | Op::AddAssign + | Op::SubAssign + | Op::ShlAssign + | Op::ShrAssign + | Op::UShrAssign + | Op::BitAndAssign + | Op::BitXorAssign + | Op::BitOrAssign + | Op::ExpAssign + | Op::LogicalAndAssign + | Op::LogicalOrAssign + | Op::CoalesceAssign, + ) { + return None; + } + let l = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + let r = match nd.children.as_slice().get(1) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + lemma_child_before(a, m as int, 1); + } + if l >= m || r >= m { + return None; + } + let lo_l = match done.lo.get(l) { + Some(x) => *x, + None => { + return None; + }, + }; + let mi = match done.hi.get(l) { + Some(x) => *x, + None => { + return None; + }, + }; + let lo_r = match done.lo.get(r) { + Some(x) => *x, + None => { + return None; + }, + }; + let j = match done.hi.get(r) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_l = match done.lv.get(l) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_r = match done.lv.get(r) { + Some(x) => *x, + None => { + return None; + }, + }; + let left_plain = match nodes.get(l) { + 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) { + return None; + } + let opt = match toks.get(mi) { + Some(t) => t, + None => { + return None; + }, + }; + let first = match toks.get(lo_l) { + Some(t) => t, + None => { + return None; + }, + }; + let last = match toks.get(j.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, mi as int); + assert(opt == &toks@[mi as int]); + } + if !tok_is_op(src, opt, nd.op) || nd.start != first.start || nd.end != last.end { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + assert(a.nodes[m as int].children =~= seq![l as int, r as int]) by { + assert(a.nodes[m as int].children.len() == 2); + assert(a.nodes[m as int].children[0] == l as int); + assert(a.nodes[m as int].children[1] == r 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, r as int, g.cx, 1); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_l as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, j as int - 1); + assert(mi < toks.len()); + assert(toks.len() <= usize::MAX); + assert(lo_r == mi + 1); + assert(j >= 1); + assert(first.start as int == g.ts.toks[lo_l as int].start); + assert(last.end as int == g.ts.toks[j as int - 1].end); + 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(kd(a, l as int) == nodes@[l as int].kind); + assert(dtok_matches(src@, *opt, g.ts.toks[mi as int])); + assert(is_op_tok(g.ts, mi as int, nd.op)) by { + lemma_op_chars(nd.op); + } + lemma_assign_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, 0)) +} + +/// A `MemberExpression` `object . name` of the check whose parts are as read. +#[verifier::spinoff_prover] +proof fn lemma_member_dot_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + o: int, + p: int, + lo_o: int, + mo: int, + lo_p: int, + j: 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::Dot, + a.nodes[m].children =~= seq![o, p], + lvl(a, o) == 15 || lvl(a, o) == 17, + kd(a, p) == NodeKind::IdentifierName, + node_done(a, ts, o, cx, lo_o, mo), + node_done(a, ts, p, cx, lo_p, j), + lo_p == mo + 1, + j == mo + 2, + mo + 2 <= tn(ts), + ts.toks[mo].kind == TokKind::Punctuator, + ts.toks[mo].text =~= seq![0x2Eu32], + a.nodes[m].start == ts.toks[lo_o].start, + a.nodes[m].end == ts.toks[mo + 1].end, + ensures + node_done(a, ts, m, cx, lo_o, mo + 2), + lvl(a, m) == (if lvl(a, o) == 17 { + 17int + } else { + 15int + }), +{ + lemma_punct_text(); + assert(is_p(ts, mo, "."@)); + assert(is_member_or_call(a, o)); + assert(span_ok(a, ts, m, lo_o, mo + 2)); + crate::derive::lemma_member_dot_derives(a, ts, m, cx, lo_o, mo, o, p); + lemma_ed_of(a, ts, m, lo_o, mo + 2); +} + +/// §13.3.2 `object . name`. +#[verifier::spinoff_prover] +fn check_member_dot( + 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, + }, +{ + let _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::MemberExpression) || !matches!(nd.op, Op::Dot) + || nd.children.len() != 2 { + return None; + } + let o = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + let p = match nd.children.as_slice().get(1) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + lemma_child_before(a, m as int, 1); + } + if o >= m || p >= m { + return None; + } + let lo_o = match done.lo.get(o) { + Some(x) => *x, + None => { + return None; + }, + }; + let mo = match done.hi.get(o) { + Some(x) => *x, + None => { + return None; + }, + }; + let lo_p = match done.lo.get(p) { + Some(x) => *x, + None => { + return None; + }, + }; + let j = match done.hi.get(p) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_o = match done.lv.get(o) { + Some(x) => *x, + None => { + return None; + }, + }; + let name_ok = match nodes.get(p) { + Some(x) => matches!(x.kind, NodeKind::IdentifierName), + None => false, + }; + if !(lvl_o == 15 || lvl_o == 17) || !name_ok || lo_p != mo.saturating_add(1) + || j != mo.saturating_add(2) { + return None; + } + let dot = match toks.get(mo) { + Some(t) => t, + None => { + return None; + }, + }; + let first = match toks.get(lo_o) { + Some(t) => t, + None => { + return None; + }, + }; + let last = match toks.get(mo.saturating_add(1)) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, mo as int); + assert(dot == &toks@[mo as int]); + } + if !matches!(dot.kind, TokKind::Punctuator) || !text_is1(src, dot, 0x2E) || nd.start + != first.start || nd.end != last.end { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + 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].children[0] == o as int); + assert(a.nodes[m as int].children[1] == p 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); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_o as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, mo as int + 1); + assert(mo < toks.len()); + assert(toks.len() <= usize::MAX); + assert(lo_p == mo + 1); + assert(j == mo + 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); + assert(kd(a, p as int) == nodes@[p as int].kind); + assert(first.start as int == g.ts.toks[lo_o as int].start); + assert(last.end as int == g.ts.toks[mo as int + 1].end); + assert(g.ts.toks[mo as int].kind == TokKind::Punctuator); + assert(g.ts.toks[mo as int].text =~= seq![0x2Eu32]); + lemma_member_dot_done(a, g.ts, g.cx, m as int, o as int, p as int, lo_o as int, mo as int, lo_p as int, j as int); + } + let own: i32 = if lvl_o == 17 { 17 } else { 15 }; + Some((lo_o, j, own)) +} + +/// Node `j < m`, the child `k` of node `m` checked in `cx`, is done (`inv`). +proof fn lemma_inv_get( + a: AstV, + ts: Stream, + cxs: Seq, + lo: Seq, + hi: Seq, + lv: Seq, + m: int, + j: int, + cx: Ctx, + k: int, +) + requires + inv(a, ts, cxs, lo, hi, lv, m), + cx_ok(a, cxs, m, cx), + 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, cx, lo[j] as int, hi[j] as int), + lvl(a, j) != -100 ==> node_done(a, ts, j, 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]]) == cx); + assert(cx_of(cxs[j]) == 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) + requires + env_ok(src, nodes, toks, gaps, ts), + 0 <= k < toks.len(), + ensures + dtok_matches(src, toks[k], ts.toks[k]), +{ + reveal(env_ok); +} + +/// 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) + requires + env_ok(src, nodes, toks, gaps, ts), + ensures + toks_wf(ts), + tn(ts) == toks.len(), + ast_wf(arena_of(nodes)), +{ + reveal(env_ok); +} + +/// `lemma_leaf_literal` for the three literal kinds. +proof fn lemma_leaf_literal_in(a: AstV, ts: Stream, n: int, cx: Ctx, i: int) + requires + 0 <= n < a.nodes.len(), + 0 <= i < tn(ts), + a.nodes[n].children.len() == 0, + a.nodes[n].start == ts.toks[i].start, + a.nodes[n].end == ts.toks[i].end, + toks_wf(ts), + a.nodes[n].kind == NodeKind::IdentifierName && ts.toks[i].kind == TokKind::IdentifierName + || a.nodes[n].kind == NodeKind::NumericLiteral && ts.toks[i].kind == TokKind::NumericLiteral + || a.nodes[n].kind == NodeKind::StringLiteral && ts.toks[i].kind + == TokKind::StringLiteral || a.nodes[n].kind == NodeKind::RegularExpressionLiteral + && ts.toks[i].kind == TokKind::RegularExpressionLiteral, + ensures + node_done(a, ts, n, cx, i, i + 1), +{ + assert(ts.toks[i].start < ts.toks[i].end); + crate::derive::lemma_leaf_literal(a, ts, n, cx, i); + assert(span_ok(a, ts, n, i, i + 1)); + lemma_ed_of(a, ts, n, i, i + 1); +} + +/// What `check_literal` reads of the stream. +proof fn lemma_env_literal_facts(src: Seq, nodes: Seq, toks: Seq, gaps: Seq, ts: Stream) + requires + env_ok(src, nodes, toks, gaps, ts), + ensures + toks_wf(ts), + toks.len() == tn(ts), + arena_of(nodes).nodes.len() == nodes.len(), + forall|k: int| + 0 <= k < toks.len() ==> (toks[k].kind == (#[trigger] ts.toks[k]).kind && toks[k].start + as int == ts.toks[k].start && toks[k].end as int == ts.toks[k].end), +{ + reveal(env_ok); + assert forall|k: int| 0 <= k < toks.len() implies (toks[k].kind == (#[trigger] ts.toks[k]).kind + && toks[k].start as int == ts.toks[k].start && toks[k].end as int == ts.toks[k].end) by { + assert(dtok_matches(src, toks[k], ts.toks[k])); + } +} + +/// The inserted `;` of gap `k` is the specification's. +proof fn lemma_env_gap(src: Seq, nodes: Seq, toks: Seq, gaps: Seq, ts: Stream, k: int) + requires + env_ok(src, nodes, toks, gaps, ts), + 0 <= k < gaps.len(), + ensures + 0 <= k < ts.gaps.len(), + ts.gaps[k] == gaps[k], + ts.gaps.len() == tn(ts) + 1, +{ + reveal(env_ok); +} + +/// Extending the processed nodes by one. +proof fn lemma_inv_push( + a: AstV, + ts: Stream, + cxs: Seq, + lo: Seq, + hi: Seq, + lv: Seq, + m: int, + l0: usize, + h0: usize, + l: i32, +) + requires + inv(a, ts, cxs, lo, hi, lv, m), + m < cxs.len(), + node_done(a, ts, m, cx_of(cxs[m]), l0 as int, h0 as int), + l as int == lvl(a, m), + ensures + inv(a, ts, cxs, lo.push(l0), hi.push(h0), lv.push(l), m + 1), +{ + let lo2 = lo.push(l0); + let hi2 = hi.push(h0); + let lv2 = lv.push(l); + assert(inv(a, ts, cxs, lo2, hi2, lv2, m + 1)) by { + reveal(inv); + assert(m + 1 <= cxs.len()); + assert(m >= 0); + 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( + a, + ts, + j, + cx_of(cxs[j]), + lo2[j] as int, + hi2[j] as int, + )) by { + if j < m { + 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); + } + } + } +} + +/// Extending the processed nodes by an `Absent` node, which has no token range of +/// its own: its parent places it. +proof fn lemma_inv_push_absent( + a: AstV, + ts: Stream, + cxs: Seq, + lo: Seq, + hi: Seq, + lv: Seq, + m: int, +) + requires + inv(a, ts, cxs, lo, hi, lv, m), + 0 <= m < a.nodes.len(), + m < cxs.len(), + kd(a, m) == NodeKind::Absent, + ensures + inv(a, ts, cxs, lo.push(usize::MAX), hi.push(usize::MAX), lv.push(-100i32), m + 1), +{ + let lo2 = lo.push(usize::MAX); + let hi2 = hi.push(usize::MAX); + let lv2 = lv.push(-100i32); + assert(lvl(a, m) == -100); + assert(inv(a, ts, cxs, lo2, hi2, lv2, m + 1)) by { + reveal(inv); + 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( + a, + ts, + j, + cx_of(cxs[j]), + lo2[j] as int, + hi2[j] as int, + )) by { + if j < m { + assert(lo2[j] == lo[j]); + assert(hi2[j] == hi[j]); + assert(lv2[j] == lv[j]); + } else { + assert(lv2[j] == -100i32); + } + } + } +} + +/// A parenthesized node of the check whose parts are as read. +#[verifier::spinoff_prover] +proof fn lemma_paren_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c: int, + lo_c: int, + hc: int, +) + 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), + lvl(a, c) >= -1, + 1 <= lo_c, + hc < tn(ts), + ts.toks[lo_c - 1].kind == TokKind::Punctuator, + ts.toks[lo_c - 1].text =~= seq![0x28u32], + ts.toks[hc].kind == TokKind::Punctuator, + ts.toks[hc].text =~= seq![0x29u32], + a.nodes[m].start == ts.toks[lo_c - 1].start, + a.nodes[m].end == ts.toks[hc].end, + ensures + node_done(a, ts, m, cx, lo_c - 1, hc + 1), + lvl(a, m) == 17, +{ + lemma_punct_text(); + assert(is_p(ts, lo_c - 1, "("@)); + assert(is_p(ts, hc, ")"@)); + assert(span_ok(a, ts, m, lo_c - 1, hc + 1)); + crate::derive::lemma_parenthesized_derives(a, ts, m, cx, lo_c - 1, hc, c); + lemma_ed_of(a, ts, m, lo_c - 1, hc + 1); +} + +/// An `ExpressionStatement` of the check whose parts are as read. +#[verifier::spinoff_prover] +proof fn lemma_exprstmt_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c: int, + lo_c: int, + hc: int, + j: int, + real_semicolon: bool, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::ExpressionStatement, + a.nodes[m].children =~= seq![c], + node_done(a, ts, c, cx, lo_c, hc), + lvl(a, c) >= -1, + ts.toks[lo_c].kind != TokKind::IdentifierName || ts.toks[lo_c].escaped || (!(ts.toks[lo_c].text + =~= seq![0x66u32, 0x75, 0x6E, 0x63, 0x74, 0x69, 0x6F, 0x6E]) && !(ts.toks[lo_c].text + =~= seq![0x63u32, 0x6C, 0x61, 0x73, 0x73]) && !(ts.toks[lo_c].text =~= seq![0x61u32, + 0x73, 0x79, 0x6E, 0x63]) && !(ts.toks[lo_c].text =~= seq![0x6Cu32, 0x65, 0x74])), + !(ts.toks[lo_c].kind == TokKind::Punctuator && ts.toks[lo_c].text =~= seq![0x7Bu32]), + a.nodes[m].start == ts.toks[lo_c].start, + 0 <= j <= tn(ts), + if real_semicolon { + hc < tn(ts) && ts.toks[hc].kind == TokKind::Punctuator && ts.toks[hc].text + =~= seq![0x3Bu32] && j == hc + 1 && a.nodes[m].end == ts.toks[hc].end + } else { + 0 <= hc < ts.gaps.len() && ts.gaps[hc] == Ins::Semicolon && j == hc && a.nodes[m].end + == ts.toks[hc - 1].end + }, + ensures + node_done(a, ts, m, cx, lo_c, j), + lvl(a, m) == -100, +{ + lemma_punct_text(); + assert(!is_p(ts, lo_c, "{"@)); + lemma_kw_text(); + assert(!is_kw(ts, lo_c, "function"@)); + assert(!is_kw(ts, lo_c, "class"@)); + assert(!is_kw(ts, lo_c, "async"@)); + assert(!is_kw(ts, lo_c, "let"@)); + assert(semi(ts, hc, j, false)) by { + if real_semicolon { + assert(is_p(ts, hc, ";"@)); + } + } + assert(span_ok(a, ts, m, lo_c, j)); + lemma_expr_stmt_derives(a, ts, m, cx, lo_c, hc, j, c); + lemma_ed_of(a, ts, m, lo_c, j); +} + +/// The goal symbol of the source text. +pub open spec fn goal_of(module: bool) -> SourceGoal { + if module { + SourceGoal::Module + } else { + SourceGoal::Script + } +} + +/// The context of the root has `[+In]` and no `[Default]`, so `cx_in` does not change it. +proof fn lemma_root_cx(g: SourceGoal) + ensures + root_ctx(g).in_, + cx_in(root_ctx(g), true) == root_ctx(g), +{ +} + +/// §13.2.9 `( Expression )`. +#[verifier::spinoff_prover] +fn check_paren( + 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, + }, +{ + let _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ParenthesizedExpression) || nd.children.len() != 1 { + return None; + } + let c = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + } + if c >= m { + return None; + } + let lo_c = match done.lo.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let hc = match done.hi.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_c = match done.lv.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + if lvl_c < -1 || lo_c < 1 { + return None; + } + let open = match toks.get(lo_c.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + let close = match toks.get(hc) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_c as int - 1); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int); + assert(open == &toks@[lo_c as int - 1]); + assert(close == &toks@[hc as int]); + } + if !matches!(open.kind, TokKind::Punctuator) || !text_is1(src, open, 0x28) || !matches!( + close.kind, + TokKind::Punctuator, + ) || !text_is1(src, close, 0x29) || nd.start != open.start || nd.end != close.end { + return None; + } + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + 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].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); + 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(g.ts.toks[lo_c as int - 1].kind == TokKind::Punctuator); + assert(g.ts.toks[lo_c as int - 1].text =~= seq![0x28u32]); + assert(g.ts.toks[hc as int].kind == TokKind::Punctuator); + assert(g.ts.toks[hc as int].text =~= seq![0x29u32]); + assert(open.start as int == g.ts.toks[lo_c as int - 1].start); + assert(close.end as int == g.ts.toks[hc as int].end); + lemma_paren_done(a, g.ts, g.cx, m as int, c as int, lo_c as int, hc as int); + assert(lo_c >= 1); + assert(lo_c.saturating_sub(1) as int == lo_c as int - 1); + assert(hc < toks.len()); + assert(toks.len() <= usize::MAX); + assert(hc.saturating_add(1) as int == hc as int + 1); + } + Some((lo_c.saturating_sub(1), hc.saturating_add(1), 17)) +} + +/// §14.5 `Expression ;`. +#[verifier::spinoff_prover] +fn check_exprstmt( + 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, + }, +{ + let _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ExpressionStatement) || nd.children.len() != 1 { + return None; + } + let c = match nd.children.as_slice().get(ZERO) { + Some(x) => *x as usize, + None => { + return None; + }, + }; + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + lemma_child_before(a, m as int, 0); + } + if c >= m { + return None; + } + let lo_c = match done.lo.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let hc = match done.hi.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let lvl_c = match done.lv.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + if lvl_c < -1 { + return None; + } + let t0 = match toks.get(lo_c) { + Some(t) => t, + None => { + return None; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, lo_c as int); + assert(t0 == &toks@[lo_c as int]); + } + let kw_start = matches!(t0.kind, TokKind::IdentifierName) && !t0.escaped && (text_is( + src, + t0, + &[0x66, 0x75, 0x6E, 0x63, 0x74, 0x69, 0x6F, 0x6E], + ) || text_is(src, t0, &[0x63, 0x6C, 0x61, 0x73, 0x73]) || text_is( + src, + t0, + &[0x61, 0x73, 0x79, 0x6E, 0x63], + ) || text_is(src, t0, &[0x6C, 0x65, 0x74])); + if kw_start || (matches!(t0.kind, TokKind::Punctuator) && text_is1(src, t0, 0x7B)) + || nd.start != t0.start { + return None; + } + // The `;`: a has_semi one at token `hc`, or one inserted in the gap before it. + let mut has_semi = false; + if let Some(st) = toks.get(hc) { + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int); + assert(st == &toks@[hc as int]); + } + if matches!(st.kind, TokKind::Punctuator) && text_is1(src, st, 0x3B) && nd.end == st.end { + has_semi = true; + } + } + let j: usize = if has_semi { + hc.saturating_add(1) + } else { + let g = match gaps.get(hc) { + Some(g) => g, + None => { + return None; + }, + }; + let prev = match toks.get(hc.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + if !matches!(g, Ins::Semicolon) || hc < 1 || nd.end != prev.end { + return None; + } + hc + }; + proof { + let a = arena_of(nodes@); + assert(nd == &nodes@[m as int]); + 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].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); + 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(t0.start as int == g.ts.toks[lo_c as int].start); + assert(g.ts.toks[lo_c as int].kind == t0.kind); + assert(!(g.ts.toks[lo_c as int].kind == TokKind::Punctuator && g.ts.toks[lo_c as int].text + =~= seq![0x7Bu32])); + if has_semi { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int); + assert(hc < toks.len()); + assert(g.ts.toks[hc as int].kind == TokKind::Punctuator); + assert(g.ts.toks[hc as int].text =~= seq![0x3Bu32]); + assert(j as int == hc as int + 1); + assert(toks@[hc as int].end as int == g.ts.toks[hc as int].end); + lemma_exprstmt_done(a, g.ts, g.cx, m as int, c as int, lo_c as int, hc as int, j as int, true); + } else { + lemma_env_gap(src@, nodes@, toks@, gaps@, g.ts, hc as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int - 1); + assert(g.ts.gaps[hc as int] == Ins::Semicolon); + assert(toks@[hc as int - 1].end as int == g.ts.toks[hc as int - 1].end); + assert(hc <= toks.len()); + lemma_exprstmt_done(a, g.ts, g.cx, m as int, c as int, lo_c as int, hc as int, j as int, false); + } + } + Some((lo_c, j, -100)) +} + +/// §14.4 `;`. +#[verifier::spinoff_prover] +fn check_empty( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + by_start: &[usize], + 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, + }, +{ + let _ = gaps.len(); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::EmptyStatement) || !nd.children.is_empty() { + return None; + } + let x = match by_start.get(nd.start) { + 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::Punctuator) || !text_is1(src, t, 0x3B) || t.start != nd.start + || 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]@); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + 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].kind == TokKind::Punctuator); + assert(g.ts.toks[x as int].text =~= seq![0x3Bu32]); + assert(x < toks.len()); + assert(toks.len() <= usize::MAX); + lemma_empty_done(a, g.ts, g.cx, m as int, x as int); + } + Some((x, x.saturating_add(1), -100)) +} + +/// The token ranges and binding strengths of the nodes checked so far, node by node. +pub struct Done { + /// The first token of each node. + pub lo: Vec, + /// One past the last token of each node. + pub hi: Vec, + /// The binding strength (`spec::lvl`) of each node. + pub lv: Vec, + /// The context each node is checked in: the one its parent's production gives + /// it, which the parent's check compares (`cx_ok`); not trusted otherwise. + pub cxs: Vec, + /// The token that starts at each source position (a hint, checked at use). + pub by_start: Vec, + /// The goal symbol is `Module`. + pub module: bool, +} + +/// The specification's stream and the context the nodes are derived in (ghost). +pub struct GCx { + /// The stream. + pub ts: Stream, + /// The context. + pub cx: Ctx, +} + +/// What `check_derivation` found. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum Derivation { + /// Every node derives the tokens it spans: `derives_root` holds. + Verified, + /// A node of a kind the check does not cover: nothing is claimed. + Unsupported, + /// A covered node that is not derived: the parser built what the grammar does not allow. + Failed, +} + +/// §16.1, §16.2: the root derives the whole stream, from the items it holds. +#[verifier::spinoff_prover] +fn check_root( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + module: bool, + Ghost(ts): Ghost, +) -> (r: bool) + requires + nodes.len() > 0, + env_ok(src@, nodes@, toks@, gaps@, ts), + inv( + arena_of(nodes@), + ts, + done.cxs@, + done.lo@, + done.hi@, + done.lv@, + nodes.len() as int - 1, + ), + cx_ok(arena_of(nodes@), done.cxs@, nodes.len() as int - 1, root_ctx(goal_of(module))), + ensures + r ==> derives_root(arena_of(nodes@), ts, goal_of(module)), +{ + let n = nodes.len(); + let root = n.saturating_sub(1); + let _ = (src.len(), gaps.len(), done.lv.len()); + let nt = toks.len(); + let ghost a = arena_of(nodes@); + let ghost cx = root_ctx(goal_of(module)); + let nd = match nodes.get(root) { + Some(x) => x, + None => { + return false; + }, + }; + let kind_ok = if module { + matches!(nd.kind, NodeKind::Module) + } else { + matches!(nd.kind, NodeKind::Script) + }; + if !kind_ok { + return false; + } + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, ts); + assert(a.nodes[root as int] == nodes@[root as int]@); + assert(nd == &nodes@[root as int]); + assert(a.nodes[root as int].kind == nd.kind); + } + let len = nd.children.len(); + let mut k = len; + let mut pos = nt; + while k > 0 + invariant + nodes.len() > 0, + n == nodes.len(), + root + 1 == n, + nt == toks.len(), + tn(ts) == nt, + ast_wf(a), + a == arena_of(nodes@), + cx == root_ctx(goal_of(module)), + nd == &nodes@[root as int], + len == nd.children.len(), + k <= len, + pos <= nt, + env_ok(src@, nodes@, toks@, gaps@, ts), + inv(a, ts, done.cxs@, done.lo@, done.hi@, done.lv@, root as int), + 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])), + decreases k, + { + let c = match nd.children.as_slice().get(k.saturating_sub(1)) { + Some(x) => *x as usize, + None => { + return false; + }, + }; + proof { + assert(a.nodes[root as int].children[k as int - 1] == c as int); + lemma_child_before(a, root as int, k as int - 1); + } + let lo_c = match done.lo.get(c) { + Some(x) => *x, + None => { + return false; + }, + }; + let hi_c = match done.hi.get(c) { + Some(x) => *x, + None => { + return false; + }, + }; + let item = match nodes.get(c) { + Some(x) => item_kind_exec(x.kind), + None => false, + }; + if hi_c != pos || !item { + return false; + } + proof { + lemma_inv_get(a, ts, done.cxs@, done.lo@, done.hi@, done.lv@, root as int, c as int, cx, k as int - 1); + 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))); + } + pos = lo_c; + k = k.saturating_sub(1); + } + if pos != 0 { + return false; + } + let spans = if nt == 0 { + nd.start == 0 && nd.end == 0 + } else { + let first = match toks.get(ZERO) { + Some(t) => t, + None => { + return false; + }, + }; + let last = match toks.get(nt.saturating_sub(1)) { + Some(t) => t, + None => { + return false; + }, + }; + nd.start == first.start && nd.end == last.end + }; + if !spans { + return false; + } + proof { + lemma_root_cx(goal_of(module)); + assert(nd.start as int == a.nodes[root as int].start); + assert(nd.end as int == a.nodes[root as int].end); + if nt == 0 { + assert(span_ok(a, ts, root as int, 0, 0)); + } else { + lemma_env_tok(src@, nodes@, toks@, gaps@, ts, 0); + 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)); + lemma_root_derives(a, ts, root as int, cx, goal_of(module)); + } + true +} + +/// Checks, bottom-up, that every node of the arena derives the tokens it spans +/// (`spec::d`), for the kinds the check covers, and that the root derives the +/// whole stream. `ast` has passed [`crate::check::finish`]; `toks` and `gaps` are +/// the tokens the parser consumed and the gaps where it inserted a `;` +/// (`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. +pub fn check_derivation( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + module: bool, + Ghost(ts): Ghost, +) -> (r: Derivation) + requires + ast_wf(arena_of(nodes@)), + stream_matches(src@, toks@, gaps@, ts), + gaps@.len() == toks@.len() + 1, + ensures + r is Verified ==> derives_root(arena_of(nodes@), ts, goal_of(module)), +{ + let n = nodes.len(); + let nt = toks.len(); + // The tokens are not empty and their ends increase. + let mut k: usize = 0; + while k < nt + invariant + nt == toks.len(), + k <= nt, + stream_matches(src@, toks@, gaps@, ts), + forall|x: int| 0 <= x < k ==> toks@[x].start < toks@[x].end, + forall|x: int, y: int| 0 <= x < y < k ==> toks@[x].end < toks@[y].end, + decreases nt - k, + { + let t = match toks.get(k) { + Some(t) => t, + None => { + return Derivation::Failed; + }, + }; + if t.start >= t.end { + return Derivation::Failed; + } + if k > 0 { + let p = match toks.get(k.saturating_sub(1)) { + Some(p) => p, + None => { + return Derivation::Failed; + }, + }; + if p.end >= t.end { + return Derivation::Failed; + } + } + k = k.saturating_add(1); + } + proof { + assert(toks_wf(ts)) by { + assert forall|x: int| 0 <= x < tn(ts) implies (#[trigger] ts.toks[x]).start + < ts.toks[x].end by { + assert(dtok_matches(src@, toks@[x], ts.toks[x])); + } + assert forall|x: int, y: int| 0 <= x < y < tn(ts) implies (#[trigger] ts.toks[x]).end + < (#[trigger] ts.toks[y]).end by { + assert(dtok_matches(src@, toks@[x], ts.toks[x])); + assert(dtok_matches(src@, toks@[y], ts.toks[y])); + } + } + assert(env_ok(src@, nodes@, toks@, gaps@, ts)) by { + reveal(env_ok); + } + lemma_root_cx(goal_of(module)); + } + // A hint for finding a token from the start of a node. + let mut by_start: Vec = Vec::new(); + let mut q: usize = 0; + let size = src.len().saturating_add(1); + 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::Failed; + }, + }; + if t.start < by_start.len() { + by_start.set(t.start, x); + } + x = x.saturating_add(1); + } + let cxs = assign_ctx(nodes, module); + let ghost cxs0 = cxs@; + let mut done = Done { + lo: Vec::new(), + hi: Vec::new(), + lv: Vec::new(), + cxs, + by_start, + module, + }; + let last = n.saturating_sub(1); + let mut m: usize = 0; + proof { + reveal(inv); + } + while m < last + invariant + n == nodes.len(), + last + 1 == n, + n > 0, + m <= last, + done.cxs@ == cxs0, + done.cxs.len() == n, + done.module == module, + env_ok(src@, nodes@, toks@, gaps@, ts), + inv(arena_of(nodes@), ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int), + decreases last - m, + { + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return Derivation::Failed; + }, + }; + if matches!(nd.kind, NodeKind::Absent) { + proof { + let a = arena_of(nodes@); + assert(a.nodes[m as int] == nodes@[m as int]@); + assert(nd == &nodes@[m as int]); + assert(kd(a, m as int) == NodeKind::Absent); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, ts); + lemma_inv_push_absent(a, ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int); + } + done.lo.push(usize::MAX); + done.hi.push(usize::MAX); + done.lv.push(-100); + m = m.saturating_add(1); + continue; + } + let cxm: Cx5 = match done.cxs.get(m) { + Some(c) => *c, + None => { + return Derivation::Failed; + }, + }; + // The statements and expressions covered so far are checked with `[+In]`. + if !cxm.i || cxm.d { + return Derivation::Unsupported; + } + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, ts); + } + let ghost cx = cx_of(cxm); + proof { + assert(cx_in(cx, true) == cx); + assert(cxm == done.cxs@[m as int]); + } + let is_arrow = matches!(nd.kind, NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction); + let res = if is_arrow { + let is_async = matches!(nd.kind, NodeKind::AsyncArrowFunction); + if nd.children.len() != 2 { + return Derivation::Unsupported; + } + let pc = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return Derivation::Unsupported; + }, + }; + let bc = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return Derivation::Unsupported; + }, + }; + let ident = matches!(kind_at(nodes, pc), NodeKind::BindingIdentifier); + let braced = matches!(kind_at(nodes, bc), NodeKind::FunctionBody); + let (c0, c1) = arrow_cx5(&cxm, is_async, ident, braced); + if !kids_cx2(nodes, done.cxs.as_slice(), m, &c0, &c1) { + return Derivation::Unsupported; + } + let sh = ArrowShape { m, is_async }; + proof { + assert(nd == &nodes@[m as int]); + } + check_arrow(src, nodes, toks, gaps, &done, &sh, Ghost(GCx { ts, cx })) + } else { + match fn_flags(nd.kind) { + Some((fa, fg, fe)) => { + let (c0, c1, c2) = fn_cx5(&cxm, fa, fg, fe); + if !kids_cx3(nodes, done.cxs.as_slice(), m, &c0, &c1, &c2) { + return Derivation::Unsupported; + } + let fs = FuncShape { m, is_async: fa, is_gen: fg, is_expr: fe }; + proof { + assert(nd == &nodes@[m as int]); + } + check_function(src, nodes, toks, gaps, &done, &fs, Ghost(GCx { ts, cx })) + }, + None => { + if !kids_cx(nodes, done.cxs.as_slice(), m, &cxm) { + return Derivation::Unsupported; + } + match nd.kind { + 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, + nodes, + toks, + gaps, + &done, + m, + Ghost(GCx { ts, cx }), + ), + NodeKind::IdentifierReference | NodeKind::BindingIdentifier | NodeKind::LabelIdentifier => check_ident( + src, + nodes, + toks, + gaps, + &done, + m, + Ghost(GCx { ts, cx }), + ), + NodeKind::ThisExpression | NodeKind::NullLiteral | NodeKind::BooleanLiteral => { + check_keyword_leaf(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })) + }, + NodeKind::IfStatement => check_if( + src, + nodes, + toks, + gaps, + &done, + m, + Ghost(GCx { ts, cx }), + ), + NodeKind::BlockStatement | NodeKind::FunctionBody => check_block( + src, + nodes, + toks, + gaps, + &done, + 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, + toks, + gaps, + &done, + 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, + toks, + gaps, + &done, + m, + Ghost(GCx { ts, cx }), + ), + NodeKind::Expression => check_expression( + src, + nodes, + toks, + gaps, + &done, + m, + Ghost(GCx { ts, cx }), + ), + NodeKind::SpreadElement => check_spread( + src, + nodes, + toks, + gaps, + &done, + m, + Ghost(GCx { ts, cx }), + ), + NodeKind::UnaryExpression => check_unary( + src, + nodes, + toks, + gaps, + &done, + 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, + toks, + gaps, + &done, + m, + Ghost(GCx { ts, cx }), + ), + NodeKind::MemberExpression => check_member_dot( + src, + nodes, + toks, + gaps, + &done, + m, + Ghost(GCx { ts, cx }), + ), + NodeKind::ExpressionStatement => check_exprstmt( + src, + nodes, + toks, + gaps, + &done, + m, + Ghost(GCx { ts, cx }), + ), + NodeKind::EmptyStatement => check_empty( + src, + nodes, + toks, + gaps, + &done.by_start, + m, + Ghost(GCx { ts, cx }), + ), + 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::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 })), + NodeKind::Catch => check_catch(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::Finally => check_finally(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::SwitchStatement => check_switch(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + NodeKind::CaseClause => check_case(src, nodes, toks, gaps, &done, m, Ghost(GCx { ts, cx })), + 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::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 })), + _ => { + return Derivation::Unsupported; + }, + } + }, + } + }; + match res { + Some((l0, h0, l)) => { + proof { + lemma_inv_push(arena_of(nodes@), ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, l0, h0, l); + } + done.lo.push(l0); + done.hi.push(h0); + done.lv.push(l); + }, + None => { + return Derivation::Unsupported; + }, + } + m = m.saturating_add(1); + } + // The root's items are in the root's context. + let root_cx = Cx5 { y: false, a: module, i: true, r: false, d: false }; + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, ts); + assert(cx_of(root_cx) == root_ctx(goal_of(module))); + } + if !kids_cx(nodes, done.cxs.as_slice(), last, &root_cx) { + return Derivation::Unsupported; + } + if check_root(src, nodes, toks, gaps, &done, module, Ghost(ts)) { + Derivation::Verified + } else { + Derivation::Unsupported + } +} + +/// The context each node of the arena is to be checked in, from the root down (a +/// node's index is above its children's). Untrusted: [`kids_cx`] compares it with +/// what the production of the parent gives, so a wrong guess is `Unsupported`. +fn assign_ctx(nodes: &[Node], module: bool) -> (r: Vec) + ensures + r.len() == nodes.len(), +{ + let n = nodes.len(); + let root = Cx5 { y: false, a: module, i: true, r: false, d: false }; + let mut cxs: Vec = Vec::new(); + let mut q: usize = 0; + while q < n + invariant + q <= n, + cxs.len() == q, + decreases n - q, + { + cxs.push(root); + q = q.saturating_add(1); + } + let mut i = n; + while i > 0 + invariant + i <= n, + n == nodes.len(), + cxs.len() == n, + decreases i, + { + i = i.saturating_sub(1); + let p: Cx5 = match cxs.get(i) { + Some(p) => *p, + None => { + return cxs; + }, + }; + let nd = match nodes.get(i) { + Some(x) => x, + None => { + return cxs; + }, + }; + 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 mut k: usize = 0; + while k < len + invariant + k <= len, + len == nd.children.len(), + cxs.len() == n, + decreases len - k, + { + if let Some(c) = nd.children.as_slice().get(k) { + let c = *c as usize; + if c < n { + let cc = child_cx(nd.kind, k, &p, ident, braced); + cxs.set(c, cc); + } + } + k = k.saturating_add(1); + } + } + cxs +} + +/// Child `idx` of `nd` is a `BindingIdentifier` (`ident`) or a `FunctionBody`. +fn child_kind_is(nodes: &[Node], nd: &Node, idx: usize, ident: bool) -> bool { + let c = match nd.children.as_slice().get(idx) { + Some(c) => *c as usize, + None => { + return false; + }, + }; + match nodes.get(c) { + Some(x) => if ident { + matches!(x.kind, NodeKind::BindingIdentifier) + } else { + matches!(x.kind, NodeKind::FunctionBody) + }, + None => false, + } +} + +/// The context the production of node kind `kind` gives its child `k`, when it is +/// checked in `p`: `[+In]`, as the statements and expressions covered so far have. +fn child_cx(kind: NodeKind, k: usize, p: &Cx5, ident: bool, braced: bool) -> (r: Cx5) { + if let Some((fa, fg, fe)) = fn_flags(kind) { + let (c0, c1, c2) = fn_cx5(p, fa, fg, fe); + return if k == 0 { + c0 + } else if k == 1 { + c1 + } else { + c2 + }; + } + if matches!(kind, NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction) { + 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 } +} + +/// 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 + m < nodes.len(), + cxs.len() == nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + r ==> cx_ok(arena_of(nodes@), cxs@, m as int, cx_of(*p)), +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + match cxs.get(m) { + Some(c) => { + if !cx_eq(c, p) { + 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(*p), + 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(*p), + decreases len - k, + { + let c = 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] == c as int); + lemma_child_before(a, m as int, k as int); + } + let cc = match cxs.get(c) { + Some(x) => x, + None => { + return false; + }, + }; + if !cx_eq(cc, p) { + 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. +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(), + escaped: e.escaped, + nl_before: e.nl, + legacy: false, + bigint: false, + invalid_escape: false, + regexp_valid: false, + } +} + +/// The stream of the executable tokens and gaps. +pub open spec fn canon_stream(src: Seq, toks: Seq, gaps: Seq) -> Stream { + Stream { toks: toks.map_values(|e: DTok| canon_tok(src, e)), gaps: gaps } +} + +/// [`check_derivation`] on the canonical stream, which needs no ghost argument. +/// +/// Proven: `Verified` implies `derives_root` for [`canon_stream`]. That the real +/// stream (the lexer's tokens, with its own `name`, `legacy` and the like) derives +/// the same is the lexer's contract, which is assumed (item 7.2), and that the +/// productions covered read only the fields `canon_tok` keeps. +pub fn check_derivation_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 ==> derives_root( + arena_of(nodes@), + canon_stream(src@, toks@, gaps@), + goal_of(module), + ), +{ + 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::Failed; + }, + }; + if t.start > t.end || t.end > src.len() { + return Derivation::Failed; + } + k = k.saturating_add(1); + } + let ghost ts = canon_stream(src@, toks@, gaps@); + proof { + assert(stream_matches(src@, toks@, gaps@, ts)) by { + assert(toks@.len() == ts.toks.len()); + assert(gaps@ =~= ts.gaps); + 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_derivation(src, nodes, toks, gaps, module, Ghost(ts)) +} + +/// An `EmptyStatement` of the check. +proof fn lemma_empty_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::EmptyStatement, + a.nodes[m].children.len() == 0, + ts.toks[x].kind == TokKind::Punctuator, + ts.toks[x].text =~= seq![0x3Bu32], + 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, +{ + lemma_punct_text(); + assert(is_p(ts, x, ";"@)); + assert(span_ok(a, ts, m, x, x + 1)); + lemma_empty_statement_derives(a, ts, m, cx, x); + lemma_ed_of(a, ts, m, x, x + 1); +} + +// --------------------------------------------------------------------- +// The rest of the statements of §14 that the check covers: `while`, `do`-`while`, +// `throw`, `break`, `continue`, labelled, `try`, `switch` and `debugger`. +// --------------------------------------------------------------------- + +/// The words of those statements, as code points. +proof fn lemma_words2() + ensures + cps("do"@) =~= seq![0x64u32, 0x6F], + cps("while"@) =~= seq![0x77u32, 0x68, 0x69, 0x6C, 0x65], + cps("throw"@) =~= seq![0x74u32, 0x68, 0x72, 0x6F, 0x77], + cps("break"@) =~= seq![0x62u32, 0x72, 0x65, 0x61, 0x6B], + cps("continue"@) =~= seq![0x63u32, 0x6F, 0x6E, 0x74, 0x69, 0x6E, 0x75, 0x65], + cps("debugger"@) =~= seq![0x64u32, 0x65, 0x62, 0x75, 0x67, 0x67, 0x65, 0x72], + cps("try"@) =~= seq![0x74u32, 0x72, 0x79], + cps("catch"@) =~= seq![0x63u32, 0x61, 0x74, 0x63, 0x68], + cps("finally"@) =~= seq![0x66u32, 0x69, 0x6E, 0x61, 0x6C, 0x6C, 0x79], + cps("switch"@) =~= seq![0x73u32, 0x77, 0x69, 0x74, 0x63, 0x68], + cps("case"@) =~= seq![0x63u32, 0x61, 0x73, 0x65], + cps("default"@) =~= seq![0x64u32, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74], + cps("}"@) =~= seq![0x7Du32], +{ + reveal_strlit("do"); + reveal_strlit("while"); + reveal_strlit("throw"); + reveal_strlit("break"); + reveal_strlit("continue"); + reveal_strlit("debugger"); + reveal_strlit("try"); + reveal_strlit("catch"); + reveal_strlit("finally"); + reveal_strlit("switch"); + reveal_strlit("case"); + reveal_strlit("default"); + reveal_strlit("}"); + assert("do"@ =~= seq!['d', 'o']); + assert("while"@ =~= seq!['w', 'h', 'i', 'l', 'e']); + assert("throw"@ =~= seq!['t', 'h', 'r', 'o', 'w']); + assert("break"@ =~= seq!['b', 'r', 'e', 'a', 'k']); + assert("continue"@ =~= seq!['c', 'o', 'n', 't', 'i', 'n', 'u', 'e']); + assert("debugger"@ =~= seq!['d', 'e', 'b', 'u', 'g', 'g', 'e', 'r']); + assert("try"@ =~= seq!['t', 'r', 'y']); + assert("catch"@ =~= seq!['c', 'a', 't', 'c', 'h']); + assert("finally"@ =~= seq!['f', 'i', 'n', 'a', 'l', 'l', 'y']); + assert("switch"@ =~= seq!['s', 'w', 'i', 't', 'c', 'h']); + assert("case"@ =~= seq!['c', 'a', 's', 'e']); + assert("default"@ =~= seq!['d', 'e', 'f', 'a', 'u', 'l', 't']); + assert("}"@ =~= seq!['}']); +} + +/// 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) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + match r { + Some(c) => c < m && k < arena_of(nodes@).nodes[m as int].children.len() && arena_of( + nodes@, + ).nodes[m as int].children[k as int] == c as int, + None => true, + }, +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let c = match nd.children.as_slice().get(k) { + Some(x) => *x 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]); + lemma_child_before(a, m as int, k as int); + } + if c >= m { + return None; + } + Some(c) +} + +/// The kind of node `c`. +fn kind_at(nodes: &[Node], c: usize) -> (r: NodeKind) + requires + c < nodes.len(), + ensures + r == kd(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]@); + assert(x == &nodes@[c as int]); + } + x.kind + }, + None => { + proof { + assert(false); + } + NodeKind::Absent + }, + } +} + +/// 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)>) + ensures + match r { + Some((l, h, v)) => c < done.lo@.len() && c < done.hi@.len() && c < done.lv@.len() && l + == done.lo@[c as int] && h == done.hi@[c as int] && v == done.lv@[c as int], + None => true, + }, +{ + let l = match done.lo.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let h = match done.hi.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + let v = match done.lv.get(c) { + Some(x) => *x, + None => { + return None; + }, + }; + Some((l, h, v)) +} + +/// The token a node starts at. +fn start_tok( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + m: usize, + Ghost(g): Ghost, +) -> (r: Option) + requires + m < nodes.len(), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + match r { + Some(x) => x < toks.len() && g.ts.toks[x as int].start == nodes@[m as int].start as int, + None => true, + }, +{ + let _ = (src.len(), gaps.len()); + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let x = match done.by_start.get(nd.start) { + 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]); + assert(nd == &nodes@[m as int]); + } + if t.start != nd.start { + return None; + } + Some(x) +} + +/// The context, and the characters of the word or punctuator a token is checked against. +pub struct GW { + /// The stream and context. + pub g: GCx, + /// The characters of the word. + pub w: Seq, +} + +/// Token `x` is the keyword `w` (`gw.w` as characters). +fn 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 ==> x < toks.len() && is_kw(gw.g.ts, x as int, gw.w), +{ + let _ = (nodes.len(), gaps.len()); + let ghost g = gw.g; + let ghost ws = 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::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)); + assert(g.ts.toks[x as int].text =~= cps(ws)); + } + } + r +} + +/// Token `x` is the one-character punctuator `lit` (code point `code`). +fn punct_at( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + x: usize, + code: u32, + Ghost(gw): Ghost, +) -> (r: bool) + requires + env_ok(src@, nodes@, toks@, gaps@, gw.g.ts), + cps(gw.w) =~= seq![code], + 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_is1(src, t, code); + 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 +} + +/// No line terminator before token `x`. +fn no_nl_at( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + x: usize, + Ghost(g): Ghost, +) -> (r: bool) + requires + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + r ==> !nl(g.ts, x as int), +{ + let _ = (src.len(), nodes.len(), gaps.len()); + proof { + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + match toks.get(x) { + Some(t) => { + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, x as int); + assert(t == &toks@[x as int]); + } + !t.nl + }, + None => true, + } +} + +/// The token before `j` ends where `e` says. +fn end_ok( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + e: usize, + j: usize, + Ghost(g): Ghost, +) -> (r: bool) + requires + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + r ==> 1 <= j && j <= toks.len() && g.ts.toks[j as int - 1].end == e as int, +{ + let _ = (src.len(), nodes.len(), gaps.len()); + if j < 1 || j > toks.len() { + return false; + } + let t = match toks.get(j.saturating_sub(1)) { + Some(t) => t, + None => { + return false; + }, + }; + proof { + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, j as int - 1); + assert(j.saturating_sub(1) as int == j as int - 1); + assert(t == &toks@[j as int - 1]); + } + t.end == e +} + +/// [`find_semi`] for the `;` after `do`-`while` (§12.10.1 rule 1, third condition). +fn find_semi_dw( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + hc: usize, + end: usize, + Ghost(g): Ghost, +) -> (r: Option) + requires + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + match r { + Some(j) => semi(g.ts, hc as int, j as int, true) && 1 <= j && j <= toks.len() && g.ts.toks[j as int + - 1].end == end as int && hc <= j, + None => true, + }, +{ + if let Some(j) = find_semi(src, nodes, toks, gaps, hc, end, Ghost(g)) { + proof { + assert(semi(g.ts, hc as int, j as int, false)); + } + return Some(j); + } + let g0 = match gaps.get(hc) { + Some(x) => x, + None => { + return None; + }, + }; + let prev = match toks.get(hc.saturating_sub(1)) { + Some(t) => t, + None => { + return None; + }, + }; + if !matches!(g0, Ins::DoWhile) || hc < 1 || end != prev.end { + return None; + } + proof { + lemma_env_gap(src@, nodes@, toks@, gaps@, g.ts, hc as int); + lemma_env_tok(src@, nodes@, toks@, gaps@, g.ts, hc as int - 1); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + assert(hc.saturating_sub(1) as int == hc as int - 1); + assert(prev.end as int == g.ts.toks[hc as int - 1].end); + assert(hc <= toks.len()); + } + Some(hc) +} + +/// §14.16 `debugger ;`. +#[verifier::spinoff_prover] +proof fn lemma_debugger_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::DebuggerStatement, + a.nodes[m].children.len() == 0, + is_kw(ts, x, "debugger"@), + semi(ts, x + 1, j, false), + 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(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.16 `debugger ;`. +#[verifier::spinoff_prover] +fn check_debugger( + 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_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::DebuggerStatement) || !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, &[0x64, 0x65, 0x62, 0x75, 0x67, 0x67, 0x65, 0x72], Ghost(GW { g, w: "debugger"@ })) { + return None; + } + let j = match find_semi(src, nodes, toks, gaps, x.saturating_add(1), nd.end, Ghost(g)) { + Some(j) => j, + None => { + 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.len() == 0); + lemma_debugger_done(a, g.ts, g.cx, m as int, x as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.14 `throw Expression ;`. +#[verifier::spinoff_prover] +proof fn lemma_throw_done(a: AstV, ts: Stream, cx: Ctx, m: int, e: int, x: int, me: int, j: int) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= e < m, + a.nodes[m].kind == NodeKind::ThrowStatement, + a.nodes[m].children =~= seq![e], + is_kw(ts, x, "throw"@), + !nl(ts, x + 1), + lvl(a, e) >= -1, + node_done(a, ts, e, cx, x + 1, me), + semi(ts, me, j, false), + 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] == e); + assert(dc(a, ts, m, e, cx_in(cx, true), x + 1, me)); + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.14 `throw [no LineTerminator here] Expression ;`. +#[verifier::spinoff_prover] +fn check_throw( + 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_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::ThrowStatement) || nd.children.len() != 1 { + return None; + } + let e = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_e, me, lv_e) = match done_at(done, e) { + 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 lv_e < -1 || lo_e != x.saturating_add(1) { + return None; + } + if !word_at(src, nodes, toks, gaps, x, &[0x74, 0x68, 0x72, 0x6F, 0x77], Ghost(GW { g, w: "throw"@ })) + || !no_nl_at(src, nodes, toks, gaps, x.saturating_add(1), Ghost(g)) { + return None; + } + let j = match find_semi(src, nodes, toks, gaps, me, nd.end, Ghost(g)) { + Some(j) => j, + None => { + 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![e 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, e as int, g.cx, 0); + assert(lvl(a, e as int) >= -1); + lemma_throw_done(a, g.ts, g.cx, m as int, e as int, x as int, me as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.8 `continue` and §14.9 `break`. +#[verifier::spinoff_prover] +proof fn lemma_jump_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c: int, + x: int, + labelled: bool, + j: int, + is_break: bool, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == (if is_break { + NodeKind::BreakStatement + } else { + NodeKind::ContinueStatement + }), + a.nodes[m].children =~= seq![c], + is_kw(ts, x, if is_break { + "break"@ + } else { + "continue"@ + }), + if labelled { + kd(a, c) == NodeKind::LabelIdentifier && !nl(ts, x + 1) && node_done( + a, + ts, + c, + cx, + x + 1, + x + 2, + ) && semi(ts, x + 2, j, false) + } else { + kd(a, c) == NodeKind::Absent && d(a, ts, c, cx, x + 1, x + 1) && semi( + ts, + x + 1, + j, + false, + ) + }, + 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); + if labelled { + assert(dc(a, ts, m, c, cx, x + 1, x + 2)); + } else { + assert(dc(a, ts, m, c, cx, x + 1, x + 1)); + } + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.8 `continue`, §14.9 `break`, with an optional label. +#[verifier::spinoff_prover] +fn check_jump( + 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_words2(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let is_break = matches!(nd.kind, NodeKind::BreakStatement); + if !(is_break || matches!(nd.kind, NodeKind::ContinueStatement)) || 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 ok = if is_break { + word_at(src, nodes, toks, gaps, x, &[0x62, 0x72, 0x65, 0x61, 0x6B], Ghost(GW { g, w: "break"@ })) + } else { + word_at(src, nodes, toks, gaps, x, &[0x63, 0x6F, 0x6E, 0x74, 0x69, 0x6E, 0x75, 0x65], Ghost(GW { g, w: "continue"@ })) + }; + if !ok { + return None; + } + let x1 = x.saturating_add(1); + let kc = kind_at(nodes, c); + let labelled = matches!(kc, NodeKind::LabelIdentifier); + let j: usize; + if labelled { + let (lo_c, hi_c, _) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + let x2 = x.saturating_add(2); + if lo_c != x1 || hi_c != x2 || !no_nl_at(src, nodes, toks, gaps, x1, Ghost(g)) { + return None; + } + j = match find_semi(src, nodes, toks, gaps, x2, nd.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, 0); + assert(x1 as int == x as int + 1); + assert(x2 as int == x as int + 2); + } + } else { + if !matches!(kc, NodeKind::Absent) { + return None; + } + if !check_absent(src, nodes, toks, gaps, c, x1, Ghost(g)) { + return None; + } + j = match find_semi(src, nodes, toks, gaps, x1, nd.end, Ghost(g)) { + Some(j) => j, + None => { + 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].end == nd.end as int); + lemma_jump_done(a, g.ts, g.cx, m as int, c as int, x as int, labelled, j as int, is_break); + } + Some((x, j, -100)) +} + +/// §14.7.3 `while ( Expression ) Statement`. +#[verifier::spinoff_prover] +#[verifier::rlimit(60)] +proof fn lemma_while_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + t: int, + c: int, + x: int, + mt: int, + j: int, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= t < m, + 0 <= c < m, + a.nodes[m].kind == NodeKind::WhileStatement, + a.nodes[m].children =~= seq![t, c], + is_kw(ts, x, "while"@), + is_p(ts, x + 1, "("@), + lvl(a, t) >= -1, + node_done(a, ts, t, cx, x + 2, mt), + is_p(ts, mt, ")"@), + node_done(a, ts, c, cx, mt + 1, j), + is_statement_kind(kd(a, c)), + 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] == t); + assert(a.nodes[m].children[1] == c); + assert(dc(a, ts, m, t, cx_in(cx, true), x + 2, mt)); + assert(dc(a, ts, m, c, cx, mt + 1, j)); + 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)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.7.3 `while ( Expression ) Statement`. +#[verifier::spinoff_prover] +fn check_while( + 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_words2(); + 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::WhileStatement) || nd.children.len() != 2 { + return None; + } + let t = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let c = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_t, mt, lv_t) = match done_at(done, t) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo_c, j, _) = 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 c_ok = stmt_kind_exec(kind_at(nodes, c)); + 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"@ })) + || !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 { + 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(x.saturating_add(2) as int == x as int + 2); + assert(mt.saturating_add(1) as int == mt as int + 1); + assert(a.nodes[m as int].children =~= seq![t as int, c 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, c as int, g.cx, 1); + assert(lvl(a, t as int) >= -1); + assert(is_statement_kind(kd(a, c as int))); + 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)) +} + +/// §14.7.2 `do Statement while ( Expression ) ;`. +#[verifier::spinoff_prover] +#[verifier::rlimit(60)] +proof fn lemma_do_while_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + b: int, + t: int, + x: int, + mb: int, + q: int, + j: int, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= b < m, + 0 <= t < m, + a.nodes[m].kind == NodeKind::DoWhileStatement, + a.nodes[m].children =~= seq![b, t], + is_kw(ts, x, "do"@), + node_done(a, ts, b, cx, x + 1, mb), + is_statement_kind(kd(a, b)), + is_kw(ts, mb, "while"@), + is_p(ts, mb + 1, "("@), + lvl(a, t) >= -1, + node_done(a, ts, t, cx, mb + 2, q), + is_p(ts, q, ")"@), + semi(ts, q + 1, j, true), + 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] == b); + assert(a.nodes[m].children[1] == t); + assert(dc(a, ts, m, b, cx, x + 1, mb)); + assert(dc(a, ts, m, t, cx_in(cx, true), mb + 2, q)); + assert(ed(a, ts, b, x + 1) == mb); + assert(ed(a, ts, t, mb + 2) == q); + assert(a.nodes[m].children.len() == 2); + assert(is_kw(ts, mb, "while"@)); + assert(semi(ts, q + 1, j, true)); + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.7.2 `do Statement while ( Expression ) ;`. +#[verifier::spinoff_prover] +fn check_do_while( + 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_words2(); + 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::DoWhileStatement) || nd.children.len() != 2 { + return None; + } + let b = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let t = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_b, mb, _) = match done_at(done, b) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo_t, q, lv_t) = match done_at(done, t) { + 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 b_ok = stmt_kind_exec(kind_at(nodes, b)); + if !b_ok || lv_t < -1 || lo_b != x.saturating_add(1) || lo_t != mb.saturating_add(2) { + return None; + } + if !word_at(src, nodes, toks, gaps, x, &[0x64, 0x6F], Ghost(GW { g, w: "do"@ })) + || !word_at(src, nodes, toks, gaps, mb, &[0x77, 0x68, 0x69, 0x6C, 0x65], Ghost(GW { g, w: "while"@ })) + || !punct_at(src, nodes, toks, gaps, mb.saturating_add(1), 0x28, Ghost(GW { g, w: "("@ })) + || !punct_at(src, nodes, toks, gaps, q, 0x29, Ghost(GW { g, w: ")"@ })) { + return None; + } + let j = match find_semi_dw(src, nodes, toks, gaps, q.saturating_add(1), 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(x.saturating_add(1) as int == x as int + 1); + assert(mb.saturating_add(1) as int == mb as int + 1); + assert(mb.saturating_add(2) as int == mb as int + 2); + assert(q.saturating_add(1) as int == q as int + 1); + assert(a.nodes[m as int].children =~= seq![b as int, t 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, b as int, g.cx, 0); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, t as int, g.cx, 1); + assert(lvl(a, t as int) >= -1); + assert(is_statement_kind(kd(a, b as int))); + lemma_do_while_done(a, g.ts, g.cx, m as int, b as int, t as int, x as int, mb as int, q as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.13 `LabelIdentifier : LabelledItem`. +#[verifier::spinoff_prover] +proof fn lemma_labelled_done(a: AstV, ts: Stream, cx: Ctx, m: int, l: int, b: int, x: int, j: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= l < m, + 0 <= b < m, + a.nodes[m].kind == NodeKind::LabelledStatement, + a.nodes[m].children =~= seq![l, b], + kd(a, l) == NodeKind::LabelIdentifier, + node_done(a, ts, l, cx, x, x + 1), + is_p(ts, x + 1, ":"@), + is_stmt_or_function(a, b), + node_done(a, ts, b, cx, x + 2, 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] == l); + assert(a.nodes[m].children[1] == b); + assert(dc(a, ts, m, l, cx, x, x + 1)); + assert(dc(a, ts, m, b, cx, x + 2, j)); + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.13 `LabelIdentifier : LabelledItem`. +#[verifier::spinoff_prover] +fn check_labelled( + 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::LabelledStatement) || nd.children.len() != 2 { + return None; + } + let l = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let b = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_l, hi_l, _) = match done_at(done, l) { + Some(r) => r, + None => { + return None; + }, + }; + let (lo_b, j, _) = match done_at(done, b) { + 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 b_ok = stmt_kind_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; + } + if !punct_at(src, nodes, toks, gaps, x.saturating_add(1), 0x3A, Ghost(GW { g, w: ":"@ })) + || !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(x.saturating_add(2) as int == x as int + 2); + assert(a.nodes[m as int].children =~= seq![l as int, b 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, 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); + } + Some((x, j, -100)) +} + +/// §14.15 `try Block Catch? Finally?`. +#[verifier::spinoff_prover] +proof fn lemma_try_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + b: int, + ca: int, + fi: int, + x: int, + mb: int, + q: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= b < m, + 0 <= ca < m, + 0 <= fi < m, + a.nodes[m].kind == NodeKind::TryStatement, + a.nodes[m].children =~= seq![b, ca, fi], + is_kw(ts, x, "try"@), + kd(a, b) == NodeKind::BlockStatement, + node_done(a, ts, b, cx, x + 1, mb), + kd(a, ca) == NodeKind::Catch || kd(a, ca) == NodeKind::Absent, + kd(a, fi) == NodeKind::Finally || kd(a, fi) == NodeKind::Absent, + kd(a, ca) == NodeKind::Catch || kd(a, fi) == NodeKind::Finally, + d(a, ts, ca, cx, mb, q), + ed(a, ts, ca, mb) == q, + d(a, ts, fi, cx, q, 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] == b); + assert(a.nodes[m].children[1] == ca); + assert(a.nodes[m].children[2] == fi); + assert(dc(a, ts, m, b, cx, x + 1, mb)); + assert(dc(a, ts, m, ca, cx, mb, q)); + assert(dc(a, ts, m, fi, cx, q, j)); + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.15 `try Block Catch`, `try Block Finally`, `try Block Catch Finally`. +#[verifier::spinoff_prover] +fn check_try( + 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_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::TryStatement) || nd.children.len() != 3 { + return None; + } + let b = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let ca = match kid_of(nodes, m, 1) { + Some(c) => c, + None => { + return None; + }, + }; + let fi = 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; + }, + }; + let (lo_b, mb, _) = match done_at(done, b) { + Some(r) => r, + None => { + return None; + }, + }; + if !matches!(kind_at(nodes, b), NodeKind::BlockStatement) || lo_b != x.saturating_add(1) { + return None; + } + if !word_at(src, nodes, toks, gaps, x, &[0x74, 0x72, 0x79], Ghost(GW { g, w: "try"@ })) { + return None; + } + let kc = kind_at(nodes, ca); + let kf = kind_at(nodes, fi); + let has_catch = matches!(kc, NodeKind::Catch); + let has_finally = matches!(kf, NodeKind::Finally); + if !(has_catch || has_finally) { + return None; + } + let q: usize = if has_catch { + let (lo_c, hi_c, _) = match done_at(done, ca) { + Some(r) => r, + None => { + return None; + }, + }; + if lo_c != mb { + return None; + } + hi_c + } else { + if !matches!(kc, NodeKind::Absent) || !check_absent(src, nodes, toks, gaps, ca, mb, Ghost(g)) { + return None; + } + mb + }; + let j: usize = if has_finally { + let (lo_f, hi_f, _) = match done_at(done, fi) { + Some(r) => r, + None => { + return None; + }, + }; + if lo_f != q { + return None; + } + hi_f + } else { + if !matches!(kf, NodeKind::Absent) || !check_absent(src, nodes, toks, gaps, fi, q, Ghost(g)) { + return None; + } + q + }; + 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![b as int, ca as int, fi 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); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, b as int, g.cx, 0); + if has_catch { + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, ca as int, g.cx, 1); + } + if has_finally { + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, fi as int, g.cx, 2); + } + lemma_try_done(a, g.ts, g.cx, m as int, b as int, ca as int, fi as int, x as int, mb as int, q as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.15 `Catch : catch ( CatchParameter ) Block | catch Block`. +#[verifier::spinoff_prover] +proof fn lemma_catch_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + p: int, + blk: int, + x: int, + has_param: bool, + mp: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= p < m, + 0 <= blk < m, + a.nodes[m].kind == NodeKind::Catch, + a.nodes[m].children =~= seq![p, blk], + 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( + a, + ts, + p, + cx, + x + 2, + mp, + ) && is_p(ts, mp, ")"@) && node_done(a, ts, blk, cx, mp + 1, j) + } else { + kd(a, p) == NodeKind::Absent && d(a, ts, p, cx, x + 1, x + 1) && node_done( + a, + ts, + blk, + 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] == p); + assert(a.nodes[m].children[1] == blk); + if has_param { + assert(dc(a, ts, m, p, cx, x + 2, mp)); + assert(dc(a, ts, m, blk, cx, mp + 1, j)); + } else { + assert(dc(a, ts, m, p, cx, x + 1, x + 1)); + assert(dc(a, ts, m, blk, cx, x + 1, j)); + } + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.15 `Catch : catch ( CatchParameter ) Block | catch Block`. +#[verifier::spinoff_prover] +fn check_catch( + 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_words2(); + 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::Catch) || nd.children.len() != 2 { + return None; + } + let p = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let blk = 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_k, j, _) = match done_at(done, blk) { + Some(r) => r, + None => { + return None; + }, + }; + if !matches!(kind_at(nodes, blk), NodeKind::BlockStatement) { + return None; + } + if !word_at(src, nodes, toks, gaps, x, &[0x63, 0x61, 0x74, 0x63, 0x68], Ghost(GW { g, w: "catch"@ })) { + return None; + } + let x1 = x.saturating_add(1); + let kp = kind_at(nodes, p); + let has_param = matches!(kp, NodeKind::BindingIdentifier); + let mp: usize = if has_param { + let (lo_p, hi_p, _) = match done_at(done, p) { + Some(r) => r, + None => { + return None; + }, + }; + if lo_p != x.saturating_add(2) || lo_k != hi_p.saturating_add(1) || !punct_at(src, nodes, toks, gaps, x1, 0x28, Ghost(GW { g, w: "("@ })) + || !punct_at(src, nodes, toks, gaps, hi_p, 0x29, Ghost(GW { g, w: ")"@ })) { + return None; + } + hi_p + } else { + if !matches!(kp, NodeKind::Absent) || !check_absent(src, nodes, toks, gaps, p, x1, Ghost(g)) || lo_k != x1 { + return None; + } + 0 + }; + let _ = mp; + 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![p as int, blk 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, blk as int, g.cx, 1); + if has_param { + assert(x.saturating_add(2) as int == x as int + 2); + assert(mp.saturating_add(1) as int == mp as int + 1); + lemma_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, p as int, g.cx, 0); + } + lemma_catch_done(a, g.ts, g.cx, m as int, p as int, blk as int, x as int, has_param, mp as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.15 `Finally : finally Block`. +#[verifier::spinoff_prover] +proof fn lemma_finally_done(a: AstV, ts: Stream, cx: Ctx, m: int, blk: int, x: int, j: int) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= blk < m, + a.nodes[m].kind == NodeKind::Finally, + a.nodes[m].children =~= seq![blk], + is_kw(ts, x, "finally"@), + kd(a, blk) == NodeKind::BlockStatement, + node_done(a, ts, blk, 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] == blk); + assert(dc(a, ts, m, blk, cx, x + 1, j)); + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.15 `Finally : finally Block`. +#[verifier::spinoff_prover] +fn check_finally( + 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_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::Finally) || nd.children.len() != 1 { + return None; + } + let blk = 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_k, j, _) = match done_at(done, blk) { + Some(r) => r, + None => { + return None; + }, + }; + if !matches!(kind_at(nodes, blk), NodeKind::BlockStatement) || lo_k != x.saturating_add(1) { + return None; + } + if !word_at(src, nodes, toks, gaps, x, &[0x66, 0x69, 0x6E, 0x61, 0x6C, 0x6C, 0x79], Ghost(GW { g, w: "finally"@ })) + || !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![blk 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, blk as int, g.cx, 0); + lemma_finally_done(a, g.ts, g.cx, m as int, blk as int, x as int, j as int); + } + Some((x, j, -100)) +} + +/// What [`check_dl_from`] checks: the children `from..` of node `m` are statement list +/// items, or (`clauses`) the clauses of a `switch`, over the tokens `start..end`. +pub struct DlSpec { + /// The node whose children are listed. + pub m: usize, + /// The first token of the list (where it ends, if empty). + pub start: usize, + /// One past the last token of the list. + pub end: usize, + /// The index of the first child of the list. + pub from: usize, + /// The children are `CaseClause`s and `DefaultClause`s. + pub clauses: bool, +} + +/// A `CaseClause` or `DefaultClause` kind, executable. +fn clause_kind_exec(k: NodeKind) -> (r: bool) + ensures + r ==> (k == NodeKind::CaseClause || k == NodeKind::DefaultClause), +{ + matches!(k, NodeKind::CaseClause | NodeKind::DefaultClause) +} + +/// The children `from..` of `rng.m` derive consecutive token ranges from `start` to +/// `end` (`spec::dl`): statement list items, or (`clauses`) the clauses of a `switch`. +fn check_dl_from( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + sp: &DlSpec, + Ghost(g): Ghost, +) -> (r: bool) + requires + sp.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@, sp.m as int), + cx_ok(arena_of(nodes@), done.cxs@, sp.m as int, g.cx), + sp.end <= toks.len(), + ensures + r ==> (dl( + arena_of(nodes@), + g.ts, + sp.m as int, + arena_of(nodes@).nodes[sp.m as int].children, + sp.from as int, + g.cx, + sp.start as int, + sp.end as int, + ) && sp.start <= sp.end && sp.from <= arena_of(nodes@).nodes[sp.m as int].children.len() + && (forall|q: int| + sp.from <= q < arena_of(nodes@).nodes[sp.m as int].children.len() ==> (if sp.clauses { + kd(arena_of(nodes@), #[trigger] arena_of(nodes@).nodes[sp.m as int].children[q]) + == NodeKind::CaseClause || kd(arena_of(nodes@), arena_of(nodes@).nodes[sp.m as int].children[q]) + == NodeKind::DefaultClause + } else { + is_item_kind(kd(arena_of(nodes@), #[trigger] arena_of(nodes@).nodes[sp.m as int].children[q])) + }))), +{ + let _ = (src.len(), gaps.len(), toks.len()); + let m = sp.m; + let nd = match nodes.get(m) { + Some(x) => x, + 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); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + if sp.from > len { + return false; + } + let mut k = len; + let mut pos = sp.end; + while k > sp.from + 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(), + sp.from <= k <= len, + pos <= toks.len(), + pos <= sp.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), + 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, + sp.end as int, + ), + forall|q: int| + k <= q < len ==> (if sp.clauses { + kd(a, #[trigger] a.nodes[m as int].children[q]) == NodeKind::CaseClause || kd( + a, + a.nodes[m as int].children[q], + ) == NodeKind::DefaultClause + } else { + is_item_kind(kd(a, #[trigger] a.nodes[m as int].children[q])) + }), + decreases k, + { + let c = match nd.children.as_slice().get(k.saturating_sub(1)) { + Some(x) => *x as usize, + None => { + return false; + }, + }; + 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] == c as int); + lemma_child_before(a, m as int, k as int - 1); + } + if c >= m { + return false; + } + let lo_c = match done.lo.get(c) { + Some(x) => *x, + None => { + return false; + }, + }; + let hi_c = match done.hi.get(c) { + Some(x) => *x, + None => { + return false; + }, + }; + let item = match nodes.get(c) { + Some(x) => if sp.clauses { + clause_kind_exec(x.kind) + } else { + item_kind_exec(x.kind) + }, + None => false, + }; + if hi_c != pos || !item { + return false; + } + proof { + assert(a.nodes[c as int] == nodes@[c as int]@); + assert(kd(a, c as int) == nodes@[c as int].kind); + assert(kd(a, c as int) != NodeKind::Absent); + 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); + let ks = a.nodes[m as int].children; + assert(node_done(a, g.ts, c as int, g.cx, lo_c as int, hi_c as int)); + lemma_dl_cons(a, g.ts, m as int, ks, k as int - 1, g.cx, lo_c as int, hi_c as int, sp.end as int); + } + pos = lo_c; + k = k.saturating_sub(1); + } + if pos != sp.start { + return false; + } + true +} + +/// A `switch` has at most one `DefaultClause` (§14.12). +fn check_one_default(nodes: &[Node], m: usize) -> (r: bool) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + r ==> at_most_one_default( + arena_of(nodes@), + arena_of(nodes@).nodes[m as int].children, + ), +{ + let nd = match nodes.get(m) { + Some(x) => x, + 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); + } + if len <= 1 { + return true; + } + let mut k: usize = 1; + let mut seen = false; + while k < len + invariant + m < nodes.len(), + nodes@.len() == nodes.len(), + a == arena_of(nodes@), + ast_wf(a), + len == a.nodes[m as int].children.len(), + 1 <= k <= len, + forall|x: int, y: int| + 1 <= x < y < k ==> !(kd(a, #[trigger] a.nodes[m as int].children[x]) + == NodeKind::DefaultClause && kd(a, #[trigger] a.nodes[m as int].children[y]) + == NodeKind::DefaultClause), + !seen ==> forall|x: int| + 1 <= x < k ==> kd(a, #[trigger] a.nodes[m as int].children[x]) + != NodeKind::DefaultClause, + decreases len - k, + { + let c = match kid_of(nodes, m, k) { + Some(c) => c, + None => { + return false; + }, + }; + let is_default = matches!(kind_at(nodes, c), NodeKind::DefaultClause); + if is_default { + if seen { + proof { + // A default before: the loop invariant `seen` is only set after one. + } + return false; + } + seen = true; + } + k = k.saturating_add(1); + } + true +} + +/// §14.12 `CaseClause : case Expression : StatementList?`. +#[verifier::spinoff_prover] +proof fn lemma_case_done(a: AstV, ts: Stream, cx: Ctx, m: int, e: int, x: int, me: int, j: int) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= e < m, + a.nodes[m].kind == NodeKind::CaseClause, + a.nodes[m].children.len() >= 1, + a.nodes[m].children[0] == e, + is_kw(ts, x, "case"@), + lvl(a, e) >= -1, + forall|k: int| + 1 <= k < a.nodes[m].children.len() ==> is_item_kind( + kd(a, #[trigger] a.nodes[m].children[k]), + ), + node_done(a, ts, e, cx, x + 1, me), + is_p(ts, me, ":"@), + dl(a, ts, m, a.nodes[m].children, 1, cx, me + 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(dc(a, ts, m, e, cx_in(cx, true), x + 1, me)); + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.12 `CaseClause : case Expression : StatementList?`. +#[verifier::spinoff_prover] +fn check_case( + 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_words2(); + 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::CaseClause) || nd.children.is_empty() { + return None; + } + let len = nd.children.len(); + let e = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_e, me, lv_e) = match done_at(done, e) { + 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 lv_e < -1 || lo_e != x.saturating_add(1) { + return None; + } + if !word_at(src, nodes, toks, gaps, x, &[0x63, 0x61, 0x73, 0x65], Ghost(GW { g, w: "case"@ })) + || !punct_at(src, nodes, toks, gaps, me, 0x3A, Ghost(GW { g, w: ":"@ })) { + return None; + } + let start = me.saturating_add(1); + let j: usize = if len == 1 { + 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; + }, + } + }; + if j > toks.len() { + return None; + } + let rng = DlSpec { m, start, end: j, from: 1, clauses: false }; + if !check_dl_from(src, nodes, toks, gaps, done, &rng, Ghost(g)) { + 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(a.nodes[m as int] == nodes@[m as int]@); + assert(a.nodes[m as int].children.len() == len); + assert(x.saturating_add(1) as int == x as int + 1); + assert(me.saturating_add(1) as int == me 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_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, e as int, g.cx, 0); + assert(lvl(a, e as int) >= -1); + lemma_case_done(a, g.ts, g.cx, m as int, e as int, x as int, me as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.12 `DefaultClause : default : StatementList?`. +#[verifier::spinoff_prover] +proof fn lemma_default_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::DefaultClause, + is_kw(ts, x, "default"@), + is_p(ts, x + 1, ":"@), + all_items(a, a.nodes[m].children), + dl(a, ts, m, a.nodes[m].children, 0, cx, x + 2, 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(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.12 `DefaultClause : default : StatementList?`. +#[verifier::spinoff_prover] +fn check_default( + 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_words2(); + 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::DefaultClause) { + return None; + } + let len = nd.children.len(); + 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, &[0x64, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74], Ghost(GW { g, w: "default"@ })) + || !punct_at(src, nodes, toks, gaps, x.saturating_add(1), 0x3A, Ghost(GW { g, w: ":"@ })) { + return None; + } + let start = x.saturating_add(2); + let j: 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; + }, + } + }; + if j > toks.len() { + return None; + } + let rng = DlSpec { m, start, end: j, from: 0, clauses: false }; + if !check_dl_from(src, nodes, toks, gaps, done, &rng, Ghost(g)) { + 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(a.nodes[m as int] == nodes@[m as int]@); + assert(x.saturating_add(1) as int == x as int + 1); + assert(x.saturating_add(2) as int == x as int + 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); + assert(all_items(a, a.nodes[m as int].children)); + lemma_default_done(a, g.ts, g.cx, m as int, x as int, j as int); + } + Some((x, j, -100)) +} + +/// §14.12 `switch ( Expression ) CaseBlock`. +#[verifier::spinoff_prover] +#[verifier::rlimit(60)] +proof fn lemma_switch_done(a: AstV, ts: Stream, cx: Ctx, m: int, e: int, x: int, me: int, j: int) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + 0 <= m < a.nodes.len(), + 0 <= e < m, + a.nodes[m].kind == NodeKind::SwitchStatement, + a.nodes[m].children.len() >= 1, + a.nodes[m].children[0] == e, + is_kw(ts, x, "switch"@), + is_p(ts, x + 1, "("@), + lvl(a, e) >= -1, + all_kind2(a, a.nodes[m].children, 1, NodeKind::CaseClause, NodeKind::DefaultClause), + at_most_one_default(a, a.nodes[m].children), + node_done(a, ts, e, cx, x + 2, me), + is_p(ts, me, ")"@), + is_p(ts, me + 1, "{"@), + is_p(ts, j - 1, "}"@), + me + 2 <= j - 1, + dl(a, ts, m, a.nodes[m].children, 1, cx, me + 2, j - 1), + 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(dc(a, ts, m, e, cx_in(cx, true), x + 2, me)); + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.12 `switch ( Expression ) CaseBlock`. +#[verifier::spinoff_prover] +fn check_switch( + 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_words2(); + lemma_punct_text(); + 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; + }, + }; + if !matches!(nd.kind, NodeKind::SwitchStatement) || nd.children.is_empty() { + return None; + } + let len = nd.children.len(); + let e = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let (lo_e, me, lv_e) = match done_at(done, e) { + 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 lv_e < -1 || lo_e != x.saturating_add(2) { + return None; + } + if !word_at(src, nodes, toks, gaps, x, &[0x73, 0x77, 0x69, 0x74, 0x63, 0x68], Ghost(GW { g, w: "switch"@ })) + || !punct_at(src, nodes, toks, gaps, x.saturating_add(1), 0x28, Ghost(GW { g, w: "("@ })) + || !punct_at(src, nodes, toks, gaps, me, 0x29, Ghost(GW { g, w: ")"@ })) + || !punct_at(src, nodes, toks, gaps, me.saturating_add(1), 0x7B, Ghost(GW { g, w: "{"@ })) { + return None; + } + let start = me.saturating_add(2); + let close: usize = if len == 1 { + 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; + }, + } + }; + if close > toks.len() { + return None; + } + let rng = DlSpec { m, start, end: close, from: 1, clauses: true }; + if !check_dl_from(src, nodes, toks, gaps, done, &rng, Ghost(g)) + || !check_one_default(nodes, m) { + return None; + } + if !punct_at(src, nodes, toks, gaps, close, 0x7D, Ghost(GW { g, w: "}"@ })) { + return None; + } + let j = close.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(a.nodes[m as int].children.len() == len); + assert(x.saturating_add(1) as int == x as int + 1); + assert(x.saturating_add(2) as int == x as int + 2); + assert(me.saturating_add(1) as int == me as int + 1); + assert(me.saturating_add(2) as int == me as int + 2); + assert(close.saturating_add(1) as int == close 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_inv_get(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, e as int, g.cx, 0); + assert(lvl(a, e as int) >= -1); + assert(all_kind2(a, a.nodes[m as int].children, 1, NodeKind::CaseClause, NodeKind::DefaultClause)); + lemma_switch_done(a, g.ts, g.cx, m as int, e as int, x as int, me as int, j as int); + } + Some((x, j, -100)) +} + +// --------------------------------------------------------------------- +// Functions (§15.1 and §15.2): `return`, formal parameters, and the function +// declarations and expressions, which give their parts the contexts of +// `cx_fn` (a node is checked in the context `Done.cxs` assigns it). +// --------------------------------------------------------------------- + +/// The ghost context of an assigned one, for a function's part. +pub open spec fn fcx(y: bool, a: bool, r: bool) -> Ctx { + cx_fn(y, a, r) +} + +/// Node `j < m`, the child `k` of node `m`, is done in the context `cxs` assigns it. +proof fn lemma_inv_get_at( + a: AstV, + ts: Stream, + cxs: Seq, + lo: Seq, + hi: Seq, + lv: Seq, + m: int, + j: int, +) + requires + inv(a, ts, cxs, lo, hi, lv, m), + 0 <= j < m, + ensures + kd(a, j) != NodeKind::Absent ==> node_done(a, ts, j, cx_of(cxs[j]), lo[j] as int, hi[j] as int), + lvl(a, j) != -100 ==> node_done(a, ts, j, cx_of(cxs[j]), lo[j] as int, hi[j] as int), + lv[j] as int == lvl(a, j), +{ + reveal(inv); + assert(lv[j] as int == lvl(a, j)); + if kd(a, j) == NodeKind::Absent { + assert(lvl(a, j) == -100); + } +} + +/// No child of node `m` but the last is a `...rest`. +fn check_rest_last(nodes: &[Node], m: usize) -> (r: bool) + requires + m < nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + r ==> rest_only_last(arena_of(nodes@), arena_of(nodes@).nodes[m as int].children), +{ + let nd = match nodes.get(m) { + Some(x) => x, + 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 = len.saturating_sub(1); + proof { + let ks = a.nodes[m as int].children; + assert(rest_free_from(a, ks, k as int)); + } + while k > 0 + invariant + m < nodes.len(), + nodes@.len() == nodes.len(), + a == arena_of(nodes@), + ast_wf(a), + len == a.nodes[m as int].children.len(), + k < len || k == 0, + rest_free_from(a, a.nodes[m as int].children, k as int), + decreases k, + { + let c = match kid_of(nodes, m, k.saturating_sub(1)) { + Some(c) => c, + None => { + return false; + }, + }; + let kc = kind_at(nodes, c); + if matches!( + kc, + NodeKind::FunctionRestParameter | NodeKind::BindingRestElement | NodeKind::BindingRestProperty + | NodeKind::AssignmentRestElement | NodeKind::AssignmentRestProperty + ) { + return false; + } + proof { + assert(k.saturating_sub(1) as int == k as int - 1); + let ks = a.nodes[m as int].children; + assert(ks[k as int - 1] == c as int); + assert(!is_rest_kind(kd(a, ks[k as int - 1]))); + assert(rest_free_from(a, ks, k as int - 1)); + } + k = k.saturating_sub(1); + } + true +} + +/// §15.1 `FormalParameters : ( FormalParameterList? )`. +#[verifier::spinoff_prover] +proof fn lemma_params_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, + x + 1 <= e, + e < tn(ts), + a.nodes[m].kind == NodeKind::FormalParameters, + 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], + 4, + ), + 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(all_kind2(a, a.nodes[m].children, 0, NodeKind::BindingElement, NodeKind::FunctionRestParameter)) by { + 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::FunctionRestParameter by { + assert(list_item_ok(a, a.nodes[m].children[k], 4)); + } + } + 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.1 `FormalParameters : ( FormalParameterList? )`. +#[verifier::spinoff_prover] +fn check_params( + 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::FormalParameters) { + 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, 0x28, Ghost(GW { g, w: "("@ })) { + return None; + } + let ls = ListSpec { m, start: x.saturating_add(1), mode: 4, trail: true }; + let e = match check_list(src, nodes, toks, gaps, done, &ls, Ghost(g)) { + Some(e) => e, + None => { + return None; + }, + }; + if !punct_at(src, nodes, toks, gaps, e, 0x29, Ghost(GW { g, w: ")"@ })) + || !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(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_params_done(a, g.ts, g.cx, m as int, x as int, e as int); + } + Some((x, e.saturating_add(1), -100)) +} + +/// §14.10 `return Expression? ;`. +#[verifier::spinoff_prover] +proof fn lemma_return_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c: int, + x: int, + has_expr: bool, + me: int, + j: int, +) + requires + toks_wf(ts), + cx_in(cx, true) == cx, + cx.return_, + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::ReturnStatement, + a.nodes[m].children =~= seq![c], + is_kw(ts, x, "return"@), + if has_expr { + !nl(ts, x + 1) && lvl(a, c) >= -1 && node_done(a, ts, c, cx, x + 1, me) && semi( + ts, + me, + j, + false, + ) + } else { + kd(a, c) == NodeKind::Absent && d(a, ts, c, cx, x + 1, x + 1) && semi( + ts, + x + 1, + j, + false, + ) + }, + 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); + if has_expr { + assert(kd(a, c) != NodeKind::Absent); + assert(dc(a, ts, m, c, cx_in(cx, true), x + 1, me)); + } else { + assert(dc(a, ts, m, c, cx, x + 1, x + 1)); + } + assert(d_stmt(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §14.10 `return ;` and `return [no LineTerminator here] Expression ;`. +#[verifier::spinoff_prover] +fn check_return( + 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_words3(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let cxm = match done.cxs.get(m) { + Some(c) => c, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::ReturnStatement) || nd.children.len() != 1 || !cxm.r { + 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; + }, + }; + if !word_at(src, nodes, toks, gaps, x, &[0x72, 0x65, 0x74, 0x75, 0x72, 0x6E], Ghost(GW { g, w: "return"@ })) { + return None; + } + let x1 = x.saturating_add(1); + let has_expr = !matches!(kind_at(nodes, c), NodeKind::Absent); + let (me, j): (usize, usize) = if has_expr { + let (lo_c, hi_c, lv_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + if lv_c < -1 || lo_c != x1 || !no_nl_at(src, nodes, toks, gaps, x1, Ghost(g)) { + return None; + } + let j = match find_semi(src, nodes, toks, gaps, hi_c, nd.end, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + (hi_c, j) + } else { + if !check_absent(src, nodes, toks, gaps, c, x1, Ghost(g)) { + return None; + } + let j = match find_semi(src, nodes, toks, gaps, x1, nd.end, Ghost(g)) { + Some(j) => j, + None => { + return None; + }, + }; + (0, j) + }; + let _ = me; + 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(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].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(cxm == &done.cxs@[m as int]); + assert(g.cx.return_); + if has_expr { + 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) >= -1); + } + lemma_return_done(a, g.ts, g.cx, m as int, c as int, x as int, has_expr, me as int, j as int); + } + Some((x, j, -100)) +} + +/// The words `return` and `function`, as code points. +proof fn lemma_words3() + ensures + cps("return"@) =~= seq![0x72u32, 0x65, 0x74, 0x75, 0x72, 0x6E], + cps("function"@) =~= seq![0x66u32, 0x75, 0x6E, 0x63, 0x74, 0x69, 0x6F, 0x6E], + cps("async"@) =~= seq![0x61u32, 0x73, 0x79, 0x6E, 0x63], + cps("*"@) =~= seq![0x2Au32], +{ + reveal_strlit("return"); + reveal_strlit("function"); + reveal_strlit("async"); + reveal_strlit("*"); + assert("return"@ =~= seq!['r', 'e', 't', 'u', 'r', 'n']); + assert("function"@ =~= seq!['f', 'u', 'n', 'c', 't', 'i', 'o', 'n']); + assert("async"@ =~= seq!['a', 's', 'y', 'n', 'c']); + assert("*"@ =~= seq!['*']); +} + +/// The kind of node of the function with the given flags. +pub open spec fn fn_kind(is_async: bool, is_gen: bool, is_expr: bool) -> NodeKind { + if is_async { + if is_gen { + if is_expr { + NodeKind::AsyncGeneratorExpression + } else { + NodeKind::AsyncGeneratorDeclaration + } + } else if is_expr { + NodeKind::AsyncFunctionExpression + } else { + NodeKind::AsyncFunctionDeclaration + } + } else if is_gen { + if is_expr { + NodeKind::GeneratorExpression + } else { + NodeKind::GeneratorDeclaration + } + } else if is_expr { + NodeKind::FunctionExpression + } else { + NodeKind::FunctionDeclaration + } +} + +/// The flags (`async`, generator, expression) of a function kind. +fn fn_flags(k: NodeKind) -> (r: Option<(bool, bool, bool)>) + ensures + match r { + Some((a, g, e)) => k == fn_kind(a, g, e), + None => true, + }, +{ + match k { + NodeKind::FunctionDeclaration => Some((false, false, false)), + NodeKind::FunctionExpression => Some((false, false, true)), + NodeKind::GeneratorDeclaration => Some((false, true, false)), + NodeKind::GeneratorExpression => Some((false, true, true)), + NodeKind::AsyncFunctionDeclaration => Some((true, false, false)), + NodeKind::AsyncFunctionExpression => Some((true, false, true)), + NodeKind::AsyncGeneratorDeclaration => Some((true, true, false)), + NodeKind::AsyncGeneratorExpression => Some((true, true, true)), + _ => None, + } +} + +/// The three children of node `m` are checked in `c0`, `c1`, `c2`. +pub open spec fn cx_kids3(a: AstV, cxs: Seq, m: int, c0: Ctx, c1: Ctx, c2: Ctx) -> bool { + 0 <= m < a.nodes.len() && a.nodes[m].children.len() == 3 && 0 <= a.nodes[m].children[0] + < cxs.len() && 0 <= a.nodes[m].children[1] < cxs.len() && 0 <= a.nodes[m].children[2] + < 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 +} + +/// The contexts of the parts of a function (§15.2): the name in `cx` of a +/// declaration, in the function's own of an expression; the parameters and the +/// body in `cx_fn`. +pub open spec fn fn_cx(cx: Ctx, is_async: bool, is_gen: bool, is_expr: bool) -> (Ctx, Ctx, Ctx) { + ( + if is_expr { + cx_fn(is_gen, is_async, false) + } else { + cx + }, + cx_fn(is_gen, is_async, false), + cx_fn(is_gen, is_async, true), + ) +} + +/// §15.2 `function BindingIdentifier? ( FormalParameters ) { FunctionBody }` and the +/// generator, `async` and `async` generator forms (`d_function`). +#[verifier::spinoff_prover] +proof fn lemma_function_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + n0: int, + p: int, + b: int, + is_async: bool, + is_gen: bool, + is_expr: bool, + x: int, + nm: 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 == fn_kind(is_async, is_gen, is_expr), + a.nodes[m].children =~= seq![n0, p, b], + !is_async || (is_kw(ts, x, "async"@) && !nl(ts, x + 1)), + is_kw(ts, if is_async { + x + 1 + } else { + x + }, "function"@), + nm == (if is_async { + x + 2 + } else { + x + 1 + }) + (if is_gen { + 1int + } else { + 0int + }), + !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 + } else { + kd(a, n0) == NodeKind::BindingIdentifier && node_done( + a, + ts, + n0, + fn_cx(cx, is_async, is_gen, is_expr).0, + nm, + mn, + ) + }, + kd(a, p) == NodeKind::FormalParameters, + node_done(a, ts, p, fn_cx(cx, is_async, is_gen, is_expr).1, mn, q), + kd(a, b) == NodeKind::FunctionBody, + node_done(a, ts, b, fn_cx(cx, is_async, is_gen, is_expr).2, q, 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) == (if is_expr { + 17int + } else { + -100int + }), +{ + 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 cxn = fn_cx(cx, is_async, is_gen, is_expr).0; + assert(cxn == (if is_expr { + cx_fn(is_gen, is_async, false) + } else { + cx + })); + assert(dc(a, ts, m, n0, cxn, 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_function(a, ts, m, cx, x, j, is_async, is_gen, is_expr)); + assert(d_func(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// What `check_function` reads: the node and the flags of its kind. +pub struct FuncShape { + /// The node. + pub m: usize, + /// `async`. + pub is_async: bool, + /// A generator. + pub is_gen: bool, + /// An expression (not a declaration). + pub is_expr: bool, +} + +/// §15.2 and §15.5, §15.8 and §15.6: a function, generator, `async` function or +/// `async` generator, declaration or expression. +#[verifier::spinoff_prover] +fn check_function( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + fs: &FuncShape, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + fs.m < nodes.len(), + nodes@[fs.m as int].kind == fn_kind(fs.is_async, fs.is_gen, fs.is_expr), + env_ok(src@, nodes@, toks@, gaps@, g.ts), + inv(arena_of(nodes@), g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, fs.m as int), + 0 <= fs.m < done.cxs@.len(), + cx_of(done.cxs@[fs.m as int]) == g.cx, + cx_kids3( + arena_of(nodes@), + done.cxs@, + fs.m as int, + fn_cx(g.cx, fs.is_async, fs.is_gen, fs.is_expr).0, + fn_cx(g.cx, fs.is_async, fs.is_gen, fs.is_expr).1, + fn_cx(g.cx, fs.is_async, fs.is_gen, fs.is_expr).2, + ), + ensures + match r { + Some((l0, h0, l)) => node_done( + arena_of(nodes@), + g.ts, + fs.m as int, + g.cx, + l0 as int, + h0 as int, + ) && l as int == lvl(arena_of(nodes@), fs.m as int), + None => true, + }, +{ + let m = fs.m; + proof { + lemma_words3(); + 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 { + 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; + }, + }; + let x1 = x.saturating_add(1); + let s: usize = if fs.is_async { x1 } else { x }; + if fs.is_async + && (!word_at(src, nodes, toks, gaps, x, &[0x61, 0x73, 0x79, 0x6E, 0x63], Ghost(GW { g, w: "async"@ })) + || !no_nl_at(src, nodes, toks, gaps, x1, Ghost(g))) { + return None; + } + if !word_at(src, nodes, toks, gaps, s, &[0x66, 0x75, 0x6E, 0x63, 0x74, 0x69, 0x6F, 0x6E], Ghost(GW { g, w: "function"@ })) { + return None; + } + let nm: usize = if fs.is_gen { s.saturating_add(2) } else { s.saturating_add(1) }; + if fs.is_gen + && !punct_at(src, nodes, toks, gaps, s.saturating_add(1), 0x2A, Ghost(GW { g, w: "*"@ })) { + return None; + } + let ghost cxn = fn_cx(g.cx, fs.is_async, fs.is_gen, fs.is_expr).0; + let ghost cxp = fn_cx(g.cx, fs.is_async, fs.is_gen, fs.is_expr).1; + 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 })) { + return None; + } + nm + } else { + if !matches!(kn, NodeKind::BindingIdentifier) { + return None; + } + let (lo_n, hi_n, _) = match done_at(done, n0) { + Some(r) => r, + None => { + return None; + }, + }; + if lo_n != nm { + return None; + } + hi_n + }; + 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; + }, + }; + if !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(x1 as int == x as int + 1); + assert(s as int == (if fs.is_async { x as int + 1 } else { x 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].start == nd.start as int); + assert(a.nodes[m as int].end == nd.end as int); + assert(s < toks.len()); + assert(nm as int == (if fs.is_async { x as int + 2 } else { x as int + 1 }) + (if fs.is_gen { 1int } else { 0int })); + 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(cx_of(done.cxs@[p as int]) == cxp); + assert(cx_of(done.cxs@[b as int]) == cxb); + if !matches!(kn, NodeKind::Absent) { + lemma_inv_get_at(a, g.ts, done.cxs@, done.lo@, done.hi@, done.lv@, m as int, n0 as int); + assert(cx_of(done.cxs@[n0 as int]) == cxn); + } + lemma_function_done( + a, + g.ts, + g.cx, + m as int, + n0 as int, + p as int, + b as int, + fs.is_async, + fs.is_gen, + fs.is_expr, + x as int, + nm as int, + mn as int, + q as int, + j as int, + ); + } + let l: i32 = if fs.is_expr { 17 } else { -100 }; + Some((x, j, l)) +} + +/// The three children of node `m` are checked in `c0`, `c1` and `c2`. +fn kids_cx3(nodes: &[Node], cxs: &[Cx5], m: usize, c0: &Cx5, c1: &Cx5, c2: &Cx5) -> (r: bool) + requires + m < nodes.len(), + cxs.len() == nodes.len(), + ast_wf(arena_of(nodes@)), + ensures + r ==> cx_kids3(arena_of(nodes@), cxs@, m as int, cx_of(*c0), cx_of(*c1), cx_of(*c2)), +{ + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return false; + }, + }; + if nd.children.len() != 3 { + 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 (k0, k1, k2) = match (cxs.get(a0), cxs.get(a1), cxs.get(a2)) { + (Some(x), Some(y), Some(z)) => (x, y, z), + _ => { + return false; + }, + }; + cx_eq(k0, c0) && cx_eq(k1, c1) && cx_eq(k2, c2) +} + +/// The contexts of the parts of a function, as carried: `fn_cx`. +fn fn_cx5(cx: &Cx5, is_async: bool, is_gen: bool, is_expr: bool) -> (r: (Cx5, Cx5, Cx5)) + ensures + cx_of(r.0) == fn_cx(cx_of(*cx), is_async, is_gen, is_expr).0, + cx_of(r.1) == fn_cx(cx_of(*cx), is_async, is_gen, is_expr).1, + cx_of(r.2) == fn_cx(cx_of(*cx), is_async, is_gen, is_expr).2, +{ + let own = Cx5 { y: is_gen, a: is_async, i: true, r: false, d: false }; + let n0 = if is_expr { own } else { *cx }; + let body = Cx5 { y: is_gen, a: is_async, i: true, r: true, d: false }; + (n0, own, body) +} + +// --------------------------------------------------------------------- +// Arrow functions (§15.3, §15.9), `yield` (§15.5) and `await` (§15.8). +// --------------------------------------------------------------------- + +/// The context of the parameters of an arrow function: the parent's, but an `async` +/// arrow's are `[+Await]` (§15.9). +pub open spec fn arrow_pcx(cx: Ctx, is_async: bool, ident: bool) -> Ctx { + if !is_async { + cx + } else if ident { + Ctx { yield_: cx.yield_, await_: true, in_: true, return_: false, deflt: false } + } else { + cx_fn(false, true, false) + } +} + +/// The context of the body of an arrow function: `cx_fn` for a braced one, `[?In]` +/// for an expression. +pub open spec fn arrow_bcx(cx: Ctx, is_async: bool, braced: bool) -> Ctx { + if braced { + cx_fn(false, is_async, true) + } else { + Ctx { yield_: false, await_: is_async, in_: cx.in_, return_: false, deflt: false } + } +} + +/// The two children of node `m` are checked in `c0` and `c1`. +pub open spec fn cx_kids2(a: AstV, cxs: Seq, m: int, c0: Ctx, c1: Ctx) -> bool { + 0 <= m < a.nodes.len() && a.nodes[m].children.len() == 2 && 0 <= a.nodes[m].children[0] + < cxs.len() && 0 <= a.nodes[m].children[1] < cxs.len() && cx_of( + cxs[a.nodes[m].children[0]], + ) == c0 && cx_of(cxs[a.nodes[m].children[1]]) == c1 +} + +/// §15.3 `ArrowFunction`, §15.9 `AsyncArrowFunction`. +#[verifier::spinoff_prover] +proof fn lemma_arrow_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + p: int, + b: int, + is_async: bool, + x: int, + mp: int, + j: int, +) + requires + toks_wf(ts), + 0 <= m < a.nodes.len(), + 0 <= p < m, + 0 <= b < m, + a.nodes[m].kind == (if is_async { + NodeKind::AsyncArrowFunction + } else { + NodeKind::ArrowFunction + }), + a.nodes[m].children =~= seq![p, b], + !is_async || (is_kw(ts, x, "async"@) && !nl(ts, x + 1)), + kd(a, p) == NodeKind::BindingIdentifier || kd(a, p) == NodeKind::FormalParameters, + node_done( + a, + ts, + p, + arrow_pcx(cx, is_async, kd(a, p) == NodeKind::BindingIdentifier), + if is_async { + x + 1 + } else { + x + }, + mp, + ), + is_p(ts, mp, "=>"@), + !nl(ts, mp), + if kd(a, b) == NodeKind::FunctionBody { + node_done(a, ts, b, arrow_bcx(cx, is_async, true), mp + 1, j) + } else { + is_assign_expr(a, b) && !is_p(ts, mp + 1, "{"@) && node_done( + a, + ts, + b, + arrow_bcx(cx, is_async, false), + mp + 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) == 0, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == p); + assert(a.nodes[m].children[1] == b); + let s = if is_async { + x + 1 + } else { + x + }; + if is_async { + assert(dc(a, ts, m, p, arrow_pcx(cx, true, kd(a, p) == NodeKind::BindingIdentifier), x + 1, mp)); + assert(d_func(a, ts, m, cx, x, j)); + } else { + assert(dc(a, ts, m, p, cx, x, mp)); + assert(d_func(a, ts, m, cx, x, j)); + } + assert(ed(a, ts, p, s) == mp); + lemma_ed_of(a, ts, m, x, j); +} + +/// What `check_arrow` reads: the node and whether it is `async`. +pub struct ArrowShape { + /// The node. + pub m: usize, + /// `async`. + pub is_async: bool, +} + +/// §15.3 `ArrowParameters => ConciseBody` and §15.9 `async ArrowParameters => ConciseBody`. +#[verifier::spinoff_prover] +fn check_arrow( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + done: &Done, + sh: &ArrowShape, + Ghost(g): Ghost, +) -> (r: Option<(usize, usize, i32)>) + requires + sh.m < nodes.len(), + nodes@[sh.m as int].kind == (if sh.is_async { + NodeKind::AsyncArrowFunction + } else { + NodeKind::ArrowFunction + }), + 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_kids2( + arena_of(nodes@), + done.cxs@, + sh.m as int, + arrow_pcx( + g.cx, + sh.is_async, + kd(arena_of(nodes@), arena_of(nodes@).nodes[sh.m as int].children[0]) + == NodeKind::BindingIdentifier, + ), + arrow_bcx( + g.cx, + sh.is_async, + kd(arena_of(nodes@), arena_of(nodes@).nodes[sh.m as int].children[1]) + == NodeKind::FunctionBody, + ), + ), + 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_words3(); + 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() != 2 { + return None; + } + let p = match kid_of(nodes, m, 0) { + Some(c) => c, + None => { + return None; + }, + }; + let b = 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 x1 = x.saturating_add(1); + if sh.is_async + && (!word_at(src, nodes, toks, gaps, x, &[0x61, 0x73, 0x79, 0x6E, 0x63], Ghost(GW { g, w: "async"@ })) + || !no_nl_at(src, nodes, toks, gaps, x1, Ghost(g))) { + return None; + } + let s: usize = if sh.is_async { x1 } else { x }; + let kp = kind_at(nodes, p); + if !matches!(kp, NodeKind::BindingIdentifier | NodeKind::FormalParameters) { + return None; + } + let (lo_p, mp, _) = 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; + }, + }; + let kb = kind_at(nodes, b); + let braced = matches!(kb, NodeKind::FunctionBody); + let mp1 = mp.saturating_add(1); + if lo_p != s || lo_b != mp1 || !punct2_at(src, nodes, toks, gaps, mp, Ghost(g)) || !no_nl_at(src, nodes, toks, gaps, mp, Ghost(g)) { + return None; + } + if !braced { + let (_, _, lv_b) = match done_at(done, b) { + Some(r) => r, + None => { + return None; + }, + }; + if lv_b < 0 || brace_at(src, nodes, toks, gaps, mp1, Ghost(g)) { + 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(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![p as int, b 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); + assert(mp < toks.len()); + assert(mp1 as int == mp as int + 1); + 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, p as int) == kp); + assert(kd(a, b as int) == kb); + assert(cx_of(done.cxs@[p as int]) == arrow_pcx(g.cx, sh.is_async, kd(a, p as int) == NodeKind::BindingIdentifier)); + assert(cx_of(done.cxs@[b as int]) == arrow_bcx(g.cx, sh.is_async, kd(a, b as int) == NodeKind::FunctionBody)); + if !braced { + assert(lvl(a, b as int) >= 0); + assert(is_assign_expr(a, b as int)); + } + lemma_arrow_done(a, g.ts, g.cx, m as int, p as int, b as int, sh.is_async, x as int, mp as int, j as int); + } + Some((x, j, 0)) +} + +/// Token `x` is `=>`. +fn punct2_at( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + x: usize, + 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, "=>"@), +{ + let _ = (nodes.len(), gaps.len()); + 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]); + reveal_strlit("=>"); + assert("=>"@ =~= seq!['=', '>']); + } + let r = matches!(t.kind, TokKind::Punctuator) && text_is(src, t, &[0x3D, 0x3E]); + proof { + if r { + assert(g.ts.toks[x as int].text =~= src@.subrange(t.start as int, t.end as int)); + assert(cps("=>"@) =~= seq![0x3Du32, 0x3E]); + } + } + r +} + +/// Token `x` is `{`. (True when it is; a missing token is not.) +fn brace_at( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + x: usize, + Ghost(g): Ghost, +) -> (r: bool) + requires + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + !r ==> !is_p(g.ts, x as int, "{"@), +{ + proof { + lemma_brace_text(); + } + !punct_at_not(src, nodes, toks, gaps, x, Ghost(g)) +} + +/// Token `x` is not the punctuator `{` (it may not exist). +fn punct_at_not( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + x: usize, + Ghost(g): Ghost, +) -> (r: bool) + requires + env_ok(src@, nodes@, toks@, gaps@, g.ts), + ensures + r ==> !is_p(g.ts, x as int, "{"@), +{ + let _ = (nodes.len(), gaps.len()); + proof { + lemma_brace_text(); + 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::Punctuator) && text_is1(src, t, 0x7B)); + proof { + if r { + assert(g.ts.toks[x as int].text =~= src@.subrange(t.start as int, t.end as int)); + } + } + r +} + +/// The two children of node `m` are checked in `c0` and `c1`. +fn kids_cx2(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_kids2(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; + }, + }; + if nd.children.len() != 2 { + 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 (k0, k1) = match (cxs.get(a0), cxs.get(a1)) { + (Some(x), Some(y)) => (x, y), + _ => { + return false; + }, + }; + cx_eq(k0, c0) && cx_eq(k1, c1) +} + +/// The contexts of the parts of an arrow function, as carried (`arrow_pcx`, `arrow_bcx`). +fn arrow_cx5(cx: &Cx5, is_async: bool, ident: bool, braced: bool) -> (r: (Cx5, Cx5)) + ensures + cx_of(r.0) == arrow_pcx(cx_of(*cx), is_async, ident), + cx_of(r.1) == arrow_bcx(cx_of(*cx), is_async, braced), +{ + let pc = if !is_async { + *cx + } else if ident { + Cx5 { y: cx.y, a: true, i: true, r: false, d: false } + } else { + Cx5 { y: false, a: true, i: true, r: false, d: false } + }; + let bc = if braced { + Cx5 { y: false, a: is_async, i: true, r: true, d: false } + } else { + Cx5 { y: false, a: is_async, i: cx.i, r: false, d: false } + }; + (pc, bc) +} + +/// §15.5 `yield`, `yield AssignmentExpression`, `yield * AssignmentExpression`. +#[verifier::spinoff_prover] +proof fn lemma_yield_done( + a: AstV, + ts: Stream, + cx: Ctx, + m: int, + c: int, + x: int, + delegate: bool, + bare: bool, + j: int, +) + requires + toks_wf(ts), + cx.yield_, + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::YieldExpression, + a.nodes[m].children =~= seq![c], + a.nodes[m].op == (if delegate { + Op::YieldDelegate + } else { + Op::YieldPlain + }), + !(delegate && bare), + is_kw(ts, x, "yield"@), + if bare { + kd(a, c) == NodeKind::Absent && j == x + 1 && d(a, ts, c, cx, x + 1, x + 1) + } else if delegate { + !nl(ts, x + 1) && is_p(ts, x + 1, "*"@) && is_assign_expr(a, c) && node_done( + a, + ts, + c, + cx, + x + 2, + j, + ) + } else { + !nl(ts, x + 1) && is_assign_expr(a, c) && 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) == 0, +{ + assert(span_ok(a, ts, m, x, j)); + assert(a.nodes[m].children[0] == c); + if bare { + assert(dc(a, ts, m, c, cx, x + 1, x + 1)); + } else if delegate { + assert(dc(a, ts, m, c, cx, x + 2, j)); + } else { + assert(dc(a, ts, m, c, cx, x + 1, j)); + } + assert(d_expr(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §15.5 `YieldExpression`. +#[verifier::spinoff_prover] +fn check_yield( + 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_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; + }, + }; + let cxm = match done.cxs.get(m) { + Some(c) => c, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::YieldExpression) || nd.children.len() != 1 || !cxm.y { + return None; + } + let delegate = matches!(nd.op, Op::YieldDelegate); + if !(delegate || matches!(nd.op, Op::YieldPlain)) { + 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; + }, + }; + if !word_at(src, nodes, toks, gaps, x, &[0x79, 0x69, 0x65, 0x6C, 0x64], Ghost(GW { g, w: "yield"@ })) { + return None; + } + let x1 = x.saturating_add(1); + let bare = matches!(kind_at(nodes, c), NodeKind::Absent); + if bare && delegate { + return None; + } + let j: usize = if bare { + if !check_absent(src, nodes, toks, gaps, c, x1, Ghost(g)) { + return None; + } + x1 + } else { + let lo_expect: usize = if delegate { x.saturating_add(2) } else { x1 }; + let (lo_c, hi_c, lv_c) = match done_at(done, c) { + Some(r) => r, + None => { + return None; + }, + }; + if lv_c < 0 || lo_c != lo_expect || !no_nl_at(src, nodes, toks, gaps, x1, Ghost(g)) { + return None; + } + if delegate && !punct_at(src, nodes, toks, gaps, x1, 0x2A, Ghost(GW { g, w: "*"@ })) { + return None; + } + hi_c + }; + 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); + assert(cxm == &done.cxs@[m as int]); + assert(g.cx.yield_); + if !bare { + 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); + assert(is_assign_expr(a, c as int)); + if delegate { + assert(x.saturating_add(2) as int == x as int + 2); + } + } + lemma_yield_done(a, g.ts, g.cx, m as int, c as int, x as int, delegate, bare, j as int); + } + Some((x, j, 0)) +} + +/// §15.8 `AwaitExpression : await UnaryExpression`. +#[verifier::spinoff_prover] +proof fn lemma_await_done(a: AstV, ts: Stream, cx: Ctx, m: int, c: int, x: int, j: int) + requires + toks_wf(ts), + cx.await_, + 0 <= m < a.nodes.len(), + 0 <= c < m, + a.nodes[m].kind == NodeKind::AwaitExpression, + a.nodes[m].children =~= seq![c], + is_kw(ts, x, "await"@), + lvl(a, c) >= 12, + 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) == 12, +{ + 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_expr(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// §15.8 `AwaitExpression`. +#[verifier::spinoff_prover] +fn check_await( + 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_words4(); + lemma_env_toks_wf(src@, nodes@, toks@, gaps@, g.ts); + } + let nd = match nodes.get(m) { + Some(x) => x, + None => { + return None; + }, + }; + let cxm = match done.cxs.get(m) { + Some(c) => c, + None => { + return None; + }, + }; + if !matches!(nd.kind, NodeKind::AwaitExpression) || nd.children.len() != 1 || !cxm.a { + 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; + }, + }; + if !word_at(src, nodes, toks, gaps, x, &[0x61, 0x77, 0x61, 0x69, 0x74], Ghost(GW { g, w: "await"@ })) + || lv_c < 12 || 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); + assert(cxm == &done.cxs@[m as int]); + assert(g.cx.await_); + 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) >= 12); + lemma_await_done(a, g.ts, g.cx, m as int, c as int, x as int, j as int); + } + Some((x, j, 12)) +} + +/// The words `yield` and `await`, as code points. +proof fn lemma_words4() + ensures + cps("yield"@) =~= seq![0x79u32, 0x69, 0x65, 0x6C, 0x64], + cps("await"@) =~= seq![0x61u32, 0x77, 0x61, 0x69, 0x74], +{ + reveal_strlit("yield"); + reveal_strlit("await"); + assert("yield"@ =~= seq!['y', 'i', 'e', 'l', 'd']); + assert("await"@ =~= seq!['a', 'w', 'a', 'i', 't']); +} + +/// §15.1 `FunctionRestParameter : ... BindingIdentifier`. +#[verifier::spinoff_prover] +proof fn lemma_rest_param_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::FunctionRestParameter, + a.nodes[m].children =~= seq![c], + is_p(ts, x, "..."@), + 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_func(a, ts, m, cx, x, j)); + lemma_ed_of(a, ts, m, x, j); +} + +/// Token `x` is `...`. +fn ellipsis_at( + src: &[u32], + nodes: &[Node], + toks: &[DTok], + gaps: &[Ins], + x: usize, + 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, "..."@), +{ + let _ = (nodes.len(), gaps.len()); + 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]); + reveal_strlit("..."); + assert("..."@ =~= seq!['.', '.', '.']); + } + let r = matches!(t.kind, TokKind::Punctuator) && text_is(src, t, &[0x2E, 0x2E, 0x2E]); + proof { + if r { + assert(g.ts.toks[x as int].text =~= src@.subrange(t.start as int, t.end as int)); + assert(cps("..."@) =~= seq![0x2Eu32, 0x2E, 0x2E]); + } + } + r +} + +/// §15.1 `FunctionRestParameter`. +#[verifier::spinoff_prover] +fn check_rest_param( + 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::FunctionRestParameter) || 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 !ellipsis_at(src, nodes, toks, gaps, x, Ghost(g)) + || !matches!(kind_at(nodes, c), NodeKind::BindingIdentifier) + || 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); + lemma_rest_param_done(a, g.ts, g.cx, m as int, c as int, x as int, j as int); + } + Some((x, j, -100)) +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/check.rs b/crates/clazel-js-syntax/src/check.rs new file mode 100644 index 000000000..e159c25c7 --- /dev/null +++ b/crates/clazel-js-syntax/src/check.rs @@ -0,0 +1,390 @@ +//! The arena check: the part of the parser that is proven. +//! +//! [`finish`] takes the nodes the recursive-descent parser built (after the +//! nodes that the cover grammars left behind are dropped) and returns an +//! [`Ast`] only if it is well formed ([`ast_wf`]), every span is inside the +//! source text and every child's span inside its parent's ([`spans_wf`]), and its +//! height is at most [`MAX_DEPTH`]: the structural half of +//! [`crate::contract::parse_ensures`] for an `Ok`, whatever the parser did to +//! build the nodes. The check is linear: one pass for the shape and the spans, +//! one for the parents (no garbage), one for the heights. +//! +//! What is *not* proven: that the arena is a derivation of a token stream of +//! the source text by the grammar, with no early error, and that an `Err` means +//! there is no parse (the rest of `parse_ensures`). Those are tested. + +use crate::api::ParseError; +use crate::ast::*; +use crate::spec::*; +use vstd::prelude::*; + +verus! { + +/// The arena of `nodes` with the root `root`, as the specification reads it +/// (the view of an [`Ast`]). +pub open spec fn arena(nodes: Seq, root: int) -> AstV { + AstV { nodes: nodes.map_values(|n: Node| n@), root: root } +} + +/// Node `m` of `a` is well formed in an arena whose root is `a.root`, in a source +/// text of length `len`: its children are before it, only the root is a `Script` or +/// `Module`, its span is inside the source text, and its children's spans inside it. +pub open spec fn node_ok(a: AstV, m: int, len: int) -> bool { + let nd = a.nodes[m]; + (forall|k: int| 0 <= k < nd.children.len() ==> 0 <= #[trigger] nd.children[k] < m) && (( + nd.kind == NodeKind::Script || nd.kind == NodeKind::Module) <==> m == a.root) && 0 <= nd.start + <= nd.end <= len && (forall|k: int| + 0 <= k < nd.children.len() ==> nd.start <= a.nodes[#[trigger] nd.children[k]].start + && a.nodes[nd.children[k]].end <= nd.end) +} + +/// What [`finish`] promises of an `Ok`: the structural half of +/// [`crate::contract::parse_ensures`]. +pub open spec fn finish_ensures(src_len: int, r: Result) -> bool { + match r { + Ok(ast) => ast_wf(ast@) && spans_wf(ast@, src_len) && height(ast@, ast@.root) + <= MAX_DEPTH as int, + Err(_) => true, + } +} + +/// `height(a, c) <= b` for every child of `n` in `ks[k..]`, so is `max_height`. +proof fn lemma_max_height_le(a: AstV, n: int, ks: Seq, k: int, b: int) + requires + b >= 0, + forall|j: int| k <= j < ks.len() ==> (0 <= #[trigger] ks[j] < n ==> height(a, ks[j]) <= b), + ensures + max_height(a, n, ks, k) <= b, + decreases ks.len() - k, +{ + if k < 0 || k >= ks.len() { + } else { + lemma_max_height_le(a, n, ks, k + 1, b); + } +} + +/// `height(a, n) <= b + 1` when every child of `n` has height at most `b`. +proof fn lemma_height_le(a: AstV, n: int, b: int) + requires + b >= 0, + 0 <= n < a.nodes.len(), + forall|j: int| + 0 <= j < a.nodes[n].children.len() ==> (0 <= #[trigger] a.nodes[n].children[j] < n + ==> height(a, a.nodes[n].children[j]) <= b), + ensures + height(a, n) <= b + 1, +{ + lemma_max_height_le(a, n, a.nodes[n].children, 0, b); +} + +/// Checks the arena `nodes` (children before parents, the root last) of a +/// source text of `src_len` code points, and returns it as an [`Ast`] if it is +/// well formed, inside the source text and at most [`MAX_DEPTH`] high. +/// +/// Proven: [`finish_ensures`] of the result. +pub fn finish(nodes: Vec, src_len: usize) -> (r: Result) + ensures + finish_ensures(src_len as int, r), +{ + let n = nodes.len(); + if n == 0 || n > 0xFFFF_FFFFusize { + return Err(ParseError::Syntax); + } + let root = n.saturating_sub(1); + let ghost a = arena(nodes@, root as int); + assert(a.nodes.len() == n); + // Pass 1: the shape of every node, and its span. + let mut m: usize = 0; + while m < n + invariant + n == nodes.len(), + root + 1 == n, + n <= 0xFFFF_FFFF, + a == arena(nodes@, root as int), + m <= n, + forall|j: int| 0 <= j < m ==> node_ok(a, j, src_len as int), + decreases n - m, + { + let nd = match nodes.as_slice().get(m) { + Some(x) => x, + None => { + return Err(ParseError::Syntax); + }, + }; + let is_root_kind = matches!(nd.kind, NodeKind::Script | NodeKind::Module); + if is_root_kind != (m == root) { + return Err(ParseError::Syntax); + } + if !(nd.start <= nd.end && nd.end <= src_len) { + return Err(ParseError::Syntax); + } + let mut k: usize = 0; + while k < nd.children.len() + invariant + n == nodes.len(), + m < n, + nd == nodes@[m as int], + a == arena(nodes@, root as int), + k <= nd.children.len(), + forall|j: int| + 0 <= j < k ==> (0 <= #[trigger] nd.children@[j] as int) && (nd.children@[j] as int) + < m && nd.start as int <= nodes@[nd.children@[j] as int].start as int + && nodes@[nd.children@[j] as int].end as int <= nd.end as int, + decreases nd.children.len() - k, + { + let c = match nd.children.as_slice().get(k) { + Some(x) => *x, + None => { + return Err(ParseError::Syntax); + }, + }; + let cu = c as usize; + if cu >= m { + return Err(ParseError::Syntax); + } + let cn = match nodes.as_slice().get(cu) { + Some(x) => x, + None => { + return Err(ParseError::Syntax); + }, + }; + if !(nd.start <= cn.start && cn.end <= nd.end) { + return Err(ParseError::Syntax); + } + k = k.saturating_add(1); + } + proof { + assert(node_ok(a, m as int, src_len as int)) by { + let ndv = a.nodes[m as int]; + assert(ndv.children.len() == nd.children.len()); + assert forall|k2: int| 0 <= k2 < ndv.children.len() implies 0 <= #[trigger] ndv.children[k2] + < m by { + assert(ndv.children[k2] == nd.children@[k2] as int); + } + assert forall|k2: int| + 0 <= k2 < ndv.children.len() implies ndv.start <= a.nodes[#[trigger] ndv.children[k2]].start + && a.nodes[ndv.children[k2]].end <= ndv.end by { + assert(ndv.children[k2] == nd.children@[k2] as int); + let c2 = nd.children@[k2] as int; + assert(a.nodes[c2] == nodes@[c2]@); + } + } + } + m = m.saturating_add(1); + } + // Pass 2: every node but the root is a child of some node. + let mut marks: Vec = Vec::new(); + let mut i: usize = 0; + while i < n + invariant + i <= n, + marks.len() == i, + forall|q: int| 0 <= q < i ==> !#[trigger] marks@[q], + decreases n - i, + { + marks.push(false); + i = i.saturating_add(1); + } + let mut p: usize = 0; + while p < n + invariant + n == nodes.len(), + root + 1 == n, + a == arena(nodes@, root as int), + marks.len() == n, + p <= n, + forall|j: int| 0 <= j < n ==> node_ok(a, j, src_len as int), + forall|q: int| 0 <= q < n && #[trigger] marks@[q] ==> has_parent(a, q), + decreases n - p, + { + let nd = match nodes.as_slice().get(p) { + Some(x) => x, + None => { + return Err(ParseError::Syntax); + }, + }; + let mut k: usize = 0; + while k < nd.children.len() + invariant + n == nodes.len(), + p < n, + nd == nodes@[p as int], + a == arena(nodes@, root as int), + marks.len() == n, + k <= nd.children.len(), + forall|j: int| 0 <= j < n ==> node_ok(a, j, src_len as int), + forall|q: int| 0 <= q < n && #[trigger] marks@[q] ==> has_parent(a, q), + decreases nd.children.len() - k, + { + let c = match nd.children.as_slice().get(k) { + Some(x) => *x, + None => { + return Err(ParseError::Syntax); + }, + }; + let cu = c as usize; + if cu >= n { + return Err(ParseError::Syntax); + } + proof { + let ndv = a.nodes[p as int]; + assert(ndv.children[k as int] == nd.children@[k as int] as int); + assert(node_ok(a, p as int, src_len as int)); + assert(cu < p) by { + assert(0 <= ndv.children[k as int] < p as int); + } + assert(has_parent(a, cu as int)) by { + assert(a.nodes[p as int].children[k as int] == cu as int); + } + } + marks.set(cu, true); + k = k.saturating_add(1); + } + p = p.saturating_add(1); + } + let mut q: usize = 0; + while q < root + invariant + root + 1 == n, + marks.len() == n, + q <= root, + forall|j: int| 0 <= j < n && #[trigger] marks@[j] ==> has_parent(a, j), + forall|j: int| 0 <= j < q ==> has_parent(a, j), + decreases root - q, + { + match marks.as_slice().get(q) { + Some(true) => {}, + _ => { + return Err(ParseError::Syntax); + }, + } + q = q.saturating_add(1); + } + // Pass 3: the heights. A height past the limit is stored as `cap`. + let cap: usize = MAX_DEPTH.saturating_add(1); + let mut h: Vec = Vec::new(); + let mut m: usize = 0; + while m < n + invariant + n == nodes.len(), + root + 1 == n, + a == arena(nodes@, root as int), + m <= n, + h.len() == m, + cap == MAX_DEPTH + 1, + forall|j: int| 0 <= j < n ==> node_ok(a, j, src_len as int), + forall|j: int| + 0 <= j < m ==> 1 <= #[trigger] h@[j] <= cap && (h@[j] <= MAX_DEPTH ==> height(a, j) + <= h@[j] as int), + decreases n - m, + { + let nd = match nodes.as_slice().get(m) { + Some(x) => x, + None => { + return Err(ParseError::Syntax); + }, + }; + let mut best: usize = 0; + let mut k: usize = 0; + while k < nd.children.len() + invariant + n == nodes.len(), + m < n, + nd == nodes@[m as int], + a == arena(nodes@, root as int), + h.len() == m, + best <= cap, + k <= nd.children.len(), + forall|j: int| 0 <= j < n ==> node_ok(a, j, src_len as int), + forall|j: int| 0 <= j < k ==> (nd.children@[j] as int) < m, + forall|j: int| + 0 <= j < k ==> #[trigger] h@[nd.children@[j] as int] <= best, + forall|j: int| + 0 <= j < m ==> 1 <= #[trigger] h@[j] <= cap && (h@[j] <= MAX_DEPTH ==> height(a, j) + <= h@[j] as int), + decreases nd.children.len() - k, + { + let c = match nd.children.as_slice().get(k) { + Some(x) => *x, + None => { + return Err(ParseError::Syntax); + }, + }; + let cu = c as usize; + proof { + let ndv = a.nodes[m as int]; + assert(ndv.children[k as int] == nd.children@[k as int] as int); + assert(node_ok(a, m as int, src_len as int)); + } + let hc = match h.as_slice().get(cu) { + Some(x) => *x, + None => { + return Err(ParseError::Syntax); + }, + }; + if hc > best { + best = hc; + } + k = k.saturating_add(1); + } + let hm = if best >= MAX_DEPTH { + cap + } else { + best.saturating_add(1) + }; + proof { + if hm <= MAX_DEPTH { + let ndv = a.nodes[m as int]; + assert(node_ok(a, m as int, src_len as int)); + assert forall|j: int| + 0 <= j < ndv.children.len() implies 0 <= #[trigger] ndv.children[j] < m as int + && height(a, ndv.children[j]) <= best as int by { + assert(ndv.children[j] == nd.children@[j] as int); + let cc = ndv.children[j]; + assert(h@[cc] <= best); + assert(h@[cc] <= MAX_DEPTH); + assert(height(a, cc) <= h@[cc] as int); + } + lemma_height_le(a, m as int, best as int); + } + } + h.push(hm); + m = m.saturating_add(1); + } + let hr = match h.as_slice().get(root) { + Some(x) => *x, + None => { + return Err(ParseError::Syntax); + }, + }; + if hr > MAX_DEPTH { + return Err(ParseError::TooDeep); + } + let ast = Ast { nodes, root: root as u32 }; + proof { + assert(ast@ == a); + assert(ast_wf(a)) by { + assert forall|j: int| 0 <= j < a.nodes.len() implies { + let nd = #[trigger] a.nodes[j]; + (forall|k: int| 0 <= k < nd.children.len() ==> 0 <= #[trigger] nd.children[k] < j) + && ((nd.kind == NodeKind::Script || nd.kind == NodeKind::Module) <==> j == a.root) + } by { + assert(node_ok(a, j, src_len as int)); + } + } + assert(spans_wf(a, src_len as int)) by { + assert forall|j: int| 0 <= j < a.nodes.len() implies { + let nd = #[trigger] a.nodes[j]; + 0 <= nd.start <= nd.end <= src_len as int && (forall|k: int| + 0 <= k < nd.children.len() ==> nd.start <= a.nodes[#[trigger] nd.children[k]].start + && a.nodes[nd.children[k]].end <= nd.end) + } by { + assert(node_ok(a, j, src_len as int)); + } + } + assert(height(a, a.root) <= MAX_DEPTH as int); + } + Ok(ast) +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/cover.rs b/crates/clazel-js-syntax/src/cover.rs new file mode 100644 index 000000000..e92c65c0c --- /dev/null +++ b/crates/clazel-js-syntax/src/cover.rs @@ -0,0 +1,246 @@ +//! The cover grammars (ECMA-262 §13.2.9, §13.2.5.1, §13.15.5, §15.9), read as +//! the refined productions. The parser reads an `ObjectLiteral`, an +//! `ArrayLiteral` or a parenthesized list as an expression first; once the +//! `=` or `=>` that says what it is has been seen, these functions build the +//! pattern node for the same tokens (the standard: "the interpretation of +//! [the cover] is refined using the following grammar"). + +use crate::ast::{NodeKind as K, Op}; +use crate::parser::{Parser, R, syn}; + +impl<'a> Parser<'a> { + /// An absent optional symbol at `pos`. + fn absent_at(&mut self, pos: usize) -> u32 { + self.leaf(K::Absent, pos, pos) + } + + /// The children of `id`. + fn kids(&self, id: u32) -> Vec { + self.node(id) + .map(|n| n.children.clone()) + .unwrap_or_default() + } + + /// `DestructuringAssignmentTarget`, and the target of `=`: a + /// `LeftHandSideExpression` that is not an `ObjectLiteral` or an + /// `ArrayLiteral`, or an assignment pattern read from one of them. + pub fn as_assign_target(&mut self, id: u32) -> R { + match self.kind(id) { + K::ObjectLiteral => self.obj_to_assign(id), + K::ArrayLiteral => self.arr_to_assign(id), + K::ObjectAssignmentPattern | K::ArrayAssignmentPattern => Ok(id), + _ => { + if self.lvl(id) >= 14 { + Ok(id) + } else { + syn() + } + } + } + } + + /// An `AssignmentElement`: a target with an optional initializer. + fn as_assign_element(&mut self, c: u32) -> R { + let (s, e) = (self.st(c), self.en(c)); + if self.kind(c) == K::AssignmentExpression && self.op(c) == Op::Assign { + let (t, init) = (self.child(c, 0), self.child(c, 1)); + Ok(self.mk(K::AssignmentElement, Op::None, false, s, e, vec![t, init])) + } else { + let t = self.as_assign_target(c)?; + let a = self.absent_at(e); + Ok(self.mk(K::AssignmentElement, Op::None, false, s, e, vec![t, a])) + } + } + + /// §13.15.5 `ObjectAssignmentPattern` read from an `ObjectLiteral`. + fn obj_to_assign(&mut self, id: u32) -> R { + let kids = self.kids(id); + let n = kids.len(); + let mut out = Vec::new(); + for (k, c) in kids.iter().copied().enumerate() { + let last = k.saturating_add(1) == n; + let (s, e) = (self.st(c), self.en(c)); + match (self.kind(c), self.op(c)) { + (K::PropertyDefinition, Op::Shorthand) => { + let ident = self.child(c, 0); + let a = self.absent_at(e); + out.push(self.mk( + K::AssignmentProperty, + Op::Shorthand, + false, + s, + e, + vec![ident, a], + )); + } + (K::CoverInitializedName, _) => { + let (ident, init) = (self.child(c, 0), self.child(c, 1)); + out.push(self.mk( + K::AssignmentProperty, + Op::Shorthand, + false, + s, + e, + vec![ident, init], + )); + } + (K::PropertyDefinition, Op::KeyValue) => { + let (name, v) = (self.child(c, 0), self.child(c, 1)); + let el = self.as_assign_element(v)?; + out.push(self.mk( + K::AssignmentProperty, + Op::KeyValue, + false, + s, + e, + vec![name, el], + )); + } + (K::PropertyDefinition, Op::Spread) => { + if !last || self.rest_comma.contains(&id) { + return syn(); + } + let x = self.child(c, 0); + if matches!(self.kind(x), K::ObjectLiteral | K::ArrayLiteral) { + return syn(); + } + let t = self.as_assign_target(x)?; + out.push(self.mk(K::AssignmentRestProperty, Op::None, false, s, e, vec![t])); + } + _ => return syn(), + } + } + let (s, e) = (self.st(id), self.en(id)); + Ok(self.mk(K::ObjectAssignmentPattern, Op::None, false, s, e, out)) + } + + /// §13.15.5 `ArrayAssignmentPattern` read from an `ArrayLiteral`. + fn arr_to_assign(&mut self, id: u32) -> R { + let kids = self.kids(id); + let n = kids.len(); + let mut out = Vec::new(); + for (k, c) in kids.iter().copied().enumerate() { + let last = k.saturating_add(1) == n; + let (s, e) = (self.st(c), self.en(c)); + match self.kind(c) { + K::Elision => out.push(c), + K::SpreadElement => { + if !last || self.rest_comma.contains(&id) { + return syn(); + } + let t = self.as_assign_target(self.child(c, 0))?; + out.push(self.mk(K::AssignmentRestElement, Op::None, false, s, e, vec![t])); + } + _ => out.push(self.as_assign_element(c)?), + } + } + let (s, e) = (self.st(id), self.en(id)); + Ok(self.mk(K::ArrayAssignmentPattern, Op::None, false, s, e, out)) + } + + /// A `BindingIdentifier` or `BindingPattern` read from an expression or an + /// assignment pattern (the parameters of an arrow function). + pub fn as_binding_target(&mut self, id: u32) -> R { + let (s, e) = (self.st(id), self.en(id)); + match self.kind(id) { + K::IdentifierReference => Ok(self.leaf(K::BindingIdentifier, s, e)), + K::BindingIdentifier | K::ObjectBindingPattern | K::ArrayBindingPattern => Ok(id), + K::ObjectLiteral | K::ObjectAssignmentPattern => self.obj_to_binding(id), + K::ArrayLiteral | K::ArrayAssignmentPattern => self.arr_to_binding(id), + _ => syn(), + } + } + + /// A `BindingElement` read from an expression or an assignment element. + pub fn as_binding_element(&mut self, id: u32) -> R { + let (s, e) = (self.st(id), self.en(id)); + match (self.kind(id), self.op(id)) { + (K::AssignmentExpression, Op::Assign) | (K::AssignmentElement, _) => { + let (t, init) = (self.child(id, 0), self.child(id, 1)); + let bt = self.as_binding_target(t)?; + Ok(self.mk(K::BindingElement, Op::None, false, s, e, vec![bt, init])) + } + _ => { + let bt = self.as_binding_target(id)?; + let a = self.absent_at(e); + Ok(self.mk(K::BindingElement, Op::None, false, s, e, vec![bt, a])) + } + } + } + + /// §14.3.3 `ObjectBindingPattern`. + fn obj_to_binding(&mut self, id: u32) -> R { + let kids = self.kids(id); + let n = kids.len(); + let mut out = Vec::new(); + for (k, c) in kids.iter().copied().enumerate() { + let last = k.saturating_add(1) == n; + let (s, e) = (self.st(c), self.en(c)); + match (self.kind(c), self.op(c)) { + (K::PropertyDefinition, Op::Shorthand) => { + let ident = self.child(c, 0); + let bi = self.as_binding_target(ident)?; + let a = self.absent_at(e); + let el = self.mk(K::BindingElement, Op::None, false, s, e, vec![bi, a]); + out.push(self.mk(K::BindingProperty, Op::Shorthand, false, s, e, vec![el])); + } + (K::CoverInitializedName, _) | (K::AssignmentProperty, Op::Shorthand) => { + let (ident, init) = (self.child(c, 0), self.child(c, 1)); + let bi = self.as_binding_target(ident)?; + let el = self.mk(K::BindingElement, Op::None, false, s, e, vec![bi, init]); + out.push(self.mk(K::BindingProperty, Op::Shorthand, false, s, e, vec![el])); + } + (K::PropertyDefinition, Op::KeyValue) | (K::AssignmentProperty, Op::KeyValue) => { + let (name, v) = (self.child(c, 0), self.child(c, 1)); + let el = self.as_binding_element(v)?; + out.push(self.mk( + K::BindingProperty, + Op::KeyValue, + false, + s, + e, + vec![name, el], + )); + } + (K::PropertyDefinition, Op::Spread) | (K::AssignmentRestProperty, _) => { + if !last || self.rest_comma.contains(&id) { + return syn(); + } + let x = self.child(c, 0); + if !matches!(self.kind(x), K::IdentifierReference | K::BindingIdentifier) { + return syn(); + } + let bi = self.as_binding_target(x)?; + out.push(self.mk(K::BindingRestProperty, Op::None, false, s, e, vec![bi])); + } + _ => return syn(), + } + } + let (s, e) = (self.st(id), self.en(id)); + Ok(self.mk(K::ObjectBindingPattern, Op::None, false, s, e, out)) + } + + /// §14.3.3 `ArrayBindingPattern`. + fn arr_to_binding(&mut self, id: u32) -> R { + let kids = self.kids(id); + let n = kids.len(); + let mut out = Vec::new(); + for (k, c) in kids.iter().copied().enumerate() { + let last = k.saturating_add(1) == n; + let (s, e) = (self.st(c), self.en(c)); + match self.kind(c) { + K::Elision => out.push(c), + K::SpreadElement | K::AssignmentRestElement => { + if !last || self.rest_comma.contains(&id) { + return syn(); + } + let t = self.as_binding_target(self.child(c, 0))?; + out.push(self.mk(K::BindingRestElement, Op::None, false, s, e, vec![t])); + } + _ => out.push(self.as_binding_element(c)?), + } + } + let (s, e) = (self.st(id), self.en(id)); + Ok(self.mk(K::ArrayBindingPattern, Op::None, false, s, e, out)) + } +} diff --git a/crates/clazel-js-syntax/src/derive.rs b/crates/clazel-js-syntax/src/derive.rs new file mode 100644 index 000000000..3cb3a81fc --- /dev/null +++ b/crates/clazel-js-syntax/src/derive.rs @@ -0,0 +1,388 @@ +//! Proofs that what the parser builds derives from the tokens by the +//! grammar of [`crate::spec`]: the first pieces of "an `Ok` arena satisfies +//! [`is_valid_parse`]" (item 7.3, second half). +//! +//! Two kinds of statement are proven here, neither about the whole parser: +//! +//! - **Rule lemmas** (`lemma_*`): for one production, the shape the parser builds +//! (the node's kind, children and span, and the tokens it consumed) is +//! derived by [`d`]. They are the steps of an induction over the arena that +//! the parser's code would discharge production by production. +//! - **Checked leaves** ([`check_leaf`]): an executable check, on the source +//! text and the tokens as the parser read them, that a leaf node is derived +//! by [`d`] at a token. It is relative to the tokens: that they are what the +//! lexer's specification says ([`lexes_as`]) is the lexer's contract +//! (item 7.2), which the executable lexer does not meet yet. + +use crate::ast::*; +use crate::spec::*; +use vstd::prelude::*; + +verus! { + +/// A leaf node `n` (no children) of one of the kinds below, at token `i`, whose +/// span is the token's and whose token has the kind the production wants, is +/// derived: `d(n, i, i + 1)`. §12.7 `IdentifierName`, `PrivateIdentifier`, +/// §13.2.3 `NumericLiteral`, `StringLiteral`, §13.2.7 +/// `RegularExpressionLiteral`, §13.2.8 `TemplateElement`. +pub proof fn lemma_leaf_literal(a: AstV, ts: Stream, n: int, cx: Ctx, i: int) + requires + 0 <= n < a.nodes.len(), + 0 <= i < tn(ts), + a.nodes[n].children.len() == 0, + a.nodes[n].start == ts.toks[i].start, + a.nodes[n].end == ts.toks[i].end, + ts.toks[i].start < ts.toks[i].end, + match a.nodes[n].kind { + NodeKind::IdentifierName => ts.toks[i].kind == TokKind::IdentifierName, + NodeKind::PrivateIdentifier => ts.toks[i].kind == TokKind::PrivateIdentifier, + NodeKind::NumericLiteral => ts.toks[i].kind == TokKind::NumericLiteral, + NodeKind::StringLiteral => ts.toks[i].kind == TokKind::StringLiteral, + NodeKind::RegularExpressionLiteral => ts.toks[i].kind + == TokKind::RegularExpressionLiteral, + NodeKind::TemplateElement => ts.toks[i].kind is Template, + _ => false, + }, + ensures + d(a, ts, n, cx, i, i + 1), +{ + assert(span_ok(a, ts, n, i, i + 1)); + assert(d_expr_lhs(a, ts, n, cx, i, i + 1)); + assert(d_expr(a, ts, n, cx, i, i + 1)); +} + +/// §13.6 to §13.13 a binary operator node `n` (kind `k`, operator `op`) with +/// children `l` and `r`: `l` derives `ts[i..m)`, the operator is the token `m`, +/// `r` derives `ts[m + 1..j)`, the operands bind tightly enough +/// ([`binary_operands_ok`]), and `in` only where `[+In]`. Then `n` derives +/// `ts[i..j)`. +pub proof fn lemma_binary_derives( + a: AstV, + ts: Stream, + n: int, + cx: Ctx, + i: int, + m: int, + j: int, + l: int, + r: int, +) + requires + 0 <= n < a.nodes.len(), + 0 <= i < m, + m < j <= tn(ts), + a.nodes[n].children =~= seq![l, r], + 0 <= l < n, + 0 <= r < n, + bin_op_ok(a.nodes[n].kind, a.nodes[n].op), + a.nodes[n].op == Op::In ==> cx.in_, + binary_operands_ok(a, a.nodes[n].kind, a.nodes[n].op, l, r), + ed(a, ts, l, i) == m, + d(a, ts, l, cx, i, m), + is_op_tok(ts, m, a.nodes[n].op), + d(a, ts, r, cx, m + 1, j), + span_ok(a, ts, n, i, j), + ensures + d(a, ts, n, cx, i, j), +{ + let k = a.nodes[n].kind; + assert(a.nodes[n].children[0] == l); + assert(dc(a, ts, n, l, cx, i, m)); + assert(dc(a, ts, n, r, cx, m + 1, j)); + assert(d_expr(a, ts, n, cx, i, j)); +} + +/// The arena of `nodes` as the specification reads it (the root is irrelevant to [`d`]). +pub open spec fn nodes_arena(nodes: Seq) -> AstV { + AstV { nodes: nodes.map_values(|nd: Node| nd@), root: 0 } +} + +/// What the executable parser knows of a token that the leaf productions read: +/// its kind and its span (the rest of [`Tok`] is the lexer's). +pub struct ETok { + /// The lexer's kind. + pub kind: TokKind, + /// The index of the first code point. + pub start: usize, + /// One past the last code point. + pub end: usize, +} + +/// The executable token `e` is the specification's token `t`, as far as +/// [`ETok`] goes. +pub open spec fn etok_matches(e: ETok, t: Tok) -> bool { + e.kind == t.kind && e.start as int == t.start && e.end as int == t.end +} + +/// Checks that node `n` of `nodes` is a literal or name leaf derived by the +/// grammar at token `i` of `toks`: it has no children, its span is the token's, +/// and the token is of the kind of the production. If so, [`d`] says so, in +/// every context. `ts` is the specification's view of the tokens: that the +/// executable tokens are the specification's (`etok_matches`) is the +/// assumption, since the executable lexer does not yet meet the lexer's +/// contract (item 7.2). +pub fn check_leaf(nodes: &Vec, toks: &Vec, n: usize, i: usize, Ghost(ts): Ghost) -> (ok: bool) + requires + toks@.len() == tn(ts), + forall|k: int| 0 <= k < toks@.len() ==> etok_matches(toks@[k], #[trigger] ts.toks[k]), + ensures + ok ==> (0 <= n < nodes@.len() && 0 <= i < tn(ts) && forall|cx: Ctx| + #[trigger] d(nodes_arena(nodes@), ts, n as int, cx, i as int, i as int + 1)), +{ + let nd = match nodes.as_slice().get(n) { + Some(x) => x, + None => { + return false; + }, + }; + let t = match toks.as_slice().get(i) { + Some(x) => x, + None => { + return false; + }, + }; + if !nd.children.is_empty() || nd.start != t.start || nd.end != t.end || !(t.start < t.end) { + return false; + } + let kind_ok = match nd.kind { + NodeKind::IdentifierName => matches!(t.kind, TokKind::IdentifierName), + NodeKind::PrivateIdentifier => matches!(t.kind, TokKind::PrivateIdentifier), + NodeKind::NumericLiteral => matches!(t.kind, TokKind::NumericLiteral), + NodeKind::StringLiteral => matches!(t.kind, TokKind::StringLiteral), + NodeKind::RegularExpressionLiteral => matches!(t.kind, TokKind::RegularExpressionLiteral), + NodeKind::TemplateElement => matches!(t.kind, TokKind::Template(_)), + _ => false, + }; + if !kind_ok { + return false; + } + proof { + let a = nodes_arena(nodes@); + assert(a.nodes[n as int] == nodes@[n as int]@); + assert(etok_matches(toks@[i as int], ts.toks[i as int])); + assert forall|cx: Ctx| #[trigger] d(a, ts, n as int, cx, i as int, i as int + 1) by { + lemma_leaf_literal(a, ts, n as int, cx, i as int); + } + } + true +} + +/// §13.1 `IdentifierReference`, `BindingIdentifier`, `LabelIdentifier`: a leaf at +/// an identifier token ([`ident_tok`]). +pub proof fn lemma_leaf_identifier(a: AstV, ts: Stream, n: int, cx: Ctx, i: int) + requires + 0 <= n < a.nodes.len(), + 0 <= i < tn(ts), + a.nodes[n].children.len() == 0, + a.nodes[n].kind == NodeKind::IdentifierReference || a.nodes[n].kind + == NodeKind::BindingIdentifier || a.nodes[n].kind == NodeKind::LabelIdentifier, + a.nodes[n].start == ts.toks[i].start, + a.nodes[n].end == ts.toks[i].end, + ts.toks[i].start < ts.toks[i].end, + ident_tok(ts, i, cx), + ensures + d(a, ts, n, cx, i, i + 1), +{ + assert(span_ok(a, ts, n, i, i + 1)); + assert(d_expr_lhs(a, ts, n, cx, i, i + 1)); + assert(d_expr(a, ts, n, cx, i, i + 1)); +} + +/// §13.2.1, §13.2.3 `this`, `null`, `true`, `false`: a leaf at that keyword. +pub proof fn lemma_leaf_keyword(a: AstV, ts: Stream, n: int, cx: Ctx, i: int) + requires + 0 <= n < a.nodes.len(), + 0 <= i < tn(ts), + a.nodes[n].children.len() == 0, + a.nodes[n].start == ts.toks[i].start, + a.nodes[n].end == ts.toks[i].end, + ts.toks[i].start < ts.toks[i].end, + match a.nodes[n].kind { + NodeKind::ThisExpression => is_kw(ts, i, "this"@), + NodeKind::NullLiteral => is_kw(ts, i, "null"@), + NodeKind::BooleanLiteral => is_kw(ts, i, "true"@) || is_kw(ts, i, "false"@), + _ => false, + }, + ensures + d(a, ts, n, cx, i, i + 1), +{ + assert(span_ok(a, ts, n, i, i + 1)); + assert(d_expr_lhs(a, ts, n, cx, i, i + 1)); + assert(d_expr(a, ts, n, cx, i, i + 1)); +} + +/// §13.5 `UnaryExpression : delete | void | typeof | + | - | ~ | ! UnaryExpression`. +pub proof fn lemma_unary_derives(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int, c: int) + requires + 0 <= n < a.nodes.len(), + 0 <= i < j <= tn(ts), + a.nodes[n].kind == NodeKind::UnaryExpression, + a.nodes[n].children =~= seq![c], + 0 <= c < n, + is_unary_op(a.nodes[n].op), + is_op_tok(ts, i, a.nodes[n].op), + lvl(a, c) >= 12, + d(a, ts, c, cx, i + 1, j), + span_ok(a, ts, n, i, j), + ensures + d(a, ts, n, cx, i, j), +{ + assert(a.nodes[n].children[0] == c); + assert(dc(a, ts, n, c, cx, i + 1, j)); + assert(d_expr(a, ts, n, cx, i, j)); +} + +/// §13.14 `ConditionalExpression : ShortCircuitExpression ? AssignmentExpression : +/// AssignmentExpression`. +#[verifier::spinoff_prover] +pub proof fn lemma_conditional_derives( + a: AstV, + ts: Stream, + n: int, + cx: Ctx, + i: int, + m: int, + q: int, + j: int, + t: int, + c: int, + e: int, +) + requires + 0 <= n < a.nodes.len(), + 0 <= i < m, + m < q < j <= tn(ts), + a.nodes[n].kind == NodeKind::ConditionalExpression, + a.nodes[n].children =~= seq![t, c, e], + 0 <= t < n, + 0 <= c < n, + 0 <= e < n, + lvl(a, t) >= 1 || kd(a, t) == NodeKind::CoalesceExpression, + is_assign_expr(a, c), + is_assign_expr(a, e), + ed(a, ts, t, i) == m, + ed(a, ts, c, m + 1) == q, + d(a, ts, t, cx, i, m), + is_p(ts, m, "?"@), + d(a, ts, c, cx_in(cx, true), m + 1, q), + is_p(ts, q, ":"@), + d(a, ts, e, cx, q + 1, j), + span_ok(a, ts, n, i, j), + ensures + d(a, ts, n, cx, i, j), +{ + assert(a.nodes[n].children[0] == t); + assert(a.nodes[n].children[1] == c); + assert(a.nodes[n].children[2] == e); + assert(dc(a, ts, n, t, cx, i, m)); + assert(dc(a, ts, n, c, cx_in(cx, true), m + 1, q)); + assert(dc(a, ts, n, e, cx, q + 1, j)); + assert(d_expr(a, ts, n, cx, i, j)); +} + +/// §13.15 `AssignmentExpression : LeftHandSideExpression AssignmentOperator +/// AssignmentExpression` (and `=` with a destructuring target). +pub proof fn lemma_assignment_derives( + a: AstV, + ts: Stream, + n: int, + cx: Ctx, + i: int, + m: int, + j: int, + l: int, + r: int, +) + requires + 0 <= n < a.nodes.len(), + 0 <= i < m, + m < j <= tn(ts), + a.nodes[n].kind == NodeKind::AssignmentExpression, + a.nodes[n].children =~= seq![l, r], + 0 <= l < n, + 0 <= r < n, + is_assign_op(a.nodes[n].op), + if a.nodes[n].op == Op::Assign { + is_destructuring_target(a, l) + } else { + is_lhs_expr(a, l) + }, + is_assign_expr(a, r), + ed(a, ts, l, i) == m, + d(a, ts, l, cx, i, m), + is_op_tok(ts, m, a.nodes[n].op), + d(a, ts, r, cx, m + 1, j), + span_ok(a, ts, n, i, j), + ensures + d(a, ts, n, cx, i, j), +{ + assert(a.nodes[n].children[0] == l); + assert(dc(a, ts, n, l, cx, i, m)); + assert(dc(a, ts, n, r, cx, m + 1, j)); + assert(d_expr(a, ts, n, cx, i, j)); +} + +/// §13.2.9 `ParenthesizedExpression : ( Expression )`. +pub proof fn lemma_parenthesized_derives(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, m: int, c: int) + requires + 0 <= n < a.nodes.len(), + 0 <= i, + i + 1 < m, + m < tn(ts), + a.nodes[n].kind == NodeKind::ParenthesizedExpression, + a.nodes[n].children =~= seq![c], + 0 <= c < n, + lvl(a, c) >= -1, + ed(a, ts, c, i + 1) == m, + is_p(ts, i, "("@), + d(a, ts, c, cx_in(cx, true), i + 1, m), + is_p(ts, m, ")"@), + span_ok(a, ts, n, i, m + 1), + ensures + d(a, ts, n, cx, i, m + 1), +{ + assert(a.nodes[n].children[0] == c); + assert(dc(a, ts, n, c, cx_in(cx, true), i + 1, m)); + assert(d_expr_lhs(a, ts, n, cx, i, m + 1)); + assert(d_expr(a, ts, n, cx, i, m + 1)); +} + +/// §13.3.2 `MemberExpression : MemberExpression . IdentifierName`. +pub proof fn lemma_member_dot_derives( + a: AstV, + ts: Stream, + n: int, + cx: Ctx, + i: int, + m: int, + o: int, + p: int, +) + requires + 0 <= n < a.nodes.len(), + 0 <= i < m, + m + 2 <= tn(ts), + a.nodes[n].kind == NodeKind::MemberExpression, + a.nodes[n].op == Op::Dot, + a.nodes[n].children =~= seq![o, p], + 0 <= o < n, + 0 <= p < n, + is_member_or_call(a, o), + kd(a, p) == NodeKind::IdentifierName, + ed(a, ts, o, i) == m, + d(a, ts, o, cx, i, m), + is_p(ts, m, "."@), + d(a, ts, p, cx, m + 1, m + 2), + span_ok(a, ts, n, i, m + 2), + ensures + d(a, ts, n, cx, i, m + 2), +{ + assert(a.nodes[n].children[0] == o); + assert(a.nodes[n].children[1] == p); + assert(dc(a, ts, n, o, cx, i, m)); + assert(dc(a, ts, n, p, cx, m + 1, m + 2)); + assert(d_expr_lhs(a, ts, n, cx, i, m + 2)); + assert(d_expr(a, ts, n, cx, i, m + 2)); +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/early.rs b/crates/clazel-js-syntax/src/early.rs new file mode 100644 index 000000000..d7e788bbd --- /dev/null +++ b/crates/clazel-js-syntax/src/early.rs @@ -0,0 +1,1250 @@ +//! Early errors (ECMA-262 §16 and the rules it names), as a walk over the +//! arena with the environment of each node: the same rules as +//! [`crate::spec::early_local`] and [`crate::spec::child_env`], which this +//! mirrors. Not proven: tested (`tests/test262.rs`). +//! +//! Not checked, as in the spec: `IsValidRegularExpressionLiteral` of a regular +//! expression's contents (`clazel-js-regexp`, item 7.10, is not written, so +//! every regular expression literal is taken as valid), §16.2.1.1's rules on +//! duplicate and undeclared exports, and the rules for code a direct `eval` +//! processes. + +use crate::api::ParseError; +use crate::ast::{Ast, Node, NodeKind as K, Op}; +use crate::token::{lex, name_is, name_is_reserved_always, name_is_strict_reserved}; +use clazel_js_lexer::spec::Goal; +use std::collections::HashSet; + +/// What the early errors read of the position of a node (`spec::EarlyEnv`). +#[derive(Clone, Copy)] +struct Env { + strict: bool, + module: bool, + yield_: bool, + await_: bool, + in_loop: bool, + in_breakable: bool, + super_prop: bool, + super_call: bool, + new_target: bool, + no_arguments: bool, + no_yield_await: bool, + derived: bool, +} + +/// The parts of an [`Env`] that a function-like construct sets (the +/// arguments of `spec::env_fn`): `[Yield]`, `[Await]`, `super.x` allowed, +/// `super()` allowed, `new.target` allowed, `arguments` not allowed, and no +/// `yield` or `await` expression allowed. +#[derive(Clone, Copy)] +struct Fl { + y: bool, + aw: bool, + sp: bool, + sc: bool, + nt: bool, + na: bool, + nya: bool, +} + +/// A declared name, and whether it is a plain `FunctionDeclaration`. +struct Decl { + name: Vec, + plain_function: bool, +} + +/// The assignment target type of §8.6.4. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Target { + Simple, + WebCompat, + Invalid, +} + +struct Chk<'a> { + src: &'a [u32], + nodes: &'a [Node], + /// The labels in scope, and whether each labels an iteration statement. + labels: Vec<(Vec, bool)>, + /// The `#names` that the enclosing class bodies declare. + privates: Vec>, +} + +/// Rejects `ast` if it has an early error (the rules of `spec::early_local`). +pub fn check(src: &[u32], ast: &Ast, module: bool) -> Result<(), ParseError> { + let mut c = Chk { + src, + nodes: &ast.nodes, + labels: Vec::new(), + privates: Vec::new(), + }; + let root = ast.root as usize; + let strict = module || c.has_use_strict(c.ch(root)); + let env = Env { + strict, + module, + yield_: false, + await_: module, + in_loop: false, + in_breakable: false, + super_prop: false, + super_call: false, + new_target: false, + no_arguments: false, + no_yield_await: false, + derived: false, + }; + if c.walk(root, env) { + Ok(()) + } else { + Err(ParseError::Syntax) + } +} + +fn is_assignment_pattern(k: K) -> bool { + matches!(k, K::ObjectAssignmentPattern | K::ArrayAssignmentPattern) +} + +fn is_function_kind(k: K) -> bool { + matches!( + k, + K::FunctionDeclaration + | K::FunctionExpression + | K::GeneratorDeclaration + | K::GeneratorExpression + | K::AsyncFunctionDeclaration + | K::AsyncFunctionExpression + | K::AsyncGeneratorDeclaration + | K::AsyncGeneratorExpression + ) +} + +fn is_generator_kind(k: K) -> bool { + matches!( + k, + K::GeneratorDeclaration + | K::GeneratorExpression + | K::AsyncGeneratorDeclaration + | K::AsyncGeneratorExpression + ) +} + +fn is_async_kind(k: K) -> bool { + matches!( + k, + K::AsyncFunctionDeclaration + | K::AsyncFunctionExpression + | K::AsyncGeneratorDeclaration + | K::AsyncGeneratorExpression + ) +} + +fn is_function_expression_kind(k: K) -> bool { + matches!( + k, + K::FunctionExpression + | K::GeneratorExpression + | K::AsyncFunctionExpression + | K::AsyncGeneratorExpression + ) +} + +/// `s`, read as UTF-16 code units, has no lone surrogate (`spec::well_formed_utf16`). +fn well_formed_utf16(s: &[u32]) -> bool { + s.iter().enumerate().all(|(k, u)| { + let next = s.get(k.saturating_add(1)); + let prev = k.checked_sub(1).and_then(|p| s.get(p)); + (!(0xD800..=0xDBFF).contains(u) || next.is_some_and(|x| (0xDC00..=0xDFFF).contains(x))) + && (!(0xDC00..=0xDFFF).contains(u) + || prev.is_some_and(|x| (0xD800..=0xDBFF).contains(x))) + }) +} + +fn has_dup(ns: &[Vec]) -> bool { + let mut seen: HashSet<&Vec> = HashSet::new(); + ns.iter().any(|n| !seen.insert(n)) +} + +fn intersects(xs: &[Vec], ys: &[Vec]) -> bool { + if xs.is_empty() || ys.is_empty() { + return false; + } + let set: HashSet<&Vec> = ys.iter().collect(); + xs.iter().any(|x| set.contains(x)) +} + +impl<'a> Chk<'a> { + // ----- the arena, read totally ------------------------------------- + + fn node(&self, i: usize) -> Option<&'a Node> { + self.nodes.get(i) + } + + fn kd(&self, i: usize) -> K { + self.node(i).map_or(K::Script, |n| n.kind) + } + + fn op(&self, i: usize) -> Op { + self.node(i).map_or(Op::None, |n| n.op) + } + + fn flag(&self, i: usize) -> bool { + self.node(i).is_some_and(|n| n.flag) + } + + /// The children of `i`. + fn ch(&self, i: usize) -> &'a [u32] { + self.node(i).map_or(&[], |n| n.children.as_slice()) + } + + /// Child `k` of `i`, `usize::MAX` (outside the arena) if none. + fn k(&self, i: usize, k: usize) -> usize { + self.ch(i).get(k).map_or(usize::MAX, |c| *c as usize) + } + + /// The name of the identifier, string or private name node `i`. + fn name(&self, i: usize) -> Vec { + let Some(n) = self.node(i) else { + return Vec::new(); + }; + lex(self.src, n.start, Goal::InputElementDiv, true) + .map(|t| t.name) + .unwrap_or_default() + } + + /// The first token of node `i`. + fn tok(&self, i: usize) -> Option { + let n = self.node(i)?; + lex(self.src, n.start, Goal::InputElementDiv, true) + } + + // ----- static semantics -------------------------------------------- + + /// `AssignmentTargetType` (§8.6.4). + fn target_type(&self, c: usize, strict: bool) -> Target { + match self.kd(c) { + K::IdentifierReference => { + let n = self.name(c); + if strict && (name_is(&n, "eval") || name_is(&n, "arguments")) { + Target::Invalid + } else { + Target::Simple + } + } + K::MemberExpression | K::SuperProperty => Target::Simple, + K::ParenthesizedExpression => { + let k0 = self.k(c, 0); + if k0 < c { + self.target_type(k0, strict) + } else { + Target::Invalid + } + } + K::CallExpression => { + if strict { + Target::Invalid + } else { + Target::WebCompat + } + } + _ => Target::Invalid, + } + } + + fn destructuring_target_ok(&self, c: usize, strict: bool) -> bool { + is_assignment_pattern(self.kd(c)) || self.target_type(c, strict) == Target::Simple + } + + fn strip_parens(&self, mut c: usize) -> usize { + while self.kd(c) == K::ParenthesizedExpression { + let k0 = self.k(c, 0); + if k0 >= c { + break; + } + c = k0; + } + c + } + + fn is_iteration(&self, mut c: usize) -> bool { + loop { + match self.kd(c) { + K::DoWhileStatement + | K::WhileStatement + | K::ForStatement + | K::ForInStatement + | K::ForOfStatement => { + return true; + } + K::LabelledStatement => { + let k1 = self.k(c, 1); + if k1 >= c { + return false; + } + c = k1; + } + _ => return false, + } + } + } + + fn is_labelled_function(&self, mut c: usize) -> bool { + while self.kd(c) == K::LabelledStatement { + let k1 = self.k(c, 1); + if self.kd(k1) == K::FunctionDeclaration { + return true; + } + if k1 >= c { + return false; + } + c = k1; + } + false + } + + /// §11.2.1: a Directive Prologue item. + fn is_directive(&self, c: usize) -> bool { + self.kd(c) == K::ExpressionStatement && self.kd(self.k(c, 0)) == K::StringLiteral + } + + /// A directive whose string is exactly `"use strict"` or `'use strict'`. + fn is_use_strict_directive(&self, c: usize) -> bool { + if !self.is_directive(c) { + return false; + } + let s = self.k(c, 0); + let Some(n) = self.node(s) else { return false }; + let Some(text) = self.src.get(n.start..n.end) else { + return false; + }; + text.len() == 12 + && matches!(text.first(), Some(0x22 | 0x27)) + && text.get(1..11).is_some_and(|t| name_is(t, "use strict")) + && text.last() == text.first() + } + + fn has_use_strict(&self, ks: &[u32]) -> bool { + for c in ks { + let c = *c as usize; + if !self.is_directive(c) { + return false; + } + if self.is_use_strict_directive(c) { + return true; + } + } + false + } + + /// `IsSimpleParameterList` of a `FormalParameters` node. + fn is_simple_params(&self, params: usize) -> bool { + self.ch(params).iter().all(|p| { + let p = *p as usize; + self.kd(p) == K::BindingElement + && self.kd(self.k(p, 0)) == K::BindingIdentifier + && self.kd(self.k(p, 1)) == K::Absent + }) + } + + /// `PropName` of `c` is `w`: an identifier or string key. + fn prop_name_is(&self, c: usize, w: &str) -> bool { + matches!(self.kd(c), K::IdentifierName | K::StringLiteral) && name_is(&self.name(c), w) + } + + fn is_constructor_named(&self, c: usize) -> bool { + self.kd(c) == K::MethodDefinition + && !self.flag(c) + && self.prop_name_is(self.k(c, 0), "constructor") + } + + /// BoundNames of the binding node `c`. + fn bound_names(&self, c: usize) -> Vec> { + let mut out = Vec::new(); + self.bound_names_into(c, &mut out); + out + } + + fn bound_names_into(&self, c: usize, out: &mut Vec>) { + match self.kd(c) { + K::BindingIdentifier => out.push(self.name(c)), + K::BindingElement + | K::BindingRestElement + | K::BindingRestProperty + | K::FunctionRestParameter + | K::VariableDeclaration + | K::LexicalBinding + | K::ForDeclaration => { + let k0 = self.k(c, 0); + if k0 < c { + self.bound_names_into(k0, out); + } + } + K::BindingProperty => { + let last = self.ch(c).last().map_or(usize::MAX, |x| *x as usize); + if last < c { + self.bound_names_into(last, out); + } + } + K::ObjectBindingPattern + | K::ArrayBindingPattern + | K::FormalParameters + | K::VariableStatement + | K::LexicalDeclaration + | K::VariableDeclarationList => { + for x in self.ch(c) { + let x = *x as usize; + if x < c { + self.bound_names_into(x, out); + } + } + } + _ => {} + } + } + + /// LexicallyDeclaredNames of one item (TopLevel with `top`). + fn lex_names_item(&self, c: usize, top: bool, out: &mut Vec) { + let k0 = self.k(c, 0); + match self.kd(c) { + K::LexicalDeclaration => { + for n in self.bound_names(c) { + out.push(Decl { + name: n, + plain_function: false, + }); + } + } + K::ClassDeclaration => { + if self.kd(k0) == K::BindingIdentifier { + out.push(Decl { + name: self.name(k0), + plain_function: false, + }); + } + } + K::FunctionDeclaration + | K::GeneratorDeclaration + | K::AsyncFunctionDeclaration + | K::AsyncGeneratorDeclaration => { + if !top && self.kd(k0) == K::BindingIdentifier { + out.push(Decl { + name: self.name(k0), + plain_function: self.kd(c) == K::FunctionDeclaration, + }); + } + } + K::LabelledStatement => { + let k1 = self.k(c, 1); + if !top && k1 < c { + self.lex_names_item(k1, top, out); + } + } + K::ImportDeclaration => { + if k0 < c { + self.bound_names_import(k0, out); + } + } + K::ExportDeclaration + if matches!( + self.op(c), + Op::ExportDecl | Op::ExportDefaultHoistable | Op::ExportDefaultClass + ) && k0 < c => + { + self.lex_names_item(k0, false, out); + } + _ => {} + } + } + + /// The names an `ImportClause` binds. + fn bound_names_import(&self, c: usize, out: &mut Vec) { + let (k0, k1) = (self.k(c, 0), self.k(c, 1)); + if self.kd(k0) == K::BindingIdentifier { + out.push(Decl { + name: self.name(k0), + plain_function: false, + }); + } + if self.kd(k1) == K::NameSpaceImport { + out.push(Decl { + name: self.name(self.k(k1, 0)), + plain_function: false, + }); + } else if self.kd(k1) == K::NamedImports { + for s in self.ch(k1) { + out.push(Decl { + name: self.name(self.k(*s as usize, 1)), + plain_function: false, + }); + } + } + } + + fn lex_names_list(&self, ks: &[u32], from: usize, top: bool) -> Vec { + let mut out = Vec::new(); + for c in ks.iter().skip(from) { + self.lex_names_item(*c as usize, top, &mut out); + } + out + } + + fn has_forbidden_duplicate(ds: &[Decl], strict: bool) -> bool { + let mut index: std::collections::HashMap<&Vec, Vec<&Decl>> = + std::collections::HashMap::new(); + for d in ds { + if index.get(&d.name).is_some_and(|prev| { + prev.iter() + .any(|p| !(!strict && p.plain_function && d.plain_function)) + }) { + return true; + } + index.entry(&d.name).or_default().push(d); + } + false + } + + fn plain(ds: &[Decl]) -> Vec> { + ds.iter().map(|d| d.name.clone()).collect() + } + + /// VarDeclaredNames of statement `c` (TopLevel with `top`). + fn var_names_item(&self, c: usize, top: bool, out: &mut Vec>) { + let ks = self.ch(c); + let k0 = self.k(c, 0); + let k1 = self.k(c, 1); + let k2 = self.k(c, 2); + let k3 = self.k(c, 3); + match self.kd(c) { + K::VariableStatement | K::VariableDeclarationList => self.bound_names_into(c, out), + K::BlockStatement => self.var_names_list(c, ks, 0, out), + K::IfStatement => { + self.var_names_sub(c, k1, out); + self.var_names_sub(c, k2, out); + } + K::DoWhileStatement => self.var_names_sub(c, k0, out), + K::WhileStatement => self.var_names_sub(c, k1, out), + K::ForStatement => { + self.var_names_sub(c, k0, out); + self.var_names_sub(c, k3, out); + } + K::ForInStatement | K::ForOfStatement => { + if k0 < c && self.kd(k0) == K::ForDeclaration && self.op(k0) == Op::Var { + self.bound_names_into(k0, out); + } + self.var_names_sub(c, k2, out); + } + K::WithStatement => self.var_names_sub(c, k1, out), + K::SwitchStatement | K::CaseClause => self.var_names_list(c, ks, 1, out), + K::DefaultClause => self.var_names_list(c, ks, 0, out), + K::LabelledStatement => { + if k1 < c { + self.var_names_item(k1, top, out); + } + } + K::TryStatement => { + self.var_names_sub(c, k0, out); + self.var_names_sub(c, k1, out); + self.var_names_sub(c, k2, out); + } + K::Catch => self.var_names_sub(c, k1, out), + K::Finally => self.var_names_sub(c, k0, out), + K::FunctionDeclaration + | K::GeneratorDeclaration + | K::AsyncFunctionDeclaration + | K::AsyncGeneratorDeclaration => { + if top && self.kd(k0) == K::BindingIdentifier { + out.push(self.name(k0)); + } + } + K::ExportDeclaration if self.op(c) == Op::ExportVar => self.var_names_sub(c, k0, out), + _ => {} + } + } + + fn var_names_sub(&self, c: usize, s: usize, out: &mut Vec>) { + if s < c { + self.var_names_item(s, false, out); + } + } + + fn var_names_list(&self, n: usize, ks: &[u32], from: usize, out: &mut Vec>) { + for c in ks.iter().skip(from) { + self.var_names_sub(n, *c as usize, out); + } + } + + fn top_var_names_list(&self, ks: &[u32]) -> Vec> { + let mut out = Vec::new(); + for c in ks { + self.var_names_item(*c as usize, true, &mut out); + } + out + } + + /// §14.2.1, §15.2.1, §16.1.1: the lexical names of a list are unique and + /// not var-declared. + fn list_early(&self, n: usize, ks: &[u32], strict: bool, top: bool) -> bool { + let lex = self.lex_names_list(ks, 0, top); + let vars = if top { + self.top_var_names_list(ks) + } else { + let mut v = Vec::new(); + self.var_names_list(n, ks, 0, &mut v); + v + }; + !Self::has_forbidden_duplicate(&lex, strict) && !intersects(&Self::plain(&lex), &vars) + } + + /// §15.1.1 and the other rules shared by the function-like productions. + fn function_early(&self, params: usize, body_ks: &[u32], unique: bool, strict: bool) -> bool { + let names = self.bound_names(params); + let simple = self.is_simple_params(params); + if self.has_use_strict(body_ks) && !simple { + return false; + } + let lex = Self::plain(&self.lex_names_list(body_ks, 0, true)); + if intersects(&names, &lex) { + return false; + } + !((strict || unique || !simple) && has_dup(&names)) + } + + /// §15.7.1 the early errors of the elements of a `ClassBody`. + fn class_body_early(&self, ks: &[u32]) -> bool { + let mut constructors = 0usize; + for c in ks { + let c = *c as usize; + let key = self.k(c, 0); + let is_static = self.flag(c); + if self.is_constructor_named(c) { + constructors = constructors.saturating_add(1); + if self.op(c) != Op::Method { + return false; + } + } + if self.kd(c) == K::MethodDefinition && is_static && self.prop_name_is(key, "prototype") + { + return false; + } + if self.kd(c) == K::FieldDefinition + && (self.prop_name_is(key, "constructor") + || (is_static && self.prop_name_is(key, "prototype"))) + { + return false; + } + if self.kd(key) == K::PrivateIdentifier && name_is(&self.name(key), "constructor") { + return false; + } + } + if constructors > 1 { + return false; + } + // Private names are declared once, except a getter and a setter pair. + let mut seen: std::collections::HashMap, Vec<(Op, bool, bool)>> = + std::collections::HashMap::new(); + for c in ks { + let c = *c as usize; + let key = self.k(c, 0); + if self.kd(key) == K::PrivateIdentifier { + let nm = self.name(key); + let entry = (self.op(c), self.flag(c), self.kd(c) == K::MethodDefinition); + if let Some(prev) = seen.get(&nm) { + let ok = prev.len() == 1 + && prev.iter().all(|(po, ps, pm)| { + *pm && entry.2 + && ((*po == Op::Getter && entry.0 == Op::Setter) + || (*po == Op::Setter && entry.0 == Op::Getter)) + && *ps == entry.1 + }); + if !ok { + return false; + } + } + seen.entry(nm).or_default().push(entry); + } + } + true + } + + fn private_declared(&self, nm: &[u32]) -> bool { + self.privates.iter().any(|p| p.as_slice() == nm) + } + + fn is_proto_entry(&self, c: usize) -> bool { + self.kd(c) == K::PropertyDefinition + && self.op(c) == Op::KeyValue + && self.prop_name_is(self.k(c, 0), "__proto__") + } + + /// §13.5.1.1: `delete` of an identifier or private member in strict code. + fn delete_error(&self, operand: usize, strict: bool) -> bool { + let o = self.strip_parens(operand); + let last = self.ch(o).last().map_or(usize::MAX, |x| *x as usize); + strict + && (self.kd(o) == K::IdentifierReference + || (self.kd(o) == K::MemberExpression && self.op(o) == Op::PrivateDot) + || (self.kd(o) == K::OptionalExpression + && self.kd(last) == K::OptionalChain + && self.op(last) == Op::ChainPrivateDot)) + } + + /// §13.1.1 identifiers. + fn identifier_ok(&self, n: usize, env: Env) -> bool { + let nm = self.name(n); + let is_yield = name_is(&nm, "yield"); + let is_await = name_is(&nm, "await"); + let kind = self.kd(n); + !(kind == K::BindingIdentifier + && env.strict + && (name_is(&nm, "arguments") || name_is(&nm, "eval"))) + && !(env.strict && name_is_strict_reserved(&nm)) + && !(env.module && is_await) + && !(env.yield_ && is_yield) + && !(env.await_ && is_await) + && !name_is_reserved_always(&nm) + && !(kind == K::IdentifierReference && env.no_arguments && name_is(&nm, "arguments")) + } + + // ----- the rules of one node --------------------------------------- + + fn local(&self, n: usize, env: Env) -> bool { + let ks = self.ch(n); + let (k0, k1, k2, k3) = (self.k(n, 0), self.k(n, 1), self.k(n, 2), self.k(n, 3)); + match self.kd(n) { + K::IdentifierReference | K::BindingIdentifier | K::LabelIdentifier => { + self.identifier_ok(n, env) + } + K::NumericLiteral | K::StringLiteral => { + !(env.strict && self.tok(n).is_some_and(|t| t.legacy)) + } + K::ObjectLiteral => { + let protos = ks + .iter() + .filter(|c| self.is_proto_entry(**c as usize)) + .count(); + protos <= 1 + } + K::CoverInitializedName => false, + K::SuperProperty => env.super_prop, + K::SuperCall => env.super_call, + K::NewTarget => env.new_target, + K::ImportMeta => env.module, + K::MemberExpression => { + self.op(n) != Op::PrivateDot || self.private_declared(&self.name(k1)) + } + K::OptionalChain => { + self.op(n) != Op::ChainTemplate + && (self.op(n) != Op::ChainPrivateDot || self.private_declared(&self.name(k0))) + } + K::RelationalExpression => { + self.kd(k0) != K::PrivateIdentifier || self.private_declared(&self.name(k0)) + } + K::UnaryExpression => !(self.op(n) == Op::Delete && self.delete_error(k0, env.strict)), + K::UpdateExpression => self.target_type(k0, env.strict) != Target::Invalid, + K::AssignmentExpression => { + let op = self.op(n); + (op == Op::Assign && is_assignment_pattern(self.kd(k0))) + || (self.target_type(k0, env.strict) != Target::Invalid + && (!matches!( + op, + Op::LogicalAndAssign | Op::LogicalOrAssign | Op::CoalesceAssign + ) || self.target_type(k0, env.strict) == Target::Simple)) + } + K::AssignmentProperty => { + self.op(n) != Op::Shorthand || self.target_type(k0, env.strict) == Target::Simple + } + K::AssignmentElement | K::AssignmentRestElement => { + self.destructuring_target_ok(k0, env.strict) + } + K::AssignmentRestProperty => { + !is_assignment_pattern(self.kd(k0)) && self.destructuring_target_ok(k0, env.strict) + } + K::YieldExpression | K::AwaitExpression => !env.no_yield_await, + K::BlockStatement => self.list_early(n, ks, env.strict, false), + K::LexicalDeclaration => { + let bn = self.bound_names(n); + !bn.iter().any(|x| name_is(x, "let")) + && !has_dup(&bn) + && (self.op(n) != Op::Const + || ks + .iter() + .all(|b| self.kd(self.k(*b as usize, 1)) != K::Absent)) + } + K::IfStatement => { + !self.is_labelled_function(k1) + && !self.is_labelled_function(k2) + && !(env.strict + && (self.kd(k1) == K::FunctionDeclaration + || self.kd(k2) == K::FunctionDeclaration)) + } + K::DoWhileStatement => !self.is_labelled_function(k0), + K::WhileStatement => !self.is_labelled_function(k1), + K::ForStatement => { + if self.is_labelled_function(k3) { + return false; + } + if self.kd(k0) == K::LexicalDeclaration { + let mut v = Vec::new(); + self.var_names_sub(n, k3, &mut v); + let bn = self.bound_names(k0); + if intersects(&bn, &v) { + return false; + } + } + true + } + K::ForInStatement | K::ForOfStatement => { + // 15.7.1 ContainsAwait: no `for await` in a static block. + if self.kd(n) == K::ForOfStatement && self.flag(n) && env.no_yield_await { + return false; + } + if self.is_labelled_function(k2) { + return false; + } + if self.kd(k0) == K::ForDeclaration { + let bn = self.bound_names(k0); + if self.op(k0) != Op::Var { + let mut v = Vec::new(); + self.var_names_sub(n, k2, &mut v); + if bn.iter().any(|x| name_is(x, "let")) + || has_dup(&bn) + || intersects(&bn, &v) + { + return false; + } + } + let d = self.k(k0, 0); + if self.kd(d) == K::VariableDeclaration { + // Annex B.3.5. + return !env.strict + && self.kd(self.k(d, 0)) == K::BindingIdentifier + && self.kd(self.k(d, 1)) != K::Absent; + } + true + } else { + is_assignment_pattern(self.kd(k0)) + || self.target_type(k0, env.strict) != Target::Invalid + } + } + K::ContinueStatement => { + if self.kd(k0) == K::LabelIdentifier { + let nm = self.name(k0); + self.labels.iter().any(|(l, it)| *l == nm && *it) + } else { + env.in_loop + } + } + K::BreakStatement => { + if self.kd(k0) == K::LabelIdentifier { + let nm = self.name(k0); + self.labels.iter().any(|(l, _)| *l == nm) + } else { + env.in_breakable + } + } + K::WithStatement => !env.strict && !self.is_labelled_function(k1), + K::SwitchStatement => { + let mut lex = Vec::new(); + for c in ks.iter().skip(1) { + let c = *c as usize; + let from = if self.kd(c) == K::CaseClause { 1 } else { 0 }; + lex.extend(self.lex_names_list(self.ch(c), from, false)); + } + let mut vars = Vec::new(); + self.var_names_list(n, ks, 1, &mut vars); + !Self::has_forbidden_duplicate(&lex, env.strict) + && !intersects(&Self::plain(&lex), &vars) + } + K::LabelledStatement => { + let nm = self.name(k0); + !self.labels.iter().any(|(l, _)| *l == nm) + && !(env.strict && self.kd(k1) == K::FunctionDeclaration) + } + K::Catch => { + let bn = self.bound_names(k0); + let bk = self.ch(k1); + let lex = Self::plain(&self.lex_names_list(bk, 0, false)); + if has_dup(&bn) || intersects(&bn, &lex) { + return false; + } + if self.kd(k0) != K::BindingIdentifier { + let mut v = Vec::new(); + self.var_names_list(k1, bk, 0, &mut v); + if intersects(&bn, &v) { + return false; + } + } + true + } + K::FunctionBody => self.list_early(n, ks, env.strict, true), + k if is_function_kind(k) => { + let body = self.ch(k2); + self.function_early(k1, body, false, env.strict || self.has_use_strict(body)) + } + K::ArrowFunction | K::AsyncArrowFunction => { + let body: &[u32] = if self.kd(k1) == K::FunctionBody { + self.ch(k1) + } else { + &[] + }; + self.function_early(k0, body, true, env.strict || self.has_use_strict(body)) + } + K::MethodDefinition => { + let body = self.ch(k2); + self.function_early(k1, body, true, env.strict || self.has_use_strict(body)) + } + K::ClassBody => self.class_body_early(ks), + K::Script => self.list_early(n, ks, env.strict, true), + K::Module => self.list_early(n, ks, true, false) && self.exports_early(n, ks), + K::ExportSpecifier => self.export_name_ok(k0) && self.export_name_ok(k1), + K::ImportSpecifier => self.export_name_ok(k0), + K::WithClause => { + let keys: Vec> = ks + .iter() + .map(|c| self.name(self.k(*c as usize, 0))) + .collect(); + !has_dup(&keys) + } + K::ExportDeclaration => { + (self.op(n) != Op::ExportNamed + || self.ch(k0).iter().all(|s| { + let loc = self.k(*s as usize, 0); + let nm = self.name(loc); + self.kd(loc) != K::StringLiteral + && !name_is_reserved_always(&nm) + && !name_is(&nm, "yield") + && !name_is(&nm, "await") + && !name_is_strict_reserved(&nm) + })) + && (self.op(n) != Op::ExportAllAs || self.export_name_ok(k0)) + } + _ => true, + } + } + + /// §16.2.2.1, §16.2.3.1: a `StringLiteral` that is a `ModuleExportName` is well + /// formed (`spec::module_export_name_ok`). + fn export_name_ok(&self, c: usize) -> bool { + self.kd(c) != K::StringLiteral || well_formed_utf16(&self.name(c)) + } + + /// `spec::exported_names_item`: the names an export declaration exports. + fn exported_names_item(&self, c: usize) -> Vec> { + if self.kd(c) != K::ExportDeclaration { + return Vec::new(); + } + let k0 = self.k(c, 0); + match self.op(c) { + Op::ExportAll => Vec::new(), + Op::ExportAllAs => vec![self.name(k0)], + Op::ExportFrom | Op::ExportNamed => self + .ch(k0) + .iter() + .map(|s| { + let s = *s as usize; + let e = self.k(s, 1); + if self.kd(e) == K::Absent { + self.name(self.k(s, 0)) + } else { + self.name(e) + } + }) + .collect(), + Op::ExportVar => self.bound_names(k0), + Op::ExportDecl => { + let mut out = Vec::new(); + self.lex_names_item(k0, false, &mut out); + Self::plain(&out) + } + _ => vec!["default".chars().map(u32::from).collect()], + } + } + + /// §16.2.1.1: no name is exported twice, and every local name of an + /// `export { ... };` is declared in the module (`spec::exports_early`). + fn exports_early(&self, n: usize, ks: &[u32]) -> bool { + let mut exported = Vec::new(); + let mut local = Vec::new(); + for c in ks { + let c = *c as usize; + exported.extend(self.exported_names_item(c)); + if self.kd(c) == K::ExportDeclaration && self.op(c) == Op::ExportNamed { + for s in self.ch(self.k(c, 0)) { + local.push(self.name(self.k(*s as usize, 0))); + } + } + } + if has_dup(&exported) { + return false; + } + if local.is_empty() { + return true; + } + let mut declared = Self::plain(&self.lex_names_list(ks, 0, false)); + self.var_names_list(n, ks, 0, &mut declared); + let set: HashSet<&Vec> = declared.iter().collect(); + local.iter().all(|l| set.contains(l)) + } + + // ----- the environment of the children ----------------------------- + + /// A function-like environment: no label, no loop. + fn env_fn(env: Env, strict: bool, f: Fl) -> Env { + Env { + strict, + module: env.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, + derived: env.derived, + } + } + + /// The flags of an arrow function's parameters (`params`) or body, which + /// inherit `super`, `new.target` and `arguments` from the enclosing code. + fn arrow_flags(env: Env, y: bool, aw: bool, params: bool) -> Fl { + Fl { + y, + aw, + sp: env.super_prop, + sc: env.super_call, + nt: env.new_target, + na: env.no_arguments, + nya: params, + } + } + + /// The environment of child `k` of `n`, and whether the labels are cleared. + fn child_env(&self, n: usize, k: usize, env: Env) -> (Env, bool) { + let kind = self.kd(n); + let (k0, k1, k2) = (self.k(n, 0), self.k(n, 1), self.k(n, 2)); + let g = is_generator_kind(kind); + let aw = is_async_kind(kind); + let fn_strict = env.strict || self.has_use_strict(self.ch(k2)); + let arrow_strict = + env.strict || (self.kd(k1) == K::FunctionBody && self.has_use_strict(self.ch(k1))); + let loop_body = |b: bool| { + if b { + Env { + in_loop: true, + in_breakable: true, + ..env + } + } else { + env + } + }; + match kind { + kd if is_function_kind(kd) => { + if k == 0 { + let expr = is_function_expression_kind(kd); + ( + Env { + strict: fn_strict, + yield_: if expr { g } else { env.yield_ }, + await_: if expr { aw } else { env.await_ }, + ..env + }, + false, + ) + } else { + let f = Fl { + y: g, + aw, + sp: false, + sc: false, + nt: true, + na: false, + nya: k == 1, + }; + (Self::env_fn(env, fn_strict, f), true) + } + } + K::ArrowFunction => { + if k == 0 { + ( + Self::env_fn( + env, + arrow_strict, + Self::arrow_flags(env, env.yield_, env.await_, true), + ), + false, + ) + } else { + ( + Self::env_fn( + env, + arrow_strict, + Self::arrow_flags(env, false, false, false), + ), + true, + ) + } + } + K::AsyncArrowFunction => { + if k == 0 { + let y = self.kd(k0) == K::BindingIdentifier && env.yield_; + ( + Self::env_fn(env, arrow_strict, Self::arrow_flags(env, y, true, true)), + false, + ) + } else { + ( + Self::env_fn( + env, + arrow_strict, + Self::arrow_flags(env, false, true, false), + ), + true, + ) + } + } + K::MethodDefinition => { + if k == 0 { + (env, false) + } else { + let op = self.op(n); + let mg = matches!(op, Op::GeneratorMethod | Op::AsyncGeneratorMethod); + let ma = matches!(op, Op::AsyncMethod | Op::AsyncGeneratorMethod); + let sc = env.derived && self.is_constructor_named(n) && op == Op::Method; + let f = Fl { + y: mg, + aw: ma, + sp: true, + sc, + nt: true, + na: false, + nya: k == 1, + }; + (Self::env_fn(env, fn_strict, f), true) + } + } + K::FieldDefinition => { + if k == 1 { + let f = Fl { + y: env.yield_, + aw: env.await_, + sp: true, + sc: false, + nt: true, + na: true, + nya: false, + }; + (Self::env_fn(env, env.strict, f), true) + } else { + (env, false) + } + } + // 15.7.1 ContainsAwait: no `await` and no `for await` in a static block. + K::ClassStaticBlock => { + let f = Fl { + y: false, + aw: true, + sp: true, + sc: false, + nt: true, + na: true, + nya: true, + }; + (Self::env_fn(env, env.strict, f), true) + } + K::ClassDeclaration | K::ClassExpression => ( + Env { + strict: true, + derived: if k == 2 { + self.kd(k1) != K::Absent + } else { + env.derived + }, + ..env + }, + false, + ), + K::ObjectLiteral => ( + Env { + derived: false, + ..env + }, + false, + ), + K::DoWhileStatement => (loop_body(k == 0), false), + K::WhileStatement => (loop_body(k == 1), false), + K::ForStatement => (loop_body(k == 3), false), + K::ForInStatement | K::ForOfStatement => (loop_body(k == 2), false), + K::SwitchStatement => ( + if k >= 1 { + Env { + in_breakable: true, + ..env + } + } else { + env + }, + false, + ), + _ => (env, false), + } + } + + /// Node `n` and everything below it meet the early errors. + fn walk(&mut self, n: usize, env: Env) -> bool { + if !self.local(n, env) { + return false; + } + let kind = self.kd(n); + let kids = self.ch(n); + let pushed_privates = if kind == K::ClassBody { + let before = self.privates.len(); + for c in kids { + let key = self.k(*c as usize, 0); + if self.kd(key) == K::PrivateIdentifier { + let nm = self.name(key); + self.privates.push(nm); + } + } + Some(before) + } else { + None + }; + let mut ok = true; + for (k, c) in kids.iter().enumerate() { + let c = *c as usize; + if c >= n { + return false; + } + let (cenv, clear) = self.child_env(n, k, env); + let saved = if clear { + Some(std::mem::take(&mut self.labels)) + } else { + None + }; + let pushed_label = kind == K::LabelledStatement && k == 1; + if pushed_label { + let nm = self.name(self.k(n, 0)); + let it = self.is_iteration(self.k(n, 1)); + self.labels.push((nm, it)); + } + let r = self.walk(c, cenv); + if pushed_label { + self.labels.pop(); + } + if let Some(s) = saved { + self.labels = s; + } + if !r { + ok = false; + break; + } + } + if let Some(b) = pushed_privates { + self.privates.truncate(b); + } + ok + } +} diff --git a/crates/clazel-js-syntax/src/examples.rs b/crates/clazel-js-syntax/src/examples.rs index 5f4dcd1d6..d8e23a435 100644 --- a/crates/clazel-js-syntax/src/examples.rs +++ b/crates/clazel-js-syntax/src/examples.rs @@ -250,4 +250,139 @@ pub proof fn example_depth_limit() assert(max_height(a, 0, a.nodes[0].children, 0) == 0); } +/// An `IdentifierName` token of the one-letter name `a` at `start`. +pub open spec fn name_a(start: int) -> Tok { + Tok { + kind: TokKind::IdentifierName, + start: start, + end: start + 1, + text: seq![0x61u32], + name: seq![0x61u32], + escaped: false, + nl_before: false, + legacy: false, + bigint: false, + invalid_escape: false, + regexp_valid: false, + } +} + +/// The module of `export {a}; export {a}`: two `ExportNamed` items. +pub open spec fn module_export_a_twice() -> AstV { + AstV { + nodes: seq![ + node(NodeKind::IdentifierName, Op::None, 8, 9, Seq::empty()), + node(NodeKind::Absent, Op::None, 9, 9, Seq::empty()), + node(NodeKind::ExportSpecifier, Op::None, 8, 9, seq![0int, 1int]), + node(NodeKind::NamedExports, Op::None, 7, 10, seq![2int]), + node(NodeKind::ExportDeclaration, Op::ExportNamed, 0, 11, seq![3int]), + node(NodeKind::IdentifierName, Op::None, 27, 28, Seq::empty()), + node(NodeKind::Absent, Op::None, 28, 28, Seq::empty()), + node(NodeKind::ExportSpecifier, Op::None, 27, 28, seq![5int, 6int]), + node(NodeKind::NamedExports, Op::None, 26, 29, seq![7int]), + node(NodeKind::ExportDeclaration, Op::ExportNamed, 19, 30, seq![8int]), + node(NodeKind::Module, Op::None, 0, 30, seq![4int, 9int]), + ], + root: 10, + } +} + +/// §16.2.1.1: a name exported twice is an early error: `export {a}; export {a}` +/// exports `a` twice. +pub proof fn example_duplicate_export_is_an_early_error() + ensures + ({ + let a = module_export_a_twice(); + let ts = Stream { toks: seq![name_a(8), name_a(27)], gaps: seq![Ins::None, Ins::None, Ins::None] }; + !exports_early(a, ts, 10, a.nodes[10].children) + }), +{ + let a = module_export_a_twice(); + let ts = Stream { toks: seq![name_a(8), name_a(27)], gaps: seq![Ins::None, Ins::None, Ins::None] }; + assert(exists|x: int| 0 <= x < tn(ts) && ts.toks[x].start == a.nodes[0].start) by { + assert(ts.toks[0].start == 8); + } + assert(exists|x: int| 0 <= x < tn(ts) && ts.toks[x].start == a.nodes[5].start) by { + assert(ts.toks[1].start == 27); + } + assert(node_tok(a, ts, 0) == ts.toks[0]); + assert(node_tok(a, ts, 5) == ts.toks[1]); + assert(name_of(a, ts, 0) == seq![0x61u32]); + assert(name_of(a, ts, 5) == seq![0x61u32]); + let ks = a.nodes[10].children; + reveal_with_fuel(exported_names_list, 3); + let n4 = exported_names_item(a, ts, 4); + let n9 = exported_names_item(a, ts, 9); + assert(n4 =~= seq![seq![0x61u32]]); + assert(n9 =~= seq![seq![0x61u32]]); + let all = exported_names_list(a, ts, ks, 0); + assert(all =~= seq![seq![0x61u32], seq![0x61u32]]); + example_repeated_export_names_are_not_unique(); + assert(all == seq![seq![0x61u32], seq![0x61u32]]); +} + +/// §16.2.1.1: the same name twice is a duplicate. +pub proof fn example_repeated_export_names_are_not_unique() + ensures + !exported_names_unique(seq![seq![0x61u32], seq![0x61u32]]), +{ + let ns = seq![seq![0x61u32], seq![0x61u32]]; + assert(ns[0] == ns[1]); + assert(name_in(ns, ns[0], 1)); + assert(names_repeat_from(ns, 0)); +} + +/// §16.2.2.1: a string that holds a lone surrogate is not well formed; a +/// surrogate pair is. +pub proof fn example_lone_surrogate_is_ill_formed() + ensures + !well_formed_utf16(seq![0xD800u32]), + !well_formed_utf16(seq![0xDC00u32]), + !surrogate_ok_at(seq![0xDC00u32], 0), + well_formed_utf16(seq![0xD83Du32, 0xDE00u32]), +{ + let lone = seq![0xD800u32]; + assert(!surrogate_ok_at(lone, 0)); + let low = seq![0xDC00u32]; + assert(!surrogate_ok_at(low, 0)); + let pair = seq![0xD83Du32, 0xDE00u32]; + assert forall|k: int| 0 <= k < pair.len() implies #[trigger] surrogate_ok_at(pair, k) by { + if k == 0 { + } else { + assert(k == 1); + } + } +} + +/// §15.1 `FormalParameters`, §14.3.3: a `...rest` ends its list. A list of the rest +/// alone is fine; a rest followed by another item is not. +pub proof fn example_rest_ends_its_list() + ensures + ({ + let a = AstV { + nodes: seq![ + node(NodeKind::FunctionRestParameter, Op::None, 0, 0, Seq::empty()), + node(NodeKind::FunctionRestParameter, Op::None, 0, 0, Seq::empty()), + ], + root: 1, + }; + !rest_only_last(a, seq![0int, 1int]) && rest_only_last(a, seq![1int]) + }), +{ + let a = AstV { + nodes: seq![ + node(NodeKind::FunctionRestParameter, Op::None, 0, 0, Seq::empty()), + node(NodeKind::FunctionRestParameter, Op::None, 0, 0, Seq::empty()), + ], + root: 1, + }; + let two = seq![0int, 1int]; + assert(two[0] == 0); + assert(is_rest_kind(kd(a, two[0]))); + reveal_with_fuel(rest_free_from, 3); + assert(!rest_only_last(a, two)); + let one = seq![1int]; + assert(rest_only_last(a, one)); +} + } // verus! diff --git a/crates/clazel-js-syntax/src/exec.rs b/crates/clazel-js-syntax/src/exec.rs new file mode 100644 index 000000000..d8ad9ec9a --- /dev/null +++ b/crates/clazel-js-syntax/src/exec.rs @@ -0,0 +1,115 @@ +//! The executable parser: [`parse`]. +//! +//! The recursive-descent parser ([`crate::parser`] and its grammar modules) +//! proposes an arena; [`compact`] drops the nodes the cover grammars left +//! behind; [`crate::early`] rejects what §16's early errors reject; +//! [`crate::check::finish`] (proven) accepts the arena only if it is well +//! formed, inside the source text and within [`MAX_DEPTH`]. + +use crate::api::ParseError; +use crate::arena_d::{Derivation, check_derivation_canon}; +use crate::ast::{Ast, Node, NodeKind}; +use crate::parser::Parser; +use crate::spec::SourceGoal; + +/// Parses `src` (code points) as a `Script` or `Module`. +/// +/// Total: every `src` and `goal` has a result. See the crate header for what +/// of [`crate::contract::parse_ensures`] is proven and what is only tested. +pub fn parse(src: &[u32], goal: SourceGoal) -> Result { + parse_checked(src, goal).map(|(ast, _)| ast) +} + +/// [`parse`], and what [`crate::arena_d::check_derivation_canon`] found of the arena: +/// `Failed` (a covered node the grammar does not derive) is an error, so an `Ok` +/// 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> { + // 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. + const SMALL: usize = 64; + if src.len() <= SMALL { + return parse_inner(src, goal); + } + let run = || parse_inner(src, goal); + std::thread::scope(|s| { + match std::thread::Builder::new() + .stack_size(256 * 1024 * 1024) + .spawn_scoped(s, run) + { + Ok(h) => h.join().unwrap_or(Err(ParseError::Syntax)), + // No thread to be had: parse here, on the stack this thread has. + Err(_) => run(), + } + }) +} + +fn parse_inner(src: &[u32], goal: SourceGoal) -> Result<(Ast, Derivation), ParseError> { + let module = goal == SourceGoal::Module; + let mut p = Parser::new(src, module)?; + let root = p.parse_program()?; + if p.reachable_bad(root) { + return Err(ParseError::Syntax); + } + let nodes = compact(std::mem::take(&mut p.nodes), root); + // The arena check first: it bounds the height, which bounds the recursion of + // the early errors. + let ast = crate::check::finish(nodes, src.len())?; + crate::early::check(src, &ast, module)?; + let mut gaps = vec![crate::spec::Ins::None; p.rec.len().saturating_add(1)]; + for (at, ins) in p.gap_events.iter() { + if let Some(g) = gaps.get_mut(*at) { + *g = *ins; + } + } + let d = check_derivation_canon(src, &ast.nodes, &p.rec, &gaps, module); + if d == Derivation::Failed { + return Err(ParseError::Syntax); + } + Ok((ast, d)) +} + +/// The nodes reachable from `root`, in their original order, renumbered. The +/// order keeps every child before its parent. +pub fn compact(nodes: Vec, root: u32) -> Vec { + let mut keep = vec![false; nodes.len()]; + let mut stack = vec![root]; + while let Some(id) = stack.pop() { + let Some(k) = keep.get_mut(id as usize) else { + continue; + }; + if *k { + continue; + } + *k = true; + if let Some(n) = nodes.get(id as usize) { + stack.extend(n.children.iter().copied()); + } + } + let mut remap = vec![u32::MAX; nodes.len()]; + let mut next: u32 = 0; + for (i, k) in keep.iter().enumerate() { + if *k { + if let Some(r) = remap.get_mut(i) { + *r = next; + } + next = next.saturating_add(1); + } + } + let mut out = Vec::new(); + for (i, mut n) in nodes.into_iter().enumerate() { + if keep.get(i).copied().unwrap_or(false) { + for c in n.children.iter_mut() { + *c = remap.get(*c as usize).copied().unwrap_or(u32::MAX); + } + out.push(n); + } + } + out +} + +/// Whether the node is a root kind. +pub fn is_root_kind(k: NodeKind) -> bool { + matches!(k, NodeKind::Script | NodeKind::Module) +} diff --git a/crates/clazel-js-syntax/src/expr.rs b/crates/clazel-js-syntax/src/expr.rs new file mode 100644 index 000000000..9621acda4 --- /dev/null +++ b/crates/clazel-js-syntax/src/expr.rs @@ -0,0 +1,1205 @@ +//! The parser's expressions (ECMA-262 §13, §15.3, §15.5, §15.9) and its +//! patterns: precedence climbing for the binary operators, and the cover +//! grammars (§13.2.9, §13.15.5) read as the refined production by converting +//! the arena nodes once the token that decides (`=`, `=>`) is seen +//! ([`crate::cover`]). + +use crate::ast::{NodeKind as K, Op}; +use crate::parser::{Cover, Cx, Parser, R, syn}; +use clazel_js_lexer::spec::{ElementKind, TemplateKind}; + +/// The cx of the body of an arrow function with a braced body, and of its +/// expression body (`In` kept by the caller). +fn arrow_body_cx(is_async: bool, i: bool, braced: bool) -> Cx { + Cx { + y: false, + a: is_async, + i: if braced { true } else { i }, + r: braced, + } +} + +impl<'a> Parser<'a> { + // ----- Expression, AssignmentExpression (§13.15, §13.16) ----------- + + /// `Expression`: assignment expressions separated by `,` (§13.16). + pub fn parse_expression(&mut self, cx: Cx) -> R { + let first = self.parse_assign(cx)?; + if !self.t.is_p(",") { + return Ok(first); + } + let start = self.st(first); + let mut items = vec![first]; + let mut end = self.en(first); + while self.t.is_p(",") { + self.bump()?; + let e = self.parse_assign(cx)?; + end = self.en(e); + items.push(e); + } + Ok(self.mk(K::Expression, Op::None, false, start, end, items)) + } + + /// `AssignmentExpression`, `ArrowFunction`, `AsyncArrowFunction` and + /// `YieldExpression`. + pub fn parse_assign(&mut self, cx: Cx) -> R { + self.nest(|p| p.parse_assign_inner(cx)) + } + + fn parse_assign_inner(&mut self, cx: Cx) -> R { + if cx.y && self.t.is_w("yield") { + return self.parse_yield(cx); + } + if self.t.is_name() { + // `x => ...` and `async x => ...`. + let nx = self.peek()?; + if self.t.is_w("async") + && !nx.nl + && nx.is_name() + && !matches!(nx.s, "function" | "in" | "instanceof") + { + let nx2 = self.peek_after(&nx)?; + if nx2.is_p("=>") && !nx2.nl { + return self.parse_async_ident_arrow(cx); + } + } + if nx.is_p("=>") && !nx.nl && self.ident_ok(cx) { + let p = self.parse_ident(K::BindingIdentifier, cx)?; + return self.parse_arrow_rest(cx, p, self.st(p), false); + } + } + let lhs = self.parse_cond(cx)?; + if self.t.is_p("=>") { + return self.parse_arrow_from_cover(cx, lhs); + } + if let Some(op) = self.assign_op() { + let ok = if op == Op::Assign { + matches!(self.kind(lhs), K::ObjectLiteral | K::ArrayLiteral) || self.lvl(lhs) >= 14 + } else { + self.lvl(lhs) >= 14 + }; + if !ok { + return syn(); + } + let target = if op == Op::Assign { + self.as_assign_target(lhs)? + } else { + lhs + }; + self.bump()?; + let rhs = self.parse_assign(cx)?; + let (s, e) = (self.st(lhs), self.en(rhs)); + return Ok(self.mk(K::AssignmentExpression, op, false, s, e, vec![target, rhs])); + } + Ok(lhs) + } + + /// The assignment operator at the current token. + fn assign_op(&self) -> Option { + if self.t.eof + || !matches!( + self.t.kind, + ElementKind::Punctuator | ElementKind::DivPunctuator + ) + { + return None; + } + Some(match self.t.s { + "=" => Op::Assign, + "*=" => Op::MulAssign, + "/=" => Op::DivAssign, + "%=" => Op::RemAssign, + "+=" => Op::AddAssign, + "-=" => Op::SubAssign, + "<<=" => Op::ShlAssign, + ">>=" => Op::ShrAssign, + ">>>=" => Op::UShrAssign, + "&=" => Op::BitAndAssign, + "^=" => Op::BitXorAssign, + "|=" => Op::BitOrAssign, + "**=" => Op::ExpAssign, + "&&=" => Op::LogicalAndAssign, + "||=" => Op::LogicalOrAssign, + "??=" => Op::CoalesceAssign, + _ => return None, + }) + } + + /// §15.5 `YieldExpression`. + fn parse_yield(&mut self, cx: Cx) -> R { + let start = self.t.start; + let kw_end = self.t.end; + self.bump()?; + let ends = self.t.nl + || self.t.eof + || (!self.t.is_name() + && matches!(self.t.s, ")" | "]" | "}" | "," | ";" | ":" | "?" | "=>")) + || self.t.is_w("in") + || matches!( + self.t.kind, + ElementKind::Template(TemplateKind::Middle | TemplateKind::Tail) + ); + if ends { + let a = self.absent(); + return Ok(self.mk( + K::YieldExpression, + Op::YieldPlain, + false, + start, + kw_end, + vec![a], + )); + } + let delegate = self.eat_p("*")?; + let e = self.parse_assign(cx)?; + let op = if delegate { + Op::YieldDelegate + } else { + Op::YieldPlain + }; + let end = self.en(e); + Ok(self.mk(K::YieldExpression, op, false, start, end, vec![e])) + } + + /// `ConditionalExpression` (§13.14). + fn parse_cond(&mut self, cx: Cx) -> R { + let test = self.parse_binary(cx, 0)?; + if !self.t.is_p("?") { + return Ok(test); + } + self.bump()?; + let cons = self.parse_assign(cx.with_in(true))?; + self.expect_p(":")?; + let alt = self.parse_assign(cx)?; + let (s, e) = (self.st(test), self.en(alt)); + Ok(self.mk( + K::ConditionalExpression, + Op::None, + false, + s, + e, + vec![test, cons, alt], + )) + } + + /// The binary operator at the current token and its precedence (§13.6 to + /// §13.13: `??` 0, `||` 1, `&&` 2, `|` 3, `^` 4, `&` 5, equality 6, + /// relational 7, shift 8, additive 9, multiplicative 10). + fn binary_op(&self, cx: Cx) -> Option<(K, Op, u8)> { + if self.t.eof { + return None; + } + if self.t.is_name() { + return match self.t.s { + "instanceof" => Some((K::RelationalExpression, Op::InstanceOf, 7)), + "in" if cx.i => Some((K::RelationalExpression, Op::In, 7)), + _ => None, + }; + } + if !matches!( + self.t.kind, + ElementKind::Punctuator | ElementKind::DivPunctuator + ) { + return None; + } + Some(match self.t.s { + "??" => (K::CoalesceExpression, Op::Coalesce, 0), + "||" => (K::LogicalORExpression, Op::LogicalOr, 1), + "&&" => (K::LogicalANDExpression, Op::LogicalAnd, 2), + "|" => (K::BitwiseORExpression, Op::BitOr, 3), + "^" => (K::BitwiseXORExpression, Op::BitXor, 4), + "&" => (K::BitwiseANDExpression, Op::BitAnd, 5), + "==" => (K::EqualityExpression, Op::Eq, 6), + "!=" => (K::EqualityExpression, Op::Ne, 6), + "===" => (K::EqualityExpression, Op::StrictEq, 6), + "!==" => (K::EqualityExpression, Op::StrictNe, 6), + "<" => (K::RelationalExpression, Op::Lt, 7), + ">" => (K::RelationalExpression, Op::Gt, 7), + "<=" => (K::RelationalExpression, Op::Le, 7), + ">=" => (K::RelationalExpression, Op::Ge, 7), + "<<" => (K::ShiftExpression, Op::Shl, 8), + ">>" => (K::ShiftExpression, Op::Shr, 8), + ">>>" => (K::ShiftExpression, Op::UShr, 8), + "+" => (K::AdditiveExpression, Op::Add, 9), + "-" => (K::AdditiveExpression, Op::Sub, 9), + "*" => (K::MultiplicativeExpression, Op::Mul, 10), + "/" => (K::MultiplicativeExpression, Op::Div, 10), + "%" => (K::MultiplicativeExpression, Op::Rem, 10), + _ => return None, + }) + } + + /// The binary operators of precedence at least `min` (precedence climbing). + fn parse_binary(&mut self, cx: Cx, min: u8) -> R { + let mut left = if self.t.kind == ElementKind::PrivateIdentifier && !self.t.eof { + // §13.10 `RelationalExpression : PrivateIdentifier in ShiftExpression`. + let nx = self.peek()?; + if !(nx.is_w("in") && cx.i && min <= 7) { + return syn(); + } + let (s, e) = (self.t.start, self.t.end); + self.bump()?; + self.leaf(K::PrivateIdentifier, s, e) + } else { + self.parse_exp(cx)? + }; + while let Some((kind, op, prec)) = self.binary_op(cx) { + if prec < min { + break; + } + let mixes = match kind { + K::CoalesceExpression => { + !(self.lvl(left) >= 3 || self.kind(left) == K::CoalesceExpression) + } + K::LogicalORExpression | K::LogicalANDExpression => { + self.kind(left) == K::CoalesceExpression + } + _ => false, + }; + if mixes { + return syn(); + } + if self.kind(left) == K::PrivateIdentifier && op != Op::In { + return syn(); + } + self.bump()?; + let right = if kind == K::CoalesceExpression { + self.parse_binary(cx, 3)? + } else { + self.parse_binary(cx, prec.saturating_add(1))? + }; + let (s, e) = (self.st(left), self.en(right)); + left = self.mk(kind, op, false, s, e, vec![left, right]); + } + if self.kind(left) == K::PrivateIdentifier { + return syn(); + } + Ok(left) + } + + /// `ExponentiationExpression` (§13.6): `UpdateExpression ** ExponentiationExpression` + /// or a `UnaryExpression`. + fn parse_exp(&mut self, cx: Cx) -> R { + let u = self.parse_unary(cx)?; + if !self.t.is_p("**") { + return Ok(u); + } + if self.lvl(u) < 13 { + return syn(); + } + self.bump()?; + let r = self.nest(|p| p.parse_exp(cx))?; + let (s, e) = (self.st(u), self.en(r)); + Ok(self.mk( + K::ExponentiationExpression, + Op::Exp, + false, + s, + e, + vec![u, r], + )) + } + + /// `UnaryExpression` (§13.5), `UpdateExpression` (§13.4), `AwaitExpression`. + fn parse_unary(&mut self, cx: Cx) -> R { + if self.t.eof { + return syn(); + } + let start = self.t.start; + let op = if self.t.is_name() { + match self.t.s { + "delete" => Some(Op::Delete), + "void" => Some(Op::Void), + "typeof" => Some(Op::Typeof), + _ => None, + } + } else if matches!(self.t.kind, ElementKind::Punctuator) { + match self.t.s { + "+" => Some(Op::Plus), + "-" => Some(Op::Minus), + "~" => Some(Op::BitNot), + "!" => Some(Op::LogicalNot), + "++" => Some(Op::PrefixIncrement), + "--" => Some(Op::PrefixDecrement), + _ => None, + } + } else { + None + }; + if let Some(op) = op { + self.bump()?; + let operand = self.nest(|p| p.parse_unary(cx))?; + let end = self.en(operand); + let kind = if matches!(op, Op::PrefixIncrement | Op::PrefixDecrement) { + K::UpdateExpression + } else { + K::UnaryExpression + }; + return Ok(self.mk(kind, op, false, start, end, vec![operand])); + } + if cx.a && self.t.is_w("await") { + self.bump()?; + let operand = self.nest(|p| p.parse_unary(cx))?; + let end = self.en(operand); + return Ok(self.mk( + K::AwaitExpression, + Op::None, + false, + start, + end, + vec![operand], + )); + } + let e = self.parse_lhs(cx)?; + if (self.t.is_p("++") || self.t.is_p("--")) && !self.t.nl { + let op = if self.t.is_p("++") { + Op::PostfixIncrement + } else { + Op::PostfixDecrement + }; + let end = self.t.end; + self.bump()?; + let s = self.st(e); + return Ok(self.mk(K::UpdateExpression, op, false, s, end, vec![e])); + } + Ok(e) + } + + // ----- LeftHandSideExpression (§13.3) ------------------------------ + + /// `LeftHandSideExpression`: member, call and optional chain expressions. + pub fn parse_lhs(&mut self, cx: Cx) -> R { + let mut e = if self.t.is_w("new") { + self.nest(|p| p.parse_new(cx))? + } else if self.t.is_w("super") { + self.parse_super(cx)? + } else if self.t.is_w("import") { + self.parse_import_expr(cx)? + } else { + self.parse_primary(cx)? + }; + loop { + if self.t.eof { + break; + } + if self.t.is_p(".") { + e = self.parse_dot(e)?; + } else if self.t.is_p("[") { + e = self.parse_bracket(cx, e)?; + } else if self.t.is_p("(") { + let callee_is_async = self.kind(e) == K::IdentifierReference + && self.en(e).saturating_sub(self.st(e)) == 5 + && !self.t.nl + && self + .src + .get(self.st(e)..self.en(e)) + .is_some_and(|t| crate::token::name_is(t, "async")); + let args = self.parse_arguments(cx)?; + let (s, en) = (self.st(e), self.en(args)); + if !matches!(self.lvl(e), 15 | 17) { + return syn(); + } + let call = self.mk(K::CallExpression, Op::None, false, s, en, vec![e, args]); + if callee_is_async { + self.async_calls.insert(call); + } + e = call; + } else if self.t.kind == ElementKind::Template(TemplateKind::NoSubstitution) + || self.t.kind == ElementKind::Template(TemplateKind::Head) + { + e = self.parse_tagged(cx, e)?; + } else if self.t.is_p("?.") { + return self.parse_optional(cx, e); + } else { + break; + } + } + Ok(e) + } + + /// `MemberExpression . IdentifierName` and `. PrivateIdentifier`. + fn parse_dot(&mut self, obj: u32) -> R { + if !matches!(self.lvl(obj), 15 | 17) { + return syn(); + } + self.bump()?; + let (s, e) = (self.t.start, self.t.end); + let (name, op) = if self.t.is_name() { + (self.leaf(K::IdentifierName, s, e), Op::Dot) + } else if self.t.kind == ElementKind::PrivateIdentifier && !self.t.eof { + (self.leaf(K::PrivateIdentifier, s, e), Op::PrivateDot) + } else { + return syn(); + }; + self.bump()?; + let os = self.st(obj); + Ok(self.mk(K::MemberExpression, op, false, os, e, vec![obj, name])) + } + + /// `MemberExpression [ Expression ]`. + fn parse_bracket(&mut self, cx: Cx, obj: u32) -> R { + if !matches!(self.lvl(obj), 15 | 17) { + return syn(); + } + self.bump()?; + let idx = self.parse_expression(cx.with_in(true))?; + let end = self.t.end; + self.expect_p("]")?; + let os = self.st(obj); + Ok(self.mk( + K::MemberExpression, + Op::Bracket, + false, + os, + end, + vec![obj, idx], + )) + } + + /// `MemberExpression TemplateLiteral[+Tagged]`. + fn parse_tagged(&mut self, cx: Cx, tag: u32) -> R { + if !matches!(self.lvl(tag), 15 | 17) { + return syn(); + } + let tpl = self.parse_template(cx, true)?; + let (s, e) = (self.st(tag), self.en(tpl)); + Ok(self.mk(K::TaggedTemplate, Op::None, false, s, e, vec![tag, tpl])) + } + + /// §13.3.9 `OptionalExpression`: the base and then its links. + fn parse_optional(&mut self, cx: Cx, base: u32) -> R { + if !matches!(self.lvl(base), 15 | 17) { + return syn(); + } + let start = self.st(base); + let mut kids = vec![base]; + let mut end = self.en(base); + loop { + if self.t.eof { + break; + } + let flag = self.t.is_p("?."); + let link_start = self.t.start; + if flag { + self.bump()?; + if self.t.kind == ElementKind::Template(TemplateKind::NoSubstitution) + || self.t.kind == ElementKind::Template(TemplateKind::Head) + { + return syn(); + } + } else if !(self.t.is_p(".") || self.t.is_p("[") || self.t.is_p("(")) { + if self.t.kind == ElementKind::Template(TemplateKind::NoSubstitution) + || self.t.kind == ElementKind::Template(TemplateKind::Head) + { + return syn(); + } + break; + } + let link = if self.t.is_p("(") { + let args = self.parse_arguments(cx)?; + end = self.en(args); + self.mk( + K::OptionalChain, + Op::ChainCall, + flag, + link_start, + end, + vec![args], + ) + } else if self.t.is_p("[") { + self.bump()?; + let idx = self.parse_expression(cx.with_in(true))?; + end = self.t.end; + self.expect_p("]")?; + self.mk( + K::OptionalChain, + Op::ChainBracket, + flag, + link_start, + end, + vec![idx], + ) + } else { + if !flag { + // `.` + self.bump()?; + } + let (s, e) = (self.t.start, self.t.end); + let (name, op) = if self.t.is_name() { + (self.leaf(K::IdentifierName, s, e), Op::ChainDot) + } else if self.t.kind == ElementKind::PrivateIdentifier && !self.t.eof { + (self.leaf(K::PrivateIdentifier, s, e), Op::ChainPrivateDot) + } else { + return syn(); + }; + self.bump()?; + end = e; + self.mk(K::OptionalChain, op, flag, link_start, end, vec![name]) + }; + kids.push(link); + } + Ok(self.mk(K::OptionalExpression, Op::None, false, start, end, kids)) + } + + /// `Arguments` (§13.3.8). + pub fn parse_arguments(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.expect_p("(")?; + let cxi = cx.with_in(true); + let mut items = Vec::new(); + while !self.t.is_p(")") { + if self.t.eof { + return syn(); + } + let item = if self.t.is_p("...") { + let s = self.t.start; + self.bump()?; + let e = self.parse_assign(cxi)?; + let end = self.en(e); + self.mk(K::SpreadElement, Op::None, false, s, end, vec![e]) + } else { + self.parse_assign(cxi)? + }; + items.push(item); + if !self.eat_p(",")? { + break; + } + } + let end = self.t.end; + self.expect_p(")")?; + Ok(self.mk(K::Arguments, Op::None, false, start, end, items)) + } + + /// `new` (§13.3.5, §13.3.12). + fn parse_new(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + if self.t.is_p(".") { + self.bump()?; + if !self.t.is_w("target") { + return syn(); + } + let end = self.t.end; + self.bump()?; + return Ok(self.leaf(K::NewTarget, start, end)); + } + let callee = self.parse_member_no_call(cx)?; + if self.t.is_p("(") { + let args = self.parse_arguments(cx)?; + let end = self.en(args); + Ok(self.mk( + K::NewExpression, + Op::None, + false, + start, + end, + vec![callee, args], + )) + } else { + let a = self.absent(); + let end = self.en(callee); + Ok(self.mk( + K::NewExpression, + Op::None, + false, + start, + end, + vec![callee, a], + )) + } + } + + /// The `MemberExpression` that is the callee of `new`: no call, no `?.`. + fn parse_member_no_call(&mut self, cx: Cx) -> R { + let mut e = if self.t.is_w("new") { + self.nest(|p| p.parse_new(cx))? + } else if self.t.is_w("super") { + let s = self.parse_super(cx)?; + if self.kind(s) == K::SuperCall { + return syn(); + } + s + } else if self.t.is_w("import") { + let i = self.parse_import_expr(cx)?; + if self.kind(i) != K::ImportMeta { + return syn(); + } + i + } else { + self.parse_primary(cx)? + }; + loop { + if self.t.eof { + break; + } + if self.t.is_p(".") { + e = self.parse_dot(e)?; + } else if self.t.is_p("[") { + e = self.parse_bracket(cx, e)?; + } else if self.t.kind == ElementKind::Template(TemplateKind::NoSubstitution) + || self.t.kind == ElementKind::Template(TemplateKind::Head) + { + e = self.parse_tagged(cx, e)?; + } else { + break; + } + } + Ok(e) + } + + /// `super . IdentifierName`, `super [ Expression ]` and `super Arguments` (§13.3.7). + fn parse_super(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + if self.t.is_p("(") { + let args = self.parse_arguments(cx)?; + let end = self.en(args); + Ok(self.mk(K::SuperCall, Op::None, false, start, end, vec![args])) + } else if self.t.is_p(".") { + self.bump()?; + if !self.t.is_name() { + return syn(); + } + let (s, e) = (self.t.start, self.t.end); + let name = self.leaf(K::IdentifierName, s, e); + self.bump()?; + Ok(self.mk(K::SuperProperty, Op::Dot, false, start, e, vec![name])) + } else if self.t.is_p("[") { + self.bump()?; + let idx = self.parse_expression(cx.with_in(true))?; + let end = self.t.end; + self.expect_p("]")?; + Ok(self.mk(K::SuperProperty, Op::Bracket, false, start, end, vec![idx])) + } else { + syn() + } + } + + /// `import ( ... )` and `import . meta` (§13.3.10, §13.3.12). + fn parse_import_expr(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + if self.t.is_p(".") { + self.bump()?; + if !self.t.is_w("meta") { + return syn(); + } + let end = self.t.end; + self.bump()?; + return Ok(self.leaf(K::ImportMeta, start, end)); + } + self.expect_p("(")?; + let cxi = cx.with_in(true); + let spec = self.parse_assign(cxi)?; + let spec_end = self.en(spec); + let mut opts = None; + if self.eat_p(",")? && !self.t.is_p(")") { + opts = Some(self.parse_assign(cxi)?); + self.eat_p(",")?; + } + let end = self.t.end; + self.expect_p(")")?; + // The absent options are at the end of the specifier (`spec::d_expr`). + let o = match opts { + Some(o) => o, + None => self.leaf(K::Absent, spec_end, spec_end), + }; + Ok(self.mk(K::ImportCall, Op::None, false, start, end, vec![spec, o])) + } + + // ----- PrimaryExpression (§13.2) ----------------------------------- + + /// `PrimaryExpression`. + pub fn parse_primary(&mut self, cx: Cx) -> R { + if self.t.eof { + return syn(); + } + let (s, e) = (self.t.start, self.t.end); + match self.t.kind { + ElementKind::IdentifierName => match self.t.s { + "this" => { + self.bump()?; + Ok(self.leaf(K::ThisExpression, s, e)) + } + "null" => { + self.bump()?; + Ok(self.leaf(K::NullLiteral, s, e)) + } + "true" | "false" => { + self.bump()?; + Ok(self.leaf(K::BooleanLiteral, s, e)) + } + "function" => self.parse_function(cx, None, true, false), + "class" => self.parse_class(cx, true, false), + "async" => { + let nx = self.peek()?; + if nx.is_w("function") && !nx.nl { + self.parse_function(cx, Some(s), true, false) + } else { + self.parse_ident(K::IdentifierReference, cx) + } + } + _ => self.parse_ident(K::IdentifierReference, cx), + }, + ElementKind::NumericLiteral => { + self.bump()?; + Ok(self.leaf(K::NumericLiteral, s, e)) + } + ElementKind::StringLiteral => { + self.bump()?; + Ok(self.leaf(K::StringLiteral, s, e)) + } + // The first token is read with `InputElementHashbangOrRegExp`. + ElementKind::RegularExpressionLiteral => { + self.bump()?; + Ok(self.leaf(K::RegularExpressionLiteral, s, e)) + } + ElementKind::Template(TemplateKind::NoSubstitution | TemplateKind::Head) => { + self.parse_template(cx, false) + } + ElementKind::Punctuator | ElementKind::DivPunctuator => match self.t.s { + "(" => self.parse_paren(cx), + "[" => self.parse_array(cx), + "{" => self.parse_object(cx), + "/" | "/=" => { + self.relex_regexp()?; + let (s, e) = (self.t.start, self.t.end); + self.bump()?; + Ok(self.leaf(K::RegularExpressionLiteral, s, e)) + } + _ => syn(), + }, + _ => syn(), + } + } + + /// `TemplateLiteral` (§13.2.8). + pub fn parse_template(&mut self, cx: Cx, tagged: bool) -> R { + let start = self.t.start; + let mut kids = Vec::new(); + let cxi = cx.with_in(true); + loop { + let (s, e) = (self.t.start, self.t.end); + let kind = self.t.kind; + let first = kids.is_empty(); + let ok = match kind { + ElementKind::Template(TemplateKind::NoSubstitution | TemplateKind::Head) => first, + ElementKind::Template(TemplateKind::Middle | TemplateKind::Tail) => !first, + _ => false, + }; + if !ok || (!tagged && self.t.invalid_escape) { + return syn(); + } + let el = self.leaf(K::TemplateElement, s, e); + kids.push(el); + self.bump()?; + match kind { + ElementKind::Template(TemplateKind::NoSubstitution | TemplateKind::Tail) => { + return Ok(self.mk(K::TemplateLiteral, Op::None, false, start, e, kids)); + } + _ => { + let ex = self.parse_expression(cxi)?; + kids.push(ex); + if !self.t.is_p("}") { + return syn(); + } + self.relex_template_tail()?; + } + } + } + } + + /// `( ... )`: a `ParenthesizedExpression`, or the cover of an arrow + /// function's parameters (§13.2.9). + fn parse_paren(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let cxi = cx.with_in(true); + let mut items: Vec = Vec::new(); + let mut trailing = false; + let mut has_rest = false; + while !self.t.is_p(")") { + if self.t.eof { + return syn(); + } + if self.t.is_p("...") { + let s = self.t.start; + self.bump()?; + let target = self.parse_binding_target(cxi)?; + let end = self.en(target); + let rest = self.mk( + K::FunctionRestParameter, + Op::None, + false, + s, + end, + vec![target], + ); + items.push(rest); + has_rest = true; + if !self.t.is_p(")") { + return syn(); + } + break; + } + let item = self.parse_assign(cxi)?; + items.push(item); + if self.t.is_p(",") { + self.bump()?; + if self.t.is_p(")") { + trailing = true; + } + } else if !self.t.is_p(")") { + return syn(); + } + } + let end = self.t.end; + self.expect_p(")")?; + let cover_items = items.clone(); + let children = if items.len() == 1 && !trailing && !has_rest { + items + } else if items.len() >= 2 && !trailing && !has_rest { + let (s, e) = ( + self.st(items.first().copied().unwrap_or(u32::MAX)), + self.en(items.last().copied().unwrap_or(u32::MAX)), + ); + vec![self.mk(K::Expression, Op::None, false, s, e, items)] + } else { + items + }; + let id = self.mk( + K::ParenthesizedExpression, + Op::None, + false, + start, + end, + children, + ); + if cover_items.is_empty() || trailing || has_rest { + self.bad.insert(id); + } + self.covers.insert(id, Cover { items: cover_items }); + Ok(id) + } + + /// `ArrayLiteral` (§13.2.4). + fn parse_array(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let cxi = cx.with_in(true); + let mut kids = Vec::new(); + let mut rest_comma = false; + while !self.t.is_p("]") { + if self.t.eof { + return syn(); + } + if self.t.is_p(",") { + let (s, e) = (self.t.start, self.t.end); + self.bump()?; + kids.push(self.leaf(K::Elision, s, e)); + continue; + } + let is_spread = self.t.is_p("..."); + let item = if is_spread { + let s = self.t.start; + self.bump()?; + let e = self.parse_assign(cxi)?; + let end = self.en(e); + self.mk(K::SpreadElement, Op::None, false, s, end, vec![e]) + } else { + self.parse_assign(cxi)? + }; + kids.push(item); + if self.t.is_p(",") { + if is_spread { + rest_comma = true; + } + self.bump()?; + } else if !self.t.is_p("]") { + return syn(); + } + } + let end = self.t.end; + self.bump()?; + let id = self.mk(K::ArrayLiteral, Op::None, false, start, end, kids); + if rest_comma { + self.rest_comma.insert(id); + } + Ok(id) + } + + /// `ObjectLiteral` (§13.2.5). + fn parse_object(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let mut kids = Vec::new(); + let mut rest_comma = false; + while !self.t.is_p("}") { + if self.t.eof { + return syn(); + } + let p = self.parse_property(cx)?; + let is_spread = self.kind(p) == K::PropertyDefinition && self.op(p) == Op::Spread; + kids.push(p); + if self.t.is_p(",") { + if is_spread { + rest_comma = true; + } + self.bump()?; + } else if !self.t.is_p("}") { + return syn(); + } + } + let end = self.t.end; + self.bump()?; + let id = self.mk(K::ObjectLiteral, Op::None, false, start, end, kids); + if rest_comma { + self.rest_comma.insert(id); + } + Ok(id) + } + + /// The token ends a property or class element name's modifier: what + /// follows `get`, `set`, `async` or `static` when it is itself the name. + pub fn ends_name(&self, t: &crate::token::Tok) -> bool { + t.eof || (!t.is_name() && matches!(t.s, "," | ":" | "(" | "}" | "=" | ";")) + } + + /// `PropertyName` (§13.2.5): an `IdentifierName`, `StringLiteral`, + /// `NumericLiteral` or `ComputedPropertyName`. + pub fn parse_prop_name(&mut self, cx: Cx) -> R { + let (s, e) = (self.t.start, self.t.end); + if self.t.eof { + return syn(); + } + match self.t.kind { + ElementKind::IdentifierName => { + self.bump()?; + Ok(self.leaf(K::IdentifierName, s, e)) + } + ElementKind::StringLiteral => { + self.bump()?; + Ok(self.leaf(K::StringLiteral, s, e)) + } + ElementKind::NumericLiteral => { + self.bump()?; + Ok(self.leaf(K::NumericLiteral, s, e)) + } + ElementKind::Punctuator if self.t.is_p("[") => { + self.bump()?; + let ex = self.parse_assign(cx.with_in(true))?; + let end = self.t.end; + self.expect_p("]")?; + Ok(self.mk(K::ComputedPropertyName, Op::None, false, s, end, vec![ex])) + } + _ => syn(), + } + } + + /// One `PropertyDefinition` or `MethodDefinition` of an object literal. + fn parse_property(&mut self, cx: Cx) -> R { + let start = self.t.start; + if self.t.is_p("...") { + self.bump()?; + let e = self.parse_assign(cx.with_in(true))?; + let end = self.en(e); + return Ok(self.mk( + K::PropertyDefinition, + Op::Spread, + false, + start, + end, + vec![e], + )); + } + // Modifiers. + if self.t.is_p("*") { + self.bump()?; + let name = self.parse_prop_name(cx)?; + return self.parse_method(cx, Op::GeneratorMethod, false, start, name); + } + if self.t.is_w("async") { + let nx = self.peek()?; + if !self.ends_name(&nx) && !nx.nl { + self.bump()?; + let op = if self.eat_p("*")? { + Op::AsyncGeneratorMethod + } else { + Op::AsyncMethod + }; + let name = self.parse_prop_name(cx)?; + return self.parse_method(cx, op, false, start, name); + } + } + if self.t.is_w("get") || self.t.is_w("set") { + let nx = self.peek()?; + if !self.ends_name(&nx) { + let op = if self.t.is_w("get") { + Op::Getter + } else { + Op::Setter + }; + self.bump()?; + let name = self.parse_prop_name(cx)?; + return self.parse_method(cx, op, false, start, name); + } + } + // `name : value`, `name ( ... ) { ... }`, shorthand. + if self.t.is_name() { + let nx = self.peek()?; + if !(nx.is_p(":") || nx.is_p("(")) { + // Shorthand `x` or `x = init` (a `CoverInitializedName`). + let id = self.parse_ident(K::IdentifierReference, cx)?; + if self.t.is_p("=") { + self.bump()?; + let init = self.parse_assign(cx.with_in(true))?; + let end = self.en(init); + let n = self.mk( + K::CoverInitializedName, + Op::None, + false, + start, + end, + vec![id, init], + ); + self.bad.insert(n); + return Ok(n); + } + let end = self.en(id); + return Ok(self.mk( + K::PropertyDefinition, + Op::Shorthand, + false, + start, + end, + vec![id], + )); + } + } + let name = self.parse_prop_name(cx)?; + if self.t.is_p("(") { + return self.parse_method(cx, Op::Method, false, start, name); + } + self.expect_p(":")?; + let v = self.parse_assign(cx.with_in(true))?; + let end = self.en(v); + Ok(self.mk( + K::PropertyDefinition, + Op::KeyValue, + false, + start, + end, + vec![name, v], + )) + } + + // ----- Arrow functions (§15.3, §15.9) ------------------------------ + + /// `async x => ...`, at `async`. + fn parse_async_ident_arrow(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let p = self.parse_ident(K::BindingIdentifier, Cx { a: true, ..cx })?; + self.parse_arrow_rest(cx, p, start, true) + } + + /// From `=>` on, with the parameters `params` (a `BindingIdentifier` or + /// `FormalParameters`) already parsed; `start` is where the function starts. + fn parse_arrow_rest(&mut self, cx: Cx, params: u32, start: usize, is_async: bool) -> R { + if !self.t.is_p("=>") || self.t.nl { + return syn(); + } + self.bump()?; + let kind = if is_async { + K::AsyncArrowFunction + } else { + K::ArrowFunction + }; + if self.t.is_p("{") { + let body = self.parse_function_body(arrow_body_cx(is_async, cx.i, true))?; + let end = self.en(body); + Ok(self.mk(kind, Op::None, false, start, end, vec![params, body])) + } else { + let body = self.parse_assign(arrow_body_cx(is_async, cx.i, false))?; + let end = self.en(body); + Ok(self.mk(kind, Op::None, false, start, end, vec![params, body])) + } + } + + /// `lhs =>`: `lhs` is a parenthesized cover or an `async (...)` call. + fn parse_arrow_from_cover(&mut self, cx: Cx, lhs: u32) -> R { + if self.t.nl { + return syn(); + } + if self.kind(lhs) == K::ParenthesizedExpression && self.covers.contains_key(&lhs) { + let items = self + .covers + .get(&lhs) + .map(|c| c.items.clone()) + .unwrap_or_default(); + let (s, e) = (self.st(lhs), self.en(lhs)); + let params = self.cover_to_params(&items, s, e)?; + return self.parse_arrow_rest(cx, params, s, false); + } + if self.kind(lhs) == K::CallExpression && self.async_calls.contains(&lhs) { + let callee = self.child(lhs, 0); + let args = self.child(lhs, 1); + let start = self.st(callee); + let items = self + .node(args) + .map(|n| n.children.clone()) + .unwrap_or_default(); + let (s, e) = (self.st(args), self.en(args)); + let mut conv = Vec::new(); + let n = items.len(); + for (k, it) in items.iter().enumerate() { + if self.kind(*it) == K::SpreadElement { + if k.saturating_add(1) != n || self.args_trailing_comma(args) { + return syn(); + } + let t = self.as_binding_target(self.child(*it, 0))?; + let (is_, ie) = (self.st(*it), self.en(*it)); + conv.push(self.mk(K::FunctionRestParameter, Op::None, false, is_, ie, vec![t])); + } else { + conv.push(*it); + } + } + let params = self.cover_to_params(&conv, s, e)?; + return self.parse_arrow_rest(cx, params, start, true); + } + syn() + } + + /// Whether the `Arguments` node `args` ends with a `,` before its `)`. + fn args_trailing_comma(&self, args: u32) -> bool { + let Some(last) = self.node(args).and_then(|n| n.children.last().copied()) else { + return false; + }; + let e = self.en(last); + crate::token::lex( + self.src, + e, + clazel_js_lexer::spec::Goal::InputElementDiv, + false, + ) + .is_some_and(|t| t.is_p(",")) + } + + /// The `FormalParameters` node of an arrow function, from the items of its + /// cover (`span`: the `(` to the `)`). + fn cover_to_params(&mut self, items: &[u32], s: usize, e: usize) -> R { + let mut out = Vec::new(); + let n = items.len(); + for (k, it) in items.iter().enumerate() { + if self.kind(*it) == K::FunctionRestParameter { + if k.saturating_add(1) != n { + return syn(); + } + out.push(*it); + } else { + out.push(self.as_binding_element(*it)?); + } + } + Ok(self.mk(K::FormalParameters, Op::None, false, s, e, out)) + } +} diff --git a/crates/clazel-js-syntax/src/lib.rs b/crates/clazel-js-syntax/src/lib.rs index d7738cd55..82b96270f 100644 --- a/crates/clazel-js-syntax/src/lib.rs +++ b/crates/clazel-js-syntax/src/lib.rs @@ -1,5 +1,6 @@ //! The JavaScript parser for Clazel's Phase 7 browser (phase-7.md item 7.3): the -//! AST, the specification of what a correct parse is, and the parser's contract. +//! AST, the specification of what a correct parse is, the parser's contract, +//! and the parser. //! //! Spec: ECMA-262, 2026 edition, §12.10 and §13 to §16 (and the early errors and //! Annex B.3 syntax they bring), at commit @@ -7,9 +8,6 @@ //! `clazel-js-lexer`). Tier: see `[package.metadata.clazel]` in `Cargo.toml` //! and `docs/VERIFICATION.md`. //! -//! This is the first half of item 7.3 (CLAUDE.md rule 3, contracts before code): -//! there is no parser yet. -//! //! - [`ast`]: the arena AST, with spans, its node kinds named after the //! productions of the standard. //! - [`spec`]: a transcription of the syntactic grammar as a relation between an @@ -19,9 +17,57 @@ //! - [`contract`]: what the executable parser must be proven to satisfy. //! - [`api`]: the contract-only trait [`api::JsParser`]. //! - [`examples`]: a few small proven facts about the spec. +//! - [`exec`]: [`exec::parse`], the executable parser; [`parser`], [`expr`], +//! [`stmt`], [`module`], [`cover`] and [`token`] are its recursive-descent +//! grammar over `clazel-js-lexer`; [`early`] its early errors; [`check`] the +//! arena check that is proven. +//! +//! # What is proven, and what is not +//! +//! The recursive-descent parser and the early errors are plain Rust that +//! *proposes* an arena; [`check::finish`] is the only way out, and Verus proves +//! that it returns an [`ast::Ast`] only if the arena is well formed +//! ([`spec::ast_wf`]: the root is last, children come first, only the root is a +//! `Script` or `Module`, no node is garbage), every span is inside the source +//! text and every child's span inside its parent's ([`spec::spans_wf`]), and its +//! height is at most [`spec::MAX_DEPTH`]. [`check::finish`] runs in linear time +//! and cannot panic or overflow. +//! +//! Not proven, only tested (`tests/`): that an `Ok` arena is a derivation of a +//! token stream of the source text by the grammar of [`spec`] with no early +//! error ([`spec::is_valid_parse`]), and that an `Err` means there is no such +//! 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. +//! +//! Where the parser differs from the specification, on purpose or not: +//! +//! - **Regular expression literals** are taken as valid +//! (`IsValidRegularExpressionLiteral`, §13.2.7.1): `clazel-js-regexp` (item +//! 7.10) is not written. Test262 has 167 negative tests of their contents; they +//! are the only vendored negative tests the parser does not reject. pub mod api; +pub mod arena_d; pub mod ast; +pub mod check; pub mod contract; +pub mod cover; +pub mod derive; +pub mod early; pub mod examples; +pub mod exec; +pub mod expr; +pub mod module; +pub mod parser; pub mod spec; +pub mod stmt; +pub mod token; diff --git a/crates/clazel-js-syntax/src/module.rs b/crates/clazel-js-syntax/src/module.rs new file mode 100644 index 000000000..cf28da19e --- /dev/null +++ b/crates/clazel-js-syntax/src/module.rs @@ -0,0 +1,303 @@ +//! The parser's `import` and `export` declarations (ECMA-262 §16.2). + +use crate::ast::{NodeKind as K, Op}; +use crate::parser::{Cx, Parser, R, syn}; +use clazel_js_lexer::spec::ElementKind; + +impl<'a> Parser<'a> { + /// A `ModuleItem`: an import or export declaration, or a `StatementListItem`. + pub fn parse_module_item(&mut self, cx: Cx) -> R { + if self.t.is_w("import") { + let nx = self.peek()?; + if !(nx.is_p("(") || nx.is_p(".")) { + return self.parse_import(cx); + } + } + if self.t.is_w("export") { + return self.parse_export(cx); + } + self.parse_item(cx) + } + + /// An `IdentifierName` or a `StringLiteral`: a `ModuleExportName`. + fn parse_module_export_name(&mut self) -> R { + let (s, e) = (self.t.start, self.t.end); + if self.t.is_name() { + self.bump()?; + Ok(self.leaf(K::IdentifierName, s, e)) + } else if self.t.kind == ElementKind::StringLiteral && !self.t.eof { + self.bump()?; + Ok(self.leaf(K::StringLiteral, s, e)) + } else { + syn() + } + } + + /// A `StringLiteral` (the `ModuleSpecifier`). + fn parse_module_specifier(&mut self) -> R { + if self.t.kind != ElementKind::StringLiteral || self.t.eof { + return syn(); + } + let (s, e) = (self.t.start, self.t.end); + self.bump()?; + Ok(self.leaf(K::StringLiteral, s, e)) + } + + /// `WithClause?`: `with { ... }`, or an absent one. + fn parse_with_clause(&mut self) -> R { + if !self.t.is_w("with") { + return Ok(self.absent()); + } + let start = self.t.start; + self.bump()?; + self.expect_p("{")?; + let mut kids = Vec::new(); + while !self.t.is_p("}") { + if self.t.eof { + return syn(); + } + let ks = self.t.start; + let key = self.parse_module_export_name()?; + self.expect_p(":")?; + let v = self.parse_module_specifier()?; + let e = self.en(v); + kids.push(self.mk(K::ImportAttribute, Op::None, false, ks, e, vec![key, v])); + if !self.eat_p(",")? && !self.t.is_p("}") { + return syn(); + } + } + let end = self.t.end; + self.bump()?; + Ok(self.mk(K::WithClause, Op::None, false, start, end, kids)) + } + + /// `ImportDeclaration` (§16.2.2). + fn parse_import(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + if self.t.kind == ElementKind::StringLiteral && !self.t.eof { + let clause = self.absent(); + let spec = self.parse_module_specifier()?; + let with = self.parse_with_clause()?; + let end = self.semi()?; + return Ok(self.mk( + K::ImportDeclaration, + Op::None, + false, + start, + end, + vec![clause, spec, with], + )); + } + // The absent default binding is at the start of the clause, inside it + // (`spec::span_ok` allows any point of the white space before the clause). + let cs = self.t.start; + let mut default = None; + let mut named = None; + if self.t.is_name() { + default = Some(self.parse_ident(K::BindingIdentifier, cx)?); + if self.eat_p(",")? { + named = Some(self.parse_import_named_or_ns(cx)?); + } + } else { + named = Some(self.parse_import_named_or_ns(cx)?); + } + let ce = self.prev_end; + let d = match default { + Some(d) => d, + None => self.leaf(K::Absent, cs, cs), + }; + let n = match named { + Some(n) => n, + None => self.leaf(K::Absent, ce, ce), + }; + let clause = self.mk(K::ImportClause, Op::None, false, cs, ce, vec![d, n]); + if !self.t.is_w("from") { + return syn(); + } + self.bump()?; + let spec = self.parse_module_specifier()?; + let with = self.parse_with_clause()?; + let end = self.semi()?; + Ok(self.mk( + K::ImportDeclaration, + Op::None, + false, + start, + end, + vec![clause, spec, with], + )) + } + + /// `NameSpaceImport` or `NamedImports`. + fn parse_import_named_or_ns(&mut self, cx: Cx) -> R { + let start = self.t.start; + if self.t.is_p("*") { + self.bump()?; + if !self.t.is_w("as") { + return syn(); + } + self.bump()?; + let b = self.parse_ident(K::BindingIdentifier, cx)?; + let end = self.en(b); + return Ok(self.mk(K::NameSpaceImport, Op::None, false, start, end, vec![b])); + } + self.expect_p("{")?; + let mut kids = Vec::new(); + while !self.t.is_p("}") { + if self.t.eof { + return syn(); + } + let ss = self.t.start; + let is_str = self.t.kind == ElementKind::StringLiteral; + let name_tok = self.t.clone(); + let first = self.parse_module_export_name()?; + if self.t.is_w("as") { + self.bump()?; + let b = self.parse_ident(K::BindingIdentifier, cx)?; + let end = self.en(b); + kids.push(self.mk(K::ImportSpecifier, Op::None, false, ss, end, vec![first, b])); + } else { + if is_str || !Self::name_ok(&name_tok.name, cx) { + return syn(); + } + let (s, e) = (self.st(first), self.en(first)); + let b = self.leaf(K::BindingIdentifier, s, e); + // At the start of the specifier, inside it (see `parse_import`). + let a = self.leaf(K::Absent, s, s); + kids.push(self.mk(K::ImportSpecifier, Op::None, false, ss, e, vec![a, b])); + } + if !self.eat_p(",")? && !self.t.is_p("}") { + return syn(); + } + } + let end = self.t.end; + self.bump()?; + Ok(self.mk(K::NamedImports, Op::None, false, start, end, kids)) + } + + /// `NamedExports`: `{ ExportSpecifier, ... }`. + fn parse_named_exports(&mut self) -> R { + let start = self.t.start; + self.expect_p("{")?; + let mut kids = Vec::new(); + while !self.t.is_p("}") { + if self.t.eof { + return syn(); + } + let ss = self.t.start; + let local = self.parse_module_export_name()?; + let (exported, end) = if self.t.is_w("as") { + self.bump()?; + let x = self.parse_module_export_name()?; + (x, self.en(x)) + } else { + let e = self.en(local); + (self.leaf(K::Absent, e, e), e) + }; + kids.push(self.mk( + K::ExportSpecifier, + Op::None, + false, + ss, + end, + vec![local, exported], + )); + if !self.eat_p(",")? && !self.t.is_p("}") { + return syn(); + } + } + let end = self.t.end; + self.bump()?; + Ok(self.mk(K::NamedExports, Op::None, false, start, end, kids)) + } + + /// `ExportDeclaration` (§16.2.3). + fn parse_export(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let mk = |p: &mut Self, op: Op, end: usize, kids: Vec| { + p.mk(K::ExportDeclaration, op, false, start, end, kids) + }; + if self.t.is_p("*") { + self.bump()?; + if self.t.is_w("as") { + self.bump()?; + let name = self.parse_module_export_name()?; + if !self.t.is_w("from") { + return syn(); + } + self.bump()?; + let spec = self.parse_module_specifier()?; + let with = self.parse_with_clause()?; + let end = self.semi()?; + return Ok(mk(self, Op::ExportAllAs, end, vec![name, spec, with])); + } + if !self.t.is_w("from") { + return syn(); + } + self.bump()?; + let spec = self.parse_module_specifier()?; + let with = self.parse_with_clause()?; + let end = self.semi()?; + return Ok(mk(self, Op::ExportAll, end, vec![spec, with])); + } + if self.t.is_p("{") { + let ne = self.parse_named_exports()?; + if self.t.is_w("from") { + self.bump()?; + let spec = self.parse_module_specifier()?; + let with = self.parse_with_clause()?; + let end = self.semi()?; + return Ok(mk(self, Op::ExportFrom, end, vec![ne, spec, with])); + } + let end = self.semi()?; + return Ok(mk(self, Op::ExportNamed, end, vec![ne])); + } + if self.t.is_w("var") { + let v = self.parse_statement(cx)?; + let end = self.en(v); + return Ok(mk(self, Op::ExportVar, end, vec![v])); + } + if self.t.is_w("default") { + self.bump()?; + if self.t.is_w("function") { + let d = self.parse_function(cx, None, false, true)?; + let end = self.en(d); + return Ok(mk(self, Op::ExportDefaultHoistable, end, vec![d])); + } + if self.t.is_w("async") { + let nx = self.peek()?; + if nx.is_w("function") && !nx.nl { + let s = self.t.start; + let d = self.parse_function(cx, Some(s), false, true)?; + let end = self.en(d); + return Ok(mk(self, Op::ExportDefaultHoistable, end, vec![d])); + } + } + if self.t.is_w("class") { + let d = self.parse_class(cx, false, true)?; + let end = self.en(d); + return Ok(mk(self, Op::ExportDefaultClass, end, vec![d])); + } + let e = self.parse_assign(cx.with_in(true))?; + let end = self.semi()?; + return Ok(mk(self, Op::ExportDefaultExpression, end, vec![e])); + } + // `export Declaration`. + let is_decl = self.t.is_w("function") + || self.t.is_w("class") + || self.t.is_w("const") + || (self.t.is_w("let") && self.let_starts_declaration()?) + || (self.t.is_w("async") && { + let nx = self.peek()?; + nx.is_w("function") && !nx.nl + }); + if !is_decl { + return syn(); + } + let d = self.parse_item(cx)?; + let end = self.en(d); + Ok(mk(self, Op::ExportDecl, end, vec![d])) + } +} diff --git a/crates/clazel-js-syntax/src/parser.rs b/crates/clazel-js-syntax/src/parser.rs new file mode 100644 index 000000000..bea971511 --- /dev/null +++ b/crates/clazel-js-syntax/src/parser.rs @@ -0,0 +1,415 @@ +//! The recursive-descent parser: the core (tokens, nodes, ASI, the depth +//! counter). The grammar is in [`crate::expr`], [`crate::stmt`] and +//! [`crate::module`]; the early errors are in [`crate::early`]. +//! +//! This is the *proposer* of the design: it is plain Rust, whose output is an +//! arena that [`crate::check::finish`] (proven in Verus) accepts only when it +//! is well formed, within the source text and within [`MAX_DEPTH`]. That the +//! arena is the *right* parse (the rest of [`crate::contract::parse_ensures`]) +//! is not proven; it is tested (`tests/`). + +use crate::api::ParseError; +use crate::ast::{Node, NodeKind, Op}; +use crate::spec::MAX_DEPTH; +use crate::token::{Tok, lex, name_is, name_is_reserved}; +use clazel_js_lexer::spec::{ElementKind, Goal}; +use std::collections::{HashMap, HashSet}; + +/// The result of a parsing function. +pub type R = Result; + +/// The error for a source text that is not derived by the grammar. +pub fn syn() -> R { + Err(ParseError::Syntax) +} + +/// The grammar parameters `[Yield]`, `[Await]`, `[In]`, `[Return]` (§5.1.5). +#[derive(Clone, Copy)] +pub struct Cx { + /// `[Yield]`. + pub y: bool, + /// `[Await]`. + pub a: bool, + /// `[In]`. + pub i: bool, + /// `[Return]`. + pub r: bool, +} + +impl Cx { + /// The same context with `[In]` set. + pub fn with_in(self, i: bool) -> Cx { + Cx { i, ..self } + } +} + +/// What a `ParenthesizedExpression` read as the cover grammar of §13.2.9 holds, +/// for the case that an `=>` follows. +pub struct Cover { + /// The items: assignment expressions, then maybe a `FunctionRestParameter`. + pub items: Vec, +} + +/// The parser. +pub struct Parser<'a> { + /// The source text. + pub src: &'a [u32], + /// Annex B.1.1 applies (scripts). + pub html: bool, + /// The goal symbol is `Module`. + pub module: bool, + /// The current token. + pub t: Tok, + /// The end of the token before the current one. + pub prev_end: usize, + /// The arena (garbage included: [`crate::check`] sees the compacted one). + pub nodes: Vec, + /// The tokens consumed so far. + pub rec: Vec, + /// Where a `;` was inserted (before the token with this index), and which. + pub gap_events: Vec<(usize, crate::spec::Ins)>, + /// `spec::lvl` of each node. + pub lv: Vec, + /// The nesting of recursive calls that each sit inside a node. + pub depth: usize, + /// Nodes that are only valid as a cover grammar (§13.2.5.1's + /// `CoverInitializedName`, `()`, `(a,)`, `(...a)`): an error if reachable. + pub bad: HashSet, + /// The covers of the parenthesized expressions. + pub covers: HashMap, + /// `CallExpression`s that are `async (...)` with no line terminator before + /// the `(`: candidates for the head of an async arrow function. + pub async_calls: HashSet, + /// Array and object literals in which a `,` follows a `...` element. + pub rest_comma: HashSet, +} + +impl<'a> Parser<'a> { + /// A parser at the start of `src`. + pub fn new(src: &'a [u32], module: bool) -> R> { + let html = !module; + let t = lex(src, 0, Goal::InputElementHashbangOrRegExp, html).ok_or(ParseError::Syntax)?; + Ok(Parser { + src, + html, + module, + t, + prev_end: 0, + nodes: Vec::new(), + rec: Vec::new(), + gap_events: Vec::new(), + lv: Vec::new(), + depth: 0, + bad: HashSet::new(), + covers: HashMap::new(), + async_calls: HashSet::new(), + rest_comma: HashSet::new(), + }) + } + + // ----- tokens ----------------------------------------------------- + + /// Moves to the next token, read with `InputElementDiv`. + pub fn bump(&mut self) -> R<()> { + if !self.t.eof { + self.rec.push(crate::token::dtok_of(&self.t)); + } + self.prev_end = self.t.end; + let t = lex(self.src, self.t.end, Goal::InputElementDiv, self.html) + .ok_or(ParseError::Syntax)?; + self.t = t; + Ok(()) + } + + /// The token after the current one (read with `InputElementDiv`). + pub fn peek(&self) -> R { + lex(self.src, self.t.end, Goal::InputElementDiv, self.html).ok_or(ParseError::Syntax) + } + + /// The token after `t`. + pub fn peek_after(&self, t: &Tok) -> R { + lex(self.src, t.end, Goal::InputElementDiv, self.html).ok_or(ParseError::Syntax) + } + + /// The current `/` or `/=` is the start of a regular expression literal: + /// read it again with `InputElementRegExp`. + pub fn relex_regexp(&mut self) -> R<()> { + let nl = self.t.nl; + let mut t = lex(self.src, self.t.start, Goal::InputElementRegExp, self.html) + .ok_or(ParseError::Syntax)?; + t.nl = nl; + if t.kind != ElementKind::RegularExpressionLiteral { + return syn(); + } + self.t = t; + Ok(()) + } + + /// The current `}` ends a template substitution: read it again with + /// `InputElementTemplateTail`. + pub fn relex_template_tail(&mut self) -> R<()> { + let nl = self.t.nl; + let mut t = lex( + self.src, + self.t.start, + Goal::InputElementTemplateTail, + self.html, + ) + .ok_or(ParseError::Syntax)?; + t.nl = nl; + self.t = t; + Ok(()) + } + + /// The current token is the punctuator `p`; consumes it. + pub fn eat_p(&mut self, p: &str) -> R { + if self.t.is_p(p) { + self.bump()?; + Ok(true) + } else { + Ok(false) + } + } + + /// Consumes the punctuator `p`, or a syntax error. + pub fn expect_p(&mut self, p: &str) -> R<()> { + if self.t.is_p(p) { self.bump() } else { syn() } + } + + /// Consumes the word `w`, or a syntax error. + pub fn expect_w(&mut self, w: &str) -> R<()> { + if self.t.is_w(w) { self.bump() } else { syn() } + } + + /// §12.10: the end of a statement that ends with `;`: a real `;`, or one + /// inserted before a `}`, the end of the input or a token after a line + /// terminator. Returns the end of the statement (the `;` token's end, or + /// the end of the last token when inserted). + pub fn semi(&mut self) -> R { + if self.t.is_p(";") { + let e = self.t.end; + self.bump()?; + Ok(e) + } else if self.t.eof || self.t.nl || self.t.is_p("}") { + self.gap_events + .push((self.rec.len(), crate::spec::Ins::Semicolon)); + Ok(self.prev_end) + } else { + syn() + } + } + + /// Runs `f` one level deeper; [`ParseError::TooDeep`] past [`MAX_DEPTH`]. + pub fn nest(&mut self, f: impl FnOnce(&mut Self) -> R) -> R { + // Each nested call sits inside a node, but one node can have two (an + // assignment expression whose right side is a function), so the counter + // may be a few times the height: it bounds the stack, and [`crate::check`] + // applies the exact limit, to the height. + if self.depth >= MAX_DEPTH.saturating_mul(4) { + return Err(ParseError::TooDeep); + } + self.depth = self.depth.saturating_add(1); + let r = f(self); + self.depth = self.depth.saturating_sub(1); + r + } + + // ----- nodes ------------------------------------------------------ + + /// Adds a node; its id. + pub fn mk( + &mut self, + kind: NodeKind, + op: Op, + flag: bool, + start: usize, + end: usize, + children: Vec, + ) -> u32 { + let id = u32::try_from(self.nodes.len()).unwrap_or(u32::MAX); + let first = children.first().copied(); + self.nodes.push(Node { + kind, + op, + flag, + start, + end, + children, + }); + // A chain of `MemberExpression` and `TaggedTemplate` nodes is 17 when + // its base is, 15 otherwise (remembered, so that a long chain is not + // walked at every link). + let l = match kind { + NodeKind::MemberExpression | NodeKind::TaggedTemplate => { + if first.is_some_and(|c| c < id && self.lvl(c) == 17) { + 17 + } else { + 15 + } + } + k => self.lvl_kind(id, k), + }; + self.lv.push(l); + id + } + + /// A node with no children. + pub fn leaf(&mut self, kind: NodeKind, start: usize, end: usize) -> u32 { + self.mk(kind, Op::None, false, start, end, Vec::new()) + } + + /// An absent optional symbol, at the end of the previous token. + pub fn absent(&mut self) -> u32 { + let p = self.prev_end; + self.leaf(NodeKind::Absent, p, p) + } + + /// The node `id`. + pub fn node(&self, id: u32) -> Option<&Node> { + self.nodes.get(id as usize) + } + + /// The kind of `id`; `Script` (a kind no position accepts) if there is none. + pub fn kind(&self, id: u32) -> NodeKind { + self.node(id).map_or(NodeKind::Script, |n| n.kind) + } + + /// The op of `id`. + pub fn op(&self, id: u32) -> Op { + self.node(id).map_or(Op::None, |n| n.op) + } + + /// The start of `id`. + pub fn st(&self, id: u32) -> usize { + self.node(id).map_or(usize::MAX, |n| n.start) + } + + /// The end of `id`. + pub fn en(&self, id: u32) -> usize { + self.node(id).map_or(usize::MAX, |n| n.end) + } + + /// Child `k` of `id`, `u32::MAX` (which no check accepts) if none. + pub fn child(&self, id: u32, k: usize) -> u32 { + self.node(id) + .and_then(|n| n.children.get(k).copied()) + .unwrap_or(u32::MAX) + } + + /// The number of children of `id`. + pub fn nchildren(&self, id: u32) -> usize { + self.node(id).map_or(0, |n| n.children.len()) + } + + /// The binding strength of expression `id` (`spec::lvl`). + pub fn lvl(&self, id: u32) -> i32 { + self.lv.get(id as usize).copied().unwrap_or(-100) + } + + /// `lvl` of a node of kind `k` that is not a member chain's link. + fn lvl_kind(&self, id: u32, k: NodeKind) -> i32 { + use NodeKind as K; + match k { + K::Expression => -1, + K::ConditionalExpression + | K::AssignmentExpression + | K::ArrowFunction + | K::AsyncArrowFunction + | K::YieldExpression + | K::CoalesceExpression => 0, + K::LogicalORExpression => 1, + K::LogicalANDExpression => 2, + K::BitwiseORExpression => 3, + K::BitwiseXORExpression => 4, + K::BitwiseANDExpression => 5, + K::EqualityExpression => 6, + K::RelationalExpression => 7, + K::ShiftExpression => 8, + K::AdditiveExpression => 9, + K::MultiplicativeExpression => 10, + K::ExponentiationExpression => 11, + K::UnaryExpression | K::AwaitExpression => 12, + K::UpdateExpression => 13, + K::OptionalExpression => 14, + K::CallExpression | K::SuperCall | K::ImportCall => 15, + K::NewExpression => { + if self.nchildren(id) == 2 && self.kind(self.child(id, 1)) == K::Absent { + 16 + } else { + 17 + } + } + K::IdentifierReference + | K::ThisExpression + | K::NullLiteral + | K::BooleanLiteral + | K::NumericLiteral + | K::StringLiteral + | K::RegularExpressionLiteral + | K::TemplateLiteral + | K::ArrayLiteral + | K::ObjectLiteral + | K::ParenthesizedExpression + | K::FunctionExpression + | K::GeneratorExpression + | K::AsyncFunctionExpression + | K::AsyncGeneratorExpression + | K::ClassExpression + | K::SuperProperty + | K::ImportMeta + | K::NewTarget => 17, + _ => -100, + } + } + + // ----- identifiers ------------------------------------------------ + + /// The resolved name `n` is an `Identifier` in context `cx` (§13.1): not a + /// `ReservedWord`, except `yield` when `[~Yield]` and `await` when `[~Await]`. + pub fn name_ok(n: &[u32], cx: Cx) -> bool { + if name_is(n, "yield") { + !cx.y + } else if name_is(n, "await") { + !cx.a + } else { + !name_is_reserved(n) + } + } + + /// The current token can be an `Identifier` in `cx`. + pub fn ident_ok(&self, cx: Cx) -> bool { + self.t.is_name() && Self::name_ok(&self.t.name, cx) + } + + /// Parses an `Identifier` as a node of `kind` (`IdentifierReference`, + /// `BindingIdentifier` or `LabelIdentifier`). + pub fn parse_ident(&mut self, kind: NodeKind, cx: Cx) -> R { + if !self.ident_ok(cx) { + return syn(); + } + let (s, e) = (self.t.start, self.t.end); + self.bump()?; + Ok(self.leaf(kind, s, e)) + } + + /// Whether `id` has any cover-only node reachable (checked at the end). + pub fn reachable_bad(&self, root: u32) -> bool { + if self.bad.is_empty() { + return false; + } + let mut stack = vec![root]; + let mut seen: HashSet = HashSet::new(); + while let Some(id) = stack.pop() { + if !seen.insert(id) { + continue; + } + if self.bad.contains(&id) { + return true; + } + if let Some(n) = self.node(id) { + stack.extend(n.children.iter().copied()); + } + } + false + } +} diff --git a/crates/clazel-js-syntax/src/spec.rs b/crates/clazel-js-syntax/src/spec.rs index 236c4f964..92b67a73c 100644 --- a/crates/clazel-js-syntax/src/spec.rs +++ b/crates/clazel-js-syntax/src/spec.rs @@ -37,7 +37,8 @@ //! mode, the targets of assignments, `super`, `new.target`, `import.meta`, //! `yield` and `await` in parameters, private names and the constructors of //! classes, directive prologues, duplicate `__proto__`, `arguments` in field -//! initializers), and §16.2.3.1's rule on the local names of `export {..};`, +//! initializers), §16.2.3.1's rule on the local names of `export {..};`, and +//! §16.2.1.1's on duplicate and undeclared exports and ill-formed export names, //! with Annex B.3.2 (duplicate sloppy function declarations in a block), B.3.3, //! B.3.4 (`var` of a `catch` parameter's name), B.3.5 and B.3.9 (calls as //! assignment targets: `~web-compat~`). @@ -45,9 +46,7 @@ //! //! # What it leaves out //! -//! - **Early errors not yet specified.** §13.2.8.1's limit of 2^32 template strings; -//! §16.2.1.1's rules on ExportedNames (no duplicate), ExportedBindings (each is -//! declared) and `IsStringWellFormedUnicode` of module export names; the +//! - **Early errors not yet specified.** §13.2.8.1's limit of 2^32 template strings; the //! exception of Annex B.3.4 for a `for (var e of ...)` inside a `catch (e)`; //! the rules about the *contents* of a regular expression (§22.2.1.1, Annex //! B.1.2), which are `regexp_valid` of its token, supplied by `clazel-js-regexp`; @@ -1200,6 +1199,132 @@ pub open spec fn private_declared(env: EarlyEnv, nm: Seq) -> bool { has_name(env.privates, nm) } +/// `s`, read as UTF-16 code units, has no lone surrogate: `IsStringWellFormedUnicode` +/// (§7.1.? ) of the StringValue of a `StringLiteral` (§16.2.2.1, §16.2.3.1: "It +/// is a Syntax Error if IsStringWellFormedUnicode(the SV of the StringLiteral) is +/// false"). +pub open spec fn well_formed_utf16(s: Seq) -> bool { + forall|k: int| 0 <= k < s.len() ==> #[trigger] surrogate_ok_at(s, k) +} + +/// The code unit at `k` of `s` is not a lone surrogate: a high surrogate is +/// followed by a low one, and a low one preceded by a high one. +pub open spec fn surrogate_ok_at(s: Seq, k: int) -> bool { + (0xD800 <= s[k] <= 0xDBFF ==> (k + 1 < s.len() && 0xDC00 <= s[k + 1] <= 0xDFFF)) && (0xDC00 + <= s[k] <= 0xDFFF ==> (k > 0 && 0xD800 <= s[k - 1] <= 0xDBFF)) +} + +/// `n` is the name at `ns[k]` or after it. +pub open spec fn name_in(ns: Seq>, n: Seq, k: int) -> bool + decreases ns.len() - k, +{ + k < ns.len() && (ns[k] == n || name_in(ns, n, k + 1)) +} + +/// A name of `ns[k..]` occurs again after it. +pub open spec fn names_repeat_from(ns: Seq>, k: int) -> bool + decreases ns.len() - k, +{ + k + 1 < ns.len() && (name_in(ns, ns[k], k + 1) || names_repeat_from(ns, k + 1)) +} + +/// The exported names `ns` are all different (§16.2.1.1: "the ExportedNames of +/// ModuleItemList contains any duplicate entries" is a Syntax Error). +pub open spec fn exported_names_unique(ns: Seq>) -> bool { + !names_repeat_from(ns, 0) +} + +/// §16.2.2.1, §16.2.3.1 `ModuleExportName : StringLiteral`: the string is well formed. +pub open spec fn module_export_name_ok(a: AstV, ts: Stream, c: int) -> bool { + kd(a, c) != NodeKind::StringLiteral || well_formed_utf16(name_of(a, ts, c)) +} + +/// The names that an `ExportSpecifier` list exports (the name after `as`, or the +/// name itself). +pub open spec fn exported_specifier_names(a: AstV, ts: Stream, ss: Seq) -> Seq> { + ss.map_values( + |s: int| + if kd(a, kid(kids(a, s), 1)) == NodeKind::Absent { + name_of(a, ts, kid(kids(a, s), 0)) + } else { + name_of(a, ts, kid(kids(a, s), 1)) + }, + ) +} + +/// §16.2.3.? ExportedNames of one module item `c`. +pub open spec fn exported_names_item(a: AstV, ts: Stream, c: int) -> Seq> { + if 0 <= c < a.nodes.len() && a.nodes[c].kind == NodeKind::ExportDeclaration { + let k0 = kid(kids(a, c), 0); + match a.nodes[c].op { + Op::ExportAll => Seq::empty(), + Op::ExportAllAs => seq![name_of(a, ts, k0)], + Op::ExportFrom | Op::ExportNamed => exported_specifier_names(a, ts, kids(a, k0)), + Op::ExportVar => bound_names(a, ts, k0), + Op::ExportDecl => plain_names(lex_names_item(a, ts, k0, false)), + _ => seq![sv("default"@)], + } + } else { + Seq::empty() + } +} + +/// §16.2.3.? ExportedBindings of one module item `c`: the local names of an +/// `export { ... };` that has no `from`. +pub open spec fn exported_bindings_item(a: AstV, ts: Stream, c: int) -> Seq> { + if 0 <= c < a.nodes.len() && a.nodes[c].kind == NodeKind::ExportDeclaration + && a.nodes[c].op == Op::ExportNamed { + kids(a, kid(kids(a, c), 0)).map_values(|s: int| name_of(a, ts, kid(kids(a, s), 0))) + } else { + Seq::empty() + } +} + +/// The ExportedNames of the module items `ks[k..]`. +pub open spec fn exported_names_list(a: AstV, ts: Stream, ks: Seq, k: int) -> Seq> + decreases ks.len() - k, +{ + if k < 0 || k >= ks.len() { + Seq::empty() + } else { + exported_names_item(a, ts, ks[k]) + exported_names_list(a, ts, ks, k + 1) + } +} + +/// The ExportedBindings of the module items `ks[k..]`. +pub open spec fn exported_bindings_list(a: AstV, ts: Stream, ks: Seq, k: int) -> Seq> + decreases ks.len() - k, +{ + if k < 0 || k >= ks.len() { + Seq::empty() + } else { + exported_bindings_item(a, ts, ks[k]) + exported_bindings_list(a, ts, ks, k + 1) + } +} + +/// §16.2.1.1 the rules of a `ModuleItemList` `ks` (the children of the `Module` +/// `n`) on its exports: "It is a Syntax Error if the ExportedNames of +/// ModuleItemList contains any duplicate entries", and "if any element of the +/// ExportedBindings of ModuleItemList does not also occur in either the +/// VarDeclaredNames of ModuleItemList, or the LexicallyDeclaredNames of +/// ModuleItemList". Names are compared as sequences of code points or code +/// units, as `name_of` gives them: an identifier and a string literal name that +/// are the same text above U+FFFF are not taken as the same name. +pub open spec fn exports_early(a: AstV, ts: Stream, n: int, ks: Seq) -> bool { + let declared = plain_names(lex_names_list(a, ts, n, ks, 0, false)) + var_names_list( + a, + ts, + n, + ks, + 0, + ); + exported_names_unique(exported_names_list(a, ts, ks, 0)) && (forall|x: int| + 0 <= x < exported_bindings_list(a, ts, ks, 0).len() ==> has_name( + declared, + #[trigger] exported_bindings_list(a, ts, ks, 0)[x], + )) +} + /// The early errors of node `n` itself, in the environment `env`: every "It is a /// Syntax Error if" rule of the clauses cited, for the production `n` is. pub open spec fn early_local(a: AstV, ts: Stream, n: int, env: EarlyEnv) -> bool { @@ -1396,18 +1521,25 @@ pub open spec fn early_local(a: AstV, ts: Stream, n: int, env: EarlyEnv) -> bool NodeKind::ClassBody => class_body_early(a, ts, ks), // 16.1.1 Script, 16.2.1.1 Module: names, and for a module its exports (16.2.3.1) NodeKind::Script => list_early(a, ts, n, ks, env.strict, true), - NodeKind::Module => list_early(a, ts, n, ks, true, false), + NodeKind::Module => list_early(a, ts, n, ks, true, false) && exports_early(a, ts, n, ks), // 16.2.2.1: "WithClauseToAttributes of WithClause has two different entries with the same [[Key]]" NodeKind::WithClause => !has_duplicate( ks.map_values(|c: int| name_of(a, ts, kid(kids(a, c), 0))), ), // 16.2.3.1: "ReferencedBindings of NamedExports contains any StringLiteral", or a reserved word - NodeKind::ExportDeclaration => nd.op != Op::ExportNamed || forall|x: int| + NodeKind::ExportDeclaration => (nd.op != Op::ExportNamed || forall|x: int| 0 <= x < kids(a, k0).len() ==> { let loc = kid(kids(a, #[trigger] kids(a, k0)[x]), 0); kd(a, loc) != NodeKind::StringLiteral && !is_reserved_text(name_of(a, ts, loc)) && !is_strict_reserved(name_of(a, ts, loc)) - }, + }) && (nd.op != Op::ExportAllAs || module_export_name_ok(a, ts, k0)), + // 16.2.2.1, 16.2.3.1: a `StringLiteral` that is a `ModuleExportName` is well formed + NodeKind::ExportSpecifier => module_export_name_ok(a, ts, k0) && module_export_name_ok( + a, + ts, + k1, + ), + NodeKind::ImportSpecifier => module_export_name_ok(a, ts, k0), _ => true, } } @@ -2377,9 +2509,22 @@ pub open spec fn all_args(a: AstV, ks: Seq) -> bool { == NodeKind::SpreadElement } -/// A `...rest` child is the last of its list. +/// A `...rest` child is the last of its list: no child but the last is one. (Written +/// as a recursion over the positions `0 <= k` and `k + 1 < len`: a `forall` over +/// `0 <= k + 1 < len` would take in `k == -1`, whose `ks[k]` is unconstrained.) pub open spec fn rest_only_last(a: AstV, ks: Seq) -> bool { - forall|k: int| 0 <= k + 1 < ks.len() ==> !is_rest_kind(kd(a, #[trigger] ks[k])) + rest_free_from(a, ks, 0) +} + +/// No child of `ks[k..]` but the last is a `...rest`. +pub open spec fn rest_free_from(a: AstV, ks: Seq, k: int) -> bool + decreases ks.len() - k, +{ + if k + 1 >= ks.len() { + true + } else { + !is_rest_kind(kd(a, ks[k])) && rest_free_from(a, ks, k + 1) + } } /// An optional `Expression`: absent, or an `Expression`. diff --git a/crates/clazel-js-syntax/src/stmt.rs b/crates/clazel-js-syntax/src/stmt.rs new file mode 100644 index 000000000..04680bb27 --- /dev/null +++ b/crates/clazel-js-syntax/src/stmt.rs @@ -0,0 +1,1129 @@ +//! The parser's statements, declarations, functions, classes and binding +//! patterns (ECMA-262 §14, §15, §14.3.3, §16.1). + +use crate::ast::{NodeKind as K, Op}; +use crate::parser::{Cx, Parser, R, syn}; +use clazel_js_lexer::spec::ElementKind; + +impl<'a> Parser<'a> { + // ----- Script (§16.1) ---------------------------------------------- + + /// `Script` or `Module`: every item until the end of the source text. + pub fn parse_program(&mut self) -> R { + let cx = Cx { + y: false, + a: self.module, + i: true, + r: false, + }; + let start = if self.t.eof { 0 } else { self.t.start }; + let mut items = Vec::new(); + while !self.t.eof { + let item = if self.module { + self.parse_module_item(cx)? + } else { + self.parse_item(cx)? + }; + items.push(item); + } + let end = if items.is_empty() { 0 } else { self.prev_end }; + let kind = if self.module { K::Module } else { K::Script }; + Ok(self.mk(kind, Op::None, false, start, end, items)) + } + + // ----- Statement lists and items (§14.2) --------------------------- + + /// A `StatementListItem`: a `Statement` or a `Declaration`. + pub fn parse_item(&mut self, cx: Cx) -> R { + if self.t.is_w("function") { + return self.parse_function(cx, None, false, false); + } + if self.t.is_w("async") { + let nx = self.peek()?; + if nx.is_w("function") && !nx.nl { + let s = self.t.start; + return self.parse_function(cx, Some(s), false, false); + } + } + if self.t.is_w("class") { + return self.parse_class(cx, false, false); + } + if self.t.is_w("const") { + return self.parse_lexical(cx, Op::Const); + } + if self.t.is_w("let") && self.let_starts_declaration()? { + return self.parse_lexical(cx, Op::Let); + } + self.parse_statement(cx) + } + + /// The `let` at the current token begins a `LexicalDeclaration` (the next + /// token can start a binding), as against an identifier named `let`. + pub fn let_starts_declaration(&self) -> R { + let nx = self.peek()?; + Ok(nx.is_p("[") + || nx.is_p("{") + || (nx.is_name() && !crate::token::is_reserved_always(word_of(&nx.name)))) + } + + /// `{ StatementList? }`, the items up to the `}` (not consumed). + fn parse_items_until_brace(&mut self, cx: Cx) -> R> { + let mut items = Vec::new(); + while !self.t.is_p("}") { + if self.t.eof { + return syn(); + } + items.push(self.parse_item(cx)?); + } + Ok(items) + } + + /// `Block` (§14.2). + fn parse_block(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.expect_p("{")?; + let items = self.parse_items_until_brace(cx)?; + let end = self.t.end; + self.bump()?; + Ok(self.mk(K::BlockStatement, Op::None, false, start, end, items)) + } + + /// `LexicalDeclaration` (§14.3.1). + fn parse_lexical(&mut self, cx: Cx, op: Op) -> R { + let start = self.t.start; + self.bump()?; + let cxi = cx.with_in(true); + let mut kids = vec![self.parse_binding_decl(cxi, K::LexicalBinding, false)?]; + while self.eat_p(",")? { + kids.push(self.parse_binding_decl(cxi, K::LexicalBinding, false)?); + } + let end = self.semi()?; + Ok(self.mk(K::LexicalDeclaration, op, false, start, end, kids)) + } + + /// `VariableDeclaration` or `LexicalBinding`: a target and its initializer. + /// A pattern needs one, unless `in_for` (the head of `for-in` and `for-of`). + pub fn parse_binding_decl(&mut self, cx: Cx, kind: K, in_for: bool) -> R { + let target = self.parse_binding_target(cx)?; + let start = self.st(target); + let (init, end) = if self.t.is_p("=") { + self.bump()?; + let e = self.parse_assign(cx)?; + (e, self.en(e)) + } else { + if !in_for && self.kind(target) != K::BindingIdentifier { + return syn(); + } + let end = self.en(target); + (self.absent(), end) + }; + Ok(self.mk(kind, Op::None, false, start, end, vec![target, init])) + } + + // ----- Statement (§14) --------------------------------------------- + + /// A `Statement`. + pub fn parse_statement(&mut self, cx: Cx) -> R { + self.nest(|p| p.parse_statement_inner(cx)) + } + + /// The body of an `if` or a labelled statement: a `Statement`, or (Annex + /// B.3.2, B.3.3) a `FunctionDeclaration`. + fn parse_stmt_or_function(&mut self, cx: Cx) -> R { + if self.t.is_w("function") { + let nx = self.peek()?; + if nx.is_p("*") { + return syn(); + } + return self.parse_function(cx, None, false, false); + } + self.parse_statement(cx) + } + + fn parse_statement_inner(&mut self, cx: Cx) -> R { + if self.t.eof { + return syn(); + } + let start = self.t.start; + if self.t.is_p("{") { + return self.parse_block(cx); + } + if self.t.is_p(";") { + let e = self.t.end; + self.bump()?; + return Ok(self.leaf(K::EmptyStatement, start, e)); + } + if self.t.is_name() { + match self.t.s { + "var" => return self.parse_var_statement(cx), + "if" => return self.parse_if(cx), + "do" => return self.parse_do(cx), + "while" => return self.parse_while(cx), + "for" => return self.parse_for(cx), + "continue" | "break" => return self.parse_jump(cx), + "return" => return self.parse_return(cx), + "with" => return self.parse_with(cx), + "switch" => return self.parse_switch(cx), + "throw" => return self.parse_throw(cx), + "try" => return self.parse_try(cx), + "debugger" => { + let kw_end = self.t.end; + self.bump()?; + let end = self.semi()?; + let _ = kw_end; + return Ok(self.leaf(K::DebuggerStatement, start, end)); + } + "function" | "class" | "const" => return syn(), + "let" => { + let nx = self.peek()?; + if nx.is_p("[") { + return syn(); + } + } + "async" => { + let nx = self.peek()?; + if nx.is_w("function") && !nx.nl { + return syn(); + } + } + _ => {} + } + // `Identifier :` is a labelled statement. + if self.ident_ok(cx) { + let nx = self.peek()?; + if nx.is_p(":") { + let label = self.parse_ident(K::LabelIdentifier, cx)?; + self.bump()?; + let body = self.parse_stmt_or_function(cx)?; + let end = self.en(body); + return Ok(self.mk( + K::LabelledStatement, + Op::None, + false, + start, + end, + vec![label, body], + )); + } + } + } + let e = self.parse_expression(cx.with_in(true))?; + let end = self.semi()?; + Ok(self.mk(K::ExpressionStatement, Op::None, false, start, end, vec![e])) + } + + /// `VariableStatement` (§14.3.2). + fn parse_var_statement(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let cxi = cx.with_in(true); + let mut kids = vec![self.parse_binding_decl(cxi, K::VariableDeclaration, false)?]; + while self.eat_p(",")? { + kids.push(self.parse_binding_decl(cxi, K::VariableDeclaration, false)?); + } + let end = self.semi()?; + Ok(self.mk(K::VariableStatement, Op::None, false, start, end, kids)) + } + + /// `( Expression )`, the head of `if`, `while`, `with` and `switch`. + fn parse_paren_head(&mut self, cx: Cx) -> R { + self.expect_p("(")?; + let e = self.parse_expression(cx.with_in(true))?; + self.expect_p(")")?; + Ok(e) + } + + /// `IfStatement` (§14.6). + fn parse_if(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let test = self.parse_paren_head(cx)?; + let cons = self.parse_stmt_or_function(cx)?; + let (alt, end) = if self.t.is_w("else") { + self.bump()?; + let a = self.parse_stmt_or_function(cx)?; + (a, self.en(a)) + } else { + (self.absent(), self.en(cons)) + }; + Ok(self.mk( + K::IfStatement, + Op::None, + false, + start, + end, + vec![test, cons, alt], + )) + } + + /// `DoWhileStatement` (§14.7.2), with §12.10.1's third rule for the `;`. + fn parse_do(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let body = self.parse_statement(cx)?; + self.expect_w("while")?; + let test = self.parse_paren_head(cx)?; + let end = if self.t.is_p(";") { + let e = self.t.end; + self.bump()?; + e + } else { + self.gap_events + .push((self.rec.len(), crate::spec::Ins::DoWhile)); + self.prev_end + }; + Ok(self.mk( + K::DoWhileStatement, + Op::None, + false, + start, + end, + vec![body, test], + )) + } + + /// `WhileStatement` (§14.7.3). + fn parse_while(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let test = self.parse_paren_head(cx)?; + let body = self.parse_statement(cx)?; + let end = self.en(body); + Ok(self.mk( + K::WhileStatement, + Op::None, + false, + start, + end, + vec![test, body], + )) + } + + /// `WithStatement` (§14.11). + fn parse_with(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let obj = self.parse_paren_head(cx)?; + let body = self.parse_statement(cx)?; + let end = self.en(body); + Ok(self.mk( + K::WithStatement, + Op::None, + false, + start, + end, + vec![obj, body], + )) + } + + /// `ContinueStatement` and `BreakStatement` (§14.8, §14.9). + fn parse_jump(&mut self, cx: Cx) -> R { + let start = self.t.start; + let kind = if self.t.is_w("continue") { + K::ContinueStatement + } else { + K::BreakStatement + }; + self.bump()?; + let label = if self.t.is_name() && !self.t.nl && self.ident_ok(cx) { + self.parse_ident(K::LabelIdentifier, cx)? + } else { + self.absent() + }; + let end = self.semi()?; + Ok(self.mk(kind, Op::None, false, start, end, vec![label])) + } + + /// `ReturnStatement` (§14.10). + fn parse_return(&mut self, cx: Cx) -> R { + if !cx.r { + return syn(); + } + let start = self.t.start; + self.bump()?; + let e = if self.t.nl || self.t.eof || self.t.is_p(";") || self.t.is_p("}") { + self.absent() + } else { + self.parse_expression(cx.with_in(true))? + }; + let end = self.semi()?; + Ok(self.mk(K::ReturnStatement, Op::None, false, start, end, vec![e])) + } + + /// `ThrowStatement` (§14.14). + fn parse_throw(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + if self.t.nl { + return syn(); + } + let e = self.parse_expression(cx.with_in(true))?; + let end = self.semi()?; + Ok(self.mk(K::ThrowStatement, Op::None, false, start, end, vec![e])) + } + + /// `SwitchStatement` (§14.12). + fn parse_switch(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let disc = self.parse_paren_head(cx)?; + self.expect_p("{")?; + let mut kids = vec![disc]; + while !self.t.is_p("}") { + let cs = self.t.start; + let mut clause_kids = Vec::new(); + let kind = if self.t.is_w("case") { + self.bump()?; + clause_kids.push(self.parse_expression(cx.with_in(true))?); + K::CaseClause + } else if self.t.is_w("default") { + // §14.12: a `CaseBlock` has at most one `DefaultClause`. + if kids.iter().any(|c| self.kind(*c) == K::DefaultClause) { + return syn(); + } + self.bump()?; + K::DefaultClause + } else { + return syn(); + }; + let mut end = self.t.end; + self.expect_p(":")?; + while !(self.t.is_p("}") || self.t.is_w("case") || self.t.is_w("default")) { + if self.t.eof { + return syn(); + } + let it = self.parse_item(cx)?; + end = self.en(it); + clause_kids.push(it); + } + kids.push(self.mk(kind, Op::None, false, cs, end, clause_kids)); + } + let end = self.t.end; + self.bump()?; + Ok(self.mk(K::SwitchStatement, Op::None, false, start, end, kids)) + } + + /// `TryStatement` (§14.15). + fn parse_try(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let block = self.parse_block(cx)?; + let mut end = self.en(block); + let catch = if self.t.is_w("catch") { + let cs = self.t.start; + self.bump()?; + let param = if self.t.is_p("(") { + self.bump()?; + let p = self.parse_binding_target(cx)?; + self.expect_p(")")?; + p + } else { + self.absent() + }; + let b = self.parse_block(cx)?; + end = self.en(b); + self.mk(K::Catch, Op::None, false, cs, end, vec![param, b]) + } else { + self.absent() + }; + let finally = if self.t.is_w("finally") { + let fs = self.t.start; + self.bump()?; + let b = self.parse_block(cx)?; + end = self.en(b); + self.mk(K::Finally, Op::None, false, fs, end, vec![b]) + } else { + self.absent() + }; + if self.kind(catch) == K::Absent && self.kind(finally) == K::Absent { + return syn(); + } + Ok(self.mk( + K::TryStatement, + Op::None, + false, + start, + end, + vec![block, catch, finally], + )) + } + + // ----- for (§14.7.4, §14.7.5) -------------------------------------- + + /// `for ( ... ) Statement` in all its forms. + fn parse_for(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let is_await = if self.t.is_w("await") { + if !cx.a { + return syn(); + } + self.bump()?; + true + } else { + false + }; + self.expect_p("(")?; + let cxn = cx.with_in(false); + // The initializer, or the head of `for-in` and `for-of`. + if self.t.is_p(";") { + if is_await { + return syn(); + } + let init = self.absent(); + return self.parse_for_rest(cx, start, init); + } + if self.t.is_w("var") + || self.t.is_w("const") + || (self.t.is_w("let") && self.let_starts_declaration()?) + { + let dstart = self.t.start; + let op = if self.t.is_w("var") { + Op::Var + } else if self.t.is_w("let") { + Op::Let + } else { + Op::Const + }; + let (decl_kind, list_kind) = if op == Op::Var { + (K::VariableDeclaration, K::VariableDeclarationList) + } else { + (K::LexicalBinding, K::LexicalDeclaration) + }; + self.bump()?; + let first = self.parse_binding_decl(cxn, decl_kind, true)?; + if self.t.is_w("of") || self.t.is_w("in") { + let of = self.t.is_w("of"); + let target = self.child(first, 0); + let has_init = self.kind(self.child(first, 1)) != K::Absent; + let binding = if has_init { + // Annex B.3.5: `for (var a = 1 in x)`. + if of || op != Op::Var || self.kind(target) != K::BindingIdentifier { + return syn(); + } + first + } else { + target + }; + let head = self.mk( + K::ForDeclaration, + op, + false, + dstart, + self.en(first), + vec![binding], + ); + return self.parse_for_in_of(cx, start, head, of, is_await); + } + if is_await { + return syn(); + } + // A classic `for`: the rest of the list. + let mut kids = vec![first]; + if self.kind(self.child(first, 1)) == K::Absent + && self.kind(self.child(first, 0)) != K::BindingIdentifier + { + return syn(); + } + while self.eat_p(",")? { + kids.push(self.parse_binding_decl(cxn, decl_kind, false)?); + } + let init = if op == Op::Var { + let end = self.prev_end; + self.mk(list_kind, Op::None, false, dstart, end, kids) + } else { + // A `LexicalDeclaration` takes a real `;` (§12.10.1, rule 4). + if !self.t.is_p(";") { + return syn(); + } + let end = self.t.end; + self.bump()?; + let d = self.mk(list_kind, op, false, dstart, end, kids); + return self.parse_for_after_init(cx, start, d, false); + }; + return self.parse_for_rest(cx, start, init); + } + // An expression, or a `LeftHandSideExpression` or pattern before `in`/`of`. + let first_is_let = self.t.is_w("let"); + let first_is_async = self.t.is_w("async"); + let e0 = self.parse_assign(cxn)?; + if self.t.is_w("of") || self.t.is_w("in") { + let of = self.t.is_w("of"); + if of + && (first_is_let + || (first_is_async && !is_await && self.kind(e0) == K::IdentifierReference)) + { + return syn(); + } + let head = self.as_assign_target(e0)?; + if self.lvl(head) < 14 + && !matches!( + self.kind(head), + K::ObjectAssignmentPattern | K::ArrayAssignmentPattern + ) + { + return syn(); + } + return self.parse_for_in_of(cx, start, head, of, is_await); + } + if is_await { + return syn(); + } + let init = if self.t.is_p(",") { + let s = self.st(e0); + let mut items = vec![e0]; + let mut end = self.en(e0); + while self.eat_p(",")? { + let e = self.parse_assign(cxn)?; + end = self.en(e); + items.push(e); + } + self.mk(K::Expression, Op::None, false, s, end, items) + } else { + e0 + }; + self.parse_for_rest(cx, start, init) + } + + /// After a classic `for`'s initializer (not a `LexicalDeclaration`): the `;`. + fn parse_for_rest(&mut self, cx: Cx, start: usize, init: u32) -> R { + self.expect_p(";")?; + self.parse_for_after_init(cx, start, init, true) + } + + /// The test, the update and the body of a classic `for`. + fn parse_for_after_init( + &mut self, + cx: Cx, + start: usize, + init: u32, + _semi_done: bool, + ) -> R { + let cxi = cx.with_in(true); + let test = if self.t.is_p(";") { + self.absent() + } else { + self.parse_expression(cxi)? + }; + self.expect_p(";")?; + let update = if self.t.is_p(")") { + self.absent() + } else { + self.parse_expression(cxi)? + }; + self.expect_p(")")?; + let body = self.parse_statement(cx)?; + let end = self.en(body); + Ok(self.mk( + K::ForStatement, + Op::None, + false, + start, + end, + vec![init, test, update, body], + )) + } + + /// `for ( head in/of expression ) Statement`, at the `in` or `of`. + fn parse_for_in_of( + &mut self, + cx: Cx, + start: usize, + head: u32, + of: bool, + is_await: bool, + ) -> R { + if is_await && !of { + return syn(); + } + self.bump()?; + let cxi = cx.with_in(true); + let e = if of { + self.parse_assign(cxi)? + } else { + self.parse_expression(cxi)? + }; + self.expect_p(")")?; + let body = self.parse_statement(cx)?; + let end = self.en(body); + let kind = if of { + K::ForOfStatement + } else { + K::ForInStatement + }; + Ok(self.mk(kind, Op::None, is_await, start, end, vec![head, e, body])) + } + + // ----- Binding patterns (§14.3.3) ---------------------------------- + + /// `BindingIdentifier` or `BindingPattern`. + pub fn parse_binding_target(&mut self, cx: Cx) -> R { + if self.t.is_p("[") { + self.nest(|p| p.parse_array_binding(cx)) + } else if self.t.is_p("{") { + self.nest(|p| p.parse_object_binding(cx)) + } else { + self.parse_ident(K::BindingIdentifier, cx) + } + } + + /// `BindingElement` (and `SingleNameBinding`): a target with an optional initializer. + pub fn parse_binding_element(&mut self, cx: Cx) -> R { + let target = self.parse_binding_target(cx)?; + let start = self.st(target); + let (init, end) = if self.t.is_p("=") { + self.bump()?; + let e = self.parse_assign(cx.with_in(true))?; + (e, self.en(e)) + } else { + (self.absent(), self.en(target)) + }; + Ok(self.mk( + K::BindingElement, + Op::None, + false, + start, + end, + vec![target, init], + )) + } + + fn parse_array_binding(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let mut kids = Vec::new(); + while !self.t.is_p("]") { + if self.t.eof { + return syn(); + } + if self.t.is_p(",") { + let (s, e) = (self.t.start, self.t.end); + self.bump()?; + kids.push(self.leaf(K::Elision, s, e)); + continue; + } + if self.t.is_p("...") { + let s = self.t.start; + self.bump()?; + let t = self.parse_binding_target(cx)?; + let e = self.en(t); + kids.push(self.mk(K::BindingRestElement, Op::None, false, s, e, vec![t])); + if !self.t.is_p("]") { + return syn(); + } + break; + } + kids.push(self.parse_binding_element(cx)?); + if !self.eat_p(",")? && !self.t.is_p("]") { + return syn(); + } + } + let end = self.t.end; + self.bump()?; + Ok(self.mk(K::ArrayBindingPattern, Op::None, false, start, end, kids)) + } + + fn parse_object_binding(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.bump()?; + let mut kids = Vec::new(); + while !self.t.is_p("}") { + if self.t.eof { + return syn(); + } + let ps = self.t.start; + if self.t.is_p("...") { + self.bump()?; + let t = self.parse_ident(K::BindingIdentifier, cx)?; + let e = self.en(t); + kids.push(self.mk(K::BindingRestProperty, Op::None, false, ps, e, vec![t])); + if !self.t.is_p("}") { + return syn(); + } + break; + } + let shorthand = self.t.is_name() && !self.peek()?.is_p(":"); + if shorthand { + let bi = self.parse_ident(K::BindingIdentifier, cx)?; + let (init, end) = if self.t.is_p("=") { + self.bump()?; + let e = self.parse_assign(cx.with_in(true))?; + (e, self.en(e)) + } else { + (self.absent(), self.en(bi)) + }; + let el = self.mk(K::BindingElement, Op::None, false, ps, end, vec![bi, init]); + kids.push(self.mk(K::BindingProperty, Op::Shorthand, false, ps, end, vec![el])); + } else { + let name = self.parse_prop_name(cx)?; + self.expect_p(":")?; + let el = self.parse_binding_element(cx)?; + let end = self.en(el); + kids.push(self.mk( + K::BindingProperty, + Op::KeyValue, + false, + ps, + end, + vec![name, el], + )); + } + if !self.eat_p(",")? && !self.t.is_p("}") { + return syn(); + } + } + let end = self.t.end; + self.bump()?; + Ok(self.mk(K::ObjectBindingPattern, Op::None, false, start, end, kids)) + } + + // ----- Functions (§15) --------------------------------------------- + + /// `FormalParameters`, from `(` to `)`. + pub fn parse_formal_params(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.expect_p("(")?; + let mut kids = Vec::new(); + while !self.t.is_p(")") { + if self.t.eof { + return syn(); + } + if self.t.is_p("...") { + let s = self.t.start; + self.bump()?; + let t = self.parse_binding_target(cx)?; + let e = self.en(t); + kids.push(self.mk(K::FunctionRestParameter, Op::None, false, s, e, vec![t])); + if !self.t.is_p(")") { + return syn(); + } + break; + } + kids.push(self.parse_binding_element(cx)?); + if !self.eat_p(",")? && !self.t.is_p(")") { + return syn(); + } + } + let end = self.t.end; + self.bump()?; + Ok(self.mk(K::FormalParameters, Op::None, false, start, end, kids)) + } + + /// `FunctionBody` and its relatives: `{ StatementList }`. + pub fn parse_function_body(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.expect_p("{")?; + let items = self.parse_items_until_brace(cx)?; + let end = self.t.end; + self.bump()?; + Ok(self.mk(K::FunctionBody, Op::None, false, start, end, items)) + } + + /// A function, generator, async function or async generator, declaration + /// or expression, at `function` (or at `async` when `async_start`). + /// `deflt`: the name may be absent (`export default`). + pub fn parse_function( + &mut self, + cx: Cx, + async_start: Option, + is_expr: bool, + deflt: bool, + ) -> R { + self.nest(|p| p.parse_function_inner(cx, async_start, is_expr, deflt)) + } + + fn parse_function_inner( + &mut self, + cx: Cx, + async_start: Option, + is_expr: bool, + deflt: bool, + ) -> R { + let start = async_start.unwrap_or(self.t.start); + let is_async = async_start.is_some(); + if is_async { + self.bump()?; + } + self.expect_w("function")?; + let is_gen = self.eat_p("*")?; + let fcx = Cx { + y: is_gen, + a: is_async, + i: true, + r: false, + }; + let name = if is_expr { + if self.t.is_name() && !self.t.is_p("(") { + self.parse_ident(K::BindingIdentifier, fcx)? + } else { + self.absent() + } + } else if self.t.is_name() && self.ident_ok(cx) { + self.parse_ident(K::BindingIdentifier, cx)? + } else if deflt { + self.absent() + } else { + return syn(); + }; + let params = self.parse_formal_params(fcx)?; + let body = self.parse_function_body(Cx { r: true, ..fcx })?; + let end = self.en(body); + let kind = match (is_async, is_gen, is_expr) { + (false, false, false) => K::FunctionDeclaration, + (false, false, true) => K::FunctionExpression, + (false, true, false) => K::GeneratorDeclaration, + (false, true, true) => K::GeneratorExpression, + (true, false, false) => K::AsyncFunctionDeclaration, + (true, false, true) => K::AsyncFunctionExpression, + (true, true, false) => K::AsyncGeneratorDeclaration, + (true, true, true) => K::AsyncGeneratorExpression, + }; + Ok(self.mk(kind, Op::None, false, start, end, vec![name, params, body])) + } + + /// The rest of a `MethodDefinition` (§15.4) after its name: `( ... ) { ... }`. + pub fn parse_method( + &mut self, + _cx: Cx, + op: Op, + is_static: bool, + start: usize, + name: u32, + ) -> R { + let is_gen = matches!(op, Op::GeneratorMethod | Op::AsyncGeneratorMethod); + let is_async = matches!(op, Op::AsyncMethod | Op::AsyncGeneratorMethod); + let pcx = Cx { + y: is_gen, + a: is_async, + i: true, + r: false, + }; + let params = self.parse_formal_params(pcx)?; + let n = self.nchildren(params); + match op { + Op::Getter if n != 0 => return syn(), + Op::Setter if n != 1 || self.kind(self.child(params, 0)) != K::BindingElement => { + return syn(); + } + _ => {} + } + let body = self.parse_function_body(Cx { r: true, ..pcx })?; + let end = self.en(body); + Ok(self.mk( + K::MethodDefinition, + op, + is_static, + start, + end, + vec![name, params, body], + )) + } + + // ----- Classes (§15.7) --------------------------------------------- + + /// `ClassDeclaration` or `ClassExpression`, at `class`. + pub fn parse_class(&mut self, cx: Cx, is_expr: bool, deflt: bool) -> R { + self.nest(|p| p.parse_class_inner(cx, is_expr, deflt)) + } + + fn parse_class_inner(&mut self, cx: Cx, is_expr: bool, deflt: bool) -> R { + let start = self.t.start; + self.expect_w("class")?; + let name = if self.t.is_name() && self.ident_ok(cx) { + self.parse_ident(K::BindingIdentifier, cx)? + } else if is_expr || deflt { + self.absent() + } else { + return syn(); + }; + let heritage = if self.t.is_w("extends") { + self.bump()?; + self.parse_lhs(cx)? + } else { + self.absent() + }; + let body = self.parse_class_body(cx)?; + let end = self.en(body); + let kind = if is_expr { + K::ClassExpression + } else { + K::ClassDeclaration + }; + Ok(self.mk( + kind, + Op::None, + false, + start, + end, + vec![name, heritage, body], + )) + } + + /// `ClassBody`. + fn parse_class_body(&mut self, cx: Cx) -> R { + let start = self.t.start; + self.expect_p("{")?; + let mut kids = Vec::new(); + while !self.t.is_p("}") { + if self.t.eof { + return syn(); + } + if self.t.is_p(";") { + self.bump()?; + continue; + } + kids.push(self.parse_class_element(cx)?); + } + let end = self.t.end; + self.bump()?; + Ok(self.mk(K::ClassBody, Op::None, false, start, end, kids)) + } + + /// `ClassElementName`: a `PropertyName` or a `PrivateIdentifier`. + fn parse_class_element_name(&mut self, cx: Cx) -> R { + if self.t.kind == ElementKind::PrivateIdentifier && !self.t.eof { + let (s, e) = (self.t.start, self.t.end); + self.bump()?; + Ok(self.leaf(K::PrivateIdentifier, s, e)) + } else { + self.parse_prop_name(cx) + } + } + + /// One `ClassElement` that is not `;`. + fn parse_class_element(&mut self, cx: Cx) -> R { + let start = self.t.start; + let mut is_static = false; + if self.t.is_w("static") { + let nx = self.peek()?; + if !self.ends_name(&nx) { + is_static = true; + self.bump()?; + if self.t.is_p("{") { + let bcx = Cx { + y: false, + a: true, + i: true, + r: false, + }; + let body = self.parse_function_body(bcx)?; + let end = self.en(body); + return Ok(self.mk( + K::ClassStaticBlock, + Op::None, + false, + start, + end, + vec![body], + )); + } + } + } + let mstart = self.t.start; + let _ = mstart; + if self.t.is_p("*") { + self.bump()?; + let name = self.parse_class_element_name(cx)?; + return self.parse_method(cx, Op::GeneratorMethod, is_static, start, name); + } + if self.t.is_w("async") { + let nx = self.peek()?; + if !self.ends_name(&nx) && !nx.nl { + self.bump()?; + let op = if self.eat_p("*")? { + Op::AsyncGeneratorMethod + } else { + Op::AsyncMethod + }; + let name = self.parse_class_element_name(cx)?; + return self.parse_method(cx, op, is_static, start, name); + } + } + if self.t.is_w("get") || self.t.is_w("set") { + let nx = self.peek()?; + if !self.ends_name(&nx) { + let op = if self.t.is_w("get") { + Op::Getter + } else { + Op::Setter + }; + self.bump()?; + let name = self.parse_class_element_name(cx)?; + return self.parse_method(cx, op, is_static, start, name); + } + } + let name = self.parse_class_element_name(cx)?; + if self.t.is_p("(") { + return self.parse_method(cx, Op::Method, is_static, start, name); + } + // A field. + let init = if self.t.is_p("=") { + self.bump()?; + self.parse_assign(Cx { + y: false, + a: false, + i: true, + r: false, + })? + } else { + self.absent() + }; + let end = self.semi()?; + Ok(self.mk( + K::FieldDefinition, + Op::None, + is_static, + start, + end, + vec![name, init], + )) + } +} + +/// The ASCII word `n` as a `&str` if it is short and ASCII, `""` otherwise. +fn word_of(n: &[u32]) -> &'static str { + const RESERVED: &[&str] = &[ + "break", + "case", + "catch", + "class", + "const", + "continue", + "debugger", + "default", + "delete", + "do", + "else", + "enum", + "export", + "extends", + "false", + "finally", + "for", + "function", + "if", + "import", + "in", + "instanceof", + "new", + "null", + "return", + "super", + "switch", + "this", + "throw", + "true", + "try", + "typeof", + "var", + "void", + "while", + "with", + ]; + for w in RESERVED { + if crate::token::name_is(n, w) { + return w; + } + } + "" +} diff --git a/crates/clazel-js-syntax/src/token.rs b/crates/clazel-js-syntax/src/token.rs new file mode 100644 index 000000000..5c18f48eb --- /dev/null +++ b/crates/clazel-js-syntax/src/token.rs @@ -0,0 +1,347 @@ +//! The parser's view of the lexer: one token at a time, with the goal symbol of +//! each token chosen by the parser (ECMA-262 §12: "the syntactic grammar +//! chooses the goal symbol"). Not proof code: the proven part of the parser is +//! [`crate::check`], which the arena the parser builds must pass. + +use clazel_js_lexer::api::{Next, Value}; +use clazel_js_lexer::exec::next_token; +use clazel_js_lexer::spec::{ElementKind, Goal, TemplateKind}; + +/// A token, with what the parser reads of it. +#[derive(Clone)] +pub struct Tok { + /// The lexer's kind of input element. + pub kind: ElementKind, + /// The index of its first code point. + pub start: usize, + /// One past its last code point. + pub end: usize, + /// A line terminator precedes it. + pub nl: bool, + /// The end of the source text (no token). + pub eof: bool, + /// For a punctuator, its text; for an `IdentifierName` written without + /// escapes, the word if it is one the parser knows (a reserved or + /// contextual word); `""` otherwise. + pub s: &'static str, + /// For an `IdentifierName` or `PrivateIdentifier`, its code points with + /// the escapes resolved. + pub name: Vec, + /// An `IdentifierName` or `PrivateIdentifier` with a `UnicodeEscapeSequence`. + pub escaped: bool, + /// A `NumericLiteral` that is a BigInt. + pub bigint: bool, + /// A `NumericLiteral` that is legacy octal or `NonOctalDecimalIntegerLiteral`. + pub legacy: bool, + /// A template component with an undefined TV. + pub invalid_escape: bool, +} + +/// The words the parser compares tokens with. +const WORDS: &[&str] = &[ + "await", + "break", + "case", + "catch", + "class", + "const", + "continue", + "debugger", + "default", + "delete", + "do", + "else", + "enum", + "export", + "extends", + "false", + "finally", + "for", + "function", + "if", + "import", + "in", + "instanceof", + "new", + "null", + "return", + "super", + "switch", + "this", + "throw", + "true", + "try", + "typeof", + "var", + "void", + "while", + "with", + "yield", + "let", + "static", + "async", + "of", + "get", + "set", + "from", + "as", + "target", + "meta", + "implements", + "interface", + "package", + "private", + "protected", + "public", + "eval", + "arguments", + "constructor", + "prototype", + "__proto__", +]; + +/// The punctuators. +const PUNCTS: &[&str] = &[ + "{", "(", ")", "[", "]", ".", "...", ";", ",", "<", ">", "<=", ">=", "==", "!=", "===", "!==", + "+", "-", "*", "%", "**", "++", "--", "<<", ">>", ">>>", "&", "|", "^", "!", "~", "&&", "||", + "??", "?", "?.", ":", "=", "+=", "-=", "*=", "%=", "**=", "<<=", ">>=", ">>>=", "&=", "|=", + "^=", "&&=", "||=", "??=", "=>", "/", "/=", "}", +]; + +/// The interned form of `text` among `table`, `""` if not there. +fn intern(text: &[u32], table: &'static [&'static str]) -> &'static str { + for w in table { + if name_is(text, w) { + return w; + } + } + "" +} + +/// Lexes the token at `pos` with `goal`; `None` is a lexical error. +pub fn lex(src: &[u32], pos: usize, goal: Goal, html: bool) -> Option { + let r = next_token(src, pos, goal, html).ok()?; + match r { + Next::Eof { newline_before } => Some(Tok { + kind: ElementKind::WhiteSpace, + start: src.len(), + end: src.len(), + nl: newline_before, + eof: true, + s: "", + name: Vec::new(), + escaped: false, + bigint: false, + legacy: false, + invalid_escape: false, + }), + Next::Token(t) => { + let el = t.element; + let text = src.get(el.start..el.end)?; + let mut tok = Tok { + kind: el.kind, + start: el.start, + end: el.end, + nl: t.newline_before, + eof: false, + s: "", + name: Vec::new(), + escaped: false, + bigint: false, + legacy: false, + invalid_escape: false, + }; + match el.kind { + ElementKind::Punctuator + | ElementKind::DivPunctuator + | ElementKind::RightBracePunctuator => tok.s = intern(text, PUNCTS), + ElementKind::IdentifierName => { + if let Value::Name { + code_points, + escaped, + } = el.value + { + tok.escaped = escaped; + if !escaped { + tok.s = intern(text, WORDS); + } + tok.name = code_points; + } + } + ElementKind::PrivateIdentifier => { + if let Value::Name { + code_points, + escaped, + } = el.value + { + tok.escaped = escaped; + tok.name = code_points; + } + } + ElementKind::NumericLiteral => { + if let Value::Numeric { bigint, legacy } = el.value { + tok.bigint = bigint; + tok.legacy = legacy; + } + } + ElementKind::Template(_) => { + if let Value::Template { cooked, .. } = el.value { + tok.invalid_escape = cooked.is_none(); + } + } + ElementKind::StringLiteral => { + if let Value::String { units } = el.value { + tok.name = units.iter().map(|u| u32::from(*u)).collect(); + } + tok.legacy = has_legacy_escape(text); + } + _ => {} + } + Some(tok) + } + } +} + +/// 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 { + 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) { + if *c == 0x5C { + let n = text.get(i.saturating_add(1)); + if n.is_some_and(|n| (0x31..=0x39).contains(n)) { + return true; + } + if n == Some(&0x30) && digit(text.get(i.saturating_add(2))) { + return true; + } + i = i.saturating_add(2); + } else { + i = i.saturating_add(1); + } + } + false +} + +/// The resolved name `n` is a `ReservedWord` other than `yield` and `await`. +pub fn name_is_reserved_always(n: &[u32]) -> bool { + is_reserved_always(intern(n, WORDS)) +} + +/// The token as the arena check ([`crate::arena_d`]) reads it. +pub fn dtok_of(t: &Tok) -> crate::arena_d::DTok { + use crate::spec::{TemplateKind as ST, TokKind as SK}; + let kind = match t.kind { + ElementKind::IdentifierName => SK::IdentifierName, + ElementKind::PrivateIdentifier => SK::PrivateIdentifier, + ElementKind::NumericLiteral => SK::NumericLiteral, + ElementKind::StringLiteral => SK::StringLiteral, + ElementKind::RegularExpressionLiteral => SK::RegularExpressionLiteral, + ElementKind::Template(TemplateKind::NoSubstitution) => SK::Template(ST::NoSubstitution), + ElementKind::Template(TemplateKind::Head) => SK::Template(ST::Head), + ElementKind::Template(TemplateKind::Middle) => SK::Template(ST::Middle), + ElementKind::Template(TemplateKind::Tail) => SK::Template(ST::Tail), + _ => SK::Punctuator, + }; + crate::arena_d::DTok { + kind, + start: t.start, + end: t.end, + nl: t.nl, + escaped: t.escaped, + } +} + +impl Tok { + /// The token is the punctuator `p`. + pub fn is_p(&self, p: &str) -> bool { + !self.eof && self.s == p && !matches!(self.kind, ElementKind::IdentifierName) + } + /// The token is the word `w` written without escapes. + pub fn is_w(&self, w: &str) -> bool { + !self.eof && self.s == w && matches!(self.kind, ElementKind::IdentifierName) + } + /// The token is an `IdentifierName`. + pub fn is_name(&self) -> bool { + !self.eof && matches!(self.kind, ElementKind::IdentifierName) + } + /// The token is a template component of kind `k`. + pub fn is_tpl(&self, k: TemplateKind) -> bool { + !self.eof && self.kind == ElementKind::Template(k) + } +} + +/// The words that cannot be an `Identifier` (§12.7.2), whatever the context: +/// `ReservedWord` except `yield` and `await`. +pub fn is_reserved_always(w: &str) -> bool { + matches!( + w, + "break" + | "case" + | "catch" + | "class" + | "const" + | "continue" + | "debugger" + | "default" + | "delete" + | "do" + | "else" + | "enum" + | "export" + | "extends" + | "false" + | "finally" + | "for" + | "function" + | "if" + | "import" + | "in" + | "instanceof" + | "new" + | "null" + | "return" + | "super" + | "switch" + | "this" + | "throw" + | "true" + | "try" + | "typeof" + | "var" + | "void" + | "while" + | "with" + ) +} + +/// The resolved name `n` is the word `w`. +pub fn name_is(n: &[u32], w: &str) -> bool { + w.len() == n.len() && w.chars().zip(n.iter()).all(|(c, t)| u32::from(c) == *t) +} + +/// The resolved name `n` is a `ReservedWord`, `yield` and `await` included. +pub fn name_is_reserved(n: &[u32]) -> bool { + let w = intern(n, WORDS); + is_reserved_always(w) || w == "yield" || w == "await" +} + +/// The resolved name `n` is a word that is reserved in strict mode code +/// (§13.1.1 and §12.7.2's list): `implements`, `interface`, `let`, `package`, +/// `private`, `protected`, `public`, `static`, `yield`. +pub fn name_is_strict_reserved(n: &[u32]) -> bool { + matches!( + intern(n, WORDS), + "implements" + | "interface" + | "let" + | "package" + | "private" + | "protected" + | "public" + | "static" + | "yield" + ) +} diff --git a/crates/clazel-js-syntax/tests/corpus.rs b/crates/clazel-js-syntax/tests/corpus.rs new file mode 100644 index 000000000..aaeb2f013 --- /dev/null +++ b/crates/clazel-js-syntax/tests/corpus.rs @@ -0,0 +1,782 @@ +//! A corpus of small programs: what the parser must accept and reject. The +//! vendored Test262 has few positive tests of the syntax (its `expressions/` and +//! `statements/` directories are not vendored), so the grammar's coverage is +//! here. + +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; + +fn cps(s: &str) -> Vec { + s.chars().map(u32::from).collect() +} + +fn accepted(s: &str, goal: SourceGoal) -> bool { + parse(&cps(s), goal).is_ok() +} + +const SCRIPT_OK: &[&str] = &[ + "#!/usr/bin/env node\nx", + " also a comment", + "a = b ? c : d ? e : f", + "a = b ? (c, d) : e", + "x = a ? b => c : d => e", + "x = function () {}; y = function* () { yield; yield 1; yield* g(); }", + "async function f() { await x; for await (const a of b) ; }", + "async function* g() { yield await 1; }", + "var o = { a, b, c: 1, 'd': 2, 3: 4, [e]: 5, get f() { return 1 }, set f(v) {}, g() {}, *h() {}, async i() {}, async *j() {}, ...k }", + "var { a, b: { c = 1 }, ...d } = e; var [f, , g = 2, ...[h]] = i;", + "[a, b] = [b, a]; ({ a, b } = c); [{ a }] = b; ({ a: [b] } = c);", + "[a.b, c[d]] = e; ({ x: a.b } = c)", + "for ({ a } of b) ; for ([a] in b) ; for (a.b of c) ; for (var [a] of b) ;", + "for (let i = 0, j = 1; i < j; i++, j--) ;", + "for (var i in o) if (i) ; else ;", + "for (let of of x) ;", + "for (async of => {}; ; ) break;", + "for (let in x) ;", + "do x; while (y) z", + "do { } while (0); y", + "a\n++\nb", + "a = b\n(c)", + "a = b\n/c/g", + "x = a / b / c; y = /=/.test(z); w = a /= 2", + "if (a) /re/.test(b)", + "x = `a${`b${c}d`}e`", + "x = `${a}${b}`", + "x = {} / 1", + "label: { break label; }", + "a: b: c: while (1) { continue a; }", + "a: function f() {}", + "if (a) function f() {} else function g() {}", + "switch (a) { case 1: case 2: let x; break; default: }", + "try { a } catch { b }", + "try { a } catch (e) { b } finally { c }", + "try { } catch ([a, b]) { }", + "with (a) b", + "new a; new a(); new a.b.c; new (a())(); new new a()(); new a`x`", + "function f() { new.target }", + "a?.b.c?.[d]?.(e).f", + "a ?? b ?? c; a ?? (b || c); (a ?? b) || c", + "a ||= b; a &&= b; a ??= b; a **= b; a >>>= 1", + "a ** b ** c; (-a) ** b; a ** -b", + "typeof a === 'x'; void 0; delete a.b; !a; ~a; +a; -a", + "a++; a--; ++a; --a; a\n++b", + "x = a in b; for (var i = (a in b); ;) break;", + "class A { }; class B extends A { }; class C extends (a, b) { }", + "class A { static a; static b = 1; c; d = 2; 'e' = 3; 4 = 5; [f] = 6; #g = 7; static #h; }", + "class A { a() {} static b() {} get c() {} set c(v) {} static get d() {} *e() {} async f() {} async *g() {} static async h() {} #i() {} get #j() { return 1 } set #j(v) {} }", + "class A { static static() {} static get() {} get get() {} set set(v) {} async async() {} static async *[x]() {} }", + "class A { static { this.x = 1; } static { } }", + "class A { constructor() { super.x; } m() { super.y; super[z]; } }", + "class A extends B { constructor() { super(); } }", + "class A { #x; m() { return #x in this; } n() { return this.#x; } }", + "class A { static async\n m() {} }", + "class A { get\n x() {} }", + "(class {}); (class A {}); (class extends B {})", + "(a, b) => { return a }; (a = 1, [b], {c}, ...d) => 0; () => ({}); a => b => c", + "async (a, b) => a; async a => a; async () => {}; async(a, b); async\n(a)", + "(async function() {}); (async function f() {}); (async function*() {})", + "x = async function () { await 1 }; x = async () => await 1", + "f(a, ...b, c,); f(...a)", + "[...a, b]; [a, , ]; [, ]; [,,a]; [a,,b]", + "({ get: 1, set: 2, async: 3, static: 4 }); ({ get, set, async })", + "({ async, await: 1, yield: 2 })", + "({ if: 1, class: 2, function: 3 }); a.if; a.class", + "({ __proto__: 1, ['__proto__']: 2, __proto__() {} })", + "var yield, await, async, let, of, from, as, get, set, static;", + "yield: 1; await: 2", + "let\nx", + "let = 1; let\n= 2", + "var let; var of; for (let;;) break;", + "async\nfunction f() {}", + "'use strict'; var a", + "function f() { 'use strict'; }", + "var a = 1, b = 2\nvar c", + "0, 1.5, .5, 5., 0x1f, 0o7, 0b1, 1e3, 1_000, 10n, 0xFn", + "'a\\n'; \"b\\u{1F600}\"; 'c\\\nd'", + "012; 08; 09.5; '\\07'", + "/[/]/; /a\\/b/; /a/gimsuyd; /(?a)\\k/", + "a = b\n/ c / d", + "{ }", + "if (a) { } else if (b) { } else { }", + "function f(a, a) {}", + "function f() { var a; var a; function a() {} }", + "{ function f() {} function f() {} }", + "var a; { let a; }", + "x = { a() { super.b; }, c: function () {} }", + "async () => { await 1 }; async function f(a = 1) {}", + "function* g(a = 1) { yield a }", + "var f = function yield() {}; var g = function await() {}", + "a = function* () { yield\n1 }", + "a = function* () { yield /re/ }", + "a = function* () { x = yield; y = [yield, yield] }", + "a = function* () { (yield) ; yield * 2; yield yield 1 }", + "x = a => ({}) ; y = a => { }\n/b/g", + "(a) => 1", + "a = (b, c)", + "a = (b)", + "(a, b) => a, c", + "(((a)))", + "((a)) = 1", + "((a.b)) = 1; (a.b) += 1; (a)++", + "[(a)] = b; [(a.b)] = c; ({ x: (a) } = b)", + "import('x'); import('x', { with: {} })", + "x = import('a').then(b)", + "a = { 'b': 1, 0: 2, 1n: 3 }", + "while (a) b; while (a) { }", + "debugger; debugger\n", + "throw a\n", + "if (a)\n b\nelse\n c", + "var a = function () { return\n1 }", + "for (;;) { break }", + "for (var a = 1 in {}) ;", + "a = b ? c : d, e", + "tag`a${1}b`; tag`\\unicode`", + "x = a\n`b`", + "if (1) ;\nelse ;", + "async function f() { for await (x of y) ; for await (var x of y) ; }", + "label: for (;;) { label2: for (;;) { continue label; } }", + "function f() { return /a/ }", + "a = typeof /a/", + "x = {\n} / 2", + "a = b => c ? d : e", + "let async; async\n[0]", + "var {} = a; var [] = b; ({} = a); [] = b", + "a = { b: c = 1 } = d", + "new Foo\n(1)", + "var a = b\n++c", + "(function () {})()\n(function () {})()", + "!function () {}()", + "a = b || c && d | e ^ f & g == h < i << j + k * l ** m", + "a instanceof b in c", + "async function f() { class C { static { } } }", + "class A { static { async () => { await 0 }; async function f() { for await (x of y); } } }", + "class C { static async *#m() {} static accessor }", + "x = class { static x = 1; y = () => this }", + "x = { f() { return () => super.g } }", + "function f() { return function() { return new.target } }", + "a = 1 // c\n/* d */ b = 2", + "a /* \n */ ++b", + "\u{FEFF}a\u{2028}b\u{2029}c", + "x = { async *gen() {}, get [k]() {}, set [k](v) {}, async [k]() {}, 'quoted'() {}, 1() {}, 1n() {} }", + "for (let [a, b] = c; ; ) break; for (const {a} of b) ; for (let {a = 1} = {}; ; ) break;", + "({ a: b.c } = d); [a, ...b.c] = d; [...[a]] = b; ({ ...a.b } = c); [a = 1, [b] = [2]] = c", + "var { [a]: b } = c; var { 'x': y, 1: z } = d; var [ , , e] = f", + "class A { [a]() {} static [b] = 1; static async *[c]() {} 'd'() {} 5() {} }", + "function f() { return\n }", + "a = b ? c : d => e", + "async function f() { (await a) ** b; await a; await (a, b); await a.b(c) }", + "async function f() { for await (x of y) ; for await (var x of y) ; for await (let [x] of y) ; }", + "function* g() { yield a ? b : c; x = yield; yield (yield); yield [a]; yield {}; yield /re/; yield `t`; }", + "function* g() { yield\n/re/ }", + "a = b\n++c", + "x\n/y/g", + "var a = 1\n/2/3", + "x = a ? /re/ : /re2/", + "x = /[\\]/]/ ; y = /=/", + "if (a) { } /re/.test(b)", + "x = typeof /a/ ; y = void /b/ ; z = [/c/] ; w = {a: /d/}; f(/e/); u = a, /f/", + "x = {}\n/a/g", + "x = function f() { return /a/ }", + "a = `${/re/.test(b)}`", + "a = `${{a: 1}.a}`", + "a = `${`${`${b}`}`}`", + "'use strict'; 'a'; a", + "function f() { 'use asm'; 'use strict'; with_ = 1 }", + "label: { label2: { break label; } }", + "a: b: c: ;", + "if (a) b; else if (c) d; else e", + "switch (a) { case b: { } case c: ; default: ; }", + "switch (a) { default: case b: }", + "try { } catch (e) { var e }", + "try { } catch (e) { { let e } }", + "try { } catch ({ a, b = 1, ...c }) { }", + "while (a) { if (b) continue; else break }", + "do a; while (b); do { } while (c)", + "for (;;) ; for (a;;) ; for (;a;) ; for (;;a) ; for (a, b; c, d; e, f) ;", + "for (var a, b = 1; ; ) break; for (var [a] = b; ; ) break;", + "for (let;;) break", + "for (var a in b, c) ;", + "for (a in b) for (c of d) ;", + "x = y = z = 1", + "x = a ? b ? c : d : e", + "a = b => c, d", + "a = (b => c)", + "a = (b, c) => d", + "a = async (b, c) => d", + "a = async b => c, d", + "a = ({ b }) => c; a = ([b]) => c; a = ({ b = 1 }) => c; a = ([b = 1]) => c", + "a = ({ b: [c] }) => d; a = ({ ...b }) => c; a = ([...b]) => c", + "a = (b = 1, { c } = {}) => d", + "f(async () => {}, async x => x, async function () {})", + "f(a => a, b => ({}), c => [], (d) => d)", + "new (a.b()); new (a().b)(); new a.b.c(); new (a)(); new (a, b)()", + "a.b.c.d(e).f[g](h)`i`.j", + "(a || b).c; (a ?? b).c; (a, b).c; (a = b).c; (a ? b : c).d", + "a?.[0]; a?.(); a?.b?.c; a?.b(); a?.[b]?.(c)", + "delete a.b; delete a[b]; delete a; void 0; typeof a; typeof typeof a", + "a ? b : c ? d : e", + "!a, ~b, +c, -d, ++e, --f, g++, h--", + "a in b; a instanceof b; a < b; a <= b; a > b; a >= b; a == b; a != b; a === b; a !== b", + "a << b; a >> b; a >>> b; a & b; a | b; a ^ b; a && b; a || b; a ?? b", + "a *= b; a /= b; a %= b; a += b; a -= b; a <<= b; a >>= b; a >>>= b; a &= b; a |= b; a ^= b; a **= b", + "x = 1 + +1 - -1; y = a+++b; z = a---b; w = a- --b; v = a + ++b", + "class A { static async x() {} }", + "class A { static get; static set; static async; static static; get\n; }", + "class A { x; }", + "(class A { x; y\n z; w = 1\n v = 2 })", + "class A { static x = 1\n static y = 2\n static { } }", + "class A { a = 1; b = () => this; c = function () { return this } }", + "class A { static #a = 1; static m() { return A.#a } }", + "class A { #a() {} get #b() {} set #b(v) {} static async *#c() {} m() { this.#a(); this.#b; this.#c; #a in this } }", + "class A extends B { x = super.y; static z = super.w; constructor() { super(); super.m(); } }", + "x = { m() { super.a; }, get n() { return super.b }, set o(v) { super.c = v } }", + "async () => { await 1 }; async () => await 1; async () => { for await (a of b) ; }", + "function f() { 'use strict'; return function g() { return this } }", + "var \\u0061bc = 1; var ab\\u{63} = 2; a\\u0062c", + "var a", + "var ℮ = 1; var a\u{b7}b = 2", +]; + +const SCRIPT_BAD: &[&str] = &[ + "a b", + "var 1", + "if (a) else", + "1 +", + "function () {}", + "class {}", + "let let = 1", + "const a;", + "const [a]", + "var [a]", + "for (let a of b, c) ;", + "for (var a, b of c) ;", + "for (var a = 1 of b) ;", + "for (let a = 1 in b) ;", + "for (a + b of c) ;", + "for (async of x) ;", + "for (let of x) ;", + "for (let.x of y) ;", + "for await (x of y) ;", + "({ a = 1 })", + "({ a: 1 } = 1)", + "({ ...a, b } = c)", + "({ ...{a} } = c)", + "[...a, b] = c", + "[...a,] = c", + "[a += 1] = c", + "(a, b) = 1", + "(a = 1) = 2", + "a + b = 1", + "a++ = 1", + "++a++", + "1++", + "a?.b = 1", + "a?.b++", + "new a?.b", + "a?.`x`", + "a?.b`x`", + "a ?? b || c", + "a || b ?? c", + "a && b ?? c", + "-a ** b", + "typeof a ** b", + "(a, b) => { }()", + "(a, b)\n=> 1", + "async (a, b)\n=> 1", + "(...a, b) => 1", + "(a,) ", + "() ", + "(...a)", + "(a, ...b) + 1", + "(a.b) => 1", + "([a.b]) => 1", + "(1) => 1", + "x => { } +", + "class A { constructor() {} constructor() {} }", + "class A { get constructor() {} }", + "class A { static prototype() {} }", + "class A { constructor }", + "class A { #a; #a; }", + "class A { m() { this.#b } }", + "this.#a", + "class A { constructor() { super(); } }", + "class A extends B { m() { super(); } }", + "function f() { super.x }", + "super.x", + "new.target", + "import.meta", + "return", + "break", + "continue", + "a: a: ;", + "a: { continue a; }", + "while (1) { break b; }", + "function f() { break; }", + "if (a) class C {}", + "while (a) let x", + "while (a) function f() {}", + "if (a) async function f() {}", + "a: const x = 1", + "a: function* g() {}", + "{ let a; let a; }", + "{ let a; var a; }", + "{ var a; let a; }", + "let a; var a;", + "function f(a) { let a; }", + "{ function f() {} let f; }", + "{ async function f() {} async function f() {} }", + "try { } catch (e) { let e; }", + "try { } catch ([e]) { var e; }", + "switch (a) { case 1: let x; case 2: let x; }", + "switch (a) { default: default: }", + "try { }", + "'use strict'; with (a) b", + "'use strict'; var yield", + "'use strict'; var let", + "'use strict'; var static", + "'use strict'; eval = 1", + "'use strict'; arguments++", + "'use strict'; var eval", + "'use strict'; delete a", + "'use strict'; 010", + "'use strict'; '\\07'", + "'use strict'; 08", + "'use strict'; function f(a, a) {}", + "'use strict'; if (a) function f() {}", + "'use strict'; a: function f() {}", + "function f(a, a) { 'use strict' }", + "function f(a = 1) { 'use strict' }", + "function f({a}) { 'use strict' }", + "(a, a) => 1", + "function* g() { var yield }", + "function* g(a = yield) {}", + "async function f(a = await 1) {}", + "async function f() { var await }", + "async (a = await 1) => 1", + "function* g() { yield => 1 }", + "function* g() { yield\n* 1 }", + "async function f() { await }", + "var a = { get a(x) {} }", + "var a = { set a() {} }", + "var a = { set a(...x) {} }", + "var a = { __proto__: 1, __proto__: 2 }", + "var a = { __proto__: 1, '__proto__': 2 }", + "'use strict'; delete ((a))", + "a\n=> 1", + "var enum", + "var if", + "var a\\u0020b", + "v\\u0061r a", + "({ v\\u0061r })", + "a = { if }", + "`${}`", + "`${a", + "`\\u{`", + "x = `\\01`", + "'\\u{110000}'", + "1_", + "1__0", + "0_1", + "5in x", + "@", + "var a = #b", + "#x in y", + "class A { #x; m() { #x } }", + "class A { #x; m() { 1 + #x in y } }", + "class A { static { await } }", + "async function f() { class C { static { await 0 } } }", + "async function f() { class C { static { for await (x of y); } } }", + "class A { static { (x = await 0) => x } }", + "class A { static { return } }", + "class A { x = arguments }", + "class A { static { arguments } }", + "class A { x = () => arguments }", + "label: { label: ; }", + "import a from 'b'", + "export var a", + "do x while (y)", + "if (a) b else c", + "for (;;", + "for (var a of) ;", + "new import('x')", + "import()", + "import(a, b, c)", + "x = {a:1, ...}", + "x = [..., a]", + "f(...)", + "a ? b", + "a ? b :", + "function f() { 'use strict'; with (a) b }", + "function f() { 'use strict'; 010 }", + "let [a, a] = b", + "for (let [a, a] of b) ;", + "for (let a of b) { var a }", + "for (let let of b) ;", + "for (let a = 1; ; ) { var a }", + "const a = 1, a = 2", + "for (x of y, z) ;", + "for (var x of y, z) ;", + "for (let x, y of z) ;", + "for (let x = 1 of y) ;", + "for ((a, b) of c) ;", + "for ([a.b()] of c) ;", + "for (a of b of c) ;", + "for (a in b of c) ;", + "for (const x in y) { var x }", + "for (let x in y) { var x }", + "async function f() { await a ** b }", + "async function f() { function g() { await 1 } }", + "function f() { await 1 }", + "function* g() { function h() { yield 1 } }", + "function f() { yield 1 }", + "async () => { var await }", + "x = { async get a() {} }", + "x = { get *a() {} }", + "x = { *get a() {} }", + "x = { async *a: 1 }", + "x = { a: 1, , }", + "x = { 1 }", + "x = { 'a' }", + "x = { [a] }", + "x = { get a }", + "class A { static static static() {} }", + "class A { async *get x() {} }", + "class A { x y }", + "class A { x = 1 y = 2 }", + "class A { static async *#constructor() {} }", + "class A { #constructor }", + "class A { static #a; static #a }", + "class A { get #a() {} get #a() {} }", + "class A { get #a() {} static set #a(v) {} }", + "class A { static { var await } }", + "class A { static { var yield } }", + "class A extends B, C {}", + "class A { async constructor() {} }", + "class A { *constructor() {} }", + "class A { set constructor(v) {} }", + "(a, b) => { 'use strict'; }; function f(a = 1) { 'use strict'; }", + "(a = 1) => { 'use strict' }", + "'use strict'; (eval) => 1", + "'use strict'; (arguments) => 1", + "'use strict'; function eval() {}", + "'use strict'; function f(eval) {}", + "'use strict'; try { } catch (eval) { }", + "'use strict'; ({ eval } = a)", + "'use strict'; [arguments] = a", + "'use strict'; for (eval of a) ;", + "'use strict'; implements = 1", + "'use strict'; var public", + "'use strict'; ({ public })", + "'use strict'; yield = 1", + "class A { m() { var yield } }", + "class A { m() { var let } }", + "class A extends (function() { with (a) b }) {}", + "x = () => { super.a }", + "function f() { super() }", + "function f() { class A extends B { constructor() { function g() { super() } } } }", + "function f() { class A { constructor() { super() } } }", + "({ m() { super() } })", + "a: while (1) { function f() { break a } }", + "a: while (1) { (() => { break a })() }", + "while (1) { function f() { break } }", + "while (1) { (() => { continue })() }", + "a: { while (1) { continue a } }", + "a: if (1) { continue a }", + "switch (a) { case 1: continue }", + "if (a) { break }", + "x = a ? b : ", + "x = a ? : b", + "x = (a, )", + "x = (, a)", + "x = [a b]", + "x = f(a b)", + "x = {a b}", + "x = `a${b c}`", + "x = a.", + "x = a.1", + "x = a?.[", + "x = new", + "x = new.foo", + "x = import.foo", + "x = import", + "x = super", + "x = super.", + "var a = b c", + "var a, ", + "var a,, b", + "let a, ", + "let a b", + "let let", + "let [let] = a", + "const let = 1", + "let x; { var x }", + "{ let x; { var x } }", + "{ let x; var x }", + "function f() { let x; var x }", + "function f(x) { let x }", + "(x) => { let x }", + "x => { let x }", + "async x => { let x }", + "({ m(x) { let x } })", + "class A { m(x) { let x } }", + "function f() { const x = 1; function x() {} }", + "{ function f() {} var f }", + "{ var f; function f() {} }", + "{ class A {} var A }", + "{ class A {} class A {} }", + "{ class A {} function A() {} }", + "class A { static { let a; var a } }", + "class A { static { function f() {} let f } }", + "if (a) label: function f() {}", + "while (a) label: function f() {}", + "label: label2: function* g() {}", + "label: async function f() {}", + "for (;;) label: function f() {}", + "do function f() {} while (a)", + "with (a) function f() {}", + "if (a) let [a] = b", + "do ; while (a ,", + "var [a, a] = b; let [c, c] = d", + "function f([a, a]) {}", + "function f({ a, b: a }) {}", + "function f(a, ...a) {}", + "(a, ...a) => 1", + "try { } catch (a) { let a }", + "try { } catch (a) { let a }", + "try { } catch ({ a, a }) { }", + "try { } catch (a, b) { }", + "try { } catch () { }", + "try catch (a) { }", + "x = a\n=> b", + "x = async a\n=> b", + "x = async\n(a) => b", + "async (a, b) => c d", + "(async function* () { yield await })", + "async function f() { yield await 1 }", + "async function* g() { yield* await }", + "async function f(await) {}", + "async function f() { (await) => 1 }", + "async function f() { (a = await 1) => 1 }", + "async function* g(a = yield) {}", + "function* g(a = yield) {}", + "function* g() { (a = yield) => 1 }", + "function* g() { yield => 1 }", + "function* g() { ({ yield } = a) }", + "function* g() { var o = { yield } }", + "x = { yield } = a; function* g() { ({ yield: yield } = a) }", + "a ?? b || c", + "a ?? b && c", + "a || b ?? c", + "a?.b = c", + "a?.b++", + "++a?.b", + "delete a?.#b", + "a?.b`c`", + "a`b`?.c`d`", + "new a?.()", + "import.meta = 1", + "new.target = 1", + "this = 1", + "null = 1", + "'use strict'; a() = 1", + "'use strict'; a()++", + "'use strict'; for (a() in b) ;", + "'use strict'; for (a() of b) ;", + "(a = 1) += 2", + "(a, b) += 1", + "a + b++ = 1", + "[a] += 1", + "({a}) = 1", + "([a]) = 1", + "({a} += 1)", + "(a) => { } = 1", + "async () => {} = 1", + "a => b = 1 = 2", + "1 = a", + "'a' = 1", + "`a` = 1", + "/a/ = 1", + "a ? b : c = d = ", + "typeof a = 1", + "!a = 1", + "-a = 1", + "{ a } = 1", +]; + +const MODULE_OK: &[&str] = &[ + "var a, b; export { a, b as c }; export { a as d }", + "import { a } from 'm'; export { a }", + "let a; export { a as 'x' , a as 'y' }", + "var a; export { a as '\\uD83D\\uDE00' }", + "export { a } from 'm'; export { a as b } from 'n'", + "export default function f() {} export { f as g }", + "export * from 'm'; export * from 'n'", + "class C {} export { C }", + "import {a} from 'm'", + "import {a, b as c} from 'm'", + "import d, {a} from 'm'", + "import a from 'm'", + "import * as a from 'm'", + "import { a } from 'm'", + "import { a as b, c, 'd' as e } from 'm'", + "import a, * as b from 'm'", + "import a, { b } from 'm'", + "import 'm'", + "import { } from 'm'", + "import { a, } from 'm'", + "import a from 'm' with { type: 'json' }", + "import {default as a} from 'm'", + "export var a = 1, b", + "export let a; export const b = 1", + "export function f() {}", + "export function* g() {}", + "export async function h() {}", + "export class C {}", + "export default function () {}", + "export default function f() {}", + "export default function* () {}", + "export default async function () {}", + "export default class {}", + "export default class C {}", + "export default 1 + 2", + "export default (function () {})", + "export default async () => {}", + "export default a = 1;", + "var a; export { a }", + "var a, b; export { a as b, b as a }", + "var a; export { a as 'x y' }", + "export { a } from 'm'", + "export { a as b, c as 'd' } from 'm'", + "export { } from 'm'", + "export * from 'm'", + "export * as a from 'm'", + "export * as 'a b' from 'm'", + "export * from 'm' with { type: 'json' }", + "var default_; export { default_ as default }", + "export { default } from 'm'", + "export { default as a } from 'm'", + "await 1", + "await x; async function f() { await 1 }", + "for await (a of b) ;", + "x = import.meta", + "x = import('a')", + "function f() { return await_ }", + "class A { m() { 'strict by default' } }", + "export default a", +]; + +const MODULE_BAD: &[&str] = &[ + "var a; export { a }; export { a }", + "var a, b; export { a, b as a }", + "export var a; export { a }", + "export default 1; export default 2", + "var a; export { a as default }; export default 1", + "export { a }", + "export { a as b }", + "export * as a from 'm'; export * as a from 'n'", + "export { 'a' as b }; var a", + "var a; export { a as '\\uD800' }", + "export * as '\\uDC00' from 'm'", + "import { '\\uD800' as a } from 'm'", + "export { a as b } from 'm'; export { c as b } from 'n'", + "var await", + "function await() {}", + "await: 1", + "a = await", + "with (a) b", + "010", + "'\\01'", + "var yield", + "var static", + "function f(a, a) {}", + "export { 'a' }", + "export default a; var", + "import a from 'm' import", + "import a", + "import { a b } from 'm'", + "import { 'a' } from 'm'", + "import { if } from 'm'", + "import { if as a } from 'm'; import { a as if } from 'm'", + "import * from 'm'", + "import a, from 'm'", + "import { a } 'm'", + "export", + "export {", + "export { a as } ", + "export { if }", + "export { a as b", + "export * as from 'm'", + "export default", + "export default var a", + "if (a) { export var b }", + "function f() { import a from 'm' }", + "{ import a from 'm' }", + "let a; let a;", + "var a; let a;", + "function f() {} function f() {}", + "async () => await", + " comment +x", +]; + +#[test] +fn scripts_that_parse() { + let mut failed = Vec::new(); + for s in SCRIPT_OK { + if !accepted(s, SourceGoal::Script) { + failed.push(*s); + } + } + assert!( + failed.is_empty(), + "should parse as a Script:\n{}", + failed.join("\n---\n") + ); +} + +#[test] +fn scripts_that_do_not_parse() { + let mut failed = Vec::new(); + for s in SCRIPT_BAD { + if accepted(s, SourceGoal::Script) { + failed.push(*s); + } + } + assert!( + failed.is_empty(), + "should not parse as a Script:\n{}", + failed.join("\n---\n") + ); +} + +#[test] +fn modules_that_parse() { + let mut failed = Vec::new(); + for s in MODULE_OK { + if !accepted(s, SourceGoal::Module) { + failed.push(*s); + } + } + assert!( + failed.is_empty(), + "should parse as a Module:\n{}", + failed.join("\n---\n") + ); +} + +#[test] +fn modules_that_do_not_parse() { + let mut failed = Vec::new(); + for s in MODULE_BAD { + if accepted(s, SourceGoal::Module) { + failed.push(*s); + } + } + assert!( + failed.is_empty(), + "should not parse as a Module:\n{}", + failed.join("\n---\n") + ); +} diff --git a/crates/clazel-js-syntax/tests/deep.rs b/crates/clazel-js-syntax/tests/deep.rs new file mode 100644 index 000000000..c53c95a53 --- /dev/null +++ b/crates/clazel-js-syntax/tests/deep.rs @@ -0,0 +1,73 @@ +//! Totality on hostile input: deeply nested and very long sources are +//! rejected (or accepted) without overflowing the stack, and a source that +//! nests more than the limit is [`ParseError::TooDeep`]. + +use clazel_js_syntax::api::ParseError; +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; + +fn run(s: &str) -> Result<(), ParseError> { + let src: Vec = s.chars().map(u32::from).collect(); + parse(&src, SourceGoal::Script).map(|_| ()) +} + +// Each case is 100 000 levels; under Miri one 2 000-token chain already takes +// minutes, and this test ran the CI runner out until it was shut down. +// `a_chain_past_the_limit_is_too_deep` covers the `TooDeep` path under Miri. +#[test] +#[cfg_attr(miri, ignore = "each case takes minutes under Miri")] +fn deeply_nested_is_rejected_not_a_crash() { + let n = 100_000; + let cases: Vec = vec![ + "(".repeat(n), + "[".repeat(n), + "{".repeat(n), + format!("{}a", "!".repeat(n)), + format!("{}a", "- ".repeat(n)), + format!("{}a", "a+".repeat(n)), + format!("{}a", "a.".repeat(n)), + format!("a{}", "(a)".repeat(n)), + format!("{}a", "a=".repeat(n)), + format!("{}a", "a?b:".repeat(n)), + format!("{}x", "x=>".repeat(n)), + format!("{}a", "new ".repeat(n)), + format!("{}b", "if(a)".repeat(n)), + format!("{}a", "a**".repeat(n)), + format!("{}1", "({a:".repeat(n)), + format!("{}1", "[".repeat(n)), + "function f(){".repeat(n), + "x=function(){".repeat(n), + "class A{m(){".repeat(n), + "async()=>{".repeat(n), + "`${".repeat(n), + "label:".repeat(n), + "var [".repeat(n), + "for(;;)".repeat(n), + "try{".repeat(n), + ]; + for (i, c) in cases.iter().enumerate() { + let r = run(c); + assert!(r.is_err(), "case {i} was accepted"); + } +} + +#[test] +fn a_chain_past_the_limit_is_too_deep() { + // Left associative: a loop, no recursion, but 1025 nodes high. + let s = format!("a{}", "+a".repeat(1100)); + assert!(matches!(run(&s), Err(ParseError::TooDeep))); + let s = format!("a{}", "+a".repeat(1000)); + assert!(run(&s).is_ok()); +} + +// Sources of 600 000 to 900 000 characters: hours under Miri. +#[test] +#[cfg_attr(miri, ignore = "each case takes minutes under Miri")] +fn long_flat_programs_parse() { + let s = "a;\n".repeat(200_000); + assert!(run(&s).is_ok()); + let s = format!("[{}]", "1,".repeat(300_000)); + assert!(run(&s).is_ok()); + let s = format!("f({})", "a,".repeat(300_000)); + assert!(run(&s).is_ok()); +} diff --git a/crates/clazel-js-syntax/tests/derivation.rs b/crates/clazel-js-syntax/tests/derivation.rs new file mode 100644 index 000000000..fa20d5535 --- /dev/null +++ b/crates/clazel-js-syntax/tests/derivation.rs @@ -0,0 +1,160 @@ +//! The proven check of the whole arena against the tokens +//! ([`clazel_js_syntax::arena_d`]): which programs it covers, that it never +//! finds a covered node the parser built wrongly, and what it costs. + +use clazel_js_syntax::arena_d::Derivation; +use clazel_js_syntax::exec::parse_checked; +use clazel_js_syntax::spec::SourceGoal; +use std::time::Instant; + +fn cps(s: &str) -> Vec { + s.chars().map(u32::from).collect() +} + +fn derivation(s: &str, goal: SourceGoal) -> Option { + parse_checked(&cps(s), goal).ok().map(|(_, d)| d) +} + +#[test] +fn covered_programs_are_verified() { + for s in [ + "", + ";", + ";;", + "1;", + "1\n2\n3", + "1 + 2 * 3;", + "(1 + 2) * 3\n4 < 5 > 6", + "'a'; ; /re/;", + "((1));", + "1 - 2 - 3 / 4 % 5;", + "2 ** 3 ** 2;", + "1 << 2 >> 3 >>> 4;", + "1 == 2 != 3 === 4 !== 5;", + "1 & 2 ^ 3 | 4;", + "1 && 2 || 3 && 4;", + "1 ?? 2 ?? 3;", + "1 <= 2 >= 3 < 4;", + "1 in 2 instanceof 3;", + "-1; !2; ~3; +4; typeof 5; void 6; delete (7).a;", + "- -1 + +2;", + "1 ? 2 : 3 ? 4 : 5;", + "(1).a;", + "(1).a.b.c;", + "(1).a = 2;", + "(1).a += 2 ** 3;", + "(1).a &&= (1).b ||= 3;", + "a;", + "{}", + "{ a; b = 1; { ; } }", + "if (a) b; else c;", + "if (a) { var x = 1; } else if (b) ; else { let y = 2 }", + "if (a) b +else c", + "var a = 1, b;", + "let x = a + 1; +const y = f(2), z = (3);", + "var a +var b = 2 +var c;", + "f(1);", + "f(a, ...b,);", + "f();", + "(1, 2, 3);", + "a.b(c)(d, e);", + "this; null; true; false;", + "f(g(h(1)), 2) + (a, b);", + "a + b * c;", + "a.b.c = d ? e : f;", + "typeof a === 'x';", + "function f() {}", + "function f(a, b) { return a + b; }", + "function f(a, b = 1, ...c) { return; }", + "function f(a,) { return\nx }", + "(function () { return 1 })();", + "(function g(a) { return g })", + "function* g() { yield; yield 1; yield * 2; }", + "async function h() { await 1; }", + "async function* k() { yield await 1; }", + "(a => a + 1);", + "(() => { return 1 });", + "((a, b) => a);", + "(async a => await a);", + "(async (a) => 1);", + "function f() { function g() { return 1 } return g }", + "while (a) b;", + "while (a) { b; c }", + "do a; while (b)", + "do { a } while (b);", + "do a; while (b) c;", + "throw a;", + "throw a\nx", + "debugger;", + "debugger\nx", + "a: b;", + "a: b: c;", + "a: while (x) { break a; continue a; break; continue; }", + "while (1) { break\nx }", + "try { a } catch (e) { b }", + "try { a } catch { b }", + "try { a } finally { b }", + "try { a } catch (e) { b } finally { c }", + "switch (a) {}", + "switch (a) { case 1: b; c; case 2: default: d; }", + "switch (a) { default: case 1: }", + "a in b;", + "// c\n 1 /* d */ + 2 ;;", + ] { + assert!( + derivation(s, SourceGoal::Script) == Some(Derivation::Verified), + "{s}" + ); + assert!( + derivation(s, SourceGoal::Module) == Some(Derivation::Verified), + "{s}" + ); + } +} + +#[test] +fn other_programs_are_unsupported_not_failed() { + for s in ["for (;;) ;", "var [a] = 1;", "[1];", "({});"] { + assert!( + derivation(s, SourceGoal::Script) == Some(Derivation::Unsupported), + "{s}" + ); + } +} + +#[test] +fn errors_are_still_errors() { + for s in ["1 +", "(", "1 2", ")"] { + assert!(derivation(s, SourceGoal::Script).is_none(), "{s}"); + } +} + +/// The cost of the check: the same program of covered nodes, parsed with the +/// check running to the end and with an uncovered node first, which stops it at +/// once (the parse and the other checks of `parse` are the same). +#[test] +#[cfg_attr(miri, ignore = "times two parses of 1.7 million characters")] +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 time = |src: &[u32]| { + let t = Instant::now(); + let r = parse_checked(src, SourceGoal::Script); + (t.elapsed(), r.map(|(_, d)| d).ok()) + }; + let (_, d0) = time(&covered); + assert!(d0 == Some(Derivation::Verified)); + let (_, d1) = time(&stopped); + assert!(d1 == Some(Derivation::Unsupported)); + let best = |src: &[u32]| (0..3).map(|_| time(src).0).min().unwrap_or_default(); + let with = best(&covered); + let without = best(&stopped); + println!( + "covered, checked to the end: {with:?}; same size, check stopped at once: {without:?}" + ); +} diff --git a/crates/clazel-js-syntax/tests/expected_failures.txt b/crates/clazel-js-syntax/tests/expected_failures.txt new file mode 100644 index 000000000..ca6968d21 --- /dev/null +++ b/crates/clazel-js-syntax/tests/expected_failures.txt @@ -0,0 +1,167 @@ +language/literals/regexp/early-err-arithmetic-modifiers-add-remove-i.js +language/literals/regexp/early-err-arithmetic-modifiers-add-remove-m.js +language/literals/regexp/early-err-arithmetic-modifiers-add-remove-multi-duplicate.js +language/literals/regexp/early-err-arithmetic-modifiers-add-remove-s-escape.js +language/literals/regexp/early-err-arithmetic-modifiers-add-remove-s.js +language/literals/regexp/early-err-arithmetic-modifiers-both-empty.js +language/literals/regexp/early-err-arithmetic-modifiers-code-point-repeat-i-1.js +language/literals/regexp/early-err-arithmetic-modifiers-code-point-repeat-i-2.js +language/literals/regexp/early-err-arithmetic-modifiers-no-colon-1.js +language/literals/regexp/early-err-arithmetic-modifiers-no-colon-2.js +language/literals/regexp/early-err-arithmetic-modifiers-no-colon-3.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-arbitrary.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-combining-i.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-combining-m.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-combining-s.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-d.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-g.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-non-display-1.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-non-display-2.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-non-flag.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-u.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-uppercase-I.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-y.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-zwj.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-zwnbsp.js +language/literals/regexp/early-err-arithmetic-modifiers-other-code-point-zwnj.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-add-remove-i.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-add-remove-m.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-add-remove-multi-duplicate.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-add-remove-s-escape.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-add-remove-s.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-code-point-repeat-i-1.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-code-point-repeat-i-2.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-arbitrary.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-combining-i.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-combining-m.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-combining-s.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-d.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-g.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-non-display-1.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-non-display-2.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-non-flag.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-u.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-uppercase-I.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-y.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-zwj.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-zwnbsp.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-other-code-point-zwnj.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-should-not-case-fold-i.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-should-not-case-fold-m.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-should-not-case-fold-s.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-should-not-unicode-case-fold-i.js +language/literals/regexp/early-err-arithmetic-modifiers-reverse-should-not-unicode-case-fold-s.js +language/literals/regexp/early-err-arithmetic-modifiers-should-not-case-fold-i.js +language/literals/regexp/early-err-arithmetic-modifiers-should-not-case-fold-m.js +language/literals/regexp/early-err-arithmetic-modifiers-should-not-case-fold-s.js +language/literals/regexp/early-err-arithmetic-modifiers-should-not-unicode-case-fold-i.js +language/literals/regexp/early-err-arithmetic-modifiers-should-not-unicode-case-fold-s.js +language/literals/regexp/early-err-bad-flag.js +language/literals/regexp/early-err-dup-flag.js +language/literals/regexp/early-err-modifiers-code-point-repeat-i-1.js +language/literals/regexp/early-err-modifiers-code-point-repeat-i-2.js +language/literals/regexp/early-err-modifiers-other-code-point-arbitrary.js +language/literals/regexp/early-err-modifiers-other-code-point-combining-i.js +language/literals/regexp/early-err-modifiers-other-code-point-combining-m.js +language/literals/regexp/early-err-modifiers-other-code-point-combining-s.js +language/literals/regexp/early-err-modifiers-other-code-point-d.js +language/literals/regexp/early-err-modifiers-other-code-point-g.js +language/literals/regexp/early-err-modifiers-other-code-point-non-display-1.js +language/literals/regexp/early-err-modifiers-other-code-point-non-display-2.js +language/literals/regexp/early-err-modifiers-other-code-point-non-flag.js +language/literals/regexp/early-err-modifiers-other-code-point-u.js +language/literals/regexp/early-err-modifiers-other-code-point-uppercase-I.js +language/literals/regexp/early-err-modifiers-other-code-point-y.js +language/literals/regexp/early-err-modifiers-other-code-point-zwj.js +language/literals/regexp/early-err-modifiers-other-code-point-zwnbsp.js +language/literals/regexp/early-err-modifiers-other-code-point-zwnj.js +language/literals/regexp/early-err-modifiers-should-not-case-fold-i.js +language/literals/regexp/early-err-modifiers-should-not-case-fold-m.js +language/literals/regexp/early-err-modifiers-should-not-case-fold-s.js +language/literals/regexp/early-err-modifiers-should-not-unicode-case-fold-i.js +language/literals/regexp/early-err-modifiers-should-not-unicode-case-fold-s.js +language/literals/regexp/early-err-modifiers-should-not-unicode-escape-i.js +language/literals/regexp/early-err-modifiers-should-not-unicode-escape-m.js +language/literals/regexp/early-err-modifiers-should-not-unicode-escape-s.js +language/literals/regexp/early-err-pattern.js +language/literals/regexp/invalid-braced-quantifier-exact.js +language/literals/regexp/invalid-braced-quantifier-lower.js +language/literals/regexp/invalid-braced-quantifier-range.js +language/literals/regexp/invalid-optional-lookbehind.js +language/literals/regexp/invalid-optional-negative-lookbehind.js +language/literals/regexp/invalid-range-lookbehind.js +language/literals/regexp/invalid-range-negative-lookbehind.js +language/literals/regexp/named-groups/invalid-dangling-groupname-2-u.js +language/literals/regexp/named-groups/invalid-dangling-groupname-2.js +language/literals/regexp/named-groups/invalid-dangling-groupname-3-u.js +language/literals/regexp/named-groups/invalid-dangling-groupname-3.js +language/literals/regexp/named-groups/invalid-dangling-groupname-4-u.js +language/literals/regexp/named-groups/invalid-dangling-groupname-4.js +language/literals/regexp/named-groups/invalid-dangling-groupname-5.js +language/literals/regexp/named-groups/invalid-dangling-groupname-u.js +language/literals/regexp/named-groups/invalid-dangling-groupname-without-group-u.js +language/literals/regexp/named-groups/invalid-dangling-groupname.js +language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-2-u.js +language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-2.js +language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-u.js +language/literals/regexp/named-groups/invalid-duplicate-groupspecifier.js +language/literals/regexp/named-groups/invalid-empty-groupspecifier-u.js +language/literals/regexp/named-groups/invalid-empty-groupspecifier.js +language/literals/regexp/named-groups/invalid-identity-escape-in-capture-u.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-2-u.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-2.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-3-u.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-3.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-4.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-5.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-6.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-u.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-without-group-2-u.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-without-group-3-u.js +language/literals/regexp/named-groups/invalid-incomplete-groupname-without-group-u.js +language/literals/regexp/named-groups/invalid-incomplete-groupname.js +language/literals/regexp/named-groups/invalid-non-id-continue-groupspecifier-4-u.js +language/literals/regexp/named-groups/invalid-non-id-continue-groupspecifier-4.js +language/literals/regexp/named-groups/invalid-non-id-continue-groupspecifier.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-2-u.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-2.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-3.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-4-u.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-4.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-5-u.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-5.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-6.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-7.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-8-u.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-8.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-9-u.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier-u.js +language/literals/regexp/named-groups/invalid-non-id-start-groupspecifier.js +language/literals/regexp/named-groups/invalid-numeric-groupspecifier-u.js +language/literals/regexp/named-groups/invalid-numeric-groupspecifier.js +language/literals/regexp/named-groups/invalid-punctuator-starting-groupspecifier-u.js +language/literals/regexp/named-groups/invalid-punctuator-starting-groupspecifier.js +language/literals/regexp/named-groups/invalid-punctuator-within-groupspecifier-u.js +language/literals/regexp/named-groups/invalid-punctuator-within-groupspecifier.js +language/literals/regexp/named-groups/invalid-unterminated-groupspecifier-u.js +language/literals/regexp/named-groups/invalid-unterminated-groupspecifier.js +language/literals/regexp/u-invalid-class-escape.js +language/literals/regexp/u-invalid-extended-pattern-char.js +language/literals/regexp/u-invalid-identity-escape.js +language/literals/regexp/u-invalid-legacy-octal-escape.js +language/literals/regexp/u-invalid-non-empty-class-ranges-no-dash-a.js +language/literals/regexp/u-invalid-non-empty-class-ranges-no-dash-ab.js +language/literals/regexp/u-invalid-non-empty-class-ranges-no-dash-b.js +language/literals/regexp/u-invalid-non-empty-class-ranges.js +language/literals/regexp/u-invalid-oob-decimal-escape.js +language/literals/regexp/u-invalid-optional-lookahead.js +language/literals/regexp/u-invalid-optional-lookbehind.js +language/literals/regexp/u-invalid-optional-negative-lookahead.js +language/literals/regexp/u-invalid-optional-negative-lookbehind.js +language/literals/regexp/u-invalid-range-lookahead.js +language/literals/regexp/u-invalid-range-lookbehind.js +language/literals/regexp/u-invalid-range-negative-lookahead.js +language/literals/regexp/u-invalid-range-negative-lookbehind.js +language/literals/regexp/u-unicode-esc-bounds.js +language/literals/regexp/u-unicode-esc-non-hex.js +language/literals/regexp/unicode-escape-nls-err.js diff --git a/crates/clazel-js-syntax/tests/order.rs b/crates/clazel-js-syntax/tests/order.rs new file mode 100644 index 000000000..a58e2531e --- /dev/null +++ b/crates/clazel-js-syntax/tests/order.rs @@ -0,0 +1,77 @@ +//! In every parse, the children of a node are in source order and do not +//! overlap, and no non-empty node starts or ends in white space: the spans are +//! those of tokens (the first token's start to the last token's end). The +//! verified check ([`clazel_js_syntax::check::finish`]) proves only that a +//! child is inside its parent. + +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; + +fn check_ordered(src: &str, goal: SourceGoal) -> Result<(), String> { + let cps: Vec = src.chars().map(u32::from).collect(); + let Ok(ast) = parse(&cps, goal) else { + return Ok(()); + }; + for (i, n) in ast.nodes.iter().enumerate() { + let mut prev_end = n.start; + for c in &n.children { + let Some(cn) = ast.nodes.get(*c as usize) else { + return Err(format!("node {i}: no child {c}")); + }; + if cn.start < prev_end { + return Err(format!( + "node {i}: child {c} starts at {} before {prev_end}", + cn.start + )); + } + prev_end = cn.end; + } + if n.start < n.end { + let first = cps.get(n.start).copied().unwrap_or(0); + let last = cps.get(n.end.saturating_sub(1)).copied().unwrap_or(0); + let is_space = + |c: u32| char::from_u32(c).is_some_and(char::is_whitespace) || c == 0xFEFF; + if is_space(first) || is_space(last) { + return Err(format!( + "node {i} {}..{} starts or ends in white space", + n.start, n.end + )); + } + } + } + Ok(()) +} + +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn children_are_ordered_and_spans_are_tokens() -> Result<(), String> { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../clazel-js-lexer/tests/data/test262/harness"); + let mut n = 0; + for entry in std::fs::read_dir(root).map_err(|e| e.to_string())? { + let p = entry.map_err(|e| e.to_string())?.path(); + if p.extension().is_some_and(|x| x == "js") { + let text = std::fs::read_to_string(&p).map_err(|e| e.to_string())?; + check_ordered(&text, SourceGoal::Script) + .map_err(|e| format!("{}: {e}", p.display()))?; + n += 1; + } + } + assert!(n > 20); + Ok(()) +} + +/// The same property on inline sources, so that it also runs under Miri. +#[test] +fn inline_children_are_ordered_and_spans_are_tokens() -> Result<(), String> { + for s in [ + "a = b ? (c, d) : e; for (var a of b) ; ({ a = 1 } = b); (a, [b] = c) => d; class A { static x = 1; #y; m() {} }", + "export default class {}; import a, { b as c } from 'm'", + "async function* f() { for await (const [a, ...b] of c) { yield* d; } } label: while (1) { break label }", + "x = `a${b}c${`d${e}`}f`; tag`x`; a?.b?.[c]?.(d); new a.b(c)(d); y = a ?? b", + ] { + check_ordered(s, SourceGoal::Script)?; + check_ordered(s, SourceGoal::Module)?; + } + Ok(()) +} diff --git a/crates/clazel-js-syntax/tests/parser.rs b/crates/clazel-js-syntax/tests/parser.rs new file mode 100644 index 000000000..a80c5e727 --- /dev/null +++ b/crates/clazel-js-syntax/tests/parser.rs @@ -0,0 +1,111 @@ +//! Unit tests of the parser: small programs it must accept, and ones it must +//! reject. + +use clazel_js_syntax::api::ParseError; +use clazel_js_syntax::ast::{Ast, NodeKind}; +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; + +fn cps(s: &str) -> Vec { + s.chars().map(u32::from).collect() +} + +fn script(s: &str) -> Result { + parse(&cps(s), SourceGoal::Script) +} + +fn module(s: &str) -> Result { + parse(&cps(s), SourceGoal::Module) +} + +fn ok(s: &str) { + assert!(script(s).is_ok(), "should parse: {s}"); +} + +fn bad(s: &str) { + assert!( + matches!(script(s), Err(ParseError::Syntax)), + "should be a syntax error: {s}" + ); +} + +#[test] +fn empty_and_simple() { + ok(""); + ok(";"); + ok("var a = 1, b;"); + ok("let x = 1; const y = 2;"); + ok("a + b * c - d / e"); + ok("if (a) b; else c"); + ok("for (var i = 0; i < 10; i++) { continue; }"); + ok("for (const x of y) ;"); + ok("for (x in y) ;"); + ok("function f(a, b = 1, ...c) { return a; }"); + ok("a => a; (a, b) => a + b; async x => x; async (x) => x;"); + ok( + "class A extends B { constructor() { super(); } static x = 1; #p = 2; get a() { return 1; } static { } }", + ); + ok("x = `a${b}c${d}e`; tag`x`;"); + ok("var re = /ab+c/gi; a = b / c / d;"); + ok("a?.b?.[c]?.(d)"); + ok("({a, b: c, ...d} = e); [a, , b = 1, ...c] = d;"); + ok("label: for (;;) break label;"); + ok("switch (a) { case 1: break; default: }"); + ok("try { } catch ({a}) { } finally { }"); + ok("a\nb"); + ok("return_ = 1"); +} + +#[test] +fn asi() { + ok("a = 1\nb = 2"); + ok("do ; while (0) x"); + ok("var a = 1\n++b"); + bad("a b"); + bad("if (a) b else c"); +} + +#[test] +fn syntax_errors() { + bad("var"); + bad("a +"); + bad("("); + bad("1 = 2"); + bad("({a = 1})"); + bad("a ?? b || c"); + bad("-a ** b"); + bad("return"); + bad("let [a];"); + bad("class A { constructor() {} constructor() {} }"); + bad("`\\u{`"); + bad("function* g() { yield\n* 1 }"); +} + +#[test] +fn modules() { + assert!( + module("import a, {b as c} from 'm'; export default a; export {c}; export * from 'x';") + .is_ok() + ); + assert!(module("await 1").is_ok()); + assert!(module("export default class {}").is_ok()); + assert!(script("import a from 'm'").is_err()); +} + +#[test] +fn root_and_spans() { + let r = script("a;\n b;"); + assert!(r.is_ok()); + if let Ok(ast) = r { + let root = ast.nodes.get(ast.root as usize); + assert!(root.is_some_and(|n| n.kind == NodeKind::Script && n.start == 0 && n.end == 6)); + } +} + +#[test] +fn depth_limit() { + let deep = format!("{}1{}", "(".repeat(1500), ")".repeat(1500)); + assert!(matches!(script(&deep), Err(ParseError::TooDeep))); + let ok_deep = format!("{}1{}", "(".repeat(500), ")".repeat(500)); + assert!(script(&ok_deep).is_ok()); +} diff --git a/crates/clazel-js-syntax/tests/shape.rs b/crates/clazel-js-syntax/tests/shape.rs new file mode 100644 index 000000000..d7700f3c5 --- /dev/null +++ b/crates/clazel-js-syntax/tests/shape.rs @@ -0,0 +1,311 @@ +//! The shape of the AST for small programs, written out and checked by hand +//! against the productions of `spec.rs` (`d_stmt`, `d_expr`, ...): the layout of +//! each node's children, the operator, and the spans (the first token's start to +//! the last token's end; the empty span at the end of the token before, for an +//! absent symbol; a `;` that is inserted is in no span). + +use clazel_js_syntax::ast::{Ast, NodeKind, Op}; +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; + +fn kind_name(k: NodeKind) -> &'static str { + match k { + NodeKind::Script => "Script", + NodeKind::Module => "Module", + NodeKind::BlockStatement => "Block", + NodeKind::VariableStatement => "Var", + NodeKind::VariableDeclarationList => "VarList", + NodeKind::VariableDeclaration => "VarDecl", + NodeKind::LexicalDeclaration => "Lex", + NodeKind::LexicalBinding => "LexBinding", + NodeKind::EmptyStatement => "Empty", + NodeKind::ExpressionStatement => "ExprStmt", + NodeKind::IfStatement => "If", + NodeKind::DoWhileStatement => "DoWhile", + NodeKind::WhileStatement => "While", + NodeKind::ForStatement => "For", + NodeKind::ForInStatement => "ForIn", + NodeKind::ForOfStatement => "ForOf", + NodeKind::ForDeclaration => "ForDecl", + NodeKind::BreakStatement => "Break", + NodeKind::ReturnStatement => "Return", + NodeKind::FunctionDeclaration => "FnDecl", + NodeKind::FunctionExpression => "FnExpr", + NodeKind::FormalParameters => "Params", + NodeKind::FunctionBody => "Body", + NodeKind::ArrowFunction => "Arrow", + NodeKind::MethodDefinition => "Method", + NodeKind::ClassDeclaration => "ClassDecl", + NodeKind::ClassBody => "ClassBody", + NodeKind::FieldDefinition => "Field", + NodeKind::BindingElement => "BindingElem", + NodeKind::ArrayBindingPattern => "ArrBinding", + NodeKind::ObjectBindingPattern => "ObjBinding", + NodeKind::BindingProperty => "BindingProp", + NodeKind::ArrayAssignmentPattern => "ArrAssignPat", + NodeKind::ObjectAssignmentPattern => "ObjAssignPat", + NodeKind::AssignmentProperty => "AssignProp", + NodeKind::AssignmentElement => "AssignElem", + NodeKind::Elision => "Elision", + NodeKind::Absent => "Absent", + NodeKind::IdentifierReference => "Ref", + NodeKind::BindingIdentifier => "Bind", + NodeKind::IdentifierName => "Name", + NodeKind::NumericLiteral => "Num", + NodeKind::StringLiteral => "Str", + NodeKind::TemplateLiteral => "Template", + NodeKind::TemplateElement => "TplElem", + NodeKind::ArrayLiteral => "Array", + NodeKind::ObjectLiteral => "Object", + NodeKind::PropertyDefinition => "Prop", + NodeKind::ParenthesizedExpression => "Paren", + NodeKind::MemberExpression => "Member", + NodeKind::NewExpression => "New", + NodeKind::CallExpression => "Call", + NodeKind::Arguments => "Args", + NodeKind::OptionalExpression => "Optional", + NodeKind::OptionalChain => "Chain", + NodeKind::UpdateExpression => "Update", + NodeKind::UnaryExpression => "Unary", + NodeKind::AdditiveExpression => "Add", + NodeKind::MultiplicativeExpression => "Mul", + NodeKind::LogicalORExpression => "Or", + NodeKind::CoalesceExpression => "Coalesce", + NodeKind::ConditionalExpression => "Cond", + NodeKind::AssignmentExpression => "Assign", + NodeKind::YieldExpression => "Yield", + NodeKind::Expression => "Seq", + NodeKind::SpreadElement => "Spread", + NodeKind::ImportDeclaration => "Import", + NodeKind::ImportClause => "ImportClause", + NodeKind::ExportDeclaration => "Export", + _ => "?", + } +} + +fn op_name(o: Op) -> &'static str { + match o { + Op::None => "", + Op::Add => ":+", + Op::Mul => ":*", + Op::Assign => ":=", + Op::Dot => ":.", + Op::Bracket => ":[]", + Op::PostfixIncrement => ":x++", + Op::Let => ":let", + Op::Const => ":const", + Op::Var => ":var", + Op::Shorthand => ":short", + Op::KeyValue => ":kv", + Op::ChainDot => ":?.", + Op::Getter => ":get", + Op::Method => ":method", + Op::ExportDefaultExpression => ":default-expr", + Op::Typeof => ":typeof", + Op::LogicalOr => ":||", + Op::Coalesce => ":??", + Op::YieldPlain => ":yield", + _ => ":?", + } +} + +fn dump_node(ast: &Ast, id: u32, out: &mut String) { + let Some(n) = ast.nodes.get(id as usize) else { + return; + }; + out.push_str(kind_name(n.kind)); + out.push_str(op_name(n.op)); + if n.flag { + out.push('!'); + } + out.push_str(&format!("({}..{})", n.start, n.end)); + if !n.children.is_empty() { + out.push('['); + for (i, c) in n.children.iter().enumerate() { + if i > 0 { + out.push(' '); + } + dump_node(ast, *c, out); + } + out.push(']'); + } +} + +fn dump(src: &str, goal: SourceGoal) -> String { + let cps: Vec = src.chars().map(u32::from).collect(); + match parse(&cps, goal) { + Ok(ast) => { + let mut s = String::new(); + dump_node(&ast, ast.root, &mut s); + s + } + Err(_) => "ERROR".to_string(), + } +} + +fn script(src: &str) -> String { + dump(src, SourceGoal::Script) +} + +#[test] +fn statements() { + // 14.5: the `;` is in the statement's span; an inserted one is not. + assert_eq!(script("a;"), "Script(0..2)[ExprStmt(0..2)[Ref(0..1)]]"); + assert_eq!( + script("a\nb"), + "Script(0..3)[ExprStmt(0..1)[Ref(0..1)] ExprStmt(2..3)[Ref(2..3)]]" + ); + // 14.3.2, 14.3.1: declarations; the absent initializer is at the end of the target. + assert_eq!( + script("var a = 1, b;"), + "Script(0..13)[Var(0..13)[VarDecl(4..9)[Bind(4..5) Num(8..9)] VarDecl(11..12)[Bind(11..12) Absent(12..12)]]]" + ); + assert_eq!( + script("let x = 1;"), + "Script(0..10)[Lex:let(0..10)[LexBinding(4..9)[Bind(4..5) Num(8..9)]]]" + ); + // 14.6: the absent `else` is at the end of the consequent. + assert_eq!( + script("if (a) b;"), + "Script(0..9)[If(0..9)[Ref(4..5) ExprStmt(7..9)[Ref(7..8)] Absent(9..9)]]" + ); + // 14.7.4: `for (;;)` has three absent symbols. + assert_eq!( + script("for (;;) ;"), + "Script(0..10)[For(0..10)[Absent(5..5) Absent(6..6) Absent(7..7) Empty(9..10)]]" + ); + // 14.9: `break;`. + assert_ne!(script("a: for (;;) break a;"), "ERROR"); + assert_eq!( + script("for (var a of b) ;"), + "Script(0..18)[ForOf(0..18)[ForDecl:var(5..10)[Bind(9..10)] Ref(14..15) Empty(17..18)]]" + ); + assert_eq!( + script("for (let [a] in b) ;"), + "Script(0..20)[ForIn(0..20)[ForDecl:let(5..12)[ArrBinding(9..12)[BindingElem(10..11)[Bind(10..11) Absent(11..11)]]] Ref(16..17) Empty(19..20)]]" + ); +} + +#[test] +fn expressions() { + // Left associative binary operators; `*` binds tighter than `+`. + assert_eq!( + script("a + b * c + d"), + "Script(0..13)[ExprStmt(0..13)[Add:+(0..13)[Add:+(0..9)[Ref(0..1) Mul:*(4..9)[Ref(4..5) Ref(8..9)]] Ref(12..13)]]]" + ); + // 13.3: member, call, new. + assert_eq!( + script("a.b[c](d)"), + "Script(0..9)[ExprStmt(0..9)[Call(0..9)[Member:[](0..6)[Member:.(0..3)[Ref(0..1) Name(2..3)] Ref(4..5)] Args(6..9)[Ref(7..8)]]]]" + ); + assert_eq!( + script("new a"), + "Script(0..5)[ExprStmt(0..5)[New(0..5)[Ref(4..5) Absent(5..5)]]]" + ); + assert_eq!( + script("new a(b).c"), + "Script(0..10)[ExprStmt(0..10)[Member:.(0..10)[New(0..8)[Ref(4..5) Args(5..8)[Ref(6..7)]] Name(9..10)]]]" + ); + // 13.3.9: an optional chain is one node with its links. + assert_eq!( + script("a?.b.c"), + "Script(0..6)[ExprStmt(0..6)[Optional(0..6)[Ref(0..1) Chain:?.!(1..4)[Name(3..4)] Chain:?.(4..6)[Name(5..6)]]]]" + ); + // 13.14, 13.15, 13.16. + assert_eq!( + script("a ? b : c"), + "Script(0..9)[ExprStmt(0..9)[Cond(0..9)[Ref(0..1) Ref(4..5) Ref(8..9)]]]" + ); + assert_eq!( + script("a = b, c"), + "Script(0..8)[ExprStmt(0..8)[Seq(0..8)[Assign:=(0..5)[Ref(0..1) Ref(4..5)] Ref(7..8)]]]" + ); + assert_eq!( + script("a ?? b"), + "Script(0..6)[ExprStmt(0..6)[Coalesce:??(0..6)[Ref(0..1) Ref(5..6)]]]" + ); + assert_eq!( + script("a++"), + "Script(0..3)[ExprStmt(0..3)[Update:x++(0..3)[Ref(0..1)]]]" + ); + assert_eq!( + script("typeof a"), + "Script(0..8)[ExprStmt(0..8)[Unary:typeof(0..8)[Ref(7..8)]]]" + ); + // 13.2.9: parentheses are a node of their own. + assert_eq!( + script("(a)"), + "Script(0..3)[ExprStmt(0..3)[Paren(0..3)[Ref(1..2)]]]" + ); + assert_eq!( + script("(a, b)"), + "Script(0..6)[ExprStmt(0..6)[Paren(0..6)[Seq(1..5)[Ref(1..2) Ref(4..5)]]]]" + ); + // 13.2.8: template elements and substitutions alternate. + assert_eq!( + script("`a${b}c`"), + "Script(0..8)[ExprStmt(0..8)[Template(0..8)[TplElem(0..4) Ref(4..5) TplElem(5..8)]]]" + ); +} + +#[test] +fn literals_and_patterns() { + // 13.2.4: a hole is an `Elision` of one comma; a trailing comma is none. + assert_eq!( + script("[a,,b,]"), + "Script(0..7)[ExprStmt(0..7)[Array(0..7)[Ref(1..2) Elision(3..4) Ref(4..5)]]]" + ); + assert_eq!( + script("({a, b: c, d() {}})"), + "Script(0..19)[ExprStmt(0..19)[Paren(0..19)[Object(1..18)[Prop:short(2..3)[Ref(2..3)] Prop:kv(5..9)[Name(5..6) Ref(8..9)] Method:method(11..17)[Name(11..12) Params(12..14) Body(15..17)]]]]]" + ); + // 13.15.5: the left side of `=` is read as an assignment pattern. + assert_eq!( + script("[a, b = 1] = c"), + "Script(0..14)[ExprStmt(0..14)[Assign:=(0..14)[ArrAssignPat(0..10)[AssignElem(1..2)[Ref(1..2) Absent(2..2)] AssignElem(4..9)[Ref(4..5) Num(8..9)]] Ref(13..14)]]]" + ); + assert_eq!( + script("({a = 1} = b)"), + "Script(0..13)[ExprStmt(0..13)[Paren(0..13)[Assign:=(1..12)[ObjAssignPat(1..8)[AssignProp:short(2..7)[Ref(2..3) Num(6..7)]] Ref(11..12)]]]]" + ); + // 14.3.3: binding patterns. + assert_eq!( + script("var {a, b: [c]} = d;"), + "Script(0..20)[Var(0..20)[VarDecl(4..19)[ObjBinding(4..15)[BindingProp:short(5..6)[BindingElem(5..6)[Bind(5..6) Absent(6..6)]] BindingProp:kv(8..14)[Name(8..9) BindingElem(11..14)[ArrBinding(11..14)[BindingElem(12..13)[Bind(12..13) Absent(13..13)]] Absent(14..14)]]] Ref(18..19)]]]" + ); +} + +#[test] +fn functions_and_classes() { + // 15.2: the name, `FormalParameters` from `(` to `)`, the body from `{` to `}`. + assert_eq!( + script("function f(a, b = 1) { }"), + "Script(0..24)[FnDecl(0..24)[Bind(9..10) Params(10..20)[BindingElem(11..12)[Bind(11..12) Absent(12..12)] BindingElem(14..19)[Bind(14..15) Num(18..19)]] Body(21..24)]]" + ); + // 15.3: an arrow function's parameters are read from the parenthesized cover. + assert_eq!( + script("(a) => a"), + "Script(0..8)[ExprStmt(0..8)[Arrow(0..8)[Params(0..3)[BindingElem(1..2)[Bind(1..2) Absent(2..2)]] Ref(7..8)]]]" + ); + assert_eq!( + script("a => a"), + "Script(0..6)[ExprStmt(0..6)[Arrow(0..6)[Bind(0..1) Ref(5..6)]]]" + ); + // 15.7: a class element's span includes `static`; a field's includes its `;`. + assert_eq!( + script("class A { static x = 1; get y() { } }"), + "Script(0..37)[ClassDecl(0..37)[Bind(6..7) Absent(7..7) ClassBody(8..37)[Field!(10..23)[Name(17..18) Num(21..22)] Method:get(24..35)[Name(28..29) Params(29..31) Body(32..35)]]]]" + ); +} + +#[test] +fn modules() { + assert_eq!( + dump("export default a;", SourceGoal::Module), + "Module(0..17)[Export:default-expr(0..17)[Ref(15..16)]]" + ); + assert_eq!( + dump("import a from 'm';", SourceGoal::Module), + "Module(0..18)[Import(0..18)[ImportClause(7..8)[Bind(7..8) Absent(8..8)] Str(14..17) Absent(17..17)]]" + ); +} diff --git a/crates/clazel-js-syntax/tests/test262.rs b/crates/clazel-js-syntax/tests/test262.rs new file mode 100644 index 000000000..baa87faaa --- /dev/null +++ b/crates/clazel-js-syntax/tests/test262.rs @@ -0,0 +1,292 @@ +//! The parser over the vendored Test262 syntax tests (`clazel-js-lexer`'s +//! `tests/data/test262/` and its generated table). +//! +//! Every test that must parse (no `negative:` of phase `parse` or `early`) is +//! parsed in each mode its flags allow, and every negative one must be +//! rejected in each. Tests that expect a failure (a feature newer than the +//! edition) are skipped. The set of tests the parser gets wrong is compared to +//! `tests/expected_failures.txt`: a test that starts failing, or one that +//! starts passing and is still listed, fails this test, so the list only +//! shrinks. Set `UPDATE_EXPECTED=1` to rewrite it. + +use clazel_js_syntax::exec::parse; +use clazel_js_syntax::spec::SourceGoal; +use std::collections::BTreeSet; +use std::path::PathBuf; + +/// 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") +} + +fn read(case: &Case) -> Result, String> { + let path = root().join(case.path); + let text = std::fs::read_to_string(&path).map_err(|e| format!("{}: {e}", case.path))?; + Ok(text.chars().map(u32::from).collect()) +} + +/// Whether `src` is accepted as `goal`. +fn accepts(src: &[u32], goal: SourceGoal) -> bool { + parse(src, goal).is_ok() +} + +/// 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("module") { + vec![(SourceGoal::Module, false)] + } else 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)] + } +} + +/// Whether the test is judged by the parser: it is not an expected failure, +/// and a negative one is of phase `parse` or `early`. +fn judged(case: &Case) -> bool { + matches!(case.expect, Expect::Run) + && !matches!(case.negative, Some((phase, _)) if phase != "parse" && phase != "early") +} + +fn passes(case: &Case) -> Result { + 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()); + let ok = accepts(&s, goal); + if ok == case.negative.is_some() { + return Ok(false); + } + } + Ok(true) +} + +fn expected_path() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/expected_failures.txt") +} + +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn test262_parse_and_early_errors() -> Result<(), String> { + assert_eq!(TREE_SHA256.len(), 64); + let mut failing = BTreeSet::new(); + let (mut total, mut neg_total, mut neg_fail, mut pos_fail) = (0, 0, 0, 0); + let mut features = BTreeSet::new(); + let mut with_includes = 0; + let mut reasons = BTreeSet::new(); + for case in TEST262 { + features.extend(case.features.iter().copied()); + if !case.includes.is_empty() { + with_includes += 1; + } + if let Expect::Fail(reason) = case.expect { + reasons.insert(reason); + } + if !judged(case) { + continue; + } + total += 1; + if case.negative.is_some() { + neg_total += 1; + } + if !passes(case)? { + if case.negative.is_some() { + neg_fail += 1; + } else { + pos_fail += 1; + if std::env::var("SHOW_POSITIVE").is_ok() { + println!("POSITIVE-FAIL {}", case.path); + } + } + failing.insert(case.path.to_string()); + } + } + println!( + "{total} judged tests: {neg_total} negative ({neg_fail} not rejected), {} positive ({pos_fail} not accepted); \ + {} features, {with_includes} tests with includes, {} reasons for expected failures", + total - neg_total, + features.len(), + reasons.len() + ); + let listing: String = failing.iter().map(|p| format!("{p}\n")).collect(); + if std::env::var("UPDATE_EXPECTED").is_ok() { + std::fs::write(expected_path(), &listing).map_err(|e| e.to_string())?; + return Ok(()); + } + let expected: BTreeSet = std::fs::read_to_string(expected_path()) + .unwrap_or_default() + .lines() + .map(str::to_string) + .collect(); + let newly: Vec<_> = failing.difference(&expected).collect(); + let fixed: Vec<_> = expected.difference(&failing).collect(); + assert!( + newly.is_empty(), + "{} tests newly fail:\n{}", + newly.len(), + newly + .iter() + .map(|s| s.as_str()) + .collect::>() + .join("\n") + ); + assert!( + fixed.is_empty(), + "{} listed tests now pass (remove them):\n{}", + fixed.len(), + fixed + .iter() + .map(|s| s.as_str()) + .collect::>() + .join("\n") + ); + Ok(()) +} + +fn all_js(dir: &std::path::Path, out: &mut Vec) { + let Ok(rd) = std::fs::read_dir(dir) else { + return; + }; + let mut entries: Vec<_> = rd.filter_map(Result::ok).collect(); + entries.sort_by_key(|e| e.path()); + for e in entries { + let p = e.path(); + if p.is_dir() { + all_js(&p, out); + } else if p.extension().is_some_and(|x| x == "js") { + out.push(p); + } + } +} + +/// Test262's harness is real JavaScript (about 600 KB of it): every file +/// must parse as a `Script`. +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn test262_harness_parses() -> Result<(), String> { + let dir = root().join("../harness"); + let mut files = Vec::new(); + all_js(&dir, &mut files); + assert!(files.len() > 20); + let mut bad = Vec::new(); + 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 !accepts(&src, SourceGoal::Script) { + bad.push(f.display().to_string()); + } + } + assert!( + bad.is_empty(), + "harness files that do not parse:\n{}", + bad.join("\n") + ); + Ok(()) +} + +/// The fixtures that are meant to be syntax errors (`break;`, `0++;`, ...): +/// the modules that tests of linking import to get an error. +const ERRONEOUS_FIXTURES: &[&str] = &[ + "language/import/import-attributes/text-javascript_FIXTURE.js", + "language/import/import-defer/errors/syntax-error/syntax-error_FIXTURE.js", + "language/module-code/instn-resolve-empty-export_FIXTURE.js", + "language/module-code/instn-resolve-empty-import_FIXTURE.js", + "language/module-code/instn-resolve-err-syntax-1_FIXTURE.js", + "language/module-code/instn-resolve-err-syntax-2_FIXTURE.js", + "language/module-code/instn-resolve-order-depth-reference_FIXTURE.js", + "language/module-code/instn-resolve-order-depth-syntax_FIXTURE.js", + "language/module-code/instn-resolve-order-src-reference_FIXTURE.js", + "language/module-code/instn-resolve-order-src-syntax_FIXTURE.js", +]; + +/// The `_FIXTURE.js` files that tests import parse as a `Module` or a +/// `Script`, except those that are meant to be syntax errors. +#[test] +#[cfg_attr(miri, ignore = "reads files")] +fn test262_fixtures_parse() -> Result<(), String> { + let mut files = Vec::new(); + all_js(&root(), &mut files); + let mut rejected = BTreeSet::new(); + let mut n = 0; + for f in files + .iter() + .filter(|f| f.to_string_lossy().ends_with("_FIXTURE.js")) + { + n += 1; + let text = std::fs::read_to_string(f).map_err(|e| e.to_string())?; + let src: Vec = text.chars().map(u32::from).collect(); + if !accepts(&src, SourceGoal::Module) && !accepts(&src, SourceGoal::Script) { + let rel = f.strip_prefix(root()).map_err(|e| e.to_string())?; + rejected.insert(rel.to_string_lossy().replace('\\', "/")); + } + } + assert!(n > 100); + let expected: BTreeSet = ERRONEOUS_FIXTURES.iter().map(|s| s.to_string()).collect(); + assert_eq!(rejected, expected); + 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. +#[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; + let (mut positives, mut verified) = (0usize, 0usize); + 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()); + positives += 1; + if let Ok((_, Derivation::Verified)) = parse_checked(&s, goal) { + verified += 1; + } + } + } + let mut files = Vec::new(); + all_js(&root().join("../harness"), &mut files); + let (mut hv, mut hn) = (0usize, 0usize); + 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(); + hn += 1; + if let Ok((_, Derivation::Verified)) = parse_checked(&src, SourceGoal::Script) { + hv += 1; + } + } + println!("Verified: {verified} of {positives} positive runs, {hv} of {hn} harness files"); + Ok(()) +} diff --git a/crates/clazel-x64/src/contract.rs b/crates/clazel-x64/src/contract.rs index 73249572e..63efe600c 100644 --- a/crates/clazel-x64/src/contract.rs +++ b/crates/clazel-x64/src/contract.rs @@ -52,9 +52,9 @@ pub open spec fn decode_ensures(bytes: Seq, r: Option<(Instr, usize)>) -> bo /// No canonical encoding is a proper prefix of another instruction's, nor /// two different instructions' encodings of one input: the set of canonical -/// encodings is a prefix code, so a decoder's `Some` is unique. To be proven -/// about [`encode`] in the implementation half; the contracts above are -/// meaningful (a decoder may pick any match) without it. +/// encodings is a prefix code, so a decoder's `Some` is unique. Proven: +/// `decoder_proofs::lemma_canonical_prefix_free`, from the completeness of +/// the specification-level decoder in `decoder`. pub open spec fn canonical_prefix_free() -> bool { forall|i: Instr, j: Instr, bytes: Seq| starts_with_encoding(bytes, i) && starts_with_encoding(bytes, j) ==> i == j diff --git a/crates/clazel-x64/src/decoder.rs b/crates/clazel-x64/src/decoder.rs new file mode 100644 index 000000000..44cf96518 --- /dev/null +++ b/crates/clazel-x64/src/decoder.rs @@ -0,0 +1,373 @@ +//! A specification-level **decoder** for the canonical encoding of +//! [`super::spec`]: [`extract`] reads the fields of the instruction a byte +//! string would begin with, and [`dec`] accepts the result only if it really +//! is the canonical encoding that the bytes begin with. +//! +//! This is the proof device for [`crate::contract::canonical_prefix_free`] +//! and the model that an executable decoder refines (jit.md J.3, J.6): +//! +//! - **Soundness is by construction.** [`dec`] returns `Some(i)` only when +//! `starts_with_encoding(bytes, i)`; there is nothing to prove. +//! - **Completeness is the lemma** `completeness`: for every +//! instruction of the subset, `dec(encode(i) + rest) == Some(i)`. It is +//! proven below one instruction at a time (the shapes `enc_rr` and +//! `enc_rm_mem` first). +//! - **Prefix-freeness follows**: if two instructions' encodings both begin +//! the same bytes, `dec` returns both, so they are equal. +//! +//! [`extract`] is total: it reads bytes past the end as 0 and returns a +//! meaningless instruction for bytes that are no encoding, which the final +//! check of [`dec`] then refuses. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Byte `k` of `b` as an integer, or 0 past either end. +pub open spec fn at(b: Seq, k: int) -> int { + if 0 <= k < b.len() { + b[k] as int + } else { + 0 + } +} + +/// The four bytes at `k`, little-endian, as an unsigned number. +pub open spec fn le32(b: Seq, k: int) -> int { + at(b, k) + 256 * (at(b, k + 1) + 256 * (at(b, k + 2) + 256 * at(b, k + 3))) +} + +/// The eight bytes at `k`, little-endian, as an unsigned number. +pub open spec fn le64(b: Seq, k: int) -> int { + le32(b, k) + 0x1_0000_0000 * le32(b, k + 4) +} + +/// A 32-bit unsigned number as two's complement signed. +pub open spec fn sx32(v: int) -> int { + if v >= 0x8000_0000 { + v - 0x1_0000_0000 + } else { + v + } +} + +/// The fields of the bytes before the opcode: the mandatory prefix, `REX`. +pub struct Head { + /// The prefix byte (`0x66` or `0xF2`), or 0. + pub pf: int, + /// A `REX` byte is present. + pub rexp: bool, + /// `REX.W`. + pub w: bool, + /// `REX.R` (0 or 1). + pub r: int, + /// `REX.X` (0 or 1). + pub x: int, + /// `REX.B` (0 or 1). + pub bb: int, + /// The index of the first opcode byte. + pub kop: int, +} + +/// Reads the optional prefix and the optional `REX` byte. +pub open spec fn head(b: Seq) -> Head { + let p = at(b, 0); + let pf = if p == 0x66 || p == 0xF2 { + p + } else { + 0 + }; + let k: int = if p == 0x66 || p == 0xF2 { + 1 + } else { + 0 + }; + let rv = at(b, k); + if 0x40 <= rv <= 0x4F { + let v = rv - 0x40; + Head { + pf: pf, + rexp: true, + w: (v / 8) % 2 == 1, + r: (v / 4) % 2, + x: (v / 2) % 2, + bb: v % 2, + kop: k + 1, + } + } else { + Head { pf: pf, rexp: false, w: false, r: 0, x: 0, bb: 0, kop: k } + } +} + +/// The operand size `REX.W` selects. +pub open spec fn sz_of(h: Head) -> Size { + if h.w { + Size::W64 + } else { + Size::W32 + } +} + +/// The `reg` register: the ModR/M `reg` field and `REX.R`. +pub open spec fn reg_f(b: Seq, h: Head, ko: int) -> u8 { + ((at(b, ko) / 8) % 8 + 8 * h.r) as u8 +} + +/// The `rm` register: the ModR/M `rm` field and `REX.B`. +pub open spec fn rm_f(b: Seq, h: Head, ko: int) -> u8 { + (at(b, ko) % 8 + 8 * h.bb) as u8 +} + +/// The ModR/M `mod` field. +pub open spec fn mod_f(b: Seq, ko: int) -> int { + at(b, ko) / 64 +} + +/// The ModR/M `reg` field alone, an opcode extension. +pub open spec fn ext_f(b: Seq, ko: int) -> int { + (at(b, ko) / 8) % 8 +} + +/// The memory operand of a `mod = 10`, `rm = 100` instruction whose ModR/M +/// byte is at `ko`: the SIB byte, then a 32-bit displacement. +pub open spec fn mem_f(b: Seq, h: Head, ko: int) -> Mem { + mem_of(at(b, ko + 1), h.x, h.bb, le32(b, ko + 2)) +} + +/// The memory operand from its SIB byte, the `REX.X` and `REX.B` bits and the +/// unsigned displacement word. +pub open spec fn mem_of(sib: int, x: int, bb: int, disp: int) -> Mem { + let ix = (sib / 8) % 8 + 8 * x; + Mem { + base: (sib % 8 + 8 * bb) as u8, + index: if ix == 4 { + None + } else { + Some(Index { reg: ix as u8, scale: (sib / 64) as u8 }) + }, + disp: sx32(disp) as i32, + } +} + +/// The ALU operation whose `op r/m, r` opcode is `v`. +pub open spec fn alu_of_base(v: int) -> Option { + if v == 0x00 { + Some(AluOp::Add) + } else if v == 0x08 { + Some(AluOp::Or) + } else if v == 0x20 { + Some(AluOp::And) + } else if v == 0x28 { + Some(AluOp::Sub) + } else if v == 0x30 { + Some(AluOp::Xor) + } else if v == 0x38 { + Some(AluOp::Cmp) + } else { + None + } +} + +/// The ALU operation of the group-1 extension `e` (`81 /e`). +pub open spec fn alu_of_ext(e: int) -> AluOp { + if e == 0 { + AluOp::Add + } else if e == 1 { + AluOp::Or + } else if e == 4 { + AluOp::And + } else if e == 5 { + AluOp::Sub + } else if e == 6 { + AluOp::Xor + } else { + AluOp::Cmp + } +} + +/// The shift of the group-2 extension `e`. +pub open spec fn shift_of_ext(e: int) -> ShiftOp { + if e == 4 { + ShiftOp::Shl + } else if e == 5 { + ShiftOp::Shr + } else { + ShiftOp::Sar + } +} + +/// The scalar double operation of the second opcode byte `o2`. +pub open spec fn fop_of(o2: int) -> BinOp { + if o2 == 0x58 { + BinOp::Add + } else if o2 == 0x59 { + BinOp::Mul + } else if o2 == 0x5C { + BinOp::Sub + } else { + BinOp::Div + } +} + +/// The XMM source operand: a register for `mod = 11`, else memory. +pub open spec fn xsrc_f(b: Seq, h: Head, ko: int) -> XmmSrc { + if mod_f(b, ko) == 3 { + XmmSrc::Reg(rm_f(b, h, ko)) + } else { + XmmSrc::Mem(mem_f(b, h, ko)) + } +} + +/// The fields of the instruction the bytes would begin with, if they began +/// with a canonical encoding; otherwise an arbitrary instruction. +#[verifier::opaque] +pub open spec fn extract(b: Seq) -> Instr { + let h = head(b); + let o = at(b, h.kop); + let o2 = at(b, h.kop + 1); + let ko = h.kop + 1; + let ko2 = h.kop + 2; + let sz = sz_of(h); + if h.pf == 0 { + if o == 0x0F { + if o2 == 0xAF { + Instr::ImulRR { sz: sz, dst: reg_f(b, h, ko2), src: rm_f(b, h, ko2) } + } else if 0x80 <= o2 <= 0x8F { + Instr::Jcc { cc: (o2 - 0x80) as u8, rel: sx32(le32(b, h.kop + 2)) as i32 } + } else if 0x40 <= o2 <= 0x4F { + Instr::Cmovcc { + cc: (o2 - 0x40) as u8, + sz: sz, + dst: reg_f(b, h, ko2), + src: rm_f(b, h, ko2), + } + } else { + Instr::Setcc { cc: ((o2 - 0x90) % 16) as u8, dst: rm_f(b, h, ko2) } + } + } else if o == 0x89 { + if mod_f(b, ko) == 3 { + Instr::MovRR { sz: sz, dst: rm_f(b, h, ko), src: reg_f(b, h, ko) } + } else { + Instr::MovStore { sz: sz, mem: mem_f(b, h, ko), src: reg_f(b, h, ko) } + } + } else if o == 0x8B { + Instr::MovLoad { sz: sz, dst: reg_f(b, h, ko), mem: mem_f(b, h, ko) } + } else if 0xB8 <= o <= 0xBF { + if h.w { + Instr::MovImm64 { dst: ((o - 0xB8) + 8 * h.bb) as u8, imm_u64: le64(b, ko) as u64 } + } else { + Instr::MovImm32 { dst: ((o - 0xB8) + 8 * h.bb) as u8, imm_u32: le32(b, ko) as u32 } + } + } else if o == 0xC7 { + Instr::MovImm32S { dst: rm_f(b, h, ko), imm: sx32(le32(b, ko + 1)) as i32 } + } else if o == 0x81 { + Instr::AluRI { + alu: alu_of_ext(ext_f(b, ko)), + sz: sz, + dst: rm_f(b, h, ko), + imm: sx32(le32(b, ko + 1)) as i32, + } + } else if o == 0x85 { + Instr::TestRR { sz: sz, a: rm_f(b, h, ko), b: reg_f(b, h, ko) } + } else if o == 0xF7 { + if ext_f(b, ko) == 0 { + Instr::TestRI { sz: sz, a: rm_f(b, h, ko), imm: sx32(le32(b, ko + 1)) as i32 } + } else if ext_f(b, ko) == 3 { + Instr::Neg { sz: sz, r: rm_f(b, h, ko) } + } else { + Instr::Not { sz: sz, r: rm_f(b, h, ko) } + } + } else if o == 0xC1 { + Instr::ShiftImm { + shift: shift_of_ext(ext_f(b, ko)), + sz: sz, + r: rm_f(b, h, ko), + count: at(b, ko + 1) as u8, + } + } else if o == 0xD3 { + Instr::ShiftCl { shift: shift_of_ext(ext_f(b, ko)), sz: sz, r: rm_f(b, h, ko) } + } else if o == 0x69 { + Instr::ImulRRI { + sz: sz, + dst: reg_f(b, h, ko), + src: rm_f(b, h, ko), + imm: sx32(le32(b, ko + 1)) as i32, + } + } else if o == 0xE9 { + Instr::Jmp { rel: sx32(le32(b, ko)) as i32 } + } else if o == 0xC3 { + Instr::Ret + } else if o % 8 == 1 && alu_of_base(o - 1) is Some { + Instr::AluRR { + alu: alu_of_base(o - 1)->Some_0, + sz: sz, + dst: rm_f(b, h, ko), + src: reg_f(b, h, ko), + } + } else { + Instr::AluRM { + alu: if alu_of_base(o - 3) is Some { + alu_of_base(o - 3)->Some_0 + } else { + AluOp::Add + }, + sz: sz, + dst: reg_f(b, h, ko), + mem: mem_f(b, h, ko), + } + } + } else if h.pf == 0xF2 { + if o2 == 0x51 { + Instr::Sqrtsd { dst: reg_f(b, h, ko2), xsrc: xsrc_f(b, h, ko2) } + } else if o2 == 0x2A { + Instr::Cvtsi2sd { sz: sz, dst: reg_f(b, h, ko2), src: rm_f(b, h, ko2) } + } else if o2 == 0x2C { + Instr::Cvttsd2si { sz: sz, dst: reg_f(b, h, ko2), src: rm_f(b, h, ko2) } + } else if o2 == 0x2D { + Instr::Cvtsd2si { sz: sz, dst: reg_f(b, h, ko2), src: rm_f(b, h, ko2) } + } else if o2 == 0x10 { + Instr::MovsdLoad { dst: reg_f(b, h, ko2), mem: mem_f(b, h, ko2) } + } else if o2 == 0x11 { + Instr::MovsdStore { mem: mem_f(b, h, ko2), src: reg_f(b, h, ko2) } + } else { + Instr::FpArith { + fop: fop_of(o2), + dst: reg_f(b, h, ko2), + xsrc: xsrc_f(b, h, ko2), + } + } + } else { + if o2 == 0x6E { + Instr::MovqToXmm { dst: reg_f(b, h, ko2), src: rm_f(b, h, ko2) } + } else if o2 == 0x7E { + Instr::MovqFromXmm { dst: rm_f(b, h, ko2), src: reg_f(b, h, ko2) } + } else { + Instr::Ucomisd { a: reg_f(b, h, ko2), xsrc: xsrc_f(b, h, ko2) } + } + } +} + +/// The decoder: the instruction `bytes` begin with, if they begin with the +/// canonical encoding of one. +pub open spec fn dec(bytes: Seq) -> Option { + let c = extract(bytes); + if starts_with_encoding(bytes, c) { + Some(c) + } else { + None + } +} + +/// Soundness of [`dec`] (by construction). +pub proof fn dec_sound(bytes: Seq, i: Instr) + requires + dec(bytes) == Some(i), + ensures + starts_with_encoding(bytes, i), +{ +} + +} // verus! diff --git a/crates/clazel-x64/src/decoder_exec.rs b/crates/clazel-x64/src/decoder_exec.rs new file mode 100644 index 000000000..483e334c7 --- /dev/null +++ b/crates/clazel-x64/src/decoder_exec.rs @@ -0,0 +1,570 @@ +//! The **executable decoder**: [`extract_exec`] is proven equal to the +//! specification-level [`super::decoder::extract`], and [`decode_exec`] +//! meets [`crate::contract::decode_ensures`]: a `Some` result is the +//! canonical encoding of an instruction of the subset, and `None` means no +//! instruction's encoding begins the bytes (by the completeness of `extract`, +//! `decoder_proofs::lemma_extract_starts`, and the encoder +//! [`super::encoder::encode_exec`], which is equal to `encode`). +//! +//! Each function mirrors the spec function named in its doc, branch for +//! branch. + +pub use super::decoder::*; +use super::encoder::encode_exec; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Byte `k`, or 0 past the end (the spec's `at`). +pub fn at_e(b: &[u8], k: usize) -> (r: u8) + ensures + r as int == at(b@, k as int), +{ + match b.get(k) { + Some(x) => *x, + None => 0, + } +} + +/// Four bytes at `k`, little-endian (the spec's `le32`). +pub fn le32_e(b: &[u8], k: usize) -> (r: u32) + requires + k < 1000, + ensures + r as int == le32(b@, k as int), +{ + let b0 = at_e(b, k) as u32; + let b1 = at_e(b, k.saturating_add(1)) as u32; + let b2 = at_e(b, k.saturating_add(2)) as u32; + let b3 = at_e(b, k.saturating_add(3)) as u32; + b0.saturating_add(b1.saturating_add(b2.saturating_add(b3.saturating_mul(256)).saturating_mul(256)).saturating_mul(256)) +} + +/// Eight bytes at `k`, little-endian (the spec's `le64`). +pub fn le64_e(b: &[u8], k: usize) -> (r: u64) + requires + k < 990, + ensures + r as int == le64(b@, k as int), +{ + let lo = le32_e(b, k) as u64; + let hi = le32_e(b, k.saturating_add(4)) as u64; + lo.saturating_add(hi.saturating_mul(0x1_0000_0000)) +} + +/// A 32-bit word as a signed number (the spec's `sx32`). +pub fn sx32_e(x: u32) -> (r: i32) + ensures + r as int == sx32(x as int), +{ + if x >= 0x8000_0000 { + ((x as i64).wrapping_add(-4294967296i64)) as i32 + } else { + x as i32 + } +} + +/// The prefix and `REX` fields (the spec's `Head`). +pub struct HeadE { + /// The prefix byte or 0. + pub pf: u8, + /// `REX.W`. + pub w: bool, + /// `REX.R`. + pub r: u8, + /// `REX.X`. + pub x: u8, + /// `REX.B`. + pub bb: u8, + /// The index of the first opcode byte. + pub kop: usize, +} + +/// The exec head describes the spec head of `b`. +pub open spec fn hm(h: HeadE, b: Seq) -> bool { + h.pf as int == head(b).pf && h.w == head(b).w && h.r as int == head(b).r && h.x as int == head( + b, + ).x && h.bb as int == head(b).bb && h.kop as int == head(b).kop && h.r <= 1 && h.x <= 1 && h.bb + <= 1 && h.kop <= 2 +} + +/// The spec's `head`. +pub fn head_e(b: &[u8]) -> (h: HeadE) + ensures + hm(h, b@), +{ + let p = at_e(b, 0); + let pf: u8 = if p == 0x66 || p == 0xF2 { + p + } else { + 0 + }; + let k: usize = if p == 0x66 || p == 0xF2 { + 1 + } else { + 0 + }; + let rv = at_e(b, k); + if rv >= 0x40 && rv.saturating_sub(0x40) <= 0x0F { + let v = rv.saturating_sub(0x40); + HeadE { + pf, + w: (v / 8) % 2 == 1, + r: (v / 4) % 2, + x: (v / 2) % 2, + bb: v % 2, + kop: k.saturating_add(1), + } + } else { + HeadE { pf, w: false, r: 0, x: 0, bb: 0, kop: k } + } +} + +/// The operand size `REX.W` selects. +pub fn sz_e(h: &HeadE) -> (r: Size) + ensures + r == sz_of(Head { pf: h.pf as int, rexp: false, w: h.w, r: 0, x: 0, bb: 0, kop: 0 }), +{ + if h.w { + Size::W64 + } else { + Size::W32 + } +} + +/// The spec's `reg_f`. +pub fn reg_e(b: &[u8], h: &HeadE, ko: usize) -> (r: u8) + requires + hm(*h, b@), + ko < 1000, + ensures + r == reg_f(b@, head(b@), ko as int), +{ + let m = at_e(b, ko); + ((m / 8) % 8).saturating_add(h.r.saturating_mul(8)) +} + +/// The spec's `rm_f`. +pub fn rm_e(b: &[u8], h: &HeadE, ko: usize) -> (r: u8) + requires + hm(*h, b@), + ko < 1000, + ensures + r == rm_f(b@, head(b@), ko as int), +{ + let m = at_e(b, ko); + (m % 8).saturating_add(h.bb.saturating_mul(8)) +} + +/// The spec's `mod_f`. +pub fn mod_e(b: &[u8], ko: usize) -> (r: u8) + ensures + r as int == mod_f(b@, ko as int), +{ + at_e(b, ko) / 64 +} + +/// The spec's `ext_f`. +pub fn ext_e(b: &[u8], ko: usize) -> (r: u8) + ensures + r as int == ext_f(b@, ko as int), +{ + (at_e(b, ko) / 8) % 8 +} + +/// The spec's `mem_f`. +pub fn mem_e(b: &[u8], h: &HeadE, ko: usize) -> (r: Mem) + requires + hm(*h, b@), + ko < 990, + ensures + r == mem_f(b@, head(b@), ko as int), +{ + let sib = at_e(b, ko.saturating_add(1)); + let ix: u8 = ((sib / 8) % 8).saturating_add(h.x.saturating_mul(8)); + let disp = sx32_e(le32_e(b, ko.saturating_add(2))); + Mem { + base: (sib % 8).saturating_add(h.bb.saturating_mul(8)), + index: if ix == 4 { + None + } else { + Some(Index { reg: ix, scale: sib / 64 }) + }, + disp, + } +} + +/// The spec's `alu_of_base`. +pub fn alu_of_base_e(v: u8) -> (r: Option) + ensures + r == alu_of_base(v as int), +{ + if v == 0x00 { + Some(AluOp::Add) + } else if v == 0x08 { + Some(AluOp::Or) + } else if v == 0x20 { + Some(AluOp::And) + } else if v == 0x28 { + Some(AluOp::Sub) + } else if v == 0x30 { + Some(AluOp::Xor) + } else if v == 0x38 { + Some(AluOp::Cmp) + } else { + None + } +} + +/// The spec's `alu_of_ext`. +pub fn alu_of_ext_e(e: u8) -> (r: AluOp) + ensures + r == alu_of_ext(e as int), +{ + if e == 0 { + AluOp::Add + } else if e == 1 { + AluOp::Or + } else if e == 4 { + AluOp::And + } else if e == 5 { + AluOp::Sub + } else if e == 6 { + AluOp::Xor + } else { + AluOp::Cmp + } +} + +/// The spec's `shift_of_ext`. +pub fn shift_of_ext_e(e: u8) -> (r: ShiftOp) + ensures + r == shift_of_ext(e as int), +{ + if e == 4 { + ShiftOp::Shl + } else if e == 5 { + ShiftOp::Shr + } else { + ShiftOp::Sar + } +} + +/// The spec's `fop_of`. +pub fn fop_of_e(o2: u8) -> (r: BinOp) + ensures + r == fop_of(o2 as int), +{ + if o2 == 0x58 { + BinOp::Add + } else if o2 == 0x59 { + BinOp::Mul + } else if o2 == 0x5C { + BinOp::Sub + } else { + BinOp::Div + } +} + +/// The spec's `xsrc_f`. +pub fn xsrc_e(b: &[u8], h: &HeadE, ko: usize) -> (r: XmmSrc) + requires + hm(*h, b@), + ko < 990, + ensures + r == xsrc_f(b@, head(b@), ko as int), +{ + if mod_e(b, ko) == 3 { + XmmSrc::Reg(rm_e(b, h, ko)) + } else { + XmmSrc::Mem(mem_e(b, h, ko)) + } +} + +/// The spec's `extract`. +#[verifier::spinoff_prover] +#[verifier::rlimit(4000)] +pub fn extract_exec(b: &[u8]) -> (r: Instr) + ensures + r == extract(b@), +{ + reveal(extract); + let h = head_e(b); + let o = at_e(b, h.kop); + let o2 = at_e(b, h.kop.saturating_add(1)); + let ko = h.kop.saturating_add(1); + let ko2 = h.kop.saturating_add(2); + let sz = sz_e(&h); + if h.pf == 0 { + if o == 0x0F { + if o2 == 0xAF { + Instr::ImulRR { sz, dst: reg_e(b, &h, ko2), src: rm_e(b, &h, ko2) } + } else if o2 >= 0x80 && o2.saturating_sub(0x80) <= 0x0F { + Instr::Jcc { cc: o2.saturating_sub(0x80), rel: sx32_e(le32_e(b, h.kop.saturating_add(2))) } + } else if o2 >= 0x40 && o2.saturating_sub(0x40) <= 0x0F { + Instr::Cmovcc { + cc: o2.saturating_sub(0x40), + sz, + dst: reg_e(b, &h, ko2), + src: rm_e(b, &h, ko2), + } + } else { + Instr::Setcc { cc: o2 % 16, dst: rm_e(b, &h, ko2) } + } + } else if o == 0x89 { + if mod_e(b, ko) == 3 { + Instr::MovRR { sz, dst: rm_e(b, &h, ko), src: reg_e(b, &h, ko) } + } else { + Instr::MovStore { sz, mem: mem_e(b, &h, ko), src: reg_e(b, &h, ko) } + } + } else if o == 0x8B { + Instr::MovLoad { sz, dst: reg_e(b, &h, ko), mem: mem_e(b, &h, ko) } + } else if o >= 0xB8 && o.saturating_sub(0xB8) <= 0x07 { + if h.w { + Instr::MovImm64 { dst: o.saturating_sub(0xB8).saturating_add(h.bb.saturating_mul(8)), imm_u64: le64_e(b, ko) } + } else { + Instr::MovImm32 { dst: o.saturating_sub(0xB8).saturating_add(h.bb.saturating_mul(8)), imm_u32: le32_e(b, ko) } + } + } else if o == 0xC7 { + Instr::MovImm32S { dst: rm_e(b, &h, ko), imm: sx32_e(le32_e(b, ko.saturating_add(1))) } + } else if o == 0x81 { + Instr::AluRI { + alu: alu_of_ext_e(ext_e(b, ko)), + sz, + dst: rm_e(b, &h, ko), + imm: sx32_e(le32_e(b, ko.saturating_add(1))), + } + } else if o == 0x85 { + Instr::TestRR { sz, a: rm_e(b, &h, ko), b: reg_e(b, &h, ko) } + } else if o == 0xF7 { + let e = ext_e(b, ko); + if e == 0 { + Instr::TestRI { sz, a: rm_e(b, &h, ko), imm: sx32_e(le32_e(b, ko.saturating_add(1))) } + } else if e == 3 { + Instr::Neg { sz, r: rm_e(b, &h, ko) } + } else { + Instr::Not { sz, r: rm_e(b, &h, ko) } + } + } else if o == 0xC1 { + Instr::ShiftImm { + shift: shift_of_ext_e(ext_e(b, ko)), + sz, + r: rm_e(b, &h, ko), + count: at_e(b, ko.saturating_add(1)), + } + } else if o == 0xD3 { + Instr::ShiftCl { shift: shift_of_ext_e(ext_e(b, ko)), sz, r: rm_e(b, &h, ko) } + } else if o == 0x69 { + Instr::ImulRRI { + sz, + dst: reg_e(b, &h, ko), + src: rm_e(b, &h, ko), + imm: sx32_e(le32_e(b, ko.saturating_add(1))), + } + } else if o == 0xE9 { + Instr::Jmp { rel: sx32_e(le32_e(b, ko)) } + } else if o == 0xC3 { + Instr::Ret + } else { + let am1 = if o >= 1 { + alu_of_base_e(o.saturating_sub(1)) + } else { + None + }; + if o % 8 == 1 && am1.is_some() { + Instr::AluRR { + alu: match am1 { + Some(a) => a, + None => AluOp::Add, + }, + sz, + dst: rm_e(b, &h, ko), + src: reg_e(b, &h, ko), + } + } else { + let am3 = if o >= 3 { + alu_of_base_e(o.saturating_sub(3)) + } else { + None + }; + Instr::AluRM { + alu: match am3 { + Some(a) => a, + None => AluOp::Add, + }, + sz, + dst: reg_e(b, &h, ko), + mem: mem_e(b, &h, ko), + } + } + } + } else if h.pf == 0xF2 { + if o2 == 0x51 { + Instr::Sqrtsd { dst: reg_e(b, &h, ko2), xsrc: xsrc_e(b, &h, ko2) } + } else if o2 == 0x2A { + Instr::Cvtsi2sd { sz, dst: reg_e(b, &h, ko2), src: rm_e(b, &h, ko2) } + } else if o2 == 0x2C { + Instr::Cvttsd2si { sz, dst: reg_e(b, &h, ko2), src: rm_e(b, &h, ko2) } + } else if o2 == 0x2D { + Instr::Cvtsd2si { sz, dst: reg_e(b, &h, ko2), src: rm_e(b, &h, ko2) } + } else if o2 == 0x10 { + Instr::MovsdLoad { dst: reg_e(b, &h, ko2), mem: mem_e(b, &h, ko2) } + } else if o2 == 0x11 { + Instr::MovsdStore { mem: mem_e(b, &h, ko2), src: reg_e(b, &h, ko2) } + } else { + Instr::FpArith { + fop: fop_of_e(o2), + dst: reg_e(b, &h, ko2), + xsrc: xsrc_e(b, &h, ko2), + } + } + } else { + if o2 == 0x6E { + Instr::MovqToXmm { dst: reg_e(b, &h, ko2), src: rm_e(b, &h, ko2) } + } else if o2 == 0x7E { + Instr::MovqFromXmm { dst: rm_e(b, &h, ko2), src: reg_e(b, &h, ko2) } + } else { + Instr::Ucomisd { a: reg_e(b, &h, ko2), xsrc: xsrc_e(b, &h, ko2) } + } + } +} + +/// The spec's `mem_ok`. +pub fn mem_ok_e(m: &Mem) -> (r: bool) + ensures + r == mem_ok(*m), +{ + m.base < 16 && match &m.index { + None => true, + Some(ix) => ix.reg < 16 && ix.reg != 4 && ix.scale < 4, + } +} + +/// The spec's `xmm_src_ok`. +pub fn xmm_src_ok_e(s: &XmmSrc) -> (r: bool) + ensures + r == xmm_src_ok(*s), +{ + match s { + XmmSrc::Reg(r) => *r < 16, + XmmSrc::Mem(m) => mem_ok_e(m), + } +} + +/// The spec's `valid_instr`. +pub fn valid_exec(i: &Instr) -> (r: bool) + ensures + r == valid_instr(*i), +{ + match i { + Instr::MovRR { dst, src, .. } => *dst < 16 && *src < 16, + Instr::MovLoad { dst, mem, .. } => *dst < 16 && mem_ok_e(mem), + Instr::MovStore { mem, src, .. } => mem_ok_e(mem) && *src < 16, + Instr::MovImm32 { dst, .. } => *dst < 16, + Instr::MovImm32S { dst, .. } => *dst < 16, + Instr::MovImm64 { dst, .. } => *dst < 16, + Instr::AluRR { dst, src, .. } => *dst < 16 && *src < 16, + Instr::AluRM { dst, mem, .. } => *dst < 16 && mem_ok_e(mem), + Instr::AluRI { dst, .. } => *dst < 16, + Instr::TestRR { a, b, .. } => *a < 16 && *b < 16, + Instr::TestRI { a, .. } => *a < 16, + Instr::Neg { r, .. } => *r < 16, + Instr::Not { r, .. } => *r < 16, + Instr::ShiftImm { sz, r, count, .. } => *r < 16 && 1 <= *count && *count <= (match sz { + Size::W64 => 63u8, + Size::W32 => 31u8, + }), + Instr::ShiftCl { r, .. } => *r < 16, + Instr::ImulRR { dst, src, .. } => *dst < 16 && *src < 16, + Instr::ImulRRI { dst, src, .. } => *dst < 16 && *src < 16, + Instr::Jcc { cc, .. } => *cc < 16, + Instr::Jmp { .. } => true, + Instr::Ret => true, + Instr::Cmovcc { cc, dst, src, .. } => *cc < 16 && *dst < 16 && *src < 16, + Instr::Setcc { cc, dst } => *cc < 16 && *dst < 16, + Instr::FpArith { dst, xsrc, .. } => *dst < 16 && xmm_src_ok_e(xsrc), + Instr::Sqrtsd { dst, xsrc } => *dst < 16 && xmm_src_ok_e(xsrc), + Instr::Ucomisd { a, xsrc } => *a < 16 && xmm_src_ok_e(xsrc), + Instr::Cvtsi2sd { dst, src, .. } => *dst < 16 && *src < 16, + Instr::Cvttsd2si { dst, src, .. } => *dst < 16 && *src < 16, + Instr::Cvtsd2si { dst, src, .. } => *dst < 16 && *src < 16, + Instr::MovqToXmm { dst, src } => *dst < 16 && *src < 16, + Instr::MovqFromXmm { dst, src } => *dst < 16 && *src < 16, + Instr::MovsdLoad { dst, mem } => *dst < 16 && mem_ok_e(mem), + Instr::MovsdStore { mem, src } => mem_ok_e(mem) && *src < 16, + } +} + +/// Whether `e` is a prefix of `b`. +pub fn starts_with_exec(b: &[u8], e: &[u8]) -> (r: bool) + ensures + r == (e@.len() <= b@.len() && b@.subrange(0, e@.len() as int) == e@), +{ + if e.len() > b.len() { + return false; + } + let mut k: usize = 0; + while k < e.len() + invariant + k <= e.len(), + e.len() <= b.len(), + forall|j: int| 0 <= j < k ==> b@[j] == e@[j], + decreases e.len() - k, + { + let (x, y) = match (b.get(k), e.get(k)) { + (Some(x), Some(y)) => (*x, *y), + _ => (0u8, 0u8), + }; + if x != y { + proof { + assert(b@.subrange(0, e@.len() as int)[k as int] != e@[k as int]); + } + return false; + } + k = k.saturating_add(1); + } + assert(b@.subrange(0, e@.len() as int) =~= e@); + true +} + +/// **The executable decoder**: meets [`crate::contract::decode_ensures`]. +/// It reads the fields ([`extract_exec`]), re-encodes them +/// ([`encode_exec`]), and accepts only if the bytes begin with exactly that. +#[verifier::spinoff_prover] +#[verifier::rlimit(1000)] +pub fn decode_exec(bytes: &[u8]) -> (r: Option<(Instr, usize)>) + ensures + crate::contract::decode_ensures(bytes@, r), +{ + let c = extract_exec(bytes); + let ok = valid_exec(&c); + if !ok { + proof { + assert forall|i: Instr| !starts_with_encoding(bytes@, i) by { + if starts_with_encoding(bytes@, i) { + super::decoder_proofs::lemma_extract_starts(bytes@, i); + } + } + } + return None; + } + let e = encode_exec(&c); + let sw = starts_with_exec(bytes, &e); + if sw { + Some((c, e.len())) + } else { + proof { + assert forall|i: Instr| !starts_with_encoding(bytes@, i) by { + if starts_with_encoding(bytes@, i) { + super::decoder_proofs::lemma_extract_starts(bytes@, i); + } + } + } + None + } +} + +} // verus! diff --git a/crates/clazel-x64/src/decoder_proofs.rs b/crates/clazel-x64/src/decoder_proofs.rs new file mode 100644 index 000000000..2b4a9fe4c --- /dev/null +++ b/crates/clazel-x64/src/decoder_proofs.rs @@ -0,0 +1,997 @@ +//! Proofs about [`super::decoder`]: its completeness, `dec(encode(i) + rest) +//! == Some(i)` for every instruction of the subset, and from it the +//! prefix-freeness of the canonical encoding. + +pub use super::decoder::*; +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Four little-endian bytes read back. +pub proof fn lemma_le32(pre: Seq, v: int, rest: Seq) + ensures + le32(pre + le_bytes(v, 4) + rest, pre.len() as int) == v % 0x1_0000_0000, +{ + reveal_with_fuel(le_bytes, 5); + let s = pre + le_bytes(v, 4) + rest; + let p = pre.len() as int; + assert(le_bytes(v, 4).len() == 4); + assert(at(s, p) == v % 256); + assert(at(s, p + 1) == (v / 256) % 256); + assert(at(s, p + 2) == ((v / 256) / 256) % 256); + assert(at(s, p + 3) == (((v / 256) / 256) / 256) % 256); + assert(le32(s, p) == v % 0x1_0000_0000); +} + +/// Eight little-endian bytes are four, then four of the quotient. +pub proof fn lemma_le_bytes_8(v: int) + ensures + le_bytes(v, 8) =~= le_bytes(v, 4) + le_bytes(v / 0x1_0000_0000, 4), +{ + reveal_with_fuel(le_bytes, 9); + let w = v / 0x1_0000_0000; + assert(((v / 256) / 256) / 256 / 256 == w); + assert(le_bytes(v, 8).len() == 8); + assert((le_bytes(v, 4) + le_bytes(w, 4)).len() == 8); + assert forall|k: int| 0 <= k < 8 implies le_bytes(v, 8)[k] == (le_bytes(v, 4) + le_bytes( + w, + 4, + ))[k] by { + assert(((((v / 256) / 256) / 256) / 256) / 256 == w / 256); + assert((((((v / 256) / 256) / 256) / 256) / 256) / 256 == (w / 256) / 256); + assert(((((((v / 256) / 256) / 256) / 256) / 256) / 256) / 256 == ((w / 256) / 256) / 256); + } +} + +/// Eight little-endian bytes read back. +pub proof fn lemma_le64(pre: Seq, v: int, rest: Seq) + ensures + le64(pre + le_bytes(v, 8) + rest, pre.len() as int) == v % 0x1_0000_0000_0000_0000, +{ + lemma_le_bytes_8(v); + let w = v / 0x1_0000_0000; + let s = pre + le_bytes(v, 8) + rest; + let p = pre.len() as int; + assert(s =~= pre + le_bytes(v, 4) + (le_bytes(w, 4) + rest)); + lemma_le32(pre, v, le_bytes(w, 4) + rest); + assert(s =~= (pre + le_bytes(v, 4)) + le_bytes(w, 4) + rest); + lemma_le32(pre + le_bytes(v, 4), w, rest); + reveal_with_fuel(le_bytes, 5); + assert((pre + le_bytes(v, 4)).len() == p + 4); + assert(le64(s, p) == v % 0x1_0000_0000_0000_0000); +} + +/// The code [`Head`] gives a mandatory prefix. +pub open spec fn pfcode(pfx: Seq) -> int { + if pfx.len() == 0 { + 0 + } else { + pfx[0] as int + } +} + +/// `pfx` is none, `66` or `F2`. +pub open spec fn pfx_ok(pfx: Seq) -> bool { + pfx.len() <= 1 && (pfx.len() == 1 ==> pfx[0] == 0x66 || pfx[0] == 0xF2) +} + +/// `rest` starts with a byte that is neither a prefix nor `REX`. +pub open spec fn plain_start(rest: Seq) -> bool { + rest.len() >= 1 && !(0x40 <= rest[0] <= 0x4F) && rest[0] != 0x66 && rest[0] != 0xF2 +} + +/// What [`head`] reads from a prefix, a `REX` byte and the rest. +pub proof fn lemma_head(pfx: Seq, w: bool, r: bool, x: bool, bb: bool, force: bool, rest: Seq) + requires + pfx_ok(pfx), + plain_start(rest), + ensures + ({ + let e = pfx + rex_seq(w, r, x, bb, force) + rest; + let h = head(e); + &&& h.pf == pfcode(pfx) + &&& h.rexp == (w || r || x || bb || force) + &&& h.w == w + &&& h.r == bool_to_bit(r) + &&& h.x == bool_to_bit(x) + &&& h.bb == bool_to_bit(bb) + &&& h.kop == pfx.len() + rex_seq(w, r, x, bb, force).len() + &&& forall|j: int| + 0 <= j < rest.len() ==> at(e, h.kop + j) == #[trigger] rest[j] as int + }), +{ + let e = pfx + rex_seq(w, r, x, bb, force) + rest; + let h = head(e); + let rx = rex_seq(w, r, x, bb, force); + if w || r || x || bb || force { + assert(rx.len() == 1); + assert(rx[0] == (0x40 + 8 * bool_to_bit(w) + 4 * bool_to_bit(r) + 2 * bool_to_bit(x) + + bool_to_bit(bb)) as u8); + } else { + assert(rx.len() == 0); + } + assert(h.pf == pfcode(pfx)); + assert(h.kop == pfx.len() + rx.len()); + assert forall|j: int| 0 <= j < rest.len() implies at(e, h.kop + j) == #[trigger] rest[j] as int by {} +} + +/// The ModR/M byte of a register-direct instruction, in numbers. +pub proof fn lemma_modrm_rr(reg: int, rm: int) + requires + 0 <= reg < 16, + 0 <= rm < 16, + ensures + 0 <= 192 + (reg % 8) * 8 + rm % 8 < 256, + (192 + (reg % 8) * 8 + rm % 8) / 64 == 3, + ((192 + (reg % 8) * 8 + rm % 8) / 8) % 8 + 8 * (if reg >= 8 { + 1int + } else { + 0 + }) == reg, + (192 + (reg % 8) * 8 + rm % 8) % 8 + 8 * (if rm >= 8 { + 1int + } else { + 0 + }) == rm, +{ +} + +/// The ModR/M byte of a memory instruction with a SIB byte, in numbers. +pub proof fn lemma_modrm_mem(reg: int) + requires + 0 <= reg < 16, + ensures + 0 <= 128 + (reg % 8) * 8 + 4 < 256, + (128 + (reg % 8) * 8 + 4) / 64 == 2, + (((128 + (reg % 8) * 8 + 4) / 8) % 8) + 8 * (if reg >= 8 { + 1int + } else { + 0 + }) == reg, +{ +} + +/// The SIB byte, in numbers. +pub proof fn lemma_sib(scale: int, ix: int, base: int) + requires + 0 <= scale < 4, + 0 <= ix < 16, + 0 <= base < 16, + ensures + 0 <= scale * 64 + (ix % 8) * 8 + base % 8 < 256, + (scale * 64 + (ix % 8) * 8 + base % 8) % 8 + 8 * (if base >= 8 { + 1int + } else { + 0 + }) == base, + ((scale * 64 + (ix % 8) * 8 + base % 8) / 8) % 8 + 8 * (if ix >= 8 { + 1int + } else { + 0 + }) == ix, + (scale * 64 + (ix % 8) * 8 + base % 8) / 64 == scale, +{ +} + +/// What [`extract`]'s field readers see in a register-direct instruction +/// followed by `t`: the prefix, `W`, the opcode bytes, `mod = 11`, both +/// registers, and where the tail begins. +#[verifier::spinoff_prover] +#[verifier::rlimit(800)] +pub proof fn lemma_rr( + pfx: Seq, + w: bool, + reg: u8, + rm: u8, + opc: Seq, + force: bool, + t: Seq, +) + requires + reg < 16, + rm < 16, + pfx_ok(pfx), + plain_start(opc), + ensures + ({ + let e = enc_rr(pfx, w, reg, rm, opc, force) + t; + let h = head(e); + let ko = h.kop + opc.len(); + &&& h.pf == pfcode(pfx) + &&& h.w == w + &&& forall|j: int| 0 <= j < opc.len() ==> at(e, h.kop + j) == #[trigger] opc[j] as int + &&& mod_f(e, ko) == 3 + &&& reg_f(e, h, ko) == reg + &&& rm_f(e, h, ko) == rm + &&& ko + 1 == enc_rr(pfx, w, reg, rm, opc, force).len() + &&& forall|j: int| + 0 <= j < t.len() ==> at(e, ko + 1 + j) == #[trigger] t[j] as int + }), +{ + lemma_modrm_rr(reg as int, rm as int); + let rx = rex_seq(w, reg >= 8, false, rm >= 8, force); + let mb = modrm(3, reg as int, rm as int); + let body = opc + seq![mb] + t; + assert(plain_start(body)); + lemma_head(pfx, w, reg >= 8, false, rm >= 8, force, body); + let e = enc_rr(pfx, w, reg, rm, opc, force) + t; + assert(e =~= pfx + rx + body); + let h = head(e); + let ko = h.kop + opc.len(); + assert(body[opc.len() as int] == mb); + assert(at(e, ko) == mb as int); + assert(mb as int == 192 + (reg as int % 8) * 8 + rm as int % 8); + assert forall|j: int| 0 <= j < opc.len() implies at(e, h.kop + j) == #[trigger] opc[j] as int by { + assert(body[j] == opc[j]); + } + assert forall|j: int| 0 <= j < t.len() implies at(e, ko + 1 + j) == #[trigger] t[j] as int by { + assert(body[opc.len() + 1 + j] == t[j]); + } +} + +/// The SIB byte of a memory operand, as a number. +pub open spec fn sib_int(m: Mem) -> int { + sib_scale(m) * 64 + (sib_index_reg(m) % 8) * 8 + (m.base as int) % 8 +} + +/// The memory operand read back from its SIB byte, `REX.X`, `REX.B` and +/// displacement word is the one encoded. +pub proof fn lemma_mem_of(m: Mem) + requires + mem_ok(m), + ensures + mem_of( + sib_int(m), + bool_to_bit(sib_index_reg(m) >= 8), + bool_to_bit(m.base >= 8), + (m.disp as int) % 0x1_0000_0000, + ) == m, +{ + let ix = sib_index_reg(m); + lemma_sib(sib_scale(m), ix, m.base as int); + lemma_sx32(m.disp as int); + match m.index { + None => { + assert(ix == 4); + }, + Some(i) => { + assert(ix == i.reg as int); + }, + } +} + +/// The same for a memory operand with `mod = 10` and a SIB byte: the +/// operand read back is the one encoded. +#[verifier::spinoff_prover] +#[verifier::rlimit(400)] +pub proof fn lemma_mem(pfx: Seq, w: bool, reg: u8, m: Mem, opc: Seq, t: Seq) + requires + reg < 16, + mem_ok(m), + pfx_ok(pfx), + plain_start(opc), + ensures + ({ + let e = enc_rm_mem(pfx, w, reg, m, opc) + t; + let h = head(e); + let ko = h.kop + opc.len(); + &&& h.pf == pfcode(pfx) + &&& h.w == w + &&& forall|j: int| 0 <= j < opc.len() ==> at(e, h.kop + j) == #[trigger] opc[j] as int + &&& mod_f(e, ko) == 2 + &&& reg_f(e, h, ko) == reg + &&& mem_f(e, h, ko) == m + &&& ko + 6 == enc_rm_mem(pfx, w, reg, m, opc).len() + &&& forall|j: int| + 0 <= j < t.len() ==> at(e, ko + 6 + j) == #[trigger] t[j] as int + }), +{ + let ix = sib_index_reg(m); + lemma_modrm_mem(reg as int); + lemma_sib(sib_scale(m), ix, m.base as int); + let rx = rex_seq(w, reg >= 8, ix >= 8, m.base >= 8, false); + let mb = modrm(2, reg as int, 4); + let sibv = (sib_scale(m) * 64 + (ix % 8) * 8 + (m.base as int) % 8) as u8; + let dispb = le_bytes(m.disp as int, 4); + reveal_with_fuel(le_bytes, 5); + assert(dispb.len() == 4); + let body = opc + seq![mb, sibv] + dispb + t; + assert(plain_start(body)); + lemma_head(pfx, w, reg >= 8, ix >= 8, m.base >= 8, false, body); + let e = enc_rm_mem(pfx, w, reg, m, opc) + t; + assert(e =~= pfx + rx + body); + let h = head(e); + let ko = h.kop + opc.len(); + assert(body[opc.len() as int] == mb); + assert(body[(opc.len() + 1) as int] == sibv); + assert(at(e, ko) == mb as int); + assert(at(e, ko + 1) == sibv as int); + let pre = pfx + rx + opc + seq![mb, sibv]; + assert(e =~= pre + dispb + t); + assert(pre.len() == ko + 2); + lemma_le32(pre, m.disp as int, t); + lemma_sx32(m.disp as int); + lemma_mem_of(m); + assert(at(e, ko + 1) == sib_int(m)); + assert(mem_f(e, h, ko) == mem_of( + sib_int(m), + bool_to_bit(ix >= 8), + bool_to_bit(m.base >= 8), + (m.disp as int) % 0x1_0000_0000, + )); + assert forall|j: int| 0 <= j < opc.len() implies at(e, h.kop + j) == #[trigger] opc[j] as int by { + assert(body[j] == opc[j]); + } + assert forall|j: int| 0 <= j < t.len() implies at(e, ko + 6 + j) == #[trigger] t[j] as int by { + assert(body[opc.len() + 6 + j] == t[j]); + } +} + +/// A register-direct instruction with a 32-bit immediate after the ModR/M +/// byte: [`lemma_rr`] with `t` the immediate then `rest`, and the immediate +/// read back. +#[verifier::spinoff_prover] +#[verifier::rlimit(800)] +pub proof fn lemma_rr_imm( + pfx: Seq, + w: bool, + reg: u8, + rm: u8, + opc: Seq, + imm: i32, + rest: Seq, +) + requires + reg < 16, + rm < 16, + pfx_ok(pfx), + plain_start(opc), + ensures + ({ + let e = enc_rr(pfx, w, reg, rm, opc, false) + le_bytes(imm as int, 4) + rest; + let h = head(e); + let ko = h.kop + opc.len(); + &&& h.pf == pfcode(pfx) + &&& h.w == w + &&& forall|j: int| 0 <= j < opc.len() ==> at(e, h.kop + j) == #[trigger] opc[j] as int + &&& mod_f(e, ko) == 3 + &&& ext_f(e, ko) == (reg % 8) as int + &&& reg_f(e, h, ko) == reg + &&& rm_f(e, h, ko) == rm + &&& sx32(le32(e, ko + 1)) == imm as int + }), +{ + let t = le_bytes(imm as int, 4) + rest; + lemma_rr(pfx, w, reg, rm, opc, false, t); + let pre = enc_rr(pfx, w, reg, rm, opc, false); + let e = pre + le_bytes(imm as int, 4) + rest; + assert(e =~= pre + t); + lemma_le32(pre, imm as int, rest); + lemma_sx32(imm as int); + lemma_modrm_rr(reg as int, rm as int); +} + +/// The immediate of a 4-byte value read back, unsigned. +pub proof fn lemma_dst_low(dst: u8) + requires + dst < 16, + ensures + (dst % 8) as int + 8 * bool_to_bit(dst >= 8) == dst as int, +{ +} + +// ---- completeness, one lemma per instruction ---- +// +// Each says: `extract` of the canonical encoding of the instruction, +// followed by anything, is the instruction. The proof calls the shape lemma +// ([`lemma_rr`], [`lemma_mem`], ...) and leaves the rest to evaluating +// `extract` on the facts it gives. + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_mov_rr(sz: Size, dst: u8, src: u8, rest: Seq) + requires + valid_instr(Instr::MovRR { sz, dst, src }), + ensures + extract(encode(Instr::MovRR { sz, dst, src }) + rest) == (Instr::MovRR { sz, dst, src }), +{ + reveal(extract); + lemma_rr(Seq::empty(), is64(sz), src, dst, seq![0x89u8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_alu_rr(alu: AluOp, sz: Size, dst: u8, src: u8, rest: Seq) + requires + valid_instr(Instr::AluRR { alu, sz, dst, src }), + ensures + extract(encode(Instr::AluRR { alu, sz, dst, src }) + rest) == (Instr::AluRR { + alu, + sz, + dst, + src, + }), +{ + reveal(extract); + lemma_rr(Seq::empty(), is64(sz), src, dst, seq![(alu_base(alu) + 1) as u8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_test_rr(sz: Size, a: u8, b: u8, rest: Seq) + requires + valid_instr(Instr::TestRR { sz, a, b }), + ensures + extract(encode(Instr::TestRR { sz, a, b }) + rest) == (Instr::TestRR { sz, a, b }), +{ + reveal(extract); + lemma_rr(Seq::empty(), is64(sz), b, a, seq![0x85u8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_neg(sz: Size, r: u8, rest: Seq) + requires + valid_instr(Instr::Neg { sz, r }), + ensures + extract(encode(Instr::Neg { sz, r }) + rest) == (Instr::Neg { sz, r }), +{ + reveal(extract); + lemma_rr(Seq::empty(), is64(sz), 3, r, seq![0xF7u8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_not(sz: Size, r: u8, rest: Seq) + requires + valid_instr(Instr::Not { sz, r }), + ensures + extract(encode(Instr::Not { sz, r }) + rest) == (Instr::Not { sz, r }), +{ + reveal(extract); + lemma_rr(Seq::empty(), is64(sz), 2, r, seq![0xF7u8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_shift_cl(shift: ShiftOp, sz: Size, r: u8, rest: Seq) + requires + valid_instr(Instr::ShiftCl { shift, sz, r }), + ensures + extract(encode(Instr::ShiftCl { shift, sz, r }) + rest) == (Instr::ShiftCl { shift, sz, r }), +{ + reveal(extract); + lemma_rr(Seq::empty(), is64(sz), shift_ext(shift) as u8, r, seq![0xD3u8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_imul_rr(sz: Size, dst: u8, src: u8, rest: Seq) + requires + valid_instr(Instr::ImulRR { sz, dst, src }), + ensures + extract(encode(Instr::ImulRR { sz, dst, src }) + rest) == (Instr::ImulRR { sz, dst, src }), +{ + reveal(extract); + lemma_rr(Seq::empty(), is64(sz), dst, src, seq![0x0Fu8, 0xAFu8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_cmovcc(cc: u8, sz: Size, dst: u8, src: u8, rest: Seq) + requires + valid_instr(Instr::Cmovcc { cc, sz, dst, src }), + ensures + extract(encode(Instr::Cmovcc { cc, sz, dst, src }) + rest) == (Instr::Cmovcc { + cc, + sz, + dst, + src, + }), +{ + reveal(extract); + lemma_rr(Seq::empty(), is64(sz), dst, src, seq![0x0Fu8, (0x40 + cc as int) as u8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_setcc(cc: u8, dst: u8, rest: Seq) + requires + valid_instr(Instr::Setcc { cc, dst }), + ensures + extract(encode(Instr::Setcc { cc, dst }) + rest) == (Instr::Setcc { cc, dst }), +{ + reveal(extract); + lemma_rr( + Seq::empty(), + false, + 0, + dst, + seq![0x0Fu8, (0x90 + cc as int) as u8], + 4 <= dst && dst < 8, + rest, + ); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_cvtsi2sd(sz: Size, dst: u8, src: u8, rest: Seq) + requires + valid_instr(Instr::Cvtsi2sd { sz, dst, src }), + ensures + extract(encode(Instr::Cvtsi2sd { sz, dst, src }) + rest) == (Instr::Cvtsi2sd { + sz, + dst, + src, + }), +{ + reveal(extract); + lemma_rr(seq![0xF2u8], is64(sz), dst, src, seq![0x0Fu8, 0x2Au8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_cvttsd2si(sz: Size, dst: u8, src: u8, rest: Seq) + requires + valid_instr(Instr::Cvttsd2si { sz, dst, src }), + ensures + extract(encode(Instr::Cvttsd2si { sz, dst, src }) + rest) == (Instr::Cvttsd2si { + sz, + dst, + src, + }), +{ + reveal(extract); + lemma_rr(seq![0xF2u8], is64(sz), dst, src, seq![0x0Fu8, 0x2Cu8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_cvtsd2si(sz: Size, dst: u8, src: u8, rest: Seq) + requires + valid_instr(Instr::Cvtsd2si { sz, dst, src }), + ensures + extract(encode(Instr::Cvtsd2si { sz, dst, src }) + rest) == (Instr::Cvtsd2si { + sz, + dst, + src, + }), +{ + reveal(extract); + lemma_rr(seq![0xF2u8], is64(sz), dst, src, seq![0x0Fu8, 0x2Du8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_movq_to_xmm(dst: u8, src: u8, rest: Seq) + requires + valid_instr(Instr::MovqToXmm { dst, src }), + ensures + extract(encode(Instr::MovqToXmm { dst, src }) + rest) == (Instr::MovqToXmm { dst, src }), +{ + reveal(extract); + lemma_rr(seq![0x66u8], true, dst, src, seq![0x0Fu8, 0x6Eu8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_movq_from_xmm(dst: u8, src: u8, rest: Seq) + requires + valid_instr(Instr::MovqFromXmm { dst, src }), + ensures + extract(encode(Instr::MovqFromXmm { dst, src }) + rest) == (Instr::MovqFromXmm { + dst, + src, + }), +{ + reveal(extract); + lemma_rr(seq![0x66u8], true, src, dst, seq![0x0Fu8, 0x7Eu8], false, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_fp_arith(fop: BinOp, dst: u8, xsrc: XmmSrc, rest: Seq) + requires + valid_instr(Instr::FpArith { fop, dst, xsrc }), + ensures + extract(encode(Instr::FpArith { fop, dst, xsrc }) + rest) == (Instr::FpArith { + fop, + dst, + xsrc, + }), +{ + reveal(extract); + let opc = seq![0x0Fu8, fp_opcode(fop) as u8]; + match xsrc { + XmmSrc::Reg(r) => lemma_rr(seq![0xF2u8], false, dst, r, opc, false, rest), + XmmSrc::Mem(m) => lemma_mem(seq![0xF2u8], false, dst, m, opc, rest), + } +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_sqrtsd(dst: u8, xsrc: XmmSrc, rest: Seq) + requires + valid_instr(Instr::Sqrtsd { dst, xsrc }), + ensures + extract(encode(Instr::Sqrtsd { dst, xsrc }) + rest) == (Instr::Sqrtsd { dst, xsrc }), +{ + reveal(extract); + let opc = seq![0x0Fu8, 0x51u8]; + match xsrc { + XmmSrc::Reg(r) => lemma_rr(seq![0xF2u8], false, dst, r, opc, false, rest), + XmmSrc::Mem(m) => lemma_mem(seq![0xF2u8], false, dst, m, opc, rest), + } +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_ucomisd(a: u8, xsrc: XmmSrc, rest: Seq) + requires + valid_instr(Instr::Ucomisd { a, xsrc }), + ensures + extract(encode(Instr::Ucomisd { a, xsrc }) + rest) == (Instr::Ucomisd { a, xsrc }), +{ + reveal(extract); + let opc = seq![0x0Fu8, 0x2Eu8]; + match xsrc { + XmmSrc::Reg(r) => lemma_rr(seq![0x66u8], false, a, r, opc, false, rest), + XmmSrc::Mem(m) => lemma_mem(seq![0x66u8], false, a, m, opc, rest), + } +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_mov_load(sz: Size, dst: u8, mem: Mem, rest: Seq) + requires + valid_instr(Instr::MovLoad { sz, dst, mem }), + ensures + extract(encode(Instr::MovLoad { sz, dst, mem }) + rest) == (Instr::MovLoad { sz, dst, mem }), +{ + reveal(extract); + lemma_mem(Seq::empty(), is64(sz), dst, mem, seq![0x8Bu8], rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_mov_store(sz: Size, mem: Mem, src: u8, rest: Seq) + requires + valid_instr(Instr::MovStore { sz, mem, src }), + ensures + extract(encode(Instr::MovStore { sz, mem, src }) + rest) == (Instr::MovStore { + sz, + mem, + src, + }), +{ + reveal(extract); + lemma_mem(Seq::empty(), is64(sz), src, mem, seq![0x89u8], rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_alu_rm(alu: AluOp, sz: Size, dst: u8, mem: Mem, rest: Seq) + requires + valid_instr(Instr::AluRM { alu, sz, dst, mem }), + ensures + extract(encode(Instr::AluRM { alu, sz, dst, mem }) + rest) == (Instr::AluRM { + alu, + sz, + dst, + mem, + }), +{ + reveal(extract); + lemma_mem(Seq::empty(), is64(sz), dst, mem, seq![(alu_base(alu) + 3) as u8], rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_movsd_load(dst: u8, mem: Mem, rest: Seq) + requires + valid_instr(Instr::MovsdLoad { dst, mem }), + ensures + extract(encode(Instr::MovsdLoad { dst, mem }) + rest) == (Instr::MovsdLoad { dst, mem }), +{ + reveal(extract); + lemma_mem(seq![0xF2u8], false, dst, mem, seq![0x0Fu8, 0x10u8], rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_movsd_store(mem: Mem, src: u8, rest: Seq) + requires + valid_instr(Instr::MovsdStore { mem, src }), + ensures + extract(encode(Instr::MovsdStore { mem, src }) + rest) == (Instr::MovsdStore { mem, src }), +{ + reveal(extract); + lemma_mem(seq![0xF2u8], false, src, mem, seq![0x0Fu8, 0x11u8], rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_mov_imm32s(dst: u8, imm: i32, rest: Seq) + requires + valid_instr(Instr::MovImm32S { dst, imm }), + ensures + extract(encode(Instr::MovImm32S { dst, imm }) + rest) == (Instr::MovImm32S { dst, imm }), +{ + reveal(extract); + lemma_rr_imm(Seq::empty(), true, 0, dst, seq![0xC7u8], imm, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_alu_ri(alu: AluOp, sz: Size, dst: u8, imm: i32, rest: Seq) + requires + valid_instr(Instr::AluRI { alu, sz, dst, imm }), + ensures + extract(encode(Instr::AluRI { alu, sz, dst, imm }) + rest) == (Instr::AluRI { + alu, + sz, + dst, + imm, + }), +{ + reveal(extract); + lemma_rr_imm(Seq::empty(), is64(sz), alu_ext(alu) as u8, dst, seq![0x81u8], imm, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_test_ri(sz: Size, a: u8, imm: i32, rest: Seq) + requires + valid_instr(Instr::TestRI { sz, a, imm }), + ensures + extract(encode(Instr::TestRI { sz, a, imm }) + rest) == (Instr::TestRI { sz, a, imm }), +{ + reveal(extract); + lemma_rr_imm(Seq::empty(), is64(sz), 0, a, seq![0xF7u8], imm, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_imul_rri(sz: Size, dst: u8, src: u8, imm: i32, rest: Seq) + requires + valid_instr(Instr::ImulRRI { sz, dst, src, imm }), + ensures + extract(encode(Instr::ImulRRI { sz, dst, src, imm }) + rest) == (Instr::ImulRRI { + sz, + dst, + src, + imm, + }), +{ + reveal(extract); + lemma_rr_imm(Seq::empty(), is64(sz), dst, src, seq![0x69u8], imm, rest); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_shift_imm(shift: ShiftOp, sz: Size, r: u8, count: u8, rest: Seq) + requires + valid_instr(Instr::ShiftImm { shift, sz, r, count }), + ensures + extract(encode(Instr::ShiftImm { shift, sz, r, count }) + rest) == (Instr::ShiftImm { + shift, + sz, + r, + count, + }), +{ + reveal(extract); + lemma_rr( + Seq::empty(), + is64(sz), + shift_ext(shift) as u8, + r, + seq![0xC1u8], + false, + seq![count] + rest, + ); + assert(seq![count].len() == 1); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_mov_imm32(dst: u8, imm_u32: u32, rest: Seq) + requires + valid_instr(Instr::MovImm32 { dst, imm_u32 }), + ensures + extract(encode(Instr::MovImm32 { dst, imm_u32 }) + rest) == (Instr::MovImm32 { + dst, + imm_u32, + }), +{ + reveal(extract); + let empty = Seq::::empty(); + let rx = rex_seq(false, false, false, dst >= 8, false); + let op = (0xB8 + (dst as int) % 8) as u8; + let body = seq![op] + le_bytes(imm_u32 as int, 4) + rest; + lemma_head(empty, false, false, false, dst >= 8, false, body); + let e = encode(Instr::MovImm32 { dst, imm_u32 }) + rest; + assert(e =~= empty + rx + body); + let pre = rx + seq![op]; + assert(e =~= pre + le_bytes(imm_u32 as int, 4) + rest); + lemma_le32(pre, imm_u32 as int, rest); + lemma_dst_low(dst); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_mov_imm64(dst: u8, imm_u64: u64, rest: Seq) + requires + valid_instr(Instr::MovImm64 { dst, imm_u64 }), + ensures + extract(encode(Instr::MovImm64 { dst, imm_u64 }) + rest) == (Instr::MovImm64 { + dst, + imm_u64, + }), +{ + reveal(extract); + let empty = Seq::::empty(); + let rx = rex_seq(true, false, false, dst >= 8, false); + let op = (0xB8 + (dst as int) % 8) as u8; + let body = seq![op] + le_bytes(imm_u64 as int, 8) + rest; + lemma_head(empty, true, false, false, dst >= 8, false, body); + let e = encode(Instr::MovImm64 { dst, imm_u64 }) + rest; + assert(e =~= empty + rx + body); + let pre = rx + seq![op]; + assert(e =~= pre + le_bytes(imm_u64 as int, 8) + rest); + lemma_le64(pre, imm_u64 as int, rest); + lemma_dst_low(dst); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_jcc(cc: u8, rel: i32, rest: Seq) + requires + valid_instr(Instr::Jcc { cc, rel }), + ensures + extract(encode(Instr::Jcc { cc, rel }) + rest) == (Instr::Jcc { cc, rel }), +{ + reveal(extract); + let empty = Seq::::empty(); + let body = seq![0x0Fu8, (0x80 + cc as int) as u8] + le_bytes(rel as int, 4) + rest; + lemma_head(empty, false, false, false, false, false, body); + let e = encode(Instr::Jcc { cc, rel }) + rest; + assert(e =~= empty + rex_seq(false, false, false, false, false) + body); + let pre = seq![0x0Fu8, (0x80 + cc as int) as u8]; + assert(e =~= pre + le_bytes(rel as int, 4) + rest); + lemma_le32(pre, rel as int, rest); + lemma_sx32(rel as int); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_jmp(rel: i32, rest: Seq) + requires + valid_instr(Instr::Jmp { rel }), + ensures + extract(encode(Instr::Jmp { rel }) + rest) == (Instr::Jmp { rel }), +{ + reveal(extract); + let empty = Seq::::empty(); + let body = seq![0xE9u8] + le_bytes(rel as int, 4) + rest; + lemma_head(empty, false, false, false, false, false, body); + let e = encode(Instr::Jmp { rel }) + rest; + assert(e =~= empty + rex_seq(false, false, false, false, false) + body); + let pre = seq![0xE9u8]; + assert(e =~= pre + le_bytes(rel as int, 4) + rest); + lemma_le32(pre, rel as int, rest); + lemma_sx32(rel as int); +} + +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn c_ret(rest: Seq) + ensures + extract(encode(Instr::Ret) + rest) == Instr::Ret, +{ + reveal(extract); + let empty = Seq::::empty(); + let body = seq![0xC3u8] + rest; + lemma_head(empty, false, false, false, false, false, body); + assert(encode(Instr::Ret) + rest =~= empty + rex_seq(false, false, false, false, false) + body); +} + +/// Completeness of [`extract`]: for every instruction of the subset, the +/// fields read back from its canonical encoding, followed by any bytes, are +/// the instruction. +#[verifier::spinoff_prover] +#[verifier::rlimit(300)] +pub proof fn lemma_extract_complete(i: Instr, rest: Seq) + requires + valid_instr(i), + ensures + extract(encode(i) + rest) == i, +{ + match i { + Instr::MovRR { sz, dst, src } => c_mov_rr(sz, dst, src, rest), + Instr::MovLoad { sz, dst, mem } => c_mov_load(sz, dst, mem, rest), + Instr::MovStore { sz, mem, src } => c_mov_store(sz, mem, src, rest), + Instr::MovImm32 { dst, imm_u32 } => c_mov_imm32(dst, imm_u32, rest), + Instr::MovImm32S { dst, imm } => c_mov_imm32s(dst, imm, rest), + Instr::MovImm64 { dst, imm_u64 } => c_mov_imm64(dst, imm_u64, rest), + Instr::AluRR { alu, sz, dst, src } => c_alu_rr(alu, sz, dst, src, rest), + Instr::AluRM { alu, sz, dst, mem } => c_alu_rm(alu, sz, dst, mem, rest), + Instr::AluRI { alu, sz, dst, imm } => c_alu_ri(alu, sz, dst, imm, rest), + Instr::TestRR { sz, a, b } => c_test_rr(sz, a, b, rest), + Instr::TestRI { sz, a, imm } => c_test_ri(sz, a, imm, rest), + Instr::Neg { sz, r } => c_neg(sz, r, rest), + Instr::Not { sz, r } => c_not(sz, r, rest), + Instr::ShiftImm { shift, sz, r, count } => c_shift_imm(shift, sz, r, count, rest), + Instr::ShiftCl { shift, sz, r } => c_shift_cl(shift, sz, r, rest), + Instr::ImulRR { sz, dst, src } => c_imul_rr(sz, dst, src, rest), + Instr::ImulRRI { sz, dst, src, imm } => c_imul_rri(sz, dst, src, imm, rest), + Instr::Jcc { cc, rel } => c_jcc(cc, rel, rest), + Instr::Jmp { rel } => c_jmp(rel, rest), + Instr::Ret => c_ret(rest), + Instr::Cmovcc { cc, sz, dst, src } => c_cmovcc(cc, sz, dst, src, rest), + Instr::Setcc { cc, dst } => c_setcc(cc, dst, rest), + Instr::FpArith { fop, dst, xsrc } => c_fp_arith(fop, dst, xsrc, rest), + Instr::Sqrtsd { dst, xsrc } => c_sqrtsd(dst, xsrc, rest), + Instr::Ucomisd { a, xsrc } => c_ucomisd(a, xsrc, rest), + Instr::Cvtsi2sd { sz, dst, src } => c_cvtsi2sd(sz, dst, src, rest), + Instr::Cvttsd2si { sz, dst, src } => c_cvttsd2si(sz, dst, src, rest), + Instr::Cvtsd2si { sz, dst, src } => c_cvtsd2si(sz, dst, src, rest), + Instr::MovqToXmm { dst, src } => c_movq_to_xmm(dst, src, rest), + Instr::MovqFromXmm { dst, src } => c_movq_from_xmm(dst, src, rest), + Instr::MovsdLoad { dst, mem } => c_movsd_load(dst, mem, rest), + Instr::MovsdStore { mem, src } => c_movsd_store(mem, src, rest), + } +} + +/// If `bytes` begin with the canonical encoding of `i`, `extract` reads `i`. +pub proof fn lemma_extract_starts(bytes: Seq, i: Instr) + requires + starts_with_encoding(bytes, i), + ensures + extract(bytes) == i, +{ + let n = encode(i).len() as int; + let rest = bytes.subrange(n, bytes.len() as int); + assert(bytes =~= encode(i) + rest); + lemma_extract_complete(i, rest); +} + +/// **Completeness of the decoder** (the `None` half of +/// [`crate::contract::decode_ensures`]): if some instruction's canonical +/// encoding begins `bytes`, [`dec`] finds it. +pub proof fn lemma_dec_complete(bytes: Seq, i: Instr) + requires + starts_with_encoding(bytes, i), + ensures + dec(bytes) == Some(i), +{ + lemma_extract_starts(bytes, i); +} + +/// **The canonical encoding is a prefix code** (CLAUDE.md rule 7: proven, +/// not assumed): two instructions whose encodings both begin the same bytes +/// are equal, because the decoder reads both back from those bytes. +pub proof fn lemma_canonical_prefix_free() + ensures + crate::contract::canonical_prefix_free(), +{ + assert forall|i: Instr, j: Instr, bytes: Seq| + starts_with_encoding(bytes, i) && starts_with_encoding(bytes, j) implies i == j by { + lemma_extract_starts(bytes, i); + lemma_extract_starts(bytes, j); + } +} + +/// A signed 32-bit number read back from its four bytes. +pub proof fn lemma_sx32(v: int) + requires + -0x8000_0000 <= v < 0x8000_0000, + ensures + sx32(v % 0x1_0000_0000) == v, +{ +} + +} // verus! diff --git a/crates/clazel-x64/src/encoder.rs b/crates/clazel-x64/src/encoder.rs new file mode 100644 index 000000000..ebfda8bb0 --- /dev/null +++ b/crates/clazel-x64/src/encoder.rs @@ -0,0 +1,430 @@ +//! The **executable encoder**: [`encode_exec`] writes the canonical encoding +//! of an instruction of the subset, and is proven equal to the specification +//! [`super::spec::encode`] for every form (jit.md J.3, J.6). It is the +//! building block of the executable decoder, which accepts bytes only if +//! they are exactly what this encoder writes for what it read. +//! +//! Every function ensures that the bytes it appends are the spec's +//! (`rex_seq`, `enc_rr`, `enc_rm_mem`, `le_bytes`), so the proof of +//! [`encode_exec`] is the composition of those facts. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The prefix byte as the spec's sequence: none for 0. +pub open spec fn pfx_seq(pf: u8) -> Seq { + if pf == 0 { + Seq::empty() + } else { + seq![pf] + } +} + +/// Four little-endian bytes depend only on the value modulo 2^32. +pub proof fn lemma_le_bytes_4_mod(v: int, u: int) + requires + u == v % 0x1_0000_0000, + ensures + le_bytes(v, 4) =~= le_bytes(u, 4), +{ + reveal_with_fuel(le_bytes, 5); + let q = v / 0x1_0000_0000; + assert(v == u + 0x1_0000_0000 * q); + assert(v / 256 == u / 256 + 0x100_0000 * q) by (nonlinear_arith) + requires + v == u + 0x1_0000_0000 * q, + ; + let a = u / 256; + assert((v / 256) / 256 == a / 256 + 0x1_0000 * q) by (nonlinear_arith) + requires + v / 256 == a + 0x100_0000 * q, + ; + let b = a / 256; + assert(((v / 256) / 256) / 256 == b / 256 + 0x100 * q) by (nonlinear_arith) + requires + (v / 256) / 256 == b + 0x1_0000 * q, + ; + assert(v % 256 == u % 256) by (nonlinear_arith) + requires + v == u + 0x1_0000_0000 * q, + ; + vstd::arithmetic::div_mod::lemma_mod_multiples_vanish(0x1_0000 * q, a, 256); + assert(256 * (0x1_0000 * q) + a == v / 256); + assert((v / 256) % 256 == (u / 256) % 256); + vstd::arithmetic::div_mod::lemma_mod_multiples_vanish(0x100 * q, b, 256); + assert(256 * (0x100 * q) + b == (v / 256) / 256); + assert(((v / 256) / 256) % 256 == b % 256); + vstd::arithmetic::div_mod::lemma_mod_multiples_vanish(q, b / 256, 256); + assert(256 * q + b / 256 == ((v / 256) / 256) / 256); + assert((((v / 256) / 256) / 256) % 256 == (b / 256) % 256); +} + +/// Appends the four little-endian bytes of `x`. +pub fn push_u32(out: &mut Vec, x: u32) + ensures + final(out)@ =~= old(out)@ + le_bytes(x as int, 4), +{ + reveal_with_fuel(le_bytes, 5); + out.push((x % 256) as u8); + out.push(((x / 256) % 256) as u8); + out.push(((x / 65536) % 256) as u8); + out.push((x / 16777216) as u8); + assert(((x as int / 256) / 256) / 256 == x as int / 16777216); + assert(((x as int / 256) / 256) == x as int / 65536); + assert(out@ =~= old(out)@ + le_bytes(x as int, 4)); +} + +/// Appends the four little-endian bytes of `v`, two's complement. +pub fn push_i32(out: &mut Vec, v: i32) + ensures + final(out)@ =~= old(out)@ + le_bytes(v as int, 4), +{ + let x: u32 = if v >= 0 { + v as u32 + } else { + ((v as i64).wrapping_add(4294967296i64)) as u32 + }; + proof { + assert(x as int == (v as int) % 0x1_0000_0000); + } + push_u32(out, x); + proof { + lemma_le_bytes_4_mod(v as int, x as int); + } +} + +/// Appends the eight little-endian bytes of `x`. +pub fn push_u64(out: &mut Vec, x: u64) + ensures + final(out)@ =~= old(out)@ + le_bytes(x as int, 8), +{ + let lo = (x % 0x1_0000_0000) as u32; + let hi = (x / 0x1_0000_0000) as u32; + push_u32(out, lo); + push_u32(out, hi); + proof { + super::decoder_proofs::lemma_le_bytes_8(x as int); + lemma_le_bytes_4_mod(x as int, lo as int); + assert(out@ =~= old(out)@ + le_bytes(x as int, 8)); + } +} + +/// Appends `opc`. +pub fn push_all(out: &mut Vec, opc: &[u8]) + ensures + final(out)@ =~= old(out)@ + opc@, +{ + let mut k: usize = 0; + while k < opc.len() + invariant + k <= opc.len(), + out@ =~= old(out)@ + opc@.subrange(0, k as int), + decreases opc.len() - k, + { + if let Some(bt) = opc.get(k) { + out.push(*bt); + } + k = k.saturating_add(1); + assert(opc@.subrange(0, k as int) =~= opc@.subrange(0, k as int - 1).push(opc@[k as int + - 1])); + } + assert(opc@.subrange(0, opc.len() as int) =~= opc@); +} + +/// Appends the `REX` prefix when [`rex_seq`] has one. +pub fn push_rex(out: &mut Vec, w: bool, r: bool, x: bool, b: bool, force: bool) + ensures + final(out)@ =~= old(out)@ + rex_seq(w, r, x, b, force), +{ + if w || r || x || b || force { + let wv: u8 = if w { + 8 + } else { + 0 + }; + let rv: u8 = if r { + 4 + } else { + 0 + }; + let xv: u8 = if x { + 2 + } else { + 0 + }; + let bv: u8 = if b { + 1 + } else { + 0 + }; + let v: u8 = 0x40u8.saturating_add(wv).saturating_add(rv).saturating_add(xv).saturating_add(bv); + out.push(v); + assert(v as int == 0x40 + 8 * bool_to_bit(w) + 4 * bool_to_bit(r) + 2 * bool_to_bit(x) + + bool_to_bit(b)); + } +} + +/// Appends a register-direct instruction (the spec's `enc_rr`), `pf` being +/// the mandatory prefix or 0. +pub fn enc_rr_exec(out: &mut Vec, pf: u8, w: bool, reg: u8, rm: u8, opc: &[u8], force: bool) + requires + reg < 16, + rm < 16, + ensures + final(out)@ =~= old(out)@ + enc_rr(pfx_seq(pf), w, reg, rm, opc@, force), +{ + if pf != 0 { + out.push(pf); + } + push_rex(out, w, reg >= 8, false, rm >= 8, force); + push_all(out, opc); + out.push(0xC0u8.saturating_add((reg % 8).saturating_mul(8)).saturating_add(rm % 8)); + assert(out@ =~= old(out)@ + enc_rr(pfx_seq(pf), w, reg, rm, opc@, force)); +} + +/// Appends a memory instruction (the spec's `enc_rm_mem`). +pub fn enc_mem_exec(out: &mut Vec, pf: u8, w: bool, reg: u8, m: &Mem, opc: &[u8]) + requires + reg < 16, + mem_ok(*m), + ensures + final(out)@ =~= old(out)@ + enc_rm_mem(pfx_seq(pf), w, reg, *m, opc@), +{ + let ix: u8 = match &m.index { + None => 4, + Some(i) => i.reg, + }; + let scale: u8 = match &m.index { + None => 0, + Some(i) => i.scale, + }; + if pf != 0 { + out.push(pf); + } + push_rex(out, w, reg >= 8, ix >= 8, m.base >= 8, false); + push_all(out, opc); + out.push(0x80u8.saturating_add((reg % 8).saturating_mul(8)).saturating_add(4)); + out.push(scale.saturating_mul(64).saturating_add((ix % 8).saturating_mul(8)).saturating_add(m.base % 8)); + push_i32(out, m.disp); + assert(ix as int == sib_index_reg(*m)); + assert(scale as int == sib_scale(*m)); + assert(out@ =~= old(out)@ + enc_rm_mem(pfx_seq(pf), w, reg, *m, opc@)); +} + +/// Appends an instruction with an XMM source operand. +pub fn enc_xsrc_exec(out: &mut Vec, pf: u8, reg: u8, s: &XmmSrc, opc: &[u8]) + requires + reg < 16, + xmm_src_ok(*s), + ensures + final(out)@ =~= old(out)@ + enc_xmm_src(pfx_seq(pf), false, reg, *s, opc@), +{ + match s { + XmmSrc::Reg(r) => enc_rr_exec(out, pf, false, reg, *r, opc, false), + XmmSrc::Mem(m) => enc_mem_exec(out, pf, false, reg, m, opc), + } +} + +/// `REX.W` for an operand size. +pub fn is64_exec(sz: &Size) -> (r: bool) + ensures + r == is64(*sz), +{ + match sz { + Size::W64 => true, + Size::W32 => false, + } +} + +/// The opcode base of a group-1 operation. +pub fn alu_base_exec(op: &AluOp) -> (r: u8) + ensures + r as int == alu_base(*op), +{ + match op { + AluOp::Add => 0x00, + AluOp::Or => 0x08, + AluOp::And => 0x20, + AluOp::Sub => 0x28, + AluOp::Xor => 0x30, + AluOp::Cmp => 0x38, + } +} + +/// The ModR/M `reg` extension of a group-1 operation. +pub fn alu_ext_exec(op: &AluOp) -> (r: u8) + ensures + r as int == alu_ext(*op), +{ + match op { + AluOp::Add => 0, + AluOp::Or => 1, + AluOp::And => 4, + AluOp::Sub => 5, + AluOp::Xor => 6, + AluOp::Cmp => 7, + } +} + +/// The ModR/M `reg` extension of a shift. +pub fn shift_ext_exec(op: &ShiftOp) -> (r: u8) + ensures + r as int == shift_ext(*op), +{ + match op { + ShiftOp::Shl => 4, + ShiftOp::Shr => 5, + ShiftOp::Sar => 7, + } +} + +/// The second opcode byte of a scalar double operation. +pub fn fp_opcode_exec(op: &BinOp) -> (r: u8) + ensures + r as int == fp_opcode(*op), +{ + match op { + BinOp::Add => 0x58, + BinOp::Mul => 0x59, + BinOp::Sub => 0x5C, + BinOp::Div => 0x5E, + } +} + +/// **The executable encoder**: the canonical encoding of an instruction of +/// the subset, equal to the spec's [`encode`]. +#[verifier::spinoff_prover] +#[verifier::rlimit(3000)] +pub fn encode_exec(i: &Instr) -> (out: Vec) + requires + valid_instr(*i), + ensures + out@ =~= encode(*i), +{ + let mut o: Vec = Vec::new(); + let out = &mut o; + match i { + Instr::MovRR { sz, dst, src } => { + enc_rr_exec(out, 0, is64_exec(sz), *src, *dst, &[0x89], false); + }, + Instr::MovLoad { sz, dst, mem } => { + enc_mem_exec(out, 0, is64_exec(sz), *dst, mem, &[0x8B]); + }, + Instr::MovStore { sz, mem, src } => { + enc_mem_exec(out, 0, is64_exec(sz), *src, mem, &[0x89]); + }, + Instr::MovImm32 { dst, imm_u32 } => { + push_rex(out, false, false, false, *dst >= 8, false); + out.push(0xB8u8.saturating_add(*dst % 8)); + push_u32(out, *imm_u32); + }, + Instr::MovImm32S { dst, imm } => { + enc_rr_exec(out, 0, true, 0, *dst, &[0xC7], false); + push_i32(out, *imm); + }, + Instr::MovImm64 { dst, imm_u64 } => { + push_rex(out, true, false, false, *dst >= 8, false); + out.push(0xB8u8.saturating_add(*dst % 8)); + push_u64(out, *imm_u64); + }, + Instr::AluRR { alu, sz, dst, src } => { + let b = alu_base_exec(alu); + enc_rr_exec(out, 0, is64_exec(sz), *src, *dst, &[b.saturating_add(1)], false); + }, + Instr::AluRM { alu, sz, dst, mem } => { + let b = alu_base_exec(alu); + enc_mem_exec(out, 0, is64_exec(sz), *dst, mem, &[b.saturating_add(3)]); + }, + Instr::AluRI { alu, sz, dst, imm } => { + let e = alu_ext_exec(alu); + enc_rr_exec(out, 0, is64_exec(sz), e, *dst, &[0x81], false); + push_i32(out, *imm); + }, + Instr::TestRR { sz, a, b } => { + enc_rr_exec(out, 0, is64_exec(sz), *b, *a, &[0x85], false); + }, + Instr::TestRI { sz, a, imm } => { + enc_rr_exec(out, 0, is64_exec(sz), 0, *a, &[0xF7], false); + push_i32(out, *imm); + }, + Instr::Neg { sz, r } => { + enc_rr_exec(out, 0, is64_exec(sz), 3, *r, &[0xF7], false); + }, + Instr::Not { sz, r } => { + enc_rr_exec(out, 0, is64_exec(sz), 2, *r, &[0xF7], false); + }, + Instr::ShiftImm { shift, sz, r, count } => { + let e = shift_ext_exec(shift); + enc_rr_exec(out, 0, is64_exec(sz), e, *r, &[0xC1], false); + out.push(*count); + }, + Instr::ShiftCl { shift, sz, r } => { + let e = shift_ext_exec(shift); + enc_rr_exec(out, 0, is64_exec(sz), e, *r, &[0xD3], false); + }, + Instr::ImulRR { sz, dst, src } => { + enc_rr_exec(out, 0, is64_exec(sz), *dst, *src, &[0x0F, 0xAF], false); + }, + Instr::ImulRRI { sz, dst, src, imm } => { + enc_rr_exec(out, 0, is64_exec(sz), *dst, *src, &[0x69], false); + push_i32(out, *imm); + }, + Instr::Jcc { cc, rel } => { + out.push(0x0F); + out.push(0x80u8.saturating_add(*cc)); + push_i32(out, *rel); + }, + Instr::Jmp { rel } => { + out.push(0xE9); + push_i32(out, *rel); + }, + Instr::Ret => { + out.push(0xC3); + }, + Instr::Cmovcc { cc, sz, dst, src } => { + enc_rr_exec(out, 0, is64_exec(sz), *dst, *src, &[0x0F, 0x40u8.saturating_add(*cc)], false); + }, + Instr::Setcc { cc, dst } => { + enc_rr_exec(out, 0, false, 0, *dst, &[0x0F, 0x90u8.saturating_add(*cc)], 4 <= *dst && *dst < 8); + }, + Instr::FpArith { fop, dst, xsrc } => { + let o2 = fp_opcode_exec(fop); + enc_xsrc_exec(out, 0xF2, *dst, xsrc, &[0x0F, o2]); + }, + Instr::Sqrtsd { dst, xsrc } => { + enc_xsrc_exec(out, 0xF2, *dst, xsrc, &[0x0F, 0x51]); + }, + Instr::Ucomisd { a, xsrc } => { + enc_xsrc_exec(out, 0x66, *a, xsrc, &[0x0F, 0x2E]); + }, + Instr::Cvtsi2sd { sz, dst, src } => { + enc_rr_exec(out, 0xF2, is64_exec(sz), *dst, *src, &[0x0F, 0x2A], false); + }, + Instr::Cvttsd2si { sz, dst, src } => { + enc_rr_exec(out, 0xF2, is64_exec(sz), *dst, *src, &[0x0F, 0x2C], false); + }, + Instr::Cvtsd2si { sz, dst, src } => { + enc_rr_exec(out, 0xF2, is64_exec(sz), *dst, *src, &[0x0F, 0x2D], false); + }, + Instr::MovqToXmm { dst, src } => { + enc_rr_exec(out, 0x66, true, *dst, *src, &[0x0F, 0x6E], false); + }, + Instr::MovqFromXmm { dst, src } => { + enc_rr_exec(out, 0x66, true, *src, *dst, &[0x0F, 0x7E], false); + }, + Instr::MovsdLoad { dst, mem } => { + enc_mem_exec(out, 0xF2, false, *dst, mem, &[0x0F, 0x10]); + }, + Instr::MovsdStore { mem, src } => { + enc_mem_exec(out, 0xF2, false, *src, mem, &[0x0F, 0x11]); + }, + } + assert(o@ =~= encode(*i)); + o +} + +} // verus! diff --git a/crates/clazel-x64/src/lib.rs b/crates/clazel-x64/src/lib.rs index c30689f45..8f70551d9 100644 --- a/crates/clazel-x64/src/lib.rs +++ b/crates/clazel-x64/src/lib.rs @@ -18,5 +18,9 @@ pub mod api; pub mod contract; +pub mod decoder; +pub mod decoder_exec; +pub mod decoder_proofs; +pub mod encoder; pub mod examples; pub mod spec; diff --git a/xtask/src/status.rs b/xtask/src/status.rs index 0ecc9c1d0..95f092639 100644 --- a/xtask/src/status.rs +++ b/xtask/src/status.rs @@ -91,6 +91,8 @@ const PLACES: &[(&str, Place)] = &[ // The JIT's instruction-set layer (docs/phases/jit.md item J.2 creates them empty). ("clazel-x64", Place::Primitive), ("clazel-a64", Place::Primitive), + // The JIT's validators (item J.6), linked into whichever process validates. + ("clazel-codecheck", Place::Primitive), // The JIT's low-level IR (item J.5). ("clazel-lir", Place::Renderer), ("clazel-ipc", Place::Primitive),