Skip to content

Ariadne loader: the schema check goes 128 levels of YAML deep, and a scene node is a level - #1588

Merged
transfix merged 3 commits into
masterfrom
tfx/charming-hugle-d241a4
Nov 27, 2026
Merged

transfix merged 3 commits into
masterfrom
tfx/charming-hugle-d241a4

Conversation

@transfix

Copy link
Copy Markdown
Owner

The nesting limit of #1586 (kMaxNesting, 256) counted widgets, include: and load: entries. Two parts of a load still went down a document a level at a time with nothing counting them, bounded only by yaml-cpp's ~500-level parse cap. This PR bounds both, so every load keeps at least half of wasm's 2 MiB stack.

Stacked on #1586: this branch is its head (d0587dc) plus one commit, c2421bc. Review that commit. Once #1586 squash-merges, this gets rebased onto master.

The two recursions, before and after

The least stack each worst case loads on, MSVC 14.44 Release x64, bisected with ariadne_deep_nesting_test's probes. Windows rounds a thread's stack to 64 KiB, so each is given as fails on / passes on:

worst case #1586 this PR
a scene nested to the limit through use:s of prototypes (256 levels) 2,176 / 2,240 KiB: past 2 MiB 128 / 192 KiB
a scene written inline as deep as yaml-cpp reads (247 nodes) 2,112 / 2,176 KiB: past 2 MiB 384 / 448 KiB
a document as deep as yaml-cpp reads (246 groups inline) 1,280 / 1,344 KiB 384 / 448 KiB
that document as a fragment, load:ed at level 254 1,536 / 1,600 KiB 640 / 704 KiB
the deepest schema check that runs (128 levels of YAML) – 320 / 384 KiB

The rows at 384 / 448 KiB now cost what yaml-cpp's own parse of that YAML costs (DISABLED_ProbeParseInlineGroups at 246 needs the same). The worst case of a load is now the fragment row, about a third of 2 MiB.

1. The schema check stops at 128 levels of YAML

check_schema → yaml_to_json + json-schema-validator takes about 2.8 KB of stack per YAML level, inside the validator, where the loader can't shrink it. So the check now runs only on a tree at most kMaxSchemaDepth levels of YAML deep (loader.h, 128; the document or include: expansion is level 1).

  • An iterative walk (deeper_than) checks the depth first.
  • A document, library, fragment or include: expansion that goes deeper is not checked. It gets one SchemaIssue, at its first value past the limit: nested more than 128 levels of YAML deep, the most the schema check goes (docs/ARIADNE.md §6.7): this document is not checked.
  • A load goes on and warns, as it does for any schema issue. ari validate fails the document (exit 1) and prints the issue at its place (deep70.ari:4:1566: /windows/0/children/0/...: nested more than 128 levels ...).

Why this option and not refusing a fragment whose YAML can't fit the nesting levels left:

  • That option bounds only a fragment. The main document's check (1,280 / 1,344 KiB at yaml-cpp's cap) would stay where it was.
  • YAML depth is not widget depth. A fragment with a deep state: value or scene would be refused though its widgets fit.

Why 128:

  • It costs about 360 KB, under the ~450 KB yaml-cpp's own parse of the deepest YAML takes. So the check is no longer the largest user of the stack.
  • It is ten times the deepest shipped document: 13 levels of YAML over the 195 .ari documents in the tree (volume_grid.ari). The deepest scene among them is 2 nodes.
  • An inline document deeper than about 61 widgets still loads; it just goes unchecked.

2. parse_scene_node: a small frame, and a level of the count

It's split the way #1586 split parse_widget_impl. It builds in place (SceneNode &). Its keys go in scene_node_keys and its repeat: in scene_node_repeat, both CVC_ARI_NOINLINE, and expand_use is no longer inlined into it. Frames in bytes (MSVC from the /FAs listing, alloc + pushes; wasm32 from em++ -O2 prologues, loader.cpp alone):

function MSVC #1586 MSVC this PR wasm #1586 wasm this PR
parse_scene_node (on the way down) 8,760 568 2,048 176
scene_node_keys (never on the way down) – 2,632 – 864
scene_node_repeat (after the children) – 264 – 160
expand_use (returns before the recursion) inlined 1,288 inlined 672
load_node (once per load) 11,416 11,832 3,568 3,552

