The stack fits the nesting limit by construction, on every thread
MAX_NESTING_DEPTH turns over-deep input into a parse error only on a stack big enough to reach level 64, and that was an unenforced README assumption: debug frames drifted to ~94 KiB/level under rustc 1.97 (worst family nested case, 5.9 MiB at the limit), so a default 2 MiB spawned thread aborted around level 30 with the limiter never firing. New vlog_parser::PARSE_STACK_SIZE (16 MiB, 2.7x the re-measured worst case) is applied at every parse-reaching site: both binaries' work moved onto named spawned threads (a main stack is the OS's to size — 1 MiB on win32; pre-fix lucisv aborts under ulimit -s 1024, fixed one diagnoses), rayon pool workers resized, forwarder and lsp-server IO threads audited parse-free. Deep-nesting suites across four crates on controlled stacks, sabotage-verified; caller-side stack contract added to every public parse entry point's rustdoc. analyze measured at 71 KiB on a max-legal tree — no second bound needed. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
cf32e92e58
commit
d4eb8f48ff
13 files changed
+809
-61
No files matched your search
@@ -14,7 +14,7 @@ pub mod token;
|
||||
|
||||
pub use lexer::{tokenize, LexError};
|
||||
pub use line_index::LineIndex;
|
||||
pub use parser::{parse, parse_tolerant, ParseDiagnostic, ParseError};
|
||||
pub use parser::{parse, parse_tolerant, ParseDiagnostic, ParseError, PARSE_STACK_SIZE};
|
||||
pub use span::TextRange;
|
||||
pub use token::KEYWORDS;
|
||||
|
||||
|
||||
@@ -160,13 +160,40 @@ const DRIVE_STRENGTHS: &[&str] = &[
|
||||
/// How deeply constructs may nest before the parser gives up.
|
||||
///
|
||||
/// Chosen from measured stack use, not from taste: nested statements are the
|
||||
/// most expensive family at ~62 KiB of stack per level in an unoptimized
|
||||
/// build, so formatting an input that trips the limit peaks at ~3.9 MiB —
|
||||
/// comfortably under the 8 MiB default main-thread stack, and ~0.5 MiB in
|
||||
/// an optimized build. Real RTL nests roughly an order of magnitude less
|
||||
/// than this; see `docs/features/recursion-limit/README.md`.
|
||||
/// most expensive family at ~94 KiB of stack per level in an unoptimized
|
||||
/// build, so the whole pipeline on an input that trips the limit peaks at
|
||||
/// ~5.9 MiB there and ~0.8 MiB in an optimized build. That budget is only
|
||||
/// safe on a thread big enough to hold it, which is what
|
||||
/// [`PARSE_STACK_SIZE`] names — this limit bounds how *many* levels run, not
|
||||
/// what one costs. Real RTL nests roughly an order of magnitude less than
|
||||
/// this; see `docs/features/recursion-limit/README.md`.
|
||||
pub const MAX_NESTING_DEPTH: u32 = 64;
|
||||
|
||||
/// How much stack a thread that parses must be given.
|
||||
///
|
||||
/// [`MAX_NESTING_DEPTH`] turns over-deep input into a parse error instead of
|
||||
/// a stack overflow, but only on a thread that can hold the levels it *does*
|
||||
/// allow: the guard fires at level 64, and reaching level 64 costs stack. On
|
||||
/// a smaller thread the overflow happens first, and a stack overflow is an
|
||||
/// abort, not a `Result` — the whole process dies. So the stack has to fit
|
||||
/// the limit; the limit is not lowered to fit some thread's stack.
|
||||
///
|
||||
/// Measured worst case for the full pipeline (tokenize → parse → analyze →
|
||||
/// format → verify) over every nesting family, on input sized to trip the
|
||||
/// limit: **~5.9 MiB unoptimized, ~0.8 MiB optimized** (nested `case`, the
|
||||
/// fattest family). 16 MiB is that with 2.7× to spare, which is the margin
|
||||
/// this wants: unoptimized frame sizes drift upward with the compiler — the
|
||||
/// same worst case measured ~3.9 MiB in 2026-08 and ~5.9 MiB in 2026-08-28
|
||||
/// on rustc 1.97 — and there is no recoverable failure mode to fall back to.
|
||||
///
|
||||
/// Every thread that may reach a parse is spawned with at least this:
|
||||
/// `std::thread::Builder::new().stack_size(PARSE_STACK_SIZE)`, or
|
||||
/// `rayon::ThreadPoolBuilder::new().stack_size(PARSE_STACK_SIZE)`. Thread
|
||||
/// stacks are committed by the OS a page at a time, so an unused one costs
|
||||
/// address space rather than memory. `docs/features/recursion-limit/README.md`
|
||||
/// lists every site.
|
||||
pub const PARSE_STACK_SIZE: usize = 16 * 1024 * 1024;
|
||||
|
||||
/// How many links a left-nested chain may add to the tree on one path.
|
||||
///
|
||||
/// Some constructs are parsed by a loop rather than by recursion — postfix
|
||||
@@ -178,9 +205,9 @@ pub const MAX_NESTING_DEPTH: u32 = 64;
|
||||
/// the two limits together no matter how chains and nesting are combined.
|
||||
///
|
||||
/// It is larger than [`MAX_NESTING_DEPTH`] because a chain link is cheap —
|
||||
/// ~6 KiB of stack per link in an unoptimized build against ~62 KiB for a
|
||||
/// nested statement, so a maximal chain peaks near 1.5 MiB, well under the
|
||||
/// ~3.9 MiB that maximal statement nesting already costs — and because long
|
||||
/// ~9 KiB of stack per link in an unoptimized build against ~94 KiB for a
|
||||
/// nested statement, so a maximal chain peaks near 2.4 MiB, well under the
|
||||
/// ~5.9 MiB that maximal statement nesting already costs — and because long
|
||||
/// flat chains are the one shape real RTL does produce (a 256-term generated
|
||||
/// sum, a 64-bit parity XOR). Raising it, unlike raising
|
||||
/// [`MAX_NESTING_DEPTH`], does not move the worst case.
|
||||
@@ -241,6 +268,10 @@ pub struct Parser<'a> {
|
||||
/// `source` must be the exact text `tokens` were lexed from: the constructs
|
||||
/// lucisv keeps verbatim (`constraint`, `covergroup` — see
|
||||
/// `docs/features/classes/README.md`) copy their bytes out of it by span.
|
||||
///
|
||||
/// Recurses with input nesting: call this only on a thread with at least
|
||||
/// [`PARSE_STACK_SIZE`] of stack, or over-deep input aborts the process
|
||||
/// instead of returning `Err`.
|
||||
pub fn parse(tokens: Vec<Token>, source: &str) -> PResult<SourceFile> {
|
||||
Parser::new(tokens, source, false).parse_source_file()
|
||||
}
|
||||
@@ -253,6 +284,10 @@ pub fn parse(tokens: Vec<Token>, source: &str) -> PResult<SourceFile> {
|
||||
/// still spans the whole file. `source` must be the exact text `tokens` were
|
||||
/// lexed from. On a well-formed file the tree is identical to [`parse`]'s and
|
||||
/// the diagnostics are empty.
|
||||
///
|
||||
/// Same stack requirement as [`parse`]: needs a thread with at least
|
||||
/// [`PARSE_STACK_SIZE`] of stack, recovery included — tolerant and strict
|
||||
/// parses of the same over-deep input overflow at the same depth.
|
||||
pub fn parse_tolerant(tokens: Vec<Token>, source: &str) -> (SourceFile, Vec<ParseDiagnostic>) {
|
||||
let mut p = Parser::new(tokens, source, true);
|
||||
match p.parse_source_file() {
|
||||
@@ -4898,13 +4933,15 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse on a thread with a generous stack. Nesting all the way to
|
||||
/// [`MAX_NESTING_DEPTH`] peaks around 4 MiB in an unoptimized build,
|
||||
/// which is more than the 2 MiB a default test thread gets — the shipped
|
||||
/// binary parses on the main thread, which has 8 MiB.
|
||||
/// Parse on the stack every production parse site is given. Nesting all
|
||||
/// the way to [`MAX_NESTING_DEPTH`] peaks around 5.9 MiB in an
|
||||
/// unoptimized build, which is more than the 2 MiB a default test thread
|
||||
/// gets — hence [`PARSE_STACK_SIZE`], and hence the fact that the tests
|
||||
/// spell it rather than a number of their own: what they exercise is the
|
||||
/// stack the shipped binaries actually run on.
|
||||
fn parse_deep(src: String) -> PResult<SourceFile> {
|
||||
std::thread::Builder::new()
|
||||
.stack_size(16 * 1024 * 1024)
|
||||
.stack_size(PARSE_STACK_SIZE)
|
||||
.spawn(move || parse(tokenize(&src).expect("lex error"), &src))
|
||||
.expect("spawn parser thread")
|
||||
.join()
|
||||
@@ -5014,6 +5051,47 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
/// `n` copies of a block opener and its closer around one statement, in
|
||||
/// an `initial` — the shape generated RTL nests.
|
||||
fn nested_stmt_blocks(open: &str, close: &str, n: usize) -> String {
|
||||
format!(
|
||||
"module m;\ninitial begin\n{}x <= 1;\n{}end\nendmodule\n",
|
||||
open.repeat(n),
|
||||
close.repeat(n)
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deep_nesting_errors_for_every_block_form() {
|
||||
let d = 200;
|
||||
// `fork`/`join` blocks are statements like `begin`/`end`.
|
||||
assert_too_deep(nested_stmt_blocks("fork\n", "join\n", d));
|
||||
// `case` carries the fattest frame in the grammar; the arm's own
|
||||
// statement is a second gated level per source level.
|
||||
assert_too_deep(nested_stmt_blocks("case (s)\n1: ", "endcase\n", d));
|
||||
// Bodies that need no closer still recurse through the body
|
||||
// statement.
|
||||
assert_too_deep(nested_stmt_blocks("foreach (a[j])\n", "", d));
|
||||
assert_too_deep(nested_stmt_blocks("while (c)\n", "", d));
|
||||
assert_too_deep(nested_stmt_blocks("for (int i = 0; i < 4; i++)\n", "", d));
|
||||
// Nested module declarations (IEEE 1800 §23.4) nest items, not
|
||||
// statements, so they are gated by `parse_item`.
|
||||
assert_too_deep(format!("{}wire w;\n{}", "module m;\n".repeat(d), "endmodule\n".repeat(d)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_fattest_block_forms_still_parse_at_the_limit() {
|
||||
// The stack these tests run on is `PARSE_STACK_SIZE`, so this is the
|
||||
// load-bearing direction: the deepest *legal* input in the two most
|
||||
// expensive families has to fit the stack every production parse site
|
||||
// is given. `module` + `initial` + `begin` + the innermost statement
|
||||
// and its expression cost six levels between them.
|
||||
let n = MAX_NESTING_DEPTH as usize - 6;
|
||||
parse_deep(nested_stmt_blocks("fork\n", "join\n", n)).expect("legal `fork` nesting");
|
||||
parse_deep(nested_stmt_blocks("case (s)\n1: ", "endcase\n", n))
|
||||
.expect("legal `case` nesting");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chain_just_below_the_limit_parses() {
|
||||
parse_deep(chain(".b", MAX_CHAIN_LENGTH as usize))
|
||||
|
||||
Reference in new issue
Block a user