Skip to content

feat(rust): make source size limit configurable via Limits::max_source_bytes - #176

Merged
kommander merged 5 commits into
devfrom
source-limit
Dec 2, 2026
Merged

kommander merged 5 commits into
devfrom
source-limit

Conversation

@simonklee

Copy link
Copy Markdown
Member

What

  • Export DEFAULT_MAX_SOURCE_BYTES (256 KiB, the value of the hardcoded MAX_SOURCE_BYTES) and add Limits::max_source_bytes: Option<usize>.
  • Validate max_source_bytes != Some(0) in Runtime::limits. Check the limit once, in run::compile, before the scan for nesting and before the compile thread starts (Runtime::compile, run::start). compiler.rs no longer checks the size itself.
  • A saved state compiles its source again whatever its size (run::start_for_resume, snapshot capture and baseline). The limit bounds the source a run starts from, and the run that saved the state had accepted it. So the limit is not saved in a snapshot, and a run of more than 256 KiB suspends and resumes on any runtime.
  • Document it in rust/README.md under "Only Rust has".

Why

The hardcoded 256 KiB keeps an embedder from accepting a larger program. The OpenCode Rust client allows a plugin 1 MiB of source. Limits::max_source_bytes keeps 256 KiB as the default and lets an embedder raise or lower it.

Cost of raising it

The limit is what bounds the parser's recursion: the parser recurses once per level of syntax, a source nests no deeper than it has bytes, and run::compile gives the compile thread the stack that depth needs. Measured in a debug build, up to the refusal with a syntax error:

Source 256 KiB (the default) 1 MiB
[ repeated 1.07 GiB, 1.0 s 4.2 GiB, 3.8 s
( repeated 0.84 GiB, 0.7 s 3.3 GiB, 2.9 s
{ repeated 0.46 GiB, 0.5 s 1.8 GiB, 2.1 s

At 8 MiB the thread can't be made and the compile is refused ("nested too deeply"). The doc comment of max_source_bytes says so.

In a release build the same sources cost 132 ms and 261 MiB ({) and 101 ms and 196 MiB ([) at 256 KiB, and 576 ms and 961 MiB and 361 ms and 705 MiB at 1 MiB.

# refuses these sources before they are parsed (14 ms and 47 MiB at 1 MiB) and merges with this PR without a conflict. Once both are in, the last sentence of the doc comment on max_source_bytes overstates the cost of raising the limit and should point at MAX_COMPILE_LEVELS.

Upstream

Rebased onto origin/dev (dec90d1; tip cc5763a). Resolved one conflict in rust/crates/psychoscript/src/run.rs keeping #153's wasm32 doc comment and compile guard alongside the max_source_bytes parameter and pre-scan size check; nothing was superseded by merged upstream work.

Compared with open PR #159 (d926fa6), which adds CompileOptions::max_source_bytes for Runtime::compile_with / compile_module_with:

Tests

  • Added rust/crates/psychoscript/tests/source_limit.rs, 2 tests:
    • The default rejects a source over 256 KiB. Some(1 << 20) compiles and runs a 1 MiB program and rejects 1 MiB plus one byte.
    • A 300 KiB program suspends, and resumes on a runtime with the default limit.
  • cargo fmt --all --check, cargo clippy --locked -p psychoscript -p psychoscript-bench --all-targets -- -D warnings, cargo clippy --locked -p psychoscript-mobile --no-default-features --features locale --all-targets -- -D warnings, and cargo test --locked -p psychoscript --lib --test source_limit --test snapshot --test host --test host_recording --test globals --test boundary --test program_lines --test number_locale --test value_depth (117 passed, 0 failed) pass.

@kommander
kommander merged commit a218cc5 into dev Dec 2, 2026
2 checks passed
@kommander
kommander deleted the source-limit branch December 2, 2026 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants