Skip to content

fix(parser): fail on deep nesting instead of overflowing the stack - #5675

Open
hatemosphere wants to merge 3 commits into
boa-dev:mainfrom
hatemosphere:fix-parser-stack-guard
Open

hatemosphere wants to merge 3 commits into
boa-dev:mainfrom
hatemosphere:fix-parser-stack-guard

Conversation

@hatemosphere

@hatemosphere hatemosphere commented Nov 16, 2026 •

Copy link
Copy Markdown

Fixes #4397.

  • Cursor::peek returns a "too much recursion" SyntaxError when less than min(128 KiB, stack / 4) of the thread's stack is left, instead of overflowing and aborting.
  • Stack bounds: pthread_getattr_np on Linux (RLIMIT_STACK for the musl main thread), pthread_get_stackaddr_np/pthread_get_stacksize_np on Apple, GetCurrentThreadStackLimits on Windows. Other targets are unchanged.
  • Unlike Resolve the problem of parser stack overflow #4311 (stacker), the stack isn't grown, so hostile input fails fast instead of eating memory.

The parser is recursive descent, so deeply nested input such as Function('('.repeat(N)) overflows the native stack of the parsing thread and aborts the process (boa-dev#4397). Cursor::peek now returns a "too much recursion" syntax error once less than 512 KiB of the thread's stack is left. The stack bounds come from pthread_getattr_np, so the guard is Linux-only for now; elsewhere it never trips.
@hatemosphere
hatemosphere requested a review from a team as a code owner November 16, 2026 14:37
@github-actions github-actions Bot added C-Dependencies Pull requests that update a dependency file C-Parser Issues surrounding the parser C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers labels Nov 16, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Nov 16, 2026
@abdulhamid999

Copy link
Copy Markdown

Nice work tackling #4397! Bounding stack recursion via early fail-fast syntax errors without growing the stack is a great approach for hostile input.

A couple of observations regarding stack bounds handling:

  1. Small stack sizes / musl default thread stack (false positives):
    If size <= STACK_RED_ZONE (for example, on musl libc where the default thread stack size is 128 KiB, or if an embedding application spawns parsing worker threads with smaller stacks like 256 KiB), addr as usize + STACK_RED_ZONE will be higher than the stack top (addr + size).
    In that scenario, (&raw const here as usize) < *limit will evaluate to true immediately on the very first token peek, causing even trivial scripts like "" or "1" to fail with "too much recursion".
    Clamping the red zone based on the thread stack size avoids this:

    let red_zone = STACK_RED_ZONE.min(size / 4);
    addr as usize + red_zone

    or only applying the limit if size > STACK_RED_ZONE * 2.

  2. Cross-platform support:
    If you decide to expand this beyond Linux in a follow-up or in this PR:

    • On macOS: pthread_get_stackaddr_np and pthread_get_stacksize_np provide stack base and size.
    • On Windows: GetCurrentThreadStackLimits(&mut low, &mut high) directly returns the stack bounds.

- Red zone is now min(128 KiB, stack / 4). A fixed 512 KiB put the limit above the stack top on
  stacks <= 512 KiB (musl's 128 KiB default), so even "1" failed. Measured worst case between two
  checks: ~32 KiB debug, ~16 KiB release.
- musl's pthread_getattr_np reports only the mapped part of the main thread's stack; use
  RLIMIT_STACK there.
Bounds from pthread_get_stackaddr_np/pthread_get_stacksize_np and GetCurrentThreadStackLimits (not on
win7 targets, which lack it). The test now runs on every CI OS.

This branch has not been deployed

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

Labels

C-Dependencies Pull requests that update a dependency file C-Parser Issues surrounding the parser C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stack overflow, when evaluating Function('('.repeat(703))

2 participants