Repository navigation
Fix stack overflow abort on deeply nested function blocks - #1191
Open
MaxWang007 wants to merge 2 commits into
Open
MaxWang007 wants to merge 2 commits into
MaxWang007 wants to merge 2 commits into
Conversation
full_moon's parse_stmt keeps a large frame live across each nested block, and the formatter walks the same tree recursively. CLI worker threads only have a 2MiB stack, so about 100 nested functions abort instead of formatting valid code or returning a parse error. Grow the stack from the input's block depth before parsing, and before each formatted block.
The depth scanner under-counted functions inside Luau interpolated strings and `\z` strings, so those inputs still aborted. format_code now switches to a single 16MiB stack when the caller has less than that free.
This branch has not been deployed
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.
Fixes #1189.
StyLua 2.6.0 aborts with a stack overflow (exit 134) on the CLI's default 2MiB worker thread when checking or formatting about 100 nested
functionbodies. The same nesting with the closingends missing also aborts, instead of reporting a parse error. With a larger stack, valid input formats and invalid input exits 2 witherror parsing:.format_codenow switches to one fixed 16MiB stack segment when the caller has less than that free. That covers the reported 100-200 level range in release builds, and the same regressions in debug builds, which use larger frames. The segment does not grow with input depth, so a few thousand nested blocks can still overflow. wasm has no portable stack switch and keeps using the host stack.I first tried estimating nest depth from a lexer and growing per block. That under-counted functions inside a one-line Luau interpolated string and after a
\znewline inside a string, and those inputs still aborted. Those cases are now regression tests, and the scanner is gone.Tests run on
f71a3659298155e5e6440d1924cde81fff39cec2(featureslua52,lua53,lua54,luau,luajit,cfxluaunless noted):interpolated_nested_functions_formataborted (SIGABRT);z_escape_nested_functions_formatsegfaulted (SIGSEGV).cargo test --offline --features lua52,lua53,lua54,luau,luajit,cfxlua --test test_deep_nesting -- --test-threads=1— pass (6 tests), including 2MiB worker threads at depths 1/20/50/100, 100 nesteddoblocks, unterminated functions, Luau method signatures, interpolation, and\z.cargo test --offline --features lua52,lua53,lua54,luau,luajit,cfxlua --test cli -- deeply_ interpolated_ z_escape -- --test-threads=1— pass (4 tests).cargo test --offline -- --test-threads=8— passcargo test --offline --no-default-features -- --test-threads=8— passcargo test --offline --all-features -- --test-threads=8— passcargo clippy --offline --all-features --all-targets -- -D warnings— passcargo fmt -- --check— passcargo check --offline --target wasm32-unknown-unknown --features luau,lua52,lua53,lua54,luajit,cfxlua— passcargo build --release --offline --features lua52,lua53,lua54,luau,luajit,cfxlua): 100 nestedfunction/endformats (--checkis exit 1, then 0 after format); 100 unterminated Luau method signatures--checkexits 2 witherror parsing:; 100-level one-line interpolation--syntax luau --checkexits 1 (not 134); 4×40\zcase--checkexits 1 (not 139).This change was written with assistance from an AI coding agent. I reviewed the approach, the tests, and the remaining limits before opening this PR.