Skip to content

test(js): the parser's limits were untested, and it had no fuzz target - #189

Open
jamesconwaydev wants to merge 2 commits into
js-modulesfrom
js-limits
Open

jamesconwaydev wants to merge 2 commits into
js-modulesfrom
js-limits

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Fixes #155.

Builds on #188, which this is based on; once that merges, this is rebased onto its base.

Problem. Since #187 the parser counts its depth against Limits::max_depth (256 by default) and spends a step per token from the budget, but no test showed that hostile input stops there, or that wide input stays linear. Only the lexer had a fuzz target, so nothing threw arbitrary input at the grammar or the ESTree writer.

Change.

  • tests/limits.rs, in the shape of crates/parsec-css/tests/limits.rs and crates/parsec's hostile tests:
    • A 1,000,000-deep (((( fails with too-deep on a 1 MB thread, stopping within the first thousand bytes, so its time doesn't grow with the input.
    • 100,000-deep nesting of 20 other kinds fails the same way: arrays, objects, blocks, if, loops, labels, functions, classes, arrows, conditionals, assignments, prefix operators, new, **, calls, templates, computed keys, binding and assignment patterns, and arrow parameters.
    • The same shapes 32 deep parse and write out as ESTree on that thread.
    • 23 kinds of wide input grow linearly: four times the input in under eight times the time, in one of five rounds, as grows_linearly in crates/parsec does. They're chains of operators, members, calls and optional chains, long lists of every kind, 20,000 private names, 20,000 labels, an else if chain, a switch with 20,000 cases, {a = 1} patterns waiting to be resolved, comments where the parser looks ahead, and wide arrays 50 patterns deep.
    • A member chain and a + chain a million deep (trees built in a loop) write out as ESTree on a 256 KB thread.
    • max_depth, max_steps and max_input each stop exactly where they're set. A limit above the default runs on a thread sized for it: about 1 KB a level in a release build, 3 KB in a debug one, which DEFAULT_MAX_DEPTH's doc now says.
  • fuzz/fuzz_targets/parse.rs: any input as a script and as a module, with the default limits and with max_depth: 8, max_steps: 64. Every node's and comment's span is inside the input and on character boundaries, every error's span is inside the input, and a tree that parsed writes out as ESTree. It joins lex in fuzz.yml's nightly matrix.
  • fuzz/corpus/parse: 2,603 test262 files from language/ and annexB/ under 4 KB, with their licence headers and frontmatter stripped (they're noise to a parser) and minimized with cargo fuzz cmin, 750 KB in all. test262's LICENSE is already beside the corpora.
  • The first fuzz run found a panic. In a tagged template, an invalid \u escape cut short before a character of more than one byte (tag`\u/0é`) made text::template_cooked slice inside that character. The four-digit form now reads hex digits only.
  • ROADMAP's M10 marks Limits: depth, step budget, no stack overflow #155 done, CONTRIBUTING has the new fuzz command, and the changelog has the limits.

Tests.

  • tests/limits.rs: 7 tests, 8 s in a debug build and 1.5 s in a release build.
  • text::tests::template_values fails without the fix: `\u/00é` panics with "byte index 4 is not a char boundary".
  • tests/estree/literals.js gains tag`\u/00é and \u00é`; acorn 8.19.0 agrees that its cooked value is null.
  • cargo +nightly fuzz run parse -- -max_total_time=300 -max_len=4096, as the nightly job runs it: 3,810,632 runs in 301 seconds, no crash.

cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --all --check, cargo doc with -D warnings, the fuzz manifests' cargo metadata, and scripts/check-headers.sh pass locally.

The parser counted its depth and spent its step budget, but no test
showed that hostile input stops there, and only the lexer was fuzzed.

- tests/limits.rs: a 1,000,000-deep `((((` fails with `too-deep` on a
  1 MB thread, as does nesting of 20 other kinds, and nesting inside
  the limit parses and writes out. 23 kinds of wide input parse in time
  that grows linearly, timed the way crates/parsec's hostile tests are,
  and a million-deep member chain writes out on a 256 KB thread.
  `max_depth`, `max_steps` and `max_input` each stop where they're set.
- A `parse` fuzz target: scripts and modules, with the default limits
  and small ones; every span inside the input and on character
  boundaries, and every tree written out. In the nightly matrix, seeded
  with 2,603 test262 files minimized with `cargo fuzz cmin`.
- The first run found a panic: a tagged template's cut-short `\u`
  escape before a character of more than one byte sliced inside it in
  `text::template_cooked`. Fixed, with a unit test and a fixture case.
- DEFAULT_MAX_DEPTH says how much stack a higher limit needs.
Brings the branch onto its new base after the pull request below it merged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

javascript The JavaScript parser, parsec-js

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant