Skip to content

Throw InsufficientExecutionStackException instead of overflowing the stack - #244

Open
wickedmachinator wants to merge 1 commit into
microsoft:mainfrom
wickedmachinator:stack-check-in-grammar-rules
Open

wickedmachinator wants to merge 1 commit into
microsoft:mainfrom
wickedmachinator:stack-check-in-grammar-rules

Conversation

@wickedmachinator

Copy link
Copy Markdown

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 catchable InsufficientExecutionStackException instead:

  • At the start of every generated lexer and parser rule method. ParserPostProcessing.ps1 and LexerPostProcessing.ps1 insert 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.
  • At the start of every generated Accept method (938), emitted by AstGen. This is the recursion in Stack overflow when parsing script with thousands of UNIONs #49.
  • In SqlScriptGeneratorVisitor.GenerateQueryExpression, because a UNION chain recurses there without passing an Accept method.

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, GenerateScript or Accept.

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:

Statement .NET 8, overflows at .NET 8, now throws at .NET Framework 4.7.2, overflows at .NET Framework 4.7.2, now throws at
Nested parentheses 1,437 1,270 1,719 872
Nested scalar subqueries 828 743 963 492
1 + 1 + ... terms 15,919 14,294 9,132 4,053
UNION ALL branches, visitor 15,919 14,300 9,132 4,039

On .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 EnsureSufficientExecutionStack throws 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/TestScripts with TSql180Parser, 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.cs adds seven tests. Six run a deep statement on a thread with a 1 MB stack and expect InsufficientExecutionStackException: nested parentheses for every parser version, nested scalar subqueries, nested function calls, a long + chain, and a long UNION ALL chain 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.md entry, but I could not find that file, so there is none.

Code Changes

…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

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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

Labels

None yet

1 participant