Repository navigation
feat(rust): make source size limit configurable via Limits::max_source_bytes - #176
Merged
Merged
Conversation
# Conflicts: # rust/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
DEFAULT_MAX_SOURCE_BYTES(256 KiB, the value of the hardcodedMAX_SOURCE_BYTES) and addLimits::max_source_bytes: Option<usize>.max_source_bytes != Some(0)inRuntime::limits. Check the limit once, inrun::compile, before the scan for nesting and before the compile thread starts (Runtime::compile,run::start).compiler.rsno longer checks the size itself.run::start_for_resume,snapshotcapture 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.rust/README.mdunder "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_byteskeeps 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::compilegives the compile thread the stack that depth needs. Measured in a debug build, up to the refusal with a syntax error:[repeated(repeated{repeatedAt 8 MiB the thread can't be made and the compile is refused ("nested too deeply"). The doc comment of
max_source_bytessays 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_bytesoverstates the cost of raising the limit and should point atMAX_COMPILE_LEVELS.Upstream
Rebased onto
origin/dev(dec90d1; tipcc5763a). Resolved one conflict inrust/crates/psychoscript/src/run.rskeeping #153'swasm32doc comment and compile guard alongside themax_source_bytesparameter and pre-scan size check; nothing was superseded by merged upstream work.Compared with open PR #159 (
d926fa6), which addsCompileOptions::max_source_bytesforRuntime::compile_with/compile_module_with:compiler.rs::compile_in(afterrun::compile's nesting scan and helper thread spawn), leavesRuntime::executeandRuntime::programfrom source fixed at 256 KiB (not onLimits), does not rejectSome(0), and compiles snapshots withCompileOptions::new()(max_source_bytes: None), so suspending/resuming a >256 KiB program fails.max_source_bytesonLimits(applying uniformly tocompile,execute, andprogram), rejectsSome(0), checks before the nesting scan and thread spawn, and allows saved >256 KiB states to suspend and resume on any runtime.compiler.rs,lib.rs,run.rs,runtime.rs, andsnapshot.rs. Open PR feat(rust): persistent Session and zero-copy HostCtx API #157 merges cleanly (0 conflicts).Tests
rust/crates/psychoscript/tests/source_limit.rs, 2 tests:Some(1 << 20)compiles and runs a 1 MiB program and rejects 1 MiB plus one byte.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, andcargo 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.