Repository navigation
tokenizer: a split pattern is held to a work allowance, and one too deep or too large is refused - #3071
Conversation
|
Review: ready. We checked the split patterns of the tokenizers we serve: Qwen3.5/3.8, Flash Next, LFM3/LFM2.5, Bolt, MiniMax, Unlimited-OCR, plus GPT-2 ByteLevel. The deepest group is 1 level (limit 64), at most 2 quantifiers in an element (limit 8), and 2–123 instructions (limit 65,536). We also ran this PR's matcher on 64 KiB adversarial runs, 3,000 random mixes and real texts up to 1 MB. The worst case was 33.5 instructions a byte against the 2,048 allowance, about 61× headroom, and every split was identical to main's. The allowance resets per piece and counts instructions, not time, so it's deterministic. The regex pass is level with main within noise. One follow-up, not blocking: the new
|
…eep or too large is refused
A tokenizer.json's Split pattern runs on a backtracking matcher with no limit. `(?:a+)+b` never finished
on a run of 64 letters, 100,000 nested groups ran the parser off its stack, and
`(?:(?:a{1000}){1000}){1000}` compiled to 29 GB. A search now runs at most 2^20 instructions plus 2,048
for each byte of its text and then ends with RegexBudget, about 60 times what the shipped patterns need
on their worst runs. A pattern is refused past 65,536 instructions, 64 nested groups or 8 quantifiers on
one element. The tests move to regex_test.zig to keep regex.zig within 600 lines.
1a26bd6 to
ccb33a4
Compare
What this changes
Before: a
tokenizer.jsonSplit pattern ran on a backtracking matcher with no limit.(?:a+)+bnever finished on a run of 64 letters, 100,000 nested groups ran the pattern parser off its stack, and the 26-byte pattern(?:(?:a{1000}){1000}){1000}compiled to 29 GB. After: a search runs at most 2^20 instructions plus 2,048 for each byte of its text and then ends withRegexBudget, so the request fails by name. A pattern is refused at load past 65,536 instructions, 64 nested groups or 8 quantifiers on one element.SECURITY.mdasks for private reports, so there is no public issue.atc overlap --json: not run.Checks on this head
1a26bd6149c156b097b6d53c7c68755cfe90af97python3 tools/zig/lean_check.py, on the base and on this head: 345 on the base, 345 on this head, 0 new.zig fmt --checkandgit diff --check: passzig test --dep sys -Mroot=zig/src/core/tokenizer/tokenizer.zig -Msys=zig/src/platform/sys.zig -lc(the tokenizer module has no host step of its own): all 58 tests passedzig build test-server-cpu: 5/5 steps succeeded; 1325/1332 tests passed (7 skipped)zig build test-golden test-server-security test-pull-security: 76/76 steps succeededzig build check-native-cuda-host compile-flashnext-mtp-parity-host compile-glm-native-open: 11/11 steps succeededzig build native: 66/66 steps succeededsecurity: a pattern that backtracks without end stops at its text's allowancesecurity: a pattern nested, repeated or quantified past the compiler's limits is refused, not compiledthe split patterns checkpoints ship use a small part of their allowance on long runszig build testand every step that runs on a GPU were not run: this Mac's GPU was serving a model, and no CUDA, ROCm or Intel hardware was available.Receipt
Checklist
For a change that reaches inference:
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Not measured: no served run.Notes for the reviewer
regex.zig.regex_test.zig, which keepsregex.zigwithin 600 lines.hf_text.zigis at 600, so its message chain became a switch of the same length.Suggested changelog line
Tokenizer: a split pattern that backtracks without end stops at a work allowance for its text, and one nested or repeated past the compiler's limits is refused at load.