Repository navigation
fix(css): deep or self-multiplying stylesheets ran out of stack or time - #128
Conversation
Deep nesting parsed fine, but a derived Clone, Debug or comparison of
the result recursed on it, and so did the cascade, so a few hundred KB
of `{` or `var(--a, ` overflowed the stack. Values, blocks and rules now
nest at most MAX_NESTING (512) deep together; deeper ones are dropped
whole as they're read, and a dropped rule is an InvalidRule error.
The cascade had its own ways to blow up:
- var() substitution counted values, not bytes, so a few doubling
custom properties over one long string copied gigabytes. A value may
now copy in at most 1 MiB.
- Each level of nesting wrote its parents' selectors in again, so
`& &, & &` doubled per level. A nested rule's selectors stop at
64 KiB, and a lone parent is written in without `:is()`, which
matched in time that doubled with each level.
- Layer names were looked up by a linear scan per part. They're in a
map now, and a name may have 64 parts.
- Rules nested deeper than 256 are left out of the cascade.
parse_block_contents on `a{}a{}…` scanned to the end of the input for
each `a`, looking for a `;` that would make it a declaration. It now
stops as soon as what it has seen can't be one.
The parsec-html-css docs linked to items rustdoc couldn't resolve. CI
now builds every crate's docs with warnings denied.
foothillsdigital
left a comment
There was a problem hiding this comment.
Checked f2aa73f. The parser and cascade changes hold up, and one check is red for a real reason.
The Windows job: test (windows-latest) fails in tests/limits.rs with thread '<unknown>' has overflowed its stack (STATUS_STACK_OVERFLOW). That's after layer_names_are_capped, shallow_enough_still_applies, var_substitution_is_capped_in_bytes and nesting_selectors_are_capped passed.
On Linux, with the test's stack made a variable (debug build, --test-threads=1):
| stack | deep_values_and_rules_stay_on_the_stack |
rules_without_semicolons_… |
|---|---|---|
| 1024 KiB | ok | ok |
| 896 KiB | overflows | ok |
| 768 KiB | overflows | ok |
So in debug the deep test needs between 896 KiB and 1 MiB of its 1 MiB thread. Windows' bigger debug frames take it over. In release, the whole file passes at 512 KiB and overflows at 256 KiB.
I'd rather not fix this by raising the test's stack. 1 MiB is Windows' default main-thread stack, so a debug build of an app that styles a hostile sheet on its main thread would still abort, which is the crash this PR is closing. Lowering whichever bound does the deepest recursing would leave real margin: MAX_NESTING (512) for the derived Clone/PartialEq/Debug, or MAX_RULE_DEPTH (256) for Collect::rule, whose frames are much bigger. I didn't narrow it to one case of the five. Running each case on its own thread at 896 KiB would show which.
The rest of my checks passed:
-
A parser differential. 200,000 random stylesheets, built from 38 fragments (
{ } ; : ! important --x & @media @layer, functions, strings, comments), up to 40 pieces each. I hashed the output of:parse_stylesheet_deep(Debug andto_string);parse_stylesheet;parse_block_contents;parse_declaration_list.
Every hash matched upstream/main. So
Watchonly stops sooner and never changes what's parsed, as you say. That includes{} !important,! importantwith a space, and custom properties holding anything. -
nest(). Writing in a single parent in place of:is(parent)is equivalent where you use it.:is(X)has X's specificity, and with&alone at the start, followed by a combinator or nothing,P > .cmatches what:is(P) > .cmatches. A compound such as&.c, a second&, or several parents still take:is(). A parent with a pseudo-element is invalid either way. -
Layers:id(&[])first still makes the unlayered name 0, andranksuses the index for the same lookups. -
The other jobs: the remaining 32 checks pass.
Debug builds on Windows ran out of a 1 MiB stack printing, cloning and cascading a stylesheet nested to the cap. MAX_NESTING is now 256 and the cascade looks 128 rules deep: on macOS the deep tests need under 512 KiB in a debug build, so a 1 MiB stack has room on every platform.
foothillsdigital
left a comment
There was a problem hiding this comment.
Re-checked 2b27298, with MAX_NESTING at 256 and MAX_RULE_DEPTH at 128. The same Linux debug measurement as before:
| stack | f2aa73f | 2b27298 |
|---|---|---|
| 1024 KiB | ok | ok |
| 896 KiB | overflows | ok |
| 512 KiB | overflows | ok |
| 448 KiB | overflows | overflows |
So the deep test now needs under 512 KiB in debug, about half of Windows' 1 MiB main thread, where it was over 896 KiB. That matches your macOS figure.
The rest still holds:
deep.rs's own test followsMAX_NESTING - 1without changes, andparsec-html-css's tests pass here.- My parser differential stayed well under either limit, so it's unaffected.
- All checks are green,
test (windows-latest)included.
Approving.
Problem
The CSS parser doesn't recurse, but plenty of code that reads its output does: the derived
Clone,DebugandPartialEqon component values and rules, and the cascade's walk over nested rules. A stylesheet orstyleattribute nested tens of thousands deep aborted the process with a stack overflow.Other inputs ran far longer than their size suggests:
var()substitution capped the number of values copied, not their size, so a short sheet could copy gigabytes.:is(), so both the selector text and the time to match it could double per level.parse_block_contentson rules with no;between them scanned to the end of the input once per rule.Separately,
cargo doc -p parsec-html-cssfailed with warnings denied: two links rustdoc couldn't resolve and one to a private item.Change
MAX_NESTING(256) deep, counted together. Anything that would open deeper is skipped whole as it's read, so nothing kept is deeper than that; a skipped rule is reported asInvalidRule.MAX_NESTINGis exported.&, or with&alone at the start, is written in as it is rather than as:is(parent). It matches the same elements with the same specificity.{}block with something other than!importantbeside it). Both the lazy parser andparse_stylesheet_deepdo this.RUSTDOCFLAGS=-D warnings, a crate at a time.Tests
crates/parsec-css/tests/limits.rs: each deep input parsed, cloned, compared, debug-printed and cascaded on a 1 MiB stack (on macOS, a debug build needs under 512 KiB; the caps were 512 and 256 at first, which overflowed 1 MiB in Windows debug builds); doubling custom properties are invalid, not slow; nested selectors under real ancestors match in bounded time; long and deeply nested layer names;a{}repeated 40,000 times through all three block parsers. Shallow nesting,& >and layers still give the same results.deep.rsnow checks where the cut happens and that nothing past it is kept.main.cargo test -p parsec-html-css,cargo clippy --workspace --all-targets -- -D warnings,cargo fmt --check, andcargo doc --no-deps -p <crate>with warnings denied for every crate.