Repository navigation
Throw InsufficientExecutionStackException instead of overflowing the stack - #244
Open
wickedmachinator wants to merge 1 commit into
Open
wickedmachinator wants to merge 1 commit into
wickedmachinator wants to merge 1 commit into
Conversation
…stack A deeply nested statement makes the parser, the fragment visitors and the script generator recurse once per level. When the stack runs out the process ends, and a stack overflow cannot be caught (issue microsoft#49). The parser and lexer post-processing scripts now insert a call to RuntimeHelpers.EnsureSufficientExecutionStack() at the start of every generated rule method, and fail the build when the number of inserted calls differs from the number of rule methods. AstGen emits the same call at the start of every generated Accept method. GenerateQueryExpression in the script generator calls it too, because a UNION chain recurses there without passing an Accept method. No grammar file changes. Callers get a catchable InsufficientExecutionStackException where the process used to end.
wickedmachinator
requested review from
allancascante,
Karl Burtram (kburtram),
Leila Lali (llali) and
mhyon
as code owners
November 24, 2026 10:51
Author
|
@microsoft-github-policy-service agree |
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.
Description
A deeply nested statement makes the parser recurse once per level, and the fragment visitors and the script generator recurse once per level of the tree. When the thread's stack runs out the process ends. A stack overflow cannot be caught in .NET, so a caller that parses input it does not control has no way to recover. #49 shows it for a script with thousands of
UNIONs.This change calls
RuntimeHelpers.EnsureSufficientExecutionStack()in three places, so the caller gets a catchableInsufficientExecutionStackExceptioninstead:ParserPostProcessing.ps1andLexerPostProcessing.ps1insert the call after ANTLR has generated the code, and fail the build when the number of inserted calls differs from the number of rule methods (16,784 today), so a rule added later cannot miss it.Acceptmethod (938), emitted by AstGen. This is the recursion in Stack overflow when parsing script with thousands of UNIONs #49.SqlScriptGeneratorVisitor.GenerateQueryExpression, because aUNIONchain recurses there without passing anAcceptmethod.No grammar file changes and no generated file is edited by hand. The library does not catch the exception; it reaches the caller of
Parse,GenerateScriptorAccept.Where the exception is thrown
The runtime keeps part of the stack in reserve, so the exception comes somewhat before the depth at which the current build overflows. Smallest failing depth on a thread with a 1 MB stack, Release build:
1 + 1 + ...termsUNION ALLbranches, visitorOn .NET 8 that is 88 to 91 percent of the old limit (Windows; Linux is the same), and the share grows with the stack size. On .NET Framework
EnsureSufficientExecutionStackthrows once about half of the stack is in use, so there a statement that needed more than half of the stack and parsed before now throws. I think that is acceptable for depths like these, but it is a behavior change and you may see it differently. If it is a problem, the call could be limited to the .NET Core targets.Cost
Parse time over the 492 files in
Test/SqlDom/TestScriptswithTSql180Parser, Release, warm, old and new build alternated in one process: +0.9 to +1.3 percent on .NET 8 on Windows, +0.9 to +2.1 percent on Linux. On .NET Framework 4.7.2 the difference was within the noise.Tests
Test/SqlDom/DeepNestingTests.csadds seven tests. Six run a deep statement on a thread with a 1 MB stack and expectInsufficientExecutionStackException: nested parentheses for every parser version, nested scalar subqueries, nested function calls, a long+chain, and a longUNION ALLchain through the script generator and through a visitor. Without the change each of them ends the test host. The seventh parses ordinary and moderately nested statements on the same thread and expects no error.The full suite passes on Windows (net8.0 and net472) and on Linux (net8.0): 1,212 tests, against 1,205 on
main.This does not bound parse time or memory. CONTRIBUTING asks for a
CHANGELOG.mdentry, but I could not find that file, so there is none.Code Changes