Skip to content

7.19: engine limits as errors (heap RangeError, native and nesting depth) and hostile-page tests - #296

Closed
atiernan wants to merge 8 commits into
mainfrom
claude/js-limits
Closed

atiernan wants to merge 8 commits into
mainfrom
claude/js-limits

Conversation

@atiernan

Copy link
Copy Markdown
Member

Phase 7 item 7.19 (limits and hostile pages) for the JS engine, on top of the integration branch (#286).

Limits

Every limit is a field of clazel_js::Limits. The engine has no Default. Limits::recommended() is a new named starting point for untrusted pages on a 2 MiB thread stack, and Limits::unlimited() is unchanged. The values below are recommended().

Limit Value Error past it
heap_bytes 512 MiB A RangeError ("Out of memory: the heap limit was reached") that JS can catch. It is made from a 64 KiB reserve past the limit. Once the reserve is spent too, the run ends in Error::OutOfMemory, which JS cannot catch.
value_stack_bytes 4 MiB RangeError "Maximum call stack size exceeded" (this already existed)
native_depth 32 A RangeError. It now applies to every VM native, not only to the embedder's closures. Nested runs of the interpreter loop (ToPrimitive calling toString) are bounded at the limit plus one.
nesting_depth 512 Source: a SyntaxError, checked on the height of the parse tree. JSON.parse: a SyntaxError (7.11's listed deviation). JSON.stringify: a RangeError. The fixed caps stay and the smaller value applies: 1024 for source, 512 for JSON.parse, 128 for JSON.stringify.
regexp_backtrack_entries 1 Mi Stored in the VM (Vm::regexp_backtrack_limit) for the RegExp built-in, which does not exist yet. The matcher gives StackOverflow, and the engine will report that as a RangeError.
code_bytes 64 MiB Not used: there is no JIT.
Pending timers (clazel-web event_loop::Limits) 65,536 (Limits::default(), unchanged) LimitError::TooManyTimers. The test binding turns it into a RangeError. HTML specifies a QuotaExceededError, which needs a DOMException.

How it works

  • Heap byte count (clazel-js-vm heap.rs). The VM heap now counts bytes as an estimate:

    • 64 bytes per cell;
    • a string's code units at 1 byte each (Latin-1) or 2;
    • 8 bytes per initial slot;
    • 32 bytes per added property.

    Runtime::heap_used reports this count. When an op fails with Abrupt::OutOfMemory inside Vm::execute (or in a native that Vm::call invoked), the VM throws a RangeError built inside the reserve instead.

  • Native depth (clazel-js-vm). DEFAULT_NATIVE_DEPTH drops from 150 to 32. I measured the host stack each level of JS → native → JS uses on aarch64 macOS: about 40 KB in debug and 1.5 KB in release. At 150 levels, function f(){[0].forEach(f)} f() overflowed the 2 MiB test-thread stack and aborted the process (SIGABRT) on the base branch. So did recursion through ToPrimitive, which no native counted.

  • Setters. Runtime::new now passes every limit to the VM through new setters: set_heap_limit, set_native_depth_limit, set_nesting_depth_limit and set_regexp_backtrack_limit. The VM also gains Vm::check_terminated for natives with long loops of their own.

Hostile-page tests

All of these pass natively. Under Miri each stays under 300 s; the slowest is about 150 s.

crates/clazel-js/tests/hostile.rs uses the public API only:

  • while(true){}: the Terminator, set from a watchdog thread, ends it with Error::Terminated. Neither catch nor finally runs.
  • Unbounded recursion in five forms: JS → JS, forEach, map, f.call, and ToPrimitive. Each is a catchable RangeError and none overflows the host stack.
  • Recursion through the embedder's own natives: a RangeError.
  • (a+)+b through the API: returns Error::NotImplemented, because there is no RegExp built-in. It neither panics nor runs.
  • The 1 GiB string build (s = s + s thirty times): a RangeError at a 16 MiB heap. heap_used stays at or below the limit plus the reserve. Building a real 1 GiB string would need a 1 GiB heap, so the test uses the smaller one.
  • Catching the heap limit and allocating again for ever: ends in OutOfMemory, so it does not hang.
  • push for ever and o['k'+i] for ever: a RangeError.
  • 10⁶ nested arrays in source: a SyntaxError. With nesting_depth: 64, a 100-deep array is also a SyntaxError and a 5-deep one parses.
  • 10⁶ nested arrays in JSON: a SyntaxError. With nesting_depth: 16, 10 levels parse and 20 levels are a SyntaxError. JSON.stringify of a 1000-deep object is a RangeError.

crates/clazel-js-regexp/tests/limits.rs:

  • (a+)+b on 100 000 as with no step budget: ends as MatchError::Interrupted. The interrupt hook reads a flag that another thread sets.

crates/clazel-web/tests/hostile.rs (adds clazel-js as a dev-dependency only):

  • 10⁶ setTimeout calls from script, through a test binding over EventLoop. Exactly 65,536 are accepted and the rest are caught RangeErrors. All accepted timers fire once and the loop ends empty. This takes about 18 s in debug.
  • Timers that multiply for ever: the timer count stays at or below the cap, and the refusals are caught.

Not done

  • In-flight fetches per renderer: the kernel's, and out of scope here. I did not touch clazel-kernel.
  • A MutationObserver that mutates in its callback: MutationObserver exists only in the generated WebIDL model, so there is nothing to test.
  • A finalizer that resurrects: there is no FinalizationRegistry (only clazel-js-heap's collector models one), so there is nothing to test.
  • (a+)+b through a real RegExp: the RegExp built-in does not exist. regexp_backtrack_entries is plumbed to the VM for it.
  • "After a full collection": the VM does not use clazel-js-heap's collector yet and frees nothing. The RangeError therefore comes at once, and a page that filled the heap leaves it full, so compiling another script in that runtime gives OutOfMemory.
  • Long loops inside built-ins (e.g. join on a length-2³²−1 array) do not check the Terminator. I added Vm::check_terminated but did not edit the built-ins, which another PR is changing.

Deviations and behaviour changes

  • Heap limit error: it is now a RangeError where it was Error::OutOfMemory. I updated the assertion of the_heap_limit_stops_a_script_that_allocates_for_ever in tests/api.rs to expect it, and the test now also checks heap_used.
  • Byte count is an estimate: shapes and interned keys are counted only through the 32 bytes per property, and spare Vec capacity is not counted.
  • OutOfMemory becomes a RangeError in every case, including a string past MAX_LENGTH.
  • Limits docs said JSON.parse gives a RangeError. I followed phase-7.md 7.11 instead: a SyntaxError. The docs are updated.
  • Limits::recommended() is new. The README discussed defaults, and this is a named choice rather than a default. The browser still picks its own values.
  • native_depth values: Limits::unlimited() leaves native_depth at u32::MAX. On a small stack, a page can then overflow the host stack, and this is documented. The VM's default without an embedder drops from 150 to 32.
  • Limits::nesting_depth for source counts the parse tree's height: the same measure as the parser's MAX_DEPTH.

Verification

cargo xtask verify --changed origin/claude/phase7-integration:

  • Selected: 6 of 65 crates (clazel-jit, clazel-js, clazel-js-builtins, clazel-js-regexp, clazel-js-vm, clazel-web).
  • Passed:
    • the static checks, rustfmt, clippy and tests;
    • 634 tests under Miri;
    • Kani on clazel-js-vm and clazel-js-regexp;
    • Verus on clazel-jit and clazel-js-vm;
    • proof mutations on clazel-jit and clazel-js-vm (237 of 237 killed for clazel-js-vm);
    • conformance on clazel-js-vm, clazel-js-builtins, clazel-js-regexp and clazel-web.
  • Failed: status table and images. The message was "no committed row to carry over for clazel-deflate, clazel-forms, clazel-gif, clazel-image-shape, clazel-jpeg, clazel-png, clazel-svg, clazel-webp". This comes from the base's docs/STATUS.md at e35f03f, not from this change. I did not edit STATUS.md.
  • Final line: RESULT: FAILED (not CHANGED CRATES PASSED), for that reason only.

CI's full verification has not run.

🤖 Generated with Claude Code

atiernan and others added 8 commits November 23, 2026 10:45
…ng and RegExp limits (7.19)

The heap counts bytes (cells, string payloads, properties) against a
settable limit; an op that runs out throws a RangeError made from a 64 KiB
reserve, and OutOfMemory only once that is spent. Nested runs of the
interpreter loop count against the native depth too, so recursion through
ToPrimitive cannot overflow the host stack; the default drops from 150 to
32, measured at about 40 KB of host stack a level unoptimised.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…esting limit (7.19)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Endless loop (Terminator), unbounded recursion through JS, natives and
ToPrimitive, a gigabyte string build, catching the heap limit for ever,
growing arrays and objects, 10^6 nested arrays in source and in JSON, and
recursion through the embedder's own natives.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…7.19)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oop (7.19)

clazel-js is a dev-dependency only, for tests/hostile.rs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ir errors

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ds globals with ToString

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@atiernan

Copy link
Copy Markdown
Member Author

Contained in #302 (engine: Object, classes, Map/Set, Reflect), which merges this branch; one verification. Branch kept.

@atiernan atiernan closed this Nov 23, 2026

This branch was successfully deployed

No deployments
full-verification — 5e1d4a79 Deployed Nov 23, 2026 by atiernan via Approve the proofs #782
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.

1 participant