feat(libc): reserve the guest arena on macOS #6124
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6124"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by yripper on 2026-10-14T22:50:33Z.
What
The guest arena never came up on macOS, so every guest allocation threw: the arena seeds its used ranges from
/proc/self/mapsand reserves the rest withMAP_FIXED_NOREPLACE, and macOS has neither. This keeps the sameArenaclass and constants and swaps only those two host primitives under__APPLE__:mach_vm_regionwalk fromArenaStarttoApplicationAreaEnd.mach_vm_mapwithVM_FLAGS_FIXED,VM_PROT_NONEand max protectionVM_PROT_ALL, in the same 1 GiB chunks. A fixedmach_vm_mapfails instead of replacing, andKERN_NO_SPACEmaps toEEXIST, so the loop that skips occupied chunks is unchanged.The POSIX commit and protection paths (
PlaceAt,GuestArenaSetProtection) are enabled for__APPLE__as well. On Linux the code is split intoHostMappingsandReserveNoReplacewith the same calls and error mapping.The ranges macOS never lets a process map (0x10_0000_0000–0x70_0000_0000), and the dyld and Rosetta runtime regions inside the application area, are recorded as debt in
docs/dev/debt/libc.md.This is the arena step agreed in #1, so @mugurc's direct-memory branch can be based on it.
Tested
guest_arenatest (core/libs/tests/guest_arena, macOS only), 4 modes:available,unmappable,host,commit. All pass on macOS 26 on an M1 Pro, both as x86-64 under Rosetta 2 and as arm64./proc/self/mapsparsing andmmap(MAP_FIXED_NOREPLACE)handling; Windows is untouched.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T05:28:30Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/tests/GuestArenaHost.cppcore/libs/tests/guest_arena/CMakeLists.txtSome comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libc/src/GuestArena.cppcore/libs/CMakeLists.txtcore/libs/prx/libc/src/GuestArena.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/CMakeLists.txtcore/libs/CMakeLists.txtcore/libs/prx/libc/src/GuestArena.cppcore/libs/tests/GuestArenaHost.cppcore/libs/tests/guest_arena/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/src/GuestArena.cppcore/libs/CMakeLists.txtcore/libs/prx/libc/src/GuestArena.cppdocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T23:36:32Z.
Checked the Linux side:
HostMappings/ReserveNoReplacekeep the same/proc/self/mapsparsing,MAP_FIXED_NOREPLACEcall and error mapping; build and the libs ctest scope are green on the PR merged onto main. The macOS path (mach_vm_regionwalk, fixedmach_vm_map, theguest_arenatests) can't be run here, so that part rests on your M1 Pro runs; the full Build will compile it on the macOS job. Debt line is indocs/dev/debt/libc.md. Merging.Imported from boykopovar/AnyPS5. Originally posted by xsyetopz on 2026-10-17T00:43:45Z.
@yripper I have an Intel Mac running i7-4820HQ and macOS Monterey (v12). If you could set up the things I need to do to test it on an Intel Mac, do so here. Thanks!
Imported from boykopovar/AnyPS5. Originally posted by Alexius0815 on 2026-10-17T06:44:42Z.
Tested #6124 (head
913adca3a) on an Apple M4 (16 GB), macOS 27.2 (26B5112a). Standalone guest_arena project, Release, built per architecture: available, unmappable, host (12 / 33 protected host regions), commit all pass, both as x86-64 under Rosetta 2 and native arm64; ctest 4/4. So the arena also comes up on macOS 27 and M4.ccleste (PPSA59322, catalog 1.00, sha256 checked, no sce_module) on mugurc/ci/macos-series-2 + #6124 + #6448 + #6580 (without it libc does not compile with the macOS SDK) + #6928 + #6668 + #6667 + #6235, with the three DirectMemory.cpp arena guards widened to APPLE as @DavidSamir described: it creates the MoltenVK 1.4.2 device and swapchain, opens audio, loads its sounds and songs and dispatches compute work, then stops at a pixel shader that reads barycentrics without VK_KHR_fragment_shader_barycentric needs a vertex shader before it. The draw is a rect list whose pixel shader reads raw per-vertex parameters, so there is no path for it on macOS (no geometry shaders, barycentric extension disabled on MoltenVK).
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-18T14:08:09Z.
Tested on an Apple M4 Pro (MacBook Pro, 24 GB), macOS 27.0.1 (26A434), Rosetta 2, Apple clang 21.0.0, CMake 4.4.4, Ninja 1.13.2.
PR head
913adca3amerged into currentmain(385ed71f2), standalonecore/libs/tests/guest_arenaproject, Release and Debug, built per architecture:--repeat until-fail:20on both Release builds: no failures.Address space on this host. A separate program that walks
mach_vm_regionfrom 0x2_0000_0000 to 0xFC_0000_0000 at startup, six launches per architecture:MAP_FIXEDmapping at 0xF_BFFF_0000 succeeds and one at 0xF_C000_0000 fails withEACCES; 0x6F_FFFF_0000 fails and 0x70_0000_0000 succeeds. The extra GiB lies insideSystemReservedStart-SystemReservedEnd, so placement is unaffected; only the range in the debt line could start at 0xF_C000_0000.What the tests do not cover. With the
mach_vm_regionwalk disabled (HostMappingsstarting atApplicationAreaEnd, so it returns no regions), all four tests still pass on both architectures:TryReserverecords every 1 GiB chunk it cannot reserve as occupied, so host regions are still refused, at chunk granularity. The walk makes a difference for free space next to a host region. A check linked against this PR'sGuestArena.cpp, with the host map captured in a static initializer that runs beforeg_reserved:overlaps a host region in the guest arena)Native arm64 gives the same pattern with a host region at 0x74_AC00_0000 and a free gap at 0x74_8000_0000. Without the walk, the guest loses the whole GiB around dyld and the one around the Rosetta runtime. A test case like this one would keep the walk from regressing unnoticed.
Check source
Built with
xcrun clang++ -std=c++20 -O1 -arch x86_64 -I core/libs -o gap arena_gap.cpp core/libs/prx/libc/src/GuestArena.cpp core/libs/prx/libc/src/GuestWriteWatch.cpp(this file first, so its initializer runs before the arena's), and with-arch arm64.Not checked: Linux and Windows, the full macOS libc build (it does not link on
mainyet) and any title.AI-assisted: yes (Claude Code). I ran all of the above on my machine.