From 24b2e53d54dafce4f22c333f1bd4c2dfe9676c3e Mon Sep 17 00:00:00 2001 From: MaxWang007 Date: Tue, 17 Nov 2026 03:02:31 +0000 Subject: [PATCH 1/2] Fix stack overflow on deeply nested blocks 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. --- CHANGELOG.md | 4 + Cargo.lock | 91 ++++++++++++- Cargo.toml | 5 + src/formatters/block.rs | 7 + src/lib.rs | 30 ++++- src/recursion.rs | 267 +++++++++++++++++++++++++++++++++++++ tests/cli.rs | 71 ++++++++++ tests/test_deep_nesting.rs | 123 +++++++++++++++++ 8 files changed, 585 insertions(+), 13 deletions(-) create mode 100644 src/recursion.rs create mode 100644 tests/test_deep_nesting.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ac6dc18d..fe6501dd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fixed a stack overflow abort when formatting or checking deeply nested blocks (about 100 nested `function` bodies, and the same nesting with the closing `end`s missing). The process now formats valid input and reports a normal parse error for invalid input ([#1189](https://github.com/JohnnyMorganz/StyLua/issues/1189)) + ## [2.6.0] - 2026-10-10 ### Added diff --git a/Cargo.lock b/Cargo.lock index 1db0b7ab3..4b0365efd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,15 @@ version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" +[[package]] +name = "ar_archive_writer" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523e5e187fff36fcb8d1d19ccf0ca4ca43ec3d158316057f66dc6f72716bc5ec" +dependencies = [ + "object", +] + [[package]] name = "assert_cmd" version = "2.2.2" @@ -135,6 +144,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" +[[package]] +name = "cc" +version = "1.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ef9e3e8154a45c7a95cff4c1299702106ad94062d7bb85fdc5d3a5afd91d45" +dependencies = [ + "find-msvc-tools", + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -223,7 +242,7 @@ dependencies = [ "lazy_static", "libc", "unicode-width", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -375,7 +394,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -384,6 +403,12 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +[[package]] +name = "find-msvc-tools" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" + [[package]] name = "fluent-uri" version = "0.1.4" @@ -570,9 +595,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.155" +version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" [[package]] name = "linked-hash-map" @@ -653,6 +678,15 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.39.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e5a6c098c7a3b6547378093f5cc30bc54fd361ce711e05293a5cc589562739b" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.19.0" @@ -765,6 +799,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "psm" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dcd034599e63b970727f70d79e02d62390a4a84f7c6b827c27c46d5ac3fa622" +dependencies = [ + "ar_archive_writer", + "cc", +] + [[package]] name = "quote" version = "1.0.35" @@ -842,7 +886,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -917,6 +961,12 @@ dependencies = [ "serde", ] +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + [[package]] name = "similar" version = "2.4.0" @@ -937,6 +987,19 @@ dependencies = [ "serde", ] +[[package]] +name = "stacker" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "707f49d46706bacf8a2b00d51dace3f9de527c13eec3778f570c411f89e69967" +dependencies = [ + "cc", + "cfg-if", + "libc", + "psm", + "windows-sys 0.61.2", +] + [[package]] name = "strsim" version = "0.10.0" @@ -993,6 +1056,7 @@ dependencies = [ "serde", "serde_json", "similar", + "stacker", "strum", "thiserror", "threadpool", @@ -1032,7 +1096,7 @@ dependencies = [ "fastrand", "redox_syscall", "rustix", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -1258,6 +1322,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + [[package]] name = "windows-sys" version = "0.52.0" @@ -1267,6 +1337,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-targets" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index b459ef356..70529fa22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,11 @@ lsp-textdocument = { version = "0.4.2", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = { version = "0.2.108", optional = true } +# Non-wasm builds grow the stack before recursive parsing/formatting. wasm has no +# portable stack switch, and the host supplies the call stack there. +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +stacker = "0.1.25" + # full-moon should be compiled to release optimisations in debug mode due to stack overflows # https://github.com/Kampfkarren/full-moon/issues/140 [profile.dev.package.full_moon] diff --git a/src/formatters/block.rs b/src/formatters/block.rs index 12d0ef5dc..8fb9c2fb3 100644 --- a/src/formatters/block.rs +++ b/src/formatters/block.rs @@ -581,6 +581,13 @@ fn check_stmt_requires_semicolon( /// Formats a block node. Note: the given shape to the block formatter should already be at the correct indentation level pub fn format_block(ctx: &Context, block: &Block, shape: Shape) -> Block { + // Nested blocks recurse through statement formatting back into this function. + // Switch stacks before allocating that chain so deep trees do not abort. + crate::recursion::with_block_stack(|| format_block_inner(ctx, block, shape)) +} + +#[inline(never)] +fn format_block_inner(ctx: &Context, block: &Block, shape: Shape) -> Block { let mut ctx = *ctx; let mut formatted_statements: Vec<(Stmt, Option)> = Vec::new(); let mut remove_next_stmt_leading_newlines = !ctx.should_preserve_leading_block_newline_gaps(); diff --git a/src/lib.rs b/src/lib.rs index 62db706fe..dca82c0bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ mod context; #[cfg(feature = "editorconfig")] pub mod editorconfig; mod formatters; +mod recursion; mod shape; mod sort_requires; mod verify_ast; @@ -433,13 +434,14 @@ pub fn format_ast( // If we are verifying, reparse the output then check it matches the original input if let Some(input_ast) = input_ast_for_verification { let output = ast.to_string(); - let reparsed_output = - match full_moon::parse_fallible(&output, config.syntax.into()).into_result() { - Ok(ast) => ast, - Err(error) => { - return Err(Error::VerificationAstError(error)); - } - }; + let reparsed_output = match recursion::with_parser_stack(&output, || { + full_moon::parse_fallible(&output, config.syntax.into()).into_result() + }) { + Ok(ast) => ast, + Err(error) => { + return Err(Error::VerificationAstError(error)); + } + }; let mut ast_verifier = verify_ast::AstVerifier::new(); if !ast_verifier.compare(input_ast, reparsed_output) { @@ -457,6 +459,20 @@ pub fn format_code( config: Config, range: Option, verify_output: OutputVerification, +) -> Result { + // Parsing and formatting both recurse once per nested block, and the parser + // keeps a large frame live across that recursion. Give the call a stack + // sized for this input so a 2MiB worker thread does not abort. + recursion::with_parser_stack(code, || { + format_code_impl(code, config, range, verify_output) + }) +} + +fn format_code_impl( + code: &str, + config: Config, + range: Option, + verify_output: OutputVerification, ) -> Result { let input_ast = match full_moon::parse_fallible(code, config.syntax.into()).into_result() { Ok(ast) => ast, diff --git a/src/recursion.rs b/src/recursion.rs new file mode 100644 index 000000000..f2bbca421 --- /dev/null +++ b/src/recursion.rs @@ -0,0 +1,267 @@ +//! Stack space for recursive parsing and formatting. +//! +//! `full_moon`'s `parse_stmt` is one function covering every statement kind, so +//! its frame stays live while a nested block is parsed. On an optimized build +//! that frame is about 13KiB, and a `function` body adds the declaration, +//! function-body, and block frames on top (about 23KiB per nested function). +//! The formatter then walks the same tree: `format_block` → `format_stmt` → +//! `format_function_*` → `format_block`, about 17KiB per level when optimized +//! and several times that in debug. +//! +//! The CLI runs each file on a `threadpool` worker. Those threads use Rust's +//! default 2MiB stack, so roughly 100 nested functions abort the process +//! (`thread '' has overflowed its stack`, exit 134) instead of +//! formatting valid code or returning a parse error. Growing the stack here, +//! from the input's block depth, fixes that without asking the caller to set +//! `RUST_MIN_STACK`. + +/// Optimized `parse_stmt` plus the function-body chain is ~23KiB per nested +/// block. 48KiB leaves room for the frames measured on the all-features build. +#[cfg(not(target_arch = "wasm32"))] +const PARSER_STACK_PER_LEVEL: usize = 48 * 1024; +/// Outer parse/format frames (`parse_fallible`, the formatter entry, printing). +#[cfg(not(target_arch = "wasm32"))] +const PARSER_STACK_BASE: usize = 1024 * 1024; +/// Stop a depth-scan glitch from requesting an unbounded mapping. +#[cfg(not(target_arch = "wasm32"))] +const PARSER_STACK_MAX: usize = 64 * 1024 * 1024; + +/// Bytes that must still be free before entering another formatted block. +/// The statement/function frames between two `format_block` calls are ~20KiB +/// optimized and ~80KiB in debug; this stays above both. +#[cfg(not(target_arch = "wasm32"))] +const FORMAT_STACK_RED_ZONE: usize = 256 * 1024; +/// Fresh stack segment used when the red zone is exhausted. Larger than the +/// red zone so the check does not fire again on the new segment. +#[cfg(not(target_arch = "wasm32"))] +const FORMAT_STACK_SEGMENT: usize = 2 * 1024 * 1024; + +/// Run `f` with enough stack for `full_moon` to recurse through `code`. +/// +/// `stacker::grow` always allocates a fresh segment, so this only switches +/// when the current thread is shorter than the budget. Shallow files on a +/// normal thread stay put. A reparse that is already inside this guard sees +/// the grown segment and does not allocate another one. +pub(crate) fn with_parser_stack(code: &str, f: impl FnOnce() -> R) -> R { + #[cfg(not(target_arch = "wasm32"))] + { + let needed = parser_stack_budget(estimate_block_depth(code)); + let enough = matches!(stacker::remaining_stack(), Some(remaining) if remaining >= needed); + if enough { + f() + } else { + stacker::grow(needed, f) + } + } + #[cfg(target_arch = "wasm32")] + { + let _ = code; + f() + } +} + +/// Ensure the next nested formatted block has room for its own frames. +pub(crate) fn with_block_stack(f: impl FnOnce() -> R) -> R { + #[cfg(not(target_arch = "wasm32"))] + { + stacker::maybe_grow(FORMAT_STACK_RED_ZONE, FORMAT_STACK_SEGMENT, f) + } + #[cfg(target_arch = "wasm32")] + { + f() + } +} + +#[cfg(not(target_arch = "wasm32"))] +fn parser_stack_budget(depth: usize) -> usize { + let scaled = depth.saturating_mul(PARSER_STACK_PER_LEVEL); + PARSER_STACK_BASE + .saturating_add(scaled) + .min(PARSER_STACK_MAX) +} + +/// Maximum block nesting `full_moon` will recurse through. +/// +/// Counts `function` / `do` / `if` / `while` / `for` / `repeat` outside of +/// strings and comments, and matches them against `end` / `until`. This +/// over-counts some forms (`for`/`while` also contain a `do`) which only +/// reserves extra stack. It must not under-count real nesting: an unclosed +/// `function` still contributes, because that is the invalid input that used +/// to abort. +#[cfg(not(target_arch = "wasm32"))] +fn estimate_block_depth(code: &str) -> usize { + let bytes = code.as_bytes(); + let mut i = 0; + let mut depth = 0usize; + let mut max_depth = 0usize; + + while i < bytes.len() { + let byte = bytes[i]; + if byte.is_ascii_whitespace() { + i += 1; + continue; + } + + // `--` line comment, or `--[[` / `--[=[...` long comment. + if byte == b'-' && bytes.get(i + 1) == Some(&b'-') { + i += 2; + if let Some(level) = long_bracket_level(bytes, i) { + i = skip_long_bracket(bytes, i + level + 2, level); + } else { + while i < bytes.len() && bytes[i] != b'\n' { + i += 1; + } + } + continue; + } + + if byte == b'"' || byte == b'\'' || byte == b'`' { + i = skip_short_string(bytes, i, byte); + continue; + } + + if byte == b'[' { + if let Some(level) = long_bracket_level(bytes, i) { + i = skip_long_bracket(bytes, i + level + 2, level); + continue; + } + } + + if byte.is_ascii_alphabetic() || byte == b'_' { + let start = i; + i += 1; + while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') { + i += 1; + } + match &code[start..i] { + "function" | "do" | "if" | "while" | "for" | "repeat" => { + depth += 1; + max_depth = max_depth.max(depth); + } + "end" | "until" => { + depth = depth.saturating_sub(1); + } + _ => {} + } + continue; + } + + i += 1; + } + + max_depth +} + +/// `[[`, `[=[`, `[==[`, ... at `i`, returning the number of `=` signs. +#[cfg(not(target_arch = "wasm32"))] +fn long_bracket_level(bytes: &[u8], i: usize) -> Option { + if bytes.get(i) != Some(&b'[') { + return None; + } + let mut j = i + 1; + while bytes.get(j) == Some(&b'=') { + j += 1; + } + if bytes.get(j) == Some(&b'[') { + Some(j - (i + 1)) + } else { + None + } +} + +/// `i` points just past the opening long bracket. Returns the index past the close. +#[cfg(not(target_arch = "wasm32"))] +fn skip_long_bracket(bytes: &[u8], mut i: usize, level: usize) -> usize { + while i < bytes.len() { + if bytes[i] == b']' { + let mut j = i + 1; + let mut eqs = 0; + while eqs < level && bytes.get(j) == Some(&b'=') { + j += 1; + eqs += 1; + } + if eqs == level && bytes.get(j) == Some(&b']') { + return j + 1; + } + } + i += 1; + } + bytes.len() +} + +/// `i` points at the opening quote. An unescaped newline ends the literal so a +/// broken string cannot swallow the rest of the file and hide real blocks. +#[cfg(not(target_arch = "wasm32"))] +fn skip_short_string(bytes: &[u8], mut i: usize, quote: u8) -> usize { + i += 1; + while i < bytes.len() { + match bytes[i] { + b'\\' => i += 2, + b'\n' => return i, + c if c == quote => return i + 1, + _ => i += 1, + } + } + bytes.len() +} + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod tests { + use super::*; + + #[test] + fn depth_counts_nested_functions_and_ignores_strings_and_comments() { + assert_eq!(estimate_block_depth("local x = 1\n"), 0); + assert_eq!(estimate_block_depth("function F()\nend\n"), 1); + assert_eq!( + estimate_block_depth("function F()\nfunction G()\nend\nend\n"), + 2 + ); + assert_eq!(estimate_block_depth("do do end end\n"), 2); + assert_eq!( + estimate_block_depth("-- function\nlocal s = \"function end\"\nfunction F() end\n"), + 1 + ); + assert_eq!( + estimate_block_depth("--[=[\nfunction\n]=]\nfunction F() end\n"), + 1 + ); + // Unclosed functions keep their depth. `for`/`while` also contain `do`, + // so those are counted twice; that only reserves extra stack. + assert_eq!( + estimate_block_depth("function F()\nfunction G()\nfunction H()\n"), + 3 + ); + assert_eq!(estimate_block_depth("for i = 1, 10 do\nend\n"), 2); + } + + #[test] + fn worker_thread_reports_a_small_remaining_stack() { + // The CLI pool uses this stack size. If the probe over-reports it, the + // parser guard would skip growing and the overflow would come back. + let remaining = std::thread::Builder::new() + .stack_size(2 * 1024 * 1024) + .spawn(stacker::remaining_stack) + .unwrap() + .join() + .unwrap(); + let remaining = remaining.expect("stack bounds should be visible"); + assert!( + remaining < 3 * 1024 * 1024, + "2MiB thread reported {} bytes free", + remaining + ); + assert!( + remaining > 256 * 1024, + "2MiB thread reported only {} bytes free", + remaining + ); + } + + #[test] + fn parser_budget_scales_with_depth_and_is_capped() { + assert_eq!(parser_stack_budget(0), PARSER_STACK_BASE); + assert!(parser_stack_budget(100) > parser_stack_budget(10)); + assert_eq!(parser_stack_budget(usize::MAX), PARSER_STACK_MAX); + } +} diff --git a/tests/cli.rs b/tests/cli.rs index cf041dd9e..ec5346662 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -17,6 +17,77 @@ fn create_stylua() -> Command { Command::new(assert_cmd::cargo::cargo_bin!("stylua")) } +fn nested_function_source(depth: usize, close: bool) -> String { + let mut source = String::new(); + for i in 1..=depth { + source.push_str(&format!("function F{i}()\n")); + } + if close { + for _ in 0..depth { + source.push_str("end\n"); + } + } + source +} + +#[test] +fn deeply_nested_functions_check_and_format() { + let cwd = assert_fs::TempDir::new().unwrap(); + cwd.child("nested.lua") + .write_str(&nested_function_source(100, true)) + .unwrap(); + + // Unformatted but valid: --check reports a diff (exit 1) and must not abort. + create_stylua() + .current_dir(cwd.path()) + .args(["--check", "nested.lua"]) + .assert() + .code(1); + + create_stylua() + .current_dir(cwd.path()) + .arg("nested.lua") + .assert() + .success(); + + create_stylua() + .current_dir(cwd.path()) + .args(["--check", "nested.lua"]) + .assert() + .success(); + + cwd.close().unwrap(); +} + +#[test] +fn deeply_unterminated_functions_are_a_parse_error() { + let cwd = assert_fs::TempDir::new().unwrap(); + cwd.child("open.lua") + .write_str(&nested_function_source(100, false)) + .unwrap(); + + let output = create_stylua() + .current_dir(cwd.path()) + .args(["--check", "open.lua"]) + .output() + .unwrap(); + + assert_eq!( + output.status.code(), + Some(2), + "stderr: {}", + String::from_utf8_lossy(&output.stderr) + ); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + stderr.contains("error parsing"), + "expected a parse error, got {}", + stderr + ); + + cwd.close().unwrap(); +} + #[test] fn test_no_files_provided() { let mut cmd = create_stylua(); diff --git a/tests/test_deep_nesting.rs b/tests/test_deep_nesting.rs new file mode 100644 index 000000000..01d3176c9 --- /dev/null +++ b/tests/test_deep_nesting.rs @@ -0,0 +1,123 @@ +//! Regression tests for deeply nested blocks. +//! +//! About 100 nested `function` bodies used to abort the process (exit 134) on +//! the CLI's 2MiB worker threads. These tests call the library on a thread +//! with that same stack size, so a passing run does not depend on the main +//! thread's larger stack or on `RUST_MIN_STACK`. + +use stylua_lib::{format_code, Config, Error, LuaVersion, OutputVerification}; + +const WORKER_STACK: usize = 2 * 1024 * 1024; + +fn on_worker_stack(f: impl FnOnce() -> T + Send + 'static) -> T { + std::thread::Builder::new() + .name("stylua-worker".to_string()) + .stack_size(WORKER_STACK) + .spawn(f) + .expect("failed to spawn worker thread") + .join() + .expect("worker thread panicked") +} + +fn nested_functions(depth: usize) -> String { + let mut code = String::new(); + for i in 1..=depth { + code.push_str(&format!("function F{i}()\n")); + } + for _ in 0..depth { + code.push_str("end\n"); + } + code +} + +fn unterminated_functions(depth: usize) -> String { + let mut code = String::new(); + for i in 1..=depth { + code.push_str(&format!("function F{i}()\n")); + } + code +} + +fn assert_formats_idempotently(source: String) -> String { + let formatted = on_worker_stack(move || { + format_code(&source, Config::default(), None, OutputVerification::None) + .expect("valid nested code should format") + }); + let formatted_again = formatted.clone(); + let reformatted = on_worker_stack(move || { + format_code( + &formatted_again, + Config::default(), + None, + OutputVerification::Full, + ) + .expect("formatted nested code should reformat") + }); + assert_eq!(formatted, reformatted); + assert!(formatted.ends_with("end\n")); + formatted +} + +#[test] +fn deeply_nested_functions_format_on_a_worker_stack() { + for depth in [1, 20, 50, 100] { + let formatted = assert_formats_idempotently(nested_functions(depth)); + assert!(formatted.contains(&format!("function F{depth}()"))); + } +} + +#[test] +fn deeply_nested_do_blocks_format_on_a_worker_stack() { + let mut code = String::new(); + for _ in 0..100 { + code.push_str("do\n"); + } + for _ in 0..100 { + code.push_str("end\n"); + } + assert_formats_idempotently(code); +} + +#[test] +fn deeply_unterminated_functions_are_parse_errors() { + for depth in [1, 20, 50, 100] { + let source = unterminated_functions(depth); + let result = on_worker_stack(move || { + format_code(&source, Config::default(), None, OutputVerification::None) + }); + assert!( + matches!(&result, Err(Error::ParseError(_))), + "depth {}: {:?}", + depth, + result + ); + let message = result.unwrap_err().to_string(); + assert!( + message.contains("error parsing"), + "depth {}: {}", + depth, + message + ); + } +} + +#[cfg(feature = "luau")] +#[test] +fn deeply_unterminated_luau_method_signatures_are_parse_errors() { + let mut code = String::new(); + for i in 1..=100 { + code.push_str(&format!("function F{i}(self): number\n")); + } + let result = on_worker_stack(move || { + let config = Config { + syntax: LuaVersion::Luau, + ..Config::default() + }; + format_code(&code, config, None, OutputVerification::None) + }); + assert!( + matches!(result, Err(Error::ParseError(_))), + "unterminated Luau functions should be a parse error, got {:?}", + result + ); +} From f71a3659298155e5e6440d1924cde81fff39cec2 Mon Sep 17 00:00:00 2001 From: MaxWang007 Date: Tue, 17 Nov 2026 03:50:31 +0000 Subject: [PATCH 2/2] Use one fixed stack segment for deep nesting 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. --- CHANGELOG.md | 2 +- Cargo.toml | 5 +- src/formatters/block.rs | 7 - src/lib.rs | 26 ++-- src/recursion.rs | 261 ++++--------------------------------- tests/cli.rs | 79 +++++++++++ tests/test_deep_nesting.rs | 59 +++++++++ 7 files changed, 180 insertions(+), 259 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe6501dd8..a64fcf223 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fixed a stack overflow abort when formatting or checking deeply nested blocks (about 100 nested `function` bodies, and the same nesting with the closing `end`s missing). The process now formats valid input and reports a normal parse error for invalid input ([#1189](https://github.com/JohnnyMorganz/StyLua/issues/1189)) +- Fixed a stack overflow abort on the reported case of about 100–200 nested `function` bodies, including that nesting inside a one-line Luau interpolated string or split by a `\z` string that contains the word `end`. Valid input formats. The same nesting with the closing `end`s missing reports a parse error instead of aborting. `format_code` switches to one fixed 16MiB stack when the caller has less than that free (about 8MiB covers the release build of the reported range; debug formatting needs the rest). A few thousand nested blocks can still overflow ([#1189](https://github.com/JohnnyMorganz/StyLua/issues/1189)) ## [2.6.0] - 2026-10-10 diff --git a/Cargo.toml b/Cargo.toml index 70529fa22..c7c8b11d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,8 +64,9 @@ lsp-textdocument = { version = "0.4.2", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = { version = "0.2.108", optional = true } -# Non-wasm builds grow the stack before recursive parsing/formatting. wasm has no -# portable stack switch, and the host supplies the call stack there. +# Non-wasm `format_code` switches to a fixed stack segment when the caller +# does not already have enough room for nested blocks. wasm has no portable +# stack switch; the host supplies that call stack. [target.'cfg(not(target_arch = "wasm32"))'.dependencies] stacker = "0.1.25" diff --git a/src/formatters/block.rs b/src/formatters/block.rs index 8fb9c2fb3..12d0ef5dc 100644 --- a/src/formatters/block.rs +++ b/src/formatters/block.rs @@ -581,13 +581,6 @@ fn check_stmt_requires_semicolon( /// Formats a block node. Note: the given shape to the block formatter should already be at the correct indentation level pub fn format_block(ctx: &Context, block: &Block, shape: Shape) -> Block { - // Nested blocks recurse through statement formatting back into this function. - // Switch stacks before allocating that chain so deep trees do not abort. - crate::recursion::with_block_stack(|| format_block_inner(ctx, block, shape)) -} - -#[inline(never)] -fn format_block_inner(ctx: &Context, block: &Block, shape: Shape) -> Block { let mut ctx = *ctx; let mut formatted_statements: Vec<(Stmt, Option)> = Vec::new(); let mut remove_next_stmt_leading_newlines = !ctx.should_preserve_leading_block_newline_gaps(); diff --git a/src/lib.rs b/src/lib.rs index dca82c0bc..ba26cd188 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -434,14 +434,13 @@ pub fn format_ast( // If we are verifying, reparse the output then check it matches the original input if let Some(input_ast) = input_ast_for_verification { let output = ast.to_string(); - let reparsed_output = match recursion::with_parser_stack(&output, || { - full_moon::parse_fallible(&output, config.syntax.into()).into_result() - }) { - Ok(ast) => ast, - Err(error) => { - return Err(Error::VerificationAstError(error)); - } - }; + let reparsed_output = + match full_moon::parse_fallible(&output, config.syntax.into()).into_result() { + Ok(ast) => ast, + Err(error) => { + return Err(Error::VerificationAstError(error)); + } + }; let mut ast_verifier = verify_ast::AstVerifier::new(); if !ast_verifier.compare(input_ast, reparsed_output) { @@ -460,12 +459,11 @@ pub fn format_code( range: Option, verify_output: OutputVerification, ) -> Result { - // Parsing and formatting both recurse once per nested block, and the parser - // keeps a large frame live across that recursion. Give the call a stack - // sized for this input so a 2MiB worker thread does not abort. - recursion::with_parser_stack(code, || { - format_code_impl(code, config, range, verify_output) - }) + // Parsing and formatting recurse once per nested block, and the parser + // keeps a large frame live across that recursion. A 2MiB worker thread + // aborts around 100 nested functions; give the call a fixed segment when + // the caller does not already have that much stack. + recursion::with_sufficient_stack(|| format_code_impl(code, config, range, verify_output)) } fn format_code_impl( diff --git a/src/recursion.rs b/src/recursion.rs index f2bbca421..83a6a0051 100644 --- a/src/recursion.rs +++ b/src/recursion.rs @@ -1,244 +1,47 @@ //! Stack space for recursive parsing and formatting. //! -//! `full_moon`'s `parse_stmt` is one function covering every statement kind, so -//! its frame stays live while a nested block is parsed. On an optimized build -//! that frame is about 13KiB, and a `function` body adds the declaration, -//! function-body, and block frames on top (about 23KiB per nested function). -//! The formatter then walks the same tree: `format_block` → `format_stmt` → -//! `format_function_*` → `format_block`, about 17KiB per level when optimized -//! and several times that in debug. +//! `full_moon`'s `parse_stmt` keeps one large frame live while it parses a +//! nested block (about 23KiB per nested function on an optimized build with +//! every syntax feature). The CLI calls [`crate::format_code`] on a +//! `threadpool` worker whose stack is Rust's default 2MiB, so about 100 nested +//! functions abort the process. Debug builds of the formatter use several +//! times that much per level. //! -//! The CLI runs each file on a `threadpool` worker. Those threads use Rust's -//! default 2MiB stack, so roughly 100 nested functions abort the process -//! (`thread '' has overflowed its stack`, exit 134) instead of -//! formatting valid code or returning a parse error. Growing the stack here, -//! from the input's block depth, fixes that without asking the caller to set -//! `RUST_MIN_STACK`. - -/// Optimized `parse_stmt` plus the function-body chain is ~23KiB per nested -/// block. 48KiB leaves room for the frames measured on the all-features build. -#[cfg(not(target_arch = "wasm32"))] -const PARSER_STACK_PER_LEVEL: usize = 48 * 1024; -/// Outer parse/format frames (`parse_fallible`, the formatter entry, printing). -#[cfg(not(target_arch = "wasm32"))] -const PARSER_STACK_BASE: usize = 1024 * 1024; -/// Stop a depth-scan glitch from requesting an unbounded mapping. -#[cfg(not(target_arch = "wasm32"))] -const PARSER_STACK_MAX: usize = 64 * 1024 * 1024; - -/// Bytes that must still be free before entering another formatted block. -/// The statement/function frames between two `format_block` calls are ~20KiB -/// optimized and ~80KiB in debug; this stays above both. -#[cfg(not(target_arch = "wasm32"))] -const FORMAT_STACK_RED_ZONE: usize = 256 * 1024; -/// Fresh stack segment used when the red zone is exhausted. Larger than the -/// red zone so the check does not fire again on the new segment. -#[cfg(not(target_arch = "wasm32"))] -const FORMAT_STACK_SEGMENT: usize = 2 * 1024 * 1024; - -/// Run `f` with enough stack for `full_moon` to recurse through `code`. -/// -/// `stacker::grow` always allocates a fresh segment, so this only switches -/// when the current thread is shorter than the budget. Shallow files on a -/// normal thread stay put. A reparse that is already inside this guard sees -/// the grown segment and does not allocate another one. -pub(crate) fn with_parser_stack(code: &str, f: impl FnOnce() -> R) -> R { +//! [`with_sufficient_stack`] switches to one fixed segment when the caller has +//! less than that left. Release parsing of the reported 100–200 nested +//! functions needs only a few megabytes. The segment is 16MiB so a debug +//! build of the formatter — several times the release frame size — can also +//! run the 160-level regression without aborting. It does not grow with the +//! input, so a few thousand nested blocks can still overflow. Shallow calls +//! on a thread that already has this much free stay put. + +/// Stack reserved for one `format_code` call. +const REQUIRED_STACK: usize = 16 * 1024 * 1024; + +/// Run `f` with at least [`REQUIRED_STACK`] bytes of stack available. +pub(crate) fn with_sufficient_stack(f: impl FnOnce() -> R) -> R { #[cfg(not(target_arch = "wasm32"))] { - let needed = parser_stack_budget(estimate_block_depth(code)); - let enough = matches!(stacker::remaining_stack(), Some(remaining) if remaining >= needed); + let enough = + matches!(stacker::remaining_stack(), Some(remaining) if remaining >= REQUIRED_STACK); if enough { f() } else { - stacker::grow(needed, f) + stacker::grow(REQUIRED_STACK, f) } } #[cfg(target_arch = "wasm32")] - { - let _ = code; - f() - } -} - -/// Ensure the next nested formatted block has room for its own frames. -pub(crate) fn with_block_stack(f: impl FnOnce() -> R) -> R { - #[cfg(not(target_arch = "wasm32"))] - { - stacker::maybe_grow(FORMAT_STACK_RED_ZONE, FORMAT_STACK_SEGMENT, f) - } - #[cfg(target_arch = "wasm32")] { f() } } -#[cfg(not(target_arch = "wasm32"))] -fn parser_stack_budget(depth: usize) -> usize { - let scaled = depth.saturating_mul(PARSER_STACK_PER_LEVEL); - PARSER_STACK_BASE - .saturating_add(scaled) - .min(PARSER_STACK_MAX) -} - -/// Maximum block nesting `full_moon` will recurse through. -/// -/// Counts `function` / `do` / `if` / `while` / `for` / `repeat` outside of -/// strings and comments, and matches them against `end` / `until`. This -/// over-counts some forms (`for`/`while` also contain a `do`) which only -/// reserves extra stack. It must not under-count real nesting: an unclosed -/// `function` still contributes, because that is the invalid input that used -/// to abort. -#[cfg(not(target_arch = "wasm32"))] -fn estimate_block_depth(code: &str) -> usize { - let bytes = code.as_bytes(); - let mut i = 0; - let mut depth = 0usize; - let mut max_depth = 0usize; - - while i < bytes.len() { - let byte = bytes[i]; - if byte.is_ascii_whitespace() { - i += 1; - continue; - } - - // `--` line comment, or `--[[` / `--[=[...` long comment. - if byte == b'-' && bytes.get(i + 1) == Some(&b'-') { - i += 2; - if let Some(level) = long_bracket_level(bytes, i) { - i = skip_long_bracket(bytes, i + level + 2, level); - } else { - while i < bytes.len() && bytes[i] != b'\n' { - i += 1; - } - } - continue; - } - - if byte == b'"' || byte == b'\'' || byte == b'`' { - i = skip_short_string(bytes, i, byte); - continue; - } - - if byte == b'[' { - if let Some(level) = long_bracket_level(bytes, i) { - i = skip_long_bracket(bytes, i + level + 2, level); - continue; - } - } - - if byte.is_ascii_alphabetic() || byte == b'_' { - let start = i; - i += 1; - while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') { - i += 1; - } - match &code[start..i] { - "function" | "do" | "if" | "while" | "for" | "repeat" => { - depth += 1; - max_depth = max_depth.max(depth); - } - "end" | "until" => { - depth = depth.saturating_sub(1); - } - _ => {} - } - continue; - } - - i += 1; - } - - max_depth -} - -/// `[[`, `[=[`, `[==[`, ... at `i`, returning the number of `=` signs. -#[cfg(not(target_arch = "wasm32"))] -fn long_bracket_level(bytes: &[u8], i: usize) -> Option { - if bytes.get(i) != Some(&b'[') { - return None; - } - let mut j = i + 1; - while bytes.get(j) == Some(&b'=') { - j += 1; - } - if bytes.get(j) == Some(&b'[') { - Some(j - (i + 1)) - } else { - None - } -} - -/// `i` points just past the opening long bracket. Returns the index past the close. -#[cfg(not(target_arch = "wasm32"))] -fn skip_long_bracket(bytes: &[u8], mut i: usize, level: usize) -> usize { - while i < bytes.len() { - if bytes[i] == b']' { - let mut j = i + 1; - let mut eqs = 0; - while eqs < level && bytes.get(j) == Some(&b'=') { - j += 1; - eqs += 1; - } - if eqs == level && bytes.get(j) == Some(&b']') { - return j + 1; - } - } - i += 1; - } - bytes.len() -} - -/// `i` points at the opening quote. An unescaped newline ends the literal so a -/// broken string cannot swallow the rest of the file and hide real blocks. -#[cfg(not(target_arch = "wasm32"))] -fn skip_short_string(bytes: &[u8], mut i: usize, quote: u8) -> usize { - i += 1; - while i < bytes.len() { - match bytes[i] { - b'\\' => i += 2, - b'\n' => return i, - c if c == quote => return i + 1, - _ => i += 1, - } - } - bytes.len() -} - #[cfg(all(test, not(target_arch = "wasm32")))] mod tests { - use super::*; - - #[test] - fn depth_counts_nested_functions_and_ignores_strings_and_comments() { - assert_eq!(estimate_block_depth("local x = 1\n"), 0); - assert_eq!(estimate_block_depth("function F()\nend\n"), 1); - assert_eq!( - estimate_block_depth("function F()\nfunction G()\nend\nend\n"), - 2 - ); - assert_eq!(estimate_block_depth("do do end end\n"), 2); - assert_eq!( - estimate_block_depth("-- function\nlocal s = \"function end\"\nfunction F() end\n"), - 1 - ); - assert_eq!( - estimate_block_depth("--[=[\nfunction\n]=]\nfunction F() end\n"), - 1 - ); - // Unclosed functions keep their depth. `for`/`while` also contain `do`, - // so those are counted twice; that only reserves extra stack. - assert_eq!( - estimate_block_depth("function F()\nfunction G()\nfunction H()\n"), - 3 - ); - assert_eq!(estimate_block_depth("for i = 1, 10 do\nend\n"), 2); - } - #[test] - fn worker_thread_reports_a_small_remaining_stack() { - // The CLI pool uses this stack size. If the probe over-reports it, the - // parser guard would skip growing and the overflow would come back. + fn worker_thread_has_less_than_the_reserved_stack() { + // The CLI pool uses this size. If the probe over-reports it, the + // guard skips the switch and the overflow comes back. let remaining = std::thread::Builder::new() .stack_size(2 * 1024 * 1024) .spawn(stacker::remaining_stack) @@ -247,21 +50,9 @@ mod tests { .unwrap(); let remaining = remaining.expect("stack bounds should be visible"); assert!( - remaining < 3 * 1024 * 1024, + remaining < super::REQUIRED_STACK, "2MiB thread reported {} bytes free", remaining ); - assert!( - remaining > 256 * 1024, - "2MiB thread reported only {} bytes free", - remaining - ); - } - - #[test] - fn parser_budget_scales_with_depth_and_is_capped() { - assert_eq!(parser_stack_budget(0), PARSER_STACK_BASE); - assert!(parser_stack_budget(100) > parser_stack_budget(10)); - assert_eq!(parser_stack_budget(usize::MAX), PARSER_STACK_MAX); } } diff --git a/tests/cli.rs b/tests/cli.rs index ec5346662..7449a0aca 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -88,6 +88,85 @@ fn deeply_unterminated_functions_are_a_parse_error() { cwd.close().unwrap(); } +#[cfg(feature = "luau")] +#[test] +fn interpolated_nested_functions_do_not_abort() { + let mut body = "nil".to_string(); + for _ in 0..100 { + body = format!("function() local a = {body} end"); + } + let cwd = assert_fs::TempDir::new().unwrap(); + cwd.child("interp.lua") + .write_str(&format!("local x = `{{{body}}}`\n")) + .unwrap(); + + let output = create_stylua() + .current_dir(cwd.path()) + .args(["--syntax", "luau", "--check", "interp.lua"]) + .output() + .unwrap(); + let code = output.status.code(); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + code == Some(0) || code == Some(1), + "interpolated nesting should format or diff, got exit {:?} stderr {}", + code, + stderr + ); + assert!( + !stderr.contains("overflowed its stack"), + "interpolated nesting aborted: {}", + stderr + ); + + cwd.close().unwrap(); +} + +#[cfg(any(feature = "lua52", feature = "luau"))] +#[test] +fn z_escape_nested_functions_do_not_abort() { + const SEGMENT: usize = 40; + const SEGMENTS: usize = 4; + let mut source = String::new(); + for segment in 0..SEGMENTS { + for i in 0..SEGMENT { + source.push_str(&format!("function S{segment}_{i}()\n")); + } + source.push_str("local s = \"\\z\n"); + for _ in 0..SEGMENT { + source.push_str("end "); + } + source.push_str("\"\n"); + } + for _ in 0..(SEGMENT * SEGMENTS) { + source.push_str("end\n"); + } + + let cwd = assert_fs::TempDir::new().unwrap(); + cwd.child("zreset.lua").write_str(&source).unwrap(); + + let output = create_stylua() + .current_dir(cwd.path()) + .args(["--check", "zreset.lua"]) + .output() + .unwrap(); + let code = output.status.code(); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + code == Some(0) || code == Some(1), + "\\z nesting should format or diff, got exit {:?} stderr {}", + code, + stderr + ); + assert!( + !stderr.contains("overflowed its stack"), + "\\z nesting aborted: {}", + stderr + ); + + cwd.close().unwrap(); +} + #[test] fn test_no_files_provided() { let mut cmd = create_stylua(); diff --git a/tests/test_deep_nesting.rs b/tests/test_deep_nesting.rs index 01d3176c9..d14160308 100644 --- a/tests/test_deep_nesting.rs +++ b/tests/test_deep_nesting.rs @@ -101,6 +101,65 @@ fn deeply_unterminated_functions_are_parse_errors() { } } +/// One-line Luau interpolation whose expression is `depth` nested functions. +/// A backtick-skipping depth scan treats this as a string and does not grow +/// the stack, so 100 levels aborted on a 2MiB worker (exit 134). +fn luau_interpolated_nested_functions(depth: usize) -> String { + let mut body = "nil".to_string(); + for _ in 0..depth { + body = format!("function() local a = {body} end"); + } + format!("local x = `{{{body}}}`\n") +} + +/// 4×40 nested functions with a `\z` string full of the word `end` between +/// segments. Those `end`s are inside the string for full-moon, but a newline +/// scan counts them as real closers, underestimates depth, and previously +/// SIGSEGV'd (exit 139). +fn z_escape_underestimated_nesting() -> String { + const SEGMENT: usize = 40; + const SEGMENTS: usize = 4; + let mut code = String::new(); + for segment in 0..SEGMENTS { + for i in 0..SEGMENT { + code.push_str(&format!("function S{segment}_{i}()\n")); + } + code.push_str("local s = \"\\z\n"); + for _ in 0..SEGMENT { + code.push_str("end "); + } + code.push_str("\"\n"); + } + for _ in 0..(SEGMENT * SEGMENTS) { + code.push_str("end\n"); + } + code +} + +#[cfg(feature = "luau")] +#[test] +fn interpolated_nested_functions_format_on_a_worker_stack() { + let source = luau_interpolated_nested_functions(100); + let formatted = on_worker_stack(move || { + let config = Config { + syntax: LuaVersion::Luau, + ..Config::default() + }; + format_code(&source, config, None, OutputVerification::None) + .expect("nested functions inside a Luau interpolated string should format") + }); + assert!(formatted.contains("function()")); + assert!(formatted.contains("local x")); +} + +#[cfg(any(feature = "lua52", feature = "luau"))] +#[test] +fn z_escape_nested_functions_format_on_a_worker_stack() { + let formatted = assert_formats_idempotently(z_escape_underestimated_nesting()); + assert!(formatted.contains("function S3_39()")); + assert!(formatted.contains("\\z")); +} + #[cfg(feature = "luau")] #[test] fn deeply_unterminated_luau_method_signatures_are_parse_errors() {