diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ac6dc18..a64fcf22 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 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 ### Added diff --git a/Cargo.lock b/Cargo.lock index 1db0b7ab..4b0365ef 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 b459ef35..c7c8b11d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,12 @@ lsp-textdocument = { version = "0.4.2", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = { version = "0.2.108", optional = true } +# 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" + # 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/lib.rs b/src/lib.rs index 62db706f..ba26cd18 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; @@ -457,6 +458,19 @@ pub fn format_code( config: Config, range: Option, verify_output: OutputVerification, +) -> Result { + // 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( + 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 00000000..83a6a005 --- /dev/null +++ b/src/recursion.rs @@ -0,0 +1,58 @@ +//! Stack space for recursive parsing and formatting. +//! +//! `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. +//! +//! [`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 enough = + matches!(stacker::remaining_stack(), Some(remaining) if remaining >= REQUIRED_STACK); + if enough { + f() + } else { + stacker::grow(REQUIRED_STACK, f) + } + } + #[cfg(target_arch = "wasm32")] + { + f() + } +} + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod tests { + #[test] + 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) + .unwrap() + .join() + .unwrap(); + let remaining = remaining.expect("stack bounds should be visible"); + assert!( + remaining < super::REQUIRED_STACK, + "2MiB thread reported {} bytes free", + remaining + ); + } +} diff --git a/tests/cli.rs b/tests/cli.rs index cf041dd9..7449a0ac 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -17,6 +17,156 @@ 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(); +} + +#[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 new file mode 100644 index 00000000..d1416030 --- /dev/null +++ b/tests/test_deep_nesting.rs @@ -0,0 +1,182 @@ +//! 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 + ); + } +} + +/// 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() { + 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 + ); +}