Decision: scene depth counts toward the Nesting limit; it gets no cap of its own.

  • Each scene node is a level, and so is each use:. The prototype's node goes a level below its use:, as a unit's widget goes below its include: entry.
  • Scenes are parsed only at the top of a document, after the widgets, when the count is back at 0. So a scene has all 256 levels and costs the widgets nothing.
  • It shares the error machinery and the place lookup. If a scene is ever parsed inside a fragment, its levels stack with the widgets' and the shared count stays safe.
  • A cap is needed, not just a smaller frame. Through use:s of prototypes that use: others, a scene nests without end while its YAML stays a few levels deep. On Ariadne loader: about 1 KB of stack a level, and a document nests at most 256 levels #1586, 128 chained prototypes (256 levels) needed 2.2 MiB.
  • Past the limit the load fails, and builds no tree and no scenes. The error is at the node; for a node in a prototype's instance, it's where the node is written in the prototype (place_of now maps use: expansions as it maps include:s):
ari: scene.ari:102:1310: a scene node nested deeper than 256 levels, the most a document may (each
scene node and use: a level; docs/ARIADNE.md §6.7), with 100 use: on the way

Scene depth per stack (MSVC Release)

stack through use:s, #1586 this PR written inline, #1586 this PR
256 KiB 24 levels the limit (256) not measured 135 nodes
512 KiB 54 the limit 54 nodes 247 (yaml-cpp's cap)
2 MiB 232 the limit 233 247
8 MiB 950 the limit 247 247

load_node's two nesting failures now share fail_nesting, so its frame stays near #1586's.

Tests

  • ariadne_deep_nesting_test loads each worst case on 1 MiB, half of wasm's 2 MiB (8 MiB in a Debug build): the fragment at level 254, the document and the inline scene at yaml-cpp's cap, and the scene at the limit. A scene one level past the limit fails there. There are new probes for scenes, DISABLED_ProbeLoadSceneInline and DISABLED_ProbeLoadScenePrototypes. DISABLED_ProbeValidateInlineGroups now expects the not-checked issue past the limit.
  • ariadne_loader_test: NestingLimitCountsSceneNodesAndUses (256 loads; 257 fails, placed at its line and column in the prototype, counting the use:s) and NestingLimitCountsEveryScene (a scenes: entry).
  • ariadne_schema_test: TheCheckGoesAtMostkMaxSchemaDepthLevels. A document at 128 levels is checked; one a level deeper gets one issue, with its pointer, line, column and message, and a load warns it.

Run locally (Windows, MSVC Release, with the validator and FTXUI):

  • ariadne_deep_nesting_test: 17 passed, 1 skipped (tvision).
  • ariadne_loader_test: 279.
  • ariadne_schema_test: 13 + 1 new, 1 skipped.
  • ariadne_host_test: 69.
  • ariadne_runtime_test: 368.
  • the 11 ari_* ctests that are built here.

Not run here: Linux, macOS, wasm, Debug (CI). The validator's own frames on wasm weren't measured; only loader.cpp was compiled for wasm.

Docs

  • docs/ARIADNE.md §6.7 covers scenes in the count, the scene error, a scene level's cost, and the schema check's limit with the numbers above.
  • §14.2 says each checked tree stops at kMaxSchemaDepth.
  • The prototypes notes (§5.18) say each use: is a level.

Conflicts expected

The same as #1586's (#1566 read_fragment, #1585 common_keys); this commit touches neither.

…most 256 levels

The .ari loader parsed a document a level at a time with a 95 KB frame (parse_widget_impl: MSVC
gave each by-value Widget temporary of its per-kind branches a slot of its own), so a load took
about 100 KB of stack a widget level and 200 KB an include: level: 9 include: levels on wasm's
2 MiB stack, 40 on 8 MiB (the 41-deep `ari snapshot` crash).

- parse_widget_impl's branches are never-inlined functions, each building its widget in place.
  parse_widget, parse_seq, expand_include, expand_load and parse_document build in place too, and
  what a level does besides going down is in functions of their own. MSVC Release frames:
  parse_widget_impl 95,656 -> 248 bytes, expand_load 11,384 -> 584, parse_document 6,264 -> 344,
  parse_seq 3,432 -> 568, expand_include 3,384 -> 296. A level now takes about 0.9 KB through
  include:s: 1,177 include: levels load on 2 MiB (was 9), 266 on 512 KiB (was 1).
- kMaxNesting (256, loader.h): a widget is a level, and so is each include: and load: entry, counted
  across every unit and fragment (Nesting, shared by a load:'s sub-Ctx). The first widget past it
  stops the parse and the load fails, its error at the widget's place (in an include: expansion,
  the same place in the unit's template), counting the include:s and load:s on the way and naming
  each that goes into another file. LoadResult::error_source, error_line and error_column hold the
  place. The shipped documents nest at most 11 levels.
- ari validate reports such an error at its place (file:line:col: error: ...), and --json adds
  load_error_source, load_error_line and load_error_column.
- any_descendant_repeats and refs_from_widget are loops.

Tests: the loader at the limit and one past it through plain children, include: and load:; a
document at the limit through include:s and through load:s loads and draws on a 2 MiB stack
(8 MiB in a Debug build); ari_validate_too_deep; probes for the schema check and yaml-cpp's parse
alone, and for a deep fragment loaded at the limit. docs/ARIADNE.md §6.7, with §6.1, §6.3, §14.3.
…scene node is a level

Two recursions in a load were bounded only by yaml-cpp's ~500-level parse cap, not by kMaxNesting.
Both now keep a load well inside wasm's 2 MiB stack.

The schema check (json-schema-validator, ~2.8 KB of stack a YAML level in MSVC Release) runs only
on a tree at most kMaxSchemaDepth (128, loader.h) levels of YAML deep, ten times the deepest
shipped document's 13. An iterative walk (deeper_than) looks first. A deeper document, library,
fragment or include: expansion is not checked and gets one SchemaIssue at its first value past the
limit, saying so. A load goes on, as for any schema issue; ari validate fails the document.
Chosen over refusing a fragment by the nesting levels left: that would leave the main document's
check at 1.25-1.5 MiB, and YAML depth is not widget depth.

parse_scene_node builds in place. Its keys and its repeat: are in never-inlined helpers
(scene_node_keys, scene_node_repeat), and expand_use is never inlined. Its frame drops from
8,760 to 568 B (MSVC) and from 2,048 to 176 B (wasm32). A scene node is a level of the Nesting
count, and so is a use: of a prototype. Scenes are parsed after the widgets, so a scene has all
256 levels. Through use:s a scene could otherwise nest without end with its YAML a few levels
deep. Past the limit the load fails at the node (in a prototype's instance, where the node is
written in the prototype), with the use:s on the way counted. load_node's two nesting failures
share fail_nesting.

Measured, MSVC Release, the least stack each loads on (Windows rounds to 64 KiB):
- a scene nested to the limit through use:s: 2,176-2,240 KiB (past 2 MiB) -> 128-192 KiB
- a scene written inline as deep as yaml-cpp reads: 2,112-2,176 -> 384-448 KiB
- a document as deep as yaml-cpp reads: 1,280-1,344 -> 384-448 KiB
- a fragment that deep loaded at level 254: 1,536-1,600 -> 640-704 KiB

Tests: ariadne_deep_nesting_test loads each worst case on 1 MiB (half of 2 MiB; 8 MiB in Debug),
with new probes for scenes. ariadne_loader_test covers a scene at 256 and 257 levels (placed in the
prototype) and a scenes: entry. ariadne_schema_test covers a document at kMaxSchemaDepth (checked)
and one level past it (one placed issue, which a load warns). docs/ARIADNE.md 6.7, 14.2 and the
prototypes note are updated.
…04bca2)

The code files are c2421bc's. docs/ARIADNE.md is master's plus c2421bc's
change, merged against #1586's head d0587dc.
@transfix
transfix enabled auto-merge (squash) November 27, 2026 12:35
@transfix
transfix merged commit f15ffe6 into master Nov 27, 2026
30 checks passed
@transfix
transfix deleted the tfx/charming-hugle-d241a4 branch November 27, 2026 18:32
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