Application memory accounting

Index

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.

Implemented behavior

These are the single-application cases relevant to this crash. This is not a claim that every memory SVC now reproduces the kernel.

Remaining HLE differences

References

All paths below are under libraries/libmesosphere in the referenced Atmosphere commit:

Validation

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.