Harden JSON parser byte reads - #103
Open
MesTTo wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR is stacked on #61.
It turns the JSON parser byte-read and index-advance invariants into runtime assertions.
read_bytenow checksindex < lengthbefore the pointer read, andbumpchecks the same invariant before advancing. The pointer read usesadd(index)after the bound check.The added tests are public parser contract checks for truncated string inputs. They verify that normal EOF paths still return
UnexpectedEndOfJsonafter the internal byte-read hardening.Validation:
git diff --checkrustfmt --check frontend/tests/json_parser.rsjscpd --reporters ai --min-lines 5 --min-tokens 50 frontend/src/json_parser.rs frontend/tests/json_parser.rsCARGO_HOME=/home/user/Dev/.cargo-isolated RUSTFLAGS='-C target-cpu=native -Awarnings' cargo +nightly test -p mork-frontend --test json_parserCARGO_HOME=/home/user/Dev/.cargo-isolated RUSTFLAGS='-C target-cpu=native -Awarnings' cargo +nightly test -p mork-frontend --testsMeasured cost. Interleaved release A/B on a 28.5MB JSON corpus: best 2016 MB/s on main vs 1864 MB/s with this branch (which includes #61), about 7.6% on the frontend bulk-load path — the kernel is untouched. A 30-document valid-input differential (deep nesting, big mantissas, near-boundary exponents, escapes, unicode) produces byte-identical transcriber event streams, so the asserts change no accepted input. If the cost matters,
read_bytecan stay adebug_assert(every call site is eof-guarded) and onlybumpkeeps the hard assert, recovering most of it.