Skip to content

Report the recursion limit when serializing deeply nested values - #5684

Open
aviralgarg05 wants to merge 2 commits into
boa-dev:mainfrom
aviralgarg05:fix/5637-json-recursion-limit
Open

aviralgarg05 wants to merge 2 commits into
boa-dev:mainfrom
aviralgarg05:fix/5637-json-recursion-limit

Conversation

@aviralgarg05

Copy link
Copy Markdown

This Pull Request fixes/closes #5637.

It changes the following:

  • Counts the native descent in SerializeJSONObject / SerializeJSONArray as host recursion, so JSON.stringify reports the configured recursion limit instead of exhausting the native stack and aborting the process.
  • Does the same for JsValue::to_json, which recursed through to_json_inner without consulting the runtime limits either.
  • Adds regressions for deeply nested objects, deeply nested arrays and to_json, and asserts that nesting within the limit is unaffected.

Both paths recurse one native frame per level of nesting and the cycle set only guards against cyclic graphs, so a deep acyclic value had nothing to stop it. This follows #4699, which added Vm::host_call_depth for the equivalent accessor problem in #4535, rather than introducing a second mechanism.

One behaviour note worth your call: with the default recursion_limit of 512, a value nested deeper than that now reports the limit where it previously serialized, up to the depth at which the native stack gave out (measured in the issue at roughly 2.7k levels on a 2 MiB stack). I asked on the issue whether you would prefer serialization to have a separate, higher limit, and I am happy to change it.

Testing:

  • cargo test -p boa_engine --lib twice, 1142 passed each time
  • cargo test -p boa_engine --doc, 185 passed
  • cargo make run-ci, which is cargo fmt -- --check plus clippy with --all-features --all-targets and --no-default-features
  • Both new tests fail with the two source changes reverted

`JSON.stringify` and `JsValue::to_json` recurse natively over the object graph,
one native frame per level of nesting, and neither path consulted
`check_runtime_limits`, so a deeply nested but acyclic value exhausted the
native stack and aborted the process regardless of the configured
`RuntimeLimits`. The cycle set only guards against cyclic graphs.

Count both descents as host recursion through the `host_call_depth` counter, so
the existing recursion-limit error is reported instead, and cover nested
objects, nested arrays and `to_json` with regressions.

Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
@aviralgarg05
aviralgarg05 requested a review from a team as a code owner November 23, 2026 14:38
@github-actions github-actions Bot added Waiting On Review Waiting on reviews from the maintainers C-Tests Issues and PRs related to the tests. C-Builtins PRs and Issues related to builtins/intrinsics and removed Waiting On Review Waiting on reviews from the maintainers labels Nov 23, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Nov 23, 2026

@abdulhamid999 abdulhamid999 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean approach — reusing Vm::host_call_depth and check_runtime_limits() via serialize_json_nested and to_json_inner avoids introducing a parallel recursion counter, and the regression tests cover both within-limit and over-limit nesting cleanly.

Two minor details worth noting:

  1. In core/engine/src/value/conversions/serde_json.rs, to_json_inner increments host_call_depth for every value including primitive leaf properties, whereas serialize_json_nested only increments on nested JsObjects (objects/arrays). Guarding inside the JsVariant::Object branch would keep to_json and JSON.stringify depth counting consistent (and avoid consuming depth on a top-level primitive).
  2. In core/engine/src/object/operations.rs (lines 460 and 516), decrements use context.vm.host_call_depth = context.vm.host_call_depth.saturating_sub(1); rather than -= 1 for extra safety.

(As a quick related observation for a potential follow-up: internalize_json_property in json/mod.rs for JSON.parse(..., reviver) and JsValue::from_json in serde_json.rs also recurse natively per nesting level without consulting host_call_depth.)

to_json_inner charged every value, including primitive leaves, while
serialize_json_nested only ever sees a JsObject. The two therefore reported
the recursion limit at different depths, and a top-level primitive consumed a
level for nothing. Return early for non-objects, and match operations.rs by
decrementing with saturating_sub.
@aviralgarg05

Copy link
Copy Markdown
Author

Thanks @abdulhamid999, both of those were right and I’ve applied them. to_json_inner now returns early for anything that is not an object, so only objects and arrays consume a level and the count matches serialize_json_nested, which takes a &JsObject and could never charge a primitive; a top-level primitive no longer costs depth either. I’ve also switched both new decrements to host_call_depth.saturating_sub(1) to match operations.rs. The existing deep-nesting test still reports the limit, and the json and serde_json suites pass, 46 tests. On the follow-up you spotted, internalize_json_property and JsValue::from_json do recurse natively the same way, and I agree they want the same treatment, though I would rather keep that to a separate PR than widen this one.

@abdulhamid999

Copy link
Copy Markdown

Thanks for the quick update! e85eaba looks good to me — returning early on non-objects in to_json_inner keeps the depth accounting aligned with serialize_json_nested, and matching saturating_sub(1) across both paths is clean.

Agreed on keeping internalize_json_property and JsValue::from_json separate so this PR stays focused.

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-Builtins PRs and Issues related to builtins/intrinsics C-Tests Issues and PRs related to the tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON.stringify of a deeply nested object overflows the native stack and aborts the process

2 participants