Skip to content

Ocarina of Time: a full model heap no longer crashes the model rebuild (from HookLink) - #2497

Closed
oliverames wants to merge 1 commit into
seedds:mainfrom
oliverames:hooklink-heap-retry
Closed

oliverames wants to merge 1 commit into
seedds:mainfrom
oliverames:hooklink-heap-retry

Conversation

@oliverames

@oliverames oliverames commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What and why

HookLink's iPhone build of Ocarina of Time crashed about 11 minutes in: BufferInitialize: pool has no storage, then h_nvnMemoryPoolGetFlags on a null pool, with the backtrace at main+0x436fb4.

The game's OptimizeChangeBank thread rebuilds a model's material block (main+0x4366e0) with memory from small per-model heaps, and it checks none of its four allocations: two CPU buffers from the heap's tryAlloc (returning to main+0x436910 and +0x436da4) and two blocks of GPU memory from main+0x502050 (returning to +0x436f1c and +0x437098). When the heap is full, the game goes on with a null block. Hookshot found the same crash on PCs (NotRexed/Hookshot issue 1697), and its notes give these sites and the sead heap layout (the parent at +0x38).

patches.py heap_retry adds stubs after each of the four calls. A failed allocation is tried again in the heap's parents (at most 16 for the CPU buffers), and the GPU memory last in the game's default heap; then the game goes on where it would have. State page +0x170 and +0x174 count the blocks made elsewhere, and the power line logs full model heap when they change. DM_HEAP_RETRY=0 (+0x17c) leaves the game as shipped. It applies to v131072 only.

Ported from HookLink's commit cc6e6b47 on the hooklink branch of dsi-hub/nxnative (MIT), with dsi-hub as co-author; the stubs are theirs. On main the call sits after dynamic_resolution, since main's frame_step has none of HookLink's wide-view or half-rate hooks. The game note gains a line under Memory. Offsets 0x170 to 0x17c on the state page are unused on main (the dynamic-resolution stub uses +0x40 and +0x200 onward; loader.c writes up to +0x9c).

Checked

  • Rebased onto main bee218d97 on 2026-11-14 (head 581148a0), with no merge commit; only the game's README conflicted. On that tree test_patches.py (22), test_env_switches.py and test_docs.py pass, docs_patches.py leaves no diff, and lint_c.py tidy has nothing over the baseline. I couldn't run patch_nso.py on the game itself for this head, because this Mac only has the decompressed main.orig.bin, so I checked the four patch-site words against that image instead. Upstream CI is queued behind a long backlog.
  • Tests: dm/tools/patch_nso.py on this game's v131072 main patches all four sites (they're in main.changed.tsv), after checking each site's original word. make -C dm/host GAME=0400486024DEC000 builds; GAME=0400486024DEC000 python3 dm/ios/tools/compile_check.py reports 263 files, 0 failed; test_patches.py, test_env_switches.py and test_docs.py pass.
  • Games run: none. I haven't reproduced the crash on main, and HookLink hadn't run this on a device when it shipped in 1.8.5. There's no test that executes the stubs.

Docs

  • The finding is in the game's note, and python3 dm/tools/docs_symptoms.py was run
  • No chain tool changed
  • No game files, keys or data extracted from a game in the change or the attachments
  • docs/env_switches.tsv has a row for DM_HEAP_RETRY.

@github-actions github-actions Bot added host The Horizon host: SVCs, services, threads, memory (dm/host) nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) rewriter The NSO rewriter and per-build patches docs Documentation game: 0400486024DEC000 A game labels Nov 12, 2026
@oliverames
oliverames marked this pull request as ready for review November 12, 2026 18:46
… rebuild: its CPU buffers and GPU memory are tried again in the heap's parents (DM_HEAP_RETRY, on by default)

Symptom: Ocarina of Time crashed about 11 minutes in on an iPhone (HookLink's test build): "BufferInitialize: pool has no storage", then h_nvnMemoryPoolGetFlags on a null pool, backtrace main+0x436fb4.

Cause: the game's OptimizeChangeBank thread rebuilds a model's material block (main+0x4366e0) with memory from small per-model heaps and checks none of its four allocations: two CPU buffers from the heap's tryAlloc (lr main+0x436910, +0x436da4) and two blocks of GPU memory from main+0x502050 (lr main+0x436f1c, +0x437098). When the heap is full the game goes on with a null block. Hookshot found the same on PCs (NotRexed/Hookshot issue 1697); its notes give these sites and the sead heap layout (+0x38 the parent).

Fix: patches.py heap_retry, stubs after the game's call: a failure is tried again in the heap's parents (at most 16 for the CPU buffers), the GPU memory last in the game's default heap, then the game goes on where it would have. State page +0x170 / +0x174 count the blocks made elsewhere and the power line logs them when they change; DM_HEAP_RETRY=0 (+0x17c) leaves the game as shipped. v131072 only.

Checked: patch_nso.py on this game's v131072 main patches all four sites (main.changed.tsv) after checking each one's original word; host build; iOS compile check 263 files, 0 failed; test_patches, test_env_switches, test_docs pass. Not run in the game here, and HookLink had not run it on a device when it shipped in 1.8.5.

Ported from HookLink's commit cc6e6b4 (https://github.com/dsi-hub/nxnative/tree/hooklink, MIT); the stubs are theirs. Changes from it: the call sits after dynamic_resolution (main's frame_step has no wide view or half-rate hooks), and a game note.

Co-authored-by: dsi-hub <260802842+dsi-hub@users.noreply.github.com>
@gnavarro24

Copy link
Copy Markdown
Contributor

Hi! This branch is based on a main from before commit c70c242 ("ci: repair host and tool test failures on main"). Most of the failing checks here (Host unit tests, Tool tests, clang-tidy, Build check) come from that older base, not from this change.

To pick up the fix, merge the current main into this branch and push:

git fetch origin
git merge origin/main
git push

If the merge has conflicts, ask here if you want help with them.

@seedds

seedds commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @oliverames for porting this over (and thanks to @dsi-hub for the original work). I'm closing this without merging for now:

  • Root cause is still unknown on current main. The game doesn't null-check these allocations because on Switch 2 hardware the model's own heap is expected to have enough room. Most reports of this crash predate the general NVN command-memory compaction changes on main, and the crash hasn't been reproduced on plain current main yet. Falling back to parent heaps would hide why the model's heap is running out instead of fixing the underlying sizing issue.
  • Scope and implementation issues. The patch is tied to fixed offsets for one game build, adds game-specific environment switch and logging code to generic host files, and the four stubs haven't been executed in a test or game run yet. Reading the stub code also turned up a few edge-case bugs (a missing null check on the CPU heap pointer before dereferencing its parent, a retry cap of 15 instead of 16, and non-atomic counter increments).

What was checked: Reviewed the PR diff against Ocarina of Time v131072 main (the model setup, GPU allocator, model destructor, and heap layout). Allocation ownership itself checks out — both the CPU buffers and GPU blocks are freed back to whichever heap allocated them when the model is destroyed — and the target functions match uniquely by fingerprint if a game-level fallback were ever proven necessary.

Still open: The crash remains tracked in #1445. The next step is reproducing it on plain current main with the failure probes listed there so we can see which model heap runs out and what filled it.

@seedds seedds closed this Nov 24, 2026
@oliverames
oliverames deleted the hooklink-heap-retry branch November 28, 2026 01:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation game: 0400486024DEC000 A game host The Horizon host: SVCs, services, threads, memory (dm/host) nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) rewriter The NSO rewriter and per-build patches

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants