Repository navigation
fix(syntax): stop slow parses and hangs around heredoc bodies - #885
Merged
Merged
Conversation
…nential time Resolving `((` and `$((` parses the text after them as arithmetic, as a subshell or command substitution, and finally as the chosen one. Heredoc bodies are parsed by fresh sub-parsers that resolve every `((` in them the same way, and an unterminated body runs to the end of the input, so each `$( ((` level holding a heredoc with the next level tripled the work. Every pass over a body also re-parsed each line that might hold its delimiter with a fresh, fully probing parser. Add a per-parse heredoc cache shared by the owner, its probes and heredoc body sub-parsers. It memoizes the body parses of probes (whose trees are thrown away), serving any nesting depth that cannot reach the limit like rawPartCache, and near-miss close candidates (returned as copies). The 175-byte mksh and 304-byte zsh fuzz finds from the unterminated-backticks attack took 6.5s and 25s; they now take a few milliseconds, and 4 KB versions take under 100ms. DebugPrint and error output are unchanged on fileTests, errorCases, known attacks, the fuzz corpora and the repo's scripts in all dialects.
The error of a heredoc body's sub-parser was assigned straight to p.err,
leaving the outer lexer on its current token. callExpr then kept meeting
a token it could not consume, and since curUnexpectedErr keeps only the
first error, it looped forever. Stop the lexer as errPass does.
Found by FuzzParseAttackMutations from the process-subst-unclosed attack:
`cat <(echo hi` followed by two `${$(EOF` heredoc levels hung in zsh mode.
Parsing also no longer runs on past the body's error. Before, a backquote
recovery further on could replace it with an error at a position that does
not exist (5:5 on a three-byte line instead of the unmatched `{` at 4:5).
Merged
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.
What changed
Two parser fixes in
shell/syntax, both found byFuzzParseAttackMutations.1. Nested
((probes with heredocs parsed in exponential timeTo resolve
((and$((, the parser parses the text after them three times: as arithmetic, as a subshell or command substitution, and finally as the chosen form. Heredoc bodies get a fresh sub-parser, which resolves every((in them the same way. An unterminated heredoc body runs to end of input. So when each$( ((level opened a heredoc whose body held the next level, the work tripled per level. Every pass over a body also re-parsed each line that might hold its closing delimiter, using a fresh, fully probing parser.The new per-parse
heredocCache(heredoc_cache.go) is shared by the owning parser, its probe clones and its heredoc body sub-parsers. It memoizes:rawPartCache, an entry serves any nesting and pattern-nesting depth that can't reach the limits, and a nesting-failure entry only serves the same depths.There was a tempting smaller fix: the probe depth handed to body sub-parsers is zeroed by
reset(), and keeping it would also cut the work. But it changes parse results (the mksh reproducer's error moved from9:5to2:6), so this PR leaves that as is.RecoverErrors(1))KeepComments,RecoverErrors(3))2. Hang after a heredoc body error
The heredoc body sub-parser's error was assigned directly to
p.err, leaving the outer lexer on its current token.callExprthen spun forever on a token it couldn't consume:curUnexpectedErris a no-op once an error is set. Repro (zsh):cat <(echo hifollowed by two${$(EOFheredoc levels. The fix stops the lexer the wayerrPassdoes, withouterrPass's heredoc-warning rewrite.Visible change for reviewers: parsing no longer runs on past a heredoc body's error. On main, a later backquote recovery could replace that error with one at a nonexistent position. For example,
$(x\nEOF\nfollowed by two`$((1+2))${x} <<EOF\n $( { EOF\nblocks reported5:5on the three-byte lineEOF. It now reports the unmatched{at4:5, which is also what main reports for the one-block version.TestHeredocBodyErrorKeepsFirstErrorpins this.Verification
testdata/fuzz, the local fuzz caches for the three parser fuzz targets, and every.sh/.bash/.bats/.zsh/.kshin the repo). Each input ran in 5 dialects × {default, KeepComments, RecoverErrors(3)}, plus each fuzz entry's own options: 168,156 results.MaxNestingDepth: 6 shapes × 91 depths × RecoverErrors 0/3; 58 s here versus 307 s on main.MaxPatternNestingDepth: 5 shapes × depths 55–70 × bash/zsh × RecoverErrors 0/3; both include the point where the limit kicks in.TestParenAmbiguityHeredocsParseInPolynomialTimeandTestHeredocBodyErrorStopsParseboth time out on main.TestHeredocBodyErrorKeepsFirstErrorcovers the error-position change, and the hang seed is added totestdata/fuzz.go test ./shell/...andmake lintpass.FuzzParseAttackMutations,FuzzParseAdversarialandFuzzParsePrinteach ran 60 s clean.SPEC
SPEC.mdgains one sentence in the parser-limits paragraph, saying heredoc bodies and near-miss closes are memoized while resolving((and$((.