This change targets the heap rejection that one game met at startup. A
3200 MiB request was refused because the runtime charged a fixed 256 MiB in
addition to the requested heap against a 3285 MiB application budget. The game
subsequently initialized an allocator with a bad buffer. Increasing memory_gb
avoided that path, but did not correct the accounting.
A 32-bit title then hit a second startup failure. It read the total from GetInfo
6 (3285 MiB), subtracted a small reserve, and asked SetHeapSize for 3260 MiB.
That is larger than a 32-bit process's heap region, so the kernel refused it and
the SDK's heap manager stayed uninitialized. Mesosphere caps GetInfo's total at
the process's heap region (m_max_process_memory), not at the application pool:
2 GiB for a 32-bit process without an alias region, 1 GiB with one. GetInfo 6 and
21 now do the same, and the title asks for 2024 MiB, which fits.
The memory behavior below was checked against Atmosphere's Mesosphere sources at
commit c7e6c35e900b52c364d557bdbfe554c857158d97. This is an independently written
implementation in ScarletNX, not a port of Atmosphere's GPL-2.0-only code.
memory_gb=12 setting gives a 10 GiB application budget and extends a 39-bit process's heap
region to 12 GiB when the host has at least 32 GiB of virtual address space for the guest. This setting is for
diagnosis of newer applications. It does not implement their remote-memory or graphics services. An Ounce game
(its NPDM, docs/platforms/nx/ounce.md) gets the same heap region without the setting, and the console's own
9152 MiB application pool.memory_fit_mode=0 (Auto, the default), the pool the game is told is at most what
the host can still give the process as the game starts, plus what the process
holds, less what the rest takes (the runtime then and 256 MiB for the graphics,
700 MiB at least), never under 1 GiB (mem_fit_budget), and never under the
console's own pool, an Ounce game's included (mem_fit): a host with less than
the console's runs as with Off. Every GetInfo above and the heap's limit follow it.heap_max_mb
checks separate from resource-limit failures.m_max_process_memory does: a 32-bit process sees 2 GiB without an alias
region and 1 GiB with one, however large the application pool is. A 64-bit
process sees its layout's heap size, or the pool when that is smaller. GetInfo
7/22 still report what is actually used.These are the single-application cases relevant to this crash. This is not a claim that every memory SVC now reproduces the kernel.
All paths below are under libraries/libmesosphere in the referenced Atmosphere
commit:
source/kern_k_process.cpp: GetUsedUserPhysicalMemorySize,
GetTotalUserPhysicalMemorySize, their non-system variants, and Run.include/mesosphere/kern_k_page_table_base.hpp: GetNormalMemorySize.source/kern_k_page_table_base.cpp: SetHeapSize, MapPhysicalMemory and
UnmapPhysicalMemory.source/svc/kern_svc_physical_memory.cpp: request validation and the private
system-resource prerequisite.source/kern_k_system_resource.cpp and
source/board/nintendo/nx/kern_k_system_control.cpp: private application
system-resource reservations and the application-pool sizes.tests/memory_accounting.c links the actual mem.c and svc_mem.c. Its POSIX VM
backend reserves the guest address space sparsely, so the large-heap tests do not
need gigabytes of resident RAM. It injects commit failures and stubs unrelated
thread, handle, clock and GPU services. No game binaries or extracted data are
needed.
The suite checks the 3200 MiB startup request with synthetic 55 MiB images and a 1 MiB main stack, boundary sizes, GetInfo memory values, private system-resource charges, locked-page shrink rejection, repeated and overlapping physical maps, partial-failure rollback, incompatible unmaps, and 50 two-thread races for the last 2 MiB of the shared heap/physical budget. It also switches to both 32-bit layouts and checks that GetInfo 6/21 name the heap region (2 GiB without an alias region, 1 GiB with one), that the application pool's total is still refused, and that a request just inside the region succeeds.
Run it through the normal CMake/CTest configuration on a Unix host, or without the runtime's third-party dependencies from the repository root:
cc -std=gnu23 -O2 -Wall -Wextra -Werror -Iruntime \
tests/memory_accounting.c src/nx/memory/mem.c src/nx/hle/svc/svc_mem.c \
-pthread -o /tmp/snx-test-memory
/tmp/snx-test-memory
This standalone test was run with GCC 14.2, including ASan/UBSan. The project's
production build still requires Clang. The 32-bit fix was also checked with a
full runtime build and a headless run of the affected title at memory_gb=0: the
heap request changed from a refused 3260 MiB to a successful 2024 MiB, and the run
continued past the old crash for a minute of rendering with no failure logged.
Game verification should still include repeated startup at memory_gb=0 or 4,
followed by normal gameplay. Passing this suite alone does not prove that every
downstream guest allocator path is fixed.
Made from docs/platforms/nx/memory-accounting.md at 69237645142c. GPL-3.0-or-later.