Skip to content

Ariadne loader: about 1 KB of stack a level, and a document nests at most 256 levels - #1586

Merged
transfix merged 1 commit into
masterfrom
tfx/focused-diffie-d62ba4
Nov 27, 2026
Merged

transfix merged 1 commit into
masterfrom
tfx/focused-diffie-d62ba4

Conversation

@transfix

Copy link
Copy Markdown
Owner

The .ari loader ran out of native stack at shallow nesting: parse_widget_impl had a 95 KB frame, and it runs once a level. This PR makes a level cheap, then caps nesting at 256 levels with an error that says where. The per-frame walk was made stackless in #1577; after this PR the loader's limit is the only depth limit a document has.

The frame, before and after

MSVC 14.44 Release x64 (/FAs listing of loader.cpp), in bytes:

function (on the way down) master this PR
parse_widget_impl 95,656 248
expand_load 11,384 584
parse_document 6,264 344
parse_seq 3,432 568
expand_include 3,384 296
parse_widget 376 72

MSVC gave each by-value Widget temporary in the per-kind branches (text(), separator(), spinner(), ...) a slot of its own, and a Widget is about 3.3 KB. Now:

  • Each branch is a never-inlined function that builds its widget in place (Widget &w).
  • The kinds that hold widgets (menubar, menu, tray icon, window/overlay, tabs, custom, group) keep only YAML nodes in their frames on the way down. Their other keys are read in helpers.
  • parse_widget, parse_seq, expand_include, expand_load and parse_document build in place too.
  • The leaf dispatcher (parse_leaf, 2,984 B) and finish_mount (2,344 B) are never on the way down.

wasm32 (em++ -O2, loader.cpp compiled alone, shadow stack read from each prologue): parse_widget_impl 3,184 → inlined into parse_widget (48), expand_load 3,696 → 272, parse_seq 1,472 → 144, parse_document 1,456 → 80. That is about 0.2–0.3 KB a level there.

Depth per stack, before and after

Bisected with ariadne_deep_nesting_test's probes (MSVC Release; the "after" include column with the limit lifted for the measurement):

stack inline groups, master inline, this PR include: levels, master include:, this PR
256 KiB 0 (a) 37 0 (a) 114
512 KiB 2 87 1 266
2 MiB 18 246 9 1,177
8 MiB 81 246 40 > 4,000

(a) From the walk session's measurement on 327a791; the rest measured here on 93c9da4.

An include: level is two levels of the count. So the parse alone goes about 2,350 levels deep on 2 MiB, and the 256 limit is about 9× under that. Inline nesting stops at 246 on 2 MiB and on 8 MiB: that is yaml-cpp's own parse cap (about 500 levels of YAML, two a widget). The inline numbers are dominated by the schema check (see "What the limit does not bound").

The limit

kMaxNesting = 256 (loader.h).

  • What counts: each widget is a level, and so is each include: and load: entry. The unit's or fragment's widgets go on below the entry. The count runs across every unit and mounted fragment: a Nesting state on the Ctx, shared with a load:'s sub-Ctx the way mount_count is. It is per load, never a static.
  • What happens past it: the first widget past the limit stops the parse, and the load fails (ok false, no tree). Its error is at that widget's place; in an include: expansion, that is the same place in the unit's template, found by the key/index path. The error counts the include:s and load:s on the way down, and names each one that goes into another file:
ari: lib.ari:510:9: a widget nested deeper than 256 levels, the most a document may (each widget, include: and load: a level; docs/ARIADNE.md §6.7), with 127 include: and 1 load: on the way, by way of main.ari:5:9 load: frag.ari, frag.ari:4:5 include: level0 (in lib.ari)
  • New fields: LoadResult::error_source, error_line and error_column hold the place.
  • ari validate: reports the error there, compiler-style (deep.ari:514:9: error: a widget nested deeper than 256 levels ...). --json adds load_error_source, load_error_line and load_error_column. A JSON schema cannot count across files, so ari.v1.schema.json is unchanged.
  • Shipped documents: the deepest of the 202 tracked .ari in libcvc nests 11 levels (the volume_grid family). GRL-SNAM's documents nest 8, SH1's shell.ari 4. The 40-deep include test is 82.

