lexer_kit 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +265 -0
- data/Cargo.toml +37 -0
- data/README.md +7 -3
- data/crates/lexer_kit_core/Cargo.toml +17 -0
- data/crates/lexer_kit_core/src/dfa.rs +437 -0
- data/crates/lexer_kit_core/src/format.rs +599 -0
- data/crates/lexer_kit_core/src/lib.rs +18 -0
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/opcodes.rs +375 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/safety_test.rs +21 -16
- data/crates/lexer_kit_core/src/token_buffer.rs +130 -0
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/trie.rs +37 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/types.rs +1 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/vm.rs +165 -2
- data/ext/lexer_kit_rust/Cargo.toml +7 -3
- data/ext/lexer_kit_rust/src/fast_stream.rs +101 -438
- data/ext/lexer_kit_rust/src/lib.rs +200 -215
- data/lib/lexer_kit/builder/compiler.rb +50 -15
- data/lib/lexer_kit/builder/conflict_detector.rb +18 -20
- data/lib/lexer_kit/builder/validator.rb +45 -0
- data/lib/lexer_kit/builder.rb +34 -7
- data/lib/lexer_kit/cli/commands.rb +17 -7
- data/lib/lexer_kit/core/source.rb +3 -2
- data/lib/lexer_kit/debug/disassembler.rb +0 -2
- data/lib/lexer_kit/dfa/dfa_builder.rb +1 -1
- data/lib/lexer_kit/dfa/dfa_minimizer.rb +2 -2
- data/lib/lexer_kit/dfa/regex_ast.rb +4 -0
- data/lib/lexer_kit/dfa/regex_parser.rb +41 -8
- data/lib/lexer_kit/errors.rb +2 -2
- data/lib/lexer_kit/format/lkb1.rb +3 -3
- data/lib/lexer_kit/format/lkt1.rb +3 -1
- data/lib/lexer_kit/format.rb +4 -4
- data/lib/lexer_kit/ir/compiled_program.rb +39 -26
- data/lib/lexer_kit/ir/constant_pool.rb +33 -11
- data/lib/lexer_kit/ir/dfa_table.rb +9 -19
- data/lib/lexer_kit/ir/instruction.rb +3 -0
- data/lib/lexer_kit/ir/jump_table.rb +3 -13
- data/lib/lexer_kit/ir/keyword_table.rb +0 -9
- data/lib/lexer_kit/ir/limits.rb +48 -0
- data/lib/lexer_kit/ir/serializer.rb +69 -30
- data/lib/lexer_kit/ir.rb +1 -0
- data/lib/lexer_kit/trie.rb +1 -1
- data/lib/lexer_kit/version.rb +1 -1
- data/lib/lexer_kit.rb +20 -2
- data/vendor/nupur/Cargo.toml +24 -0
- data/vendor/nupur/LICENSE +176 -0
- data/vendor/nupur/NOTICE +64 -0
- data/vendor/nupur/README.ja.md +150 -0
- data/vendor/nupur/README.md +257 -0
- data/vendor/nupur/src/abi.rs +143 -0
- data/vendor/nupur/src/args.rs +318 -0
- data/vendor/nupur/src/build.rs +101 -0
- data/vendor/nupur/src/caught.rs +171 -0
- data/vendor/nupur/src/cname.rs +37 -0
- data/vendor/nupur/src/describe.rs +175 -0
- data/vendor/nupur/src/encoding.rs +240 -0
- data/vendor/nupur/src/gvl.rs +181 -0
- data/vendor/nupur/src/hash.rs +52 -0
- data/vendor/nupur/src/inject.rs +20 -0
- data/vendor/nupur/src/lib.rs +280 -0
- data/vendor/nupur/src/lint_probe.rs +142 -0
- data/vendor/nupur/src/msg.rs +193 -0
- data/vendor/nupur/src/no_unwind.rs +39 -0
- data/vendor/nupur/src/raw/check.rs +71 -0
- data/vendor/nupur/src/raw/data.rs +28 -0
- data/vendor/nupur/src/raw/define.rs +215 -0
- data/vendor/nupur/src/raw/exception.rs +69 -0
- data/vendor/nupur/src/raw/iterate.rs +115 -0
- data/vendor/nupur/src/raw/mod.rs +105 -0
- data/vendor/nupur/src/raw/protect.rs +144 -0
- data/vendor/nupur/src/raw/string.rs +148 -0
- data/vendor/nupur/src/raw/symbol.rs +38 -0
- data/vendor/nupur/src/raw/values.rs +293 -0
- data/vendor/nupur/src/rb/call.rs +118 -0
- data/vendor/nupur/src/rb/class.rs +170 -0
- data/vendor/nupur/src/rb/convert.rs +134 -0
- data/vendor/nupur/src/rb/error.rs +355 -0
- data/vendor/nupur/src/rb/global.rs +141 -0
- data/vendor/nupur/src/rb/kept.rs +55 -0
- data/vendor/nupur/src/rb/method/frame.rs +81 -0
- data/vendor/nupur/src/rb/method.rs +468 -0
- data/vendor/nupur/src/rb/mod.rs +40 -0
- data/vendor/nupur/src/rb/object.rs +66 -0
- data/vendor/nupur/src/rb/ruby.rs +107 -0
- data/vendor/nupur/src/rb/value/header.rs +65 -0
- data/vendor/nupur/src/rb/value/id.rs +61 -0
- data/vendor/nupur/src/rb/value/integer.rs +132 -0
- data/vendor/nupur/src/rb/value.rs +454 -0
- data/vendor/nupur/src/stack.rs +32 -0
- data/vendor/nupur/src/string/make.rs +234 -0
- data/vendor/nupur/src/string/mod.rs +24 -0
- data/vendor/nupur/src/string/read.rs +411 -0
- data/vendor/nupur/src/table.rs +58 -0
- data/vendor/nupur/src/typed/callbacks.rs +125 -0
- data/vendor/nupur/src/typed/data_type.rs +132 -0
- data/vendor/nupur/src/typed/embedded.rs +103 -0
- data/vendor/nupur/src/typed/external.rs +76 -0
- data/vendor/nupur/src/typed/hidden.rs +95 -0
- data/vendor/nupur/src/typed/mod.rs +373 -0
- data/vendor/nupur/src/utf8_copy.rs +262 -0
- data/vendor/nupur/sys/Cargo.toml +29 -0
- data/vendor/nupur/sys/src/data_type.rs +213 -0
- data/vendor/nupur/sys/src/int.rs +248 -0
- data/vendor/nupur/sys/src/lib.rs +61 -0
- data/vendor/nupur/sys/src/lint_probe.rs +93 -0
- data/vendor/nupur/sys/src/no_unwind.rs +99 -0
- data/vendor/nupur/sys/src/non_raising.rs +585 -0
- data/vendor/nupur/sys/src/protect.rs +349 -0
- data/vendor/nupur/sys/src/raising.rs +257 -0
- data/vendor/nupur/sys/src/value.rs +279 -0
- data/vendor/nupur.sha256 +68 -0
- metadata +102 -8
- data/ext/lexer_kit_rust/src/deserializer.rs +0 -213
- data/ext/lexer_kit_rust/src/dfa.rs +0 -217
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//! Opcode definitions and handlers
|
|
2
2
|
//!
|
|
3
3
|
//! This module defines all opcodes and their execution handlers.
|
|
4
|
-
//!
|
|
4
|
+
//! The opcode values must match lib/lexer_kit/ir/opcode.rb.
|
|
5
5
|
|
|
6
6
|
use crate::dfa::{match_literal, scan_until, scan_until_escape};
|
|
7
7
|
use crate::trie;
|
|
@@ -111,11 +111,16 @@ fn load_constant_or_fail<'a>(vm: &mut VmState<'a>, const_id: usize) -> Option<&'
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
/// Check for zero-progress after emit (infinite loop detection)
|
|
114
|
+
///
|
|
115
|
+
/// 形式仕様: formal/tla/Progress.tla (termination via zero-progress detection).
|
|
116
|
+
/// Kani では扱えない多ステップ停止性のため TLA+ のみが担保する。この関数の
|
|
117
|
+
/// カウンタ・停止ロジックを変える場合は Progress.tla を必ず見直すこと。
|
|
114
118
|
#[inline]
|
|
115
119
|
fn check_zero_progress(vm: &mut VmState) -> VmResult {
|
|
116
120
|
if vm.pos == vm.last_emit_pos {
|
|
117
121
|
vm.zero_progress_count += 1;
|
|
118
122
|
if vm.zero_progress_count >= MAX_ZERO_PROGRESS_TOKENS {
|
|
123
|
+
vm.error = Some("no progress: too many tokens ended at the same position");
|
|
119
124
|
return VmResult::Halt;
|
|
120
125
|
}
|
|
121
126
|
} else {
|
|
@@ -220,6 +225,7 @@ pub fn execute(vm: &mut VmState, opcode: u8, arg: u32) -> (VmResult, Option<Emit
|
|
|
220
225
|
/// OP_MARK: Set mark position
|
|
221
226
|
fn op_mark(vm: &mut VmState) {
|
|
222
227
|
vm.mark = vm.pos;
|
|
228
|
+
vm.has_pending_token = false;
|
|
223
229
|
vm.ip += 1;
|
|
224
230
|
}
|
|
225
231
|
|
|
@@ -510,6 +516,8 @@ fn op_emit(vm: &mut VmState, arg: u32) -> (VmResult, Option<EmitResult>) {
|
|
|
510
516
|
/// OP_EMIT_SKIP: Skip matched content without emitting
|
|
511
517
|
fn op_emit_skip(vm: &mut VmState) -> VmResult {
|
|
512
518
|
vm.mark = vm.pos;
|
|
519
|
+
// A keyword lookup made for the skipped token must not rename the next one.
|
|
520
|
+
vm.has_pending_token = false;
|
|
513
521
|
vm.ip += 1;
|
|
514
522
|
check_zero_progress(vm)
|
|
515
523
|
}
|
|
@@ -558,6 +566,7 @@ fn op_emit_and_jump(vm: &mut VmState, arg: u32) -> (VmResult, Option<EmitResult>
|
|
|
558
566
|
/// OP_EMIT_SKIP_AND_JUMP: Skip content and jump
|
|
559
567
|
fn op_emit_skip_and_jump(vm: &mut VmState, arg: u32) -> VmResult {
|
|
560
568
|
vm.mark = vm.pos;
|
|
569
|
+
vm.has_pending_token = false;
|
|
561
570
|
|
|
562
571
|
if check_zero_progress(vm) == VmResult::Halt {
|
|
563
572
|
return VmResult::Halt;
|
|
@@ -651,9 +660,25 @@ fn op_clear_best(vm: &mut VmState) {
|
|
|
651
660
|
vm.ip += 1;
|
|
652
661
|
}
|
|
653
662
|
|
|
663
|
+
/// Whether the best match ends inside the input. A malformed program can commit a stale best
|
|
664
|
+
/// match after `mark` has moved; `pos` must never pass the end of the input.
|
|
665
|
+
#[inline]
|
|
666
|
+
fn commit_fits(vm: &mut VmState) -> bool {
|
|
667
|
+
match vm.mark.checked_add(vm.best_match_len) {
|
|
668
|
+
Some(end) if end <= vm.bytes.len() => true,
|
|
669
|
+
_ => {
|
|
670
|
+
vm.error = Some("invalid program: a match ends past the input");
|
|
671
|
+
false
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
654
676
|
/// OP_COMMIT_BEST: Jump to best match action or default
|
|
655
677
|
fn op_commit_best(vm: &mut VmState, arg: u32) -> VmResult {
|
|
656
678
|
if vm.best_match_len > 0 {
|
|
679
|
+
if !commit_fits(vm) {
|
|
680
|
+
return VmResult::Halt;
|
|
681
|
+
}
|
|
657
682
|
vm.pos = vm.mark + vm.best_match_len;
|
|
658
683
|
if validate_jump(vm.best_match_ip, vm.prog) == VmResult::Halt {
|
|
659
684
|
return VmResult::Halt;
|
|
@@ -702,6 +727,9 @@ fn op_literal_trie_commit(vm: &mut VmState, arg: u32) -> VmResult {
|
|
|
702
727
|
|
|
703
728
|
// COMMIT_BEST
|
|
704
729
|
if vm.best_match_len > 0 {
|
|
730
|
+
if !commit_fits(vm) {
|
|
731
|
+
return VmResult::Halt;
|
|
732
|
+
}
|
|
705
733
|
vm.pos = vm.mark + vm.best_match_len;
|
|
706
734
|
if validate_jump(vm.best_match_ip, vm.prog) == VmResult::Halt {
|
|
707
735
|
return VmResult::Halt;
|
|
@@ -716,3 +744,349 @@ fn op_literal_trie_commit(vm: &mut VmState, arg: u32) -> VmResult {
|
|
|
716
744
|
|
|
717
745
|
VmResult::Continue
|
|
718
746
|
}
|
|
747
|
+
|
|
748
|
+
// Kani proof harness: verify op_switch_byte's dispatch soundness on real code.
|
|
749
|
+
// Mirrors formal/tla/SwitchByte.tla (DispatchSound) but checks the actual VM
|
|
750
|
+
// against a jump table with fully symbolic (possibly malformed) offsets.
|
|
751
|
+
// Run with: cargo kani
|
|
752
|
+
#[cfg(kani)]
|
|
753
|
+
mod kani_proofs {
|
|
754
|
+
use super::{
|
|
755
|
+
op_commit_best, op_emit, op_keyword_lookup, op_match_range, op_pop_mode, op_push_mode,
|
|
756
|
+
op_switch_byte, PackedArg,
|
|
757
|
+
};
|
|
758
|
+
use crate::types::{
|
|
759
|
+
CompiledProgram, Instruction, JumpTable, KeywordEntry, KeywordTable, VmResult, VmState,
|
|
760
|
+
MAX_MODE_STACK,
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
// Build a CompiledProgram with `n` no-op instructions (so validate_jump has
|
|
764
|
+
// a bound to check against).
|
|
765
|
+
fn prog_with_instructions(n: usize) -> CompiledProgram {
|
|
766
|
+
let mut prog = CompiledProgram::new();
|
|
767
|
+
for _ in 0..n {
|
|
768
|
+
prog.instructions.push(Instruction {
|
|
769
|
+
opcode: 0,
|
|
770
|
+
arg_hi: 0,
|
|
771
|
+
arg_mid: 0,
|
|
772
|
+
arg_lo: 0,
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
prog
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/// After SWITCH_BYTE dispatch with a valid jump-table id, either the VM
|
|
779
|
+
/// halts (validate_jump rejected an out-of-bounds offset) or `ip` points to
|
|
780
|
+
/// a valid instruction. A malformed jump table must never leave `ip`
|
|
781
|
+
/// pointing out of bounds without halting.
|
|
782
|
+
#[kani::proof]
|
|
783
|
+
#[kani::unwind(6)]
|
|
784
|
+
fn switch_byte_dispatch_sound() {
|
|
785
|
+
const N: usize = 4; // instruction count; valid ip is 0..N
|
|
786
|
+
|
|
787
|
+
let mut prog = prog_with_instructions(N);
|
|
788
|
+
|
|
789
|
+
// One jump table with symbolic entries: models malformed serialized
|
|
790
|
+
// data whose offsets may point anywhere, including out of bounds.
|
|
791
|
+
// Only entries for byte values 0 and 1 are reachable (byte is
|
|
792
|
+
// constrained below), so only those need to be symbolic.
|
|
793
|
+
let mut jt = JumpTable::new();
|
|
794
|
+
jt.lookup[0] = kani::any();
|
|
795
|
+
jt.lookup[1] = kani::any();
|
|
796
|
+
jt.default_offset = kani::any();
|
|
797
|
+
prog.jump_tables.push(jt);
|
|
798
|
+
prog.default_mode_offset = 0;
|
|
799
|
+
|
|
800
|
+
// Input: a single symbolic byte in {0,1}. pos selects byte-present
|
|
801
|
+
// (current_byte = Some) vs EOF (current_byte = None -> default_offset).
|
|
802
|
+
let byte: u8 = kani::any();
|
|
803
|
+
kani::assume(byte <= 1);
|
|
804
|
+
let bytes = [byte];
|
|
805
|
+
|
|
806
|
+
let mut vm = VmState::new(&prog, &bytes);
|
|
807
|
+
let pos: usize = kani::any();
|
|
808
|
+
kani::assume(pos <= 1);
|
|
809
|
+
vm.pos = pos;
|
|
810
|
+
|
|
811
|
+
// arg = 0: jump-table id 0 (valid, since one table was pushed).
|
|
812
|
+
let result = op_switch_byte(&mut vm, 0);
|
|
813
|
+
|
|
814
|
+
if let VmResult::Continue = result {
|
|
815
|
+
assert!((vm.ip as usize) < prog.instructions.len());
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/// COMMIT_BEST commits the best match correctly on the real VM:
|
|
820
|
+
/// - best_match_len > 0 => pos = mark + best_match_len, ip = best_match_ip,
|
|
821
|
+
/// if that end is inside the input; otherwise it halts with an error
|
|
822
|
+
/// - best_match_len == 0 => ip = arg (default), pos unchanged
|
|
823
|
+
/// - in both cases a Continue result leaves ip pointing at a valid
|
|
824
|
+
/// instruction (validate_jump rejects out-of-bounds action ips).
|
|
825
|
+
/// Mirrors formal/tla/CommitBest.tla (CommitCorrect / NoMatchCommit /
|
|
826
|
+
/// JumpValid) on the actual code, with a fully symbolic best match.
|
|
827
|
+
#[kani::proof]
|
|
828
|
+
#[kani::unwind(6)]
|
|
829
|
+
fn commit_best_correct() {
|
|
830
|
+
const N: usize = 4; // instruction count; valid ip is 0..N
|
|
831
|
+
|
|
832
|
+
let prog = prog_with_instructions(N);
|
|
833
|
+
let bytes: [u8; 4] = kani::any();
|
|
834
|
+
let mut vm = VmState::new(&prog, &bytes);
|
|
835
|
+
|
|
836
|
+
// Symbolic best match. mark and best_match_len are unconstrained, so a
|
|
837
|
+
// malformed program's commit past the input (or an overflowing end) is
|
|
838
|
+
// covered; best_match_ip / arg are unconstrained to model
|
|
839
|
+
// arbitrary/malformed action offsets.
|
|
840
|
+
let mark: usize = kani::any();
|
|
841
|
+
let best_len: usize = kani::any();
|
|
842
|
+
let best_ip: u32 = kani::any();
|
|
843
|
+
let arg: u32 = kani::any();
|
|
844
|
+
|
|
845
|
+
vm.mark = mark;
|
|
846
|
+
vm.best_match_len = best_len;
|
|
847
|
+
vm.best_match_ip = best_ip;
|
|
848
|
+
|
|
849
|
+
let result = op_commit_best(&mut vm, arg);
|
|
850
|
+
|
|
851
|
+
if let VmResult::Continue = result {
|
|
852
|
+
// JumpValid: committed ip is always in bounds.
|
|
853
|
+
assert!((vm.ip as usize) < prog.instructions.len());
|
|
854
|
+
if best_len > 0 {
|
|
855
|
+
// CommitCorrect: pos advanced to mark+best_len, inside the input,
|
|
856
|
+
// jumped to best_ip.
|
|
857
|
+
assert!(mark + best_len <= bytes.len());
|
|
858
|
+
assert!(vm.pos == mark + best_len);
|
|
859
|
+
assert!(vm.ip == best_ip);
|
|
860
|
+
} else {
|
|
861
|
+
// NoMatchCommit: jumped to the default arg.
|
|
862
|
+
assert!(vm.ip == arg);
|
|
863
|
+
}
|
|
864
|
+
} else if best_len > 0
|
|
865
|
+
&& mark
|
|
866
|
+
.checked_add(best_len)
|
|
867
|
+
.is_none_or(|end| end > bytes.len())
|
|
868
|
+
{
|
|
869
|
+
// A commit past the input stops with an error.
|
|
870
|
+
assert!(vm.error.is_some());
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
/// PUSH_MODE keeps the mode stack safe (inductive step): from any valid
|
|
875
|
+
/// mode_sp (0..=MAX_MODE_STACK), one push writes mode_stack in bounds and
|
|
876
|
+
/// leaves mode_sp <= MAX_MODE_STACK. At depth MAX_MODE_STACK it halts
|
|
877
|
+
/// (overflow guard) rather than writing out of bounds.
|
|
878
|
+
/// Mirrors formal/tla/ModeStack.tla (BoundedDepth / StackConsistency).
|
|
879
|
+
#[kani::proof]
|
|
880
|
+
#[kani::unwind(3)]
|
|
881
|
+
fn push_mode_preserves_stack_bounds() {
|
|
882
|
+
let prog = prog_with_instructions(2);
|
|
883
|
+
let bytes: [u8; 0] = [];
|
|
884
|
+
let mut vm = VmState::new(&prog, &bytes);
|
|
885
|
+
|
|
886
|
+
// Any valid starting depth.
|
|
887
|
+
let sp: u8 = kani::any();
|
|
888
|
+
kani::assume(sp as usize <= MAX_MODE_STACK);
|
|
889
|
+
vm.mode_sp = sp;
|
|
890
|
+
|
|
891
|
+
let arg: u32 = kani::any();
|
|
892
|
+
let _ = op_push_mode(&mut vm, arg);
|
|
893
|
+
|
|
894
|
+
// The mode_stack[mode_sp] write in-bounds and the mode_sp += 1 overflow
|
|
895
|
+
// are checked automatically by Kani; the bound is documented here.
|
|
896
|
+
assert!(vm.mode_sp as usize <= MAX_MODE_STACK);
|
|
897
|
+
assert!(vm.mode_sp >= sp);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/// POP_MODE never underflows (inductive step): from any valid mode_sp, one
|
|
901
|
+
/// pop reads mode_stack in bounds, does not increase the depth, and popping
|
|
902
|
+
/// an empty stack (mode_sp == 0) leaves it at 0 (no u8 wrap).
|
|
903
|
+
/// Mirrors formal/tla/ModeStack.tla (NoUnderflow).
|
|
904
|
+
#[kani::proof]
|
|
905
|
+
#[kani::unwind(3)]
|
|
906
|
+
fn pop_mode_no_underflow() {
|
|
907
|
+
let prog = prog_with_instructions(2);
|
|
908
|
+
let bytes: [u8; 0] = [];
|
|
909
|
+
let mut vm = VmState::new(&prog, &bytes);
|
|
910
|
+
|
|
911
|
+
let sp: u8 = kani::any();
|
|
912
|
+
kani::assume(sp as usize <= MAX_MODE_STACK);
|
|
913
|
+
vm.mode_sp = sp;
|
|
914
|
+
|
|
915
|
+
let _ = op_pop_mode(&mut vm);
|
|
916
|
+
|
|
917
|
+
// No underflow / no OOB read are checked automatically by Kani.
|
|
918
|
+
assert!(vm.mode_sp as usize <= MAX_MODE_STACK);
|
|
919
|
+
assert!(vm.mode_sp <= sp);
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
/// PackedArg::from_u32 decodes a 24-bit argument into a 10-bit upper field
|
|
923
|
+
/// and a 14-bit lower field. Verify the fields are always within range and
|
|
924
|
+
/// losslessly reconstruct the low 24 bits (the packed encoding is a
|
|
925
|
+
/// bijection on 24-bit values). Used by SET_MATCH / EMIT_AND_JUMP etc.
|
|
926
|
+
#[kani::proof]
|
|
927
|
+
fn packed_arg_decode_is_consistent() {
|
|
928
|
+
let val: u32 = kani::any();
|
|
929
|
+
let p = PackedArg::from_u32(val);
|
|
930
|
+
|
|
931
|
+
// Field ranges: upper is 10 bits, lower is 14 bits.
|
|
932
|
+
assert!(p.upper < 1024); // 2^10
|
|
933
|
+
assert!(p.lower < 16384); // 2^14
|
|
934
|
+
|
|
935
|
+
// Lossless over the low 24 bits: (upper << 14) | lower == val & 0xFFFFFF.
|
|
936
|
+
assert!(((p.upper as u32) << 14) | p.lower == val & 0x00FF_FFFF);
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/// op_match_range decodes [lo,hi] from arg and matches one byte. Verify it
|
|
940
|
+
/// is memory-safe and consistent for a symbolic arg and symbolic input:
|
|
941
|
+
/// it advances pos by at most 1, only succeeds within the input, and a
|
|
942
|
+
/// success implies the current byte was in [lo,hi].
|
|
943
|
+
#[kani::proof]
|
|
944
|
+
#[kani::unwind(3)]
|
|
945
|
+
fn match_range_is_consistent() {
|
|
946
|
+
let prog = prog_with_instructions(2);
|
|
947
|
+
|
|
948
|
+
// symbolic 0- or 1-byte input
|
|
949
|
+
let byte: u8 = kani::any();
|
|
950
|
+
let buf = [byte];
|
|
951
|
+
let len: usize = kani::any();
|
|
952
|
+
kani::assume(len <= 1);
|
|
953
|
+
let input = &buf[..len];
|
|
954
|
+
|
|
955
|
+
let mut vm = VmState::new(&prog, input);
|
|
956
|
+
let start_pos = vm.pos;
|
|
957
|
+
|
|
958
|
+
let arg: u32 = kani::any();
|
|
959
|
+
op_match_range(&mut vm, arg);
|
|
960
|
+
|
|
961
|
+
let lo = ((arg >> 8) & 0xFF) as u8;
|
|
962
|
+
let hi = (arg & 0xFF) as u8;
|
|
963
|
+
|
|
964
|
+
// pos advances by 0 or 1, never past the input.
|
|
965
|
+
assert!(vm.pos == start_pos || vm.pos == start_pos + 1);
|
|
966
|
+
assert!(vm.pos <= input.len());
|
|
967
|
+
// last_match_len is at most one byte.
|
|
968
|
+
assert!(vm.last_match_len <= 1);
|
|
969
|
+
if !vm.failed {
|
|
970
|
+
// success => a byte was present and in range, one byte consumed.
|
|
971
|
+
assert!(vm.pos == start_pos + 1);
|
|
972
|
+
assert!(start_pos < input.len());
|
|
973
|
+
assert!(input[start_pos] >= lo && input[start_pos] <= hi);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
/// VmState::update_best_if_better keeps the best match per the maximal-munch
|
|
978
|
+
/// ordering: last_match replaces best iff it is longer, or the same length
|
|
979
|
+
/// with a strictly smaller definition order. Otherwise best is unchanged.
|
|
980
|
+
/// Mirrors formal/tla/LongestMatch.tla on the real code.
|
|
981
|
+
#[kani::proof]
|
|
982
|
+
fn update_best_if_better_is_correct() {
|
|
983
|
+
let prog = prog_with_instructions(1);
|
|
984
|
+
let bytes: [u8; 0] = [];
|
|
985
|
+
let mut vm = VmState::new(&prog, &bytes);
|
|
986
|
+
|
|
987
|
+
// Symbolic current best and new candidate (last match).
|
|
988
|
+
let ob_len: usize = kani::any();
|
|
989
|
+
let ob_ord: u16 = kani::any();
|
|
990
|
+
let ob_ip: u32 = kani::any();
|
|
991
|
+
let lm_len: usize = kani::any();
|
|
992
|
+
let lm_ord: u16 = kani::any();
|
|
993
|
+
let lm_ip: u32 = kani::any();
|
|
994
|
+
|
|
995
|
+
vm.best_match_len = ob_len;
|
|
996
|
+
vm.best_match_order = ob_ord;
|
|
997
|
+
vm.best_match_ip = ob_ip;
|
|
998
|
+
vm.last_match_len = lm_len;
|
|
999
|
+
vm.last_match_order = lm_ord;
|
|
1000
|
+
vm.last_match_ip = lm_ip;
|
|
1001
|
+
|
|
1002
|
+
vm.update_best_if_better();
|
|
1003
|
+
|
|
1004
|
+
let better = lm_len > ob_len || (lm_len == ob_len && lm_ord < ob_ord);
|
|
1005
|
+
if better {
|
|
1006
|
+
assert!(vm.best_match_len == lm_len);
|
|
1007
|
+
assert!(vm.best_match_order == lm_ord);
|
|
1008
|
+
assert!(vm.best_match_ip == lm_ip);
|
|
1009
|
+
} else {
|
|
1010
|
+
assert!(vm.best_match_len == ob_len);
|
|
1011
|
+
assert!(vm.best_match_order == ob_ord);
|
|
1012
|
+
assert!(vm.best_match_ip == ob_ip);
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
/// op_keyword_lookup is memory-safe when pos <= bytes.len() (a VM invariant)
|
|
1017
|
+
/// and resolves the pending token correctly: the matched text is looked up
|
|
1018
|
+
/// in the keyword table, yielding the entry's token id or the base id.
|
|
1019
|
+
/// Uses a concrete table {"ab" -> 9, base 8}. Mirrors KeywordLookup.tla.
|
|
1020
|
+
#[kani::proof]
|
|
1021
|
+
#[kani::unwind(3)]
|
|
1022
|
+
fn keyword_lookup_resolves_correctly() {
|
|
1023
|
+
let mut prog = prog_with_instructions(2);
|
|
1024
|
+
let mut kt = KeywordTable::new(8); // base token id
|
|
1025
|
+
kt.entries.push(KeywordEntry {
|
|
1026
|
+
keyword: vec![0u8, 1u8], // "ab"
|
|
1027
|
+
token_id: 9,
|
|
1028
|
+
});
|
|
1029
|
+
prog.keyword_tables.push(kt);
|
|
1030
|
+
|
|
1031
|
+
// Symbolic input over {0,1}.
|
|
1032
|
+
let buf: [u8; 3] = kani::any();
|
|
1033
|
+
kani::assume(buf[0] <= 1 && buf[1] <= 1 && buf[2] <= 1);
|
|
1034
|
+
let blen: usize = kani::any();
|
|
1035
|
+
kani::assume(blen <= 3);
|
|
1036
|
+
let bytes = &buf[..blen];
|
|
1037
|
+
|
|
1038
|
+
let mut vm = VmState::new(&prog, bytes);
|
|
1039
|
+
let mark: usize = kani::any();
|
|
1040
|
+
let pos: usize = kani::any();
|
|
1041
|
+
// VM invariant at call sites: mark <= pos <= input length.
|
|
1042
|
+
kani::assume(mark <= pos && pos <= bytes.len());
|
|
1043
|
+
vm.mark = mark;
|
|
1044
|
+
vm.pos = pos;
|
|
1045
|
+
|
|
1046
|
+
op_keyword_lookup(&mut vm, 0);
|
|
1047
|
+
|
|
1048
|
+
assert!(vm.has_pending_token);
|
|
1049
|
+
// Resolution: token id is 9 iff the matched text is exactly "ab", else 8.
|
|
1050
|
+
let text = &bytes[mark..pos];
|
|
1051
|
+
let is_ab = text.len() == 2 && text[0] == 0 && text[1] == 1;
|
|
1052
|
+
if is_ab {
|
|
1053
|
+
assert!(vm.pending_token_id == 9);
|
|
1054
|
+
} else {
|
|
1055
|
+
assert!(vm.pending_token_id == 8);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
/// op_emit computes the token span correctly and advances mark: the emitted
|
|
1060
|
+
/// span starts at the old mark, its length is pos-mark (saturating, never
|
|
1061
|
+
/// underflowing), and mark moves to pos. With pos >= mark the span is
|
|
1062
|
+
/// contiguous up to the new mark (single-step tiling). Mirrors TokenSpans.tla.
|
|
1063
|
+
#[kani::proof]
|
|
1064
|
+
#[kani::unwind(3)]
|
|
1065
|
+
fn emit_span_is_correct() {
|
|
1066
|
+
let prog = prog_with_instructions(2);
|
|
1067
|
+
let bytes: [u8; 0] = [];
|
|
1068
|
+
let mut vm = VmState::new(&prog, &bytes);
|
|
1069
|
+
|
|
1070
|
+
let mark: usize = kani::any();
|
|
1071
|
+
let pos: usize = kani::any();
|
|
1072
|
+
kani::assume(mark <= 1_000_000 && pos <= 1_000_000);
|
|
1073
|
+
vm.mark = mark;
|
|
1074
|
+
vm.pos = pos;
|
|
1075
|
+
vm.has_pending_token = false; // token id comes from arg
|
|
1076
|
+
// zero_progress_count is 0 (from new), so check_zero_progress cannot halt
|
|
1077
|
+
// in a single step and op_emit always yields Some.
|
|
1078
|
+
let arg: u32 = kani::any();
|
|
1079
|
+
|
|
1080
|
+
let (_result, emit) = op_emit(&mut vm, arg);
|
|
1081
|
+
let e = emit.unwrap();
|
|
1082
|
+
|
|
1083
|
+
assert!(e.start == mark);
|
|
1084
|
+
assert!(e.length == pos.saturating_sub(mark));
|
|
1085
|
+
assert!(vm.mark == pos); // mark advanced to pos
|
|
1086
|
+
if pos >= mark {
|
|
1087
|
+
// contiguous: span end == new mark == pos
|
|
1088
|
+
assert!(e.start + e.length == pos);
|
|
1089
|
+
assert!(e.start + e.length == vm.mark);
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
@@ -3,15 +3,23 @@
|
|
|
3
3
|
//! Run with: cargo +nightly miri test
|
|
4
4
|
//!
|
|
5
5
|
//! These tests verify memory safety for:
|
|
6
|
-
//! -
|
|
6
|
+
//! - TokenBuffer (Vec-based token buffer)
|
|
7
7
|
//! - DFA execution (boundary conditions)
|
|
8
8
|
//! - Trie execution (malformed data handling)
|
|
9
9
|
//! - VM execution (infinite loop detection, stack overflow)
|
|
10
|
+
//!
|
|
11
|
+
//! Scope: Miri covers the pure-Rust core reachable from these tests. It does
|
|
12
|
+
//! NOT execute the extension's boundary with Ruby (Nupur; e.g. reading the
|
|
13
|
+
//! program binary out of a Ruby String in `ext/lexer_kit_rust/src/lib.rs`),
|
|
14
|
+
//! which requires a real Ruby runtime and is exercised by the rspec suite
|
|
15
|
+
//! instead. The Kani harnesses (`cargo kani`) additionally
|
|
16
|
+
//! prove bounds/overflow safety over symbolic inputs for the scan/DFA/trie
|
|
17
|
+
//! and opcode functions.
|
|
10
18
|
|
|
11
19
|
#[cfg(test)]
|
|
12
20
|
mod tests {
|
|
13
21
|
use crate::dfa::{match_literal, scan_until, scan_until_escape};
|
|
14
|
-
use crate::
|
|
22
|
+
use crate::token_buffer::TokenBuffer;
|
|
15
23
|
use crate::trie;
|
|
16
24
|
use crate::types::{
|
|
17
25
|
CompiledProgram, DfaTable as DfaTableType, Instruction, KeywordEntry, KeywordTable, Mode,
|
|
@@ -20,7 +28,7 @@ mod tests {
|
|
|
20
28
|
use crate::vm;
|
|
21
29
|
|
|
22
30
|
// =========================================================================
|
|
23
|
-
//
|
|
31
|
+
// TokenBuffer tests
|
|
24
32
|
// =========================================================================
|
|
25
33
|
|
|
26
34
|
#[test]
|
|
@@ -30,7 +38,7 @@ mod tests {
|
|
|
30
38
|
(2u16, 5usize, 3usize),
|
|
31
39
|
(3u16, 8usize, 2usize),
|
|
32
40
|
];
|
|
33
|
-
let mut buffer =
|
|
41
|
+
let mut buffer = TokenBuffer::from_tokens(tokens).unwrap();
|
|
34
42
|
|
|
35
43
|
assert!(!buffer.eof());
|
|
36
44
|
assert_eq!(buffer.token_id(), 1);
|
|
@@ -50,7 +58,7 @@ mod tests {
|
|
|
50
58
|
|
|
51
59
|
#[test]
|
|
52
60
|
fn test_empty_buffer() {
|
|
53
|
-
let buffer =
|
|
61
|
+
let buffer = TokenBuffer::from_tokens(vec![]).unwrap();
|
|
54
62
|
assert!(buffer.eof());
|
|
55
63
|
assert_eq!(buffer.token_id(), -1);
|
|
56
64
|
assert_eq!(buffer.start(), -1);
|
|
@@ -60,7 +68,7 @@ mod tests {
|
|
|
60
68
|
#[test]
|
|
61
69
|
fn test_peek() {
|
|
62
70
|
let tokens = vec![(1u16, 0usize, 5usize), (2u16, 5usize, 3usize)];
|
|
63
|
-
let buffer =
|
|
71
|
+
let buffer = TokenBuffer::from_tokens(tokens).unwrap();
|
|
64
72
|
|
|
65
73
|
assert_eq!(buffer.peek_token_id(0), 1);
|
|
66
74
|
assert_eq!(buffer.peek_token_id(1), 2);
|
|
@@ -76,7 +84,7 @@ mod tests {
|
|
|
76
84
|
#[test]
|
|
77
85
|
fn test_single_token() {
|
|
78
86
|
let tokens = vec![(42u16, 100usize, 50usize)];
|
|
79
|
-
let mut buffer =
|
|
87
|
+
let mut buffer = TokenBuffer::from_tokens(tokens).unwrap();
|
|
80
88
|
|
|
81
89
|
assert_eq!(buffer.count(), 1);
|
|
82
90
|
assert_eq!(buffer.index(), 0);
|
|
@@ -91,16 +99,14 @@ mod tests {
|
|
|
91
99
|
|
|
92
100
|
#[test]
|
|
93
101
|
fn test_many_tokens() {
|
|
94
|
-
let tokens: Vec<_> = (0..1000)
|
|
95
|
-
|
|
96
|
-
.collect();
|
|
97
|
-
let mut buffer = FastTokenBuffer::from_tokens(tokens).unwrap();
|
|
102
|
+
let tokens: Vec<_> = (0..1000).map(|i| (i as u16, i * 10, 10usize)).collect();
|
|
103
|
+
let mut buffer = TokenBuffer::from_tokens(tokens).unwrap();
|
|
98
104
|
|
|
99
105
|
assert_eq!(buffer.count(), 1000);
|
|
100
106
|
|
|
101
107
|
for i in 0..1000 {
|
|
102
|
-
assert_eq!(buffer.token_id(), i
|
|
103
|
-
assert_eq!(buffer.start(),
|
|
108
|
+
assert_eq!(buffer.token_id(), i);
|
|
109
|
+
assert_eq!(buffer.start(), i * 10);
|
|
104
110
|
buffer.advance();
|
|
105
111
|
}
|
|
106
112
|
|
|
@@ -110,7 +116,7 @@ mod tests {
|
|
|
110
116
|
#[test]
|
|
111
117
|
fn test_advance_past_end() {
|
|
112
118
|
let tokens = vec![(1u16, 0usize, 1usize)];
|
|
113
|
-
let mut buffer =
|
|
119
|
+
let mut buffer = TokenBuffer::from_tokens(tokens).unwrap();
|
|
114
120
|
|
|
115
121
|
buffer.advance();
|
|
116
122
|
assert!(buffer.eof());
|
|
@@ -485,7 +491,7 @@ mod tests {
|
|
|
485
491
|
|
|
486
492
|
let tokens: Vec<_> = (0..count).map(|i| (i as u16, i, 1usize)).collect();
|
|
487
493
|
|
|
488
|
-
let mut buffer =
|
|
494
|
+
let mut buffer = TokenBuffer::from_tokens(tokens).unwrap();
|
|
489
495
|
|
|
490
496
|
for i in 0..count {
|
|
491
497
|
assert_eq!(buffer.token_id(), i as i32);
|
|
@@ -494,5 +500,4 @@ mod tests {
|
|
|
494
500
|
|
|
495
501
|
assert!(buffer.eof());
|
|
496
502
|
}
|
|
497
|
-
|
|
498
503
|
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
//! Token buffer: the tokens of one input, read in order with lookahead.
|
|
2
|
+
//!
|
|
3
|
+
//! Ruby-independent: the extension's LexStream wraps it.
|
|
4
|
+
|
|
5
|
+
const STRIDE: usize = 3;
|
|
6
|
+
|
|
7
|
+
/// Token buffer using Vec for safe memory management
|
|
8
|
+
///
|
|
9
|
+
/// Memory allocation and deallocation is handled safely by Vec.
|
|
10
|
+
pub struct TokenBuffer {
|
|
11
|
+
/// Flat array: [token_id, start, len] * count
|
|
12
|
+
data: Vec<i32>,
|
|
13
|
+
/// Number of tokens (data.len() / 3)
|
|
14
|
+
count: usize,
|
|
15
|
+
/// Current position
|
|
16
|
+
index: usize,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// `len` is the length of the current token, not of the buffer, so `is_empty` would mislead.
|
|
20
|
+
#[allow(clippy::len_without_is_empty)]
|
|
21
|
+
impl TokenBuffer {
|
|
22
|
+
/// Create from collected tokens
|
|
23
|
+
pub fn from_tokens(tokens: Vec<(u16, usize, usize)>) -> Result<Self, &'static str> {
|
|
24
|
+
let count = tokens.len();
|
|
25
|
+
let mut data = Vec::with_capacity(count * STRIDE);
|
|
26
|
+
|
|
27
|
+
for (token_id, start, length) in tokens {
|
|
28
|
+
if start > i32::MAX as usize || length > i32::MAX as usize {
|
|
29
|
+
return Err("token position exceeds maximum value (input too large)");
|
|
30
|
+
}
|
|
31
|
+
data.push(token_id as i32);
|
|
32
|
+
data.push(start as i32);
|
|
33
|
+
data.push(length as i32);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
Ok(Self {
|
|
37
|
+
data,
|
|
38
|
+
count,
|
|
39
|
+
index: 0,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// Bytes this buffer holds on the heap (for the extension's memsize report).
|
|
44
|
+
pub fn heap_bytes(&self) -> usize {
|
|
45
|
+
self.data.capacity() * std::mem::size_of::<i32>()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#[inline(always)]
|
|
49
|
+
pub fn eof(&self) -> bool {
|
|
50
|
+
self.index >= self.count
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#[inline(always)]
|
|
54
|
+
pub fn token_id(&self) -> i32 {
|
|
55
|
+
if self.index >= self.count {
|
|
56
|
+
-1
|
|
57
|
+
} else {
|
|
58
|
+
self.data[self.index * STRIDE]
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
#[inline(always)]
|
|
63
|
+
pub fn start(&self) -> i32 {
|
|
64
|
+
if self.index >= self.count {
|
|
65
|
+
-1
|
|
66
|
+
} else {
|
|
67
|
+
self.data[self.index * STRIDE + 1]
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#[inline(always)]
|
|
72
|
+
pub fn len(&self) -> i32 {
|
|
73
|
+
if self.index >= self.count {
|
|
74
|
+
-1
|
|
75
|
+
} else {
|
|
76
|
+
self.data[self.index * STRIDE + 2]
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
#[inline(always)]
|
|
81
|
+
pub fn peek_token_id(&self, n: usize) -> i32 {
|
|
82
|
+
let i = self.index + n;
|
|
83
|
+
if i >= self.count {
|
|
84
|
+
-1
|
|
85
|
+
} else {
|
|
86
|
+
self.data[i * STRIDE]
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#[inline(always)]
|
|
91
|
+
pub fn peek_start(&self, n: usize) -> i32 {
|
|
92
|
+
let i = self.index + n;
|
|
93
|
+
if i >= self.count {
|
|
94
|
+
-1
|
|
95
|
+
} else {
|
|
96
|
+
self.data[i * STRIDE + 1]
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#[inline(always)]
|
|
101
|
+
pub fn peek_len(&self, n: usize) -> i32 {
|
|
102
|
+
let i = self.index + n;
|
|
103
|
+
if i >= self.count {
|
|
104
|
+
-1
|
|
105
|
+
} else {
|
|
106
|
+
self.data[i * STRIDE + 2]
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#[inline(always)]
|
|
111
|
+
pub fn advance(&mut self) {
|
|
112
|
+
if self.index < self.count {
|
|
113
|
+
self.index += 1;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// Get total number of tokens (for testing)
|
|
118
|
+
#[cfg(test)]
|
|
119
|
+
pub fn count(&self) -> usize {
|
|
120
|
+
self.count
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/// Get current index (for testing)
|
|
124
|
+
#[cfg(test)]
|
|
125
|
+
pub fn index(&self) -> usize {
|
|
126
|
+
self.index
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// No manual Drop needed - Vec handles memory deallocation automatically
|