From cd0700a8b01d90d3b66005d90cc111f30131f9a8 Mon Sep 17 00:00:00 2001 From: Oliver Ames Date: Tue, 3 Nov 2026 12:25:16 -0500 Subject: [PATCH] hle_svc: cap the application pool's answers at launch (DM_APP_POOL) for 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= 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. --- dm/host/hle_svc.c | 20 +++++++++++++++++++- docs/env_switches.tsv | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/dm/host/hle_svc.c b/dm/host/hle_svc.c index e87442e27d..e3f05d87f1 100644 --- a/dm/host/hle_svc.c +++ b/dm/host/hle_svc.c @@ -349,7 +349,25 @@ static void mem_init(void) { // and hands the whole block to its own StandardAllocator (main+0x32137a0) -- so heap size = pool − image footprint, it has only as much as the host reports. // Previously 3 GB was reported here and SetHeapSize never failed: the heap was about 150 MB smaller than on the real device, at the run entry peak the main heap had only about 18 MB left, and under high local load a timing shift made a 128 KB request return null, // which the guest writes to without a null check (CRASH main+0x1b36138, re-verified after merge with inv_tap). -#define APP_POOL DM_GAME_APP_POOL // game profile app_pool (Mario 0xCD500000) +// The application pool: the game profile's app_pool, capped at launch by DM_APP_POOL (bytes; smaller of the two wins). +// The reason to cap it: an iPhone gives a process ~8 GiB resident (jetsam per-process-limit, memstat: phys_footprint + os_proc_available_memory = 8192 MiB), less than what a +// Switch 2's application pool holds; engines that plan their memory from the pool's size adapt (set DM_APP_POOL to what the phone can charge and watch the game's own boot decisions). +// A game that needs the whole profile pool aborts on a smaller answer instead: the memstat log says which it did. +static uint64_t g_app_pool; static int g_app_pool_set; +static uint64_t app_pool(void) { + if (!g_app_pool_set) { + g_app_pool_set = 1; + uint64_t v = DM_GAME_APP_POOL; + const char *e = getenv("DM_APP_POOL"); + if (e && *e) { + uint64_t p = strtoull(e, NULL, 0); + if (p && p < v) { v = p; LOG("[mem] DM_APP_POOL caps the application pool at %#llx (game profile app_pool %#llx)", (unsigned long long)p, (unsigned long long)DM_GAME_APP_POOL); } + } + g_app_pool = v; + } + return g_app_pool; +} +#define APP_POOL app_pool() static size_t image_bytes(void) { size_t n = 0; for (int i = 0; i < g_nmod; i++) if (!g_mod[i].nro) n += g_mod[i].size; // an NRO lives in memory the game allocated (its image and bss buffers) return (n + 0x1fffff) & ~(size_t)0x1fffff; } // Giant bss (modules with the patch area before the base, patch_nso.py FAR): the image bytes beyond the file part. Used memory was reported as "heap + 64 MB", where the 64 MB covers ordinary modules' code and data (Mario's two modules are about 74 MB together, diff --git a/docs/env_switches.tsv b/docs/env_switches.tsv index 5c2b71baa2..84512398c3 100644 --- a/docs/env_switches.tsv +++ b/docs/env_switches.tsv @@ -6,6 +6,7 @@ DM_ALLOW_OTHER_METAL switch `1` lets `own_compilers.py` build the shader librari DM_AOC_DIR path Folder of add-on content: one numbered subfolder per DLC index holding that DLC's RomFS, reported by `aoc` and mounted read-only; the app, the Mac launcher and the NSP script set it when the build has DLC DM_AOC_OWNED switch `0` reports no add-on content even when `DM_AOC_DIR` has some, for runs without the DLC DM_APP_DM path Set by the iOS app of a game of several programs: the running program's `dm/p` folder, where the settings screen finds its `launcher.json` +DM_APP_POOL switch `=` (decimal or `0x` hex) caps the application pool the host reports at launch, the smaller of it and the profile's `app_pool`: GetInfo's pool total, the SetHeapSize limit and the MapPhysicalMemory limit (`app_pool` in `hle_svc.c`). For a Switch 2 game whose pool is larger than a phone gives one process; a cap that takes effect is logged once as `[mem] DM_APP_POOL caps the application pool at ...`. Unset, 0 or a value not below the profile's leaves the profile's pool DM_APP_VERSION switch Overrides the IPA's version in `build_ipa.sh`; by default it is the packed game's display version from its control data (the update's when one is packed) DM_AUDIO switch `1` plays audio through Core Audio, `0` uses the silent sink; real output is the default on iOS, the silent sink on the Mac (the Mac launcher sets `1`) DM_AUDIO_DUMP diagnostic `=` writes every PCM block the game appends as is (16-bit, 48 kHz, channel count as in the OpenAudioOut log), to judge whether a sound played