any_descendant_repeats and refs_from_widget are loops now.

What the limit does not bound (findings, not changed here)

  • The schema check: for deep inline YAML it is the largest stack user, about 2.8 KB a YAML level (5.7 KB an inline widget level), all inside json-schema-validator. yaml-cpp's 500-level cap bounds it:
    • a 246-deep inline document needs 1.25–1.5 MiB to load;
    • the worst stacking found needs 1.5–1.75 MiB. That is a chain to level 254 whose last unit load:s a 246-deep inline fragment, which is checked before its parse is refused (DISABLED_ProbeLoadDeepFragmentAtTheLimit).
    • Both fit 2 MiB on MSVC, with thin headroom there.
  • parse_scene_node: it recurses a scene level at a time with an 8.7 KB MSVC frame (2 KB wasm), bounded only by yaml-cpp (about 245 scene levels, about 2.1 MB on MSVC). The limit doesn't count scene nodes.

Tests

  • ariadne_loader_test: NestingLimitCountsPlainChildren, NestingLimitCountsIncludes and NestingLimitCountsLoads. Each has a document at 256 that loads and one at 257 that fails, with its place checked (line and column, the files on the way).
  • ariadne_deep_nesting_test: a document nested to the limit through include:s, and one through load:s, each loads and draws on a 2 MiB stack (8 MiB in a Debug build, whose frames are several times larger); one past the limit fails there. There are new probes for the schema check alone, yaml-cpp's parse alone, and a deep fragment loaded at the limit.
  • ari_validate_too_deep (ctest): a generated 128-unit document, reported at validate_too_deep.ari:514:9.

Run locally (Windows, MSVC Release, with the validator and FTXUI): ariadne_loader_test 277, ariadne_schema_test 13 + 1 skipped, ariadne_host_test 69, ariadne_deep_nesting_test 13 + 1 skipped (tvision), ariadne_runtime_test 368, and the ari_* ctests (11, ari_validate_too_deep among them). Not run here: Linux, macOS, wasm (CI).

Docs

docs/ARIADNE.md has a new §6.7 "How deep a document nests". §6.1 and §6.3 point to it, and §14.3 covers the placed load error in ari validate.

Conflicts expected

…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.
@transfix
transfix enabled auto-merge (squash) November 27, 2026 07:29
@transfix
transfix merged commit 104bca2 into master Nov 27, 2026
30 checks passed
@transfix
transfix deleted the tfx/focused-diffie-d62ba4 branch November 27, 2026 12:28
transfix added a commit that referenced this pull request Nov 27, 2026
…04bca2)

The code files are c2421bc's. docs/ARIADNE.md is master's plus c2421bc's
change, merged against #1586's head d0587dc.
transfix added a commit that referenced this pull request Nov 27, 2026
…ounts-fb75fd

#1586 builds each widget in place and splits expand_load into read_fragment, open_mount and
finish_mount. SH1's LoadOutcome (why a fragment was not mounted, its canonical path, whether it has
a scene, and the min_libcvc gate a run-time mount keeps) moves into read_fragment, and expand_load
passes it through. parse_mounts builds its slot in place like the other widgets.

load_mount gives its context a Nesting, as load_node does, and refuses a document nested past
kMaxNesting with that error rather than mounting it cut short: a mounts: entry's document nests as
a document of its own does. New AriadneLoader.LoadMountRefusesADocumentNestedPastTheLimit.

ARIADNE.md: #1586's "How deep a document nests" keeps 6.7, which its error message cites, and
mounts: is now 6.8; SH1's references to it follow. The deep nesting tests keep both sides.
transfix added a commit that referenced this pull request Nov 27, 2026
…bare name

- close_mount cleared the mount's principal right after quit_app, before
  the children that share its scope were killed, and the Runtime's teardown
  before the group's own teardown reaped what was left. A program built in
  between would have run as the scope's parent, local in a catalog shell.
  Both now clear it once the mount's processes are gone.
- mount_principal gave "file:" (no directory) for a mount whose name has no
  directory and whose loader gives no canonical path, and SH11's store
  refuses an empty file: principal, so open_mount refused every such mount
  (AriadneDeepNesting.AMountsSlotsEntriesDraw, from #1586). Such a name is in
  the current directory: file:.
transfix added a commit that referenced this pull request Nov 28, 2026
…rincipal (#1576)

* Ariadne shell SH11: the network, credentials, keys and sign-ins per principal

A program's verbs are bound to the network of the principal its scope runs as
(docs/roadmap/ARIADNE_SHELL.md 4.12), so a document mounted beside nicotina cannot
post as the reader or reach the origins nicotina may.

- principal.h: a table per app of scope to principal (local, file:<dir>,
  remote:<origin>), the innermost recorded scope deciding and a local one inside a
  sandboxed one unable to raise it; hosts may check a principal as it is recorded.
- CredentialStore: one store and file, viewed per principal (for_principal); each
  view has its own entries, bindings and renewers. File lines are
  "<principal> <name> <origin> <token>"; three-word lines load as local.
- enable_net: each program's http verbs, fetch, http-upload, ws-*, net-*,
  open-url and oidc-* use net_host_access(app, scope): the launcher's for local,
  its own origin and granted origins for remote:, granted origins only for file:.
  A local application may have its own policy, credential store and e2ee store.
- WebSocket handles and OIDC sign-ins are their owner's; a sandboxed principal's
  sign-ins are not posted to the global oidc# channel.
- e2ee: a sandboxed principal gets its own in-memory key store.
- Pictures, video and Save with auth: are fetched as the widget's principal.
- Tests: ariadne_net_principal_test (credential isolation, allowlists, the file,
  nicotina's own policy and store against a local stand-in, WebSockets, sign-ins,
  e2ee, pictures) and the store's own in net_policy_test.

* SH11: scope last in ImageRequest, PlayerSource and MediaSave

The new scope member sat between auth and w in ImageRequest, so the
positional initializers in ariadne_media_test.cpp (uri, auth, w, h, fit,
shape) no longer compiled, and every package job failed on it. Each
struct now ends with scope, so an initializer written before SH11 still
lines up and leaves scope empty (the launcher's).

* SH11 on SH1's mounts: each mount runs as its principal

- open_mount records the mount's principal (SH1's mount_principal, with the
  entry's net_policy, credential_store and e2ee_store) under its scope before any
  of its programs is built; a check that fails refuses the mount. close_mount and
  the Runtime's teardown clear it.
- shell.ari: a catalog entry's net_policy, credential_store and e2ee_store
  (relative to the catalog) are kept with its row; such an entry opens under its
  slug with them written into shell.apps.<slug> first.
- Tests: a catalog mount as local and another file as file:<dir> (credentials and
  allowlist), a local application's own network from its entry and the refusals,
  and shell.ari's catalog entry with its own network.
- ARIADNE.md 6.7 lists the entry's keys; ARIADNE_SHELL.md's SH11 note says where
  the principal is recorded.

* SH11 on the merged SH1: clear a mount's principal last; file:. for a bare name

- close_mount cleared the mount's principal right after quit_app, before
  the children that share its scope were killed, and the Runtime's teardown
  before the group's own teardown reaped what was left. A program built in
  between would have run as the scope's parent, local in a catalog shell.
  Both now clear it once the mount's processes are gone.
- mount_principal gave "file:" (no directory) for a mount whose name has no
  directory and whose loader gives no canonical path, and SH11's store
  refuses an empty file: principal, so open_mount refused every such mount
  (AriadneDeepNesting.AMountsSlotsEntriesDraw, from #1586). Such a name is in
  the current directory: file:.

* SH11: compare the shell test's store path canonically

AriadneComponents.ShellGivesACatalogEntryItsOwnNetwork compared the
credential store's path with temp_directory_path() / ... after
lexically_normal. On the windows-2022 runner the temp directory comes
back in its 8.3 spelling (C:/Users/RUNNER~1/...), while the store's
path is resolved to long names, so the same file compared unequal.
Both sides are now weakly_canonical. Reproduced locally with TMP set to
a short-name folder: the old assertion fails, the new one passes.
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