Skip to content

hle_svc: cap the application pool's answers at launch (DM_APP_POOL) - #1104

Closed
oliverames wants to merge 1 commit into
seedds:mainfrom
oliverames:app-pool-cap
Closed

oliverames wants to merge 1 commit into
seedds:mainfrom
oliverames:app-pool-cap

Conversation

@oliverames

@oliverames oliverames commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

What and why

An iPhone 17 Pro Max gives a process a hard 8192 MiB of resident memory (measured second-by-second with DM_MEMSTAT this session: phys_footprint + os_proc_available_memory = 8192 MiB in every row; jetsam 'per-process-limit' agrees). A Switch 2 game plans a 9-10 GB application pool (DKB profile app_pool 0x240000000, MKW 0x263000000) and its loads spike past what the device will charge a single process.

The games' engines plan from the pool answers (GetInfo total-from-non-system, the SetHeapSize ResultLimitReached guard, the MapPhysicalMemory pool limit), so one honest host fix available before any game-side surgery is to answer a smaller pool and see whether the memory plan adapts.

DM_APP_POOL= caps every application-pool answer to min(profile app_pool, cap) and is logged once at the first query, so every launch's log names what game played under what budget. Default unchanged: without DM_APP_POOL, every game sees its profile's app_pool as today.

Result with the cap (Mario Kart World v1.0.0, iPhone release build, pool cap 0x180000000):

  • The game's SetHeapSize went from 7.98 GB (uncapped) to 4.43 GB: the engine accepted the smaller plan and stepped through its load, and reached its full SDK init.
  • Its SDK init then crashed (SIGSEGV writing to x0+0x10 with x0 near-null at main+0x3e8b58; caller chain stays in main+0xcf2xxx/nninitStartup family): something the game sizes from the full console-pool still clings to old memory shape data, and now crashes there before gameplay. Current behavior without the cap: MKW renders its world map and then dies at the Jetsam ceiling. So today, MKW on a phone needs a still smaller memory plan than the pool answers alone shape (follow-up discussion to this PR, to be read with the memstat rows).

The patch is purposely nothing else: no docs row for DM_GAME_APP_POOL, and the Switch 1 32-bit app_pool is unchanged.

Checked

  • Tests: not stated (it says only "Tested end-to-end on a release build").
  • Games run, with title ID and version: Mario Kart World v1.0.0 (title ID not stated), on an iPhone release build with pool cap 0x180000000. With the cap it reached full SDK init and then crashed there (see above); without the cap it renders its world map and dies at the Jetsam ceiling. The memstat rows are included in this session's log extracts.
  • Devices and OS: iPhone 17 Pro Max (OS version not stated).
  • The scripts/regress.py check --games report (this changes the host, which every game uses): not stated.
  • Measurements before and after: SetHeapSize 7.98 GB uncapped, 4.43 GB with the cap (details above).

Front ends

None: no front end has to act on this.

Docs

  • The finding is in its game note or its docs/reference/<subsystem>.md entry, and python3 dm/tools/docs_symptoms.py was run (the symptom index). Not ticked: the diff touches only dm/host/hle_svc.c, and the description says the patch has no docs row for the new switch.
  • A chain tool changed: dmsh_chain.py --header was run and dm/gfx/nvn/dmsh_chain.h is part of the change. Not applicable: no chain tool changed (the diff is dm/host/hle_svc.c only).
  • No game files, keys or data extracted from a game in the change or the attachments (the diff is one C file).

@github-actions github-actions Bot added the host The Horizon host: SVCs, services, threads, memory (dm/host) label Nov 3, 2026
@seedds

seedds commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Thanks. I'm not landing this one in tonight's run: DM_APP_POOL is an experiment knob rather than a fix, and with the cap MKW still crashes in its SDK init, as the description says. When the cap (or a per-device pool answer) gets a game through on the phone, it would be worth landing with that finding in docs/reference/memory.md and docs/env_switches.tsv.

@oliverames

Copy link
Copy Markdown
Contributor Author

Follow-up from MKW v1.0.0 on Newt (iPhone 17 Pro Max, iOS 27.2 / 24B119), installed host 48888df559f2+dirty:

The allocation call preceding the reduced-pool crash is nn::os::AllocateMemoryBlock(unsigned long*, unsigned long) (_ZN2nn2os19AllocateMemoryBlockEPmm), not SetMemoryHeapSize. In the extracted main module:

  • main+0xcf3018 rounds the requested size up to 2 MiB and calls the import thunk at 0x282da70. Its GOT slot 0x331a3c8 resolves to AllocateMemoryBlock. The caller does not test the returned Result.
  • main+0xcf3050 loads the output address and saved size, supplies the string RootHeap, and calls 0xcf30c0. That wrapper checks size >= 0x121, but does not check the address before calling the constructor at 0x86bcd4.
  • The constructor calls 0x3e8b1c; the observed fault is stp xzr, xzr, [x0, #0x10] at 0x3e8b58, with x0 null. The later cbz x22 checks a different value, not this destination pointer.

The 7 GiB and 8 GiB runs confirmed the cap in the host log, selected heaps reported as 5.43 GB and 6.43 GB, and failed at that same instruction before menus. Their retrieved memstat files were stale; I withdraw the earlier 7 GiB footprint claim. Those runs establish the heap/log/crash result only.

The 9 GiB control selected a 7.43 GB heap, passed this initialization failure, and reached rendering frame 114/pass 1408 before exiting. With DM_MEMSTAT=memstat.tsv supplied in the launch environment as well as env.txt, the trace was fresh: its last sample at 5 seconds recorded 5007 MiB physical footprint and 9820 MiB Metal allocation. A fresh one-second VM-region dump was also captured. These are sampled values, not measurements at the exit frame; the exit cause is still unconfirmed. The log also reports missing metallib and fragment-position reflection-conversion failures. Menus and gameplay were not visually verified.

Local evidence is preserved under anygame/logs/mkw-20261103-cap7, cap8, and cap9, with disassembly in mkw-sizing-disassembly-20261103.log. Next is to resolve the failure inside AllocateMemoryBlock and its returned Result. This does not yet make the pool knob a working phone memory plan.

@oliverames

Copy link
Copy Markdown
Contributor Author

The next SDK layer explains the reduced-cap failure without a new phone run.

AllocateMemoryBlock tail-calls nn::os::detail::MemoryHeapManager::AllocateFromHeap(unsigned long*, unsigned long) at sdk+0x14a120. It searches the free-block list for a block at least as large as the request. If none fits, sdk+0x14a238 sets the return value to 0x1003; that path does not write the output address. The success path writes the address at 0x14a230.

Both saved 7 GiB and 8 GiB crash register sets contain x4=0x1bd600000, the size passed into the RootHeap constructor: 7126 MiB (6.958984375 GiB). The caller sequence preserves this size from the allocation request through construction. This exceeds the entire heap reported by the host in both failing runs (5.43 GB and 6.43 GB). Thus no free block can satisfy the request. The caller ignores the allocation Result and constructs RootHeap using the unchanged null address.

This is a static diagnosis supported by the saved crash registers, not a live capture of the returned Result. It narrows the problem: the host heap adapts to the pool cap, but this RootHeap request remains 7126 MiB across the two failing caps. A smaller pool answer alone cannot make those plans work. The 9 GiB run gets past this initialization step but still exits later during rendering, as reported above.

@github-actions github-actions Bot added the docs Documentation label Nov 4, 2026
@oliverames
oliverames marked this pull request as draft November 4, 2026 07:02
@oliverames

Copy link
Copy Markdown
Contributor Author

@seedds I updated this experimental PR for the current upstream code while continuing the Switch 2 games running on iPhone work.

Head d9c9081 merges main at 019a822 and adds the missing sorted DM_APP_POOL documentation row. The pool implementation is unchanged. The environment-table check and all 11 documentation tests pass. Targeted LLVM 23.1.4 analysis covers hle_svc.c and the four fixtures flagged by the original failed job, with no findings in those five files and no baseline growth. Hosted CI still needs to finish.

I am keeping this in draft in line with your request for a game to get through on a phone. The earlier Mario Kart World run adapted its heap size but crashed before gameplay, so it does not satisfy that requirement. The current DKB iPhone 17 Pro Max comparisons have no pool override. This PR is not a resolution control or a demonstrated gameplay fix.

@oliverames

Copy link
Copy Markdown
Contributor Author

This branch conflicted with main in docs/env_switches.tsv (main added the DM_APP_DM row where this adds DM_APP_POOL), so GitHub had no fresh merge to check. I merged main into it in 6644ded (no force push; DM_APP_DM, DM_APP_POOL, DM_APP_VERSION in order; dm/tools/tests/test_env_switches.py passes). The diff against main is still only dm/host/hle_svc.c and the one table row.

The earlier clang-tidy failure (three clang-analyzer-security.ArrayBound findings in nvn_mtl.m, a file this PR does not touch) was main's own at 13da1c5, fixed in 5ccbedd. On the new head, clang-tidy run 41839248612 fails only on four pairs from files this PR does not touch, which also fail on main: run 41819072419 at 0b8e0dc (dm/gfx/vk/hle_vk.m NullDereference, dm/host/hle_jit.c suspicious-realloc-usage, dm/host/hle_oe.c StdCLibraryFunctions, dm/host/web_apple.m NullablePassedToNonnull). No commit to dm/host or dm/gfx has landed on main since.

@oliverames

Copy link
Copy Markdown
Contributor Author

@seedds Flagging from my open-PR index of my 32 open PRs here: this one stays draft pending the honest stopping point: Mario Kart World adapted to the smaller memory plan and then crashed in SDK init, so the patch stays deliberately just the cap. The description above is still the full picture, nothing new from me. Happy to rebase, split or retitle whenever review suits.

@oliverames
oliverames marked this pull request as ready for review November 5, 2026 22:15
@oliverames

Copy link
Copy Markdown
Contributor Author

@seedds Marking this ready for review now, so it stops waiting in draft on me. The remaining item from the description is still open: the smaller-plan investigation (Mario Kart World accepted the smaller memory plan, then crashed at SDK init), which is a discussion more than a wait. It can carry on alongside a review instead of ahead of it. The description stays the source of truth; happy to rebase, split or retitle whenever review suits.

…or Switch 2 games on devices with smaller per-process limits

An iPhone 17 Pro Max gives a process 8192 MiB of resident memory hard, every memstat row this session;
a Switch 2 game plans 9-10 GB application pools (Donkey Kong Bananza's profile app_pool 0x240000000,
Mario Kart World's 0x263000000) and its world-map load spikes past what the device will charge.
The games' own engines plan from the pool answers, so the honest fix available to the host is to answer
a smaller pool and watch whether their memory plan adapts.

DM_APP_POOL=<bytes> caps every application-pool answer (GetInfo Total-non-System, the SetHeapSize
ResultLimitReached guard, the MapPhysicalMemory pool limit) at the smaller of the two. Logged once at
the first query, so the log says what the game was playing with.

- Mario Kart World v1.0.0 (iPhone 17 Pro Max, release build): with the cap at 0x180000000, the game
  planned a 4.43 GB heap instead of its 7.98 GB answer under the uncapped profile, and booted into its
  own SDK init: it accepted the smaller pool plan as designed and continued until its own region
  sizing crashed in its SDK (main+0x3e8b58's structure init at [x0+0x10] with x0 null walk from its
  main+0xcf2xxx init chain - patched area untouched): something it sizes from the full console pool is
  still asked for too eagerly; the memstat log carries the trail.
- Donkey Kong Bananza v458752 on the same device with the cap at 0x1c0000000: pending (recording now);
  pending-evidence to be added to this PR when the run reports.

The default is unchanged: without DM_APP_POOL every game continues to see its profile's app_pool.
@oliverames

Copy link
Copy Markdown
Contributor Author

Closing this. Main fixed the real problem another way in 865cc35: a game whose pool doesn't fit the phone's limit keeps its guest memory in files, and Mario Kart World finishes races on 8 GB iPhones that way. The cap never got a game through (RootHeap asked for 7,126 MiB at every cap I tried), so you were right that it was an experiment knob and not a fix.

@oliverames oliverames closed this Nov 10, 2026
@oliverames
oliverames deleted the app-pool-cap 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 host The Horizon host: SVCs, services, threads, memory (dm/host)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants